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

[RFC PATCH 1/1] Add branchname in commit header

From
ABArnaud Bertrand <arnaud.bertrand@abalgo.com>
Date
Dec 29, 2019, 22:26 UTC
Message-ID
<20191229222633.23815-2-arnaud.bertrand@abalgo.com>
In-Reply-To
<20191229222633.23815-1-arnaud.bertrand@abalgo.com>
From: Arnaud Bertrand <xda@abalgo.com>

Add the branchname in the commit header before the commit message the following line is added:

branch <branchname>

where <branchname> comes from the function resolve_ref_unsafe("HEAD",...) without the prefix refs/heads/ A placeholder is added to the pretty format "%Xb" to print the branch information, X if for "extra-header" and can be use in the future for new features b is of course for "branch"

the %Xb returns an empty string when branchname information is not found
---
 Documentation/pretty-formats.txt |  1 +
 commit.c                         | 11 +++++++++++
 pretty.c                         | 15 +++++++++++++++
 3 files changed, 27 insertions(+)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 1a7212ce5a..bd52908f53 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -241,6 +241,7 @@ endif::git-rev-list[]
 '%gE':: reflog identity email (respecting .mailmap, see
 	linkgit:git-shortlog[1] or linkgit:git-blame[1])
 '%gs':: reflog subject
+'%Xb':: branchname in which commit was done
 '%(trailers[:options])':: display the trailers of the body as
 			  interpreted by
 			  linkgit:git-interpret-trailers[1]. The
diff --git a/commit.c b/commit.c
index 434ec030d6..f64a0698be 100644
--- a/commit.c
+++ b/commit.c
@@ -1425,6 +1425,9 @@ int commit_tree_extended(const char *msg, size_t msg_len,
 	int result;
 	int encoding_is_utf8;
 	struct strbuf buffer;
+	const char *branch = "Unknown";
+	int flags;
+	const char *lbranch =resolve_ref_unsafe("HEAD",0,NULL,&flags);
 
 	assert_oid_type(tree, OBJ_TREE);
 
@@ -1453,6 +1456,14 @@ 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);
 
diff --git a/pretty.c b/pretty.c
index 305e903192..5961c39398 100644
--- a/pretty.c
+++ b/pretty.c
@@ -804,6 +804,7 @@ struct format_commit_context {
 
 	/* The following ones are relative to the result struct strbuf. */
 	size_t wrap_start;
+	char *branch;
 };
 
 static void parse_commit_header(struct format_commit_context *context)
@@ -1367,6 +1368,20 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 		return 1;
 	}
 
+
+	/* Now add extra header info */
+	if (placeholder[0] == 'X') {
+		switch (placeholder[1]) {
+		case 'b': /* branch ... */
+			c->branch = get_header(msg,"branch");
+			if (c->branch)
+				strbuf_addstr(sb, c->branch);
+			free(c->branch);
+			return 2;
+		}
+	}
+
+
 	/* Now we need to parse the commit message. */
 	if (!c->commit_message_parsed)
 		parse_commit_message(c);
-- 
2.25.0.rc0.7.g17b02bf28a
Previous: Arnaud BertrandNext: brian m. carlson
Message 2 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.