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 8588452ceb7 allowed to amend a merge commit that does not change the tree, but 4fb5fd5d301 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>
Johannes Sixt committed
Dec 3, 2007 at 08:24 UTC
13aba1e514c1dceee9d731d157bbc261426be351
2 files changed
+38
-3
git-commit.sh
+6
-3
@@ -515,13 +515,16 @@ else
515
# we need to check if there is anything to commit
516
run_status >/dev/null
517
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
+*)
523
rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
524
use_status_color=t
525
run_status
526
exit 1
524
-fi
527
+esac
528
529
case "$no_edit" in
530
'')
t/t7501-commit.sh
+32
@@ -244,4 +244,36 @@ test_expect_success 'multiple -m' '
244
245
'
246
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
+
279
test_done