2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "cache-tree.h"
13 #include "string-list.h"
14 #include "parse-options.h"
15 #include "submodule.h"
18 static const char * const builtin_mv_usage
[] = {
19 N_("git mv [<options>] <source>... <destination>"),
24 WORKING_DIRECTORY
= (1 << 1),
27 SKIP_WORKTREE_DIR
= (1 << 4),
30 #define DUP_BASENAME 1
31 #define KEEP_TRAILING_SLASH 2
33 static const char **internal_prefix_pathspec(const char *prefix
,
34 const char **pathspec
,
35 int count
, unsigned flags
)
39 int prefixlen
= prefix
? strlen(prefix
) : 0;
40 ALLOC_ARRAY(result
, count
+ 1);
42 /* Create an intermediate copy of the pathspec based on the flags */
43 for (i
= 0; i
< count
; i
++) {
44 int length
= strlen(pathspec
[i
]);
47 while (!(flags
& KEEP_TRAILING_SLASH
) &&
48 to_copy
> 0 && is_dir_sep(pathspec
[i
][to_copy
- 1]))
51 it
= xmemdupz(pathspec
[i
], to_copy
);
52 if (flags
& DUP_BASENAME
) {
53 result
[i
] = xstrdup(basename(it
));
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
]);
71 static const 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 void prepare_move_submodule(const char *src
, int first
,
87 const char **submodule_gitfile
)
89 struct strbuf submodule_dotgit
= STRBUF_INIT
;
90 if (!S_ISGITLINK(the_index
.cache
[first
]->ce_mode
))
91 die(_("Directory %s is in index and no submodule?"), src
);
92 if (!is_staging_gitmodules_ok(&the_index
))
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
);
99 *submodule_gitfile
= SUBMODULE_WITH_GITDIR
;
100 strbuf_release(&submodule_dotgit
);
103 static int index_range_of_same_dir(const char *src
, int length
,
104 int *first_p
, int *last_p
)
106 const char *src_w_slash
= add_slash(src
);
107 int first
, last
, len_w_slash
= length
+ 1;
109 first
= index_name_pos(&the_index
, src_w_slash
, len_w_slash
);
111 die(_("%.*s is in index"), len_w_slash
, src_w_slash
);
114 for (last
= first
; last
< the_index
.cache_nr
; last
++) {
115 const char *path
= the_index
.cache
[last
]->name
;
116 if (strncmp(path
, src_w_slash
, len_w_slash
))
119 if (src_w_slash
!= src
)
120 free((char *)src_w_slash
);
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
130 * Return 1 if such index entries exist.
131 * Return 0 otherwise.
133 static int empty_dir_has_sparse_contents(const char *name
)
136 const char *with_slash
= add_slash(name
);
137 int length
= strlen(with_slash
);
139 int pos
= index_name_pos(&the_index
, with_slash
, length
);
140 const struct cache_entry
*ce
;
144 if (pos
>= the_index
.cache_nr
)
146 ce
= the_index
.cache
[pos
];
147 if (strncmp(with_slash
, ce
->name
, length
))
149 if (ce_skip_worktree(ce
))
154 if (with_slash
!= name
)
155 free((char *)with_slash
);
159 int cmd_mv(int argc
, const char **argv
, const char *prefix
)
161 int i
, flags
, gitmodules_modified
= 0;
162 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0, ignore_sparse
= 0;
163 struct option builtin_mv_options
[] = {
164 OPT__VERBOSE(&verbose
, N_("be verbose")),
165 OPT__DRY_RUN(&show_only
, N_("dry run")),
166 OPT__FORCE(&force
, N_("force move/rename even if target exists"),
167 PARSE_OPT_NOCOMPLETE
),
168 OPT_BOOL('k', NULL
, &ignore_errors
, N_("skip move/rename errors")),
169 OPT_BOOL(0, "sparse", &ignore_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
172 const char **source
, **destination
, **dest_path
, **submodule_gitfile
;
173 const char *dst_w_slash
;
174 const char **src_dir
= NULL
;
175 int src_dir_nr
= 0, src_dir_alloc
= 0;
176 struct strbuf a_src_dir
= STRBUF_INIT
;
177 enum update_mode
*modes
, dst_mode
= 0;
179 struct string_list src_for_dst
= STRING_LIST_INIT_NODUP
;
180 struct lock_file lock_file
= LOCK_INIT
;
181 struct cache_entry
*ce
;
182 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
183 struct string_list dirty_paths
= STRING_LIST_INIT_NODUP
;
185 git_config(git_default_config
, NULL
);
187 argc
= parse_options(argc
, argv
, prefix
, builtin_mv_options
,
188 builtin_mv_usage
, 0);
190 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
192 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
193 if (repo_read_index(the_repository
) < 0)
194 die(_("index file corrupt"));
196 source
= internal_prefix_pathspec(prefix
, argv
, argc
, 0);
197 CALLOC_ARRAY(modes
, argc
);
200 * Keep trailing slash, needed to let
201 * "git mv file no-such-dir/" error out, except in the case
202 * "git mv directory no-such-dir/".
204 flags
= KEEP_TRAILING_SLASH
;
205 if (argc
== 1 && is_directory(argv
[0]) && !is_directory(argv
[1]))
207 dest_path
= internal_prefix_pathspec(prefix
, argv
+ argc
, 1, flags
);
208 dst_w_slash
= add_slash(dest_path
[0]);
209 submodule_gitfile
= xcalloc(argc
, sizeof(char *));
211 if (dest_path
[0][0] == '\0')
212 /* special case: "." was normalized to "" */
213 destination
= internal_prefix_pathspec(dest_path
[0], argv
, argc
, DUP_BASENAME
);
214 else if (!lstat(dest_path
[0], &st
) &&
215 S_ISDIR(st
.st_mode
)) {
216 destination
= internal_prefix_pathspec(dst_w_slash
, argv
, argc
, DUP_BASENAME
);
218 if (!path_in_sparse_checkout(dst_w_slash
, &the_index
) &&
219 empty_dir_has_sparse_contents(dst_w_slash
)) {
220 destination
= internal_prefix_pathspec(dst_w_slash
, argv
, argc
, DUP_BASENAME
);
221 dst_mode
= SKIP_WORKTREE_DIR
;
222 } else if (argc
!= 1) {
223 die(_("destination '%s' is not a directory"), dest_path
[0]);
225 destination
= dest_path
;
227 * <destination> is a file outside of sparse-checkout
228 * cone. Insist on cone mode here for backward
229 * compatibility. We don't want dst_mode to be assigned
230 * for a file when the repo is using no-cone mode (which
231 * is deprecated at this point) sparse-checkout. As
232 * SPARSE here is only considering cone-mode situation.
234 if (!path_in_cone_mode_sparse_checkout(destination
[0], &the_index
))
238 if (dst_w_slash
!= dest_path
[0]) {
239 free((char *)dst_w_slash
);
244 for (i
= 0; i
< argc
; i
++) {
245 const char *src
= source
[i
], *dst
= destination
[i
];
247 const char *bad
= NULL
;
251 printf(_("Checking rename of '%s' to '%s'\n"), src
, dst
);
253 length
= strlen(src
);
254 if (lstat(src
, &st
) < 0) {
256 const struct cache_entry
*ce
;
258 pos
= index_name_pos(&the_index
, src
, length
);
260 const char *src_w_slash
= add_slash(src
);
261 if (!path_in_sparse_checkout(src_w_slash
, &the_index
) &&
262 empty_dir_has_sparse_contents(src
)) {
263 modes
[i
] |= SKIP_WORKTREE_DIR
;
266 /* only error if existence is expected. */
267 if (!(modes
[i
] & SPARSE
))
268 bad
= _("bad source");
271 ce
= the_index
.cache
[pos
];
272 if (!ce_skip_worktree(ce
)) {
273 bad
= _("bad source");
276 if (!ignore_sparse
) {
277 string_list_append(&only_match_skip_worktree
, src
);
280 /* Check if dst exists in index */
281 if (index_name_pos(&the_index
, dst
, strlen(dst
)) < 0) {
286 bad
= _("destination exists");
292 if (!strncmp(src
, dst
, length
) &&
293 (dst
[length
] == 0 || dst
[length
] == '/')) {
294 bad
= _("can not move directory into itself");
297 if (S_ISDIR(st
.st_mode
)
298 && lstat(dst
, &st
) == 0) {
299 bad
= _("cannot move directory over file");
304 if (S_ISDIR(st
.st_mode
)) {
306 int first
= index_name_pos(&the_index
, src
, length
), last
;
309 prepare_move_submodule(src
, first
,
310 submodule_gitfile
+ i
);
312 } else if (index_range_of_same_dir(src
, length
,
313 &first
, &last
) < 1) {
314 bad
= _("source directory is empty");
318 /* last - first >= 1 */
319 modes
[i
] |= WORKING_DIRECTORY
;
321 ALLOC_GROW(src_dir
, src_dir_nr
+ 1, src_dir_alloc
);
322 src_dir
[src_dir_nr
++] = src
;
324 n
= argc
+ last
- first
;
325 REALLOC_ARRAY(source
, n
);
326 REALLOC_ARRAY(destination
, n
);
327 REALLOC_ARRAY(modes
, n
);
328 REALLOC_ARRAY(submodule_gitfile
, n
);
330 dst
= add_slash(dst
);
331 dst_len
= strlen(dst
);
333 for (j
= 0; j
< last
- first
; j
++) {
334 const struct cache_entry
*ce
= the_index
.cache
[first
+ j
];
335 const char *path
= ce
->name
;
336 source
[argc
+ j
] = path
;
337 destination
[argc
+ j
] =
338 prefix_path(dst
, dst_len
, path
+ length
+ 1);
339 memset(modes
+ argc
+ j
, 0, sizeof(enum update_mode
));
340 modes
[argc
+ j
] |= ce_skip_worktree(ce
) ? SPARSE
: INDEX
;
341 submodule_gitfile
[argc
+ j
] = NULL
;
343 argc
+= last
- first
;
346 if (!(ce
= index_file_exists(&the_index
, src
, length
, 0))) {
347 bad
= _("not under version control");
351 bad
= _("conflicted");
354 if (lstat(dst
, &st
) == 0 &&
355 (!ignore_case
|| strcasecmp(src
, dst
))) {
356 bad
= _("destination exists");
359 * only files can overwrite each other:
360 * check both source and destination
362 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
364 warning(_("overwriting '%s'"), dst
);
367 bad
= _("Cannot overwrite");
371 if (string_list_has_string(&src_for_dst
, dst
)) {
372 bad
= _("multiple sources for the same target");
375 if (is_dir_sep(dst
[strlen(dst
) - 1])) {
376 bad
= _("destination directory does not exist");
381 (dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
382 index_entry_exists(&the_index
, dst
, strlen(dst
))) {
383 bad
= _("destination exists in the index");
386 warning(_("overwriting '%s'"), dst
);
393 * We check if the paths are in the sparse-checkout
394 * definition as a very final check, since that
395 * allows us to point the user to the --sparse
396 * option as a way to have a successful run.
398 if (!ignore_sparse
&&
399 !path_in_sparse_checkout(src
, &the_index
)) {
400 string_list_append(&only_match_skip_worktree
, src
);
403 if (!ignore_sparse
&&
404 !path_in_sparse_checkout(dst
, &the_index
)) {
405 string_list_append(&only_match_skip_worktree
, dst
);
412 string_list_insert(&src_for_dst
, dst
);
418 die(_("%s, source=%s, destination=%s"),
423 MOVE_ARRAY(source
+ i
, source
+ i
+ 1, n
);
424 MOVE_ARRAY(destination
+ i
, destination
+ i
+ 1, n
);
425 MOVE_ARRAY(modes
+ i
, modes
+ i
+ 1, n
);
426 MOVE_ARRAY(submodule_gitfile
+ i
,
427 submodule_gitfile
+ i
+ 1, n
);
432 if (only_match_skip_worktree
.nr
) {
433 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
438 for (i
= 0; i
< argc
; i
++) {
439 const char *src
= source
[i
], *dst
= destination
[i
];
440 enum update_mode mode
= modes
[i
];
442 int sparse_and_dirty
= 0;
443 struct checkout state
= CHECKOUT_INIT
;
444 state
.istate
= &the_index
;
448 if (show_only
|| verbose
)
449 printf(_("Renaming %s to %s\n"), src
, dst
);
452 if (!(mode
& (INDEX
| SPARSE
| SKIP_WORKTREE_DIR
)) &&
453 !(dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
454 rename(src
, dst
) < 0) {
457 die_errno(_("renaming '%s' failed"), src
);
459 if (submodule_gitfile
[i
]) {
460 if (!update_path_in_gitmodules(src
, dst
))
461 gitmodules_modified
= 1;
462 if (submodule_gitfile
[i
] != SUBMODULE_WITH_GITDIR
)
463 connect_work_tree_and_git_dir(dst
,
464 submodule_gitfile
[i
],
468 if (mode
& (WORKING_DIRECTORY
| SKIP_WORKTREE_DIR
))
471 pos
= index_name_pos(&the_index
, src
, strlen(src
));
473 if (!(mode
& SPARSE
) && !lstat(src
, &st
))
474 sparse_and_dirty
= ie_modified(&the_index
,
475 the_index
.cache
[pos
],
478 rename_index_entry_at(&the_index
, pos
, dst
);
481 core_apply_sparse_checkout
&&
482 core_sparse_checkout_cone
) {
484 * NEEDSWORK: we are *not* paying attention to
485 * "out-to-out" move (<source> is out-of-cone and
486 * <destination> is out-of-cone) at this point. It
487 * should be added in a future patch.
489 if ((mode
& SPARSE
) &&
490 path_in_sparse_checkout(dst
, &the_index
)) {
491 /* from out-of-cone to in-cone */
492 int dst_pos
= cache_name_pos(dst
, strlen(dst
));
493 struct cache_entry
*dst_ce
= the_index
.cache
[dst_pos
];
495 dst_ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
497 if (checkout_entry(dst_ce
, &state
, NULL
, NULL
))
498 die(_("cannot checkout %s"), dst_ce
->name
);
499 } else if ((dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
501 !path_in_sparse_checkout(dst
, &the_index
)) {
502 /* from in-cone to out-of-cone */
503 int dst_pos
= cache_name_pos(dst
, strlen(dst
));
504 struct cache_entry
*dst_ce
= the_index
.cache
[dst_pos
];
507 * if src is clean, it will suffice to remove it
509 if (!sparse_and_dirty
) {
510 dst_ce
->ce_flags
|= CE_SKIP_WORKTREE
;
514 * if src is dirty, move it to the
515 * destination and create leading
518 char *dst_dup
= xstrdup(dst
);
519 string_list_append(&dirty_paths
, dst
);
520 safe_create_leading_directories(dst_dup
);
521 FREE_AND_NULL(dst_dup
);
529 * cleanup the empty src_dirs
531 for (i
= 0; i
< src_dir_nr
; i
++) {
533 strbuf_addstr(&a_src_dir
, src_dir
[i
]);
535 * if entries under a_src_dir are all moved away,
536 * recursively remove a_src_dir to cleanup
538 if (index_range_of_same_dir(a_src_dir
.buf
, a_src_dir
.len
,
539 &dummy
, &dummy
) < 1) {
540 remove_dir_recursively(&a_src_dir
, 0);
542 strbuf_reset(&a_src_dir
);
545 strbuf_release(&a_src_dir
);
549 advise_on_moving_dirty_path(&dirty_paths
);
551 if (gitmodules_modified
)
552 stage_updated_gitmodules(&the_index
);
554 if (write_locked_index(&the_index
, &lock_file
,
555 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
556 die(_("Unable to write new index file"));
558 string_list_clear(&src_for_dst
, 0);
559 string_list_clear(&dirty_paths
, 0);
562 free(submodule_gitfile
);