]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
SCRIPTS: git-show-backports: automatic ref and base detection with -m
authorWilly Tarreau <w@1wt.eu>
Mon, 14 Aug 2023 11:03:46 +0000 (13:03 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 14 Aug 2023 11:12:56 +0000 (13:12 +0200)
When running with -m (check for missing backports) we often have to fill
lots of information that can be determined automatically the vast majority
of the time:
  - restart point (last cherry-picked ID from one of the last commits)
  - current branch (HEAD)
  - reference branch (the one that contains most of the last commits)

These elements are not that hard to determine, so let's make sure we
can fall back to them when running in missing mode.

The reference branch is guessed by looking at the upstream branch that
most frequently contains some of the last 10 commits. It can be inaccurate
if multiple branches exist with these commits, or when upstream changes
due to a non-LTS branch disappearing in the middle of the series, in which
case passing "-r" will help. But most of the time it works OK. It also gives
precedence to local branches over remote ones for such choices. A test in
2.4 at commit 793a4b520 correctly shows 2.6/master as the upstream despite
2.5 having been used for the early ones of the tag.

For the restart point, we assume that the most recent commit that was
backported serves as a reference (and not the most recently backported
commit). This means that the usual case where an old commit was found
to be missing will not fool the analysis. Commits are inspected from
2 commits before the last tag, and reordered from the parent's tree
to see which one is the last one.

With this, it's sufficient to issue "git-show-backports -q -m" to get
the list of backports from the upstream branch, restarting from the
last backported one.

scripts/git-show-backports

index 01a9e6331b9563af5e15c5196278db1243805996..f2c40fec0cdd2c5c251c3d4e8ec2c4d223a22802 100755 (executable)
@@ -31,7 +31,7 @@
 USAGE="Usage: ${0##*/} [-q] [-H] [-m] [-u] [-r reference] [-l logexpr] [-s subject] [-b base] {branch|range} [...] [-- file*]"
 BASES=( )
 BRANCHES=( )
-REF=master
+REF=
 BASE=
 QUIET=
 LOGEXPR=
@@ -158,6 +158,34 @@ while [ -n "$1" -a -z "${1##-*}" ]; do
        esac
 done
 
+# if no ref, either we're checking missing backports and we'll guess
+# the upstream reference branch based on which one contains most of
+# the latest commits, or we'll use master.
+if [ -z "$REF" ]; then
+       if [ -n "$MISSING" ]; then
+               # check the last 10 commits in the base branch, and see where
+               # the seem to be coming from.
+               TAG="$(git describe --tags ${BASE:-HEAD} --abbrev=0)"
+               LAST_COMMITS=( $(git rev-list --abbrev-commit --reverse "$TAG^^.." | tail -n10) )
+               REF=$(for i in "${LAST_COMMITS[@]}"; do
+                       upstream=$(git log -1 --pretty --format=%B $i |
+                                  sed -n 's/^commit \([^)]*\) upstream\.$/\1/p;s/^(cherry picked from commit \([^)]*\))/\1/p' |
+                                  tail -n1)
+                       if [ -n "$upstream" ]; then
+                               # use local first then remote branch
+                               ( git branch --sort=refname --contains $upstream | head -n1 ;
+                                 git branch -r --sort=refname --contains $upstream | head -n1) 2>&1 |
+                                       grep 'master\|maint' | head -n1
+                       fi
+                     done | sort | uniq -c | sort -nr | awk '{ print $NF; exit;}')
+               # here we have a name, e.g. "2.6/master" in REF
+               REF="${REF:-master}"
+               err "Warning! No ref specified, using $REF."
+       else
+               REF=master
+       fi
+fi
+
 # branches may also appear as id1..id2 to limit the history instead of looking
 # back to the common base. The field is left empty if not set.
 BRANCHES=( )
@@ -182,7 +210,11 @@ done
 ARGS=( "$@" )
 
 if [ ${#BRANCHES[@]} = 0 ]; then
-       die "$USAGE"
+       if [ -n "$MISSING" ]; then
+               BRANCHES=( HEAD )
+       else
+               die "$USAGE"
+       fi
 fi
 
 for branch in "$REF" "${BRANCHES[@]}"; do
@@ -191,6 +223,37 @@ for branch in "$REF" "${BRANCHES[@]}"; do
        fi
 done
 
+if [ -z "$BASE" -a -n "$MISSING" ]; then
+       err "Warning! No base specified, checking latest backports from current branch since last tag."
+
+       TAG="$(git describe --tags HEAD --abbrev=0)"
+       COMMITS=( $(git rev-list --abbrev-commit --reverse "$TAG^^..") )
+       tip=""
+       for commit in "${COMMITS[@]}"; do
+               parent=$(git log -1 --pretty --format=%B $commit |
+                        sed -n 's/^commit \([^)]*\) upstream\.$/\1/p;s/^(cherry picked from commit \([^)]*\))/\1/p' |
+                        tail -n1)
+               if [ -z "$tip" ]; then
+                       tip=$parent
+               elif [ -n "$parent" ]; then
+                       base=$(git merge-base "$tip" "$parent")
+                       if [ "$base" = "$tip" ]; then
+                               # tip is older than parent, switch tip to it if it
+                               # belongs to the upstream branch
+                               if [ "$(git merge-base $parent $REF)" = "$parent" ]; then
+                                       tip=$parent
+                               fi
+                       fi
+               fi
+       done
+       BASE="$tip"
+       if [ -n "$BASE" ]; then
+               echo "Restarting from $(git log -1 --no-decorate --oneline $BASE)"
+       else
+               echo "Could not figure the base."
+       fi
+fi
+
 if [ -z "$BASE" ]; then
        err "Warning! No base specified, looking for common ancestor."
        BASE=$(git merge-base --all "$REF" "${BRANCHES[@]}")