diff options
Diffstat (limited to 'bin/git-sync')
| -rwxr-xr-x | bin/git-sync | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/bin/git-sync b/bin/git-sync new file mode 100755 index 0000000..b18c94d --- /dev/null +++ b/bin/git-sync @@ -0,0 +1,38 @@ +#!/bin/bash +set -eu +[ -n "${DEBUG:-}" ] && set -x + +# use cli arg +base_branch=${1:-} + +# detect base branch (when git-new was used) +[ -n "$base_branch" ] || base_branch=$(git symbolic-ref --short refs/bases/"$(git symbolic-ref --short HEAD)" 2>/dev/null || :) + +# detect trunk via origin/HEAD +[ -n "$base_branch" ] || base_branch=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null || :) + +# fallback to base branch => current branch +[ -n "$base_branch" ] || base_branch=$(git symbolic-ref --short HEAD 2>/dev/null || :) + +# resolve upstream +if [[ "$base_branch" = "origin/"* ]]; then + upstream=$base_branch +else + upstream=$(git rev-parse --abbrev-ref --symbolic-full-name "$base_branch"'@{u}') +fi + +[ -n "${base_branch:?}" ] || exit 1 +[ -n "${upstream:?}" ] || exit 1 + +echo "Syncing with $upstream (base: $base_branch)" + +# fetch upstream +git fetch ${upstream/\// } + +# check if base and upstream have diverged +if ! git diff --quiet "$upstream" "$base_branch"; then + git diff --stat "$upstream" "$base_branch" +fi + +# rebase +git rebase --interactive "$upstream" |