git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [REGRESSION ps/stash-in-c] git stash show -v
@ 2019-03-19 19:05 Denton Liu
  2019-03-19 23:18 ` Thomas Gummerer
  0 siblings, 1 reply; 15+ messages in thread
From: Denton Liu @ 2019-03-19 19:05 UTC (permalink / raw)
  To: Git Mailing List
  Cc: Paul-Sebastian Ungureanu, Joel Teichroeb, Johannes Schindelin,
	Junio C Hamano, Matthew Kraai, Thomas Gummerer

Hi all,

I've been using jch's branch and I discovered a regression in
ps/stash-in-c.

Here's a test-case on git.git:

	echo '/**/' >>abspath.c
	git stash
	git stash show -v
	# I am expecting the diff to show up here

I am expecting the diff to show up but in reality, I get no output.
However, if I compile the latest master, the diff does show up.

Note that I can get the patch to show up using "git stash show -p" so
it's not really a showstopper. However, my understanding is that this
was supposed to be a one-to-one (bug included!) port.

Please let me know if we're keeping this behaviour so I can retrain my
fingers.

Thanks,

Denton

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [REGRESSION ps/stash-in-c] git stash show -v
  2019-03-19 19:05 [REGRESSION ps/stash-in-c] git stash show -v Denton Liu
@ 2019-03-19 23:18 ` Thomas Gummerer
  2019-03-20  1:00   ` Junio C Hamano
                     ` (3 more replies)
  0 siblings, 4 replies; 15+ messages in thread
From: Thomas Gummerer @ 2019-03-19 23:18 UTC (permalink / raw)
  To: Denton Liu
  Cc: Git Mailing List, Paul-Sebastian Ungureanu, Joel Teichroeb,
	Johannes Schindelin, Junio C Hamano, Matthew Kraai

On 03/19, Denton Liu wrote:
> Hi all,
> 
> I've been using jch's branch and I discovered a regression in
> ps/stash-in-c.
> 
> Here's a test-case on git.git:
> 
> 	echo '/**/' >>abspath.c
> 	git stash
> 	git stash show -v
> 	# I am expecting the diff to show up here
> 
> I am expecting the diff to show up but in reality, I get no output.
> However, if I compile the latest master, the diff does show up.
> 
> Note that I can get the patch to show up using "git stash show -p" so
> it's not really a showstopper. However, my understanding is that this
> was supposed to be a one-to-one (bug included!) port.

Yes, I think this is indeed a regression, even if it worked more or
less accidentally.  '-v' would just be passed through to 'git diff'
which wouldn't do anything with it.  It would show the patch because
that's the default behaviour of 'git diff'.

From a quick search I couldn't find where 'git diff' actually parses
the '-v' argument, but I wonder if we should actually disallow it, in
case we want to use it for something else in the future?  It's not
documented anywhere in the docs either.

Anyway a patch to fix this is below, as we should have some
deprecation plan if we wanted to get rid of the '-v', and not just
silently change the behaviour.

--- >8 ---
Subject: [PATCH] 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 we a user uses
'git stash show -v', they would be shown the diff, because that's the
default behaviour of 'git diff', but not actually directly triggered
by passing the '-v'.

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 -v' in the builtin
version of stash would be completely silent, while it would show the
diff before.

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>
---
 builtin/stash.c  |  4 ++++
 t/t3903-stash.sh | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/builtin/stash.c b/builtin/stash.c
index 51df092633..012662ce68 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -761,6 +761,10 @@ static int show_stash(int argc, const char **argv, const char *prefix)
 		free_stash_info(&info);
 		usage_with_options(git_stash_show_usage, options);
 	}
+	if (!rev.diffopt.output_format) {
+		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
+		diff_setup_done(&rev.diffopt);
+	}
 
 	rev.diffopt.flags.recursive = 1;
 	setup_diff_pager(&rev.diffopt);
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 97cc71fbaf..e0a50ab267 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -612,6 +612,24 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 	test_cmp expected actual
 '
 
