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