Skip to content

Commit 7d0c1f4

Browse files
committed
Merge branch 'tg/checkout-no-overlay'
"git checkout --no-overlay" can be used to trigger a new mode of checking out paths out of the tree-ish, that allows paths that match the pathspec that are in the current index and working tree and are not in the tree-ish. * tg/checkout-no-overlay: revert "checkout: introduce checkout.overlayMode config" checkout: introduce checkout.overlayMode config checkout: introduce --{,no-}overlay option checkout: factor out mark_cache_entry_for_checkout function checkout: clarify comment read-cache: add invalidate parameter to remove_marked_cache_entries entry: support CE_WT_REMOVE flag in checkout_entry entry: factor out unlink_entry function move worktree tests to t24*
2 parents 36eb1cb + e92aa0e commit 7d0c1f4

14 files changed

+191
-58
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
@@ -280,6 +283,13 @@ section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
280283
Do not attempt to create a branch if a remote tracking branch
281284
of the same name exists.
282285

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

builtin/checkout.c

Lines changed: 92 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct checkout_opts {
4646
int ignore_other_worktrees;
4747
int show_progress;
4848
int count_checkout_paths;
49+
int overlay_mode;
4950
/*
5051
* If new checkout options are added, skip_merge_working_tree
5152
* should be updated accordingly.
@@ -135,14 +136,17 @@ static int skip_same_name(const struct cache_entry *ce, int pos)
135136
return pos;
136137
}
137138

138-
static int check_stage(int stage, const struct cache_entry *ce, int pos)
139+
static int check_stage(int stage, const struct cache_entry *ce, int pos,
140+
int overlay_mode)
139141
{
140142
while (pos < active_nr &&
141143
!strcmp(active_cache[pos]->name, ce->name)) {
142144
if (ce_stage(active_cache[pos]) == stage)
143145
return 0;
144146
pos++;
145147
}
148+
if (!overlay_mode)
149+
return 0;
146150
if (stage == 2)
147151
return error(_("path '%s' does not have our version"), ce->name);
148152
else
@@ -168,7 +172,8 @@ static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
168172
}
169173

170174
static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
171-
const struct checkout *state, int *nr_checkouts)
175+
const struct checkout *state, int *nr_checkouts,
176+
int overlay_mode)
172177
{
173178
while (pos < active_nr &&
174179
!strcmp(active_cache[pos]->name, ce->name)) {
@@ -177,6 +182,10 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
177182
NULL, nr_checkouts);
178183
pos++;
179184
}
185+
if (!overlay_mode) {
186+
unlink_entry(ce);
187+
return 0;
188+
}
180189
if (stage == 2)
181190
return error(_("path '%s' does not have our version"), ce->name);
182191
else
@@ -251,6 +260,59 @@ static int checkout_merged(int pos, const struct checkout *state, int *nr_checko
251260
return status;
252261
}
253262

263+
static void mark_ce_for_checkout_overlay(struct cache_entry *ce,
264+
char *ps_matched,
265+
const struct checkout_opts *opts)
266+
{
267+
ce->ce_flags &= ~CE_MATCHED;
268+
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
269+
return;
270+
if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
271+
/*
272+
* "git checkout tree-ish -- path", but this entry
273+
* is in the original index but is not in tree-ish
274+
* or does not match the pathspec; it will not be
275+
* checked out to the working tree. We will not do
276+
* anything to this entry at all.
277+
*/
278+
return;
279+
/*
280+
* Either this entry came from the tree-ish we are
281+
* checking the paths out of, or we are checking out
282+
* of the index.
283+
*
284+
* If it comes from the tree-ish, we already know it
285+
* matches the pathspec and could just stamp
286+
* CE_MATCHED to it from update_some(). But we still
287+
* need ps_matched and read_tree_recursive (and
288+
* eventually tree_entry_interesting) cannot fill
289+
* ps_matched yet. Once it can, we can avoid calling
290+
* match_pathspec() for _all_ entries when
291+
* opts->source_tree != NULL.
292+
*/
293+
if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched))
294+
ce->ce_flags |= CE_MATCHED;
295+
}
296+
297+
static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce,
298+
char *ps_matched,
299+
const struct checkout_opts *opts)
300+
{
301+
ce->ce_flags &= ~CE_MATCHED;
302+
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
303+
return;
304+
if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) {
305+
ce->ce_flags |= CE_MATCHED;
306+
if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
307+
/*
308+
* In overlay mode, but the path is not in
309+
* tree-ish, which means we should remove it
310+
* from the index and the working tree.
311+
*/
312+
ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE;
313+
}
314+
}
315+
254316
static int checkout_paths(const struct checkout_opts *opts,
255317
const char *revision)
256318
{
@@ -302,37 +364,15 @@ static int checkout_paths(const struct checkout_opts *opts,
302364
* Make sure all pathspecs participated in locating the paths
303365
* to be checked out.
304366
*/
305-
for (pos = 0; pos < active_nr; pos++) {
306-
struct cache_entry *ce = active_cache[pos];
307-
ce->ce_flags &= ~CE_MATCHED;
308-
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
309-
continue;
310-
if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
311-
/*
312-
* "git checkout tree-ish -- path", but this entry
313-
* is in the original index; it will not be checked
314-
* out to the working tree and it does not matter
315-
* if pathspec matched this entry. We will not do
316-
* anything to this entry at all.
317-
*/
318-
continue;
319-
/*
320-
* Either this entry came from the tree-ish we are
321-
* checking the paths out of, or we are checking out
322-
* of the index.
323-
*
324-
* If it comes from the tree-ish, we already know it
325-
* matches the pathspec and could just stamp
326-
* CE_MATCHED to it from update_some(). But we still
327-
* need ps_matched and read_tree_recursive (and
328-
* eventually tree_entry_interesting) cannot fill
329-
* ps_matched yet. Once it can, we can avoid calling
330-
* match_pathspec() for _all_ entries when
331-
* opts->source_tree != NULL.
332-
*/
333-
if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched))
334-
ce->ce_flags |= CE_MATCHED;
335-
}
367+
for (pos = 0; pos < active_nr; pos++)
368+
if (opts->overlay_mode)
369+
mark_ce_for_checkout_overlay(active_cache[pos],
370+
ps_matched,
371+
opts);
372+
else
373+
mark_ce_for_checkout_no_overlay(active_cache[pos],
374+
ps_matched,
375+
opts);
336376

