-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-rebase-all
executable file
·102 lines (84 loc) · 2.31 KB
/
git-rebase-all
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#! /bin/bash
# Copyright (C) 2015 Ross Burton <[email protected]>
# MIT licensed
set -eu
BLACKLIST=$(git config --get rebase-all.blacklist) || true
COUNT=0
SKIPPED=0
FAILED=0
# report [colour code] [header] [message]
report() {
COLOUR=$1
HEADER=$2
shift 2
echo -e "$(tput setaf $COLOUR)$HEADER:$(tput sgr0) $*"
}
# Returns error if blacklisted
check_blacklisted() {
NAME=$1
for BL in $BLACKLIST; do
if [ "$NAME" = "$BL" ]; then
echo $NAME is blacklisted
return 1
fi
done
return 0
}
# Echo the upstream branch for $1, non-zero exit if none
get_upstream() {
NAME=$1
git rev-parse --abbrev-ref --symbolic-full-name $NAME@{upstream} 2>/dev/null
}
# 0 if $1 and $2 are the same SHA, 1 otherwise
is_same() {
test $(git rev-parse $1) = $(git rev-parse $2)
}
# Rebase $1 to it's upstream branch
try_rebase() {
NAME=$1
UPSTREAM=$(get_upstream $NAME)
if test -z $UPSTREAM; then
report 3 SKIP $NAME has no upstream set
SKIPPED=$((SKIPPED + 1))
return 1
fi
git rebase --quiet $UPSTREAM $NAME 2>/dev/null
if [ $? -eq 0 ] ; then
if is_same $NAME $UPSTREAM; then
report 6 MERGED $NAME is now $UPSTREAM
else
report 2 SUCCESS rebased $NAME to $UPSTREAM
fi
return 0
else
report 1 FAIL $NAME does not rebase to $UPSTREAM
git rebase --abort
FAILED=$((FAILED + 1))
return 1
fi
}
# 0 if the tree is dirty, 1 otherwise
is_dirty() {
test -n "$(git status --porcelain)"
}
if is_dirty; then
report 1 FAIL tree is dirty, refusing to rebase
exit 1
fi
BRANCHES=$(git for-each-ref refs/heads --format='%(refname:strip=2)')
for NAME in $BRANCHES; do
COUNT=$((COUNT + 1))
check_blacklisted $NAME || continue
try_rebase $NAME || :
done
# https://stackoverflow.com/a/66895556
DEFAULT_BRANCH=$(git rev-parse --abbrev-ref origin/HEAD 2>/dev/null || true)
DEFAULT_BRANCH=${DEFAULT_BRANCH#origin/}
if [ $DEFAULT_BRANCH = HEAD ]; then
DEFAULT_BRANCH=""
report 1 WARNING "cannot identify default branch (run git remote set-head origin --auto)"
fi
test -n "$DEFAULT_BRANCH" && git checkout --quiet $DEFAULT_BRANCH
echo
echo "Rebased $COUNT branches, $SKIPPED skipped, $FAILED didn't rebase successfully."
git branch --verbose