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"
1314
static const char * const builtin_mv_usage[] = {
15N_("git mv [options] <source>... <destination>"),
16NULL
17};
1819
static const char **internal_copy_pathspec(const char *prefix,
20const char **pathspec,
21int count, int base_name)
22{
23int i;
24const char **result = xmalloc((count + 1) * sizeof(const char *));
25memcpy(result, pathspec, count * sizeof(const char *));
26result[count] = NULL;
27for (i = 0; i < count; i++) {
28int length = strlen(result[i]);
29int to_copy = length;
30while (to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
31to_copy--;
32if (to_copy != length || base_name) {
33char *it = xmemdupz(result[i], to_copy);
34if (base_name) {
35result[i] = xstrdup(basename(it));
36free(it);
37} else
38result[i] = it;
39}
40}
41return get_pathspec(prefix, result);
42}
4344
static const char *add_slash(const char *path)
45{
46int len = strlen(path);
47if (path[len - 1] != '/') {
48char *with_slash = xmalloc(len + 2);
49memcpy(with_slash, path, len);
50with_slash[len++] = '/';
51with_slash[len] = 0;
52return with_slash;
53}
54return path;
55}
5657
static struct lock_file lock_file;
58#define SUBMODULE_WITH_GITDIR ((const char *)1)
5960
int cmd_mv(int argc, const char **argv, const char *prefix)
61{
62int i, newfd, gitmodules_modified = 0;
63int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
64struct option builtin_mv_options[] = {
65OPT__VERBOSE(&verbose, N_("be verbose")),
66OPT__DRY_RUN(&show_only, N_("dry run")),
67OPT__FORCE(&force, N_("force move/rename even if target exists")),
68OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
69OPT_END(),
70};
71const char **source, **destination, **dest_path, **submodule_gitfile;
72enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
73struct stat st;
74struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
7576
gitmodules_config();
77git_config(git_default_config, NULL);
7879
argc = parse_options(argc, argv, prefix, builtin_mv_options,
80builtin_mv_usage, 0);
81if (--argc < 1)
82usage_with_options(builtin_mv_usage, builtin_mv_options);
8384
newfd = hold_locked_index(&lock_file, 1);
85if (read_cache() < 0)
86die(_("index file corrupt"));
8788
source = internal_copy_pathspec(prefix, argv, argc, 0);
89modes = xcalloc(argc, sizeof(enum update_mode));
90dest_path = internal_copy_pathspec(prefix, argv + argc, 1, 0);
91submodule_gitfile = xcalloc(argc, sizeof(char *));
9293
if (dest_path[0][0] == '\0')
94/* special case: "." was normalized to "" */
95destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
96else if (!lstat(dest_path[0], &st) &&
97S_ISDIR(st.st_mode)) {
98dest_path[0] = add_slash(dest_path[0]);
99destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
100} else {
101if (argc != 1)
102die("destination '%s' is not a directory", dest_path[0]);
103destination = dest_path;
104}
105106
/* Checking */
107for (i = 0; i < argc; i++) {
108const char *src = source[i], *dst = destination[i];
109int length, src_is_dir;
110const char *bad = NULL;
111112
if (show_only)
113printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
114115
length = strlen(src);
116if (lstat(src, &st) < 0)
117bad = _("bad source");
118else if (!strncmp(src, dst, length) &&
119(dst[length] == 0 || dst[length] == '/')) {
120bad = _("can not move directory into itself");
121} else if ((src_is_dir = S_ISDIR(st.st_mode))
122&& lstat(dst, &st) == 0)
123bad = _("cannot move directory over file");
124else if (src_is_dir) {
125int first = cache_name_pos(src, length);
126if (first >= 0) {
127struct strbuf submodule_dotgit = STRBUF_INIT;
128if (!S_ISGITLINK(active_cache[first]->ce_mode))
129die (_("Huh? Directory %s is in index and no submodule?"), src);
130if (!is_staging_gitmodules_ok())
131die (_("Please, stage your changes to .gitmodules or stash them to proceed"));
132strbuf_addf(&submodule_dotgit, "%s/.git", src);
133submodule_gitfile[i] = read_gitfile(submodule_dotgit.buf);
134if (submodule_gitfile[i])
135submodule_gitfile[i] = xstrdup(submodule_gitfile[i]);
136else
137submodule_gitfile[i] = SUBMODULE_WITH_GITDIR;
138strbuf_release(&submodule_dotgit);
139} else {
140const char *src_w_slash = add_slash(src);
141int last, len_w_slash = length + 1;
142143
modes[i] = WORKING_DIRECTORY;
144145
first = cache_name_pos(src_w_slash, len_w_slash);
146if (first >= 0)
147die (_("Huh? %.*s is in index?"),
148len_w_slash, src_w_slash);
149150
first = -1 - first;
151for (last = first; last < active_nr; last++) {
152const char *path = active_cache[last]->name;
153if (strncmp(path, src_w_slash, len_w_slash))
154break;
155}
156free((char *)src_w_slash);
157158
if (last - first < 1)
159bad = _("source directory is empty");
160else {
161int j, dst_len;
162163
if (last - first > 0) {
164source = xrealloc(source,
165(argc + last - first)
166* sizeof(char *));
167destination = xrealloc(destination,
168(argc + last - first)
169* sizeof(char *));
170modes = xrealloc(modes,
171(argc + last - first)
172* sizeof(enum update_mode));
173}
174175
dst = add_slash(dst);
176dst_len = strlen(dst);
177178
for (j = 0; j < last - first; j++) {
179const char *path =
180active_cache[first + j]->name;
181source[argc + j] = path;
182destination[argc + j] =
183prefix_path(dst, dst_len,
184path + length + 1);
185modes[argc + j] = INDEX;
186}
187argc += last - first;
188}
189}
190} else if (cache_name_pos(src, length) < 0)
191bad = _("not under version control");
192else if (lstat(dst, &st) == 0) {
193bad = _("destination exists");
194if (force) {
195/*
196* only files can overwrite each other:
197* check both source and destination
198*/
199if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
200if (verbose)
201warning(_("overwriting '%s'"), dst);
202bad = NULL;
203} else
204bad = _("Cannot overwrite");
205}
206} else if (string_list_has_string(&src_for_dst, dst))
207bad = _("multiple sources for the same target");
208else
209string_list_insert(&src_for_dst, dst);
210211
if (bad) {
212if (ignore_errors) {
213if (--argc > 0) {
214memmove(source + i, source + i + 1,
215(argc - i) * sizeof(char *));
216memmove(destination + i,
217destination + i + 1,
218(argc - i) * sizeof(char *));
219i--;
220}
221} else
222die (_("%s, source=%s, destination=%s"),
223bad, src, dst);
224}
225}
226227
for (i = 0; i < argc; i++) {
228const char *src = source[i], *dst = destination[i];
229enum update_mode mode = modes[i];
230int pos;
231if (show_only || verbose)
232printf(_("Renaming %s to %s\n"), src, dst);
233if (!show_only && mode != INDEX) {
234if (rename(src, dst) < 0 && !ignore_errors)
235die_errno (_("renaming '%s' failed"), src);
236if (submodule_gitfile[i]) {
237if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
238connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
239if (!update_path_in_gitmodules(src, dst))
240gitmodules_modified = 1;
241}
242}
243244
if (mode == WORKING_DIRECTORY)
245continue;
246247
pos = cache_name_pos(src, strlen(src));
248assert(pos >= 0);
249if (!show_only)
250rename_cache_entry_at(pos, dst);
251}
252253
if (gitmodules_modified)
254stage_updated_gitmodules();
255256
if (active_cache_changed) {
257if (write_cache(newfd, active_cache, active_nr) ||
258commit_locked_index(&lock_file))
259die(_("Unable to write new index file"));
260}
261262
return 0;
263}