]>
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" |
11be42a4 | 16 | |
c7a20c11 | 17 | static const char * const builtin_mv_usage[] = { |
9c9b4f2f | 18 | N_("git mv [<options>] <source>... <destination>"), |
c7a20c11 PH |
19 | NULL |
20 | }; | |
11be42a4 | 21 | |
c57f6281 MM |
22 | #define DUP_BASENAME 1 |
23 | #define KEEP_TRAILING_SLASH 2 | |
24 | ||
2ec87741 BW |
25 | static const char **internal_prefix_pathspec(const char *prefix, |
26 | const char **pathspec, | |
27 | int count, unsigned flags) | |
11be42a4 | 28 | { |
d78b0f3d | 29 | int i; |
b32fa95f | 30 | const char **result; |
2ec87741 | 31 | int prefixlen = prefix ? strlen(prefix) : 0; |
b32fa95f | 32 | ALLOC_ARRAY(result, count + 1); |
2ec87741 BW |
33 | |
34 | /* Create an intermediate copy of the pathspec based on the flags */ | |
d78b0f3d | 35 | for (i = 0; i < count; i++) { |
2ec87741 | 36 | int length = strlen(pathspec[i]); |
af82559b | 37 | int to_copy = length; |
2ec87741 | 38 | char *it; |
c57f6281 | 39 | while (!(flags & KEEP_TRAILING_SLASH) && |
2ec87741 | 40 | to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1])) |
af82559b | 41 | to_copy--; |
2ec87741 BW |
42 | |
43 | it = xmemdupz(pathspec[i], to_copy); | |
44 | if (flags & DUP_BASENAME) { | |
45 | result[i] = xstrdup(basename(it)); | |
46 | free(it); | |
47 | } else { | |
48 | result[i] = it; | |
af82559b | 49 | } |
11be42a4 | 50 | } |
2ec87741 BW |
51 | result[count] = NULL; |
52 | ||
53 | /* Prefix the pathspec and free the old intermediate strings */ | |
54 | for (i = 0; i < count; i++) { | |
55 | const char *match = prefix_path(prefix, prefixlen, result[i]); | |
56 | free((char *) result[i]); | |
57 | result[i] = match; | |
58 | } | |
59 | ||
60 | return result; | |
11be42a4 JS |
61 | } |
62 | ||
ac64a722 JS |
63 | static const char *add_slash(const char *path) |
64 | { | |
50a6c8ef | 65 | size_t len = strlen(path); |
ac64a722 | 66 | if (path[len - 1] != '/') { |
50a6c8ef | 67 | char *with_slash = xmalloc(st_add(len, 2)); |
ac64a722 | 68 | memcpy(with_slash, path, len); |
329a3047 JH |
69 | with_slash[len++] = '/'; |
70 | with_slash[len] = 0; | |
ac64a722 JS |
71 | return with_slash; |
72 | } | |
73 | return path; | |
74 | } | |
75 | ||
04c1ee57 | 76 | #define SUBMODULE_WITH_GITDIR ((const char *)1) |
11be42a4 | 77 | |
3af05a6d NTND |
78 | static void prepare_move_submodule(const char *src, int first, |
79 | const char **submodule_gitfile) | |
80 | { | |
81 | struct strbuf submodule_dotgit = STRBUF_INIT; | |
82 | if (!S_ISGITLINK(active_cache[first]->ce_mode)) | |
83 | die(_("Directory %s is in index and no submodule?"), src); | |
91b83480 | 84 | if (!is_staging_gitmodules_ok(&the_index)) |
3af05a6d NTND |
85 | die(_("Please stage your changes to .gitmodules or stash them to proceed")); |
86 | strbuf_addf(&submodule_dotgit, "%s/.git", src); | |
87 | *submodule_gitfile = read_gitfile(submodule_dotgit.buf); | |
88 | if (*submodule_gitfile) | |
89 | *submodule_gitfile = xstrdup(*submodule_gitfile); | |
90 | else | |
91 | *submodule_gitfile = SUBMODULE_WITH_GITDIR; | |
92 | strbuf_release(&submodule_dotgit); | |
93 | } | |
94 | ||
e2b6cfa0 NTND |
95 | static int index_range_of_same_dir(const char *src, int length, |
96 | int *first_p, int *last_p) | |
97 | { | |
98 | const char *src_w_slash = add_slash(src); | |
99 | int first, last, len_w_slash = length + 1; | |
100 | ||
101 | first = cache_name_pos(src_w_slash, len_w_slash); | |
102 | if (first >= 0) | |
103 | die(_("%.*s is in index"), len_w_slash, src_w_slash); | |
104 | ||
105 | first = -1 - first; | |
106 | for (last = first; last < active_nr; last++) { | |
107 | const char *path = active_cache[last]->name; | |
108 | if (strncmp(path, src_w_slash, len_w_slash)) | |
109 | break; | |
110 | } | |
111 | if (src_w_slash != src) | |
112 | free((char *)src_w_slash); | |
113 | *first_p = first; | |
114 | *last_p = last; | |
115 | return last - first; | |
116 | } | |
117 | ||
7061cf0f | 118 | int cmd_mv(int argc, const char **argv, const char *prefix) |
11be42a4 | 119 | { |
189d035e | 120 | int i, flags, gitmodules_modified = 0; |
11be42a4 | 121 | int verbose = 0, show_only = 0, force = 0, ignore_errors = 0; |
c7a20c11 | 122 | struct option builtin_mv_options[] = { |
6c54dc46 NTND |
123 | OPT__VERBOSE(&verbose, N_("be verbose")), |
124 | OPT__DRY_RUN(&show_only, N_("dry run")), | |
61d15cd6 NTND |
125 | OPT__FORCE(&force, N_("force move/rename even if target exists"), |
126 | PARSE_OPT_NOCOMPLETE), | |
d5d09d47 | 127 | OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")), |
c7a20c11 PH |
128 | OPT_END(), |
129 | }; | |
a88c915d | 130 | const char **source, **destination, **dest_path, **submodule_gitfile; |
ac64a722 | 131 | enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes; |
11be42a4 | 132 | struct stat st; |
183113a5 | 133 | struct string_list src_for_dst = STRING_LIST_INIT_NODUP; |
0fa5a2ed | 134 | struct lock_file lock_file = LOCK_INIT; |
9b906af6 | 135 | struct cache_entry *ce; |
11be42a4 | 136 | |
ef90d6d4 | 137 | git_config(git_default_config, NULL); |
11be42a4 | 138 | |
37782920 SB |
139 | argc = parse_options(argc, argv, prefix, builtin_mv_options, |
140 | builtin_mv_usage, 0); | |
c7a20c11 PH |
141 | if (--argc < 1) |
142 | usage_with_options(builtin_mv_usage, builtin_mv_options); | |
11be42a4 | 143 | |
b3e83cc7 | 144 | hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); |
99caeed0 | 145 | if (read_cache() < 0) |
431b049e | 146 | die(_("index file corrupt")); |
99caeed0 | 147 | |
2ec87741 | 148 | source = internal_prefix_pathspec(prefix, argv, argc, 0); |
c7a20c11 | 149 | modes = xcalloc(argc, sizeof(enum update_mode)); |
c57f6281 MM |
150 | /* |
151 | * Keep trailing slash, needed to let | |
189d035e JS |
152 | * "git mv file no-such-dir/" error out, except in the case |
153 | * "git mv directory no-such-dir/". | |
c57f6281 | 154 | */ |
189d035e JS |
155 | flags = KEEP_TRAILING_SLASH; |
156 | if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1])) | |
157 | flags = 0; | |
2ec87741 | 158 | dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags); |
a88c915d | 159 | submodule_gitfile = xcalloc(argc, sizeof(char *)); |
11be42a4 | 160 | |
c5203bdf JS |
161 | if (dest_path[0][0] == '\0') |
162 | /* special case: "." was normalized to "" */ | |
2ec87741 | 163 | destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME); |
c5203bdf | 164 | else if (!lstat(dest_path[0], &st) && |
ac64a722 JS |
165 | S_ISDIR(st.st_mode)) { |
166 | dest_path[0] = add_slash(dest_path[0]); | |
2ec87741 | 167 | destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME); |
ac64a722 | 168 | } else { |
c7a20c11 | 169 | if (argc != 1) |
eac0ccc2 | 170 | die(_("destination '%s' is not a directory"), dest_path[0]); |
11be42a4 JS |
171 | destination = dest_path; |
172 | } | |
173 | ||
174 | /* Checking */ | |
c7a20c11 | 175 | for (i = 0; i < argc; i++) { |
60a6bf5f JS |
176 | const char *src = source[i], *dst = destination[i]; |
177 | int length, src_is_dir; | |
11be42a4 JS |
178 | const char *bad = NULL; |
179 | ||
180 | if (show_only) | |
431b049e | 181 | printf(_("Checking rename of '%s' to '%s'\n"), src, dst); |
11be42a4 | 182 | |
60a6bf5f JS |
183 | length = strlen(src); |
184 | if (lstat(src, &st) < 0) | |
a7d5629f | 185 | bad = _("bad source"); |
60a6bf5f JS |
186 | else if (!strncmp(src, dst, length) && |
187 | (dst[length] == 0 || dst[length] == '/')) { | |
a7d5629f | 188 | bad = _("can not move directory into itself"); |
60a6bf5f JS |
189 | } else if ((src_is_dir = S_ISDIR(st.st_mode)) |
190 | && lstat(dst, &st) == 0) | |
a7d5629f | 191 | bad = _("cannot move directory over file"); |
60a6bf5f | 192 | else if (src_is_dir) { |
b46b15de | 193 | int first = cache_name_pos(src, length), last; |
3af05a6d NTND |
194 | |
195 | if (first >= 0) | |
196 | prepare_move_submodule(src, first, | |
197 | submodule_gitfile + i); | |
b46b15de NTND |
198 | else if (index_range_of_same_dir(src, length, |
199 | &first, &last) < 1) | |
200 | bad = _("source directory is empty"); | |
201 | else { /* last - first >= 1 */ | |
202 | int j, dst_len, n; | |
11502468 JL |
203 | |
204 | modes[i] = WORKING_DIRECTORY; | |
b46b15de | 205 | n = argc + last - first; |
2756ca43 RS |
206 | REALLOC_ARRAY(source, n); |
207 | REALLOC_ARRAY(destination, n); | |
208 | REALLOC_ARRAY(modes, n); | |
209 | REALLOC_ARRAY(submodule_gitfile, n); | |
b46b15de NTND |
210 | |
211 | dst = add_slash(dst); | |
212 | dst_len = strlen(dst); | |
213 | ||
214 | for (j = 0; j < last - first; j++) { | |
215 | const char *path = active_cache[first + j]->name; | |
216 | source[argc + j] = path; | |
217 | destination[argc + j] = | |
218 | prefix_path(dst, dst_len, path + length + 1); | |
219 | modes[argc + j] = INDEX; | |
220 | submodule_gitfile[argc + j] = NULL; | |
ac64a722 | 221 | } |
b46b15de | 222 | argc += last - first; |
ac64a722 | 223 | } |
9b906af6 | 224 | } else if (!(ce = cache_file_exists(src, length, ignore_case))) { |
a7d5629f | 225 | bad = _("not under version control"); |
9b906af6 CT |
226 | } else if (ce_stage(ce)) { |
227 | bad = _("conflicted"); | |
228 | } else if (lstat(dst, &st) == 0 && | |
baa37bff | 229 | (!ignore_case || strcasecmp(src, dst))) { |
a7d5629f | 230 | bad = _("destination exists"); |
11be42a4 JS |
231 | if (force) { |
232 | /* | |
233 | * only files can overwrite each other: | |
234 | * check both source and destination | |
235 | */ | |
81dc2307 | 236 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { |
534376ca JK |
237 | if (verbose) |
238 | warning(_("overwriting '%s'"), dst); | |
11be42a4 | 239 | bad = NULL; |
11be42a4 | 240 | } else |
a7d5629f | 241 | bad = _("Cannot overwrite"); |
11be42a4 | 242 | } |
5aed3c6a | 243 | } else if (string_list_has_string(&src_for_dst, dst)) |
a7d5629f | 244 | bad = _("multiple sources for the same target"); |
a8933469 JS |
245 | else if (is_dir_sep(dst[strlen(dst) - 1])) |
246 | bad = _("destination directory does not exist"); | |
60a6bf5f | 247 | else |
78a395d3 | 248 | string_list_insert(&src_for_dst, dst); |
11be42a4 | 249 | |
ad1a19d0 NTND |
250 | if (!bad) |
251 | continue; | |
252 | if (!ignore_errors) | |
4f1bbd23 | 253 | die(_("%s, source=%s, destination=%s"), |
ad1a19d0 NTND |
254 | bad, src, dst); |
255 | if (--argc > 0) { | |
256 | int n = argc - i; | |
257 | memmove(source + i, source + i + 1, | |
258 | n * sizeof(char *)); | |
259 | memmove(destination + i, destination + i + 1, | |
260 | n * sizeof(char *)); | |
261 | memmove(modes + i, modes + i + 1, | |
262 | n * sizeof(enum update_mode)); | |
263 | memmove(submodule_gitfile + i, submodule_gitfile + i + 1, | |
264 | n * sizeof(char *)); | |
265 | i--; | |
11be42a4 JS |
266 | } |
267 | } | |
268 | ||
c7a20c11 | 269 | for (i = 0; i < argc; i++) { |
60a6bf5f JS |
270 | const char *src = source[i], *dst = destination[i]; |
271 | enum update_mode mode = modes[i]; | |
81dc2307 | 272 | int pos; |
11be42a4 | 273 | if (show_only || verbose) |
431b049e | 274 | printf(_("Renaming %s to %s\n"), src, dst); |
a127331c SB |
275 | if (show_only) |
276 | continue; | |
277 | if (mode != INDEX && rename(src, dst) < 0) { | |
278 | if (ignore_errors) | |
279 | continue; | |
280 | die_errno(_("renaming '%s' failed"), src); | |
281 | } | |
282 | if (submodule_gitfile[i]) { | |
a127331c SB |
283 | if (!update_path_in_gitmodules(src, dst)) |
284 | gitmodules_modified = 1; | |
da62f786 SB |
285 | if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR) |
286 | connect_work_tree_and_git_dir(dst, | |
287 | submodule_gitfile[i], | |
288 | 1); | |
a88c915d | 289 | } |
60a6bf5f JS |
290 | |
291 | if (mode == WORKING_DIRECTORY) | |
ac64a722 JS |
292 | continue; |
293 | ||
81dc2307 PB |
294 | pos = cache_name_pos(src, strlen(src)); |
295 | assert(pos >= 0); | |
4cbe92fd | 296 | rename_cache_entry_at(pos, dst); |
11be42a4 JS |
297 | } |
298 | ||
0656781f | 299 | if (gitmodules_modified) |
3b8317a9 | 300 | stage_updated_gitmodules(&the_index); |
0656781f | 301 | |
61000814 MÅ |
302 | if (write_locked_index(&the_index, &lock_file, |
303 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) | |
dcadc8b8 | 304 | die(_("Unable to write new index file")); |
11be42a4 JS |
305 | |
306 | return 0; | |
307 | } |