Documentation/git-submodule: cleanup "add" section
[git/debian.git] / builtin / mv.c
blob61d20037add7cf56df721f8d69612b50412f94e9
1 /*
2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
5 */
6 #include "builtin.h"
7 #include "pathspec.h"
8 #include "lockfile.h"
9 #include "dir.h"
10 #include "cache-tree.h"
11 #include "string-list.h"
12 #include "parse-options.h"
13 #include "submodule.h"
15 static const char * const builtin_mv_usage[] = {
16 N_("git mv [<options>] <source>... <destination>"),
17 NULL
20 #define DUP_BASENAME 1
21 #define KEEP_TRAILING_SLASH 2
23 static const char **internal_prefix_pathspec(const char *prefix,
24 const char **pathspec,
25 int count, unsigned flags)
27 int i;
28 const char **result;
29 int prefixlen = prefix ? strlen(prefix) : 0;
30 ALLOC_ARRAY(result, count + 1);
32 /* Create an intermediate copy of the pathspec based on the flags */
33 for (i = 0; i < count; i++) {
34 int length = strlen(pathspec[i]);
35 int to_copy = length;
36 char *it;
37 while (!(flags & KEEP_TRAILING_SLASH) &&
38 to_copy > 0 && is_dir_sep(pathspec[i][to_copy - 1]))
39 to_copy--;
41 it = xmemdupz(pathspec[i], to_copy);
42 if (flags & DUP_BASENAME) {
43 result[i] = xstrdup(basename(it));
44 free(it);
45 } else {
46 result[i] = it;
49 result[count] = NULL;
51 /* Prefix the pathspec and free the old intermediate strings */
52 for (i = 0; i < count; i++) {
53 const char *match = prefix_path(prefix, prefixlen, result[i]);
54 free((char *) result[i]);
55 result[i] = match;
58 return result;
61 static const char *add_slash(const char *path)
63 size_t len = strlen(path);
64 if (path[len - 1] != '/') {
65 char *with_slash = xmalloc(st_add(len, 2));
66 memcpy(with_slash, path, len);
67 with_slash[len++] = '/';
68 with_slash[len] = 0;
69 return with_slash;
71 return path;
74 static struct lock_file lock_file;
75 #define SUBMODULE_WITH_GITDIR ((const char *)1)
77 static void prepare_move_submodule(const char *src, int first,
78 const char **submodule_gitfile)
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())
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);
94 static int index_range_of_same_dir(const char *src, int length,
95 int *first_p, int *last_p)
97 const char *src_w_slash = add_slash(src);
98 int first, last, len_w_slash = length + 1;
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);
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;
110 if (src_w_slash != src)
111 free((char *)src_w_slash);
112 *first_p = first;
113 *last_p = last;
114 return last - first;
117 int cmd_mv(int argc, const char **argv, const char *prefix)
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 OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
126 OPT_END(),
128 const char **source, **destination, **dest_path, **submodule_gitfile;
129 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
130 struct stat st;
131 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
133 gitmodules_config();
134 git_config(git_default_config, NULL);
136 argc = parse_options(argc, argv, prefix, builtin_mv_options,
137 builtin_mv_usage, 0);
138 if (--argc < 1)
139 usage_with_options(builtin_mv_usage, builtin_mv_options);
141 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
142 if (read_cache() < 0)
143 die(_("index file corrupt"));
145 source = internal_prefix_pathspec(prefix, argv, argc, 0);
146 modes = xcalloc(argc, sizeof(enum update_mode));
148 * Keep trailing slash, needed to let
149 * "git mv file no-such-dir/" error out, except in the case
150 * "git mv directory no-such-dir/".
152 flags = KEEP_TRAILING_SLASH;
153 if (argc == 1 && is_directory(argv[0]) && !is_directory(argv[1]))
154 flags = 0;
155 dest_path = internal_prefix_pathspec(prefix, argv + argc, 1, flags);
156 submodule_gitfile = xcalloc(argc, sizeof(char *));
158 if (dest_path[0][0] == '\0')
159 /* special case: "." was normalized to "" */
160 destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
161 else if (!lstat(dest_path[0], &st) &&
162 S_ISDIR(st.st_mode)) {
163 dest_path[0] = add_slash(dest_path[0]);
164 destination = internal_prefix_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
165 } else {
166 if (argc != 1)
167 die(_("destination '%s' is not a directory"), dest_path[0]);
168 destination = dest_path;
171 /* Checking */
172 for (i = 0; i < argc; i++) {
173 const char *src = source[i], *dst = destination[i];
174 int length, src_is_dir;
175 const char *bad = NULL;
177 if (show_only)
178 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
180 length = strlen(src);
181 if (lstat(src, &st) < 0)
182 bad = _("bad source");
183 else if (!strncmp(src, dst, length) &&
184 (dst[length] == 0 || dst[length] == '/')) {
185 bad = _("can not move directory into itself");
186 } else if ((src_is_dir = S_ISDIR(st.st_mode))
187 && lstat(dst, &st) == 0)
188 bad = _("cannot move directory over file");
189 else if (src_is_dir) {
190 int first = cache_name_pos(src, length), last;
192 if (first >= 0)
193 prepare_move_submodule(src, first,
194 submodule_gitfile + i);
195 else if (index_range_of_same_dir(src, length,
196 &first, &last) < 1)
197 bad = _("source directory is empty");
198 else { /* last - first >= 1 */
199 int j, dst_len, n;
201 modes[i] = WORKING_DIRECTORY;
202 n = argc + last - first;
203 REALLOC_ARRAY(source, n);
204 REALLOC_ARRAY(destination, n);
205 REALLOC_ARRAY(modes, n);
206 REALLOC_ARRAY(submodule_gitfile, n);
208 dst = add_slash(dst);
209 dst_len = strlen(dst);
211 for (j = 0; j < last - first; j++) {
212 const char *path = active_cache[first + j]->name;
213 source[argc + j] = path;
214 destination[argc + j] =
215 prefix_path(dst, dst_len, path + length + 1);
216 modes[argc + j] = INDEX;
217 submodule_gitfile[argc + j] = NULL;
219 argc += last - first;
221 } else if (cache_name_pos(src, length) < 0)
222 bad = _("not under version control");
223 else if (lstat(dst, &st) == 0 &&
224 (!ignore_case || strcasecmp(src, dst))) {
225 bad = _("destination exists");
226 if (force) {
228 * only files can overwrite each other:
229 * check both source and destination
231 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
232 if (verbose)
233 warning(_("overwriting '%s'"), dst);
234 bad = NULL;
235 } else
236 bad = _("Cannot overwrite");
238 } else if (string_list_has_string(&src_for_dst, dst))
239 bad = _("multiple sources for the same target");
240 else if (is_dir_sep(dst[strlen(dst) - 1]))
241 bad = _("destination directory does not exist");
242 else
243 string_list_insert(&src_for_dst, dst);
245 if (!bad)
246 continue;
247 if (!ignore_errors)
248 die(_("%s, source=%s, destination=%s"),
249 bad, src, dst);
250 if (--argc > 0) {
251 int n = argc - i;
252 memmove(source + i, source + i + 1,
253 n * sizeof(char *));
254 memmove(destination + i, destination + i + 1,
255 n * sizeof(char *));
256 memmove(modes + i, modes + i + 1,
257 n * sizeof(enum update_mode));
258 memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
259 n * sizeof(char *));
260 i--;
264 for (i = 0; i < argc; i++) {
265 const char *src = source[i], *dst = destination[i];
266 enum update_mode mode = modes[i];
267 int pos;
268 if (show_only || verbose)
269 printf(_("Renaming %s to %s\n"), src, dst);
270 if (show_only)
271 continue;
272 if (mode != INDEX && rename(src, dst) < 0) {
273 if (ignore_errors)
274 continue;
275 die_errno(_("renaming '%s' failed"), src);
277 if (submodule_gitfile[i]) {
278 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
279 connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
280 if (!update_path_in_gitmodules(src, dst))
281 gitmodules_modified = 1;
284 if (mode == WORKING_DIRECTORY)
285 continue;
287 pos = cache_name_pos(src, strlen(src));
288 assert(pos >= 0);
289 if (!show_only)
290 rename_cache_entry_at(pos, dst);
293 if (gitmodules_modified)
294 stage_updated_gitmodules();
296 if (active_cache_changed &&
297 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
298 die(_("Unable to write new index file"));
300 return 0;