builtin-mv.c 7.3 KB
Newer Older
J
Johannes Schindelin 已提交
1 2 3 4 5 6 7 8 9
/*
 * "git mv" builtin command
 *
 * Copyright (C) 2006 Johannes Schindelin
 */
#include "cache.h"
#include "builtin.h"
#include "dir.h"
#include "cache-tree.h"
10
#include "string-list.h"
11
#include "parse-options.h"
J
Johannes Schindelin 已提交
12

13
static const char * const builtin_mv_usage[] = {
S
Stephan Beyer 已提交
14
	"git mv [options] <source>... <destination>",
15 16
	NULL
};
J
Johannes Schindelin 已提交
17 18 19 20

static const char **copy_pathspec(const char *prefix, const char **pathspec,
				  int count, int base_name)
{
21
	int i;
J
Johannes Schindelin 已提交
22 23 24
	const char **result = xmalloc((count + 1) * sizeof(const char *));
	memcpy(result, pathspec, count * sizeof(const char *));
	result[count] = NULL;
25 26 27
	for (i = 0; i < count; i++) {
		int length = strlen(result[i]);
		if (length > 0 && result[i][length - 1] == '/') {
P
Pierre Habouzit 已提交
28
			result[i] = xmemdupz(result[i], length - 1);
29 30
		}
		if (base_name) {
J
Johannes Schindelin 已提交
31 32 33 34 35
			const char *last_slash = strrchr(result[i], '/');
			if (last_slash)
				result[i] = last_slash + 1;
		}
	}
36
	return get_pathspec(prefix, result);
J
Johannes Schindelin 已提交
37 38
}

39
static void show_list(const char *label, struct string_list *list)
J
Johannes Schindelin 已提交
40 41 42 43 44
{
	if (list->nr > 0) {
		int i;
		printf("%s", label);
		for (i = 0; i < list->nr; i++)
45 46
			printf("%s%s", i > 0 ? ", " : "",
					list->items[i].string);
J
Johannes Schindelin 已提交
47 48 49 50
		putchar('\n');
	}
}

51 52 53 54 55 56
static const char *add_slash(const char *path)
{
	int len = strlen(path);
	if (path[len - 1] != '/') {
		char *with_slash = xmalloc(len + 2);
		memcpy(with_slash, path, len);
57 58
		with_slash[len++] = '/';
		with_slash[len] = 0;
59 60 61 62 63
		return with_slash;
	}
	return path;
}

J
Johannes Schindelin 已提交
64 65
static struct lock_file lock_file;

