Skip to content

Commit 091e04b

Browse files
tgummerergitster
authored andcommitted
checkout: introduce --{,no-}overlay option
Currently 'git checkout' is defined as an overlay operation, which means that if in 'git checkout <tree-ish> -- [<pathspec>]' we have an entry in the index that matches <pathspec>, but that doesn't exist in <tree-ish>, that entry will not be removed from the index or the working tree. Introduce a new --{,no-}overlay option, which allows using 'git checkout' in non-overlay mode, thus removing files from the working tree if they do not exist in <tree-ish> but match <pathspec>. Note that 'git checkout -p <tree-ish> -- [<pathspec>]' already works this way, so no changes are needed for the patch mode. We disallow 'git checkout --overlay -p' to avoid confusing users who would expect to be able to force overlay mode in 'git checkout -p' this way. Untracked files are not affected by this change, so 'git checkout --no-overlay HEAD -- untracked' will not remove untracked from the working tree. This is so e.g. 'git checkout --no-overlay HEAD -- dir/' doesn't delete all untracked files in dir/, but rather just resets the state of files that are known to git. Suggested-by: Junio C Hamano <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b7033e7 commit 091e04b

File tree

4 files changed

+116
-8
lines changed

4 files changed

+116
-8
lines changed

Documentation/git-checkout.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ the conflicted merge in the specified paths.
260260
This means that you can use `git checkout -p` to selectively discard
261261
edits from your current working tree. See the ``Interactive Mode''
262262
section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
263+
+
264+
Note that this option uses the no overlay mode by default (see also
265+
`--[no-]overlay`), and currently doesn't support overlay mode.
263266

264267
--ignore-other-worktrees::
265268
`git checkout` refuses when the wanted ref is already checked
@@ -276,6 +279,13 @@ section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
276279
Just like linkgit:git-submodule[1], this will detach the
277280
submodules HEAD.
278281

282+
--[no-]overlay::
283+
In the default overlay mode, `git checkout` never
284+
removes files from the index or the working tree. When
285+
specifying `--no-overlay`, files that appear in the index and
286+
working tree, but not in <tree-ish> are removed, to make them
287+
match <tree-ish> exactly.
288+
279289
<branch>::
280290
Branch to checkout; if it refers to a branch (i.e., a name that,
281291
when prepended with "refs/heads/", is a valid ref), then that

