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