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