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