2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 static int unpack_tree(unsigned char *sha1
)
17 buffer
= read_object_with_reference(sha1
, "tree", &size
, NULL
);
20 ret
= read_tree(buffer
, size
, stage
);
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
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
))
64 * Ok, all three entries describe the same
65 * filename, but maybe the contents or file
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,
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.
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
)
97 if (!lstat(ce
->name
, &st
)) {
98 unsigned changed
= ce_match_stat(ce
, &st
);
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
;
127 struct cache_entry
*ce
, *result
;
131 /* We throw away original cache entries except for the stat information */
139 if (old
&& !path_matches(old
, ce
))
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
150 if (old
&& same(old
, result
)) {
158 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
165 * If we had an old entry that we now effectively
166 * overwrite, make sure it wasn't dirty.
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
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
200 verify_uptodate(old
);
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
)
214 struct cache_entry
*old
= NULL
;
215 struct cache_entry
**dst
= src
;
218 struct cache_entry
*ce
= *src
++;
219 int stage
= ce_stage(ce
);
234 if (!path_matches(old
, ce
) || !same(old
, ce
))
240 ce
->ce_flags
|= htons(CE_UPDATE
);
242 twoway_check(old
, seen_stage1
, ce
);
245 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
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..
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
;
271 struct cache_entry
*ce
= *src
++;
273 /* We throw away original cache entries except for the stat information */
279 if (path_matches(ce
, stat
) && same(ce
, stat
))
281 ce
->ce_flags
&= ~htons(CE_STAGEMASK
);
286 static void check_updates(struct cache_entry
**src
, int nr
)
288 static struct checkout state
= {
294 unsigned short mask
= htons(CE_UPDATE
);
296 struct cache_entry
*ce
= *src
++;
297 if (ce
->ce_flags
& mask
) {
298 ce
->ce_flags
&= ~mask
;
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
)
312 unsigned char sha1
[20];
314 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
316 die("unable to create new cachefile");
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")) {
328 /* "-m" stands for "merge", meaning we start in stage 1 */
329 if (!strcmp(arg
, "-m")) {
332 die("-m needs to come first");
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");
342 if (get_sha1(arg
, sha1
) < 0)
343 usage(read_tree_usage
);
345 usage(read_tree_usage
);
346 if (unpack_tree(sha1
) < 0)
347 die("failed to unpack tree object %s", arg
);
352 case 4: /* Three-way merge */
353 trivially_merge_cache(active_cache
, active_nr
);
354 check_updates(active_cache
, active_nr
);
356 case 3: /* Update from one tree to another */
357 twoway_merge(active_cache
, active_nr
);
358 check_updates(active_cache
, active_nr
);
360 case 2: /* Just read a tree, merge with old cache contents */
361 merge_stat_info(active_cache
, active_nr
);
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");