1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include "cache.h"
8 #include "object.h"
9 #include "tree.h"
10 #include "tree-walk.h"
11 #include "cache-tree.h"
12 #include "unpack-trees.h"
13 #include "dir.h"
14 #include "builtin.h"
16 static struct object_list *trees;
18 static int list_tree(unsigned char *sha1)
19 {
20 struct tree *tree = parse_tree_indirect(sha1);
21 if (!tree)
22 return -1;
23 object_list_append(&tree->object, &trees);
24 return 0;
25 }
27 static int read_cache_unmerged(void)
28 {
29 int i;
30 struct cache_entry **dst;
31 struct cache_entry *last = NULL;
33 read_cache();
34 dst = active_cache;
35 for (i = 0; i < active_nr; i++) {
36 struct cache_entry *ce = active_cache[i];
37 if (ce_stage(ce)) {
38 if (last && !strcmp(ce->name, last->name))
39 continue;
40 cache_tree_invalidate_path(active_cache_tree, ce->name);
41 last = ce;
42 ce->ce_mode = 0;
43 ce->ce_flags &= ~htons(CE_STAGEMASK);
44 }
45 *dst++ = ce;
46 }
47 active_nr = dst - active_cache;
48 return !!last;
49 }
51 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
52 {
53 struct tree_desc desc;
54 struct name_entry entry;
55 int cnt;
57 hashcpy(it->sha1, tree->object.sha1);
58 desc.buf = tree->buffer;
59 desc.size = tree->size;
60 cnt = 0;
61 while (tree_entry(&desc, &entry)) {
62 if (!S_ISDIR(entry.mode))
63 cnt++;
64 else {
65 struct cache_tree_sub *sub;
66 struct tree *subtree = lookup_tree(entry.sha1);
67 if (!subtree->object.parsed)
68 parse_tree(subtree);
69 sub = cache_tree_sub(it, entry.path);
70 sub->cache_tree = cache_tree();
71 prime_cache_tree_rec(sub->cache_tree, subtree);
72 cnt += sub->cache_tree->entry_count;
73 }
74 }
75 it->entry_count = cnt;
76 }
78 static void prime_cache_tree(void)
79 {
80 struct tree *tree = (struct tree *)trees->item;
81 if (!tree)
82 return;
83 active_cache_tree = cache_tree();
84 prime_cache_tree_rec(active_cache_tree, tree);
86 }
88 static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] <sha1> [<sha2> [<sha3>]])";
90 static struct lock_file lock_file;
92 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
93 {
94 int i, newfd, stage = 0;
95 unsigned char sha1[20];
96 struct unpack_trees_options opts;
98 memset(&opts, 0, sizeof(opts));
99 opts.head_idx = -1;
101 setup_git_directory();
102 git_config(git_default_config);
104 newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
106 git_config(git_default_config);
108 for (i = 1; i < argc; i++) {
109 const char *arg = argv[i];
111 /* "-u" means "update", meaning that a merge will update
112 * the working tree.
113 */
114 if (!strcmp(arg, "-u")) {
115 opts.update = 1;
116 continue;
117 }
119 if (!strcmp(arg, "-v")) {
120 opts.verbose_update = 1;
121 continue;
122 }
124 /* "-i" means "index only", meaning that a merge will
125 * not even look at the working tree.
126 */
127 if (!strcmp(arg, "-i")) {
128 opts.index_only = 1;
129 continue;
130 }
132 /* "--prefix=<subdirectory>/" means keep the current index
133 * entries and put the entries from the tree under the
134 * given subdirectory.
135 */
136 if (!strncmp(arg, "--prefix=", 9)) {
137 if (stage || opts.merge || opts.prefix)
138 usage(read_tree_usage);
139 opts.prefix = arg + 9;
140 opts.merge = 1;
141 stage = 1;
142 if (read_cache_unmerged())
143 die("you need to resolve your current index first");
144 continue;
145 }
147 /* This differs from "-m" in that we'll silently ignore
148 * unmerged entries and overwrite working tree files that
149 * correspond to them.
150 */
151 if (!strcmp(arg, "--reset")) {
152 if (stage || opts.merge || opts.prefix)
153 usage(read_tree_usage);
154 opts.reset = 1;
155 opts.merge = 1;
156 stage = 1;
157 read_cache_unmerged();
158 continue;
159 }
161 if (!strcmp(arg, "--trivial")) {
162 opts.trivial_merges_only = 1;
163 continue;
164 }
166 if (!strcmp(arg, "--aggressive")) {
167 opts.aggressive = 1;
168 continue;
169 }
171 /* "-m" stands for "merge", meaning we start in stage 1 */
172 if (!strcmp(arg, "-m")) {
173 if (stage || opts.merge || opts.prefix)
174 usage(read_tree_usage);
175 if (read_cache_unmerged())
176 die("you need to resolve your current index first");
177 stage = 1;
178 opts.merge = 1;
179 continue;
180 }
182 if (!strncmp(arg, "--exclude-per-directory=", 24)) {
183 struct dir_struct *dir;
185 if (opts.dir)
186 die("more than one --exclude-per-directory are given.");
188 dir = calloc(1, sizeof(*opts.dir));
189 dir->show_ignored = 1;
190 dir->exclude_per_dir = arg + 24;
191 opts.dir = dir;
192 /* We do not need to nor want to do read-directory
193 * here; we are merely interested in reusing the
194 * per directory ignore stack mechanism.
195 */
196 continue;
197 }
199 /* using -u and -i at the same time makes no sense */
200 if (1 < opts.index_only + opts.update)
201 usage(read_tree_usage);
203 if (get_sha1(arg, sha1))
204 die("Not a valid object name %s", arg);
205 if (list_tree(sha1) < 0)
206 die("failed to unpack tree object %s", arg);
207 stage++;
208 }
209 if ((opts.update||opts.index_only) && !opts.merge)
210 usage(read_tree_usage);
211 if ((opts.dir && !opts.update))
212 die("--exclude-per-directory is meaningless unless -u");
214 if (opts.prefix) {
215 int pfxlen = strlen(opts.prefix);
216 int pos;
217 if (opts.prefix[pfxlen-1] != '/')
218 die("prefix must end with /");
219 if (stage != 2)
220 die("binding merge takes only one tree");
221 pos = cache_name_pos(opts.prefix, pfxlen);
222 if (0 <= pos)
223 die("corrupt index file");
224 pos = -pos-1;
225 if (pos < active_nr &&
226 !strncmp(active_cache[pos]->name, opts.prefix, pfxlen))
227 die("subdirectory '%s' already exists.", opts.prefix);
228 pos = cache_name_pos(opts.prefix, pfxlen-1);
229 if (0 <= pos)
230 die("file '%.*s' already exists.",
231 pfxlen-1, opts.prefix);
232 }
234 if (opts.merge) {
235 if (stage < 2)
236 die("just how do you expect me to merge %d trees?", stage-1);
237 switch (stage - 1) {
238 case 1:
239 opts.fn = opts.prefix ? bind_merge : oneway_merge;
240 break;
241 case 2:
242 opts.fn = twoway_merge;
243 break;
244 case 3:
245 default:
246 opts.fn = threeway_merge;
247 cache_tree_free(&active_cache_tree);
248 break;
249 }
251 if (stage - 1 >= 3)
252 opts.head_idx = stage - 2;
253 else
254 opts.head_idx = 1;
255 }
257 unpack_trees(trees, &opts);
259 /*
260 * When reading only one tree (either the most basic form,
261 * "-m ent" or "--reset ent" form), we can obtain a fully
262 * valid cache-tree because the index must match exactly
263 * what came from the tree.
264 */
265 if (trees && trees->item && !opts.prefix && (!opts.merge || (stage == 2))) {
266 cache_tree_free(&active_cache_tree);
267 prime_cache_tree();
268 }
270 if (write_cache(newfd, active_cache, active_nr) ||
271 close(newfd) || commit_lock_file(&lock_file))
272 die("unable to write new index file");
273 return 0;
274 }