[PATCH master] expand "<branch>" in format.subjectprefix

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Replace "<branch>" with the current branch name for
[format]
        subjectprefix = PATCH <branch>

A subject-prefix given on the command-line overrides the one given in
the config.

Previously this didn't work for me:
$ git config --replace-all format.subjectprefix 'PATCH <branch>'
# edit something, commit it
$ git format-patch -o ~/foo/ HEAD^..HEAD
and that patch still has "<branch>" in the subject, even when
send-email'ed.

A manual
$ git-format-patch -o ~/foo/ HEAD^..HEAD --subject-prefix="PATCH something"
works, but is not convenient since i usually want to denote which branch
the patch is against.

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@xxxxxxxxx>
---
 builtin-log.c |   43 ++++++++++++++++++++++++++++++++++++++++---
 log-tree.c    |    6 +++---
 revision.h    |    2 +-
 3 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/builtin-log.c b/builtin-log.c
index 41b6df4..d7624bf 100644
--- a/builtin-log.c
+++ b/builtin-log.c
@@ -31,6 +31,43 @@ static const char * const builtin_log_usage =
 	"git log [<options>] [<since>..<until>] [[--] <path>...]\n"
 	"   or: git show [options] <object>...";
 
+static const char* subst_subject_prefix(void)
+{
+	char *ch;
+
+	/* now expand <branch> */
+	if ((ch = strstr(fmt_patch_subject_prefix, "<branch>")) != NULL)
+	{
+		/* get_or_ask_branch_name(): */
+		struct branch *current_branch;
+		size_t len;
+		int a, b = ch - fmt_patch_subject_prefix;
+		char *branch_name, *new_subject;
+
+		current_branch = branch_get(NULL);
+		if (!current_branch || !current_branch->merge
+				|| !current_branch->merge[0]
+				|| !current_branch->merge[0]->dst) {
+			branch_name = "(no branch)";
+		} else {
+			branch_name = current_branch->merge[0]->dst;
+			ch = strrchr(branch_name, '/');
+			if (ch)
+				branch_name = ++ch;
+		}
+		len = strlen(branch_name);
+		a = strlen(fmt_patch_subject_prefix) - b - strlen("<branch>");
+		new_subject = xcalloc(1, 1 + b + len + a);
+		memcpy(new_subject, fmt_patch_subject_prefix, b);
+		memcpy(new_subject + b, branch_name, len);
+		memcpy(new_subject + b + len,
+			fmt_patch_subject_prefix + b + strlen("<branch>"),
+			a);
+		*&fmt_patch_subject_prefix = new_subject;
+	}
+	return fmt_patch_subject_prefix;
+}
+
 static void cmd_log_init(int argc, const char **argv, const char *prefix,
 		      struct rev_info *rev)
 {
@@ -44,7 +81,7 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
 	rev->verbose_header = 1;
 	DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
 	rev->show_root_diff = default_show_root;
-	rev->subject_prefix = fmt_patch_subject_prefix;
+	rev->subject_prefix = &subst_subject_prefix;
 	DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
 
 	if (default_date_mode)
@@ -795,7 +832,7 @@ static int subject_prefix_callback(const struct option *opt, const char *arg,
 			    int unset)
 {
 	subject_prefix = 1;
-	((struct rev_info *)opt->value)->subject_prefix = arg;
+	*&fmt_patch_subject_prefix = arg;
 	return 0;
 }
 
@@ -962,7 +999,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	rev.ignore_merges = 1;
 	DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
 
-	rev.subject_prefix = fmt_patch_subject_prefix;
+	rev.subject_prefix = &subst_subject_prefix;
 
 	if (default_attach) {
 		rev.mime_boundary = default_attach;
diff --git a/log-tree.c b/log-tree.c
index 0fdf159..e29f009 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -203,15 +203,15 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 		static char buffer[64];
 		snprintf(buffer, sizeof(buffer),
 			 "Subject: [%s %0*d/%d] ",
-			 opt->subject_prefix,
+			 opt->subject_prefix(),
 			 digits_in_number(opt->total),
 			 opt->nr, opt->total);
 		subject = buffer;
-	} else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
+	} else if (opt->total == 0 && opt->subject_prefix()) {
 		static char buffer[256];
 		snprintf(buffer, sizeof(buffer),
 			 "Subject: [%s] ",
-			 opt->subject_prefix);
+			 opt->subject_prefix());
 		subject = buffer;
 	} else {
 		subject = "Subject: ";
diff --git a/revision.h b/revision.h
index d368003..ca45c57 100644
--- a/revision.h
+++ b/revision.h
@@ -100,7 +100,7 @@ struct rev_info {
 	const char	*add_signoff;
 	const char	*extra_headers;
 	const char	*log_reencode;
-	const char	*subject_prefix;
+	const char	*(*subject_prefix)(void);
 	int		no_inline;
 	int		show_log_size;
 
-- 
1.6.6.103.gbcea0.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]