Skip to content

Commit 13aba1e

Browse files
Johannes Sixtgitster
authored andcommitted
git-commit: Allow to amend a merge commit that does not change the tree
Normally, it should not be allowed to generate an empty commit. A merge commit generated with git 'merge -s ours' does not change the tree (along the first parent), but merges are not "empty" even if they do not change the tree. Hence, commit 8588452 allowed to amend a merge commit that does not change the tree, but 4fb5fd5 disallowed it again in an attempt to avoid that an existing commit is amended such that it becomes empty. With this change, a commit can be edited (create a new one or amend an existing one) either if there are changes or if there are at least two parents. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent afcc4f7 commit 13aba1e

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

git-commit.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,16 @@ else
515515
# we need to check if there is anything to commit
516516
run_status >/dev/null
517517
fi
518-
if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
519-
then
518+
case "$?,$PARENTS" in
519+
0,* | *,-p' '?*-p' '?*)
520+
# a merge commit can record the same tree as its parent.
521+
;;
522+
*)
520523
rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
521524
use_status_color=t
522525
run_status
523526
exit 1
524-
fi
527+
esac
525528

526529
case "$no_edit" in
527530
'')

t/t7501-commit.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,36 @@ test_expect_success 'multiple -m' '
244244
245245
'
246246

247+
test_expect_success 'same tree (single parent)' '
248+
249+
if git commit -m empty
250+
then
251+
echo oops -- should have complained
252+
false
253+
else
254+
: happy
255+
fi
256+
257+
'
258+
259+
test_expect_success 'same tree (merge and amend merge)' '
260+
261+
git checkout -b side HEAD^ &&
262+
echo zero >zero &&
263+
git add zero &&
264+
git commit -m "add zero" &&
265+
git checkout master &&
266+
267+
git merge -s ours side -m "empty ok" &&
268+
git diff HEAD^ HEAD >actual &&
269+
: >expected &&
270+
diff -u expected actual &&
271+
272+
git commit --amend -m "empty really ok" &&
273+
git diff HEAD^ HEAD >actual &&
274+
: >expected &&
275+
diff -u expected actual
276+
277+
'
278+
247279
test_done

0 commit comments

Comments
 (0)