+test_expect_success 'stash show -v shows diff' '
+	git reset --hard &&
+	echo foo >>file &&
+	STASH_ID=$(git stash create) &&
+	git reset --hard &&
+	cat >expected <<-EOF &&
+	diff --git a/file b/file
+	index 7601807..71b52c4 100644
+	--- a/file
+	+++ b/file
+	@@ -1 +1,2 @@
+	 baz
+	+foo
+	EOF
+	git stash show -v ${STASH_ID} >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
-- 
2.21.0.157.g0e94f7aa73.dirty


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [REGRESSION ps/stash-in-c] git stash show -v
  2019-03-19 23:18 ` Thomas Gummerer
@ 2019-03-20  1:00   ` Junio C Hamano
  2019-03-20 21:45     ` Thomas Gummerer
  2019-03-20  1:04   ` Junio C Hamano
                     ` (2 subsequent siblings)
  3 siblings, 1 reply; 15+ messages in thread
From: Junio C Hamano @ 2019-03-20  1:00 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: Denton Liu, Git Mailing List, Paul-Sebastian Ungureanu,
	Joel Teichroeb, Johannes Schindelin, Matthew Kraai

Thomas Gummerer <t.gummerer@gmail.com> writes:

> Subject: [PATCH] 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 we a user uses
> 'git stash show -v', they would be shown the diff, because that's the
> default behaviour of 'git diff', but not actually directly triggered
> by passing the '-v'.
>
> 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.

Well explained.  It might have avoided such a bug if the code did
not manually stuff the diffopt.* structure fields (instead, e.g.
prepare an array of strings like {"diff", "--stat", NULL} and let
the option parser diff_opt_parse() to do its job), but that is
lamenting over water under the bridge.

> So 'git stash show -v' in the builtin
> version of stash would be completely silent, while it would show the
> diff before.

That sounds reasonable.  Thanks for a quick diagnosis and a fix.

> 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>
> ---
>  builtin/stash.c  |  4 ++++
>  t/t3903-stash.sh | 18 ++++++++++++++++++
>  2 files changed, 22 insertions(+)
>
> diff --git a/builtin/stash.c b/builtin/stash.c
> index 51df092633..012662ce68 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -761,6 +761,10 @@ static int show_stash(int argc, const char **argv, const char *prefix)
>  		free_stash_info(&info);
>  		usage_with_options(git_stash_show_usage, options);
>  	}
> +	if (!rev.diffopt.output_format) {
> +		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
> +		diff_setup_done(&rev.diffopt);
> +	}

Hmph.  Does this result in setup_done() called twice?  As it would
indicate another bug in the original code if setup_done() was never
called, I am assuming that another setup_done() call in the same
codeflow is already there.

>  	rev.diffopt.flags.recursive = 1;
>  	setup_diff_pager(&rev.diffopt);
> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> index 97cc71fbaf..e0a50ab267 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -612,6 +612,24 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
>  	test_cmp expected actual
>  '
>  
> +test_expect_success 'stash show -v shows diff' '
> +	git reset --hard &&
> +	echo foo >>file &&
> +	STASH_ID=$(git stash create) &&
> +	git reset --hard &&
> +	cat >expected <<-EOF &&
> +	diff --git a/file b/file
> +	index 7601807..71b52c4 100644
> +	--- a/file
> +	+++ b/file
> +	@@ -1 +1,2 @@
> +	 baz
> +	+foo
> +	EOF
> +	git stash show -v ${STASH_ID} >actual &&
> +	test_cmp expected actual
> +'
> +
>  test_expect_success 'drop: fail early if specified stash is not a stash ref' '
>  	git stash clear &&
>  	test_when_finished "git reset --hard HEAD && git stash clear" &&

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [REGRESSION ps/stash-in-c] git stash show -v
  2019-03-19 23:18 ` Thomas Gummerer
  2019-03-20  1:00   ` Junio C Hamano
@ 2019-03-20  1:04   ` Junio C Hamano
  2019-03-20  5:04   ` Jeff King
  2019-03-20 22:49   ` [PATCH v2] stash: setup default diff output format if necessary Thomas Gummerer
  3 siblings, 0 replies; 15+ messages in thread
From: Junio C Hamano @ 2019-03-20  1:04 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: Denton Liu, Git Mailing List, Paul-Sebastian Ungureanu,
	Joel Teichroeb, Johannes Schindelin, Matthew Kraai

Thomas Gummerer <t.gummerer@gmail.com> writes:

> Subject: [PATCH] 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 we a user uses
> 'git stash show -v', they would be shown the diff, because that's the
> default behaviour of 'git diff', but not actually directly triggered
> by passing the '-v'.

