mv: move submodules together with their work trees
[git/git.git] / builtin / mv.c
CommitLineData
11be42a4
JS
1/*
2 * "git mv" builtin command
3 *
4 * Copyright (C) 2006 Johannes Schindelin
5 */
11be42a4
JS
6#include "cache.h"
7#include "builtin.h"
8#include "dir.h"
9#include "cache-tree.h"
c455c87c 10#include "string-list.h"
c7a20c11 11#include "parse-options.h"
11be42a4 12
c7a20c11 13static const char * const builtin_mv_usage[] = {
6c54dc46 14 N_("git mv [options] <source>... <destination>"),
c7a20c11
PH
15 NULL
16};
11be42a4 17
e4d92cdc
NTND
18static const char **internal_copy_pathspec(const char *prefix,
19 const char **pathspec,
20 int count, int base_name)
11be42a4 21{
d78b0f3d 22 int i;
11be42a4
JS
23 const char **result = xmalloc((count + 1) * sizeof(const char *));
24 memcpy(result, pathspec, count * sizeof(const char *));
25 result[count] = NULL;
d78b0f3d
JS
26 for (i = 0; i < count; i++) {
27 int length = strlen(result[i]);
af82559b
JH
28 int to_copy = length;
29 while (to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
30 to_copy--;
31 if (to_copy != length || base_name) {
32 char *it = xmemdupz(result[i], to_copy);
0d0ff65c
BC
33 if (base_name) {
34 result[i] = xstrdup(basename(it));
35 free(it);
36 } else
37 result[i] = it;
af82559b 38 }
11be42a4 39 }
971dfa19 40 return get_pathspec(prefix, result);
11be42a4
JS
41}
42
ac64a722
JS
43static const char *add_slash(const char *path)
44{
45 int len = strlen(path);
46 if (path[len - 1] != '/') {
47 char *with_slash = xmalloc(len + 2);
48 memcpy(with_slash, path, len);
329a3047
JH
49 with_slash[len++] = '/';
50 with_slash[len] = 0;
ac64a722
JS
51 return with_slash;
52 }
53 return path;
54}
55
11be42a4
JS
56static struct lock_file lock_file;
57
7061cf0f 58int cmd_mv(int argc, const char **argv, const char *prefix)
11be42a4 59{
c7a20c11 60 int i, newfd;
11be42a4 61 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
c7a20c11 62 struct option builtin_mv_options[] = {
6c54dc46
NTND
63 OPT__VERBOSE(&verbose, N_("be verbose")),
64 OPT__DRY_RUN(&show_only, N_("dry run")),
65 OPT__FORCE(&force, N_("force move/rename even if target exists")),
66 OPT_BOOLEAN('k', NULL, &ignore_errors, N_("skip move/rename errors")),
c7a20c11
PH
67 OPT_END(),
68 };
11be42a4 69 const char **source, **destination, **dest_path;
ac64a722 70 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
11be42a4 71 struct stat st;
183113a5 72 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
11be42a4 73
ef90d6d4 74 git_config(git_default_config, NULL);
11be42a4 75
37782920
SB
76 argc = parse_options(argc, argv, prefix, builtin_mv_options,
77 builtin_mv_usage, 0);
c7a20c11
PH
78 if (--argc < 1)
79 usage_with_options(builtin_mv_usage, builtin_mv_options);
11be42a4 80
99caeed0
JN
81 newfd = hold_locked_index(&lock_file, 1);
82 if (read_cache() < 0)
431b049e 83 die(_("index file corrupt"));
99caeed0 84
e4d92cdc 85 source = internal_copy_pathspec(prefix, argv, argc, 0);
c7a20c11 86 modes = xcalloc(argc, sizeof(enum update_mode));
e4d92cdc 87 dest_path = internal_copy_pathspec(prefix, argv + argc, 1, 0);
11be42a4 88
c5203bdf
JS
89 if (dest_path[0][0] == '\0')
90 /* special case: "." was normalized to "" */
e4d92cdc 91 destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
c5203bdf 92 else if (!lstat(dest_path[0], &st) &&
ac64a722
JS
93 S_ISDIR(st.st_mode)) {
94 dest_path[0] = add_slash(dest_path[0]);
e4d92cdc 95 destination = internal_copy_pathspec(dest_path[0], argv, argc, 1);
ac64a722 96 } else {
c7a20c11 97 if (argc != 1)
77471646 98 die("destination '%s' is not a directory", dest_path[0]);
11be42a4
JS
99 destination = dest_path;
100 }
101
102 /* Checking */
c7a20c11 103 for (i = 0; i < argc; i++) {
60a6bf5f
JS
104 const char *src = source[i], *dst = destination[i];
105 int length, src_is_dir;
11be42a4
JS
106 const char *bad = NULL;
107
108 if (show_only)
431b049e 109 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
11be42a4 110
60a6bf5f
JS
111 length = strlen(src);
112 if (lstat(src, &st) < 0)
a7d5629f 113 bad = _("bad source");
60a6bf5f
JS
114 else if (!strncmp(src, dst, length) &&
115 (dst[length] == 0 || dst[length] == '/')) {
a7d5629f 116 bad = _("can not move directory into itself");
60a6bf5f
JS
117 } else if ((src_is_dir = S_ISDIR(st.st_mode))
118 && lstat(dst, &st) == 0)
a7d5629f 119 bad = _("cannot move directory over file");
60a6bf5f 120 else if (src_is_dir) {
11502468
JL
121 int first = cache_name_pos(src, length);
122 if (first >= 0) {
123 if (!S_ISGITLINK(active_cache[first]->ce_mode))
124 die (_("Huh? Directory %s is in index and no submodule?"), src);
125 } else {
126 const char *src_w_slash = add_slash(src);
127 int last, len_w_slash = length + 1;
128
129 modes[i] = WORKING_DIRECTORY;
130
131 first = cache_name_pos(src_w_slash, len_w_slash);
132 if (first >= 0)
133 die (_("Huh? %.*s is in index?"),
134 len_w_slash, src_w_slash);
135
136 first = -1 - first;
137 for (last = first; last < active_nr; last++) {
138 const char *path = active_cache[last]->name;
139 if (strncmp(path, src_w_slash, len_w_slash))
140 break;
ac64a722 141 }
11502468
JL
142 free((char *)src_w_slash);
143
144 if (last - first < 1)
145 bad = _("source directory is empty");
146 else {
147 int j, dst_len;
148
149 if (last - first > 0) {
150 source = xrealloc(source,
151 (argc + last - first)
152 * sizeof(char *));
153 destination = xrealloc(destination,
154 (argc + last - first)
155 * sizeof(char *));
156 modes = xrealloc(modes,
157 (argc + last - first)
158 * sizeof(enum update_mode));
159 }
160
161 dst = add_slash(dst);
162 dst_len = strlen(dst);
163
164 for (j = 0; j < last - first; j++) {
165 const char *path =
166 active_cache[first + j]->name;
167 source[argc + j] = path;
168 destination[argc + j] =
169 prefix_path(dst, dst_len,
170 path + length + 1);
171 modes[argc + j] = INDEX;
172 }
173 argc += last - first;
ac64a722 174 }
ac64a722 175 }
5aed3c6a 176 } else if (cache_name_pos(src, length) < 0)
a7d5629f 177 bad = _("not under version control");
5aed3c6a 178 else if (lstat(dst, &st) == 0) {
a7d5629f 179 bad = _("destination exists");
11be42a4
JS
180 if (force) {
181 /*
182 * only files can overwrite each other:
183 * check both source and destination
184 */
81dc2307 185 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
534376ca
JK
186 if (verbose)
187 warning(_("overwriting '%s'"), dst);
11be42a4 188 bad = NULL;
11be42a4 189 } else
a7d5629f 190 bad = _("Cannot overwrite");
11be42a4 191 }
5aed3c6a 192 } else if (string_list_has_string(&src_for_dst, dst))
a7d5629f 193 bad = _("multiple sources for the same target");
60a6bf5f 194 else
78a395d3 195 string_list_insert(&src_for_dst, dst);
11be42a4 196
11be42a4
JS
197 if (bad) {
198 if (ignore_errors) {
c7a20c11 199 if (--argc > 0) {
11be42a4 200 memmove(source + i, source + i + 1,
c7a20c11 201 (argc - i) * sizeof(char *));
11be42a4
JS
202 memmove(destination + i,
203 destination + i + 1,
c7a20c11 204 (argc - i) * sizeof(char *));
be17262d 205 i--;
11be42a4
JS
206 }
207 } else
431b049e 208 die (_("%s, source=%s, destination=%s"),
60a6bf5f 209 bad, src, dst);
11be42a4
JS
210 }
211 }
212
c7a20c11 213 for (i = 0; i < argc; i++) {
60a6bf5f
JS
214 const char *src = source[i], *dst = destination[i];
215 enum update_mode mode = modes[i];
81dc2307 216 int pos;
11be42a4 217 if (show_only || verbose)
431b049e 218 printf(_("Renaming %s to %s\n"), src, dst);
60a6bf5f
JS
219 if (!show_only && mode != INDEX &&
220 rename(src, dst) < 0 && !ignore_errors)
431b049e 221 die_errno (_("renaming '%s' failed"), src);
60a6bf5f
JS
222
223 if (mode == WORKING_DIRECTORY)
ac64a722
JS
224 continue;
225
81dc2307
PB
226 pos = cache_name_pos(src, strlen(src));
227 assert(pos >= 0);
228 if (!show_only)
229 rename_cache_entry_at(pos, dst);
11be42a4
JS
230 }
231
81dc2307
PB
232 if (active_cache_changed) {
233 if (write_cache(newfd, active_cache, active_nr) ||
234 commit_locked_index(&lock_file))
431b049e 235 die(_("Unable to write new index file"));
11be42a4
JS
236 }
237
238 return 0;
239}