builtin/checkout.c

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct checkout_opts {
4444
int ignore_skipworktree;
4545
int ignore_other_worktrees;
4646
int show_progress;
47+
int overlay_mode;
4748
/*
4849
* If new checkout options are added, skip_merge_working_tree
4950
* should be updated accordingly.
@@ -132,14 +133,17 @@ static int skip_same_name(const struct cache_entry *ce, int pos)
132133
return pos;
133134
}
134135

135-
static int check_stage(int stage, const struct cache_entry *ce, int pos)
136+
static int check_stage(int stage, const struct cache_entry *ce, int pos,
137+
int overlay_mode)
136138
{
137139
while (pos < active_nr &&
138140
!strcmp(active_cache[pos]->name, ce->name)) {
139141
if (ce_stage(active_cache[pos]) == stage)
140142
return 0;
141143
pos++;
142144
}
145+
if (!overlay_mode)
146+
return 0;
143147
if (stage == 2)
144148
return error(_("path '%s' does not have our version"), ce->name);
145149
else
@@ -165,14 +169,18 @@ static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
165169
}
166170

167171
static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
168-
const struct checkout *state)
172+
const struct checkout *state, int overlay_mode)
169173
{
170174
while (pos < active_nr &&
171175
!strcmp(active_cache[pos]->name, ce->name)) {
172176
if (ce_stage(active_cache[pos]) == stage)
173177
return checkout_entry(active_cache[pos], state, NULL);
174178
pos++;
175179
}
180+
if (!overlay_mode) {
181+
unlink_entry(ce);
182+
return 0;
183+
}
176184
if (stage == 2)
177185
return error(_("path '%s' does not have our version"), ce->name);
178186
else
@@ -247,9 +255,9 @@ static int checkout_merged(int pos, const struct checkout *state)
247255
return status;
248256
}
249257

250-
static void mark_ce_for_checkout(struct cache_entry *ce,
251-
char *ps_matched,
252-
const struct checkout_opts *opts)
258+
static void mark_ce_for_checkout_overlay(struct cache_entry *ce,
259+
char *ps_matched,
260+
const struct checkout_opts *opts)
253261
{
254262
ce->ce_flags &= ~CE_MATCHED;
255263
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
@@ -281,6 +289,25 @@ static void mark_ce_for_checkout(struct cache_entry *ce,
281289
ce->ce_flags |= CE_MATCHED;
282290
}
283291

292+
static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce,
293+
char *ps_matched,
294+
const struct checkout_opts *opts)
295+
{
296+
ce->ce_flags &= ~CE_MATCHED;
297+
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
298+
return;
299+
if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) {
300+
ce->ce_flags |= CE_MATCHED;
301+
if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
302+
/*
303+
* In overlay mode, but the path is not in
304+
* tree-ish, which means we should remove it
305+
* from the index and the working tree.
306+
*/
307+
ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE;
308+
}
309+
}
310+
284311
static int checkout_paths(const struct checkout_opts *opts,
285312
const char *revision)
286313
{
@@ -332,7 +359,14 @@ static int checkout_paths(const struct checkout_opts *opts,
332359
* to be checked out.
333360
*/
334361
for (pos = 0; pos < active_nr; pos++)
335-
mark_ce_for_checkout(active_cache[pos], ps_matched, opts);
362+
if (opts->overlay_mode)
363+
mark_ce_for_checkout_overlay(active_cache[pos],
364+
ps_matched,
365+
opts);
366+
else
367+
mark_ce_for_checkout_no_overlay(active_cache[pos],
368+
ps_matched,
369+
opts);
336370

337371
if (report_path_error(ps_matched, &opts->pathspec, opts->prefix)) {
338372
free(ps_matched);
@@ -353,7 +387,7 @@ static int checkout_paths(const struct checkout_opts *opts,
353387
if (opts->force) {
354388
warning(_("path '%s' is unmerged"), ce->name);
355389
} else if (opts->writeout_stage) {
356-
errs |= check_stage(opts->writeout_stage, ce, pos);
390+
errs |= check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode);
357391
} else if (opts->merge) {
358392
errs |= check_stages((1<<2) | (1<<3), ce, pos);
359393
} else {
@@ -380,12 +414,14 @@ static int checkout_paths(const struct checkout_opts *opts,
380414
continue;
381415
}
382416
if (opts->writeout_stage)
383-
errs |= checkout_stage(opts->writeout_stage, ce, pos, &state);
417+
errs |= checkout_stage(opts->writeout_stage, ce, pos, &state, opts->overlay_mode);
384418
else if (opts->merge)
385419
errs |= checkout_merged(pos, &state);
386420
pos = skip_same_name(ce, pos) - 1;
387421
}
388422
}
423+
remove_marked_cache_entries(&the_index, 1);
424+
remove_scheduled_dirs();
389425
errs |= finish_delayed_checkout(&state);
390426

391427
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
@@ -547,6 +583,11 @@ static int skip_merge_working_tree(const struct checkout_opts *opts,
547583
* opts->show_progress only impacts output so doesn't require a merge
548584
*/
549585

586+
/*
587+
* opts->overlay_mode cannot be used with switching branches so is
588+
* not tested here
589+
*/
590+
550591
/*
551592
* If we aren't creating a new branch any changes or updates will
552593
* happen in the existing branch. Since that could only be updating
@@ -1183,6 +1224,10 @@ static int checkout_branch(struct checkout_opts *opts,
11831224
die(_("'%s' cannot be used with switching branches"),
11841225
"--patch");
11851226

1227+
if (!opts->overlay_mode)
1228+
die(_("'%s' cannot be used with switching branches"),
1229+
"--no-overlay");
1230+
11861231
if (opts->writeout_stage)
11871232
die(_("'%s' cannot be used with switching branches"),
11881233
"--ours/--theirs");
@@ -1271,6 +1316,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
12711316
"checkout", "control recursive updating of submodules",
12721317
PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
12731318
OPT_BOOL(0, "progress", &opts.show_progress, N_("force progress reporting")),
1319+
OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
12741320
OPT_END(),
12751321
};
12761322

@@ -1279,6 +1325,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
12791325
opts.overwrite_ignore = 1;
12801326
opts.prefix = prefix;
12811327
opts.show_progress = -1;
1328+
opts.overlay_mode = -1;
12821329

12831330
git_config(git_checkout_config, &opts);
12841331

@@ -1302,6 +1349,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
13021349
if ((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) > 1)
13031350
die(_("-b, -B and --orphan are mutually exclusive"));
13041351

1352+
if (opts.overlay_mode == 1 && opts.patch_mode)
1353+
die(_("-p and --overlay are mutually exclusive"));
1354+
13051355
/*
13061356
* From here on, new_branch will contain the branch to be checked out,
13071357
* and new_branch_force and new_orphan_branch will tell us which one of

t/t2025-checkout-no-overlay.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
3+
test_description='checkout --no-overlay <tree-ish> -- <pathspec>'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'setup' '
8+
git commit --allow-empty -m "initial"
9+
'
10+
11+
test_expect_success 'checkout --no-overlay deletes files not in <tree-ish>' '
12+
>file &&
13+
mkdir dir &&
14+
>dir/file1 &&
15+
git add file dir/file1 &&
16+
git checkout --no-overlay HEAD -- file &&
17+
test_path_is_missing file &&
18+
test_path_is_file dir/file1
19+
'
20+
21+
test_expect_success 'checkout --no-overlay removing last file from directory' '
22+
git checkout --no-overlay HEAD -- dir/file1 &&
23+
test_path_is_missing dir
24+
'
25+
26+
test_expect_success 'checkout -p --overlay is disallowed' '
27+
test_must_fail git checkout -p --overlay HEAD 2>actual &&
28+
test_i18ngrep "fatal: -p and --overlay are mutually exclusive" actual
29+
'
30+
31+
test_expect_success '--no-overlay --theirs with D/F conflict deletes file' '
32+
test_commit file1 file1 &&
33+
test_commit file2 file2 &&
34+
git rm --cached file1 &&
35+
echo 1234 >file1 &&
36+
F1=$(git rev-parse HEAD:file1) &&
37+
F2=$(git rev-parse HEAD:file2) &&
38+
{
39+
echo "100644 $F1 1 file1" &&
40+
echo "100644 $F2 2 file1"
41+
} | git update-index --index-info &&
42+
test_path_is_file file1 &&
43+
git checkout --theirs --no-overlay -- file1 &&
44+
test_path_is_missing file1
45+
'
46+
47+
test_done

t/t9902-completion.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,7 @@ test_expect_success 'double dash "git checkout"' '
14361436
--progress Z
14371437
--no-quiet Z
14381438
--no-... Z
1439+
--overlay Z
14391440
EOF
14401441
'
14411442

0 commit comments

Comments
 (0)