A more interesting use case would be not with an otherwise useless
"-v" but a real option that does have impact to how "diff" works,
e.g. "--patience".


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [REGRESSION ps/stash-in-c] git stash show -v
  2019-03-19 23:18 ` Thomas Gummerer
  2019-03-20  1:00   ` Junio C Hamano
  2019-03-20  1:04   ` Junio C Hamano
@ 2019-03-20  5:04   ` Jeff King
  2019-03-20  9:30     ` Duy Nguyen
  2019-03-20 21:59     ` Thomas Gummerer
  2019-03-20 22:49   ` [PATCH v2] stash: setup default diff output format if necessary Thomas Gummerer
  3 siblings, 2 replies; 15+ messages in thread
From: Jeff King @ 2019-03-20  5:04 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: Denton Liu, Git Mailing List, Paul-Sebastian Ungureanu,
	Joel Teichroeb, Johannes Schindelin, Junio C Hamano,
	Matthew Kraai

On Tue, Mar 19, 2019 at 11:18:26PM +0000, Thomas Gummerer wrote:

> From a quick search I couldn't find where 'git diff' actually parses
> the '-v' argument, but I wonder if we should actually disallow it, in
> case we want to use it for something else in the future?  It's not
> documented anywhere in the docs either.

It's a bit interesting, actually. git-diff uses setup_revisions() to
parse its arguments, which picks up any diff options, as well as parsing
the revs and pathspecs.

But it also means we accept any random revision options. So nonsense
like:

  git diff --ancestry-path HEAD^ HEAD

is accepted, even though nobody ever looks at the flags set by parsing
that option.

And "-v" is mostly in the same boat. It works more or less like --pretty
(try rev-list with and without it), and does nothing for an endpoint
diff. What's a little interesting, though, is that it was originally
added as a diff-tree option in the very early days, via cee99d2257
(diff-tree: add "verbose header" mode, 2005-05-06). And the reason there
is that "diff-tree --stdin" filled a "log"-like role; it didn't traverse
the commits itself, but it was responsible for showing them.

Most of that is historical curiosity, but I think the takeaways are:

  - we probably should use a less bizarre option to demonstrate this bug
    (Junio suggested --patience, which makes perfect sense to me)

  - we may want to teach the "diff" porcelain not to accept useless
    revision options. I suspect it may be a bit tricky, just because of
    the way the code takes advantage of setup_revisions.  It would also
    be nice if "diff-tree" in non-stdin mode could do the same, but I
    suspect that is even trickier (we do not even know whether we are in
    --stdin mode or not until we've fed the options to setup_revisions).
    So I'd guess this is not really worth the effort it would take.

  - "-v" is a real thing; we should consider either documenting it or
    deprecating it.

-Peff

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [REGRESSION ps/stash-in-c] git stash show -v
  2019-03-20  5:04   ` Jeff King
@ 2019-03-20  9:30     ` Duy Nguyen
  2019-03-20 21:59     ` Thomas Gummerer
  1 sibling, 0 replies; 15+ messages in thread
From: Duy Nguyen @ 2019-03-20  9:30 UTC (permalink / raw)
  To: Jeff King
  Cc: Thomas Gummerer, Denton Liu, Git Mailing List,
	Paul-Sebastian Ungureanu, Joel Teichroeb, Johannes Schindelin,
	Junio C Hamano, Matthew Kraai

On Wed, Mar 20, 2019 at 12:06 PM Jeff King <peff@peff.net> wrote:
>
> On Tue, Mar 19, 2019 at 11:18:26PM +0000, Thomas Gummerer wrote:
>
> > From a quick search I couldn't find where 'git diff' actually parses
> > the '-v' argument, but I wonder if we should actually disallow it, in
> > case we want to use it for something else in the future?  It's not
> > documented anywhere in the docs either.
>
> It's a bit interesting, actually. git-diff uses setup_revisions() to
> parse its arguments, which picks up any diff options, as well as parsing
> the revs and pathspecs.
>
> ...
>
>   - we may want to teach the "diff" porcelain not to accept useless
>     revision options. I suspect it may be a bit tricky, just because of
>     the way the code takes advantage of setup_revisions.

This is actually in my TODO list. Once the parseopt conversion for
revision.c and diff.c is done, "git diff --help" would be _very_ long
because of this exact problem. But then once the conversion is over, I
think removing unused options is quite simple (by manipulating 'struct
option[]' array before calling parse_options).
--
Duy

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [REGRESSION ps/stash-in-c] git stash show -v
  2019-03-20  1:00   ` Junio C Hamano
@ 2019-03-20 21:45     ` Thomas Gummerer
  0 siblings, 0 replies; 15+ messages in thread
From: Thomas Gummerer @ 2019-03-20 21:45 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Denton Liu, Git Mailing List, Paul-Sebastian Ungureanu,
	Joel Teichroeb, Johannes Schindelin, Matthew Kraai

On 03/20, Junio C Hamano wrote:
> Thomas Gummerer <t.gummerer@gmail.com> writes:
> 
> > Subject: [PATCH] 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 we a user uses
> > 'git stash show -v', they would be shown the diff, because that's the
> > default behaviour of 'git diff', but not actually directly triggered
> > by passing the '-v'.
> >
> > 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.
> 
> Well explained.  It might have avoided such a bug if the code did
> not manually stuff the diffopt.* structure fields (instead, e.g.
> prepare an array of strings like {"diff", "--stat", NULL} and let
> the option parser diff_opt_parse() to do its job), but that is
> lamenting over water under the bridge.

I actually think this is still a valid point.  I'm not too familiar
with the diff API, which is why I missed this during my review in the
first place, but I feel like using 'diff_opt_parse()' is still the
right choice here.  Both to fix this bug and to avoid potential
further ones.

Let me give implementing using that a try, I have a feeling like that
might turn out to be better code.

> > So 'git stash show -v' in the builtin
> > version of stash would be completely silent, while it would show the
> > diff before.
> 
> That sounds reasonable.  Thanks for a quick diagnosis and a fix.
> 
> > 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>
> > ---
> >  builtin/stash.c  |  4 ++++
> >  t/t3903-stash.sh | 18 ++++++++++++++++++
> >  2 files changed, 22 insertions(+)
> >
> > diff --git a/builtin/stash.c b/builtin/stash.c
> > index 51df092633..012662ce68 100644
> > --- a/builtin/stash.c
> > +++ b/builtin/stash.c
> > @@ -761,6 +761,10 @@ static int show_stash(int argc, const char **argv, const char *prefix)
> >  		free_stash_info(&info);
> >  		usage_with_options(git_stash_show_usage, options);
> >  	}
> > +	if (!rev.diffopt.output_format) {
> > +		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
> > +		diff_setup_done(&rev.diffopt);
> > +	}
> 
> Hmph.  Does this result in setup_done() called twice?  As it would
> indicate another bug in the original code if setup_done() was never
> called, I am assuming that another setup_done() call in the same
> codeflow is already there.

