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 char*lockfile_name; 26 27static voidremove_lock_file(void) 28{ 29if(lockfile_name) 30unlink(lockfile_name); 31} 32 33static intpath_matches(struct cache_entry *a,struct cache_entry *b) 34{ 35int len =ce_namelen(a); 36returnce_namelen(b) == len && 37!memcmp(a->name, b->name, len); 38} 39 40static intsame(struct cache_entry *a,struct cache_entry *b) 41{ 42return a->ce_mode == b->ce_mode && 43!memcmp(a->sha1, b->sha1,20); 44} 45 46 47/* 48 * This removes all trivial merges that don't change the tree 49 * and collapses them to state 0. 50 * 51 * _Any_ other merge is left to user policy. That includes "both 52 * created the same file", and "both removed the same file" - which are 53 * trivial, but the user might still want to _note_ it. 54 */ 55static struct cache_entry *merge_entries(struct cache_entry *a, 56struct cache_entry *b, 57struct cache_entry *c) 58{ 59int len =ce_namelen(a); 60 61/* 62 * Are they all the same filename? We won't do 63 * any name merging 64 */ 65if(ce_namelen(b) != len || 66ce_namelen(c) != len || 67memcmp(a->name, b->name, len) || 68memcmp(a->name, c->name, len)) 69return NULL; 70 71/* 72 * Ok, all three entries describe the same 73 * filename, but maybe the contents or file 74 * mode have changed? 75 * 76 * The trivial cases end up being the ones where two 77 * out of three files are the same: 78 * - both destinations the same, trivially take either 79 * - one of the destination versions hasn't changed, 80 * take the other. 81 * 82 * The "all entries exactly the same" case falls out as 83 * a special case of any of the "two same" cases. 84 * 85 * Here "a" is "original", and "b" and "c" are the two 86 * trees we are merging. 87 */ 88if(same(b,c)) 89return c; 90if(same(a,b)) 91return c; 92if(same(a,c)) 93return b; 94return NULL; 95} 96 97/* 98 * When a CE gets turned into an unmerged entry, we 99 * want it to be up-to-date 100 */ 101static voidverify_uptodate(struct cache_entry *ce) 102{ 103struct stat st; 104 105if(!lstat(ce->name, &st)) { 106unsigned changed =ce_match_stat(ce, &st); 107if(!changed) 108return; 109 errno =0; 110} 111if(errno == ENOENT) 112return; 113die("Entry '%s' not uptodate. Cannot merge.", ce->name); 114} 115 116/* 117 * If the old tree contained a CE that isn't even in the 118 * result, that's always a problem, regardless of whether 119 * it's up-to-date or not (ie it can be a file that we 120 * have updated but not committed yet). 121 */ 122static voidreject_merge(struct cache_entry *ce) 123{ 124die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name); 125} 126 127#define CHECK_OLD(ce) if (old && same(old, ce)) { verify_uptodate(old); old = NULL; } 128 129static voidtrivially_merge_cache(struct cache_entry **src,int nr) 130{ 131struct cache_entry **dst = src; 132struct cache_entry *old = NULL; 133 134while(nr--) { 135struct cache_entry *ce, *result; 136 137 ce = *src++; 138 139/* We throw away original cache entries except for the stat information */ 140if(!ce_stage(ce)) { 141if(old) 142reject_merge(old); 143 old = ce; 144 active_nr--; 145continue; 146} 147if(old && !path_matches(old, ce)) 148reject_merge(old); 149if(nr >2&& (result =merge_entries(ce, src[1], src[2])) != NULL) { 150 result->ce_flags |=htons(CE_UPDATE); 151/* 152 * See if we can re-use the old CE directly? 153 * That way we get the uptodate stat info. 154 * 155 * This also removes the UPDATE flag on 156 * a match. 157 */ 158if(old &&same(old, result)) { 159*result = *old; 160 old = NULL; 161} 162CHECK_OLD(ce); 163CHECK_OLD(src[1]); 164CHECK_OLD(src[2]); 165 ce = result; 166 ce->ce_flags &= ~htons(CE_STAGEMASK); 167 src +=2; 168 nr -=2; 169 active_nr -=2; 170} 171 172/* 173 * If we had an old entry that we now effectively 174 * overwrite, make sure it wasn't dirty. 175 */ 176CHECK_OLD(ce); 177*dst++ = ce; 178} 179if(old) 180reject_merge(old); 181} 182 183/* 184 * Two-way merge. 185 * 186 * The rule is: 187 * - every current entry has to match the old tree 188 * - if the current entry matches the new tree, we leave it 189 * as-is. Otherwise we require that it be up-to-date. 190 */ 191static voidtwoway_merge(struct cache_entry **src,int nr) 192{ 193static struct cache_entry null_entry; 194struct cache_entry *old = NULL, *stat = &null_entry; 195struct cache_entry **dst = src; 196 197while(nr--) { 198struct cache_entry *ce = *src++; 199int stage =ce_stage(ce); 200 201switch(stage) { 202case0: 203if(old) 204reject_merge(old); 205 old = ce; 206 stat = ce; 207 active_nr--; 208continue; 209 210case1: 211 active_nr--; 212if(!old) 213continue; 214if(!path_matches(old, ce) || !same(old, ce)) 215reject_merge(old); 216continue; 217 218case2: 219 ce->ce_flags |=htons(CE_UPDATE); 220if(old) { 221if(!path_matches(old, ce)) 222reject_merge(old); 223/* 224 * This also removes the UPDATE flag on 225 * a match 226 */ 227if(same(old, ce)) 228*ce = *old; 229else 230verify_uptodate(old); 231 old = NULL; 232} 233 ce->ce_flags &= ~htons(CE_STAGEMASK); 234*dst++ = ce; 235continue; 236} 237die("impossible two-way stage"); 238} 239if(old) 240reject_merge(old); 241} 242 243static voidmerge_stat_info(struct cache_entry **src,int nr) 244{ 245static struct cache_entry null_entry; 246struct cache_entry **dst = src; 247struct cache_entry *stat = &null_entry; 248 249while(nr--) { 250struct cache_entry *ce = *src++; 251 252/* We throw away original cache entries except for the stat information */ 253if(!ce_stage(ce)) { 254 stat = ce; 255 active_nr--; 256continue; 257} 258if(path_matches(ce, stat) &&same(ce, stat)) 259*ce = *stat; 260 ce->ce_flags &= ~htons(CE_STAGEMASK); 261*dst++ = ce; 262} 263} 264 265static voidcheck_updates(struct cache_entry **src,int nr) 266{ 267static struct checkout state = { 268.base_dir ="", 269.force =1, 270.quiet =1, 271.refresh_cache =1, 272}; 273unsigned short mask =htons(CE_UPDATE); 274while(nr--) { 275struct cache_entry *ce = *src++; 276if(ce->ce_flags & mask) { 277 ce->ce_flags &= ~mask; 278if(update) 279checkout_entry(ce, &state); 280} 281} 282} 283 284static char*read_tree_usage ="git-read-tree (<sha> | -m <sha1> [<sha2> [<sha3>]])"; 285 286intmain(int argc,char**argv) 287{ 288int i, newfd, merge; 289unsigned char sha1[20]; 290static char lockfile[MAXPATHLEN+1]; 291const char*indexfile =get_index_file(); 292 293snprintf(lockfile,sizeof(lockfile),"%s.lock", indexfile); 294 295 newfd =open(lockfile, O_RDWR | O_CREAT | O_EXCL,0600); 296if(newfd <0) 297die("unable to create new cachefile"); 298atexit(remove_lock_file); 299 lockfile_name = lockfile; 300 301 merge =0; 302for(i =1; i < argc; i++) { 303const char*arg = argv[i]; 304 305/* "-u" means "update", meaning that a merge will update the working directory */ 306if(!strcmp(arg,"-u")) { 307 update =1; 308continue; 309} 310 311/* "-m" stands for "merge", meaning we start in stage 1 */ 312if(!strcmp(arg,"-m")) { 313int i; 314if(stage) 315die("-m needs to come first"); 316read_cache(); 317for(i =0; i < active_nr; i++) { 318if(ce_stage(active_cache[i])) 319die("you need to resolve your current index first"); 320} 321 stage =1; 322 merge =1; 323continue; 324} 325if(get_sha1(arg, sha1) <0) 326usage(read_tree_usage); 327if(stage >3) 328usage(read_tree_usage); 329if(unpack_tree(sha1) <0) 330die("failed to unpack tree object%s", arg); 331 stage++; 332} 333if(merge) { 334switch(stage) { 335case4:/* Three-way merge */ 336trivially_merge_cache(active_cache, active_nr); 337check_updates(active_cache, active_nr); 338break; 339case3:/* Update from one tree to another */ 340twoway_merge(active_cache, active_nr); 341check_updates(active_cache, active_nr); 342break; 343case2:/* Just read a tree, merge with old cache contents */ 344merge_stat_info(active_cache, active_nr); 345break; 346default: 347die("just how do you expect me to merge%dtrees?", stage-1); 348} 349} 350if(write_cache(newfd, active_cache, active_nr) ||rename(lockfile, indexfile)) 351die("unable to write new index file"); 352 lockfile_name = NULL; 353return0; 354}