builtin-read-tree.c 6.8 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
#define MAX_TREES 8
17
static int nr_trees;
18
static struct tree *trees[MAX_TREES];
D
Daniel Barkalow 已提交
19 20 21

static int list_tree(unsigned char *sha1)
{
22 23
	struct tree *tree;

24 25
	if (nr_trees >= MAX_TREES)
		die("I cannot read more than %d trees", MAX_TREES);
26
	tree = parse_tree_indirect(sha1);
D
Daniel Barkalow 已提交
27 28
	if (!tree)
		return -1;
29
	trees[nr_trees++] = tree;
D
Daniel Barkalow 已提交
30 31 32
	return 0;
}

33 34
static int read_cache_unmerged(void)
{
35
	int i;
36
	struct cache_entry **dst;
37
	struct cache_entry *last = NULL;
38 39 40 41 42 43

	read_cache();
	dst = active_cache;
	for (i = 0; i < active_nr; i++) {
		struct cache_entry *ce = active_cache[i];
		if (ce_stage(ce)) {
44 45
			if (last && !strcmp(ce->name, last->name))
				continue;
46
			cache_tree_invalidate_path(active_cache_tree, ce->name);
47
			last = ce;
48
			continue;
49
		}
50
		*dst++ = ce;
51
	}
52 53
	active_nr = dst - active_cache;
	return !!last;
54 55
}

56 57
static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
{
58
	struct tree_desc desc;
59
	struct name_entry entry;
60
	int cnt;
61

62
	hashcpy(it->sha1, tree->object.sha1);
63
	init_tree_desc(&desc, tree->buffer, tree->size);
64
	cnt = 0;
65 66
	while (tree_entry(&desc, &entry)) {
		if (!S_ISDIR(entry.mode))
67 68 69
			cnt++;
		else {
			struct cache_tree_sub *sub;
70
			struct tree *subtree = lookup_tree(entry.sha1);
71 72
			if (!subtree->object.parsed)
				parse_tree(subtree);
73
			sub = cache_tree_sub(it, entry.path);
74 75 76 77 78 79 80 81 82 83
			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)
{
84
	if (!nr_trees)
85 86
		return;
	active_cache_tree = cache_tree();
87
	prime_cache_tree_rec(active_cache_tree, trees[0]);
88 89 90

}

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

93
static struct lock_file lock_file;
94

95
int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
96
{
L
Linus Torvalds 已提交
97
	int i, newfd, stage = 0;
98
	unsigned char sha1[20];
99
	struct tree_desc t[MAX_TREES];
100
	struct unpack_trees_options opts;
101

102 103
	memset(&opts, 0, sizeof(opts));
	opts.head_idx = -1;
S
Shawn Pearce 已提交
104

105
	git_config(git_default_config);
106

107
	newfd = hold_locked_index(&lock_file, 1);
108

109 110
	git_config(git_default_config);

111 112 113
	for (i = 1; i < argc; i++) {
		const char *arg = argv[i];

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

J
Junio C Hamano 已提交
122
		if (!strcmp(arg, "-v")) {
123
			opts.verbose_update = 1;
J
Junio C Hamano 已提交
124 125 126
			continue;
		}

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

135 136 137 138 139
		if (!prefixcmp(arg, "--index-output=")) {
			set_alternate_index_output(arg + 15);
			continue;
		}

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

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

L
Linus Torvalds 已提交
169
		if (!strcmp(arg, "--trivial")) {
170
			opts.trivial_merges_only = 1;
L
Linus Torvalds 已提交
171 172 173
			continue;
		}

J
Junio C Hamano 已提交
174
		if (!strcmp(arg, "--aggressive")) {
175
			opts.aggressive = 1;
J
Junio C Hamano 已提交
176 177 178
			continue;
		}

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

190
		if (!prefixcmp(arg, "--exclude-per-directory=")) {
191 192 193 194 195
			struct dir_struct *dir;

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

J
James Bowes 已提交
196
			dir = xcalloc(1, sizeof(*opts.dir));
197 198 199 200 201 202 203 204 205 206
			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;
		}

207
		/* using -u and -i at the same time makes no sense */
208
		if (1 < opts.index_only + opts.update)
209 210
			usage(read_tree_usage);

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

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

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

		if (stage - 1 >= 3)
261
			opts.head_idx = stage - 2;
D
Daniel Barkalow 已提交
262
		else
263
			opts.head_idx = 1;
D
Daniel Barkalow 已提交
264 265
	}

266 267 268 269 270 271
	for (i = 0; i < nr_trees; i++) {
		struct tree *tree = trees[i];
		parse_tree(tree);
		init_tree_desc(t+i, tree->buffer, tree->size);
	}
	unpack_trees(nr_trees, t, &opts);
272 273 274 275 276 277 278

	/*
	 * 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.
	 */
279
	if (nr_trees && !opts.prefix && (!opts.merge || (stage == 2))) {
280
		cache_tree_free(&active_cache_tree);
281 282 283
		prime_cache_tree();
	}

284
	if (write_cache(newfd, active_cache, active_nr) ||
B
Brandon Casey 已提交
285
	    commit_locked_index(&lock_file))
286
		die("unable to write new index file");
287
	return 0;
288
}