pack-bitmap: improve grammar of "xor chain" error message
[git/debian.git] / builtin / mv.c
blob4729bb1a1ab9d33829d8abb3328564faa8ed7ef6
1 /*
2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
5 */
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
7 #include "builtin.h"
8 #include "config.h"
9 #include "pathspec.h"
10 #include "lockfile.h"
11 #include "dir.h"
12 #include "cache-tree.h"
13 #include "string-list.h"
14 #include "parse-options.h"
15 #include "submodule.h"
16 #include "entry.h"
18 static const char * const builtin_mv_usage[] = {
19 N_("git mv [<options>] <source>... <destination>"),
20 NULL
23 enum update_mode {
24 BOTH = 0,
25 WORKING_DIRECTORY = (1 << 1),
26 INDEX = (1 << 2),
27 SPARSE = (1 << 3),
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)
38 int i;
39 const char **result;
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]);
46 int to_copy = length;
47 char *it;
48 while (!(flags & KEEP_TRAILING_SLASH) &&
49 to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1]))
50 to_copy--;
52 it = xmemdupz(pathspec[i], to_copy);
53 if (flags & DUP_BASENAME) {
54 result[i] = xstrdup(basename(it));
55 free(it);
56 } else {
57 result[i] = it;
60 result[count] = NULL;
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]);
66 result[i] = match;
69 return result;
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++] = '/';
79 with_slash[len] = 0;
80 return with_slash;
82 return path;
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);
99 else
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);
111 if (first >= 0)
112 die(_("%.*s is in index"), len_w_slash, src_w_slash);
114 first = -1 - first;
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))
118 break;
120 if (src_w_slash != src)
121 free((char *)src_w_slash);
122 *first_p = first;
123 *last_p = last;
124 return last - first;
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;
144 if (pos < 0) {
145 pos = -pos - 1;
146 if (pos >= the_index.cache_nr)
147 return 1;
148 ce = active_cache[pos];
149 if (strncmp(with_slash, ce->name, length))
150 return 1;
151 if (ce_skip_worktree(ce))
152 return 0;
154 return 1;
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")),
168 OPT_END(),
170 const char **source, **destination, **dest_path, **submodule_gitfile;
171 enum update_mode *modes;
172 struct stat st;
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);
182 if (--argc < 1)
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 CALLOC_ARRAY(modes, argc);
193 * Keep trailing slash, needed to let
194 * "git mv file no-such-dir/" error out, except in the case
195 * "git mv directory no-such-dir/".
197 flags = KEEP_TRAILING_SLASH;
198 if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
199 flags = 0;
200 dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags);
201 submodule_gitfile = xcalloc(argc, sizeof(char *));
203 if (dest_path[0][0] == '\0')
204 /* special case: "." was normalized to "" */
205 destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
206 else if (!lstat(dest_path[0], &st) &&
207 S_ISDIR(st.st_mode)) {
208 dest_path[0] = add_slash(dest_path[0]);
209 destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
210 } else {
211 if (argc != 1)
212 die(_("destination '%s' is not a directory"), dest_path[0]);
213 destination = dest_path;
216 /* Checking */
217 for (i = 0; i < argc; i++) {
218 const char *src = source[i], *dst = destination[i];
219 int length;
220 const char *bad = NULL;
221 int skip_sparse = 0;
223 if (show_only)
224 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
226 length = strlen(src);
227 if (lstat(src, &st) < 0) {
228 int pos;
229 const struct cache_entry *ce;
231 pos = cache_name_pos(src, length);
232 if (pos < 0) {
233 const char *src_w_slash = add_slash(src);
234 if (!path_in_sparse_checkout(src_w_slash, &the_index) &&
235 !check_dir_in_index(src)) {
236 modes[i] |= SKIP_WORKTREE_DIR;
237 goto dir_check;
239 /* only error if existence is expected. */
240 if (!(modes[i] & SPARSE))
241 bad = _("bad source");
242 goto act_on_entry;
244 ce = active_cache[pos];
245 if (!ce_skip_worktree(ce)) {
246 bad = _("bad source");
247 goto act_on_entry;
249 if (!ignore_sparse) {
250 string_list_append(&only_match_skip_worktree, src);
251 goto act_on_entry;
253 /* Check if dst exists in index */
254 if (cache_name_pos(dst, strlen(dst)) < 0) {
255 modes[i] |= SPARSE;
256 goto act_on_entry;
258 if (!force) {
259 bad = _("destination exists");
260 goto act_on_entry;
262 modes[i] |= SPARSE;
263 goto act_on_entry;
265 if (!strncmp(src, dst, length) &&
266 (dst[length] == 0 || dst[length] == '/')) {
267 bad = _("can not move directory into itself");
268 goto act_on_entry;
270 if (S_ISDIR(st.st_mode)
271 && lstat(dst, &st) == 0) {
272 bad = _("cannot move directory over file");
273 goto act_on_entry;
276 dir_check:
277 if (S_ISDIR(st.st_mode)) {
278 int j, dst_len, n;
279 int first = cache_name_pos(src, length), last;
281 if (first >= 0) {
282 prepare_move_submodule(src, first,
283 submodule_gitfile + i);
284 goto act_on_entry;
285 } else if (index_range_of_same_dir(src, length,
286 &first, &last) < 1) {
287 bad = _("source directory is empty");
288 goto act_on_entry;
291 /* last - first >= 1 */
292 modes[i] |= WORKING_DIRECTORY;
293 n = argc + last - first;
294 REALLOC_ARRAY(source, n);
295 REALLOC_ARRAY(destination, n);
296 REALLOC_ARRAY(modes, n);
297 REALLOC_ARRAY(submodule_gitfile, n);
299 dst = add_slash(dst);
300 dst_len = strlen(dst);
302 for (j = 0; j < last - first; j++) {
303 const struct cache_entry *ce = active_cache[first + j];
304 const char *path = ce->name;
305 source[argc + j] = path;
306 destination[argc + j] =
307 prefix_path(dst, dst_len, path + length + 1);
308 memset(modes + argc + j, 0, sizeof(enum update_mode));
309 modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX;
310 submodule_gitfile[argc + j] = NULL;
312 argc += last - first;
313 goto act_on_entry;
315 if (!(ce = cache_file_exists(src, length, 0))) {
316 bad = _("not under version control");
317 goto act_on_entry;
319 if (ce_stage(ce)) {
320 bad = _("conflicted");
321 goto act_on_entry;
323 if (lstat(dst, &st) == 0 &&
324 (!ignore_case || strcasecmp(src, dst))) {
325 bad = _("destination exists");
326 if (force) {
328 * only files can overwrite each other:
329 * check both source and destination
331 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
332 if (verbose)
333 warning(_("overwriting '%s'"), dst);
334 bad = NULL;
335 } else
336 bad = _("Cannot overwrite");
338 goto act_on_entry;
340 if (string_list_has_string(&src_for_dst, dst)) {
341 bad = _("multiple sources for the same target");
342 goto act_on_entry;
344 if (is_dir_sep(dst[strlen(dst) - 1])) {
345 bad = _("destination directory does not exist");
346 goto act_on_entry;
350 * We check if the paths are in the sparse-checkout
351 * definition as a very final check, since that
352 * allows us to point the user to the --sparse
353 * option as a way to have a successful run.
355 if (!ignore_sparse &&
356 !path_in_sparse_checkout(src, &the_index)) {
357 string_list_append(&only_match_skip_worktree, src);
358 skip_sparse = 1;
360 if (!ignore_sparse &&
361 !path_in_sparse_checkout(dst, &the_index)) {
362 string_list_append(&only_match_skip_worktree, dst);
363 skip_sparse = 1;
366 if (skip_sparse)
367 goto remove_entry;
369 string_list_insert(&src_for_dst, dst);
371 act_on_entry:
372 if (!bad)
373 continue;
374 if (!ignore_errors)
375 die(_("%s, source=%s, destination=%s"),
376 bad, src, dst);
377 remove_entry:
378 if (--argc > 0) {
379 int n = argc - i;
380 MOVE_ARRAY(source + i, source + i + 1, n);
381 MOVE_ARRAY(destination + i, destination + i + 1, n);
382 MOVE_ARRAY(modes + i, modes + i + 1, n);
383 MOVE_ARRAY(submodule_gitfile + i,
384 submodule_gitfile + i + 1, n);
385 i--;
389 if (only_match_skip_worktree.nr) {
390 advise_on_updating_sparse_paths(&only_match_skip_worktree);
391 if (!ignore_errors)
392 return 1;
395 for (i = 0; i < argc; i++) {
396 const char *src = source[i], *dst = destination[i];
397 enum update_mode mode = modes[i];
398 int pos;
399 struct checkout state = CHECKOUT_INIT;
400 state.istate = &the_index;
402 if (force)
403 state.force = 1;
404 if (show_only || verbose)
405 printf(_("Renaming %s to %s\n"), src, dst);
406 if (show_only)
407 continue;
408 if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) &&
409 rename(src, dst) < 0) {
410 if (ignore_errors)
411 continue;
412 die_errno(_("renaming '%s' failed"), src);
414 if (submodule_gitfile[i]) {
415 if (!update_path_in_gitmodules(src, dst))
416 gitmodules_modified = 1;
417 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
418 connect_work_tree_and_git_dir(dst,
419 submodule_gitfile[i],
423 if (mode & (WORKING_DIRECTORY | SKIP_WORKTREE_DIR))
424 continue;
426 pos = cache_name_pos(src, strlen(src));
427 assert(pos >= 0);
428 rename_cache_entry_at(pos, dst);
430 if ((mode & SPARSE) &&
431 (path_in_sparse_checkout(dst, &the_index))) {
432 int dst_pos;
434 dst_pos = cache_name_pos(dst, strlen(dst));
435 active_cache[dst_pos]->ce_flags &= ~CE_SKIP_WORKTREE;
437 if (checkout_entry(active_cache[dst_pos], &state, NULL, NULL))
438 die(_("cannot checkout %s"), active_cache[dst_pos]->name);
442 if (gitmodules_modified)
443 stage_updated_gitmodules(&the_index);
445 if (write_locked_index(&the_index, &lock_file,
446 COMMIT_LOCK | SKIP_IF_UNCHANGED))
447 die(_("Unable to write new index file"));
449 string_list_clear(&src_for_dst, 0);
450 UNLEAK(source);
451 UNLEAK(dest_path);
452 free(submodule_gitfile);
453 free(modes);
454 return 0;