@matheusbsbs / matheus-bagliano / commits / ac64a72207
Fork 0

builtin git-mv: support moving directories

This fixes the builtin mv for the test which Josef provided, and also fixes moving directories into existing directories, as noted by Jon Smirl. In case the destination exists, fail early (this cannot be overridden by -f). Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jul 26, 2006 at 19:47 UTC ac64a722072bb348476a8a029de9a82073e07fba
2 files changed +86 -5
builtin-mv.c
+82 -5
@@ -42,6 +42,18 @@ static void show_list(const char *label, struct path_list *list)
42 }
43 }
44
45 +static const char *add_slash(const char *path)
46 +{
47 + int len = strlen(path);
48 + if (path[len - 1] != '/') {
49 + char *with_slash = xmalloc(len + 2);
50 + memcpy(with_slash, path, len);
51 + strcat(with_slash + len, "/");
52 + return with_slash;
53 + }
54 + return path;
55 +}
56 +
57 static struct lock_file lock_file;
58
59 int cmd_mv(int argc, const char **argv, char **envp)
@@ -50,6 +62,7 @@ int cmd_mv(int argc, const char **argv, char **envp)
62 int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
63 const char *prefix = setup_git_directory();
64 const char **source, **destination, **dest_path;
65 + enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
66 struct stat st;
67 struct path_list overwritten = {NULL, 0, 0, 0};
68 struct path_list src_for_dst = {NULL, 0, 0, 0};
@@ -94,11 +107,14 @@ int cmd_mv(int argc, const char **argv, char **envp)
107 usage(builtin_mv_usage);
108
109 source = copy_pathspec(prefix, argv + i, count, 0);
110 + modes = xcalloc(count, sizeof(enum update_mode));
111 dest_path = copy_pathspec(prefix, argv + argc - 1, 1, 0);
112
99 - if (!lstat(dest_path[0], &st) && S_ISDIR(st.st_mode))
113 + if (!lstat(dest_path[0], &st) &&
114 + S_ISDIR(st.st_mode)) {
115 + dest_path[0] = add_slash(dest_path[0]);
116 destination = copy_pathspec(dest_path[0], argv + i, count, 1);
101 - else {
117 + } else {
118 if (count != 1)
119 usage(builtin_mv_usage);
120 destination = dest_path;
@@ -114,7 +130,64 @@ int cmd_mv(int argc, const char **argv, char **envp)
130
131 if (lstat(source[i], &st) < 0)
132 bad = "bad source";
117 - else if (lstat(destination[i], &st) == 0) {
133 +
134 + if (S_ISDIR(st.st_mode)) {
135 + const char *dir = source[i], *dest_dir = destination[i];
136 + int first, last, len = strlen(dir);
137 +
138 + if (lstat(dest_dir, &st) == 0) {
139 + bad = "cannot move directory over file";
140 + goto next;
141 + }
142 +
143 + modes[i] = WORKING_DIRECTORY;
144 +
145 + first = cache_name_pos(source[i], len);
146 + if (first >= 0)
147 + die ("Huh? %s/ is in index?", dir);
148 +
149 + first = -1 - first;
150 + for (last = first; last < active_nr; last++) {
151 + const char *path = active_cache[last]->name;
152 + if (strncmp(path, dir, len) || path[len] != '/')
153 + break;
154 + }
155 +
156 + if (last - first < 1)
157 + bad = "source directory is empty";
158 + else if (!bad) {
159 + int j, dst_len = strlen(dest_dir);
160 +
161 + if (last - first > 0) {
162 + source = realloc(source,
163 + (count + last - first)
164 + * sizeof(char *));
165 + destination = realloc(destination,
166 + (count + last - first)
167 + * sizeof(char *));
168 + modes = realloc(modes,
169 + (count + last - first)
170 + * sizeof(enum update_mode));
171 + }
172 +
173 + dest_dir = add_slash(dest_dir);
174 +
175 + for (j = 0; j < last - first; j++) {
176 + const char *path =
177 + active_cache[first + j]->name;
178 + source[count + j] = path;
179 + destination[count + j] =
180 + prefix_path(dest_dir, dst_len,
181 + path + len);
182 + modes[count + j] = INDEX;
183 + }
184 + count += last - first;
185 + }
186 +
187 + goto next;
188 + }
189 +
190 + if (!bad && lstat(destination[i], &st) == 0) {
191 bad = "destination exists";
192 if (force) {
193 /*
@@ -147,6 +220,7 @@ int cmd_mv(int argc, const char **argv, char **envp)
220 path_list_insert(destination[i], &src_for_dst);
221 }
222
223 +next:
224 if (bad) {
225 if (ignore_errors) {
226 if (--count > 0) {
@@ -157,7 +231,7 @@ int cmd_mv(int argc, const char **argv, char **envp)
231 (count - i) * sizeof(char *));
232 }
233 } else
160 - die ("Error: %s, source=%s, destination=%s",
234 + die ("%s, source=%s, destination=%s",
235 bad, source[i], destination[i]);
236 }
237 }
@@ -166,12 +240,15 @@ int cmd_mv(int argc, const char **argv, char **envp)
240 if (show_only || verbose)
241 printf("Renaming %s to %s\n",
242 source[i], destination[i]);
169 - if (!show_only &&
243 + if (!show_only && modes[i] != INDEX &&
244 rename(source[i], destination[i]) < 0 &&
245 !ignore_errors)
246 die ("renaming %s failed: %s",
247 source[i], strerror(errno));
248
249 + if (modes[i] == WORKING_DIRECTORY)
250 + continue;
251 +
252 if (cache_name_pos(source[i], strlen(source[i])) >= 0) {
253 path_list_insert(source[i], &deleted);
254
t/t7001-mv.sh
+4
@@ -74,4 +74,8 @@ test_expect_success \
74 git-diff-tree -r -M --name-status HEAD^ HEAD | \
75 grep -E "^R100.+path2/README.+path1/path2/README"'
76
77 +test_expect_failure \
78 + 'do not move directory over existing directory' \
79 + 'mkdir path0 && mkdir path0/path2 && git-mv path2 path0'
80 +
81 test_done