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