Skip to content

Commit a9ed6ce

Browse files
committed
Merge branch 'jc/maint-format-patch-o-relative' into maint
* jc/maint-format-patch-o-relative: Teach format-patch to handle output directory relative to cwd Conflicts: t/t4014-format-patch.sh
2 parents 9530eb1 + 9800a75 commit a9ed6ce

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

builtin-log.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ static const char *get_oneline_for_filename(struct commit *commit,
553553

554554
static FILE *realstdout = NULL;
555555
static const char *output_directory = NULL;
556+
static int outdir_offset;
556557

557558
static int reopen_stdout(const char *oneline, int nr, int total)
558559
{
@@ -579,7 +580,7 @@ static int reopen_stdout(const char *oneline, int nr, int total)
579580
strcpy(filename + len, fmt_patch_suffix);
580581
}
581582

582-
fprintf(realstdout, "%s\n", filename);
583+
fprintf(realstdout, "%s\n", filename + outdir_offset);
583584
if (freopen(filename, "w", stdout) == NULL)
584585
return error("Cannot open patch file %s",filename);
585586

@@ -740,6 +741,27 @@ static const char *clean_message_id(const char *msg_id)
740741
return xmemdupz(a, z - a);
741742
}
742743

744+
static const char *set_outdir(const char *prefix, const char *output_directory)
745+
{
746+
if (output_directory && is_absolute_path(output_directory))
747+
return output_directory;
748+
749+
if (!prefix || !*prefix) {
750+
if (output_directory)
751+
return output_directory;
752+
/* The user did not explicitly ask for "./" */
753+
outdir_offset = 2;
754+
return "./";
755+
}
756+
757+
outdir_offset = strlen(prefix);
758+
if (!output_directory)
759+
return prefix;
760+
761+
return xstrdup(prefix_filename(prefix, outdir_offset,
762+
output_directory));
763+
}
764+
743765
int cmd_format_patch(int argc, const char **argv, const char *prefix)
744766
{
745767
struct commit *commit;
@@ -917,8 +939,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
917939
if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
918940
DIFF_OPT_SET(&rev.diffopt, BINARY);
919941

920-
if (!output_directory && !use_stdout)
921-
output_directory = prefix;
942+
if (!use_stdout)
943+
output_directory = set_outdir(prefix, output_directory);
922944

923945
if (output_directory) {
924946
if (use_stdout)

t/t4014-format-patch.sh

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (c) 2006 Junio C Hamano
44
#
55

6-
test_description='Format-patch skipping already incorporated patches'
6+
test_description='various format-patch tests'
77

88
. ./test-lib.sh
99

@@ -255,4 +255,54 @@ test_expect_success 'format-patch respects -U' '
255255
256256
'
257257

258+
test_expect_success 'format-patch from a subdirectory (1)' '
259+
filename=$(
260+
rm -rf sub &&
261+
mkdir -p sub/dir &&
262+
cd sub/dir &&
263+
git format-patch -1
264+
) &&
265+
case "$filename" in
266+
0*)
267+
;; # ok
268+
*)
269+
echo "Oops? $filename"
270+
false
271+
;;
272+
esac &&
273+
test -f "$filename"
274+
'
275+
276+
test_expect_success 'format-patch from a subdirectory (2)' '
277+
filename=$(
278+
rm -rf sub &&
279+
mkdir -p sub/dir &&
280+
cd sub/dir &&
281+
git format-patch -1 -o ..
282+
) &&
283+
case "$filename" in
284+
../0*)
285+
;; # ok
286+
*)
287+
echo "Oops? $filename"
288+
false
289+
;;
290+
esac &&
291+
basename=$(expr "$filename" : ".*/\(.*\)") &&
292+
test -f "sub/$basename"
293+
'
294+
295+
test_expect_success 'format-patch from a subdirectory (3)' '
296+
here="$TEST_DIRECTORY/$test" &&
297+
rm -f 0* &&
298+
filename=$(
299+
rm -rf sub &&
300+
mkdir -p sub/dir &&
301+
cd sub/dir &&
302+
git format-patch -1 -o "$here"
303+
) &&
304+
basename=$(expr "$filename" : ".*/\(.*\)") &&
305+
test -f "$basename"
306+
'
307+
258308
test_done

0 commit comments

Comments
 (0)