Merge branch 'ks/perf-build-with-autoconf' into maint
[alt-git.git] / builtin / mv.c
blob446a316738e0e739b6a89992879c6e4838f23c8f
1 /*
2 * "git mv" builtin command
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"
14 static const char * const builtin_mv_usage[] = {
15 N_("git mv [<options>] <source>... <destination>"),
16 NULL
19 #define DUP_BASENAME 1
20 #define KEEP_TRAILING_SLASH 2
22 static const char **internal_copy_pathspec(const char *prefix,
23 const char **pathspec,
24 int count, unsigned flags)
26 int i;
27 const char **result;
28 ALLOC_ARRAY(result, count + 1);
29 memcpy(result, pathspec, count * sizeof(const char *));
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;
46 return get_pathspec(prefix, result);
49 static const char *add_slash(const char *path)
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;
59 return path;
62 static struct lock_file lock_file;
63 #define SUBMODULE_WITH_GITDIR ((const char *)1)
65 static void prepare_move_submodule(const char *src, int first,
66 const char **submodule_gitfile)
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);
82 static int index_range_of_same_dir(const char *src, int length,
83 int *first_p, int *last_p)
85 const char *src_w_slash = add_slash(src);
86 int first, last, len_w_slash = length + 1;
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);
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;
98 if (src_w_slash != src)
99 free((char *)src_w_slash);
100 *first_p = first;
101 *last_p = last;
102 return last - first;
105 int cmd_mv(int argc, const char **argv, const char *prefix)
107 int i, flags, 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(),
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;
121 gitmodules_config();
122 git_config(git_default_config, NULL);
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);
129 hold_locked_index(&lock_file, 1);
130 if (read_cache() < 0)
131 die(_("index file corrupt"));
133 source = internal_copy_pathspec(prefix, argv, argc, 0);
134 modes = xcalloc(argc, sizeof(enum update_mode));
136 * Keep trailing slash, needed to let
137 * "git mv file no-such-dir/" error out, except in the case
138 * "git mv directory no-such-dir/".
140 flags = KEEP_TRAILING_SLASH;
141 if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
142 flags = 0;
143 dest_path = internal_copy_pathspec(prefix, argv + argc, 1, flags);
144 submodule_gitfile = xcalloc(argc, sizeof(char *));
146 if (dest_path[0][0] == '\0')
147 /* special case: "." was normalized to "" */
148 destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
149 else if (!lstat(dest_path[0], &st) &&
150 S_ISDIR(st.st_mode)) {
151 dest_path[0] = add_slash(dest_path[0]);
152 destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
153 } else {
154 if (argc != 1)
155 die(_("destination '%s' is not a directory"), dest_path[0]);
156 destination = dest_path;
159 /* Checking */
160 for (i = 0; i < argc; i++) {
161 const char *src = source[i], *dst = destination[i];
162 int length, src_is_dir;
163 const char *bad = NULL;
165 if (show_only)
166 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
168 length = strlen(src);
169 if (lstat(src, &st) < 0)
170 bad = _("bad source");
171 else if (!strncmp(src, dst, length) &&
172 (dst[length] == 0 || dst[length] == '/')) {
173 bad = _("can not move directory into itself");
174 } else if ((src_is_dir = S_ISDIR(st.st_mode))
175 && lstat(dst, &st) == 0)
176 bad = _("cannot move directory over file");
177 else if (src_is_dir) {
178 int first = cache_name_pos(src, length), last;
180 if (first >= 0)
181 prepare_move_submodule(src, first,
182 submodule_gitfile + i);
183 else if (index_range_of_same_dir(src, length,
184 &first, &last) < 1)
185 bad = _("source directory is empty");
186 else { /* last - first >= 1 */
187 int j, dst_len, n;
189 modes[i] = WORKING_DIRECTORY;
190 n = argc + last - first;
191 REALLOC_ARRAY(source, n);
192 REALLOC_ARRAY(destination, n);
193 REALLOC_ARRAY(modes, n);
194 REALLOC_ARRAY(submodule_gitfile, n);
196 dst = add_slash(dst);
197 dst_len = strlen(dst);
199 for (j = 0; j < last - first; j++) {
200 const char *path = active_cache[first + j]->name;
201 source[argc + j] = path;
202 destination[argc + j] =
203 prefix_path(dst, dst_len, path + length + 1);
204 modes[argc + j] = INDEX;
205 submodule_gitfile[argc + j] = NULL;
207 argc += last - first;
209 } else if (cache_name_pos(src, length) < 0)
210 bad = _("not under version control");
211 else if (lstat(dst, &st) == 0 &&
212 (!ignore_case || strcasecmp(src, dst))) {
213 bad = _("destination exists");
214 if (force) {
216 * only files can overwrite each other:
217 * check both source and destination
219 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
220 if (verbose)
221 warning(_("overwriting '%s'"), dst);
222 bad = NULL;
223 } else
224 bad = _("Cannot overwrite");
226 } else if (string_list_has_string(&src_for_dst, dst))
227 bad = _("multiple sources for the same target");
228 else if (is_dir_sep(dst[strlen(dst) - 1]))
229 bad = _("destination directory does not exist");
230 else
231 string_list_insert(&src_for_dst, dst);
233 if (!bad)
234 continue;
235 if (!ignore_errors)
236 die(_("%s, source=%s, destination=%s"),
237 bad, src, dst);
238 if (--argc > 0) {
239 int n = argc - i;
240 memmove(source + i, source + i + 1,
241 n * sizeof(char *));
242 memmove(destination + i, destination + i + 1,
243 n * sizeof(char *));
244 memmove(modes + i, modes + i + 1,
245 n * sizeof(enum update_mode));
246 memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
247 n * sizeof(char *));
248 i--;
252 for (i = 0; i < argc; i++) {
253 const char *src = source[i], *dst = destination[i];
254 enum update_mode mode = modes[i];
255 int pos;
256 if (show_only || verbose)
257 printf(_("Renaming %s to %s\n"), src, dst);
258 if (show_only)
259 continue;
260 if (mode != INDEX && rename(src, dst) < 0) {
261 if (ignore_errors)
262 continue;
263 die_errno(_("renaming '%s' failed"), src);
265 if (submodule_gitfile[i]) {
266 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
267 connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
268 if (!update_path_in_gitmodules(src, dst))
269 gitmodules_modified = 1;
272 if (mode == WORKING_DIRECTORY)
273 continue;
275 pos = cache_name_pos(src, strlen(src));
276 assert(pos >= 0);
277 if (!show_only)
278 rename_cache_entry_at(pos, dst);
281 if (gitmodules_modified)
282 stage_updated_gitmodules();
284 if (active_cache_changed &&
285 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
286 die(_("Unable to write new index file"));
288 return 0;