Commit | Line | Data |
---|---|---|
11be42a4 JS |
1 | /* |
2 | * "git mv" builtin command | |
3 | * | |
4 | * Copyright (C) 2006 Johannes Schindelin | |
5 | */ | |
11be42a4 JS |
6 | #include "cache.h" |
7 | #include "builtin.h" | |
8 | #include "dir.h" | |
9 | #include "cache-tree.h" | |
c455c87c | 10 | #include "string-list.h" |
c7a20c11 | 11 | #include "parse-options.h" |
a88c915d | 12 | #include "submodule.h" |
11be42a4 | 13 | |
c7a20c11 | 14 | static const char * const builtin_mv_usage[] = { |
6c54dc46 | 15 | N_("git mv [options] <source>... <destination>"), |
c7a20c11 PH |
16 | NULL |
17 | }; | |
11be42a4 | 18 | |
c57f6281 MM |
19 | #define DUP_BASENAME 1 |
20 | #define KEEP_TRAILING_SLASH 2 | |
21 | ||
e4d92cdc NTND |
22 | static const char **internal_copy_pathspec(const char *prefix, |
23 | const char **pathspec, | |
c57f6281 | 24 | int count, unsigned flags) |
11be42a4 | 25 | { |
d78b0f3d | 26 | int i; |
11be42a4 JS |
27 | const char **result = xmalloc((count + 1) * sizeof(const char *)); |
28 | memcpy(result, pathspec, count * sizeof(const char *)); | |
29 | result[count] = NULL; | |
d78b0f3d JS |
30 | for (i = 0; i < count; i++) { |
31 | int length = strlen(result[i]); | |
af82559b | 32 | int to_copy = length; |
c57f6281 MM |
33 | while (!(flags & KEEP_TRAILING_SLASH) && |
34 | to_copy > 0 && is_dir_sep(result[i][to_copy - 1])) | |
af82559b | 35 | to_copy--; |
c57f6281 | 36 | if (to_copy != length || flags & DUP_BASENAME) { |
af82559b | 37 | char *it = xmemdupz(result[i], to_copy); |
c57f6281 | 38 | if (flags & DUP_BASENAME) { |
0d0ff65c BC |
39 | result[i] = xstrdup(basename(it)); |
40 | free(it); | |
41 | } else | |
42 | result[i] = it; | |
af82559b | 43 | } |
11be42a4 | 44 | } |
971dfa19 | 45 | return get_pathspec(prefix, result); |
11be42a4 JS |
46 | } |
47 | ||
ac64a722 JS |
48 | static const char *add_slash(const char *path) |
49 | { | |
50 | int len = strlen(path); | |
51 | if (path[len - 1] != '/') { | |
52 | char *with_slash = xmalloc(len + 2); | |
53 | memcpy(with_slash, path, len); | |
329a3047 JH |
54 | with_slash[len++] = '/'; |
55 | with_slash[len] = 0; | |
ac64a722 JS |
56 | return with_slash; |
57 | } | |
58 | return path; | |
59 | } | |
60 | ||
11be42a4 | 61 | static struct lock_file lock_file; |
04c1ee57 | 62 | #define SUBMODULE_WITH_GITDIR ((const char *)1) |
11be42a4 | 63 | |
7061cf0f | 64 | int cmd_mv(int argc, const char **argv, const char *prefix) |
11be42a4 | 65 | { |
0656781f | 66 | int i, newfd, gitmodules_modified = 0; |
11be42a4 | 67 | int verbose = 0, show_only = 0, force = 0, ignore_errors = 0; |
c7a20c11 | 68 | struct option builtin_mv_options[] = { |
6c54dc46 NTND |
69 | OPT__VERBOSE(&verbose, N_("be verbose")), |
70 | OPT__DRY_RUN(&show_only, N_("dry run")), | |
71 | OPT__FORCE(&force, N_("force move/rename even if target exists")), | |
d5d09d47 | 72 | OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")), |
c7a20c11 PH |
73 | OPT_END(), |
74 | }; | |
a88c915d | 75 | const char **source, **destination, **dest_path, **submodule_gitfile; |
ac64a722 | 76 | enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes; |
11be42a4 | 77 | struct stat st; |
183113a5 | 78 | struct string_list src_for_dst = STRING_LIST_INIT_NODUP; |
11be42a4 | 79 | |
0656781f | 80 | gitmodules_config(); |
ef90d6d4 | 81 | git_config(git_default_config, NULL); |
11be42a4 | 82 | |
37782920 SB |
83 | argc = parse_options(argc, argv, prefix, builtin_mv_options, |
84 | builtin_mv_usage, 0); | |
c7a20c11 PH |
85 | if (--argc < 1) |
86 | usage_with_options(builtin_mv_usage, builtin_mv_options); | |
11be42a4 | 87 | |
99caeed0 JN |
88 | newfd = hold_locked_index(&lock_file, 1); |
89 | if (read_cache() < 0) | |
431b049e | 90 | die(_("index file corrupt")); |
99caeed0 | 91 | |
e4d92cdc | 92 | source = internal_copy_pathspec(prefix, argv, argc, 0); |
c7a20c11 | 93 | modes = xcalloc(argc, sizeof(enum update_mode)); |
c57f6281 MM |
94 | /* |
95 | * Keep trailing slash, needed to let | |
96 | * "git mv file no-such-dir/" error out. | |
97 | */ | |
98 | dest_path = internal_copy_pathspec(prefix, argv + argc, 1, | |
99 | KEEP_TRAILING_SLASH); | |
a88c915d | 100 | submodule_gitfile = xcalloc(argc, sizeof(char *)); |
11be42a4 | 101 | |
c5203bdf JS |
102 | if (dest_path[0][0] == '\0') |
103 | /* special case: "." was normalized to "" */ | |
c57f6281 | 104 | destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME); |
c5203bdf | 105 | else if (!lstat(dest_path[0], &st) && |
ac64a722 JS |
106 | S_ISDIR(st.st_mode)) { |
107 | dest_path[0] = add_slash(dest_path[0]); | |
c57f6281 | 108 | destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME); |
ac64a722 | 109 | } else { |
c7a20c11 | 110 | if (argc != 1) |
77471646 | 111 | die("destination '%s' is not a directory", dest_path[0]); |
11be42a4 JS |
112 | destination = dest_path; |
113 | } | |
114 | ||
115 | /* Checking */ | |
c7a20c11 | 116 | for (i = 0; i < argc; i++) { |
60a6bf5f JS |
117 | const char *src = source[i], *dst = destination[i]; |
118 | int length, src_is_dir; | |
11be42a4 JS |
119 | const char *bad = NULL; |
120 | ||
121 | if (show_only) | |
431b049e | 122 | printf(_("Checking rename of '%s' to '%s'\n"), src, dst); |
11be42a4 | 123 | |
60a6bf5f JS |
124 | length = strlen(src); |
125 | if (lstat(src, &st) < 0) | |
a7d5629f | 126 | bad = _("bad source"); |
60a6bf5f JS |
127 | else if (!strncmp(src, dst, length) && |
128 | (dst[length] == 0 || dst[length] == '/')) { | |
a7d5629f | 129 | bad = _("can not move directory into itself"); |
60a6bf5f JS |
130 | } else if ((src_is_dir = S_ISDIR(st.st_mode)) |
131 | && lstat(dst, &st) == 0) | |
a7d5629f | 132 | bad = _("cannot move directory over file"); |
60a6bf5f | 133 | else if (src_is_dir) { |
11502468 JL |
134 | int first = cache_name_pos(src, length); |
135 | if (first >= 0) { | |
a88c915d | 136 | struct strbuf submodule_dotgit = STRBUF_INIT; |
11502468 JL |
137 | if (!S_ISGITLINK(active_cache[first]->ce_mode)) |
138 | die (_("Huh? Directory %s is in index and no submodule?"), src); | |
0656781f JL |
139 | if (!is_staging_gitmodules_ok()) |
140 | die (_("Please, stage your changes to .gitmodules or stash them to proceed")); | |
a88c915d JL |
141 | strbuf_addf(&submodule_dotgit, "%s/.git", src); |
142 | submodule_gitfile[i] = read_gitfile(submodule_dotgit.buf); | |
143 | if (submodule_gitfile[i]) | |
144 | submodule_gitfile[i] = xstrdup(submodule_gitfile[i]); | |
04c1ee57 JL |
145 | else |
146 | submodule_gitfile[i] = SUBMODULE_WITH_GITDIR; | |
a88c915d | 147 | strbuf_release(&submodule_dotgit); |
11502468 JL |
148 | } else { |
149 | const char *src_w_slash = add_slash(src); | |
150 | int last, len_w_slash = length + 1; | |
151 | ||
152 | modes[i] = WORKING_DIRECTORY; | |
153 | ||
154 | first = cache_name_pos(src_w_slash, len_w_slash); | |
155 | if (first >= 0) | |
156 | die (_("Huh? %.*s is in index?"), | |
157 | len_w_slash, src_w_slash); | |
158 | ||
159 | first = -1 - first; | |
160 | for (last = first; last < active_nr; last++) { | |
161 | const char *path = active_cache[last]->name; | |
162 | if (strncmp(path, src_w_slash, len_w_slash)) | |
163 | break; | |
ac64a722 | 164 | } |
11502468 JL |
165 | free((char *)src_w_slash); |
166 | ||
167 | if (last - first < 1) | |
168 | bad = _("source directory is empty"); | |
169 | else { | |
170 | int j, dst_len; | |
ac64a722 | 171 | |
11502468 JL |
172 | if (last - first > 0) { |
173 | source = xrealloc(source, | |
174 | (argc + last - first) | |
175 | * sizeof(char *)); | |
176 | destination = xrealloc(destination, | |
177 | (argc + last - first) | |
178 | * sizeof(char *)); | |
179 | modes = xrealloc(modes, | |
180 | (argc + last - first) | |
181 | * sizeof(enum update_mode)); | |
182 | } | |
183 | ||
184 | dst = add_slash(dst); | |
185 | dst_len = strlen(dst); | |
186 | ||
187 | for (j = 0; j < last - first; j++) { | |
188 | const char *path = | |
189 | active_cache[first + j]->name; | |
190 | source[argc + j] = path; | |
191 | destination[argc + j] = | |
192 | prefix_path(dst, dst_len, | |
193 | path + length + 1); | |
194 | modes[argc + j] = INDEX; | |
195 | } | |
196 | argc += last - first; | |
ac64a722 | 197 | } |
ac64a722 | 198 | } |
5aed3c6a | 199 | } else if (cache_name_pos(src, length) < 0) |
a7d5629f | 200 | bad = _("not under version control"); |
5aed3c6a | 201 | else if (lstat(dst, &st) == 0) { |
a7d5629f | 202 | bad = _("destination exists"); |
11be42a4 JS |
203 | if (force) { |
204 | /* | |
205 | * only files can overwrite each other: | |
206 | * check both source and destination | |
207 | */ | |
81dc2307 | 208 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { |
534376ca JK |
209 | if (verbose) |
210 | warning(_("overwriting '%s'"), dst); | |
11be42a4 | 211 | bad = NULL; |
11be42a4 | 212 | } else |
a7d5629f | 213 | bad = _("Cannot overwrite"); |
11be42a4 | 214 | } |
5aed3c6a | 215 | } else if (string_list_has_string(&src_for_dst, dst)) |
a7d5629f | 216 | bad = _("multiple sources for the same target"); |
a8933469 JS |
217 | else if (is_dir_sep(dst[strlen(dst) - 1])) |
218 | bad = _("destination directory does not exist"); | |
60a6bf5f | 219 | else |
78a395d3 | 220 | string_list_insert(&src_for_dst, dst); |
11be42a4 | 221 | |
11be42a4 JS |
222 | if (bad) { |
223 | if (ignore_errors) { | |
c7a20c11 | 224 | if (--argc > 0) { |
11be42a4 | 225 | memmove(source + i, source + i + 1, |
c7a20c11 | 226 | (argc - i) * sizeof(char *)); |
11be42a4 JS |
227 | memmove(destination + i, |
228 | destination + i + 1, | |
c7a20c11 | 229 | (argc - i) * sizeof(char *)); |
be17262d | 230 | i--; |
11be42a4 JS |
231 | } |
232 | } else | |
431b049e | 233 | die (_("%s, source=%s, destination=%s"), |
60a6bf5f | 234 | bad, src, dst); |
11be42a4 JS |
235 | } |
236 | } | |
237 | ||
c7a20c11 | 238 | for (i = 0; i < argc; i++) { |
60a6bf5f JS |
239 | const char *src = source[i], *dst = destination[i]; |
240 | enum update_mode mode = modes[i]; | |
81dc2307 | 241 | int pos; |
11be42a4 | 242 | if (show_only || verbose) |
431b049e | 243 | printf(_("Renaming %s to %s\n"), src, dst); |
a88c915d JL |
244 | if (!show_only && mode != INDEX) { |
245 | if (rename(src, dst) < 0 && !ignore_errors) | |
246 | die_errno (_("renaming '%s' failed"), src); | |
04c1ee57 JL |
247 | if (submodule_gitfile[i]) { |
248 | if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR) | |
249 | connect_work_tree_and_git_dir(dst, submodule_gitfile[i]); | |
250 | if (!update_path_in_gitmodules(src, dst)) | |
251 | gitmodules_modified = 1; | |
252 | } | |
a88c915d | 253 | } |
60a6bf5f JS |
254 | |
255 | if (mode == WORKING_DIRECTORY) | |
ac64a722 JS |
256 | continue; |
257 | ||
81dc2307 PB |
258 | pos = cache_name_pos(src, strlen(src)); |
259 | assert(pos >= 0); | |
260 | if (!show_only) | |
261 | rename_cache_entry_at(pos, dst); | |
11be42a4 JS |
262 | } |
263 | ||
0656781f JL |
264 | if (gitmodules_modified) |
265 | stage_updated_gitmodules(); | |
266 | ||
81dc2307 PB |
267 | if (active_cache_changed) { |
268 | if (write_cache(newfd, active_cache, active_nr) || | |
269 | commit_locked_index(&lock_file)) | |
431b049e | 270 | die(_("Unable to write new index file")); |
11be42a4 JS |
271 | } |
272 | ||
273 | return 0; | |
274 | } |