Fix header breakage due to redefining PATH_MAX.
[git/gitweb.git] / builtin-mv.c
blob62ae937cb13f43f871bf2ec3543f7db37f9e87b6
1 /*
2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
5 */
6 #include <fnmatch.h>
8 #include "cache.h"
9 #include "builtin.h"
10 #include "dir.h"
11 #include "cache-tree.h"
12 #include "path-list.h"
14 static const char builtin_mv_usage[] =
15 "git-mv [-n] [-f] (<source> <destination> | [-k] <source>... <destination>)";
17 static const char **copy_pathspec(const char *prefix, const char **pathspec,
18 int count, int base_name)
20 const char **result = xmalloc((count + 1) * sizeof(const char *));
21 memcpy(result, pathspec, count * sizeof(const char *));
22 result[count] = NULL;
23 if (base_name) {
24 int i;
25 for (i = 0; i < count; i++) {
26 const char *last_slash = strrchr(result[i], '/');
27 if (last_slash)
28 result[i] = last_slash + 1;
31 return get_pathspec(prefix, result);
34 static void show_list(const char *label, struct path_list *list)
36 if (list->nr > 0) {
37 int i;
38 printf("%s", label);
39 for (i = 0; i < list->nr; i++)
40 printf("%s%s", i > 0 ? ", " : "", list->items[i].path);
41 putchar('\n');
45 static const char *add_slash(const char *path)
47 int len = strlen(path);
48 if (path[len - 1] != '/') {
49 char *with_slash = xmalloc(len + 2);
50 memcpy(with_slash, path, len);
51 strcat(with_slash + len, "/");
52 return with_slash;
54 return path;
57 static struct lock_file lock_file;
59 int cmd_mv(int argc, const char **argv, const char *prefix)
61 int i, newfd, count;
62 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
63 const char **source, **destination, **dest_path;
64 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
65 struct stat st;
66 struct path_list overwritten = {NULL, 0, 0, 0};
67 struct path_list src_for_dst = {NULL, 0, 0, 0};
68 struct path_list added = {NULL, 0, 0, 0};
69 struct path_list deleted = {NULL, 0, 0, 0};
70 struct path_list changed = {NULL, 0, 0, 0};
72 git_config(git_default_config);
74 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
75 if (newfd < 0)
76 die("unable to create new index file");
78 if (read_cache() < 0)
79 die("index file corrupt");
81 for (i = 1; i < argc; i++) {
82 const char *arg = argv[i];
84 if (arg[0] != '-')
85 break;
86 if (!strcmp(arg, "--")) {
87 i++;
88 break;
90 if (!strcmp(arg, "-n")) {
91 show_only = 1;
92 continue;
94 if (!strcmp(arg, "-f")) {
95 force = 1;
96 continue;
98 if (!strcmp(arg, "-k")) {
99 ignore_errors = 1;
100 continue;
102 die(builtin_mv_usage);
104 count = argc - i - 1;
105 if (count < 1)
106 usage(builtin_mv_usage);
108 source = copy_pathspec(prefix, argv + i, count, 0);
109 modes = xcalloc(count, sizeof(enum update_mode));
110 dest_path = copy_pathspec(prefix, argv + argc - 1, 1, 0);
112 if (!lstat(dest_path[0], &st) &&
113 S_ISDIR(st.st_mode)) {
114 dest_path[0] = add_slash(dest_path[0]);
115 destination = copy_pathspec(dest_path[0], argv + i, count, 1);
116 } else {
117 if (count != 1)
118 usage(builtin_mv_usage);
119 destination = dest_path;
122 /* Checking */
123 for (i = 0; i < count; i++) {
124 const char *bad = NULL;
126 if (show_only)
127 printf("Checking rename of '%s' to '%s'\n",
128 source[i], destination[i]);
130 if (lstat(source[i], &st) < 0)
131 bad = "bad source";
133 if (S_ISDIR(st.st_mode)) {
134 const char *dir = source[i], *dest_dir = destination[i];
135 int first, last, len = strlen(dir);
137 if (lstat(dest_dir, &st) == 0) {
138 bad = "cannot move directory over file";
139 goto next;
142 modes[i] = WORKING_DIRECTORY;
144 first = cache_name_pos(source[i], len);
145 if (first >= 0)
146 die ("Huh? %s/ is in index?", dir);
148 first = -1 - first;
149 for (last = first; last < active_nr; last++) {
150 const char *path = active_cache[last]->name;
151 if (strncmp(path, dir, len) || path[len] != '/')
152 break;
155 if (last - first < 1)
156 bad = "source directory is empty";
157 else if (!bad) {
158 int j, dst_len = strlen(dest_dir);
160 if (last - first > 0) {
161 source = realloc(source,
162 (count + last - first)
163 * sizeof(char *));
164 destination = realloc(destination,
165 (count + last - first)
166 * sizeof(char *));
167 modes = realloc(modes,
168 (count + last - first)
169 * sizeof(enum update_mode));
172 dest_dir = add_slash(dest_dir);
174 for (j = 0; j < last - first; j++) {
175 const char *path =
176 active_cache[first + j]->name;
177 source[count + j] = path;
178 destination[count + j] =
179 prefix_path(dest_dir, dst_len,
180 path + len);
181 modes[count + j] = INDEX;
183 count += last - first;
186 goto next;
189 if (!bad && lstat(destination[i], &st) == 0) {
190 bad = "destination exists";
191 if (force) {
193 * only files can overwrite each other:
194 * check both source and destination
196 if (S_ISREG(st.st_mode)) {
197 fprintf(stderr, "Warning: %s;"
198 " will overwrite!\n",
199 bad);
200 bad = NULL;
201 path_list_insert(destination[i],
202 &overwritten);
203 } else
204 bad = "Cannot overwrite";
208 if (!bad &&
209 !strncmp(destination[i], source[i], strlen(source[i])))
210 bad = "can not move directory into itself";
212 if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
213 bad = "not under version control";
215 if (!bad) {
216 if (path_list_has_path(&src_for_dst, destination[i]))
217 bad = "multiple sources for the same target";
218 else
219 path_list_insert(destination[i], &src_for_dst);
222 next:
223 if (bad) {
224 if (ignore_errors) {
225 if (--count > 0) {
226 memmove(source + i, source + i + 1,
227 (count - i) * sizeof(char *));
228 memmove(destination + i,
229 destination + i + 1,
230 (count - i) * sizeof(char *));
232 } else
233 die ("%s, source=%s, destination=%s",
234 bad, source[i], destination[i]);
238 for (i = 0; i < count; i++) {
239 if (show_only || verbose)
240 printf("Renaming %s to %s\n",
241 source[i], destination[i]);
242 if (!show_only && modes[i] != INDEX &&
243 rename(source[i], destination[i]) < 0 &&
244 !ignore_errors)
245 die ("renaming %s failed: %s",
246 source[i], strerror(errno));
248 if (modes[i] == WORKING_DIRECTORY)
249 continue;
251 if (cache_name_pos(source[i], strlen(source[i])) >= 0) {
252 path_list_insert(source[i], &deleted);
254 /* destination can be a directory with 1 file inside */
255 if (path_list_has_path(&overwritten, destination[i]))
256 path_list_insert(destination[i], &changed);
257 else
258 path_list_insert(destination[i], &added);
259 } else
260 path_list_insert(destination[i], &added);
263 if (show_only) {
264 show_list("Changed : ", &changed);
265 show_list("Adding : ", &added);
266 show_list("Deleting : ", &deleted);
267 } else {
268 for (i = 0; i < changed.nr; i++) {
269 const char *path = changed.items[i].path;
270 int i = cache_name_pos(path, strlen(path));
271 struct cache_entry *ce = active_cache[i];
273 if (i < 0)
274 die ("Huh? Cache entry for %s unknown?", path);
275 refresh_cache_entry(ce, 0);
278 for (i = 0; i < added.nr; i++) {
279 const char *path = added.items[i].path;
280 add_file_to_index(path, verbose);
283 for (i = 0; i < deleted.nr; i++) {
284 const char *path = deleted.items[i].path;
285 remove_file_from_cache(path);
288 if (active_cache_changed) {
289 if (write_cache(newfd, active_cache, active_nr) ||
290 close(newfd) ||
291 commit_lock_file(&lock_file))
292 die("Unable to write new index file");
296 return 0;