blob: cbe40bbc296469d56130b23e2798087b15e4d906 [file] [log] [blame]
Willy Tarreau62b71ee2016-05-10 12:04:13 +02001#!/bin/bash
2#
3# Compares multiple branches against a reference and shows which ones contain
4# each commit, and the level of backports since the origin or its own ancestors.
5#
6# Copyright (c) 2016 Willy Tarreau <w@1wt.eu>
7#
8# The purpose is to make it easy to visualize what backports might be missing
9# in a maintenance branch, and to easily spot the ones that are needed and the
10# ones that are not. It solely relies on the "cherry-picked from" tags in the
11# commit messages to find what commit is available where, and can even find a
12# reference commit's ancestor in another branch's commit ancestors as well to
13# detect that the patch is present. When done with the proper references and
14# a correct ordering of the branches, it can be used to quickly apply a set of
15# fixes to a branch since it dumps suggested commands at the end. When doing
16# so it is a good idea to use "HEAD" as the last branch to avoid doing mistakes.
17#
18# Examples :
19# - find what's in master and not in current branch :
20# show-backports -q -m -r master HEAD
21# - find what's in 1.6/master and in hapee-maint-1.5r2 but not in current branch :
22# show-backports -q -m -r 1.6/master hapee-maint-1.5r2 HEAD | grep ' [a-f0-9]\{8\}[-+][0-9] '
23# - check that no recent fix from master is missing in any maintenance branch :
24# show-backports -r master hapee-maint-1.5r2 aloha-7.5 hapee-maint-1.5r1 aloha-7.0
25# - see what was recently merged into 1.6 and has no equivalent in local master :
26# show-backports -q -m -r 1.6/master -b "1.6/master@{1 week ago}" master
27# - check what extra backports are present in hapee-r2 compared to hapee-r1 :
28# show-backports -q -m -r hapee-r2 hapee-r1
29
30
Willy Tarreau29c44e12016-05-16 17:01:12 +020031USAGE="Usage: ${0##*/} [-q] [-m] [-u] [-r reference] [-l logexpr] [-s subject] [-b base] {branch|range} [...]"
32BASES=( )
Willy Tarreau62b71ee2016-05-10 12:04:13 +020033BRANCHES=( )
34REF=master
35BASE=
36QUIET=
37LOGEXPR=
38SUBJECT=
39MISSING=
Willy Tarreau29b684b2016-05-16 16:39:38 +020040UPSTREAM=
Willy Tarreau62b71ee2016-05-10 12:04:13 +020041
42die() {
43 [ "$#" -eq 0 ] || echo "$*" >&2
44 exit 1
45}
46
47err() {
48 echo "$*" >&2
49}
50
51quit() {
52 [ "$#" -eq 0 ] || echo "$*"
53 exit 0
54}
55
56short() {
57 # git rev-parse --short $1
58 echo "${1::8}"
59}
60
61dump_commit_matrix() {
62 title=":$REF:"
63 for branch in "${BRANCHES[@]}"; do
64 #echo -n " $branch"
65 title="$title :${branch}:"
66 done
67 title="$title |"
68
69 count=0
70 # now look up commits
71 while read ref subject; do
72 if [ -n "$MISSING" -a "${subject:0:9}" = "[RELEASE]" ]; then
73 continue
74 fi
75
76 upstream="none"
77 missing=0
Willy Tarreau29b684b2016-05-16 16:39:38 +020078 line=""
Willy Tarreau62b71ee2016-05-10 12:04:13 +020079 for branch in "${BRANCHES[@]}"; do
80 set -- $(grep -m 1 $ref "$WORK/${branch//\//_}")
81 newhash=$1 ; shift
82 # count the number of cherry-picks after this one. Since we shift,
83 # the result is in "$#"
84 while [ -n "$1" -a "$1" != "$ref" ]; do
85 shift
86 done
87 if [ -n "$newhash" ]; then
88 line="${line} $(short $newhash)-$#"
89 else
90 # before giving up we can check if our current commit was
91 # itself cherry-picked and check this again. In order not
92 # to have to do it all the time, we can cache the result
93 # for the current line. If a match is found we report it
94 # with the '+' delimiter instead of '-'.
95 if [ "$upstream" = "none" ]; then
96 upstream=( $(git log -1 --pretty --format=%B "$ref" | \
97 sed -n 's/^commit \([^)]*\) upstream\.$/\1/p;s/^(cherry picked from commit \([^)]*\))/\1/p') )
98 fi
99 newhash=""
100 for h in ${upstream[@]}; do
101 set -- $(grep -m 1 $h "$WORK/${branch//\//_}")
102 newhash=$1 ; shift
103 while [ -n "$1" -a "$1" != "$h" ]; do
104 shift
105 done
106 if [ -n "$newhash" ]; then
107 line="${line} $(short $newhash)+$#"
108 break
109 fi
110 done
111 if [ -z "$newhash" ]; then
112 line="${line} -"
113 missing=1
114 fi
115 fi
116 done
117 line="${line} |"
118 if [ -z "$MISSING" -o $missing -gt 0 ]; then
119 [ $((count++)) -gt 0 ] || echo $title
120 [ "$QUIET" != "" -o $count -lt 20 ] || count=0
Willy Tarreau29b684b2016-05-16 16:39:38 +0200121 if [ -z "$UPSTREAM" -o "$upstream" = "none" -o -z "$upstream" ]; then
122 echo "$(short $ref) $line"
123 else
124 echo "$(short $upstream) $line"
125 fi
Willy Tarreau62b71ee2016-05-10 12:04:13 +0200126 fi
127 done < "$WORK/${REF//\//_}"
128}
129
130while [ -n "$1" -a -z "${1##-*}" ]; do
131 case "$1" in
132 -b) BASE="$2" ; shift 2 ;;
133 -r) REF="$2" ; shift 2 ;;
134 -l) LOGEXPR="$2" ; shift 2 ;;
135 -s) SUBJECT="$2" ; shift 2 ;;
136 -q) QUIET=1 ; shift ;;
137 -m) MISSING=1 ; shift ;;
Willy Tarreau29b684b2016-05-16 16:39:38 +0200138 -u) UPSTREAM=1 ; shift ;;
Willy Tarreau62b71ee2016-05-10 12:04:13 +0200139 -h|--help) quit "$USAGE" ;;
140 *) die "$USAGE" ;;
141 esac
142done
143
Willy Tarreau29c44e12016-05-16 17:01:12 +0200144# branches may also appear as id1..id2 to limit the history instead of looking
145# back to the common base. The field is left empty if not set.
146BRANCHES=( )
147BASES=( )
148while [ $# -gt 0 ]; do
149 branch="${1##*..}"
150 if [ "$branch" == "$1" ]; then
151 base=""
152 else
153 base="${1%%..*}"
154 fi
155 BASES[${#BRANCHES[@]}]="$base"
156 BRANCHES[${#BRANCHES[@]}]="$branch"
157 shift
158done
159
Willy Tarreau62b71ee2016-05-10 12:04:13 +0200160if [ ${#BRANCHES[@]} = 0 ]; then
161 die "$USAGE"
162fi
163
164for branch in "$REF" "${BRANCHES[@]}"; do
165 if ! git rev-parse --verify -q "$branch" >/dev/null; then
166 die "Failed to check git branch $branch."
167 fi
168done
169
170if [ -z "$BASE" ]; then
171 err "Warning! No base specified, looking for common ancestor."
172 BASE=$(git merge-base --all "$REF" "${BRANCHES[@]}")
173 if [ -z "$BASE" ]; then
174 die "Couldn't find a common ancestor between these branches"
175 fi
176fi
177
178# we want to go to the git root dir
179DIR="$PWD"
180cd $(git rev-parse --show-toplevel)
181
182mkdir -p .git/.show-backports #|| die "Can't create .git/.show-backports"
183WORK=.git/.show-backports
184
185rm -f "$WORK/${REF//\//_}"
186git log --reverse ${LOGEXPR:+--grep $LOGEXPR} --pretty="%H %s" "$BASE".."$REF" | grep "${SUBJECT}" > "$WORK/${branch//\//_}" > "$WORK/${REF//\//_}"
187
188# for each branch, enumerate all commits and their ancestry
Willy Tarreau29c44e12016-05-16 17:01:12 +0200189
190branch_num=0;
191while [ $branch_num -lt "${#BRANCHES[@]}" ]; do
192 branch="${BRANCHES[$branch_num]}"
193 base="${BASES[$branch_num]}"
194 base="${base:-$BASE}"
Willy Tarreau62b71ee2016-05-10 12:04:13 +0200195 rm -f "$WORK/${branch//\//_}"
Willy Tarreau29c44e12016-05-16 17:01:12 +0200196 git log --reverse --pretty="%H %s" "$base".."$branch" | grep "${SUBJECT}" | while read h subject; do
Willy Tarreau62b71ee2016-05-10 12:04:13 +0200197 echo "$h" $(git log -1 --pretty --format=%B "$h" | \
198 sed -n 's/^commit \([^)]*\) upstream\.$/\1/p;s/^(cherry picked from commit \([^)]*\))/\1/p')
199 done > "$WORK/${branch//\//_}"
Willy Tarreau29c44e12016-05-16 17:01:12 +0200200 (( branch_num++ ))
Willy Tarreau62b71ee2016-05-10 12:04:13 +0200201done
202
203count=0
204dump_commit_matrix | column -t | \
205(
206 left_commits=( )
207 right_commits=( )
208 while read line; do
209 # append the subject at the end of the line
210 set -- $line
211 echo -n "$line "
212 if [ "${line::1}" = ":" ]; then
213 echo "---- Subject ----"
214 else
215 # doing it this way prevents git from abusing the terminal
216 echo $(git log -1 --pretty="%s" "$1")
217 left_commits[${#left_commits[@]}]="$1"
218 comm=""
219 while [ -n "$1" -a "$1" != "-" -a "$1" != "|" ]; do
220 comm="${1%-*}"
221 shift
222 done
223 right_commits[${#right_commits[@]}]="$comm"
224 fi
225 done
226 if [ -n "$MISSING" -a ${#left_commits[@]} -eq 0 ]; then
227 echo "No missing commit to apply."
228 elif [ -n "$MISSING" ]; then
229 echo
230 echo "In order to apply all leftmost commits to current branch :"
231 echo " git cherry-pick -x ${left_commits[@]}"
232 echo
233 echo "In order to apply all rightmost commits to current branch :"
234 echo " git cherry-pick -x ${right_commits[@]}"
235 fi
236)