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 intunpack_tree(unsigned char*sha1) 12{ 13void*buffer; 14unsigned long size; 15int ret; 16 17 buffer =read_object_with_reference(sha1,"tree", &size, NULL); 18if(!buffer) 19return-1; 20 ret =read_tree(buffer, size, stage); 21free(buffer); 22return ret; 23} 24 25static intpath_matches(struct cache_entry *a,struct cache_entry *b) 26{ 27int len =ce_namelen(a); 28returnce_namelen(b) == len && 29!memcmp(a->name, b->name, len); 30} 31 32static intsame(struct cache_entry *a,struct cache_entry *b) 33{ 34return 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, 48struct cache_entry *b, 49struct cache_entry *c) 50{ 51int len =ce_namelen(a); 52 53/* 54 * Are they all the same filename? We won't do 55 * any name merging 56 */ 57if(ce_namelen(b) != len || 58ce_namelen(c) != len || 59memcmp(a->name, b->name, len) || 60memcmp(a->name, c->name, len)) 61return 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 */ 80if(same(b,c)) 81return c; 82if(same(a,b)) 83return c; 84if(same(a,c)) 85return b; 86return 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 voidverify_uptodate(struct cache_entry *ce) 94{ 95struct stat st; 96 97if(!lstat(ce->name, &st)) { 98unsigned changed =ce_match_stat(ce, &st); 99if(!changed) 100return; 101 errno =0; 102} 103if(errno == ENOENT) 104return; 105die("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 voidreject_merge(struct cache_entry *ce) 115{ 116die("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 voidtrivially_merge_cache(struct cache_entry **src,int nr) 122{ 123struct cache_entry **dst = src; 124struct cache_entry *old = NULL; 125 126while(nr--) { 127struct cache_entry *ce, *result; 128 129 ce = *src++; 130 131/* We throw away original cache entries except for the stat information */ 132if(!ce_stage(ce)) { 133if(old) 134reject_merge(old); 135 old = ce; 136 active_nr--; 137continue; 138} 139if(old && !path_matches(old, ce)) 140reject_merge(old); 141if(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 */ 150if(old &&same(old, result)) { 151*result = *old; 152 old = NULL; 153} 154CHECK_OLD(ce); 155CHECK_OLD(src[0]); 156CHECK_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 */ 168CHECK_OLD(ce); 169*dst++ = ce; 170} 171if(old) 172reject_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 voidtwoway_check(struct cache_entry *old,int seen_stage1,struct cache_entry *ce) 187{ 188if(path_matches(old, ce)) { 189/* 190 * This also removes the UPDATE flag on 191 * a match 192 */ 193if(same(old, ce)) { 194*ce = *old; 195return; 196} 197if(!seen_stage1) 198reject_merge(old); 199} 200verify_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 voidtwoway_merge(struct cache_entry **src,int nr) 212{ 213int seen_stage1 =0; 214struct cache_entry *old = NULL; 215struct cache_entry **dst = src; 216 217while(nr--) { 218struct cache_entry *ce = *src++; 219int stage =ce_stage(ce); 220 221switch(stage) { 222case0: 223if(old) 224reject_merge(old); 225 old = ce; 226 seen_stage1 =0; 227 active_nr--; 228continue; 229 230case1: 231 active_nr--; 232if(!old) 233continue; 234if(!path_matches(old, ce) || !same(old, ce)) 235reject_merge(old); 236 seen_stage1 =1; 237continue; 238 239case2: 240 ce->ce_flags |=htons(CE_UPDATE); 241if(old) { 242twoway_check(old, seen_stage1, ce); 243 old = NULL; 244} 245 ce->ce_flags &= ~htons(CE_STAGEMASK); 246*dst++ = ce; 247continue; 248} 249die("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 */ 257if(old) { 258if(!seen_stage1) 259reject_merge(old); 260verify_uptodate(old); 261} 262} 263 264static voidmerge_stat_info(struct cache_entry **src,int nr) 265{ 266static struct cache_entry null_entry; 267struct cache_entry **dst = src; 268struct cache_entry *stat = &null_entry; 269 270while(nr--) { 271struct cache_entry *ce = *src++; 272 273/* We throw away original cache entries except for the stat information */ 274if(!ce_stage(ce)) { 275 stat = ce; 276 active_nr--; 277continue; 278} 279if(path_matches(ce, stat) &&same(ce, stat)) 280*ce = *stat; 281 ce->ce_flags &= ~htons(CE_STAGEMASK); 282*dst++ = ce; 283} 284} 285 286static voidcheck_updates(struct cache_entry **src,int nr) 287{ 288static struct checkout state = { 289.base_dir ="", 290.force =1, 291.quiet =1, 292.refresh_cache =1, 293}; 294unsigned short mask =htons(CE_UPDATE); 295while(nr--) { 296struct cache_entry *ce = *src++; 297if(ce->ce_flags & mask) { 298 ce->ce_flags &= ~mask; 299if(update) 300checkout_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 309intmain(int argc,char**argv) 310{ 311int i, newfd, merge; 312unsigned char sha1[20]; 313 314 newfd =hold_index_file_for_update(&cache_file,get_index_file()); 315if(newfd <0) 316die("unable to create new cachefile"); 317 318 merge =0; 319for(i =1; i < argc; i++) { 320const char*arg = argv[i]; 321 322/* "-u" means "update", meaning that a merge will update the working directory */ 323if(!strcmp(arg,"-u")) { 324 update =1; 325continue; 326} 327 328/* "-m" stands for "merge", meaning we start in stage 1 */ 329if(!strcmp(arg,"-m")) { 330int i; 331if(stage) 332die("-m needs to come first"); 333read_cache(); 334for(i =0; i < active_nr; i++) { 335if(ce_stage(active_cache[i])) 336die("you need to resolve your current index first"); 337} 338 stage =1; 339 merge =1; 340continue; 341} 342if(get_sha1(arg, sha1) <0) 343usage(read_tree_usage); 344if(stage >3) 345usage(read_tree_usage); 346if(unpack_tree(sha1) <0) 347die("failed to unpack tree object%s", arg); 348 stage++; 349} 350if(merge) { 351switch(stage) { 352case4:/* Three-way merge */ 353trivially_merge_cache(active_cache, active_nr); 354check_updates(active_cache, active_nr); 355break; 356case3:/* Update from one tree to another */ 357twoway_merge(active_cache, active_nr); 358check_updates(active_cache, active_nr); 359break; 360case2:/* Just read a tree, merge with old cache contents */ 361merge_stat_info(active_cache, active_nr); 362break; 363default: 364die("just how do you expect me to merge%dtrees?", stage-1); 365} 366} 367if(write_cache(newfd, active_cache, active_nr) || 368commit_index_file(&cache_file)) 369die("unable to write new index file"); 370return0; 371}