Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2022-07-15 01:03:59 +0300
committerJunio C Hamano <gitster@pobox.com>2022-07-15 01:04:00 +0300
commit0455aad1e39f21acbaa8a84129fec8eb77682e0d (patch)
tree1df8116ca1240d3b5021f4ab044f387bb1c25bbd /builtin
parent73b9ef6ab18fa7c9631f24eba0c67357067a1267 (diff)
parentb91a2b6594a1da7d4d2f936f7db25bfa5a3d775e (diff)
Merge branch 'sy/mv-out-of-cone'
"git mv A B" in a sparsely populated working tree can be asked to move a path between directories that are "in cone" (i.e. expected to be materialized in the working tree) and "out of cone" (i.e. expected to be hidden). The handling of such cases has been improved. * sy/mv-out-of-cone: mv: add check_dir_in_index() and solve general dir check issue mv: use flags mode for update_mode mv: check if <destination> exists in index to handle overwriting mv: check if out-of-cone file exists in index with SKIP_WORKTREE bit mv: decouple if/else-if checks using goto mv: update sparsity after moving from out-of-cone to in-cone t1092: mv directory from out-of-cone to in-cone t7002: add tests for moving out-of-cone file/directory
Diffstat (limited to 'builtin')
-rw-r--r--builtin/mv.c239
1 files changed, 175 insertions, 64 deletions
diff --git a/builtin/mv.c b/builtin/mv.c
index 83a465ba83..2a38e2af46 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -13,12 +13,21 @@
#include "string-list.h"
#include "parse-options.h"
#include "submodule.h"
+#include "entry.h"
static const char * const builtin_mv_usage[] = {
N_("git mv [<options>] <source>... <destination>"),
NULL
};
+enum update_mode {
+ BOTH = 0,
+ WORKING_DIRECTORY = (1 << 1),
+ INDEX = (1 << 2),
+ SPARSE = (1 << 3),
+ SKIP_WORKTREE_DIR = (1 << 4),
+};
+
#define DUP_BASENAME 1
#define KEEP_TRAILING_SLASH 2
@@ -115,6 +124,36 @@ static int index_range_of_same_dir(const char *src, int length,
return last - first;
}
+/*
+ * Check if an out-of-cone directory should be in the index. Imagine this case
+ * that all the files under a directory are marked with 'CE_SKIP_WORKTREE' bit
+ * and thus the directory is sparsified.
+ *
+ * Return 0 if such directory exist (i.e. with any of its contained files not
+ * marked with CE_SKIP_WORKTREE, the directory would be present in working tree).
+ * Return 1 otherwise.
+ */
+static int check_dir_in_index(const char *name)
+{
+ const char *with_slash = add_slash(name);
+ int length = strlen(with_slash);
+
+ int pos = cache_name_pos(with_slash, length);
+ const struct cache_entry *ce;
+
+ if (pos < 0) {
+ pos = -pos - 1;
+ if (pos >= the_index.cache_nr)
+ return 1;
+ ce = active_cache[pos];
+ if (strncmp(with_slash, ce->name, length))
+ return 1;
+ if (ce_skip_worktree(ce))
+ return 0;
+ }
+ return 1;
+}
+
int cmd_mv(int argc, const char **argv, const char *prefix)
{
int i, flags, gitmodules_modified = 0;
@@ -129,7 +168,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
OPT_END(),
};
const char **source, **destination, **dest_path, **submodule_gitfile;
- enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX, SPARSE } *modes;
+ enum update_mode *modes;
struct stat st;
struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
struct lock_file lock_file = LOCK_INIT;
@@ -176,7 +215,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
/* Checking */
for (i = 0; i < argc; i++) {
const char *src = source[i], *dst = destination[i];
- int length, src_is_dir;
+ int length;
const char *bad = NULL;
int skip_sparse = 0;
@@ -185,54 +224,103 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
length = strlen(src);
if (lstat(src, &st) < 0) {
- /* only error if existence is expected. */
- if (modes[i] != SPARSE)
+ int pos;
+ const struct cache_entry *ce;
+
+ pos = cache_name_pos(src, length);
+ if (pos < 0) {
+ const char *src_w_slash = add_slash(src);
+ if (!path_in_sparse_checkout(src_w_slash, &the_index) &&
+ !check_dir_in_index(src)) {
+ modes[i] |= SKIP_WORKTREE_DIR;
+ goto dir_check;
+ }
+ /* only error if existence is expected. */
+ if (!(modes[i] & SPARSE))
+ bad = _("bad source");
+ goto act_on_entry;
+ }
+ ce = active_cache[pos];
+ if (!ce_skip_worktree(ce)) {
bad = _("bad source");
- } else if (!strncmp(src, dst, length) &&
- (dst[length] == 0 || dst[length] == '/')) {
+ goto act_on_entry;
+ }
+ if (!ignore_sparse) {
+ string_list_append(&only_match_skip_worktree, src);
+ goto act_on_entry;
+ }
+ /* Check if dst exists in index */
+ if (cache_name_pos(dst, strlen(dst)) < 0) {
+ modes[i] |= SPARSE;
+ goto act_on_entry;
+ }
+ if (!force) {
+ bad = _("destination exists");
+ goto act_on_entry;
+ }
+ modes[i] |= SPARSE;
+ goto act_on_entry;
+ }
+ if (!strncmp(src, dst, length) &&
+ (dst[length] == 0 || dst[length] == '/')) {
bad = _("can not move directory into itself");
- } else if ((src_is_dir = S_ISDIR(st.st_mode))
- && lstat(dst, &st) == 0)
+ goto act_on_entry;
+ }
+ if (S_ISDIR(st.st_mode)
+ && lstat(dst, &st) == 0) {
bad = _("cannot move directory over file");
- else if (src_is_dir) {
+ goto act_on_entry;
+ }
+
+dir_check:
+ if (S_ISDIR(st.st_mode)) {
+ int j, dst_len, n;
int first = cache_name_pos(src, length), last;
- if (first >= 0)
+ if (first >= 0) {
prepare_move_submodule(src, first,
submodule_gitfile + i);
- else if (index_range_of_same_dir(src, length,
- &first, &last) < 1)
+ goto act_on_entry;
+ } else if (index_range_of_same_dir(src, length,
+ &first, &last) < 1) {
bad = _("source directory is empty");
- else { /* last - first >= 1 */
- int j, dst_len, n;
-
- modes[i] = WORKING_DIRECTORY;
- n = argc + last - first;
- REALLOC_ARRAY(source, n);
- REALLOC_ARRAY(destination, n);
- REALLOC_ARRAY(modes, n);
- REALLOC_ARRAY(submodule_gitfile, n);
-
- dst = add_slash(dst);
- dst_len = strlen(dst);
-
- for (j = 0; j < last - first; j++) {
- const struct cache_entry *ce = active_cache[first + j];
- const char *path = ce->name;
- source[argc + j] = path;
- destination[argc + j] =
- prefix_path(dst, dst_len, path + length + 1);
- modes[argc + j] = ce_skip_worktree(ce) ? SPARSE : INDEX;
- submodule_gitfile[argc + j] = NULL;
- }
- argc += last - first;
+ goto act_on_entry;
+ }
+
+ /* last - first >= 1 */
+ modes[i] |= WORKING_DIRECTORY;
+ n = argc + last - first;
+ REALLOC_ARRAY(source, n);
+ REALLOC_ARRAY(destination, n);
+ REALLOC_ARRAY(modes, n);
+ REALLOC_ARRAY(submodule_gitfile, n);
+
+ dst = add_slash(dst);
+ dst_len = strlen(dst);
+
+ for (j = 0; j < last - first; j++) {
+ const struct cache_entry *ce = active_cache[first + j];
+ const char *path = ce->name;
+ source[argc + j] = path;
+ destination[argc + j] =
+ prefix_path(dst, dst_len, path + length + 1);
+ memset(modes + argc + j, 0, sizeof(enum update_mode));
+ modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX;
+ submodule_gitfile[argc + j] = NULL;
}
- } else if (!(ce = cache_file_exists(src, length, 0))) {
+ argc += last - first;
+ goto act_on_entry;
+ }
+ if (!(ce = cache_file_exists(src, length, 0))) {
bad = _("not under version control");
- } else if (ce_stage(ce)) {
+ goto act_on_entry;
+ }
+ if (ce_stage(ce)) {
bad = _("conflicted");
- } else if (lstat(dst, &st) == 0 &&
- (!ignore_case || strcasecmp(src, dst))) {
+ goto act_on_entry;
+ }
+ if (lstat(dst, &st) == 0 &&
+ (!ignore_case || strcasecmp(src, dst))) {
bad = _("destination exists");
if (force) {
/*
@@ -246,34 +334,40 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
} else
bad = _("Cannot overwrite");
}
- } else if (string_list_has_string(&src_for_dst, dst))
+ goto act_on_entry;
+ }
+ if (string_list_has_string(&src_for_dst, dst)) {
bad = _("multiple sources for the same target");
- else if (is_dir_sep(dst[strlen(dst) - 1]))
+ goto act_on_entry;
+ }
+ if (is_dir_sep(dst[strlen(dst) - 1])) {
bad = _("destination directory does not exist");
- else {
- /*
- * We check if the paths are in the sparse-checkout
- * definition as a very final check, since that
- * allows us to point the user to the --sparse
- * option as a way to have a successful run.
- */
- if (!ignore_sparse &&
- !path_in_sparse_checkout(src, &the_index)) {
- string_list_append(&only_match_skip_worktree, src);
- skip_sparse = 1;
- }
- if (!ignore_sparse &&
- !path_in_sparse_checkout(dst, &the_index)) {
- string_list_append(&only_match_skip_worktree, dst);
- skip_sparse = 1;
- }
-
- if (skip_sparse)
- goto remove_entry;
+ goto act_on_entry;
+ }
- string_list_insert(&src_for_dst, dst);
+ /*
+ * We check if the paths are in the sparse-checkout
+ * definition as a very final check, since that
+ * allows us to point the user to the --sparse
+ * option as a way to have a successful run.
+ */
+ if (!ignore_sparse &&
+ !path_in_sparse_checkout(src, &the_index)) {
+ string_list_append(&only_match_skip_worktree, src);
+ skip_sparse = 1;
}
+ if (!ignore_sparse &&
+ !path_in_sparse_checkout(dst, &the_index)) {
+ string_list_append(&only_match_skip_worktree, dst);
+ skip_sparse = 1;
+ }
+
+ if (skip_sparse)
+ goto remove_entry;
+
+ string_list_insert(&src_for_dst, dst);
+act_on_entry:
if (!bad)
continue;
if (!ignore_errors)
@@ -304,11 +398,17 @@ remove_entry:
const char *src = source[i], *dst = destination[i];
enum update_mode mode = modes[i];
int pos;
+ struct checkout state = CHECKOUT_INIT;
+ state.istate = &the_index;
+
+ if (force)
+ state.force = 1;
if (show_only || verbose)
printf(_("Renaming %s to %s\n"), src, dst);
if (show_only)
continue;
- if (mode != INDEX && mode != SPARSE && rename(src, dst) < 0) {
+ if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) &&
+ rename(src, dst) < 0) {
if (ignore_errors)
continue;
die_errno(_("renaming '%s' failed"), src);
@@ -322,12 +422,23 @@ remove_entry:
1);
}
- if (mode == WORKING_DIRECTORY)
+ if (mode & (WORKING_DIRECTORY | SKIP_WORKTREE_DIR))
continue;
pos = cache_name_pos(src, strlen(src));
assert(pos >= 0);
rename_cache_entry_at(pos, dst);
+
+ if ((mode & SPARSE) &&
+ (path_in_sparse_checkout(dst, &the_index))) {
+ int dst_pos;
+
+ dst_pos = cache_name_pos(dst, strlen(dst));
+ active_cache[dst_pos]->ce_flags &= ~CE_SKIP_WORKTREE;
+
+ if (checkout_entry(active_cache[dst_pos], &state, NULL, NULL))
+ die(_("cannot checkout %s"), active_cache[dst_pos]->name);
+ }
}
if (gitmodules_modified)