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 */ 43static struct cache_entry *merge_entries(struct cache_entry *a, 44struct cache_entry *b, 45struct cache_entry *c) 46{ 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 */ 64if(a && b && c) { 65if(same(b,c)) 66return c; 67if(same(a,b)) 68return c; 69if(same(a,c)) 70return b; 71} 72return NULL; 73} 74 75/* 76 * When a CE gets turned into an unmerged entry, we 77 * want it to be up-to-date 78 */ 79static voidverify_uptodate(struct cache_entry *ce) 80{ 81struct stat st; 82 83if(!lstat(ce->name, &st)) { 84unsigned changed =ce_match_stat(ce, &st); 85if(!changed) 86return; 87 errno =0; 88} 89if(errno == ENOENT) 90return; 91die("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 */ 100static voidreject_merge(struct cache_entry *ce) 101{ 102die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name); 103} 104 105static intmerged_entry(struct cache_entry *merge,struct cache_entry *old,struct cache_entry **dst) 106{ 107 merge->ce_flags |=htons(CE_UPDATE); 108if(old) { 109/* 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. 115 */ 116if(same(old, merge)) { 117*merge = *old; 118}else{ 119verify_uptodate(old); 120} 121} 122 merge->ce_flags &= ~htons(CE_STAGEMASK); 123*dst++ = merge; 124return1; 125} 126 127static intthreeway_merge(struct cache_entry *stages[4],struct cache_entry **dst) 128{ 129struct cache_entry *old = stages[0]; 130struct cache_entry *a = stages[1], *b = stages[2], *c = stages[3]; 131struct cache_entry *merge; 132int 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 */ 139if(old) { 140if(!b || !same(old, b)) 141return-1; 142} 143 merge =merge_entries(a, b, c); 144if(merge) 145returnmerged_entry(merge, old, dst); 146if(old) 147verify_uptodate(old); 148 count =0; 149if(a) { *dst++ = a; count++; } 150if(b) { *dst++ = b; count++; } 151if(c) { *dst++ = c; count++; } 152return count; 153} 154 155/* 156 * Two-way merge. 157 * 158 * The rule is: 159 * - every current entry has to match the old tree 160 * - if the current entry matches the new tree, we leave it 161 * as-is. Otherwise we require that it be up-to-date. 162 */ 163static inttwoway_merge(struct cache_entry **src,struct cache_entry **dst) 164{ 165struct cache_entry *old = src[0]; 166struct cache_entry *a = src[1], *b = src[2]; 167 168if(src[3]) 169return-1; 170 171if(old) { 172if(!a || !same(old, a)) 173return-1; 174} 175if(b) 176returnmerged_entry(b, old, dst); 177if(old) 178verify_uptodate(old); 179return0; 180} 181 182/* 183 * One-way merge. 184 * 185 * The rule is: 186 * - take the stat information from stage0, take the data from stage1 187 */ 188static intoneway_merge(struct cache_entry **src,struct cache_entry **dst) 189{ 190struct cache_entry *old = src[0]; 191struct cache_entry *a = src[1]; 192 193if(src[2] || src[3]) 194return-1; 195 196if(!a) 197return0; 198if(old &&same(old, a)) 199*a = *old; 200 a->ce_flags &= ~htons(CE_STAGEMASK); 201*dst++ = a; 202return1; 203} 204 205static voidcheck_updates(struct cache_entry **src,int nr) 206{ 207static struct checkout state = { 208.base_dir ="", 209.force =1, 210.quiet =1, 211.refresh_cache =1, 212}; 213unsigned short mask =htons(CE_UPDATE); 214while(nr--) { 215struct cache_entry *ce = *src++; 216if(ce->ce_flags & mask) { 217 ce->ce_flags &= ~mask; 218if(update) 219checkout_entry(ce, &state); 220} 221} 222} 223 224typedefint(*merge_fn_t)(struct cache_entry **,struct cache_entry **); 225 226static voidmerge_cache(struct cache_entry **src,int nr, merge_fn_t fn) 227{ 228struct cache_entry **dst = src; 229 230while(nr) { 231int entries; 232struct cache_entry *name, *ce, *stages[4] = { NULL, }; 233 234 name = ce = *src; 235for(;;) { 236int stage =ce_stage(ce); 237 stages[stage] = ce; 238 ce = *++src; 239 active_nr--; 240if(!--nr) 241break; 242if(!path_matches(ce, name)) 243break; 244} 245 246 entries =fn(stages, dst); 247if(entries <0) 248reject_merge(name); 249 dst += entries; 250 active_nr += entries; 251} 252check_updates(active_cache, active_nr); 253} 254 255static char*read_tree_usage ="git-read-tree (<sha> | -m <sha1> [<sha2> [<sha3>]])"; 256 257static struct cache_file cache_file; 258 259intmain(int argc,char**argv) 260{ 261int i, newfd, merge; 262unsigned char sha1[20]; 263 264 newfd =hold_index_file_for_update(&cache_file,get_index_file()); 265if(newfd <0) 266die("unable to create new cachefile"); 267 268 merge =0; 269for(i =1; i < argc; i++) { 270const char*arg = argv[i]; 271 272/* "-u" means "update", meaning that a merge will update the working directory */ 273if(!strcmp(arg,"-u")) { 274 update =1; 275continue; 276} 277 278/* "-m" stands for "merge", meaning we start in stage 1 */ 279if(!strcmp(arg,"-m")) { 280int i; 281if(stage) 282die("-m needs to come first"); 283read_cache(); 284for(i =0; i < active_nr; i++) { 285if(ce_stage(active_cache[i])) 286die("you need to resolve your current index first"); 287} 288 stage =1; 289 merge =1; 290continue; 291} 292if(get_sha1(arg, sha1) <0) 293usage(read_tree_usage); 294if(stage >3) 295usage(read_tree_usage); 296if(unpack_tree(sha1) <0) 297die("failed to unpack tree object%s", arg); 298 stage++; 299} 300if(merge) { 301static const merge_fn_t merge_function[] = { 302[1] = oneway_merge, 303[2] = twoway_merge, 304[3] = threeway_merge, 305}; 306if(stage <2|| stage >4) 307die("just how do you expect me to merge%dtrees?", stage-1); 308merge_cache(active_cache, active_nr, merge_function[stage-1]); 309} 310if(write_cache(newfd, active_cache, active_nr) || 311commit_index_file(&cache_file)) 312die("unable to write new index file"); 313return0; 314}