Provide git_config with a callback-data parameter
[git/git.git] / builtin-mv.c
CommitLineData
11be42a4
JS
1/*
2 * "git mv" builtin command
3 *
4 * Copyright (C) 2006 Johannes Schindelin
5 */
11be42a4
JS
6#include "cache.h"
7#include "builtin.h"
8#include "dir.h"
9#include "cache-tree.h"
10#include "path-list.h"
c7a20c11 11#include "parse-options.h"
11be42a4 12
c7a20c11
PH
13static const char * const builtin_mv_usage[] = {
14 "git-mv [options] <source>... <destination>",
15 NULL
16};
11be42a4
JS
17
18static const char **copy_pathspec(const char *prefix, const char **pathspec,
19 int count, int base_name)
20{
d78b0f3d 21 int i;
11be42a4
JS
22 const char **result = xmalloc((count + 1) * sizeof(const char *));
23 memcpy(result, pathspec, count * sizeof(const char *));
24 result[count] = NULL;
d78b0f3d
JS
25 for (i = 0; i < count; i++) {
26 int length = strlen(result[i]);
27 if (length > 0 && result[i][length - 1] == '/') {
182af834 28 result[i] = xmemdupz(result[i], length - 1);
d78b0f3d
JS
29 }
30 if (base_name) {
11be42a4
JS
31 const char *last_slash = strrchr(result[i], '/');
32 if (last_slash)
33 result[i] = last_slash + 1;
34 }
35 }
971dfa19 36 return get_pathspec(prefix, result);
11be42a4
JS
37}
38
39static void show_list(const char *label, struct path_list *list)
40{
41 if (list->nr > 0) {
42 int i;
43 printf("%s", label);
44 for (i = 0; i < list->nr; i++)
45 printf("%s%s", i > 0 ? ", " : "", list->items[i].path);
46 putchar('\n');
47 }
48}
49
ac64a722
JS
50static const char *add_slash(const char *path)
51{
52 int len = strlen(path);
53 if (path[len - 1] != '/') {
54 char *with_slash = xmalloc(len + 2);
55 memcpy(with_slash, path, len);
329a3047
JH
56 with_slash[len++] = '/';
57 with_slash[len] = 0;
ac64a722
JS
58 return with_slash;
59 }
60 return path;
61}
62
11be42a4
JS
63static struct lock_file lock_file;
64
7061cf0f 65int cmd_mv(int argc, const char **argv, const char *prefix)
11be42a4 66{
c7a20c11 67 int i, newfd;
11be42a4 68 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
c7a20c11
PH
69 struct option builtin_mv_options[] = {
70 OPT__DRY_RUN(&show_only),
71 OPT_BOOLEAN('f', NULL, &force, "force move/rename even if target exists"),
72 OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
73 OPT_END(),
74 };
11be42a4 75 const char **source, **destination, **dest_path;
ac64a722 76 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
11be42a4
JS
77 struct stat st;
78 struct path_list overwritten = {NULL, 0, 0, 0};
79 struct path_list src_for_dst = {NULL, 0, 0, 0};
80 struct path_list added = {NULL, 0, 0, 0};
81 struct path_list deleted = {NULL, 0, 0, 0};
82 struct path_list changed = {NULL, 0, 0, 0};
83
ef90d6d4 84 git_config(git_default_config, NULL);
11be42a4 85
30ca07a2 86 newfd = hold_locked_index(&lock_file, 1);
11be42a4
JS
87 if (read_cache() < 0)
88 die("index file corrupt");
89
c7a20c11
PH
90 argc = parse_options(argc, argv, builtin_mv_options, builtin_mv_usage, 0);
91 if (--argc < 1)
92 usage_with_options(builtin_mv_usage, builtin_mv_options);
11be42a4 93
c7a20c11
PH
94 source = copy_pathspec(prefix, argv, argc, 0);
95 modes = xcalloc(argc, sizeof(enum update_mode));
96 dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
11be42a4 97
c5203bdf
JS
98 if (dest_path[0][0] == '\0')
99 /* special case: "." was normalized to "" */
c7a20c11 100 destination = copy_pathspec(dest_path[0], argv, argc, 1);
c5203bdf 101 else if (!lstat(dest_path[0], &st) &&
ac64a722
JS
102 S_ISDIR(st.st_mode)) {
103 dest_path[0] = add_slash(dest_path[0]);
c7a20c11 104 destination = copy_pathspec(dest_path[0], argv, argc, 1);
ac64a722 105 } else {
c7a20c11
PH
106 if (argc != 1)
107 usage_with_options(builtin_mv_usage, builtin_mv_options);
11be42a4
JS
108 destination = dest_path;
109 }
110
111 /* Checking */
c7a20c11 112 for (i = 0; i < argc; i++) {
60a6bf5f
JS
113 const char *src = source[i], *dst = destination[i];
114 int length, src_is_dir;
11be42a4
JS
115 const char *bad = NULL;
116
117 if (show_only)
60a6bf5f 118 printf("Checking rename of '%s' to '%s'\n", src, dst);
11be42a4 119
60a6bf5f
JS
120 length = strlen(src);
121 if (lstat(src, &st) < 0)
11be42a4 122 bad = "bad source";
60a6bf5f
JS
123 else if (!strncmp(src, dst, length) &&
124 (dst[length] == 0 || dst[length] == '/')) {
d78b0f3d 125 bad = "can not move directory into itself";
60a6bf5f
JS
126 } else if ((src_is_dir = S_ISDIR(st.st_mode))
127 && lstat(dst, &st) == 0)
128 bad = "cannot move directory over file";
129 else if (src_is_dir) {
aca085e5
JS
130 const char *src_w_slash = add_slash(src);
131 int len_w_slash = length + 1;
60a6bf5f 132 int first, last;
ac64a722
JS
133
134 modes[i] = WORKING_DIRECTORY;
135
aca085e5 136 first = cache_name_pos(src_w_slash, len_w_slash);
ac64a722 137 if (first >= 0)
aca085e5
JS
138 die ("Huh? %.*s is in index?",
139 len_w_slash, src_w_slash);
ac64a722
JS
140
141 first = -1 - first;
142 for (last = first; last < active_nr; last++) {
143 const char *path = active_cache[last]->name;
aca085e5 144 if (strncmp(path, src_w_slash, len_w_slash))
ac64a722
JS
145 break;
146 }
aca085e5 147 free((char *)src_w_slash);
ac64a722
JS
148
149 if (last - first < 1)
150 bad = "source directory is empty";
60a6bf5f
JS
151 else {
152 int j, dst_len;
ac64a722
JS
153
154 if (last - first > 0) {
83572c1a 155 source = xrealloc(source,
c7a20c11 156 (argc + last - first)
ac64a722 157 * sizeof(char *));
83572c1a 158 destination = xrealloc(destination,
c7a20c11 159 (argc + last - first)
ac64a722 160 * sizeof(char *));
83572c1a 161 modes = xrealloc(modes,
c7a20c11 162 (argc + last - first)
ac64a722
JS
163 * sizeof(enum update_mode));
164 }
165
60a6bf5f 166 dst = add_slash(dst);
d089ebaa 167 dst_len = strlen(dst);
ac64a722
JS
168
169 for (j = 0; j < last - first; j++) {
170 const char *path =
171 active_cache[first + j]->name;
c7a20c11
PH
172 source[argc + j] = path;
173 destination[argc + j] =
60a6bf5f 174 prefix_path(dst, dst_len,
d089ebaa 175 path + length + 1);
c7a20c11 176 modes[argc + j] = INDEX;
ac64a722 177 }
c7a20c11 178 argc += last - first;
ac64a722 179 }
60a6bf5f 180 } else if (lstat(dst, &st) == 0) {
11be42a4
JS
181 bad = "destination exists";
182 if (force) {
183 /*
184 * only files can overwrite each other:
185 * check both source and destination
186 */
187 if (S_ISREG(st.st_mode)) {
188 fprintf(stderr, "Warning: %s;"
189 " will overwrite!\n",
190 bad);
191 bad = NULL;
60a6bf5f 192 path_list_insert(dst, &overwritten);
11be42a4
JS
193 } else
194 bad = "Cannot overwrite";
195 }
60a6bf5f 196 } else if (cache_name_pos(src, length) < 0)
11be42a4 197 bad = "not under version control";
60a6bf5f
JS
198 else if (path_list_has_path(&src_for_dst, dst))
199 bad = "multiple sources for the same target";
200 else
201 path_list_insert(dst, &src_for_dst);
11be42a4 202
11be42a4
JS
203 if (bad) {
204 if (ignore_errors) {
c7a20c11 205 if (--argc > 0) {
11be42a4 206 memmove(source + i, source + i + 1,
c7a20c11 207 (argc - i) * sizeof(char *));
11be42a4
JS
208 memmove(destination + i,
209 destination + i + 1,
c7a20c11 210 (argc - i) * sizeof(char *));
11be42a4
JS
211 }
212 } else
ac64a722 213 die ("%s, source=%s, destination=%s",
60a6bf5f 214 bad, src, dst);
11be42a4
JS
215 }
216 }
217
c7a20c11 218 for (i = 0; i < argc; i++) {
60a6bf5f
JS
219 const char *src = source[i], *dst = destination[i];
220 enum update_mode mode = modes[i];
11be42a4 221 if (show_only || verbose)
60a6bf5f
JS
222 printf("Renaming %s to %s\n", src, dst);
223 if (!show_only && mode != INDEX &&
224 rename(src, dst) < 0 && !ignore_errors)
225 die ("renaming %s failed: %s", src, strerror(errno));
226
227 if (mode == WORKING_DIRECTORY)
ac64a722
JS
228 continue;
229
60a6bf5f
JS
230 if (cache_name_pos(src, strlen(src)) >= 0) {
231 path_list_insert(src, &deleted);
11be42a4
JS
232
233 /* destination can be a directory with 1 file inside */
60a6bf5f
JS
234 if (path_list_has_path(&overwritten, dst))
235 path_list_insert(dst, &changed);
11be42a4 236 else
60a6bf5f 237 path_list_insert(dst, &added);
11be42a4 238 } else
60a6bf5f 239 path_list_insert(dst, &added);
11be42a4
JS
240 }
241
c7a20c11 242 if (show_only) {
11be42a4
JS
243 show_list("Changed : ", &changed);
244 show_list("Adding : ", &added);
245 show_list("Deleting : ", &deleted);
246 } else {
247 for (i = 0; i < changed.nr; i++) {
248 const char *path = changed.items[i].path;
d828f6dd
PH
249 int j = cache_name_pos(path, strlen(path));
250 struct cache_entry *ce = active_cache[j];
11be42a4 251
d828f6dd 252 if (j < 0)
11be42a4
JS
253 die ("Huh? Cache entry for %s unknown?", path);
254 refresh_cache_entry(ce, 0);
255 }
256
257 for (i = 0; i < added.nr; i++) {
258 const char *path = added.items[i].path;
fd1c3bf0 259 add_file_to_cache(path, verbose);
11be42a4
JS
260 }
261
09d5dc32
JH
262 for (i = 0; i < deleted.nr; i++)
263 remove_file_from_cache(deleted.items[i].path);
11be42a4
JS
264
265 if (active_cache_changed) {
266 if (write_cache(newfd, active_cache, active_nr) ||
30ca07a2 267 commit_locked_index(&lock_file))
11be42a4
JS
268 die("Unable to write new index file");
269 }
270 }
271
272 return 0;
273}