Skip to content

Commit 9800a75

Browse files
committed
Teach format-patch to handle output directory relative to cwd
Without any explicit -o parameter, we correctly avoided putting the resulting patch output to the toplevel. We should do the same when the user gave a relative pathname to be consistent with this case. Noticed by Cesar Eduardo Barros. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 141201d commit 9800a75

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
@@ -568,6 +568,7 @@ static const char *get_oneline_for_filename(struct commit *commit,
568568

569569
static FILE *realstdout = NULL;
570570
static const char *output_directory = NULL;
571+
static int outdir_offset;
571572

572573
static int reopen_stdout(const char *oneline, int nr, int total)
573574
{
@@ -594,7 +595,7 @@ static int reopen_stdout(const char *oneline, int nr, int total)
594595
strcpy(filename + len, fmt_patch_suffix);
595596
}
596597

597-
fprintf(realstdout, "%s\n", filename);
598+
fprintf(realstdout, "%s\n", filename + outdir_offset);
598599
if (freopen(filename, "w", stdout) == NULL)
599600
return error("Cannot open patch file %s",filename);
600601

@@ -757,6 +758,27 @@ static const char *clean_message_id(const char *msg_id)
757758
return xmemdupz(a, z - a);
758759
}
759760

761+
static const char *set_outdir(const char *prefix, const char *output_directory)
762+
{
763+
if (output_directory && is_absolute_path(output_directory))
764+
return output_directory;
765+
766+
if (!prefix || !*prefix) {
767+
if (output_directory)
768+
return output_directory;
769+
/* The user did not explicitly ask for "./" */
770+
outdir_offset = 2;
771+
return "./";
772+
}
773+
774+
outdir_offset = strlen(prefix);
775+
if (!output_directory)
776+
return prefix;
777+
778+
return xstrdup(prefix_filename(prefix, outdir_offset,
779+
output_directory));
780+
}
781+
760782
int cmd_format_patch(int argc, const char **argv, const char *prefix)
761783
{
762784
struct commit *commit;
@@ -935,8 +957,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
935957
if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
936958
DIFF_OPT_SET(&rev.diffopt, BINARY);
937959

938-
if (!output_directory && !use_stdout)
939-
output_directory = prefix;
960+
if (!use_stdout)
961+
output_directory = set_outdir(prefix, output_directory);
940962

941963
if (output_directory) {
942964
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

@@ -230,4 +230,54 @@ test_expect_success 'shortlog of cover-letter wraps overly-long onelines' '
230230
231231
'
232232

233+
test_expect_success 'format-patch from a subdirectory (1)' '
234+
filename=$(
235+
rm -rf sub &&
236+
mkdir -p sub/dir &&
237+
cd sub/dir &&
238+
git format-patch -1
239+
) &&
240+
case "$filename" in
241+
0*)
242+
;; # ok
243+
*)
244+
echo "Oops? $filename"
245+
false
246+
;;
247+
esac &&
248+
test -f "$filename"
249+
'
250+
251+
test_expect_success 'format-patch from a subdirectory (2)' '
252+
filename=$(
253+
rm -rf sub &&
254+
mkdir -p sub/dir &&
255+
cd sub/dir &&
256+
git format-patch -1 -o ..
257+
) &&
258+
case "$filename" in
259+
../0*)
260+
;; # ok
261+
*)
262+
echo "Oops? $filename"
263+
false
264+
;;
265+
esac &&
266+
basename=$(expr "$filename" : ".*/\(.*\)") &&
267+
test -f "sub/$basename"
268+
'
269+
270+
test_expect_success 'format-patch from a subdirectory (3)' '
271+
here="$TEST_DIRECTORY/$test" &&
272+
rm -f 0* &&
273+
filename=$(
274+
rm -rf sub &&
275+
mkdir -p sub/dir &&
276+
cd sub/dir &&
277+
git format-patch -1 -o "$here"
278+
) &&
279+
basename=$(expr "$filename" : ".*/\(.*\)") &&
280+
test -f "$basename"
281+
'
282+
233283
test_done

0 commit comments

Comments
 (0)