git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/3] commit: give a hint when a commit message has been abandoned

From
Jeff King <peff@peff.net>
Date
Jul 23, 2012, 18:52 UTC
Message-ID
<20120723185218.GC27588@sigill.intra.peff.net>
In-Reply-To
<20120723184634.GA31905@sigill.intra.peff.net>

If we launch an editor for the user to create a commit message, they may put significant work into doing so. Typically we try to check common mistakes that could cause the commit to fail early, so that we die before the user goes to the trouble.

We may still experience some errors afterwards, though; in this case, the user is given no hint that their commit message has been saved. Let's tell them where it is.

Signed-off-by: Jeff King <peff@peff.net>
---
I did not bother protecting this with advice.* config, as it is unlikely
to come up regularly. If somebody cares, they are welcome to add it on
top.
 builtin/commit.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
diff --git a/builtin/commit.c b/builtin/commit.c
index 20cef95..149e07d 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -116,6 +116,16 @@ static enum {
 	STATUS_FORMAT_PORCELAIN
 } status_format = STATUS_FORMAT_LONG;
 
+static int mention_abandoned_message;
+static void maybe_mention_abandoned_message(void)
+{
+	if (!mention_abandoned_message)
+		return;
+	advise(_("Your commit message has been saved in '%s' and will be\n"
+		 "overwritten by the next invocation of \"git commit\"."),
+	       git_path(commit_editmsg));
+}
+
 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
 {
 	struct strbuf *buf = opt->value;
@@ -848,6 +858,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 			_("Please supply the message using either -m or -F option.\n"));
 			exit(1);
 		}
+		atexit(maybe_mention_abandoned_message);
+		mention_abandoned_message = 1;
 	}
 
 	if (!no_verify &&
@@ -1532,11 +1544,13 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	if (template_untouched(&sb) && !allow_empty_message) {
 		rollback_index_files();
 		fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
+		mention_abandoned_message = 0;
 		exit(1);
 	}
 	if (message_is_empty(&sb) && !allow_empty_message) {
 		rollback_index_files();
 		fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
+		mention_abandoned_message = 0;
 		exit(1);
 	}
 
@@ -1579,6 +1593,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		die(_("cannot update HEAD ref"));
 	}
 
+	mention_abandoned_message = 0;
 	unlink(git_path("CHERRY_PICK_HEAD"));
 	unlink(git_path("REVERT_HEAD"));
 	unlink(git_path("MERGE_HEAD"));
-- 
1.7.10.5.40.g059818d
Previous: Jeff KingNext: Junio C Hamano
Message 9 of 16 in “Re: empty ident name trashes commit message”
  1. Ramana KumarJul 21, 2012
  2. Jeff KingJul 23, 2012
  3. Ramana KumarJul 23, 2012
  4. Jeff KingJul 23, 2012
  5. 1/3 advice: pass varargs to strbuf_vaddf, not strbuf_addfJeff King, Jul 23, 2012
  6. 2/3 commit: check committer identity more strictlyJeff King, Jul 23, 2012
  7. Junio C HamanoJul 23, 2012
  8. Jeff KingJul 23, 2012
  9. 3/3 commit: give a hint when a commit message has been abandonedJeff King, Jul 23, 2012
  10. Junio C HamanoJul 23, 2012
  11. Jeff KingJul 23, 2012
  12. Junio C HamanoJul 23, 2012
  13. Jeff KingJul 23, 2012
  14. Junio C HamanoJul 23, 2012
  15. Jeff KingJul 23, 2012
  16. Junio C HamanoJul 23, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.