]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-mv.c
[PATCH] git-mv: add more path normalization
[thirdparty/git.git] / builtin-mv.c
CommitLineData
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
14static const char builtin_mv_usage[] =
15"git-mv [-n] [-f] (<source> <destination> | [-k] <source>... <destination>)";
16
17static 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);
29 without_slash[length] = '\0';
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
41static 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
52static 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
65static struct lock_file lock_file;
66
7061cf0f 67int 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
ac64a722
JS
117 if (!lstat(dest_path[0], &st) &&
118 S_ISDIR(st.st_mode)) {
119 dest_path[0] = add_slash(dest_path[0]);
11be42a4 120 destination = copy_pathspec(dest_path[0], argv + i, count, 1);
ac64a722 121 } else {
11be42a4
JS
122 if (count != 1)
123 usage(builtin_mv_usage);
124 destination = dest_path;
125 }
126
127 /* Checking */
128 for (i = 0; i < count; i++) {
1d6249e6 129 int length;
11be42a4
JS
130 const char *bad = NULL;
131
132 if (show_only)
133 printf("Checking rename of '%s' to '%s'\n",
134 source[i], destination[i]);
135
136 if (lstat(source[i], &st) < 0)
137 bad = "bad source";
ac64a722 138
d78b0f3d
JS
139 if (!bad &&
140 (length = strlen(source[i])) >= 0 &&
141 !strncmp(destination[i], source[i], length) &&
142 (destination[i][length] == 0 || destination[i][length] == '/'))
143 bad = "can not move directory into itself";
144
ac64a722
JS
145 if (S_ISDIR(st.st_mode)) {
146 const char *dir = source[i], *dest_dir = destination[i];
147 int first, last, len = strlen(dir);
148
149 if (lstat(dest_dir, &st) == 0) {
150 bad = "cannot move directory over file";
151 goto next;
152 }
153
154 modes[i] = WORKING_DIRECTORY;
155
156 first = cache_name_pos(source[i], len);
157 if (first >= 0)
158 die ("Huh? %s/ is in index?", dir);
159
160 first = -1 - first;
161 for (last = first; last < active_nr; last++) {
162 const char *path = active_cache[last]->name;
163 if (strncmp(path, dir, len) || path[len] != '/')
164 break;
165 }
166
167 if (last - first < 1)
168 bad = "source directory is empty";
169 else if (!bad) {
170 int j, dst_len = strlen(dest_dir);
171
172 if (last - first > 0) {
173 source = realloc(source,
174 (count + last - first)
175 * sizeof(char *));
176 destination = realloc(destination,
177 (count + last - first)
178 * sizeof(char *));
179 modes = realloc(modes,
180 (count + last - first)
181 * sizeof(enum update_mode));
182 }
183
184 dest_dir = add_slash(dest_dir);
185
186 for (j = 0; j < last - first; j++) {
187 const char *path =
188 active_cache[first + j]->name;
189 source[count + j] = path;
190 destination[count + j] =
191 prefix_path(dest_dir, dst_len,
192 path + len);
193 modes[count + j] = INDEX;
194 }
195 count += last - first;
196 }
197
198 goto next;
199 }
200
201 if (!bad && lstat(destination[i], &st) == 0) {
11be42a4
JS
202 bad = "destination exists";
203 if (force) {
204 /*
205 * only files can overwrite each other:
206 * check both source and destination
207 */
208 if (S_ISREG(st.st_mode)) {
209 fprintf(stderr, "Warning: %s;"
210 " will overwrite!\n",
211 bad);
212 bad = NULL;
213 path_list_insert(destination[i],
214 &overwritten);
215 } else
216 bad = "Cannot overwrite";
217 }
218 }
219
11be42a4
JS
220 if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
221 bad = "not under version control";
222
223 if (!bad) {
224 if (path_list_has_path(&src_for_dst, destination[i]))
225 bad = "multiple sources for the same target";
226 else
227 path_list_insert(destination[i], &src_for_dst);
228 }
229
ac64a722 230next:
11be42a4
JS
231 if (bad) {
232 if (ignore_errors) {
233 if (--count > 0) {
234 memmove(source + i, source + i + 1,
235 (count - i) * sizeof(char *));
236 memmove(destination + i,
237 destination + i + 1,
238 (count - i) * sizeof(char *));
239 }
240 } else
ac64a722 241 die ("%s, source=%s, destination=%s",
11be42a4
JS
242 bad, source[i], destination[i]);
243 }
244 }
245
246 for (i = 0; i < count; i++) {
247 if (show_only || verbose)
248 printf("Renaming %s to %s\n",
249 source[i], destination[i]);
ac64a722 250 if (!show_only && modes[i] != INDEX &&
11be42a4
JS
251 rename(source[i], destination[i]) < 0 &&
252 !ignore_errors)
253 die ("renaming %s failed: %s",
254 source[i], strerror(errno));
255
ac64a722
JS
256 if (modes[i] == WORKING_DIRECTORY)
257 continue;
258
11be42a4
JS
259 if (cache_name_pos(source[i], strlen(source[i])) >= 0) {
260 path_list_insert(source[i], &deleted);
261
262 /* destination can be a directory with 1 file inside */
263 if (path_list_has_path(&overwritten, destination[i]))
264 path_list_insert(destination[i], &changed);
265 else
266 path_list_insert(destination[i], &added);
267 } else
268 path_list_insert(destination[i], &added);
269 }
270
271 if (show_only) {
272 show_list("Changed : ", &changed);
273 show_list("Adding : ", &added);
274 show_list("Deleting : ", &deleted);
275 } else {
276 for (i = 0; i < changed.nr; i++) {
277 const char *path = changed.items[i].path;
278 int i = cache_name_pos(path, strlen(path));
279 struct cache_entry *ce = active_cache[i];
280
281 if (i < 0)
282 die ("Huh? Cache entry for %s unknown?", path);
283 refresh_cache_entry(ce, 0);
284 }
285
286 for (i = 0; i < added.nr; i++) {
287 const char *path = added.items[i].path;
288 add_file_to_index(path, verbose);
289 }
290
291 for (i = 0; i < deleted.nr; i++) {
292 const char *path = deleted.items[i].path;
293 remove_file_from_cache(path);
294 }
295
296 if (active_cache_changed) {
297 if (write_cache(newfd, active_cache, active_nr) ||
298 close(newfd) ||
299 commit_lock_file(&lock_file))
300 die("Unable to write new index file");
301 }
302 }
303
304 return 0;
305}