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

[PATCH 3/3] Fix commit-msg hook to allow editing

From
Wincent Colaiuta <win@wincent.com>
Date
Dec 8, 2007, 11:38 UTC
Message-ID
<1197113889-16243-4-git-send-email-win@wincent.com>
In-Reply-To
<1197113889-16243-3-git-send-email-win@wincent.com>

The old git-commit.sh script allowed the commit-msg hook to not only prevent a commit from proceding, but also to edit the commit message on the fly and allow it to proceed. So here we teach builtin-commit to do the same.

Signed-off-by: Wincent Colaiuta <win@wincent.com>
---
 builtin-commit.c |   16 ++++++++++------
 1 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/builtin-commit.c b/builtin-commit.c
index df9377e..a6223d2 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -787,14 +787,18 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		const char *env[2] = { index, NULL };
 		snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
 		launch_editor(git_path(commit_editmsg), &sb, env);
-	} else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
-		rollback_index_files();
-		die("could not read commit message");
 	}
-	if (!no_verify &&
-	    run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
+	if (!no_verify) {
+		if (run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
+			rollback_index_files();
+			exit(1);
+		}
+		strbuf_setlen(&sb, header_len);
+	}
+	if ((no_edit || !no_verify) &&
+	    strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
 		rollback_index_files();
-		exit(1);
+		die("could not read commit message");
 	}
 
 	/* Truncate the message just before the diff, if any. */
-- 
1.5.3.7.1115.gaa595
Previous: Wincent ColaiutaNext: Junio C Hamano
Message 4 of 8 in “builtin-commit fixes”
  1. 0/3 builtin-commit fixesWincent Colaiuta, Dec 8, 2007
  2. 1/3 Allow --no-verify to bypass commit-msg hookWincent Colaiuta, Dec 8, 2007
  3. 2/3 Documentation: fix --no-verify documentation for "git commit"Wincent Colaiuta, Dec 8, 2007
  4. 3/3 Fix commit-msg hook to allow editingWincent Colaiuta, Dec 8, 2007
  5. Junio C HamanoDec 9, 2007
  6. Wincent ColaiutaDec 9, 2007
  7. 4/4 Add tests for pre-commit and commit-msg hooksWincent Colaiuta, Dec 8, 2007
  8. Wincent ColaiutaDec 8, 2007

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.