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 static const char **internal_copy_pathspec(const char *prefix,
20 const char **pathspec,
21 int count, int base_name)
24 const char **result = xmalloc((count + 1) * sizeof(const char *));
25 memcpy(result, pathspec, count * sizeof(const char *));
27 for (i = 0; i < count; i++) {
28 int length = strlen(result[i]);
30 while (to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
32 if (to_copy != length || base_name) {
33 char *it = xmemdupz(result[i], to_copy);
35 result[i] = xstrdup(basename(it));
41 return get_pathspec(prefix, result);
44 static const char *add_slash(const char *path)
46 int len = strlen(path);
47 if (path[len - 1] != '/') {
48 char *with_slash = xmalloc(len + 2);
49 memcpy(with_slash, path, len);
50 with_slash[len++] = '/';
57 static struct lock_file lock_file;
58 #define SUBMODULE_WITH_GITDIR ((const char *)1)
60 int cmd_mv(int argc, const char **argv, const char *prefix)
62 int i, newfd, gitmodules_modified = 0;
63 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
64 struct option builtin_mv_options[] = {
65 OPT__VERBOSE(&verbose, N_("be verbose")),
66 OPT__DRY_RUN(&show_only, N_("dry run")),
67 OPT__FORCE(&force, N_("force move/rename even if target exists")),
68 OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
71 const char **source, **destination, **dest_path, **submodule_gitfile;
72 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
74 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
77 git_config(git_default_config, NULL);
79 argc = parse_options(argc, argv, prefix, builtin_mv_options,
82 usage_with_options(builtin_mv_usage, builtin_mv_options);
84 newfd = hold_locked_index(&lock_file, 1);
86 die(_("index file corrupt"));
88 source = internal_copy_pathspec(prefix, argv, argc, 0);
89 modes = xcalloc(argc, sizeof(enum update_mode));
90 dest_path = internal_copy_pathspec(prefix, argv + argc, 1, 0);
91 submodule_gitfile = xcalloc(argc, sizeof(char *));
93 if (dest_path[0][0] == '\0')
94 /* special case: "." was normalized to "" */
95 destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
96 else if (!lstat(dest_path[0], &st) &&
97 S_ISDIR(st.st_mode)) {
98 dest_path[0] = add_slash(dest_path[0]);
99 destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
102 die("destination '%s' is not a directory", dest_path[0]);
103 destination = dest_path;
107 for (i = 0; i < argc; i++) {
108 const char *src = source[i], *dst = destination[i];
109 int length, src_is_dir;
110 const char *bad = NULL;
113 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
115 length = strlen(src);
116 if (lstat(src, &st) < 0)
117 bad = _("bad source");
118 else if (!strncmp(src, dst, length) &&
119 (dst[length] == 0 || dst[length] == '/')) {
120 bad = _("can not move directory into itself");
121 } else if ((src_is_dir = S_ISDIR(st.st_mode))
122 && lstat(dst, &st) == 0)
123 bad = _("cannot move directory over file");
124 else if (src_is_dir) {
125 int first = cache_name_pos(src, length);
127 struct strbuf submodule_dotgit = STRBUF_INIT;
128 if (!S_ISGITLINK(active_cache[first]->ce_mode))
129 die (_("Huh? Directory %s is in index and no submodule?"), src);
130 if (!is_staging_gitmodules_ok())
131 die (_("Please, stage your changes to .gitmodules or stash them to proceed"));
132 strbuf_addf(&submodule_dotgit, "%s/.git", src);
133 submodule_gitfile[i] = read_gitfile(submodule_dotgit.buf);
134 if (submodule_gitfile[i])
135 submodule_gitfile[i] = xstrdup(submodule_gitfile[i]);
137 submodule_gitfile[i] = SUBMODULE_WITH_GITDIR;
138 strbuf_release(&submodule_dotgit);
140 const char *src_w_slash = add_slash(src);
141 int last, len_w_slash = length + 1;
143 modes[i] = WORKING_DIRECTORY;
145 first = cache_name_pos(src_w_slash, len_w_slash);
147 die (_("Huh? %.*s is in index?"),
148 len_w_slash, src_w_slash);
151 for (last = first; last < active_nr; last++) {
152 const char *path = active_cache[last]->name;
153 if (strncmp(path, src_w_slash, len_w_slash))
156 free((char *)src_w_slash);
158 if (last - first < 1)
159 bad = _("source directory is empty");
163 if (last - first > 0) {
164 source = xrealloc(source,
165 (argc + last - first)
167 destination = xrealloc(destination,
168 (argc + last - first)
170 modes = xrealloc(modes,
171 (argc + last - first)
172 * sizeof(enum update_mode));
175 dst = add_slash(dst);
176 dst_len = strlen(dst);
178 for (j = 0; j < last - first; j++) {
180 active_cache[first + j]->name;
181 source[argc + j] = path;
182 destination[argc + j] =
183 prefix_path(dst, dst_len,
185 modes[argc + j] = INDEX;
187 argc += last - first;
190 } else if (cache_name_pos(src, length) < 0)
191 bad = _("not under version control");
192 else if (lstat(dst, &st) == 0) {
193 bad = _("destination exists");
196 * only files can overwrite each other:
197 * check both source and destination
199 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
201 warning(_("overwriting '%s'"), dst);
204 bad = _("Cannot overwrite");
206 } else if (string_list_has_string(&src_for_dst, dst))
207 bad = _("multiple sources for the same target");
209 string_list_insert(&src_for_dst, dst);
214 memmove(source + i, source + i + 1,
215 (argc - i) * sizeof(char *));
216 memmove(destination + i,
218 (argc - i) * sizeof(char *));
222 die (_("%s, source=%s, destination=%s"),
227 for (i = 0; i < argc; i++) {
228 const char *src = source[i], *dst = destination[i];
229 enum update_mode mode = modes[i];
231 if (show_only || verbose)
232 printf(_("Renaming %s to %s\n"), src, dst);
233 if (!show_only && mode != INDEX) {
234 if (rename(src, dst) < 0 && !ignore_errors)
235 die_errno (_("renaming '%s' failed"), src);
236 if (submodule_gitfile[i]) {
237 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
238 connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
239 if (!update_path_in_gitmodules(src, dst))
240 gitmodules_modified = 1;
244 if (mode == WORKING_DIRECTORY)
247 pos = cache_name_pos(src, strlen(src));
250 rename_cache_entry_at(pos, dst);
253 if (gitmodules_modified)
254 stage_updated_gitmodules();
256 if (active_cache_changed) {
257 if (write_cache(newfd, active_cache, active_nr) ||
258 commit_locked_index(&lock_file))
259 die(_("Unable to write new index file"));