Willy Tarreau | be9b00f | 2020-02-05 04:45:18 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 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 | 98115e7 | 2021-11-03 08:41:01 +0100 | [diff] [blame] | 31 | USAGE="Usage: ${0##*/} [-q] [-H] [-m] [-u] [-r reference] [-l logexpr] [-s subject] [-b base] {branch|range} [...] [-- file*]" |
Willy Tarreau | 29c44e1 | 2016-05-16 17:01:12 +0200 | [diff] [blame] | 32 | BASES=( ) |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 33 | BRANCHES=( ) |
Willy Tarreau | ddf6660 | 2023-08-14 13:03:46 +0200 | [diff] [blame] | 34 | REF= |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 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 | 5e637e5 | 2016-12-14 16:44:45 +0100 | [diff] [blame] | 41 | BODYHASH= |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 42 | |
| 43 | die() { |
| 44 | [ "$#" -eq 0 ] || echo "$*" >&2 |
| 45 | exit 1 |
| 46 | } |
| 47 | |
| 48 | err() { |
| 49 | echo "$*" >&2 |
| 50 | } |
| 51 | |
| 52 | quit() { |
| 53 | [ "$#" -eq 0 ] || echo "$*" |
| 54 | exit 0 |
| 55 | } |
| 56 | |
| 57 | short() { |
| 58 | # git rev-parse --short $1 |
| 59 | echo "${1::8}" |
| 60 | } |
| 61 | |
| 62 | dump_commit_matrix() { |
| 63 | title=":$REF:" |
| 64 | for branch in "${BRANCHES[@]}"; do |
| 65 | #echo -n " $branch" |
| 66 | title="$title :${branch}:" |
| 67 | done |
| 68 | title="$title |" |
| 69 | |
| 70 | count=0 |
| 71 | # now look up commits |
| 72 | while read ref subject; do |
| 73 | if [ -n "$MISSING" -a "${subject:0:9}" = "[RELEASE]" ]; then |
| 74 | continue |
| 75 | fi |
| 76 | |
| 77 | upstream="none" |
| 78 | missing=0 |
Willy Tarreau | 5e637e5 | 2016-12-14 16:44:45 +0100 | [diff] [blame] | 79 | refbhash="" |
Willy Tarreau | 29b684b | 2016-05-16 16:39:38 +0200 | [diff] [blame] | 80 | line="" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 81 | for branch in "${BRANCHES[@]}"; do |
| 82 | set -- $(grep -m 1 $ref "$WORK/${branch//\//_}") |
| 83 | newhash=$1 ; shift |
Willy Tarreau | 5e637e5 | 2016-12-14 16:44:45 +0100 | [diff] [blame] | 84 | bhash="" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 85 | # count the number of cherry-picks after this one. Since we shift, |
| 86 | # the result is in "$#" |
| 87 | while [ -n "$1" -a "$1" != "$ref" ]; do |
| 88 | shift |
| 89 | done |
| 90 | if [ -n "$newhash" ]; then |
| 91 | line="${line} $(short $newhash)-$#" |
| 92 | else |
| 93 | # before giving up we can check if our current commit was |
| 94 | # itself cherry-picked and check this again. In order not |
| 95 | # to have to do it all the time, we can cache the result |
| 96 | # for the current line. If a match is found we report it |
| 97 | # with the '+' delimiter instead of '-'. |
| 98 | if [ "$upstream" = "none" ]; then |
| 99 | upstream=( $(git log -1 --pretty --format=%B "$ref" | \ |
| 100 | sed -n 's/^commit \([^)]*\) upstream\.$/\1/p;s/^(cherry picked from commit \([^)]*\))/\1/p') ) |
| 101 | fi |
| 102 | newhash="" |
| 103 | for h in ${upstream[@]}; do |
| 104 | set -- $(grep -m 1 $h "$WORK/${branch//\//_}") |
| 105 | newhash=$1 ; shift |
| 106 | while [ -n "$1" -a "$1" != "$h" ]; do |
| 107 | shift |
| 108 | done |
| 109 | if [ -n "$newhash" ]; then |
| 110 | line="${line} $(short $newhash)+$#" |
| 111 | break |
| 112 | fi |
| 113 | done |
Willy Tarreau | 5e637e5 | 2016-12-14 16:44:45 +0100 | [diff] [blame] | 114 | if [ -z "$newhash" -a -n "$BODYHASH" ]; then |
| 115 | if [ -z "$refbhash" ]; then |
| 116 | refbhash=$(git log -1 --pretty="%an|%ae|%at|%B" "$ref" | sed -n '/^\(Signed-off-by\|(cherry picked\)/q;p' | md5sum) |
| 117 | fi |
| 118 | |
| 119 | |
| 120 | set -- $(grep -m 1 "H$refbhash\$" "$WORK/${branch//\//_}") |
| 121 | newhash=$1 ; shift |
| 122 | if [ -n "$newhash" ]; then |
| 123 | line="${line} $(short $newhash)+?" |
| 124 | break |
| 125 | fi |
| 126 | fi |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 127 | if [ -z "$newhash" ]; then |
| 128 | line="${line} -" |
| 129 | missing=1 |
| 130 | fi |
| 131 | fi |
| 132 | done |
| 133 | line="${line} |" |
| 134 | if [ -z "$MISSING" -o $missing -gt 0 ]; then |
Willy Tarreau | 48d92ee | 2018-07-30 14:45:55 +0200 | [diff] [blame] | 135 | [ $((count++)) -gt 0 ] || echo "$title" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 136 | [ "$QUIET" != "" -o $count -lt 20 ] || count=0 |
Willy Tarreau | 29b684b | 2016-05-16 16:39:38 +0200 | [diff] [blame] | 137 | if [ -z "$UPSTREAM" -o "$upstream" = "none" -o -z "$upstream" ]; then |
| 138 | echo "$(short $ref) $line" |
| 139 | else |
| 140 | echo "$(short $upstream) $line" |
| 141 | fi |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 142 | fi |
| 143 | done < "$WORK/${REF//\//_}" |
| 144 | } |
| 145 | |
| 146 | while [ -n "$1" -a -z "${1##-*}" ]; do |
| 147 | case "$1" in |
| 148 | -b) BASE="$2" ; shift 2 ;; |
| 149 | -r) REF="$2" ; shift 2 ;; |
| 150 | -l) LOGEXPR="$2" ; shift 2 ;; |
| 151 | -s) SUBJECT="$2" ; shift 2 ;; |
| 152 | -q) QUIET=1 ; shift ;; |
| 153 | -m) MISSING=1 ; shift ;; |
Willy Tarreau | 29b684b | 2016-05-16 16:39:38 +0200 | [diff] [blame] | 154 | -u) UPSTREAM=1 ; shift ;; |
Willy Tarreau | 5e637e5 | 2016-12-14 16:44:45 +0100 | [diff] [blame] | 155 | -H) BODYHASH=1 ; shift ;; |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 156 | -h|--help) quit "$USAGE" ;; |
| 157 | *) die "$USAGE" ;; |
| 158 | esac |
| 159 | done |
| 160 | |
Willy Tarreau | ddf6660 | 2023-08-14 13:03:46 +0200 | [diff] [blame] | 161 | # if no ref, either we're checking missing backports and we'll guess |
| 162 | # the upstream reference branch based on which one contains most of |
| 163 | # the latest commits, or we'll use master. |
| 164 | if [ -z "$REF" ]; then |
| 165 | if [ -n "$MISSING" ]; then |
| 166 | # check the last 10 commits in the base branch, and see where |
| 167 | # the seem to be coming from. |
| 168 | TAG="$(git describe --tags ${BASE:-HEAD} --abbrev=0)" |
| 169 | LAST_COMMITS=( $(git rev-list --abbrev-commit --reverse "$TAG^^.." | tail -n10) ) |
| 170 | REF=$(for i in "${LAST_COMMITS[@]}"; do |
| 171 | upstream=$(git log -1 --pretty --format=%B $i | |
| 172 | sed -n 's/^commit \([^)]*\) upstream\.$/\1/p;s/^(cherry picked from commit \([^)]*\))/\1/p' | |
| 173 | tail -n1) |
| 174 | if [ -n "$upstream" ]; then |
| 175 | # use local first then remote branch |
| 176 | ( git branch --sort=refname --contains $upstream | head -n1 ; |
| 177 | git branch -r --sort=refname --contains $upstream | head -n1) 2>&1 | |
| 178 | grep 'master\|maint' | head -n1 |
| 179 | fi |
| 180 | done | sort | uniq -c | sort -nr | awk '{ print $NF; exit;}') |
| 181 | # here we have a name, e.g. "2.6/master" in REF |
| 182 | REF="${REF:-master}" |
| 183 | err "Warning! No ref specified, using $REF." |
| 184 | else |
| 185 | REF=master |
| 186 | fi |
| 187 | fi |
| 188 | |
Willy Tarreau | 29c44e1 | 2016-05-16 17:01:12 +0200 | [diff] [blame] | 189 | # branches may also appear as id1..id2 to limit the history instead of looking |
| 190 | # back to the common base. The field is left empty if not set. |
| 191 | BRANCHES=( ) |
| 192 | BASES=( ) |
| 193 | while [ $# -gt 0 ]; do |
Willy Tarreau | 98115e7 | 2021-11-03 08:41:01 +0100 | [diff] [blame] | 194 | if [ "$1" = "--" ]; then |
| 195 | shift |
| 196 | break |
| 197 | fi |
Willy Tarreau | 29c44e1 | 2016-05-16 17:01:12 +0200 | [diff] [blame] | 198 | branch="${1##*..}" |
| 199 | if [ "$branch" == "$1" ]; then |
| 200 | base="" |
| 201 | else |
| 202 | base="${1%%..*}" |
| 203 | fi |
| 204 | BASES[${#BRANCHES[@]}]="$base" |
| 205 | BRANCHES[${#BRANCHES[@]}]="$branch" |
| 206 | shift |
| 207 | done |
| 208 | |
Willy Tarreau | 98115e7 | 2021-11-03 08:41:01 +0100 | [diff] [blame] | 209 | # args left for git-log |
| 210 | ARGS=( "$@" ) |
| 211 | |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 212 | if [ ${#BRANCHES[@]} = 0 ]; then |
Willy Tarreau | ddf6660 | 2023-08-14 13:03:46 +0200 | [diff] [blame] | 213 | if [ -n "$MISSING" ]; then |
| 214 | BRANCHES=( HEAD ) |
| 215 | else |
| 216 | die "$USAGE" |
| 217 | fi |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 218 | fi |
| 219 | |
| 220 | for branch in "$REF" "${BRANCHES[@]}"; do |
| 221 | if ! git rev-parse --verify -q "$branch" >/dev/null; then |
| 222 | die "Failed to check git branch $branch." |
| 223 | fi |
| 224 | done |
| 225 | |
Willy Tarreau | ddf6660 | 2023-08-14 13:03:46 +0200 | [diff] [blame] | 226 | if [ -z "$BASE" -a -n "$MISSING" ]; then |
| 227 | err "Warning! No base specified, checking latest backports from current branch since last tag." |
| 228 | |
| 229 | TAG="$(git describe --tags HEAD --abbrev=0)" |
| 230 | COMMITS=( $(git rev-list --abbrev-commit --reverse "$TAG^^..") ) |
| 231 | tip="" |
| 232 | for commit in "${COMMITS[@]}"; do |
| 233 | parent=$(git log -1 --pretty --format=%B $commit | |
| 234 | sed -n 's/^commit \([^)]*\) upstream\.$/\1/p;s/^(cherry picked from commit \([^)]*\))/\1/p' | |
| 235 | tail -n1) |
| 236 | if [ -z "$tip" ]; then |
| 237 | tip=$parent |
| 238 | elif [ -n "$parent" ]; then |
| 239 | base=$(git merge-base "$tip" "$parent") |
| 240 | if [ "$base" = "$tip" ]; then |
| 241 | # tip is older than parent, switch tip to it if it |
| 242 | # belongs to the upstream branch |
| 243 | if [ "$(git merge-base $parent $REF)" = "$parent" ]; then |
| 244 | tip=$parent |
| 245 | fi |
| 246 | fi |
| 247 | fi |
| 248 | done |
| 249 | BASE="$tip" |
| 250 | if [ -n "$BASE" ]; then |
| 251 | echo "Restarting from $(git log -1 --no-decorate --oneline $BASE)" |
| 252 | else |
| 253 | echo "Could not figure the base." |
| 254 | fi |
| 255 | fi |
| 256 | |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 257 | if [ -z "$BASE" ]; then |
| 258 | err "Warning! No base specified, looking for common ancestor." |
| 259 | BASE=$(git merge-base --all "$REF" "${BRANCHES[@]}") |
| 260 | if [ -z "$BASE" ]; then |
| 261 | die "Couldn't find a common ancestor between these branches" |
| 262 | fi |
| 263 | fi |
| 264 | |
| 265 | # we want to go to the git root dir |
| 266 | DIR="$PWD" |
| 267 | cd $(git rev-parse --show-toplevel) |
| 268 | |
| 269 | mkdir -p .git/.show-backports #|| die "Can't create .git/.show-backports" |
| 270 | WORK=.git/.show-backports |
| 271 | |
| 272 | rm -f "$WORK/${REF//\//_}" |
Willy Tarreau | 98115e7 | 2021-11-03 08:41:01 +0100 | [diff] [blame] | 273 | git log --reverse ${LOGEXPR:+--grep $LOGEXPR} --pretty="%H %s" "$BASE".."$REF" -- "${ARGS[@]}" | grep "${SUBJECT}" > "$WORK/${REF//\//_}" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 274 | |
| 275 | # for each branch, enumerate all commits and their ancestry |
Willy Tarreau | 29c44e1 | 2016-05-16 17:01:12 +0200 | [diff] [blame] | 276 | |
| 277 | branch_num=0; |
| 278 | while [ $branch_num -lt "${#BRANCHES[@]}" ]; do |
| 279 | branch="${BRANCHES[$branch_num]}" |
| 280 | base="${BASES[$branch_num]}" |
| 281 | base="${base:-$BASE}" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 282 | rm -f "$WORK/${branch//\//_}" |
Willy Tarreau | 98115e7 | 2021-11-03 08:41:01 +0100 | [diff] [blame] | 283 | git log --reverse --pretty="%H %s" "$base".."$branch" -- "${ARGS[@]}" | grep "${SUBJECT}" | while read h subject; do |
Willy Tarreau | 5e637e5 | 2016-12-14 16:44:45 +0100 | [diff] [blame] | 284 | echo -n "$h" $(git log -1 --pretty --format=%B "$h" | \ |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 285 | sed -n 's/^commit \([^)]*\) upstream\.$/\1/p;s/^(cherry picked from commit \([^)]*\))/\1/p') |
Willy Tarreau | 5e637e5 | 2016-12-14 16:44:45 +0100 | [diff] [blame] | 286 | if [ -n "$BODYHASH" ]; then |
| 287 | echo " H$(git log -1 --pretty="%an|%ae|%at|%B" "$h" | sed -n '/^\(Signed-off-by\|(cherry picked\)/q;p' | md5sum)" |
| 288 | else |
| 289 | echo |
| 290 | fi |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 291 | done > "$WORK/${branch//\//_}" |
Willy Tarreau | 29c44e1 | 2016-05-16 17:01:12 +0200 | [diff] [blame] | 292 | (( branch_num++ )) |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 293 | done |
| 294 | |
| 295 | count=0 |
| 296 | dump_commit_matrix | column -t | \ |
| 297 | ( |
| 298 | left_commits=( ) |
| 299 | right_commits=( ) |
| 300 | while read line; do |
| 301 | # append the subject at the end of the line |
| 302 | set -- $line |
| 303 | echo -n "$line " |
| 304 | if [ "${line::1}" = ":" ]; then |
| 305 | echo "---- Subject ----" |
| 306 | else |
| 307 | # doing it this way prevents git from abusing the terminal |
Willy Tarreau | 48d92ee | 2018-07-30 14:45:55 +0200 | [diff] [blame] | 308 | echo "$(git log -1 --pretty="%s" "$1")" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 309 | left_commits[${#left_commits[@]}]="$1" |
| 310 | comm="" |
| 311 | while [ -n "$1" -a "$1" != "-" -a "$1" != "|" ]; do |
| 312 | comm="${1%-*}" |
| 313 | shift |
| 314 | done |
| 315 | right_commits[${#right_commits[@]}]="$comm" |
| 316 | fi |
| 317 | done |
| 318 | if [ -n "$MISSING" -a ${#left_commits[@]} -eq 0 ]; then |
| 319 | echo "No missing commit to apply." |
| 320 | elif [ -n "$MISSING" ]; then |
| 321 | echo |
Willy Tarreau | f456f6f | 2020-07-31 16:38:57 +0200 | [diff] [blame] | 322 | echo |
Willy Tarreau | ef393a6 | 2016-10-25 22:12:54 +0200 | [diff] [blame] | 323 | echo "In order to show and/or apply all leftmost commits to current branch :" |
Willy Tarreau | 1f927d1 | 2020-07-31 16:47:44 +0200 | [diff] [blame] | 324 | echo " git show --pretty=format:'%C(yellow)commit %H%C(normal)%nAuthor: %an <%ae>%nDate: %aD%n%n%C(green)%C(bold)git cherry-pick -sx %h%n%n%w(72,4,4)%B%N' ${left_commits[@]}" |
Willy Tarreau | f456f6f | 2020-07-31 16:38:57 +0200 | [diff] [blame] | 325 | echo |
Willy Tarreau | 1a3af78 | 2019-11-25 15:51:47 +0100 | [diff] [blame] | 326 | echo " git cherry-pick -sx ${left_commits[@]}" |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 327 | echo |
Willy Tarreau | f456f6f | 2020-07-31 16:38:57 +0200 | [diff] [blame] | 328 | if [ "${left_commits[*]}" != "${right_commits[*]}" ]; then |
| 329 | echo "In order to show and/or apply all rightmost commits to current branch :" |
Willy Tarreau | 1f927d1 | 2020-07-31 16:47:44 +0200 | [diff] [blame] | 330 | echo " git show --pretty=format:'%C(yellow)commit %H%C(normal)%nAuthor: %an <%ae>%nDate: %aD%n%n%C(green)%C(bold)git cherry-pick -sx %h%n%n%w(72,4,4)%B%N' ${right_commits[@]}" |
Willy Tarreau | f456f6f | 2020-07-31 16:38:57 +0200 | [diff] [blame] | 331 | echo |
| 332 | echo " git cherry-pick -sx ${right_commits[@]}" |
| 333 | echo |
| 334 | fi |
Willy Tarreau | 62b71ee | 2016-05-10 12:04:13 +0200 | [diff] [blame] | 335 | fi |
| 336 | ) |