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

[PATCH] builtin-commit: Include the diff in the commit message when verbose.

From
Kristian Høgsberg <krh@redhat.com>
Date
Nov 21, 2007, 19:10 UTC
Message-ID
<1195672234-3919-1-git-send-email-krh@redhat.com>

run_diff_index() and the entire diff machinery is hard coded to output to stdout, so just redirect that and restore it when done.

Signed-off-by: Kristian Høgsberg <krh@redhat.com>
---
 builtin-commit.c |   24 +++++++++++++++++++++++-
 1 files changed, 23 insertions(+), 1 deletions(-)
diff --git a/builtin-commit.c b/builtin-commit.c
index 446a252..d5b8479 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -277,6 +277,7 @@ static char *prepare_index(const char **files, const char *prefix)
 static int run_status(FILE *fp, const char *index_file, const char *prefix)
 {
 	struct wt_status s;
+	int saved_stdout;
 
 	wt_status_prepare(&s);
 	s.prefix = prefix;
@@ -290,8 +291,23 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix)
 	s.index_file = index_file;
 	s.fp = fp;
 
+	fflush(fp);
+
+	/* Sigh, the entire diff machinery is hardcoded to output to
+	 * stdout.  Do the dup-dance...*/
+	saved_stdout = dup(STDOUT_FILENO);
+	if (saved_stdout < 0 ||	dup2(fileno(fp), STDOUT_FILENO) < 0)
+		die("couldn't redirect stdout\n");
+
 	wt_status_print(&s);
 
+	fflush(fp);
+	fflush(stdout);
+
+	if (dup2(saved_stdout, STDOUT_FILENO) < 0)
+		die("couldn't restore stdout\n");
+	close(saved_stdout);
+
 	return s.commitable;
 }
 
@@ -661,7 +677,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	int header_len;
 	struct strbuf sb;
 	const char *index_file, *reflog_msg;
-	char *nl;
+	char *nl, *p;
 	unsigned char commit_sha1[20];
 	struct ref_lock *ref_lock;
 
@@ -757,6 +773,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		rollback_index_files();
 		exit(1);
 	}
+
+	/* Truncate the message just before the diff, if any. */
+	p = strstr(sb.buf, "\ndiff --git a/");
+	if (p != NULL)
+		strbuf_setlen(&sb, p - sb.buf);
+
 	stripspace(&sb, 1);
 	if (sb.len < header_len || message_is_empty(&sb, header_len)) {
 		rollback_index_files();
-- 
1.5.3.4.206.g58ba4
Message 1 of 1 in “builtin-commit: Include the diff in the commit message when verbose.”
  1. builtin-commit: Include the diff in the commit message when verbose.Kristian Høgsberg, Nov 21, 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.