builtin-read-tree.c 6.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * GIT - The information manager from hell
 *
 * Copyright (C) Linus Torvalds, 2005
 */
6

7
#include "cache.h"
D
Daniel Barkalow 已提交
8 9
#include "object.h"
#include "tree.h"
10
#include "tree-walk.h"
11
#include "cache-tree.h"
12
#include "unpack-trees.h"
13
#include "dir.h"
P
Peter Eriksen 已提交
14
#include "builtin.h"
D
Daniel Barkalow 已提交
15

16
static struct object_list *trees;
D
Daniel Barkalow 已提交
17 18 19 20 21 22 23 24 25 26

static int list_tree(unsigned char *sha1)
{
	struct tree *tree = parse_tree_indirect(sha1);
	if (!tree)
		return -1;
	object_list_append(&tree->object, &trees);
	return 0;
}

27 28
static int read_cache_unmerged(void)
{
29
	int i;
30
	struct cache_entry **dst;
31
	struct cache_entry *last = NULL;
32 33 34 35 36 37

	read_cache();
	dst = active_cache;
	for (i = 0; i < active_nr; i++) {
		struct cache_entry *ce = active_cache[i];
		if (ce_stage(ce)) {
38 39
			if (last && !strcmp(ce->name, last->name))
				continue;
40
			cache_tree_invalidate_path(active_cache_tree, ce->name);
41 42 43
			last = ce;
			ce->ce_mode = 0;
			ce->ce_flags &= ~htons(CE_STAGEMASK);
44
		}
45
		*dst++ = ce;
46
	}
47 48
	active_nr = dst - active_cache;
	return !!last;
49 50
}

51 52
static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
{
53
	struct tree_desc desc;
54
	struct name_entry entry;
55
	int cnt;
56

57
	hashcpy(it->sha1, tree->object.sha1);
58
	init_tree_desc(&desc, tree->buffer, tree->size);
59
	cnt = 0;
60 61
	while (tree_entry(&desc, &entry)) {
		if (!S_ISDIR(entry.mode))
62 63 64
			cnt++;
		else {
			struct cache_tree_sub *sub;
65
			struct tree *subtree = lookup_tree(entry.sha1);
66 67
			if (!subtree->object.parsed)
				parse_tree(subtree);
68
			sub = cache_tree_sub(it, entry.path);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
			sub->cache_tree = cache_tree();
			prime_cache_tree_rec(sub->cache_tree, subtree);
			cnt += sub->cache_tree->entry_count;
		}
	}
	it->entry_count = cnt;
}

static void prime_cache_tree(void)
{
	struct tree *tree = (struct tree *)trees->item;
	if (!tree)
		return;
	active_cache_tree = cache_tree();
	prime_cache_tree_rec(active_cache_tree, tree);

}

87
static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <sha1> [<sha2> [<sha3>]])";
J
Junio C Hamano 已提交
88

89
static struct lock_file lock_file;
90

