{"thread":{"id":"22203","subject":"[PATCH master] expand \"<branch>\" in format.subjectprefix","startedAt":"2010-01-13T17:16:28Z","lastAt":"2010-01-13T20:29:31Z","messageCount":2,"participants":["Bernhard Reutner-Fischer","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"131492","messageId":"1263402988-925-1-git-send-email-rep.dot.nop@gmail.com","threadId":"22203","inReplyTo":null,"subject":"[PATCH master] expand \"<branch>\" in format.subjectprefix","fromName":"Bernhard Reutner-Fischer","fromEmail":"rep.dot.nop@gmail.com","sentAt":"2010-01-13T17:16:28Z","receivedAt":"2010-01-13T17:16:28Z","isPatch":true,"sender":{"key":"rep.dot.nop@gmail.com","avatar":null},"body":"Replace \"<branch>\" with the current branch name for\n[format]\n        subjectprefix = PATCH <branch>\n\nA subject-prefix given on the command-line overrides the one given in\nthe config.\n\nPreviously this didn't work for me:\n$ git config --replace-all format.subjectprefix 'PATCH <branch>'\n# edit something, commit it\n$ git format-patch -o ~/foo/ HEAD^..HEAD\nand that patch still has \"<branch>\" in the subject, even when\nsend-email'ed.\n\nA manual\n$ git-format-patch -o ~/foo/ HEAD^..HEAD --subject-prefix=\"PATCH something\"\nworks, but is not convenient since i usually want to denote which branch\nthe patch is against.\n\nSigned-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>\n---\n builtin-log.c |   43 ++++++++++++++++++++++++++++++++++++++++---\n log-tree.c    |    6 +++---\n revision.h    |    2 +-\n 3 files changed, 44 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin-log.c b/builtin-log.c\nindex 41b6df4..d7624bf 100644\n--- a/builtin-log.c\n+++ b/builtin-log.c\n@@ -31,6 +31,43 @@ static const char * const builtin_log_usage =\n \t\"git log [<options>] [<since>..<until>] [[--] <path>...]\\n\"\n \t\"   or: git show [options] <object>...\";\n \n+static const char* subst_subject_prefix(void)\n+{\n+\tchar *ch;\n+\n+\t/* now expand <branch> */\n+\tif ((ch = strstr(fmt_patch_subject_prefix, \"<branch>\")) != NULL)\n+\t{\n+\t\t/* get_or_ask_branch_name(): */\n+\t\tstruct branch *current_branch;\n+\t\tsize_t len;\n+\t\tint a, b = ch - fmt_patch_subject_prefix;\n+\t\tchar *branch_name, *new_subject;\n+\n+\t\tcurrent_branch = branch_get(NULL);\n+\t\tif (!current_branch || !current_branch->merge\n+\t\t\t\t|| !current_branch->merge[0]\n+\t\t\t\t|| !current_branch->merge[0]->dst) {\n+\t\t\tbranch_name = \"(no branch)\";\n+\t\t} else {\n+\t\t\tbranch_name = current_branch->merge[0]->dst;\n+\t\t\tch = strrchr(branch_name, '/');\n+\t\t\tif (ch)\n+\t\t\t\tbranch_name = ++ch;\n+\t\t}\n+\t\tlen = strlen(branch_name);\n+\t\ta = strlen(fmt_patch_subject_prefix) - b - strlen(\"<branch>\");\n+\t\tnew_subject = xcalloc(1, 1 + b + len + a);\n+\t\tmemcpy(new_subject, fmt_patch_subject_prefix, b);\n+\t\tmemcpy(new_subject + b, branch_name, len);\n+\t\tmemcpy(new_subject + b + len,\n+\t\t\tfmt_patch_subject_prefix + b + strlen(\"<branch>\"),\n+\t\t\ta);\n+\t\t*&fmt_patch_subject_prefix = new_subject;\n+\t}\n+\treturn fmt_patch_subject_prefix;\n+}\n+\n static void cmd_log_init(int argc, const char **argv, const char *prefix,\n \t\t      struct rev_info *rev)\n {\n@@ -44,7 +81,7 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,\n \trev->verbose_header = 1;\n \tDIFF_OPT_SET(&rev->diffopt, RECURSIVE);\n \trev->show_root_diff = default_show_root;\n-\trev->subject_prefix = fmt_patch_subject_prefix;\n+\trev->subject_prefix = &subst_subject_prefix;\n \tDIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);\n \n \tif (default_date_mode)\n@@ -795,7 +832,7 @@ static int subject_prefix_callback(const struct option *opt, const char *arg,\n \t\t\t    int unset)\n {\n \tsubject_prefix = 1;\n-\t((struct rev_info *)opt->value)->subject_prefix = arg;\n+\t*&fmt_patch_subject_prefix = arg;\n \treturn 0;\n }\n \n@@ -962,7 +999,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)\n \trev.ignore_merges = 1;\n \tDIFF_OPT_SET(&rev.diffopt, RECURSIVE);\n \n-\trev.subject_prefix = fmt_patch_subject_prefix;\n+\trev.subject_prefix = &subst_subject_prefix;\n \n \tif (default_attach) {\n \t\trev.mime_boundary = default_attach;\ndiff --git a/log-tree.c b/log-tree.c\nindex 0fdf159..e29f009 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -203,15 +203,15 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,\n \t\tstatic char buffer[64];\n \t\tsnprintf(buffer, sizeof(buffer),\n \t\t\t \"Subject: [%s %0*d/%d] \",\n-\t\t\t opt->subject_prefix,\n+\t\t\t opt->subject_prefix(),\n \t\t\t digits_in_number(opt->total),\n \t\t\t opt->nr, opt->total);\n \t\tsubject = buffer;\n-\t} else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {\n+\t} else if (opt->total == 0 && opt->subject_prefix()) {\n \t\tstatic char buffer[256];\n \t\tsnprintf(buffer, sizeof(buffer),\n \t\t\t \"Subject: [%s] \",\n-\t\t\t opt->subject_prefix);\n+\t\t\t opt->subject_prefix());\n \t\tsubject = buffer;\n \t} else {\n \t\tsubject = \"Subject: \";\ndiff --git a/revision.h b/revision.h\nindex d368003..ca45c57 100644\n--- a/revision.h\n+++ b/revision.h\n@@ -100,7 +100,7 @@ struct rev_info {\n \tconst char\t*add_signoff;\n \tconst char\t*extra_headers;\n \tconst char\t*log_reencode;\n-\tconst char\t*subject_prefix;\n+\tconst char\t*(*subject_prefix)(void);\n \tint\t\tno_inline;\n \tint\t\tshow_log_size;\n \n-- \n1.6.6.103.gbcea0.dirty\n"},{"id":"131527","messageId":"20100113202930.GC23018@coredump.intra.peff.net","threadId":"22203","inReplyTo":"1263402988-925-1-git-send-email-rep.dot.nop@gmail.com","subject":"Re: [PATCH master] expand \"<branch>\" in format.subjectprefix","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2010-01-13T20:29:31Z","receivedAt":"2010-01-13T20:29:31Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Jan 13, 2010 at 06:16:28PM +0100, Bernhard Reutner-Fischer wrote:\n\n> Replace \"<branch>\" with the current branch name for\n> [format]\n>         subjectprefix = PATCH <branch>\n> \n> A subject-prefix given on the command-line overrides the one given in\n> the config.\n\nI don't have a big opinion on whether this feature is useful (it\nwouldn't be to me, but I can see workflows where it could be).\n\nI'm not sure that \"current branch\" makes sense, though.  format-patch is\nabout showing commits, and the current branch is not a property of that\ncommit.  It is about where you happen to be currently. So doing:\n\n  git checkout X\n  git format-patch Y..Z --subject-prefix \"PATCH <branch>\"\n\nshows \"X\" which is not really useful information. Something like git\nlog's \"--source\" would be more useful; it shows the command-line ref\nthat was used to reach a given commit.\n\nAlso, please don't introduce a new substitution syntax. We already have\ncode to do %-expansion. In fact, if you are going to do something like\nthis, maybe it would be best as two patches:\n\n  1. Support '%B' as a user-format expansion for the --source branch.\n\n  2. Support user-format expansions in the subject prefix.\n\nBut I don't know if people would find any of the other substitutions\nuseful in the subject line.\n\n-Peff\n"}]}