expand "<branch>" in format.subjectprefix
PATCH 1/1 expand "<branch>" in format.subjectprefix
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.
--- builtin-log.c | 43 ++++++++++++++++++++++++++++++++++++++++--- log-tree.c | 6 +++--- revision.h | 2 +- 3 files changed, 44 insertions(+), 7 deletions(-)
builtin-log.c +40 -3
@@ -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;
log-tree.c +3 -3
@@ -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: " ;
revision.h +1 -1
@@ -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;
Signature · 1 line
1.6.6.103.gbcea0.dirty
Quoted 6 lines · Bernhard Reutner-Fischer
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.
I don't have a big opinion on whether this feature is useful (it wouldn't be to me, but I can see workflows where it could be).
I'm not sure that "current branch" makes sense, though. format-patch is about showing commits, and the current branch is not a property of that commit. It is about where you happen to be currently. So doing:
git checkout X git format-patch Y..Z --subject-prefix "PATCH <branch>"
shows "X" which is not really useful information. Something like git log's "--source" would be more useful; it shows the command-line ref that was used to reach a given commit.
Also, please don't introduce a new substitution syntax. We already have code to do %-expansion. In fact, if you are going to do something like this, maybe it would be best as two patches:
1. Support '%B' as a user-format expansion for the --source branch.
2. Support user-format expansions in the subject prefix.
But I don't know if people would find any of the other substitutions useful in the subject line.
-Peff