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

[PATCH 3/9] commit: split off a function to fetch the default log message

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jul 25, 2010, 00:58 UTC
Message-ID
<20100725005808.GC18420@burratino>
In-Reply-To
<20100725005443.GA18370@burratino>

The details of how the default message template is grabbed from MERGE_MSG will be irrelevant to most people reading the commit preparation code.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 builtin/commit.c |   94 +++++++++++++++++++++++++++++++----------------------
 1 files changed, 55 insertions(+), 39 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index a78dbd8..6774180 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -549,62 +549,78 @@ static int ends_rfc2822_footer(struct strbuf *sb)
 	return 1;
 }
 
-static int prepare_to_commit(const char *index_file, const char *prefix,
-			     struct wt_status *s)
+/*
+ * Return value is the "source" argument for hooks/prepare-commit-msg.
+ */
+static const char *get_template_message(struct strbuf *sb,
+					const char **hook_arg2)
 {
 	struct stat statbuf;
-	int commitable, saved_color_setting;
-	struct strbuf sb = STRBUF_INIT;
-	char *buffer;
-	FILE *fp;
-	const char *hook_arg1 = NULL;
-	const char *hook_arg2 = NULL;
-	int ident_shown = 0;
-
-	if (!no_verify && run_hook(index_file, "pre-commit", NULL))
-		return 0;
-
 	if (message.len) {
-		strbuf_addbuf(&sb, &message);
-		hook_arg1 = "message";
-	} else if (logfile && !strcmp(logfile, "-")) {
+		strbuf_addbuf(sb, &message);
+		return "message";
+	}
+	if (logfile && !strcmp(logfile, "-")) {
 		if (isatty(0))
 			fprintf(stderr, "(reading log message from standard input)\n");
-		if (strbuf_read(&sb, 0, 0) < 0)
+		if (strbuf_read(sb, 0, 0) < 0)
 			die_errno("could not read log from standard input");
-		hook_arg1 = "message";
-	} else if (logfile) {
-		if (strbuf_read_file(&sb, logfile, 0) < 0)
-			die_errno("could not read log file '%s'",
-				  logfile);
-		hook_arg1 = "message";
-	} else if (use_message) {
-		buffer = strstr(use_message_buffer, "\n\n");
+		return "message";
+	}
+	if (logfile) {
+		if (strbuf_read_file(sb, logfile, 0) < 0)
+			die_errno("could not read log file '%s'", logfile);
+		return "message";
+	}
+	if (use_message) {
+		char *buffer = strstr(use_message_buffer, "\n\n");
 		if (!buffer || buffer[2] == '\0')
 			die("commit has empty message");
-		strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
-		hook_arg1 = "commit";
-		hook_arg2 = use_message;
-	} else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
-		if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
+		strbuf_add(sb, buffer + 2, strlen(buffer + 2));
+		*hook_arg2 = use_message;
+		return "commit";
+	}
+	if (!stat(git_path("MERGE_MSG"), &statbuf)) {
+		if (strbuf_read_file(sb, git_path("MERGE_MSG"), 0) < 0)
 			die_errno("could not read MERGE_MSG");
-		hook_arg1 = "merge";
-	} else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
-		if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
+		return "merge";
+	}
+	if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
+		if (strbuf_read_file(sb, git_path("SQUASH_MSG"), 0) < 0)
 			die_errno("could not read SQUASH_MSG");
-		hook_arg1 = "squash";
-	} else if (template_file && !stat(template_file, &statbuf)) {
-		if (strbuf_read_file(&sb, template_file, 0) < 0)
+		return "squash";
+	}
+	if (template_file && !stat(template_file, &statbuf)) {
+		if (strbuf_read_file(sb, template_file, 0) < 0)
 			die_errno("could not read '%s'", template_file);
-		hook_arg1 = "template";
+		return "template";
 	}
 
 	/*
 	 * This final case does not modify the template message,
 	 * it just sets the argument to the prepare-commit-msg hook.
 	 */
