convert trivial cases to ALLOC_ARRAY
[git/git.git] / builtin / mv.c
CommitLineData
11be42a4
JS
1/*
2 * "git mv" builtin command
3 *
4 * Copyright (C) 2006 Johannes Schindelin
5 */
11be42a4 6#include "builtin.h"
697cc8ef 7#include "lockfile.h"
11be42a4
JS
8#include "dir.h"
9#include "cache-tree.h"
c455c87c 10#include "string-list.h"
c7a20c11 11#include "parse-options.h"
a88c915d 12#include "submodule.h"
11be42a4 13
c7a20c11 14static const char * const builtin_mv_usage[] = {
9c9b4f2f 15 N_("git mv [<options>] <source>... <destination>"),
c7a20c11
PH
16 NULL
17};
11be42a4 18
c57f6281
MM
19#define DUP_BASENAME 1
20#define KEEP_TRAILING_SLASH 2
21
e4d92cdc
NTND
22static const char **internal_copy_pathspec(const char *prefix,
23 const char **pathspec,
c57f6281 24 int count, unsigned flags)
11be42a4 25{
d78b0f3d 26 int i;
b32fa95f
JK
27 const char **result;
28 ALLOC_ARRAY(result, count + 1);
11be42a4
JS
29 memcpy(result, pathspec, count * sizeof(const char *));
30 result[count] = NULL;
d78b0f3d
JS
31 for (i = 0; i < count; i++) {
32 int length = strlen(result[i]);
af82559b 33 int to_copy = length;
c57f6281
MM
34 while (!(flags & KEEP_TRAILING_SLASH) &&
35 to_copy > 0 && is_dir_sep(result[i][to_copy - 1]))
af82559b 36 to_copy--;
c57f6281 37 if (to_copy != length || flags & DUP_BASENAME) {
af82559b 38 char *it = xmemdupz(result[i], to_copy);
c57f6281 39 if (flags & DUP_BASENAME) {
0d0ff65c
BC
40 result[i] = xstrdup(basename(it));
41 free(it);
42 } else
43 result[i] = it;
af82559b 44 }
11be42a4 45 }
971dfa19 46 return get_pathspec(prefix, result);
11be42a4
JS
47}
48
ac64a722
JS
49static const char *add_slash(const char *path)
50{
51 int len = strlen(path);
52 if (path[len - 1] != '/') {
53 char *with_slash = xmalloc(len + 2);
54 memcpy(with_slash, path, len);
329a3047
JH
55 with_slash[len++] = '/';
56 with_slash[len] = 0;
ac64a722
JS
57 return with_slash;
58 }
59 return path;
60}
61
11be42a4 62static struct lock_file lock_file;
04c1ee57 63#define SUBMODULE_WITH_GITDIR ((const char *)1)
11be42a4 64
3af05a6d
NTND
65static void prepare_move_submodule(const char *src, int first,
66 const char **submodule_gitfile)
67{
68 struct strbuf submodule_dotgit = STRBUF_INIT;
69 if (!S_ISGITLINK(active_cache[first]->ce_mode))
70 die(_("Directory %s is in index and no submodule?"), src);
71 if (!is_staging_gitmodules_ok())
72 die(_("Please stage your changes to .gitmodules or stash them to proceed"));
73 strbuf_addf(&submodule_dotgit, "%s/.git", src);
74 *submodule_gitfile = read_gitfile(submodule_dotgit.buf);
75 if (*submodule_gitfile)
76 *submodule_gitfile = xstrdup(*submodule_gitfile);
77 else
78 *submodule_gitfile = SUBMODULE_WITH_GITDIR;
79 strbuf_release(&submodule_dotgit);
80}
81
e2b6cfa0
NTND
82static int index_range_of_same_dir(const char *src, int length,
83 int *first_p, int *last_p)
84{
85 const char *src_w_slash = add_slash(src);
86 int first, last, len_w_slash = length + 1;
87
88 first = cache_name_pos(src_w_slash, len_w_slash);
89 if (first >= 0)
90 die(_("%.*s is in index"), len_w_slash, src_w_slash);
91
92 first = -1 - first;
93 for (last = first; last < active_nr; last++) {
94 const char *path = active_cache[last]->name;
95 if (strncmp(path, src_w_slash, len_w_slash))
96 break;
97 }
98 if (src_w_slash != src)
99 free((char *)src_w_slash);
100 *first_p = first;
101 *last_p = last;
102 return last - first;
103}
104
7061cf0f 105int cmd_mv(int argc, const char **argv, const char *prefix)
11be42a4 106{
03b86647 107 int i, gitmodules_modified = 0;
11be42a4 108 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
c7a20c11 109 struct option builtin_mv_options[] = {
6c54dc46
NTND
110 OPT__VERBOSE(&verbose, N_("be verbose")),
111 OPT__DRY_RUN(&show_only, N_("dry run")),
112 OPT__FORCE(&force, N_("force move/rename even if target exists")),
d5d09d47 113 OPT_BOOL('k', NULL, &ignore_errors, N_("skip move/rename errors")),
c7a20c11
PH
114 OPT_END(),
115 };
a88c915d 116 const char **source, **destination, **dest_path, **submodule_gitfile;
ac64a722 117 enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
11be42a4 118 struct stat st;
183113a5 119 struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
11be42a4 120
0656781f 121 gitmodules_config();
ef90d6d4 122 git_config(git_default_config, NULL);
11be42a4 123
37782920
SB
124 argc = parse_options(argc, argv, prefix, builtin_mv_options,
125 builtin_mv_usage, 0);
c7a20c11
PH
126 if (--argc < 1)
127 usage_with_options(builtin_mv_usage, builtin_mv_options);
11be42a4 128
03b86647 129 hold_locked_index(&lock_file, 1);
99caeed0 130 if (read_cache() < 0)
431b049e 131 die(_("index file corrupt"));
99caeed0 132
e4d92cdc 133 source = internal_copy_pathspec(prefix, argv, argc, 0);
c7a20c11 134 modes = xcalloc(argc, sizeof(enum update_mode));
c57f6281
MM
135 /*
136 * Keep trailing slash, needed to let
137 * "git mv file no-such-dir/" error out.
138 */
139 dest_path = internal_copy_pathspec(prefix, argv + argc, 1,
140 KEEP_TRAILING_SLASH);
a88c915d 141 submodule_gitfile = xcalloc(argc, sizeof(char *));
11be42a4 142
c5203bdf
JS
143 if (dest_path[0][0] == '\0')
144 /* special case: "." was normalized to "" */
c57f6281 145 destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
c5203bdf 146 else if (!lstat(dest_path[0], &st) &&
ac64a722
JS
147 S_ISDIR(st.st_mode)) {
148 dest_path[0] = add_slash(dest_path[0]);
c57f6281 149 destination = internal_copy_pathspec(dest_path[0], argv, argc, DUP_BASENAME);
ac64a722 150 } else {
c7a20c11 151 if (argc != 1)
eac0ccc2 152 die(_("destination '%s' is not a directory"), dest_path[0]);
11be42a4
JS
153 destination = dest_path;
154 }
155
156 /* Checking */
c7a20c11 157 for (i = 0; i < argc; i++) {
60a6bf5f
JS
158 const char *src = source[i], *dst = destination[i];
159 int length, src_is_dir;
11be42a4
JS
160 const char *bad = NULL;
161
162 if (show_only)
431b049e 163 printf(_("Checking rename of '%s' to '%s'\n"), src, dst);
11be42a4 164
60a6bf5f
JS
165 length = strlen(src);
166 if (lstat(src, &st) < 0)
a7d5629f 167 bad = _("bad source");
60a6bf5f
JS
168 else if (!strncmp(src, dst, length) &&
169 (dst[length] == 0 || dst[length] == '/')) {
a7d5629f 170 bad = _("can not move directory into itself");
60a6bf5f
JS
171 } else if ((src_is_dir = S_ISDIR(st.st_mode))
172 && lstat(dst, &st) == 0)
a7d5629f 173 bad = _("cannot move directory over file");
60a6bf5f 174 else if (src_is_dir) {
b46b15de 175 int first = cache_name_pos(src, length), last;
3af05a6d
NTND
176
177 if (first >= 0)
178 prepare_move_submodule(src, first,
179 submodule_gitfile + i);
b46b15de
NTND
180 else if (index_range_of_same_dir(src, length,
181 &first, &last) < 1)
182 bad = _("source directory is empty");
183 else { /* last - first >= 1 */
184 int j, dst_len, n;
11502468
JL
185
186 modes[i] = WORKING_DIRECTORY;
b46b15de 187 n = argc + last - first;
2756ca43
RS
188 REALLOC_ARRAY(source, n);
189 REALLOC_ARRAY(destination, n);
190 REALLOC_ARRAY(modes, n);
191 REALLOC_ARRAY(submodule_gitfile, n);
b46b15de
NTND
192
193 dst = add_slash(dst);
194 dst_len = strlen(dst);
195
196 for (j = 0; j < last - first; j++) {
197 const char *path = active_cache[first + j]->name;
198 source[argc + j] = path;
199 destination[argc + j] =
200 prefix_path(dst, dst_len, path + length + 1);
201 modes[argc + j] = INDEX;
202 submodule_gitfile[argc + j] = NULL;
ac64a722 203 }
b46b15de 204 argc += last - first;
ac64a722 205 }
5aed3c6a 206 } else if (cache_name_pos(src, length) < 0)
a7d5629f 207 bad = _("not under version control");
baa37bff
DT
208 else if (lstat(dst, &st) == 0 &&
209 (!ignore_case || strcasecmp(src, dst))) {
a7d5629f 210 bad = _("destination exists");
11be42a4
JS
211 if (force) {
212 /*
213 * only files can overwrite each other:
214 * check both source and destination
215 */
81dc2307 216 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
534376ca
JK
217 if (verbose)
218 warning(_("overwriting '%s'"), dst);
11be42a4 219 bad = NULL;
11be42a4 220 } else
a7d5629f 221 bad = _("Cannot overwrite");
11be42a4 222 }
5aed3c6a 223 } else if (string_list_has_string(&src_for_dst, dst))
a7d5629f 224 bad = _("multiple sources for the same target");
a8933469
JS
225 else if (is_dir_sep(dst[strlen(dst) - 1]))
226 bad = _("destination directory does not exist");
60a6bf5f 227 else
78a395d3 228 string_list_insert(&src_for_dst, dst);
11be42a4 229
ad1a19d0
NTND
230 if (!bad)
231 continue;
232 if (!ignore_errors)
4f1bbd23 233 die(_("%s, source=%s, destination=%s"),
ad1a19d0
NTND
234 bad, src, dst);
235 if (--argc > 0) {
236 int n = argc - i;
237 memmove(source + i, source + i + 1,
238 n * sizeof(char *));
239 memmove(destination + i, destination + i + 1,
240 n * sizeof(char *));
241 memmove(modes + i, modes + i + 1,
242 n * sizeof(enum update_mode));
243 memmove(submodule_gitfile + i, submodule_gitfile + i + 1,
244 n * sizeof(char *));
245 i--;
11be42a4
JS
246 }
247 }
248
c7a20c11 249 for (i = 0; i < argc; i++) {
60a6bf5f
JS
250 const char *src = source[i], *dst = destination[i];
251 enum update_mode mode = modes[i];
81dc2307 252 int pos;
11be42a4 253 if (show_only || verbose)
431b049e 254 printf(_("Renaming %s to %s\n"), src, dst);
a88c915d
JL
255 if (!show_only && mode != INDEX) {
256 if (rename(src, dst) < 0 && !ignore_errors)
4f1bbd23 257 die_errno(_("renaming '%s' failed"), src);
04c1ee57
JL
258 if (submodule_gitfile[i]) {
259 if (submodule_gitfile[i] != SUBMODULE_WITH_GITDIR)
260 connect_work_tree_and_git_dir(dst, submodule_gitfile[i]);
261 if (!update_path_in_gitmodules(src, dst))
262 gitmodules_modified = 1;
263 }
a88c915d 264 }
60a6bf5f
JS
265
266 if (mode == WORKING_DIRECTORY)
ac64a722
JS
267 continue;
268
81dc2307
PB
269 pos = cache_name_pos(src, strlen(src));
270 assert(pos >= 0);
271 if (!show_only)
272 rename_cache_entry_at(pos, dst);
11be42a4
JS
273 }
274
0656781f
JL
275 if (gitmodules_modified)
276 stage_updated_gitmodules();
277
dcadc8b8
NTND
278 if (active_cache_changed &&
279 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
280 die(_("Unable to write new index file"));
11be42a4
JS
281
282 return 0;
283}