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