Skip to content
Browse files

add check mode

If something requires attention, git-sync will tell you,
but it won't touch anything else.

Signed-off-by: Simon Thum <simon.thum@gmx.de>
  • Loading branch information...
1 parent 8c959ab commit 1bdf6b2887b6fe9d31a1bf6afb020314fc096d27 @simonthum committed
Showing with 56 additions and 11 deletions.
  1. +25 −8 README.md
  2. +31 −3 git-sync
View
33 README.md
@@ -14,7 +14,8 @@ Licensed under CC0
Unlike the myriad of scripts to do just that already available,
it follows the KISS principle: It is safe, small, requires nothing but
-git and bash, but does not even try to shield you from git.
+git and bash, but does not even try to shield you from git. It is
+non-interactive, but will probably leave with a useful hint or error.
It is ultimately intended for git-savy people. As a rule of thumb, if
you know how to complete a failed rebase you're fine.
@@ -38,23 +39,39 @@ as something non-trivial occurs. It is designed to be safe
in that `git-sync` will likely refuse to do anything not known to
be safe.
+You can invoke git-sync in "check" mode, in which git-sync will not do
+anything except return zero if syncing may start, and non-zero if
+manual intervention is required.
+
### How am I supposed to use it?
-Just call `git-sync` inside your average joe's repository (not in the
-middle of a rebase, git-am, merge or whatever, not detached, no
-untracked files) and everything will likely just work. Else, a clear
+ git-sync [mode]
+
+Mode can be empty, sync, or check.
+
+In "check" mode, it will indicate if syncing may start. This is useful
+to see if manual intervention is required (indicated by text and
+non-zero exit code).
+
+In sync mode (the default), just calling `git-sync` inside your
+repository will sync with the remote, if the current branch is
+whitelisted. The repository must not be in the middle of a rebase,
+git-am, merge or whatever, not detached, and untracked files may also
+be a problem (see Options). Likely, sync will just work. Else, a clear
error message should appear. If you don't sync in an intertwined
manner (from multiple repositories/machines), `git-sync` is virtually
-guaranteed to work.
+guaranteed to succeed. Otherwise it will try to rebase, which may
+fail. This is where you'll need your git skills.
## How does it work?
The flow is roughly:
1. sanity checks. You don't want to do this in the middle of a rebase.
-2. Check for new files; exit if there are, unless allowed in config.
-3. Check for auto-commitable changes (see syncNewFiles option).
-4. perform auto-commit
+2. Check for new files; exit if there are, unless allowed (see Options).
+2b. In check mode, exit with 0 here.
+3. Check for auto-commitable changes.
+4. perform auto-commit (see Options)
5. one more check for leftover changes / general tidyness
6. fetch upstream
7. Relate upstream to ours. If ahead, push. If behind, pull. If diverged, rebase, then push.
View
34 git-sync
@@ -15,14 +15,24 @@
# it follows the KISS principle: It is small, requires nothing but
# git and bash, but does not even try to shield you from git.
#
-# It will likely get from you from a dull normal git repo with trivial
+# Mode sync (default)
+#
+# Sync will likely get from you from a dull normal git repo with trivial
# changes to an updated dull normal git repo equal to origin. No more,
# no less. The intent is to do everything that's needed to sync
# automatically, and resort to manual intervention as soon
# as something non-trivial occurs. It is designed to be safe
# in that it will likely refuse to do anything not known to
# be safe.
-
+#
+# Mode check
+#
+# Check only performs the basic checks sync checks for to
+# make sure the repository is in a state sufficiently "normal"
+# to continue syncing, i.e. committing changes, pull etc. without
+# losing any data. When check returns 0, sync can start immediately.
+# This does not, however, indicate that syncing is at all likely to
+# succeed.
# command used to auto-commit file modifications
DEFAULT_AUTOCOMMIT_CMD="git add -u ; git commit -m \"changes from $(uname -n) on $(date)\";"
@@ -169,7 +179,7 @@ exit_assuming_sync() {
# first some sanity checks
rstate="$(git_repo_state)"
if [[ -z "$rstate" || "|DIRTY" = "$rstate" ]]; then
- echo "git-sync: Preparing for sync. Repo in $(__gitdir)"
+ echo "git-sync: Preparing. Repo in $(__gitdir)"
elif [[ "NOGIT" = "$rstate" ]] ; then
echo "git-sync: No git repository detected. Exiting."
exit 128 # matches git's error code
@@ -213,6 +223,17 @@ if [ "true" != "$(git config --get --bool branch.$branch_name.sync)" ] ; then
exit 1
fi
+# determine mode
+if [[ -z "$1" || "$1" == "sync" ]]; then
+ mode="sync"
+elif [[ "check" == "$1" ]]; then
+ mode="check"
+else
+ echo "git-sync: Mode $1 not recognized"
+ exit 100
+fi
+
+echo "git-sync: Mode $mode"
echo "git-sync: Using $remote_name/$branch_name"
@@ -223,6 +244,12 @@ if [ ! -z "$(check_initial_file_state)" ] ; then
exit 1
fi
+# if in check mode, this is all we need to know
+if [ $mode == "check" ] ; then
+ echo "git-sync: check OK; sync may start."
+ exit 0
+fi
+
# check if we have to commit local changes, if yes, do so
if [ ! -z "$(local_changes)" ]; then
autocommit_cmd=""
@@ -299,3 +326,4 @@ case "$(sync_state)" in
# TODO: save master, if rebasing fails, make a branch of old master
;;
esac
+

0 comments on commit 1bdf6b2

Please sign in to comment.
Something went wrong with that request. Please try again.