mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager
synced 2024-10-06 16:21:50 +00:00
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.
This commit is contained in:
parent
c8c2e51916
commit
0ee784f1f0
58
contrib/scripts/git-backport-merge
Executable file
58
contrib/scripts/git-backport-merge
Executable file
|
@ -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 "$@"
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue