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

[PATCHv2 3/3] commit: Show committer if different from the parent and automatic

From
SBSanti Béjar <sbejar@gmail.com>
Date
May 1, 2008, 12:09 UTC
Message-ID
<1209643775-2362-4-git-send-email-sbejar@gmail.com>
In-Reply-To
<1209643775-2362-1-git-send-email-sbejar@gmail.com>

To remind the committer ident in case it is not what you want (taken from the gecos field, want to use different ident in different repositories).

[sb: thanks to Jeff King for the enhancement and fix to this patch]
Signed-off-by: Santi Béjar <sbejar@gmail.com>
---
 builtin-commit.c  |   42 +++++++++++++++++++++++++++++++++++++++---
 cache.h           |    1 +
 config.c          |    4 ++++
 environment.c     |    1 +
 t/t7502-commit.sh |   12 ++++++++++++
 5 files changed, 57 insertions(+), 3 deletions(-)
diff --git a/builtin-commit.c b/builtin-commit.c
index 057749b..3501ba7 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -437,6 +437,29 @@ static void determine_author_info()
 	author_date = date;
 }
 
+const char *get_parent_ident()
+{
+	unsigned char sha1[20];
+	struct commit *commit;
+	const char *a, *lb, *rb, *eol;
+
+
+	get_sha1("HEAD", sha1);
+	commit = lookup_commit_reference(sha1);
+	if (!commit)
+		return NULL;
+
+	a = strstr(commit->buffer, "\ncommitter ");
+
+	lb = strstr(a + 11, " <");
+	rb = strstr(a + 11, "> ");
+	eol = strchr(a + 11, '\n');
+	if (!lb || !rb || !eol)
+		return NULL;
+
+	return xstrndup(a + 11, rb + 1 - (a + 11));
+}
+
 static int prepare_to_commit(const char *index_file, const char *prefix)
 {
 	struct stat statbuf;
@@ -448,6 +471,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
 	const char *hook_arg2 = NULL;
 	const char *author_ident;
 	const char *committer_ident;
+	const char *parent_ident;
+	int showed_ident = 0;
 
 	if (!no_verify && run_hook(index_file, "pre-commit", NULL))
 		return 0;
@@ -558,11 +583,22 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
 					 getenv("GIT_COMMITTER_EMAIL")));
 		if (strcmp(author_ident, committer_ident))
 			fprintf(fp,
-				"#\n"
-				"# Author:    %s\n"
-				"#\n",
+				"%s"
+				"# Author:    %s\n",
+				showed_ident++ ? "" : "#\n",
 				fmt_name(author_name, author_email));
 
+		parent_ident = get_parent_ident();
+		if (!user_explicit &&
+		    (!parent_ident || strcmp(parent_ident, committer_ident)))
+			fprintf(fp,
+				"%s# Committer: %s\n",
+				showed_ident++ ? "" : "#\n",
+				committer_ident);
+
+		if (showed_ident)
+			fprintf(fp, "#\n");
+
 		saved_color_setting = wt_status_use_color;
 		wt_status_use_color = 0;
 		commitable = run_status(fp, index_file, prefix, 1);
diff --git a/cache.h b/cache.h
index 3fcc283..941441a 100644
--- a/cache.h
+++ b/cache.h
@@ -718,6 +718,7 @@ extern int config_error_nonbool(const char *);
 #define MAX_GITNAME (1000)
 extern char git_default_email[MAX_GITNAME];
 extern char git_default_name[MAX_GITNAME];
+extern int user_explicit;
 
 extern const char *git_commit_encoding;
 extern const char *git_log_output_encoding;
diff --git a/config.c b/config.c
index b0ada51..e39df5b 100644
--- a/config.c
+++ b/config.c
@@ -443,6 +443,8 @@ int git_default_config(const char *var, const char *value)
 		if (!value)
 			return config_error_nonbool(var);
 		strlcpy(git_default_name, value, sizeof(git_default_name));
+		if (!git_default_email[0])
+			user_explicit = 1;
 		return 0;
 	}
 
@@ -450,6 +452,8 @@ int git_default_config(const char *var, const char *value)
 		if (!value)
 			return config_error_nonbool(var);
 		strlcpy(git_default_email, value, sizeof(git_default_email));
+		if (!git_default_name[0])
+			user_explicit = 1;
 		return 0;
 	}
 
diff --git a/environment.c b/environment.c
index 6739a3f..b941971 100644
--- a/environment.c
+++ b/environment.c
@@ -11,6 +11,7 @@
 
 char git_default_email[MAX_GITNAME];
 char git_default_name[MAX_GITNAME];
+int user_explicit = 0;
 int trust_executable_bit = 1;
 int quote_path_fully = 1;
 int has_symlinks = 1;
diff --git a/t/t7502-commit.sh b/t/t7502-commit.sh
index 11de910..b1509a0 100755
--- a/t/t7502-commit.sh
+++ b/t/t7502-commit.sh
@@ -165,6 +165,18 @@ test_expect_success 'author different from committer' '
 	test_cmp expect actual
 '
 
+GIT_COMMITTER_NAME="C. O. Mitter"
+export GIT_COMMITTER_NAME
+echo "# Committer: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" >> expect
+
+test_expect_success 'committer different from the parent committer' '
+
+	echo >>negative &&
+	git commit -e -m "sample"
+	head -n 7 .git/COMMIT_EDITMSG >actual &&
+	test_cmp expect actual
+'
+
 pwd=`pwd`
 cat >> .git/FAKE_EDITOR << EOF
 #! /bin/sh
-- 
1.5.5.1.102.gfcc7d.dirty
Previous: Santi BéjarNext: Santi Béjar
Message 4 of 5 in “[PATCHv2 0/3] Show author and/or committer in some cases”
  1. Santi BéjarMay 1, 2008
  2. 1/3 Preparation to call determine_author_info from prepare_to_commitSanti Béjar, May 1, 2008
  3. 2/3 commit: Show author if different from committerSanti Béjar, May 1, 2008
  4. 3/3 commit: Show committer if different from the parent and automaticSanti Béjar, May 1, 2008
  5. 1/3 Preparation to call determine_author_info from prepare_to_commitSanti Béjar, May 1, 2008

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.