Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 1 | #!/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 Tarreau | 29c44e1 | 2016-05-16 17:01:12 +0200 | [diff] [blame] | 31 | USAGE="Usage: ${0##*/} [-q] [-m] [-u] [-r reference] [-l logexpr] [-s subject] [-b base] {branch|range} [...]" |
| 32 | BASES=( ) |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 33 | BRANCHES=( ) |
| 34 | REF=master |
| 35 | BASE= |
| 36 | QUIET= |
| 37 | LOGEXPR= |
| 38 | SUBJECT= |
| 39 | MISSING= |
Willy Tarreau | 29b684b | 2016-05-16 16:39:38 +0200 | [diff] [blame] | 40 | UPSTREAM= |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 41 | |
| 42 | die() { |
| 43 | [ "$#" -eq 0 ] || echo "$*" >&2 |
| 44 | exit 1 |
| 45 | } |
| 46 | |
| 47 | err() { |
| 48 | echo "$*" >&2 |
| 49 | } |
| 50 | |
| 51 | quit() { |
| 52 | [ "$#" -eq 0 ] || echo "$*" |
| 53 | exit 0 |
| 54 | } |
| 55 | |
| 56 | short() { |
| 57 | # git rev-parse --short $1 |
| 58 | echo "${1::8}" |
| 59 | } |
| 60 | |
| 61 | dump_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 Tarreau | 29b684b | 2016-05-16 16:39:38 +0200 | [diff] [blame] | 78 | line="" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 79 | 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 Tarreau | 29b684b | 2016-05-16 16:39:38 +0200 | [diff] [blame] | 121 | 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 Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 126 | fi |
| 127 | done < "$WORK/${REF//\//_}" |
| 128 | } |
| 129 | |
| 130 | while [ -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 Tarreau | 29b684b | 2016-05-16 16:39:38 +0200 | [diff] [blame] | 138 | -u) UPSTREAM=1 ; shift ;; |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 139 | -h|--help) quit "$USAGE" ;; |
| 140 | *) die "$USAGE" ;; |
| 141 | esac |
| 142 | done |
| 143 | |
Willy Tarreau | 29c44e1 | 2016-05-16 17:01:12 +0200 | [diff] [blame] | 144 | # 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. |
| 146 | BRANCHES=( ) |
| 147 | BASES=( ) |
| 148 | while [ $# -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 |
| 158 | done |
| 159 | |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 160 | if [ ${#BRANCHES[@]} = 0 ]; then |
| 161 | die "$USAGE" |
| 162 | fi |
| 163 | |
| 164 | for 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 |
| 168 | done |
| 169 | |
| 170 | if [ -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 |
| 176 | fi |
| 177 | |
| 178 | # we want to go to the git root dir |
| 179 | DIR="$PWD" |
| 180 | cd $(git rev-parse --show-toplevel) |
| 181 | |
| 182 | mkdir -p .git/.show-backports #|| die "Can't create .git/.show-backports" |
| 183 | WORK=.git/.show-backports |
| 184 | |
| 185 | rm -f "$WORK/${REF//\//_}" |
Willy Tarreau | b684cd4 | 2016-12-14 16:43:23 +0100 | [diff] [blame^] | 186 | git log --reverse ${LOGEXPR:+--grep $LOGEXPR} --pretty="%H %s" "$BASE".."$REF" | grep "${SUBJECT}" > "$WORK/${REF//\//_}" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 187 | |
| 188 | # for each branch, enumerate all commits and their ancestry |
Willy Tarreau | 29c44e1 | 2016-05-16 17:01:12 +0200 | [diff] [blame] | 189 | |
| 190 | branch_num=0; |
| 191 | while [ $branch_num -lt "${#BRANCHES[@]}" ]; do |
| 192 | branch="${BRANCHES[$branch_num]}" |
| 193 | base="${BASES[$branch_num]}" |
| 194 | base="${base:-$BASE}" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 195 | rm -f "$WORK/${branch//\//_}" |
Willy Tarreau | 29c44e1 | 2016-05-16 17:01:12 +0200 | [diff] [blame] | 196 | git log --reverse --pretty="%H %s" "$base".."$branch" | grep "${SUBJECT}" | while read h subject; do |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 197 | 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 Tarreau | 29c44e1 | 2016-05-16 17:01:12 +0200 | [diff] [blame] | 200 | (( branch_num++ )) |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 201 | done |
| 202 | |
| 203 | count=0 |
| 204 | dump_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 |
Willy Tarreau | ef393a6 | 2016-10-25 22:12:54 +0200 | [diff] [blame] | 230 | echo "In order to show and/or apply all leftmost commits to current branch :" |
| 231 | echo " git show ${left_commits[@]}" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 232 | echo " git cherry-pick -x ${left_commits[@]}" |
| 233 | echo |
Willy Tarreau | ef393a6 | 2016-10-25 22:12:54 +0200 | [diff] [blame] | 234 | echo "In order to show and/or apply all rightmost commits to current branch :" |
| 235 | echo " git show ${right_commits[@]}" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 236 | echo " git cherry-pick -x ${right_commits[@]}" |
| 237 | fi |
| 238 | ) |