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