-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-prepare
More file actions
executable file
·57 lines (49 loc) · 1.76 KB
/
git-prepare
File metadata and controls
executable file
·57 lines (49 loc) · 1.76 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
#
# Prepare the current branch for a pull request: pull and rebase from upstream,
# automatically avoiding interactive mode unless needed.
#
# $1 = upstream (defaults to `git hub-remotes --upstream`)
# $2 = branch (defaults to "main")
set -e # stop on error
SCRIPTS="$(dirname "$0")"
upstream=${1:-$("$SCRIPTS"/git-remotes --upstream | cut -f1)}
current=$(git symbolic-ref --short HEAD)
branch=${2:-$("$SCRIPTS"/git-default-branch)}
echo
echo "Fetching latest commits for $upstream..."
echo "> git fetch $upstream"
git fetch $upstream
echo "Done."
all_changes=$(git log --oneline $upstream/$branch...$current)
merge_base=$(git merge-base $upstream/$branch $current)
local_changes=$(git log --oneline $merge_base...$current)
# When counting lines, skip blanks.
all_num=$(echo "$all_changes" | sed '/^\s*$/d' | wc -l)
local_num=$(echo "$local_changes" | sed '/^\s*$/d' | wc -l)
non_fixup_num=$(echo "$local_changes" | egrep -v ' fixup! ' | sed '/^\s*$/d' | wc -l)
echo
if (( $all_num > 0 )); then
echo "Rebasing on $upstream/$branch..."
if (( $local_num > 0 )); then
if (( $non_fixup_num == 1 )); then
# Accept commits as-is if there's only one non-fixup.
set_rebase_editor="env GIT_SEQUENCE_EDITOR=cat"
else
# Otherwise, launch editor for review.
set_rebase_editor="env -u GIT_SEQUENCE_EDITOR"
fi
echo "> git rebase -i --autosquash $upstream/$branch"
rebase_log=$($set_rebase_editor git rebase -i --autosquash $upstream/$branch)
else
echo "> git rebase $upstream/$branch"
rebase_log=$(git rebase $upstream/$branch)
fi
echo "$rebase_log" | egrep '^# Rebase ' || true
echo "Done."
else
echo "Nothing to do."
fi
echo
echo "Commits on $current since $upstream/$branch:"
git log --oneline $upstream/$branch...$current