]>
Commit | Line | Data |
---|---|---|
11be42a4 JS |
1 | /* |
2 | * "git mv" builtin command | |
3 | * | |
4 | * Copyright (C) 2006 Johannes Schindelin | |
5 | */ | |
6 | #include <fnmatch.h> | |
7 | ||
8 | #include "cache.h" | |
9 | #include "builtin.h" | |
10 | #include "dir.h" | |
11 | #include "cache-tree.h" | |
12 | #include "path-list.h" | |
13 | ||
14 | static const char builtin_mv_usage[] = | |
15 | "git-mv [-n] [-f] (<source> <destination> | [-k] <source>... <destination>)"; | |
16 | ||
17 | static const char **copy_pathspec(const char *prefix, const char **pathspec, | |
18 | int count, int base_name) | |
19 | { | |
d78b0f3d | 20 | int i; |
11be42a4 JS |
21 | const char **result = xmalloc((count + 1) * sizeof(const char *)); |
22 | memcpy(result, pathspec, count * sizeof(const char *)); | |
23 | result[count] = NULL; | |
d78b0f3d JS |
24 | for (i = 0; i < count; i++) { |
25 | int length = strlen(result[i]); | |
26 | if (length > 0 && result[i][length - 1] == '/') { | |
27 | char *without_slash = xmalloc(length); | |
28 | memcpy(without_slash, result[i], length - 1); | |
6e17886d | 29 | without_slash[length - 1] = '\0'; |
d78b0f3d JS |
30 | result[i] = without_slash; |
31 | } | |
32 | if (base_name) { | |
11be42a4 JS |
33 | const char *last_slash = strrchr(result[i], '/'); |
34 | if (last_slash) | |
35 | result[i] = last_slash + 1; | |
36 | } | |
37 | } | |
38 | return get_pathspec(prefix, result); | |
39 | } | |
40 | ||
41 | static void show_list(const char *label, struct path_list *list) | |
42 | { | |
43 | if (list->nr > 0) { | |
44 | int i; | |
45 | printf("%s", label); | |
46 | for (i = 0; i < list->nr; i++) | |
47 | printf("%s%s", i > 0 ? ", " : "", list->items[i].path); | |
48 | putchar('\n'); | |
49 | } | |
50 | } | |
51 | ||
ac64a722 JS |
52 | static const char *add_slash(const char *path) |
53 | { | |
54 | int len = strlen(path); | |
55 | if (path[len - 1] != '/') { | |
56 | char *with_slash = xmalloc(len + 2); | |
57 | memcpy(with_slash, path, len); | |
329a3047 JH |
58 | with_slash[len++] = '/'; |
59 | with_slash[len] = 0; | |
ac64a722 JS |
60 | return with_slash; |
61 | } | |
62 | return path; | |
63 | } | |
64 | ||
11be42a4 JS |
65 | static struct lock_file lock_file; |
66 | ||
7061cf0f | 67 | int cmd_mv(int argc, const char **argv, const char *prefix) |
11be42a4 JS |
68 | { |
69 | int i, newfd, count; | |
70 | int verbose = 0, show_only = 0, force = 0, ignore_errors = 0; | |
11be42a4 | 71 | const char **source, **destination, **dest_path; |
ac64a722 | 72 | enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes; |
11be42a4 JS |
73 | struct stat st; |
74 | struct path_list overwritten = {NULL, 0, 0, 0}; | |
75 | struct path_list src_for_dst = {NULL, 0, 0, 0}; | |
76 | struct path_list added = {NULL, 0, 0, 0}; | |
77 | struct path_list deleted = {NULL, 0, 0, 0}; | |
78 | struct path_list changed = {NULL, 0, 0, 0}; | |
79 | ||
80 | git_config(git_default_config); | |
81 | ||
40aaae88 | 82 | newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1); |
11be42a4 JS |
83 | if (read_cache() < 0) |
84 | die("index file corrupt"); | |
85 | ||
86 | for (i = 1; i < argc; i++) { | |
87 | const char *arg = argv[i]; | |
88 | ||
89 | if (arg[0] != '-') | |
90 | break; | |
91 | if (!strcmp(arg, "--")) { | |
92 | i++; | |
93 | break; | |
94 | } | |
95 | if (!strcmp(arg, "-n")) { | |
96 | show_only = 1; | |
97 | continue; | |
98 | } | |
99 | if (!strcmp(arg, "-f")) { | |
100 | force = 1; | |
101 | continue; | |
102 | } | |
103 | if (!strcmp(arg, "-k")) { | |
104 | ignore_errors = 1; | |
105 | continue; | |
106 | } | |
cba05fa8 | 107 | usage(builtin_mv_usage); |
11be42a4 JS |
108 | } |
109 | count = argc - i - 1; | |
110 | if (count < 1) | |
111 | usage(builtin_mv_usage); | |
112 | ||
113 | source = copy_pathspec(prefix, argv + i, count, 0); | |
ac64a722 | 114 | modes = xcalloc(count, sizeof(enum update_mode)); |
11be42a4 JS |
115 | dest_path = copy_pathspec(prefix, argv + argc - 1, 1, 0); |
116 | ||
c5203bdf JS |
117 | if (dest_path[0][0] == '\0') |
118 | /* special case: "." was normalized to "" */ | |
119 | destination = copy_pathspec(dest_path[0], argv + i, count, 1); | |
120 | else if (!lstat(dest_path[0], &st) && | |
ac64a722 JS |
121 | S_ISDIR(st.st_mode)) { |
122 | dest_path[0] = add_slash(dest_path[0]); | |
11be42a4 | 123 | destination = copy_pathspec(dest_path[0], argv + i, count, 1); |
ac64a722 | 124 | } else { |
11be42a4 JS |
125 | if (count != 1) |
126 | usage(builtin_mv_usage); | |
127 | destination = dest_path; | |
128 | } | |
129 | ||
130 | /* Checking */ | |
131 | for (i = 0; i < count; i++) { | |
60a6bf5f JS |
132 | const char *src = source[i], *dst = destination[i]; |
133 | int length, src_is_dir; | |
11be42a4 JS |
134 | const char *bad = NULL; |
135 | ||
136 | if (show_only) | |
60a6bf5f | 137 | printf("Checking rename of '%s' to '%s'\n", src, dst); |
11be42a4 | 138 | |
60a6bf5f JS |
139 | length = strlen(src); |
140 | if (lstat(src, &st) < 0) | |
11be42a4 | 141 | bad = "bad source"; |
60a6bf5f JS |
142 | else if (!strncmp(src, dst, length) && |
143 | (dst[length] == 0 || dst[length] == '/')) { | |
d78b0f3d | 144 | bad = "can not move directory into itself"; |
60a6bf5f JS |
145 | } else if ((src_is_dir = S_ISDIR(st.st_mode)) |
146 | && lstat(dst, &st) == 0) | |
147 | bad = "cannot move directory over file"; | |
148 | else if (src_is_dir) { | |
149 | int first, last; | |
ac64a722 JS |
150 | |
151 | modes[i] = WORKING_DIRECTORY; | |
152 | ||
60a6bf5f | 153 | first = cache_name_pos(src, length); |
ac64a722 | 154 | if (first >= 0) |
60a6bf5f | 155 | die ("Huh? %s/ is in index?", src); |
ac64a722 JS |
156 | |
157 | first = -1 - first; | |
158 | for (last = first; last < active_nr; last++) { | |
159 | const char *path = active_cache[last]->name; | |
60a6bf5f JS |
160 | if (strncmp(path, src, length) |
161 | || path[length] != '/') | |
ac64a722 JS |
162 | break; |
163 | } | |
164 | ||
165 | if (last - first < 1) | |
166 | bad = "source directory is empty"; | |
60a6bf5f JS |
167 | else { |
168 | int j, dst_len; | |
ac64a722 JS |
169 | |
170 | if (last - first > 0) { | |
83572c1a | 171 | source = xrealloc(source, |
ac64a722 JS |
172 | (count + last - first) |
173 | * sizeof(char *)); | |
83572c1a | 174 | destination = xrealloc(destination, |
ac64a722 JS |
175 | (count + last - first) |
176 | * sizeof(char *)); | |
83572c1a | 177 | modes = xrealloc(modes, |
ac64a722 JS |
178 | (count + last - first) |
179 | * sizeof(enum update_mode)); | |
180 | } | |
181 | ||
60a6bf5f JS |
182 | dst = add_slash(dst); |
183 | dst_len = strlen(dst) - 1; | |
ac64a722 JS |
184 | |
185 | for (j = 0; j < last - first; j++) { | |
186 | const char *path = | |
187 | active_cache[first + j]->name; | |
188 | source[count + j] = path; | |
189 | destination[count + j] = | |
60a6bf5f JS |
190 | prefix_path(dst, dst_len, |
191 | path + length); | |
ac64a722 JS |
192 | modes[count + j] = INDEX; |
193 | } | |
194 | count += last - first; | |
195 | } | |
60a6bf5f | 196 | } else if (lstat(dst, &st) == 0) { |
11be42a4 JS |
197 | bad = "destination exists"; |
198 | if (force) { | |
199 | /* | |
200 | * only files can overwrite each other: | |
201 | * check both source and destination | |
202 | */ | |
203 | if (S_ISREG(st.st_mode)) { | |
204 | fprintf(stderr, "Warning: %s;" | |
205 | " will overwrite!\n", | |
206 | bad); | |
207 | bad = NULL; | |
60a6bf5f | 208 | path_list_insert(dst, &overwritten); |
11be42a4 JS |
209 | } else |
210 | bad = "Cannot overwrite"; | |
211 | } | |
60a6bf5f | 212 | } else if (cache_name_pos(src, length) < 0) |
11be42a4 | 213 | bad = "not under version control"; |
60a6bf5f JS |
214 | else if (path_list_has_path(&src_for_dst, dst)) |
215 | bad = "multiple sources for the same target"; | |
216 | else | |
217 | path_list_insert(dst, &src_for_dst); | |
11be42a4 | 218 | |
11be42a4 JS |
219 | if (bad) { |
220 | if (ignore_errors) { | |
221 | if (--count > 0) { | |
222 | memmove(source + i, source + i + 1, | |
223 | (count - i) * sizeof(char *)); | |
224 | memmove(destination + i, | |
225 | destination + i + 1, | |
226 | (count - i) * sizeof(char *)); | |
227 | } | |
228 | } else | |
ac64a722 | 229 | die ("%s, source=%s, destination=%s", |
60a6bf5f | 230 | bad, src, dst); |
11be42a4 JS |
231 | } |
232 | } | |
233 | ||
234 | for (i = 0; i < count; i++) { | |
60a6bf5f JS |
235 | const char *src = source[i], *dst = destination[i]; |
236 | enum update_mode mode = modes[i]; | |
11be42a4 | 237 | if (show_only || verbose) |
60a6bf5f JS |
238 | printf("Renaming %s to %s\n", src, dst); |
239 | if (!show_only && mode != INDEX && | |
240 | rename(src, dst) < 0 && !ignore_errors) | |
241 | die ("renaming %s failed: %s", src, strerror(errno)); | |
242 | ||
243 | if (mode == WORKING_DIRECTORY) | |
ac64a722 JS |
244 | continue; |
245 | ||
60a6bf5f JS |
246 | if (cache_name_pos(src, strlen(src)) >= 0) { |
247 | path_list_insert(src, &deleted); | |
11be42a4 JS |
248 | |
249 | /* destination can be a directory with 1 file inside */ | |
60a6bf5f JS |
250 | if (path_list_has_path(&overwritten, dst)) |
251 | path_list_insert(dst, &changed); | |
11be42a4 | 252 | else |
60a6bf5f | 253 | path_list_insert(dst, &added); |
11be42a4 | 254 | } else |
60a6bf5f | 255 | path_list_insert(dst, &added); |
11be42a4 JS |
256 | } |
257 | ||
258 | if (show_only) { | |
259 | show_list("Changed : ", &changed); | |
260 | show_list("Adding : ", &added); | |
261 | show_list("Deleting : ", &deleted); | |
262 | } else { | |
263 | for (i = 0; i < changed.nr; i++) { | |
264 | const char *path = changed.items[i].path; | |
d828f6dd PH |
265 | int j = cache_name_pos(path, strlen(path)); |
266 | struct cache_entry *ce = active_cache[j]; | |
11be42a4 | 267 | |
d828f6dd | 268 | if (j < 0) |
11be42a4 JS |
269 | die ("Huh? Cache entry for %s unknown?", path); |
270 | refresh_cache_entry(ce, 0); | |
271 | } | |
272 | ||
273 | for (i = 0; i < added.nr; i++) { | |
274 | const char *path = added.items[i].path; | |
275 | add_file_to_index(path, verbose); | |
276 | } | |
277 | ||
278 | for (i = 0; i < deleted.nr; i++) { | |
279 | const char *path = deleted.items[i].path; | |
280 | remove_file_from_cache(path); | |
281 | } | |
282 | ||
283 | if (active_cache_changed) { | |
284 | if (write_cache(newfd, active_cache, active_nr) || | |
285 | close(newfd) || | |
286 | commit_lock_file(&lock_file)) | |
287 | die("Unable to write new index file"); | |
288 | } | |
289 | } | |
290 | ||
291 | return 0; | |
292 | } |