summary refs log tree commit diff
path: root/bin/git-sync
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2026-04-08 13:24:01 +0900
committerRobert Günzler <r@gnzler.io>2026-04-08 13:24:01 +0900
commit28db7c76e26c312918959384e02e433f8858de49 (patch)
treecc06c8ffa2444b49cbaf01d45aede08411d23049 /bin/git-sync
parentb61b58ce2c1371d6f904f108fa7feed3abfbe5c7 (diff)
*
Automated-commit-by: dots
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to 'bin/git-sync')
-rwxr-xr-xbin/git-sync38
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"