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