More work on merging with git-read-tree..
[git/mingw.git] / read-tree.c
blob1a575eb0d29d298ab714c7455cff5b6eced4de2f
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 char *lockfile_name;
27 static void remove_lock_file(void)
29 if (lockfile_name)
30 unlink(lockfile_name);
33 static int path_matches(struct cache_entry *a, struct cache_entry *b)
35 int len = ce_namelen(a);
36 return ce_namelen(b) == len &&
37 !memcmp(a->name, b->name, len);
40 static int same(struct cache_entry *a, struct cache_entry *b)
42 return a->ce_mode == b->ce_mode &&
43 !memcmp(a->sha1, b->sha1, 20);
48 * This removes all trivial merges that don't change the tree
49 * and collapses them to state 0.
51 * _Any_ other merge is left to user policy. That includes "both
52 * created the same file", and "both removed the same file" - which are
53 * trivial, but the user might still want to _note_ it.
55 static struct cache_entry *merge_entries(struct cache_entry *a,
56 struct cache_entry *b,
57 struct cache_entry *c)
59 int len = ce_namelen(a);
62 * Are they all the same filename? We won't do
63 * any name merging
65 if (ce_namelen(b) != len ||
66 ce_namelen(c) != len ||
67 memcmp(a->name, b->name, len) ||
68 memcmp(a->name, c->name, len))
69 return NULL;
72 * Ok, all three entries describe the same
73 * filename, but maybe the contents or file
74 * mode have changed?
76 * The trivial cases end up being the ones where two
77 * out of three files are the same:
78 * - both destinations the same, trivially take either
79 * - one of the destination versions hasn't changed,
80 * take the other.
82 * The "all entries exactly the same" case falls out as
83 * a special case of any of the "two same" cases.
85 * Here "a" is "original", and "b" and "c" are the two
86 * trees we are merging.
88 if (same(b,c))
89 return c;
90 if (same(a,b))
91 return c;
92 if (same(a,c))
93 return b;
94 return NULL;
98 * When a CE gets turned into an unmerged entry, we
99 * want it to be up-to-date
101 static void verify_uptodate(struct cache_entry *ce)
103 struct stat st;
105 if (!lstat(ce->name, &st)) {
106 unsigned changed = ce_match_stat(ce, &st);
107 if (!changed)
108 return;
109 errno = 0;
111 if (errno == ENOENT)
112 return;
113 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
117 * If the old tree contained a CE that isn't even in the
118 * result, that's always a problem, regardless of whether
119 * it's up-to-date or not (ie it can be a file that we
120 * have updated but not committed yet).
122 static void reject_merge(struct cache_entry *ce)
124 die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name);
127 #define CHECK_OLD(ce) if (old && same(old, ce)) { verify_uptodate(old); old = NULL; }
129 static void trivially_merge_cache(struct cache_entry **src, int nr)
131 struct cache_entry **dst = src;
132 struct cache_entry *old = NULL;
134 while (nr--) {
135 struct cache_entry *ce, *result;
137 ce = *src++;
139 /* We throw away original cache entries except for the stat information */
140 if (!ce_stage(ce)) {
141 if (old)
142 reject_merge(old);
143 old = ce;
144 active_nr--;
145 continue;
147 if (old && !path_matches(old, ce))
148 reject_merge(old);
149 if (nr > 2 && (result = merge_entries(ce, src[1], src[2])) != NULL) {
150 result->ce_flags |= htons(CE_UPDATE);
152 * See if we can re-use the old CE directly?
153 * That way we get the uptodate stat info.
155 * This also removes the UPDATE flag on
156 * a match.
158 if (old && same(old, result)) {
159 *result = *old;
160 old = NULL;
162 CHECK_OLD(ce);
163 CHECK_OLD(src[1]);
164 CHECK_OLD(src[2]);
165 ce = result;
166 ce->ce_flags &= ~htons(CE_STAGEMASK);
167 src += 2;
168 nr -= 2;
169 active_nr -= 2;
173 * If we had an old entry that we now effectively
174 * overwrite, make sure it wasn't dirty.
176 CHECK_OLD(ce);
177 *dst++ = ce;
179 if (old)
180 reject_merge(old);
184 * Two-way merge.
186 * The rule is:
187 * - every current entry has to match the old tree
188 * - if the current entry matches the new tree, we leave it
189 * as-is. Otherwise we require that it be up-to-date.
191 static void twoway_merge(struct cache_entry **src, int nr)
193 static struct cache_entry null_entry;
194 struct cache_entry *old = NULL, *stat = &null_entry;
195 struct cache_entry **dst = src;
197 while (nr--) {
198 struct cache_entry *ce = *src++;
199 int stage = ce_stage(ce);
201 switch (stage) {
202 case 0:
203 if (old)
204 reject_merge(old);
205 old = ce;
206 stat = ce;
207 active_nr--;
208 continue;
210 case 1:
211 active_nr--;
212 if (!old)
213 continue;
214 if (!path_matches(old, ce) || !same(old, ce))
215 reject_merge(old);
216 continue;
218 case 2:
219 ce->ce_flags |= htons(CE_UPDATE);
220 if (old) {
221 if (!path_matches(old, ce))
222 reject_merge(old);
224 * This also removes the UPDATE flag on
225 * a match
227 if (same(old, ce))
228 *ce = *old;
229 else
230 verify_uptodate(old);
231 old = NULL;
233 ce->ce_flags &= ~htons(CE_STAGEMASK);
234 *dst++ = ce;
235 continue;
237 die("impossible two-way stage");
239 if (old)
240 reject_merge(old);
243 static void merge_stat_info(struct cache_entry **src, int nr)
245 static struct cache_entry null_entry;
246 struct cache_entry **dst = src;
247 struct cache_entry *stat = &null_entry;
249 while (nr--) {
250 struct cache_entry *ce = *src++;
252 /* We throw away original cache entries except for the stat information */
253 if (!ce_stage(ce)) {
254 stat = ce;
255 active_nr--;
256 continue;
258 if (path_matches(ce, stat) && same(ce, stat))
259 *ce = *stat;
260 ce->ce_flags &= ~htons(CE_STAGEMASK);
261 *dst++ = ce;
265 static void check_updates(struct cache_entry **src, int nr)
267 static struct checkout state = {
268 .base_dir = "",
269 .force = 1,
270 .quiet = 1,
271 .refresh_cache = 1,
273 unsigned short mask = htons(CE_UPDATE);
274 while (nr--) {
275 struct cache_entry *ce = *src++;
276 if (ce->ce_flags & mask) {
277 ce->ce_flags &= ~mask;
278 if (update)
279 checkout_entry(ce, &state);
284 static char *read_tree_usage = "git-read-tree (<sha> | -m <sha1> [<sha2> [<sha3>]])";
286 int main(int argc, char **argv)
288 int i, newfd, merge;
289 unsigned char sha1[20];
290 static char lockfile[MAXPATHLEN+1];
291 const char *indexfile = get_index_file();
293 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
295 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
296 if (newfd < 0)
297 die("unable to create new cachefile");
298 atexit(remove_lock_file);
299 lockfile_name = lockfile;
301 merge = 0;
302 for (i = 1; i < argc; i++) {
303 const char *arg = argv[i];
305 /* "-u" means "update", meaning that a merge will update the working directory */
306 if (!strcmp(arg, "-u")) {
307 update = 1;
308 continue;
311 /* "-m" stands for "merge", meaning we start in stage 1 */
312 if (!strcmp(arg, "-m")) {
313 int i;
314 if (stage)
315 die("-m needs to come first");
316 read_cache();
317 for (i = 0; i < active_nr; i++) {
318 if (ce_stage(active_cache[i]))
319 die("you need to resolve your current index first");
321 stage = 1;
322 merge = 1;
323 continue;
325 if (get_sha1(arg, sha1) < 0)
326 usage(read_tree_usage);
327 if (stage > 3)
328 usage(read_tree_usage);
329 if (unpack_tree(sha1) < 0)
330 die("failed to unpack tree object %s", arg);
331 stage++;
333 if (merge) {
334 switch (stage) {
335 case 4: /* Three-way merge */
336 trivially_merge_cache(active_cache, active_nr);
337 check_updates(active_cache, active_nr);
338 break;
339 case 3: /* Update from one tree to another */
340 twoway_merge(active_cache, active_nr);
341 check_updates(active_cache, active_nr);
342 break;
343 case 2: /* Just read a tree, merge with old cache contents */
344 merge_stat_info(active_cache, active_nr);
345 break;
346 default:
347 die("just how do you expect me to merge %d trees?", stage-1);
350 if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
351 die("unable to write new index file");
352 lockfile_name = NULL;
353 return 0;