]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/mv.c
mv: cleanup empty WORKING_DIRECTORY
[thirdparty/git.git] / builtin / mv.c
CommitLineData
11be42a4
JS
1/*
2 * "git mv" builtin command
3 *
4 * Copyright (C) 2006 Johannes Schindelin
5 */
f8adbec9 6#define USE_THE_INDEX_COMPATIBILITY_MACROS
11be42a4 7#include "builtin.h"
b2141fc1 8#include "config.h"
2ec87741 9#include "pathspec.h"
697cc8ef 10#include "lockfile.h"
11be42a4
JS
11#include "dir.h"
12#include "cache-tree.h"
c455c87c 13#include "string-list.h"
c7a20c11 14#include "parse-options.h"
a88c915d 15#include "submodule.h"
707fa2f7 16#include "entry.h"
11be42a4 17
c7a20c11 18static const char * const builtin_mv_usage[] = {
9c9b4f2f 19 N_("git mv [<options>] <source>... <destination>"),
c7a20c11
PH
20 NULL
21};
11be42a4 22
24ea81d9 23enum update_mode {
24ea81d9
SY
24 WORKING_DIRECTORY = (1 << 1),
25 INDEX = (1 << 2),
26 SPARSE = (1 << 3),
b91a2b65 27 SKIP_WORKTREE_DIR = (1 << 4),
24ea81d9
SY
28};
29
c57f6281
MM
30#define DUP_BASENAME 1
31#define KEEP_TRAILING_SLASH 2
32
2ec87741
BW
33static const char **internal_prefix_pathspec(const char *prefix,
34 const char **pathspec,
35 int count, unsigned flags)
11be42a4 36{
d78b0f3d 37 int i;
b32fa95f 38 const char **result;
2ec87741 39 int prefixlen = prefix ? strlen(prefix) : 0;
b32fa95f 40 ALLOC_ARRAY(result, count + 1);
2ec87741
BW
41
42 /* Create an intermediate copy of the pathspec based on the flags */
d78b0f3d 43 for (i = 0; i < count; i++) {
2ec87741 44 int length = strlen(pathspec[i]);
af82559b 45 int to_copy = length;
2ec87741 46 char *it;
c57f6281 47 while (!(flags & KEEP_TRAILING_SLASH) &&
2ec87741 48 to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1]))
af82559b 49 to_copy--;
2ec87741
BW
50
51 it = xmemdupz(pathspec[i], to_copy);
52 if (flags & DUP_BASENAME) {
53 result[i] = xstrdup(basename(it));
54 free(it);
55 } else {
56 result[i] = it;
af82559b 57 }
11be42a4 58 }
2ec87741
BW
59 result[count] = NULL;
60
61 /* Prefix the pathspec and free the old intermediate strings */
62 for (i = 0; i < count; i++) {
63 const char *match = prefix_path(prefix, prefixlen, result[i]);
64 free((char *) result[i]);
65 result[i] = match;
66 }
67
68 return result;
11be42a4
JS
69}
70
ac64a722
JS
71static const char *add_slash(const char *path)
72{
50a6c8ef 73 size_t len = strlen(path);
ac64a722 74 if (path[len - 1] != '/') {
50a6c8ef 75 char *with_slash = xmalloc(st_add(len, 2));
ac64a722 76 memcpy(with_slash, path, len);
329a3047
JH
77 with_slash[len++] = '/';
78 with_slash[len] = 0;
ac64a722
JS
79 return with_slash;
80 }
81 return path;
82}
83
04c1ee57 84#define SUBMODULE_WITH_GITDIR ((const char *)1)
11be42a4 85
3af05a6d
NTND
86static void prepare_move_submodule(const char *src, int first,
87 const char **submodule_gitfile)
88{
89 struct strbuf submodule_dotgit = STRBUF_INIT;
90 if (!S_ISGITLINK(active_cache[first]->ce_mode))
91 die(_("Directory %s is in index and no submodule?"), src);
91b83480 92 if (!is_staging_gitmodules_ok(&the_index))
3af05a6d
NTND
93 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
94 strbuf_addf(&submodule_dotgit, "%s/.git", src);
95 *submodule_gitfile = read_gitfile(submodule_dotgit.buf);
96 if (*submodule_gitfile)
97 *submodule_gitfile = xstrdup(*submodule_gitfile);
98 else
99 *submodule_gitfile = SUBMODULE_WITH_GITDIR;
100 strbuf_release(&submodule_dotgit);
101}
102
e2b6cfa0
NTND
103static int index_range_of_same_dir(const char *src, int length,
104 int *first_p, int *last_p)
105{
106 const char *src_w_slash = add_slash(src);
107 int first, last, len_w_slash = length + 1;
108
109 first = cache_name_pos(src_w_slash, len_w_slash);
110 if (first >= 0)
111 die(_("%.*s is in index"), len_w_slash, src_w_slash);
112
113 first = -1 - first;
114 for (last = first; last < active_nr; last++) {
115 const char *path = active_cache[last]->name;
116 if (strncmp(path, src_w_slash, len_w_slash))
117 break;
118 }
119 if (src_w_slash != src)
120 free((char *)src_w_slash);
121 *first_p = first;
122 *last_p = last;
123 return last - first;
124}
125
b91a2b65 126/*
72e59ba1
SY
127 * Given the path of a directory that does not exist on-disk, check whether the
128 * directory contains any entries in the index with the SKIP_WORKTREE flag
129 * enabled.
130 * Return 1 if such index entries exist.
131 * Return 0 otherwise.
b91a2b65 132 */
72e59ba1 133static int empty_dir_has_sparse_contents(const char *name)
b91a2b65 134{
d57690a9 135 int ret = 0;
b91a2b65
SY
136 const char *with_slash = add_slash(name);
137 int length = strlen(with_slash);
138
139 int pos = cache_name_pos(with_slash, length);
140 const struct cache_entry *ce;
141
142 if (pos < 0) {
143 pos = -pos - 1;
144 if (pos >= the_index.cache_nr)
d57690a9 145 goto free_return;
b91a2b65
SY
146 ce = active_cache[pos];
147 if (strncmp(with_slash, ce->name, length))
d57690a9 148 goto free_return;
72e59ba1 149 if (ce_skip_worktree(ce))
d57690a9 150 ret = 1;
b91a2b65 151 }
d57690a9
SY
152
153free_return:
154 if (with_slash != name)
155 free((char *)with_slash);
156 return ret;
b91a2b65
SY
157}
158
7061cf0f 159int cmd_mv(int argc, const char **argv, const char *prefix)
11be42a4 160{
189d035e 161 int i, flags, gitmodules_modified = 0;
93d2c160 162 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0, ignore_sparse = 0;
c7a20c11 163 struct option builtin_mv_options[] = {
6c54dc46
NTND
164 OPT__VERBOSE(&verbose, N_("be verbose")),
165 OPT__DRY_RUN(&show_only, N_("dry run")),
61d15cd6
NTND
166 OPT__FORCE(&force, N_("force move/rename even if target exists"),
167 PARSE_OPT_NOCOMPLETE),
d5d09d47 168 OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
93d2c160 169 OPT_BOOL(0, "sparse", &ignore_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
c7a20c11
PH
170 OPT_END(),
171 };
a88c915d 172 const char **source, **destination, **dest_path, **submodule_gitfile;
c08830de 173 const char *dst_w_slash;
b6f51e3d
SY
174 const char **src_dir = NULL;
175 int src_dir_nr = 0, src_dir_alloc = 0;
176 struct strbuf a_src_dir = STRBUF_INIT;
5784db1b 177 enum update_mode *modes, dst_mode = 0;
11be42a4 178 struct stat st;
183113a5 179 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
0fa5a2ed 180 struct lock_file lock_file = LOCK_INIT;
9b906af6 181 struct cache_entry *ce;
93d2c160 182 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
5784db1b 183 struct string_list dirty_paths = STRING_LIST_INIT_NODUP;
11be42a4 184
ef90d6d4 185 git_config(git_default_config, NULL);
11be42a4 186
37782920
SB
187 argc = parse_options(argc, argv, prefix, builtin_mv_options,
188 builtin_mv_usage, 0);
c7a20c11
PH
189 if (--argc < 1)
190 usage_with_options(builtin_mv_usage, builtin_mv_options);
11be42a4 191
b3e83cc7 192 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
99caeed0 193 if (read_cache() < 0)
431b049e 194 die(_("index file corrupt"));
99caeed0 195
2ec87741 196 source = internal_prefix_pathspec(prefix, argv, argc, 0);
c7a20c11 197 modes = xcalloc(argc, sizeof(enum update_mode));
c57f6281
MM
198 /*
199 * Keep trailing slash, needed to let
189d035e
JS
200 * "git mv file no-such-dir/" error out, except in the case
201 * "git mv directory no-such-dir/".
c57f6281 202 */
189d035e
JS
203 flags = KEEP_TRAILING_SLASH;
204 if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
205 flags = 0;
2ec87741 206 dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags);
c08830de 207 dst_w_slash = add_slash(dest_path[0]);
a88c915d 208 submodule_gitfile = xcalloc(argc, sizeof(char *));
11be42a4 209
c5203bdf
JS
210 if (dest_path[0][0] == '\0')
211 /* special case: "." was normalized to "" */
2ec87741 212 destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
c5203bdf 213 else if (!lstat(dest_path[0], &st) &&
ac64a722 214 S_ISDIR(st.st_mode)) {
c08830de 215 destination = internal_prefix_pathspec(dst_w_slash, argv, argc, DUP_BASENAME);
ac64a722 216 } else {
c08830de
SY
217 if (!path_in_sparse_checkout(dst_w_slash, &the_index) &&
218 empty_dir_has_sparse_contents(dst_w_slash)) {
219 destination = internal_prefix_pathspec(dst_w_slash, argv, argc, DUP_BASENAME);
5784db1b 220 dst_mode = SKIP_WORKTREE_DIR;
c08830de 221 } else if (argc != 1) {
eac0ccc2 222 die(_("destination '%s' is not a directory"), dest_path[0]);
c08830de
SY
223 } else {
224 destination = dest_path;
5784db1b
SY
225 /*
226 * <destination> is a file outside of sparse-checkout
227 * cone. Insist on cone mode here for backward
228 * compatibility. We don't want dst_mode to be assigned
229 * for a file when the repo is using no-cone mode (which
230 * is deprecated at this point) sparse-checkout. As
231 * SPARSE here is only considering cone-mode situation.
232 */
233 if (!path_in_cone_mode_sparse_checkout(destination[0], &the_index))
234 dst_mode = SPARSE;
c08830de
SY
235 }
236 }
237 if (dst_w_slash != dest_path[0]) {
238 free((char *)dst_w_slash);
239 dst_w_slash = NULL;
11be42a4
JS
240 }
241
242 /* Checking */
c7a20c11 243 for (i = 0; i < argc; i++) {
60a6bf5f 244 const char *src = source[i], *dst = destination[i];
b91a2b65 245 int length;
11be42a4 246 const char *bad = NULL;
93d2c160 247 int skip_sparse = 0;
11be42a4
JS
248
249 if (show_only)
431b049e 250 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
11be42a4 251
60a6bf5f 252 length = strlen(src);
93d2c160 253 if (lstat(src, &st) < 0) {
6645b03c
SY
254 int pos;
255 const struct cache_entry *ce;
256
257 pos = cache_name_pos(src, length);
258 if (pos < 0) {
b91a2b65
SY
259 const char *src_w_slash = add_slash(src);
260 if (!path_in_sparse_checkout(src_w_slash, &the_index) &&
72e59ba1 261 empty_dir_has_sparse_contents(src)) {
b91a2b65
SY
262 modes[i] |= SKIP_WORKTREE_DIR;
263 goto dir_check;
264 }
6645b03c 265 /* only error if existence is expected. */
24ea81d9 266 if (!(modes[i] & SPARSE))
6645b03c
SY
267 bad = _("bad source");
268 goto act_on_entry;
269 }
6645b03c
SY
270 ce = active_cache[pos];
271 if (!ce_skip_worktree(ce)) {
93d2c160 272 bad = _("bad source");
7889755b
SY
273 goto act_on_entry;
274 }
8a26a391 275 if (!ignore_sparse) {
6645b03c 276 string_list_append(&only_match_skip_worktree, src);
8a26a391
SY
277 goto act_on_entry;
278 }
279 /* Check if dst exists in index */
280 if (cache_name_pos(dst, strlen(dst)) < 0) {
24ea81d9 281 modes[i] |= SPARSE;
8a26a391
SY
282 goto act_on_entry;
283 }
284 if (!force) {
285 bad = _("destination exists");
286 goto act_on_entry;
287 }
24ea81d9 288 modes[i] |= SPARSE;
6645b03c 289 goto act_on_entry;
7889755b
SY
290 }
291 if (!strncmp(src, dst, length) &&
292 (dst[length] == 0 || dst[length] == '/')) {
a7d5629f 293 bad = _("can not move directory into itself");
7889755b
SY
294 goto act_on_entry;
295 }
b91a2b65 296 if (S_ISDIR(st.st_mode)
7889755b 297 && lstat(dst, &st) == 0) {
a7d5629f 298 bad = _("cannot move directory over file");
7889755b
SY
299 goto act_on_entry;
300 }
b91a2b65
SY
301
302dir_check:
303 if (S_ISDIR(st.st_mode)) {
7889755b 304 int j, dst_len, n;
b46b15de 305 int first = cache_name_pos(src, length), last;
3af05a6d 306
7889755b 307 if (first >= 0) {
3af05a6d
NTND
308 prepare_move_submodule(src, first,
309 submodule_gitfile + i);
7889755b
SY
310 goto act_on_entry;
311 } else if (index_range_of_same_dir(src, length,
312 &first, &last) < 1) {
b46b15de 313 bad = _("source directory is empty");
7889755b
SY
314 goto act_on_entry;
315 }
11502468 316
7889755b 317 /* last - first >= 1 */
24ea81d9 318 modes[i] |= WORKING_DIRECTORY;
b6f51e3d
SY
319
320 ALLOC_GROW(src_dir, src_dir_nr + 1, src_dir_alloc);
321 src_dir[src_dir_nr++] = src;
322
7889755b
SY
323 n = argc + last - first;
324 REALLOC_ARRAY(source, n);
325 REALLOC_ARRAY(destination, n);
326 REALLOC_ARRAY(modes, n);
327 REALLOC_ARRAY(submodule_gitfile, n);
b46b15de 328
7889755b
SY
329 dst = add_slash(dst);
330 dst_len = strlen(dst);
b46b15de 331
7889755b
SY
332 for (j = 0; j < last - first; j++) {
333 const struct cache_entry *ce = active_cache[first + j];
334 const char *path = ce->name;
335 source[argc + j] = path;
336 destination[argc + j] =
337 prefix_path(dst, dst_len, path + length + 1);
24ea81d9
SY
338 memset(modes + argc + j, 0, sizeof(enum update_mode));
339 modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX;
7889755b 340 submodule_gitfile[argc + j] = NULL;
ac64a722 341 }
7889755b
SY
342 argc += last - first;
343 goto act_on_entry;
344 }
345 if (!(ce = cache_file_exists(src, length, 0))) {
a7d5629f 346 bad = _("not under version control");
7889755b
SY
347 goto act_on_entry;
348 }
349 if (ce_stage(ce)) {
9b906af6 350 bad = _("conflicted");
7889755b
SY
351 goto act_on_entry;
352 }
353 if (lstat(dst, &st) == 0 &&
354 (!ignore_case || strcasecmp(src, dst))) {
a7d5629f 355 bad = _("destination exists");
11be42a4
JS
356 if (force) {
357 /*
358 * only files can overwrite each other:
359 * check both source and destination
360 */
81dc2307 361 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
534376ca
JK
362 if (verbose)
363 warning(_("overwriting '%s'"), dst);
11be42a4 364 bad = NULL;
11be42a4 365 } else
a7d5629f 366 bad = _("Cannot overwrite");
11be42a4 367 }
7889755b
SY
368 goto act_on_entry;
369 }
370 if (string_list_has_string(&src_for_dst, dst)) {
a7d5629f 371 bad = _("multiple sources for the same target");
7889755b
SY
372 goto act_on_entry;
373 }
374 if (is_dir_sep(dst[strlen(dst) - 1])) {
a8933469 375 bad = _("destination directory does not exist");
7889755b
SY
376 goto act_on_entry;
377 }
93d2c160 378
7889755b
SY
379 /*
380 * We check if the paths are in the sparse-checkout
381 * definition as a very final check, since that
382 * allows us to point the user to the --sparse
383 * option as a way to have a successful run.
384 */
385 if (!ignore_sparse &&
386 !path_in_sparse_checkout(src, &the_index)) {
387 string_list_append(&only_match_skip_worktree, src);
388 skip_sparse = 1;
389 }
390 if (!ignore_sparse &&
391 !path_in_sparse_checkout(dst, &the_index)) {
392 string_list_append(&only_match_skip_worktree, dst);
393 skip_sparse = 1;
93d2c160 394 }
11be42a4 395
7889755b
SY
396 if (skip_sparse)
397 goto remove_entry;
398
399 string_list_insert(&src_for_dst, dst);
400
401act_on_entry:
ad1a19d0
NTND
402 if (!bad)
403 continue;
404 if (!ignore_errors)
4f1bbd23 405 die(_("%s, source=%s, destination=%s"),
ad1a19d0 406 bad, src, dst);
93d2c160 407remove_entry:
ad1a19d0
NTND
408 if (--argc > 0) {
409 int n = argc - i;
410 memmove(source + i, source + i + 1,
411 n * sizeof(char *));
412 memmove(destination + i, destination + i + 1,
413 n * sizeof(char *));
414 memmove(modes + i, modes + i + 1,
415 n * sizeof(enum update_mode));
416 memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
417 n * sizeof(char *));
418 i--;
11be42a4
JS
419 }
420 }
421
93d2c160
DS
422 if (only_match_skip_worktree.nr) {
423 advise_on_updating_sparse_paths(&only_match_skip_worktree);
424 if (!ignore_errors)
425 return 1;
426 }
427
c7a20c11 428 for (i = 0; i < argc; i++) {
60a6bf5f
JS
429 const char *src = source[i], *dst = destination[i];
430 enum update_mode mode = modes[i];
81dc2307 431 int pos;
5784db1b 432 int sparse_and_dirty = 0;
707fa2f7
SY
433 struct checkout state = CHECKOUT_INIT;
434 state.istate = &the_index;
435
436 if (force)
437 state.force = 1;
11be42a4 438 if (show_only || verbose)
431b049e 439 printf(_("Renaming %s to %s\n"), src, dst);
a127331c
SB
440 if (show_only)
441 continue;
b91a2b65 442 if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) &&
5784db1b 443 !(dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
24ea81d9 444 rename(src, dst) < 0) {
a127331c
SB
445 if (ignore_errors)
446 continue;
447 die_errno(_("renaming '%s' failed"), src);
448 }
449 if (submodule_gitfile[i]) {
a127331c
SB
450 if (!update_path_in_gitmodules(src, dst))
451 gitmodules_modified = 1;
da62f786
SB
452 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
453 connect_work_tree_and_git_dir(dst,
454 submodule_gitfile[i],
455 1);
a88c915d 456 }
60a6bf5f 457
b91a2b65 458 if (mode & (WORKING_DIRECTORY | SKIP_WORKTREE_DIR))
ac64a722
JS
459 continue;
460
81dc2307
PB
461 pos = cache_name_pos(src, strlen(src));
462 assert(pos >= 0);
5784db1b
SY
463 if (!(mode & SPARSE) && !lstat(src, &st))
464 sparse_and_dirty = ce_modified(active_cache[pos], &st, 0);
4cbe92fd 465 rename_cache_entry_at(pos, dst);
707fa2f7 466
5784db1b
SY
467 if (ignore_sparse &&
468 core_apply_sparse_checkout &&
469 core_sparse_checkout_cone) {
470 /*
471 * NEEDSWORK: we are *not* paying attention to
472 * "out-to-out" move (<source> is out-of-cone and
473 * <destination> is out-of-cone) at this point. It
474 * should be added in a future patch.
475 */
476 if ((mode & SPARSE) &&
477 path_in_sparse_checkout(dst, &the_index)) {
478 /* from out-of-cone to in-cone */
479 int dst_pos = cache_name_pos(dst, strlen(dst));
480 struct cache_entry *dst_ce = active_cache[dst_pos];
481
482 dst_ce->ce_flags &= ~CE_SKIP_WORKTREE;
483
484 if (checkout_entry(dst_ce, &state, NULL, NULL))
485 die(_("cannot checkout %s"), dst_ce->name);
486 } else if ((dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
487 !(mode & SPARSE) &&
488 !path_in_sparse_checkout(dst, &the_index)) {
489 /* from in-cone to out-of-cone */
490 int dst_pos = cache_name_pos(dst, strlen(dst));
491 struct cache_entry *dst_ce = active_cache[dst_pos];
707fa2f7 492
5784db1b
SY
493 /*
494 * if src is clean, it will suffice to remove it
495 */
496 if (!sparse_and_dirty) {
497 dst_ce->ce_flags |= CE_SKIP_WORKTREE;
498 unlink_or_warn(src);
499 } else {
500 /*
501 * if src is dirty, move it to the
502 * destination and create leading
503 * dirs if necessary
504 */
505 char *dst_dup = xstrdup(dst);
506 string_list_append(&dirty_paths, dst);
507 safe_create_leading_directories(dst_dup);
508 FREE_AND_NULL(dst_dup);
509 rename(src, dst);
510 }
511 }
707fa2f7 512 }
11be42a4
JS
513 }
514
b6f51e3d
SY
515 /*
516 * cleanup the empty src_dirs
517 */
518 for (i = 0; i < src_dir_nr; i++) {
519 int dummy;
520 strbuf_addstr(&a_src_dir, src_dir[i]);
521 /*
522 * if entries under a_src_dir are all moved away,
523 * recursively remove a_src_dir to cleanup
524 */
525 if (index_range_of_same_dir(a_src_dir.buf, a_src_dir.len,
526 &dummy, &dummy) < 1) {
527 remove_dir_recursively(&a_src_dir, 0);
528 }
529 strbuf_reset(&a_src_dir);
530 }
531
532 strbuf_release(&a_src_dir);
533 free(src_dir);
534
0656781f 535 if (gitmodules_modified)
3b8317a9 536 stage_updated_gitmodules(&the_index);
0656781f 537
61000814
538 if (write_locked_index(&the_index, &lock_file,
539 COMMIT_LOCK | SKIP_IF_UNCHANGED))
dcadc8b8 540 die(_("Unable to write new index file"));
11be42a4 541
ed3c566d 542 string_list_clear(&src_for_dst, 0);
5784db1b 543 string_list_clear(&dirty_paths, 0);
ed3c566d
AH
544 UNLEAK(source);
545 UNLEAK(dest_path);
546 free(submodule_gitfile);
547 free(modes);
11be42a4
JS
548 return 0;
549}