Commit 7d0c1f45 authored by Junio C Hamano's avatar Junio C Hamano

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*
parents 36eb1cb9 e92aa0e4
......@@ -260,6 +260,9 @@ the conflicted merge in the specified paths.
This means that you can use `git checkout -p` to selectively discard
edits from your current working tree. See the ``Interactive Mode''
section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
+
Note that this option uses the no overlay mode by default (see also
`--[no-]overlay`), and currently doesn't support overlay mode.
--ignore-other-worktrees::
`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.
Do not attempt to create a branch if a remote tracking branch
of the same name exists.
--[no-]overlay::
In the default overlay mode, `git checkout` never
removes files from the index or the working tree. When
specifying `--no-overlay`, files that appear in the index and
working tree, but not in <tree-ish> are removed, to make them
match <tree-ish> exactly.
<branch>::
Branch to checkout; if it refers to a branch (i.e., a name that,
when prepended with "refs/heads/", is a valid ref), then that
......
......@@ -46,6 +46,7 @@ struct checkout_opts {
int ignore_other_worktrees;
int show_progress;
int count_checkout_paths;
int overlay_mode;
/*
* If new checkout options are added, skip_merge_working_tree
* should be updated accordingly.
......@@ -135,7 +136,8 @@ static int skip_same_name(const struct cache_entry *ce, int pos)
return pos;
}
static int check_stage(int stage, const struct cache_entry *ce, int pos)
static int check_stage(int stage, const struct cache_entry *ce, int pos,
int overlay_mode)
{
while (pos < active_nr &&
!strcmp(active_cache[pos]->name, ce->name)) {
......@@ -143,6 +145,8 @@ static int check_stage(int stage, const struct cache_entry *ce, int pos)
return 0;
pos++;
}
if (!overlay_mode)
return 0;
if (stage == 2)
return error(_("path '%s' does not have our version"), ce->name);
else
......@@ -168,7 +172,8 @@ static int check_stages(unsigned stages, const struct cache_entry *ce, int pos)
}
static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
const struct checkout *state, int *nr_checkouts)
const struct checkout *state, int *nr_checkouts,
int overlay_mode)
{
while (pos < active_nr &&
!strcmp(active_cache[pos]->name, ce->name)) {
......@@ -177,6 +182,10 @@ static int checkout_stage(int stage, const struct cache_entry *ce, int pos,
NULL, nr_checkouts);
pos++;
}
if (!overlay_mode) {
unlink_entry(ce);
return 0;
}
if (stage == 2)
return error(_("path '%s' does not have our version"), ce->name);
else
......@@ -251,6 +260,59 @@ static int checkout_merged(int pos, const struct checkout *state, int *nr_checko
return status;
}
static void mark_ce_for_checkout_overlay(struct cache_entry *ce,
char *ps_matched,
const struct checkout_opts *opts)
{
ce->ce_flags &= ~CE_MATCHED;
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
return;
if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
/*
* "git checkout tree-ish -- path", but this entry
* is in the original index but is not in tree-ish
* or does not match the pathspec; it will not be
* checked out to the working tree. We will not do
* anything to this entry at all.
*/
return;
/*
* Either this entry came from the tree-ish we are
* checking the paths out of, or we are checking out
* of the index.
*
* If it comes from the tree-ish, we already know it
* matches the pathspec and could just stamp
* CE_MATCHED to it from update_some(). But we still
* need ps_matched and read_tree_recursive (and
* eventually tree_entry_interesting) cannot fill
* ps_matched yet. Once it can, we can avoid calling
* match_pathspec() for _all_ entries when
* opts->source_tree != NULL.
*/
if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched))
ce->ce_flags |= CE_MATCHED;
}
static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce,
char *ps_matched,
const struct checkout_opts *opts)
{
ce->ce_flags &= ~CE_MATCHED;
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
return;
if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) {
ce->ce_flags |= CE_MATCHED;
if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
/*
* In overlay mode, but the path is not in
* tree-ish, which means we should remove it
* from the index and the working tree.
*/
ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE;
}
}
static int checkout_paths(const struct checkout_opts *opts,
const char *revision)
{
......@@ -302,37 +364,15 @@ static int checkout_paths(const struct checkout_opts *opts,
* Make sure all pathspecs participated in locating the paths
* to be checked out.
*/
for (pos = 0; pos < active_nr; pos++) {
struct cache_entry *ce = active_cache[pos];
ce->ce_flags &= ~CE_MATCHED;
if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
continue;
if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
/*
* "git checkout tree-ish -- path", but this entry
* is in the original index; it will not be checked
* out to the working tree and it does not matter
* if pathspec matched this entry. We will not do
* anything to this entry at all.
*/
continue;
/*
* Either this entry came from the tree-ish we are
* checking the paths out of, or we are checking out
* of the index.
*
* If it comes from the tree-ish, we already know it
* matches the pathspec and could just stamp
* CE_MATCHED to it from update_some(). But we still
* need ps_matched and read_tree_recursive (and
* eventually tree_entry_interesting) cannot fill
* ps_matched yet. Once it can, we can avoid calling
* match_pathspec() for _all_ entries when
* opts->source_tree != NULL.
*/
if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched))
ce->ce_flags |= CE_MATCHED;
}
for (pos = 0; pos < active_nr; pos++)
if (opts->overlay_mode)
mark_ce_for_checkout_overlay(active_cache[pos],
ps_matched,
opts);
else
mark_ce_for_checkout_no_overlay(active_cache[pos],
ps_matched,
opts);
if (report_path_error(ps_matched, &opts->pathspec, opts->prefix)) {
free(ps_matched);
......@@ -353,7 +393,7 @@ static int checkout_paths(const struct checkout_opts *opts,
if (opts->force) {
warning(_("path '%s' is unmerged"), ce->name);
} else if (opts->writeout_stage) {
errs |= check_stage(opts->writeout_stage, ce, pos);
errs |= check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode);
} else if (opts->merge) {
errs |= check_stages((1<<2) | (1<<3), ce, pos);
} else {
......@@ -383,13 +423,16 @@ static int checkout_paths(const struct checkout_opts *opts,
if (opts->writeout_stage)
errs |= checkout_stage(opts->writeout_stage,
ce, pos,
&state, &nr_checkouts);
&state,
&nr_checkouts, opts->overlay_mode);
else if (opts->merge)
errs |= checkout_merged(pos, &state,
&nr_unmerged);
pos = skip_same_name(ce, pos) - 1;
}
}
remove_marked_cache_entries(&the_index, 1);
remove_scheduled_dirs();
errs |= finish_delayed_checkout(&state, &nr_checkouts);
if (opts->count_checkout_paths) {
......@@ -571,6 +614,11 @@ static int skip_merge_working_tree(const struct checkout_opts *opts,
* opts->show_progress only impacts output so doesn't require a merge
*/
/*
* opts->overlay_mode cannot be used with switching branches so is
* not tested here
*/
/*
* If we aren't creating a new branch any changes or updates will
* happen in the existing branch. Since that could only be updating
......@@ -1224,6 +1272,10 @@ static int checkout_branch(struct checkout_opts *opts,
die(_("'%s' cannot be used with switching branches"),
"--patch");
if (!opts->overlay_mode)
die(_("'%s' cannot be used with switching branches"),
"--no-overlay");
if (opts->writeout_stage)
die(_("'%s' cannot be used with switching branches"),
"--ours/--theirs");
......@@ -1312,6 +1364,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
"checkout", "control recursive updating of submodules",
PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
OPT_BOOL(0, "progress", &opts.show_progress, N_("force progress reporting")),
OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
OPT_END(),
};
......@@ -1320,6 +1373,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
opts.overwrite_ignore = 1;
opts.prefix = prefix;
opts.show_progress = -1;
opts.overlay_mode = -1;
git_config(git_checkout_config, &opts);
......@@ -1344,6 +1398,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
if ((!!opts.new_branch + !!opts.new_branch_force + !!opts.new_orphan_branch) > 1)
die(_("-b, -B and --orphan are mutually exclusive"));
if (opts.overlay_mode == 1 && opts.patch_mode)
die(_("-p and --overlay are mutually exclusive"));
/*
* From here on, new_branch will contain the branch to be checked out,
* and new_branch_force and new_orphan_branch will tell us which one of
......
......@@ -758,7 +758,7 @@ extern void rename_index_entry_at(struct index_state *, int pos, const char *new
/* Remove entry, return true if there are more entries to go. */
extern int remove_index_entry_at(struct index_state *, int pos);
extern void remove_marked_cache_entries(struct index_state *istate);
extern void remove_marked_cache_entries(struct index_state *istate, int invalidate);
extern int remove_file_from_index(struct index_state *, const char *path);
#define ADD_CACHE_VERBOSE 1
#define ADD_CACHE_PRETEND 2
......@@ -1569,6 +1569,11 @@ struct checkout {
extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath, int *nr_checkouts);
extern void enable_delayed_checkout(struct checkout *state);
extern int finish_delayed_checkout(struct checkout *state, int *nr_checkouts);
/*
* Unlink the last component and schedule the leading directories for
* removal, such that empty directories get removed.
*/
extern void unlink_entry(const struct cache_entry *ce);
struct cache_def {
struct strbuf path;
......
......@@ -441,6 +441,17 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state,
static struct strbuf path = STRBUF_INIT;
struct stat st;
if (ce->ce_flags & CE_WT_REMOVE) {
if (topath)
/*
* No content and thus no path to create, so we have
* no pathname to return.
*/
BUG("Can't remove entry to a path");
unlink_entry(ce);
return 0;
}
if (topath)
return write_entry(ce, topath, state, 1);
......@@ -510,3 +521,18 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state,
(*nr_checkouts)++;
return write_entry(ce, path.buf, state, 0);
}
void unlink_entry(const struct cache_entry *ce)
{
const struct submodule *sub = submodule_from_ce(ce);
if (sub) {
/* state.force is set at the caller. */
submodule_move_head(ce->name, "HEAD", NULL,
SUBMODULE_MOVE_HEAD_FORCE);
}
if (!check_leading_path(ce->name, ce_namelen(ce)))
return;
if (remove_or_warn(ce->ce_mode, ce->name))
return;
schedule_dir_for_removal(ce->name, ce_namelen(ce));
}
......@@ -588,13 +588,19 @@ int remove_index_entry_at(struct index_state *istate, int pos)
* CE_REMOVE is set in ce_flags. This is much more effective than
* calling remove_index_entry_at() for each entry to be removed.
*/
void remove_marked_cache_entries(struct index_state *istate)
void remove_marked_cache_entries(struct index_state *istate, int invalidate)
{
struct cache_entry **ce_array = istate->cache;
unsigned int i, j;
for (i = j = 0; i < istate->cache_nr; i++) {
if (ce_array[i]->ce_flags & CE_REMOVE) {
if (invalidate) {
cache_tree_invalidate_path(istate,
ce_array[i]->name);
untracked_cache_remove_from_index(istate,
ce_array[i]->name);
}
remove_name_hash(istate, ce_array[i]);
save_or_free_index_entry(istate, ce_array[i]);
}
......
......@@ -162,7 +162,7 @@ void merge_base_index(struct index_state *istate)
ewah_each_bit(si->replace_bitmap, replace_entry, istate);
ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
if (si->nr_deletions)
remove_marked_cache_entries(istate);
remove_marked_cache_entries(istate, 0);
for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
if (!ce_namelen(si->saved_cache[i]))
......
#!/bin/sh
test_description='checkout --no-overlay <tree-ish> -- <pathspec>'
. ./test-lib.sh
test_expect_success 'setup' '
git commit --allow-empty -m "initial"
'
test_expect_success 'checkout --no-overlay deletes files not in <tree-ish>' '
>file &&
mkdir dir &&
>dir/file1 &&
git add file dir/file1 &&
git checkout --no-overlay HEAD -- file &&
test_path_is_missing file &&
test_path_is_file dir/file1
'
test_expect_success 'checkout --no-overlay removing last file from directory' '
git checkout --no-overlay HEAD -- dir/file1 &&
test_path_is_missing dir
'
test_expect_success 'checkout -p --overlay is disallowed' '
test_must_fail git checkout -p --overlay HEAD 2>actual &&
test_i18ngrep "fatal: -p and --overlay are mutually exclusive" actual
'
test_expect_success '--no-overlay --theirs with D/F conflict deletes file' '
test_commit file1 file1 &&
test_commit file2 file2 &&
git rm --cached file1 &&
echo 1234 >file1 &&
F1=$(git rev-parse HEAD:file1) &&
F2=$(git rev-parse HEAD:file2) &&
{
echo "100644 $F1 1 file1" &&
echo "100644 $F2 2 file1"
} | git update-index --index-info &&
test_path_is_file file1 &&
git checkout --theirs --no-overlay -- file1 &&
test_path_is_missing file1
'
test_done
......@@ -1437,6 +1437,7 @@ test_expect_success 'double dash "git checkout"' '
--guess Z
--no-guess Z
--no-... Z
--overlay Z
EOF
'
......
......@@ -299,25 +299,6 @@ static void load_gitmodules_file(struct index_state *index,
}
}
/*
* Unlink the last component and schedule the leading directories for
* removal, such that empty directories get removed.
*/
static void unlink_entry(const struct cache_entry *ce)
{
const struct submodule *sub = submodule_from_ce(ce);
if (sub) {
/* state.force is set at the caller. */
submodule_move_head(ce->name, "HEAD", NULL,
SUBMODULE_MOVE_HEAD_FORCE);
}
if (!check_leading_path(ce->name, ce_namelen(ce)))
return;
if (remove_or_warn(ce->ce_mode, ce->name))
return;
schedule_dir_for_removal(ce->name, ce_namelen(ce));
}
static struct progress *get_progress(struct unpack_trees_options *o)
{
unsigned cnt = 0, total = 0;
......@@ -410,7 +391,7 @@ static int check_updates(struct unpack_trees_options *o)
unlink_entry(ce);
}
}
remove_marked_cache_entries(index);
remove_marked_cache_entries(index, 0);
remove_scheduled_dirs();
if (should_update_submodules() && o->update && !o->dry_run)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment