Merge branch 'rj/header'
[git/dscho.git] / builtin-mv.c
blobce8187c1e96833e1a6db2fa368a84b02fec7b0c2
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 with_slash[len++] = '/';
52 with_slash[len] = 0;
53 return with_slash;
55 return path;
58 static struct lock_file lock_file;
60 int cmd_mv(int argc, const char **argv, const char *prefix)
62 int i, newfd, count;
63 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
64 const char **source, **destination, **dest_path;
65 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
66 struct stat st;
67 struct path_list overwritten = {NULL, 0, 0, 0};
68 struct path_list src_for_dst = {NULL, 0, 0, 0};
69 struct path_list added = {NULL, 0, 0, 0};
70 struct path_list deleted = {NULL, 0, 0, 0};
71 struct path_list changed = {NULL, 0, 0, 0};
73 git_config(git_default_config);
75 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
76 if (newfd < 0)
77 die("unable to create new index file");
79 if (read_cache() < 0)
80 die("index file corrupt");
82 for (i = 1; i < argc; i++) {
83 const char *arg = argv[i];
85 if (arg[0] != '-')
86 break;
87 if (!strcmp(arg, "--")) {
88 i++;
89 break;
91 if (!strcmp(arg, "-n")) {
92 show_only = 1;
93 continue;
95 if (!strcmp(arg, "-f")) {
96 force = 1;
97 continue;
99 if (!strcmp(arg, "-k")) {
100 ignore_errors = 1;
101 continue;
103 usage(builtin_mv_usage);
105 count = argc - i - 1;
106 if (count < 1)
107 usage(builtin_mv_usage);
109 source = copy_pathspec(prefix, argv + i, count, 0);
110 modes = xcalloc(count, sizeof(enum update_mode));
111 dest_path = copy_pathspec(prefix, argv + argc - 1, 1, 0);
113 if (!lstat(dest_path[0], &st) &&
114 S_ISDIR(st.st_mode)) {
115 dest_path[0] = add_slash(dest_path[0]);
116 destination = copy_pathspec(dest_path[0], argv + i, count, 1);
117 } else {
118 if (count != 1)
119 usage(builtin_mv_usage);
120 destination = dest_path;
123 /* Checking */
124 for (i = 0; i < count; i++) {
125 const char *bad = NULL;
127 if (show_only)
128 printf("Checking rename of '%s' to '%s'\n",
129 source[i], destination[i]);
131 if (lstat(source[i], &st) < 0)
132 bad = "bad source";
134 if (S_ISDIR(st.st_mode)) {
135 const char *dir = source[i], *dest_dir = destination[i];
136 int first, last, len = strlen(dir);
138 if (lstat(dest_dir, &st) == 0) {
139 bad = "cannot move directory over file";
140 goto next;
143 modes[i] = WORKING_DIRECTORY;
145 first = cache_name_pos(source[i], len);
146 if (first >= 0)
147 die ("Huh? %s/ is in index?", dir);
149 first = -1 - first;
150 for (last = first; last < active_nr; last++) {
151 const char *path = active_cache[last]->name;
152 if (strncmp(path, dir, len) || path[len] != '/')
153 break;
156 if (last - first < 1)
157 bad = "source directory is empty";
158 else if (!bad) {
159 int j, dst_len = strlen(dest_dir);
161 if (last - first > 0) {
162 source = realloc(source,
163 (count + last - first)
164 * sizeof(char *));
165 destination = realloc(destination,
166 (count + last - first)
167 * sizeof(char *));
168 modes = realloc(modes,
169 (count + last - first)
170 * sizeof(enum update_mode));
173 dest_dir = add_slash(dest_dir);
175 for (j = 0; j < last - first; j++) {
176 const char *path =
177 active_cache[first + j]->name;
178 source[count + j] = path;
179 destination[count + j] =
180 prefix_path(dest_dir, dst_len,
181 path + len);
182 modes[count + j] = INDEX;
184 count += last - first;
187 goto next;
190 if (!bad && lstat(destination[i], &st) == 0) {
191 bad = "destination exists";
192 if (force) {
194 * only files can overwrite each other:
195 * check both source and destination
197 if (S_ISREG(st.st_mode)) {
198 fprintf(stderr, "Warning: %s;"
199 " will overwrite!\n",
200 bad);
201 bad = NULL;
202 path_list_insert(destination[i],
203 &overwritten);
204 } else
205 bad = "Cannot overwrite";
209 if (!bad &&
210 !strncmp(destination[i], source[i], strlen(source[i])))
211 bad = "can not move directory into itself";
213 if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
214 bad = "not under version control";
216 if (!bad) {
217 if (path_list_has_path(&src_for_dst, destination[i]))
218 bad = "multiple sources for the same target";
219 else
220 path_list_insert(destination[i], &src_for_dst);
223 next:
224 if (bad) {
225 if (ignore_errors) {
226 if (--count > 0) {
227 memmove(source + i, source + i + 1,
228 (count - i) * sizeof(char *));
229 memmove(destination + i,
230 destination + i + 1,
231 (count - i) * sizeof(char *));
233 } else
234 die ("%s, source=%s, destination=%s",
235 bad, source[i], destination[i]);
239 for (i = 0; i < count; i++) {
240 if (show_only || verbose)
241 printf("Renaming %s to %s\n",
242 source[i], destination[i]);
243 if (!show_only && modes[i] != INDEX &&
244 rename(source[i], destination[i]) < 0 &&
245 !ignore_errors)
246 die ("renaming %s failed: %s",
247 source[i], strerror(errno));
249 if (modes[i] == WORKING_DIRECTORY)
250 continue;
252 if (cache_name_pos(source[i], strlen(source[i])) >= 0) {
253 path_list_insert(source[i], &deleted);
255 /* destination can be a directory with 1 file inside */
256 if (path_list_has_path(&overwritten, destination[i]))
257 path_list_insert(destination[i], &changed);
258 else
259 path_list_insert(destination[i], &added);
260 } else
261 path_list_insert(destination[i], &added);
264 if (show_only) {
265 show_list("Changed : ", &changed);
266 show_list("Adding : ", &added);
267 show_list("Deleting : ", &deleted);
268 } else {
269 for (i = 0; i < changed.nr; i++) {
270 const char *path = changed.items[i].path;
271 int i = cache_name_pos(path, strlen(path));
272 struct cache_entry *ce = active_cache[i];
274 if (i < 0)
275 die ("Huh? Cache entry for %s unknown?", path);
276 refresh_cache_entry(ce, 0);
279 for (i = 0; i < added.nr; i++) {
280 const char *path = added.items[i].path;
281 add_file_to_index(path, verbose);
284 for (i = 0; i < deleted.nr; i++) {
285 const char *path = deleted.items[i].path;
286 remove_file_from_cache(path);
289 if (active_cache_changed) {
290 if (write_cache(newfd, active_cache, active_nr) ||
291 close(newfd) ||
292 commit_lock_file(&lock_file))
293 die("Unable to write new index file");
297 return 0;