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