2 * "git mv" builtin command
4 * Copyright (C) 2006 Johannes Schindelin
9 #include "cache-tree.h"
10 #include "string-list.h"
11 #include "parse-options.h"
12 #include "submodule.h"
14 static const char * const builtin_mv_usage[] = {
15 N_("git mv [<options>] <source>... <destination>"),
19 #define DUP_BASENAME 1
20 #define KEEP_TRAILING_SLASH 2
22 static const char **internal_copy_pathspec(const char *prefix,
23 const char **pathspec,
24 int count, unsigned flags)
28 ALLOC_ARRAY(result, count + 1);
29 memcpy(result, pathspec, count * sizeof(const char *));
31 for (i = 0; i < count; i++) {
32 int length = strlen(result[i]);
34 while (!(flags & KEEP_TRAILING_SLASH) &&
35 to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
37 if (to_copy != length || flags & DUP_BASENAME) {
38 char *it = xmemdupz(result[i], to_copy);
39 if (flags & DUP_BASENAME) {
40 result[i] = xstrdup(basename(it));
46 return get_pathspec(prefix, result);
49 static const char *add_slash(const char *path)
51 size_t len = strlen(path);
52 if (path[len - 1] != '/') {
53 char *with_slash = xmalloc(st_add(len, 2));
54 memcpy(with_slash, path, len);
55 with_slash[len++] = '/';
62 static struct lock_file lock_file;
63 #define SUBMODULE_WITH_GITDIR ((const char *)1)
65 static void prepare_move_submodule(const char *src, int first,
66 const char **submodule_gitfile)
68 struct strbuf submodule_dotgit = STRBUF_INIT;
69 if (!S_ISGITLINK(active_cache[first]->ce_mode))
70 die(_("Directory %s is in index and no submodule?"), src);
71 if (!is_staging_gitmodules_ok())
72 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
73 strbuf_addf(&submodule_dotgit, "%s/.git", src);
74 *submodule_gitfile = read_gitfile(submodule_dotgit.buf);
75 if (*submodule_gitfile)
76 *submodule_gitfile = xstrdup(*submodule_gitfile);
78 *submodule_gitfile = SUBMODULE_WITH_GITDIR;
79 strbuf_release(&submodule_dotgit);
82 static int index_range_of_same_dir(const char *src, int length,
83 int *first_p, int *last_p)
85 const char *src_w_slash = add_slash(src);
86 int first, last, len_w_slash = length + 1;
88 first = cache_name_pos(src_w_slash, len_w_slash);
90 die(_("%.*s is in index"), len_w_slash, src_w_slash);
93 for (last = first; last < active_nr; last++) {
94 const char *path = active_cache[last]->name;
95 if (strncmp(path, src_w_slash, len_w_slash))
98 if (src_w_slash != src)
99 free((char *)src_w_slash);
105 int cmd_mv(int argc, const char **argv, const char *prefix)
107 int i, gitmodules_modified = 0;
108 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
109 struct option builtin_mv_options[] = {
110 OPT__VERBOSE(&verbose, N_("be verbose")),
111 OPT__DRY_RUN(&show_only, N_("dry run")),
112 OPT__FORCE(&force, N_("force move/rename even if target exists")),
113 OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
116 const char **source, **destination, **dest_path, **submodule_gitfile;
117 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
119 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
122 git_config(git_default_config, NULL);
124 argc = parse_options(argc, argv, prefix, builtin_mv_options,
125 builtin_mv_usage, 0);
127 usage_with_options(builtin_mv_usage, builtin_mv_options);
129 hold_locked_index(&lock_file, 1);
130 if (read_cache() < 0)
131 die(_("index file corrupt"));
133 source = internal_copy_pathspec(prefix, argv, argc, 0);
134 modes = xcalloc(argc, sizeof(enum update_mode));
136 * Keep trailing slash, needed to let
137 * "git mv file no-such-dir/" error out.
139 dest_path = internal_copy_pathspec(prefix, argv + argc, 1,
140 KEEP_TRAILING_SLASH);
141 submodule_gitfile = xcalloc(argc, sizeof(char *));
143 if (dest_path[0][0] == '\0')
144 /* special case: "." was normalized to "" */
145 destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
146 else if (!lstat(dest_path[0], &st) &&
147 S_ISDIR(st.st_mode)) {
148 dest_path[0] = add_slash(dest_path[0]);
149 destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
152 die(_("destination '%s' is not a directory"), dest_path[0]);
153 destination = dest_path;
157 for (i = 0; i < argc; i++) {
158 const char *src = source[i], *dst = destination[i];
159 int length, src_is_dir;
160 const char *bad = NULL;
163 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
165 length = strlen(src);
166 if (lstat(src, &st) < 0)
167 bad = _("bad source");
168 else if (!strncmp(src, dst, length) &&
169 (dst[length] == 0 || dst[length] == '/')) {
170 bad = _("can not move directory into itself");
171 } else if ((src_is_dir = S_ISDIR(st.st_mode))
172 && lstat(dst, &st) == 0)
173 bad = _("cannot move directory over file");
174 else if (src_is_dir) {
175 int first = cache_name_pos(src, length), last;
178 prepare_move_submodule(src, first,
179 submodule_gitfile + i);
180 else if (index_range_of_same_dir(src, length,
182 bad = _("source directory is empty");
183 else { /* last - first >= 1 */
186 modes[i] = WORKING_DIRECTORY;
187 n = argc + last - first;
188 REALLOC_ARRAY(source, n);
189 REALLOC_ARRAY(destination, n);
190 REALLOC_ARRAY(modes, n);
191 REALLOC_ARRAY(submodule_gitfile, n);
193 dst = add_slash(dst);
194 dst_len = strlen(dst);
196 for (j = 0; j < last - first; j++) {
197 const char *path = active_cache[first + j]->name;
198 source[argc + j] = path;
199 destination[argc + j] =
200 prefix_path(dst, dst_len, path + length + 1);
201 modes[argc + j] = INDEX;
202 submodule_gitfile[argc + j] = NULL;
204 argc += last - first;
206 } else if (cache_name_pos(src, length) < 0)
207 bad = _("not under version control");
208 else if (lstat(dst, &st) == 0 &&
209 (!ignore_case || strcasecmp(src, dst))) {
210 bad = _("destination exists");
213 * only files can overwrite each other:
214 * check both source and destination
216 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
218 warning(_("overwriting '%s'"), dst);
221 bad = _("Cannot overwrite");
223 } else if (string_list_has_string(&src_for_dst, dst))
224 bad = _("multiple sources for the same target");
225 else if (is_dir_sep(dst[strlen(dst) - 1]))
226 bad = _("destination directory does not exist");
228 string_list_insert(&src_for_dst, dst);
233 die(_("%s, source=%s, destination=%s"),
237 memmove(source + i, source + i + 1,
239 memmove(destination + i, destination + i + 1,
241 memmove(modes + i, modes + i + 1,
242 n * sizeof(enum update_mode));
243 memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
249 for (i = 0; i < argc; i++) {
250 const char *src = source[i], *dst = destination[i];
251 enum update_mode mode = modes[i];
253 if (show_only || verbose)
254 printf(_("Renaming %s to %s\n"), src, dst);
255 if (!show_only && mode != INDEX) {
256 if (rename(src, dst) < 0 && !ignore_errors)
257 die_errno(_("renaming '%s' failed"), src);
258 if (submodule_gitfile[i]) {
259 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
260 connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
261 if (!update_path_in_gitmodules(src, dst))
262 gitmodules_modified = 1;
266 if (mode == WORKING_DIRECTORY)
269 pos = cache_name_pos(src, strlen(src));
272 rename_cache_entry_at(pos, dst);
275 if (gitmodules_modified)
276 stage_updated_gitmodules();
278 if (active_cache_changed &&
279 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
280 die(_("Unable to write new index file"));