No, it is not called anywhere, will fix that as well in v2.

> >  	rev.diffopt.flags.recursive = 1;
> >  	setup_diff_pager(&rev.diffopt);
> > diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> > index 97cc71fbaf..e0a50ab267 100755
> > --- a/t/t3903-stash.sh
> > +++ b/t/t3903-stash.sh
> > @@ -612,6 +612,24 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
> >  	test_cmp expected actual
> >  '
> >  
> > +test_expect_success 'stash show -v shows diff' '
> > +	git reset --hard &&
> > +	echo foo >>file &&
> > +	STASH_ID=$(git stash create) &&
> > +	git reset --hard &&
> > +	cat >expected <<-EOF &&
> > +	diff --git a/file b/file
> > +	index 7601807..71b52c4 100644
> > +	--- a/file
> > +	+++ b/file
> > +	@@ -1 +1,2 @@
> > +	 baz
> > +	+foo
> > +	EOF
> > +	git stash show -v ${STASH_ID} >actual &&
> > +	test_cmp expected actual
> > +'
> > +
> >  test_expect_success 'drop: fail early if specified stash is not a stash ref' '
> >  	git stash clear &&
> >  	test_when_finished "git reset --hard HEAD && git stash clear" &&

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [REGRESSION ps/stash-in-c] git stash show -v
  2019-03-20  5:04   ` Jeff King
  2019-03-20  9:30     ` Duy Nguyen
@ 2019-03-20 21:59     ` Thomas Gummerer
  1 sibling, 0 replies; 15+ messages in thread
From: Thomas Gummerer @ 2019-03-20 21:59 UTC (permalink / raw)
  To: Jeff King
  Cc: Denton Liu, Git Mailing List, Paul-Sebastian Ungureanu,
	Joel Teichroeb, Johannes Schindelin, Junio C Hamano,
	Matthew Kraai

On 03/20, Jeff King wrote:
> On Tue, Mar 19, 2019 at 11:18:26PM +0000, Thomas Gummerer wrote:
> 
> > From a quick search I couldn't find where 'git diff' actually parses
> > the '-v' argument, but I wonder if we should actually disallow it, in
> > case we want to use it for something else in the future?  It's not
> > documented anywhere in the docs either.
> 
> It's a bit interesting, actually. git-diff uses setup_revisions() to
> parse its arguments, which picks up any diff options, as well as parsing
> the revs and pathspecs.
> 
> But it also means we accept any random revision options. So nonsense
> like:
> 
>   git diff --ancestry-path HEAD^ HEAD
> 
> is accepted, even though nobody ever looks at the flags set by parsing
> that option.
> 
> And "-v" is mostly in the same boat. It works more or less like --pretty
> (try rev-list with and without it), and does nothing for an endpoint
> diff. What's a little interesting, though, is that it was originally
> added as a diff-tree option in the very early days, via cee99d2257
> (diff-tree: add "verbose header" mode, 2005-05-06). And the reason there
> is that "diff-tree --stdin" filled a "log"-like role; it didn't traverse
> the commits itself, but it was responsible for showing them.

Thanks for the explanation!

> Most of that is historical curiosity, but I think the takeaways are:
> 
>   - we probably should use a less bizarre option to demonstrate this bug
>     (Junio suggested --patience, which makes perfect sense to me)

Yep, that should make the test a bit easier to understand.  Will do
that in v2.

>   - we may want to teach the "diff" porcelain not to accept useless
>     revision options. I suspect it may be a bit tricky, just because of
>     the way the code takes advantage of setup_revisions.  It would also
>     be nice if "diff-tree" in non-stdin mode could do the same, but I
>     suspect that is even trickier (we do not even know whether we are in
>     --stdin mode or not until we've fed the options to setup_revisions).
>     So I'd guess this is not really worth the effort it would take.
> 
>   - "-v" is a real thing; we should consider either documenting it or
>     deprecating it.

Makes sense, I don't have a strong opinion on which way we should go
here, but I'll leave that separately from the bug fix either way.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v2] stash: setup default diff output format if necessary
  2019-03-19 23:18 ` Thomas Gummerer
                     ` (2 preceding siblings ...)
  2019-03-20  5:04   ` Jeff King
