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