threads / patch / 11296

patchcommit: allow --amend to reuse message from another commit

Subject: [PATCH] commit: allow --amend to reuse message from another commit

## tl;dr

One message between Dec 14, 2007 and Dec 14, 2007. Diffs are folded; open one to read it.

replies: 0people: 1as markdown or json

Junio C Hamano· Dec 14, 2007, 19:57 UTC · lore

After tentatively applying a patch from a contributor, you can get a replacement patch with corrected code and unusable commit log message. In such a case, this sequence ought to give you an editor based on the message in the earlier commit, to let you describe an incremental improvement:

	git reset --hard HEAD^ ;# discard the earlier one
        git am <corrected-patch
        git commit --amend -c HEAD@{1}

Unfortunately, --amend insisted reusing the message from the commit being amended, ignoring the -c option. This corrects it.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * This was supported in the scripted version if --amend came before -c,
   but it was broken if the flags were given in the other order.
 builtin-commit.c  |    2 +-
 t/t7501-commit.sh |   17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletions(-)
Show changes to 2 files +18 −1

builtin-commit.c, t/t7501-commit.sh

diff --git a/builtin-commit.c b/builtin-commit.c
index ad9f921..518ebe0 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -537,7 +537,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
 		die("Option -m cannot be combined with -c/-C/-F.");
 	if (edit_message)
 		use_message = edit_message;
-	if (amend)
+	if (amend && !use_message)
 		use_message = "HEAD";
 	if (use_message) {
 		unsigned char sha1[20];
diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh
index 05aa97d..d1a415a 100755
--- a/t/t7501-commit.sh
+++ b/t/t7501-commit.sh
@@ -310,4 +310,21 @@ test_expect_success 'same tree (merge and amend merge)' '
 
 '
 
+test_expect_success 'amend using the message from another commit' '
+
+	git reset --hard &&
+	test_tick &&
+	git commit --allow-empty -m "old commit" &&
+	old=$(git rev-parse --verify HEAD) &&
+	test_tick &&
+	git commit --allow-empty -m "new commit" &&
+	new=$(git rev-parse --verify HEAD) &&
+	test_tick &&
+	git commit --allow-empty --amend -C "$old" &&
+	git show --pretty="format:%ad %s" "$old" >expected &&
+	git show --pretty="format:%ad %s" HEAD >actual &&
+	diff -u expected actual
+
+'
+
 test_done

← back to recent threads