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