@ 2019-03-20 22:49   ` Thomas Gummerer
  2019-03-20 23:04     ` Denton Liu
                       ` (2 more replies)
  3 siblings, 3 replies; 15+ messages in thread
From: Thomas Gummerer @ 2019-03-20 22:49 UTC (permalink / raw)
  To: Denton Liu
  Cc: Git Mailing List, Paul-Sebastian Ungureanu, Joel Teichroeb,
	Johannes Schindelin, Junio C Hamano, Matthew Kraai, Jeff King

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 we 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>
---

Thanks Peff and Junio for your comments on the previous round.

Compared to v1, this uses the --patience flags for the tests now, and
mentions only the --patience flag in the commit message.  While the
original report was about -v, I do agree that --patience is more
relevant here.

I think this also deserves some explanation of what didn't change,
especially after what I said in [*1*].  We're still not using the
'diff_opt_parse()' option parser, as it doesn't understand '-v' for
example.  'setup_revisions()' understands that, but 'diff_opt_parse()'
doesn't, so we'd still have a change in behaviour at least there.
After discovering that I gave up on that approach.

The other thing that was pointed out is the 'diff_setup_done()' call
here.  'diff_setup_done()' is already called inside of
'setup_revisions()', so we don't need to do it again, unless we change
the output format, which is what we are doing here.  In fact this is
the same way it's implemented in 'builtin/diff.c'.

*1*: <20190320214504.GC32487@hank.intra.tgummerer.com>

 builtin/stash.c  |  4 ++++
 t/t3903-stash.sh | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/builtin/stash.c b/builtin/stash.c
index 51df092633..012662ce68 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -761,6 +761,10 @@ static int show_stash(int argc, const char **argv, const char *prefix)
 		free_stash_info(&info);
 		usage_with_options(git_stash_show_usage, options);
 	}
+	if (!rev.diffopt.output_format) {
+		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
+		diff_setup_done(&rev.diffopt);
+	}
 
 	rev.diffopt.flags.recursive = 1;
 	setup_diff_pager(&rev.diffopt);
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 97cc71fbaf..83926ab55b 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -612,6 +612,24 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 	test_cmp expected actual
 '
 
+test_expect_success 'stash show -v shows diff' '
+	git reset --hard &&
+	echo foo >>file &&
+	STASH_ID=$(git stash create) &&
+	git reset --hard &&
+	cat >expected <<-EOF &&
+	diff --git a/file b/file
+	index 7601807..71b52c4 100644
+	--- a/file
+	+++ b/file
+	@@ -1 +1,2 @@
+	 baz
+	+foo
+	EOF
+	git stash show --patience ${STASH_ID} >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
 	git stash clear &&
 	test_when_finished "git reset --hard HEAD && git stash clear" &&
-- 
2.21.0.226.g764ec437b0.dirty


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] stash: setup default diff output format if necessary
  2019-03-20 22:49   ` [PATCH v2] stash: setup default diff output format if necessary Thomas Gummerer
