use COPY_ARRAY
[git/git.git] / builtin / mv.c
1 /*
2 * "git mv" builtin command
3 *
4 * Copyright (C) 2006 Johannes Schindelin
5 */
6 #include "builtin.h"
7 #include "lockfile.h"
8 #include "dir.h"
9 #include "cache-tree.h"
10 #include "string-list.h"
11 #include "parse-options.h"
12 #include "submodule.h"
13
14 static const char * const builtin_mv_usage[] = {
15 N_("git mv [<options>] <source>... <destination>"),
16 NULL
17 };
18
19 #define DUP_BASENAME 1
20 #define KEEP_TRAILING_SLASH 2
21
22 static const char **internal_copy_pathspec(const char *prefix,
23 const char **pathspec,
24 int count, unsigned flags)
25 {
26 int i;
27 const char **result;
28 ALLOC_ARRAY(result, count + 1);
29 COPY_ARRAY(result, pathspec, count);
30 result[count] = NULL;
31 for (i = 0; i < count; i++) {
32 int length = strlen(result[i]);
33 int to_copy = length;
34 while (!(flags & KEEP_TRAILING_SLASH) &&
35 to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
36 to_copy--;
37 if (to_copy != length || flags & DUP_BASENAME) {
38 char *it = xmemdupz(result[i], to_copy);
39 if (flags & DUP_BASENAME) {
40 result[i] = xstrdup(basename(it));
41 free(it);
42 } else
43 result[i] = it;
44 }
45 }
46 return get_pathspec(prefix, result);
47 }
48
49 static const char *add_slash(const char *path)
50 {
51 size_t len = strlen(path);
52 if (path[len - 1] != '/') {
53 char *with_slash = xmalloc(st_add(len, 2));
54 memcpy(with_slash, path, len);
55 with_slash[len++] = '/';
56 with_slash[len] = 0;
57 return with_slash;
58 }
59 return path;
60 }
61
62 static struct lock_file lock_file;
63 #define SUBMODULE_WITH_GITDIR ((const char *)1)
64
65 static void prepare_move_submodule(const char *src, int first,
66 const char **submodule_gitfile)
67 {
68 struct strbuf submodule_dotgit = STRBUF_INIT;
69 if (!S_ISGITLINK(active_cache[first]->ce_mode))
70 die(_("Directory %s is in index and no submodule?"), src);
71 if (!is_staging_gitmodules_ok())
72 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
73 strbuf_addf(&submodule_dotgit, "%s/.git", src);
74 *submodule_gitfile = read_gitfile(submodule_dotgit.buf);
75 if (*submodule_gitfile)
76 *submodule_gitfile = xstrdup(*submodule_gitfile);
77 else
78 *submodule_gitfile = SUBMODULE_WITH_GITDIR;
79 strbuf_release(&submodule_dotgit);
80 }
81
82 static int index_range_of_same_dir(const char *src, int length,
83 int *first_p, int *last_p)
84 {
85 const char *src_w_slash = add_slash(src);
86 int first, last, len_w_slash = length + 1;
87
88 first = cache_name_pos(src_w_slash, len_w_slash);
89 if (first >= 0)
90 die(_("%.*s is in index"), len_w_slash, src_w_slash);
91
92 first = -1 - first;
93 for (last = first; last < active_nr; last++) {
94 const char *path = active_cache[last]->name;
95 if (strncmp(path, src_w_slash, len_w_slash))
96 break;
97 }
98 if (src_w_slash != src)
99 free((char *)src_w_slash);
100 *first_p = first;
101 *last_p = last;
102 return last - first;
103 }
104
105 int cmd_mv(int argc, const char **argv, const char *prefix)
106 {
107 int i, gitmodules_modified = 0;
108 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
109 struct option builtin_mv_options[] = {
110 OPT__VERBOSE(&verbose, N_("be verbose")),
111 OPT__DRY_RUN(&show_only, N_("dry run")),
112 OPT__FORCE(&force, N_("force move/rename even if target exists")),
113 OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
114 OPT_END(),
115 };
116 const char **source, **destination, **dest_path, **submodule_gitfile;
117 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
118 struct stat st;
119 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
120
121 gitmodules_config();
122 git_config(git_default_config, NULL);
123
124 argc = parse_options(argc, argv, prefix, builtin_mv_options,
125 builtin_mv_usage, 0);
126 if (--argc < 1)
127 usage_with_options(builtin_mv_usage, builtin_mv_options);
128
129 hold_locked_index(&lock_file, 1);
130 if (read_cache() < 0)
131 die(_("index file corrupt"));
132
133 source = internal_copy_pathspec(prefix, argv, argc, 0);
134 modes = xcalloc(argc, sizeof(enum update_mode));
135 /*
136 * Keep trailing slash, needed to let
137 * "git mv file no-such-dir/" error out.
138 */
139 dest_path = internal_copy_pathspec(prefix, argv + argc, 1,
140 KEEP_TRAILING_SLASH);
141 submodule_gitfile = xcalloc(argc, sizeof(char *));
142
143 if (dest_path[0][0] == '\0')
144 /* special case: "." was normalized to "" */
145 destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
146 else if (!lstat(dest_path[0], &st) &&
147 S_ISDIR(st.st_mode)) {
148 dest_path[0] = add_slash(dest_path[0]);
149 destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
150 } else {
151 if (argc != 1)
152 die(_("destination '%s' is not a directory"), dest_path[0]);
153 destination = dest_path;
154 }
155
156 /* Checking */
157 for (i = 0; i < argc; i++) {
158 const char *src = source[i], *dst = destination[i];
159 int length, src_is_dir;
160 const char *bad = NULL;
161
162 if (show_only)
163 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
164
165 length = strlen(src);
166 if (lstat(src, &st) < 0)
167 bad = _("bad source");
168 else if (!strncmp(src, dst, length) &&
169 (dst[length] == 0 || dst[length] == '/')) {
170 bad = _("can not move directory into itself");
171 } else if ((src_is_dir = S_ISDIR(st.st_mode))
172 && lstat(dst, &st) == 0)
173 bad = _("cannot move directory over file");
174 else if (src_is_dir) {
175 int first = cache_name_pos(src, length), last;
176
177 if (first >= 0)
178 prepare_move_submodule(src, first,
179 submodule_gitfile + i);
180 else if (index_range_of_same_dir(src, length,
181 &first, &last) < 1)
182 bad = _("source directory is empty");
183 else { /* last - first >= 1 */
184 int j, dst_len, n;
185
186 modes[i] = WORKING_DIRECTORY;
187 n = argc + last - first;
188 REALLOC_ARRAY(source, n);
189 REALLOC_ARRAY(destination, n);
190 REALLOC_ARRAY(modes, n);
191 REALLOC_ARRAY(submodule_gitfile, n);
192
193 dst = add_slash(dst);
194 dst_len = strlen(dst);
195
196 for (j = 0; j < last - first; j++) {
197 const char *path = active_cache[first + j]->name;
198 source[argc + j] = path;
199 destination[argc + j] =
200 prefix_path(dst, dst_len, path + length + 1);
201 modes[argc + j] = INDEX;
202 submodule_gitfile[argc + j] = NULL;
203 }
204 argc += last - first;
205 }
206 } else if (cache_name_pos(src, length) < 0)
207 bad = _("not under version control");
208 else if (lstat(dst, &st) == 0 &&
209 (!ignore_case || strcasecmp(src, dst))) {
210 bad = _("destination exists");
211 if (force) {
212 /*
213 * only files can overwrite each other:
214 * check both source and destination
215 */
216 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
217 if (verbose)
218 warning(_("overwriting '%s'"), dst);
219 bad = NULL;
220 } else
221 bad = _("Cannot overwrite");
222 }
223 } else if (string_list_has_string(&src_for_dst, dst))
224 bad = _("multiple sources for the same target");
225 else if (is_dir_sep(dst[strlen(dst) - 1]))
226 bad = _("destination directory does not exist");
227 else
228 string_list_insert(&src_for_dst, dst);
229
230 if (!bad)
231 continue;
232 if (!ignore_errors)
233 die(_("%s, source=%s, destination=%s"),
234 bad, src, dst);
235 if (--argc > 0) {
236 int n = argc - i;
237 memmove(source + i, source + i + 1,
238 n * sizeof(char *));
239 memmove(destination + i, destination + i + 1,
240 n * sizeof(char *));
241 memmove(modes + i, modes + i + 1,
242 n * sizeof(enum update_mode));
243 memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
244 n * sizeof(char *));
245 i--;
246 }
247 }
248
249 for (i = 0; i < argc; i++) {
250 const char *src = source[i], *dst = destination[i];
251 enum update_mode mode = modes[i];
252 int pos;
253 if (show_only || verbose)
254 printf(_("Renaming %s to %s\n"), src, dst);
255 if (show_only)
256 continue;
257 if (mode != INDEX && rename(src, dst) < 0) {
258 if (ignore_errors)
259 continue;
260 die_errno(_("renaming '%s' failed"), src);
261 }
262 if (submodule_gitfile[i]) {
263 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
264 connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
265 if (!update_path_in_gitmodules(src, dst))
266 gitmodules_modified = 1;
267 }
268
269 if (mode == WORKING_DIRECTORY)
270 continue;
271
272 pos = cache_name_pos(src, strlen(src));
273 assert(pos >= 0);
274 if (!show_only)
275 rename_cache_entry_at(pos, dst);
276 }
277
278 if (gitmodules_modified)
279 stage_updated_gitmodules();
280
281 if (active_cache_changed &&
282 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
283 die(_("Unable to write new index file"));
284
285 return 0;
286 }