Add autostash command flag to git-gitlab-sync

This commit is contained in:
Robert Maynard 2017-06-07 10:02:19 -04:00
parent 0f171de1c5
commit 569655c454

@ -21,6 +21,10 @@ OPTIONS
--dry-run
Show what would be changed without actually updating
--autostash
automatically stash/stash pop before and after
'
OPTIONS_SPEC=
SUBDIRECTORY_OK=Yes
@ -32,15 +36,19 @@ egrep-q() {
# Load the project configuration.
require_work_tree_exists
state_dir="$GIT_DIR"/gitlab-sync
#-----------------------------------------------------------------------------
remote=''
autostash="$(git config --bool gitlab.sync.autostash || echo false)"
dry_run=false
# Parse the command line options.
while test $# != 0; do
case "$1" in
--autostash) autostash=true ;;
--no-autostash) autostash=false ;;
--dry-run) dry_run=true ;;
--) shift; break ;;
-*) usage ;;
@ -61,6 +69,44 @@ if test -z "$topic" -o "$topic" = "master"; then
fi
#-----------------------------------------------------------------------------
apply_autostash () {
if test -f "$state_dir/autostash"
then
stash_sha1=$(cat "$state_dir/autostash")
if git stash apply $stash_sha1 2>&1 >/dev/null
then
gettext 'Applied autostash.'
else
git stash store -m "autostash" -q $stash_sha1 ||
die "$(eval_gettext "Cannot store \$stash_sha1")"
gettext 'Applying autostash resulted in conflicts.
Your changes are safe in the stash.
You can run "git stash pop" or "git stash drop" at any time.
'
fi
fi
}
finish_sync () {
apply_autostash &&
{ git gc --auto || true; } &&
rm -rf "$state_dir"
}
#-----------------------------------------------------------------------------
if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
then
gettext 'trying to stash local changes' &&
stash_sha1=$(git stash create "autostash") ||
die "$(gettext 'Cannot autostash')"
mkdir -p "$state_dir" &&
echo $stash_sha1 >"$state_dir/autostash" &&
stash_abbrev=$(git rev-parse --short $stash_sha1) &&
echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
git reset --hard
fi
require_clean_work_tree "sync" "$(gettext "Error syncing \
We are trying to overwrite all local changes on this branch with the version on \
@ -84,5 +130,6 @@ then
fi
fi
finish_sync
# Reproduce the push exit code.
exit $fetch_exit