@ 2019-03-20 23:04     ` Denton Liu
  2019-03-20 23:09     ` Denton Liu
  2019-03-21  9:51     ` Jeff King
  2 siblings, 0 replies; 15+ messages in thread
From: Denton Liu @ 2019-03-20 23:04 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: Git Mailing List, Paul-Sebastian Ungureanu, Joel Teichroeb,
	Johannes Schindelin, Junio C Hamano, Matthew Kraai, Jeff King

Hi Thomas,

Thanks for the quick fix!

On Wed, Mar 20, 2019 at 10:49:55PM +0000, Thomas Gummerer wrote:
> 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 we a user uses

s/we a/a/

Looks good otherwise (but I don't know the diff API very well either).

Thanks!

> '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>
> ---
> 
> Thanks Peff and Junio for your comments on the previous round.
> 
> Compared to v1, this uses the --patience flags for the tests now, and
> mentions only the --patience flag in the commit message.  While the
> original report was about -v, I do agree that --patience is more
> relevant here.
> 
> I think this also deserves some explanation of what didn't change,
> especially after what I said in [*1*].  We're still not using the
> 'diff_opt_parse()' option parser, as it doesn't understand '-v' for
> example.  'setup_revisions()' understands that, but 'diff_opt_parse()'
> doesn't, so we'd still have a change in behaviour at least there.
> After discovering that I gave up on that approach.
> 
> The other thing that was pointed out is the 'diff_setup_done()' call
> here.  'diff_setup_done()' is already called inside of
> 'setup_revisions()', so we don't need to do it again, unless we change
> the output format, which is what we are doing here.  In fact this is
> the same way it's implemented in 'builtin/diff.c'.
> 
> *1*: <20190320214504.GC32487@hank.intra.tgummerer.com>
> 
>  builtin/stash.c  |  4 ++++
>  t/t3903-stash.sh | 18 ++++++++++++++++++
>  2 files changed, 22 insertions(+)
> 
> diff --git a/builtin/stash.c b/builtin/stash.c
> index 51df092633..012662ce68 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -761,6 +761,10 @@ static int show_stash(int argc, const char **argv, const char *prefix)
>  		free_stash_info(&info);
>  		usage_with_options(git_stash_show_usage, options);
>  	}
> +	if (!rev.diffopt.output_format) {
> +		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
> +		diff_setup_done(&rev.diffopt);
> +	}
>  
>  	rev.diffopt.flags.recursive = 1;
>  	setup_diff_pager(&rev.diffopt);
> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> index 97cc71fbaf..83926ab55b 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -612,6 +612,24 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
>  	test_cmp expected actual
>  '
>  
> +test_expect_success 'stash show -v shows diff' '
> +	git reset --hard &&
> +	echo foo >>file &&
> +	STASH_ID=$(git stash create) &&
> +	git reset --hard &&
> +	cat >expected <<-EOF &&
> +	diff --git a/file b/file
> +	index 7601807..71b52c4 100644
> +	--- a/file
> +	+++ b/file
> +	@@ -1 +1,2 @@
> +	 baz
> +	+foo
> +	EOF
> +	git stash show --patience ${STASH_ID} >actual &&
> +	test_cmp expected actual
> +'
> +
>  test_expect_success 'drop: fail early if specified stash is not a stash ref' '
>  	git stash clear &&
>  	test_when_finished "git reset --hard HEAD && git stash clear" &&
> -- 
> 2.21.0.226.g764ec437b0.dirty
> 

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] stash: setup default diff output format if necessary
  2019-03-20 22:49   ` [PATCH v2] stash: setup default diff output format if necessary Thomas Gummerer
  2019-03-20 23:04     ` Denton Liu
@ 2019-03-20 23:09     ` Denton Liu
  2019-03-28 20:45       ` Thomas Gummerer
  2019-03-21  9:51     ` Jeff King
  2 siblings, 1 reply; 15+ messages in thread
From: Denton Liu @ 2019-03-20 23:09 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: Git Mailing List, Paul-Sebastian Ungureanu, Joel Teichroeb,
	Johannes Schindelin, Junio C Hamano, Matthew Kraai, Jeff King

On Wed, Mar 20, 2019 at 10:49:55PM +0000, Thomas Gummerer wrote:
> 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 we 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>
> ---
> 
> Thanks Peff and Junio for your comments on the previous round.
> 
> Compared to v1, this uses the --patience flags for the tests now, and
> mentions only the --patience flag in the commit message.  While the
> original report was about -v, I do agree that --patience is more
> relevant here.
> 
> I think this also deserves some explanation of what didn't change,
> especially after what I said in [*1*].  We're still not using the
> 'diff_opt_parse()' option parser, as it doesn't understand '-v' for
> example.  'setup_revisions()' understands that, but 'diff_opt_parse()'
> doesn't, so we'd still have a change in behaviour at least there.
> After discovering that I gave up on that approach.
> 
> The other thing that was pointed out is the 'diff_setup_done()' call
> here.  'diff_setup_done()' is already called inside of
> 'setup_revisions()', so we don't need to do it again, unless we change
> the output format, which is what we are doing here.  In fact this is
> the same way it's implemented in 'builtin/diff.c'.
> 
> *1*: <20190320214504.GC32487@hank.intra.tgummerer.com>
> 
>  builtin/stash.c  |  4 ++++
>  t/t3903-stash.sh | 18 ++++++++++++++++++
>  2 files changed, 22 insertions(+)
> 
> diff --git a/builtin/stash.c b/builtin/stash.c
> index 51df092633..012662ce68 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -761,6 +761,10 @@ static int show_stash(int argc, const char **argv, const char *prefix)
>  		free_stash_info(&info);
>  		usage_with_options(git_stash_show_usage, options);
>  	}
> +	if (!rev.diffopt.output_format) {
> +		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
> +		diff_setup_done(&rev.diffopt);
> +	}
>  
>  	rev.diffopt.flags.recursive = 1;
>  	setup_diff_pager(&rev.diffopt);
> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> index 97cc71fbaf..83926ab55b 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -612,6 +612,24 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
>  	test_cmp expected actual
>  '
>  
> +test_expect_success 'stash show -v shows diff' '

