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