mv: add check_dir_in_index() and solve general dir check issue
[alt-git.git] / builtin / mv.c
blob2a38e2af4664dc9e6920862b4c029810ebcb03c8
1 /*
2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
5 */
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
7 #include "builtin.h"
8 #include "config.h"
9 #include "pathspec.h"
10 #include "lockfile.h"
11 #include "dir.h"
12 #include "cache-tree.h"
13 #include "string-list.h"
14 #include "parse-options.h"
15 #include "submodule.h"
16 #include "entry.h"
18 static const char * const builtin_mv_usage[] = {
19 N_("git mv [<options>] <source>... <destination>"),
20 NULL
23 enum update_mode {
24 BOTH = 0,
25 WORKING_DIRECTORY = (1 << 1),
26 INDEX = (1 << 2),
27 SPARSE = (1 << 3),
28 SKIP_WORKTREE_DIR = (1 << 4),
31 #define DUP_BASENAME 1
32 #define KEEP_TRAILING_SLASH 2
34 static const char **internal_prefix_pathspec(const char *prefix,
35 const char **pathspec,
36 int count, unsigned flags)
38 int i;
39 const char **result;
40 int prefixlen = prefix ? strlen(prefix) : 0;
41 ALLOC_ARRAY(result, count + 1);
43 /* Create an intermediate copy of the pathspec based on the flags */
44 for (i = 0; i < count; i++) {
45 int length = strlen(pathspec[i]);
46 int to_copy = length;
47 char *it;
48 while (!(flags & KEEP_TRAILING_SLASH) &&
49 to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1]))
50 to_copy--;
52 it = xmemdupz(pathspec[i], to_copy);
53 if (flags & DUP_BASENAME) {
54 result[i] = xstrdup(basename(it));
55 free(it);
56 } else {
57 result[i] = it;
60 result[count] = NULL;
62 /* Prefix the pathspec and free the old intermediate strings */
63 for (i = 0; i < count; i++) {
64 const char *match = prefix_path(prefix, prefixlen, result[i]);
65 free((char *) result[i]);
66 result[i] = match;
69 return result;
72 static const char *add_slash(const char *path)
74 size_t len = strlen(path);
75 if (path[len - 1] != '/') {
76 char *with_slash = xmalloc(st_add(len, 2));
77 memcpy(with_slash, path, len);
78 with_slash[len++] = '/';
79 with_slash[len] = 0;
80 return with_slash;
82 return path;
85 #define SUBMODULE_WITH_GITDIR ((const char *)1)
87 static void prepare_move_submodule(const char *src, int first,
88 const char **submodule_gitfile)
90 struct strbuf submodule_dotgit = STRBUF_INIT;
91 if (!S_ISGITLINK(active_cache[first]->ce_mode))
92 die(_("Directory %s is in index and no submodule?"), src);
93 if (!is_staging_gitmodules_ok(&the_index))
94 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
95 strbuf_addf(&submodule_dotgit, "%s/.git", src);
96 *submodule_gitfile = read_gitfile(submodule_dotgit.buf);
97 if (*submodule_gitfile)
98 *submodule_gitfile = xstrdup(*submodule_gitfile);
99 else
100 *submodule_gitfile = SUBMODULE_WITH_GITDIR;
101 strbuf_release(&submodule_dotgit);
104 static int index_range_of_same_dir(const char *src, int length,
105 int *first_p, int *last_p)
107 const char *src_w_slash = add_slash(src);
108 int first, last, len_w_slash = length + 1;
110 first = cache_name_pos(src_w_slash, len_w_slash);
111 if (first >= 0)
112 die(_("%.*s is in index"), len_w_slash, src_w_slash);
114 first = -1 - first;
115 for (last = first; last < active_nr; last++) {
116 const char *path = active_cache[last]->name;
117 if (strncmp(path, src_w_slash, len_w_slash))
118 break;
120 if (src_w_slash != src)
121 free((char *)src_w_slash);
122 *first_p = first;
123 *last_p = last;
124 return last - first;
128 * Check if an out-of-cone directory should be in the index. Imagine this case
129 * that all the files under a directory are marked with 'CE_SKIP_WORKTREE' bit
130 * and thus the directory is sparsified.
132 * Return 0 if such directory exist (i.e. with any of its contained files not
133 * marked with CE_SKIP_WORKTREE, the directory would be present in working tree).
134 * Return 1 otherwise.
136 static int check_dir_in_index(const char *name)
138 const char *with_slash = add_slash(name);
139 int length = strlen(with_slash);
141 int pos = cache_name_pos(with_slash, length);
142 const struct cache_entry *ce;
144 if (pos < 0) {
145 pos = -pos - 1;
146 if (pos >= the_index.cache_nr)
147 return 1;
148 ce = active_cache[pos];
149 if (strncmp(with_slash, ce->name, length))
150 return 1;
151 if (ce_skip_worktree(ce))
152 return 0;
154 return 1;
157 int cmd_mv(int argc, const char **argv, const char *prefix)
159 int i, flags, gitmodules_modified = 0;
160 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0, ignore_sparse = 0;
161 struct option builtin_mv_options[] = {
162 OPT__VERBOSE(&verbose, N_("be verbose")),
163 OPT__DRY_RUN(&show_only, N_("dry run")),
164 OPT__FORCE(&force, N_("force move/rename even if target exists"),
165 PARSE_OPT_NOCOMPLETE),
166 OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
167 OPT_BOOL(0, "sparse", &ignore_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
168 OPT_END(),
170 const char **source, **destination, **dest_path, **submodule_gitfile;
171 enum update_mode *modes;
172 struct stat st;
173 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
174 struct lock_file lock_file = LOCK_INIT;
175 struct cache_entry *ce;
176 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
178 git_config(git_default_config, NULL);
180 argc = parse_options(argc, argv, prefix, builtin_mv_options,
181 builtin_mv_usage, 0);
182 if (--argc < 1)
183 usage_with_options(builtin_mv_usage, builtin_mv_options);
185 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
186 if (read_cache() < 0)
187 die(_("index file corrupt"));
189 source = internal_prefix_pathspec(prefix, argv, argc, 0);
190 modes = xcalloc(argc, sizeof(enum update_mode));
192 * Keep trailing slash, needed to let
193 * "git mv file no-such-dir/" error out, except in the case
194 * "git mv directory no-such-dir/".
196 flags = KEEP_TRAILING_SLASH;
197 if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
198 flags = 0;
199 dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags);
200 submodule_gitfile = xcalloc(argc, sizeof(char *));
202 if (dest_path[0][0] == '\0')
203 /* special case: "." was normalized to "" */
204 destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
205 else if (!lstat(dest_path[0], &st) &&
206 S_ISDIR(st.st_mode)) {
207 dest_path[0] = add_slash(dest_path[0]);
208 destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
209 } else {
210 if (argc != 1)
211 die(_("destination '%s' is not a directory"), dest_path[0]);
212 destination = dest_path;
215 /* Checking */
216 for (i = 0; i < argc; i++) {
217 const char *src = source[i], *dst = destination[i];
218 int length;
219 const char *bad = NULL;
220 int skip_sparse = 0;
222 if (show_only)
223 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
225 length = strlen(src);
226 if (lstat(src, &st) < 0) {
227 int pos;
228 const struct cache_entry *ce;
230 pos = cache_name_pos(src, length);
231 if (pos < 0) {
232 const char *src_w_slash = add_slash(src);
233 if (!path_in_sparse_checkout(src_w_slash, &the_index) &&
234 !check_dir_in_index(src)) {
235 modes[i] |= SKIP_WORKTREE_DIR;
236 goto dir_check;
238 /* only error if existence is expected. */
239 if (!(modes[i] & SPARSE))
240 bad = _("bad source");
241 goto act_on_entry;
243 ce = active_cache[pos];
244 if (!ce_skip_worktree(ce)) {
245 bad = _("bad source");
246 goto act_on_entry;
248 if (!ignore_sparse) {
249 string_list_append(&only_match_skip_worktree, src);
250 goto act_on_entry;
252 /* Check if dst exists in index */
253 if (cache_name_pos(dst, strlen(dst)) < 0) {
254 modes[i] |= SPARSE;
255 goto act_on_entry;
257 if (!force) {
258 bad = _("destination exists");
259 goto act_on_entry;
261 modes[i] |= SPARSE;
262 goto act_on_entry;
264 if (!strncmp(src, dst, length) &&
265 (dst[length] == 0 || dst[length] == '/')) {
266 bad = _("can not move directory into itself");
267 goto act_on_entry;
269 if (S_ISDIR(st.st_mode)
270 && lstat(dst, &st) == 0) {
271 bad = _("cannot move directory over file");
272 goto act_on_entry;
275 dir_check:
276 if (S_ISDIR(st.st_mode)) {
277 int j, dst_len, n;
278 int first = cache_name_pos(src, length), last;
280 if (first >= 0) {
281 prepare_move_submodule(src, first,
282 submodule_gitfile + i);
283 goto act_on_entry;
284 } else if (index_range_of_same_dir(src, length,
285 &first, &last) < 1) {
286 bad = _("source directory is empty");
287 goto act_on_entry;
290 /* last - first >= 1 */
291 modes[i] |= WORKING_DIRECTORY;
292 n = argc + last - first;
293 REALLOC_ARRAY(source, n);
294 REALLOC_ARRAY(destination, n);
295 REALLOC_ARRAY(modes, n);
296 REALLOC_ARRAY(submodule_gitfile, n);
298 dst = add_slash(dst);
299 dst_len = strlen(dst);
301 for (j = 0; j < last - first; j++) {
302 const struct cache_entry *ce = active_cache[first + j];
303 const char *path = ce->name;
304 source[argc + j] = path;
305 destination[argc + j] =
306 prefix_path(dst, dst_len, path + length + 1);
307 memset(modes + argc + j, 0, sizeof(enum update_mode));
308 modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX;
309 submodule_gitfile[argc + j] = NULL;
311 argc += last - first;
312 goto act_on_entry;
314 if (!(ce = cache_file_exists(src, length, 0))) {
315 bad = _("not under version control");
316 goto act_on_entry;
318 if (ce_stage(ce)) {
319 bad = _("conflicted");
320 goto act_on_entry;
322 if (lstat(dst, &st) == 0 &&
323 (!ignore_case || strcasecmp(src, dst))) {
324 bad = _("destination exists");
325 if (force) {
327 * only files can overwrite each other:
328 * check both source and destination
330 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
331 if (verbose)
332 warning(_("overwriting '%s'"), dst);
333 bad = NULL;
334 } else
335 bad = _("Cannot overwrite");
337 goto act_on_entry;
339 if (string_list_has_string(&src_for_dst, dst)) {
340 bad = _("multiple sources for the same target");
341 goto act_on_entry;
343 if (is_dir_sep(dst[strlen(dst) - 1])) {
344 bad = _("destination directory does not exist");
345 goto act_on_entry;
349 * We check if the paths are in the sparse-checkout
350 * definition as a very final check, since that
351 * allows us to point the user to the --sparse
352 * option as a way to have a successful run.
354 if (!ignore_sparse &&
355 !path_in_sparse_checkout(src, &the_index)) {
356 string_list_append(&only_match_skip_worktree, src);
357 skip_sparse = 1;
359 if (!ignore_sparse &&
360 !path_in_sparse_checkout(dst, &the_index)) {
361 string_list_append(&only_match_skip_worktree, dst);
362 skip_sparse = 1;
365 if (skip_sparse)
366 goto remove_entry;
368 string_list_insert(&src_for_dst, dst);
370 act_on_entry:
371 if (!bad)
372 continue;
373 if (!ignore_errors)
374 die(_("%s, source=%s, destination=%s"),
375 bad, src, dst);
376 remove_entry:
377 if (--argc > 0) {
378 int n = argc - i;
379 memmove(source + i, source + i + 1,
380 n * sizeof(char *));
381 memmove(destination + i, destination + i + 1,
382 n * sizeof(char *));
383 memmove(modes + i, modes + i + 1,
384 n * sizeof(enum update_mode));
385 memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
386 n * sizeof(char *));
387 i--;
391 if (only_match_skip_worktree.nr) {
392 advise_on_updating_sparse_paths(&only_match_skip_worktree);
393 if (!ignore_errors)
394 return 1;
397 for (i = 0; i < argc; i++) {
398 const char *src = source[i], *dst = destination[i];
399 enum update_mode mode = modes[i];
400 int pos;
401 struct checkout state = CHECKOUT_INIT;
402 state.istate = &the_index;
404 if (force)
405 state.force = 1;
406 if (show_only || verbose)
407 printf(_("Renaming %s to %s\n"), src, dst);
408 if (show_only)
409 continue;
410 if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) &&
411 rename(src, dst) < 0) {
412 if (ignore_errors)
413 continue;
414 die_errno(_("renaming '%s' failed"), src);
416 if (submodule_gitfile[i]) {
417 if (!update_path_in_gitmodules(src, dst))
418 gitmodules_modified = 1;
419 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
420 connect_work_tree_and_git_dir(dst,
421 submodule_gitfile[i],
425 if (mode & (WORKING_DIRECTORY | SKIP_WORKTREE_DIR))
426 continue;
428 pos = cache_name_pos(src, strlen(src));
429 assert(pos >= 0);
430 rename_cache_entry_at(pos, dst);
432 if ((mode & SPARSE) &&
433 (path_in_sparse_checkout(dst, &the_index))) {
434 int dst_pos;
436 dst_pos = cache_name_pos(dst, strlen(dst));
437 active_cache[dst_pos]->ce_flags &= ~CE_SKIP_WORKTREE;
439 if (checkout_entry(active_cache[dst_pos], &state, NULL, NULL))
440 die(_("cannot checkout %s"), active_cache[dst_pos]->name);
444 if (gitmodules_modified)
445 stage_updated_gitmodules(&the_index);
447 if (write_locked_index(&the_index, &lock_file,
448 COMMIT_LOCK | SKIP_IF_UNCHANGED))
449 die(_("Unable to write new index file"));
451 string_list_clear(&src_for_dst, 0);
452 UNLEAK(source);
453 UNLEAK(dest_path);
454 free(submodule_gitfile);
455 free(modes);
456 return 0;