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

[RFC PATCH 2/2 v2] Add the configuration parameter core.branchnameincommit

From
ABArnaud Bertrand <xda@abalgo.com>
Date
Dec 30, 2019, 16:32 UTC
Message-ID
<20191230163256.14749-3-xda@abalgo.com>
In-Reply-To
<20191230163256.14749-1-xda@abalgo.com>
With this parameter, which is 0 by default (no change compare
to the normal behaviour) you have the possibility to activate
this feature to have the branchname in the header commit
When it exists, the branchname is accesible in the git log
with the pretty format placehoder "%Xb".
---
 cache.h       |  1 +
 commit.c      | 24 +++++++++++++++++-------
 config.c      |  5 +++++
 environment.c |  1 +
 4 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/cache.h b/cache.h
index 1554488d66..dd7188a4e4 100644
--- a/cache.h
+++ b/cache.h
@@ -949,6 +949,7 @@ void reset_shared_repository(void);
  * commands that do not want replace references to be active.
  */
 extern int read_replace_refs;
+extern int branchname_in_commit;
 extern char *git_replace_ref_base;
 
 extern int fsync_object_files;
diff --git a/commit.c b/commit.c
index f64a0698be..e63d97d308 100644
--- a/commit.c
+++ b/commit.c
@@ -1428,6 +1428,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
 	const char *branch = "Unknown";
 	int flags;
 	const char *lbranch =resolve_ref_unsafe("HEAD",0,NULL,&flags);
+	int flbranchinextra = 0;
 
 	assert_oid_type(tree, OBJ_TREE);
 
@@ -1456,21 +1457,30 @@ int commit_tree_extended(const char *msg, size_t msg_len,
 		author = git_author_info(IDENT_STRICT);
 	strbuf_addf(&buffer, "author %s\n", author);
 	strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
-	if (lbranch) {
-	   skip_prefix(lbranch,"refs/heads/",&branch);
-	   strbuf_addf(&buffer, "branch %s\n", branch);
-	}
-	else {
-	   strbuf_addf(&buffer, "branch Unknown\n");
-	}
 
 	if (!encoding_is_utf8)
 		strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
 
 	while (extra) {
+		/* when commit is reworked - e.g. amend, the banch is already
+		 * in extra-header and must not be modified
+		*/
+		if (!strcmp(extra->key,"branch"))
+			flbranchinextra=1;
 		add_extra_header(&buffer, extra);
 		extra = extra->next;
 	}
+
+	if (branchname_in_commit && !flbranchinextra) {
+		if (lbranch) {
+			skip_prefix(lbranch,"refs/heads/",&branch);
+			strbuf_addf(&buffer, "branch %s\n", branch);
+		}
+		else {
+			strbuf_addf(&buffer, "branch Unknown\n");
+		}
+	}
+
 	strbuf_addch(&buffer, '\n');
 
 	/* And add the comment */
diff --git a/config.c b/config.c
index d75f88ca0c..bec1b5c3af 100644
--- a/config.c
+++ b/config.c
@@ -1389,6 +1389,11 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
+	if (!strcmp(var, "core.branchnameincommit")) {
+		branchname_in_commit = git_config_bool(var, value);
+		return 0;
+	}
+
 	/* Add other config variables here and to Documentation/config.txt. */
 	return platform_core_config(var, value, cb);
 }
diff --git a/environment.c b/environment.c
index e72a02d0d5..1d266a91cf 100644
--- a/environment.c
+++ b/environment.c
@@ -52,6 +52,7 @@ const char *askpass_program;
 const char *excludes_file;
 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
 int read_replace_refs = 1;
+int branchname_in_commit = 0;
 char *git_replace_ref_base;
 enum eol core_eol = EOL_UNSET;
 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
-- 
2.25.0.rc0.7.g17b02bf28a
Previous: Arnaud Bertrand
Message 7 of 7 in “*** Add branchname in commit header ***”
  1. 0/1 *** Add branchname in commit header ***Arnaud Bertrand, Dec 29, 2019
  2. 1/1 Add branchname in commit headerArnaud Bertrand, Dec 29, 2019
  3. brian m. carlsonDec 30, 2019
  4. Arnaud BertrandDec 30, 2019
  5. 0/2 *** Add branchname in commit when core.branchnameincommit is set ***Arnaud Bertrand, Dec 30, 2019
  6. 1/2 Add branchname in commit headerArnaud Bertrand, Dec 30, 2019
  7. 2/2 Add the configuration parameter core.branchnameincommitArnaud Bertrand, Dec 30, 2019

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.