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