[PATCH] index locking like everybody else
[git/dscho.git] / read-tree.c
blob63a1eb543fef2b84f2ff7d295610c09b6c2ab2da
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 static int stage = 0;
9 static int update = 0;
11 static int unpack_tree(unsigned char *sha1)
13 void *buffer;
14 unsigned long size;
15 int ret;
17 buffer = read_object_with_reference(sha1, "tree", &size, NULL);
18 if (!buffer)
19 return -1;
20 ret = read_tree(buffer, size, stage);
21 free(buffer);
22 return ret;
25 static int path_matches(struct cache_entry *a, struct cache_entry *b)
27 int len = ce_namelen(a);
28 return ce_namelen(b) == len &&
29 !memcmp(a->name, b->name, len);
32 static int same(struct cache_entry *a, struct cache_entry *b)
34 return a->ce_mode == b->ce_mode &&
35 !memcmp(a->sha1, b->sha1, 20);
40 * This removes all trivial merges that don't change the tree
41 * and collapses them to state 0.
43 * _Any_ other merge is left to user policy. That includes "both
44 * created the same file", and "both removed the same file" - which are
45 * trivial, but the user might still want to _note_ it.
47 static struct cache_entry *merge_entries(struct cache_entry *a,
48 struct cache_entry *b,
49 struct cache_entry *c)
51 int len = ce_namelen(a);
54 * Are they all the same filename? We won't do
55 * any name merging
57 if (ce_namelen(b) != len ||
58 ce_namelen(c) != len ||
59 memcmp(a->name, b->name, len) ||
60 memcmp(a->name, c->name, len))
61 return NULL;
64 * Ok, all three entries describe the same
65 * filename, but maybe the contents or file
66 * mode have changed?
68 * The trivial cases end up being the ones where two
69 * out of three files are the same:
70 * - both destinations the same, trivially take either
71 * - one of the destination versions hasn't changed,
72 * take the other.
74 * The "all entries exactly the same" case falls out as
75 * a special case of any of the "two same" cases.
77 * Here "a" is "original", and "b" and "c" are the two
78 * trees we are merging.
80 if (same(b,c))
81 return c;
82 if (same(a,b))
83 return c;
84 if (same(a,c))
85 return b;
86 return NULL;
90 * When a CE gets turned into an unmerged entry, we
91 * want it to be up-to-date
93 static void verify_uptodate(struct cache_entry *ce)
95 struct stat st;
97 if (!lstat(ce->name, &st)) {
98 unsigned changed = ce_match_stat(ce, &st);
99 if (!changed)
100 return;
101 errno = 0;
103 if (errno == ENOENT)
104 return;
105 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
109 * If the old tree contained a CE that isn't even in the
110 * result, that's always a problem, regardless of whether
111 * it's up-to-date or not (ie it can be a file that we
112 * have updated but not committed yet).
114 static void reject_merge(struct cache_entry *ce)
116 die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name);
119 #define CHECK_OLD(ce) if (old && same(old, ce)) { verify_uptodate(old); old = NULL; }
121 static void trivially_merge_cache(struct cache_entry **src, int nr)
123 struct cache_entry **dst = src;
124 struct cache_entry *old = NULL;
126 while (nr--) {
127 struct cache_entry *ce, *result;
129 ce = *src++;
131 /* We throw away original cache entries except for the stat information */
132 if (!ce_stage(ce)) {
133 if (old)
134 reject_merge(old);
135 old = ce;
136 active_nr--;
137 continue;
139 if (old && !path_matches(old, ce))
140 reject_merge(old);
141 if (nr > 1 && (result = merge_entries(ce, src[0], src[1])) != NULL) {
142 result->ce_flags |= htons(CE_UPDATE);
144 * See if we can re-use the old CE directly?
145 * That way we get the uptodate stat info.
147 * This also removes the UPDATE flag on
148 * a match.
150 if (old && same(old, result)) {
151 *result = *old;
152 old = NULL;
154 CHECK_OLD(ce);
155 CHECK_OLD(src[0]);
156 CHECK_OLD(src[1]);
157 ce = result;
158 ce->ce_flags &= ~htons(CE_STAGEMASK);
159 src += 2;
160 nr -= 2;
161 active_nr -= 2;
165 * If we had an old entry that we now effectively
166 * overwrite, make sure it wasn't dirty.
168 CHECK_OLD(ce);
169 *dst++ = ce;
171 if (old)
172 reject_merge(old);
176 * When we find a "stage2" entry in the two-way merge, that's
177 * the one that will remain. If we have an exact old match,
178 * we don't care whether the file is up-to-date or not, we just
179 * re-use the thing directly.
181 * If we didn't have an exact match, then we want to make sure
182 * that we've seen a stage1 that matched the old, and that the
183 * old file was up-to-date. Because it will be gone after this
184 * merge..
186 static void twoway_check(struct cache_entry *old, int seen_stage1, struct cache_entry *ce)
188 if (path_matches(old, ce)) {
190 * This also removes the UPDATE flag on
191 * a match
193 if (same(old, ce)) {
194 *ce = *old;
195 return;
197 if (!seen_stage1)
198 reject_merge(old);
200 verify_uptodate(old);
204 * Two-way merge.
206 * The rule is:
207 * - every current entry has to match the old tree
208 * - if the current entry matches the new tree, we leave it
209 * as-is. Otherwise we require that it be up-to-date.
211 static void twoway_merge(struct cache_entry **src, int nr)
213 int seen_stage1 = 0;
214 struct cache_entry *old = NULL;
215 struct cache_entry **dst = src;
217 while (nr--) {
218 struct cache_entry *ce = *src++;
219 int stage = ce_stage(ce);
221 switch (stage) {
222 case 0:
223 if (old)
224 reject_merge(old);
225 old = ce;
226 seen_stage1 = 0;
227 active_nr--;
228 continue;
230 case 1:
231 active_nr--;
232 if (!old)
233 continue;
234 if (!path_matches(old, ce) || !same(old, ce))
235 reject_merge(old);
236 seen_stage1 = 1;
237 continue;
239 case 2:
240 ce->ce_flags |= htons(CE_UPDATE);
241 if (old) {
242 twoway_check(old, seen_stage1, ce);
243 old = NULL;
245 ce->ce_flags &= ~htons(CE_STAGEMASK);
246 *dst++ = ce;
247 continue;
249 die("impossible two-way stage");
253 * Unmatched with a new entry? Make sure it was
254 * at least uptodate in the working directory _and_
255 * the original tree..
257 if (old) {
258 if (!seen_stage1)
259 reject_merge(old);
260 verify_uptodate(old);
264 static void merge_stat_info(struct cache_entry **src, int nr)
266 static struct cache_entry null_entry;
267 struct cache_entry **dst = src;
268 struct cache_entry *stat = &null_entry;
270 while (nr--) {
271 struct cache_entry *ce = *src++;
273 /* We throw away original cache entries except for the stat information */
274 if (!ce_stage(ce)) {
275 stat = ce;
276 active_nr--;
277 continue;
279 if (path_matches(ce, stat) && same(ce, stat))
280 *ce = *stat;
281 ce->ce_flags &= ~htons(CE_STAGEMASK);
282 *dst++ = ce;
286 static void check_updates(struct cache_entry **src, int nr)
288 static struct checkout state = {
289 .base_dir = "",
290 .force = 1,
291 .quiet = 1,
292 .refresh_cache = 1,
294 unsigned short mask = htons(CE_UPDATE);
295 while (nr--) {
296 struct cache_entry *ce = *src++;
297 if (ce->ce_flags & mask) {
298 ce->ce_flags &= ~mask;
299 if (update)
300 checkout_entry(ce, &state);
305 static char *read_tree_usage = "git-read-tree (<sha> | -m <sha1> [<sha2> [<sha3>]])";
307 static struct cache_file cache_file;
309 int main(int argc, char **argv)
311 int i, newfd, merge;
312 unsigned char sha1[20];
314 newfd = hold_index_file_for_update(&cache_file, get_index_file());
315 if (newfd < 0)
316 die("unable to create new cachefile");
318 merge = 0;
319 for (i = 1; i < argc; i++) {
320 const char *arg = argv[i];
322 /* "-u" means "update", meaning that a merge will update the working directory */
323 if (!strcmp(arg, "-u")) {
324 update = 1;
325 continue;
328 /* "-m" stands for "merge", meaning we start in stage 1 */
329 if (!strcmp(arg, "-m")) {
330 int i;
331 if (stage)
332 die("-m needs to come first");
333 read_cache();
334 for (i = 0; i < active_nr; i++) {
335 if (ce_stage(active_cache[i]))
336 die("you need to resolve your current index first");
338 stage = 1;
339 merge = 1;
340 continue;
342 if (get_sha1(arg, sha1) < 0)
343 usage(read_tree_usage);
344 if (stage > 3)
345 usage(read_tree_usage);
346 if (unpack_tree(sha1) < 0)
347 die("failed to unpack tree object %s", arg);
348 stage++;
350 if (merge) {
351 switch (stage) {
352 case 4: /* Three-way merge */
353 trivially_merge_cache(active_cache, active_nr);
354 check_updates(active_cache, active_nr);
355 break;
356 case 3: /* Update from one tree to another */
357 twoway_merge(active_cache, active_nr);
358 check_updates(active_cache, active_nr);
359 break;
360 case 2: /* Just read a tree, merge with old cache contents */
361 merge_stat_info(active_cache, active_nr);
362 break;
363 default:
364 die("just how do you expect me to merge %d trees?", stage-1);
367 if (write_cache(newfd, active_cache, active_nr) ||
368 commit_index_file(&cache_file))
369 die("unable to write new index file");
370 return 0;