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