Skip to content

Commit 6df514a

Browse files
bebarinogitster
authored andcommitted
format-patch: construct patch filename in one function
reopen_stdout() usually takes the oneline subject of a commit, appends the patch suffix, prepends the output directory (if any) and then reopens stdout as the resulting file. Now the patch filename (the oneline subject and the patch suffix) is created in get_patch_filename() and passed to reopen_stdout() which prepends the output directory and reopens stdout as that file. The original function to get the oneline description, get_oneline_for_filename(), has been renamed to get_patch_filename() to reflect its new functionality. Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 46d164b commit 6df514a

File tree

1 file changed

+33
-65
lines changed

1 file changed

+33
-65
lines changed

builtin-log.c

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,6 @@ int cmd_log(int argc, const char **argv, const char *prefix)
419419
/* format-patch */
420420
#define FORMAT_PATCH_NAME_MAX 64
421421

422-
static int istitlechar(char c)
423-
{
424-
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
425-
(c >= '0' && c <= '9') || c == '.' || c == '_';
426-
}
427-
428422
static const char *fmt_patch_suffix = ".patch";
429423
static int numbered = 0;
430424
static int auto_number = 1;
@@ -519,61 +513,33 @@ static int git_format_config(const char *var, const char *value, void *cb)
519513
}
520514

521515

522-
static const char *get_oneline_for_filename(struct commit *commit,
523-
int keep_subject)
516+
static void get_patch_filename(struct commit *commit, int nr,
517+
const char *suffix, struct strbuf *buf)
524518
{
525-
static char filename[PATH_MAX];
526-
char *sol;
527-
int len = 0;
528-
int suffix_len = strlen(fmt_patch_suffix) + 1;
529-
530-
sol = strstr(commit->buffer, "\n\n");
531-
if (!sol)
532-
filename[0] = '\0';
533-
else {
534-
int j, space = 0;
535-
536-
sol += 2;
537-
/* strip [PATCH] or [PATCH blabla] */
538-
if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
539-
char *eos = strchr(sol + 6, ']');
540-
if (eos) {
541-
while (isspace(*eos))
542-
eos++;
543-
sol = eos;
544-
}
545-
}
519+
int suffix_len = strlen(suffix) + 1;
520+
int start_len = buf->len;
546521

547-
for (j = 0;
548-
j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
549-
len < sizeof(filename) - suffix_len &&
550-
sol[j] && sol[j] != '\n';
551-
j++) {
552-
if (istitlechar(sol[j])) {
553-
if (space) {
554-
filename[len++] = '-';
555-
space = 0;
556-
}
557-
filename[len++] = sol[j];
558-
if (sol[j] == '.')
559-
while (sol[j + 1] == '.')
560-
j++;
561-
} else
562-
space = 1;
563-
}
564-
while (filename[len - 1] == '.'
565-
|| filename[len - 1] == '-')
566-
len--;
567-
filename[len] = '\0';
522+
strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
523+
if (commit) {
524+
format_commit_message(commit, "%f", buf, DATE_NORMAL);
525+
/*
526+
* Replace characters at the end with the suffix if the
527+
* filename is too long
528+
*/
529+
if (buf->len + suffix_len > FORMAT_PATCH_NAME_MAX + start_len)
530+
strbuf_splice(buf,
531+
start_len + FORMAT_PATCH_NAME_MAX - suffix_len,
532+
suffix_len, suffix, suffix_len);
533+
else
534+
strbuf_addstr(buf, suffix);
568535
}
569-
return filename;
570536
}
571537

572538
static FILE *realstdout = NULL;
573539
static const char *output_directory = NULL;
574540
static int outdir_offset;
575541

576-
static int reopen_stdout(const char *oneline, int nr, struct rev_info *rev)
542+
static int reopen_stdout(const char *oneline, struct rev_info *rev)
577543
{
578544
char filename[PATH_MAX];
579545
int len = 0;
@@ -589,14 +555,7 @@ static int reopen_stdout(const char *oneline, int nr, struct rev_info *rev)
589555
filename[len++] = '/';
590556
}
591557

592-
if (!oneline)
593-
len += sprintf(filename + len, "%d", nr);
594-
else {
595-
len += sprintf(filename + len, "%04d-", nr);
596-
len += snprintf(filename + len, sizeof(filename) - len - 1
597-
- suffix_len, "%s", oneline);
598-
strcpy(filename + len, fmt_patch_suffix);
599-
}
558+
strncpy(filename + len, oneline, PATH_MAX - len);
600559

601560
if (!DIFF_OPT_TST(&rev->diffopt, QUIET))
602561
fprintf(realstdout, "%s\n", filename + outdir_offset);
@@ -684,12 +643,17 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
684643
const char *encoding = "utf-8";
685644
struct diff_options opts;
686645
int need_8bit_cte = 0;
646+
char filename[PATH_MAX];
687647

688648
if (rev->commit_format != CMIT_FMT_EMAIL)
689649
die("Cover letter needs email format");
690650

691-
if (!use_stdout && reopen_stdout(numbered_files ?
692-
NULL : "cover-letter", 0, rev))
651+
if (numbered_files)
652+
sprintf(filename, "0");
653+
else
654+
sprintf(filename, "%04d-cover-letter%s", 0, fmt_patch_suffix);
655+
656+
if (!use_stdout && reopen_stdout(filename, rev))
693657
return;
694658

695659
head_sha1 = sha1_to_hex(head->object.sha1);
@@ -802,6 +766,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
802766
struct patch_ids ids;
803767
char *add_signoff = NULL;
804768
struct strbuf buf = STRBUF_INIT;
769+
struct strbuf patch_filename = STRBUF_INIT;
805770

806771
git_config(git_format_config, NULL);
807772
init_revisions(&rev, prefix);
@@ -1104,10 +1069,12 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
11041069
}
11051070
gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
11061071
}
1107-
if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1108-
get_oneline_for_filename(commit, keep_subject),
1109-
rev.nr, &rev))
1072+
1073+
get_patch_filename(numbered_files ? NULL : commit, rev.nr,
1074+
fmt_patch_suffix, &patch_filename);
1075+
if (!use_stdout && reopen_stdout(patch_filename.buf, &rev))
11101076
die("Failed to create output files");
1077+
strbuf_setlen(&patch_filename, 0);
11111078
shown = log_tree_commit(&rev, commit);
11121079
free(commit->buffer);
11131080
commit->buffer = NULL;
@@ -1131,6 +1098,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
11311098
if (!use_stdout)
11321099
fclose(stdout);
11331100
}
1101+
strbuf_release(&patch_filename);
11341102
free(list);
11351103
if (ignore_if_in_upstream)
11361104
free_patch_ids(&ids);

0 commit comments

Comments
 (0)