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 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
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,
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.
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
)
83 if (!lstat(ce
->name
, &st
)) {
84 unsigned changed
= ce_match_stat(ce
, &st
);
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
);
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
116 if (same(old
, merge
)) {
119 verify_uptodate(old
);
122 merge
->ce_flags
&= ~htons(CE_STAGEMASK
);
127 static int deleted_entry(struct cache_entry
*ce
, struct cache_entry
*old
, struct cache_entry
**dst
)
130 verify_uptodate(old
);
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
;
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
149 if (!b
|| !same(old
, b
))
152 merge
= merge_entries(a
, b
, c
);
154 return merged_entry(merge
, old
, dst
);
156 verify_uptodate(old
);
158 if (a
) { *dst
++ = a
; count
++; }
159 if (b
) { *dst
++ = b
; count
++; }
160 if (c
) { *dst
++ = c
; count
++; }
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];
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
))) {
193 else if (oldtree
&& !newtree
&& same(current
, oldtree
)) {
195 return deleted_entry(oldtree
, current
, dst
);
197 else if (oldtree
&& newtree
&&
198 same(current
, oldtree
) && !same(current
, newtree
)) {
200 return merged_entry(newtree
, current
, dst
);
203 /* all other failures */
207 return merged_entry(newtree
, current
, dst
);
209 return deleted_entry(oldtree
, current
, dst
);
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])
228 if (old
&& same(old
, a
)) {
232 return merged_entry(a
, NULL
, dst
);
235 static void check_updates(struct cache_entry
**src
, int nr
)
237 static struct checkout state
= {
243 unsigned short mask
= htons(CE_UPDATE
);
245 struct cache_entry
*ce
= *src
++;
251 if (ce
->ce_flags
& mask
) {
252 ce
->ce_flags
&= ~mask
;
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
;
267 struct cache_entry
*name
, *ce
, *stages
[4] = { NULL
, };
271 int stage
= ce_stage(ce
);
277 if (!path_matches(ce
, name
))
281 entries
= fn(stages
, dst
);
285 active_nr
+= entries
;
287 check_updates(active_cache
, active_nr
);
290 static int read_cache_unmerged(void)
293 struct cache_entry
**dst
;
298 for (i
= 0; i
< active_nr
; i
++) {
299 struct cache_entry
*ce
= active_cache
[i
];
308 active_nr
-= 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());
323 die("unable to create new cachefile");
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")) {
336 /* This differs from "-m" in that we'll silently ignore unmerged entries */
337 if (!strcmp(arg
, "--reset")) {
339 usage(read_tree_usage
);
343 read_cache_unmerged();
346 /* "-m" stands for "merge", meaning we start in stage 1 */
347 if (!strcmp(arg
, "-m")) {
349 usage(read_tree_usage
);
350 if (read_cache_unmerged())
351 die("you need to resolve your current index first");
356 if (get_sha1(arg
, sha1
) < 0)
357 usage(read_tree_usage
);
359 usage(read_tree_usage
);
360 if (unpack_tree(sha1
) < 0)
361 die("failed to unpack tree object %s", arg
);
364 if (update
&& !merge
)
365 usage(read_tree_usage
);
367 static const merge_fn_t merge_function
[] = {
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");