@matheusbsbs / matheus-bagliano / commits / fbcf118402
Fork 0

fix git commit --amend -m "new message"

The prepare_log_message() function serves two purposes: - Prepares the commit log message template, to be given to the end user; - Return true if there is something committable; 7168624c3530d8c7ee32f930f8fb2ba302b9801f (Do not generate full commit log message if it is not going to be used) cheated to omit the former when we know the log message template is not going to be used. However, its replacement logic to see if there is something committable was botched. When amending, it should compare the index with the parent of the HEAD, not the current HEAD. Otherwise you cannot run --amend to fix only the message without changing the tree. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 19, 2007 at 19:23 UTC fbcf1184026197128582369074201c490009b4eb
1 file changed +5 -1
builtin-commit.c
+5 -1
@@ -375,6 +375,7 @@ static int prepare_log_message(const char *index_file, const char *prefix)
375 if (no_edit) {
376 struct rev_info rev;
377 unsigned char sha1[40];
378 + const char *parent = "HEAD";
379
380 fclose(fp);
381
@@ -384,9 +385,12 @@ static int prepare_log_message(const char *index_file, const char *prefix)
385 if (get_sha1("HEAD", sha1) != 0)
386 return !!active_nr;
387
388 + if (amend)
389 + parent = "HEAD^1";
390 +
391 init_revisions(&rev, "");
392 rev.abbrev = 0;
389 - setup_revisions(0, NULL, &rev, "HEAD");
393 + setup_revisions(0, NULL, &rev, parent);
394 DIFF_OPT_SET(&rev.diffopt, QUIET);
395 DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
396 run_diff_index(&rev, 1 /* cached */);