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>"),
25 WORKING_DIRECTORY
= (1 << 1),
28 SKIP_WORKTREE_DIR
= (1 << 4),
31 #define DUP_BASENAME 1
32 #define KEEP_TRAILING_SLASH 2
34 static const char **internal_prefix_pathspec(const char *prefix
,
35 const char **pathspec
,
36 int count
, unsigned flags
)
40 int prefixlen
= prefix
? strlen(prefix
) : 0;
41 ALLOC_ARRAY(result
, count
+ 1);
43 /* Create an intermediate copy of the pathspec based on the flags */
44 for (i
= 0; i
< count
; i
++) {
45 int length
= strlen(pathspec
[i
]);
48 while (!(flags
& KEEP_TRAILING_SLASH
) &&
49 to_copy
> 0 && is_dir_sep(pathspec
[i
][to_copy
- 1]))
52 it
= xmemdupz(pathspec
[i
], to_copy
);
53 if (flags
& DUP_BASENAME
) {
54 result
[i
] = xstrdup(basename(it
));
62 /* Prefix the pathspec and free the old intermediate strings */
63 for (i
= 0; i
< count
; i
++) {
64 const char *match
= prefix_path(prefix
, prefixlen
, result
[i
]);
65 free((char *) result
[i
]);
72 static const char *add_slash(const char *path
)
74 size_t len
= strlen(path
);
75 if (path
[len
- 1] != '/') {
76 char *with_slash
= xmalloc(st_add(len
, 2));
77 memcpy(with_slash
, path
, len
);
78 with_slash
[len
++] = '/';
85 #define SUBMODULE_WITH_GITDIR ((const char *)1)
87 static void prepare_move_submodule(const char *src
, int first
,
88 const char **submodule_gitfile
)
90 struct strbuf submodule_dotgit
= STRBUF_INIT
;
91 if (!S_ISGITLINK(active_cache
[first
]->ce_mode
))
92 die(_("Directory %s is in index and no submodule?"), src
);
93 if (!is_staging_gitmodules_ok(&the_index
))
94 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
95 strbuf_addf(&submodule_dotgit
, "%s/.git", src
);
96 *submodule_gitfile
= read_gitfile(submodule_dotgit
.buf
);
97 if (*submodule_gitfile
)
98 *submodule_gitfile
= xstrdup(*submodule_gitfile
);
100 *submodule_gitfile
= SUBMODULE_WITH_GITDIR
;
101 strbuf_release(&submodule_dotgit
);
104 static int index_range_of_same_dir(const char *src
, int length
,
105 int *first_p
, int *last_p
)
107 const char *src_w_slash
= add_slash(src
);
108 int first
, last
, len_w_slash
= length
+ 1;
110 first
= cache_name_pos(src_w_slash
, len_w_slash
);
112 die(_("%.*s is in index"), len_w_slash
, src_w_slash
);
115 for (last
= first
; last
< active_nr
; last
++) {
116 const char *path
= active_cache
[last
]->name
;
117 if (strncmp(path
, src_w_slash
, len_w_slash
))
120 if (src_w_slash
!= src
)
121 free((char *)src_w_slash
);
128 * Check if an out-of-cone directory should be in the index. Imagine this case
129 * that all the files under a directory are marked with 'CE_SKIP_WORKTREE' bit
130 * and thus the directory is sparsified.
132 * Return 0 if such directory exist (i.e. with any of its contained files not
133 * marked with CE_SKIP_WORKTREE, the directory would be present in working tree).
134 * Return 1 otherwise.
136 static int check_dir_in_index(const char *name
)
138 const char *with_slash
= add_slash(name
);
139 int length
= strlen(with_slash
);
141 int pos
= cache_name_pos(with_slash
, length
);
142 const struct cache_entry
*ce
;
146 if (pos
>= the_index
.cache_nr
)
148 ce
= active_cache
[pos
];
149 if (strncmp(with_slash
, ce
->name
, length
))
151 if (ce_skip_worktree(ce
))
157 int cmd_mv(int argc
, const char **argv
, const char *prefix
)
159 int i
, flags
, gitmodules_modified
= 0;
160 int verbose
= 0, show_only
= 0, force
= 0, ignore_errors
= 0, ignore_sparse
= 0;
161 struct option builtin_mv_options
[] = {
162 OPT__VERBOSE(&verbose
, N_("be verbose")),
163 OPT__DRY_RUN(&show_only
, N_("dry run")),
164 OPT__FORCE(&force
, N_("force move/rename even if target exists"),
165 PARSE_OPT_NOCOMPLETE
),
166 OPT_BOOL('k', NULL
, &ignore_errors
, N_("skip move/rename errors")),
167 OPT_BOOL(0, "sparse", &ignore_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
170 const char **source
, **destination
, **dest_path
, **submodule_gitfile
;
171 enum update_mode
*modes
;
173 struct string_list src_for_dst
= STRING_LIST_INIT_NODUP
;
174 struct lock_file lock_file
= LOCK_INIT
;
175 struct cache_entry
*ce
;
176 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
178 git_config(git_default_config
, NULL
);
180 argc
= parse_options(argc
, argv
, prefix
, builtin_mv_options
,
181 builtin_mv_usage
, 0);
183 usage_with_options(builtin_mv_usage
, builtin_mv_options
);
185 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
186 if (read_cache() < 0)
187 die(_("index file corrupt"));
189 source
= internal_prefix_pathspec(prefix
, argv
, argc
, 0);
190 modes
= xcalloc(argc
, sizeof(enum update_mode
));
192 * Keep trailing slash, needed to let
193 * "git mv file no-such-dir/" error out, except in the case
194 * "git mv directory no-such-dir/".
196 flags
= KEEP_TRAILING_SLASH
;
197 if (argc
== 1 && is_directory(argv
[0]) && !is_directory(argv
[1]))
199 dest_path
= internal_prefix_pathspec(prefix
, argv
+ argc
, 1, flags
);
200 submodule_gitfile
= xcalloc(argc
, sizeof(char *));
202 if (dest_path
[0][0] == '\0')
203 /* special case: "." was normalized to "" */
204 destination
= internal_prefix_pathspec(dest_path
[0], argv
, argc
, DUP_BASENAME
);
205 else if (!lstat(dest_path
[0], &st
) &&
206 S_ISDIR(st
.st_mode
)) {
207 dest_path
[0] = add_slash(dest_path
[0]);
208 destination
= internal_prefix_pathspec(dest_path
[0], argv
, argc
, DUP_BASENAME
);
211 die(_("destination '%s' is not a directory"), dest_path
[0]);
212 destination
= dest_path
;
216 for (i
= 0; i
< argc
; i
++) {
217 const char *src
= source
[i
], *dst
= destination
[i
];
219 const char *bad
= NULL
;
223 printf(_("Checking rename of '%s' to '%s'\n"), src
, dst
);
225 length
= strlen(src
);
226 if (lstat(src
, &st
) < 0) {
228 const struct cache_entry
*ce
;
230 pos
= cache_name_pos(src
, length
);
232 const char *src_w_slash
= add_slash(src
);
233 if (!path_in_sparse_checkout(src_w_slash
, &the_index
) &&
234 !check_dir_in_index(src
)) {
235 modes
[i
] |= SKIP_WORKTREE_DIR
;
238 /* only error if existence is expected. */
239 if (!(modes
[i
] & SPARSE
))
240 bad
= _("bad source");
243 ce
= active_cache
[pos
];
244 if (!ce_skip_worktree(ce
)) {
245 bad
= _("bad source");
248 if (!ignore_sparse
) {
249 string_list_append(&only_match_skip_worktree
, src
);
252 /* Check if dst exists in index */
253 if (cache_name_pos(dst
, strlen(dst
)) < 0) {
258 bad
= _("destination exists");
264 if (!strncmp(src
, dst
, length
) &&
265 (dst
[length
] == 0 || dst
[length
] == '/')) {
266 bad
= _("can not move directory into itself");
269 if (S_ISDIR(st
.st_mode
)
270 && lstat(dst
, &st
) == 0) {
271 bad
= _("cannot move directory over file");
276 if (S_ISDIR(st
.st_mode
)) {
278 int first
= cache_name_pos(src
, length
), last
;
281 prepare_move_submodule(src
, first
,
282 submodule_gitfile
+ i
);
284 } else if (index_range_of_same_dir(src
, length
,
285 &first
, &last
) < 1) {
286 bad
= _("source directory is empty");
290 /* last - first >= 1 */
291 modes
[i
] |= WORKING_DIRECTORY
;
292 n
= argc
+ last
- first
;
293 REALLOC_ARRAY(source
, n
);
294 REALLOC_ARRAY(destination
, n
);
295 REALLOC_ARRAY(modes
, n
);
296 REALLOC_ARRAY(submodule_gitfile
, n
);
298 dst
= add_slash(dst
);
299 dst_len
= strlen(dst
);
301 for (j
= 0; j
< last
- first
; j
++) {
302 const struct cache_entry
*ce
= active_cache
[first
+ j
];
303 const char *path
= ce
->name
;
304 source
[argc
+ j
] = path
;
305 destination
[argc
+ j
] =
306 prefix_path(dst
, dst_len
, path
+ length
+ 1);
307 memset(modes
+ argc
+ j
, 0, sizeof(enum update_mode
));
308 modes
[argc
+ j
] |= ce_skip_worktree(ce
) ? SPARSE
: INDEX
;
309 submodule_gitfile
[argc
+ j
] = NULL
;
311 argc
+= last
- first
;
314 if (!(ce
= cache_file_exists(src
, length
, 0))) {
315 bad
= _("not under version control");
319 bad
= _("conflicted");
322 if (lstat(dst
, &st
) == 0 &&
323 (!ignore_case
|| strcasecmp(src
, dst
))) {
324 bad
= _("destination exists");
327 * only files can overwrite each other:
328 * check both source and destination
330 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
)) {
332 warning(_("overwriting '%s'"), dst
);
335 bad
= _("Cannot overwrite");
339 if (string_list_has_string(&src_for_dst
, dst
)) {
340 bad
= _("multiple sources for the same target");
343 if (is_dir_sep(dst
[strlen(dst
) - 1])) {
344 bad
= _("destination directory does not exist");
349 * We check if the paths are in the sparse-checkout
350 * definition as a very final check, since that
351 * allows us to point the user to the --sparse
352 * option as a way to have a successful run.
354 if (!ignore_sparse
&&
355 !path_in_sparse_checkout(src
, &the_index
)) {
356 string_list_append(&only_match_skip_worktree
, src
);
359 if (!ignore_sparse
&&
360 !path_in_sparse_checkout(dst
, &the_index
)) {
361 string_list_append(&only_match_skip_worktree
, dst
);
368 string_list_insert(&src_for_dst
, dst
);
374 die(_("%s, source=%s, destination=%s"),
379 memmove(source
+ i
, source
+ i
+ 1,
381 memmove(destination
+ i
, destination
+ i
+ 1,
383 memmove(modes
+ i
, modes
+ i
+ 1,
384 n
* sizeof(enum update_mode
));
385 memmove(submodule_gitfile
+ i
, submodule_gitfile
+ i
+ 1,
391 if (only_match_skip_worktree
.nr
) {
392 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
397 for (i
= 0; i
< argc
; i
++) {
398 const char *src
= source
[i
], *dst
= destination
[i
];
399 enum update_mode mode
= modes
[i
];
401 struct checkout state
= CHECKOUT_INIT
;
402 state
.istate
= &the_index
;
406 if (show_only
|| verbose
)
407 printf(_("Renaming %s to %s\n"), src
, dst
);
410 if (!(mode
& (INDEX
| SPARSE
| SKIP_WORKTREE_DIR
)) &&
411 rename(src
, dst
) < 0) {
414 die_errno(_("renaming '%s' failed"), src
);
416 if (submodule_gitfile
[i
]) {
417 if (!update_path_in_gitmodules(src
, dst
))
418 gitmodules_modified
= 1;
419 if (submodule_gitfile
[i
] != SUBMODULE_WITH_GITDIR
)
420 connect_work_tree_and_git_dir(dst
,
421 submodule_gitfile
[i
],
425 if (mode
& (WORKING_DIRECTORY
| SKIP_WORKTREE_DIR
))
428 pos
= cache_name_pos(src
, strlen(src
));
430 rename_cache_entry_at(pos
, dst
);
432 if ((mode
& SPARSE
) &&
433 (path_in_sparse_checkout(dst
, &the_index
))) {
436 dst_pos
= cache_name_pos(dst
, strlen(dst
));
437 active_cache
[dst_pos
]->ce_flags
&= ~CE_SKIP_WORKTREE
;
439 if (checkout_entry(active_cache
[dst_pos
], &state
, NULL
, NULL
))
440 die(_("cannot checkout %s"), active_cache
[dst_pos
]->name
);
444 if (gitmodules_modified
)
445 stage_updated_gitmodules(&the_index
);
447 if (write_locked_index(&the_index
, &lock_file
,
448 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
449 die(_("Unable to write new index file"));
451 string_list_clear(&src_for_dst
, 0);
454 free(submodule_gitfile
);