s/-v/--patience/

Missed this in my last email, my bad!

> +	git reset --hard &&
> +	echo foo >>file &&
> +	STASH_ID=$(git stash create) &&
> +	git reset --hard &&
> +	cat >expected <<-EOF &&
> +	diff --git a/file b/file
> +	index 7601807..71b52c4 100644
> +	--- a/file
> +	+++ b/file
> +	@@ -1 +1,2 @@
> +	 baz
> +	+foo
> +	EOF
> +	git stash show --patience ${STASH_ID} >actual &&
> +	test_cmp expected actual
> +'
> +
>  test_expect_success 'drop: fail early if specified stash is not a stash ref' '
>  	git stash clear &&
>  	test_when_finished "git reset --hard HEAD && git stash clear" &&
> -- 
> 2.21.0.226.g764ec437b0.dirty
> 

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] stash: setup default diff output format if necessary
  2019-03-20 22:49   ` [PATCH v2] stash: setup default diff output format if necessary Thomas Gummerer
  2019-03-20 23:04     ` Denton Liu
  2019-03-20 23:09     ` Denton Liu
@ 2019-03-21  9:51     ` Jeff King
  2019-03-22  3:25       ` Junio C Hamano
  2 siblings, 1 reply; 15+ messages in thread
From: Jeff King @ 2019-03-21  9:51 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: Denton Liu, Git Mailing List, Paul-Sebastian Ungureanu,
	Joel Teichroeb, Johannes Schindelin, Junio C Hamano,
	Matthew Kraai

On Wed, Mar 20, 2019 at 10:49:55PM +0000, Thomas Gummerer wrote:

> I think this also deserves some explanation of what didn't change,
> especially after what I said in [*1*].  We're still not using the
> 'diff_opt_parse()' option parser, as it doesn't understand '-v' for
> example.  'setup_revisions()' understands that, but 'diff_opt_parse()'
> doesn't, so we'd still have a change in behaviour at least there.
> After discovering that I gave up on that approach.

Yeah, I think this would get into the "why are we even looking at
revision options" thing, which really is a separate topic. Let's do the
minimal fix here.

> The other thing that was pointed out is the 'diff_setup_done()' call
> here.  'diff_setup_done()' is already called inside of
> 'setup_revisions()', so we don't need to do it again, unless we change
> the output format, which is what we are doing here.  In fact this is
> the same way it's implemented in 'builtin/diff.c'.

That makes me wonder if any part of diff_setup_done() could be surprised
at being called twice. I guess not, if nobody has reported a bug in
git-diff. :)

There is a "set_default" callback that was added by 6c374008b1
(diff_opt: track whether flags have been set explicitly, 2013-05-10),
but it looks like it was never actually used. I think the theory is that
cases like this could do their tweaking in such a callback. But I think
it makes sense to follow the lead of builtin/diff.c for the immediate
fix, and we can look at using set_default as a separate topic.

-Peff

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] stash: setup default diff output format if necessary
  2019-03-21  9:51     ` Jeff King
@ 2019-03-22  3:25       ` Junio C Hamano
  2019-03-22  3:48         ` Jeff King
  0 siblings, 1 reply; 15+ messages in thread
From: Junio C Hamano @ 2019-03-22  3:25 UTC (permalink / raw)
  To: Jeff King
  Cc: Thomas Gummerer, Denton Liu, Git Mailing List,
	Paul-Sebastian Ungureanu, Joel Teichroeb, Johannes Schindelin,
	Matthew Kraai

Jeff King <peff@peff.net> writes:

>> I think this also deserves some explanation of what didn't change,
>> especially after what I said in [*1*].  We're still not using the
>> 'diff_opt_parse()' option parser, as it doesn't understand '-v' for
>> example.  'setup_revisions()' understands that, but 'diff_opt_parse()'
>> doesn't, so we'd still have a change in behaviour at least there.
>> After discovering that I gave up on that approach.
>
> Yeah, I think this would get into the "why are we even looking at
> revision options" thing, which really is a separate topic. Let's do the
> minimal fix here.

I tend to agree.  Limiting ourselves to diff options would be a good
direction going forward in the longer term, but let's fix the regression
so that we can push the base topic to 'master' before we need to
know the shape of the next release.

