summary refs log tree commit diff
path: root/bin/git-sync
blob: b18c94d9ec3f32c259fd49c8086e4c57d018b9aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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"