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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 4, 2020 at 14:28 UTC 4c6f781f9c7ee7029c3f2fd20ddd76ce8b476bca
2 files changed +22 -9
builtin/log.c
+9 -9
@@ -1942,20 +1942,20 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1942 if (rev.show_notes)
1943 load_display_notes(&rev.notes_opt);
1944
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"));
1947
1948 - if (!use_stdout)
1949 - output_directory = set_outdir(prefix, output_directory);
1950 - else
1948 + if (use_stdout) {
1949 setup_pager();
1952 -
1953 - if (output_directory) {
1950 + } else {
1951 int saved;
1952 +
1953 + if (!output_directory)
1954 + output_directory = config_output_directory;
1955 + output_directory = set_outdir(prefix, output_directory);
1956 +
1957 if (rev.diffopt.use_color != GIT_COLOR_ALWAYS)
1958 rev.diffopt.use_color = GIT_COLOR_NEVER;
1957 - if (use_stdout)
1958 - die(_("standard output, or directory, which one?"));
1959 /*
1960 * We consider <outdir> as 'outside of gitdir', therefore avoid
1961 * applying adjust_shared_perm in s-c-l-d.
t/t4014-format-patch.sh
+13
@@ -1919,6 +1919,19 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' '
1919 test_path_is_dir patchset
1920 '
1921
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 +
1935 test_expect_success 'format-patch --base' '
1936 git checkout patchid &&
1937