зеркало из https://github.com/microsoft/git.git
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
This commit is contained in:
Коммит
0455aad1e3
241
builtin/mv.c
241
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;
|
||||
}
|
||||
} else if (!(ce = cache_file_exists(src, length, 0))) {
|
||||
|
||||
/* 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;
|
||||
}
|
||||
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;
|
||||
|
||||
string_list_insert(&src_for_dst, dst);
|
||||
goto act_on_entry;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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)
|
||||
|
|
|
@ -1828,4 +1828,29 @@ test_expect_success 'checkout behaves oddly with df-conflict-2' '
|
|||
test_cmp full-checkout-err sparse-index-err
|
||||
'
|
||||
|
||||
test_expect_success 'mv directory from out-of-cone to in-cone' '
|
||||
init_repos &&
|
||||
|
||||
# <source> as a sparse directory (or SKIP_WORKTREE_DIR without enabling
|
||||
# sparse index).
|
||||
test_all_match git mv --sparse folder1 deep &&
|
||||
test_all_match git status --porcelain=v2 &&
|
||||
test_sparse_match git ls-files -t &&
|
||||
git -C sparse-checkout ls-files -t >actual &&
|
||||
grep -e "H deep/folder1/0/0/0" actual &&
|
||||
grep -e "H deep/folder1/0/1" actual &&
|
||||
grep -e "H deep/folder1/a" actual &&
|
||||
|
||||
test_all_match git reset --hard &&
|
||||
|
||||
# <source> as a directory deeper than sparse index boundary (where
|
||||
# sparse index will expand).
|
||||
test_sparse_match git mv --sparse folder1/0 deep &&
|
||||
test_sparse_match git status --porcelain=v2 &&
|
||||
test_sparse_match git ls-files -t &&
|
||||
git -C sparse-checkout ls-files -t >actual &&
|
||||
grep -e "H deep/0/0/0" actual &&
|
||||
grep -e "H deep/0/1" actual
|
||||
'
|
||||
|
||||
test_done
|
||||
|
|
|
@ -4,6 +4,18 @@ test_description='git mv in sparse working trees'
|
|||
|
||||
. ./test-lib.sh
|
||||
|
||||
setup_sparse_checkout () {
|
||||
mkdir folder1 &&
|
||||
touch folder1/file1 &&
|
||||
git add folder1 &&
|
||||
git sparse-checkout set --cone sub
|
||||
}
|
||||
|
||||
cleanup_sparse_checkout () {
|
||||
git sparse-checkout disable &&
|
||||
git reset --hard
|
||||
}
|
||||
|
||||
test_expect_success 'setup' "
|
||||
mkdir -p sub/dir sub/dir2 &&
|
||||
touch a b c sub/d sub/dir/e sub/dir2/e &&
|
||||
|
@ -196,6 +208,7 @@ test_expect_success 'can move files to non-sparse dir' '
|
|||
'
|
||||
|
||||
test_expect_success 'refuse to move file to non-skip-worktree sparse path' '
|
||||
test_when_finished "cleanup_sparse_checkout" &&
|
||||
git reset --hard &&
|
||||
git sparse-checkout init --no-cone &&
|
||||
git sparse-checkout set a !/x y/ !x/y/z &&
|
||||
|
@ -206,4 +219,75 @@ test_expect_success 'refuse to move file to non-skip-worktree sparse path' '
|
|||
test_cmp expect stderr
|
||||
'
|
||||
|
||||
test_expect_success 'refuse to move out-of-cone directory without --sparse' '
|
||||
test_when_finished "cleanup_sparse_checkout" &&
|
||||
setup_sparse_checkout &&
|
||||
|
||||
test_must_fail git mv folder1 sub 2>stderr &&
|
||||
cat sparse_error_header >expect &&
|
||||
echo folder1/file1 >>expect &&
|
||||
cat sparse_hint >>expect &&
|
||||
test_cmp expect stderr
|
||||
'
|
||||
|
||||
test_expect_success 'can move out-of-cone directory with --sparse' '
|
||||
test_when_finished "cleanup_sparse_checkout" &&
|
||||
setup_sparse_checkout &&
|
||||
|
||||
git mv --sparse folder1 sub 2>stderr &&
|
||||
test_must_be_empty stderr &&
|
||||
|
||||
test_path_is_dir sub/folder1 &&
|
||||
test_path_is_file sub/folder1/file1
|
||||
'
|
||||
|
||||
test_expect_success 'refuse to move out-of-cone file without --sparse' '
|
||||
test_when_finished "cleanup_sparse_checkout" &&
|
||||
setup_sparse_checkout &&
|
||||
|
||||
test_must_fail git mv folder1/file1 sub 2>stderr &&
|
||||
cat sparse_error_header >expect &&
|
||||
echo folder1/file1 >>expect &&
|
||||
cat sparse_hint >>expect &&
|
||||
test_cmp expect stderr
|
||||
'
|
||||
|
||||
test_expect_success 'can move out-of-cone file with --sparse' '
|
||||
test_when_finished "cleanup_sparse_checkout" &&
|
||||
setup_sparse_checkout &&
|
||||
|
||||
git mv --sparse folder1/file1 sub 2>stderr &&
|
||||
test_must_be_empty stderr &&
|
||||
|
||||
test_path_is_file sub/file1
|
||||
'
|
||||
|
||||
test_expect_success 'refuse to move sparse file to existing destination' '
|
||||
test_when_finished "cleanup_sparse_checkout" &&
|
||||
mkdir folder1 &&
|
||||
touch folder1/file1 &&
|
||||
touch sub/file1 &&
|
||||
git add folder1 sub/file1 &&
|
||||
git sparse-checkout set --cone sub &&
|
||||
|
||||
test_must_fail git mv --sparse folder1/file1 sub 2>stderr &&
|
||||
echo "fatal: destination exists, source=folder1/file1, destination=sub/file1" >expect &&
|
||||
test_cmp expect stderr
|
||||
'
|
||||
|
||||
test_expect_success 'move sparse file to existing destination with --force and --sparse' '
|
||||
test_when_finished "cleanup_sparse_checkout" &&
|
||||
mkdir folder1 &&
|
||||
touch folder1/file1 &&
|
||||
touch sub/file1 &&
|
||||
echo "overwrite" >folder1/file1 &&
|
||||
git add folder1 sub/file1 &&
|
||||
git sparse-checkout set --cone sub &&
|
||||
|
||||
git mv --sparse --force folder1/file1 sub 2>stderr &&
|
||||
test_must_be_empty stderr &&
|
||||
echo "overwrite" >expect &&
|
||||
test_cmp expect sub/file1
|
||||
'
|
||||
|
||||
test_done
|
||||
|
|
Загрузка…
Ссылка в новой задаче