NetworkManager/contrib/scripts/git-backport-merge
Thomas Haller 0ee784f1f0
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.
2023-02-03 10:37:35 +01:00

59 lines
2 KiB
Bash
Executable file

#!/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 "$@"