@matheusbsbs / matheus-bagliano / commits / 6df514af9d
Fork 0

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 <bebarino@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephen Boyd committed Mar 22, 2009 at 19:14 UTC 6df514af9dfb44f104baa9b581e91de22af89b8d
1 file changed +33 -65
builtin-log.c
+33 -65
@@ -419,12 +419,6 @@ int cmd_log(int argc, const char **argv, const char *prefix)
419 /* format-patch */
420 #define FORMAT_PATCH_NAME_MAX 64
421
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 -
422 static const char *fmt_patch_suffix = ".patch";
423 static int numbered = 0;
424 static int auto_number = 1;
@@ -519,61 +513,33 @@ static int git_format_config(const char *var, const char *value, void *cb)
513 }
514
515
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)
518 {
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;
521
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);
535 }
569 - return filename;
536 }
537
538 static FILE *realstdout = NULL;
539 static const char *output_directory = NULL;
540 static int outdir_offset;
541
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)
543 {
544 char filename[PATH_MAX];
545 int len = 0;
@@ -589,14 +555,7 @@ static int reopen_stdout(const char *oneline, int nr, struct rev_info *rev)
555 filename[len++] = '/';
556 }
557
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);
559
560 if (!DIFF_OPT_TST(&rev->diffopt, QUIET))
561 fprintf(realstdout, "%s\n", filename + outdir_offset);
@@ -684,12 +643,17 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
643 const char *encoding = "utf-8";
644 struct diff_options opts;
645 int need_8bit_cte = 0;
646 + char filename[PATH_MAX];
647
648 if (rev->commit_format != CMIT_FMT_EMAIL)
649 die("Cover letter needs email format");
650
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))
657 return;
658
659 head_sha1 = sha1_to_hex(head->object.sha1);
@@ -802,6 +766,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
766 struct patch_ids ids;
767 char *add_signoff = NULL;
768 struct strbuf buf = STRBUF_INIT;
769 + struct strbuf patch_filename = STRBUF_INIT;
770
771 git_config(git_format_config, NULL);
772 init_revisions(&rev, prefix);
@@ -1104,10 +1069,12 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1069 }
1070 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1071 }
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))
1076 die("Failed to create output files");
1077 + strbuf_setlen(&patch_filename, 0);
1078 shown = log_tree_commit(&rev, commit);
1079 free(commit->buffer);
1080 commit->buffer = NULL;
@@ -1131,6 +1098,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1098 if (!use_stdout)
1099 fclose(stdout);
1100 }
1101 + strbuf_release(&patch_filename);
1102 free(list);
1103 if (ignore_if_in_upstream)
1104 free_patch_ids(&ids);