337377
if (report_path_error(ps_matched, &opts->pathspec, opts->prefix)) {
338378
free(ps_matched);
@@ -353,7 +393,7 @@ static int checkout_paths(const struct checkout_opts *opts,
353393
if (opts->force) {
354394
warning(_("path '%s' is unmerged"), ce->name);
355395
} else if (opts->writeout_stage) {
356-
errs |= check_stage(opts->writeout_stage, ce, pos);
396+
errs |= check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode);
357397
} else if (opts->merge) {
358398
errs |= check_stages((1<<2) | (1<<3), ce, pos);
359399
} else {
@@ -383,13 +423,16 @@ static int checkout_paths(const struct checkout_opts *opts,
383423
if (opts->writeout_stage)
384424
errs |= checkout_stage(opts->writeout_stage,
385425
ce, pos,
386-
&state, &nr_checkouts);
426+
&state,
427+
&nr_checkouts, opts->overlay_mode);
387428
else if (opts->merge)
388429
errs |= checkout_merged(pos, &state,
389430
&nr_unmerged);
390431
pos = skip_same_name(ce, pos) - 1;
391432
}
392433
}
434+
remove_marked_cache_entries(&the_index, 1);
435+
remove_scheduled_dirs();
393436
errs |= finish_delayed_checkout(&state, &nr_checkouts);
394437

