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
19static const char **internal_copy_pathspec(const char *prefix,
20 const char **pathspec,
21 int count, int base_name)
22{
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;
39 }
40 }
41 return get_pathspec(prefix, result);
42}
43
44static const char *add_slash(const char *path)
45{
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;
53 }
54 return path;
55}
56
57static struct lock_file lock_file;
58
59int cmd_mv(int argc, const char **argv, const char *prefix)
60{
61 int i, newfd;
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_BOOLEAN('k', NULL, &ignore_errors, N_("skip move/rename errors")),
68 OPT_END(),
69 };
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;
74
75 git_config(git_default_config, NULL);
76
77 argc = parse_options(argc, argv, prefix, builtin_mv_options,
78 builtin_mv_usage, 0);
79 if (--argc < 1)
80 usage_with_options(builtin_mv_usage, builtin_mv_options);
81
82 newfd = hold_locked_index(&lock_file, 1);
83 if (read_cache() < 0)
84 die(_("index file corrupt"));
85
86 source = internal_copy_pathspec(prefix, argv, argc, 0);
87 modes = xcalloc(argc, sizeof(enum update_mode));
88 dest_path = internal_copy_pathspec(prefix, argv + argc, 1, 0);
89 submodule_gitfile = xcalloc(argc, sizeof(char *));
90
91 if (dest_path[0][0] == '\0')
92 /* special case: "." was normalized to "" */
93 destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
94 else if (!lstat(dest_path[0], &st) &&
95 S_ISDIR(st.st_mode)) {
96 dest_path[0] = add_slash(dest_path[0]);
97 destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
98 } else {
99 if (argc != 1)
100 die("destination '%s' is not a directory", dest_path[0]);
101 destination = dest_path;
102 }
103
104 /* Checking */
105 for (i = 0; i < argc; i++) {
106 const char *src = source[i], *dst = destination[i];
107 int length, src_is_dir;
108 const char *bad = NULL;
109
110 if (show_only)
111 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
112
113 length = strlen(src);
114 if (lstat(src, &st) < 0)
115 bad = _("bad source");
116 else if (!strncmp(src, dst, length) &&
117 (dst[length] == 0 || dst[length] == '/')) {
118 bad = _("can not move directory into itself");
119 } else if ((src_is_dir = S_ISDIR(st.st_mode))
120 && lstat(dst, &st) == 0)
121 bad = _("cannot move directory over file");
122 else if (src_is_dir) {
123 int first = cache_name_pos(src, length);
124 if (first >= 0) {
125 struct strbuf submodule_dotgit = STRBUF_INIT;
126 if (!S_ISGITLINK(active_cache[first]->ce_mode))
127 die (_("Huh? Directory %s is in index and no submodule?"), src);
128 strbuf_addf(&submodule_dotgit, "%s/.git", src);
129 submodule_gitfile[i] = read_gitfile(submodule_dotgit.buf);
130 if (submodule_gitfile[i])
131 submodule_gitfile[i] = xstrdup(submodule_gitfile[i]);
132 strbuf_release(&submodule_dotgit);
133 } else {
134 const char *src_w_slash = add_slash(src);
135 int last, len_w_slash = length + 1;
136
137 modes[i] = WORKING_DIRECTORY;
138
139 first = cache_name_pos(src_w_slash, len_w_slash);
140 if (first >= 0)
141 die (_("Huh? %.*s is in index?"),
142 len_w_slash, src_w_slash);
143
144 first = -1 - first;
145 for (last = first; last < active_nr; last++) {
146 const char *path = active_cache[last]->name;
147 if (strncmp(path, src_w_slash, len_w_slash))
148 break;
149 }
150 free((char *)src_w_slash);
151
152 if (last - first < 1)
153 bad = _("source directory is empty");
154 else {
155 int j, dst_len;
156
157 if (last - first > 0) {
158 source = xrealloc(source,
159 (argc + last - first)
160 * sizeof(char *));
161 destination = xrealloc(destination,
162 (argc + last - first)
163 * sizeof(char *));
164 modes = xrealloc(modes,
165 (argc + last - first)
166 * sizeof(enum update_mode));
167 }
168
169 dst = add_slash(dst);
170 dst_len = strlen(dst);
171
172 for (j = 0; j < last - first; j++) {
173 const char *path =
174 active_cache[first + j]->name;
175 source[argc + j] = path;
176 destination[argc + j] =
177 prefix_path(dst, dst_len,
178 path + length + 1);
179 modes[argc + j] = INDEX;
180 }
181 argc += last - first;
182 }
183 }
184 } else if (cache_name_pos(src, length) < 0)
185 bad = _("not under version control");
186 else if (lstat(dst, &st) == 0) {
187 bad = _("destination exists");
188 if (force) {
189 /*
190 * only files can overwrite each other:
191 * check both source and destination
192 */
193 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
194 if (verbose)
195 warning(_("overwriting '%s'"), dst);
196 bad = NULL;
197 } else
198 bad = _("Cannot overwrite");
199 }
200 } else if (string_list_has_string(&src_for_dst, dst))
201 bad = _("multiple sources for the same target");
202 else
203 string_list_insert(&src_for_dst, dst);
204
205 if (bad) {
206 if (ignore_errors) {
207 if (--argc > 0) {
208 memmove(source + i, source + i + 1,
209 (argc - i) * sizeof(char *));
210 memmove(destination + i,
211 destination + i + 1,
212 (argc - i) * sizeof(char *));
213 i--;
214 }
215 } else
216 die (_("%s, source=%s, destination=%s"),
217 bad, src, dst);
218 }
219 }
220
221 for (i = 0; i < argc; i++) {
222 const char *src = source[i], *dst = destination[i];
223 enum update_mode mode = modes[i];
224 int pos;
225 if (show_only || verbose)
226 printf(_("Renaming %s to %s\n"), src, dst);
227 if (!show_only && mode != INDEX) {
228 if (rename(src, dst) < 0 && !ignore_errors)
229 die_errno (_("renaming '%s' failed"), src);
230 if (submodule_gitfile[i])
231 connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
232 }
233
234 if (mode == WORKING_DIRECTORY)
235 continue;
236
237 pos = cache_name_pos(src, strlen(src));
238 assert(pos >= 0);
239 if (!show_only)
240 rename_cache_entry_at(pos, dst);
241 }
242
243 if (active_cache_changed) {
244 if (write_cache(newfd, active_cache, active_nr) ||
245 commit_locked_index(&lock_file))
246 die(_("Unable to write new index file"));
247 }
248
249 return 0;
250}