trilbymedia / grav-plugin-git-sync

Collaboratively Synchronize your Grav `user` folder hosted on GitHub, BitBucket or GitLab
Apache License 2.0
239 stars 57 forks source link

Fix Git Sync Deleting Files and Folders, Optimize the Initialize Process, Make sparse checkout optional #229

Open hdwebpros opened 11 months ago

hdwebpros commented 11 months ago

Addressing https://github.com/trilbymedia/grav-plugin-git-sync/issues/228 and subsequently https://github.com/trilbymedia/grav-plugin-git-sync/issues/213 and possible https://github.com/trilbymedia/grav-plugin-git-sync/issues/222

Also note that sparse checkout has been flaky at best for the 50+ sites that we have tried it on. We developed a better init process and don't recommend it.

Improved Initialization Process for the Plugin

The new initialization process for the plugin has been designed to be more robust and handle the local file state and the repository more efficiently. Below is an overview of the key steps involved:

Check Git Initialization: The process begins by checking if Git is already initialized in the current directory. If not, it proceeds with the initialization steps.

Setup Branches: It determines the branches to be used by fetching the remote branch configuration and setting up a local branch accordingly.

Initialize Git Repository:

Fetch and Integrate Changes:

Commit and Push: The updates, along with any new files, are added and committed to the local repository with a predefined commit message. This initial commit is then pushed to the upstream repository.

Sparse Checkout (Optional): If the 'sparse_checkout' configuration is set, this feature is enabled for the repository, allowing partial checkouts of the repository.

Handle Authentication: If needed, the process also includes authenticated access to the remote repository. Credentials are securely retrieved and used to update the remote URL.

Backup and Restore Untracked Files: Untracked files are safely backed up before the merge and restored after the merge process, ensuring no local changes are lost.

Rsync for File Synchronization: The rsync command is used to synchronize the directories while restoring untracked files, ensuring a reliable and efficient restoration process.

Clean Up: After the restoration of untracked files, the backup directory is cleanly removed to avoid clutter and potential conflicts in the future.

This enhanced initialization process aims to streamline the setup and synchronization of the local repository with the remote one, while ensuring that local changes are not overwritten or lost during updates. It makes the plugin more user-friendly and resilient to various repository states and configurations.