Skip to content

Commit 9b40386

Browse files
peffgitster
authored andcommitted
checkout-index: delay automatic setting of to_tempfile
Using --stage=all requires writing to tempfiles, since we cannot put multiple stages into a single file. So --stage=all implies --temp. But we do so by setting to_tempfile in the options callback for --stage, rather than after all options have been parsed. This leads to two bugs: 1. If you run "checkout-index --stage=all --stage=2", this should not imply --temp, but it currently does. The callback cannot just unset to_tempfile when it sees the "2" value, because it no longer knows if its value was from the earlier --stage call, or if the user specified --temp explicitly. 2. If you run "checkout-index --stage=all --no-temp", the --no-temp will overwrite the earlier implied --temp. But this mode of operation cannot work, and the command will fail with "<path> already exists" when trying to write the higher stages. We can fix both by lazily setting to_tempfile. We'll make it a tristate, with -1 as "not yet given", and have --stage=all enable it only after all options are parsed. Likewise, after all options are parsed we can detect and reject the bogus "--no-temp" case. Note that this does technically change the behavior for "--stage=all --no-temp" for paths which have only one stage present (which accidentally worked before, but is now forbidden). But this behavior was never intended, and you'd have to go out of your way to try to trigger it. The new tests cover both cases, as well the general "--stage=all implies --temp", as most of the other tests explicitly say "--temp". Ironically, the test "checkout --temp within subdir" is the only one that _doesn't_ use "--temp", and so was implicitly covering this case. But it seems reasonable to have a more explicit test alongside the other related ones. Suggested-by: Junio C Hamano <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3ccb4c5 commit 9b40386

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

builtin/checkout-index.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
static int nul_term_line;
2525
static int checkout_stage; /* default to checkout stage0 */
2626
static int ignore_skip_worktree; /* default to 0 */
27-
static int to_tempfile;
27+
static int to_tempfile = -1;
2828
static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
2929

3030
static struct checkout state = CHECKOUT_INIT;
@@ -196,7 +196,6 @@ static int option_parse_stage(const struct option *opt,
196196
BUG_ON_OPT_NEG(unset);
197197

198198
if (!strcmp(arg, "all")) {
199-
to_tempfile = 1;
200199
checkout_stage = CHECKOUT_ALL;
201200
} else {
202201
int ch = arg[0];
@@ -269,6 +268,12 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
269268
state.base_dir = "";
270269
state.base_dir_len = strlen(state.base_dir);
271270

271+
if (to_tempfile < 0)
272+
to_tempfile = (checkout_stage == CHECKOUT_ALL);
273+
if (!to_tempfile && checkout_stage == CHECKOUT_ALL)
274+
die(_("options '%s' and '%s' cannot be used together"),
275+
"--stage=all", "--no-temp");
276+
272277
/*
273278
* when --prefix is specified we do not want to update cache.
274279
*/

t/t2004-checkout-cache-temp.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ test_expect_success 'checkout all stages/one file to temporary files' '
117117
test $(cat $s3) = tree3path1)
118118
'
119119

120+
test_expect_success '--stage=all implies --temp' '
121+
rm -f path* .merge_* actual &&
122+
git checkout-index --stage=all -- path1 &&
123+
test_path_is_missing path1
124+
'
125+
126+
test_expect_success 'overriding --stage=all resets implied --temp' '
127+
rm -f path* .merge_* actual &&
128+
git checkout-index --stage=all --stage=2 -- path1 &&
129+
echo tree2path1 >expect &&
130+
test_cmp expect path1
131+
'
132+
133+
test_expect_success '--stage=all --no-temp is rejected' '
134+
rm -f path* .merge_* actual &&
135+
test_must_fail git checkout-index --stage=all --no-temp -- path1 2>err &&
136+
grep -v "already exists" err &&
137+
grep "options .--stage=all. and .--no-temp. cannot be used together" err
138+
'
139+
120140
test_expect_success 'checkout some stages/one file to temporary files' '
121141
rm -f path* .merge_* actual &&
122142
git checkout-index --stage=all --temp -- path2 >actual &&

0 commit comments

Comments
 (0)