git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/2] fix error handling in checkout-index
@ 2020-10-27  7:30 Jeff King
  2020-10-27  7:36 ` [PATCH 1/2] checkout-index: drop error message from empty --stage=all Jeff King
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jeff King @ 2020-10-27  7:30 UTC (permalink / raw)
  To: git

While working on another topic, I noticed that "git checkout-index --
path" does not propagate errors through its exit code. It has been that
way for so long that I almost wondered if it is intentional, but I'm
pretty sure it's not. A bit scary, though. :)

  [1/2]: checkout-index: drop error message from empty --stage=all
  [2/2]: checkout-index: propagate errors to exit code

 builtin/checkout-index.c        | 16 ++++++++++++++--
 t/t2004-checkout-cache-temp.sh  | 10 +++++++++-
 t/t2006-checkout-index-basic.sh | 11 +++++++++++
 3 files changed, 34 insertions(+), 3 deletions(-)

-Peff

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

* [PATCH 1/2] checkout-index: drop error message from empty --stage=all
  2020-10-27  7:30 [PATCH 0/2] fix error handling in checkout-index Jeff King
@ 2020-10-27  7:36 ` Jeff King
  2020-10-27  7:37 ` [PATCH 2/2] checkout-index: propagate errors to exit code Jeff King
  2020-10-27 19:13 ` [PATCH 0/2] fix error handling in checkout-index Junio C Hamano
  2 siblings, 0 replies; 5+ messages in thread
From: Jeff King @ 2020-10-27  7:36 UTC (permalink / raw)
  To: git

If checkout-index is given --stage=all for a specific path, it will try
to write stages 1-3 (if present) for that path to temporary files.
However, if the file is present only at stage 0, it writes nothing but
gives a confusing message:

  $ git checkout-index --stage=all -- Makefile
  git checkout-index: Makefile does not exist at stage 4

This is nonsense. There is no stage 4 (it's just an internal enum value
we use for "all"), and the documentation clearly states:

  Paths which only have a stage 0 entry will always be omitted from the
  output.

Here it's talking about the list of tempfiles written to stdout, but it
seems clear that this case was not meant to be an error. We even have a
test which covers it, but it only checks that the command reports an
exit code of 0, not its stderr. And it reports 0 only because of another
bug which fails to propagate errors (which will be fixed in a subsequent
patch).

So let's make the test more thorough. We'll also cover the case that we
found _no_ entry, not even a stage zero, which should still be an error.
However, because of the other bug, we'll have to mark this as expecting
failure for the moment.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/checkout-index.c       |  8 ++++++++
 t/t2004-checkout-cache-temp.sh | 10 +++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index a854fd16e7..195165d8bd 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -79,6 +79,14 @@ static int checkout_file(const char *name, const char *prefix)
 		return errs > 0 ? -1 : 0;
 	}
 
