Skip to content

Commit 7e41061

Browse files
peffgitster
authored andcommitted
checkout-index: propagate errors to exit code
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 33db5f4 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0b809c8 commit 7e41061

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

builtin/checkout-index.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
167167
int prefix_length;
168168
int force = 0, quiet = 0, not_new = 0;
169169
int index_opt = 0;
170+
int err = 0;
170171
struct option builtin_checkout_index_options[] = {
171172
OPT_BOOL('a', "all", &all,
172173
N_("check out all files in the index")),
@@ -231,7 +232,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
231232
if (read_from_stdin)
232233
die("git checkout-index: don't mix '--stdin' and explicit filenames");
233234
p = prefix_path(prefix, prefix_length, arg);
234-
checkout_file(p, prefix);
235+
err |= checkout_file(p, prefix);
235236
free(p);
236237
}
237238

@@ -253,13 +254,16 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
253254
strbuf_swap(&buf, &unquoted);
254255
}
255256
p = prefix_path(prefix, prefix_length, buf.buf);
256-
checkout_file(p, prefix);
257+
err |= checkout_file(p, prefix);
257258
free(p);
258259
}
259260
strbuf_release(&unquoted);
260261
strbuf_release(&buf);
261262
}
262263

264+
if (err)
265+
return 1;
266+
263267
if (all)
264268
checkout_all(prefix, prefix_length);
265269

t/t2004-checkout-cache-temp.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ test_expect_success 'checkout all stage 2 to temporary files' '
8888
done
8989
'
9090

91-
test_expect_failure 'checkout all stages of unknown path' '
91+
test_expect_success 'checkout all stages of unknown path' '
9292
rm -f path* .merge_* actual &&
9393
test_must_fail git checkout-index --stage=all --temp \
9494
-- does-not-exist 2>stderr &&

t/t2006-checkout-index-basic.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@ test_expect_success 'checkout-index -h in broken repository' '
2121
test_i18ngrep "[Uu]sage" broken/usage
2222
'
2323

24+
test_expect_success 'checkout-index reports errors (cmdline)' '
25+
test_must_fail git checkout-index -- does-not-exist 2>stderr &&
26+
test_i18ngrep not.in.the.cache stderr
27+
'
28+
29+
test_expect_success 'checkout-index reports errors (stdin)' '
30+
echo does-not-exist |
31+
test_must_fail git checkout-index --stdin 2>stderr &&
32+
test_i18ngrep not.in.the.cache stderr
33+
'
34+
2435
test_done

0 commit comments

Comments
 (0)