@@ -419,12 +419,6 @@ int cmd_log(int argc, const char **argv, const char *prefix)
419
419
/* format-patch */
420
420
#define FORMAT_PATCH_NAME_MAX 64
421
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
-
428
422
static const char * fmt_patch_suffix = ".patch" ;
429
423
static int numbered = 0 ;
430
424
static int auto_number = 1 ;
@@ -519,61 +513,33 @@ static int git_format_config(const char *var, const char *value, void *cb)
519
513
}
520
514
521
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 )
524
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 ;
546
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 );
568
535
}
569
- return filename ;
570
536
}
571
537
572
538
static FILE * realstdout = NULL ;
573
539
static const char * output_directory = NULL ;
574
540
static int outdir_offset ;
575
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 )
577
543
{
578
544
char filename [PATH_MAX ];
579
545
int len = 0 ;
@@ -589,14 +555,7 @@ static int reopen_stdout(const char *oneline, int nr, struct rev_info *rev)
589
555
filename [len ++ ] = '/' ;
590
556
}
591
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 );
600
559
601
560
if (!DIFF_OPT_TST (& rev -> diffopt , QUIET ))
602
561
fprintf (realstdout , "%s\n" , filename + outdir_offset );
@@ -684,12 +643,17 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
684
643
const char * encoding = "utf-8" ;
685
644
struct diff_options opts ;
686
645
int need_8bit_cte = 0 ;
646
+ char filename [PATH_MAX ];
687
647
688
648
if (rev -> commit_format != CMIT_FMT_EMAIL )
689
649
die ("Cover letter needs email format" );
690
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 ))
693
657
return ;
694
658
695
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)
802
766
struct patch_ids ids ;
803
767
char * add_signoff = NULL ;
804
768
struct strbuf buf = STRBUF_INIT ;
769
+ struct strbuf patch_filename = STRBUF_INIT ;
805
770
806
771
git_config (git_format_config , NULL );
807
772
init_revisions (& rev , prefix );
@@ -1104,10 +1069,12 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1104
1069
}
1105
1070
gen_message_id (& rev , sha1_to_hex (commit -> object .sha1 ));
1106
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 ))
1110
1076
die ("Failed to create output files" );
1077
+ strbuf_setlen (& patch_filename , 0 );
1111
1078
shown = log_tree_commit (& rev , commit );
1112
1079
free (commit -> buffer );
1113
1080
commit -> buffer = NULL ;
@@ -1131,6 +1098,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1131
1098
if (!use_stdout )
1132
1099
fclose (stdout );
1133
1100
}
1101
+ strbuf_release (& patch_filename );
1134
1102
free (list );
1135
1103
if (ignore_if_in_upstream )
1136
1104
free_patch_ids (& ids );
0 commit comments