git-read-tree: remove deleted files in the working directory
[git/mingw.git] / read-tree.c
blobee7a46c909efbc78c59b49d2147165297158882e
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 static struct cache_entry *merge_entries(struct cache_entry *a,
44 struct cache_entry *b,
45 struct cache_entry *c)
48 * Ok, all three entries describe the same
49 * filename, but maybe the contents or file
50 * mode have changed?
52 * The trivial cases end up being the ones where two
53 * out of three files are the same:
54 * - both destinations the same, trivially take either
55 * - one of the destination versions hasn't changed,
56 * take the other.
58 * The "all entries exactly the same" case falls out as
59 * a special case of any of the "two same" cases.
61 * Here "a" is "original", and "b" and "c" are the two
62 * trees we are merging.
64 if (a && b && c) {
65 if (same(b,c))
66 return c;
67 if (same(a,b))
68 return c;
69 if (same(a,c))
70 return b;
72 return NULL;
76 * When a CE gets turned into an unmerged entry, we
77 * want it to be up-to-date
79 static void verify_uptodate(struct cache_entry *ce)
81 struct stat st;
83 if (!lstat(ce->name, &st)) {
84 unsigned changed = ce_match_stat(ce, &st);
85 if (!changed)
86 return;
87 errno = 0;
89 if (errno == ENOENT)
90 return;
91 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
95 * If the old tree contained a CE that isn't even in the
96 * result, that's always a problem, regardless of whether
97 * it's up-to-date or not (ie it can be a file that we
98 * have updated but not committed yet).
100 static void reject_merge(struct cache_entry *ce)
102 die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name);
105 static int merged_entry(struct cache_entry *merge, struct cache_entry *old, struct cache_entry **dst)
107 merge->ce_flags |= htons(CE_UPDATE);
108 if (old) {
110 * See if we can re-use the old CE directly?
111 * That way we get the uptodate stat info.
113 * This also removes the UPDATE flag on
114 * a match.
116 if (same(old, merge)) {
117 *merge = *old;
118 } else {
119 verify_uptodate(old);
122 merge->ce_flags &= ~htons(CE_STAGEMASK);
123 *dst++ = merge;
124 return 1;
127 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old, struct cache_entry **dst)
129 if (old)
130 verify_uptodate(old);
131 ce->ce_mode = 0;
132 *dst++ = ce;
133 return 1;
136 static int threeway_merge(struct cache_entry *stages[4], struct cache_entry **dst)
138 struct cache_entry *old = stages[0];
139 struct cache_entry *a = stages[1], *b = stages[2], *c = stages[3];
140 struct cache_entry *merge;
141 int count;
144 * If we have an entry in the index cache ("old"), then we want
145 * to make sure that it matches any entries in stage 2 ("first
146 * branch", aka "b").
148 if (old) {
149 if (!b || !same(old, b))
150 return -1;
152 merge = merge_entries(a, b, c);
153 if (merge)
154 return merged_entry(merge, old, dst);
155 if (old)
156 verify_uptodate(old);
157 count = 0;
158 if (a) { *dst++ = a; count++; }
159 if (b) { *dst++ = b; count++; }
160 if (c) { *dst++ = c; count++; }
161 return count;
165 * Two-way merge.
167 * The rule is to "carry forward" what is in the index without losing
168 * information across a "fast forward", favoring a successful merge
169 * over a merge failure when it makes sense. For details of the
170 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
173 static int twoway_merge(struct cache_entry **src, struct cache_entry **dst)
175 struct cache_entry *current = src[0];
176 struct cache_entry *oldtree = src[1], *newtree = src[2];
178 if (src[3])
179 return -1;
181 if (current) {
182 if ((!oldtree && !newtree) || /* 4 and 5 */
183 (!oldtree && newtree &&
184 same(current, newtree)) || /* 6 and 7 */
185 (oldtree && newtree &&
186 same(oldtree, newtree)) || /* 14 and 15 */
187 (oldtree && newtree &&
188 !same(oldtree, newtree) && /* 18 and 19*/
189 same(current, newtree))) {
190 *dst++ = current;
191 return 1;
193 else if (oldtree && !newtree && same(current, oldtree)) {
194 /* 10 or 11 */
195 return deleted_entry(oldtree, current, dst);
197 else if (oldtree && newtree &&
198 same(current, oldtree) && !same(current, newtree)) {
199 /* 20 or 21 */
200 return merged_entry(newtree, current, dst);
202 else
203 /* all other failures */
204 return -1;
206 else if (newtree)
207 return merged_entry(newtree, current, dst);
208 else
209 return deleted_entry(oldtree, current, dst);
213 * One-way merge.
215 * The rule is:
216 * - take the stat information from stage0, take the data from stage1
218 static int oneway_merge(struct cache_entry **src, struct cache_entry **dst)
220 struct cache_entry *old = src[0];
221 struct cache_entry *a = src[1];
223 if (src[2] || src[3])
224 return -1;
226 if (!a)
227 return 0;
228 if (old && same(old, a)) {
229 *dst++ = old;
230 return 1;
232 return merged_entry(a, NULL, dst);
235 static void check_updates(struct cache_entry **src, int nr)
237 static struct checkout state = {
238 .base_dir = "",
239 .force = 1,
240 .quiet = 1,
241 .refresh_cache = 1,
243 unsigned short mask = htons(CE_UPDATE);
244 while (nr--) {
245 struct cache_entry *ce = *src++;
246 if (!ce->ce_mode) {
247 if (update)
248 unlink(ce->name);
249 continue;
251 if (ce->ce_flags & mask) {
252 ce->ce_flags &= ~mask;
253 if (update)
254 checkout_entry(ce, &state);
259 typedef int (*merge_fn_t)(struct cache_entry **, struct cache_entry **);
261 static void merge_cache(struct cache_entry **src, int nr, merge_fn_t fn)
263 struct cache_entry **dst = src;
265 while (nr) {
266 int entries;
267 struct cache_entry *name, *ce, *stages[4] = { NULL, };
269 name = ce = *src;
270 for (;;) {
271 int stage = ce_stage(ce);
272 stages[stage] = ce;
273 ce = *++src;
274 active_nr--;
275 if (!--nr)
276 break;
277 if (!path_matches(ce, name))
278 break;
281 entries = fn(stages, dst);
282 if (entries < 0)
283 reject_merge(name);
284 dst += entries;
285 active_nr += entries;
287 check_updates(active_cache, active_nr);
290 static int read_cache_unmerged(void)
292 int i, deleted;
293 struct cache_entry **dst;
295 read_cache();
296 dst = active_cache;
297 deleted = 0;
298 for (i = 0; i < active_nr; i++) {
299 struct cache_entry *ce = active_cache[i];
300 if (ce_stage(ce)) {
301 deleted++;
302 continue;
304 if (deleted)
305 *dst = ce;
306 dst++;
308 active_nr -= deleted;
309 return deleted;
312 static char *read_tree_usage = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])";
314 static struct cache_file cache_file;
316 int main(int argc, char **argv)
318 int i, newfd, merge, reset;
319 unsigned char sha1[20];
321 newfd = hold_index_file_for_update(&cache_file, get_index_file());
322 if (newfd < 0)
323 die("unable to create new cachefile");
325 merge = 0;
326 reset = 0;
327 for (i = 1; i < argc; i++) {
328 const char *arg = argv[i];
330 /* "-u" means "update", meaning that a merge will update the working directory */
331 if (!strcmp(arg, "-u")) {
332 update = 1;
333 continue;
336 /* This differs from "-m" in that we'll silently ignore unmerged entries */
337 if (!strcmp(arg, "--reset")) {
338 if (stage || merge)
339 usage(read_tree_usage);
340 reset = 1;
341 merge = 1;
342 stage = 1;
343 read_cache_unmerged();
346 /* "-m" stands for "merge", meaning we start in stage 1 */
347 if (!strcmp(arg, "-m")) {
348 if (stage || merge)
349 usage(read_tree_usage);
350 if (read_cache_unmerged())
351 die("you need to resolve your current index first");
352 stage = 1;
353 merge = 1;
354 continue;
356 if (get_sha1(arg, sha1) < 0)
357 usage(read_tree_usage);
358 if (stage > 3)
359 usage(read_tree_usage);
360 if (unpack_tree(sha1) < 0)
361 die("failed to unpack tree object %s", arg);
362 stage++;
364 if (update && !merge)
365 usage(read_tree_usage);
366 if (merge) {
367 static const merge_fn_t merge_function[] = {
368 [1] = oneway_merge,
369 [2] = twoway_merge,
370 [3] = threeway_merge,
372 if (stage < 2 || stage > 4)
373 die("just how do you expect me to merge %d trees?", stage-1);
374 merge_cache(active_cache, active_nr, merge_function[stage-1]);
376 if (write_cache(newfd, active_cache, active_nr) ||
377 commit_index_file(&cache_file))
378 die("unable to write new index file");
379 return 0;