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