91
int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
92
{
L
Linus Torvalds 已提交
93
	int i, newfd, stage = 0;
94
	unsigned char sha1[20];
95
	struct unpack_trees_options opts;
96

97 98
	memset(&opts, 0, sizeof(opts));
	opts.head_idx = -1;
S
Shawn Pearce 已提交
99

100
	setup_git_directory();
101
	git_config(git_default_config);
102

103
	newfd = hold_locked_index(&lock_file, 1);
104

105 106
	git_config(git_default_config);

107 108 109
	for (i = 1; i < argc; i++) {
		const char *arg = argv[i];

110 111 112
		/* "-u" means "update", meaning that a merge will update
		 * the working tree.
		 */
113
		if (!strcmp(arg, "-u")) {
114
			opts.update = 1;
115 116 117
			continue;
		}

J
Junio C Hamano 已提交
118
		if (!strcmp(arg, "-v")) {
119
			opts.verbose_update = 1;
J
Junio C Hamano 已提交
120 121 122
			continue;
		}

123 124 125 126
		/* "-i" means "index only", meaning that a merge will
		 * not even look at the working tree.
		 */
		if (!strcmp(arg, "-i")) {
127
			opts.index_only = 1;
128 129 130
			continue;
		}

131 132 133 134 135
		if (!prefixcmp(arg, "--index-output=")) {
			set_alternate_index_output(arg + 15);
			continue;
		}

136 137 138 139
		/* "--prefix=<subdirectory>/" means keep the current index
		 *  entries and put the entries from the tree under the
		 * given subdirectory.
		 */
140
		if (!prefixcmp(arg, "--prefix=")) {
141
			if (stage || opts.merge || opts.prefix)
142
				usage(read_tree_usage);
143 144
			opts.prefix = arg + 9;
			opts.merge = 1;
145 146 147 148 149 150
			stage = 1;
			if (read_cache_unmerged())
				die("you need to resolve your current index first");
			continue;
		}

151 152 153 154
		/* This differs from "-m" in that we'll silently ignore
		 * unmerged entries and overwrite working tree files that
		 * correspond to them.
		 */
155
		if (!strcmp(arg, "--reset")) {
156
			if (stage || opts.merge || opts.prefix)
157
				usage(read_tree_usage);
158 159
			opts.reset = 1;
			opts.merge = 1;
160 161
			stage = 1;
			read_cache_unmerged();
162
			continue;
163 164
		}

L
Linus Torvalds 已提交
165
		if (!strcmp(arg, "--trivial")) {
166
			opts.trivial_merges_only = 1;
L
Linus Torvalds 已提交
167 168 169
			continue;
		}

J
Junio C Hamano 已提交
170
		if (!strcmp(arg, "--aggressive")) {
171
			opts.aggressive = 1;
J
Junio C Hamano 已提交
172 173 174
			continue;
		}

175
		/* "-m" stands for "merge", meaning we start in stage 1 */
176
		if (!strcmp(arg, "-m")) {
177
			if (stage || opts.merge || opts.prefix)
178 179 180
				usage(read_tree_usage);
			if (read_cache_unmerged())
				die("you need to resolve your current index first");
181
			stage = 1;
182
			opts.merge = 1;
183 184
			continue;
		}
J
Junio C Hamano 已提交
185

186
		if (!prefixcmp(arg, "--exclude-per-directory=")) {
187 188 189 190 191
			struct dir_struct *dir;

			if (opts.dir)
				die("more than one --exclude-per-directory are given.");

J
James Bowes 已提交
192
			dir = xcalloc(1, sizeof(*opts.dir));
193 194 195 196 197 198 199 200 201 202
			dir->show_ignored = 1;
			dir->exclude_per_dir = arg + 24;
			opts.dir = dir;
			/* We do not need to nor want to do read-directory
			 * here; we are merely interested in reusing the
			 * per directory ignore stack mechanism.
			 */
			continue;
		}

203
		/* using -u and -i at the same time makes no sense */
204
		if (1 < opts.index_only + opts.update)
205 206
			usage(read_tree_usage);

207 208
		if (get_sha1(arg, sha1))
			die("Not a valid object name %s", arg);
D
Daniel Barkalow 已提交
209
		if (list_tree(sha1) < 0)
210
			die("failed to unpack tree object %s", arg);
211
		stage++;
212
	}
213
	if ((opts.update||opts.index_only) && !opts.merge)
214
		usage(read_tree_usage);
215 216
	if ((opts.dir && !opts.update))
		die("--exclude-per-directory is meaningless unless -u");
J
Junio C Hamano 已提交
217

218 219
	if (opts.prefix) {
		int pfxlen = strlen(opts.prefix);
220
		int pos;
221
		if (opts.prefix[pfxlen-1] != '/')
222 223 224
			die("prefix must end with /");
		if (stage != 2)
			die("binding merge takes only one tree");
225
		pos = cache_name_pos(opts.prefix, pfxlen);
226 227 228 229
		if (0 <= pos)
			die("corrupt index file");
		pos = -pos-1;
		if (pos < active_nr &&
230 231 232
		    !strncmp(active_cache[pos]->name, opts.prefix, pfxlen))
			die("subdirectory '%s' already exists.", opts.prefix);
		pos = cache_name_pos(opts.prefix, pfxlen-1);
233
		if (0 <= pos)
234 235
			die("file '%.*s' already exists.",
					pfxlen-1, opts.prefix);
J
Junio C Hamano 已提交
236
		opts.pos = -1 - pos;
237 238
	}

239
	if (opts.merge) {
D
Daniel Barkalow 已提交
240
		if (stage < 2)
241
			die("just how do you expect me to merge %d trees?", stage-1);
D
Daniel Barkalow 已提交
242 243
		switch (stage - 1) {
		case 1:
244
			opts.fn = opts.prefix ? bind_merge : oneway_merge;
D
Daniel Barkalow 已提交
245 246
			break;
		case 2:
247
			opts.fn = twoway_merge;
D
Daniel Barkalow 已提交
248 249 250
			break;
		case 3:
		default:
251
			opts.fn = threeway_merge;
252
			cache_tree_free(&active_cache_tree);
D
Daniel Barkalow 已提交
253
			break;
J
Junio C Hamano 已提交
254
		}
D
Daniel Barkalow 已提交
255 256

		if (stage - 1 >= 3)
257
			opts.head_idx = stage - 2;
D
Daniel Barkalow 已提交
258
		else
259
			opts.head_idx = 1;
D
Daniel Barkalow 已提交
260 261
	}

262
	unpack_trees(trees, &opts);
263 264 265 266 267 268 269

	/*
	 * When reading only one tree (either the most basic form,
	 * "-m ent" or "--reset ent" form), we can obtain a fully
	 * valid cache-tree because the index must match exactly
	 * what came from the tree.
	 */
270
	if (trees && trees->item && !opts.prefix && (!opts.merge || (stage == 2))) {
271
		cache_tree_free(&active_cache_tree);
272 273 274
		prime_cache_tree();
	}

275
	if (write_cache(newfd, active_cache, active_nr) ||
276
	    close(newfd) || commit_locked_index(&lock_file))
277
		die("unable to write new index file");
278
	return 0;
279
}