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"blob.h" 14#include"resolve-undo.h" 15 16static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really); 17 18/* Index extensions. 19 * 20 * The first letter should be 'A'..'Z' for extensions that are not 21 * necessary for a correct operation (i.e. optimization data). 22 * When new extensions are added that _needs_ to be understood in 23 * order to correctly interpret the index file, pick character that 24 * is outside the range, to cause the reader to abort. 25 */ 26 27#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) 28#define CACHE_EXT_TREE 0x54524545/* "TREE" */ 29#define CACHE_EXT_RESOLVE_UNDO 0x52455543/* "REUC" */ 30 31struct index_state the_index; 32 33static voidset_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 34{ 35 istate->cache[nr] = ce; 36add_name_hash(istate, ce); 37} 38 39static voidreplace_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 40{ 41struct cache_entry *old = istate->cache[nr]; 42 43remove_name_hash(old); 44set_index_entry(istate, nr, ce); 45 istate->cache_changed =1; 46} 47 48voidrename_index_entry_at(struct index_state *istate,int nr,const char*new_name) 49{ 50struct cache_entry *old = istate->cache[nr], *new; 51int namelen =strlen(new_name); 52 53new=xmalloc(cache_entry_size(namelen)); 54copy_cache_entry(new, old); 55new->ce_flags &= ~(CE_STATE_MASK | CE_NAMEMASK); 56new->ce_flags |= (namelen >= CE_NAMEMASK ? CE_NAMEMASK : namelen); 57memcpy(new->name, new_name, namelen +1); 58 59cache_tree_invalidate_path(istate->cache_tree, old->name); 60remove_index_entry_at(istate, nr); 61add_index_entry(istate,new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 62} 63 64/* 65 * This only updates the "non-critical" parts of the directory 66 * cache, ie the parts that aren't tracked by GIT, and only used 67 * to validate the cache. 68 */ 69voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 70{ 71 ce->ce_ctime.sec = (unsigned int)st->st_ctime; 72 ce->ce_mtime.sec = (unsigned int)st->st_mtime; 73 ce->ce_ctime.nsec =ST_CTIME_NSEC(*st); 74 ce->ce_mtime.nsec =ST_MTIME_NSEC(*st); 75 ce->ce_dev = st->st_dev; 76 ce->ce_ino = st->st_ino; 77 ce->ce_uid = st->st_uid; 78 ce->ce_gid = st->st_gid; 79 ce->ce_size = st->st_size; 80 81if(assume_unchanged) 82 ce->ce_flags |= CE_VALID; 83 84if(S_ISREG(st->st_mode)) 85ce_mark_uptodate(ce); 86} 87 88static intce_compare_data(struct cache_entry *ce,struct stat *st) 89{ 90int match = -1; 91int fd =open(ce->name, O_RDONLY); 92 93if(fd >=0) { 94unsigned char sha1[20]; 95if(!index_fd(sha1, fd, st, OBJ_BLOB, ce->name,0)) 96 match =hashcmp(sha1, ce->sha1); 97/* index_fd() closed the file descriptor already */ 98} 99return match; 100} 101 102static intce_compare_link(struct cache_entry *ce,size_t expected_size) 103{ 104int match = -1; 105void*buffer; 106unsigned long size; 107enum object_type type; 108struct strbuf sb = STRBUF_INIT; 109 110if(strbuf_readlink(&sb, ce->name, expected_size)) 111return-1; 112 113 buffer =read_sha1_file(ce->sha1, &type, &size); 114if(buffer) { 115if(size == sb.len) 116 match =memcmp(buffer, sb.buf, size); 117free(buffer); 118} 119strbuf_release(&sb); 120return match; 121} 122 123static intce_compare_gitlink(struct cache_entry *ce) 124{ 125unsigned char sha1[20]; 126 127/* 128 * We don't actually require that the .git directory 129 * under GITLINK directory be a valid git directory. It 130 * might even be missing (in case nobody populated that 131 * sub-project). 132 * 133 * If so, we consider it always to match. 134 */ 135if(resolve_gitlink_ref(ce->name,"HEAD", sha1) <0) 136return0; 137returnhashcmp(sha1, ce->sha1); 138} 139 140static intce_modified_check_fs(struct cache_entry *ce,struct stat *st) 141{ 142switch(st->st_mode & S_IFMT) { 143case S_IFREG: 144if(ce_compare_data(ce, st)) 145return DATA_CHANGED; 146break; 147case S_IFLNK: 148if(ce_compare_link(ce,xsize_t(st->st_size))) 149return DATA_CHANGED; 150break; 151case S_IFDIR: 152if(S_ISGITLINK(ce->ce_mode)) 153returnce_compare_gitlink(ce) ? DATA_CHANGED :0; 154default: 155return TYPE_CHANGED; 156} 157return0; 158} 159 160static intce_match_stat_basic(struct cache_entry *ce,struct stat *st) 161{ 162unsigned int changed =0; 163 164if(ce->ce_flags & CE_REMOVE) 165return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; 166 167switch(ce->ce_mode & S_IFMT) { 168case S_IFREG: 169 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 170/* We consider only the owner x bit to be relevant for 171 * "mode changes" 172 */ 173if(trust_executable_bit && 174(0100& (ce->ce_mode ^ st->st_mode))) 175 changed |= MODE_CHANGED; 176break; 177case S_IFLNK: 178if(!S_ISLNK(st->st_mode) && 179(has_symlinks || !S_ISREG(st->st_mode))) 180 changed |= TYPE_CHANGED; 181break; 182case S_IFGITLINK: 183/* We ignore most of the st_xxx fields for gitlinks */ 184if(!S_ISDIR(st->st_mode)) 185 changed |= TYPE_CHANGED; 186else if(ce_compare_gitlink(ce)) 187 changed |= DATA_CHANGED; 188return changed; 189default: 190die("internal error: ce_mode is%o", ce->ce_mode); 191} 192if(ce->ce_mtime.sec != (unsigned int)st->st_mtime) 193 changed |= MTIME_CHANGED; 194if(trust_ctime && ce->ce_ctime.sec != (unsigned int)st->st_ctime) 195 changed |= CTIME_CHANGED; 196 197#ifdef USE_NSEC 198if(ce->ce_mtime.nsec !=ST_MTIME_NSEC(*st)) 199 changed |= MTIME_CHANGED; 200if(trust_ctime && ce->ce_ctime.nsec !=ST_CTIME_NSEC(*st)) 201 changed |= CTIME_CHANGED; 202#endif 203 204if(ce->ce_uid != (unsigned int) st->st_uid || 205 ce->ce_gid != (unsigned int) st->st_gid) 206 changed |= OWNER_CHANGED; 207if(ce->ce_ino != (unsigned int) st->st_ino) 208 changed |= INODE_CHANGED; 209 210#ifdef USE_STDEV 211/* 212 * st_dev breaks on network filesystems where different 213 * clients will have different views of what "device" 214 * the filesystem is on 215 */ 216if(ce->ce_dev != (unsigned int) st->st_dev) 217 changed |= INODE_CHANGED; 218#endif 219 220if(ce->ce_size != (unsigned int) st->st_size) 221 changed |= DATA_CHANGED; 222 223/* Racily smudged entry? */ 224if(!ce->ce_size) { 225if(!is_empty_blob_sha1(ce->sha1)) 226 changed |= DATA_CHANGED; 227} 228 229return changed; 230} 231 232static intis_racy_timestamp(const struct index_state *istate,struct cache_entry *ce) 233{ 234return(!S_ISGITLINK(ce->ce_mode) && 235 istate->timestamp.sec && 236#ifdef USE_NSEC 237/* nanosecond timestamped files can also be racy! */ 238(istate->timestamp.sec < ce->ce_mtime.sec || 239(istate->timestamp.sec == ce->ce_mtime.sec && 240 istate->timestamp.nsec <= ce->ce_mtime.nsec)) 241#else 242 istate->timestamp.sec <= ce->ce_mtime.sec 243#endif 244); 245} 246 247intie_match_stat(const struct index_state *istate, 248struct cache_entry *ce,struct stat *st, 249unsigned int options) 250{ 251unsigned int changed; 252int ignore_valid = options & CE_MATCH_IGNORE_VALID; 253int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; 254int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 255 256/* 257 * If it's marked as always valid in the index, it's 258 * valid whatever the checked-out copy says. 259 * 260 * skip-worktree has the same effect with higher precedence 261 */ 262if(!ignore_skip_worktree &&ce_skip_worktree(ce)) 263return0; 264if(!ignore_valid && (ce->ce_flags & CE_VALID)) 265return0; 266 267/* 268 * Intent-to-add entries have not been added, so the index entry 269 * by definition never matches what is in the work tree until it 270 * actually gets added. 271 */ 272if(ce->ce_flags & CE_INTENT_TO_ADD) 273return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; 274 275 changed =ce_match_stat_basic(ce, st); 276 277/* 278 * Within 1 second of this sequence: 279 * echo xyzzy >file && git-update-index --add file 280 * running this command: 281 * echo frotz >file 282 * would give a falsely clean cache entry. The mtime and 283 * length match the cache, and other stat fields do not change. 284 * 285 * We could detect this at update-index time (the cache entry 286 * being registered/updated records the same time as "now") 287 * and delay the return from git-update-index, but that would 288 * effectively mean we can make at most one commit per second, 289 * which is not acceptable. Instead, we check cache entries 290 * whose mtime are the same as the index file timestamp more 291 * carefully than others. 292 */ 293if(!changed &&is_racy_timestamp(istate, ce)) { 294if(assume_racy_is_modified) 295 changed |= DATA_CHANGED; 296else 297 changed |=ce_modified_check_fs(ce, st); 298} 299 300return changed; 301} 302 303intie_modified(const struct index_state *istate, 304struct cache_entry *ce,struct stat *st,unsigned int options) 305{ 306int changed, changed_fs; 307 308 changed =ie_match_stat(istate, ce, st, options); 309if(!changed) 310return0; 311/* 312 * If the mode or type has changed, there's no point in trying 313 * to refresh the entry - it's not going to match 314 */ 315if(changed & (MODE_CHANGED | TYPE_CHANGED)) 316return changed; 317 318/* 319 * Immediately after read-tree or update-index --cacheinfo, 320 * the length field is zero, as we have never even read the 321 * lstat(2) information once, and we cannot trust DATA_CHANGED 322 * returned by ie_match_stat() which in turn was returned by 323 * ce_match_stat_basic() to signal that the filesize of the 324 * blob changed. We have to actually go to the filesystem to 325 * see if the contents match, and if so, should answer "unchanged". 326 * 327 * The logic does not apply to gitlinks, as ce_match_stat_basic() 328 * already has checked the actual HEAD from the filesystem in the 329 * subproject. If ie_match_stat() already said it is different, 330 * then we know it is. 331 */ 332if((changed & DATA_CHANGED) && 333(S_ISGITLINK(ce->ce_mode) || ce->ce_size !=0)) 334return changed; 335 336 changed_fs =ce_modified_check_fs(ce, st); 337if(changed_fs) 338return changed | changed_fs; 339return0; 340} 341 342intbase_name_compare(const char*name1,int len1,int mode1, 343const char*name2,int len2,int mode2) 344{ 345unsigned char c1, c2; 346int len = len1 < len2 ? len1 : len2; 347int cmp; 348 349 cmp =memcmp(name1, name2, len); 350if(cmp) 351return cmp; 352 c1 = name1[len]; 353 c2 = name2[len]; 354if(!c1 &&S_ISDIR(mode1)) 355 c1 ='/'; 356if(!c2 &&S_ISDIR(mode2)) 357 c2 ='/'; 358return(c1 < c2) ? -1: (c1 > c2) ?1:0; 359} 360 361/* 362 * df_name_compare() is identical to base_name_compare(), except it 363 * compares conflicting directory/file entries as equal. Note that 364 * while a directory name compares as equal to a regular file, they 365 * then individually compare _differently_ to a filename that has 366 * a dot after the basename (because '\0' < '.' < '/'). 367 * 368 * This is used by routines that want to traverse the git namespace 369 * but then handle conflicting entries together when possible. 370 */ 371intdf_name_compare(const char*name1,int len1,int mode1, 372const char*name2,int len2,int mode2) 373{ 374int len = len1 < len2 ? len1 : len2, cmp; 375unsigned char c1, c2; 376 377 cmp =memcmp(name1, name2, len); 378if(cmp) 379return cmp; 380/* Directories and files compare equal (same length, same name) */ 381if(len1 == len2) 382return0; 383 c1 = name1[len]; 384if(!c1 &&S_ISDIR(mode1)) 385 c1 ='/'; 386 c2 = name2[len]; 387if(!c2 &&S_ISDIR(mode2)) 388 c2 ='/'; 389if(c1 =='/'&& !c2) 390return0; 391if(c2 =='/'&& !c1) 392return0; 393return c1 - c2; 394} 395 396intcache_name_compare(const char*name1,int flags1,const char*name2,int flags2) 397{ 398int len1 = flags1 & CE_NAMEMASK; 399int len2 = flags2 & CE_NAMEMASK; 400int len = len1 < len2 ? len1 : len2; 401int cmp; 402 403 cmp =memcmp(name1, name2, len); 404if(cmp) 405return cmp; 406if(len1 < len2) 407return-1; 408if(len1 > len2) 409return1; 410 411/* Compare stages */ 412 flags1 &= CE_STAGEMASK; 413 flags2 &= CE_STAGEMASK; 414 415if(flags1 < flags2) 416return-1; 417if(flags1 > flags2) 418return1; 419return0; 420} 421 422intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 423{ 424int first, last; 425 426 first =0; 427 last = istate->cache_nr; 428while(last > first) { 429int next = (last + first) >>1; 430struct cache_entry *ce = istate->cache[next]; 431int cmp =cache_name_compare(name, namelen, ce->name, ce->ce_flags); 432if(!cmp) 433return next; 434if(cmp <0) { 435 last = next; 436continue; 437} 438 first = next+1; 439} 440return-first-1; 441} 442 443/* Remove entry, return true if there are more entries to go.. */ 444intremove_index_entry_at(struct index_state *istate,int pos) 445{ 446struct cache_entry *ce = istate->cache[pos]; 447 448record_resolve_undo(istate, ce); 449remove_name_hash(ce); 450 istate->cache_changed =1; 451 istate->cache_nr--; 452if(pos >= istate->cache_nr) 453return0; 454memmove(istate->cache + pos, 455 istate->cache + pos +1, 456(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 457return1; 458} 459 460/* 461 * Remove all cache ententries marked for removal, that is where 462 * CE_REMOVE is set in ce_flags. This is much more effective than 463 * calling remove_index_entry_at() for each entry to be removed. 464 */ 465voidremove_marked_cache_entries(struct index_state *istate) 466{ 467struct cache_entry **ce_array = istate->cache; 468unsigned int i, j; 469 470for(i = j =0; i < istate->cache_nr; i++) { 471if(ce_array[i]->ce_flags & CE_REMOVE) 472remove_name_hash(ce_array[i]); 473else 474 ce_array[j++] = ce_array[i]; 475} 476 istate->cache_changed =1; 477 istate->cache_nr = j; 478} 479 480intremove_file_from_index(struct index_state *istate,const char*path) 481{ 482int pos =index_name_pos(istate, path,strlen(path)); 483if(pos <0) 484 pos = -pos-1; 485cache_tree_invalidate_path(istate->cache_tree, path); 486while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 487remove_index_entry_at(istate, pos); 488return0; 489} 490 491static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 492{ 493return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 494} 495 496static intindex_name_pos_also_unmerged(struct index_state *istate, 497const char*path,int namelen) 498{ 499int pos =index_name_pos(istate, path, namelen); 500struct cache_entry *ce; 501 502if(pos >=0) 503return pos; 504 505/* maybe unmerged? */ 506 pos = -1- pos; 507if(pos >= istate->cache_nr || 508compare_name((ce = istate->cache[pos]), path, namelen)) 509return-1; 510 511/* order of preference: stage 2, 1, 3 */ 512if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 513ce_stage((ce = istate->cache[pos +1])) ==2&& 514!compare_name(ce, path, namelen)) 515 pos++; 516return pos; 517} 518 519static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 520{ 521int len =ce_namelen(ce); 522returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 523} 524 525/* 526 * If we add a filename that aliases in the cache, we will use the 527 * name that we already have - but we don't want to update the same 528 * alias twice, because that implies that there were actually two 529 * different files with aliasing names! 530 * 531 * So we use the CE_ADDED flag to verify that the alias was an old 532 * one before we accept it as 533 */ 534static struct cache_entry *create_alias_ce(struct cache_entry *ce,struct cache_entry *alias) 535{ 536int len; 537struct cache_entry *new; 538 539if(alias->ce_flags & CE_ADDED) 540die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 541 542/* Ok, create the new entry using the name of the existing alias */ 543 len =ce_namelen(alias); 544new=xcalloc(1,cache_entry_size(len)); 545memcpy(new->name, alias->name, len); 546copy_cache_entry(new, ce); 547free(ce); 548return new; 549} 550 551static voidrecord_intent_to_add(struct cache_entry *ce) 552{ 553unsigned char sha1[20]; 554if(write_sha1_file("",0, blob_type, sha1)) 555die("cannot create an empty blob in the object database"); 556hashcpy(ce->sha1, sha1); 557} 558 559intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 560{ 561int size, namelen, was_same; 562 mode_t st_mode = st->st_mode; 563struct cache_entry *ce, *alias; 564unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; 565int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 566int pretend = flags & ADD_CACHE_PRETEND; 567int intent_only = flags & ADD_CACHE_INTENT; 568int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| 569(intent_only ? ADD_CACHE_NEW_ONLY :0)); 570 571if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 572returnerror("%s: can only add regular files, symbolic links or git-directories", path); 573 574 namelen =strlen(path); 575if(S_ISDIR(st_mode)) { 576while(namelen && path[namelen-1] =='/') 577 namelen--; 578} 579 size =cache_entry_size(namelen); 580 ce =xcalloc(1, size); 581memcpy(ce->name, path, namelen); 582 ce->ce_flags = namelen; 583if(!intent_only) 584fill_stat_cache_info(ce, st); 585else 586 ce->ce_flags |= CE_INTENT_TO_ADD; 587 588if(trust_executable_bit && has_symlinks) 589 ce->ce_mode =create_ce_mode(st_mode); 590else{ 591/* If there is an existing entry, pick the mode bits and type 592 * from it, otherwise assume unexecutable regular file. 593 */ 594struct cache_entry *ent; 595int pos =index_name_pos_also_unmerged(istate, path, namelen); 596 597 ent = (0<= pos) ? istate->cache[pos] : NULL; 598 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 599} 600 601/* When core.ignorecase=true, determine if a directory of the same name but differing 602 * case already exists within the Git repository. If it does, ensure the directory 603 * case of the file being added to the repository matches (is folded into) the existing 604 * entry's directory case. 605 */ 606if(ignore_case) { 607const char*startPtr = ce->name; 608const char*ptr = startPtr; 609while(*ptr) { 610while(*ptr && *ptr !='/') 611++ptr; 612if(*ptr =='/') { 613struct cache_entry *foundce; 614++ptr; 615 foundce =index_name_exists(&the_index, ce->name, ptr - ce->name, ignore_case); 616if(foundce) { 617memcpy((void*)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr); 618 startPtr = ptr; 619} 620} 621} 622} 623 624 alias =index_name_exists(istate, ce->name,ce_namelen(ce), ignore_case); 625if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 626/* Nothing changed, really */ 627free(ce); 628if(!S_ISGITLINK(alias->ce_mode)) 629ce_mark_uptodate(alias); 630 alias->ce_flags |= CE_ADDED; 631return0; 632} 633if(!intent_only) { 634if(index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) 635returnerror("unable to index file%s", path); 636}else 637record_intent_to_add(ce); 638 639if(ignore_case && alias &&different_name(ce, alias)) 640 ce =create_alias_ce(ce, alias); 641 ce->ce_flags |= CE_ADDED; 642 643/* It was suspected to be racily clean, but it turns out to be Ok */ 644 was_same = (alias && 645!ce_stage(alias) && 646!hashcmp(alias->sha1, ce->sha1) && 647 ce->ce_mode == alias->ce_mode); 648 649if(pretend) 650; 651else if(add_index_entry(istate, ce, add_option)) 652returnerror("unable to add%sto index",path); 653if(verbose && !was_same) 654printf("add '%s'\n", path); 655return0; 656} 657 658intadd_file_to_index(struct index_state *istate,const char*path,int flags) 659{ 660struct stat st; 661if(lstat(path, &st)) 662die_errno("unable to stat '%s'", path); 663returnadd_to_index(istate, path, &st, flags); 664} 665 666struct cache_entry *make_cache_entry(unsigned int mode, 667const unsigned char*sha1,const char*path,int stage, 668int refresh) 669{ 670int size, len; 671struct cache_entry *ce; 672 673if(!verify_path(path)) { 674error("Invalid path '%s'", path); 675return NULL; 676} 677 678 len =strlen(path); 679 size =cache_entry_size(len); 680 ce =xcalloc(1, size); 681 682hashcpy(ce->sha1, sha1); 683memcpy(ce->name, path, len); 684 ce->ce_flags =create_ce_flags(len, stage); 685 ce->ce_mode =create_ce_mode(mode); 686 687if(refresh) 688returnrefresh_cache_entry(ce,0); 689 690return ce; 691} 692 693intce_same_name(struct cache_entry *a,struct cache_entry *b) 694{ 695int len =ce_namelen(a); 696returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 697} 698 699intce_path_match(const struct cache_entry *ce,const struct pathspec *pathspec) 700{ 701returnmatch_pathspec_depth(pathspec, ce->name,ce_namelen(ce),0, NULL); 702} 703 704/* 705 * We fundamentally don't like some paths: we don't want 706 * dot or dot-dot anywhere, and for obvious reasons don't 707 * want to recurse into ".git" either. 708 * 709 * Also, we don't want double slashes or slashes at the 710 * end that can make pathnames ambiguous. 711 */ 712static intverify_dotfile(const char*rest) 713{ 714/* 715 * The first character was '.', but that 716 * has already been discarded, we now test 717 * the rest. 718 */ 719 720/* "." is not allowed */ 721if(*rest =='\0'||is_dir_sep(*rest)) 722return0; 723 724switch(*rest) { 725/* 726 * ".git" followed by NUL or slash is bad. This 727 * shares the path end test with the ".." case. 728 */ 729case'g': 730if(rest[1] !='i') 731break; 732if(rest[2] !='t') 733break; 734 rest +=2; 735/* fallthrough */ 736case'.': 737if(rest[1] =='\0'||is_dir_sep(rest[1])) 738return0; 739} 740return1; 741} 742 743intverify_path(const char*path) 744{ 745char c; 746 747if(has_dos_drive_prefix(path)) 748return0; 749 750goto inside; 751for(;;) { 752if(!c) 753return1; 754if(is_dir_sep(c)) { 755inside: 756 c = *path++; 757if((c =='.'&& !verify_dotfile(path)) || 758is_dir_sep(c) || c =='\0') 759return0; 760} 761 c = *path++; 762} 763} 764 765/* 766 * Do we have another file that has the beginning components being a 767 * proper superset of the name we're trying to add? 768 */ 769static inthas_file_name(struct index_state *istate, 770const struct cache_entry *ce,int pos,int ok_to_replace) 771{ 772int retval =0; 773int len =ce_namelen(ce); 774int stage =ce_stage(ce); 775const char*name = ce->name; 776 777while(pos < istate->cache_nr) { 778struct cache_entry *p = istate->cache[pos++]; 779 780if(len >=ce_namelen(p)) 781break; 782if(memcmp(name, p->name, len)) 783break; 784if(ce_stage(p) != stage) 785continue; 786if(p->name[len] !='/') 787continue; 788if(p->ce_flags & CE_REMOVE) 789continue; 790 retval = -1; 791if(!ok_to_replace) 792break; 793remove_index_entry_at(istate, --pos); 794} 795return retval; 796} 797 798/* 799 * Do we have another file with a pathname that is a proper 800 * subset of the name we're trying to add? 801 */ 802static inthas_dir_name(struct index_state *istate, 803const struct cache_entry *ce,int pos,int ok_to_replace) 804{ 805int retval =0; 806int stage =ce_stage(ce); 807const char*name = ce->name; 808const char*slash = name +ce_namelen(ce); 809 810for(;;) { 811int len; 812 813for(;;) { 814if(*--slash =='/') 815break; 816if(slash <= ce->name) 817return retval; 818} 819 len = slash - name; 820 821 pos =index_name_pos(istate, name,create_ce_flags(len, stage)); 822if(pos >=0) { 823/* 824 * Found one, but not so fast. This could 825 * be a marker that says "I was here, but 826 * I am being removed". Such an entry is 827 * not a part of the resulting tree, and 828 * it is Ok to have a directory at the same 829 * path. 830 */ 831if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 832 retval = -1; 833if(!ok_to_replace) 834break; 835remove_index_entry_at(istate, pos); 836continue; 837} 838} 839else 840 pos = -pos-1; 841 842/* 843 * Trivial optimization: if we find an entry that 844 * already matches the sub-directory, then we know 845 * we're ok, and we can exit. 846 */ 847while(pos < istate->cache_nr) { 848struct cache_entry *p = istate->cache[pos]; 849if((ce_namelen(p) <= len) || 850(p->name[len] !='/') || 851memcmp(p->name, name, len)) 852break;/* not our subdirectory */ 853if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 854/* 855 * p is at the same stage as our entry, and 856 * is a subdirectory of what we are looking 857 * at, so we cannot have conflicts at our 858 * level or anything shorter. 859 */ 860return retval; 861 pos++; 862} 863} 864return retval; 865} 866 867/* We may be in a situation where we already have path/file and path 868 * is being added, or we already have path and path/file is being 869 * added. Either one would result in a nonsense tree that has path 870 * twice when git-write-tree tries to write it out. Prevent it. 871 * 872 * If ok-to-replace is specified, we remove the conflicting entries 873 * from the cache so the caller should recompute the insert position. 874 * When this happens, we return non-zero. 875 */ 876static intcheck_file_directory_conflict(struct index_state *istate, 877const struct cache_entry *ce, 878int pos,int ok_to_replace) 879{ 880int retval; 881 882/* 883 * When ce is an "I am going away" entry, we allow it to be added 884 */ 885if(ce->ce_flags & CE_REMOVE) 886return0; 887 888/* 889 * We check if the path is a sub-path of a subsequent pathname 890 * first, since removing those will not change the position 891 * in the array. 892 */ 893 retval =has_file_name(istate, ce, pos, ok_to_replace); 894 895/* 896 * Then check if the path might have a clashing sub-directory 897 * before it. 898 */ 899return retval +has_dir_name(istate, ce, pos, ok_to_replace); 900} 901 902static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 903{ 904int pos; 905int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 906int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 907int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 908int new_only = option & ADD_CACHE_NEW_ONLY; 909 910cache_tree_invalidate_path(istate->cache_tree, ce->name); 911 pos =index_name_pos(istate, ce->name, ce->ce_flags); 912 913/* existing match? Just replace it. */ 914if(pos >=0) { 915if(!new_only) 916replace_index_entry(istate, pos, ce); 917return0; 918} 919 pos = -pos-1; 920 921/* 922 * Inserting a merged entry ("stage 0") into the index 923 * will always replace all non-merged entries.. 924 */ 925if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 926while(ce_same_name(istate->cache[pos], ce)) { 927 ok_to_add =1; 928if(!remove_index_entry_at(istate, pos)) 929break; 930} 931} 932 933if(!ok_to_add) 934return-1; 935if(!verify_path(ce->name)) 936returnerror("Invalid path '%s'", ce->name); 937 938if(!skip_df_check && 939check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 940if(!ok_to_replace) 941returnerror("'%s' appears as both a file and as a directory", 942 ce->name); 943 pos =index_name_pos(istate, ce->name, ce->ce_flags); 944 pos = -pos-1; 945} 946return pos +1; 947} 948 949intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 950{ 951int pos; 952 953if(option & ADD_CACHE_JUST_APPEND) 954 pos = istate->cache_nr; 955else{ 956int ret; 957 ret =add_index_entry_with_check(istate, ce, option); 958if(ret <=0) 959return ret; 960 pos = ret -1; 961} 962 963/* Make sure the array is big enough .. */ 964if(istate->cache_nr == istate->cache_alloc) { 965 istate->cache_alloc =alloc_nr(istate->cache_alloc); 966 istate->cache =xrealloc(istate->cache, 967 istate->cache_alloc *sizeof(struct cache_entry *)); 968} 969 970/* Add it in.. */ 971 istate->cache_nr++; 972if(istate->cache_nr > pos +1) 973memmove(istate->cache + pos +1, 974 istate->cache + pos, 975(istate->cache_nr - pos -1) *sizeof(ce)); 976set_index_entry(istate, pos, ce); 977 istate->cache_changed =1; 978return0; 979} 980 981/* 982 * "refresh" does not calculate a new sha1 file or bring the 983 * cache up-to-date for mode/content changes. But what it 984 * _does_ do is to "re-match" the stat information of a file 985 * with the cache, so that you can refresh the cache for a 986 * file that hasn't been changed but where the stat entry is 987 * out of date. 988 * 989 * For example, you'd want to do this after doing a "git-read-tree", 990 * to link up the stat cache details with the proper files. 991 */ 992static struct cache_entry *refresh_cache_ent(struct index_state *istate, 993struct cache_entry *ce, 994unsigned int options,int*err, 995int*changed_ret) 996{ 997struct stat st; 998struct cache_entry *updated; 999int changed, size;1000int ignore_valid = options & CE_MATCH_IGNORE_VALID;1001int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;10021003if(ce_uptodate(ce))1004return ce;10051006/*1007 * CE_VALID or CE_SKIP_WORKTREE means the user promised us1008 * that the change to the work tree does not matter and told1009 * us not to worry.1010 */1011if(!ignore_skip_worktree &&ce_skip_worktree(ce)) {1012ce_mark_uptodate(ce);1013return ce;1014}1015if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1016ce_mark_uptodate(ce);1017return ce;1018}10191020if(lstat(ce->name, &st) <0) {1021if(err)1022*err = errno;1023return NULL;1024}10251026 changed =ie_match_stat(istate, ce, &st, options);1027if(changed_ret)1028*changed_ret = changed;1029if(!changed) {1030/*1031 * The path is unchanged. If we were told to ignore1032 * valid bit, then we did the actual stat check and1033 * found that the entry is unmodified. If the entry1034 * is not marked VALID, this is the place to mark it1035 * valid again, under "assume unchanged" mode.1036 */1037if(ignore_valid && assume_unchanged &&1038!(ce->ce_flags & CE_VALID))1039;/* mark this one VALID again */1040else{1041/*1042 * We do not mark the index itself "modified"1043 * because CE_UPTODATE flag is in-core only;1044 * we are not going to write this change out.1045 */1046if(!S_ISGITLINK(ce->ce_mode))1047ce_mark_uptodate(ce);1048return ce;1049}1050}10511052if(ie_modified(istate, ce, &st, options)) {1053if(err)1054*err = EINVAL;1055return NULL;1056}10571058 size =ce_size(ce);1059 updated =xmalloc(size);1060memcpy(updated, ce, size);1061fill_stat_cache_info(updated, &st);1062/*1063 * If ignore_valid is not set, we should leave CE_VALID bit1064 * alone. Otherwise, paths marked with --no-assume-unchanged1065 * (i.e. things to be edited) will reacquire CE_VALID bit1066 * automatically, which is not really what we want.1067 */1068if(!ignore_valid && assume_unchanged &&1069!(ce->ce_flags & CE_VALID))1070 updated->ce_flags &= ~CE_VALID;10711072return updated;1073}10741075static voidshow_file(const char* fmt,const char* name,int in_porcelain,1076int* first,const char*header_msg)1077{1078if(in_porcelain && *first && header_msg) {1079printf("%s\n", header_msg);1080*first =0;1081}1082printf(fmt, name);1083}10841085intrefresh_index(struct index_state *istate,unsigned int flags,const char**pathspec,1086char*seen,const char*header_msg)1087{1088int i;1089int has_errors =0;1090int really = (flags & REFRESH_REALLY) !=0;1091int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1092int quiet = (flags & REFRESH_QUIET) !=0;1093int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1094int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1095int first =1;1096int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1097unsigned int options = really ? CE_MATCH_IGNORE_VALID :0;1098const char*modified_fmt;1099const char*deleted_fmt;1100const char*typechange_fmt;1101const char*added_fmt;1102const char*unmerged_fmt;11031104 modified_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1105 deleted_fmt = (in_porcelain ?"D\t%s\n":"%s: needs update\n");1106 typechange_fmt = (in_porcelain ?"T\t%s\n":"%sneeds update\n");1107 added_fmt = (in_porcelain ?"A\t%s\n":"%sneeds update\n");1108 unmerged_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1109for(i =0; i < istate->cache_nr; i++) {1110struct cache_entry *ce, *new;1111int cache_errno =0;1112int changed =0;1113int filtered =0;11141115 ce = istate->cache[i];1116if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1117continue;11181119if(pathspec &&1120!match_pathspec(pathspec, ce->name,strlen(ce->name),0, seen))1121 filtered =1;11221123if(ce_stage(ce)) {1124while((i < istate->cache_nr) &&1125!strcmp(istate->cache[i]->name, ce->name))1126 i++;1127 i--;1128if(allow_unmerged)1129continue;1130if(!filtered)1131show_file(unmerged_fmt, ce->name, in_porcelain,1132&first, header_msg);1133 has_errors =1;1134continue;1135}11361137if(filtered)1138continue;11391140new=refresh_cache_ent(istate, ce, options, &cache_errno, &changed);1141if(new== ce)1142continue;1143if(!new) {1144const char*fmt;11451146if(not_new && cache_errno == ENOENT)1147continue;1148if(really && cache_errno == EINVAL) {1149/* If we are doing --really-refresh that1150 * means the index is not valid anymore.1151 */1152 ce->ce_flags &= ~CE_VALID;1153 istate->cache_changed =1;1154}1155if(quiet)1156continue;11571158if(cache_errno == ENOENT)1159 fmt = deleted_fmt;1160else if(ce->ce_flags & CE_INTENT_TO_ADD)1161 fmt = added_fmt;/* must be before other checks */1162else if(changed & TYPE_CHANGED)1163 fmt = typechange_fmt;1164else1165 fmt = modified_fmt;1166show_file(fmt,1167 ce->name, in_porcelain, &first, header_msg);1168 has_errors =1;1169continue;1170}11711172replace_index_entry(istate, i,new);1173}1174return has_errors;1175}11761177static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really)1178{1179returnrefresh_cache_ent(&the_index, ce, really, NULL, NULL);1180}11811182static intverify_hdr(struct cache_header *hdr,unsigned long size)1183{1184 git_SHA_CTX c;1185unsigned char sha1[20];11861187if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1188returnerror("bad signature");1189if(hdr->hdr_version !=htonl(2) && hdr->hdr_version !=htonl(3))1190returnerror("bad index version");1191git_SHA1_Init(&c);1192git_SHA1_Update(&c, hdr, size -20);1193git_SHA1_Final(sha1, &c);1194if(hashcmp(sha1, (unsigned char*)hdr + size -20))1195returnerror("bad index file sha1 signature");1196return0;1197}11981199static intread_index_extension(struct index_state *istate,1200const char*ext,void*data,unsigned long sz)1201{1202switch(CACHE_EXT(ext)) {1203case CACHE_EXT_TREE:1204 istate->cache_tree =cache_tree_read(data, sz);1205break;1206case CACHE_EXT_RESOLVE_UNDO:1207 istate->resolve_undo =resolve_undo_read(data, sz);1208break;1209default:1210if(*ext <'A'||'Z'< *ext)1211returnerror("index uses %.4s extension, which we do not understand",1212 ext);1213fprintf(stderr,"ignoring %.4s extension\n", ext);1214break;1215}1216return0;1217}12181219intread_index(struct index_state *istate)1220{1221returnread_index_from(istate,get_index_file());1222}12231224static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk)1225{1226struct cache_entry *ce;1227size_t len;1228const char*name;1229unsigned int flags;12301231/* On-disk flags are just 16 bits */1232 flags =ntohs(ondisk->flags);1233 len = flags & CE_NAMEMASK;12341235if(flags & CE_EXTENDED) {1236struct ondisk_cache_entry_extended *ondisk2;1237int extended_flags;1238 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1239 extended_flags =ntohs(ondisk2->flags2) <<16;1240/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1241if(extended_flags & ~CE_EXTENDED_FLAGS)1242die("Unknown index entry format%08x", extended_flags);1243 flags |= extended_flags;1244 name = ondisk2->name;1245}1246else1247 name = ondisk->name;12481249if(len == CE_NAMEMASK)1250 len =strlen(name);12511252 ce =xmalloc(cache_entry_size(len));12531254 ce->ce_ctime.sec =ntohl(ondisk->ctime.sec);1255 ce->ce_mtime.sec =ntohl(ondisk->mtime.sec);1256 ce->ce_ctime.nsec =ntohl(ondisk->ctime.nsec);1257 ce->ce_mtime.nsec =ntohl(ondisk->mtime.nsec);1258 ce->ce_dev =ntohl(ondisk->dev);1259 ce->ce_ino =ntohl(ondisk->ino);1260 ce->ce_mode =ntohl(ondisk->mode);1261 ce->ce_uid =ntohl(ondisk->uid);1262 ce->ce_gid =ntohl(ondisk->gid);1263 ce->ce_size =ntohl(ondisk->size);1264 ce->ce_flags = flags;12651266hashcpy(ce->sha1, ondisk->sha1);12671268memcpy(ce->name, name, len);1269 ce->name[len] ='\0';1270return ce;1271}12721273/* remember to discard_cache() before reading a different cache! */1274intread_index_from(struct index_state *istate,const char*path)1275{1276int fd, i;1277struct stat st;1278unsigned long src_offset;1279struct cache_header *hdr;1280void*mmap;1281size_t mmap_size;12821283 errno = EBUSY;1284if(istate->initialized)1285return istate->cache_nr;12861287 errno = ENOENT;1288 istate->timestamp.sec =0;1289 istate->timestamp.nsec =0;1290 fd =open(path, O_RDONLY);1291if(fd <0) {1292if(errno == ENOENT)1293return0;1294die_errno("index file open failed");1295}12961297if(fstat(fd, &st))1298die_errno("cannot stat the open index");12991300 errno = EINVAL;1301 mmap_size =xsize_t(st.st_size);1302if(mmap_size <sizeof(struct cache_header) +20)1303die("index file smaller than expected");13041305 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1306close(fd);1307if(mmap == MAP_FAILED)1308die_errno("unable to map index file");13091310 hdr = mmap;1311if(verify_hdr(hdr, mmap_size) <0)1312goto unmap;13131314 istate->cache_nr =ntohl(hdr->hdr_entries);1315 istate->cache_alloc =alloc_nr(istate->cache_nr);1316 istate->cache =xcalloc(istate->cache_alloc,sizeof(struct cache_entry *));1317 istate->initialized =1;13181319 src_offset =sizeof(*hdr);1320for(i =0; i < istate->cache_nr; i++) {1321struct ondisk_cache_entry *disk_ce;1322struct cache_entry *ce;13231324 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1325 ce =create_from_disk(disk_ce);1326set_index_entry(istate, i, ce);13271328 src_offset +=ondisk_ce_size(ce);1329}1330 istate->timestamp.sec = st.st_mtime;1331 istate->timestamp.nsec =ST_MTIME_NSEC(st);13321333while(src_offset <= mmap_size -20-8) {1334/* After an array of active_nr index entries,1335 * there can be arbitrary number of extended1336 * sections, each of which is prefixed with1337 * extension name (4-byte) and section length1338 * in 4-byte network byte order.1339 */1340uint32_t extsize;1341memcpy(&extsize, (char*)mmap + src_offset +4,4);1342 extsize =ntohl(extsize);1343if(read_index_extension(istate,1344(const char*) mmap + src_offset,1345(char*) mmap + src_offset +8,1346 extsize) <0)1347goto unmap;1348 src_offset +=8;1349 src_offset += extsize;1350}1351munmap(mmap, mmap_size);1352return istate->cache_nr;13531354unmap:1355munmap(mmap, mmap_size);1356 errno = EINVAL;1357die("index file corrupt");1358}13591360intis_index_unborn(struct index_state *istate)1361{1362return(!istate->cache_nr && !istate->timestamp.sec);1363}13641365intdiscard_index(struct index_state *istate)1366{1367int i;13681369for(i =0; i < istate->cache_nr; i++)1370free(istate->cache[i]);1371resolve_undo_clear_index(istate);1372 istate->cache_nr =0;1373 istate->cache_changed =0;1374 istate->timestamp.sec =0;1375 istate->timestamp.nsec =0;1376 istate->name_hash_initialized =0;1377free_hash(&istate->name_hash);1378cache_tree_free(&(istate->cache_tree));1379 istate->initialized =0;13801381/* no need to throw away allocated active_cache */1382return0;1383}13841385intunmerged_index(const struct index_state *istate)1386{1387int i;1388for(i =0; i < istate->cache_nr; i++) {1389if(ce_stage(istate->cache[i]))1390return1;1391}1392return0;1393}13941395#define WRITE_BUFFER_SIZE 81921396static unsigned char write_buffer[WRITE_BUFFER_SIZE];1397static unsigned long write_buffer_len;13981399static intce_write_flush(git_SHA_CTX *context,int fd)1400{1401unsigned int buffered = write_buffer_len;1402if(buffered) {1403git_SHA1_Update(context, write_buffer, buffered);1404if(write_in_full(fd, write_buffer, buffered) != buffered)1405return-1;1406 write_buffer_len =0;1407}1408return0;1409}14101411static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1412{1413while(len) {1414unsigned int buffered = write_buffer_len;1415unsigned int partial = WRITE_BUFFER_SIZE - buffered;1416if(partial > len)1417 partial = len;1418memcpy(write_buffer + buffered, data, partial);1419 buffered += partial;1420if(buffered == WRITE_BUFFER_SIZE) {1421 write_buffer_len = buffered;1422if(ce_write_flush(context, fd))1423return-1;1424 buffered =0;1425}1426 write_buffer_len = buffered;1427 len -= partial;1428 data = (char*) data + partial;1429}1430return0;1431}14321433static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1434unsigned int ext,unsigned int sz)1435{1436 ext =htonl(ext);1437 sz =htonl(sz);1438return((ce_write(context, fd, &ext,4) <0) ||1439(ce_write(context, fd, &sz,4) <0)) ? -1:0;1440}14411442static intce_flush(git_SHA_CTX *context,int fd)1443{1444unsigned int left = write_buffer_len;14451446if(left) {1447 write_buffer_len =0;1448git_SHA1_Update(context, write_buffer, left);1449}14501451/* Flush first if not enough space for SHA1 signature */1452if(left +20> WRITE_BUFFER_SIZE) {1453if(write_in_full(fd, write_buffer, left) != left)1454return-1;1455 left =0;1456}14571458/* Append the SHA1 signature at the end */1459git_SHA1_Final(write_buffer + left, context);1460 left +=20;1461return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1462}14631464static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1465{1466/*1467 * The only thing we care about in this function is to smudge the1468 * falsely clean entry due to touch-update-touch race, so we leave1469 * everything else as they are. We are called for entries whose1470 * ce_mtime match the index file mtime.1471 *1472 * Note that this actually does not do much for gitlinks, for1473 * which ce_match_stat_basic() always goes to the actual1474 * contents. The caller checks with is_racy_timestamp() which1475 * always says "no" for gitlinks, so we are not called for them ;-)1476 */1477struct stat st;14781479if(lstat(ce->name, &st) <0)1480return;1481if(ce_match_stat_basic(ce, &st))1482return;1483if(ce_modified_check_fs(ce, &st)) {1484/* This is "racily clean"; smudge it. Note that this1485 * is a tricky code. At first glance, it may appear1486 * that it can break with this sequence:1487 *1488 * $ echo xyzzy >frotz1489 * $ git-update-index --add frotz1490 * $ : >frotz1491 * $ sleep 31492 * $ echo filfre >nitfol1493 * $ git-update-index --add nitfol1494 *1495 * but it does not. When the second update-index runs,1496 * it notices that the entry "frotz" has the same timestamp1497 * as index, and if we were to smudge it by resetting its1498 * size to zero here, then the object name recorded1499 * in index is the 6-byte file but the cached stat information1500 * becomes zero --- which would then match what we would1501 * obtain from the filesystem next time we stat("frotz").1502 *1503 * However, the second update-index, before calling1504 * this function, notices that the cached size is 61505 * bytes and what is on the filesystem is an empty1506 * file, and never calls us, so the cached size information1507 * for "frotz" stays 6 which does not match the filesystem.1508 */1509 ce->ce_size =0;1510}1511}15121513static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce)1514{1515int size =ondisk_ce_size(ce);1516struct ondisk_cache_entry *ondisk =xcalloc(1, size);1517char*name;1518int result;15191520 ondisk->ctime.sec =htonl(ce->ce_ctime.sec);1521 ondisk->mtime.sec =htonl(ce->ce_mtime.sec);1522 ondisk->ctime.nsec =htonl(ce->ce_ctime.nsec);1523 ondisk->mtime.nsec =htonl(ce->ce_mtime.nsec);1524 ondisk->dev =htonl(ce->ce_dev);1525 ondisk->ino =htonl(ce->ce_ino);1526 ondisk->mode =htonl(ce->ce_mode);1527 ondisk->uid =htonl(ce->ce_uid);1528 ondisk->gid =htonl(ce->ce_gid);1529 ondisk->size =htonl(ce->ce_size);1530hashcpy(ondisk->sha1, ce->sha1);1531 ondisk->flags =htons(ce->ce_flags);1532if(ce->ce_flags & CE_EXTENDED) {1533struct ondisk_cache_entry_extended *ondisk2;1534 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1535 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1536 name = ondisk2->name;1537}1538else1539 name = ondisk->name;1540memcpy(name, ce->name,ce_namelen(ce));15411542 result =ce_write(c, fd, ondisk, size);1543free(ondisk);1544return result;1545}15461547static inthas_racy_timestamp(struct index_state *istate)1548{1549int entries = istate->cache_nr;1550int i;15511552for(i =0; i < entries; i++) {1553struct cache_entry *ce = istate->cache[i];1554if(is_racy_timestamp(istate, ce))1555return1;1556}1557return0;1558}15591560/*1561 * Opportunisticly update the index but do not complain if we can't1562 */1563voidupdate_index_if_able(struct index_state *istate,struct lock_file *lockfile)1564{1565if((istate->cache_changed ||has_racy_timestamp(istate)) &&1566!write_index(istate, lockfile->fd))1567commit_locked_index(lockfile);1568else1569rollback_lock_file(lockfile);1570}15711572intwrite_index(struct index_state *istate,int newfd)1573{1574 git_SHA_CTX c;1575struct cache_header hdr;1576int i, err, removed, extended;1577struct cache_entry **cache = istate->cache;1578int entries = istate->cache_nr;1579struct stat st;15801581for(i = removed = extended =0; i < entries; i++) {1582if(cache[i]->ce_flags & CE_REMOVE)1583 removed++;15841585/* reduce extended entries if possible */1586 cache[i]->ce_flags &= ~CE_EXTENDED;1587if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {1588 extended++;1589 cache[i]->ce_flags |= CE_EXTENDED;1590}1591}15921593 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1594/* for extended format, increase version so older git won't try to read it */1595 hdr.hdr_version =htonl(extended ?3:2);1596 hdr.hdr_entries =htonl(entries - removed);15971598git_SHA1_Init(&c);1599if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1600return-1;16011602for(i =0; i < entries; i++) {1603struct cache_entry *ce = cache[i];1604if(ce->ce_flags & CE_REMOVE)1605continue;1606if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1607ce_smudge_racily_clean_entry(ce);1608if(ce_write_entry(&c, newfd, ce) <0)1609return-1;1610}16111612/* Write extension data here */1613if(istate->cache_tree) {1614struct strbuf sb = STRBUF_INIT;16151616cache_tree_write(&sb, istate->cache_tree);1617 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01618||ce_write(&c, newfd, sb.buf, sb.len) <0;1619strbuf_release(&sb);1620if(err)1621return-1;1622}1623if(istate->resolve_undo) {1624struct strbuf sb = STRBUF_INIT;16251626resolve_undo_write(&sb, istate->resolve_undo);1627 err =write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,1628 sb.len) <01629||ce_write(&c, newfd, sb.buf, sb.len) <0;1630strbuf_release(&sb);1631if(err)1632return-1;1633}16341635if(ce_flush(&c, newfd) ||fstat(newfd, &st))1636return-1;1637 istate->timestamp.sec = (unsigned int)st.st_mtime;1638 istate->timestamp.nsec =ST_MTIME_NSEC(st);1639return0;1640}16411642/*1643 * Read the index file that is potentially unmerged into given1644 * index_state, dropping any unmerged entries. Returns true if1645 * the index is unmerged. Callers who want to refuse to work1646 * from an unmerged state can call this and check its return value,1647 * instead of calling read_cache().1648 */1649intread_index_unmerged(struct index_state *istate)1650{1651int i;1652int unmerged =0;16531654read_index(istate);1655for(i =0; i < istate->cache_nr; i++) {1656struct cache_entry *ce = istate->cache[i];1657struct cache_entry *new_ce;1658int size, len;16591660if(!ce_stage(ce))1661continue;1662 unmerged =1;1663 len =strlen(ce->name);1664 size =cache_entry_size(len);1665 new_ce =xcalloc(1, size);1666memcpy(new_ce->name, ce->name, len);1667 new_ce->ce_flags =create_ce_flags(len,0) | CE_CONFLICTED;1668 new_ce->ce_mode = ce->ce_mode;1669if(add_index_entry(istate, new_ce,0))1670returnerror("%s: cannot drop to stage #0",1671 ce->name);1672 i =index_name_pos(istate, new_ce->name, len);1673}1674return unmerged;1675}16761677/*1678 * Returns 1 if the path is an "other" path with respect to1679 * the index; that is, the path is not mentioned in the index at all,1680 * either as a file, a directory with some files in the index,1681 * or as an unmerged entry.1682 *1683 * We helpfully remove a trailing "/" from directories so that1684 * the output of read_directory can be used as-is.1685 */1686intindex_name_is_other(const struct index_state *istate,const char*name,1687int namelen)1688{1689int pos;1690if(namelen && name[namelen -1] =='/')1691 namelen--;1692 pos =index_name_pos(istate, name, namelen);1693if(0<= pos)1694return0;/* exact match */1695 pos = -pos -1;1696if(pos < istate->cache_nr) {1697struct cache_entry *ce = istate->cache[pos];1698if(ce_namelen(ce) == namelen &&1699!memcmp(ce->name, name, namelen))1700return0;/* Yup, this one exists unmerged */1701}1702return1;1703}