Merge branch 'jc/doc-format-patch-clarify-auto-base'
[alt-git.git] / builtin / mv.c
blob83a465ba831adf94561a13f3d358926f516fe9ac
1 /*
2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
5 */
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
7 #include "builtin.h"
8 #include "config.h"
9 #include "pathspec.h"
10 #include "lockfile.h"
11 #include "dir.h"
12 #include "cache-tree.h"
13 #include "string-list.h"
14 #include "parse-options.h"
15 #include "submodule.h"
17 static const char * const builtin_mv_usage[] = {
18 N_("git mv [<options>] <source>... <destination>"),
19 NULL
22 #define DUP_BASENAME 1
23 #define KEEP_TRAILING_SLASH 2
25 static const char **internal_prefix_pathspec(const char *prefix,
26 const char **pathspec,
27 int count, unsigned flags)
29 int i;
30 const char **result;
31 int prefixlen = prefix ? strlen(prefix) : 0;
32 ALLOC_ARRAY(result, count + 1);
34 /* Create an intermediate copy of the pathspec based on the flags */
35 for (i = 0; i < count; i++) {
36 int length = strlen(pathspec[i]);
37 int to_copy = length;
38 char *it;
39 while (!(flags & KEEP_TRAILING_SLASH) &&
40 to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1]))
41 to_copy--;
43 it = xmemdupz(pathspec[i], to_copy);
44 if (flags & DUP_BASENAME) {
45 result[i] = xstrdup(basename(it));
46 free(it);
47 } else {
48 result[i] = it;
51 result[count] = NULL;
53 /* Prefix the pathspec and free the old intermediate strings */
54 for (i = 0; i < count; i++) {
55 const char *match = prefix_path(prefix, prefixlen, result[i]);
56 free((char *) result[i]);
57 result[i] = match;
60 return result;
63 static const char *add_slash(const char *path)
65 size_t len = strlen(path);
66 if (path[len - 1] != '/') {
67 char *with_slash = xmalloc(st_add(len, 2));
68 memcpy(with_slash, path, len);
69 with_slash[len++] = '/';
70 with_slash[len] = 0;
71 return with_slash;
73 return path;
76 #define SUBMODULE_WITH_GITDIR ((const char *)1)
78 static void prepare_move_submodule(const char *src, int first,
79 const char **submodule_gitfile)
81 struct strbuf submodule_dotgit = STRBUF_INIT;
82 if (!S_ISGITLINK(active_cache[first]->ce_mode))
83 die(_("Directory %s is in index and no submodule?"), src);
84 if (!is_staging_gitmodules_ok(&the_index))
85 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
86 strbuf_addf(&submodule_dotgit, "%s/.git", src);
87 *submodule_gitfile = read_gitfile(submodule_dotgit.buf);
88 if (*submodule_gitfile)
89 *submodule_gitfile = xstrdup(*submodule_gitfile);
90 else
91 *submodule_gitfile = SUBMODULE_WITH_GITDIR;
92 strbuf_release(&submodule_dotgit);
95 static int index_range_of_same_dir(const char *src, int length,
96 int *first_p, int *last_p)
98 const char *src_w_slash = add_slash(src);
99 int first, last, len_w_slash = length + 1;
101 first = cache_name_pos(src_w_slash, len_w_slash);
102 if (first >= 0)
103 die(_("%.*s is in index"), len_w_slash, src_w_slash);
105 first = -1 - first;
106 for (last = first; last < active_nr; last++) {
107 const char *path = active_cache[last]->name;
108 if (strncmp(path, src_w_slash, len_w_slash))
109 break;
111 if (src_w_slash != src)
112 free((char *)src_w_slash);
113 *first_p = first;
114 *last_p = last;
115 return last - first;
118 int cmd_mv(int argc, const char **argv, const char *prefix)
120 int i, flags, gitmodules_modified = 0;
121 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0, ignore_sparse = 0;
122 struct option builtin_mv_options[] = {
123 OPT__VERBOSE(&verbose, N_("be verbose")),
124 OPT__DRY_RUN(&show_only, N_("dry run")),
125 OPT__FORCE(&force, N_("force move/rename even if target exists"),
126 PARSE_OPT_NOCOMPLETE),
127 OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
128 OPT_BOOL(0, "sparse", &ignore_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
129 OPT_END(),
131 const char **source, **destination, **dest_path, **submodule_gitfile;
132 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX, SPARSE } *modes;
133 struct stat st;
134 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
135 struct lock_file lock_file = LOCK_INIT;
136 struct cache_entry *ce;
137 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
139 git_config(git_default_config, NULL);
141 argc = parse_options(argc, argv, prefix, builtin_mv_options,
142 builtin_mv_usage, 0);
143 if (--argc < 1)
144 usage_with_options(builtin_mv_usage, builtin_mv_options);
146 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
147 if (read_cache() < 0)
148 die(_("index file corrupt"));
150 source = internal_prefix_pathspec(prefix, argv, argc, 0);
151 modes = xcalloc(argc, sizeof(enum update_mode));
153 * Keep trailing slash, needed to let
154 * "git mv file no-such-dir/" error out, except in the case
155 * "git mv directory no-such-dir/".
157 flags = KEEP_TRAILING_SLASH;
158 if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
159 flags = 0;
160 dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags);
161 submodule_gitfile = xcalloc(argc, sizeof(char *));
163 if (dest_path[0][0] == '\0')
164 /* special case: "." was normalized to "" */
165 destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
166 else if (!lstat(dest_path[0], &st) &&
167 S_ISDIR(st.st_mode)) {
168 dest_path[0] = add_slash(dest_path[0]);
169 destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
170 } else {
171 if (argc != 1)
172 die(_("destination '%s' is not a directory"), dest_path[0]);
173 destination = dest_path;
176 /* Checking */
177 for (i = 0; i < argc; i++) {
178 const char *src = source[i], *dst = destination[i];
179 int length, src_is_dir;
180 const char *bad = NULL;
181 int skip_sparse = 0;
183 if (show_only)
184 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
186 length = strlen(src);
187 if (lstat(src, &st) < 0) {
188 /* only error if existence is expected. */
189 if (modes[i] != SPARSE)
190 bad = _("bad source");
191 } else if (!strncmp(src, dst, length) &&
192 (dst[length] == 0 || dst[length] == '/')) {
193 bad = _("can not move directory into itself");
194 } else if ((src_is_dir = S_ISDIR(st.st_mode))
195 && lstat(dst, &st) == 0)
196 bad = _("cannot move directory over file");
197 else if (src_is_dir) {
198 int first = cache_name_pos(src, length), last;
200 if (first >= 0)
201 prepare_move_submodule(src, first,
202 submodule_gitfile + i);
203 else if (index_range_of_same_dir(src, length,
204 &first, &last) < 1)
205 bad = _("source directory is empty");
206 else { /* last - first >= 1 */
207 int j, dst_len, n;
209 modes[i] = WORKING_DIRECTORY;
210 n = argc + last - first;
211 REALLOC_ARRAY(source, n);
212 REALLOC_ARRAY(destination, n);
213 REALLOC_ARRAY(modes, n);
214 REALLOC_ARRAY(submodule_gitfile, n);
216 dst = add_slash(dst);
217 dst_len = strlen(dst);
219 for (j = 0; j < last - first; j++) {
220 const struct cache_entry *ce = active_cache[first + j];
221 const char *path = ce->name;
222 source[argc + j] = path;
223 destination[argc + j] =
224 prefix_path(dst, dst_len, path + length + 1);
225 modes[argc + j] = ce_skip_worktree(ce) ? SPARSE : INDEX;
226 submodule_gitfile[argc + j] = NULL;
228 argc += last - first;
230 } else if (!(ce = cache_file_exists(src, length, 0))) {
231 bad = _("not under version control");
232 } else if (ce_stage(ce)) {
233 bad = _("conflicted");
234 } else if (lstat(dst, &st) == 0 &&
235 (!ignore_case || strcasecmp(src, dst))) {
236 bad = _("destination exists");
237 if (force) {
239 * only files can overwrite each other:
240 * check both source and destination
242 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
243 if (verbose)
244 warning(_("overwriting '%s'"), dst);
245 bad = NULL;
246 } else
247 bad = _("Cannot overwrite");
249 } else if (string_list_has_string(&src_for_dst, dst))
250 bad = _("multiple sources for the same target");
251 else if (is_dir_sep(dst[strlen(dst) - 1]))
252 bad = _("destination directory does not exist");
253 else {
255 * We check if the paths are in the sparse-checkout
256 * definition as a very final check, since that
257 * allows us to point the user to the --sparse
258 * option as a way to have a successful run.
260 if (!ignore_sparse &&
261 !path_in_sparse_checkout(src, &the_index)) {
262 string_list_append(&only_match_skip_worktree, src);
263 skip_sparse = 1;
265 if (!ignore_sparse &&
266 !path_in_sparse_checkout(dst, &the_index)) {
267 string_list_append(&only_match_skip_worktree, dst);
268 skip_sparse = 1;
271 if (skip_sparse)
272 goto remove_entry;
274 string_list_insert(&src_for_dst, dst);
277 if (!bad)
278 continue;
279 if (!ignore_errors)
280 die(_("%s, source=%s, destination=%s"),
281 bad, src, dst);
282 remove_entry:
283 if (--argc > 0) {
284 int n = argc - i;
285 memmove(source + i, source + i + 1,
286 n * sizeof(char *));
287 memmove(destination + i, destination + i + 1,
288 n * sizeof(char *));
289 memmove(modes + i, modes + i + 1,
290 n * sizeof(enum update_mode));
291 memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
292 n * sizeof(char *));
293 i--;
297 if (only_match_skip_worktree.nr) {
298 advise_on_updating_sparse_paths(&only_match_skip_worktree);
299 if (!ignore_errors)
300 return 1;
303 for (i = 0; i < argc; i++) {
304 const char *src = source[i], *dst = destination[i];
305 enum update_mode mode = modes[i];
306 int pos;
307 if (show_only || verbose)
308 printf(_("Renaming %s to %s\n"), src, dst);
309 if (show_only)
310 continue;
311 if (mode != INDEX && mode != SPARSE && rename(src, dst) < 0) {
312 if (ignore_errors)
313 continue;
314 die_errno(_("renaming '%s' failed"), src);
316 if (submodule_gitfile[i]) {
317 if (!update_path_in_gitmodules(src, dst))
318 gitmodules_modified = 1;
319 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
320 connect_work_tree_and_git_dir(dst,
321 submodule_gitfile[i],
325 if (mode == WORKING_DIRECTORY)
326 continue;
328 pos = cache_name_pos(src, strlen(src));
329 assert(pos >= 0);
330 rename_cache_entry_at(pos, dst);
333 if (gitmodules_modified)
334 stage_updated_gitmodules(&the_index);
336 if (write_locked_index(&the_index, &lock_file,
337 COMMIT_LOCK | SKIP_IF_UNCHANGED))
338 die(_("Unable to write new index file"));
340 string_list_clear(&src_for_dst, 0);
341 UNLEAK(source);
342 UNLEAK(dest_path);
343 free(submodule_gitfile);
344 free(modes);
345 return 0;