stash: setup default diff output format if necessary

In the scripted 'git stash show' when no arguments are passed, we just pass '--stat' to 'git diff'. When any argument is passed to 'stash show', we no longer pass '--stat' to 'git diff', and pass whatever flags are passed directly through to 'git diff'. By default 'git diff' shows the patch output. So when a user uses 'git stash show --patience', they would be shown the diff as expected, using the patience algorithm. '--patience' in this case only changes the diff algorithm, but does not cause 'git diff' to show the diff by itself. The diff is shown because that's the default behaviour of 'git diff'. In the C version of 'git stash show', we try to emulate that behaviour using the internal diff API. However we forgot to set up the default output format, in case it wasn't set by any of the flags that were passed through. So 'git stash show --patience' in the builtin version of stash would be completely silent, while it would show the diff in the scripted version. The same thing would happen for other flags that only affect the way a patch is displayed, rather than switching to a different output format than the default one. Fix this by setting up the default output format for 'git diff'. Reported-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Mar 20, 2019 at 22:49 UTC 8e407bc817febdf1ff2edcce01945b3eac6d376b
2 files changed +22
builtin/stash.c
+4
@@ -760,6 +760,10 @@ static int show_stash(int argc, const char **argv, const char *prefix)
760 free_stash_info(&info);
761 usage_with_options(git_stash_show_usage, options);
762 }
763 + if (!rev.diffopt.output_format) {
764 + rev.diffopt.output_format = DIFF_FORMAT_PATCH;
765 + diff_setup_done(&rev.diffopt);
766 + }
767
768 rev.diffopt.flags.recursive = 1;
769 setup_diff_pager(&rev.diffopt);
t/t3903-stash.sh
+18
@@ -612,6 +612,24 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
612 test_cmp expected actual
613 '
614
615 +test_expect_success 'stash show --patience shows diff' '
616 + git reset --hard &&
617 + echo foo >>file &&
618 + STASH_ID=$(git stash create) &&
619 + git reset --hard &&
620 + cat >expected <<-EOF &&
621 + diff --git a/file b/file
622 + index 7601807..71b52c4 100644
623 + --- a/file
624 + +++ b/file
625 + @@ -1 +1,2 @@
626 + baz
627 + +foo
628 + EOF
629 + git stash show --patience ${STASH_ID} >actual &&
630 + test_cmp expected actual
631 +'
632 +
633 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
634 git stash clear &&
635 test_when_finished "git reset --hard HEAD && git stash clear" &&