]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 LT |
6 | #include "cache.h" |
7 | ||
d99082e0 | 8 | static int stage = 0; |
220a0b52 | 9 | static int update = 0; |
d99082e0 | 10 | |
94537c78 | 11 | static int unpack_tree(unsigned char *sha1) |
b12ec373 JH |
12 | { |
13 | void *buffer; | |
14 | unsigned long size; | |
14242464 | 15 | int ret; |
b12ec373 | 16 | |
e99d59ff | 17 | buffer = read_object_with_reference(sha1, "tree", &size, NULL); |
aba06682 LT |
18 | if (!buffer) |
19 | return -1; | |
14242464 PB |
20 | ret = read_tree(buffer, size, stage); |
21 | free(buffer); | |
22 | return ret; | |
b12ec373 JH |
23 | } |
24 | ||
ca016f0e LT |
25 | static int path_matches(struct cache_entry *a, struct cache_entry *b) |
26 | { | |
27 | int len = ce_namelen(a); | |
28 | return ce_namelen(b) == len && | |
29 | !memcmp(a->name, b->name, len); | |
30 | } | |
31 | ||
43f91266 LT |
32 | static int same(struct cache_entry *a, struct cache_entry *b) |
33 | { | |
34 | return a->ce_mode == b->ce_mode && | |
35 | !memcmp(a->sha1, b->sha1, 20); | |
36 | } | |
37 | ||
38 | ||
d99082e0 | 39 | /* |
43f91266 LT |
40 | * This removes all trivial merges that don't change the tree |
41 | * and collapses them to state 0. | |
d99082e0 | 42 | */ |
43f91266 LT |
43 | static struct cache_entry *merge_entries(struct cache_entry *a, |
44 | struct cache_entry *b, | |
45 | struct cache_entry *c) | |
d99082e0 | 46 | { |
43f91266 LT |
47 | /* |
48 | * Ok, all three entries describe the same | |
49 | * filename, but maybe the contents or file | |
50 | * mode have changed? | |
51 | * | |
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. | |
57 | * | |
58 | * The "all entries exactly the same" case falls out as | |
59 | * a special case of any of the "two same" cases. | |
60 | * | |
61 | * Here "a" is "original", and "b" and "c" are the two | |
62 | * trees we are merging. | |
63 | */ | |
d723c690 LT |
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; | |
71 | } | |
43f91266 | 72 | return NULL; |
d99082e0 LT |
73 | } |
74 | ||
02ede67a LT |
75 | /* |
76 | * When a CE gets turned into an unmerged entry, we | |
77 | * want it to be up-to-date | |
78 | */ | |
79 | static void verify_uptodate(struct cache_entry *ce) | |
80 | { | |
81 | struct stat st; | |
82 | ||
83 | if (!lstat(ce->name, &st)) { | |
84 | unsigned changed = ce_match_stat(ce, &st); | |
85 | if (!changed) | |
86 | return; | |
87 | errno = 0; | |
88 | } | |
89 | if (errno == ENOENT) | |
90 | return; | |
91 | die("Entry '%s' not uptodate. Cannot merge.", ce->name); | |
92 | } | |
93 | ||
94 | /* | |
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). | |
99 | */ | |
76f38347 | 100 | static void reject_merge(struct cache_entry *ce) |
02ede67a | 101 | { |
76f38347 | 102 | die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name); |
02ede67a LT |
103 | } |
104 | ||
d723c690 | 105 | static int merged_entry(struct cache_entry *merge, struct cache_entry *old, struct cache_entry **dst) |
d99082e0 | 106 | { |
d723c690 LT |
107 | merge->ce_flags |= htons(CE_UPDATE); |
108 | if (old) { | |
02ede67a | 109 | /* |
d723c690 LT |
110 | * See if we can re-use the old CE directly? |
111 | * That way we get the uptodate stat info. | |
112 | * | |
113 | * This also removes the UPDATE flag on | |
114 | * a match. | |
02ede67a | 115 | */ |
d723c690 LT |
116 | if (same(old, merge)) { |
117 | *merge = *old; | |
118 | } else { | |
119 | verify_uptodate(old); | |
120 | } | |
a3a65234 | 121 | } |
d723c690 LT |
122 | merge->ce_flags &= ~htons(CE_STAGEMASK); |
123 | *dst++ = merge; | |
124 | return 1; | |
a3a65234 LT |
125 | } |
126 | ||
d723c690 | 127 | static int threeway_merge(struct cache_entry *stages[4], struct cache_entry **dst) |
e6ee623b | 128 | { |
d723c690 LT |
129 | struct cache_entry *old = stages[0]; |
130 | struct cache_entry *a = stages[1], *b = stages[2], *c = stages[3]; | |
131 | struct cache_entry *merge; | |
132 | int count; | |
133 | ||
134 | /* | |
135 | * If we have an entry in the index cache ("old"), then we want | |
136 | * to make sure that it matches any entries in stage 2 ("first | |
137 | * branch", aka "b"). | |
138 | */ | |
139 | if (old) { | |
140 | if (!b || !same(old, b)) | |
141 | return -1; | |
e6ee623b | 142 | } |
d723c690 LT |
143 | merge = merge_entries(a, b, c); |
144 | if (merge) | |
145 | return merged_entry(merge, old, dst); | |
146 | if (old) | |
147 | verify_uptodate(old); | |
148 | count = 0; | |
149 | if (a) { *dst++ = a; count++; } | |
150 | if (b) { *dst++ = b; count++; } | |
151 | if (c) { *dst++ = c; count++; } | |
152 | return count; | |
e6ee623b LT |
153 | } |
154 | ||
220a0b52 LT |
155 | /* |
156 | * Two-way merge. | |
157 | * | |
c8596009 JH |
158 | * The rule is to "carry forward" what is in the index without losing |
159 | * information across a "fast forward", favoring a successful merge | |
160 | * over a merge failure when it makes sense. For details of the | |
161 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. | |
162 | * | |
220a0b52 | 163 | */ |
d723c690 | 164 | static int twoway_merge(struct cache_entry **src, struct cache_entry **dst) |
a3a65234 | 165 | { |
c8596009 JH |
166 | struct cache_entry *current = src[0]; |
167 | struct cache_entry *oldtree = src[1], *newtree = src[2]; | |
220a0b52 | 168 | |
d723c690 LT |
169 | if (src[3]) |
170 | return -1; | |
e6ee623b | 171 | |
c8596009 JH |
172 | if (current) { |
173 | if ((!oldtree && !newtree) || /* 4 and 5 */ | |
174 | (!oldtree && newtree && | |
175 | same(current, newtree)) || /* 6 and 7 */ | |
176 | (oldtree && newtree && | |
177 | same(oldtree, newtree)) || /* 14 and 15 */ | |
178 | (oldtree && newtree && | |
179 | !same(oldtree, newtree) && /* 18 and 19*/ | |
180 | same(current, newtree))) { | |
181 | *dst++ = current; | |
182 | return 1; | |
183 | } | |
184 | else if (oldtree && !newtree && same(current, oldtree)) { | |
185 | /* 10 or 11 */ | |
186 | verify_uptodate(current); | |
187 | return 0; | |
188 | } | |
189 | else if (oldtree && newtree && | |
190 | same(current, oldtree) && !same(current, newtree)) { | |
191 | /* 20 or 21 */ | |
192 | verify_uptodate(current); | |
193 | return merged_entry(newtree, NULL, dst); | |
194 | } | |
195 | else | |
196 | /* all other failures */ | |
d723c690 | 197 | return -1; |
e6ee623b | 198 | } |
c8596009 JH |
199 | else if (newtree) |
200 | return merged_entry(newtree, NULL, dst); | |
201 | else | |
202 | return 0; | |
220a0b52 LT |
203 | } |
204 | ||
d723c690 LT |
205 | /* |
206 | * One-way merge. | |
207 | * | |
208 | * The rule is: | |
209 | * - take the stat information from stage0, take the data from stage1 | |
210 | */ | |
211 | static int oneway_merge(struct cache_entry **src, struct cache_entry **dst) | |
220a0b52 | 212 | { |
d723c690 LT |
213 | struct cache_entry *old = src[0]; |
214 | struct cache_entry *a = src[1]; | |
a3a65234 | 215 | |
d723c690 LT |
216 | if (src[2] || src[3]) |
217 | return -1; | |
a3a65234 | 218 | |
d723c690 LT |
219 | if (!a) |
220 | return 0; | |
b5b42507 LT |
221 | if (old && same(old, a)) { |
222 | *dst++ = old; | |
223 | return 1; | |
224 | } | |
225 | return merged_entry(a, NULL, dst); | |
d99082e0 LT |
226 | } |
227 | ||
220a0b52 LT |
228 | static void check_updates(struct cache_entry **src, int nr) |
229 | { | |
230 | static struct checkout state = { | |
231 | .base_dir = "", | |
232 | .force = 1, | |
233 | .quiet = 1, | |
234 | .refresh_cache = 1, | |
235 | }; | |
236 | unsigned short mask = htons(CE_UPDATE); | |
237 | while (nr--) { | |
238 | struct cache_entry *ce = *src++; | |
239 | if (ce->ce_flags & mask) { | |
240 | ce->ce_flags &= ~mask; | |
241 | if (update) | |
242 | checkout_entry(ce, &state); | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
45132251 LT |
247 | typedef int (*merge_fn_t)(struct cache_entry **, struct cache_entry **); |
248 | ||
249 | static void merge_cache(struct cache_entry **src, int nr, merge_fn_t fn) | |
d723c690 LT |
250 | { |
251 | struct cache_entry **dst = src; | |
252 | ||
253 | while (nr) { | |
254 | int entries; | |
255 | struct cache_entry *name, *ce, *stages[4] = { NULL, }; | |
256 | ||
257 | name = ce = *src; | |
258 | for (;;) { | |
259 | int stage = ce_stage(ce); | |
260 | stages[stage] = ce; | |
261 | ce = *++src; | |
262 | active_nr--; | |
263 | if (!--nr) | |
264 | break; | |
265 | if (!path_matches(ce, name)) | |
266 | break; | |
267 | } | |
268 | ||
269 | entries = fn(stages, dst); | |
270 | if (entries < 0) | |
271 | reject_merge(name); | |
272 | dst += entries; | |
273 | active_nr += entries; | |
274 | } | |
275 | check_updates(active_cache, active_nr); | |
276 | } | |
277 | ||
438195cc LT |
278 | static int read_cache_unmerged(void) |
279 | { | |
280 | int i, deleted; | |
281 | struct cache_entry **dst; | |
282 | ||
283 | read_cache(); | |
284 | dst = active_cache; | |
285 | deleted = 0; | |
286 | for (i = 0; i < active_nr; i++) { | |
287 | struct cache_entry *ce = active_cache[i]; | |
288 | if (ce_stage(ce)) { | |
289 | deleted++; | |
290 | continue; | |
291 | } | |
292 | if (deleted) | |
293 | *dst = ce; | |
294 | dst++; | |
295 | } | |
296 | active_nr -= deleted; | |
297 | return deleted; | |
298 | } | |
299 | ||
a57f0b58 | 300 | static char *read_tree_usage = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])"; |
c5bac17a | 301 | |
96cd5429 JH |
302 | static struct cache_file cache_file; |
303 | ||
e83c5163 LT |
304 | int main(int argc, char **argv) |
305 | { | |
438195cc | 306 | int i, newfd, merge, reset; |
e83c5163 | 307 | unsigned char sha1[20]; |
bb233d69 | 308 | |
96cd5429 | 309 | newfd = hold_index_file_for_update(&cache_file, get_index_file()); |
83adac3c | 310 | if (newfd < 0) |
2de381f9 | 311 | die("unable to create new cachefile"); |
83adac3c | 312 | |
ca016f0e | 313 | merge = 0; |
438195cc | 314 | reset = 0; |
83adac3c LT |
315 | for (i = 1; i < argc; i++) { |
316 | const char *arg = argv[i]; | |
317 | ||
220a0b52 LT |
318 | /* "-u" means "update", meaning that a merge will update the working directory */ |
319 | if (!strcmp(arg, "-u")) { | |
320 | update = 1; | |
321 | continue; | |
322 | } | |
323 | ||
438195cc LT |
324 | /* This differs from "-m" in that we'll silently ignore unmerged entries */ |
325 | if (!strcmp(arg, "--reset")) { | |
326 | if (stage || merge) | |
327 | usage(read_tree_usage); | |
328 | reset = 1; | |
329 | merge = 1; | |
330 | stage = 1; | |
331 | read_cache_unmerged(); | |
332 | } | |
333 | ||
d99082e0 | 334 | /* "-m" stands for "merge", meaning we start in stage 1 */ |
83adac3c | 335 | if (!strcmp(arg, "-m")) { |
438195cc LT |
336 | if (stage || merge) |
337 | usage(read_tree_usage); | |
338 | if (read_cache_unmerged()) | |
339 | die("you need to resolve your current index first"); | |
d99082e0 | 340 | stage = 1; |
ca016f0e | 341 | merge = 1; |
83adac3c LT |
342 | continue; |
343 | } | |
3c249c95 | 344 | if (get_sha1(arg, sha1) < 0) |
c5bac17a | 345 | usage(read_tree_usage); |
d99082e0 | 346 | if (stage > 3) |
c5bac17a | 347 | usage(read_tree_usage); |
94537c78 | 348 | if (unpack_tree(sha1) < 0) |
2de381f9 | 349 | die("failed to unpack tree object %s", arg); |
d99082e0 | 350 | stage++; |
83adac3c | 351 | } |
a57f0b58 LT |
352 | if (update && !merge) |
353 | usage(read_tree_usage); | |
ca016f0e | 354 | if (merge) { |
45132251 LT |
355 | static const merge_fn_t merge_function[] = { |
356 | [1] = oneway_merge, | |
357 | [2] = twoway_merge, | |
358 | [3] = threeway_merge, | |
359 | }; | |
360 | if (stage < 2 || stage > 4) | |
a3a65234 | 361 | die("just how do you expect me to merge %d trees?", stage-1); |
45132251 | 362 | merge_cache(active_cache, active_nr, merge_function[stage-1]); |
ca016f0e | 363 | } |
96cd5429 JH |
364 | if (write_cache(newfd, active_cache, active_nr) || |
365 | commit_index_file(&cache_file)) | |
2de381f9 | 366 | die("unable to write new index file"); |
9614b8dc | 367 | return 0; |
e83c5163 | 368 | } |