From 28db7c76e26c312918959384e02e433f8858de49 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Wed, 8 Apr 2026 13:24:01 +0900 Subject: * MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Automated-commit-by: dots Signed-off-by: Robert Günzler --- bin/git-sync | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 bin/git-sync (limited to 'bin/git-sync') 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" -- cgit 1.4.1