Skip to content

Commit 4c6f781

Browse files
peffgitster
authored andcommitted
format-patch: refactor output selection
The --stdout and --output-directory options are mutually exclusive, but it's hard to tell from reading the code. We have three separate conditionals that check for use_stdout, and it's only after we've set up the output_directory fully that we check whether the user also specified --stdout. Instead, let's check the exclusion explicitly first, then have a single conditional that handles stdout versus an output directory. This is slightly easier to follow now, and also will keep things sane when we add another output mode in a future patch. We'll add a few tests as well, covering the mutual exclusion and the fact that we are not confused by a configured output directory. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 898f807 commit 4c6f781

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

builtin/log.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,20 +1942,20 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
19421942
if (rev.show_notes)
19431943
load_display_notes(&rev.notes_opt);
19441944

1945-
if (!output_directory && !use_stdout)
1946-
output_directory = config_output_directory;
1945+
if (use_stdout + !!output_directory > 1)
1946+
die(_("--stdout and --output-directory are mutually exclusive"));
19471947

1948-
if (!use_stdout)
1949-
output_directory = set_outdir(prefix, output_directory);
1950-
else
1948+
if (use_stdout) {
19511949
setup_pager();
1952-
1953-
if (output_directory) {
1950+
} else {
19541951
int saved;
1952+
1953+
if (!output_directory)
1954+
output_directory = config_output_directory;
1955+
output_directory = set_outdir(prefix, output_directory);
1956+
19551957
if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
19561958
rev.diffopt.use_color = GIT_COLOR_NEVER;
1957-
if (use_stdout)
1958-
die(_("standard output, or directory, which one?"));
19591959
/*
19601960
* We consider <outdir> as 'outside of gitdir', therefore avoid
19611961
* applying adjust_shared_perm in s-c-l-d.

t/t4014-format-patch.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,6 +1919,19 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' '
19191919
test_path_is_dir patchset
19201920
'
19211921

1922+
test_expect_success 'format-patch forbids multiple outputs' '
1923+
rm -fr outdir &&
1924+
test_must_fail \
1925+
git format-patch --stdout --output-directory=outdir
1926+
'
1927+
1928+
test_expect_success 'configured outdir does not conflict with output options' '
1929+
rm -fr outdir &&
1930+
test_config format.outputDirectory outdir &&
1931+
git format-patch --stdout &&
1932+
test_path_is_missing outdir
1933+
'
1934+
19221935
test_expect_success 'format-patch --base' '
19231936
git checkout patchid &&
19241937

0 commit comments

Comments
 (0)