]>
Commit | Line | Data |
---|---|---|
11be42a4 JS |
1 | /* |
2 | * "git mv" builtin command | |
3 | * | |
4 | * Copyright (C) 2006 Johannes Schindelin | |
5 | */ | |
f59aa5e0 | 6 | |
11be42a4 | 7 | #include "builtin.h" |
0b027f6c | 8 | #include "abspath.h" |
6c6ddf92 | 9 | #include "advice.h" |
b2141fc1 | 10 | #include "config.h" |
32a8f510 | 11 | #include "environment.h" |
f394e093 | 12 | #include "gettext.h" |
f5653856 | 13 | #include "name-hash.h" |
87bed179 | 14 | #include "object-file.h" |
2ec87741 | 15 | #include "pathspec.h" |
697cc8ef | 16 | #include "lockfile.h" |
11be42a4 | 17 | #include "dir.h" |
c455c87c | 18 | #include "string-list.h" |
c7a20c11 | 19 | #include "parse-options.h" |
08c46a49 | 20 | #include "read-cache-ll.h" |
d1cbe1e6 | 21 | #include "repository.h" |
e38da487 | 22 | #include "setup.h" |
a88c915d | 23 | #include "submodule.h" |
707fa2f7 | 24 | #include "entry.h" |
11be42a4 | 25 | |
c7a20c11 | 26 | static const char * const builtin_mv_usage[] = { |
9c9b4f2f | 27 | N_("git mv [<options>] <source>... <destination>"), |
c7a20c11 PH |
28 | NULL |
29 | }; | |
11be42a4 | 30 | |
24ea81d9 | 31 | enum update_mode { |
24ea81d9 SY |
32 | WORKING_DIRECTORY = (1 << 1), |
33 | INDEX = (1 << 2), | |
34 | SPARSE = (1 << 3), | |
b91a2b65 | 35 | SKIP_WORKTREE_DIR = (1 << 4), |
24ea81d9 SY |
36 | }; |
37 | ||
c57f6281 MM |
38 | #define DUP_BASENAME 1 |
39 | #define KEEP_TRAILING_SLASH 2 | |
40 | ||
2ec87741 BW |
41 | static const char **internal_prefix_pathspec(const char *prefix, |
42 | const char **pathspec, | |
43 | int count, unsigned flags) | |
11be42a4 | 44 | { |
d78b0f3d | 45 | int i; |
b32fa95f | 46 | const char **result; |
2ec87741 | 47 | int prefixlen = prefix ? strlen(prefix) : 0; |
b32fa95f | 48 | ALLOC_ARRAY(result, count + 1); |
2ec87741 BW |
49 | |
50 | /* Create an intermediate copy of the pathspec based on the flags */ | |
d78b0f3d | 51 | for (i = 0; i < count; i++) { |
2ec87741 | 52 | int length = strlen(pathspec[i]); |
af82559b | 53 | int to_copy = length; |
2ec87741 | 54 | char *it; |
c57f6281 | 55 | while (!(flags & KEEP_TRAILING_SLASH) && |
2ec87741 | 56 | to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1])) |
af82559b | 57 | to_copy--; |
2ec87741 BW |
58 | |
59 | it = xmemdupz(pathspec[i], to_copy); | |
60 | if (flags & DUP_BASENAME) { | |
61 | result[i] = xstrdup(basename(it)); | |
62 | free(it); | |
63 | } else { | |
64 | result[i] = it; | |
af82559b | 65 | } |
11be42a4 | 66 | } |
2ec87741 BW |
67 | result[count] = NULL; |
68 | ||
69 | /* Prefix the pathspec and free the old intermediate strings */ | |
70 | for (i = 0; i < count; i++) { | |
71 | const char *match = prefix_path(prefix, prefixlen, result[i]); | |
72 | free((char *) result[i]); | |
73 | result[i] = match; | |
74 | } | |
75 | ||
76 | return result; | |
11be42a4 JS |
77 | } |
78 | ||
ac64a722 JS |
79 | static const char *add_slash(const char *path) |
80 | { | |
50a6c8ef | 81 | size_t len = strlen(path); |
7ead4681 | 82 | if (len && path[len - 1] != '/') { |
50a6c8ef | 83 | char *with_slash = xmalloc(st_add(len, 2)); |
ac64a722 | 84 | memcpy(with_slash, path, len); |
329a3047 JH |
85 | with_slash[len++] = '/'; |
86 | with_slash[len] = 0; | |
ac64a722 JS |
87 | return with_slash; |
88 | } | |
89 | return path; | |
90 | } | |
91 | ||
04c1ee57 | 92 | #define SUBMODULE_WITH_GITDIR ((const char *)1) |
11be42a4 | 93 | |
3af05a6d NTND |
94 | static void prepare_move_submodule(const char *src, int first, |
95 | const char **submodule_gitfile) | |
96 | { | |
97 | struct strbuf submodule_dotgit = STRBUF_INIT; | |
f59aa5e0 | 98 | if (!S_ISGITLINK(the_repository->index->cache[first]->ce_mode)) |
3af05a6d | 99 | die(_("Directory %s is in index and no submodule?"), src); |
f59aa5e0 | 100 | if (!is_staging_gitmodules_ok(the_repository->index)) |
3af05a6d NTND |
101 | die(_("Please stage your changes to .gitmodules or stash them to proceed")); |
102 | strbuf_addf(&submodule_dotgit, "%s/.git", src); | |
103 | *submodule_gitfile = read_gitfile(submodule_dotgit.buf); | |
104 | if (*submodule_gitfile) | |
105 | *submodule_gitfile = xstrdup(*submodule_gitfile); | |
106 | else | |
107 | *submodule_gitfile = SUBMODULE_WITH_GITDIR; | |
108 | strbuf_release(&submodule_dotgit); | |
109 | } | |
110 | ||
e2b6cfa0 NTND |
111 | static int index_range_of_same_dir(const char *src, int length, |
112 | int *first_p, int *last_p) | |
113 | { | |
114 | const char *src_w_slash = add_slash(src); | |
115 | int first, last, len_w_slash = length + 1; | |
116 | ||
f59aa5e0 | 117 | first = index_name_pos(the_repository->index, src_w_slash, len_w_slash); |
e2b6cfa0 NTND |
118 | if (first >= 0) |
119 | die(_("%.*s is in index"), len_w_slash, src_w_slash); | |
120 | ||
121 | first = -1 - first; | |
f59aa5e0 PS |
122 | for (last = first; last < the_repository->index->cache_nr; last++) { |
123 | const char *path = the_repository->index->cache[last]->name; | |
e2b6cfa0 NTND |
124 | if (strncmp(path, src_w_slash, len_w_slash)) |
125 | break; | |
126 | } | |
127 | if (src_w_slash != src) | |
128 | free((char *)src_w_slash); | |
129 | *first_p = first; | |
130 | *last_p = last; | |
131 | return last - first; | |
132 | } | |
133 | ||
b91a2b65 | 134 | /* |
72e59ba1 SY |
135 | * Given the path of a directory that does not exist on-disk, check whether the |
136 | * directory contains any entries in the index with the SKIP_WORKTREE flag | |
137 | * enabled. | |
138 | * Return 1 if such index entries exist. | |
139 | * Return 0 otherwise. | |
b91a2b65 | 140 | */ |
72e59ba1 | 141 | static int empty_dir_has_sparse_contents(const char *name) |
b91a2b65 | 142 | { |
d57690a9 | 143 | int ret = 0; |
b91a2b65 SY |
144 | const char *with_slash = add_slash(name); |
145 | int length = strlen(with_slash); | |
146 | ||
f59aa5e0 | 147 | int pos = index_name_pos(the_repository->index, with_slash, length); |
b91a2b65 SY |
148 | const struct cache_entry *ce; |
149 | ||
150 | if (pos < 0) { | |
151 | pos = -pos - 1; | |
f59aa5e0 | 152 | if (pos >= the_repository->index->cache_nr) |
d57690a9 | 153 | goto free_return; |
f59aa5e0 | 154 | ce = the_repository->index->cache[pos]; |
b91a2b65 | 155 | if (strncmp(with_slash, ce->name, length)) |
d57690a9 | 156 | goto free_return; |
b91a2b65 | 157 | if (ce_skip_worktree(ce)) |
d57690a9 | 158 | ret = 1; |
b91a2b65 | 159 | } |
d57690a9 SY |
160 | |
161 | free_return: | |
162 | if (with_slash != name) | |
163 | free((char *)with_slash); | |
164 | return ret; | |
b91a2b65 SY |
165 | } |
166 | ||
7061cf0f | 167 | int cmd_mv(int argc, const char **argv, const char *prefix) |
11be42a4 | 168 | { |
189d035e | 169 | int i, flags, gitmodules_modified = 0; |
93d2c160 | 170 | int verbose = 0, show_only = 0, force = 0, ignore_errors = 0, ignore_sparse = 0; |
c7a20c11 | 171 | struct option builtin_mv_options[] = { |
6c54dc46 NTND |
172 | OPT__VERBOSE(&verbose, N_("be verbose")), |
173 | OPT__DRY_RUN(&show_only, N_("dry run")), | |
61d15cd6 NTND |
174 | OPT__FORCE(&force, N_("force move/rename even if target exists"), |
175 | PARSE_OPT_NOCOMPLETE), | |
d5d09d47 | 176 | OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")), |
93d2c160 | 177 | OPT_BOOL(0, "sparse", &ignore_sparse, N_("allow updating entries outside of the sparse-checkout cone")), |
c7a20c11 PH |
178 | OPT_END(), |
179 | }; | |
a88c915d | 180 | const char **source, **destination, **dest_path, **submodule_gitfile; |
c08830de | 181 | const char *dst_w_slash; |
b6f51e3d SY |
182 | const char **src_dir = NULL; |
183 | int src_dir_nr = 0, src_dir_alloc = 0; | |
184 | struct strbuf a_src_dir = STRBUF_INIT; | |
5784db1b | 185 | enum update_mode *modes, dst_mode = 0; |
72695d82 | 186 | struct stat st, dest_st; |
183113a5 | 187 | struct string_list src_for_dst = STRING_LIST_INIT_NODUP; |
0fa5a2ed | 188 | struct lock_file lock_file = LOCK_INIT; |
9b906af6 | 189 | struct cache_entry *ce; |
93d2c160 | 190 | struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP; |
5784db1b | 191 | struct string_list dirty_paths = STRING_LIST_INIT_NODUP; |
11be42a4 | 192 | |
ef90d6d4 | 193 | git_config(git_default_config, NULL); |
11be42a4 | 194 | |
37782920 SB |
195 | argc = parse_options(argc, argv, prefix, builtin_mv_options, |
196 | builtin_mv_usage, 0); | |
c7a20c11 PH |
197 | if (--argc < 1) |
198 | usage_with_options(builtin_mv_usage, builtin_mv_options); | |
11be42a4 | 199 | |
07047d68 ÆAB |
200 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); |
201 | if (repo_read_index(the_repository) < 0) | |
431b049e | 202 | die(_("index file corrupt")); |
99caeed0 | 203 | |
2ec87741 | 204 | source = internal_prefix_pathspec(prefix, argv, argc, 0); |
eee227ad JH |
205 | CALLOC_ARRAY(modes, argc); |
206 | ||
c57f6281 MM |
207 | /* |
208 | * Keep trailing slash, needed to let | |
189d035e JS |
209 | * "git mv file no-such-dir/" error out, except in the case |
210 | * "git mv directory no-such-dir/". | |
c57f6281 | 211 | */ |
189d035e JS |
212 | flags = KEEP_TRAILING_SLASH; |
213 | if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1])) | |
214 | flags = 0; | |
2ec87741 | 215 | dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags); |
c08830de | 216 | dst_w_slash = add_slash(dest_path[0]); |
a88c915d | 217 | submodule_gitfile = xcalloc(argc, sizeof(char *)); |
11be42a4 | 218 | |
c5203bdf JS |
219 | if (dest_path[0][0] == '\0') |
220 | /* special case: "." was normalized to "" */ | |
2ec87741 | 221 | destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME); |
c5203bdf | 222 | else if (!lstat(dest_path[0], &st) && |
ac64a722 | 223 | S_ISDIR(st.st_mode)) { |
c08830de | 224 | destination = internal_prefix_pathspec(dst_w_slash, argv, argc, DUP_BASENAME); |
ac64a722 | 225 | } else { |
f59aa5e0 | 226 | if (!path_in_sparse_checkout(dst_w_slash, the_repository->index) && |
c08830de SY |
227 | empty_dir_has_sparse_contents(dst_w_slash)) { |
228 | destination = internal_prefix_pathspec(dst_w_slash, argv, argc, DUP_BASENAME); | |
5784db1b | 229 | dst_mode = SKIP_WORKTREE_DIR; |
c08830de | 230 | } else if (argc != 1) { |
eac0ccc2 | 231 | die(_("destination '%s' is not a directory"), dest_path[0]); |
c08830de SY |
232 | } else { |
233 | destination = dest_path; | |
5784db1b SY |
234 | /* |
235 | * <destination> is a file outside of sparse-checkout | |
236 | * cone. Insist on cone mode here for backward | |
237 | * compatibility. We don't want dst_mode to be assigned | |
238 | * for a file when the repo is using no-cone mode (which | |
239 | * is deprecated at this point) sparse-checkout. As | |
240 | * SPARSE here is only considering cone-mode situation. | |
241 | */ | |
f59aa5e0 | 242 | if (!path_in_cone_mode_sparse_checkout(destination[0], the_repository->index)) |
5784db1b | 243 | dst_mode = SPARSE; |
c08830de SY |
244 | } |
245 | } | |
246 | if (dst_w_slash != dest_path[0]) { | |
247 | free((char *)dst_w_slash); | |
248 | dst_w_slash = NULL; | |
11be42a4 JS |
249 | } |
250 | ||
251 | /* Checking */ | |
c7a20c11 | 252 | for (i = 0; i < argc; i++) { |
60a6bf5f | 253 | const char *src = source[i], *dst = destination[i]; |
b91a2b65 | 254 | int length; |
11be42a4 | 255 | const char *bad = NULL; |
93d2c160 | 256 | int skip_sparse = 0; |
11be42a4 JS |
257 | |
258 | if (show_only) | |
431b049e | 259 | printf(_("Checking rename of '%s' to '%s'\n"), src, dst); |
11be42a4 | 260 | |
60a6bf5f | 261 | length = strlen(src); |
93d2c160 | 262 | if (lstat(src, &st) < 0) { |
6645b03c SY |
263 | int pos; |
264 | const struct cache_entry *ce; | |
265 | ||
f59aa5e0 | 266 | pos = index_name_pos(the_repository->index, src, length); |
6645b03c | 267 | if (pos < 0) { |
b91a2b65 | 268 | const char *src_w_slash = add_slash(src); |
f59aa5e0 | 269 | if (!path_in_sparse_checkout(src_w_slash, the_repository->index) && |
72e59ba1 | 270 | empty_dir_has_sparse_contents(src)) { |
b91a2b65 SY |
271 | modes[i] |= SKIP_WORKTREE_DIR; |
272 | goto dir_check; | |
273 | } | |
6645b03c | 274 | /* only error if existence is expected. */ |
24ea81d9 | 275 | if (!(modes[i] & SPARSE)) |
6645b03c SY |
276 | bad = _("bad source"); |
277 | goto act_on_entry; | |
278 | } | |
f59aa5e0 | 279 | ce = the_repository->index->cache[pos]; |
6645b03c | 280 | if (!ce_skip_worktree(ce)) { |
93d2c160 | 281 | bad = _("bad source"); |
7889755b SY |
282 | goto act_on_entry; |
283 | } | |
8a26a391 | 284 | if (!ignore_sparse) { |
6645b03c | 285 | string_list_append(&only_match_skip_worktree, src); |
8a26a391 SY |
286 | goto act_on_entry; |
287 | } | |
288 | /* Check if dst exists in index */ | |
f59aa5e0 | 289 | if (index_name_pos(the_repository->index, dst, strlen(dst)) < 0) { |
24ea81d9 | 290 | modes[i] |= SPARSE; |
8a26a391 SY |
291 | goto act_on_entry; |
292 | } | |
293 | if (!force) { | |
294 | bad = _("destination exists"); | |
295 | goto act_on_entry; | |
296 | } | |
24ea81d9 | 297 | modes[i] |= SPARSE; |
6645b03c | 298 | goto act_on_entry; |
7889755b SY |
299 | } |
300 | if (!strncmp(src, dst, length) && | |
301 | (dst[length] == 0 || dst[length] == '/')) { | |
a7d5629f | 302 | bad = _("can not move directory into itself"); |
7889755b SY |
303 | goto act_on_entry; |
304 | } | |
b91a2b65 | 305 | if (S_ISDIR(st.st_mode) |
72695d82 | 306 | && lstat(dst, &dest_st) == 0) { |
c2cbefc5 | 307 | bad = _("destination already exists"); |
7889755b SY |
308 | goto act_on_entry; |
309 | } | |
b91a2b65 SY |
310 | |
311 | dir_check: | |
312 | if (S_ISDIR(st.st_mode)) { | |
7889755b | 313 | int j, dst_len, n; |
f59aa5e0 | 314 | int first = index_name_pos(the_repository->index, src, length), last; |
3af05a6d | 315 | |
7889755b | 316 | if (first >= 0) { |
3af05a6d NTND |
317 | prepare_move_submodule(src, first, |
318 | submodule_gitfile + i); | |
7889755b SY |
319 | goto act_on_entry; |
320 | } else if (index_range_of_same_dir(src, length, | |
321 | &first, &last) < 1) { | |
b46b15de | 322 | bad = _("source directory is empty"); |
7889755b SY |
323 | goto act_on_entry; |
324 | } | |
11502468 | 325 | |
7889755b | 326 | /* last - first >= 1 */ |
24ea81d9 | 327 | modes[i] |= WORKING_DIRECTORY; |
b6f51e3d SY |
328 | |
329 | ALLOC_GROW(src_dir, src_dir_nr + 1, src_dir_alloc); | |
330 | src_dir[src_dir_nr++] = src; | |
331 | ||
7889755b SY |
332 | n = argc + last - first; |
333 | REALLOC_ARRAY(source, n); | |
334 | REALLOC_ARRAY(destination, n); | |
335 | REALLOC_ARRAY(modes, n); | |
336 | REALLOC_ARRAY(submodule_gitfile, n); | |
b46b15de | 337 | |
7889755b SY |
338 | dst = add_slash(dst); |
339 | dst_len = strlen(dst); | |
b46b15de | 340 | |
7889755b | 341 | for (j = 0; j < last - first; j++) { |
f59aa5e0 | 342 | const struct cache_entry *ce = the_repository->index->cache[first + j]; |
7889755b SY |
343 | const char *path = ce->name; |
344 | source[argc + j] = path; | |
345 | destination[argc + j] = | |
346 | prefix_path(dst, dst_len, path + length + 1); | |
24ea81d9 SY |
347 | memset(modes + argc + j, 0, sizeof(enum update_mode)); |
348 | modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX; | |
7889755b | 349 | submodule_gitfile[argc + j] = NULL; |
ac64a722 | 350 | } |
7889755b SY |
351 | argc += last - first; |
352 | goto act_on_entry; | |
353 | } | |
f59aa5e0 | 354 | if (!(ce = index_file_exists(the_repository->index, src, length, 0))) { |
a7d5629f | 355 | bad = _("not under version control"); |
7889755b SY |
356 | goto act_on_entry; |
357 | } | |
358 | if (ce_stage(ce)) { | |
9b906af6 | 359 | bad = _("conflicted"); |
7889755b SY |
360 | goto act_on_entry; |
361 | } | |
362 | if (lstat(dst, &st) == 0 && | |
363 | (!ignore_case || strcasecmp(src, dst))) { | |
a7d5629f | 364 | bad = _("destination exists"); |
11be42a4 JS |
365 | if (force) { |
366 | /* | |
367 | * only files can overwrite each other: | |
368 | * check both source and destination | |
369 | */ | |
81dc2307 | 370 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { |
534376ca JK |
371 | if (verbose) |
372 | warning(_("overwriting '%s'"), dst); | |
11be42a4 | 373 | bad = NULL; |
11be42a4 | 374 | } else |
a7d5629f | 375 | bad = _("Cannot overwrite"); |
11be42a4 | 376 | } |
7889755b SY |
377 | goto act_on_entry; |
378 | } | |
379 | if (string_list_has_string(&src_for_dst, dst)) { | |
a7d5629f | 380 | bad = _("multiple sources for the same target"); |
7889755b SY |
381 | goto act_on_entry; |
382 | } | |
383 | if (is_dir_sep(dst[strlen(dst) - 1])) { | |
a8933469 | 384 | bad = _("destination directory does not exist"); |
7889755b SY |
385 | goto act_on_entry; |
386 | } | |
93d2c160 | 387 | |
da6fe05b SY |
388 | if (ignore_sparse && |
389 | (dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) && | |
f59aa5e0 | 390 | index_entry_exists(the_repository->index, dst, strlen(dst))) { |
da6fe05b SY |
391 | bad = _("destination exists in the index"); |
392 | if (force) { | |
393 | if (verbose) | |
394 | warning(_("overwriting '%s'"), dst); | |
395 | bad = NULL; | |
396 | } else { | |
397 | goto act_on_entry; | |
398 | } | |
399 | } | |
7889755b SY |
400 | /* |
401 | * We check if the paths are in the sparse-checkout | |
402 | * definition as a very final check, since that | |
403 | * allows us to point the user to the --sparse | |
404 | * option as a way to have a successful run. | |
405 | */ | |
406 | if (!ignore_sparse && | |
f59aa5e0 | 407 | !path_in_sparse_checkout(src, the_repository->index)) { |
7889755b SY |
408 | string_list_append(&only_match_skip_worktree, src); |
409 | skip_sparse = 1; | |
410 | } | |
411 | if (!ignore_sparse && | |
f59aa5e0 | 412 | !path_in_sparse_checkout(dst, the_repository->index)) { |
7889755b SY |
413 | string_list_append(&only_match_skip_worktree, dst); |
414 | skip_sparse = 1; | |
93d2c160 | 415 | } |
11be42a4 | 416 | |
7889755b SY |
417 | if (skip_sparse) |
418 | goto remove_entry; | |
419 | ||
420 | string_list_insert(&src_for_dst, dst); | |
421 | ||
422 | act_on_entry: | |
ad1a19d0 NTND |
423 | if (!bad) |
424 | continue; | |
425 | if (!ignore_errors) | |
4f1bbd23 | 426 | die(_("%s, source=%s, destination=%s"), |
ad1a19d0 | 427 | bad, src, dst); |
93d2c160 | 428 | remove_entry: |
ad1a19d0 NTND |
429 | if (--argc > 0) { |
430 | int n = argc - i; | |
eee227ad JH |
431 | MOVE_ARRAY(source + i, source + i + 1, n); |
432 | MOVE_ARRAY(destination + i, destination + i + 1, n); | |
433 | MOVE_ARRAY(modes + i, modes + i + 1, n); | |
434 | MOVE_ARRAY(submodule_gitfile + i, | |
435 | submodule_gitfile + i + 1, n); | |
ad1a19d0 | 436 | i--; |
11be42a4 JS |
437 | } |
438 | } | |
439 | ||
93d2c160 DS |
440 | if (only_match_skip_worktree.nr) { |
441 | advise_on_updating_sparse_paths(&only_match_skip_worktree); | |
442 | if (!ignore_errors) | |
443 | return 1; | |
444 | } | |
445 | ||
c7a20c11 | 446 | for (i = 0; i < argc; i++) { |
60a6bf5f JS |
447 | const char *src = source[i], *dst = destination[i]; |
448 | enum update_mode mode = modes[i]; | |
81dc2307 | 449 | int pos; |
5784db1b | 450 | int sparse_and_dirty = 0; |
707fa2f7 | 451 | struct checkout state = CHECKOUT_INIT; |
f59aa5e0 | 452 | state.istate = the_repository->index; |
707fa2f7 SY |
453 | |
454 | if (force) | |
455 | state.force = 1; | |
11be42a4 | 456 | if (show_only || verbose) |
431b049e | 457 | printf(_("Renaming %s to %s\n"), src, dst); |
a127331c SB |
458 | if (show_only) |
459 | continue; | |
b91a2b65 | 460 | if (!(mode & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) && |
5784db1b | 461 | !(dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) && |
24ea81d9 | 462 | rename(src, dst) < 0) { |
a127331c SB |
463 | if (ignore_errors) |
464 | continue; | |
465 | die_errno(_("renaming '%s' failed"), src); | |
466 | } | |
467 | if (submodule_gitfile[i]) { | |
a127331c SB |
468 | if (!update_path_in_gitmodules(src, dst)) |
469 | gitmodules_modified = 1; | |
da62f786 SB |
470 | if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR) |
471 | connect_work_tree_and_git_dir(dst, | |
472 | submodule_gitfile[i], | |
473 | 1); | |
a88c915d | 474 | } |
60a6bf5f | 475 | |
b91a2b65 | 476 | if (mode & (WORKING_DIRECTORY | SKIP_WORKTREE_DIR)) |
ac64a722 JS |
477 | continue; |
478 | ||
f59aa5e0 | 479 | pos = index_name_pos(the_repository->index, src, strlen(src)); |
81dc2307 | 480 | assert(pos >= 0); |
5784db1b | 481 | if (!(mode & SPARSE) && !lstat(src, &st)) |
f59aa5e0 PS |
482 | sparse_and_dirty = ie_modified(the_repository->index, |
483 | the_repository->index->cache[pos], | |
dc594180 | 484 | &st, |
031b2033 | 485 | 0); |
f59aa5e0 | 486 | rename_index_entry_at(the_repository->index, pos, dst); |
707fa2f7 | 487 | |
5784db1b SY |
488 | if (ignore_sparse && |
489 | core_apply_sparse_checkout && | |
490 | core_sparse_checkout_cone) { | |
491 | /* | |
492 | * NEEDSWORK: we are *not* paying attention to | |
493 | * "out-to-out" move (<source> is out-of-cone and | |
494 | * <destination> is out-of-cone) at this point. It | |
495 | * should be added in a future patch. | |
496 | */ | |
497 | if ((mode & SPARSE) && | |
f59aa5e0 | 498 | path_in_sparse_checkout(dst, the_repository->index)) { |
5784db1b | 499 | /* from out-of-cone to in-cone */ |
f59aa5e0 | 500 | int dst_pos = index_name_pos(the_repository->index, dst, |
babed893 | 501 | strlen(dst)); |
f59aa5e0 | 502 | struct cache_entry *dst_ce = the_repository->index->cache[dst_pos]; |
5784db1b SY |
503 | |
504 | dst_ce->ce_flags &= ~CE_SKIP_WORKTREE; | |
505 | ||
506 | if (checkout_entry(dst_ce, &state, NULL, NULL)) | |
507 | die(_("cannot checkout %s"), dst_ce->name); | |
508 | } else if ((dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) && | |
509 | !(mode & SPARSE) && | |
f59aa5e0 | 510 | !path_in_sparse_checkout(dst, the_repository->index)) { |
5784db1b | 511 | /* from in-cone to out-of-cone */ |
f59aa5e0 | 512 | int dst_pos = index_name_pos(the_repository->index, dst, |
babed893 | 513 | strlen(dst)); |
f59aa5e0 | 514 | struct cache_entry *dst_ce = the_repository->index->cache[dst_pos]; |
707fa2f7 | 515 | |
5784db1b SY |
516 | /* |
517 | * if src is clean, it will suffice to remove it | |
518 | */ | |
519 | if (!sparse_and_dirty) { | |
520 | dst_ce->ce_flags |= CE_SKIP_WORKTREE; | |
521 | unlink_or_warn(src); | |
522 | } else { | |
523 | /* | |
524 | * if src is dirty, move it to the | |
525 | * destination and create leading | |
526 | * dirs if necessary | |
527 | */ | |
528 | char *dst_dup = xstrdup(dst); | |
529 | string_list_append(&dirty_paths, dst); | |
530 | safe_create_leading_directories(dst_dup); | |
531 | FREE_AND_NULL(dst_dup); | |
532 | rename(src, dst); | |
533 | } | |
534 | } | |
707fa2f7 | 535 | } |
11be42a4 | 536 | } |
707fa2f7 | 537 | |
b6f51e3d SY |
538 | /* |
539 | * cleanup the empty src_dirs | |
540 | */ | |
541 | for (i = 0; i < src_dir_nr; i++) { | |
542 | int dummy; | |
543 | strbuf_addstr(&a_src_dir, src_dir[i]); | |
544 | /* | |
545 | * if entries under a_src_dir are all moved away, | |
546 | * recursively remove a_src_dir to cleanup | |
547 | */ | |
548 | if (index_range_of_same_dir(a_src_dir.buf, a_src_dir.len, | |
549 | &dummy, &dummy) < 1) { | |
550 | remove_dir_recursively(&a_src_dir, 0); | |
707fa2f7 | 551 | } |
b6f51e3d | 552 | strbuf_reset(&a_src_dir); |
11be42a4 JS |
553 | } |
554 | ||
b6f51e3d SY |
555 | strbuf_release(&a_src_dir); |
556 | free(src_dir); | |
557 | ||
5efd533e SY |
558 | if (dirty_paths.nr) |
559 | advise_on_moving_dirty_path(&dirty_paths); | |
560 | ||
0656781f | 561 | if (gitmodules_modified) |
f59aa5e0 | 562 | stage_updated_gitmodules(the_repository->index); |
0656781f | 563 | |
f59aa5e0 | 564 | if (write_locked_index(the_repository->index, &lock_file, |
61000814 | 565 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
dcadc8b8 | 566 | die(_("Unable to write new index file")); |
11be42a4 | 567 | |
ed3c566d | 568 | string_list_clear(&src_for_dst, 0); |
5784db1b | 569 | string_list_clear(&dirty_paths, 0); |
ed3c566d AH |
570 | UNLEAK(source); |
571 | UNLEAK(dest_path); | |
572 | free(submodule_gitfile); | |
573 | free(modes); | |
11be42a4 JS |
574 | return 0; |
575 | } |