Skip to content

Commit e6ff0f4

Browse files
Jon Loeligergitster
authored andcommitted
Add the --numbered-files option to git-format-patch.
With this option, git-format-patch will generate simple numbered files as output instead of the default using with the first commit line appended. This simplifies the ability to generate an MH-style drafts folder with each message to be sent. Signed-off-by: Jon Loeliger <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ec563e8 commit e6ff0f4

File tree

2 files changed

+67
-44
lines changed

2 files changed

+67
-44
lines changed

Documentation/git-format-patch.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ SYNOPSIS
1111
[verse]
1212
'git-format-patch' [-n | -k] [-o <dir> | --stdout] [--thread]
1313
[--attach[=<boundary>] | --inline[=<boundary>]]
14-
[-s | --signoff] [<common diff options>] [--start-number <n>]
14+
[-s | --signoff] [<common diff options>]
15+
[--start-number <n>] [--numbered-files]
1516
[--in-reply-to=Message-Id] [--suffix=.<sfx>]
1617
[--ignore-if-in-upstream]
1718
[--subject-prefix=Subject-Prefix]
@@ -30,9 +31,11 @@ gitlink:git-rev-parse[1].
3031
The output of this command is convenient for e-mail submission or
3132
for use with gitlink:git-am[1].
3233

33-
Each output file is numbered sequentially from 1, and uses the
34+
By default, each output file is numbered sequentially from 1, and uses the
3435
first line of the commit message (massaged for pathname safety) as
35-
the filename. The names of the output files are printed to standard
36+
the filename. With the --numbered-files option, the output file names
37+
will only be numbers, without the first line of the commit appended.
38+
The names of the output files are printed to standard
3639
output, unless the --stdout option is specified.
3740

3841
If -o is specified, output files are created in <dir>. Otherwise
@@ -60,6 +63,11 @@ include::diff-options.txt[]
6063
--start-number <n>::
6164
Start numbering the patches at <n> instead of 1.
6265

66+
--numbered-files::
67+
Output file names will be a simple number sequence
68+
without the default first line of the commit appended.
69+
Mutually exclusive with the --stdout option.
70+
6371
-k|--keep-subject::
6472
Do not strip/add '[PATCH]' from the first line of the
6573
commit log message.

builtin-log.c

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ static int git_format_config(const char *var, const char *value)
298298
static FILE *realstdout = NULL;
299299
static const char *output_directory = NULL;
300300

301-
static int reopen_stdout(struct commit *commit, int nr, int keep_subject)
301+
static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
302+
int numbered_files)
302303
{
303304
char filename[PATH_MAX];
304305
char *sol;
@@ -315,53 +316,61 @@ static int reopen_stdout(struct commit *commit, int nr, int keep_subject)
315316
filename[len++] = '/';
316317
}
317318

318-
sprintf(filename + len, "%04d", nr);
319-
len = strlen(filename);
320-
321-
sol = strstr(commit->buffer, "\n\n");
322-
if (sol) {
323-
int j, space = 1;
324-
325-
sol += 2;
326-
/* strip [PATCH] or [PATCH blabla] */
327-
if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
328-
char *eos = strchr(sol + 6, ']');
329-
if (eos) {
330-
while (isspace(*eos))
331-
eos++;
332-
sol = eos;
333-
}
334-
}
319+
if (numbered_files) {
320+
sprintf(filename + len, "%d", nr);
321+
len = strlen(filename);
335322

336-
for (j = 0;
337-
j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
338-
len < sizeof(filename) - suffix_len &&
339-
sol[j] && sol[j] != '\n';
340-
j++) {
341-
if (istitlechar(sol[j])) {
342-
if (space) {
343-
filename[len++] = '-';
344-
space = 0;
323+
} else {
324+
sprintf(filename + len, "%04d", nr);
325+
len = strlen(filename);
326+
327+
sol = strstr(commit->buffer, "\n\n");
328+
if (sol) {
329+
int j, space = 1;
330+
331+
sol += 2;
332+
/* strip [PATCH] or [PATCH blabla] */
333+
if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
334+
char *eos = strchr(sol + 6, ']');
335+
if (eos) {
336+
while (isspace(*eos))
337+
eos++;
338+
sol = eos;
345339
}
346-
filename[len++] = sol[j];
347-
if (sol[j] == '.')
348-
while (sol[j + 1] == '.')
349-
j++;
350-
} else
351-
space = 1;
340+
}
341+
342+
for (j = 0;
343+
j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
344+
len < sizeof(filename) - suffix_len &&
345+
sol[j] && sol[j] != '\n';
346+
j++) {
347+
if (istitlechar(sol[j])) {
348+
if (space) {
349+
filename[len++] = '-';
350+
space = 0;
351+
}
352+
filename[len++] = sol[j];
353+
if (sol[j] == '.')
354+
while (sol[j + 1] == '.')
355+
j++;
356+
} else
357+
space = 1;
358+
}
359+
while (filename[len - 1] == '.'
360+
|| filename[len - 1] == '-')
361+
len--;
362+
filename[len] = 0;
352363
}
353-
while (filename[len - 1] == '.' || filename[len - 1] == '-')
354-
len--;
355-
filename[len] = 0;
364+
if (len + suffix_len >= sizeof(filename))
365+
return error("Patch pathname too long");
366+
strcpy(filename + len, fmt_patch_suffix);
356367
}
357-
if (len + suffix_len >= sizeof(filename))
358-
return error("Patch pathname too long");
359-
strcpy(filename + len, fmt_patch_suffix);
368+
360369
fprintf(realstdout, "%s\n", filename);
361370
if (freopen(filename, "w", stdout) == NULL)
362371
return error("Cannot open patch file %s",filename);
363-
return 0;
364372

373+
return 0;
365374
}
366375

367376
static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
@@ -431,6 +440,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
431440
int numbered = 0;
432441
int start_number = -1;
433442
int keep_subject = 0;
443+
int numbered_files = 0; /* _just_ numbers */
434444
int subject_prefix = 0;
435445
int ignore_if_in_upstream = 0;
436446
int thread = 0;
@@ -465,6 +475,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
465475
numbered = 1;
466476
else if (!prefixcmp(argv[i], "--start-number="))
467477
start_number = strtol(argv[i] + 15, NULL, 10);
478+
else if (!strcmp(argv[i], "--numbered-files"))
479+
numbered_files = 1;
468480
else if (!strcmp(argv[i], "--start-number")) {
469481
i++;
470482
if (i == argc)
@@ -540,6 +552,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
540552
die ("-n and -k are mutually exclusive.");
541553
if (keep_subject && subject_prefix)
542554
die ("--subject-prefix and -k are mutually exclusive.");
555+
if (numbered_files && use_stdout)
556+
die ("--numbered-files and --stdout are mutually exclusive.");
543557

544558
argc = setup_revisions(argc, argv, &rev, "HEAD");
545559
if (argc > 1)
@@ -614,7 +628,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
614628
rev.message_id = message_id;
615629
}
616630
if (!use_stdout)
617-
if (reopen_stdout(commit, rev.nr, keep_subject))
631+
if (reopen_stdout(commit, rev.nr, keep_subject,
632+
numbered_files))
618633
die("Failed to create output files");
619634
shown = log_tree_commit(&rev, commit);
620635
free(commit->buffer);

0 commit comments

Comments
 (0)