1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6#define NO_THE_INDEX_COMPATIBILITY_MACROS 7#include"cache.h" 8#include"cache-tree.h" 9#include"refs.h" 10#include"dir.h" 11#include"tree.h" 12#include"commit.h" 13#include"diff.h" 14#include"diffcore.h" 15#include"revision.h" 16#include"blob.h" 17 18static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really); 19 20/* Index extensions. 21 * 22 * The first letter should be 'A'..'Z' for extensions that are not 23 * necessary for a correct operation (i.e. optimization data). 24 * When new extensions are added that _needs_ to be understood in 25 * order to correctly interpret the index file, pick character that 26 * is outside the range, to cause the reader to abort. 27 */ 28 29#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) 30#define CACHE_EXT_TREE 0x54524545/* "TREE" */ 31 32struct index_state the_index; 33 34static voidset_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 35{ 36 istate->cache[nr] = ce; 37add_name_hash(istate, ce); 38} 39 40static voidreplace_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 41{ 42struct cache_entry *old = istate->cache[nr]; 43 44remove_name_hash(old); 45set_index_entry(istate, nr, ce); 46 istate->cache_changed =1; 47} 48 49voidrename_index_entry_at(struct index_state *istate,int nr,const char*new_name) 50{ 51struct cache_entry *old = istate->cache[nr], *new; 52int namelen =strlen(new_name); 53 54new=xmalloc(cache_entry_size(namelen)); 55copy_cache_entry(new, old); 56new->ce_flags &= ~(CE_STATE_MASK | CE_NAMEMASK); 57new->ce_flags |= (namelen >= CE_NAMEMASK ? CE_NAMEMASK : namelen); 58memcpy(new->name, new_name, namelen +1); 59 60cache_tree_invalidate_path(istate->cache_tree, old->name); 61remove_index_entry_at(istate, nr); 62add_index_entry(istate,new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 63} 64 65/* 66 * This only updates the "non-critical" parts of the directory 67 * cache, ie the parts that aren't tracked by GIT, and only used 68 * to validate the cache. 69 */ 70voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 71{ 72 ce->ce_ctime.sec = (unsigned int)st->st_ctime; 73 ce->ce_mtime.sec = (unsigned int)st->st_mtime; 74 ce->ce_ctime.nsec =ST_CTIME_NSEC(*st); 75 ce->ce_mtime.nsec =ST_MTIME_NSEC(*st); 76 ce->ce_dev = st->st_dev; 77 ce->ce_ino = st->st_ino; 78 ce->ce_uid = st->st_uid; 79 ce->ce_gid = st->st_gid; 80 ce->ce_size = st->st_size; 81 82if(assume_unchanged) 83 ce->ce_flags |= CE_VALID; 84 85if(S_ISREG(st->st_mode)) 86ce_mark_uptodate(ce); 87} 88 89static intce_compare_data(struct cache_entry *ce,struct stat *st) 90{ 91int match = -1; 92int fd =open(ce->name, O_RDONLY); 93 94if(fd >=0) { 95unsigned char sha1[20]; 96if(!index_fd(sha1, fd, st,0, OBJ_BLOB, ce->name)) 97 match =hashcmp(sha1, ce->sha1); 98/* index_fd() closed the file descriptor already */ 99} 100return match; 101} 102 103static intce_compare_link(struct cache_entry *ce,size_t expected_size) 104{ 105int match = -1; 106void*buffer; 107unsigned long size; 108enum object_type type; 109struct strbuf sb = STRBUF_INIT; 110 111if(strbuf_readlink(&sb, ce->name, expected_size)) 112return-1; 113 114 buffer =read_sha1_file(ce->sha1, &type, &size); 115if(buffer) { 116if(size == sb.len) 117 match =memcmp(buffer, sb.buf, size); 118free(buffer); 119} 120strbuf_release(&sb); 121return match; 122} 123 124static intce_compare_gitlink(struct cache_entry *ce) 125{ 126unsigned char sha1[20]; 127 128/* 129 * We don't actually require that the .git directory 130 * under GITLINK directory be a valid git directory. It 131 * might even be missing (in case nobody populated that 132 * sub-project). 133 * 134 * If so, we consider it always to match. 135 */ 136if(resolve_gitlink_ref(ce->name,"HEAD", sha1) <0) 137return0; 138returnhashcmp(sha1, ce->sha1); 139} 140 141static intce_modified_check_fs(struct cache_entry *ce,struct stat *st) 142{ 143switch(st->st_mode & S_IFMT) { 144case S_IFREG: 145if(ce_compare_data(ce, st)) 146return DATA_CHANGED; 147break; 148case S_IFLNK: 149if(ce_compare_link(ce,xsize_t(st->st_size))) 150return DATA_CHANGED; 151break; 152case S_IFDIR: 153if(S_ISGITLINK(ce->ce_mode)) 154returnce_compare_gitlink(ce) ? DATA_CHANGED :0; 155default: 156return TYPE_CHANGED; 157} 158return0; 159} 160 161static intis_empty_blob_sha1(const unsigned char*sha1) 162{ 163static const unsigned char empty_blob_sha1[20] = { 1640xe6,0x9d,0xe2,0x9b,0xb2,0xd1,0xd6,0x43,0x4b,0x8b, 1650x29,0xae,0x77,0x5a,0xd8,0xc2,0xe4,0x8c,0x53,0x91 166}; 167 168return!hashcmp(sha1, empty_blob_sha1); 169} 170 171static intce_match_stat_basic(struct cache_entry *ce,struct stat *st) 172{ 173unsigned int changed =0; 174 175if(ce->ce_flags & CE_REMOVE) 176return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; 177 178switch(ce->ce_mode & S_IFMT) { 179case S_IFREG: 180 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 181/* We consider only the owner x bit to be relevant for 182 * "mode changes" 183 */ 184if(trust_executable_bit && 185(0100& (ce->ce_mode ^ st->st_mode))) 186 changed |= MODE_CHANGED; 187break; 188case S_IFLNK: 189if(!S_ISLNK(st->st_mode) && 190(has_symlinks || !S_ISREG(st->st_mode))) 191 changed |= TYPE_CHANGED; 192break; 193case S_IFGITLINK: 194/* We ignore most of the st_xxx fields for gitlinks */ 195if(!S_ISDIR(st->st_mode)) 196 changed |= TYPE_CHANGED; 197else if(ce_compare_gitlink(ce)) 198 changed |= DATA_CHANGED; 199return changed; 200default: 201die("internal error: ce_mode is%o", ce->ce_mode); 202} 203if(ce->ce_mtime.sec != (unsigned int)st->st_mtime) 204 changed |= MTIME_CHANGED; 205if(trust_ctime && ce->ce_ctime.sec != (unsigned int)st->st_ctime) 206 changed |= CTIME_CHANGED; 207 208#ifdef USE_NSEC 209if(ce->ce_mtime.nsec !=ST_MTIME_NSEC(*st)) 210 changed |= MTIME_CHANGED; 211if(trust_ctime && ce->ce_ctime.nsec !=ST_CTIME_NSEC(*st)) 212 changed |= CTIME_CHANGED; 213#endif 214 215if(ce->ce_uid != (unsigned int) st->st_uid || 216 ce->ce_gid != (unsigned int) st->st_gid) 217 changed |= OWNER_CHANGED; 218if(ce->ce_ino != (unsigned int) st->st_ino) 219 changed |= INODE_CHANGED; 220 221#ifdef USE_STDEV 222/* 223 * st_dev breaks on network filesystems where different 224 * clients will have different views of what "device" 225 * the filesystem is on 226 */ 227if(ce->ce_dev != (unsigned int) st->st_dev) 228 changed |= INODE_CHANGED; 229#endif 230 231if(ce->ce_size != (unsigned int) st->st_size) 232 changed |= DATA_CHANGED; 233 234/* Racily smudged entry? */ 235if(!ce->ce_size) { 236if(!is_empty_blob_sha1(ce->sha1)) 237 changed |= DATA_CHANGED; 238} 239 240return changed; 241} 242 243static intis_racy_timestamp(const struct index_state *istate,struct cache_entry *ce) 244{ 245return(!S_ISGITLINK(ce->ce_mode) && 246 istate->timestamp.sec && 247#ifdef USE_NSEC 248/* nanosecond timestamped files can also be racy! */ 249(istate->timestamp.sec < ce->ce_mtime.sec || 250(istate->timestamp.sec == ce->ce_mtime.sec && 251 istate->timestamp.nsec <= ce->ce_mtime.nsec)) 252#else 253 istate->timestamp.sec <= ce->ce_mtime.sec 254#endif 255); 256} 257 258intie_match_stat(const struct index_state *istate, 259struct cache_entry *ce,struct stat *st, 260unsigned int options) 261{ 262unsigned int changed; 263int ignore_valid = options & CE_MATCH_IGNORE_VALID; 264int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 265 266/* 267 * If it's marked as always valid in the index, it's 268 * valid whatever the checked-out copy says. 269 */ 270if(!ignore_valid && (ce->ce_flags & CE_VALID)) 271return0; 272 273/* 274 * Intent-to-add entries have not been added, so the index entry 275 * by definition never matches what is in the work tree until it 276 * actually gets added. 277 */ 278if(ce->ce_flags & CE_INTENT_TO_ADD) 279return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; 280 281 changed =ce_match_stat_basic(ce, st); 282 283/* 284 * Within 1 second of this sequence: 285 * echo xyzzy >file && git-update-index --add file 286 * running this command: 287 * echo frotz >file 288 * would give a falsely clean cache entry. The mtime and 289 * length match the cache, and other stat fields do not change. 290 * 291 * We could detect this at update-index time (the cache entry 292 * being registered/updated records the same time as "now") 293 * and delay the return from git-update-index, but that would 294 * effectively mean we can make at most one commit per second, 295 * which is not acceptable. Instead, we check cache entries 296 * whose mtime are the same as the index file timestamp more 297 * carefully than others. 298 */ 299if(!changed &&is_racy_timestamp(istate, ce)) { 300if(assume_racy_is_modified) 301 changed |= DATA_CHANGED; 302else 303 changed |=ce_modified_check_fs(ce, st); 304} 305 306return changed; 307} 308 309intie_modified(const struct index_state *istate, 310struct cache_entry *ce,struct stat *st,unsigned int options) 311{ 312int changed, changed_fs; 313 314 changed =ie_match_stat(istate, ce, st, options); 315if(!changed) 316return0; 317/* 318 * If the mode or type has changed, there's no point in trying 319 * to refresh the entry - it's not going to match 320 */ 321if(changed & (MODE_CHANGED | TYPE_CHANGED)) 322return changed; 323 324/* 325 * Immediately after read-tree or update-index --cacheinfo, 326 * the length field is zero, as we have never even read the 327 * lstat(2) information once, and we cannot trust DATA_CHANGED 328 * returned by ie_match_stat() which in turn was returned by 329 * ce_match_stat_basic() to signal that the filesize of the 330 * blob changed. We have to actually go to the filesystem to 331 * see if the contents match, and if so, should answer "unchanged". 332 * 333 * The logic does not apply to gitlinks, as ce_match_stat_basic() 334 * already has checked the actual HEAD from the filesystem in the 335 * subproject. If ie_match_stat() already said it is different, 336 * then we know it is. 337 */ 338if((changed & DATA_CHANGED) && 339(S_ISGITLINK(ce->ce_mode) || ce->ce_size !=0)) 340return changed; 341 342 changed_fs =ce_modified_check_fs(ce, st); 343if(changed_fs) 344return changed | changed_fs; 345return0; 346} 347 348intbase_name_compare(const char*name1,int len1,int mode1, 349const char*name2,int len2,int mode2) 350{ 351unsigned char c1, c2; 352int len = len1 < len2 ? len1 : len2; 353int cmp; 354 355 cmp =memcmp(name1, name2, len); 356if(cmp) 357return cmp; 358 c1 = name1[len]; 359 c2 = name2[len]; 360if(!c1 &&S_ISDIR(mode1)) 361 c1 ='/'; 362if(!c2 &&S_ISDIR(mode2)) 363 c2 ='/'; 364return(c1 < c2) ? -1: (c1 > c2) ?1:0; 365} 366 367/* 368 * df_name_compare() is identical to base_name_compare(), except it 369 * compares conflicting directory/file entries as equal. Note that 370 * while a directory name compares as equal to a regular file, they 371 * then individually compare _differently_ to a filename that has 372 * a dot after the basename (because '\0' < '.' < '/'). 373 * 374 * This is used by routines that want to traverse the git namespace 375 * but then handle conflicting entries together when possible. 376 */ 377intdf_name_compare(const char*name1,int len1,int mode1, 378const char*name2,int len2,int mode2) 379{ 380int len = len1 < len2 ? len1 : len2, cmp; 381unsigned char c1, c2; 382 383 cmp =memcmp(name1, name2, len); 384if(cmp) 385return cmp; 386/* Directories and files compare equal (same length, same name) */ 387if(len1 == len2) 388return0; 389 c1 = name1[len]; 390if(!c1 &&S_ISDIR(mode1)) 391 c1 ='/'; 392 c2 = name2[len]; 393if(!c2 &&S_ISDIR(mode2)) 394 c2 ='/'; 395if(c1 =='/'&& !c2) 396return0; 397if(c2 =='/'&& !c1) 398return0; 399return c1 - c2; 400} 401 402intcache_name_compare(const char*name1,int flags1,const char*name2,int flags2) 403{ 404int len1 = flags1 & CE_NAMEMASK; 405int len2 = flags2 & CE_NAMEMASK; 406int len = len1 < len2 ? len1 : len2; 407int cmp; 408 409 cmp =memcmp(name1, name2, len); 410if(cmp) 411return cmp; 412if(len1 < len2) 413return-1; 414if(len1 > len2) 415return1; 416 417/* Compare stages */ 418 flags1 &= CE_STAGEMASK; 419 flags2 &= CE_STAGEMASK; 420 421if(flags1 < flags2) 422return-1; 423if(flags1 > flags2) 424return1; 425return0; 426} 427 428intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 429{ 430int first, last; 431 432 first =0; 433 last = istate->cache_nr; 434while(last > first) { 435int next = (last + first) >>1; 436struct cache_entry *ce = istate->cache[next]; 437int cmp =cache_name_compare(name, namelen, ce->name, ce->ce_flags); 438if(!cmp) 439return next; 440if(cmp <0) { 441 last = next; 442continue; 443} 444 first = next+1; 445} 446return-first-1; 447} 448 449/* Remove entry, return true if there are more entries to go.. */ 450intremove_index_entry_at(struct index_state *istate,int pos) 451{ 452struct cache_entry *ce = istate->cache[pos]; 453 454remove_name_hash(ce); 455 istate->cache_changed =1; 456 istate->cache_nr--; 457if(pos >= istate->cache_nr) 458return0; 459memmove(istate->cache + pos, 460 istate->cache + pos +1, 461(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 462return1; 463} 464 465/* 466 * Remove all cache ententries marked for removal, that is where 467 * CE_REMOVE is set in ce_flags. This is much more effective than 468 * calling remove_index_entry_at() for each entry to be removed. 469 */ 470voidremove_marked_cache_entries(struct index_state *istate) 471{ 472struct cache_entry **ce_array = istate->cache; 473unsigned int i, j; 474 475for(i = j =0; i < istate->cache_nr; i++) { 476if(ce_array[i]->ce_flags & CE_REMOVE) 477remove_name_hash(ce_array[i]); 478else 479 ce_array[j++] = ce_array[i]; 480} 481 istate->cache_changed =1; 482 istate->cache_nr = j; 483} 484 485intremove_file_from_index(struct index_state *istate,const char*path) 486{ 487int pos =index_name_pos(istate, path,strlen(path)); 488if(pos <0) 489 pos = -pos-1; 490cache_tree_invalidate_path(istate->cache_tree, path); 491while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 492remove_index_entry_at(istate, pos); 493return0; 494} 495 496static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 497{ 498return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 499} 500 501static intindex_name_pos_also_unmerged(struct index_state *istate, 502const char*path,int namelen) 503{ 504int pos =index_name_pos(istate, path, namelen); 505struct cache_entry *ce; 506 507if(pos >=0) 508return pos; 509 510/* maybe unmerged? */ 511 pos = -1- pos; 512if(pos >= istate->cache_nr || 513compare_name((ce = istate->cache[pos]), path, namelen)) 514return-1; 515 516/* order of preference: stage 2, 1, 3 */ 517if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 518ce_stage((ce = istate->cache[pos +1])) ==2&& 519!compare_name(ce, path, namelen)) 520 pos++; 521return pos; 522} 523 524static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 525{ 526int len =ce_namelen(ce); 527returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 528} 529 530/* 531 * If we add a filename that aliases in the cache, we will use the 532 * name that we already have - but we don't want to update the same 533 * alias twice, because that implies that there were actually two 534 * different files with aliasing names! 535 * 536 * So we use the CE_ADDED flag to verify that the alias was an old 537 * one before we accept it as 538 */ 539static struct cache_entry *create_alias_ce(struct cache_entry *ce,struct cache_entry *alias) 540{ 541int len; 542struct cache_entry *new; 543 544if(alias->ce_flags & CE_ADDED) 545die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 546 547/* Ok, create the new entry using the name of the existing alias */ 548 len =ce_namelen(alias); 549new=xcalloc(1,cache_entry_size(len)); 550memcpy(new->name, alias->name, len); 551copy_cache_entry(new, ce); 552free(ce); 553return new; 554} 555 556static voidrecord_intent_to_add(struct cache_entry *ce) 557{ 558unsigned char sha1[20]; 559if(write_sha1_file("",0, blob_type, sha1)) 560die("cannot create an empty blob in the object database"); 561hashcpy(ce->sha1, sha1); 562} 563 564intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 565{ 566int size, namelen, was_same; 567 mode_t st_mode = st->st_mode; 568struct cache_entry *ce, *alias; 569unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY; 570int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 571int pretend = flags & ADD_CACHE_PRETEND; 572int intent_only = flags & ADD_CACHE_INTENT; 573int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| 574(intent_only ? ADD_CACHE_NEW_ONLY :0)); 575 576if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 577returnerror("%s: can only add regular files, symbolic links or git-directories", path); 578 579 namelen =strlen(path); 580if(S_ISDIR(st_mode)) { 581while(namelen && path[namelen-1] =='/') 582 namelen--; 583} 584 size =cache_entry_size(namelen); 585 ce =xcalloc(1, size); 586memcpy(ce->name, path, namelen); 587 ce->ce_flags = namelen; 588if(!intent_only) 589fill_stat_cache_info(ce, st); 590else 591 ce->ce_flags |= CE_INTENT_TO_ADD; 592 593if(trust_executable_bit && has_symlinks) 594 ce->ce_mode =create_ce_mode(st_mode); 595else{ 596/* If there is an existing entry, pick the mode bits and type 597 * from it, otherwise assume unexecutable regular file. 598 */ 599struct cache_entry *ent; 600int pos =index_name_pos_also_unmerged(istate, path, namelen); 601 602 ent = (0<= pos) ? istate->cache[pos] : NULL; 603 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 604} 605 606 alias =index_name_exists(istate, ce->name,ce_namelen(ce), ignore_case); 607if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 608/* Nothing changed, really */ 609free(ce); 610ce_mark_uptodate(alias); 611 alias->ce_flags |= CE_ADDED; 612return0; 613} 614if(!intent_only) { 615if(index_path(ce->sha1, path, st,1)) 616returnerror("unable to index file%s", path); 617}else 618record_intent_to_add(ce); 619 620if(ignore_case && alias &&different_name(ce, alias)) 621 ce =create_alias_ce(ce, alias); 622 ce->ce_flags |= CE_ADDED; 623 624/* It was suspected to be racily clean, but it turns out to be Ok */ 625 was_same = (alias && 626!ce_stage(alias) && 627!hashcmp(alias->sha1, ce->sha1) && 628 ce->ce_mode == alias->ce_mode); 629 630if(pretend) 631; 632else if(add_index_entry(istate, ce, add_option)) 633returnerror("unable to add%sto index",path); 634if(verbose && !was_same) 635printf("add '%s'\n", path); 636return0; 637} 638 639intadd_file_to_index(struct index_state *istate,const char*path,int flags) 640{ 641struct stat st; 642if(lstat(path, &st)) 643die_errno("unable to stat '%s'", path); 644returnadd_to_index(istate, path, &st, flags); 645} 646 647struct cache_entry *make_cache_entry(unsigned int mode, 648const unsigned char*sha1,const char*path,int stage, 649int refresh) 650{ 651int size, len; 652struct cache_entry *ce; 653 654if(!verify_path(path)) { 655error("Invalid path '%s'", path); 656return NULL; 657} 658 659 len =strlen(path); 660 size =cache_entry_size(len); 661 ce =xcalloc(1, size); 662 663hashcpy(ce->sha1, sha1); 664memcpy(ce->name, path, len); 665 ce->ce_flags =create_ce_flags(len, stage); 666 ce->ce_mode =create_ce_mode(mode); 667 668if(refresh) 669returnrefresh_cache_entry(ce,0); 670 671return ce; 672} 673 674intce_same_name(struct cache_entry *a,struct cache_entry *b) 675{ 676int len =ce_namelen(a); 677returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 678} 679 680intce_path_match(const struct cache_entry *ce,const char**pathspec) 681{ 682const char*match, *name; 683int len; 684 685if(!pathspec) 686return1; 687 688 len =ce_namelen(ce); 689 name = ce->name; 690while((match = *pathspec++) != NULL) { 691int matchlen =strlen(match); 692if(matchlen > len) 693continue; 694if(memcmp(name, match, matchlen)) 695continue; 696if(matchlen && name[matchlen-1] =='/') 697return1; 698if(name[matchlen] =='/'|| !name[matchlen]) 699return1; 700if(!matchlen) 701return1; 702} 703return0; 704} 705 706/* 707 * We fundamentally don't like some paths: we don't want 708 * dot or dot-dot anywhere, and for obvious reasons don't 709 * want to recurse into ".git" either. 710 * 711 * Also, we don't want double slashes or slashes at the 712 * end that can make pathnames ambiguous. 713 */ 714static intverify_dotfile(const char*rest) 715{ 716/* 717 * The first character was '.', but that 718 * has already been discarded, we now test 719 * the rest. 720 */ 721switch(*rest) { 722/* "." is not allowed */ 723case'\0':case'/': 724return0; 725 726/* 727 * ".git" followed by NUL or slash is bad. This 728 * shares the path end test with the ".." case. 729 */ 730case'g': 731if(rest[1] !='i') 732break; 733if(rest[2] !='t') 734break; 735 rest +=2; 736/* fallthrough */ 737case'.': 738if(rest[1] =='\0'|| rest[1] =='/') 739return0; 740} 741return1; 742} 743 744intverify_path(const char*path) 745{ 746char c; 747 748goto inside; 749for(;;) { 750if(!c) 751return1; 752if(c =='/') { 753inside: 754 c = *path++; 755switch(c) { 756default: 757continue; 758case'/':case'\0': 759break; 760case'.': 761if(verify_dotfile(path)) 762continue; 763} 764return0; 765} 766 c = *path++; 767} 768} 769 770/* 771 * Do we have another file that has the beginning components being a 772 * proper superset of the name we're trying to add? 773 */ 774static inthas_file_name(struct index_state *istate, 775const struct cache_entry *ce,int pos,int ok_to_replace) 776{ 777int retval =0; 778int len =ce_namelen(ce); 779int stage =ce_stage(ce); 780const char*name = ce->name; 781 782while(pos < istate->cache_nr) { 783struct cache_entry *p = istate->cache[pos++]; 784 785if(len >=ce_namelen(p)) 786break; 787if(memcmp(name, p->name, len)) 788break; 789if(ce_stage(p) != stage) 790continue; 791if(p->name[len] !='/') 792continue; 793if(p->ce_flags & CE_REMOVE) 794continue; 795 retval = -1; 796if(!ok_to_replace) 797break; 798remove_index_entry_at(istate, --pos); 799} 800return retval; 801} 802 803/* 804 * Do we have another file with a pathname that is a proper 805 * subset of the name we're trying to add? 806 */ 807static inthas_dir_name(struct index_state *istate, 808const struct cache_entry *ce,int pos,int ok_to_replace) 809{ 810int retval =0; 811int stage =ce_stage(ce); 812const char*name = ce->name; 813const char*slash = name +ce_namelen(ce); 814 815for(;;) { 816int len; 817 818for(;;) { 819if(*--slash =='/') 820break; 821if(slash <= ce->name) 822return retval; 823} 824 len = slash - name; 825 826 pos =index_name_pos(istate, name,create_ce_flags(len, stage)); 827if(pos >=0) { 828/* 829 * Found one, but not so fast. This could 830 * be a marker that says "I was here, but 831 * I am being removed". Such an entry is 832 * not a part of the resulting tree, and 833 * it is Ok to have a directory at the same 834 * path. 835 */ 836if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 837 retval = -1; 838if(!ok_to_replace) 839break; 840remove_index_entry_at(istate, pos); 841continue; 842} 843} 844else 845 pos = -pos-1; 846 847/* 848 * Trivial optimization: if we find an entry that 849 * already matches the sub-directory, then we know 850 * we're ok, and we can exit. 851 */ 852while(pos < istate->cache_nr) { 853struct cache_entry *p = istate->cache[pos]; 854if((ce_namelen(p) <= len) || 855(p->name[len] !='/') || 856memcmp(p->name, name, len)) 857break;/* not our subdirectory */ 858if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 859/* 860 * p is at the same stage as our entry, and 861 * is a subdirectory of what we are looking 862 * at, so we cannot have conflicts at our 863 * level or anything shorter. 864 */ 865return retval; 866 pos++; 867} 868} 869return retval; 870} 871 872/* We may be in a situation where we already have path/file and path 873 * is being added, or we already have path and path/file is being 874 * added. Either one would result in a nonsense tree that has path 875 * twice when git-write-tree tries to write it out. Prevent it. 876 * 877 * If ok-to-replace is specified, we remove the conflicting entries 878 * from the cache so the caller should recompute the insert position. 879 * When this happens, we return non-zero. 880 */ 881static intcheck_file_directory_conflict(struct index_state *istate, 882const struct cache_entry *ce, 883int pos,int ok_to_replace) 884{ 885int retval; 886 887/* 888 * When ce is an "I am going away" entry, we allow it to be added 889 */ 890if(ce->ce_flags & CE_REMOVE) 891return0; 892 893/* 894 * We check if the path is a sub-path of a subsequent pathname 895 * first, since removing those will not change the position 896 * in the array. 897 */ 898 retval =has_file_name(istate, ce, pos, ok_to_replace); 899 900/* 901 * Then check if the path might have a clashing sub-directory 902 * before it. 903 */ 904return retval +has_dir_name(istate, ce, pos, ok_to_replace); 905} 906 907static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 908{ 909int pos; 910int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 911int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 912int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 913int new_only = option & ADD_CACHE_NEW_ONLY; 914 915cache_tree_invalidate_path(istate->cache_tree, ce->name); 916 pos =index_name_pos(istate, ce->name, ce->ce_flags); 917 918/* existing match? Just replace it. */ 919if(pos >=0) { 920if(!new_only) 921replace_index_entry(istate, pos, ce); 922return0; 923} 924 pos = -pos-1; 925 926/* 927 * Inserting a merged entry ("stage 0") into the index 928 * will always replace all non-merged entries.. 929 */ 930if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 931while(ce_same_name(istate->cache[pos], ce)) { 932 ok_to_add =1; 933if(!remove_index_entry_at(istate, pos)) 934break; 935} 936} 937 938if(!ok_to_add) 939return-1; 940if(!verify_path(ce->name)) 941returnerror("Invalid path '%s'", ce->name); 942 943if(!skip_df_check && 944check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 945if(!ok_to_replace) 946returnerror("'%s' appears as both a file and as a directory", 947 ce->name); 948 pos =index_name_pos(istate, ce->name, ce->ce_flags); 949 pos = -pos-1; 950} 951return pos +1; 952} 953 954intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 955{ 956int pos; 957 958if(option & ADD_CACHE_JUST_APPEND) 959 pos = istate->cache_nr; 960else{ 961int ret; 962 ret =add_index_entry_with_check(istate, ce, option); 963if(ret <=0) 964return ret; 965 pos = ret -1; 966} 967 968/* Make sure the array is big enough .. */ 969if(istate->cache_nr == istate->cache_alloc) { 970 istate->cache_alloc =alloc_nr(istate->cache_alloc); 971 istate->cache =xrealloc(istate->cache, 972 istate->cache_alloc *sizeof(struct cache_entry *)); 973} 974 975/* Add it in.. */ 976 istate->cache_nr++; 977if(istate->cache_nr > pos +1) 978memmove(istate->cache + pos +1, 979 istate->cache + pos, 980(istate->cache_nr - pos -1) *sizeof(ce)); 981set_index_entry(istate, pos, ce); 982 istate->cache_changed =1; 983return0; 984} 985 986/* 987 * "refresh" does not calculate a new sha1 file or bring the 988 * cache up-to-date for mode/content changes. But what it 989 * _does_ do is to "re-match" the stat information of a file 990 * with the cache, so that you can refresh the cache for a 991 * file that hasn't been changed but where the stat entry is 992 * out of date. 993 * 994 * For example, you'd want to do this after doing a "git-read-tree", 995 * to link up the stat cache details with the proper files. 996 */ 997static struct cache_entry *refresh_cache_ent(struct index_state *istate, 998struct cache_entry *ce, 999unsigned int options,int*err)1000{1001struct stat st;1002struct cache_entry *updated;1003int changed, size;1004int ignore_valid = options & CE_MATCH_IGNORE_VALID;10051006if(ce_uptodate(ce))1007return ce;10081009/*1010 * CE_VALID means the user promised us that the change to1011 * the work tree does not matter and told us not to worry.1012 */1013if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1014ce_mark_uptodate(ce);1015return ce;1016}10171018if(lstat(ce->name, &st) <0) {1019if(err)1020*err = errno;1021return NULL;1022}10231024 changed =ie_match_stat(istate, ce, &st, options);1025if(!changed) {1026/*1027 * The path is unchanged. If we were told to ignore1028 * valid bit, then we did the actual stat check and1029 * found that the entry is unmodified. If the entry1030 * is not marked VALID, this is the place to mark it1031 * valid again, under "assume unchanged" mode.1032 */1033if(ignore_valid && assume_unchanged &&1034!(ce->ce_flags & CE_VALID))1035;/* mark this one VALID again */1036else{1037/*1038 * We do not mark the index itself "modified"1039 * because CE_UPTODATE flag is in-core only;1040 * we are not going to write this change out.1041 */1042ce_mark_uptodate(ce);1043return ce;1044}1045}10461047if(ie_modified(istate, ce, &st, options)) {1048if(err)1049*err = EINVAL;1050return NULL;1051}10521053 size =ce_size(ce);1054 updated =xmalloc(size);1055memcpy(updated, ce, size);1056fill_stat_cache_info(updated, &st);1057/*1058 * If ignore_valid is not set, we should leave CE_VALID bit1059 * alone. Otherwise, paths marked with --no-assume-unchanged1060 * (i.e. things to be edited) will reacquire CE_VALID bit1061 * automatically, which is not really what we want.1062 */1063if(!ignore_valid && assume_unchanged &&1064!(ce->ce_flags & CE_VALID))1065 updated->ce_flags &= ~CE_VALID;10661067return updated;1068}10691070static voidshow_file(const char* fmt,const char* name,int in_porcelain,1071int* first,char*header_msg)1072{1073if(in_porcelain && *first && header_msg) {1074printf("%s\n", header_msg);1075*first=0;1076}1077printf(fmt, name);1078}10791080intrefresh_index(struct index_state *istate,unsigned int flags,const char**pathspec,1081char*seen,char*header_msg)1082{1083int i;1084int has_errors =0;1085int really = (flags & REFRESH_REALLY) !=0;1086int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1087int quiet = (flags & REFRESH_QUIET) !=0;1088int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1089int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1090int first =1;1091int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1092unsigned int options = really ? CE_MATCH_IGNORE_VALID :0;1093const char*needs_update_fmt;1094const char*needs_merge_fmt;10951096 needs_update_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1097 needs_merge_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1098for(i =0; i < istate->cache_nr; i++) {1099struct cache_entry *ce, *new;1100int cache_errno =0;11011102 ce = istate->cache[i];1103if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1104continue;11051106if(ce_stage(ce)) {1107while((i < istate->cache_nr) &&1108!strcmp(istate->cache[i]->name, ce->name))1109 i++;1110 i--;1111if(allow_unmerged)1112continue;1113show_file(needs_merge_fmt, ce->name, in_porcelain, &first, header_msg);1114 has_errors =1;1115continue;1116}11171118if(pathspec && !match_pathspec(pathspec, ce->name,strlen(ce->name),0, seen))1119continue;11201121new=refresh_cache_ent(istate, ce, options, &cache_errno);1122if(new== ce)1123continue;1124if(!new) {1125if(not_new && cache_errno == ENOENT)1126continue;1127if(really && cache_errno == EINVAL) {1128/* If we are doing --really-refresh that1129 * means the index is not valid anymore.1130 */1131 ce->ce_flags &= ~CE_VALID;1132 istate->cache_changed =1;1133}1134if(quiet)1135continue;1136show_file(needs_update_fmt, ce->name, in_porcelain, &first, header_msg);1137 has_errors =1;1138continue;1139}11401141replace_index_entry(istate, i,new);1142}1143return has_errors;1144}11451146static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really)1147{1148returnrefresh_cache_ent(&the_index, ce, really, NULL);1149}11501151static intverify_hdr(struct cache_header *hdr,unsigned long size)1152{1153 git_SHA_CTX c;1154unsigned char sha1[20];11551156if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1157returnerror("bad signature");1158if(hdr->hdr_version !=htonl(2) && hdr->hdr_version !=htonl(3))1159returnerror("bad index version");1160git_SHA1_Init(&c);1161git_SHA1_Update(&c, hdr, size -20);1162git_SHA1_Final(sha1, &c);1163if(hashcmp(sha1, (unsigned char*)hdr + size -20))1164returnerror("bad index file sha1 signature");1165return0;1166}11671168static intread_index_extension(struct index_state *istate,1169const char*ext,void*data,unsigned long sz)1170{1171switch(CACHE_EXT(ext)) {1172case CACHE_EXT_TREE:1173 istate->cache_tree =cache_tree_read(data, sz);1174break;1175default:1176if(*ext <'A'||'Z'< *ext)1177returnerror("index uses %.4s extension, which we do not understand",1178 ext);1179fprintf(stderr,"ignoring %.4s extension\n", ext);1180break;1181}1182return0;1183}11841185intread_index(struct index_state *istate)1186{1187returnread_index_from(istate,get_index_file());1188}11891190static voidconvert_from_disk(struct ondisk_cache_entry *ondisk,struct cache_entry *ce)1191{1192size_t len;1193const char*name;11941195 ce->ce_ctime.sec =ntohl(ondisk->ctime.sec);1196 ce->ce_mtime.sec =ntohl(ondisk->mtime.sec);1197 ce->ce_ctime.nsec =ntohl(ondisk->ctime.nsec);1198 ce->ce_mtime.nsec =ntohl(ondisk->mtime.nsec);1199 ce->ce_dev =ntohl(ondisk->dev);1200 ce->ce_ino =ntohl(ondisk->ino);1201 ce->ce_mode =ntohl(ondisk->mode);1202 ce->ce_uid =ntohl(ondisk->uid);1203 ce->ce_gid =ntohl(ondisk->gid);1204 ce->ce_size =ntohl(ondisk->size);1205/* On-disk flags are just 16 bits */1206 ce->ce_flags =ntohs(ondisk->flags);12071208hashcpy(ce->sha1, ondisk->sha1);12091210 len = ce->ce_flags & CE_NAMEMASK;12111212if(ce->ce_flags & CE_EXTENDED) {1213struct ondisk_cache_entry_extended *ondisk2;1214int extended_flags;1215 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1216 extended_flags =ntohs(ondisk2->flags2) <<16;1217/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1218if(extended_flags & ~CE_EXTENDED_FLAGS)1219die("Unknown index entry format%08x", extended_flags);1220 ce->ce_flags |= extended_flags;1221 name = ondisk2->name;1222}1223else1224 name = ondisk->name;12251226if(len == CE_NAMEMASK)1227 len =strlen(name);1228/*1229 * NEEDSWORK: If the original index is crafted, this copy could1230 * go unchecked.1231 */1232memcpy(ce->name, name, len +1);1233}12341235staticinlinesize_testimate_cache_size(size_t ondisk_size,unsigned int entries)1236{1237long per_entry;12381239 per_entry =sizeof(struct cache_entry) -sizeof(struct ondisk_cache_entry);12401241/*1242 * Alignment can cause differences. This should be "alignof", but1243 * since that's a gcc'ism, just use the size of a pointer.1244 */1245 per_entry +=sizeof(void*);1246return ondisk_size + entries*per_entry;1247}12481249/* remember to discard_cache() before reading a different cache! */1250intread_index_from(struct index_state *istate,const char*path)1251{1252int fd, i;1253struct stat st;1254unsigned long src_offset, dst_offset;1255struct cache_header *hdr;1256void*mmap;1257size_t mmap_size;12581259 errno = EBUSY;1260if(istate->initialized)1261return istate->cache_nr;12621263 errno = ENOENT;1264 istate->timestamp.sec =0;1265 istate->timestamp.nsec =0;1266 fd =open(path, O_RDONLY);1267if(fd <0) {1268if(errno == ENOENT)1269return0;1270die_errno("index file open failed");1271}12721273if(fstat(fd, &st))1274die_errno("cannot stat the open index");12751276 errno = EINVAL;1277 mmap_size =xsize_t(st.st_size);1278if(mmap_size <sizeof(struct cache_header) +20)1279die("index file smaller than expected");12801281 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1282close(fd);1283if(mmap == MAP_FAILED)1284die_errno("unable to map index file");12851286 hdr = mmap;1287if(verify_hdr(hdr, mmap_size) <0)1288goto unmap;12891290 istate->cache_nr =ntohl(hdr->hdr_entries);1291 istate->cache_alloc =alloc_nr(istate->cache_nr);1292 istate->cache =xcalloc(istate->cache_alloc,sizeof(struct cache_entry *));12931294/*1295 * The disk format is actually larger than the in-memory format,1296 * due to space for nsec etc, so even though the in-memory one1297 * has room for a few more flags, we can allocate using the same1298 * index size1299 */1300 istate->alloc =xmalloc(estimate_cache_size(mmap_size, istate->cache_nr));1301 istate->initialized =1;13021303 src_offset =sizeof(*hdr);1304 dst_offset =0;1305for(i =0; i < istate->cache_nr; i++) {1306struct ondisk_cache_entry *disk_ce;1307struct cache_entry *ce;13081309 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1310 ce = (struct cache_entry *)((char*)istate->alloc + dst_offset);1311convert_from_disk(disk_ce, ce);1312set_index_entry(istate, i, ce);13131314 src_offset +=ondisk_ce_size(ce);1315 dst_offset +=ce_size(ce);1316}1317 istate->timestamp.sec = st.st_mtime;1318 istate->timestamp.nsec =ST_MTIME_NSEC(st);13191320while(src_offset <= mmap_size -20-8) {1321/* After an array of active_nr index entries,1322 * there can be arbitrary number of extended1323 * sections, each of which is prefixed with1324 * extension name (4-byte) and section length1325 * in 4-byte network byte order.1326 */1327uint32_t extsize;1328memcpy(&extsize, (char*)mmap + src_offset +4,4);1329 extsize =ntohl(extsize);1330if(read_index_extension(istate,1331(const char*) mmap + src_offset,1332(char*) mmap + src_offset +8,1333 extsize) <0)1334goto unmap;1335 src_offset +=8;1336 src_offset += extsize;1337}1338munmap(mmap, mmap_size);1339return istate->cache_nr;13401341unmap:1342munmap(mmap, mmap_size);1343 errno = EINVAL;1344die("index file corrupt");1345}13461347intis_index_unborn(struct index_state *istate)1348{1349return(!istate->cache_nr && !istate->alloc && !istate->timestamp.sec);1350}13511352intdiscard_index(struct index_state *istate)1353{1354 istate->cache_nr =0;1355 istate->cache_changed =0;1356 istate->timestamp.sec =0;1357 istate->timestamp.nsec =0;1358 istate->name_hash_initialized =0;1359free_hash(&istate->name_hash);1360cache_tree_free(&(istate->cache_tree));1361free(istate->alloc);1362 istate->alloc = NULL;1363 istate->initialized =0;13641365/* no need to throw away allocated active_cache */1366return0;1367}13681369intunmerged_index(const struct index_state *istate)1370{1371int i;1372for(i =0; i < istate->cache_nr; i++) {1373if(ce_stage(istate->cache[i]))1374return1;1375}1376return0;1377}13781379#define WRITE_BUFFER_SIZE 81921380static unsigned char write_buffer[WRITE_BUFFER_SIZE];1381static unsigned long write_buffer_len;13821383static intce_write_flush(git_SHA_CTX *context,int fd)1384{1385unsigned int buffered = write_buffer_len;1386if(buffered) {1387git_SHA1_Update(context, write_buffer, buffered);1388if(write_in_full(fd, write_buffer, buffered) != buffered)1389return-1;1390 write_buffer_len =0;1391}1392return0;1393}13941395static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1396{1397while(len) {1398unsigned int buffered = write_buffer_len;1399unsigned int partial = WRITE_BUFFER_SIZE - buffered;1400if(partial > len)1401 partial = len;1402memcpy(write_buffer + buffered, data, partial);1403 buffered += partial;1404if(buffered == WRITE_BUFFER_SIZE) {1405 write_buffer_len = buffered;1406if(ce_write_flush(context, fd))1407return-1;1408 buffered =0;1409}1410 write_buffer_len = buffered;1411 len -= partial;1412 data = (char*) data + partial;1413}1414return0;1415}14161417static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1418unsigned int ext,unsigned int sz)1419{1420 ext =htonl(ext);1421 sz =htonl(sz);1422return((ce_write(context, fd, &ext,4) <0) ||1423(ce_write(context, fd, &sz,4) <0)) ? -1:0;1424}14251426static intce_flush(git_SHA_CTX *context,int fd)1427{1428unsigned int left = write_buffer_len;14291430if(left) {1431 write_buffer_len =0;1432git_SHA1_Update(context, write_buffer, left);1433}14341435/* Flush first if not enough space for SHA1 signature */1436if(left +20> WRITE_BUFFER_SIZE) {1437if(write_in_full(fd, write_buffer, left) != left)1438return-1;1439 left =0;1440}14411442/* Append the SHA1 signature at the end */1443git_SHA1_Final(write_buffer + left, context);1444 left +=20;1445return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1446}14471448static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1449{1450/*1451 * The only thing we care about in this function is to smudge the1452 * falsely clean entry due to touch-update-touch race, so we leave1453 * everything else as they are. We are called for entries whose1454 * ce_mtime match the index file mtime.1455 *1456 * Note that this actually does not do much for gitlinks, for1457 * which ce_match_stat_basic() always goes to the actual1458 * contents. The caller checks with is_racy_timestamp() which1459 * always says "no" for gitlinks, so we are not called for them ;-)1460 */1461struct stat st;14621463if(lstat(ce->name, &st) <0)1464return;1465if(ce_match_stat_basic(ce, &st))1466return;1467if(ce_modified_check_fs(ce, &st)) {1468/* This is "racily clean"; smudge it. Note that this1469 * is a tricky code. At first glance, it may appear1470 * that it can break with this sequence:1471 *1472 * $ echo xyzzy >frotz1473 * $ git-update-index --add frotz1474 * $ : >frotz1475 * $ sleep 31476 * $ echo filfre >nitfol1477 * $ git-update-index --add nitfol1478 *1479 * but it does not. When the second update-index runs,1480 * it notices that the entry "frotz" has the same timestamp1481 * as index, and if we were to smudge it by resetting its1482 * size to zero here, then the object name recorded1483 * in index is the 6-byte file but the cached stat information1484 * becomes zero --- which would then match what we would1485 * obtain from the filesystem next time we stat("frotz").1486 *1487 * However, the second update-index, before calling1488 * this function, notices that the cached size is 61489 * bytes and what is on the filesystem is an empty1490 * file, and never calls us, so the cached size information1491 * for "frotz" stays 6 which does not match the filesystem.1492 */1493 ce->ce_size =0;1494}1495}14961497static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce)1498{1499int size =ondisk_ce_size(ce);1500struct ondisk_cache_entry *ondisk =xcalloc(1, size);1501char*name;15021503 ondisk->ctime.sec =htonl(ce->ce_ctime.sec);1504 ondisk->mtime.sec =htonl(ce->ce_mtime.sec);1505 ondisk->ctime.nsec =htonl(ce->ce_ctime.nsec);1506 ondisk->mtime.nsec =htonl(ce->ce_mtime.nsec);1507 ondisk->dev =htonl(ce->ce_dev);1508 ondisk->ino =htonl(ce->ce_ino);1509 ondisk->mode =htonl(ce->ce_mode);1510 ondisk->uid =htonl(ce->ce_uid);1511 ondisk->gid =htonl(ce->ce_gid);1512 ondisk->size =htonl(ce->ce_size);1513hashcpy(ondisk->sha1, ce->sha1);1514 ondisk->flags =htons(ce->ce_flags);1515if(ce->ce_flags & CE_EXTENDED) {1516struct ondisk_cache_entry_extended *ondisk2;1517 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1518 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1519 name = ondisk2->name;1520}1521else1522 name = ondisk->name;1523memcpy(name, ce->name,ce_namelen(ce));15241525returnce_write(c, fd, ondisk, size);1526}15271528intwrite_index(struct index_state *istate,int newfd)1529{1530 git_SHA_CTX c;1531struct cache_header hdr;1532int i, err, removed, extended;1533struct cache_entry **cache = istate->cache;1534int entries = istate->cache_nr;1535struct stat st;15361537for(i = removed = extended =0; i < entries; i++) {1538if(cache[i]->ce_flags & CE_REMOVE)1539 removed++;15401541/* reduce extended entries if possible */1542 cache[i]->ce_flags &= ~CE_EXTENDED;1543if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {1544 extended++;1545 cache[i]->ce_flags |= CE_EXTENDED;1546}1547}15481549 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1550/* for extended format, increase version so older git won't try to read it */1551 hdr.hdr_version =htonl(extended ?3:2);1552 hdr.hdr_entries =htonl(entries - removed);15531554git_SHA1_Init(&c);1555if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1556return-1;15571558for(i =0; i < entries; i++) {1559struct cache_entry *ce = cache[i];1560if(ce->ce_flags & CE_REMOVE)1561continue;1562if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1563ce_smudge_racily_clean_entry(ce);1564if(ce_write_entry(&c, newfd, ce) <0)1565return-1;1566}15671568/* Write extension data here */1569if(istate->cache_tree) {1570struct strbuf sb = STRBUF_INIT;15711572cache_tree_write(&sb, istate->cache_tree);1573 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01574||ce_write(&c, newfd, sb.buf, sb.len) <0;1575strbuf_release(&sb);1576if(err)1577return-1;1578}15791580if(ce_flush(&c, newfd) ||fstat(newfd, &st))1581return-1;1582 istate->timestamp.sec = (unsigned int)st.st_mtime;1583 istate->timestamp.nsec =ST_MTIME_NSEC(st);1584return0;1585}15861587/*1588 * Read the index file that is potentially unmerged into given1589 * index_state, dropping any unmerged entries. Returns true if1590 * the index is unmerged. Callers who want to refuse to work1591 * from an unmerged state can call this and check its return value,1592 * instead of calling read_cache().1593 */1594intread_index_unmerged(struct index_state *istate)1595{1596int i;1597int unmerged =0;15981599read_index(istate);1600for(i =0; i < istate->cache_nr; i++) {1601struct cache_entry *ce = istate->cache[i];1602struct cache_entry *new_ce;1603int size, len;16041605if(!ce_stage(ce))1606continue;1607 unmerged =1;1608 len =strlen(ce->name);1609 size =cache_entry_size(len);1610 new_ce =xcalloc(1, size);1611hashcpy(new_ce->sha1, ce->sha1);1612memcpy(new_ce->name, ce->name, len);1613 new_ce->ce_flags =create_ce_flags(len,0);1614 new_ce->ce_mode = ce->ce_mode;1615if(add_index_entry(istate, new_ce,0))1616returnerror("%s: cannot drop to stage #0",1617 ce->name);1618 i =index_name_pos(istate, new_ce->name, len);1619}1620return unmerged;1621}16221623struct update_callback_data1624{1625int flags;1626int add_errors;1627};16281629static voidupdate_callback(struct diff_queue_struct *q,1630struct diff_options *opt,void*cbdata)1631{1632int i;1633struct update_callback_data *data = cbdata;16341635for(i =0; i < q->nr; i++) {1636struct diff_filepair *p = q->queue[i];1637const char*path = p->one->path;1638switch(p->status) {1639default:1640die("unexpected diff status%c", p->status);1641case DIFF_STATUS_UNMERGED:1642/*1643 * ADD_CACHE_IGNORE_REMOVAL is unset if "git1644 * add -u" is calling us, In such a case, a1645 * missing work tree file needs to be removed1646 * if there is an unmerged entry at stage #2,1647 * but such a diff record is followed by1648 * another with DIFF_STATUS_DELETED (and if1649 * there is no stage #2, we won't see DELETED1650 * nor MODIFIED). We can simply continue1651 * either way.1652 */1653if(!(data->flags & ADD_CACHE_IGNORE_REMOVAL))1654continue;1655/*1656 * Otherwise, it is "git add path" is asking1657 * to explicitly add it; we fall through. A1658 * missing work tree file is an error and is1659 * caught by add_file_to_index() in such a1660 * case.1661 */1662case DIFF_STATUS_MODIFIED:1663case DIFF_STATUS_TYPE_CHANGED:1664if(add_file_to_index(&the_index, path, data->flags)) {1665if(!(data->flags & ADD_CACHE_IGNORE_ERRORS))1666die("updating files failed");1667 data->add_errors++;1668}1669break;1670case DIFF_STATUS_DELETED:1671if(data->flags & ADD_CACHE_IGNORE_REMOVAL)1672break;1673if(!(data->flags & ADD_CACHE_PRETEND))1674remove_file_from_index(&the_index, path);1675if(data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))1676printf("remove '%s'\n", path);1677break;1678}1679}1680}16811682intadd_files_to_cache(const char*prefix,const char**pathspec,int flags)1683{1684struct update_callback_data data;1685struct rev_info rev;1686init_revisions(&rev, prefix);1687setup_revisions(0, NULL, &rev, NULL);1688 rev.prune_data = pathspec;1689 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;1690 rev.diffopt.format_callback = update_callback;1691 data.flags = flags;1692 data.add_errors =0;1693 rev.diffopt.format_callback_data = &data;1694run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);1695return!!data.add_errors;1696}16971698/*1699 * Returns 1 if the path is an "other" path with respect to1700 * the index; that is, the path is not mentioned in the index at all,1701 * either as a file, a directory with some files in the index,1702 * or as an unmerged entry.1703 *1704 * We helpfully remove a trailing "/" from directories so that1705 * the output of read_directory can be used as-is.1706 */1707intindex_name_is_other(const struct index_state *istate,const char*name,1708int namelen)1709{1710int pos;1711if(namelen && name[namelen -1] =='/')1712 namelen--;1713 pos =index_name_pos(istate, name, namelen);1714if(0<= pos)1715return0;/* exact match */1716 pos = -pos -1;1717if(pos < istate->cache_nr) {1718struct cache_entry *ce = istate->cache[pos];1719if(ce_namelen(ce) == namelen &&1720!memcmp(ce->name, name, namelen))1721return0;/* Yup, this one exists unmerged */1722}1723return1;1724}