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