From 0ee784f1f05e935b9d2fe7f6c54f1930059ea5b8 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 2 Feb 2023 18:24:36 +0100 Subject: [PATCH] contrib: add "git-backport-merge" script for backporting merge commits in NetworkManager On the main branch, we commonly rebase our WIP branches to latest HEAD, before merging them with "--no-ff". The effect is to have a merge commit that acts as a parentheses around the set of patches. When backporting such a branch, we should preserve that structure and take the merge commit too. We should must use `git cherry-pick -x` to record the commit IDs of the original patch. This script helps with that. Also hook it up in "contrib/scripts/nm-setup-git.sh" to create an alias for it. This alias has the advantage, of fetching the latest version of the script from "main" or "origin/main", so it also works on older branches. --- contrib/scripts/git-backport-merge | 58 ++++++++++++++++++++++++++++++ contrib/scripts/nm-setup-git.sh | 1 + 2 files changed, 59 insertions(+) create mode 100755 contrib/scripts/git-backport-merge diff --git a/contrib/scripts/git-backport-merge b/contrib/scripts/git-backport-merge new file mode 100755 index 0000000000..8ba2244f21 --- /dev/null +++ b/contrib/scripts/git-backport-merge @@ -0,0 +1,58 @@ +#!/bin/bash + +# Uses `git cherry-pick -x` to backport a merge commit to an older branch. +# +# Usage: +# First checkout the old-stable branch, that is the target for the backport. +# Then `git-backport-merge MERGE_REF [REFS...]` +# MERGE_REF is the merge commit that should be backported. +# [REFS...] is the commits that should be backported. If omitted, +# it automatically takes the parent commits of the merge commit. + +die() { + printf '%s\n' "$*" >&2 + exit 1 +} + +backport_merge() { + test "$#" -gt 0 || die "Requires the commit ref to backport (and optimally select the commits to include)" + + local M="${@:$#}" + local h + + if test "$#" -eq 1; then + local C=($(git log --reverse "--pretty=%H" "$M"^1.."$M"^2)) + else + local C=("${@:1:$#-1}") + fi + + local OLD_HEAD="$(git rev-parse HEAD)" || die "failed to get current HEAD" + + test -n "$(git status --porcelain --untracked-files=no)" && die "Working directory contains changes. Abort." + + local M_ID="$(git rev-parse "$M"^{commit})" || die "\"$M\" is not a valid commit" + + trap EXIT 'test -z "$OLD_HEAD" || git reset "$OLD_HEAD" --hard' + + for h in "${C[@]}"; do + if ! git cherry-pick --allow-empty -x "$h" ; then + git cherry-pick --abort + die "failed to cherry-pick commit \"$h\" on top of \"$(git rev-parse HEAD)\"" + fi + done + + local NEW_HEAD="$(git rev-parse HEAD)" || die "failed to get new HEAD" + + git reset --hard "$OLD_HEAD" || die "Failed to reset to previous HEAD \"$OLD_HEAD\"" + + git merge --no-ff --no-edit "$NEW_HEAD" || die "Failed to merge old HEAD \"$OLD_HEAD\" with new \"$NEW_HEAD\"" + + git commit --amend --allow-empty -C "$M" || die "Failed to amend merge commit \"$(git rev-parse HEAD)\" with commit message from \"$M\"" + + git rev-parse "$M" | sed 's/.*/(cherry picked from commit \0)/' | GIT_EDITOR='sh -c "cat >> \"$1\""' git commit --allow-empty --amend || \ + die "Failed to amend merge commit \"$(git rev-parse HEAD)\" with cherry-picked-from message from \"$M\"" + + OLD_HEAD= +} + +backport_merge "$@" diff --git a/contrib/scripts/nm-setup-git.sh b/contrib/scripts/nm-setup-git.sh index 24d9c7bc2c..392e3f36cc 100755 --- a/contrib/scripts/nm-setup-git.sh +++ b/contrib/scripts/nm-setup-git.sh @@ -109,6 +109,7 @@ git_config_reset blame.markUnblamableLines true git_config_add notes.displayref 'refs/notes/bugs' git_config_add remote.origin.fetch 'refs/notes/bugs:refs/notes/bugs' git_config_reset remote.origin.pushurl 'git@gitlab.freedesktop.org:NetworkManager/NetworkManager.git' +git_config_add 'alias.backport-merge' '! (git show main:contrib/scripts/git-backport-merge || git show origin/main:contrib/scripts/git-backport-merge) | bash -s -' if [ "$NO_TEST" != 1 ]; then printf "Run with \"--no-test\" or see \"-h\"\n" >&2