pathspec: stop --*-pathspecs impact on internal parse_pathspec() uses
[git/debian.git] / builtin / mv.c
blobaec79d18386b52a943b20e6ebe0dfc9b6f074f0f
1 /*
2 * "git mv" builtin command
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"
14 static const char * const builtin_mv_usage[] = {
15 N_("git mv [options] <source>... <destination>"),
16 NULL
19 static const char **internal_copy_pathspec(const char *prefix,
20 const char **pathspec,
21 int count, int base_name)
23 int i;
24 const char **result = xmalloc((count + 1) * sizeof(const char *));
25 memcpy(result, pathspec, count * sizeof(const char *));
26 result[count] = NULL;
27 for (i = 0; i < count; i++) {
28 int length = strlen(result[i]);
29 int to_copy = length;
30 while (to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
31 to_copy--;
32 if (to_copy != length || base_name) {
33 char *it = xmemdupz(result[i], to_copy);
34 if (base_name) {
35 result[i] = xstrdup(basename(it));
36 free(it);
37 } else
38 result[i] = 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++] = '/';
51 with_slash[len] = 0;
52 return with_slash;
54 return path;
57 static struct lock_file lock_file;
59 int cmd_mv(int argc, const char **argv, const char *prefix)
61 int i, newfd, gitmodules_modified = 0;
62 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
63 struct option builtin_mv_options[] = {
64 OPT__VERBOSE(&verbose, N_("be verbose")),
65 OPT__DRY_RUN(&show_only, N_("dry run")),
66 OPT__FORCE(&force, N_("force move/rename even if target exists")),
67 OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
68 OPT_END(),
70 const char **source, **destination, **dest_path, **submodule_gitfile;
71 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
72 struct stat st;
73 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
75 gitmodules_config();
76 git_config(git_default_config, NULL);
78 argc = parse_options(argc, argv, prefix, builtin_mv_options,
79 builtin_mv_usage, 0);
80 if (--argc < 1)
81 usage_with_options(builtin_mv_usage, builtin_mv_options);
83 newfd = hold_locked_index(&lock_file, 1);
84 if (read_cache() < 0)
85 die(_("index file corrupt"));
87 source = internal_copy_pathspec(prefix, argv, argc, 0);
88 modes = xcalloc(argc, sizeof(enum update_mode));
89 dest_path = internal_copy_pathspec(prefix, argv + argc, 1, 0);
90 submodule_gitfile = xcalloc(argc, sizeof(char *));
92 if (dest_path[0][0] == '\0')
93 /* special case: "." was normalized to "" */
94 destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
95 else if (!lstat(dest_path[0], &st) &&
96 S_ISDIR(st.st_mode)) {
97 dest_path[0] = add_slash(dest_path[0]);
98 destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
99 } else {
100 if (argc != 1)
101 die("destination '%s' is not a directory", dest_path[0]);
102 destination = dest_path;
105 /* Checking */
106 for (i = 0; i < argc; i++) {
107 const char *src = source[i], *dst = destination[i];
108 int length, src_is_dir;
109 const char *bad = NULL;
111 if (show_only)
112 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
114 length = strlen(src);
115 if (lstat(src, &st) < 0)
116 bad = _("bad source");
117 else if (!strncmp(src, dst, length) &&
118 (dst[length] == 0 || dst[length] == '/')) {
119 bad = _("can not move directory into itself");
120 } else if ((src_is_dir = S_ISDIR(st.st_mode))
121 && lstat(dst, &st) == 0)
122 bad = _("cannot move directory over file");
123 else if (src_is_dir) {
124 int first = cache_name_pos(src, length);
125 if (first >= 0) {
126 struct strbuf submodule_dotgit = STRBUF_INIT;
127 if (!S_ISGITLINK(active_cache[first]->ce_mode))
128 die (_("Huh? Directory %s is in index and no submodule?"), src);
129 if (!is_staging_gitmodules_ok())
130 die (_("Please, stage your changes to .gitmodules or stash them to proceed"));
131 strbuf_addf(&submodule_dotgit, "%s/.git", src);
132 submodule_gitfile[i] = read_gitfile(submodule_dotgit.buf);
133 if (submodule_gitfile[i])
134 submodule_gitfile[i] = xstrdup(submodule_gitfile[i]);
135 strbuf_release(&submodule_dotgit);
136 } else {
137 const char *src_w_slash = add_slash(src);
138 int last, len_w_slash = length + 1;
140 modes[i] = WORKING_DIRECTORY;
142 first = cache_name_pos(src_w_slash, len_w_slash);
143 if (first >= 0)
144 die (_("Huh? %.*s is in index?"),
145 len_w_slash, src_w_slash);
147 first = -1 - first;
148 for (last = first; last < active_nr; last++) {
149 const char *path = active_cache[last]->name;
150 if (strncmp(path, src_w_slash, len_w_slash))
151 break;
153 free((char *)src_w_slash);
155 if (last - first < 1)
156 bad = _("source directory is empty");
157 else {
158 int j, dst_len;
160 if (last - first > 0) {
161 source = xrealloc(source,
162 (argc + last - first)
163 * sizeof(char *));
164 destination = xrealloc(destination,
165 (argc + last - first)
166 * sizeof(char *));
167 modes = xrealloc(modes,
168 (argc + last - first)
169 * sizeof(enum update_mode));
172 dst = add_slash(dst);
173 dst_len = strlen(dst);
175 for (j = 0; j < last - first; j++) {
176 const char *path =
177 active_cache[first + j]->name;
178 source[argc + j] = path;
179 destination[argc + j] =
180 prefix_path(dst, dst_len,
181 path + length + 1);
182 modes[argc + j] = INDEX;
184 argc += last - first;
187 } else if (cache_name_pos(src, length) < 0)
188 bad = _("not under version control");
189 else if (lstat(dst, &st) == 0) {
190 bad = _("destination exists");
191 if (force) {
193 * only files can overwrite each other:
194 * check both source and destination
196 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
197 if (verbose)
198 warning(_("overwriting '%s'"), dst);
199 bad = NULL;
200 } else
201 bad = _("Cannot overwrite");
203 } else if (string_list_has_string(&src_for_dst, dst))
204 bad = _("multiple sources for the same target");
205 else
206 string_list_insert(&src_for_dst, dst);
208 if (bad) {
209 if (ignore_errors) {
210 if (--argc > 0) {
211 memmove(source + i, source + i + 1,
212 (argc - i) * sizeof(char *));
213 memmove(destination + i,
214 destination + i + 1,
215 (argc - i) * sizeof(char *));
216 i--;
218 } else
219 die (_("%s, source=%s, destination=%s"),
220 bad, src, dst);
224 for (i = 0; i < argc; i++) {
225 const char *src = source[i], *dst = destination[i];
226 enum update_mode mode = modes[i];
227 int pos;
228 if (show_only || verbose)
229 printf(_("Renaming %s to %s\n"), src, dst);
230 if (!show_only && mode != INDEX) {
231 if (rename(src, dst) < 0 && !ignore_errors)
232 die_errno (_("renaming '%s' failed"), src);
233 if (submodule_gitfile[i])
234 connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
235 if (!update_path_in_gitmodules(src, dst))
236 gitmodules_modified = 1;
239 if (mode == WORKING_DIRECTORY)
240 continue;
242 pos = cache_name_pos(src, strlen(src));
243 assert(pos >= 0);
244 if (!show_only)
245 rename_cache_entry_at(pos, dst);
248 if (gitmodules_modified)
249 stage_updated_gitmodules();
251 if (active_cache_changed) {
252 if (write_cache(newfd, active_cache, active_nr) ||
253 commit_locked_index(&lock_file))
254 die(_("Unable to write new index file"));
257 return 0;