395438
if (opts->count_checkout_paths) {
@@ -571,6 +614,11 @@ static int skip_merge_working_tree(const struct checkout_opts *opts,
571614
* opts->show_progress only impacts output so doesn't require a merge
572615
*/
573616

617+
/*
618+
* opts->overlay_mode cannot be used with switching branches so is
619+
* not tested here
620+
*/
621+
574622
/*
575623
* If we aren't creating a new branch any changes or updates will
576624
* happen in the existing branch. Since that could only be updating
@@ -1224,6 +1272,10 @@ static int checkout_branch(struct checkout_opts *opts,
12241272
die(_("'%s' cannot be used with switching branches"),
12251273
"--patch");
12261274

1275+
if (!opts->overlay_mode)
1276+
die(_("'%s' cannot be used with switching branches"),
1277+
"--no-overlay");
1278+
12271279
if (opts->writeout_stage)
12281280
die(_("'%s' cannot be used with switching branches"),
12291281
"--ours/--theirs");
@@ -1312,6 +1364,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
13121364
"checkout", "control recursive updating of submodules",
13131365
PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
13141366
OPT_BOOL(0, "progress", &opts.show_progress, N_("force progress reporting")),
1367+
OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
13151368
OPT_END(),
13161369
};
13171370

@@ -1320,6 +1373,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
13201373
opts.overwrite_ignore = 1;
13211374
opts.prefix = prefix;
13221375
opts.show_progress = -1;
1376+
opts.overlay_mode = -1;
13231377

13241378
git_config(git_checkout_config, &opts);
13251379

@@ -1344,6 +1398,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
13441398
if ((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) > 1)
13451399
die(_("-b, -B and --orphan are mutually exclusive"));
13461400

1401+
if (opts.overlay_mode == 1 && opts.patch_mode)
1402+
die(_("-p and --overlay are mutually exclusive"));
1403+
13471404
/*
13481405
* From here on, new_branch will contain the branch to be checked out,
13491406
* and new_branch_force and new_orphan_branch will tell us which one of

cache.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ extern void rename_index_entry_at(struct index_state *, int pos, const char *new
758758
/* Remove entry, return true if there are more entries to go. */
759759
extern int remove_index_entry_at(struct index_state *, int pos);
760760

761-
extern void remove_marked_cache_entries(struct index_state *istate);
761+
extern void remove_marked_cache_entries(struct index_state *istate, int invalidate);
762762
extern int remove_file_from_index(struct index_state *, const char *path);
763763
#define ADD_CACHE_VERBOSE 1
764764
#define ADD_CACHE_PRETEND 2
@@ -1569,6 +1569,11 @@ struct checkout {
15691569
extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath, int *nr_checkouts);
15701570
extern void enable_delayed_checkout(struct checkout *state);
15711571
extern int finish_delayed_checkout(struct checkout *state, int *nr_checkouts);
1572+
/*
1573+
* Unlink the last component and schedule the leading directories for
1574+
* removal, such that empty directories get removed.
1575+
*/
1576+
extern void unlink_entry(const struct cache_entry *ce);
15721577

15731578
struct cache_def {
15741579
struct strbuf path;

entry.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,17 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state,
441441
static struct strbuf path = STRBUF_INIT;
442442
struct stat st;
443443

444+
if (ce->ce_flags & CE_WT_REMOVE) {
445+
if (topath)
446+
/*
447+
* No content and thus no path to create, so we have
448+
* no pathname to return.
449+
*/
450+
BUG("Can't remove entry to a path");
451+
unlink_entry(ce);
452+
return 0;
453+
}
454+
444455
if (topath)
445456
return write_entry(ce, topath, state, 1);
446457

@@ -510,3 +521,18 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state,
510521
(*nr_checkouts)++;
511522
return write_entry(ce, path.buf, state, 0);
512523
}
524+
525+
void unlink_entry(const struct cache_entry *ce)
526+
{
527+
const struct submodule *sub = submodule_from_ce(ce);
528+
if (sub) {
529+
/* state.force is set at the caller. */
530+
submodule_move_head(ce->name, "HEAD", NULL,
531+
SUBMODULE_MOVE_HEAD_FORCE);
532+
}
533+
if (!check_leading_path(ce->name, ce_namelen(ce)))
534+
return;
535+
if (remove_or_warn(ce->ce_mode, ce->name))
536+
return;
537+
schedule_dir_for_removal(ce->name, ce_namelen(ce));
538+
}

read-cache.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,13 +588,19 @@ int remove_index_entry_at(struct index_state *istate, int pos)
588588
* CE_REMOVE is set in ce_flags. This is much more effective than
589589
* calling remove_index_entry_at() for each entry to be removed.
590590
*/
591-
void remove_marked_cache_entries(struct index_state *istate)
591+
void remove_marked_cache_entries(struct index_state *istate, int invalidate)
592592
{
593593
struct cache_entry **ce_array = istate->cache;
594594
unsigned int i, j;
595595

596596
for (i = j = 0; i < istate->cache_nr; i++) {
597597
if (ce_array[i]->ce_flags & CE_REMOVE) {
598+
if (invalidate) {
599+
cache_tree_invalidate_path(istate,
600+
ce_array[i]->name);
601+
untracked_cache_remove_from_index(istate,
602+
ce_array[i]->name);
603+
}
598604
remove_name_hash(istate, ce_array[i]);
599605
save_or_free_index_entry(istate, ce_array[i]);
600606
}

split-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void merge_base_index(struct index_state *istate)
162162
ewah_each_bit(si->replace_bitmap, replace_entry, istate);
163163
ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
164164
if (si->nr_deletions)
165-
remove_marked_cache_entries(istate);
165+
remove_marked_cache_entries(istate, 0);
166166

167167
for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
168168
if (!ce_namelen(si->saved_cache[i]))

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
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)