1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6#include "cache.h" 7 8static int stage = 0; 9static int update = 0; 10 11static int unpack_tree(unsigned char *sha1) 12{ 13 void *buffer; 14 unsigned long size; 15 int ret; 16 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; 23} 24 25static 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 32static 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 39/* 40 * This removes all trivial merges that don't change the tree 41 * and collapses them to state 0. 42 * 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. 46 */ 47static struct cache_entry *merge_entries(struct cache_entry *a, 48 struct cache_entry *b, 49 struct cache_entry *c) 50{ 51 int len = ce_namelen(a); 52 53 /* 54 * Are they all the same filename? We won't do 55 * any name merging 56 */ 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)) 61 return NULL; 62 63 /* 64 * Ok, all three entries describe the same 65 * filename, but maybe the contents or file 66 * mode have changed? 67 * 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, 72 * take the other. 73 * 74 * The "all entries exactly the same" case falls out as 75 * a special case of any of the "two same" cases. 76 * 77 * Here "a" is "original", and "b" and "c" are the two 78 * trees we are merging. 79 */ 80 if (same(b,c)) 81 return c; 82 if (same(a,b)) 83 return c; 84 if (same(a,c)) 85 return b; 86 return NULL; 87} 88 89/* 90 * When a CE gets turned into an unmerged entry, we 91 * want it to be up-to-date 92 */ 93static void verify_uptodate(struct cache_entry *ce) 94{ 95 struct stat st; 96 97 if (!lstat(ce->name, &st)) { 98 unsigned changed = ce_match_stat(ce, &st); 99 if (!changed) 100 return; 101 errno = 0; 102 } 103 if (errno == ENOENT) 104 return; 105 die("Entry '%s' not uptodate. Cannot merge.", ce->name); 106} 107 108/* 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). 113 */ 114static void reject_merge(struct cache_entry *ce) 115{ 116 die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name); 117} 118 119#define CHECK_OLD(ce) if (old && same(old, ce)) { verify_uptodate(old); old = NULL; } 120 121static void trivially_merge_cache(struct cache_entry **src, int nr) 122{ 123 struct cache_entry **dst = src; 124 struct cache_entry *old = NULL; 125 126 while (nr--) { 127 struct cache_entry *ce, *result; 128 129 ce = *src++; 130 131 /* We throw away original cache entries except for the stat information */ 132 if (!ce_stage(ce)) { 133 if (old) 134 reject_merge(old); 135 old = ce; 136 active_nr--; 137 continue; 138 } 139 if (old && !path_matches(old, ce)) 140 reject_merge(old); 141 if (nr > 1 && (result = merge_entries(ce, src[0], src[1])) != NULL) { 142 result->ce_flags |= htons(CE_UPDATE); 143 /* 144 * See if we can re-use the old CE directly? 145 * That way we get the uptodate stat info. 146 * 147 * This also removes the UPDATE flag on 148 * a match. 149 */ 150 if (old && same(old, result)) { 151 *result = *old; 152 old = NULL; 153 } 154 CHECK_OLD(ce); 155 CHECK_OLD(src[0]); 156 CHECK_OLD(src[1]); 157 ce = result; 158 ce->ce_flags &= ~htons(CE_STAGEMASK); 159 src += 2; 160 nr -= 2; 161 active_nr -= 2; 162 } 163 164 /* 165 * If we had an old entry that we now effectively 166 * overwrite, make sure it wasn't dirty. 167 */ 168 CHECK_OLD(ce); 169 *dst++ = ce; 170 } 171 if (old) 172 reject_merge(old); 173} 174 175/* 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. 180 * 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 184 * merge.. 185 */ 186static void twoway_check(struct cache_entry *old, int seen_stage1, struct cache_entry *ce) 187{ 188 if (path_matches(old, ce)) { 189 /* 190 * This also removes the UPDATE flag on 191 * a match 192 */ 193 if (same(old, ce)) { 194 *ce = *old; 195 return; 196 } 197 if (!seen_stage1) 198 reject_merge(old); 199 } 200 verify_uptodate(old); 201} 202 203/* 204 * Two-way merge. 205 * 206 * The rule is: 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. 210 */ 211static void twoway_merge(struct cache_entry **src, int nr) 212{ 213 int seen_stage1 = 0; 214 struct cache_entry *old = NULL; 215 struct cache_entry **dst = src; 216 217 while (nr--) { 218 struct cache_entry *ce = *src++; 219 int stage = ce_stage(ce); 220 221 switch (stage) { 222 case 0: 223 if (old) 224 reject_merge(old); 225 old = ce; 226 seen_stage1 = 0; 227 active_nr--; 228 continue; 229 230 case 1: 231 active_nr--; 232 if (!old) 233 continue; 234 if (!path_matches(old, ce) || !same(old, ce)) 235 reject_merge(old); 236 seen_stage1 = 1; 237 continue; 238 239 case 2: 240 ce->ce_flags |= htons(CE_UPDATE); 241 if (old) { 242 twoway_check(old, seen_stage1, ce); 243 old = NULL; 244 } 245 ce->ce_flags &= ~htons(CE_STAGEMASK); 246 *dst++ = ce; 247 continue; 248 } 249 die("impossible two-way stage"); 250 } 251 252 /* 253 * Unmatched with a new entry? Make sure it was 254 * at least uptodate in the working directory _and_ 255 * the original tree.. 256 */ 257 if (old) { 258 if (!seen_stage1) 259 reject_merge(old); 260 verify_uptodate(old); 261 } 262} 263 264static void merge_stat_info(struct cache_entry **src, int nr) 265{ 266 static struct cache_entry null_entry; 267 struct cache_entry **dst = src; 268 struct cache_entry *stat = &null_entry; 269 270 while (nr--) { 271 struct cache_entry *ce = *src++; 272 273 /* We throw away original cache entries except for the stat information */ 274 if (!ce_stage(ce)) { 275 stat = ce; 276 active_nr--; 277 continue; 278 } 279 if (path_matches(ce, stat) && same(ce, stat)) 280 *ce = *stat; 281 ce->ce_flags &= ~htons(CE_STAGEMASK); 282 *dst++ = ce; 283 } 284} 285 286static void check_updates(struct cache_entry **src, int nr) 287{ 288 static struct checkout state = { 289 .base_dir = "", 290 .force = 1, 291 .quiet = 1, 292 .refresh_cache = 1, 293 }; 294 unsigned short mask = htons(CE_UPDATE); 295 while (nr--) { 296 struct cache_entry *ce = *src++; 297 if (ce->ce_flags & mask) { 298 ce->ce_flags &= ~mask; 299 if (update) 300 checkout_entry(ce, &state); 301 } 302 } 303} 304 305static char *read_tree_usage = "git-read-tree (<sha> | -m <sha1> [<sha2> [<sha3>]])"; 306 307static struct cache_file cache_file; 308 309int main(int argc, char **argv) 310{ 311 int i, newfd, merge; 312 unsigned char sha1[20]; 313 314 newfd = hold_index_file_for_update(&cache_file, get_index_file()); 315 if (newfd < 0) 316 die("unable to create new cachefile"); 317 318 merge = 0; 319 for (i = 1; i < argc; i++) { 320 const char *arg = argv[i]; 321 322 /* "-u" means "update", meaning that a merge will update the working directory */ 323 if (!strcmp(arg, "-u")) { 324 update = 1; 325 continue; 326 } 327 328 /* "-m" stands for "merge", meaning we start in stage 1 */ 329 if (!strcmp(arg, "-m")) { 330 int i; 331 if (stage) 332 die("-m needs to come first"); 333 read_cache(); 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"); 337 } 338 stage = 1; 339 merge = 1; 340 continue; 341 } 342 if (get_sha1(arg, sha1) < 0) 343 usage(read_tree_usage); 344 if (stage > 3) 345 usage(read_tree_usage); 346 if (unpack_tree(sha1) < 0) 347 die("failed to unpack tree object %s", arg); 348 stage++; 349 } 350 if (merge) { 351 switch (stage) { 352 case 4: /* Three-way merge */ 353 trivially_merge_cache(active_cache, active_nr); 354 check_updates(active_cache, active_nr); 355 break; 356 case 3: /* Update from one tree to another */ 357 twoway_merge(active_cache, active_nr); 358 check_updates(active_cache, active_nr); 359 break; 360 case 2: /* Just read a tree, merge with old cache contents */ 361 merge_stat_info(active_cache, active_nr); 362 break; 363 default: 364 die("just how do you expect me to merge %d trees?", stage-1); 365 } 366 } 367 if (write_cache(newfd, active_cache, active_nr) || 368 commit_index_file(&cache_file)) 369 die("unable to write new index file"); 370 return 0; 371}