-	else if (in_merge)
-		hook_arg1 = "merge";
+	if (in_merge)
+		return "merge";
+
+	return NULL;
+}
+
+
+static int prepare_to_commit(const char *index_file, const char *prefix,
+			     struct wt_status *s)
+{
+	int commitable, saved_color_setting;
+	struct strbuf sb = STRBUF_INIT;
+	FILE *fp;
+	const char *hook_arg1 = NULL;
+	const char *hook_arg2 = NULL;
+	int ident_shown = 0;
+
+	if (!no_verify && run_hook(index_file, "pre-commit", NULL))
+		return 0;
+
+	hook_arg1 = get_template_message(&sb, &hook_arg2);
 
 	fp = fopen(git_path(commit_editmsg), "w");
 	if (fp == NULL)
-- 
1.7.2.9.ge3789.dirty
Previous: Jonathan NiederNext: Jonathan Nieder
Message 4 of 38 in “commit: more focused advice in the no-changes-staged case”
  1. 0/9 commit: more focused advice in the no-changes-staged caseJonathan Nieder, Jul 25, 2010
  2. 1/9 wt-status: split wt_status_print into digestible piecesJonathan Nieder, Jul 25, 2010
  3. 2/9 wt-status: split off a function for printing submodule summaryJonathan Nieder, Jul 25, 2010
  4. 3/9 commit: split off a function to fetch the default log messageJonathan Nieder, Jul 25, 2010
  5. 4/9 commit: split commit -s handling into its own functionJonathan Nieder, Jul 25, 2010
  6. 5/9 commit: split off the piece that writes statusJonathan Nieder, Jul 25, 2010
  7. 6/9 t7508 (status): modernize styleJonathan Nieder, Jul 25, 2010
  8. Ævar Arnfjörð BjarmasonJul 25, 2010
  9. 7/9 commit: give empty-commit avoidance code its own functionJonathan Nieder, Jul 25, 2010
  10. 8/9 commit --dry-run: give advice on empty amendJonathan Nieder, Jul 25, 2010
  11. 9/9 commit: suppress status summary when no changes stagedJonathan Nieder, Jul 25, 2010
  12. Thomas RastAug 11, 2010
  13. Jonathan NiederAug 11, 2010
  14. t6040 (branch tracking): check “status” instead of “commit”Jonathan Nieder, Aug 11, 2010
  15. Ævar Arnfjörð BjarmasonAug 12, 2010
  16. Ævar Arnfjörð BjarmasonAug 11, 2010
  17. Jonathan NiederAug 11, 2010
  18. Ævar Arnfjörð BjarmasonAug 12, 2010
  19. Jonathan NiederAug 12, 2010
  20. Ævar Arnfjörð BjarmasonJul 25, 2010
  21. Thomas RastJul 25, 2010
  22. Making error messages stand out (Re: [RFC/PATCH 0/9] commit: more focused advice in the no-changes-staged case)Jonathan Nieder, Jul 29, 2010
  23. Sverre RabbelierJul 30, 2010
  24. 0/4 Re: Making error messages stand outJonathan Nieder, Aug 11, 2010
  25. 1/4 Eliminate “Finished cherry-pick/revert” messageJonathan Nieder, Aug 11, 2010
  26. 2/4 Introduce advise() to print hintsJonathan Nieder, Aug 11, 2010
  27. 3/4 cherry-pick/revert: Use error() for failure messageJonathan Nieder, Aug 11, 2010
  28. 4/4 cherry-pick/revert: Use advise() for hintsJonathan Nieder, Aug 11, 2010
  29. Nguyen Thai Ngoc DuyAug 11, 2010
  30. Matthieu MoyAug 11, 2010
  31. Nguyen Thai Ngoc DuyAug 11, 2010
  32. Sverre RabbelierAug 11, 2010
  33. tests: fix syntax error in "Use advise() for hints" testÆvar Arnfjörð Bjarmason, Aug 18, 2010
  34. Jonathan NiederAug 19, 2010
  35. Ævar Arnfjörð BjarmasonAug 19, 2010
  36. Raja R HarinathAug 20, 2010
  37. Ævar Arnfjörð BjarmasonAug 20, 2010
  38. Junio C HamanoAug 20, 2010

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.