Skip to content

Commit

Permalink
git-commit: Allow to amend a merge commit that does not change the tree
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Johannes Sixt authored and Junio C Hamano committed Dec 3, 2007
1 parent afcc4f7 commit 13aba1e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
9 changes: 6 additions & 3 deletions git-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,16 @@ else
# we need to check if there is anything to commit
run_status >/dev/null
fi
if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
then
case "$?,$PARENTS" in
0,* | *,-p' '?*-p' '?*)
# a merge commit can record the same tree as its parent.
;;
*)
rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
use_status_color=t
run_status
exit 1
fi
esac

case "$no_edit" in
'')
Expand Down
32 changes: 32 additions & 0 deletions t/t7501-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,36 @@ test_expect_success 'multiple -m' '
'

test_expect_success 'same tree (single parent)' '
if git commit -m empty
then
echo oops -- should have complained
false
else
: happy
fi
'

test_expect_success 'same tree (merge and amend merge)' '
git checkout -b side HEAD^ &&
echo zero >zero &&
git add zero &&
git commit -m "add zero" &&
git checkout master &&
git merge -s ours side -m "empty ok" &&
git diff HEAD^ HEAD >actual &&
: >expected &&
diff -u expected actual &&
git commit --amend -m "empty really ok" &&
git diff HEAD^ HEAD >actual &&
: >expected &&
diff -u expected actual
'

test_done

0 comments on commit 13aba1e

Please sign in to comment.