> There is a "set_default" callback that was added by 6c374008b1
> (diff_opt: track whether flags have been set explicitly, 2013-05-10),
> but it looks like it was never actually used. I think the theory is that
> cases like this could do their tweaking in such a callback. But I think
> it makes sense to follow the lead of builtin/diff.c for the immediate
> fix, and we can look at using set_default as a separate topic.

I agree with the conclusion.  I wouldn't be surprised if this is one
of the things that was once used but left behind when the last
caller disappeared, though.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] stash: setup default diff output format if necessary
  2019-03-22  3:25       ` Junio C Hamano
@ 2019-03-22  3:48         ` Jeff King
  0 siblings, 0 replies; 15+ messages in thread
From: Jeff King @ 2019-03-22  3:48 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Thomas Gummerer, Denton Liu, Git Mailing List,
	Paul-Sebastian Ungureanu, Joel Teichroeb, Johannes Schindelin,
	Matthew Kraai

On Fri, Mar 22, 2019 at 12:25:17PM +0900, Junio C Hamano wrote:

> > There is a "set_default" callback that was added by 6c374008b1
> > (diff_opt: track whether flags have been set explicitly, 2013-05-10),
> > but it looks like it was never actually used. I think the theory is that
> > cases like this could do their tweaking in such a callback. But I think
> > it makes sense to follow the lead of builtin/diff.c for the immediate
> > fix, and we can look at using set_default as a separate topic.
> 
> I agree with the conclusion.  I wouldn't be surprised if this is one
> of the things that was once used but left behind when the last
> caller disappeared, though.

I wondered, too, but `git log --pickaxe-regex -S'set_default[^_]'` seems
to think it was purely aspirational. :)

That calls for a little extra caution when starting to use it (because
it has not really been tested), but from a quick look I cannot see how
it would go wrong.

-Peff

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] stash: setup default diff output format if necessary
  2019-03-20 23:09     ` Denton Liu
@ 2019-03-28 20:45       ` Thomas Gummerer
  0 siblings, 0 replies; 15+ messages in thread
From: Thomas Gummerer @ 2019-03-28 20:45 UTC (permalink / raw)
  To: Denton Liu
  Cc: Git Mailing List, Paul-Sebastian Ungureanu, Joel Teichroeb,
	Johannes Schindelin, Junio C Hamano, Matthew Kraai, Jeff King

On 03/20, Denton Liu wrote:
> On Wed, Mar 20, 2019 at 10:49:55PM +0000, Thomas Gummerer wrote:
> > diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> > index 97cc71fbaf..83926ab55b 100755
> > --- a/t/t3903-stash.sh
> > +++ b/t/t3903-stash.sh
> > @@ -612,6 +612,24 @@ test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
> >  	test_cmp expected actual
> >  '
> >  
> > +test_expect_success 'stash show -v shows diff' '
> 
> s/-v/--patience/
> 
> Missed this in my last email, my bad!

I see Junio already picked this and the fix for the commit message up
already.  Thanks both!

> > +	git reset --hard &&
> > +	echo foo >>file &&
> > +	STASH_ID=$(git stash create) &&
> > +	git reset --hard &&
> > +	cat >expected <<-EOF &&
> > +	diff --git a/file b/file
> > +	index 7601807..71b52c4 100644
> > +	--- a/file
> > +	+++ b/file
> > +	@@ -1 +1,2 @@
> > +	 baz
> > +	+foo
> > +	EOF
> > +	git stash show --patience ${STASH_ID} >actual &&
> > +	test_cmp expected actual
> > +'
> > +
> >  test_expect_success 'drop: fail early if specified stash is not a stash ref' '
> >  	git stash clear &&
> >  	test_when_finished "git reset --hard HEAD && git stash clear" &&
> > -- 
> > 2.21.0.226.g764ec437b0.dirty
> > 

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2019-03-28 20:45 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-19 19:05 [REGRESSION ps/stash-in-c] git stash show -v Denton Liu
2019-03-19 23:18 ` Thomas Gummerer
2019-03-20  1:00   ` Junio C Hamano
2019-03-20 21:45     ` Thomas Gummerer
2019-03-20  1:04   ` Junio C Hamano
2019-03-20  5:04   ` Jeff King
2019-03-20  9:30     ` Duy Nguyen
2019-03-20 21:59     ` Thomas Gummerer
2019-03-20 22:49   ` [PATCH v2] stash: setup default diff output format if necessary Thomas Gummerer
2019-03-20 23:04     ` Denton Liu
2019-03-20 23:09     ` Denton Liu
2019-03-28 20:45       ` Thomas Gummerer
2019-03-21  9:51     ` Jeff King
2019-03-22  3:25       ` Junio C Hamano
2019-03-22  3:48         ` Jeff King

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).