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 <gitster@pobox.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Jan 8, 2019 at 21:52 UTC 091e04bc8cbb0c89c8112c4784f02a44decc257e
4 files changed +116 -8
Documentation/git-checkout.txt
+10
@@ -260,6 +260,9 @@ the conflicted merge in the specified paths.
260 This means that you can use `git checkout -p` to selectively discard
261 edits from your current working tree. See the ``Interactive Mode''
262 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.
266
267 --ignore-other-worktrees::
268 `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.
279 Just like linkgit:git-submodule[1], this will detach the
280 submodules HEAD.
281
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 +
289 <branch>::
290 Branch to checkout; if it refers to a branch (i.e., a name that,
291 when prepended with "refs/heads/", is a valid ref), then that
builtin/checkout.c
+58 -8
@@ -44,6 +44,7 @@ struct checkout_opts {
44 int ignore_skipworktree;
45 int ignore_other_worktrees;
46 int show_progress;
47 + int overlay_mode;
48 /*
49 * If new checkout options are added, skip_merge_working_tree
50 * should be updated accordingly.
@@ -132,7 +133,8 @@ static int skip_same_name(const struct cache_entry *ce, int pos)
133 return pos;
134 }
135
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)
138 {
139 while (pos < active_nr &&
140 !strcmp(active_cache[pos]->name, ce->name)) {
@@ -140,6 +142,8 @@ static int check_stage(int stage, const struct cache_entry *ce, int pos)
142 return 0;
143 pos++;
144 }
145 + if (!overlay_mode)
146 + return 0;
147 if (stage == 2)
148 return error(_("path '%s' does not have our version"), ce->name);
149 else
@@ -165,7 +169,7 @@ static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
169 }
170
171 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)
173 {
174 while (pos < active_nr &&
175 !strcmp(active_cache[pos]->name, ce->name)) {
@@ -173,6 +177,10 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
177 return checkout_entry(active_cache[pos], state, NULL);
178 pos++;
179 }
180 + if (!overlay_mode) {
181 + unlink_entry(ce);
182 + return 0;
183 + }
184 if (stage == 2)
185 return error(_("path '%s' does not have our version"), ce->name);
186 else
@@ -247,9 +255,9 @@ static int checkout_merged(int pos, const struct checkout *state)
255 return status;
256 }
257
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)
261 {
262 ce->ce_flags &= ~CE_MATCHED;
263 if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
@@ -281,6 +289,25 @@ static void mark_ce_for_checkout(struct cache_entry *ce,
289 ce->ce_flags |= CE_MATCHED;
290 }
291
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 +
311 static int checkout_paths(const struct checkout_opts *opts,
312 const char *revision)
313 {
@@ -332,7 +359,14 @@ static int checkout_paths(const struct checkout_opts *opts,
359 * to be checked out.
360 */
361 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);
370
371 if (report_path_error(ps_matched, &opts->pathspec, opts->prefix)) {
372 free(ps_matched);
@@ -353,7 +387,7 @@ static int checkout_paths(const struct checkout_opts *opts,
387 if (opts->force) {
388 warning(_("path '%s' is unmerged"), ce->name);
389 } 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);
391 } else if (opts->merge) {
392 errs |= check_stages((1<<2) | (1<<3), ce, pos);
393 } else {
@@ -380,12 +414,14 @@ static int checkout_paths(const struct checkout_opts *opts,
414 continue;
415 }
416 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);
418 else if (opts->merge)
419 errs |= checkout_merged(pos, &state);
420 pos = skip_same_name(ce, pos) - 1;
421 }
422 }
423 + remove_marked_cache_entries(&the_index, 1);
424 + remove_scheduled_dirs();
425 errs |= finish_delayed_checkout(&state);
426
427 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,
583 * opts->show_progress only impacts output so doesn't require a merge
584 */
585
586 + /*
587 + * opts->overlay_mode cannot be used with switching branches so is
588 + * not tested here
589 + */
590 +
591 /*
592 * If we aren't creating a new branch any changes or updates will
593 * happen in the existing branch. Since that could only be updating
@@ -1183,6 +1224,10 @@ static int checkout_branch(struct checkout_opts *opts,
1224 die(_("'%s' cannot be used with switching branches"),
1225 "--patch");
1226
1227 + if (!opts->overlay_mode)
1228 + die(_("'%s' cannot be used with switching branches"),
1229 + "--no-overlay");
1230 +
1231 if (opts->writeout_stage)
1232 die(_("'%s' cannot be used with switching branches"),
1233 "--ours/--theirs");
@@ -1271,6 +1316,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1316 "checkout", "control recursive updating of submodules",
1317 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
1318 OPT_BOOL(0, "progress", &opts.show_progress, N_("force progress reporting")),
1319 + OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
1320 OPT_END(),
1321 };
1322
@@ -1279,6 +1325,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1325 opts.overwrite_ignore = 1;
1326 opts.prefix = prefix;
1327 opts.show_progress = -1;
1328 + opts.overlay_mode = -1;
1329
1330 git_config(git_checkout_config, &opts);
1331
@@ -1302,6 +1349,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1349 if ((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) > 1)
1350 die(_("-b, -B and --orphan are mutually exclusive"));
1351
1352 + if (opts.overlay_mode == 1 && opts.patch_mode)
1353 + die(_("-p and --overlay are mutually exclusive"));
1354 +
1355 /*
1356 * From here on, new_branch will contain the branch to be checked out,
1357 * and new_branch_force and new_orphan_branch will tell us which one of
t/t2025-checkout-no-overlay.sh new
+47
@@ -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
+1
@@ -1436,6 +1436,7 @@ test_expect_success 'double dash "git checkout"' '
1436 --progress Z
1437 --no-quiet Z
1438 --no-... Z
1439 + --overlay Z
1440 EOF
1441 '
1442