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