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