Make git-mv a builtin
[git/jrn.git] / builtin-mv.c
blob593ff9e43435d80ba9dd4eff271b8abcb7e97f06
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 struct lock_file lock_file;
47 int cmd_mv(int argc, const char **argv, char **envp)
49 int i, newfd, count;
50 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
51 const char *prefix = setup_git_directory();
52 const char **source, **destination, **dest_path;
53 struct stat st;
54 struct path_list overwritten = {NULL, 0, 0, 0};
55 struct path_list src_for_dst = {NULL, 0, 0, 0};
56 struct path_list added = {NULL, 0, 0, 0};
57 struct path_list deleted = {NULL, 0, 0, 0};
58 struct path_list changed = {NULL, 0, 0, 0};
60 git_config(git_default_config);
62 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
63 if (newfd < 0)
64 die("unable to create new index file");
66 if (read_cache() < 0)
67 die("index file corrupt");
69 for (i = 1; i < argc; i++) {
70 const char *arg = argv[i];
72 if (arg[0] != '-')
73 break;
74 if (!strcmp(arg, "--")) {
75 i++;
76 break;
78 if (!strcmp(arg, "-n")) {
79 show_only = 1;
80 continue;
82 if (!strcmp(arg, "-f")) {
83 force = 1;
84 continue;
86 if (!strcmp(arg, "-k")) {
87 ignore_errors = 1;
88 continue;
90 die(builtin_mv_usage);
92 count = argc - i - 1;
93 if (count < 1)
94 usage(builtin_mv_usage);
96 source = copy_pathspec(prefix, argv + i, count, 0);
97 dest_path = copy_pathspec(prefix, argv + argc - 1, 1, 0);
99 if (!lstat(dest_path[0], &st) && S_ISDIR(st.st_mode))
100 destination = copy_pathspec(dest_path[0], argv + i, count, 1);
101 else {
102 if (count != 1)
103 usage(builtin_mv_usage);
104 destination = dest_path;
107 /* Checking */
108 for (i = 0; i < count; i++) {
109 const char *bad = NULL;
111 if (show_only)
112 printf("Checking rename of '%s' to '%s'\n",
113 source[i], destination[i]);
115 if (lstat(source[i], &st) < 0)
116 bad = "bad source";
117 else if (lstat(destination[i], &st) == 0) {
118 bad = "destination exists";
119 if (force) {
121 * only files can overwrite each other:
122 * check both source and destination
124 if (S_ISREG(st.st_mode)) {
125 fprintf(stderr, "Warning: %s;"
126 " will overwrite!\n",
127 bad);
128 bad = NULL;
129 path_list_insert(destination[i],
130 &overwritten);
131 } else
132 bad = "Cannot overwrite";
136 if (!bad &&
137 !strncmp(destination[i], source[i], strlen(source[i])))
138 bad = "can not move directory into itself";
140 if (!bad && cache_name_pos(source[i], strlen(source[i])) < 0)
141 bad = "not under version control";
143 if (!bad) {
144 if (path_list_has_path(&src_for_dst, destination[i]))
145 bad = "multiple sources for the same target";
146 else
147 path_list_insert(destination[i], &src_for_dst);
150 if (bad) {
151 if (ignore_errors) {
152 if (--count > 0) {
153 memmove(source + i, source + i + 1,
154 (count - i) * sizeof(char *));
155 memmove(destination + i,
156 destination + i + 1,
157 (count - i) * sizeof(char *));
159 } else
160 die ("Error: %s, source=%s, destination=%s",
161 bad, source[i], destination[i]);
165 for (i = 0; i < count; i++) {
166 if (show_only || verbose)
167 printf("Renaming %s to %s\n",
168 source[i], destination[i]);
169 if (!show_only &&
170 rename(source[i], destination[i]) < 0 &&
171 !ignore_errors)
172 die ("renaming %s failed: %s",
173 source[i], strerror(errno));
175 if (cache_name_pos(source[i], strlen(source[i])) >= 0) {
176 path_list_insert(source[i], &deleted);
178 /* destination can be a directory with 1 file inside */
179 if (path_list_has_path(&overwritten, destination[i]))
180 path_list_insert(destination[i], &changed);
181 else
182 path_list_insert(destination[i], &added);
183 } else
184 path_list_insert(destination[i], &added);
187 if (show_only) {
188 show_list("Changed : ", &changed);
189 show_list("Adding : ", &added);
190 show_list("Deleting : ", &deleted);
191 } else {
192 for (i = 0; i < changed.nr; i++) {
193 const char *path = changed.items[i].path;
194 int i = cache_name_pos(path, strlen(path));
195 struct cache_entry *ce = active_cache[i];
197 if (i < 0)
198 die ("Huh? Cache entry for %s unknown?", path);
199 refresh_cache_entry(ce, 0);
202 for (i = 0; i < added.nr; i++) {
203 const char *path = added.items[i].path;
204 add_file_to_index(path, verbose);
207 for (i = 0; i < deleted.nr; i++) {
208 const char *path = deleted.items[i].path;
209 remove_file_from_cache(path);
212 if (active_cache_changed) {
213 if (write_cache(newfd, active_cache, active_nr) ||
214 close(newfd) ||
215 commit_lock_file(&lock_file))
216 die("Unable to write new index file");
220 return 0;