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