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

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

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

32 33
static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
{
34
	struct tree_desc desc;
35
	struct name_entry entry;
36
	int cnt;
37

38
	hashcpy(it->sha1, tree->object.sha1);
39
	init_tree_desc(&desc, tree->buffer, tree->size);
40
	cnt = 0;
41 42
	while (tree_entry(&desc, &entry)) {
		if (!S_ISDIR(entry.mode))
43 44 45
			cnt++;
		else {
			struct cache_tree_sub *sub;
46
			struct tree *subtree = lookup_tree(entry.sha1);
47 48
			if (!subtree->object.parsed)
				parse_tree(subtree);
49
			sub = cache_tree_sub(it, entry.path);
50 51 52 53 54 55 56 57 58 59
			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)
{
60
	if (!nr_trees)
61 62
		return;
	active_cache_tree = cache_tree();
63
	prime_cache_tree_rec(active_cache_tree, trees[0]);
64 65 66

}

67
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 已提交
68

69
static struct lock_file lock_file;
70

71
int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
72
{
L
Linus Torvalds 已提交
73
	int i, newfd, stage = 0;
74
	unsigned char sha1[20];
75
	struct tree_desc t[MAX_UNPACK_TREES];
76
	struct unpack_trees_options opts;
77

78 79
	memset(&opts, 0, sizeof(opts));
	opts.head_idx = -1;
80 81
	opts.src_index = &the_index;
	opts.dst_index = &the_index;
S
Shawn Pearce 已提交
82

83
	git_config(git_default_config, NULL);
84

85
	newfd = hold_locked_index(&lock_file, 1);
86 87 88 89

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

90 91 92
		/* "-u" means "update", meaning that a merge will update
		 * the working tree.
		 */
93
		if (!strcmp(arg, "-u")) {
94
			opts.update = 1;
95 96 97
			continue;
		}

J
Junio C Hamano 已提交
98
		if (!strcmp(arg, "-v")) {
99
			opts.verbose_update = 1;
J
Junio C Hamano 已提交
100 101 102
			continue;
		}

103 104 105 106
		/* "-i" means "index only", meaning that a merge will
		 * not even look at the working tree.
		 */
		if (!strcmp(arg, "-i")) {
107
			opts.index_only = 1;
108 109 110
			continue;
		}

111 112 113 114 115
		if (!prefixcmp(arg, "--index-output=")) {
			set_alternate_index_output(arg + 15);
			continue;
		}

116 117 118 119
		/* "--prefix=<subdirectory>/" means keep the current index
		 *  entries and put the entries from the tree under the
		 * given subdirectory.
		 */
120
		if (!prefixcmp(arg, "--prefix=")) {
121
			if (stage || opts.merge || opts.prefix)
122
				usage(read_tree_usage);
123 124
			opts.prefix = arg + 9;
			opts.merge = 1;
125 126 127 128 129 130
			stage = 1;
			if (read_cache_unmerged())
				die("you need to resolve your current index first");
			continue;
		}

131 132 133 134
		/* This differs from "-m" in that we'll silently ignore
		 * unmerged entries and overwrite working tree files that
		 * correspond to them.
		 */
135
		if (!strcmp(arg, "--reset")) {
136
			if (stage || opts.merge || opts.prefix)
137
				usage(read_tree_usage);
138 139
			opts.reset = 1;
			opts.merge = 1;
140 141
			stage = 1;
			read_cache_unmerged();
142
			continue;
143 144
		}

L
Linus Torvalds 已提交
145
		if (!strcmp(arg, "--trivial")) {
146
			opts.trivial_merges_only = 1;
L
Linus Torvalds 已提交
147 148 149
			continue;
		}

J
Junio C Hamano 已提交
150
		if (!strcmp(arg, "--aggressive")) {
151
			opts.aggressive = 1;
J
Junio C Hamano 已提交
152 153 154
			continue;
		}

155
		/* "-m" stands for "merge", meaning we start in stage 1 */
156
		if (!strcmp(arg, "-m")) {
157
			if (stage || opts.merge || opts.prefix)
158 159 160
				usage(read_tree_usage);
			if (read_cache_unmerged())
				die("you need to resolve your current index first");
161
			stage = 1;
162
			opts.merge = 1;
163 164
			continue;
		}
J
Junio C Hamano 已提交
165

166
		if (!prefixcmp(arg, "--exclude-per-directory=")) {
167 168 169 170 171
			struct dir_struct *dir;

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

J
James Bowes 已提交
172
			dir = xcalloc(1, sizeof(*opts.dir));
173 174 175 176 177 178 179 180 181 182
			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;
		}

183
		/* using -u and -i at the same time makes no sense */
184
		if (1 < opts.index_only + opts.update)
185 186
			usage(read_tree_usage);

187 188
		if (get_sha1(arg, sha1))
			die("Not a valid object name %s", arg);
D
Daniel Barkalow 已提交
189
		if (list_tree(sha1) < 0)
190
			die("failed to unpack tree object %s", arg);
191
		stage++;
192
	}
193
	if ((opts.update||opts.index_only) && !opts.merge)
194
		usage(read_tree_usage);
195 196
	if ((opts.dir && !opts.update))
		die("--exclude-per-directory is meaningless unless -u");
197 198
	if (opts.merge && !opts.index_only)
		setup_work_tree();
J
Junio C Hamano 已提交
199

200
	if (opts.merge) {
D
Daniel Barkalow 已提交
201
		if (stage < 2)
202
			die("just how do you expect me to merge %d trees?", stage-1);
D
Daniel Barkalow 已提交
203 204
		switch (stage - 1) {
		case 1:
205
			opts.fn = opts.prefix ? bind_merge : oneway_merge;
D
Daniel Barkalow 已提交
206 207
			break;
		case 2:
208
			opts.fn = twoway_merge;
209
			opts.initial_checkout = !active_nr;
D
Daniel Barkalow 已提交
210 211 212
			break;
		case 3:
		default:
213
			opts.fn = threeway_merge;
214
			cache_tree_free(&active_cache_tree);
D
Daniel Barkalow 已提交
215
			break;
J
Junio C Hamano 已提交
216
		}
D
Daniel Barkalow 已提交
217 218

		if (stage - 1 >= 3)
219
			opts.head_idx = stage - 2;
D
Daniel Barkalow 已提交
220
		else
221
			opts.head_idx = 1;
D
Daniel Barkalow 已提交
222 223
	}

224 225 226 227 228
	for (i = 0; i < nr_trees; i++) {
		struct tree *tree = trees[i];
		parse_tree(tree);
		init_tree_desc(t+i, tree->buffer, tree->size);
	}
229 230
	if (unpack_trees(nr_trees, t, &opts))
		return 128;
231 232 233 234 235 236 237

	/*
	 * 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.
	 */
238
	if (nr_trees && !opts.prefix && (!opts.merge || (stage == 2))) {
239
		cache_tree_free(&active_cache_tree);
240 241 242
		prime_cache_tree();
	}

243
	if (write_cache(newfd, active_cache, active_nr) ||
B
Brandon Casey 已提交
244
	    commit_locked_index(&lock_file))
245
		die("unable to write new index file");
246
	return 0;
247
}