Code

Merge branch 'jc/grep'
[git.git] / builtin-mv.c
1 /*
2  * "git mv" builtin command
3  *
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)
19 {
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;
29                 }
30         }
31         return get_pathspec(prefix, result);
32 }
34 static void show_list(const char *label, struct path_list *list)
35 {
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');
42         }
43 }
45 static const char *add_slash(const char *path)
46 {
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;
54         }
55         return path;
56 }
58 static struct lock_file lock_file;
60 int cmd_mv(int argc, const char **argv, const char *prefix)
61 {
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(), 1);
76         if (read_cache() < 0)
77                 die("index file corrupt");
79         for (i = 1; i < argc; i++) {
80                 const char *arg = argv[i];
82                 if (arg[0] != '-')
83                         break;
84                 if (!strcmp(arg, "--")) {
85                         i++;
86                         break;
87                 }
88                 if (!strcmp(arg, "-n")) {
89                         show_only = 1;
90                         continue;
91                 }
92                 if (!strcmp(arg, "-f")) {
93                         force = 1;
94                         continue;
95                 }
96                 if (!strcmp(arg, "-k")) {
97                         ignore_errors = 1;
98                         continue;
99                 }
100                 usage(builtin_mv_usage);
101         }
102         count = argc - i - 1;
103         if (count < 1)
104                 usage(builtin_mv_usage);
106         source = copy_pathspec(prefix, argv + i, count, 0);
107         modes = xcalloc(count, sizeof(enum update_mode));
108         dest_path = copy_pathspec(prefix, argv + argc - 1, 1, 0);
110         if (!lstat(dest_path[0], &st) &&
111                         S_ISDIR(st.st_mode)) {
112                 dest_path[0] = add_slash(dest_path[0]);
113                 destination = copy_pathspec(dest_path[0], argv + i, count, 1);
114         } else {
115                 if (count != 1)
116                         usage(builtin_mv_usage);
117                 destination = dest_path;
118         }
120         /* Checking */
121         for (i = 0; i < count; i++) {
122                 const char *bad = NULL;
124                 if (show_only)
125                         printf("Checking rename of '%s' to '%s'\n",
126                                 source[i], destination[i]);
128                 if (lstat(source[i], &st) < 0)
129                         bad = "bad source";
131                 if (S_ISDIR(st.st_mode)) {
132                         const char *dir = source[i], *dest_dir = destination[i];
133                         int first, last, len = strlen(dir);
135                         if (lstat(dest_dir, &st) == 0) {
136                                 bad = "cannot move directory over file";
137                                 goto next;
138                         }
140                         modes[i] = WORKING_DIRECTORY;
142                         first = cache_name_pos(source[i], len);
143                         if (first >= 0)
144                                 die ("Huh? %s/ is in index?", dir);
146                         first = -1 - first;
147                         for (last = first; last < active_nr; last++) {
148                                 const char *path = active_cache[last]->name;
149                                 if (strncmp(path, dir, len) || path[len] != '/')
150                                         break;
151                         }
153                         if (last - first < 1)
154                                 bad = "source directory is empty";
155                         else if (!bad) {
156                                 int j, dst_len = strlen(dest_dir);
158                                 if (last - first > 0) {
159                                         source = realloc(source,
160                                                         (count + last - first)
161                                                         * sizeof(char *));
162                                         destination = realloc(destination,
163                                                         (count + last - first)
164                                                         * sizeof(char *));
165                                         modes = realloc(modes,
166                                                         (count + last - first)
167                                                         * sizeof(enum update_mode));
168                                 }
170                                 dest_dir = add_slash(dest_dir);
172                                 for (j = 0; j < last - first; j++) {
173                                         const char *path =
174                                                 active_cache[first + j]->name;
175                                         source[count + j] = path;
176                                         destination[count + j] =
177                                                 prefix_path(dest_dir, dst_len,
178                                                         path + len);
179                                         modes[count + j] = INDEX;
180                                 }
181                                 count += last - first;
182                         }
184                         goto next;
185                 }
187                 if (!bad && lstat(destination[i], &st) == 0) {
188                         bad = "destination exists";
189                         if (force) {
190                                 /*
191                                  * only files can overwrite each other:
192                                  * check both source and destination
193                                  */
194                                 if (S_ISREG(st.st_mode)) {
195                                         fprintf(stderr, "Warning: %s;"
196                                                         " will overwrite!\n",
197                                                         bad);
198                                         bad = NULL;
199                                         path_list_insert(destination[i],
200                                                         &overwritten);
201                                 } else
202                                         bad = "Cannot overwrite";
203                         }
204                 }
206                 if (!bad &&
207                     !strncmp(destination[i], source[i], strlen(source[i])))
208                         bad = "can not move directory into itself";
210                 if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
211                         bad = "not under version control";
213                 if (!bad) {
214                         if (path_list_has_path(&src_for_dst, destination[i]))
215                                 bad = "multiple sources for the same target";
216                         else
217                                 path_list_insert(destination[i], &src_for_dst);
218                 }
220 next:
221                 if (bad) {
222                         if (ignore_errors) {
223                                 if (--count > 0) {
224                                         memmove(source + i, source + i + 1,
225                                                 (count - i) * sizeof(char *));
226                                         memmove(destination + i,
227                                                 destination + i + 1,
228                                                 (count - i) * sizeof(char *));
229                                 }
230                         } else
231                                 die ("%s, source=%s, destination=%s",
232                                      bad, source[i], destination[i]);
233                 }
234         }
236         for (i = 0; i < count; i++) {
237                 if (show_only || verbose)
238                         printf("Renaming %s to %s\n",
239                                source[i], destination[i]);
240                 if (!show_only && modes[i] != INDEX &&
241                     rename(source[i], destination[i]) < 0 &&
242                     !ignore_errors)
243                         die ("renaming %s failed: %s",
244                              source[i], strerror(errno));
246                 if (modes[i] == WORKING_DIRECTORY)
247                         continue;
249                 if (cache_name_pos(source[i], strlen(source[i])) >= 0) {
250                         path_list_insert(source[i], &deleted);
252                         /* destination can be a directory with 1 file inside */
253                         if (path_list_has_path(&overwritten, destination[i]))
254                                 path_list_insert(destination[i], &changed);
255                         else
256                                 path_list_insert(destination[i], &added);
257                 } else
258                         path_list_insert(destination[i], &added);
259         }
261         if (show_only) {
262                 show_list("Changed  : ", &changed);
263                 show_list("Adding   : ", &added);
264                 show_list("Deleting : ", &deleted);
265         } else {
266                 for (i = 0; i < changed.nr; i++) {
267                         const char *path = changed.items[i].path;
268                         int i = cache_name_pos(path, strlen(path));
269                         struct cache_entry *ce = active_cache[i];
271                         if (i < 0)
272                                 die ("Huh? Cache entry for %s unknown?", path);
273                         refresh_cache_entry(ce, 0);
274                 }
276                 for (i = 0; i < added.nr; i++) {
277                         const char *path = added.items[i].path;
278                         add_file_to_index(path, verbose);
279                 }
281                 for (i = 0; i < deleted.nr; i++) {
282                         const char *path = deleted.items[i].path;
283                         remove_file_from_cache(path);
284                 }
286                 if (active_cache_changed) {
287                         if (write_cache(newfd, active_cache, active_nr) ||
288                             close(newfd) ||
289                             commit_lock_file(&lock_file))
290                                 die("Unable to write new index file");
291                 }
292         }
294         return 0;