+	/*
+	 * At this point we know we didn't try to check anything out. If it was
+	 * because we did find an entry but it was stage 0, that's not an
+	 * error.
+	 */
+	if (has_same_name && checkout_stage == CHECKOUT_ALL)
+		return 0;
+
 	if (!state.quiet) {
 		fprintf(stderr, "git checkout-index: %s ", name);
 		if (!has_same_name)
diff --git a/t/t2004-checkout-cache-temp.sh b/t/t2004-checkout-cache-temp.sh
index a12afe93f3..4308d698b9 100755
--- a/t/t2004-checkout-cache-temp.sh
+++ b/t/t2004-checkout-cache-temp.sh
@@ -88,9 +88,17 @@ test_expect_success 'checkout all stage 2 to temporary files' '
 	done
 '
 
+test_expect_failure 'checkout all stages of unknown path' '
+	rm -f path* .merge_* actual &&
+	test_must_fail git checkout-index --stage=all --temp \
+		-- does-not-exist 2>stderr &&
+	test_i18ngrep not.in.the.cache stderr
+'
+
 test_expect_success 'checkout all stages/one file to nothing' '
 	rm -f path* .merge_* actual &&
-	git checkout-index --stage=all --temp -- path0 >actual &&
+	git checkout-index --stage=all --temp -- path0 >actual 2>stderr &&
+	test_must_be_empty stderr &&
 	test_line_count = 0 actual
 '
 
-- 
2.29.1.634.g9e41dc1bf2


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

* [PATCH 2/2] checkout-index: propagate errors to exit code
  2020-10-27  7:30 [PATCH 0/2] fix error handling in checkout-index Jeff King
  2020-10-27  7:36 ` [PATCH 1/2] checkout-index: drop error message from empty --stage=all Jeff King
@ 2020-10-27  7:37 ` Jeff King
  2020-10-27 19:13 ` [PATCH 0/2] fix error handling in checkout-index Junio C Hamano
  2 siblings, 0 replies; 5+ messages in thread
From: Jeff King @ 2020-10-27  7:37 UTC (permalink / raw)
  To: git

If we encounter an error while checking out an explicit path, we print a
message to stderr but do not actually exit with a non-zero code. While
this is a plumbing command and the behavior goes all the way back to
33db5f4d90 (Add a "checkout-cache" command which does what the name
suggests., 2005-04-09), this is almost certainly an oversight:

  - we _do_ return an exit code from checkout_file(); the caller just
    never reads it

  - errors while checking out all paths (with "-a") do result in a
    non-zero exit code.

  - it would be quite unusual not to use the exit code for an error,
    as otherwise the caller has no idea the command failed except by
    scraping stderr

To keep our tests simple and portable, we can use the most obvious
error: asking to checkout a path which is not in the index at all.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/checkout-index.c        |  8 ++++++--
 t/t2004-checkout-cache-temp.sh  |  2 +-
 t/t2006-checkout-index-basic.sh | 11 +++++++++++
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index 195165d8bd..4bbfc92dce 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -167,6 +167,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 	int prefix_length;
 	int force = 0, quiet = 0, not_new = 0;
 	int index_opt = 0;
+	int err = 0;
 	struct option builtin_checkout_index_options[] = {
 		OPT_BOOL('a', "all", &all,
 			N_("check out all files in the index")),
@@ -231,7 +232,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 		if (read_from_stdin)
 			die("git checkout-index: don't mix '--stdin' and explicit filenames");
 		p = prefix_path(prefix, prefix_length, arg);
-		checkout_file(p, prefix);
+		err |= checkout_file(p, prefix);
 		free(p);
 	}
 
@@ -253,13 +254,16 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 				strbuf_swap(&buf, &unquoted);
 			}
 			p = prefix_path(prefix, prefix_length, buf.buf);
-			checkout_file(p, prefix);
+			err |= checkout_file(p, prefix);
 			free(p);
 		}
 		strbuf_release(&unquoted);
 		strbuf_release(&buf);
 	}
 
+	if (err)
+		return 1;
+
 	if (all)
 		checkout_all(prefix, prefix_length);
 
diff --git a/t/t2004-checkout-cache-temp.sh b/t/t2004-checkout-cache-temp.sh
index 4308d698b9..a9352b08a8 100755
--- a/t/t2004-checkout-cache-temp.sh
+++ b/t/t2004-checkout-cache-temp.sh
@@ -88,7 +88,7 @@ test_expect_success 'checkout all stage 2 to temporary files' '
 	done
 '
 
-test_expect_failure 'checkout all stages of unknown path' '
+test_expect_success 'checkout all stages of unknown path' '
 	rm -f path* .merge_* actual &&
 	test_must_fail git checkout-index --stage=all --temp \
 		-- does-not-exist 2>stderr &&
diff --git a/t/t2006-checkout-index-basic.sh b/t/t2006-checkout-index-basic.sh
index 57cbdfe9bc..8e181dbf01 100755
--- a/t/t2006-checkout-index-basic.sh
+++ b/t/t2006-checkout-index-basic.sh
@@ -21,4 +21,15 @@ test_expect_success 'checkout-index -h in broken repository' '
 	test_i18ngrep "[Uu]sage" broken/usage
 '
 
+test_expect_success 'checkout-index reports errors (cmdline)' '
+	test_must_fail git checkout-index -- does-not-exist 2>stderr &&
+	test_i18ngrep not.in.the.cache stderr
+'
+
+test_expect_success 'checkout-index reports errors (stdin)' '
+	echo does-not-exist |
+	test_must_fail git checkout-index --stdin 2>stderr &&
+	test_i18ngrep not.in.the.cache stderr
+'
+
 test_done
-- 
2.29.1.634.g9e41dc1bf2

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

* Re: [PATCH 0/2] fix error handling in checkout-index
  2020-10-27  7:30 [PATCH 0/2] fix error handling in checkout-index Jeff King
  2020-10-27  7:36 ` [PATCH 1/2] checkout-index: drop error message from empty --stage=all Jeff King
  2020-10-27  7:37 ` [PATCH 2/2] checkout-index: propagate errors to exit code Jeff King
@ 2020-10-27 19:13 ` Junio C Hamano
  2020-10-27 20:27   ` Jeff King
  2 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2020-10-27 19:13 UTC (permalink / raw)
  To: Jeff King; +Cc: git

Jeff King <peff@peff.net> writes:

> While working on another topic, I noticed that "git checkout-index --
> path" does not propagate errors through its exit code. It has been that
> way for so long that I almost wondered if it is intentional, but I'm
> pretty sure it's not. A bit scary, though. :)

Quite honestly, at this point, I do not think the intention matters
any more.  If somebody depends on the behaviour and wrote

    do some thing &&
    git checkout-index -- $path_that_is_possibly_missing &&
    do another thing &&

then this change _will_ be a regression, whether it was originally
done this way on purpose or not.

I do not think it is the kind of regression that we should avoid,
though.  I'd say that we should bite the bullet and fix it, as it
should also be easy to fix/adjust such a collateral damage.  That
would make the world a better place in the end.

Thanks.

>   [1/2]: checkout-index: drop error message from empty --stage=all
>   [2/2]: checkout-index: propagate errors to exit code
>
>  builtin/checkout-index.c        | 16 ++++++++++++++--
>  t/t2004-checkout-cache-temp.sh  | 10 +++++++++-
>  t/t2006-checkout-index-basic.sh | 11 +++++++++++
>  3 files changed, 34 insertions(+), 3 deletions(-)
>
> -Peff

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

* Re: [PATCH 0/2] fix error handling in checkout-index
  2020-10-27 19:13 ` [PATCH 0/2] fix error handling in checkout-index Junio C Hamano
@ 2020-10-27 20:27   ` Jeff King
  0 siblings, 0 replies; 5+ messages in thread
From: Jeff King @ 2020-10-27 20:27 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Tue, Oct 27, 2020 at 12:13:03PM -0700, Junio C Hamano wrote:

> Jeff King <peff@peff.net> writes:
> 
> > While working on another topic, I noticed that "git checkout-index --
> > path" does not propagate errors through its exit code. It has been that
> > way for so long that I almost wondered if it is intentional, but I'm
> > pretty sure it's not. A bit scary, though. :)
> 
> Quite honestly, at this point, I do not think the intention matters
> any more.  If somebody depends on the behaviour and wrote
> 
>     do some thing &&
>     git checkout-index -- $path_that_is_possibly_missing &&
>     do another thing &&
> 
> then this change _will_ be a regression, whether it was originally
> done this way on purpose or not.
> 
> I do not think it is the kind of regression that we should avoid,
> though.  I'd say that we should bite the bullet and fix it, as it
> should also be easy to fix/adjust such a collateral damage.  That
> would make the world a better place in the end.

Right, agreed with all of that. What I meant more with "intentional"
was: is there some really clever reason I was missing that it was done
this way in the first place? And I think the answer is "no", it was just
an oversight.

Whether it is OK to change a plumbing command's behavior is somewhat
orthogonal, but I agree it a bug fix and OK to do here.

-Peff

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

end of thread, other threads:[~2020-10-27 20:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-27  7:30 [PATCH 0/2] fix error handling in checkout-index Jeff King
2020-10-27  7:36 ` [PATCH 1/2] checkout-index: drop error message from empty --stage=all Jeff King
2020-10-27  7:37 ` [PATCH 2/2] checkout-index: propagate errors to exit code Jeff King
2020-10-27 19:13 ` [PATCH 0/2] fix error handling in checkout-index Junio C Hamano
2020-10-27 20:27   ` 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).