2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
6 #define USE_THE_INDEX_VARIABLE
11 #include "environment.h"
13 #include "name-hash.h"
14 #include "object-file.h"
18 #include "cache-tree.h"
19 #include "string-list.h"
20 #include "parse-options.h"
21 #include "read-cache-ll.h"
22 #include "repository.h"
24 #include "submodule.h"
27 static const char * const builtin_mv_usage
[] = {
28 N_("git mv [<options>] <source>... <destination>"),
33 WORKING_DIRECTORY
= (1 << 1),
36 SKIP_WORKTREE_DIR
= (1 << 4),
39 #define DUP_BASENAME 1
40 #define KEEP_TRAILING_SLASH 2
42 static const char **internal_prefix_pathspec(const char *prefix
,
43 const char **pathspec
,
44 int count
, unsigned flags
)
48 int prefixlen
= prefix
? strlen(prefix
) : 0;
49 ALLOC_ARRAY(result
, count
+ 1);
51 /* Create an intermediate copy of the pathspec based on the flags */
52 for (i
= 0; i
< count
; i
++) {
53 int length
= strlen(pathspec
[i
]);
56 while (!(flags
& KEEP_TRAILING_SLASH
) &&
57 to_copy
> 0 && is_dir_sep(pathspec
[i
][to_copy
- 1]))
60 it
= xmemdupz(pathspec
[i
], to_copy
);
61 if (flags
& DUP_BASENAME
) {
62 result
[i
] = xstrdup(basename(it
));
70 /* Prefix the pathspec and free the old intermediate strings */
71 for (i
= 0; i
< count
; i
++) {
72 const char *match
= prefix_path(prefix
, prefixlen
, result
[i
]);
73 free((char *) result
[i
]);
80 static const char *add_slash(const char *path
)
82 size_t len
= strlen(path
);
83 if (len
&& path
[len
- 1] != '/') {
84 char *with_slash
= xmalloc(st_add(len
, 2));
85 memcpy(with_slash
, path
, len
);
86 with_slash
[len
++] = '/';
93 #define SUBMODULE_WITH_GITDIR ((const char *)1)
95 static void prepare_move_submodule(const char *src
, int first
,
96 const char **submodule_gitfile
)
98 struct strbuf submodule_dotgit
= STRBUF_INIT
;
99 if (!S_ISGITLINK(the_index
.cache
[first
]->ce_mode
))
100 die(_("Directory %s is in index and no submodule?"), src
);
101 if (!is_staging_gitmodules_ok(&the_index
))
102 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
103 strbuf_addf(&submodule_dotgit
, "%s/.git", src
);
104 *submodule_gitfile
= read_gitfile(submodule_dotgit
.buf
);
105 if (*submodule_gitfile
)
106 *submodule_gitfile
= xstrdup(*submodule_gitfile
);
108 *submodule_gitfile
= SUBMODULE_WITH_GITDIR
;
109 strbuf_release(&submodule_dotgit
);
112 static int index_range_of_same_dir(const char *src
, int length
,
113 int *first_p
, int *last_p
)
115 const char *src_w_slash
= add_slash(src
);
116 int first
, last
, len_w_slash
= length
+ 1;
118 first
= index_name_pos(&the_index
, src_w_slash
, len_w_slash
);
120 die(_("%.*s is in index"), len_w_slash
, src_w_slash
);
123 for (last
= first
; last
< the_index
.cache_nr
; last
++) {
124 const char *path
= the_index
.cache
[last
]->name
;
125 if (strncmp(path
, src_w_slash
, len_w_slash
))
128 if (src_w_slash
!= src
)
129 free((char *)src_w_slash
);
136 * Given the path of a directory that does not exist on-disk, check whether the
137 * directory contains any entries in the index with the SKIP_WORKTREE flag
139 * Return 1 if such index entries exist.
140 * Return 0 otherwise.
142 static int empty_dir_has_sparse_contents(const char *name
)
145 const char *with_slash
= add_slash(name
);
146 int length
= strlen(with_slash
);
148 int pos
= index_name_pos(&the_index
, with_slash
, length
);
149 const struct cache_entry
*ce
;
153 if (pos
>= the_index
.cache_nr
)
155 ce
= the_index
.cache
[pos
];
156 if (strncmp(with_slash
, ce
->name
, length
))
158 if (ce_skip_worktree(ce
))
163 if (with_slash
!= name
)
164 free((char *)with_slash
);
168 int cmd_mv(int argc
, const char **argv
, const char *prefix
)
170 int i
, flags
, gitmodules_modified
= 0;
171 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0, ignore_sparse
= 0;
172 struct option builtin_mv_options
[] = {
173 OPT__VERBOSE(&verbose
, N_("be verbose")),
174 OPT__DRY_RUN(&show_only
, N_("dry run")),
175 OPT__FORCE(&force
, N_("force move/rename even if target exists"),
176 PARSE_OPT_NOCOMPLETE
),
177 OPT_BOOL('k', NULL
, &ignore_errors
, N_("skip move/rename errors")),
178 OPT_BOOL(0, "sparse", &ignore_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
181 const char **source
, **destination
, **dest_path
, **submodule_gitfile
;
182 const char *dst_w_slash
;
183 const char **src_dir
= NULL
;
184 int src_dir_nr
= 0, src_dir_alloc
= 0;
185 struct strbuf a_src_dir
= STRBUF_INIT
;
186 enum update_mode
*modes
, dst_mode
= 0;
187 struct stat st
, dest_st
;
188 struct string_list src_for_dst
= STRING_LIST_INIT_NODUP
;
189 struct lock_file lock_file
= LOCK_INIT
;
190 struct cache_entry
*ce
;
191 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
192 struct string_list dirty_paths
= STRING_LIST_INIT_NODUP
;
194 git_config(git_default_config
, NULL
);
196 argc
= parse_options(argc
, argv
, prefix
, builtin_mv_options
,
197 builtin_mv_usage
, 0);
199 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
201 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
202 if (repo_read_index(the_repository
) < 0)
203 die(_("index file corrupt"));
205 source
= internal_prefix_pathspec(prefix
, argv
, argc
, 0);
206 CALLOC_ARRAY(modes
, argc
);
209 * Keep trailing slash, needed to let
210 * "git mv file no-such-dir/" error out, except in the case
211 * "git mv directory no-such-dir/".
213 flags
= KEEP_TRAILING_SLASH
;
214 if (argc
== 1 && is_directory(argv
[0]) && !is_directory(argv
[1]))
216 dest_path
= internal_prefix_pathspec(prefix
, argv
+ argc
, 1, flags
);
217 dst_w_slash
= add_slash(dest_path
[0]);
218 submodule_gitfile
= xcalloc(argc
, sizeof(char *));
220 if (dest_path
[0][0] == '\0')
221 /* special case: "." was normalized to "" */
222 destination
= internal_prefix_pathspec(dest_path
[0], argv
, argc
, DUP_BASENAME
);
223 else if (!lstat(dest_path
[0], &st
) &&
224 S_ISDIR(st
.st_mode
)) {
225 destination
= internal_prefix_pathspec(dst_w_slash
, argv
, argc
, DUP_BASENAME
);
227 if (!path_in_sparse_checkout(dst_w_slash
, &the_index
) &&
228 empty_dir_has_sparse_contents(dst_w_slash
)) {
229 destination
= internal_prefix_pathspec(dst_w_slash
, argv
, argc
, DUP_BASENAME
);
230 dst_mode
= SKIP_WORKTREE_DIR
;
231 } else if (argc
!= 1) {
232 die(_("destination '%s' is not a directory"), dest_path
[0]);
234 destination
= dest_path
;
236 * <destination> is a file outside of sparse-checkout
237 * cone. Insist on cone mode here for backward
238 * compatibility. We don't want dst_mode to be assigned
239 * for a file when the repo is using no-cone mode (which
240 * is deprecated at this point) sparse-checkout. As
241 * SPARSE here is only considering cone-mode situation.
243 if (!path_in_cone_mode_sparse_checkout(destination
[0], &the_index
))
247 if (dst_w_slash
!= dest_path
[0]) {
248 free((char *)dst_w_slash
);
253 for (i
= 0; i
< argc
; i
++) {
254 const char *src
= source
[i
], *dst
= destination
[i
];
256 const char *bad
= NULL
;
260 printf(_("Checking rename of '%s' to '%s'\n"), src
, dst
);
262 length
= strlen(src
);
263 if (lstat(src
, &st
) < 0) {
265 const struct cache_entry
*ce
;
267 pos
= index_name_pos(&the_index
, src
, length
);
269 const char *src_w_slash
= add_slash(src
);
270 if (!path_in_sparse_checkout(src_w_slash
, &the_index
) &&
271 empty_dir_has_sparse_contents(src
)) {
272 modes
[i
] |= SKIP_WORKTREE_DIR
;
275 /* only error if existence is expected. */
276 if (!(modes
[i
] & SPARSE
))
277 bad
= _("bad source");
280 ce
= the_index
.cache
[pos
];
281 if (!ce_skip_worktree(ce
)) {
282 bad
= _("bad source");
285 if (!ignore_sparse
) {
286 string_list_append(&only_match_skip_worktree
, src
);
289 /* Check if dst exists in index */
290 if (index_name_pos(&the_index
, dst
, strlen(dst
)) < 0) {
295 bad
= _("destination exists");
301 if (!strncmp(src
, dst
, length
) &&
302 (dst
[length
] == 0 || dst
[length
] == '/')) {
303 bad
= _("can not move directory into itself");
306 if (S_ISDIR(st
.st_mode
)
307 && lstat(dst
, &dest_st
) == 0) {
308 bad
= _("destination already exists");
313 if (S_ISDIR(st
.st_mode
)) {
315 int first
= index_name_pos(&the_index
, src
, length
), last
;
318 prepare_move_submodule(src
, first
,
319 submodule_gitfile
+ i
);
321 } else if (index_range_of_same_dir(src
, length
,
322 &first
, &last
) < 1) {
323 bad
= _("source directory is empty");
327 /* last - first >= 1 */
328 modes
[i
] |= WORKING_DIRECTORY
;
330 ALLOC_GROW(src_dir
, src_dir_nr
+ 1, src_dir_alloc
);
331 src_dir
[src_dir_nr
++] = src
;
333 n
= argc
+ last
- first
;
334 REALLOC_ARRAY(source
, n
);
335 REALLOC_ARRAY(destination
, n
);
336 REALLOC_ARRAY(modes
, n
);
337 REALLOC_ARRAY(submodule_gitfile
, n
);
339 dst
= add_slash(dst
);
340 dst_len
= strlen(dst
);
342 for (j
= 0; j
< last
- first
; j
++) {
343 const struct cache_entry
*ce
= the_index
.cache
[first
+ j
];
344 const char *path
= ce
->name
;
345 source
[argc
+ j
] = path
;
346 destination
[argc
+ j
] =
347 prefix_path(dst
, dst_len
, path
+ length
+ 1);
348 memset(modes
+ argc
+ j
, 0, sizeof(enum update_mode
));
349 modes
[argc
+ j
] |= ce_skip_worktree(ce
) ? SPARSE
: INDEX
;
350 submodule_gitfile
[argc
+ j
] = NULL
;
352 argc
+= last
- first
;
355 if (!(ce
= index_file_exists(&the_index
, src
, length
, 0))) {
356 bad
= _("not under version control");
360 bad
= _("conflicted");
363 if (lstat(dst
, &st
) == 0 &&
364 (!ignore_case
|| strcasecmp(src
, dst
))) {
365 bad
= _("destination exists");
368 * only files can overwrite each other:
369 * check both source and destination
371 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
373 warning(_("overwriting '%s'"), dst
);
376 bad
= _("Cannot overwrite");
380 if (string_list_has_string(&src_for_dst
, dst
)) {
381 bad
= _("multiple sources for the same target");
384 if (is_dir_sep(dst
[strlen(dst
) - 1])) {
385 bad
= _("destination directory does not exist");
390 (dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
391 index_entry_exists(&the_index
, dst
, strlen(dst
))) {
392 bad
= _("destination exists in the index");
395 warning(_("overwriting '%s'"), dst
);
402 * We check if the paths are in the sparse-checkout
403 * definition as a very final check, since that
404 * allows us to point the user to the --sparse
405 * option as a way to have a successful run.
407 if (!ignore_sparse
&&
408 !path_in_sparse_checkout(src
, &the_index
)) {
409 string_list_append(&only_match_skip_worktree
, src
);
412 if (!ignore_sparse
&&
413 !path_in_sparse_checkout(dst
, &the_index
)) {
414 string_list_append(&only_match_skip_worktree
, dst
);
421 string_list_insert(&src_for_dst
, dst
);
427 die(_("%s, source=%s, destination=%s"),
432 MOVE_ARRAY(source
+ i
, source
+ i
+ 1, n
);
433 MOVE_ARRAY(destination
+ i
, destination
+ i
+ 1, n
);
434 MOVE_ARRAY(modes
+ i
, modes
+ i
+ 1, n
);
435 MOVE_ARRAY(submodule_gitfile
+ i
,
436 submodule_gitfile
+ i
+ 1, n
);
441 if (only_match_skip_worktree
.nr
) {
442 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
447 for (i
= 0; i
< argc
; i
++) {
448 const char *src
= source
[i
], *dst
= destination
[i
];
449 enum update_mode mode
= modes
[i
];
451 int sparse_and_dirty
= 0;
452 struct checkout state
= CHECKOUT_INIT
;
453 state
.istate
= &the_index
;
457 if (show_only
|| verbose
)
458 printf(_("Renaming %s to %s\n"), src
, dst
);
461 if (!(mode
& (INDEX
| SPARSE
| SKIP_WORKTREE_DIR
)) &&
462 !(dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
463 rename(src
, dst
) < 0) {
466 die_errno(_("renaming '%s' failed"), src
);
468 if (submodule_gitfile
[i
]) {
469 if (!update_path_in_gitmodules(src
, dst
))
470 gitmodules_modified
= 1;
471 if (submodule_gitfile
[i
] != SUBMODULE_WITH_GITDIR
)
472 connect_work_tree_and_git_dir(dst
,
473 submodule_gitfile
[i
],
477 if (mode
& (WORKING_DIRECTORY
| SKIP_WORKTREE_DIR
))
480 pos
= index_name_pos(&the_index
, src
, strlen(src
));
482 if (!(mode
& SPARSE
) && !lstat(src
, &st
))
483 sparse_and_dirty
= ie_modified(&the_index
,
484 the_index
.cache
[pos
],
487 rename_index_entry_at(&the_index
, pos
, dst
);
490 core_apply_sparse_checkout
&&
491 core_sparse_checkout_cone
) {
493 * NEEDSWORK: we are *not* paying attention to
494 * "out-to-out" move (<source> is out-of-cone and
495 * <destination> is out-of-cone) at this point. It
496 * should be added in a future patch.
498 if ((mode
& SPARSE
) &&
499 path_in_sparse_checkout(dst
, &the_index
)) {
500 /* from out-of-cone to in-cone */
501 int dst_pos
= index_name_pos(&the_index
, dst
,
503 struct cache_entry
*dst_ce
= the_index
.cache
[dst_pos
];
505 dst_ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
507 if (checkout_entry(dst_ce
, &state
, NULL
, NULL
))
508 die(_("cannot checkout %s"), dst_ce
->name
);
509 } else if ((dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
511 !path_in_sparse_checkout(dst
, &the_index
)) {
512 /* from in-cone to out-of-cone */
513 int dst_pos
= index_name_pos(&the_index
, dst
,
515 struct cache_entry
*dst_ce
= the_index
.cache
[dst_pos
];
518 * if src is clean, it will suffice to remove it
520 if (!sparse_and_dirty
) {
521 dst_ce
->ce_flags
|= CE_SKIP_WORKTREE
;
525 * if src is dirty, move it to the
526 * destination and create leading
529 char *dst_dup
= xstrdup(dst
);
530 string_list_append(&dirty_paths
, dst
);
531 safe_create_leading_directories(dst_dup
);
532 FREE_AND_NULL(dst_dup
);
540 * cleanup the empty src_dirs
542 for (i
= 0; i
< src_dir_nr
; i
++) {
544 strbuf_addstr(&a_src_dir
, src_dir
[i
]);
546 * if entries under a_src_dir are all moved away,
547 * recursively remove a_src_dir to cleanup
549 if (index_range_of_same_dir(a_src_dir
.buf
, a_src_dir
.len
,
550 &dummy
, &dummy
) < 1) {
551 remove_dir_recursively(&a_src_dir
, 0);
553 strbuf_reset(&a_src_dir
);
556 strbuf_release(&a_src_dir
);
560 advise_on_moving_dirty_path(&dirty_paths
);
562 if (gitmodules_modified
)
563 stage_updated_gitmodules(&the_index
);
565 if (write_locked_index(&the_index
, &lock_file
,
566 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
567 die(_("Unable to write new index file"));
569 string_list_clear(&src_for_dst
, 0);
570 string_list_clear(&dirty_paths
, 0);
573 free(submodule_gitfile
);