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"tempfile.h" 9#include"lockfile.h" 10#include"cache-tree.h" 11#include"refs.h" 12#include"dir.h" 13#include"tree.h" 14#include"commit.h" 15#include"blob.h" 16#include"resolve-undo.h" 17#include"strbuf.h" 18#include"varint.h" 19#include"split-index.h" 20#include"utf8.h" 21 22/* Mask for the name length in ce_flags in the on-disk index */ 23 24#define CE_NAMEMASK (0x0fff) 25 26/* Index extensions. 27 * 28 * The first letter should be 'A'..'Z' for extensions that are not 29 * necessary for a correct operation (i.e. optimization data). 30 * When new extensions are added that _needs_ to be understood in 31 * order to correctly interpret the index file, pick character that 32 * is outside the range, to cause the reader to abort. 33 */ 34 35#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) 36#define CACHE_EXT_TREE 0x54524545/* "TREE" */ 37#define CACHE_EXT_RESOLVE_UNDO 0x52455543/* "REUC" */ 38#define CACHE_EXT_LINK 0x6c696e6b/* "link" */ 39#define CACHE_EXT_UNTRACKED 0x554E5452/* "UNTR" */ 40 41/* changes that can be kept in $GIT_DIR/index (basically all extensions) */ 42#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \ 43 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \ 44 SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED) 45 46struct index_state the_index; 47static const char*alternate_index_output; 48 49static voidset_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 50{ 51 istate->cache[nr] = ce; 52add_name_hash(istate, ce); 53} 54 55static voidreplace_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 56{ 57struct cache_entry *old = istate->cache[nr]; 58 59replace_index_entry_in_base(istate, old, ce); 60remove_name_hash(istate, old); 61free(old); 62set_index_entry(istate, nr, ce); 63 ce->ce_flags |= CE_UPDATE_IN_BASE; 64 istate->cache_changed |= CE_ENTRY_CHANGED; 65} 66 67voidrename_index_entry_at(struct index_state *istate,int nr,const char*new_name) 68{ 69struct cache_entry *old = istate->cache[nr], *new; 70int namelen =strlen(new_name); 71 72new=xmalloc(cache_entry_size(namelen)); 73copy_cache_entry(new, old); 74new->ce_flags &= ~CE_HASHED; 75new->ce_namelen = namelen; 76new->index =0; 77memcpy(new->name, new_name, namelen +1); 78 79cache_tree_invalidate_path(istate, old->name); 80untracked_cache_remove_from_index(istate, old->name); 81remove_index_entry_at(istate, nr); 82add_index_entry(istate,new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 83} 84 85voidfill_stat_data(struct stat_data *sd,struct stat *st) 86{ 87 sd->sd_ctime.sec = (unsigned int)st->st_ctime; 88 sd->sd_mtime.sec = (unsigned int)st->st_mtime; 89 sd->sd_ctime.nsec =ST_CTIME_NSEC(*st); 90 sd->sd_mtime.nsec =ST_MTIME_NSEC(*st); 91 sd->sd_dev = st->st_dev; 92 sd->sd_ino = st->st_ino; 93 sd->sd_uid = st->st_uid; 94 sd->sd_gid = st->st_gid; 95 sd->sd_size = st->st_size; 96} 97 98intmatch_stat_data(const struct stat_data *sd,struct stat *st) 99{ 100int changed =0; 101 102if(sd->sd_mtime.sec != (unsigned int)st->st_mtime) 103 changed |= MTIME_CHANGED; 104if(trust_ctime && check_stat && 105 sd->sd_ctime.sec != (unsigned int)st->st_ctime) 106 changed |= CTIME_CHANGED; 107 108#ifdef USE_NSEC 109if(check_stat && sd->sd_mtime.nsec !=ST_MTIME_NSEC(*st)) 110 changed |= MTIME_CHANGED; 111if(trust_ctime && check_stat && 112 sd->sd_ctime.nsec !=ST_CTIME_NSEC(*st)) 113 changed |= CTIME_CHANGED; 114#endif 115 116if(check_stat) { 117if(sd->sd_uid != (unsigned int) st->st_uid || 118 sd->sd_gid != (unsigned int) st->st_gid) 119 changed |= OWNER_CHANGED; 120if(sd->sd_ino != (unsigned int) st->st_ino) 121 changed |= INODE_CHANGED; 122} 123 124#ifdef USE_STDEV 125/* 126 * st_dev breaks on network filesystems where different 127 * clients will have different views of what "device" 128 * the filesystem is on 129 */ 130if(check_stat && sd->sd_dev != (unsigned int) st->st_dev) 131 changed |= INODE_CHANGED; 132#endif 133 134if(sd->sd_size != (unsigned int) st->st_size) 135 changed |= DATA_CHANGED; 136 137return changed; 138} 139 140/* 141 * This only updates the "non-critical" parts of the directory 142 * cache, ie the parts that aren't tracked by GIT, and only used 143 * to validate the cache. 144 */ 145voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 146{ 147fill_stat_data(&ce->ce_stat_data, st); 148 149if(assume_unchanged) 150 ce->ce_flags |= CE_VALID; 151 152if(S_ISREG(st->st_mode)) 153ce_mark_uptodate(ce); 154} 155 156static intce_compare_data(const struct cache_entry *ce,struct stat *st) 157{ 158int match = -1; 159static int cloexec = O_CLOEXEC; 160int fd =open(ce->name, O_RDONLY | cloexec); 161 162if((cloexec & O_CLOEXEC) && fd <0&& errno == EINVAL) { 163/* Try again w/o O_CLOEXEC: the kernel might not support it */ 164 cloexec &= ~O_CLOEXEC; 165 fd =open(ce->name, O_RDONLY | cloexec); 166} 167 168if(fd >=0) { 169unsigned char sha1[20]; 170if(!index_fd(sha1, fd, st, OBJ_BLOB, ce->name,0)) 171 match =hashcmp(sha1, ce->oid.hash); 172/* index_fd() closed the file descriptor already */ 173} 174return match; 175} 176 177static intce_compare_link(const struct cache_entry *ce,size_t expected_size) 178{ 179int match = -1; 180void*buffer; 181unsigned long size; 182enum object_type type; 183struct strbuf sb = STRBUF_INIT; 184 185if(strbuf_readlink(&sb, ce->name, expected_size)) 186return-1; 187 188 buffer =read_sha1_file(ce->oid.hash, &type, &size); 189if(buffer) { 190if(size == sb.len) 191 match =memcmp(buffer, sb.buf, size); 192free(buffer); 193} 194strbuf_release(&sb); 195return match; 196} 197 198static intce_compare_gitlink(const struct cache_entry *ce) 199{ 200unsigned char sha1[20]; 201 202/* 203 * We don't actually require that the .git directory 204 * under GITLINK directory be a valid git directory. It 205 * might even be missing (in case nobody populated that 206 * sub-project). 207 * 208 * If so, we consider it always to match. 209 */ 210if(resolve_gitlink_ref(ce->name,"HEAD", sha1) <0) 211return0; 212returnhashcmp(sha1, ce->oid.hash); 213} 214 215static intce_modified_check_fs(const struct cache_entry *ce,struct stat *st) 216{ 217switch(st->st_mode & S_IFMT) { 218case S_IFREG: 219if(ce_compare_data(ce, st)) 220return DATA_CHANGED; 221break; 222case S_IFLNK: 223if(ce_compare_link(ce,xsize_t(st->st_size))) 224return DATA_CHANGED; 225break; 226case S_IFDIR: 227if(S_ISGITLINK(ce->ce_mode)) 228returnce_compare_gitlink(ce) ? DATA_CHANGED :0; 229default: 230return TYPE_CHANGED; 231} 232return0; 233} 234 235static intce_match_stat_basic(const struct cache_entry *ce,struct stat *st) 236{ 237unsigned int changed =0; 238 239if(ce->ce_flags & CE_REMOVE) 240return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; 241 242switch(ce->ce_mode & S_IFMT) { 243case S_IFREG: 244 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 245/* We consider only the owner x bit to be relevant for 246 * "mode changes" 247 */ 248if(trust_executable_bit && 249(0100& (ce->ce_mode ^ st->st_mode))) 250 changed |= MODE_CHANGED; 251break; 252case S_IFLNK: 253if(!S_ISLNK(st->st_mode) && 254(has_symlinks || !S_ISREG(st->st_mode))) 255 changed |= TYPE_CHANGED; 256break; 257case S_IFGITLINK: 258/* We ignore most of the st_xxx fields for gitlinks */ 259if(!S_ISDIR(st->st_mode)) 260 changed |= TYPE_CHANGED; 261else if(ce_compare_gitlink(ce)) 262 changed |= DATA_CHANGED; 263return changed; 264default: 265die("internal error: ce_mode is%o", ce->ce_mode); 266} 267 268 changed |=match_stat_data(&ce->ce_stat_data, st); 269 270/* Racily smudged entry? */ 271if(!ce->ce_stat_data.sd_size) { 272if(!is_empty_blob_sha1(ce->oid.hash)) 273 changed |= DATA_CHANGED; 274} 275 276return changed; 277} 278 279static intis_racy_stat(const struct index_state *istate, 280const struct stat_data *sd) 281{ 282return(istate->timestamp.sec && 283#ifdef USE_NSEC 284/* nanosecond timestamped files can also be racy! */ 285(istate->timestamp.sec < sd->sd_mtime.sec || 286(istate->timestamp.sec == sd->sd_mtime.sec && 287 istate->timestamp.nsec <= sd->sd_mtime.nsec)) 288#else 289 istate->timestamp.sec <= sd->sd_mtime.sec 290#endif 291); 292} 293 294static intis_racy_timestamp(const struct index_state *istate, 295const struct cache_entry *ce) 296{ 297return(!S_ISGITLINK(ce->ce_mode) && 298is_racy_stat(istate, &ce->ce_stat_data)); 299} 300 301intmatch_stat_data_racy(const struct index_state *istate, 302const struct stat_data *sd,struct stat *st) 303{ 304if(is_racy_stat(istate, sd)) 305return MTIME_CHANGED; 306returnmatch_stat_data(sd, st); 307} 308 309intie_match_stat(const struct index_state *istate, 310const struct cache_entry *ce,struct stat *st, 311unsigned int options) 312{ 313unsigned int changed; 314int ignore_valid = options & CE_MATCH_IGNORE_VALID; 315int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; 316int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 317 318/* 319 * If it's marked as always valid in the index, it's 320 * valid whatever the checked-out copy says. 321 * 322 * skip-worktree has the same effect with higher precedence 323 */ 324if(!ignore_skip_worktree &&ce_skip_worktree(ce)) 325return0; 326if(!ignore_valid && (ce->ce_flags & CE_VALID)) 327return0; 328 329/* 330 * Intent-to-add entries have not been added, so the index entry 331 * by definition never matches what is in the work tree until it 332 * actually gets added. 333 */ 334if(ce_intent_to_add(ce)) 335return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; 336 337 changed =ce_match_stat_basic(ce, st); 338 339/* 340 * Within 1 second of this sequence: 341 * echo xyzzy >file && git-update-index --add file 342 * running this command: 343 * echo frotz >file 344 * would give a falsely clean cache entry. The mtime and 345 * length match the cache, and other stat fields do not change. 346 * 347 * We could detect this at update-index time (the cache entry 348 * being registered/updated records the same time as "now") 349 * and delay the return from git-update-index, but that would 350 * effectively mean we can make at most one commit per second, 351 * which is not acceptable. Instead, we check cache entries 352 * whose mtime are the same as the index file timestamp more 353 * carefully than others. 354 */ 355if(!changed &&is_racy_timestamp(istate, ce)) { 356if(assume_racy_is_modified) 357 changed |= DATA_CHANGED; 358else 359 changed |=ce_modified_check_fs(ce, st); 360} 361 362return changed; 363} 364 365intie_modified(const struct index_state *istate, 366const struct cache_entry *ce, 367struct stat *st,unsigned int options) 368{ 369int changed, changed_fs; 370 371 changed =ie_match_stat(istate, ce, st, options); 372if(!changed) 373return0; 374/* 375 * If the mode or type has changed, there's no point in trying 376 * to refresh the entry - it's not going to match 377 */ 378if(changed & (MODE_CHANGED | TYPE_CHANGED)) 379return changed; 380 381/* 382 * Immediately after read-tree or update-index --cacheinfo, 383 * the length field is zero, as we have never even read the 384 * lstat(2) information once, and we cannot trust DATA_CHANGED 385 * returned by ie_match_stat() which in turn was returned by 386 * ce_match_stat_basic() to signal that the filesize of the 387 * blob changed. We have to actually go to the filesystem to 388 * see if the contents match, and if so, should answer "unchanged". 389 * 390 * The logic does not apply to gitlinks, as ce_match_stat_basic() 391 * already has checked the actual HEAD from the filesystem in the 392 * subproject. If ie_match_stat() already said it is different, 393 * then we know it is. 394 */ 395if((changed & DATA_CHANGED) && 396(S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size !=0)) 397return changed; 398 399 changed_fs =ce_modified_check_fs(ce, st); 400if(changed_fs) 401return changed | changed_fs; 402return0; 403} 404 405intbase_name_compare(const char*name1,int len1,int mode1, 406const char*name2,int len2,int mode2) 407{ 408unsigned char c1, c2; 409int len = len1 < len2 ? len1 : len2; 410int cmp; 411 412 cmp =memcmp(name1, name2, len); 413if(cmp) 414return cmp; 415 c1 = name1[len]; 416 c2 = name2[len]; 417if(!c1 &&S_ISDIR(mode1)) 418 c1 ='/'; 419if(!c2 &&S_ISDIR(mode2)) 420 c2 ='/'; 421return(c1 < c2) ? -1: (c1 > c2) ?1:0; 422} 423 424/* 425 * df_name_compare() is identical to base_name_compare(), except it 426 * compares conflicting directory/file entries as equal. Note that 427 * while a directory name compares as equal to a regular file, they 428 * then individually compare _differently_ to a filename that has 429 * a dot after the basename (because '\0' < '.' < '/'). 430 * 431 * This is used by routines that want to traverse the git namespace 432 * but then handle conflicting entries together when possible. 433 */ 434intdf_name_compare(const char*name1,int len1,int mode1, 435const char*name2,int len2,int mode2) 436{ 437int len = len1 < len2 ? len1 : len2, cmp; 438unsigned char c1, c2; 439 440 cmp =memcmp(name1, name2, len); 441if(cmp) 442return cmp; 443/* Directories and files compare equal (same length, same name) */ 444if(len1 == len2) 445return0; 446 c1 = name1[len]; 447if(!c1 &&S_ISDIR(mode1)) 448 c1 ='/'; 449 c2 = name2[len]; 450if(!c2 &&S_ISDIR(mode2)) 451 c2 ='/'; 452if(c1 =='/'&& !c2) 453return0; 454if(c2 =='/'&& !c1) 455return0; 456return c1 - c2; 457} 458 459intname_compare(const char*name1,size_t len1,const char*name2,size_t len2) 460{ 461size_t min_len = (len1 < len2) ? len1 : len2; 462int cmp =memcmp(name1, name2, min_len); 463if(cmp) 464return cmp; 465if(len1 < len2) 466return-1; 467if(len1 > len2) 468return1; 469return0; 470} 471 472intcache_name_stage_compare(const char*name1,int len1,int stage1,const char*name2,int len2,int stage2) 473{ 474int cmp; 475 476 cmp =name_compare(name1, len1, name2, len2); 477if(cmp) 478return cmp; 479 480if(stage1 < stage2) 481return-1; 482if(stage1 > stage2) 483return1; 484return0; 485} 486 487static intindex_name_stage_pos(const struct index_state *istate,const char*name,int namelen,int stage) 488{ 489int first, last; 490 491 first =0; 492 last = istate->cache_nr; 493while(last > first) { 494int next = (last + first) >>1; 495struct cache_entry *ce = istate->cache[next]; 496int cmp =cache_name_stage_compare(name, namelen, stage, ce->name,ce_namelen(ce),ce_stage(ce)); 497if(!cmp) 498return next; 499if(cmp <0) { 500 last = next; 501continue; 502} 503 first = next+1; 504} 505return-first-1; 506} 507 508intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 509{ 510returnindex_name_stage_pos(istate, name, namelen,0); 511} 512 513intremove_index_entry_at(struct index_state *istate,int pos) 514{ 515struct cache_entry *ce = istate->cache[pos]; 516 517record_resolve_undo(istate, ce); 518remove_name_hash(istate, ce); 519save_or_free_index_entry(istate, ce); 520 istate->cache_changed |= CE_ENTRY_REMOVED; 521 istate->cache_nr--; 522if(pos >= istate->cache_nr) 523return0; 524memmove(istate->cache + pos, 525 istate->cache + pos +1, 526(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 527return1; 528} 529 530/* 531 * Remove all cache entries marked for removal, that is where 532 * CE_REMOVE is set in ce_flags. This is much more effective than 533 * calling remove_index_entry_at() for each entry to be removed. 534 */ 535voidremove_marked_cache_entries(struct index_state *istate) 536{ 537struct cache_entry **ce_array = istate->cache; 538unsigned int i, j; 539 540for(i = j =0; i < istate->cache_nr; i++) { 541if(ce_array[i]->ce_flags & CE_REMOVE) { 542remove_name_hash(istate, ce_array[i]); 543save_or_free_index_entry(istate, ce_array[i]); 544} 545else 546 ce_array[j++] = ce_array[i]; 547} 548if(j == istate->cache_nr) 549return; 550 istate->cache_changed |= CE_ENTRY_REMOVED; 551 istate->cache_nr = j; 552} 553 554intremove_file_from_index(struct index_state *istate,const char*path) 555{ 556int pos =index_name_pos(istate, path,strlen(path)); 557if(pos <0) 558 pos = -pos-1; 559cache_tree_invalidate_path(istate, path); 560untracked_cache_remove_from_index(istate, path); 561while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 562remove_index_entry_at(istate, pos); 563return0; 564} 565 566static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 567{ 568return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 569} 570 571static intindex_name_pos_also_unmerged(struct index_state *istate, 572const char*path,int namelen) 573{ 574int pos =index_name_pos(istate, path, namelen); 575struct cache_entry *ce; 576 577if(pos >=0) 578return pos; 579 580/* maybe unmerged? */ 581 pos = -1- pos; 582if(pos >= istate->cache_nr || 583compare_name((ce = istate->cache[pos]), path, namelen)) 584return-1; 585 586/* order of preference: stage 2, 1, 3 */ 587if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 588ce_stage((ce = istate->cache[pos +1])) ==2&& 589!compare_name(ce, path, namelen)) 590 pos++; 591return pos; 592} 593 594static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 595{ 596int len =ce_namelen(ce); 597returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 598} 599 600/* 601 * If we add a filename that aliases in the cache, we will use the 602 * name that we already have - but we don't want to update the same 603 * alias twice, because that implies that there were actually two 604 * different files with aliasing names! 605 * 606 * So we use the CE_ADDED flag to verify that the alias was an old 607 * one before we accept it as 608 */ 609static struct cache_entry *create_alias_ce(struct index_state *istate, 610struct cache_entry *ce, 611struct cache_entry *alias) 612{ 613int len; 614struct cache_entry *new; 615 616if(alias->ce_flags & CE_ADDED) 617die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 618 619/* Ok, create the new entry using the name of the existing alias */ 620 len =ce_namelen(alias); 621new=xcalloc(1,cache_entry_size(len)); 622memcpy(new->name, alias->name, len); 623copy_cache_entry(new, ce); 624save_or_free_index_entry(istate, ce); 625return new; 626} 627 628voidset_object_name_for_intent_to_add_entry(struct cache_entry *ce) 629{ 630unsigned char sha1[20]; 631if(write_sha1_file("",0, blob_type, sha1)) 632die("cannot create an empty blob in the object database"); 633hashcpy(ce->oid.hash, sha1); 634} 635 636intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 637{ 638int size, namelen, was_same; 639 mode_t st_mode = st->st_mode; 640struct cache_entry *ce, *alias; 641unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; 642int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 643int pretend = flags & ADD_CACHE_PRETEND; 644int intent_only = flags & ADD_CACHE_INTENT; 645int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| 646(intent_only ? ADD_CACHE_NEW_ONLY :0)); 647 648if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 649returnerror("%s: can only add regular files, symbolic links or git-directories", path); 650 651 namelen =strlen(path); 652if(S_ISDIR(st_mode)) { 653while(namelen && path[namelen-1] =='/') 654 namelen--; 655} 656 size =cache_entry_size(namelen); 657 ce =xcalloc(1, size); 658memcpy(ce->name, path, namelen); 659 ce->ce_namelen = namelen; 660if(!intent_only) 661fill_stat_cache_info(ce, st); 662else 663 ce->ce_flags |= CE_INTENT_TO_ADD; 664 665 666if(trust_executable_bit && has_symlinks) { 667 ce->ce_mode =create_ce_mode(st_mode); 668}else{ 669/* If there is an existing entry, pick the mode bits and type 670 * from it, otherwise assume unexecutable regular file. 671 */ 672struct cache_entry *ent; 673int pos =index_name_pos_also_unmerged(istate, path, namelen); 674 675 ent = (0<= pos) ? istate->cache[pos] : NULL; 676 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 677} 678 679/* When core.ignorecase=true, determine if a directory of the same name but differing 680 * case already exists within the Git repository. If it does, ensure the directory 681 * case of the file being added to the repository matches (is folded into) the existing 682 * entry's directory case. 683 */ 684if(ignore_case) { 685adjust_dirname_case(istate, ce->name); 686} 687 688 alias =index_file_exists(istate, ce->name,ce_namelen(ce), ignore_case); 689if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 690/* Nothing changed, really */ 691if(!S_ISGITLINK(alias->ce_mode)) 692ce_mark_uptodate(alias); 693 alias->ce_flags |= CE_ADDED; 694 695free(ce); 696return0; 697} 698if(!intent_only) { 699if(index_path(ce->oid.hash, path, st, HASH_WRITE_OBJECT)) { 700free(ce); 701returnerror("unable to index file%s", path); 702} 703}else 704set_object_name_for_intent_to_add_entry(ce); 705 706if(ignore_case && alias &&different_name(ce, alias)) 707 ce =create_alias_ce(istate, ce, alias); 708 ce->ce_flags |= CE_ADDED; 709 710/* It was suspected to be racily clean, but it turns out to be Ok */ 711 was_same = (alias && 712!ce_stage(alias) && 713!oidcmp(&alias->oid, &ce->oid) && 714 ce->ce_mode == alias->ce_mode); 715 716if(pretend) 717free(ce); 718else if(add_index_entry(istate, ce, add_option)) { 719free(ce); 720returnerror("unable to add%sto index", path); 721} 722if(verbose && !was_same) 723printf("add '%s'\n", path); 724return0; 725} 726 727intadd_file_to_index(struct index_state *istate,const char*path,int flags) 728{ 729struct stat st; 730if(lstat(path, &st)) 731die_errno("unable to stat '%s'", path); 732returnadd_to_index(istate, path, &st, flags); 733} 734 735struct cache_entry *make_cache_entry(unsigned int mode, 736const unsigned char*sha1,const char*path,int stage, 737unsigned int refresh_options) 738{ 739int size, len; 740struct cache_entry *ce, *ret; 741 742if(!verify_path(path)) { 743error("Invalid path '%s'", path); 744return NULL; 745} 746 747 len =strlen(path); 748 size =cache_entry_size(len); 749 ce =xcalloc(1, size); 750 751hashcpy(ce->oid.hash, sha1); 752memcpy(ce->name, path, len); 753 ce->ce_flags =create_ce_flags(stage); 754 ce->ce_namelen = len; 755 ce->ce_mode =create_ce_mode(mode); 756 757 ret =refresh_cache_entry(ce, refresh_options); 758if(ret != ce) 759free(ce); 760return ret; 761} 762 763/* 764 * Chmod an index entry with either +x or -x. 765 * 766 * Returns -1 if the chmod for the particular cache entry failed (if it's 767 * not a regular file), -2 if an invalid flip argument is passed in, 0 768 * otherwise. 769 */ 770intchmod_index_entry(struct index_state *istate,struct cache_entry *ce, 771char flip) 772{ 773if(!S_ISREG(ce->ce_mode)) 774return-1; 775switch(flip) { 776case'+': 777 ce->ce_mode |=0111; 778break; 779case'-': 780 ce->ce_mode &= ~0111; 781break; 782default: 783return-2; 784} 785cache_tree_invalidate_path(istate, ce->name); 786 ce->ce_flags |= CE_UPDATE_IN_BASE; 787 istate->cache_changed |= CE_ENTRY_CHANGED; 788 789return0; 790} 791 792intce_same_name(const struct cache_entry *a,const struct cache_entry *b) 793{ 794int len =ce_namelen(a); 795returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 796} 797 798/* 799 * We fundamentally don't like some paths: we don't want 800 * dot or dot-dot anywhere, and for obvious reasons don't 801 * want to recurse into ".git" either. 802 * 803 * Also, we don't want double slashes or slashes at the 804 * end that can make pathnames ambiguous. 805 */ 806static intverify_dotfile(const char*rest) 807{ 808/* 809 * The first character was '.', but that 810 * has already been discarded, we now test 811 * the rest. 812 */ 813 814/* "." is not allowed */ 815if(*rest =='\0'||is_dir_sep(*rest)) 816return0; 817 818switch(*rest) { 819/* 820 * ".git" followed by NUL or slash is bad. This 821 * shares the path end test with the ".." case. 822 */ 823case'g': 824case'G': 825if(rest[1] !='i'&& rest[1] !='I') 826break; 827if(rest[2] !='t'&& rest[2] !='T') 828break; 829 rest +=2; 830/* fallthrough */ 831case'.': 832if(rest[1] =='\0'||is_dir_sep(rest[1])) 833return0; 834} 835return1; 836} 837 838intverify_path(const char*path) 839{ 840char c; 841 842if(has_dos_drive_prefix(path)) 843return0; 844 845goto inside; 846for(;;) { 847if(!c) 848return1; 849if(is_dir_sep(c)) { 850inside: 851if(protect_hfs &&is_hfs_dotgit(path)) 852return0; 853if(protect_ntfs &&is_ntfs_dotgit(path)) 854return0; 855 c = *path++; 856if((c =='.'&& !verify_dotfile(path)) || 857is_dir_sep(c) || c =='\0') 858return0; 859} 860 c = *path++; 861} 862} 863 864/* 865 * Do we have another file that has the beginning components being a 866 * proper superset of the name we're trying to add? 867 */ 868static inthas_file_name(struct index_state *istate, 869const struct cache_entry *ce,int pos,int ok_to_replace) 870{ 871int retval =0; 872int len =ce_namelen(ce); 873int stage =ce_stage(ce); 874const char*name = ce->name; 875 876while(pos < istate->cache_nr) { 877struct cache_entry *p = istate->cache[pos++]; 878 879if(len >=ce_namelen(p)) 880break; 881if(memcmp(name, p->name, len)) 882break; 883if(ce_stage(p) != stage) 884continue; 885if(p->name[len] !='/') 886continue; 887if(p->ce_flags & CE_REMOVE) 888continue; 889 retval = -1; 890if(!ok_to_replace) 891break; 892remove_index_entry_at(istate, --pos); 893} 894return retval; 895} 896 897/* 898 * Do we have another file with a pathname that is a proper 899 * subset of the name we're trying to add? 900 */ 901static inthas_dir_name(struct index_state *istate, 902const struct cache_entry *ce,int pos,int ok_to_replace) 903{ 904int retval =0; 905int stage =ce_stage(ce); 906const char*name = ce->name; 907const char*slash = name +ce_namelen(ce); 908 909for(;;) { 910int len; 911 912for(;;) { 913if(*--slash =='/') 914break; 915if(slash <= ce->name) 916return retval; 917} 918 len = slash - name; 919 920 pos =index_name_stage_pos(istate, name, len, stage); 921if(pos >=0) { 922/* 923 * Found one, but not so fast. This could 924 * be a marker that says "I was here, but 925 * I am being removed". Such an entry is 926 * not a part of the resulting tree, and 927 * it is Ok to have a directory at the same 928 * path. 929 */ 930if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 931 retval = -1; 932if(!ok_to_replace) 933break; 934remove_index_entry_at(istate, pos); 935continue; 936} 937} 938else 939 pos = -pos-1; 940 941/* 942 * Trivial optimization: if we find an entry that 943 * already matches the sub-directory, then we know 944 * we're ok, and we can exit. 945 */ 946while(pos < istate->cache_nr) { 947struct cache_entry *p = istate->cache[pos]; 948if((ce_namelen(p) <= len) || 949(p->name[len] !='/') || 950memcmp(p->name, name, len)) 951break;/* not our subdirectory */ 952if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 953/* 954 * p is at the same stage as our entry, and 955 * is a subdirectory of what we are looking 956 * at, so we cannot have conflicts at our 957 * level or anything shorter. 958 */ 959return retval; 960 pos++; 961} 962} 963return retval; 964} 965 966/* We may be in a situation where we already have path/file and path 967 * is being added, or we already have path and path/file is being 968 * added. Either one would result in a nonsense tree that has path 969 * twice when git-write-tree tries to write it out. Prevent it. 970 * 971 * If ok-to-replace is specified, we remove the conflicting entries 972 * from the cache so the caller should recompute the insert position. 973 * When this happens, we return non-zero. 974 */ 975static intcheck_file_directory_conflict(struct index_state *istate, 976const struct cache_entry *ce, 977int pos,int ok_to_replace) 978{ 979int retval; 980 981/* 982 * When ce is an "I am going away" entry, we allow it to be added 983 */ 984if(ce->ce_flags & CE_REMOVE) 985return0; 986 987/* 988 * We check if the path is a sub-path of a subsequent pathname 989 * first, since removing those will not change the position 990 * in the array. 991 */ 992 retval =has_file_name(istate, ce, pos, ok_to_replace); 993 994/* 995 * Then check if the path might have a clashing sub-directory 996 * before it. 997 */ 998return retval +has_dir_name(istate, ce, pos, ok_to_replace); 999}10001001static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option)1002{1003int pos;1004int ok_to_add = option & ADD_CACHE_OK_TO_ADD;1005int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;1006int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;1007int new_only = option & ADD_CACHE_NEW_ONLY;10081009if(!(option & ADD_CACHE_KEEP_CACHE_TREE))1010cache_tree_invalidate_path(istate, ce->name);1011 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce));10121013/* existing match? Just replace it. */1014if(pos >=0) {1015if(!new_only)1016replace_index_entry(istate, pos, ce);1017return0;1018}1019 pos = -pos-1;10201021if(!(option & ADD_CACHE_KEEP_CACHE_TREE))1022untracked_cache_add_to_index(istate, ce->name);10231024/*1025 * Inserting a merged entry ("stage 0") into the index1026 * will always replace all non-merged entries..1027 */1028if(pos < istate->cache_nr &&ce_stage(ce) ==0) {1029while(ce_same_name(istate->cache[pos], ce)) {1030 ok_to_add =1;1031if(!remove_index_entry_at(istate, pos))1032break;1033}1034}10351036if(!ok_to_add)1037return-1;1038if(!verify_path(ce->name))1039returnerror("Invalid path '%s'", ce->name);10401041if(!skip_df_check &&1042check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {1043if(!ok_to_replace)1044returnerror("'%s' appears as both a file and as a directory",1045 ce->name);1046 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce));1047 pos = -pos-1;1048}1049return pos +1;1050}10511052intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option)1053{1054int pos;10551056if(option & ADD_CACHE_JUST_APPEND)1057 pos = istate->cache_nr;1058else{1059int ret;1060 ret =add_index_entry_with_check(istate, ce, option);1061if(ret <=0)1062return ret;1063 pos = ret -1;1064}10651066/* Make sure the array is big enough .. */1067ALLOC_GROW(istate->cache, istate->cache_nr +1, istate->cache_alloc);10681069/* Add it in.. */1070 istate->cache_nr++;1071if(istate->cache_nr > pos +1)1072memmove(istate->cache + pos +1,1073 istate->cache + pos,1074(istate->cache_nr - pos -1) *sizeof(ce));1075set_index_entry(istate, pos, ce);1076 istate->cache_changed |= CE_ENTRY_ADDED;1077return0;1078}10791080/*1081 * "refresh" does not calculate a new sha1 file or bring the1082 * cache up-to-date for mode/content changes. But what it1083 * _does_ do is to "re-match" the stat information of a file1084 * with the cache, so that you can refresh the cache for a1085 * file that hasn't been changed but where the stat entry is1086 * out of date.1087 *1088 * For example, you'd want to do this after doing a "git-read-tree",1089 * to link up the stat cache details with the proper files.1090 */1091static struct cache_entry *refresh_cache_ent(struct index_state *istate,1092struct cache_entry *ce,1093unsigned int options,int*err,1094int*changed_ret)1095{1096struct stat st;1097struct cache_entry *updated;1098int changed, size;1099int refresh = options & CE_MATCH_REFRESH;1100int ignore_valid = options & CE_MATCH_IGNORE_VALID;1101int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;1102int ignore_missing = options & CE_MATCH_IGNORE_MISSING;11031104if(!refresh ||ce_uptodate(ce))1105return ce;11061107/*1108 * CE_VALID or CE_SKIP_WORKTREE means the user promised us1109 * that the change to the work tree does not matter and told1110 * us not to worry.1111 */1112if(!ignore_skip_worktree &&ce_skip_worktree(ce)) {1113ce_mark_uptodate(ce);1114return ce;1115}1116if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1117ce_mark_uptodate(ce);1118return ce;1119}11201121if(has_symlink_leading_path(ce->name,ce_namelen(ce))) {1122if(ignore_missing)1123return ce;1124if(err)1125*err = ENOENT;1126return NULL;1127}11281129if(lstat(ce->name, &st) <0) {1130if(ignore_missing && errno == ENOENT)1131return ce;1132if(err)1133*err = errno;1134return NULL;1135}11361137 changed =ie_match_stat(istate, ce, &st, options);1138if(changed_ret)1139*changed_ret = changed;1140if(!changed) {1141/*1142 * The path is unchanged. If we were told to ignore1143 * valid bit, then we did the actual stat check and1144 * found that the entry is unmodified. If the entry1145 * is not marked VALID, this is the place to mark it1146 * valid again, under "assume unchanged" mode.1147 */1148if(ignore_valid && assume_unchanged &&1149!(ce->ce_flags & CE_VALID))1150;/* mark this one VALID again */1151else{1152/*1153 * We do not mark the index itself "modified"1154 * because CE_UPTODATE flag is in-core only;1155 * we are not going to write this change out.1156 */1157if(!S_ISGITLINK(ce->ce_mode))1158ce_mark_uptodate(ce);1159return ce;1160}1161}11621163if(ie_modified(istate, ce, &st, options)) {1164if(err)1165*err = EINVAL;1166return NULL;1167}11681169 size =ce_size(ce);1170 updated =xmalloc(size);1171memcpy(updated, ce, size);1172fill_stat_cache_info(updated, &st);1173/*1174 * If ignore_valid is not set, we should leave CE_VALID bit1175 * alone. Otherwise, paths marked with --no-assume-unchanged1176 * (i.e. things to be edited) will reacquire CE_VALID bit1177 * automatically, which is not really what we want.1178 */1179if(!ignore_valid && assume_unchanged &&1180!(ce->ce_flags & CE_VALID))1181 updated->ce_flags &= ~CE_VALID;11821183/* istate->cache_changed is updated in the caller */1184return updated;1185}11861187static voidshow_file(const char* fmt,const char* name,int in_porcelain,1188int* first,const char*header_msg)1189{1190if(in_porcelain && *first && header_msg) {1191printf("%s\n", header_msg);1192*first =0;1193}1194printf(fmt, name);1195}11961197intrefresh_index(struct index_state *istate,unsigned int flags,1198const struct pathspec *pathspec,1199char*seen,const char*header_msg)1200{1201int i;1202int has_errors =0;1203int really = (flags & REFRESH_REALLY) !=0;1204int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1205int quiet = (flags & REFRESH_QUIET) !=0;1206int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1207int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1208int first =1;1209int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1210unsigned int options = (CE_MATCH_REFRESH |1211(really ? CE_MATCH_IGNORE_VALID :0) |1212(not_new ? CE_MATCH_IGNORE_MISSING :0));1213const char*modified_fmt;1214const char*deleted_fmt;1215const char*typechange_fmt;1216const char*added_fmt;1217const char*unmerged_fmt;12181219 modified_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1220 deleted_fmt = (in_porcelain ?"D\t%s\n":"%s: needs update\n");1221 typechange_fmt = (in_porcelain ?"T\t%s\n":"%sneeds update\n");1222 added_fmt = (in_porcelain ?"A\t%s\n":"%sneeds update\n");1223 unmerged_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1224for(i =0; i < istate->cache_nr; i++) {1225struct cache_entry *ce, *new;1226int cache_errno =0;1227int changed =0;1228int filtered =0;12291230 ce = istate->cache[i];1231if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1232continue;12331234if(pathspec && !ce_path_match(ce, pathspec, seen))1235 filtered =1;12361237if(ce_stage(ce)) {1238while((i < istate->cache_nr) &&1239!strcmp(istate->cache[i]->name, ce->name))1240 i++;1241 i--;1242if(allow_unmerged)1243continue;1244if(!filtered)1245show_file(unmerged_fmt, ce->name, in_porcelain,1246&first, header_msg);1247 has_errors =1;1248continue;1249}12501251if(filtered)1252continue;12531254new=refresh_cache_ent(istate, ce, options, &cache_errno, &changed);1255if(new== ce)1256continue;1257if(!new) {1258const char*fmt;12591260if(really && cache_errno == EINVAL) {1261/* If we are doing --really-refresh that1262 * means the index is not valid anymore.1263 */1264 ce->ce_flags &= ~CE_VALID;1265 ce->ce_flags |= CE_UPDATE_IN_BASE;1266 istate->cache_changed |= CE_ENTRY_CHANGED;1267}1268if(quiet)1269continue;12701271if(cache_errno == ENOENT)1272 fmt = deleted_fmt;1273else if(ce_intent_to_add(ce))1274 fmt = added_fmt;/* must be before other checks */1275else if(changed & TYPE_CHANGED)1276 fmt = typechange_fmt;1277else1278 fmt = modified_fmt;1279show_file(fmt,1280 ce->name, in_porcelain, &first, header_msg);1281 has_errors =1;1282continue;1283}12841285replace_index_entry(istate, i,new);1286}1287return has_errors;1288}12891290struct cache_entry *refresh_cache_entry(struct cache_entry *ce,1291unsigned int options)1292{1293returnrefresh_cache_ent(&the_index, ce, options, NULL, NULL);1294}129512961297/*****************************************************************1298 * Index File I/O1299 *****************************************************************/13001301#define INDEX_FORMAT_DEFAULT 313021303static unsigned intget_index_format_default(void)1304{1305char*envversion =getenv("GIT_INDEX_VERSION");1306char*endp;1307int value;1308unsigned int version = INDEX_FORMAT_DEFAULT;13091310if(!envversion) {1311if(!git_config_get_int("index.version", &value))1312 version = value;1313if(version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1314warning(_("index.version set, but the value is invalid.\n"1315"Using version%i"), INDEX_FORMAT_DEFAULT);1316return INDEX_FORMAT_DEFAULT;1317}1318return version;1319}13201321 version =strtoul(envversion, &endp,10);1322if(*endp ||1323 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1324warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"1325"Using version%i"), INDEX_FORMAT_DEFAULT);1326 version = INDEX_FORMAT_DEFAULT;1327}1328return version;1329}13301331/*1332 * dev/ino/uid/gid/size are also just tracked to the low 32 bits1333 * Again - this is just a (very strong in practice) heuristic that1334 * the inode hasn't changed.1335 *1336 * We save the fields in big-endian order to allow using the1337 * index file over NFS transparently.1338 */1339struct ondisk_cache_entry {1340struct cache_time ctime;1341struct cache_time mtime;1342uint32_t dev;1343uint32_t ino;1344uint32_t mode;1345uint32_t uid;1346uint32_t gid;1347uint32_t size;1348unsigned char sha1[20];1349uint16_t flags;1350char name[FLEX_ARRAY];/* more */1351};13521353/*1354 * This struct is used when CE_EXTENDED bit is 11355 * The struct must match ondisk_cache_entry exactly from1356 * ctime till flags1357 */1358struct ondisk_cache_entry_extended {1359struct cache_time ctime;1360struct cache_time mtime;1361uint32_t dev;1362uint32_t ino;1363uint32_t mode;1364uint32_t uid;1365uint32_t gid;1366uint32_t size;1367unsigned char sha1[20];1368uint16_t flags;1369uint16_t flags2;1370char name[FLEX_ARRAY];/* more */1371};13721373/* These are only used for v3 or lower */1374#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)1375#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)1376#define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)1377#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \1378 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \1379 ondisk_cache_entry_size(ce_namelen(ce)))13801381static intverify_hdr(struct cache_header *hdr,unsigned long size)1382{1383 git_SHA_CTX c;1384unsigned char sha1[20];1385int hdr_version;13861387if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1388returnerror("bad signature");1389 hdr_version =ntohl(hdr->hdr_version);1390if(hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)1391returnerror("bad index version%d", hdr_version);1392git_SHA1_Init(&c);1393git_SHA1_Update(&c, hdr, size -20);1394git_SHA1_Final(sha1, &c);1395if(hashcmp(sha1, (unsigned char*)hdr + size -20))1396returnerror("bad index file sha1 signature");1397return0;1398}13991400static intread_index_extension(struct index_state *istate,1401const char*ext,void*data,unsigned long sz)1402{1403switch(CACHE_EXT(ext)) {1404case CACHE_EXT_TREE:1405 istate->cache_tree =cache_tree_read(data, sz);1406break;1407case CACHE_EXT_RESOLVE_UNDO:1408 istate->resolve_undo =resolve_undo_read(data, sz);1409break;1410case CACHE_EXT_LINK:1411if(read_link_extension(istate, data, sz))1412return-1;1413break;1414case CACHE_EXT_UNTRACKED:1415 istate->untracked =read_untracked_extension(data, sz);1416break;1417default:1418if(*ext <'A'||'Z'< *ext)1419returnerror("index uses %.4s extension, which we do not understand",1420 ext);1421fprintf(stderr,"ignoring %.4s extension\n", ext);1422break;1423}1424return0;1425}14261427inthold_locked_index(struct lock_file *lk,int die_on_error)1428{1429returnhold_lock_file_for_update(lk,get_index_file(),1430 die_on_error1431? LOCK_DIE_ON_ERROR1432:0);1433}14341435intread_index(struct index_state *istate)1436{1437returnread_index_from(istate,get_index_file());1438}14391440static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,1441unsigned int flags,1442const char*name,1443size_t len)1444{1445struct cache_entry *ce =xmalloc(cache_entry_size(len));14461447 ce->ce_stat_data.sd_ctime.sec =get_be32(&ondisk->ctime.sec);1448 ce->ce_stat_data.sd_mtime.sec =get_be32(&ondisk->mtime.sec);1449 ce->ce_stat_data.sd_ctime.nsec =get_be32(&ondisk->ctime.nsec);1450 ce->ce_stat_data.sd_mtime.nsec =get_be32(&ondisk->mtime.nsec);1451 ce->ce_stat_data.sd_dev =get_be32(&ondisk->dev);1452 ce->ce_stat_data.sd_ino =get_be32(&ondisk->ino);1453 ce->ce_mode =get_be32(&ondisk->mode);1454 ce->ce_stat_data.sd_uid =get_be32(&ondisk->uid);1455 ce->ce_stat_data.sd_gid =get_be32(&ondisk->gid);1456 ce->ce_stat_data.sd_size =get_be32(&ondisk->size);1457 ce->ce_flags = flags & ~CE_NAMEMASK;1458 ce->ce_namelen = len;1459 ce->index =0;1460hashcpy(ce->oid.hash, ondisk->sha1);1461memcpy(ce->name, name, len);1462 ce->name[len] ='\0';1463return ce;1464}14651466/*1467 * Adjacent cache entries tend to share the leading paths, so it makes1468 * sense to only store the differences in later entries. In the v41469 * on-disk format of the index, each on-disk cache entry stores the1470 * number of bytes to be stripped from the end of the previous name,1471 * and the bytes to append to the result, to come up with its name.1472 */1473static unsigned longexpand_name_field(struct strbuf *name,const char*cp_)1474{1475const unsigned char*ep, *cp = (const unsigned char*)cp_;1476size_t len =decode_varint(&cp);14771478if(name->len < len)1479die("malformed name field in the index");1480strbuf_remove(name, name->len - len, len);1481for(ep = cp; *ep; ep++)1482;/* find the end */1483strbuf_add(name, cp, ep - cp);1484return(const char*)ep +1- cp_;1485}14861487static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,1488unsigned long*ent_size,1489struct strbuf *previous_name)1490{1491struct cache_entry *ce;1492size_t len;1493const char*name;1494unsigned int flags;14951496/* On-disk flags are just 16 bits */1497 flags =get_be16(&ondisk->flags);1498 len = flags & CE_NAMEMASK;14991500if(flags & CE_EXTENDED) {1501struct ondisk_cache_entry_extended *ondisk2;1502int extended_flags;1503 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1504 extended_flags =get_be16(&ondisk2->flags2) <<16;1505/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1506if(extended_flags & ~CE_EXTENDED_FLAGS)1507die("Unknown index entry format%08x", extended_flags);1508 flags |= extended_flags;1509 name = ondisk2->name;1510}1511else1512 name = ondisk->name;15131514if(!previous_name) {1515/* v3 and earlier */1516if(len == CE_NAMEMASK)1517 len =strlen(name);1518 ce =cache_entry_from_ondisk(ondisk, flags, name, len);15191520*ent_size =ondisk_ce_size(ce);1521}else{1522unsigned long consumed;1523 consumed =expand_name_field(previous_name, name);1524 ce =cache_entry_from_ondisk(ondisk, flags,1525 previous_name->buf,1526 previous_name->len);15271528*ent_size = (name - ((char*)ondisk)) + consumed;1529}1530return ce;1531}15321533static voidcheck_ce_order(struct index_state *istate)1534{1535unsigned int i;15361537for(i =1; i < istate->cache_nr; i++) {1538struct cache_entry *ce = istate->cache[i -1];1539struct cache_entry *next_ce = istate->cache[i];1540int name_compare =strcmp(ce->name, next_ce->name);15411542if(0< name_compare)1543die("unordered stage entries in index");1544if(!name_compare) {1545if(!ce_stage(ce))1546die("multiple stage entries for merged file '%s'",1547 ce->name);1548if(ce_stage(ce) >ce_stage(next_ce))1549die("unordered stage entries for '%s'",1550 ce->name);1551}1552}1553}15541555static voidtweak_untracked_cache(struct index_state *istate)1556{1557switch(git_config_get_untracked_cache()) {1558case-1:/* keep: do nothing */1559break;1560case0:/* false */1561remove_untracked_cache(istate);1562break;1563case1:/* true */1564add_untracked_cache(istate);1565break;1566default:/* unknown value: do nothing */1567break;1568}1569}15701571static voidpost_read_index_from(struct index_state *istate)1572{1573check_ce_order(istate);1574tweak_untracked_cache(istate);1575}15761577/* remember to discard_cache() before reading a different cache! */1578intdo_read_index(struct index_state *istate,const char*path,int must_exist)1579{1580int fd, i;1581struct stat st;1582unsigned long src_offset;1583struct cache_header *hdr;1584void*mmap;1585size_t mmap_size;1586struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;15871588if(istate->initialized)1589return istate->cache_nr;15901591 istate->timestamp.sec =0;1592 istate->timestamp.nsec =0;1593 fd =open(path, O_RDONLY);1594if(fd <0) {1595if(!must_exist && errno == ENOENT)1596return0;1597die_errno("%s: index file open failed", path);1598}15991600if(fstat(fd, &st))1601die_errno("cannot stat the open index");16021603 mmap_size =xsize_t(st.st_size);1604if(mmap_size <sizeof(struct cache_header) +20)1605die("index file smaller than expected");16061607 mmap =xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd,0);1608if(mmap == MAP_FAILED)1609die_errno("unable to map index file");1610close(fd);16111612 hdr = mmap;1613if(verify_hdr(hdr, mmap_size) <0)1614goto unmap;16151616hashcpy(istate->sha1, (const unsigned char*)hdr + mmap_size -20);1617 istate->version =ntohl(hdr->hdr_version);1618 istate->cache_nr =ntohl(hdr->hdr_entries);1619 istate->cache_alloc =alloc_nr(istate->cache_nr);1620 istate->cache =xcalloc(istate->cache_alloc,sizeof(*istate->cache));1621 istate->initialized =1;16221623if(istate->version ==4)1624 previous_name = &previous_name_buf;1625else1626 previous_name = NULL;16271628 src_offset =sizeof(*hdr);1629for(i =0; i < istate->cache_nr; i++) {1630struct ondisk_cache_entry *disk_ce;1631struct cache_entry *ce;1632unsigned long consumed;16331634 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1635 ce =create_from_disk(disk_ce, &consumed, previous_name);1636set_index_entry(istate, i, ce);16371638 src_offset += consumed;1639}1640strbuf_release(&previous_name_buf);1641 istate->timestamp.sec = st.st_mtime;1642 istate->timestamp.nsec =ST_MTIME_NSEC(st);16431644while(src_offset <= mmap_size -20-8) {1645/* After an array of active_nr index entries,1646 * there can be arbitrary number of extended1647 * sections, each of which is prefixed with1648 * extension name (4-byte) and section length1649 * in 4-byte network byte order.1650 */1651uint32_t extsize;1652memcpy(&extsize, (char*)mmap + src_offset +4,4);1653 extsize =ntohl(extsize);1654if(read_index_extension(istate,1655(const char*) mmap + src_offset,1656(char*) mmap + src_offset +8,1657 extsize) <0)1658goto unmap;1659 src_offset +=8;1660 src_offset += extsize;1661}1662munmap(mmap, mmap_size);1663return istate->cache_nr;16641665unmap:1666munmap(mmap, mmap_size);1667die("index file corrupt");1668}16691670intread_index_from(struct index_state *istate,const char*path)1671{1672struct split_index *split_index;1673int ret;16741675/* istate->initialized covers both .git/index and .git/sharedindex.xxx */1676if(istate->initialized)1677return istate->cache_nr;16781679 ret =do_read_index(istate, path,0);16801681 split_index = istate->split_index;1682if(!split_index ||is_null_sha1(split_index->base_sha1)) {1683post_read_index_from(istate);1684return ret;1685}16861687if(split_index->base)1688discard_index(split_index->base);1689else1690 split_index->base =xcalloc(1,sizeof(*split_index->base));1691 ret =do_read_index(split_index->base,1692git_path("sharedindex.%s",1693sha1_to_hex(split_index->base_sha1)),1);1694if(hashcmp(split_index->base_sha1, split_index->base->sha1))1695die("broken index, expect%sin%s, got%s",1696sha1_to_hex(split_index->base_sha1),1697git_path("sharedindex.%s",1698sha1_to_hex(split_index->base_sha1)),1699sha1_to_hex(split_index->base->sha1));1700merge_base_index(istate);1701post_read_index_from(istate);1702return ret;1703}17041705intis_index_unborn(struct index_state *istate)1706{1707return(!istate->cache_nr && !istate->timestamp.sec);1708}17091710intdiscard_index(struct index_state *istate)1711{1712int i;17131714for(i =0; i < istate->cache_nr; i++) {1715if(istate->cache[i]->index &&1716 istate->split_index &&1717 istate->split_index->base &&1718 istate->cache[i]->index <= istate->split_index->base->cache_nr &&1719 istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index -1])1720continue;1721free(istate->cache[i]);1722}1723resolve_undo_clear_index(istate);1724 istate->cache_nr =0;1725 istate->cache_changed =0;1726 istate->timestamp.sec =0;1727 istate->timestamp.nsec =0;1728free_name_hash(istate);1729cache_tree_free(&(istate->cache_tree));1730 istate->initialized =0;1731free(istate->cache);1732 istate->cache = NULL;1733 istate->cache_alloc =0;1734discard_split_index(istate);1735free_untracked_cache(istate->untracked);1736 istate->untracked = NULL;1737return0;1738}17391740intunmerged_index(const struct index_state *istate)1741{1742int i;1743for(i =0; i < istate->cache_nr; i++) {1744if(ce_stage(istate->cache[i]))1745return1;1746}1747return0;1748}17491750#define WRITE_BUFFER_SIZE 81921751static unsigned char write_buffer[WRITE_BUFFER_SIZE];1752static unsigned long write_buffer_len;17531754static intce_write_flush(git_SHA_CTX *context,int fd)1755{1756unsigned int buffered = write_buffer_len;1757if(buffered) {1758git_SHA1_Update(context, write_buffer, buffered);1759if(write_in_full(fd, write_buffer, buffered) != buffered)1760return-1;1761 write_buffer_len =0;1762}1763return0;1764}17651766static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1767{1768while(len) {1769unsigned int buffered = write_buffer_len;1770unsigned int partial = WRITE_BUFFER_SIZE - buffered;1771if(partial > len)1772 partial = len;1773memcpy(write_buffer + buffered, data, partial);1774 buffered += partial;1775if(buffered == WRITE_BUFFER_SIZE) {1776 write_buffer_len = buffered;1777if(ce_write_flush(context, fd))1778return-1;1779 buffered =0;1780}1781 write_buffer_len = buffered;1782 len -= partial;1783 data = (char*) data + partial;1784}1785return0;1786}17871788static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1789unsigned int ext,unsigned int sz)1790{1791 ext =htonl(ext);1792 sz =htonl(sz);1793return((ce_write(context, fd, &ext,4) <0) ||1794(ce_write(context, fd, &sz,4) <0)) ? -1:0;1795}17961797static intce_flush(git_SHA_CTX *context,int fd,unsigned char*sha1)1798{1799unsigned int left = write_buffer_len;18001801if(left) {1802 write_buffer_len =0;1803git_SHA1_Update(context, write_buffer, left);1804}18051806/* Flush first if not enough space for SHA1 signature */1807if(left +20> WRITE_BUFFER_SIZE) {1808if(write_in_full(fd, write_buffer, left) != left)1809return-1;1810 left =0;1811}18121813/* Append the SHA1 signature at the end */1814git_SHA1_Final(write_buffer + left, context);1815hashcpy(sha1, write_buffer + left);1816 left +=20;1817return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1818}18191820static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1821{1822/*1823 * The only thing we care about in this function is to smudge the1824 * falsely clean entry due to touch-update-touch race, so we leave1825 * everything else as they are. We are called for entries whose1826 * ce_stat_data.sd_mtime match the index file mtime.1827 *1828 * Note that this actually does not do much for gitlinks, for1829 * which ce_match_stat_basic() always goes to the actual1830 * contents. The caller checks with is_racy_timestamp() which1831 * always says "no" for gitlinks, so we are not called for them ;-)1832 */1833struct stat st;18341835if(lstat(ce->name, &st) <0)1836return;1837if(ce_match_stat_basic(ce, &st))1838return;1839if(ce_modified_check_fs(ce, &st)) {1840/* This is "racily clean"; smudge it. Note that this1841 * is a tricky code. At first glance, it may appear1842 * that it can break with this sequence:1843 *1844 * $ echo xyzzy >frotz1845 * $ git-update-index --add frotz1846 * $ : >frotz1847 * $ sleep 31848 * $ echo filfre >nitfol1849 * $ git-update-index --add nitfol1850 *1851 * but it does not. When the second update-index runs,1852 * it notices that the entry "frotz" has the same timestamp1853 * as index, and if we were to smudge it by resetting its1854 * size to zero here, then the object name recorded1855 * in index is the 6-byte file but the cached stat information1856 * becomes zero --- which would then match what we would1857 * obtain from the filesystem next time we stat("frotz").1858 *1859 * However, the second update-index, before calling1860 * this function, notices that the cached size is 61861 * bytes and what is on the filesystem is an empty1862 * file, and never calls us, so the cached size information1863 * for "frotz" stays 6 which does not match the filesystem.1864 */1865 ce->ce_stat_data.sd_size =0;1866}1867}18681869/* Copy miscellaneous fields but not the name */1870static char*copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,1871struct cache_entry *ce)1872{1873short flags;18741875 ondisk->ctime.sec =htonl(ce->ce_stat_data.sd_ctime.sec);1876 ondisk->mtime.sec =htonl(ce->ce_stat_data.sd_mtime.sec);1877 ondisk->ctime.nsec =htonl(ce->ce_stat_data.sd_ctime.nsec);1878 ondisk->mtime.nsec =htonl(ce->ce_stat_data.sd_mtime.nsec);1879 ondisk->dev =htonl(ce->ce_stat_data.sd_dev);1880 ondisk->ino =htonl(ce->ce_stat_data.sd_ino);1881 ondisk->mode =htonl(ce->ce_mode);1882 ondisk->uid =htonl(ce->ce_stat_data.sd_uid);1883 ondisk->gid =htonl(ce->ce_stat_data.sd_gid);1884 ondisk->size =htonl(ce->ce_stat_data.sd_size);1885hashcpy(ondisk->sha1, ce->oid.hash);18861887 flags = ce->ce_flags & ~CE_NAMEMASK;1888 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK :ce_namelen(ce));1889 ondisk->flags =htons(flags);1890if(ce->ce_flags & CE_EXTENDED) {1891struct ondisk_cache_entry_extended *ondisk2;1892 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1893 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1894return ondisk2->name;1895}1896else{1897return ondisk->name;1898}1899}19001901static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce,1902struct strbuf *previous_name)1903{1904int size;1905struct ondisk_cache_entry *ondisk;1906int saved_namelen = saved_namelen;/* compiler workaround */1907char*name;1908int result;19091910if(ce->ce_flags & CE_STRIP_NAME) {1911 saved_namelen =ce_namelen(ce);1912 ce->ce_namelen =0;1913}19141915if(!previous_name) {1916 size =ondisk_ce_size(ce);1917 ondisk =xcalloc(1, size);1918 name =copy_cache_entry_to_ondisk(ondisk, ce);1919memcpy(name, ce->name,ce_namelen(ce));1920}else{1921int common, to_remove, prefix_size;1922unsigned char to_remove_vi[16];1923for(common =0;1924(ce->name[common] &&1925 common < previous_name->len &&1926 ce->name[common] == previous_name->buf[common]);1927 common++)1928;/* still matching */1929 to_remove = previous_name->len - common;1930 prefix_size =encode_varint(to_remove, to_remove_vi);19311932if(ce->ce_flags & CE_EXTENDED)1933 size =offsetof(struct ondisk_cache_entry_extended, name);1934else1935 size =offsetof(struct ondisk_cache_entry, name);1936 size += prefix_size + (ce_namelen(ce) - common +1);19371938 ondisk =xcalloc(1, size);1939 name =copy_cache_entry_to_ondisk(ondisk, ce);1940memcpy(name, to_remove_vi, prefix_size);1941memcpy(name + prefix_size, ce->name + common,ce_namelen(ce) - common);19421943strbuf_splice(previous_name, common, to_remove,1944 ce->name + common,ce_namelen(ce) - common);1945}1946if(ce->ce_flags & CE_STRIP_NAME) {1947 ce->ce_namelen = saved_namelen;1948 ce->ce_flags &= ~CE_STRIP_NAME;1949}19501951 result =ce_write(c, fd, ondisk, size);1952free(ondisk);1953return result;1954}19551956/*1957 * This function verifies if index_state has the correct sha1 of the1958 * index file. Don't die if we have any other failure, just return 0.1959 */1960static intverify_index_from(const struct index_state *istate,const char*path)1961{1962int fd;1963 ssize_t n;1964struct stat st;1965unsigned char sha1[20];19661967if(!istate->initialized)1968return0;19691970 fd =open(path, O_RDONLY);1971if(fd <0)1972return0;19731974if(fstat(fd, &st))1975goto out;19761977if(st.st_size <sizeof(struct cache_header) +20)1978goto out;19791980 n =pread_in_full(fd, sha1,20, st.st_size -20);1981if(n !=20)1982goto out;19831984if(hashcmp(istate->sha1, sha1))1985goto out;19861987close(fd);1988return1;19891990out:1991close(fd);1992return0;1993}19941995static intverify_index(const struct index_state *istate)1996{1997returnverify_index_from(istate,get_index_file());1998}19992000static inthas_racy_timestamp(struct index_state *istate)2001{2002int entries = istate->cache_nr;2003int i;20042005for(i =0; i < entries; i++) {2006struct cache_entry *ce = istate->cache[i];2007if(is_racy_timestamp(istate, ce))2008return1;2009}2010return0;2011}20122013/*2014 * Opportunistically update the index but do not complain if we can't2015 */2016voidupdate_index_if_able(struct index_state *istate,struct lock_file *lockfile)2017{2018if((istate->cache_changed ||has_racy_timestamp(istate)) &&2019verify_index(istate) &&2020write_locked_index(istate, lockfile, COMMIT_LOCK))2021rollback_lock_file(lockfile);2022}20232024static intdo_write_index(struct index_state *istate,int newfd,2025int strip_extensions)2026{2027 git_SHA_CTX c;2028struct cache_header hdr;2029int i, err, removed, extended, hdr_version;2030struct cache_entry **cache = istate->cache;2031int entries = istate->cache_nr;2032struct stat st;2033struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;20342035for(i = removed = extended =0; i < entries; i++) {2036if(cache[i]->ce_flags & CE_REMOVE)2037 removed++;20382039/* reduce extended entries if possible */2040 cache[i]->ce_flags &= ~CE_EXTENDED;2041if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {2042 extended++;2043 cache[i]->ce_flags |= CE_EXTENDED;2044}2045}20462047if(!istate->version) {2048 istate->version =get_index_format_default();2049if(getenv("GIT_TEST_SPLIT_INDEX"))2050init_split_index(istate);2051}20522053/* demote version 3 to version 2 when the latter suffices */2054if(istate->version ==3|| istate->version ==2)2055 istate->version = extended ?3:2;20562057 hdr_version = istate->version;20582059 hdr.hdr_signature =htonl(CACHE_SIGNATURE);2060 hdr.hdr_version =htonl(hdr_version);2061 hdr.hdr_entries =htonl(entries - removed);20622063git_SHA1_Init(&c);2064if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)2065return-1;20662067 previous_name = (hdr_version ==4) ? &previous_name_buf : NULL;2068for(i =0; i < entries; i++) {2069struct cache_entry *ce = cache[i];2070if(ce->ce_flags & CE_REMOVE)2071continue;2072if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))2073ce_smudge_racily_clean_entry(ce);2074if(is_null_oid(&ce->oid)) {2075static const char msg[] ="cache entry has null sha1:%s";2076static int allow = -1;20772078if(allow <0)2079 allow =git_env_bool("GIT_ALLOW_NULL_SHA1",0);2080if(allow)2081warning(msg, ce->name);2082else2083returnerror(msg, ce->name);2084}2085if(ce_write_entry(&c, newfd, ce, previous_name) <0)2086return-1;2087}2088strbuf_release(&previous_name_buf);20892090/* Write extension data here */2091if(!strip_extensions && istate->split_index) {2092struct strbuf sb = STRBUF_INIT;20932094 err =write_link_extension(&sb, istate) <0||2095write_index_ext_header(&c, newfd, CACHE_EXT_LINK,2096 sb.len) <0||2097ce_write(&c, newfd, sb.buf, sb.len) <0;2098strbuf_release(&sb);2099if(err)2100return-1;2101}2102if(!strip_extensions && istate->cache_tree) {2103struct strbuf sb = STRBUF_INIT;21042105cache_tree_write(&sb, istate->cache_tree);2106 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <02107||ce_write(&c, newfd, sb.buf, sb.len) <0;2108strbuf_release(&sb);2109if(err)2110return-1;2111}2112if(!strip_extensions && istate->resolve_undo) {2113struct strbuf sb = STRBUF_INIT;21142115resolve_undo_write(&sb, istate->resolve_undo);2116 err =write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,2117 sb.len) <02118||ce_write(&c, newfd, sb.buf, sb.len) <0;2119strbuf_release(&sb);2120if(err)2121return-1;2122}2123if(!strip_extensions && istate->untracked) {2124struct strbuf sb = STRBUF_INIT;21252126write_untracked_extension(&sb, istate->untracked);2127 err =write_index_ext_header(&c, newfd, CACHE_EXT_UNTRACKED,2128 sb.len) <0||2129ce_write(&c, newfd, sb.buf, sb.len) <0;2130strbuf_release(&sb);2131if(err)2132return-1;2133}21342135if(ce_flush(&c, newfd, istate->sha1) ||fstat(newfd, &st))2136return-1;2137 istate->timestamp.sec = (unsigned int)st.st_mtime;2138 istate->timestamp.nsec =ST_MTIME_NSEC(st);2139return0;2140}21412142voidset_alternate_index_output(const char*name)2143{2144 alternate_index_output = name;2145}21462147static intcommit_locked_index(struct lock_file *lk)2148{2149if(alternate_index_output)2150returncommit_lock_file_to(lk, alternate_index_output);2151else2152returncommit_lock_file(lk);2153}21542155static intdo_write_locked_index(struct index_state *istate,struct lock_file *lock,2156unsigned flags)2157{2158int ret =do_write_index(istate,get_lock_file_fd(lock),0);2159if(ret)2160return ret;2161assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=2162(COMMIT_LOCK | CLOSE_LOCK));2163if(flags & COMMIT_LOCK)2164returncommit_locked_index(lock);2165else if(flags & CLOSE_LOCK)2166returnclose_lock_file(lock);2167else2168return ret;2169}21702171static intwrite_split_index(struct index_state *istate,2172struct lock_file *lock,2173unsigned flags)2174{2175int ret;2176prepare_to_write_split_index(istate);2177 ret =do_write_locked_index(istate, lock, flags);2178finish_writing_split_index(istate);2179return ret;2180}21812182static struct tempfile temporary_sharedindex;21832184static intwrite_shared_index(struct index_state *istate,2185struct lock_file *lock,unsigned flags)2186{2187struct split_index *si = istate->split_index;2188int fd, ret;21892190 fd =mks_tempfile(&temporary_sharedindex,git_path("sharedindex_XXXXXX"));2191if(fd <0) {2192hashclr(si->base_sha1);2193returndo_write_locked_index(istate, lock, flags);2194}2195move_cache_to_base_index(istate);2196 ret =do_write_index(si->base, fd,1);2197if(ret) {2198delete_tempfile(&temporary_sharedindex);2199return ret;2200}2201 ret =rename_tempfile(&temporary_sharedindex,2202git_path("sharedindex.%s",sha1_to_hex(si->base->sha1)));2203if(!ret)2204hashcpy(si->base_sha1, si->base->sha1);2205return ret;2206}22072208intwrite_locked_index(struct index_state *istate,struct lock_file *lock,2209unsigned flags)2210{2211struct split_index *si = istate->split_index;22122213if(!si || alternate_index_output ||2214(istate->cache_changed & ~EXTMASK)) {2215if(si)2216hashclr(si->base_sha1);2217returndo_write_locked_index(istate, lock, flags);2218}22192220if(getenv("GIT_TEST_SPLIT_INDEX")) {2221int v = si->base_sha1[0];2222if((v &15) <6)2223 istate->cache_changed |= SPLIT_INDEX_ORDERED;2224}2225if(istate->cache_changed & SPLIT_INDEX_ORDERED) {2226int ret =write_shared_index(istate, lock, flags);2227if(ret)2228return ret;2229}22302231returnwrite_split_index(istate, lock, flags);2232}22332234/*2235 * Read the index file that is potentially unmerged into given2236 * index_state, dropping any unmerged entries. Returns true if2237 * the index is unmerged. Callers who want to refuse to work2238 * from an unmerged state can call this and check its return value,2239 * instead of calling read_cache().2240 */2241intread_index_unmerged(struct index_state *istate)2242{2243int i;2244int unmerged =0;22452246read_index(istate);2247for(i =0; i < istate->cache_nr; i++) {2248struct cache_entry *ce = istate->cache[i];2249struct cache_entry *new_ce;2250int size, len;22512252if(!ce_stage(ce))2253continue;2254 unmerged =1;2255 len =ce_namelen(ce);2256 size =cache_entry_size(len);2257 new_ce =xcalloc(1, size);2258memcpy(new_ce->name, ce->name, len);2259 new_ce->ce_flags =create_ce_flags(0) | CE_CONFLICTED;2260 new_ce->ce_namelen = len;2261 new_ce->ce_mode = ce->ce_mode;2262if(add_index_entry(istate, new_ce,0))2263returnerror("%s: cannot drop to stage #0",2264 new_ce->name);2265}2266return unmerged;2267}22682269/*2270 * Returns 1 if the path is an "other" path with respect to2271 * the index; that is, the path is not mentioned in the index at all,2272 * either as a file, a directory with some files in the index,2273 * or as an unmerged entry.2274 *2275 * We helpfully remove a trailing "/" from directories so that2276 * the output of read_directory can be used as-is.2277 */2278intindex_name_is_other(const struct index_state *istate,const char*name,2279int namelen)2280{2281int pos;2282if(namelen && name[namelen -1] =='/')2283 namelen--;2284 pos =index_name_pos(istate, name, namelen);2285if(0<= pos)2286return0;/* exact match */2287 pos = -pos -1;2288if(pos < istate->cache_nr) {2289struct cache_entry *ce = istate->cache[pos];2290if(ce_namelen(ce) == namelen &&2291!memcmp(ce->name, name, namelen))2292return0;/* Yup, this one exists unmerged */2293}2294return1;2295}22962297void*read_blob_data_from_index(struct index_state *istate,const char*path,unsigned long*size)2298{2299int pos, len;2300unsigned long sz;2301enum object_type type;2302void*data;23032304 len =strlen(path);2305 pos =index_name_pos(istate, path, len);2306if(pos <0) {2307/*2308 * We might be in the middle of a merge, in which2309 * case we would read stage #2 (ours).2310 */2311int i;2312for(i = -pos -1;2313(pos <0&& i < istate->cache_nr &&2314!strcmp(istate->cache[i]->name, path));2315 i++)2316if(ce_stage(istate->cache[i]) ==2)2317 pos = i;2318}2319if(pos <0)2320return NULL;2321 data =read_sha1_file(istate->cache[pos]->oid.hash, &type, &sz);2322if(!data || type != OBJ_BLOB) {2323free(data);2324return NULL;2325}2326if(size)2327*size = sz;2328return data;2329}23302331voidstat_validity_clear(struct stat_validity *sv)2332{2333free(sv->sd);2334 sv->sd = NULL;2335}23362337intstat_validity_check(struct stat_validity *sv,const char*path)2338{2339struct stat st;23402341if(stat(path, &st) <0)2342return sv->sd == NULL;2343if(!sv->sd)2344return0;2345returnS_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);2346}23472348voidstat_validity_update(struct stat_validity *sv,int fd)2349{2350struct stat st;23512352if(fstat(fd, &st) <0|| !S_ISREG(st.st_mode))2353stat_validity_clear(sv);2354else{2355if(!sv->sd)2356 sv->sd =xcalloc(1,sizeof(struct stat_data));2357fill_stat_data(sv->sd, &st);2358}2359}