@matheusbsbs / matheus-bagliano / commits / 9800a754f9
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Jan 12, 2009 at 15:18 UTC 9800a754f931d05c2d26da9c1b188ae2c2b80eb4
2 files changed +76 -4
builtin-log.c
+25 -3
@@ -568,6 +568,7 @@ static const char *get_oneline_for_filename(struct commit *commit,
568
569 static FILE *realstdout = NULL;
570 static const char *output_directory = NULL;
571 +static int outdir_offset;
572
573 static int reopen_stdout(const char *oneline, int nr, int total)
574 {
@@ -594,7 +595,7 @@ static int reopen_stdout(const char *oneline, int nr, int total)
595 strcpy(filename + len, fmt_patch_suffix);
596 }
597
597 - fprintf(realstdout, "%s\n", filename);
598 + fprintf(realstdout, "%s\n", filename + outdir_offset);
599 if (freopen(filename, "w", stdout) == NULL)
600 return error("Cannot open patch file %s",filename);
601
@@ -757,6 +758,27 @@ static const char *clean_message_id(const char *msg_id)
758 return xmemdupz(a, z - a);
759 }
760
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 +
782 int cmd_format_patch(int argc, const char **argv, const char *prefix)
783 {
784 struct commit *commit;
@@ -935,8 +957,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
957 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
958 DIFF_OPT_SET(&rev.diffopt, BINARY);
959
938 - if (!output_directory && !use_stdout)
939 - output_directory = prefix;
960 + if (!use_stdout)
961 + output_directory = set_outdir(prefix, output_directory);
962
963 if (output_directory) {
964 if (use_stdout)
t/t4014-format-patch.sh
+51 -1
@@ -3,7 +3,7 @@
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 -test_description='Format-patch skipping already incorporated patches'
6 +test_description='various format-patch tests'
7
8 . ./test-lib.sh
9
@@ -230,4 +230,54 @@ test_expect_success 'shortlog of cover-letter wraps overly-long onelines' '
230
231 '
232
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 +
283 test_done