2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
6 #define USE_THE_REPOSITORY_VARIABLE
12 #include "environment.h"
14 #include "name-hash.h"
15 #include "object-file.h"
19 #include "string-list.h"
20 #include "parse-options.h"
21 #include "read-cache-ll.h"
25 #include "submodule.h"
28 static const char * const builtin_mv_usage
[] = {
29 N_("git mv [<options>] <source>... <destination>"),
34 WORKING_DIRECTORY
= (1 << 1),
37 SKIP_WORKTREE_DIR
= (1 << 4),
40 #define DUP_BASENAME 1
41 #define KEEP_TRAILING_SLASH 2
43 static void internal_prefix_pathspec(struct strvec
*out
,
45 const char **pathspec
,
46 int count
, unsigned flags
)
48 int prefixlen
= prefix
? strlen(prefix
) : 0;
50 /* Create an intermediate copy of the pathspec based on the flags */
51 for (int i
= 0; i
< count
; i
++) {
52 size_t length
= strlen(pathspec
[i
]);
53 size_t to_copy
= length
;
54 const char *maybe_basename
;
55 char *trimmed
, *prefixed_path
;
57 while (!(flags
& KEEP_TRAILING_SLASH
) &&
58 to_copy
> 0 && is_dir_sep(pathspec
[i
][to_copy
- 1]))
61 trimmed
= xmemdupz(pathspec
[i
], to_copy
);
62 maybe_basename
= (flags
& DUP_BASENAME
) ? basename(trimmed
) : trimmed
;
63 prefixed_path
= prefix_path(prefix
, prefixlen
, maybe_basename
);
64 strvec_push(out
, prefixed_path
);
71 static char *add_slash(const char *path
)
73 size_t len
= strlen(path
);
74 if (len
&& path
[len
- 1] != '/') {
75 char *with_slash
= xmalloc(st_add(len
, 2));
76 memcpy(with_slash
, path
, len
);
77 with_slash
[len
++] = '/';
84 #define SUBMODULE_WITH_GITDIR ((const char *)1)
86 static const char *submodule_gitfile_path(const char *src
, int first
)
88 struct strbuf submodule_dotgit
= STRBUF_INIT
;
91 if (!S_ISGITLINK(the_repository
->index
->cache
[first
]->ce_mode
))
92 die(_("Directory %s is in index and no submodule?"), src
);
93 if (!is_staging_gitmodules_ok(the_repository
->index
))
94 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
96 strbuf_addf(&submodule_dotgit
, "%s/.git", src
);
98 path
= read_gitfile(submodule_dotgit
.buf
);
99 strbuf_release(&submodule_dotgit
);
102 return SUBMODULE_WITH_GITDIR
;
105 static int index_range_of_same_dir(const char *src
, int length
,
106 int *first_p
, int *last_p
)
108 char *src_w_slash
= add_slash(src
);
109 int first
, last
, len_w_slash
= length
+ 1;
111 first
= index_name_pos(the_repository
->index
, src_w_slash
, len_w_slash
);
113 die(_("%.*s is in index"), len_w_slash
, src_w_slash
);
116 for (last
= first
; last
< the_repository
->index
->cache_nr
; last
++) {
117 const char *path
= the_repository
->index
->cache
[last
]->name
;
118 if (strncmp(path
, src_w_slash
, len_w_slash
))
129 * Given the path of a directory that does not exist on-disk, check whether the
130 * directory contains any entries in the index with the SKIP_WORKTREE flag
132 * Return 1 if such index entries exist.
133 * Return 0 otherwise.
135 static int empty_dir_has_sparse_contents(const char *name
)
138 char *with_slash
= add_slash(name
);
139 int length
= strlen(with_slash
);
141 int pos
= index_name_pos(the_repository
->index
, with_slash
, length
);
142 const struct cache_entry
*ce
;
146 if (pos
>= the_repository
->index
->cache_nr
)
148 ce
= the_repository
->index
->cache
[pos
];
149 if (strncmp(with_slash
, ce
->name
, length
))
151 if (ce_skip_worktree(ce
))
160 static void remove_empty_src_dirs(const char **src_dir
, size_t src_dir_nr
)
163 struct strbuf a_src_dir
= STRBUF_INIT
;
165 for (i
= 0; i
< src_dir_nr
; i
++) {
167 strbuf_addstr(&a_src_dir
, src_dir
[i
]);
169 * if entries under a_src_dir are all moved away,
170 * recursively remove a_src_dir to cleanup
172 if (index_range_of_same_dir(a_src_dir
.buf
, a_src_dir
.len
,
173 &dummy
, &dummy
) < 1) {
174 remove_dir_recursively(&a_src_dir
, 0);
176 strbuf_reset(&a_src_dir
);
179 strbuf_release(&a_src_dir
);
185 struct repository
*repo UNUSED
)
187 int i
, flags
, gitmodules_modified
= 0;
188 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0, ignore_sparse
= 0;
189 struct option builtin_mv_options
[] = {
190 OPT__VERBOSE(&verbose
, N_("be verbose")),
191 OPT__DRY_RUN(&show_only
, N_("dry run")),
192 OPT__FORCE(&force
, N_("force move/rename even if target exists"),
193 PARSE_OPT_NOCOMPLETE
),
194 OPT_BOOL('k', NULL
, &ignore_errors
, N_("skip move/rename errors")),
195 OPT_BOOL(0, "sparse", &ignore_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
198 struct strvec sources
= STRVEC_INIT
;
199 struct strvec dest_paths
= STRVEC_INIT
;
200 struct strvec destinations
= STRVEC_INIT
;
201 struct strvec submodule_gitfiles_to_free
= STRVEC_INIT
;
202 const char **submodule_gitfiles
;
203 char *dst_w_slash
= NULL
;
204 struct strvec src_dir
= STRVEC_INIT
;
205 enum update_mode
*modes
, dst_mode
= 0;
206 struct stat st
, dest_st
;
207 struct string_list src_for_dst
= STRING_LIST_INIT_DUP
;
208 struct lock_file lock_file
= LOCK_INIT
;
209 struct cache_entry
*ce
;
210 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_DUP
;
211 struct string_list dirty_paths
= STRING_LIST_INIT_DUP
;
214 git_config(git_default_config
, NULL
);
216 argc
= parse_options(argc
, argv
, prefix
, builtin_mv_options
,
217 builtin_mv_usage
, 0);
219 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
221 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
222 if (repo_read_index(the_repository
) < 0)
223 die(_("index file corrupt"));
225 internal_prefix_pathspec(&sources
, prefix
, argv
, argc
, 0);
226 CALLOC_ARRAY(modes
, argc
);
229 * Keep trailing slash, needed to let
230 * "git mv file no-such-dir/" error out, except in the case
231 * "git mv directory no-such-dir/".
233 flags
= KEEP_TRAILING_SLASH
;
234 if (argc
== 1 && is_directory(argv
[0]) && !is_directory(argv
[1]))
236 internal_prefix_pathspec(&dest_paths
, prefix
, argv
+ argc
, 1, flags
);
237 dst_w_slash
= add_slash(dest_paths
.v
[0]);
238 submodule_gitfiles
= xcalloc(argc
, sizeof(char *));
240 if (dest_paths
.v
[0][0] == '\0')
241 /* special case: "." was normalized to "" */
242 internal_prefix_pathspec(&destinations
, dest_paths
.v
[0], argv
, argc
, DUP_BASENAME
);
243 else if (!lstat(dest_paths
.v
[0], &st
) && S_ISDIR(st
.st_mode
)) {
244 internal_prefix_pathspec(&destinations
, dst_w_slash
, argv
, argc
, DUP_BASENAME
);
245 } else if (!path_in_sparse_checkout(dst_w_slash
, the_repository
->index
) &&
246 empty_dir_has_sparse_contents(dst_w_slash
)) {
247 internal_prefix_pathspec(&destinations
, dst_w_slash
, argv
, argc
, DUP_BASENAME
);
248 dst_mode
= SKIP_WORKTREE_DIR
;
249 } else if (argc
!= 1) {
250 die(_("destination '%s' is not a directory"), dest_paths
.v
[0]);
252 strvec_pushv(&destinations
, dest_paths
.v
);
255 * <destination> is a file outside of sparse-checkout
256 * cone. Insist on cone mode here for backward
257 * compatibility. We don't want dst_mode to be assigned
258 * for a file when the repo is using no-cone mode (which
259 * is deprecated at this point) sparse-checkout. As
260 * SPARSE here is only considering cone-mode situation.
262 if (!path_in_cone_mode_sparse_checkout(destinations
.v
[0], the_repository
->index
))
267 for (i
= 0; i
< argc
; i
++) {
268 const char *src
= sources
.v
[i
], *dst
= destinations
.v
[i
];
270 const char *bad
= NULL
;
274 printf(_("Checking rename of '%s' to '%s'\n"), src
, dst
);
276 length
= strlen(src
);
277 if (lstat(src
, &st
) < 0) {
279 const struct cache_entry
*ce
;
281 pos
= index_name_pos(the_repository
->index
, src
, length
);
283 char *src_w_slash
= add_slash(src
);
284 if (!path_in_sparse_checkout(src_w_slash
, the_repository
->index
) &&
285 empty_dir_has_sparse_contents(src
)) {
287 modes
[i
] |= SKIP_WORKTREE_DIR
;
291 /* only error if existence is expected. */
292 if (!(modes
[i
] & SPARSE
))
293 bad
= _("bad source");
296 ce
= the_repository
->index
->cache
[pos
];
297 if (!ce_skip_worktree(ce
)) {
298 bad
= _("bad source");
301 if (!ignore_sparse
) {
302 string_list_append(&only_match_skip_worktree
, src
);
305 /* Check if dst exists in index */
306 if (index_name_pos(the_repository
->index
, dst
, strlen(dst
)) < 0) {
311 bad
= _("destination exists");
317 if (!strncmp(src
, dst
, length
) &&
318 (dst
[length
] == 0 || dst
[length
] == '/')) {
319 bad
= _("can not move directory into itself");
322 if (S_ISDIR(st
.st_mode
)
323 && lstat(dst
, &dest_st
) == 0) {
324 bad
= _("destination already exists");
329 if (S_ISDIR(st
.st_mode
)) {
330 char *dst_with_slash
;
331 size_t dst_with_slash_len
;
333 int first
= index_name_pos(the_repository
->index
, src
, length
), last
;
336 const char *path
= submodule_gitfile_path(src
, first
);
337 if (path
!= SUBMODULE_WITH_GITDIR
)
338 path
= strvec_push(&submodule_gitfiles_to_free
, path
);
339 submodule_gitfiles
[i
] = path
;
341 } else if (index_range_of_same_dir(src
, length
,
342 &first
, &last
) < 1) {
343 bad
= _("source directory is empty");
347 /* last - first >= 1 */
348 modes
[i
] |= WORKING_DIRECTORY
;
350 strvec_push(&src_dir
, src
);
352 n
= argc
+ last
- first
;
353 REALLOC_ARRAY(modes
, n
);
354 REALLOC_ARRAY(submodule_gitfiles
, n
);
356 dst_with_slash
= add_slash(dst
);
357 dst_with_slash_len
= strlen(dst_with_slash
);
359 for (j
= 0; j
< last
- first
; j
++) {
360 const struct cache_entry
*ce
= the_repository
->index
->cache
[first
+ j
];
361 const char *path
= ce
->name
;
362 char *prefixed_path
= prefix_path(dst_with_slash
, dst_with_slash_len
, path
+ length
+ 1);
364 strvec_push(&sources
, path
);
365 strvec_push(&destinations
, prefixed_path
);
367 memset(modes
+ argc
+ j
, 0, sizeof(enum update_mode
));
368 modes
[argc
+ j
] |= ce_skip_worktree(ce
) ? SPARSE
: INDEX
;
369 submodule_gitfiles
[argc
+ j
] = NULL
;
374 free(dst_with_slash
);
375 argc
+= last
- first
;
378 if (!(ce
= index_file_exists(the_repository
->index
, src
, length
, 0))) {
379 bad
= _("not under version control");
383 bad
= _("conflicted");
386 if (lstat(dst
, &st
) == 0 &&
387 (!ignore_case
|| strcasecmp(src
, dst
))) {
388 bad
= _("destination exists");
391 * only files can overwrite each other:
392 * check both source and destination
394 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
396 warning(_("overwriting '%s'"), dst
);
399 bad
= _("Cannot overwrite");
403 if (string_list_has_string(&src_for_dst
, dst
)) {
404 bad
= _("multiple sources for the same target");
407 if (is_dir_sep(dst
[strlen(dst
) - 1])) {
408 bad
= _("destination directory does not exist");
413 (dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
414 index_entry_exists(the_repository
->index
, dst
, strlen(dst
))) {
415 bad
= _("destination exists in the index");
418 warning(_("overwriting '%s'"), dst
);
425 * We check if the paths are in the sparse-checkout
426 * definition as a very final check, since that
427 * allows us to point the user to the --sparse
428 * option as a way to have a successful run.
430 if (!ignore_sparse
&&
431 !path_in_sparse_checkout(src
, the_repository
->index
)) {
432 string_list_append(&only_match_skip_worktree
, src
);
435 if (!ignore_sparse
&&
436 !path_in_sparse_checkout(dst
, the_repository
->index
)) {
437 string_list_append(&only_match_skip_worktree
, dst
);
444 string_list_insert(&src_for_dst
, dst
);
450 die(_("%s, source=%s, destination=%s"),
455 strvec_remove(&sources
, i
);
456 strvec_remove(&destinations
, i
);
457 MOVE_ARRAY(modes
+ i
, modes
+ i
+ 1, n
);
458 MOVE_ARRAY(submodule_gitfiles
+ i
,
459 submodule_gitfiles
+ i
+ 1, n
);
464 if (only_match_skip_worktree
.nr
) {
465 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
466 if (!ignore_errors
) {
472 for (i
= 0; i
< argc
; i
++) {
473 const char *src
= sources
.v
[i
], *dst
= destinations
.v
[i
];
474 enum update_mode mode
= modes
[i
];
476 int sparse_and_dirty
= 0;
477 struct checkout state
= CHECKOUT_INIT
;
478 state
.istate
= the_repository
->index
;
482 if (show_only
|| verbose
)
483 printf(_("Renaming %s to %s\n"), src
, dst
);
486 if (!(mode
& (INDEX
| SPARSE
| SKIP_WORKTREE_DIR
)) &&
487 !(dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
488 rename(src
, dst
) < 0) {
491 die_errno(_("renaming '%s' failed"), src
);
493 if (submodule_gitfiles
[i
]) {
494 if (!update_path_in_gitmodules(src
, dst
))
495 gitmodules_modified
= 1;
496 if (submodule_gitfiles
[i
] != SUBMODULE_WITH_GITDIR
)
497 connect_work_tree_and_git_dir(dst
,
498 submodule_gitfiles
[i
],
502 if (mode
& (WORKING_DIRECTORY
| SKIP_WORKTREE_DIR
))
505 pos
= index_name_pos(the_repository
->index
, src
, strlen(src
));
507 if (!(mode
& SPARSE
) && !lstat(src
, &st
))
508 sparse_and_dirty
= ie_modified(the_repository
->index
,
509 the_repository
->index
->cache
[pos
],
512 rename_index_entry_at(the_repository
->index
, pos
, dst
);
515 core_apply_sparse_checkout
&&
516 core_sparse_checkout_cone
) {
518 * NEEDSWORK: we are *not* paying attention to
519 * "out-to-out" move (<source> is out-of-cone and
520 * <destination> is out-of-cone) at this point. It
521 * should be added in a future patch.
523 if ((mode
& SPARSE
) &&
524 path_in_sparse_checkout(dst
, the_repository
->index
)) {
525 /* from out-of-cone to in-cone */
526 int dst_pos
= index_name_pos(the_repository
->index
, dst
,
528 struct cache_entry
*dst_ce
= the_repository
->index
->cache
[dst_pos
];
530 dst_ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
532 if (checkout_entry(dst_ce
, &state
, NULL
, NULL
))
533 die(_("cannot checkout %s"), dst_ce
->name
);
534 } else if ((dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
536 !path_in_sparse_checkout(dst
, the_repository
->index
)) {
537 /* from in-cone to out-of-cone */
538 int dst_pos
= index_name_pos(the_repository
->index
, dst
,
540 struct cache_entry
*dst_ce
= the_repository
->index
->cache
[dst_pos
];
543 * if src is clean, it will suffice to remove it
545 if (!sparse_and_dirty
) {
546 dst_ce
->ce_flags
|= CE_SKIP_WORKTREE
;
550 * if src is dirty, move it to the
551 * destination and create leading
554 char *dst_dup
= xstrdup(dst
);
555 string_list_append(&dirty_paths
, dst
);
556 safe_create_leading_directories(dst_dup
);
557 FREE_AND_NULL(dst_dup
);
564 remove_empty_src_dirs(src_dir
.v
, src_dir
.nr
);
567 advise_on_moving_dirty_path(&dirty_paths
);
569 if (gitmodules_modified
)
570 stage_updated_gitmodules(the_repository
->index
);
572 if (write_locked_index(the_repository
->index
, &lock_file
,
573 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
574 die(_("Unable to write new index file"));
579 strvec_clear(&src_dir
);
581 string_list_clear(&src_for_dst
, 0);
582 string_list_clear(&dirty_paths
, 0);
583 string_list_clear(&only_match_skip_worktree
, 0);
584 strvec_clear(&sources
);
585 strvec_clear(&dest_paths
);
586 strvec_clear(&destinations
);
587 strvec_clear(&submodule_gitfiles_to_free
);
588 free(submodule_gitfiles
);