2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
6 #define USE_THE_INDEX_VARIABLE
12 #include "environment.h"
14 #include "name-hash.h"
15 #include "object-file.h"
19 #include "cache-tree.h"
20 #include "string-list.h"
21 #include "parse-options.h"
22 #include "read-cache-ll.h"
23 #include "repository.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 const char **internal_prefix_pathspec(const char *prefix
,
44 const char **pathspec
,
45 int count
, unsigned flags
)
49 int prefixlen
= prefix
? strlen(prefix
) : 0;
50 ALLOC_ARRAY(result
, count
+ 1);
52 /* Create an intermediate copy of the pathspec based on the flags */
53 for (i
= 0; i
< count
; i
++) {
54 int length
= strlen(pathspec
[i
]);
57 while (!(flags
& KEEP_TRAILING_SLASH
) &&
58 to_copy
> 0 && is_dir_sep(pathspec
[i
][to_copy
- 1]))
61 it
= xmemdupz(pathspec
[i
], to_copy
);
62 if (flags
& DUP_BASENAME
) {
63 result
[i
] = xstrdup(basename(it
));
71 /* Prefix the pathspec and free the old intermediate strings */
72 for (i
= 0; i
< count
; i
++) {
73 const char *match
= prefix_path(prefix
, prefixlen
, result
[i
]);
74 free((char *) result
[i
]);
81 static const char *add_slash(const char *path
)
83 size_t len
= strlen(path
);
84 if (len
&& path
[len
- 1] != '/') {
85 char *with_slash
= xmalloc(st_add(len
, 2));
86 memcpy(with_slash
, path
, len
);
87 with_slash
[len
++] = '/';
94 #define SUBMODULE_WITH_GITDIR ((const char *)1)
96 static void prepare_move_submodule(const char *src
, int first
,
97 const char **submodule_gitfile
)
99 struct strbuf submodule_dotgit
= STRBUF_INIT
;
100 if (!S_ISGITLINK(the_index
.cache
[first
]->ce_mode
))
101 die(_("Directory %s is in index and no submodule?"), src
);
102 if (!is_staging_gitmodules_ok(&the_index
))
103 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
104 strbuf_addf(&submodule_dotgit
, "%s/.git", src
);
105 *submodule_gitfile
= read_gitfile(submodule_dotgit
.buf
);
106 if (*submodule_gitfile
)
107 *submodule_gitfile
= xstrdup(*submodule_gitfile
);
109 *submodule_gitfile
= SUBMODULE_WITH_GITDIR
;
110 strbuf_release(&submodule_dotgit
);
113 static int index_range_of_same_dir(const char *src
, int length
,
114 int *first_p
, int *last_p
)
116 const char *src_w_slash
= add_slash(src
);
117 int first
, last
, len_w_slash
= length
+ 1;
119 first
= index_name_pos(&the_index
, src_w_slash
, len_w_slash
);
121 die(_("%.*s is in index"), len_w_slash
, src_w_slash
);
124 for (last
= first
; last
< the_index
.cache_nr
; last
++) {
125 const char *path
= the_index
.cache
[last
]->name
;
126 if (strncmp(path
, src_w_slash
, len_w_slash
))
129 if (src_w_slash
!= src
)
130 free((char *)src_w_slash
);
137 * Given the path of a directory that does not exist on-disk, check whether the
138 * directory contains any entries in the index with the SKIP_WORKTREE flag
140 * Return 1 if such index entries exist.
141 * Return 0 otherwise.
143 static int empty_dir_has_sparse_contents(const char *name
)
146 const char *with_slash
= add_slash(name
);
147 int length
= strlen(with_slash
);
149 int pos
= index_name_pos(&the_index
, with_slash
, length
);
150 const struct cache_entry
*ce
;
154 if (pos
>= the_index
.cache_nr
)
156 ce
= the_index
.cache
[pos
];
157 if (strncmp(with_slash
, ce
->name
, length
))
159 if (ce_skip_worktree(ce
))
164 if (with_slash
!= name
)
165 free((char *)with_slash
);
169 int cmd_mv(int argc
, const char **argv
, const char *prefix
)
171 int i
, flags
, gitmodules_modified
= 0;
172 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0, ignore_sparse
= 0;
173 struct option builtin_mv_options
[] = {
174 OPT__VERBOSE(&verbose
, N_("be verbose")),
175 OPT__DRY_RUN(&show_only
, N_("dry run")),
176 OPT__FORCE(&force
, N_("force move/rename even if target exists"),
177 PARSE_OPT_NOCOMPLETE
),
178 OPT_BOOL('k', NULL
, &ignore_errors
, N_("skip move/rename errors")),
179 OPT_BOOL(0, "sparse", &ignore_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
182 const char **source
, **destination
, **dest_path
, **submodule_gitfile
;
183 const char *dst_w_slash
;
184 const char **src_dir
= NULL
;
185 int src_dir_nr
= 0, src_dir_alloc
= 0;
186 struct strbuf a_src_dir
= STRBUF_INIT
;
187 enum update_mode
*modes
, dst_mode
= 0;
189 struct string_list src_for_dst
= STRING_LIST_INIT_NODUP
;
190 struct lock_file lock_file
= LOCK_INIT
;
191 struct cache_entry
*ce
;
192 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
193 struct string_list dirty_paths
= STRING_LIST_INIT_NODUP
;
195 git_config(git_default_config
, NULL
);
197 argc
= parse_options(argc
, argv
, prefix
, builtin_mv_options
,
198 builtin_mv_usage
, 0);
200 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
202 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
203 if (repo_read_index(the_repository
) < 0)
204 die(_("index file corrupt"));
206 source
= internal_prefix_pathspec(prefix
, argv
, argc
, 0);
207 CALLOC_ARRAY(modes
, argc
);
210 * Keep trailing slash, needed to let
211 * "git mv file no-such-dir/" error out, except in the case
212 * "git mv directory no-such-dir/".
214 flags
= KEEP_TRAILING_SLASH
;
215 if (argc
== 1 && is_directory(argv
[0]) && !is_directory(argv
[1]))
217 dest_path
= internal_prefix_pathspec(prefix
, argv
+ argc
, 1, flags
);
218 dst_w_slash
= add_slash(dest_path
[0]);
219 submodule_gitfile
= xcalloc(argc
, sizeof(char *));
221 if (dest_path
[0][0] == '\0')
222 /* special case: "." was normalized to "" */
223 destination
= internal_prefix_pathspec(dest_path
[0], argv
, argc
, DUP_BASENAME
);
224 else if (!lstat(dest_path
[0], &st
) &&
225 S_ISDIR(st
.st_mode
)) {
226 destination
= internal_prefix_pathspec(dst_w_slash
, argv
, argc
, DUP_BASENAME
);
228 if (!path_in_sparse_checkout(dst_w_slash
, &the_index
) &&
229 empty_dir_has_sparse_contents(dst_w_slash
)) {
230 destination
= internal_prefix_pathspec(dst_w_slash
, argv
, argc
, DUP_BASENAME
);
231 dst_mode
= SKIP_WORKTREE_DIR
;
232 } else if (argc
!= 1) {
233 die(_("destination '%s' is not a directory"), dest_path
[0]);
235 destination
= dest_path
;
237 * <destination> is a file outside of sparse-checkout
238 * cone. Insist on cone mode here for backward
239 * compatibility. We don't want dst_mode to be assigned
240 * for a file when the repo is using no-cone mode (which
241 * is deprecated at this point) sparse-checkout. As
242 * SPARSE here is only considering cone-mode situation.
244 if (!path_in_cone_mode_sparse_checkout(destination
[0], &the_index
))
248 if (dst_w_slash
!= dest_path
[0]) {
249 free((char *)dst_w_slash
);
254 for (i
= 0; i
< argc
; i
++) {
255 const char *src
= source
[i
], *dst
= destination
[i
];
257 const char *bad
= NULL
;
261 printf(_("Checking rename of '%s' to '%s'\n"), src
, dst
);
263 length
= strlen(src
);
264 if (lstat(src
, &st
) < 0) {
266 const struct cache_entry
*ce
;
268 pos
= index_name_pos(&the_index
, src
, length
);
270 const char *src_w_slash
= add_slash(src
);
271 if (!path_in_sparse_checkout(src_w_slash
, &the_index
) &&
272 empty_dir_has_sparse_contents(src
)) {
273 modes
[i
] |= SKIP_WORKTREE_DIR
;
276 /* only error if existence is expected. */
277 if (!(modes
[i
] & SPARSE
))
278 bad
= _("bad source");
281 ce
= the_index
.cache
[pos
];
282 if (!ce_skip_worktree(ce
)) {
283 bad
= _("bad source");
286 if (!ignore_sparse
) {
287 string_list_append(&only_match_skip_worktree
, src
);
290 /* Check if dst exists in index */
291 if (index_name_pos(&the_index
, dst
, strlen(dst
)) < 0) {
296 bad
= _("destination exists");
302 if (!strncmp(src
, dst
, length
) &&
303 (dst
[length
] == 0 || dst
[length
] == '/')) {
304 bad
= _("can not move directory into itself");
307 if (S_ISDIR(st
.st_mode
)
308 && lstat(dst
, &st
) == 0) {
309 bad
= _("cannot move directory over file");
314 if (S_ISDIR(st
.st_mode
)) {
316 int first
= index_name_pos(&the_index
, src
, length
), last
;
319 prepare_move_submodule(src
, first
,
320 submodule_gitfile
+ i
);
322 } else if (index_range_of_same_dir(src
, length
,
323 &first
, &last
) < 1) {
324 bad
= _("source directory is empty");
328 /* last - first >= 1 */
329 modes
[i
] |= WORKING_DIRECTORY
;
331 ALLOC_GROW(src_dir
, src_dir_nr
+ 1, src_dir_alloc
);
332 src_dir
[src_dir_nr
++] = src
;
334 n
= argc
+ last
- first
;
335 REALLOC_ARRAY(source
, n
);
336 REALLOC_ARRAY(destination
, n
);
337 REALLOC_ARRAY(modes
, n
);
338 REALLOC_ARRAY(submodule_gitfile
, n
);
340 dst
= add_slash(dst
);
341 dst_len
= strlen(dst
);
343 for (j
= 0; j
< last
- first
; j
++) {
344 const struct cache_entry
*ce
= the_index
.cache
[first
+ j
];
345 const char *path
= ce
->name
;
346 source
[argc
+ j
] = path
;
347 destination
[argc
+ j
] =
348 prefix_path(dst
, dst_len
, path
+ length
+ 1);
349 memset(modes
+ argc
+ j
, 0, sizeof(enum update_mode
));
350 modes
[argc
+ j
] |= ce_skip_worktree(ce
) ? SPARSE
: INDEX
;
351 submodule_gitfile
[argc
+ j
] = NULL
;
353 argc
+= last
- first
;
356 if (!(ce
= index_file_exists(&the_index
, src
, length
, 0))) {
357 bad
= _("not under version control");
361 bad
= _("conflicted");
364 if (lstat(dst
, &st
) == 0 &&
365 (!ignore_case
|| strcasecmp(src
, dst
))) {
366 bad
= _("destination exists");
369 * only files can overwrite each other:
370 * check both source and destination
372 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
374 warning(_("overwriting '%s'"), dst
);
377 bad
= _("Cannot overwrite");
381 if (string_list_has_string(&src_for_dst
, dst
)) {
382 bad
= _("multiple sources for the same target");
385 if (is_dir_sep(dst
[strlen(dst
) - 1])) {
386 bad
= _("destination directory does not exist");
391 (dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
392 index_entry_exists(&the_index
, dst
, strlen(dst
))) {
393 bad
= _("destination exists in the index");
396 warning(_("overwriting '%s'"), dst
);
403 * We check if the paths are in the sparse-checkout
404 * definition as a very final check, since that
405 * allows us to point the user to the --sparse
406 * option as a way to have a successful run.
408 if (!ignore_sparse
&&
409 !path_in_sparse_checkout(src
, &the_index
)) {
410 string_list_append(&only_match_skip_worktree
, src
);
413 if (!ignore_sparse
&&
414 !path_in_sparse_checkout(dst
, &the_index
)) {
415 string_list_append(&only_match_skip_worktree
, dst
);
422 string_list_insert(&src_for_dst
, dst
);
428 die(_("%s, source=%s, destination=%s"),
433 MOVE_ARRAY(source
+ i
, source
+ i
+ 1, n
);
434 MOVE_ARRAY(destination
+ i
, destination
+ i
+ 1, n
);
435 MOVE_ARRAY(modes
+ i
, modes
+ i
+ 1, n
);
436 MOVE_ARRAY(submodule_gitfile
+ i
,
437 submodule_gitfile
+ i
+ 1, n
);
442 if (only_match_skip_worktree
.nr
) {
443 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
448 for (i
= 0; i
< argc
; i
++) {
449 const char *src
= source
[i
], *dst
= destination
[i
];
450 enum update_mode mode
= modes
[i
];
452 int sparse_and_dirty
= 0;
453 struct checkout state
= CHECKOUT_INIT
;
454 state
.istate
= &the_index
;
458 if (show_only
|| verbose
)
459 printf(_("Renaming %s to %s\n"), src
, dst
);
462 if (!(mode
& (INDEX
| SPARSE
| SKIP_WORKTREE_DIR
)) &&
463 !(dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
464 rename(src
, dst
) < 0) {
467 die_errno(_("renaming '%s' failed"), src
);
469 if (submodule_gitfile
[i
]) {
470 if (!update_path_in_gitmodules(src
, dst
))
471 gitmodules_modified
= 1;
472 if (submodule_gitfile
[i
] != SUBMODULE_WITH_GITDIR
)
473 connect_work_tree_and_git_dir(dst
,
474 submodule_gitfile
[i
],
478 if (mode
& (WORKING_DIRECTORY
| SKIP_WORKTREE_DIR
))
481 pos
= index_name_pos(&the_index
, src
, strlen(src
));
483 if (!(mode
& SPARSE
) && !lstat(src
, &st
))
484 sparse_and_dirty
= ie_modified(&the_index
,
485 the_index
.cache
[pos
],
488 rename_index_entry_at(&the_index
, pos
, dst
);
491 core_apply_sparse_checkout
&&
492 core_sparse_checkout_cone
) {
494 * NEEDSWORK: we are *not* paying attention to
495 * "out-to-out" move (<source> is out-of-cone and
496 * <destination> is out-of-cone) at this point. It
497 * should be added in a future patch.
499 if ((mode
& SPARSE
) &&
500 path_in_sparse_checkout(dst
, &the_index
)) {
501 /* from out-of-cone to in-cone */
502 int dst_pos
= index_name_pos(&the_index
, dst
,
504 struct cache_entry
*dst_ce
= the_index
.cache
[dst_pos
];
506 dst_ce
->ce_flags
&= ~CE_SKIP_WORKTREE
;
508 if (checkout_entry(dst_ce
, &state
, NULL
, NULL
))
509 die(_("cannot checkout %s"), dst_ce
->name
);
510 } else if ((dst_mode
& (SKIP_WORKTREE_DIR
| SPARSE
)) &&
512 !path_in_sparse_checkout(dst
, &the_index
)) {
513 /* from in-cone to out-of-cone */
514 int dst_pos
= index_name_pos(&the_index
, dst
,
516 struct cache_entry
*dst_ce
= the_index
.cache
[dst_pos
];
519 * if src is clean, it will suffice to remove it
521 if (!sparse_and_dirty
) {
522 dst_ce
->ce_flags
|= CE_SKIP_WORKTREE
;
526 * if src is dirty, move it to the
527 * destination and create leading
530 char *dst_dup
= xstrdup(dst
);
531 string_list_append(&dirty_paths
, dst
);
532 safe_create_leading_directories(dst_dup
);
533 FREE_AND_NULL(dst_dup
);
541 * cleanup the empty src_dirs
543 for (i
= 0; i
< src_dir_nr
; i
++) {
545 strbuf_addstr(&a_src_dir
, src_dir
[i
]);
547 * if entries under a_src_dir are all moved away,
548 * recursively remove a_src_dir to cleanup
550 if (index_range_of_same_dir(a_src_dir
.buf
, a_src_dir
.len
,
551 &dummy
, &dummy
) < 1) {
552 remove_dir_recursively(&a_src_dir
, 0);
554 strbuf_reset(&a_src_dir
);
557 strbuf_release(&a_src_dir
);
561 advise_on_moving_dirty_path(&dirty_paths
);
563 if (gitmodules_modified
)
564 stage_updated_gitmodules(&the_index
);
566 if (write_locked_index(&the_index
, &lock_file
,
567 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
568 die(_("Unable to write new index file"));
570 string_list_clear(&src_for_dst
, 0);
571 string_list_clear(&dirty_paths
, 0);
574 free(submodule_gitfile
);