{"thread":{"id":"18381","subject":"[PATCH 2/3] format-patch: create patch filename in get_patch_filename()","startedAt":"2009-03-18T19:51:21Z","lastAt":"2009-03-18T19:51:21Z","messageCount":1,"participants":["Stephen Boyd"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"108411","messageId":"780e0a6b0903181251l4bc1964exf91c4ae045214dc7@mail.gmail.com","threadId":"18381","inReplyTo":null,"subject":"[PATCH 2/3] format-patch: create patch filename in get_patch_filename()","fromName":"Stephen Boyd","fromEmail":"bebarino@gmail.com","sentAt":"2009-03-18T19:51:21Z","receivedAt":"2009-03-18T19:51:21Z","isPatch":true,"sender":{"key":"bebarino@gmail.com","avatar":"https://avatars.githubusercontent.com/u/38832?v=4"},"body":"reopen_stdout() used to take the oneline description of a commit, append\nthe patch suffix, prepend the output directory (if any) and then point\nstdout to that file. Now the patch filename (the oneline description and\nthe patch suffix) is created in get_patch_filename() and passed to\nreopen_stdout()\n\nSigned-off-by: Stephen Boyd <bebarino@gmail.com>\n---\n builtin-log.c |   48 ++++++++++++++++++++++++------------------------\n 1 files changed, 24 insertions(+), 24 deletions(-)\n\ndiff --git a/builtin-log.c b/builtin-log.c\nindex c72e35a..f2a687f 100644\n--- a/builtin-log.c\n+++ b/builtin-log.c\n@@ -519,21 +519,17 @@ static int git_format_config(const char *var,\nconst char *value, void *cb)\n }\n\n\n-static const char *get_oneline_for_filename(struct commit *commit,\n-\t\t\t\t\t    int keep_subject)\n+static const char *get_patch_filename(char* sol, int keep_subject, int nr)\n {\n \tstatic char filename[PATH_MAX];\n-\tchar *sol;\n \tint len = 0;\n \tint suffix_len = strlen(fmt_patch_suffix) + 1;\n\n-\tsol = strstr(commit->buffer, \"\\n\\n\");\n-\tif (!sol)\n-\t\tfilename[0] = '\\0';\n-\telse {\n+\tif (sol)\n+\t{\n \t\tint j, space = 0;\n\n-\t\tsol += 2;\n+\t\tlen += sprintf(filename + len, \"%04d-\", nr);\n \t\t/* strip [PATCH] or [PATCH blabla] */\n \t\tif (!keep_subject && !prefixcmp(sol, \"[PATCH\")) {\n \t\t\tchar *eos = strchr(sol + 6, ']');\n@@ -564,8 +560,11 @@ static const char\n*get_oneline_for_filename(struct commit *commit,\n \t\twhile (filename[len - 1] == '.'\n \t\t       || filename[len - 1] == '-')\n \t\t\tlen--;\n-\t\tfilename[len] = '\\0';\n \t}\n+\telse\n+\t\tlen += sprintf(filename + len, \"%d\", nr);\n+\tstrcpy(filename + len, fmt_patch_suffix);\n+\n \treturn filename;\n }\n\n@@ -573,7 +572,7 @@ static FILE *realstdout = NULL;\n static const char *output_directory = NULL;\n static int outdir_offset;\n\n-static int reopen_stdout(const char *oneline, int nr)\n+static int reopen_stdout(const char *oneline)\n {\n \tchar filename[PATH_MAX];\n \tint len = 0;\n@@ -588,15 +587,7 @@ static int reopen_stdout(const char *oneline, int nr)\n \t\tif (filename[len - 1] != '/')\n \t\t\tfilename[len++] = '/';\n \t}\n-\n-\tif (!oneline)\n-\t\tlen += sprintf(filename + len, \"%d\", nr);\n-\telse {\n-\t\tlen += sprintf(filename + len, \"%04d-\", nr);\n-\t\tlen += snprintf(filename + len, sizeof(filename) - len - 1\n-\t\t\t\t- suffix_len, \"%s\", oneline);\n-\t\tstrcpy(filename + len, fmt_patch_suffix);\n-\t}\n+\tstrncpy(filename+len, oneline, PATH_MAX-len);\n\n \tfprintf(realstdout, \"%s\\n\", filename + outdir_offset);\n \tif (freopen(filename, \"w\", stdout) == NULL)\n@@ -686,8 +677,9 @@ static void make_cover_letter(struct rev_info\n*rev, int use_stdout,\n \tif (rev->commit_format != CMIT_FMT_EMAIL)\n \t\tdie(\"Cover letter needs email format\");\n\n-\tif (!use_stdout && reopen_stdout(numbered_files ?\n-\t\t\t\tNULL : \"cover-letter\", 0))\n+\tif (!use_stdout && reopen_stdout(get_patch_filename(numbered_files ?\n+\t\t\t\t\t\t\tNULL : \"cover-letter\",\n+\t\t\t\t\t\t\t0, 0)))\n \t\treturn;\n\n \thead_sha1 = sha1_to_hex(head->object.sha1);\n@@ -800,6 +792,7 @@ int cmd_format_patch(int argc, const char **argv,\nconst char *prefix)\n \tstruct patch_ids ids;\n \tchar *add_signoff = NULL;\n \tstruct strbuf buf = STRBUF_INIT;\n+\tchar *sol = NULL;\n\n \tgit_config(git_format_config, NULL);\n \tinit_revisions(&rev, prefix);\n@@ -1104,9 +1097,16 @@ int cmd_format_patch(int argc, const char\n**argv, const char *prefix)\n \t\t\t}\n \t\t\tgen_message_id(&rev, sha1_to_hex(commit->object.sha1));\n \t\t}\n-\t\tif (!use_stdout && reopen_stdout(numbered_files ? NULL :\n-\t\t\t\tget_oneline_for_filename(commit, keep_subject),\n-\t\t\t\trev.nr))\n+\t\t/*\n+\t\t * We want the subject line of the commit message which starts\n+\t\t * after the first double newline occurence in the commit\n+\t\t * buffer. If we're outputting numbered files we don't need a\n+\t\t * subject line.\n+\t\t */\n+\t\tsol = numbered_files ? NULL : strstr(commit->buffer, \"\\n\\n\")+2;\n+\t\tif (!use_stdout && reopen_stdout(get_patch_filename(sol,\n+\t\t\t\t\t\t\t\tkeep_subject,\n+\t\t\t\t\t\t\t\trev.nr)))\n \t\t\tdie(\"Failed to create output files\");\n \t\tshown = log_tree_commit(&rev, commit);\n \t\tfree(commit->buffer);\n-- \n1.6.2\n"}]}