66
int cmd_mv(int argc, const char **argv, const char *prefix)
J
Johannes Schindelin 已提交
67
{
68
	int i, newfd;
J
Johannes Schindelin 已提交
69
	int verbose = 0, show_only = 0, force = 0, ignore_errors = 0;
70 71 72 73 74 75
	struct option builtin_mv_options[] = {
		OPT__DRY_RUN(&show_only),
		OPT_BOOLEAN('f', NULL, &force, "force move/rename even if target exists"),
		OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"),
		OPT_END(),
	};
J
Johannes Schindelin 已提交
76
	const char **source, **destination, **dest_path;
77
	enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes;
J
Johannes Schindelin 已提交
78
	struct stat st;
79 80 81 82 83
	struct string_list overwritten = {NULL, 0, 0, 0};
	struct string_list src_for_dst = {NULL, 0, 0, 0};
	struct string_list added = {NULL, 0, 0, 0};
	struct string_list deleted = {NULL, 0, 0, 0};
	struct string_list changed = {NULL, 0, 0, 0};
J
Johannes Schindelin 已提交
84

85
	git_config(git_default_config, NULL);
J
Johannes Schindelin 已提交
86

87
	newfd = hold_locked_index(&lock_file, 1);
J
Johannes Schindelin 已提交
88 89 90
	if (read_cache() < 0)
		die("index file corrupt");

91 92 93
	argc = parse_options(argc, argv, builtin_mv_options, builtin_mv_usage, 0);
	if (--argc < 1)
		usage_with_options(builtin_mv_usage, builtin_mv_options);
J
Johannes Schindelin 已提交
94

95 96 97
	source = copy_pathspec(prefix, argv, argc, 0);
	modes = xcalloc(argc, sizeof(enum update_mode));
	dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
J
Johannes Schindelin 已提交
98

99 100
	if (dest_path[0][0] == '\0')
		/* special case: "." was normalized to "" */
101
		destination = copy_pathspec(dest_path[0], argv, argc, 1);
102
	else if (!lstat(dest_path[0], &st) &&
103 104
			S_ISDIR(st.st_mode)) {
		dest_path[0] = add_slash(dest_path[0]);
105
		destination = copy_pathspec(dest_path[0], argv, argc, 1);
106
	} else {
107 108
		if (argc != 1)
			usage_with_options(builtin_mv_usage, builtin_mv_options);
J
Johannes Schindelin 已提交
109 110 111 112
		destination = dest_path;
	}

	/* Checking */
113
	for (i = 0; i < argc; i++) {
114 115
		const char *src = source[i], *dst = destination[i];
		int length, src_is_dir;
J
Johannes Schindelin 已提交
116 117 118
		const char *bad = NULL;

		if (show_only)
119
			printf("Checking rename of '%s' to '%s'\n", src, dst);
J
Johannes Schindelin 已提交
120

121 122
		length = strlen(src);
		if (lstat(src, &st) < 0)
J
Johannes Schindelin 已提交
123
			bad = "bad source";
124 125
		else if (!strncmp(src, dst, length) &&
				(dst[length] == 0 || dst[length] == '/')) {
126
			bad = "can not move directory into itself";
127 128 129 130
		} else if ((src_is_dir = S_ISDIR(st.st_mode))
				&& lstat(dst, &st) == 0)
			bad = "cannot move directory over file";
		else if (src_is_dir) {
131 132
			const char *src_w_slash = add_slash(src);
			int len_w_slash = length + 1;
133
			int first, last;
134 135 136

			modes[i] = WORKING_DIRECTORY;

137
			first = cache_name_pos(src_w_slash, len_w_slash);
138
			if (first >= 0)
139 140
				die ("Huh? %.*s is in index?",
						len_w_slash, src_w_slash);
141 142 143 144

			first = -1 - first;
			for (last = first; last < active_nr; last++) {
				const char *path = active_cache[last]->name;
145
				if (strncmp(path, src_w_slash, len_w_slash))
146 147
					break;
			}
148
			free((char *)src_w_slash);
149 150 151

			if (last - first < 1)
				bad = "source directory is empty";
152 153
			else {
				int j, dst_len;
154 155

				if (last - first > 0) {
J
Jonas Fonseca 已提交
156
					source = xrealloc(source,
157
							(argc + last - first)
158
							* sizeof(char *));
J
Jonas Fonseca 已提交
159
					destination = xrealloc(destination,
160
							(argc + last - first)
161
							* sizeof(char *));
J
Jonas Fonseca 已提交
162
					modes = xrealloc(modes,
163
							(argc + last - first)
164 165 166
							* sizeof(enum update_mode));
				}

167
				dst = add_slash(dst);
168
				dst_len = strlen(dst);
169 170 171 172

				for (j = 0; j < last - first; j++) {
					const char *path =
						active_cache[first + j]->name;
173 174
					source[argc + j] = path;
					destination[argc + j] =
175
						prefix_path(dst, dst_len,
176
							path + length + 1);
177
					modes[argc + j] = INDEX;
178
				}
179
				argc += last - first;
180
			}
181
		} else if (lstat(dst, &st) == 0) {
J
Johannes Schindelin 已提交
182 183 184 185 186 187 188 189 190 191 192
			bad = "destination exists";
			if (force) {
				/*
				 * only files can overwrite each other:
				 * check both source and destination
				 */
				if (S_ISREG(st.st_mode)) {
					fprintf(stderr, "Warning: %s;"
							" will overwrite!\n",
							bad);
					bad = NULL;
193
					string_list_insert(dst, &overwritten);
J
Johannes Schindelin 已提交
194 195 196
				} else
					bad = "Cannot overwrite";
			}
197
		} else if (cache_name_pos(src, length) < 0)
J
Johannes Schindelin 已提交
198
			bad = "not under version control";
199
		else if (string_list_has_string(&src_for_dst, dst))
200 201
			bad = "multiple sources for the same target";
		else
202
			string_list_insert(dst, &src_for_dst);
J
Johannes Schindelin 已提交
203 204 205

		if (bad) {
			if (ignore_errors) {
206
				if (--argc > 0) {
J
Johannes Schindelin 已提交
207
					memmove(source + i, source + i + 1,
208
						(argc - i) * sizeof(char *));
J
Johannes Schindelin 已提交
209 210
					memmove(destination + i,
						destination + i + 1,
211
						(argc - i) * sizeof(char *));
J
Johannes Schindelin 已提交
212 213
				}
			} else
214
				die ("%s, source=%s, destination=%s",
215
				     bad, src, dst);
J
Johannes Schindelin 已提交
216 217 218
		}
	}

219
	for (i = 0; i < argc; i++) {
220 221
		const char *src = source[i], *dst = destination[i];
		enum update_mode mode = modes[i];
J
Johannes Schindelin 已提交
222
		if (show_only || verbose)
223 224 225 226 227 228
			printf("Renaming %s to %s\n", src, dst);
		if (!show_only && mode != INDEX &&
				rename(src, dst) < 0 && !ignore_errors)
			die ("renaming %s failed: %s", src, strerror(errno));

		if (mode == WORKING_DIRECTORY)
229 230
			continue;

231
		if (cache_name_pos(src, strlen(src)) >= 0) {
232
			string_list_insert(src, &deleted);
J
Johannes Schindelin 已提交
233 234

			/* destination can be a directory with 1 file inside */
235 236
			if (string_list_has_string(&overwritten, dst))
				string_list_insert(dst, &changed);
J
Johannes Schindelin 已提交
237
			else
238
				string_list_insert(dst, &added);
J
Johannes Schindelin 已提交
239
		} else
240
			string_list_insert(dst, &added);
J
Johannes Schindelin 已提交
241 242
	}

243
	if (show_only) {
J
Johannes Schindelin 已提交
244 245 246 247 248
		show_list("Changed  : ", &changed);
		show_list("Adding   : ", &added);
		show_list("Deleting : ", &deleted);
	} else {
		for (i = 0; i < changed.nr; i++) {
249
			const char *path = changed.items[i].string;
250 251
			int j = cache_name_pos(path, strlen(path));
			struct cache_entry *ce = active_cache[j];
J
Johannes Schindelin 已提交
252

253
			if (j < 0)
J
Johannes Schindelin 已提交
254 255 256 257 258
				die ("Huh? Cache entry for %s unknown?", path);
			refresh_cache_entry(ce, 0);
		}

		for (i = 0; i < added.nr; i++) {
259
			const char *path = added.items[i].string;
J
Junio C Hamano 已提交
260
			if (add_file_to_cache(path, verbose ? ADD_CACHE_VERBOSE : 0))
261
				die("updating index entries failed");
J
Johannes Schindelin 已提交
262 263
		}

J
Junio C Hamano 已提交
264
		for (i = 0; i < deleted.nr; i++)
265
			remove_file_from_cache(deleted.items[i].string);
J
Johannes Schindelin 已提交
266 267 268

		if (active_cache_changed) {
			if (write_cache(newfd, active_cache, active_nr) ||
269
			    commit_locked_index(&lock_file))
J
Johannes Schindelin 已提交
270 271 272 273 274 275
				die("Unable to write new index file");
		}
	}

	return 0;
}