summary refs log tree commit diff
path: root/bin/git-sync
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2026-09-27 17:38:39 +0200
committerRobert Günzler <r@gnzler.io>2026-09-27 17:38:39 +0200
commit0860db73ae6fd9a6f6347219d820bf026a6b8b91 (patch)
tree11318c3670d1c32c5aa0bf404c22fba3bf63fc53 /bin/git-sync
parent4e34cbf9673aeaba252e01df554a0d204e83c5e2 (diff)
downloaddotfiles-master.tar.gz
Automated-commit-by: dots
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
-rwxr-xr-xbin/git-sync58
1 files changed, 33 insertions, 25 deletions
diff --git a/bin/git-sync b/bin/git-sync
index b18c94d..9a41c5c 100755
--- a/bin/git-sync
+++ b/bin/git-sync
@@ -2,37 +2,45 @@
 set -eu
 [ -n "${DEBUG:-}" ] && set -x
 
-# use cli arg
-base_branch=${1:-}
+sync() {
+	local this_branch="${1:?}"
+	local base_branch=""
+	local upstream=""
 
-# 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 base branch (when git-new was used)
+	[ -n "$base_branch" ] || base_branch=$(git symbolic-ref --short refs/baseof/"$this_branch" 2>/dev/null || :)
 
-# detect trunk via origin/HEAD
-[ -n "$base_branch" ] || base_branch=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null || :)
+	# recursive
+	[ -n "$base_branch" ] && sync "$base_branch" && git switch "$this_branch"
 
-# fallback to base branch => current branch
-[ -n "$base_branch" ] || base_branch=$(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 || :)
 
-# resolve upstream
-if [[ "$base_branch" = "origin/"* ]]; then
-	upstream=$base_branch
-else
-	upstream=$(git rev-parse --abbrev-ref --symbolic-full-name "$base_branch"'@{u}')
-fi
+	# fallback to base branch == current branch
+	[ -n "$base_branch" ] || base_branch=$this_branch
 
-[ -n "${base_branch:?}" ] || exit 1
-[ -n "${upstream:?}" ] || exit 1
+	# resolve upstream
+	if [[ "$base_branch" != "origin/"* ]]; then
+		upstream=$(git rev-parse --abbrev-ref --symbolic-full-name "$base_branch"'@{u}')
+	else
+		upstream=origin
+	fi
 
-echo "Syncing with $upstream (base: $base_branch)"
+	echo "Syncing $this_branch with $base_branch from $upstream"
 
-# fetch upstream
-git fetch ${upstream/\// }
+	[ -n "${base_branch:?}" ] || exit 1
+	[ -n "${upstream:?}" ] || exit 1
 
-# check if base and upstream have diverged
-if ! git diff --quiet "$upstream" "$base_branch"; then
-	git diff --stat "$upstream" "$base_branch"
-fi
+	# fetch upstream
+	git fetch ${upstream/\// }
 
-# rebase
-git rebase --interactive "$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" "$this_branch"
+}
+
+sync "$(git symbolic-ref --short HEAD)"