diff mbox series

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

Message ID 20191230163256.14749-3-xda@abalgo.com (mailing list archive)
State New, archived
Headers show
Series *** Add branchname in commit when core.branchnameincommit is set *** | expand

Commit Message

Arnaud Bertrand Dec. 30, 2019, 4:32 p.m. UTC
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 mbox series

Patch

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;