Last active
September 6, 2023 08:00
-
-
Save wetneb/e42483ed95ffae8911ab35840fefb1a7 to your computer and use it in GitHub Desktop.
reuse_merge: script to reuse a previous merge during a rebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# WARNING: This script will delete any untracked files present in the working tree | |
# Usage: reuse_merge <commit> | |
# where <commit> is a merge commit whose parents have identical contents to HEAD and MERGE_HEAD (which the script will check). | |
# This will reuse the state of the supplied merge to make a new merge commit on top of HEAD and MERGE_HEAD. | |
# This is useful when using `git rebase --rebase-merges`, when reworking commits between merges | |
# Licence: CC0 | |
# Extract parent commits of the merge commit to reuse | |
joined_parents=`git show $1 | head -2 | grep -P "^Merge: " | sed -e 's/^Merge: //'` | |
IFS=' ' | |
read -ra parents<<<"${joined_parents}" | |
if [ ${#parents[@]} -ne 2 ]; then | |
echo "Error: the commit $1 does not appear to be a merge commit with two parents" | |
exit 1 | |
fi | |
echo "Merge commit $1 has parents:" | |
echo ${parents[0]} | |
echo ${parents[1]} | |
# Check that the parents have the same contents as the current commits to merge | |
function check_commits_have_equal_contents () { | |
if [ $1 != $2 ]; then | |
diff_size=`git diff $1 $2 | wc -l` | |
if [ ${diff_size} -ne 0 ]; then | |
echo "Error: commits $1 and $2 differ, cannot re-apply the same merge commit!" | |
exit 1 | |
fi | |
fi | |
} | |
check_commits_have_equal_contents ${parents[0]} HEAD | |
check_commits_have_equal_contents ${parents[1]} MERGE_HEAD | |
echo "Parents are identical, merging" | |
rm -r * | |
git checkout $1 . | |
git add . | |
git commit -C $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment