]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/mv.c
submodule-config.c: strengthen URL fsck check
[thirdparty/git.git] / builtin / mv.c
1 /*
2 * "git mv" builtin command
3 *
4 * Copyright (C) 2006 Johannes Schindelin
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "builtin.h"
8 #include "abspath.h"
9 #include "advice.h"
10 #include "config.h"
11 #include "environment.h"
12 #include "gettext.h"
13 #include "name-hash.h"
14 #include "object-file.h"
15 #include "pathspec.h"
16 #include "lockfile.h"
17 #include "dir.h"
18 #include "cache-tree.h"
19 #include "string-list.h"
20 #include "parse-options.h"
21 #include "read-cache-ll.h"
22 #include "repository.h"
23 #include "setup.h"
24 #include "submodule.h"
25 #include "entry.h"
26
27 static const char * const builtin_mv_usage[] = {
28 N_("git mv [<options>] <source>... <destination>"),
29 NULL
30 };
31
32 enum update_mode {
33 WORKING_DIRECTORY = (1 << 1),
34 INDEX = (1 << 2),
35 SPARSE = (1 << 3),
36 SKIP_WORKTREE_DIR = (1 << 4),
37 };
38
39 #define DUP_BASENAME 1
40 #define KEEP_TRAILING_SLASH 2
41
42 static const char **internal_prefix_pathspec(const char *prefix,
43 const char **pathspec,
44 int count, unsigned flags)
45 {
46 int i;
47 const char **result;
48 int prefixlen = prefix ? strlen(prefix) : 0;
49 ALLOC_ARRAY(result, count + 1);
50
51 /* Create an intermediate copy of the pathspec based on the flags */
52 for (i = 0; i < count; i++) {
53 int length = strlen(pathspec[i]);
54 int to_copy = length;
55 char *it;
56 while (!(flags & KEEP_TRAILING_SLASH) &&
57 to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1]))
58 to_copy--;
59
60 it = xmemdupz(pathspec[i], to_copy);
61 if (flags & DUP_BASENAME) {
62 result[i] = xstrdup(basename(it));
63 free(it);
64 } else {
65 result[i] = it;
66 }
67 }
68 result[count] = NULL;
69
70 /* Prefix the pathspec and free the old intermediate strings */
71 for (i = 0; i < count; i++) {
72 const char *match = prefix_path(prefix, prefixlen, result[i]);
73 free((char *) result[i]);
74 result[i] = match;
75 }
76
77 return result;
78 }
79
80 static const char *add_slash(const char *path)
81 {
82 size_t len = strlen(path);
83 if (len && path[len - 1] != '/') {
84 char *with_slash = xmalloc(st_add(len, 2));
85 memcpy(with_slash, path, len);
86 with_slash[len++] = '/';
87 with_slash[len] = 0;
88 return with_slash;
89 }
90 return path;
91 }
92
93 #define SUBMODULE_WITH_GITDIR ((const char *)1)
94
95 static void prepare_move_submodule(const char *src, int first,
96 const char **submodule_gitfile)
97 {
98 struct strbuf submodule_dotgit = STRBUF_INIT;
99 if (!S_ISGITLINK(the_index.cache[first]->ce_mode))
100 die(_("Directory %s is in index and no submodule?"), src);
101 if (!is_staging_gitmodules_ok(&the_index))
102 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
103 strbuf_addf(&submodule_dotgit, "%s/.git", src);
104 *submodule_gitfile = read_gitfile(submodule_dotgit.buf);
105 if (*submodule_gitfile)
106 *submodule_gitfile = xstrdup(*submodule_gitfile);
107 else
108 *submodule_gitfile = SUBMODULE_WITH_GITDIR;
109 strbuf_release(&submodule_dotgit);
110 }
111
112 static int index_range_of_same_dir(const char *src, int length,
113 int *first_p, int *last_p)
114 {
115 const char *src_w_slash = add_slash(src);
116 int first, last, len_w_slash = length + 1;
117
118 first = index_name_pos(&the_index, src_w_slash, len_w_slash);
119 if (first >= 0)
120 die(_("%.*s is in index"), len_w_slash, src_w_slash);
121
122 first = -1 - first;
123 for (last = first; last < the_index.cache_nr; last++) {
124 const char *path = the_index.cache[last]->name;
125 if (strncmp(path, src_w_slash, len_w_slash))
126 break;
127 }
128 if (src_w_slash != src)
129 free((char *)src_w_slash);
130 *first_p = first;
131 *last_p = last;
132 return last - first;
133 }
134
135 /*
136 * Given the path of a directory that does not exist on-disk, check whether the
137 * directory contains any entries in the index with the SKIP_WORKTREE flag
138 * enabled.
139 * Return 1 if such index entries exist.
140 * Return 0 otherwise.
141 */
142 static int empty_dir_has_sparse_contents(const char *name)
143 {
144 int ret = 0;
145 const char *with_slash = add_slash(name);
146 int length = strlen(with_slash);
147
148 int pos = index_name_pos(&the_index, with_slash, length);
149 const struct cache_entry *ce;
150
151 if (pos < 0) {
152 pos = -pos - 1;
153 if (pos >= the_index.cache_nr)
154 goto free_return;
155 ce = the_index.cache[pos];
156 if (strncmp(with_slash, ce->name, length))
157 goto free_return;
158 if (ce_skip_worktree(ce))
159 ret = 1;
160 }
161
162 free_return:
163 if (with_slash != name)
164 free((char *)with_slash);
165 return ret;
166 }
167
168 int cmd_mv(int argc, const char **argv, const char *prefix)
169 {
170 int i, flags, gitmodules_modified = 0;
171 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0, ignore_sparse = 0;
172 struct option builtin_mv_options[] = {
173 OPT__VERBOSE(&verbose, N_("be verbose")),
174 OPT__DRY_RUN(&show_only, N_("dry run")),
175 OPT__FORCE(&force, N_("force move/rename even if target exists"),
176 PARSE_OPT_NOCOMPLETE),
177 OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
178 OPT_BOOL(0, "sparse", &ignore_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
179 OPT_END(),
180 };
181 const char **source, **destination, **dest_path, **submodule_gitfile;
182 const char *dst_w_slash;
183 const char **src_dir = NULL;
184 int src_dir_nr = 0, src_dir_alloc = 0;
185 struct strbuf a_src_dir = STRBUF_INIT;
186 enum update_mode *modes, dst_mode = 0;
187 struct stat st, dest_st;
188 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
189 struct lock_file lock_file = LOCK_INIT;
190 struct cache_entry *ce;
191 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
192 struct string_list dirty_paths = STRING_LIST_INIT_NODUP;
193
194 git_config(git_default_config, NULL);
195
196 argc = parse_options(argc, argv, prefix, builtin_mv_options,
197 builtin_mv_usage, 0);
198 if (--argc < 1)
199 usage_with_options(builtin_mv_usage, builtin_mv_options);
200
201 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
202 if (repo_read_index(the_repository) < 0)
203 die(_("index file corrupt"));
204
205 source = internal_prefix_pathspec(prefix, argv, argc, 0);
206 CALLOC_ARRAY(modes, argc);
207
208 /*
209 * Keep trailing slash, needed to let
210 * "git mv file no-such-dir/" error out, except in the case
211 * "git mv directory no-such-dir/".
212 */
213 flags = KEEP_TRAILING_SLASH;
214 if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
215 flags = 0;
216 dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags);
217 dst_w_slash = add_slash(dest_path[0]);
218 submodule_gitfile = xcalloc(argc, sizeof(char *));
219
220 if (dest_path[0][0] == '\0')
221 /* special case: "." was normalized to "" */
222 destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
223 else if (!lstat(dest_path[0], &st) &&
224 S_ISDIR(st.st_mode)) {
225 destination = internal_prefix_pathspec(dst_w_slash, argv, argc, DUP_BASENAME);
226 } else {
227 if (!path_in_sparse_checkout(dst_w_slash, &the_index) &&
228 empty_dir_has_sparse_contents(dst_w_slash)) {
229 destination = internal_prefix_pathspec(dst_w_slash, argv, argc, DUP_BASENAME);
230 dst_mode = SKIP_WORKTREE_DIR;
231 } else if (argc != 1) {
232 die(_("destination '%s' is not a directory"), dest_path[0]);
233 } else {
234 destination = dest_path;
235 /*
236 * <destination> is a file outside of sparse-checkout
237 * cone. Insist on cone mode here for backward
238 * compatibility. We don't want dst_mode to be assigned
239 * for a file when the repo is using no-cone mode (which
240 * is deprecated at this point) sparse-checkout. As
241 * SPARSE here is only considering cone-mode situation.
242 */
243 if (!path_in_cone_mode_sparse_checkout(destination[0], &the_index))
244 dst_mode = SPARSE;
245 }
246 }
247 if (dst_w_slash != dest_path[0]) {
248 free((char *)dst_w_slash);
249 dst_w_slash = NULL;
250 }
251
252 /* Checking */
253 for (i = 0; i < argc; i++) {
254 const char *src = source[i], *dst = destination[i];
255 int length;
256 const char *bad = NULL;
257 int skip_sparse = 0;
258
259 if (show_only)
260 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
261
262 length = strlen(src);
263 if (lstat(src, &st) < 0) {
264 int pos;
265 const struct cache_entry *ce;
266
267 pos = index_name_pos(&the_index, src, length);
268 if (pos < 0) {
269 const char *src_w_slash = add_slash(src);
270 if (!path_in_sparse_checkout(src_w_slash, &the_index) &&
271 empty_dir_has_sparse_contents(src)) {
272 modes[i] |= SKIP_WORKTREE_DIR;
273 goto dir_check;
274 }
275 /* only error if existence is expected. */
276 if (!(modes[i] & SPARSE))
277 bad = _("bad source");
278 goto act_on_entry;
279 }
280 ce = the_index.cache[pos];
281 if (!ce_skip_worktree(ce)) {
282 bad = _("bad source");
283 goto act_on_entry;
284 }
285 if (!ignore_sparse) {
286 string_list_append(&only_match_skip_worktree, src);
287 goto act_on_entry;
288 }
289 /* Check if dst exists in index */
290 if (index_name_pos(&the_index, dst, strlen(dst)) < 0) {
291 modes[i] |= SPARSE;
292 goto act_on_entry;
293 }
294 if (!force) {
295 bad = _("destination exists");
296 goto act_on_entry;
297 }
298 modes[i] |= SPARSE;
299 goto act_on_entry;
300 }
301 if (!strncmp(src, dst, length) &&
302 (dst[length] == 0 || dst[length] == '/')) {
303 bad = _("can not move directory into itself");
304 goto act_on_entry;
305 }
306 if (S_ISDIR(st.st_mode)
307 && lstat(dst, &dest_st) == 0) {
308 bad = _("destination already exists");
309 goto act_on_entry;
310 }
311
312 dir_check:
313 if (S_ISDIR(st.st_mode)) {
314 int j, dst_len, n;
315 int first = index_name_pos(&the_index, src, length), last;
316
317 if (first >= 0) {
318 prepare_move_submodule(src, first,
319 submodule_gitfile + i);
320 goto act_on_entry;
321 } else if (index_range_of_same_dir(src, length,
322 &first, &last) < 1) {
323 bad = _("source directory is empty");
324 goto act_on_entry;
325 }
326
327 /* last - first >= 1 */
328 modes[i] |= WORKING_DIRECTORY;
329
330 ALLOC_GROW(src_dir, src_dir_nr + 1, src_dir_alloc);
331 src_dir[src_dir_nr++] = src;
332
333 n = argc + last - first;
334 REALLOC_ARRAY(source, n);
335 REALLOC_ARRAY(destination, n);
336 REALLOC_ARRAY(modes, n);
337 REALLOC_ARRAY(submodule_gitfile, n);
338
339 dst = add_slash(dst);
340 dst_len = strlen(dst);
341
342 for (j = 0; j < last - first; j++) {
343 const struct cache_entry *ce = the_index.cache[first + j];
344 const char *path = ce->name;
345 source[argc + j] = path;
346 destination[argc + j] =
347 prefix_path(dst, dst_len, path + length + 1);
348 memset(modes + argc + j, 0, sizeof(enum update_mode));
349 modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX;
350 submodule_gitfile[argc + j] = NULL;
351 }
352 argc += last - first;
353 goto act_on_entry;
354 }
355 if (!(ce = index_file_exists(&the_index, src, length, 0))) {
356 bad = _("not under version control");
357 goto act_on_entry;
358 }
359 if (ce_stage(ce)) {
360 bad = _("conflicted");
361 goto act_on_entry;
362 }
363 if (lstat(dst, &st) == 0 &&
364 (!ignore_case || strcasecmp(src, dst))) {
365 bad = _("destination exists");
366 if (force) {
367 /*
368 * only files can overwrite each other:
369 * check both source and destination
370 */
371 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
372 if (verbose)
373 warning(_("overwriting '%s'"), dst);
374 bad = NULL;
375 } else
376 bad = _("Cannot overwrite");
377 }
378 goto act_on_entry;
379 }
380 if (string_list_has_string(&src_for_dst, dst)) {
381 bad = _("multiple sources for the same target");
382 goto act_on_entry;
383 }
384 if (is_dir_sep(dst[strlen(dst) - 1])) {
385 bad = _("destination directory does not exist");
386 goto act_on_entry;
387 }
388
389 if (ignore_sparse &&
390 (dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
391 index_entry_exists(&the_index, dst, strlen(dst))) {
392 bad = _("destination exists in the index");
393 if (force) {
394 if (verbose)
395 warning(_("overwriting '%s'"), dst);
396 bad = NULL;
397 } else {
398 goto act_on_entry;
399 }
400 }
401 /*
402 * We check if the paths are in the sparse-checkout
403 * definition as a very final check, since that
404 * allows us to point the user to the --sparse
405 * option as a way to have a successful run.
406 */
407 if (!ignore_sparse &&
408 !path_in_sparse_checkout(src, &the_index)) {
409 string_list_append(&only_match_skip_worktree, src);
410 skip_sparse = 1;
411 }
412 if (!ignore_sparse &&
413 !path_in_sparse_checkout(dst, &the_index)) {
414 string_list_append(&only_match_skip_worktree, dst);
415 skip_sparse = 1;
416 }
417
418 if (skip_sparse)
419 goto remove_entry;
420
421 string_list_insert(&src_for_dst, dst);
422
423 act_on_entry:
424 if (!bad)
425 continue;
426 if (!ignore_errors)
427 die(_("%s, source=%s, destination=%s"),
428 bad, src, dst);
429 remove_entry:
430 if (--argc > 0) {
431 int n = argc - i;
432 MOVE_ARRAY(source + i, source + i + 1, n);
433 MOVE_ARRAY(destination + i, destination + i + 1, n);
434 MOVE_ARRAY(modes + i, modes + i + 1, n);
435 MOVE_ARRAY(submodule_gitfile + i,
436 submodule_gitfile + i + 1, n);
437 i--;
438 }
439 }
440
441 if (only_match_skip_worktree.nr) {
442 advise_on_updating_sparse_paths(&only_match_skip_worktree);
443 if (!ignore_errors)
444 return 1;
445 }
446
447 for (i = 0; i < argc; i++) {
448 const char *src = source[i], *dst = destination[i];
449 enum update_mode mode = modes[i];
450 int pos;
451 int sparse_and_dirty = 0;
452 struct checkout state = CHECKOUT_INIT;
453 state.istate = &the_index;
454
455 if (force)
456 state.force = 1;
457 if (show_only || verbose)
458 printf(_("Renaming %s to %s\n"), src, dst);
459 if (show_only)
460 continue;
461 if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) &&
462 !(dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
463 rename(src, dst) < 0) {
464 if (ignore_errors)
465 continue;
466 die_errno(_("renaming '%s' failed"), src);
467 }
468 if (submodule_gitfile[i]) {
469 if (!update_path_in_gitmodules(src, dst))
470 gitmodules_modified = 1;
471 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
472 connect_work_tree_and_git_dir(dst,
473 submodule_gitfile[i],
474 1);
475 }
476
477 if (mode & (WORKING_DIRECTORY | SKIP_WORKTREE_DIR))
478 continue;
479
480 pos = index_name_pos(&the_index, src, strlen(src));
481 assert(pos >= 0);
482 if (!(mode & SPARSE) && !lstat(src, &st))
483 sparse_and_dirty = ie_modified(&the_index,
484 the_index.cache[pos],
485 &st,
486 0);
487 rename_index_entry_at(&the_index, pos, dst);
488
489 if (ignore_sparse &&
490 core_apply_sparse_checkout &&
491 core_sparse_checkout_cone) {
492 /*
493 * NEEDSWORK: we are *not* paying attention to
494 * "out-to-out" move (<source> is out-of-cone and
495 * <destination> is out-of-cone) at this point. It
496 * should be added in a future patch.
497 */
498 if ((mode & SPARSE) &&
499 path_in_sparse_checkout(dst, &the_index)) {
500 /* from out-of-cone to in-cone */
501 int dst_pos = index_name_pos(&the_index, dst,
502 strlen(dst));
503 struct cache_entry *dst_ce = the_index.cache[dst_pos];
504
505 dst_ce->ce_flags &= ~CE_SKIP_WORKTREE;
506
507 if (checkout_entry(dst_ce, &state, NULL, NULL))
508 die(_("cannot checkout %s"), dst_ce->name);
509 } else if ((dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
510 !(mode & SPARSE) &&
511 !path_in_sparse_checkout(dst, &the_index)) {
512 /* from in-cone to out-of-cone */
513 int dst_pos = index_name_pos(&the_index, dst,
514 strlen(dst));
515 struct cache_entry *dst_ce = the_index.cache[dst_pos];
516
517 /*
518 * if src is clean, it will suffice to remove it
519 */
520 if (!sparse_and_dirty) {
521 dst_ce->ce_flags |= CE_SKIP_WORKTREE;
522 unlink_or_warn(src);
523 } else {
524 /*
525 * if src is dirty, move it to the
526 * destination and create leading
527 * dirs if necessary
528 */
529 char *dst_dup = xstrdup(dst);
530 string_list_append(&dirty_paths, dst);
531 safe_create_leading_directories(dst_dup);
532 FREE_AND_NULL(dst_dup);
533 rename(src, dst);
534 }
535 }
536 }
537 }
538
539 /*
540 * cleanup the empty src_dirs
541 */
542 for (i = 0; i < src_dir_nr; i++) {
543 int dummy;
544 strbuf_addstr(&a_src_dir, src_dir[i]);
545 /*
546 * if entries under a_src_dir are all moved away,
547 * recursively remove a_src_dir to cleanup
548 */
549 if (index_range_of_same_dir(a_src_dir.buf, a_src_dir.len,
550 &dummy, &dummy) < 1) {
551 remove_dir_recursively(&a_src_dir, 0);
552 }
553 strbuf_reset(&a_src_dir);
554 }
555
556 strbuf_release(&a_src_dir);
557 free(src_dir);
558
559 if (dirty_paths.nr)
560 advise_on_moving_dirty_path(&dirty_paths);
561
562 if (gitmodules_modified)
563 stage_updated_gitmodules(&the_index);
564
565 if (write_locked_index(&the_index, &lock_file,
566 COMMIT_LOCK | SKIP_IF_UNCHANGED))
567 die(_("Unable to write new index file"));
568
569 string_list_clear(&src_for_dst, 0);
570 string_list_clear(&dirty_paths, 0);
571 UNLEAK(source);
572 UNLEAK(dest_path);
573 free(submodule_gitfile);
574 free(modes);
575 return 0;
576 }