Skip to content

Commit a14f154

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

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

546546
static FILE *realstdout = NULL;
547547
static const char *output_directory = NULL;
548+
static int outdir_offset;
548549

549550
static int reopen_stdout(const char *oneline, int nr, int total)
550551
{
@@ -571,7 +572,7 @@ static int reopen_stdout(const char *oneline, int nr, int total)
571572
strcpy(filename + len, fmt_patch_suffix);
572573
}
573574

574-
fprintf(realstdout, "%s\n", filename);
575+
fprintf(realstdout, "%s\n", filename + outdir_offset);
575576
if (freopen(filename, "w", stdout) == NULL)
576577
return error("Cannot open patch file %s",filename);
577578

@@ -732,6 +733,27 @@ static const char *clean_message_id(const char *msg_id)
732733
return xmemdupz(a, z - a);
733734
}
734735

736+
static const char *set_outdir(const char *prefix, const char *output_directory)
737+
{
738+
if (output_directory && is_absolute_path(output_directory))
739+
return output_directory;
740+
741+
if (!prefix || !*prefix) {
742+
if (output_directory)
743+
return output_directory;
744+
/* The user did not explicitly ask for "./" */
745+
outdir_offset = 2;
746+
return "./";
747+
}
748+
749+
outdir_offset = strlen(prefix);
750+
if (!output_directory)
751+
return prefix;
752+
753+
return xstrdup(prefix_filename(prefix, outdir_offset,
754+
output_directory));
755+
}
756+
735757
int cmd_format_patch(int argc, const char **argv, const char *prefix)
736758
{
737759
struct commit *commit;
@@ -909,8 +931,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
909931
if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
910932
DIFF_OPT_SET(&rev.diffopt, BINARY);
911933

912-
if (!output_directory && !use_stdout)
913-
output_directory = prefix;
934+
if (!use_stdout)
935+
output_directory = set_outdir(prefix, output_directory);
914936

915937
if (output_directory) {
916938
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)