2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
14 #include "cache-tree.h"
17 #include "object-store.h"
21 #include "environment.h"
23 #include "resolve-undo.h"
24 #include "run-command.h"
27 #include "split-index.h"
29 #include "fsmonitor.h"
30 #include "thread-utils.h"
32 #include "sparse-index.h"
33 #include "csum-file.h"
34 #include "promisor-remote.h"
38 /* Mask for the name length in ce_flags in the on-disk index */
40 #define CE_NAMEMASK (0x0fff)
44 * The first letter should be 'A'..'Z' for extensions that are not
45 * necessary for a correct operation (i.e. optimization data).
46 * When new extensions are added that _needs_ to be understood in
47 * order to correctly interpret the index file, pick character that
48 * is outside the range, to cause the reader to abort.
51 #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
52 #define CACHE_EXT_TREE 0x54524545 /* "TREE" */
53 #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
54 #define CACHE_EXT_LINK 0x6c696e6b /* "link" */
55 #define CACHE_EXT_UNTRACKED 0x554E5452 /* "UNTR" */
56 #define CACHE_EXT_FSMONITOR 0x46534D4E /* "FSMN" */
57 #define CACHE_EXT_ENDOFINDEXENTRIES 0x454F4945 /* "EOIE" */
58 #define CACHE_EXT_INDEXENTRYOFFSETTABLE 0x49454F54 /* "IEOT" */
59 #define CACHE_EXT_SPARSE_DIRECTORIES 0x73646972 /* "sdir" */
61 /* changes that can be kept in $GIT_DIR/index (basically all extensions) */
62 #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
63 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \
64 SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED | FSMONITOR_CHANGED)
68 * This is an estimate of the pathname length in the index. We use
69 * this for V4 index files to guess the un-deltafied size of the index
70 * in memory because of pathname deltafication. This is not required
71 * for V2/V3 index formats because their pathnames are not compressed.
72 * If the initial amount of memory set aside is not sufficient, the
73 * mem pool will allocate extra memory.
75 #define CACHE_ENTRY_PATH_LENGTH 80
77 enum index_search_mode
{
82 static inline struct cache_entry
*mem_pool__ce_alloc(struct mem_pool
*mem_pool
, size_t len
)
84 struct cache_entry
*ce
;
85 ce
= mem_pool_alloc(mem_pool
, cache_entry_size(len
));
86 ce
->mem_pool_allocated
= 1;
90 static inline struct cache_entry
*mem_pool__ce_calloc(struct mem_pool
*mem_pool
, size_t len
)
92 struct cache_entry
* ce
;
93 ce
= mem_pool_calloc(mem_pool
, 1, cache_entry_size(len
));
94 ce
->mem_pool_allocated
= 1;
98 static struct mem_pool
*find_mem_pool(struct index_state
*istate
)
100 struct mem_pool
**pool_ptr
;
102 if (istate
->split_index
&& istate
->split_index
->base
)
103 pool_ptr
= &istate
->split_index
->base
->ce_mem_pool
;
105 pool_ptr
= &istate
->ce_mem_pool
;
108 *pool_ptr
= xmalloc(sizeof(**pool_ptr
));
109 mem_pool_init(*pool_ptr
, 0);
115 static const char *alternate_index_output
;
117 static void set_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
119 if (S_ISSPARSEDIR(ce
->ce_mode
))
120 istate
->sparse_index
= INDEX_COLLAPSED
;
122 istate
->cache
[nr
] = ce
;
123 add_name_hash(istate
, ce
);
126 static void replace_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
128 struct cache_entry
*old
= istate
->cache
[nr
];
130 replace_index_entry_in_base(istate
, old
, ce
);
131 remove_name_hash(istate
, old
);
132 discard_cache_entry(old
);
133 ce
->ce_flags
&= ~CE_HASHED
;
134 set_index_entry(istate
, nr
, ce
);
135 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
136 mark_fsmonitor_invalid(istate
, ce
);
137 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
140 void rename_index_entry_at(struct index_state
*istate
, int nr
, const char *new_name
)
142 struct cache_entry
*old_entry
= istate
->cache
[nr
], *new_entry
, *refreshed
;
143 int namelen
= strlen(new_name
);
145 new_entry
= make_empty_cache_entry(istate
, namelen
);
146 copy_cache_entry(new_entry
, old_entry
);
147 new_entry
->ce_flags
&= ~CE_HASHED
;
148 new_entry
->ce_namelen
= namelen
;
149 new_entry
->index
= 0;
150 memcpy(new_entry
->name
, new_name
, namelen
+ 1);
152 cache_tree_invalidate_path(istate
, old_entry
->name
);
153 untracked_cache_remove_from_index(istate
, old_entry
->name
);
154 remove_index_entry_at(istate
, nr
);
157 * Refresh the new index entry. Using 'refresh_cache_entry' ensures
158 * we only update stat info if the entry is otherwise up-to-date (i.e.,
159 * the contents/mode haven't changed). This ensures that we reflect the
160 * 'ctime' of the rename in the index without (incorrectly) updating
161 * the cached stat info to reflect unstaged changes on disk.
163 refreshed
= refresh_cache_entry(istate
, new_entry
, CE_MATCH_REFRESH
);
164 if (refreshed
&& refreshed
!= new_entry
) {
165 add_index_entry(istate
, refreshed
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
166 discard_cache_entry(new_entry
);
168 add_index_entry(istate
, new_entry
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
171 void fill_stat_data(struct stat_data
*sd
, struct stat
*st
)
173 sd
->sd_ctime
.sec
= (unsigned int)st
->st_ctime
;
174 sd
->sd_mtime
.sec
= (unsigned int)st
->st_mtime
;
175 sd
->sd_ctime
.nsec
= ST_CTIME_NSEC(*st
);
176 sd
->sd_mtime
.nsec
= ST_MTIME_NSEC(*st
);
177 sd
->sd_dev
= st
->st_dev
;
178 sd
->sd_ino
= st
->st_ino
;
179 sd
->sd_uid
= st
->st_uid
;
180 sd
->sd_gid
= st
->st_gid
;
181 sd
->sd_size
= st
->st_size
;
184 int match_stat_data(const struct stat_data
*sd
, struct stat
*st
)
188 if (sd
->sd_mtime
.sec
!= (unsigned int)st
->st_mtime
)
189 changed
|= MTIME_CHANGED
;
190 if (trust_ctime
&& check_stat
&&
191 sd
->sd_ctime
.sec
!= (unsigned int)st
->st_ctime
)
192 changed
|= CTIME_CHANGED
;
195 if (check_stat
&& sd
->sd_mtime
.nsec
!= ST_MTIME_NSEC(*st
))
196 changed
|= MTIME_CHANGED
;
197 if (trust_ctime
&& check_stat
&&
198 sd
->sd_ctime
.nsec
!= ST_CTIME_NSEC(*st
))
199 changed
|= CTIME_CHANGED
;
203 if (sd
->sd_uid
!= (unsigned int) st
->st_uid
||
204 sd
->sd_gid
!= (unsigned int) st
->st_gid
)
205 changed
|= OWNER_CHANGED
;
206 if (sd
->sd_ino
!= (unsigned int) st
->st_ino
)
207 changed
|= INODE_CHANGED
;
212 * st_dev breaks on network filesystems where different
213 * clients will have different views of what "device"
214 * the filesystem is on
216 if (check_stat
&& sd
->sd_dev
!= (unsigned int) st
->st_dev
)
217 changed
|= INODE_CHANGED
;
220 if (sd
->sd_size
!= (unsigned int) st
->st_size
)
221 changed
|= DATA_CHANGED
;
227 * This only updates the "non-critical" parts of the directory
228 * cache, ie the parts that aren't tracked by GIT, and only used
229 * to validate the cache.
231 void fill_stat_cache_info(struct index_state
*istate
, struct cache_entry
*ce
, struct stat
*st
)
233 fill_stat_data(&ce
->ce_stat_data
, st
);
235 if (assume_unchanged
)
236 ce
->ce_flags
|= CE_VALID
;
238 if (S_ISREG(st
->st_mode
)) {
239 ce_mark_uptodate(ce
);
240 mark_fsmonitor_valid(istate
, ce
);
244 static int ce_compare_data(struct index_state
*istate
,
245 const struct cache_entry
*ce
,
249 int fd
= git_open_cloexec(ce
->name
, O_RDONLY
);
252 struct object_id oid
;
253 if (!index_fd(istate
, &oid
, fd
, st
, OBJ_BLOB
, ce
->name
, 0))
254 match
= !oideq(&oid
, &ce
->oid
);
255 /* index_fd() closed the file descriptor already */
260 static int ce_compare_link(const struct cache_entry
*ce
, size_t expected_size
)
265 enum object_type type
;
266 struct strbuf sb
= STRBUF_INIT
;
268 if (strbuf_readlink(&sb
, ce
->name
, expected_size
))
271 buffer
= repo_read_object_file(the_repository
, &ce
->oid
, &type
, &size
);
274 match
= memcmp(buffer
, sb
.buf
, size
);
281 static int ce_compare_gitlink(const struct cache_entry
*ce
)
283 struct object_id oid
;
286 * We don't actually require that the .git directory
287 * under GITLINK directory be a valid git directory. It
288 * might even be missing (in case nobody populated that
291 * If so, we consider it always to match.
293 if (resolve_gitlink_ref(ce
->name
, "HEAD", &oid
) < 0)
295 return !oideq(&oid
, &ce
->oid
);
298 static int ce_modified_check_fs(struct index_state
*istate
,
299 const struct cache_entry
*ce
,
302 switch (st
->st_mode
& S_IFMT
) {
304 if (ce_compare_data(istate
, ce
, st
))
308 if (ce_compare_link(ce
, xsize_t(st
->st_size
)))
312 if (S_ISGITLINK(ce
->ce_mode
))
313 return ce_compare_gitlink(ce
) ? DATA_CHANGED
: 0;
314 /* else fallthrough */
321 static int ce_match_stat_basic(const struct cache_entry
*ce
, struct stat
*st
)
323 unsigned int changed
= 0;
325 if (ce
->ce_flags
& CE_REMOVE
)
326 return MODE_CHANGED
| DATA_CHANGED
| TYPE_CHANGED
;
328 switch (ce
->ce_mode
& S_IFMT
) {
330 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
331 /* We consider only the owner x bit to be relevant for
334 if (trust_executable_bit
&&
335 (0100 & (ce
->ce_mode
^ st
->st_mode
)))
336 changed
|= MODE_CHANGED
;
339 if (!S_ISLNK(st
->st_mode
) &&
340 (has_symlinks
|| !S_ISREG(st
->st_mode
)))
341 changed
|= TYPE_CHANGED
;
344 /* We ignore most of the st_xxx fields for gitlinks */
345 if (!S_ISDIR(st
->st_mode
))
346 changed
|= TYPE_CHANGED
;
347 else if (ce_compare_gitlink(ce
))
348 changed
|= DATA_CHANGED
;
351 BUG("unsupported ce_mode: %o", ce
->ce_mode
);
354 changed
|= match_stat_data(&ce
->ce_stat_data
, st
);
356 /* Racily smudged entry? */
357 if (!ce
->ce_stat_data
.sd_size
) {
358 if (!is_empty_blob_sha1(ce
->oid
.hash
))
359 changed
|= DATA_CHANGED
;
365 static int is_racy_stat(const struct index_state
*istate
,
366 const struct stat_data
*sd
)
368 return (istate
->timestamp
.sec
&&
370 /* nanosecond timestamped files can also be racy! */
371 (istate
->timestamp
.sec
< sd
->sd_mtime
.sec
||
372 (istate
->timestamp
.sec
== sd
->sd_mtime
.sec
&&
373 istate
->timestamp
.nsec
<= sd
->sd_mtime
.nsec
))
375 istate
->timestamp
.sec
<= sd
->sd_mtime
.sec
380 int is_racy_timestamp(const struct index_state
*istate
,
381 const struct cache_entry
*ce
)
383 return (!S_ISGITLINK(ce
->ce_mode
) &&
384 is_racy_stat(istate
, &ce
->ce_stat_data
));
387 int match_stat_data_racy(const struct index_state
*istate
,
388 const struct stat_data
*sd
, struct stat
*st
)
390 if (is_racy_stat(istate
, sd
))
391 return MTIME_CHANGED
;
392 return match_stat_data(sd
, st
);
395 int ie_match_stat(struct index_state
*istate
,
396 const struct cache_entry
*ce
, struct stat
*st
,
397 unsigned int options
)
399 unsigned int changed
;
400 int ignore_valid
= options
& CE_MATCH_IGNORE_VALID
;
401 int ignore_skip_worktree
= options
& CE_MATCH_IGNORE_SKIP_WORKTREE
;
402 int assume_racy_is_modified
= options
& CE_MATCH_RACY_IS_DIRTY
;
403 int ignore_fsmonitor
= options
& CE_MATCH_IGNORE_FSMONITOR
;
405 if (!ignore_fsmonitor
)
406 refresh_fsmonitor(istate
);
408 * If it's marked as always valid in the index, it's
409 * valid whatever the checked-out copy says.
411 * skip-worktree has the same effect with higher precedence
413 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
415 if (!ignore_valid
&& (ce
->ce_flags
& CE_VALID
))
417 if (!ignore_fsmonitor
&& (ce
->ce_flags
& CE_FSMONITOR_VALID
))
421 * Intent-to-add entries have not been added, so the index entry
422 * by definition never matches what is in the work tree until it
423 * actually gets added.
425 if (ce_intent_to_add(ce
))
426 return DATA_CHANGED
| TYPE_CHANGED
| MODE_CHANGED
;
428 changed
= ce_match_stat_basic(ce
, st
);
431 * Within 1 second of this sequence:
432 * echo xyzzy >file && git-update-index --add file
433 * running this command:
435 * would give a falsely clean cache entry. The mtime and
436 * length match the cache, and other stat fields do not change.
438 * We could detect this at update-index time (the cache entry
439 * being registered/updated records the same time as "now")
440 * and delay the return from git-update-index, but that would
441 * effectively mean we can make at most one commit per second,
442 * which is not acceptable. Instead, we check cache entries
443 * whose mtime are the same as the index file timestamp more
444 * carefully than others.
446 if (!changed
&& is_racy_timestamp(istate
, ce
)) {
447 if (assume_racy_is_modified
)
448 changed
|= DATA_CHANGED
;
450 changed
|= ce_modified_check_fs(istate
, ce
, st
);
456 int ie_modified(struct index_state
*istate
,
457 const struct cache_entry
*ce
,
458 struct stat
*st
, unsigned int options
)
460 int changed
, changed_fs
;
462 changed
= ie_match_stat(istate
, ce
, st
, options
);
466 * If the mode or type has changed, there's no point in trying
467 * to refresh the entry - it's not going to match
469 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
473 * Immediately after read-tree or update-index --cacheinfo,
474 * the length field is zero, as we have never even read the
475 * lstat(2) information once, and we cannot trust DATA_CHANGED
476 * returned by ie_match_stat() which in turn was returned by
477 * ce_match_stat_basic() to signal that the filesize of the
478 * blob changed. We have to actually go to the filesystem to
479 * see if the contents match, and if so, should answer "unchanged".
481 * The logic does not apply to gitlinks, as ce_match_stat_basic()
482 * already has checked the actual HEAD from the filesystem in the
483 * subproject. If ie_match_stat() already said it is different,
484 * then we know it is.
486 if ((changed
& DATA_CHANGED
) &&
487 (S_ISGITLINK(ce
->ce_mode
) || ce
->ce_stat_data
.sd_size
!= 0))
490 changed_fs
= ce_modified_check_fs(istate
, ce
, st
);
492 return changed
| changed_fs
;
496 int base_name_compare(const char *name1
, size_t len1
, int mode1
,
497 const char *name2
, size_t len2
, int mode2
)
499 unsigned char c1
, c2
;
500 size_t len
= len1
< len2
? len1
: len2
;
503 cmp
= memcmp(name1
, name2
, len
);
508 if (!c1
&& S_ISDIR(mode1
))
510 if (!c2
&& S_ISDIR(mode2
))
512 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
516 * df_name_compare() is identical to base_name_compare(), except it
517 * compares conflicting directory/file entries as equal. Note that
518 * while a directory name compares as equal to a regular file, they
519 * then individually compare _differently_ to a filename that has
520 * a dot after the basename (because '\0' < '.' < '/').
522 * This is used by routines that want to traverse the git namespace
523 * but then handle conflicting entries together when possible.
525 int df_name_compare(const char *name1
, size_t len1
, int mode1
,
526 const char *name2
, size_t len2
, int mode2
)
528 unsigned char c1
, c2
;
529 size_t len
= len1
< len2
? len1
: len2
;
532 cmp
= memcmp(name1
, name2
, len
);
535 /* Directories and files compare equal (same length, same name) */
539 if (!c1
&& S_ISDIR(mode1
))
542 if (!c2
&& S_ISDIR(mode2
))
544 if (c1
== '/' && !c2
)
546 if (c2
== '/' && !c1
)
551 int name_compare(const char *name1
, size_t len1
, const char *name2
, size_t len2
)
553 size_t min_len
= (len1
< len2
) ? len1
: len2
;
554 int cmp
= memcmp(name1
, name2
, min_len
);
564 int cache_name_stage_compare(const char *name1
, int len1
, int stage1
, const char *name2
, int len2
, int stage2
)
568 cmp
= name_compare(name1
, len1
, name2
, len2
);
579 static int index_name_stage_pos(struct index_state
*istate
,
580 const char *name
, int namelen
,
582 enum index_search_mode search_mode
)
587 last
= istate
->cache_nr
;
588 while (last
> first
) {
589 int next
= first
+ ((last
- first
) >> 1);
590 struct cache_entry
*ce
= istate
->cache
[next
];
591 int cmp
= cache_name_stage_compare(name
, namelen
, stage
, ce
->name
, ce_namelen(ce
), ce_stage(ce
));
601 if (search_mode
== EXPAND_SPARSE
&& istate
->sparse_index
&&
603 /* Note: first <= istate->cache_nr */
604 struct cache_entry
*ce
= istate
->cache
[first
- 1];
607 * If we are in a sparse-index _and_ the entry before the
608 * insertion position is a sparse-directory entry that is
609 * an ancestor of 'name', then we need to expand the index
610 * and search again. This will only trigger once, because
611 * thereafter the index is fully expanded.
613 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
614 ce_namelen(ce
) < namelen
&&
615 !strncmp(name
, ce
->name
, ce_namelen(ce
))) {
616 ensure_full_index(istate
);
617 return index_name_stage_pos(istate
, name
, namelen
, stage
, search_mode
);
624 int index_name_pos(struct index_state
*istate
, const char *name
, int namelen
)
626 return index_name_stage_pos(istate
, name
, namelen
, 0, EXPAND_SPARSE
);
629 int index_name_pos_sparse(struct index_state
*istate
, const char *name
, int namelen
)
631 return index_name_stage_pos(istate
, name
, namelen
, 0, NO_EXPAND_SPARSE
);
634 int index_entry_exists(struct index_state
*istate
, const char *name
, int namelen
)
636 return index_name_stage_pos(istate
, name
, namelen
, 0, NO_EXPAND_SPARSE
) >= 0;
639 int remove_index_entry_at(struct index_state
*istate
, int pos
)
641 struct cache_entry
*ce
= istate
->cache
[pos
];
643 record_resolve_undo(istate
, ce
);
644 remove_name_hash(istate
, ce
);
645 save_or_free_index_entry(istate
, ce
);
646 istate
->cache_changed
|= CE_ENTRY_REMOVED
;
648 if (pos
>= istate
->cache_nr
)
650 MOVE_ARRAY(istate
->cache
+ pos
, istate
->cache
+ pos
+ 1,
651 istate
->cache_nr
- pos
);
656 * Remove all cache entries marked for removal, that is where
657 * CE_REMOVE is set in ce_flags. This is much more effective than
658 * calling remove_index_entry_at() for each entry to be removed.
660 void remove_marked_cache_entries(struct index_state
*istate
, int invalidate
)
662 struct cache_entry
**ce_array
= istate
->cache
;
665 for (i
= j
= 0; i
< istate
->cache_nr
; i
++) {
666 if (ce_array
[i
]->ce_flags
& CE_REMOVE
) {
668 cache_tree_invalidate_path(istate
,
670 untracked_cache_remove_from_index(istate
,
673 remove_name_hash(istate
, ce_array
[i
]);
674 save_or_free_index_entry(istate
, ce_array
[i
]);
677 ce_array
[j
++] = ce_array
[i
];
679 if (j
== istate
->cache_nr
)
681 istate
->cache_changed
|= CE_ENTRY_REMOVED
;
682 istate
->cache_nr
= j
;
685 int remove_file_from_index(struct index_state
*istate
, const char *path
)
687 int pos
= index_name_pos(istate
, path
, strlen(path
));
690 cache_tree_invalidate_path(istate
, path
);
691 untracked_cache_remove_from_index(istate
, path
);
692 while (pos
< istate
->cache_nr
&& !strcmp(istate
->cache
[pos
]->name
, path
))
693 remove_index_entry_at(istate
, pos
);
697 static int compare_name(struct cache_entry
*ce
, const char *path
, int namelen
)
699 return namelen
!= ce_namelen(ce
) || memcmp(path
, ce
->name
, namelen
);
702 static int index_name_pos_also_unmerged(struct index_state
*istate
,
703 const char *path
, int namelen
)
705 int pos
= index_name_pos(istate
, path
, namelen
);
706 struct cache_entry
*ce
;
711 /* maybe unmerged? */
713 if (pos
>= istate
->cache_nr
||
714 compare_name((ce
= istate
->cache
[pos
]), path
, namelen
))
717 /* order of preference: stage 2, 1, 3 */
718 if (ce_stage(ce
) == 1 && pos
+ 1 < istate
->cache_nr
&&
719 ce_stage((ce
= istate
->cache
[pos
+ 1])) == 2 &&
720 !compare_name(ce
, path
, namelen
))
725 static int different_name(struct cache_entry
*ce
, struct cache_entry
*alias
)
727 int len
= ce_namelen(ce
);
728 return ce_namelen(alias
) != len
|| memcmp(ce
->name
, alias
->name
, len
);
732 * If we add a filename that aliases in the cache, we will use the
733 * name that we already have - but we don't want to update the same
734 * alias twice, because that implies that there were actually two
735 * different files with aliasing names!
737 * So we use the CE_ADDED flag to verify that the alias was an old
738 * one before we accept it as
740 static struct cache_entry
*create_alias_ce(struct index_state
*istate
,
741 struct cache_entry
*ce
,
742 struct cache_entry
*alias
)
745 struct cache_entry
*new_entry
;
747 if (alias
->ce_flags
& CE_ADDED
)
748 die(_("will not add file alias '%s' ('%s' already exists in index)"),
749 ce
->name
, alias
->name
);
751 /* Ok, create the new entry using the name of the existing alias */
752 len
= ce_namelen(alias
);
753 new_entry
= make_empty_cache_entry(istate
, len
);
754 memcpy(new_entry
->name
, alias
->name
, len
);
755 copy_cache_entry(new_entry
, ce
);
756 save_or_free_index_entry(istate
, ce
);
760 void set_object_name_for_intent_to_add_entry(struct cache_entry
*ce
)
762 struct object_id oid
;
763 if (write_object_file("", 0, OBJ_BLOB
, &oid
))
764 die(_("cannot create an empty blob in the object database"));
765 oidcpy(&ce
->oid
, &oid
);
768 int add_to_index(struct index_state
*istate
, const char *path
, struct stat
*st
, int flags
)
770 int namelen
, was_same
;
771 mode_t st_mode
= st
->st_mode
;
772 struct cache_entry
*ce
, *alias
= NULL
;
773 unsigned ce_option
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
|CE_MATCH_RACY_IS_DIRTY
;
774 int verbose
= flags
& (ADD_CACHE_VERBOSE
| ADD_CACHE_PRETEND
);
775 int pretend
= flags
& ADD_CACHE_PRETEND
;
776 int intent_only
= flags
& ADD_CACHE_INTENT
;
777 int add_option
= (ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
|
778 (intent_only
? ADD_CACHE_NEW_ONLY
: 0));
779 unsigned hash_flags
= pretend
? 0 : HASH_WRITE_OBJECT
;
780 struct object_id oid
;
782 if (flags
& ADD_CACHE_RENORMALIZE
)
783 hash_flags
|= HASH_RENORMALIZE
;
785 if (!S_ISREG(st_mode
) && !S_ISLNK(st_mode
) && !S_ISDIR(st_mode
))
786 return error(_("%s: can only add regular files, symbolic links or git-directories"), path
);
788 namelen
= strlen(path
);
789 if (S_ISDIR(st_mode
)) {
790 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
791 return error(_("'%s' does not have a commit checked out"), path
);
792 while (namelen
&& path
[namelen
-1] == '/')
795 ce
= make_empty_cache_entry(istate
, namelen
);
796 memcpy(ce
->name
, path
, namelen
);
797 ce
->ce_namelen
= namelen
;
799 fill_stat_cache_info(istate
, ce
, st
);
801 ce
->ce_flags
|= CE_INTENT_TO_ADD
;
804 if (trust_executable_bit
&& has_symlinks
) {
805 ce
->ce_mode
= create_ce_mode(st_mode
);
807 /* If there is an existing entry, pick the mode bits and type
808 * from it, otherwise assume unexecutable regular file.
810 struct cache_entry
*ent
;
811 int pos
= index_name_pos_also_unmerged(istate
, path
, namelen
);
813 ent
= (0 <= pos
) ? istate
->cache
[pos
] : NULL
;
814 ce
->ce_mode
= ce_mode_from_stat(ent
, st_mode
);
817 /* When core.ignorecase=true, determine if a directory of the same name but differing
818 * case already exists within the Git repository. If it does, ensure the directory
819 * case of the file being added to the repository matches (is folded into) the existing
820 * entry's directory case.
823 adjust_dirname_case(istate
, ce
->name
);
825 if (!(flags
& ADD_CACHE_RENORMALIZE
)) {
826 alias
= index_file_exists(istate
, ce
->name
,
827 ce_namelen(ce
), ignore_case
);
830 !ie_match_stat(istate
, alias
, st
, ce_option
)) {
831 /* Nothing changed, really */
832 if (!S_ISGITLINK(alias
->ce_mode
))
833 ce_mark_uptodate(alias
);
834 alias
->ce_flags
|= CE_ADDED
;
836 discard_cache_entry(ce
);
841 if (index_path(istate
, &ce
->oid
, path
, st
, hash_flags
)) {
842 discard_cache_entry(ce
);
843 return error(_("unable to index file '%s'"), path
);
846 set_object_name_for_intent_to_add_entry(ce
);
848 if (ignore_case
&& alias
&& different_name(ce
, alias
))
849 ce
= create_alias_ce(istate
, ce
, alias
);
850 ce
->ce_flags
|= CE_ADDED
;
852 /* It was suspected to be racily clean, but it turns out to be Ok */
855 oideq(&alias
->oid
, &ce
->oid
) &&
856 ce
->ce_mode
== alias
->ce_mode
);
859 discard_cache_entry(ce
);
860 else if (add_index_entry(istate
, ce
, add_option
)) {
861 discard_cache_entry(ce
);
862 return error(_("unable to add '%s' to index"), path
);
864 if (verbose
&& !was_same
)
865 printf("add '%s'\n", path
);
869 int add_file_to_index(struct index_state
*istate
, const char *path
, int flags
)
872 if (lstat(path
, &st
))
873 die_errno(_("unable to stat '%s'"), path
);
874 return add_to_index(istate
, path
, &st
, flags
);
877 struct cache_entry
*make_empty_cache_entry(struct index_state
*istate
, size_t len
)
879 return mem_pool__ce_calloc(find_mem_pool(istate
), len
);
882 struct cache_entry
*make_empty_transient_cache_entry(size_t len
,
883 struct mem_pool
*ce_mem_pool
)
886 return mem_pool__ce_calloc(ce_mem_pool
, len
);
887 return xcalloc(1, cache_entry_size(len
));
890 enum verify_path_result
{
896 static enum verify_path_result
verify_path_internal(const char *, unsigned);
898 int verify_path(const char *path
, unsigned mode
)
900 return verify_path_internal(path
, mode
) == PATH_OK
;
903 struct cache_entry
*make_cache_entry(struct index_state
*istate
,
905 const struct object_id
*oid
,
908 unsigned int refresh_options
)
910 struct cache_entry
*ce
, *ret
;
913 if (verify_path_internal(path
, mode
) == PATH_INVALID
) {
914 error(_("invalid path '%s'"), path
);
919 ce
= make_empty_cache_entry(istate
, len
);
921 oidcpy(&ce
->oid
, oid
);
922 memcpy(ce
->name
, path
, len
);
923 ce
->ce_flags
= create_ce_flags(stage
);
924 ce
->ce_namelen
= len
;
925 ce
->ce_mode
= create_ce_mode(mode
);
927 ret
= refresh_cache_entry(istate
, ce
, refresh_options
);
929 discard_cache_entry(ce
);
933 struct cache_entry
*make_transient_cache_entry(unsigned int mode
,
934 const struct object_id
*oid
,
937 struct mem_pool
*ce_mem_pool
)
939 struct cache_entry
*ce
;
942 if (!verify_path(path
, mode
)) {
943 error(_("invalid path '%s'"), path
);
948 ce
= make_empty_transient_cache_entry(len
, ce_mem_pool
);
950 oidcpy(&ce
->oid
, oid
);
951 memcpy(ce
->name
, path
, len
);
952 ce
->ce_flags
= create_ce_flags(stage
);
953 ce
->ce_namelen
= len
;
954 ce
->ce_mode
= create_ce_mode(mode
);
960 * Chmod an index entry with either +x or -x.
962 * Returns -1 if the chmod for the particular cache entry failed (if it's
963 * not a regular file), -2 if an invalid flip argument is passed in, 0
966 int chmod_index_entry(struct index_state
*istate
, struct cache_entry
*ce
,
969 if (!S_ISREG(ce
->ce_mode
))
976 ce
->ce_mode
&= ~0111;
981 cache_tree_invalidate_path(istate
, ce
->name
);
982 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
983 mark_fsmonitor_invalid(istate
, ce
);
984 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
989 int ce_same_name(const struct cache_entry
*a
, const struct cache_entry
*b
)
991 int len
= ce_namelen(a
);
992 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
996 * We fundamentally don't like some paths: we don't want
997 * dot or dot-dot anywhere, and for obvious reasons don't
998 * want to recurse into ".git" either.
1000 * Also, we don't want double slashes or slashes at the
1001 * end that can make pathnames ambiguous.
1003 static int verify_dotfile(const char *rest
, unsigned mode
)
1006 * The first character was '.', but that
1007 * has already been discarded, we now test
1011 /* "." is not allowed */
1012 if (*rest
== '\0' || is_dir_sep(*rest
))
1017 * ".git" followed by NUL or slash is bad. Note that we match
1018 * case-insensitively here, even if ignore_case is not set.
1019 * This outlaws ".GIT" everywhere out of an abundance of caution,
1020 * since there's really no good reason to allow it.
1022 * Once we've seen ".git", we can also find ".gitmodules", etc (also
1023 * case-insensitively).
1027 if (rest
[1] != 'i' && rest
[1] != 'I')
1029 if (rest
[2] != 't' && rest
[2] != 'T')
1031 if (rest
[3] == '\0' || is_dir_sep(rest
[3]))
1033 if (S_ISLNK(mode
)) {
1035 if (skip_iprefix(rest
, "modules", &rest
) &&
1036 (*rest
== '\0' || is_dir_sep(*rest
)))
1041 if (rest
[1] == '\0' || is_dir_sep(rest
[1]))
1047 static enum verify_path_result
verify_path_internal(const char *path
,
1052 if (has_dos_drive_prefix(path
))
1053 return PATH_INVALID
;
1055 if (!is_valid_path(path
))
1056 return PATH_INVALID
;
1062 if (is_dir_sep(c
)) {
1066 if (is_hfs_dotgit(path
))
1067 return PATH_INVALID
;
1068 if (S_ISLNK(mode
)) {
1069 if (is_hfs_dotgitmodules(path
))
1070 return PATH_INVALID
;
1074 #if defined GIT_WINDOWS_NATIVE || defined __CYGWIN__
1076 return PATH_INVALID
;
1078 if (is_ntfs_dotgit(path
))
1079 return PATH_INVALID
;
1080 if (S_ISLNK(mode
)) {
1081 if (is_ntfs_dotgitmodules(path
))
1082 return PATH_INVALID
;
1087 if ((c
== '.' && !verify_dotfile(path
, mode
)) ||
1089 return PATH_INVALID
;
1091 * allow terminating directory separators for
1092 * sparse directory entries.
1095 return S_ISDIR(mode
) ? PATH_DIR_WITH_SEP
:
1097 } else if (c
== '\\' && protect_ntfs
) {
1098 if (is_ntfs_dotgit(path
))
1099 return PATH_INVALID
;
1100 if (S_ISLNK(mode
)) {
1101 if (is_ntfs_dotgitmodules(path
))
1102 return PATH_INVALID
;
1111 * Do we have another file that has the beginning components being a
1112 * proper superset of the name we're trying to add?
1114 static int has_file_name(struct index_state
*istate
,
1115 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
1118 int len
= ce_namelen(ce
);
1119 int stage
= ce_stage(ce
);
1120 const char *name
= ce
->name
;
1122 while (pos
< istate
->cache_nr
) {
1123 struct cache_entry
*p
= istate
->cache
[pos
++];
1125 if (len
>= ce_namelen(p
))
1127 if (memcmp(name
, p
->name
, len
))
1129 if (ce_stage(p
) != stage
)
1131 if (p
->name
[len
] != '/')
1133 if (p
->ce_flags
& CE_REMOVE
)
1138 remove_index_entry_at(istate
, --pos
);
1145 * Like strcmp(), but also return the offset of the first change.
1146 * If strings are equal, return the length.
1148 int strcmp_offset(const char *s1
, const char *s2
, size_t *first_change
)
1153 return strcmp(s1
, s2
);
1155 for (k
= 0; s1
[k
] == s2
[k
]; k
++)
1160 return (unsigned char)s1
[k
] - (unsigned char)s2
[k
];
1164 * Do we have another file with a pathname that is a proper
1165 * subset of the name we're trying to add?
1167 * That is, is there another file in the index with a path
1168 * that matches a sub-directory in the given entry?
1170 static int has_dir_name(struct index_state
*istate
,
1171 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
1174 int stage
= ce_stage(ce
);
1175 const char *name
= ce
->name
;
1176 const char *slash
= name
+ ce_namelen(ce
);
1181 * We are frequently called during an iteration on a sorted
1182 * list of pathnames and while building a new index. Therefore,
1183 * there is a high probability that this entry will eventually
1184 * be appended to the index, rather than inserted in the middle.
1185 * If we can confirm that, we can avoid binary searches on the
1186 * components of the pathname.
1188 * Compare the entry's full path with the last path in the index.
1190 if (istate
->cache_nr
> 0) {
1191 cmp_last
= strcmp_offset(name
,
1192 istate
->cache
[istate
->cache_nr
- 1]->name
,
1195 if (len_eq_last
== 0) {
1197 * The entry sorts AFTER the last one in the
1198 * index and their paths have no common prefix,
1199 * so there cannot be a F/D conflict.
1204 * The entry sorts AFTER the last one in the
1205 * index, but has a common prefix. Fall through
1206 * to the loop below to disect the entry's path
1207 * and see where the difference is.
1210 } else if (cmp_last
== 0) {
1212 * The entry exactly matches the last one in the
1213 * index, but because of multiple stage and CE_REMOVE
1214 * items, we fall through and let the regular search
1224 if (*--slash
== '/')
1226 if (slash
<= ce
->name
)
1233 * (len + 1) is a directory boundary (including
1234 * the trailing slash). And since the loop is
1235 * decrementing "slash", the first iteration is
1236 * the longest directory prefix; subsequent
1237 * iterations consider parent directories.
1240 if (len
+ 1 <= len_eq_last
) {
1242 * The directory prefix (including the trailing
1243 * slash) also appears as a prefix in the last
1244 * entry, so the remainder cannot collide (because
1245 * strcmp said the whole path was greater).
1250 * LT: last: xxx/file_A
1256 if (len
> len_eq_last
) {
1258 * This part of the directory prefix (excluding
1259 * the trailing slash) is longer than the known
1260 * equal portions, so this sub-directory cannot
1261 * collide with a file.
1270 * This is a possible collision. Fall through and
1271 * let the regular search code handle it.
1278 pos
= index_name_stage_pos(istate
, name
, len
, stage
, EXPAND_SPARSE
);
1281 * Found one, but not so fast. This could
1282 * be a marker that says "I was here, but
1283 * I am being removed". Such an entry is
1284 * not a part of the resulting tree, and
1285 * it is Ok to have a directory at the same
1288 if (!(istate
->cache
[pos
]->ce_flags
& CE_REMOVE
)) {
1292 remove_index_entry_at(istate
, pos
);
1300 * Trivial optimization: if we find an entry that
1301 * already matches the sub-directory, then we know
1302 * we're ok, and we can exit.
1304 while (pos
< istate
->cache_nr
) {
1305 struct cache_entry
*p
= istate
->cache
[pos
];
1306 if ((ce_namelen(p
) <= len
) ||
1307 (p
->name
[len
] != '/') ||
1308 memcmp(p
->name
, name
, len
))
1309 break; /* not our subdirectory */
1310 if (ce_stage(p
) == stage
&& !(p
->ce_flags
& CE_REMOVE
))
1312 * p is at the same stage as our entry, and
1313 * is a subdirectory of what we are looking
1314 * at, so we cannot have conflicts at our
1315 * level or anything shorter.
1324 /* We may be in a situation where we already have path/file and path
1325 * is being added, or we already have path and path/file is being
1326 * added. Either one would result in a nonsense tree that has path
1327 * twice when git-write-tree tries to write it out. Prevent it.
1329 * If ok-to-replace is specified, we remove the conflicting entries
1330 * from the cache so the caller should recompute the insert position.
1331 * When this happens, we return non-zero.
1333 static int check_file_directory_conflict(struct index_state
*istate
,
1334 const struct cache_entry
*ce
,
1335 int pos
, int ok_to_replace
)
1340 * When ce is an "I am going away" entry, we allow it to be added
1342 if (ce
->ce_flags
& CE_REMOVE
)
1346 * We check if the path is a sub-path of a subsequent pathname
1347 * first, since removing those will not change the position
1350 retval
= has_file_name(istate
, ce
, pos
, ok_to_replace
);
1353 * Then check if the path might have a clashing sub-directory
1356 return retval
+ has_dir_name(istate
, ce
, pos
, ok_to_replace
);
1359 static int add_index_entry_with_check(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
1362 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
1363 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
1364 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
1365 int new_only
= option
& ADD_CACHE_NEW_ONLY
;
1368 * If this entry's path sorts after the last entry in the index,
1369 * we can avoid searching for it.
1371 if (istate
->cache_nr
> 0 &&
1372 strcmp(ce
->name
, istate
->cache
[istate
->cache_nr
- 1]->name
) > 0)
1373 pos
= index_pos_to_insert_pos(istate
->cache_nr
);
1375 pos
= index_name_stage_pos(istate
, ce
->name
, ce_namelen(ce
), ce_stage(ce
), EXPAND_SPARSE
);
1378 * Cache tree path should be invalidated only after index_name_stage_pos,
1379 * in case it expands a sparse index.
1381 if (!(option
& ADD_CACHE_KEEP_CACHE_TREE
))
1382 cache_tree_invalidate_path(istate
, ce
->name
);
1384 /* existing match? Just replace it. */
1387 replace_index_entry(istate
, pos
, ce
);
1392 if (!(option
& ADD_CACHE_KEEP_CACHE_TREE
))
1393 untracked_cache_add_to_index(istate
, ce
->name
);
1396 * Inserting a merged entry ("stage 0") into the index
1397 * will always replace all non-merged entries..
1399 if (pos
< istate
->cache_nr
&& ce_stage(ce
) == 0) {
1400 while (ce_same_name(istate
->cache
[pos
], ce
)) {
1402 if (!remove_index_entry_at(istate
, pos
))
1409 if (verify_path_internal(ce
->name
, ce
->ce_mode
) == PATH_INVALID
)
1410 return error(_("invalid path '%s'"), ce
->name
);
1412 if (!skip_df_check
&&
1413 check_file_directory_conflict(istate
, ce
, pos
, ok_to_replace
)) {
1415 return error(_("'%s' appears as both a file and as a directory"),
1417 pos
= index_name_stage_pos(istate
, ce
->name
, ce_namelen(ce
), ce_stage(ce
), EXPAND_SPARSE
);
1423 int add_index_entry(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
1427 if (option
& ADD_CACHE_JUST_APPEND
)
1428 pos
= istate
->cache_nr
;
1431 ret
= add_index_entry_with_check(istate
, ce
, option
);
1437 /* Make sure the array is big enough .. */
1438 ALLOC_GROW(istate
->cache
, istate
->cache_nr
+ 1, istate
->cache_alloc
);
1442 if (istate
->cache_nr
> pos
+ 1)
1443 MOVE_ARRAY(istate
->cache
+ pos
+ 1, istate
->cache
+ pos
,
1444 istate
->cache_nr
- pos
- 1);
1445 set_index_entry(istate
, pos
, ce
);
1446 istate
->cache_changed
|= CE_ENTRY_ADDED
;
1451 * "refresh" does not calculate a new sha1 file or bring the
1452 * cache up-to-date for mode/content changes. But what it
1453 * _does_ do is to "re-match" the stat information of a file
1454 * with the cache, so that you can refresh the cache for a
1455 * file that hasn't been changed but where the stat entry is
1458 * For example, you'd want to do this after doing a "git-read-tree",
1459 * to link up the stat cache details with the proper files.
1461 static struct cache_entry
*refresh_cache_ent(struct index_state
*istate
,
1462 struct cache_entry
*ce
,
1463 unsigned int options
, int *err
,
1469 struct cache_entry
*updated
;
1471 int refresh
= options
& CE_MATCH_REFRESH
;
1472 int ignore_valid
= options
& CE_MATCH_IGNORE_VALID
;
1473 int ignore_skip_worktree
= options
& CE_MATCH_IGNORE_SKIP_WORKTREE
;
1474 int ignore_missing
= options
& CE_MATCH_IGNORE_MISSING
;
1475 int ignore_fsmonitor
= options
& CE_MATCH_IGNORE_FSMONITOR
;
1477 if (!refresh
|| ce_uptodate(ce
))
1480 if (!ignore_fsmonitor
)
1481 refresh_fsmonitor(istate
);
1483 * CE_VALID or CE_SKIP_WORKTREE means the user promised us
1484 * that the change to the work tree does not matter and told
1487 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
)) {
1488 ce_mark_uptodate(ce
);
1491 if (!ignore_valid
&& (ce
->ce_flags
& CE_VALID
)) {
1492 ce_mark_uptodate(ce
);
1495 if (!ignore_fsmonitor
&& (ce
->ce_flags
& CE_FSMONITOR_VALID
)) {
1496 ce_mark_uptodate(ce
);
1500 if (has_symlink_leading_path(ce
->name
, ce_namelen(ce
))) {
1510 if (lstat(ce
->name
, &st
) < 0) {
1511 if (ignore_missing
&& errno
== ENOENT
)
1518 changed
= ie_match_stat(istate
, ce
, &st
, options
);
1520 *changed_ret
= changed
;
1523 * The path is unchanged. If we were told to ignore
1524 * valid bit, then we did the actual stat check and
1525 * found that the entry is unmodified. If the entry
1526 * is not marked VALID, this is the place to mark it
1527 * valid again, under "assume unchanged" mode.
1529 if (ignore_valid
&& assume_unchanged
&&
1530 !(ce
->ce_flags
& CE_VALID
))
1531 ; /* mark this one VALID again */
1534 * We do not mark the index itself "modified"
1535 * because CE_UPTODATE flag is in-core only;
1536 * we are not going to write this change out.
1538 if (!S_ISGITLINK(ce
->ce_mode
)) {
1539 ce_mark_uptodate(ce
);
1540 mark_fsmonitor_valid(istate
, ce
);
1548 if (ie_modified(istate
, ce
, &st
, options
)) {
1554 updated
= make_empty_cache_entry(istate
, ce_namelen(ce
));
1555 copy_cache_entry(updated
, ce
);
1556 memcpy(updated
->name
, ce
->name
, ce
->ce_namelen
+ 1);
1557 fill_stat_cache_info(istate
, updated
, &st
);
1559 * If ignore_valid is not set, we should leave CE_VALID bit
1560 * alone. Otherwise, paths marked with --no-assume-unchanged
1561 * (i.e. things to be edited) will reacquire CE_VALID bit
1562 * automatically, which is not really what we want.
1564 if (!ignore_valid
&& assume_unchanged
&&
1565 !(ce
->ce_flags
& CE_VALID
))
1566 updated
->ce_flags
&= ~CE_VALID
;
1568 /* istate->cache_changed is updated in the caller */
1572 static void show_file(const char * fmt
, const char * name
, int in_porcelain
,
1573 int * first
, const char *header_msg
)
1575 if (in_porcelain
&& *first
&& header_msg
) {
1576 printf("%s\n", header_msg
);
1582 int repo_refresh_and_write_index(struct repository
*repo
,
1583 unsigned int refresh_flags
,
1584 unsigned int write_flags
,
1586 const struct pathspec
*pathspec
,
1587 char *seen
, const char *header_msg
)
1589 struct lock_file lock_file
= LOCK_INIT
;
1592 fd
= repo_hold_locked_index(repo
, &lock_file
, 0);
1593 if (!gentle
&& fd
< 0)
1595 if (refresh_index(repo
->index
, refresh_flags
, pathspec
, seen
, header_msg
))
1597 if (0 <= fd
&& write_locked_index(repo
->index
, &lock_file
, COMMIT_LOCK
| write_flags
))
1603 int refresh_index(struct index_state
*istate
, unsigned int flags
,
1604 const struct pathspec
*pathspec
,
1605 char *seen
, const char *header_msg
)
1609 int really
= (flags
& REFRESH_REALLY
) != 0;
1610 int allow_unmerged
= (flags
& REFRESH_UNMERGED
) != 0;
1611 int quiet
= (flags
& REFRESH_QUIET
) != 0;
1612 int not_new
= (flags
& REFRESH_IGNORE_MISSING
) != 0;
1613 int ignore_submodules
= (flags
& REFRESH_IGNORE_SUBMODULES
) != 0;
1614 int ignore_skip_worktree
= (flags
& REFRESH_IGNORE_SKIP_WORKTREE
) != 0;
1616 int in_porcelain
= (flags
& REFRESH_IN_PORCELAIN
);
1617 unsigned int options
= (CE_MATCH_REFRESH
|
1618 (really
? CE_MATCH_IGNORE_VALID
: 0) |
1619 (not_new
? CE_MATCH_IGNORE_MISSING
: 0));
1620 const char *modified_fmt
;
1621 const char *deleted_fmt
;
1622 const char *typechange_fmt
;
1623 const char *added_fmt
;
1624 const char *unmerged_fmt
;
1625 struct progress
*progress
= NULL
;
1626 int t2_sum_lstat
= 0;
1627 int t2_sum_scan
= 0;
1629 if (flags
& REFRESH_PROGRESS
&& isatty(2))
1630 progress
= start_delayed_progress(_("Refresh index"),
1633 trace_performance_enter();
1634 modified_fmt
= in_porcelain
? "M\t%s\n" : "%s: needs update\n";
1635 deleted_fmt
= in_porcelain
? "D\t%s\n" : "%s: needs update\n";
1636 typechange_fmt
= in_porcelain
? "T\t%s\n" : "%s: needs update\n";
1637 added_fmt
= in_porcelain
? "A\t%s\n" : "%s: needs update\n";
1638 unmerged_fmt
= in_porcelain
? "U\t%s\n" : "%s: needs merge\n";
1640 * Use the multi-threaded preload_index() to refresh most of the
1641 * cache entries quickly then in the single threaded loop below,
1642 * we only have to do the special cases that are left.
1644 preload_index(istate
, pathspec
, 0);
1645 trace2_region_enter("index", "refresh", NULL
);
1647 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1648 struct cache_entry
*ce
, *new_entry
;
1649 int cache_errno
= 0;
1652 int t2_did_lstat
= 0;
1653 int t2_did_scan
= 0;
1655 ce
= istate
->cache
[i
];
1656 if (ignore_submodules
&& S_ISGITLINK(ce
->ce_mode
))
1658 if (ignore_skip_worktree
&& ce_skip_worktree(ce
))
1662 * If this entry is a sparse directory, then there isn't
1663 * any stat() information to update. Ignore the entry.
1665 if (S_ISSPARSEDIR(ce
->ce_mode
))
1668 if (pathspec
&& !ce_path_match(istate
, ce
, pathspec
, seen
))
1672 while ((i
< istate
->cache_nr
) &&
1673 ! strcmp(istate
->cache
[i
]->name
, ce
->name
))
1679 show_file(unmerged_fmt
, ce
->name
, in_porcelain
,
1680 &first
, header_msg
);
1688 new_entry
= refresh_cache_ent(istate
, ce
, options
,
1689 &cache_errno
, &changed
,
1690 &t2_did_lstat
, &t2_did_scan
);
1691 t2_sum_lstat
+= t2_did_lstat
;
1692 t2_sum_scan
+= t2_did_scan
;
1693 if (new_entry
== ce
)
1695 display_progress(progress
, i
);
1699 if (really
&& cache_errno
== EINVAL
) {
1700 /* If we are doing --really-refresh that
1701 * means the index is not valid anymore.
1703 ce
->ce_flags
&= ~CE_VALID
;
1704 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
1705 mark_fsmonitor_invalid(istate
, ce
);
1706 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
1711 if (cache_errno
== ENOENT
)
1713 else if (ce_intent_to_add(ce
))
1714 fmt
= added_fmt
; /* must be before other checks */
1715 else if (changed
& TYPE_CHANGED
)
1716 fmt
= typechange_fmt
;
1720 ce
->name
, in_porcelain
, &first
, header_msg
);
1725 replace_index_entry(istate
, i
, new_entry
);
1727 trace2_data_intmax("index", NULL
, "refresh/sum_lstat", t2_sum_lstat
);
1728 trace2_data_intmax("index", NULL
, "refresh/sum_scan", t2_sum_scan
);
1729 trace2_region_leave("index", "refresh", NULL
);
1730 display_progress(progress
, istate
->cache_nr
);
1731 stop_progress(&progress
);
1732 trace_performance_leave("refresh index");
1736 struct cache_entry
*refresh_cache_entry(struct index_state
*istate
,
1737 struct cache_entry
*ce
,
1738 unsigned int options
)
1740 return refresh_cache_ent(istate
, ce
, options
, NULL
, NULL
, NULL
, NULL
);
1744 /*****************************************************************
1746 *****************************************************************/
1748 #define INDEX_FORMAT_DEFAULT 3
1750 static unsigned int get_index_format_default(struct repository
*r
)
1752 char *envversion
= getenv("GIT_INDEX_VERSION");
1754 unsigned int version
= INDEX_FORMAT_DEFAULT
;
1757 prepare_repo_settings(r
);
1759 if (r
->settings
.index_version
>= 0)
1760 version
= r
->settings
.index_version
;
1761 if (version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< version
) {
1762 warning(_("index.version set, but the value is invalid.\n"
1763 "Using version %i"), INDEX_FORMAT_DEFAULT
);
1764 return INDEX_FORMAT_DEFAULT
;
1769 version
= strtoul(envversion
, &endp
, 10);
1771 version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< version
) {
1772 warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1773 "Using version %i"), INDEX_FORMAT_DEFAULT
);
1774 version
= INDEX_FORMAT_DEFAULT
;
1780 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
1781 * Again - this is just a (very strong in practice) heuristic that
1782 * the inode hasn't changed.
1784 * We save the fields in big-endian order to allow using the
1785 * index file over NFS transparently.
1787 struct ondisk_cache_entry
{
1788 struct cache_time ctime
;
1789 struct cache_time mtime
;
1797 * unsigned char hash[hashsz];
1799 * if (flags & CE_EXTENDED)
1802 unsigned char data
[GIT_MAX_RAWSZ
+ 2 * sizeof(uint16_t)];
1803 char name
[FLEX_ARRAY
];
1806 /* These are only used for v3 or lower */
1807 #define align_padding_size(size, len) ((size + (len) + 8) & ~7) - (size + len)
1808 #define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,data) + (len) + 8) & ~7)
1809 #define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)
1810 #define ondisk_data_size(flags, len) (the_hash_algo->rawsz + \
1811 ((flags & CE_EXTENDED) ? 2 : 1) * sizeof(uint16_t) + len)
1812 #define ondisk_data_size_max(len) (ondisk_data_size(CE_EXTENDED, len))
1813 #define ondisk_ce_size(ce) (ondisk_cache_entry_size(ondisk_data_size((ce)->ce_flags, ce_namelen(ce))))
1815 /* Allow fsck to force verification of the index checksum. */
1816 int verify_index_checksum
;
1818 /* Allow fsck to force verification of the cache entry order. */
1819 int verify_ce_order
;
1821 static int verify_hdr(const struct cache_header
*hdr
, unsigned long size
)
1824 unsigned char hash
[GIT_MAX_RAWSZ
];
1826 unsigned char *start
, *end
;
1827 struct object_id oid
;
1829 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
1830 return error(_("bad signature 0x%08x"), hdr
->hdr_signature
);
1831 hdr_version
= ntohl(hdr
->hdr_version
);
1832 if (hdr_version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< hdr_version
)
1833 return error(_("bad index version %d"), hdr_version
);
1835 if (!verify_index_checksum
)
1838 end
= (unsigned char *)hdr
+ size
;
1839 start
= end
- the_hash_algo
->rawsz
;
1840 oidread(&oid
, start
);
1841 if (oideq(&oid
, null_oid()))
1844 the_hash_algo
->init_fn(&c
);
1845 the_hash_algo
->update_fn(&c
, hdr
, size
- the_hash_algo
->rawsz
);
1846 the_hash_algo
->final_fn(hash
, &c
);
1847 if (!hasheq(hash
, start
))
1848 return error(_("bad index file sha1 signature"));
1852 static int read_index_extension(struct index_state
*istate
,
1853 const char *ext
, const char *data
, unsigned long sz
)
1855 switch (CACHE_EXT(ext
)) {
1856 case CACHE_EXT_TREE
:
1857 istate
->cache_tree
= cache_tree_read(data
, sz
);
1859 case CACHE_EXT_RESOLVE_UNDO
:
1860 istate
->resolve_undo
= resolve_undo_read(data
, sz
);
1862 case CACHE_EXT_LINK
:
1863 if (read_link_extension(istate
, data
, sz
))
1866 case CACHE_EXT_UNTRACKED
:
1867 istate
->untracked
= read_untracked_extension(data
, sz
);
1869 case CACHE_EXT_FSMONITOR
:
1870 read_fsmonitor_extension(istate
, data
, sz
);
1872 case CACHE_EXT_ENDOFINDEXENTRIES
:
1873 case CACHE_EXT_INDEXENTRYOFFSETTABLE
:
1874 /* already handled in do_read_index() */
1876 case CACHE_EXT_SPARSE_DIRECTORIES
:
1877 /* no content, only an indicator */
1878 istate
->sparse_index
= INDEX_COLLAPSED
;
1881 if (*ext
< 'A' || 'Z' < *ext
)
1882 return error(_("index uses %.4s extension, which we do not understand"),
1884 fprintf_ln(stderr
, _("ignoring %.4s extension"), ext
);
1891 * Parses the contents of the cache entry contained within the 'ondisk' buffer
1892 * into a new incore 'cache_entry'.
1894 * Note that 'char *ondisk' may not be aligned to a 4-byte address interval in
1895 * index v4, so we cannot cast it to 'struct ondisk_cache_entry *' and access
1896 * its members. Instead, we use the byte offsets of members within the struct to
1897 * identify where 'get_be16()', 'get_be32()', and 'oidread()' (which can all
1898 * read from an unaligned memory buffer) should read from the 'ondisk' buffer
1899 * into the corresponding incore 'cache_entry' members.
1901 static struct cache_entry
*create_from_disk(struct mem_pool
*ce_mem_pool
,
1902 unsigned int version
,
1904 unsigned long *ent_size
,
1905 const struct cache_entry
*previous_ce
)
1907 struct cache_entry
*ce
;
1910 const unsigned hashsz
= the_hash_algo
->rawsz
;
1911 const char *flagsp
= ondisk
+ offsetof(struct ondisk_cache_entry
, data
) + hashsz
;
1913 size_t copy_len
= 0;
1915 * Adjacent cache entries tend to share the leading paths, so it makes
1916 * sense to only store the differences in later entries. In the v4
1917 * on-disk format of the index, each on-disk cache entry stores the
1918 * number of bytes to be stripped from the end of the previous name,
1919 * and the bytes to append to the result, to come up with its name.
1921 int expand_name_field
= version
== 4;
1923 /* On-disk flags are just 16 bits */
1924 flags
= get_be16(flagsp
);
1925 len
= flags
& CE_NAMEMASK
;
1927 if (flags
& CE_EXTENDED
) {
1929 extended_flags
= get_be16(flagsp
+ sizeof(uint16_t)) << 16;
1930 /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1931 if (extended_flags
& ~CE_EXTENDED_FLAGS
)
1932 die(_("unknown index entry format 0x%08x"), extended_flags
);
1933 flags
|= extended_flags
;
1934 name
= (const char *)(flagsp
+ 2 * sizeof(uint16_t));
1937 name
= (const char *)(flagsp
+ sizeof(uint16_t));
1939 if (expand_name_field
) {
1940 const unsigned char *cp
= (const unsigned char *)name
;
1941 size_t strip_len
, previous_len
;
1943 /* If we're at the beginning of a block, ignore the previous name */
1944 strip_len
= decode_varint(&cp
);
1946 previous_len
= previous_ce
->ce_namelen
;
1947 if (previous_len
< strip_len
)
1948 die(_("malformed name field in the index, near path '%s'"),
1950 copy_len
= previous_len
- strip_len
;
1952 name
= (const char *)cp
;
1955 if (len
== CE_NAMEMASK
) {
1957 if (expand_name_field
)
1961 ce
= mem_pool__ce_alloc(ce_mem_pool
, len
);
1964 * NEEDSWORK: using 'offsetof()' is cumbersome and should be replaced
1965 * with something more akin to 'load_bitmap_entries_v1()'s use of
1966 * 'read_be16'/'read_be32'. For consistency with the corresponding
1967 * ondisk entry write function ('copy_cache_entry_to_ondisk()'), this
1968 * should be done at the same time as removing references to
1969 * 'ondisk_cache_entry' there.
1971 ce
->ce_stat_data
.sd_ctime
.sec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, ctime
)
1972 + offsetof(struct cache_time
, sec
));
1973 ce
->ce_stat_data
.sd_mtime
.sec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, mtime
)
1974 + offsetof(struct cache_time
, sec
));
1975 ce
->ce_stat_data
.sd_ctime
.nsec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, ctime
)
1976 + offsetof(struct cache_time
, nsec
));
1977 ce
->ce_stat_data
.sd_mtime
.nsec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, mtime
)
1978 + offsetof(struct cache_time
, nsec
));
1979 ce
->ce_stat_data
.sd_dev
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, dev
));
1980 ce
->ce_stat_data
.sd_ino
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, ino
));
1981 ce
->ce_mode
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, mode
));
1982 ce
->ce_stat_data
.sd_uid
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, uid
));
1983 ce
->ce_stat_data
.sd_gid
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, gid
));
1984 ce
->ce_stat_data
.sd_size
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, size
));
1985 ce
->ce_flags
= flags
& ~CE_NAMEMASK
;
1986 ce
->ce_namelen
= len
;
1988 oidread(&ce
->oid
, (const unsigned char *)ondisk
+ offsetof(struct ondisk_cache_entry
, data
));
1990 if (expand_name_field
) {
1992 memcpy(ce
->name
, previous_ce
->name
, copy_len
);
1993 memcpy(ce
->name
+ copy_len
, name
, len
+ 1 - copy_len
);
1994 *ent_size
= (name
- ((char *)ondisk
)) + len
+ 1 - copy_len
;
1996 memcpy(ce
->name
, name
, len
+ 1);
1997 *ent_size
= ondisk_ce_size(ce
);
2002 static void check_ce_order(struct index_state
*istate
)
2006 if (!verify_ce_order
)
2009 for (i
= 1; i
< istate
->cache_nr
; i
++) {
2010 struct cache_entry
*ce
= istate
->cache
[i
- 1];
2011 struct cache_entry
*next_ce
= istate
->cache
[i
];
2012 int name_compare
= strcmp(ce
->name
, next_ce
->name
);
2014 if (0 < name_compare
)
2015 die(_("unordered stage entries in index"));
2016 if (!name_compare
) {
2018 die(_("multiple stage entries for merged file '%s'"),
2020 if (ce_stage(ce
) > ce_stage(next_ce
))
2021 die(_("unordered stage entries for '%s'"),
2027 static void tweak_untracked_cache(struct index_state
*istate
)
2029 struct repository
*r
= the_repository
;
2031 prepare_repo_settings(r
);
2033 switch (r
->settings
.core_untracked_cache
) {
2034 case UNTRACKED_CACHE_REMOVE
:
2035 remove_untracked_cache(istate
);
2037 case UNTRACKED_CACHE_WRITE
:
2038 add_untracked_cache(istate
);
2040 case UNTRACKED_CACHE_KEEP
:
2042 * Either an explicit "core.untrackedCache=keep", the
2043 * default if "core.untrackedCache" isn't configured,
2044 * or a fallback on an unknown "core.untrackedCache"
2051 static void tweak_split_index(struct index_state
*istate
)
2053 switch (git_config_get_split_index()) {
2054 case -1: /* unset: do nothing */
2057 remove_split_index(istate
);
2060 add_split_index(istate
);
2062 default: /* unknown value: do nothing */
2067 static void post_read_index_from(struct index_state
*istate
)
2069 check_ce_order(istate
);
2070 tweak_untracked_cache(istate
);
2071 tweak_split_index(istate
);
2072 tweak_fsmonitor(istate
);
2075 static size_t estimate_cache_size_from_compressed(unsigned int entries
)
2077 return entries
* (sizeof(struct cache_entry
) + CACHE_ENTRY_PATH_LENGTH
);
2080 static size_t estimate_cache_size(size_t ondisk_size
, unsigned int entries
)
2082 long per_entry
= sizeof(struct cache_entry
) - sizeof(struct ondisk_cache_entry
);
2085 * Account for potential alignment differences.
2087 per_entry
+= align_padding_size(per_entry
, 0);
2088 return ondisk_size
+ entries
* per_entry
;
2091 struct index_entry_offset
2093 /* starting byte offset into index file, count of index entries in this block */
2097 struct index_entry_offset_table
2100 struct index_entry_offset entries
[FLEX_ARRAY
];
2103 static struct index_entry_offset_table
*read_ieot_extension(const char *mmap
, size_t mmap_size
, size_t offset
);
2104 static void write_ieot_extension(struct strbuf
*sb
, struct index_entry_offset_table
*ieot
);
2106 static size_t read_eoie_extension(const char *mmap
, size_t mmap_size
);
2107 static void write_eoie_extension(struct strbuf
*sb
, git_hash_ctx
*eoie_context
, size_t offset
);
2109 struct load_index_extensions
2112 struct index_state
*istate
;
2115 unsigned long src_offset
;
2118 static void *load_index_extensions(void *_data
)
2120 struct load_index_extensions
*p
= _data
;
2121 unsigned long src_offset
= p
->src_offset
;
2123 while (src_offset
<= p
->mmap_size
- the_hash_algo
->rawsz
- 8) {
2124 /* After an array of active_nr index entries,
2125 * there can be arbitrary number of extended
2126 * sections, each of which is prefixed with
2127 * extension name (4-byte) and section length
2128 * in 4-byte network byte order.
2130 uint32_t extsize
= get_be32(p
->mmap
+ src_offset
+ 4);
2131 if (read_index_extension(p
->istate
,
2132 p
->mmap
+ src_offset
,
2133 p
->mmap
+ src_offset
+ 8,
2135 munmap((void *)p
->mmap
, p
->mmap_size
);
2136 die(_("index file corrupt"));
2139 src_offset
+= extsize
;
2146 * A helper function that will load the specified range of cache entries
2147 * from the memory mapped file and add them to the given index.
2149 static unsigned long load_cache_entry_block(struct index_state
*istate
,
2150 struct mem_pool
*ce_mem_pool
, int offset
, int nr
, const char *mmap
,
2151 unsigned long start_offset
, const struct cache_entry
*previous_ce
)
2154 unsigned long src_offset
= start_offset
;
2156 for (i
= offset
; i
< offset
+ nr
; i
++) {
2157 struct cache_entry
*ce
;
2158 unsigned long consumed
;
2160 ce
= create_from_disk(ce_mem_pool
, istate
->version
,
2162 &consumed
, previous_ce
);
2163 set_index_entry(istate
, i
, ce
);
2165 src_offset
+= consumed
;
2168 return src_offset
- start_offset
;
2171 static unsigned long load_all_cache_entries(struct index_state
*istate
,
2172 const char *mmap
, size_t mmap_size
, unsigned long src_offset
)
2174 unsigned long consumed
;
2176 istate
->ce_mem_pool
= xmalloc(sizeof(*istate
->ce_mem_pool
));
2177 if (istate
->version
== 4) {
2178 mem_pool_init(istate
->ce_mem_pool
,
2179 estimate_cache_size_from_compressed(istate
->cache_nr
));
2181 mem_pool_init(istate
->ce_mem_pool
,
2182 estimate_cache_size(mmap_size
, istate
->cache_nr
));
2185 consumed
= load_cache_entry_block(istate
, istate
->ce_mem_pool
,
2186 0, istate
->cache_nr
, mmap
, src_offset
, NULL
);
2191 * Mostly randomly chosen maximum thread counts: we
2192 * cap the parallelism to online_cpus() threads, and we want
2193 * to have at least 10000 cache entries per thread for it to
2194 * be worth starting a thread.
2197 #define THREAD_COST (10000)
2199 struct load_cache_entries_thread_data
2202 struct index_state
*istate
;
2203 struct mem_pool
*ce_mem_pool
;
2206 struct index_entry_offset_table
*ieot
;
2207 int ieot_start
; /* starting index into the ieot array */
2208 int ieot_blocks
; /* count of ieot entries to process */
2209 unsigned long consumed
; /* return # of bytes in index file processed */
2213 * A thread proc to run the load_cache_entries() computation
2214 * across multiple background threads.
2216 static void *load_cache_entries_thread(void *_data
)
2218 struct load_cache_entries_thread_data
*p
= _data
;
2221 /* iterate across all ieot blocks assigned to this thread */
2222 for (i
= p
->ieot_start
; i
< p
->ieot_start
+ p
->ieot_blocks
; i
++) {
2223 p
->consumed
+= load_cache_entry_block(p
->istate
, p
->ce_mem_pool
,
2224 p
->offset
, p
->ieot
->entries
[i
].nr
, p
->mmap
, p
->ieot
->entries
[i
].offset
, NULL
);
2225 p
->offset
+= p
->ieot
->entries
[i
].nr
;
2230 static unsigned long load_cache_entries_threaded(struct index_state
*istate
, const char *mmap
, size_t mmap_size
,
2231 int nr_threads
, struct index_entry_offset_table
*ieot
)
2233 int i
, offset
, ieot_blocks
, ieot_start
, err
;
2234 struct load_cache_entries_thread_data
*data
;
2235 unsigned long consumed
= 0;
2237 /* a little sanity checking */
2238 if (istate
->name_hash_initialized
)
2239 BUG("the name hash isn't thread safe");
2241 istate
->ce_mem_pool
= xmalloc(sizeof(*istate
->ce_mem_pool
));
2242 mem_pool_init(istate
->ce_mem_pool
, 0);
2244 /* ensure we have no more threads than we have blocks to process */
2245 if (nr_threads
> ieot
->nr
)
2246 nr_threads
= ieot
->nr
;
2247 CALLOC_ARRAY(data
, nr_threads
);
2249 offset
= ieot_start
= 0;
2250 ieot_blocks
= DIV_ROUND_UP(ieot
->nr
, nr_threads
);
2251 for (i
= 0; i
< nr_threads
; i
++) {
2252 struct load_cache_entries_thread_data
*p
= &data
[i
];
2255 if (ieot_start
+ ieot_blocks
> ieot
->nr
)
2256 ieot_blocks
= ieot
->nr
- ieot_start
;
2262 p
->ieot_start
= ieot_start
;
2263 p
->ieot_blocks
= ieot_blocks
;
2265 /* create a mem_pool for each thread */
2267 for (j
= p
->ieot_start
; j
< p
->ieot_start
+ p
->ieot_blocks
; j
++)
2268 nr
+= p
->ieot
->entries
[j
].nr
;
2269 p
->ce_mem_pool
= xmalloc(sizeof(*istate
->ce_mem_pool
));
2270 if (istate
->version
== 4) {
2271 mem_pool_init(p
->ce_mem_pool
,
2272 estimate_cache_size_from_compressed(nr
));
2274 mem_pool_init(p
->ce_mem_pool
,
2275 estimate_cache_size(mmap_size
, nr
));
2278 err
= pthread_create(&p
->pthread
, NULL
, load_cache_entries_thread
, p
);
2280 die(_("unable to create load_cache_entries thread: %s"), strerror(err
));
2282 /* increment by the number of cache entries in the ieot block being processed */
2283 for (j
= 0; j
< ieot_blocks
; j
++)
2284 offset
+= ieot
->entries
[ieot_start
+ j
].nr
;
2285 ieot_start
+= ieot_blocks
;
2288 for (i
= 0; i
< nr_threads
; i
++) {
2289 struct load_cache_entries_thread_data
*p
= &data
[i
];
2291 err
= pthread_join(p
->pthread
, NULL
);
2293 die(_("unable to join load_cache_entries thread: %s"), strerror(err
));
2294 mem_pool_combine(istate
->ce_mem_pool
, p
->ce_mem_pool
);
2295 consumed
+= p
->consumed
;
2303 static void set_new_index_sparsity(struct index_state
*istate
)
2306 * If the index's repo exists, mark it sparse according to
2309 prepare_repo_settings(istate
->repo
);
2310 if (!istate
->repo
->settings
.command_requires_full_index
&&
2311 is_sparse_index_allowed(istate
, 0))
2312 istate
->sparse_index
= 1;
2315 /* remember to discard_cache() before reading a different cache! */
2316 int do_read_index(struct index_state
*istate
, const char *path
, int must_exist
)
2320 unsigned long src_offset
;
2321 const struct cache_header
*hdr
;
2324 struct load_index_extensions p
;
2325 size_t extension_offset
= 0;
2326 int nr_threads
, cpus
;
2327 struct index_entry_offset_table
*ieot
= NULL
;
2329 if (istate
->initialized
)
2330 return istate
->cache_nr
;
2332 istate
->timestamp
.sec
= 0;
2333 istate
->timestamp
.nsec
= 0;
2334 fd
= open(path
, O_RDONLY
);
2336 if (!must_exist
&& errno
== ENOENT
) {
2337 set_new_index_sparsity(istate
);
2340 die_errno(_("%s: index file open failed"), path
);
2344 die_errno(_("%s: cannot stat the open index"), path
);
2346 mmap_size
= xsize_t(st
.st_size
);
2347 if (mmap_size
< sizeof(struct cache_header
) + the_hash_algo
->rawsz
)
2348 die(_("%s: index file smaller than expected"), path
);
2350 mmap
= xmmap_gently(NULL
, mmap_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2351 if (mmap
== MAP_FAILED
)
2352 die_errno(_("%s: unable to map index file%s"), path
,
2356 hdr
= (const struct cache_header
*)mmap
;
2357 if (verify_hdr(hdr
, mmap_size
) < 0)
2360 oidread(&istate
->oid
, (const unsigned char *)hdr
+ mmap_size
- the_hash_algo
->rawsz
);
2361 istate
->version
= ntohl(hdr
->hdr_version
);
2362 istate
->cache_nr
= ntohl(hdr
->hdr_entries
);
2363 istate
->cache_alloc
= alloc_nr(istate
->cache_nr
);
2364 CALLOC_ARRAY(istate
->cache
, istate
->cache_alloc
);
2365 istate
->initialized
= 1;
2369 p
.mmap_size
= mmap_size
;
2371 src_offset
= sizeof(*hdr
);
2373 if (git_config_get_index_threads(&nr_threads
))
2376 /* TODO: does creating more threads than cores help? */
2378 nr_threads
= istate
->cache_nr
/ THREAD_COST
;
2379 cpus
= online_cpus();
2380 if (nr_threads
> cpus
)
2387 if (nr_threads
> 1) {
2388 extension_offset
= read_eoie_extension(mmap
, mmap_size
);
2389 if (extension_offset
) {
2392 p
.src_offset
= extension_offset
;
2393 err
= pthread_create(&p
.pthread
, NULL
, load_index_extensions
, &p
);
2395 die(_("unable to create load_index_extensions thread: %s"), strerror(err
));
2402 * Locate and read the index entry offset table so that we can use it
2403 * to multi-thread the reading of the cache entries.
2405 if (extension_offset
&& nr_threads
> 1)
2406 ieot
= read_ieot_extension(mmap
, mmap_size
, extension_offset
);
2409 src_offset
+= load_cache_entries_threaded(istate
, mmap
, mmap_size
, nr_threads
, ieot
);
2412 src_offset
+= load_all_cache_entries(istate
, mmap
, mmap_size
, src_offset
);
2415 istate
->timestamp
.sec
= st
.st_mtime
;
2416 istate
->timestamp
.nsec
= ST_MTIME_NSEC(st
);
2418 /* if we created a thread, join it otherwise load the extensions on the primary thread */
2419 if (extension_offset
) {
2420 int ret
= pthread_join(p
.pthread
, NULL
);
2422 die(_("unable to join load_index_extensions thread: %s"), strerror(ret
));
2424 p
.src_offset
= src_offset
;
2425 load_index_extensions(&p
);
2427 munmap((void *)mmap
, mmap_size
);
2430 * TODO trace2: replace "the_repository" with the actual repo instance
2431 * that is associated with the given "istate".
2433 trace2_data_intmax("index", the_repository
, "read/version",
2435 trace2_data_intmax("index", the_repository
, "read/cache_nr",
2439 * If the command explicitly requires a full index, force it
2440 * to be full. Otherwise, correct the sparsity based on repository
2441 * settings and other properties of the index (if necessary).
2443 prepare_repo_settings(istate
->repo
);
2444 if (istate
->repo
->settings
.command_requires_full_index
)
2445 ensure_full_index(istate
);
2447 ensure_correct_sparsity(istate
);
2449 return istate
->cache_nr
;
2452 munmap((void *)mmap
, mmap_size
);
2453 die(_("index file corrupt"));
2457 * Signal that the shared index is used by updating its mtime.
2459 * This way, shared index can be removed if they have not been used
2462 static void freshen_shared_index(const char *shared_index
, int warn
)
2464 if (!check_and_freshen_file(shared_index
, 1) && warn
)
2465 warning(_("could not freshen shared index '%s'"), shared_index
);
2468 int read_index_from(struct index_state
*istate
, const char *path
,
2471 struct split_index
*split_index
;
2476 /* istate->initialized covers both .git/index and .git/sharedindex.xxx */
2477 if (istate
->initialized
)
2478 return istate
->cache_nr
;
2481 * TODO trace2: replace "the_repository" with the actual repo instance
2482 * that is associated with the given "istate".
2484 trace2_region_enter_printf("index", "do_read_index", the_repository
,
2486 trace_performance_enter();
2487 ret
= do_read_index(istate
, path
, 0);
2488 trace_performance_leave("read cache %s", path
);
2489 trace2_region_leave_printf("index", "do_read_index", the_repository
,
2492 split_index
= istate
->split_index
;
2493 if (!split_index
|| is_null_oid(&split_index
->base_oid
)) {
2494 post_read_index_from(istate
);
2498 trace_performance_enter();
2499 if (split_index
->base
)
2500 release_index(split_index
->base
);
2502 ALLOC_ARRAY(split_index
->base
, 1);
2503 index_state_init(split_index
->base
, istate
->repo
);
2505 base_oid_hex
= oid_to_hex(&split_index
->base_oid
);
2506 base_path
= xstrfmt("%s/sharedindex.%s", gitdir
, base_oid_hex
);
2507 trace2_region_enter_printf("index", "shared/do_read_index",
2508 the_repository
, "%s", base_path
);
2509 ret
= do_read_index(split_index
->base
, base_path
, 0);
2510 trace2_region_leave_printf("index", "shared/do_read_index",
2511 the_repository
, "%s", base_path
);
2513 char *path_copy
= xstrdup(path
);
2514 char *base_path2
= xstrfmt("%s/sharedindex.%s",
2515 dirname(path_copy
), base_oid_hex
);
2517 trace2_region_enter_printf("index", "shared/do_read_index",
2518 the_repository
, "%s", base_path2
);
2519 ret
= do_read_index(split_index
->base
, base_path2
, 1);
2520 trace2_region_leave_printf("index", "shared/do_read_index",
2521 the_repository
, "%s", base_path2
);
2524 if (!oideq(&split_index
->base_oid
, &split_index
->base
->oid
))
2525 die(_("broken index, expect %s in %s, got %s"),
2526 base_oid_hex
, base_path
,
2527 oid_to_hex(&split_index
->base
->oid
));
2529 freshen_shared_index(base_path
, 0);
2530 merge_base_index(istate
);
2531 post_read_index_from(istate
);
2532 trace_performance_leave("read cache %s", base_path
);
2537 int is_index_unborn(struct index_state
*istate
)
2539 return (!istate
->cache_nr
&& !istate
->timestamp
.sec
);
2542 void index_state_init(struct index_state
*istate
, struct repository
*r
)
2544 struct index_state blank
= INDEX_STATE_INIT(r
);
2545 memcpy(istate
, &blank
, sizeof(*istate
));
2548 void release_index(struct index_state
*istate
)
2551 * Cache entries in istate->cache[] should have been allocated
2552 * from the memory pool associated with this index, or from an
2553 * associated split_index. There is no need to free individual
2554 * cache entries. validate_cache_entries can detect when this
2555 * assertion does not hold.
2557 validate_cache_entries(istate
);
2559 resolve_undo_clear_index(istate
);
2560 free_name_hash(istate
);
2561 cache_tree_free(&(istate
->cache_tree
));
2562 free(istate
->fsmonitor_last_update
);
2563 free(istate
->cache
);
2564 discard_split_index(istate
);
2565 free_untracked_cache(istate
->untracked
);
2567 if (istate
->sparse_checkout_patterns
) {
2568 clear_pattern_list(istate
->sparse_checkout_patterns
);
2569 FREE_AND_NULL(istate
->sparse_checkout_patterns
);
2572 if (istate
->ce_mem_pool
) {
2573 mem_pool_discard(istate
->ce_mem_pool
, should_validate_cache_entries());
2574 FREE_AND_NULL(istate
->ce_mem_pool
);
2578 void discard_index(struct index_state
*istate
)
2580 release_index(istate
);
2581 index_state_init(istate
, istate
->repo
);
2585 * Validate the cache entries of this index.
2586 * All cache entries associated with this index
2587 * should have been allocated by the memory pool
2588 * associated with this index, or by a referenced
2591 void validate_cache_entries(const struct index_state
*istate
)
2595 if (!should_validate_cache_entries() ||!istate
|| !istate
->initialized
)
2598 for (i
= 0; i
< istate
->cache_nr
; i
++) {
2600 BUG("cache entry is not allocated from expected memory pool");
2601 } else if (!istate
->ce_mem_pool
||
2602 !mem_pool_contains(istate
->ce_mem_pool
, istate
->cache
[i
])) {
2603 if (!istate
->split_index
||
2604 !istate
->split_index
->base
||
2605 !istate
->split_index
->base
->ce_mem_pool
||
2606 !mem_pool_contains(istate
->split_index
->base
->ce_mem_pool
, istate
->cache
[i
])) {
2607 BUG("cache entry is not allocated from expected memory pool");
2612 if (istate
->split_index
)
2613 validate_cache_entries(istate
->split_index
->base
);
2616 int unmerged_index(const struct index_state
*istate
)
2619 for (i
= 0; i
< istate
->cache_nr
; i
++) {
2620 if (ce_stage(istate
->cache
[i
]))
2626 int repo_index_has_changes(struct repository
*repo
,
2630 struct index_state
*istate
= repo
->index
;
2631 struct object_id cmp
;
2635 cmp
= tree
->object
.oid
;
2636 if (tree
|| !repo_get_oid_tree(repo
, "HEAD", &cmp
)) {
2637 struct diff_options opt
;
2639 repo_diff_setup(repo
, &opt
);
2640 opt
.flags
.exit_with_status
= 1;
2642 opt
.flags
.quick
= 1;
2643 diff_setup_done(&opt
);
2644 do_diff_cache(&cmp
, &opt
);
2646 for (i
= 0; sb
&& i
< diff_queued_diff
.nr
; i
++) {
2648 strbuf_addch(sb
, ' ');
2649 strbuf_addstr(sb
, diff_queued_diff
.queue
[i
]->two
->path
);
2652 return opt
.flags
.has_changes
!= 0;
2654 /* TODO: audit for interaction with sparse-index. */
2655 ensure_full_index(istate
);
2656 for (i
= 0; sb
&& i
< istate
->cache_nr
; i
++) {
2658 strbuf_addch(sb
, ' ');
2659 strbuf_addstr(sb
, istate
->cache
[i
]->name
);
2661 return !!istate
->cache_nr
;
2665 static int write_index_ext_header(struct hashfile
*f
,
2666 git_hash_ctx
*eoie_f
,
2670 hashwrite_be32(f
, ext
);
2671 hashwrite_be32(f
, sz
);
2676 the_hash_algo
->update_fn(eoie_f
, &ext
, sizeof(ext
));
2677 the_hash_algo
->update_fn(eoie_f
, &sz
, sizeof(sz
));
2682 static void ce_smudge_racily_clean_entry(struct index_state
*istate
,
2683 struct cache_entry
*ce
)
2686 * The only thing we care about in this function is to smudge the
2687 * falsely clean entry due to touch-update-touch race, so we leave
2688 * everything else as they are. We are called for entries whose
2689 * ce_stat_data.sd_mtime match the index file mtime.
2691 * Note that this actually does not do much for gitlinks, for
2692 * which ce_match_stat_basic() always goes to the actual
2693 * contents. The caller checks with is_racy_timestamp() which
2694 * always says "no" for gitlinks, so we are not called for them ;-)
2698 if (lstat(ce
->name
, &st
) < 0)
2700 if (ce_match_stat_basic(ce
, &st
))
2702 if (ce_modified_check_fs(istate
, ce
, &st
)) {
2703 /* This is "racily clean"; smudge it. Note that this
2704 * is a tricky code. At first glance, it may appear
2705 * that it can break with this sequence:
2707 * $ echo xyzzy >frotz
2708 * $ git-update-index --add frotz
2711 * $ echo filfre >nitfol
2712 * $ git-update-index --add nitfol
2714 * but it does not. When the second update-index runs,
2715 * it notices that the entry "frotz" has the same timestamp
2716 * as index, and if we were to smudge it by resetting its
2717 * size to zero here, then the object name recorded
2718 * in index is the 6-byte file but the cached stat information
2719 * becomes zero --- which would then match what we would
2720 * obtain from the filesystem next time we stat("frotz").
2722 * However, the second update-index, before calling
2723 * this function, notices that the cached size is 6
2724 * bytes and what is on the filesystem is an empty
2725 * file, and never calls us, so the cached size information
2726 * for "frotz" stays 6 which does not match the filesystem.
2728 ce
->ce_stat_data
.sd_size
= 0;
2732 /* Copy miscellaneous fields but not the name */
2733 static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry
*ondisk
,
2734 struct cache_entry
*ce
)
2737 const unsigned hashsz
= the_hash_algo
->rawsz
;
2738 uint16_t *flagsp
= (uint16_t *)(ondisk
->data
+ hashsz
);
2740 ondisk
->ctime
.sec
= htonl(ce
->ce_stat_data
.sd_ctime
.sec
);
2741 ondisk
->mtime
.sec
= htonl(ce
->ce_stat_data
.sd_mtime
.sec
);
2742 ondisk
->ctime
.nsec
= htonl(ce
->ce_stat_data
.sd_ctime
.nsec
);
2743 ondisk
->mtime
.nsec
= htonl(ce
->ce_stat_data
.sd_mtime
.nsec
);
2744 ondisk
->dev
= htonl(ce
->ce_stat_data
.sd_dev
);
2745 ondisk
->ino
= htonl(ce
->ce_stat_data
.sd_ino
);
2746 ondisk
->mode
= htonl(ce
->ce_mode
);
2747 ondisk
->uid
= htonl(ce
->ce_stat_data
.sd_uid
);
2748 ondisk
->gid
= htonl(ce
->ce_stat_data
.sd_gid
);
2749 ondisk
->size
= htonl(ce
->ce_stat_data
.sd_size
);
2750 hashcpy(ondisk
->data
, ce
->oid
.hash
);
2752 flags
= ce
->ce_flags
& ~CE_NAMEMASK
;
2753 flags
|= (ce_namelen(ce
) >= CE_NAMEMASK
? CE_NAMEMASK
: ce_namelen(ce
));
2754 flagsp
[0] = htons(flags
);
2755 if (ce
->ce_flags
& CE_EXTENDED
) {
2756 flagsp
[1] = htons((ce
->ce_flags
& CE_EXTENDED_FLAGS
) >> 16);
2760 static int ce_write_entry(struct hashfile
*f
, struct cache_entry
*ce
,
2761 struct strbuf
*previous_name
, struct ondisk_cache_entry
*ondisk
)
2764 unsigned int saved_namelen
;
2765 int stripped_name
= 0;
2766 static unsigned char padding
[8] = { 0x00 };
2768 if (ce
->ce_flags
& CE_STRIP_NAME
) {
2769 saved_namelen
= ce_namelen(ce
);
2774 size
= offsetof(struct ondisk_cache_entry
,data
) + ondisk_data_size(ce
->ce_flags
, 0);
2776 if (!previous_name
) {
2777 int len
= ce_namelen(ce
);
2778 copy_cache_entry_to_ondisk(ondisk
, ce
);
2779 hashwrite(f
, ondisk
, size
);
2780 hashwrite(f
, ce
->name
, len
);
2781 hashwrite(f
, padding
, align_padding_size(size
, len
));
2783 int common
, to_remove
, prefix_size
;
2784 unsigned char to_remove_vi
[16];
2786 (ce
->name
[common
] &&
2787 common
< previous_name
->len
&&
2788 ce
->name
[common
] == previous_name
->buf
[common
]);
2790 ; /* still matching */
2791 to_remove
= previous_name
->len
- common
;
2792 prefix_size
= encode_varint(to_remove
, to_remove_vi
);
2794 copy_cache_entry_to_ondisk(ondisk
, ce
);
2795 hashwrite(f
, ondisk
, size
);
2796 hashwrite(f
, to_remove_vi
, prefix_size
);
2797 hashwrite(f
, ce
->name
+ common
, ce_namelen(ce
) - common
);
2798 hashwrite(f
, padding
, 1);
2800 strbuf_splice(previous_name
, common
, to_remove
,
2801 ce
->name
+ common
, ce_namelen(ce
) - common
);
2803 if (stripped_name
) {
2804 ce
->ce_namelen
= saved_namelen
;
2805 ce
->ce_flags
&= ~CE_STRIP_NAME
;
2812 * This function verifies if index_state has the correct sha1 of the
2813 * index file. Don't die if we have any other failure, just return 0.
2815 static int verify_index_from(const struct index_state
*istate
, const char *path
)
2820 unsigned char hash
[GIT_MAX_RAWSZ
];
2822 if (!istate
->initialized
)
2825 fd
= open(path
, O_RDONLY
);
2832 if (st
.st_size
< sizeof(struct cache_header
) + the_hash_algo
->rawsz
)
2835 n
= pread_in_full(fd
, hash
, the_hash_algo
->rawsz
, st
.st_size
- the_hash_algo
->rawsz
);
2836 if (n
!= the_hash_algo
->rawsz
)
2839 if (!hasheq(istate
->oid
.hash
, hash
))
2850 static int repo_verify_index(struct repository
*repo
)
2852 return verify_index_from(repo
->index
, repo
->index_file
);
2855 int has_racy_timestamp(struct index_state
*istate
)
2857 int entries
= istate
->cache_nr
;
2860 for (i
= 0; i
< entries
; i
++) {
2861 struct cache_entry
*ce
= istate
->cache
[i
];
2862 if (is_racy_timestamp(istate
, ce
))
2868 void repo_update_index_if_able(struct repository
*repo
,
2869 struct lock_file
*lockfile
)
2871 if ((repo
->index
->cache_changed
||
2872 has_racy_timestamp(repo
->index
)) &&
2873 repo_verify_index(repo
))
2874 write_locked_index(repo
->index
, lockfile
, COMMIT_LOCK
);
2876 rollback_lock_file(lockfile
);
2879 static int record_eoie(void)
2883 if (!git_config_get_bool("index.recordendofindexentries", &val
))
2887 * As a convenience, the end of index entries extension
2888 * used for threading is written by default if the user
2889 * explicitly requested threaded index reads.
2891 return !git_config_get_index_threads(&val
) && val
!= 1;
2894 static int record_ieot(void)
2898 if (!git_config_get_bool("index.recordoffsettable", &val
))
2902 * As a convenience, the offset table used for threading is
2903 * written by default if the user explicitly requested
2904 * threaded index reads.
2906 return !git_config_get_index_threads(&val
) && val
!= 1;
2909 enum write_extensions
{
2910 WRITE_NO_EXTENSION
= 0,
2911 WRITE_SPLIT_INDEX_EXTENSION
= 1<<0,
2912 WRITE_CACHE_TREE_EXTENSION
= 1<<1,
2913 WRITE_RESOLVE_UNDO_EXTENSION
= 1<<2,
2914 WRITE_UNTRACKED_CACHE_EXTENSION
= 1<<3,
2915 WRITE_FSMONITOR_EXTENSION
= 1<<4,
2917 #define WRITE_ALL_EXTENSIONS ((enum write_extensions)-1)
2920 * On success, `tempfile` is closed. If it is the temporary file
2921 * of a `struct lock_file`, we will therefore effectively perform
2922 * a 'close_lock_file_gently()`. Since that is an implementation
2923 * detail of lockfiles, callers of `do_write_index()` should not
2926 static int do_write_index(struct index_state
*istate
, struct tempfile
*tempfile
,
2927 enum write_extensions write_extensions
, unsigned flags
)
2929 uint64_t start
= getnanotime();
2931 git_hash_ctx
*eoie_c
= NULL
;
2932 struct cache_header hdr
;
2933 int i
, err
= 0, removed
, extended
, hdr_version
;
2934 struct cache_entry
**cache
= istate
->cache
;
2935 int entries
= istate
->cache_nr
;
2937 struct ondisk_cache_entry ondisk
;
2938 struct strbuf previous_name_buf
= STRBUF_INIT
, *previous_name
;
2939 int drop_cache_tree
= istate
->drop_cache_tree
;
2941 int csum_fsync_flag
;
2942 int ieot_entries
= 1;
2943 struct index_entry_offset_table
*ieot
= NULL
;
2945 struct repository
*r
= istate
->repo
;
2947 f
= hashfd(tempfile
->fd
, tempfile
->filename
.buf
);
2949 prepare_repo_settings(r
);
2950 f
->skip_hash
= r
->settings
.index_skip_hash
;
2952 for (i
= removed
= extended
= 0; i
< entries
; i
++) {
2953 if (cache
[i
]->ce_flags
& CE_REMOVE
)
2956 /* reduce extended entries if possible */
2957 cache
[i
]->ce_flags
&= ~CE_EXTENDED
;
2958 if (cache
[i
]->ce_flags
& CE_EXTENDED_FLAGS
) {
2960 cache
[i
]->ce_flags
|= CE_EXTENDED
;
2964 if (!istate
->version
)
2965 istate
->version
= get_index_format_default(r
);
2967 /* demote version 3 to version 2 when the latter suffices */
2968 if (istate
->version
== 3 || istate
->version
== 2)
2969 istate
->version
= extended
? 3 : 2;
2971 hdr_version
= istate
->version
;
2973 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
2974 hdr
.hdr_version
= htonl(hdr_version
);
2975 hdr
.hdr_entries
= htonl(entries
- removed
);
2977 hashwrite(f
, &hdr
, sizeof(hdr
));
2979 if (!HAVE_THREADS
|| git_config_get_index_threads(&nr_threads
))
2982 if (nr_threads
!= 1 && record_ieot()) {
2983 int ieot_blocks
, cpus
;
2986 * ensure default number of ieot blocks maps evenly to the
2987 * default number of threads that will process them leaving
2988 * room for the thread to load the index extensions.
2991 ieot_blocks
= istate
->cache_nr
/ THREAD_COST
;
2992 cpus
= online_cpus();
2993 if (ieot_blocks
> cpus
- 1)
2994 ieot_blocks
= cpus
- 1;
2996 ieot_blocks
= nr_threads
;
2997 if (ieot_blocks
> istate
->cache_nr
)
2998 ieot_blocks
= istate
->cache_nr
;
3002 * no reason to write out the IEOT extension if we don't
3003 * have enough blocks to utilize multi-threading
3005 if (ieot_blocks
> 1) {
3006 ieot
= xcalloc(1, sizeof(struct index_entry_offset_table
)
3007 + (ieot_blocks
* sizeof(struct index_entry_offset
)));
3008 ieot_entries
= DIV_ROUND_UP(entries
, ieot_blocks
);
3012 offset
= hashfile_total(f
);
3015 previous_name
= (hdr_version
== 4) ? &previous_name_buf
: NULL
;
3017 for (i
= 0; i
< entries
; i
++) {
3018 struct cache_entry
*ce
= cache
[i
];
3019 if (ce
->ce_flags
& CE_REMOVE
)
3021 if (!ce_uptodate(ce
) && is_racy_timestamp(istate
, ce
))
3022 ce_smudge_racily_clean_entry(istate
, ce
);
3023 if (is_null_oid(&ce
->oid
)) {
3024 static const char msg
[] = "cache entry has null sha1: %s";
3025 static int allow
= -1;
3028 allow
= git_env_bool("GIT_ALLOW_NULL_SHA1", 0);
3030 warning(msg
, ce
->name
);
3032 err
= error(msg
, ce
->name
);
3034 drop_cache_tree
= 1;
3036 if (ieot
&& i
&& (i
% ieot_entries
== 0)) {
3037 ieot
->entries
[ieot
->nr
].nr
= nr
;
3038 ieot
->entries
[ieot
->nr
].offset
= offset
;
3041 * If we have a V4 index, set the first byte to an invalid
3042 * character to ensure there is nothing common with the previous
3046 previous_name
->buf
[0] = 0;
3049 offset
= hashfile_total(f
);
3051 if (ce_write_entry(f
, ce
, previous_name
, (struct ondisk_cache_entry
*)&ondisk
) < 0)
3059 ieot
->entries
[ieot
->nr
].nr
= nr
;
3060 ieot
->entries
[ieot
->nr
].offset
= offset
;
3063 strbuf_release(&previous_name_buf
);
3070 offset
= hashfile_total(f
);
3073 * The extension headers must be hashed on their own for the
3074 * EOIE extension. Create a hashfile here to compute that hash.
3076 if (offset
&& record_eoie()) {
3077 CALLOC_ARRAY(eoie_c
, 1);
3078 the_hash_algo
->init_fn(eoie_c
);
3082 * Lets write out CACHE_EXT_INDEXENTRYOFFSETTABLE first so that we
3083 * can minimize the number of extensions we have to scan through to
3084 * find it during load. Write it out regardless of the
3085 * strip_extensions parameter as we need it when loading the shared
3089 struct strbuf sb
= STRBUF_INIT
;
3091 write_ieot_extension(&sb
, ieot
);
3092 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_INDEXENTRYOFFSETTABLE
, sb
.len
) < 0;
3093 hashwrite(f
, sb
.buf
, sb
.len
);
3094 strbuf_release(&sb
);
3100 if (write_extensions
& WRITE_SPLIT_INDEX_EXTENSION
&&
3101 istate
->split_index
) {
3102 struct strbuf sb
= STRBUF_INIT
;
3104 if (istate
->sparse_index
)
3105 die(_("cannot write split index for a sparse index"));
3107 err
= write_link_extension(&sb
, istate
) < 0 ||
3108 write_index_ext_header(f
, eoie_c
, CACHE_EXT_LINK
,
3110 hashwrite(f
, sb
.buf
, sb
.len
);
3111 strbuf_release(&sb
);
3115 if (write_extensions
& WRITE_CACHE_TREE_EXTENSION
&&
3116 !drop_cache_tree
&& istate
->cache_tree
) {
3117 struct strbuf sb
= STRBUF_INIT
;
3119 cache_tree_write(&sb
, istate
->cache_tree
);
3120 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_TREE
, sb
.len
) < 0;
3121 hashwrite(f
, sb
.buf
, sb
.len
);
3122 strbuf_release(&sb
);
3126 if (write_extensions
& WRITE_RESOLVE_UNDO_EXTENSION
&&
3127 istate
->resolve_undo
) {
3128 struct strbuf sb
= STRBUF_INIT
;
3130 resolve_undo_write(&sb
, istate
->resolve_undo
);
3131 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_RESOLVE_UNDO
,
3133 hashwrite(f
, sb
.buf
, sb
.len
);
3134 strbuf_release(&sb
);
3138 if (write_extensions
& WRITE_UNTRACKED_CACHE_EXTENSION
&&
3139 istate
->untracked
) {
3140 struct strbuf sb
= STRBUF_INIT
;
3142 write_untracked_extension(&sb
, istate
->untracked
);
3143 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_UNTRACKED
,
3145 hashwrite(f
, sb
.buf
, sb
.len
);
3146 strbuf_release(&sb
);
3150 if (write_extensions
& WRITE_FSMONITOR_EXTENSION
&&
3151 istate
->fsmonitor_last_update
) {
3152 struct strbuf sb
= STRBUF_INIT
;
3154 write_fsmonitor_extension(&sb
, istate
);
3155 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_FSMONITOR
, sb
.len
) < 0;
3156 hashwrite(f
, sb
.buf
, sb
.len
);
3157 strbuf_release(&sb
);
3161 if (istate
->sparse_index
) {
3162 if (write_index_ext_header(f
, eoie_c
, CACHE_EXT_SPARSE_DIRECTORIES
, 0) < 0)
3167 * CACHE_EXT_ENDOFINDEXENTRIES must be written as the last entry before the SHA1
3168 * so that it can be found and processed before all the index entries are
3169 * read. Write it out regardless of the strip_extensions parameter as we need it
3170 * when loading the shared index.
3173 struct strbuf sb
= STRBUF_INIT
;
3175 write_eoie_extension(&sb
, eoie_c
, offset
);
3176 err
= write_index_ext_header(f
, NULL
, CACHE_EXT_ENDOFINDEXENTRIES
, sb
.len
) < 0;
3177 hashwrite(f
, sb
.buf
, sb
.len
);
3178 strbuf_release(&sb
);
3183 csum_fsync_flag
= 0;
3184 if (!alternate_index_output
&& (flags
& COMMIT_LOCK
))
3185 csum_fsync_flag
= CSUM_FSYNC
;
3187 finalize_hashfile(f
, istate
->oid
.hash
, FSYNC_COMPONENT_INDEX
,
3188 CSUM_HASH_IN_STREAM
| csum_fsync_flag
);
3190 if (close_tempfile_gently(tempfile
)) {
3191 error(_("could not close '%s'"), get_tempfile_path(tempfile
));
3194 if (stat(get_tempfile_path(tempfile
), &st
))
3196 istate
->timestamp
.sec
= (unsigned int)st
.st_mtime
;
3197 istate
->timestamp
.nsec
= ST_MTIME_NSEC(st
);
3198 trace_performance_since(start
, "write index, changed mask = %x", istate
->cache_changed
);
3201 * TODO trace2: replace "the_repository" with the actual repo instance
3202 * that is associated with the given "istate".
3204 trace2_data_intmax("index", the_repository
, "write/version",
3206 trace2_data_intmax("index", the_repository
, "write/cache_nr",
3212 void set_alternate_index_output(const char *name
)
3214 alternate_index_output
= name
;
3217 static int commit_locked_index(struct lock_file
*lk
)
3219 if (alternate_index_output
)
3220 return commit_lock_file_to(lk
, alternate_index_output
);
3222 return commit_lock_file(lk
);
3225 static int do_write_locked_index(struct index_state
*istate
,
3226 struct lock_file
*lock
,
3228 enum write_extensions write_extensions
)
3231 int was_full
= istate
->sparse_index
== INDEX_EXPANDED
;
3233 ret
= convert_to_sparse(istate
, 0);
3236 warning(_("failed to convert to a sparse-index"));
3241 * TODO trace2: replace "the_repository" with the actual repo instance
3242 * that is associated with the given "istate".
3244 trace2_region_enter_printf("index", "do_write_index", the_repository
,
3245 "%s", get_lock_file_path(lock
));
3246 ret
= do_write_index(istate
, lock
->tempfile
, write_extensions
, flags
);
3247 trace2_region_leave_printf("index", "do_write_index", the_repository
,
3248 "%s", get_lock_file_path(lock
));
3251 ensure_full_index(istate
);
3255 if (flags
& COMMIT_LOCK
)
3256 ret
= commit_locked_index(lock
);
3258 ret
= close_lock_file_gently(lock
);
3260 run_hooks_l("post-index-change",
3261 istate
->updated_workdir
? "1" : "0",
3262 istate
->updated_skipworktree
? "1" : "0", NULL
);
3263 istate
->updated_workdir
= 0;
3264 istate
->updated_skipworktree
= 0;
3269 static int write_split_index(struct index_state
*istate
,
3270 struct lock_file
*lock
,
3274 prepare_to_write_split_index(istate
);
3275 ret
= do_write_locked_index(istate
, lock
, flags
, WRITE_ALL_EXTENSIONS
);
3276 finish_writing_split_index(istate
);
3280 static const char *shared_index_expire
= "2.weeks.ago";
3282 static unsigned long get_shared_index_expire_date(void)
3284 static unsigned long shared_index_expire_date
;
3285 static int shared_index_expire_date_prepared
;
3287 if (!shared_index_expire_date_prepared
) {
3288 git_config_get_expiry("splitindex.sharedindexexpire",
3289 &shared_index_expire
);
3290 shared_index_expire_date
= approxidate(shared_index_expire
);
3291 shared_index_expire_date_prepared
= 1;
3294 return shared_index_expire_date
;
3297 static int should_delete_shared_index(const char *shared_index_path
)
3300 unsigned long expiration
;
3302 /* Check timestamp */
3303 expiration
= get_shared_index_expire_date();
3306 if (stat(shared_index_path
, &st
))
3307 return error_errno(_("could not stat '%s'"), shared_index_path
);
3308 if (st
.st_mtime
> expiration
)
3314 static int clean_shared_index_files(const char *current_hex
)
3317 DIR *dir
= opendir(get_git_dir());
3320 return error_errno(_("unable to open git dir: %s"), get_git_dir());
3322 while ((de
= readdir(dir
)) != NULL
) {
3323 const char *sha1_hex
;
3324 const char *shared_index_path
;
3325 if (!skip_prefix(de
->d_name
, "sharedindex.", &sha1_hex
))
3327 if (!strcmp(sha1_hex
, current_hex
))
3329 shared_index_path
= git_path("%s", de
->d_name
);
3330 if (should_delete_shared_index(shared_index_path
) > 0 &&
3331 unlink(shared_index_path
))
3332 warning_errno(_("unable to unlink: %s"), shared_index_path
);
3339 static int write_shared_index(struct index_state
*istate
,
3340 struct tempfile
**temp
, unsigned flags
)
3342 struct split_index
*si
= istate
->split_index
;
3343 int ret
, was_full
= !istate
->sparse_index
;
3345 move_cache_to_base_index(istate
);
3346 convert_to_sparse(istate
, 0);
3348 trace2_region_enter_printf("index", "shared/do_write_index",
3349 the_repository
, "%s", get_tempfile_path(*temp
));
3350 ret
= do_write_index(si
->base
, *temp
, WRITE_NO_EXTENSION
, flags
);
3351 trace2_region_leave_printf("index", "shared/do_write_index",
3352 the_repository
, "%s", get_tempfile_path(*temp
));
3355 ensure_full_index(istate
);
3359 ret
= adjust_shared_perm(get_tempfile_path(*temp
));
3361 error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp
));
3364 ret
= rename_tempfile(temp
,
3365 git_path("sharedindex.%s", oid_to_hex(&si
->base
->oid
)));
3367 oidcpy(&si
->base_oid
, &si
->base
->oid
);
3368 clean_shared_index_files(oid_to_hex(&si
->base
->oid
));
3374 static const int default_max_percent_split_change
= 20;
3376 static int too_many_not_shared_entries(struct index_state
*istate
)
3378 int i
, not_shared
= 0;
3379 int max_split
= git_config_get_max_percent_split_change();
3381 switch (max_split
) {
3383 /* not or badly configured: use the default value */
3384 max_split
= default_max_percent_split_change
;
3387 return 1; /* 0% means always write a new shared index */
3389 return 0; /* 100% means never write a new shared index */
3391 break; /* just use the configured value */
3394 /* Count not shared entries */
3395 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3396 struct cache_entry
*ce
= istate
->cache
[i
];
3401 return (int64_t)istate
->cache_nr
* max_split
< (int64_t)not_shared
* 100;
3404 int write_locked_index(struct index_state
*istate
, struct lock_file
*lock
,
3407 int new_shared_index
, ret
, test_split_index_env
;
3408 struct split_index
*si
= istate
->split_index
;
3410 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
3411 cache_tree_verify(the_repository
, istate
);
3413 if ((flags
& SKIP_IF_UNCHANGED
) && !istate
->cache_changed
) {
3414 if (flags
& COMMIT_LOCK
)
3415 rollback_lock_file(lock
);
3419 if (istate
->fsmonitor_last_update
)
3420 fill_fsmonitor_bitmap(istate
);
3422 test_split_index_env
= git_env_bool("GIT_TEST_SPLIT_INDEX", 0);
3424 if ((!si
&& !test_split_index_env
) ||
3425 alternate_index_output
||
3426 (istate
->cache_changed
& ~EXTMASK
)) {
3427 ret
= do_write_locked_index(istate
, lock
, flags
,
3428 ~WRITE_SPLIT_INDEX_EXTENSION
);
3432 if (test_split_index_env
) {
3434 si
= init_split_index(istate
);
3435 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
3437 int v
= si
->base_oid
.hash
[0];
3439 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
3442 if (too_many_not_shared_entries(istate
))
3443 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
3445 new_shared_index
= istate
->cache_changed
& SPLIT_INDEX_ORDERED
;
3447 if (new_shared_index
) {
3448 struct tempfile
*temp
;
3451 /* Same initial permissions as the main .git/index file */
3452 temp
= mks_tempfile_sm(git_path("sharedindex_XXXXXX"), 0, 0666);
3454 ret
= do_write_locked_index(istate
, lock
, flags
,
3455 ~WRITE_SPLIT_INDEX_EXTENSION
);
3458 ret
= write_shared_index(istate
, &temp
, flags
);
3460 saved_errno
= errno
;
3461 if (is_tempfile_active(temp
))
3462 delete_tempfile(&temp
);
3463 errno
= saved_errno
;
3469 ret
= write_split_index(istate
, lock
, flags
);
3471 /* Freshen the shared index only if the split-index was written */
3472 if (!ret
&& !new_shared_index
&& !is_null_oid(&si
->base_oid
)) {
3473 const char *shared_index
= git_path("sharedindex.%s",
3474 oid_to_hex(&si
->base_oid
));
3475 freshen_shared_index(shared_index
, 1);
3479 if (flags
& COMMIT_LOCK
)
3480 rollback_lock_file(lock
);
3485 * Read the index file that is potentially unmerged into given
3486 * index_state, dropping any unmerged entries to stage #0 (potentially
3487 * resulting in a path appearing as both a file and a directory in the
3488 * index; the caller is responsible to clear out the extra entries
3489 * before writing the index to a tree). Returns true if the index is
3490 * unmerged. Callers who want to refuse to work from an unmerged
3491 * state can call this and check its return value, instead of calling
3494 int repo_read_index_unmerged(struct repository
*repo
)
3496 struct index_state
*istate
;
3500 repo_read_index(repo
);
3501 istate
= repo
->index
;
3502 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3503 struct cache_entry
*ce
= istate
->cache
[i
];
3504 struct cache_entry
*new_ce
;
3510 len
= ce_namelen(ce
);
3511 new_ce
= make_empty_cache_entry(istate
, len
);
3512 memcpy(new_ce
->name
, ce
->name
, len
);
3513 new_ce
->ce_flags
= create_ce_flags(0) | CE_CONFLICTED
;
3514 new_ce
->ce_namelen
= len
;
3515 new_ce
->ce_mode
= ce
->ce_mode
;
3516 if (add_index_entry(istate
, new_ce
, ADD_CACHE_SKIP_DFCHECK
))
3517 return error(_("%s: cannot drop to stage #0"),
3524 * Returns 1 if the path is an "other" path with respect to
3525 * the index; that is, the path is not mentioned in the index at all,
3526 * either as a file, a directory with some files in the index,
3527 * or as an unmerged entry.
3529 * We helpfully remove a trailing "/" from directories so that
3530 * the output of read_directory can be used as-is.
3532 int index_name_is_other(struct index_state
*istate
, const char *name
,
3536 if (namelen
&& name
[namelen
- 1] == '/')
3538 pos
= index_name_pos(istate
, name
, namelen
);
3540 return 0; /* exact match */
3542 if (pos
< istate
->cache_nr
) {
3543 struct cache_entry
*ce
= istate
->cache
[pos
];
3544 if (ce_namelen(ce
) == namelen
&&
3545 !memcmp(ce
->name
, name
, namelen
))
3546 return 0; /* Yup, this one exists unmerged */
3551 void *read_blob_data_from_index(struct index_state
*istate
,
3552 const char *path
, unsigned long *size
)
3556 enum object_type type
;
3560 pos
= index_name_pos(istate
, path
, len
);
3563 * We might be in the middle of a merge, in which
3564 * case we would read stage #2 (ours).
3568 (pos
< 0 && i
< istate
->cache_nr
&&
3569 !strcmp(istate
->cache
[i
]->name
, path
));
3571 if (ce_stage(istate
->cache
[i
]) == 2)
3576 data
= repo_read_object_file(the_repository
, &istate
->cache
[pos
]->oid
,
3578 if (!data
|| type
!= OBJ_BLOB
) {
3587 void stat_validity_clear(struct stat_validity
*sv
)
3589 FREE_AND_NULL(sv
->sd
);
3592 int stat_validity_check(struct stat_validity
*sv
, const char *path
)
3596 if (stat(path
, &st
) < 0)
3597 return sv
->sd
== NULL
;
3600 return S_ISREG(st
.st_mode
) && !match_stat_data(sv
->sd
, &st
);
3603 void stat_validity_update(struct stat_validity
*sv
, int fd
)
3607 if (fstat(fd
, &st
) < 0 || !S_ISREG(st
.st_mode
))
3608 stat_validity_clear(sv
);
3611 CALLOC_ARRAY(sv
->sd
, 1);
3612 fill_stat_data(sv
->sd
, &st
);
3616 void move_index_extensions(struct index_state
*dst
, struct index_state
*src
)
3618 dst
->untracked
= src
->untracked
;
3619 src
->untracked
= NULL
;
3620 dst
->cache_tree
= src
->cache_tree
;
3621 src
->cache_tree
= NULL
;
3624 struct cache_entry
*dup_cache_entry(const struct cache_entry
*ce
,
3625 struct index_state
*istate
)
3627 unsigned int size
= ce_size(ce
);
3628 int mem_pool_allocated
;
3629 struct cache_entry
*new_entry
= make_empty_cache_entry(istate
, ce_namelen(ce
));
3630 mem_pool_allocated
= new_entry
->mem_pool_allocated
;
3632 memcpy(new_entry
, ce
, size
);
3633 new_entry
->mem_pool_allocated
= mem_pool_allocated
;
3637 void discard_cache_entry(struct cache_entry
*ce
)
3639 if (ce
&& should_validate_cache_entries())
3640 memset(ce
, 0xCD, cache_entry_size(ce
->ce_namelen
));
3642 if (ce
&& ce
->mem_pool_allocated
)
3648 int should_validate_cache_entries(void)
3650 static int validate_index_cache_entries
= -1;
3652 if (validate_index_cache_entries
< 0) {
3653 if (getenv("GIT_TEST_VALIDATE_INDEX_CACHE_ENTRIES"))
3654 validate_index_cache_entries
= 1;
3656 validate_index_cache_entries
= 0;
3659 return validate_index_cache_entries
;
3662 #define EOIE_SIZE (4 + GIT_SHA1_RAWSZ) /* <4-byte offset> + <20-byte hash> */
3663 #define EOIE_SIZE_WITH_HEADER (4 + 4 + EOIE_SIZE) /* <4-byte signature> + <4-byte length> + EOIE_SIZE */
3665 static size_t read_eoie_extension(const char *mmap
, size_t mmap_size
)
3668 * The end of index entries (EOIE) extension is guaranteed to be last
3669 * so that it can be found by scanning backwards from the EOF.
3676 const char *index
, *eoie
;
3678 size_t offset
, src_offset
;
3679 unsigned char hash
[GIT_MAX_RAWSZ
];
3682 /* ensure we have an index big enough to contain an EOIE extension */
3683 if (mmap_size
< sizeof(struct cache_header
) + EOIE_SIZE_WITH_HEADER
+ the_hash_algo
->rawsz
)
3686 /* validate the extension signature */
3687 index
= eoie
= mmap
+ mmap_size
- EOIE_SIZE_WITH_HEADER
- the_hash_algo
->rawsz
;
3688 if (CACHE_EXT(index
) != CACHE_EXT_ENDOFINDEXENTRIES
)
3690 index
+= sizeof(uint32_t);
3692 /* validate the extension size */
3693 extsize
= get_be32(index
);
3694 if (extsize
!= EOIE_SIZE
)
3696 index
+= sizeof(uint32_t);
3699 * Validate the offset we're going to look for the first extension
3700 * signature is after the index header and before the eoie extension.
3702 offset
= get_be32(index
);
3703 if (mmap
+ offset
< mmap
+ sizeof(struct cache_header
))
3705 if (mmap
+ offset
>= eoie
)
3707 index
+= sizeof(uint32_t);
3710 * The hash is computed over extension types and their sizes (but not
3711 * their contents). E.g. if we have "TREE" extension that is N-bytes
3712 * long, "REUC" extension that is M-bytes long, followed by "EOIE",
3713 * then the hash would be:
3715 * SHA-1("TREE" + <binary representation of N> +
3716 * "REUC" + <binary representation of M>)
3718 src_offset
= offset
;
3719 the_hash_algo
->init_fn(&c
);
3720 while (src_offset
< mmap_size
- the_hash_algo
->rawsz
- EOIE_SIZE_WITH_HEADER
) {
3721 /* After an array of active_nr index entries,
3722 * there can be arbitrary number of extended
3723 * sections, each of which is prefixed with
3724 * extension name (4-byte) and section length
3725 * in 4-byte network byte order.
3728 memcpy(&extsize
, mmap
+ src_offset
+ 4, 4);
3729 extsize
= ntohl(extsize
);
3731 /* verify the extension size isn't so large it will wrap around */
3732 if (src_offset
+ 8 + extsize
< src_offset
)
3735 the_hash_algo
->update_fn(&c
, mmap
+ src_offset
, 8);
3738 src_offset
+= extsize
;
3740 the_hash_algo
->final_fn(hash
, &c
);
3741 if (!hasheq(hash
, (const unsigned char *)index
))
3744 /* Validate that the extension offsets returned us back to the eoie extension. */
3745 if (src_offset
!= mmap_size
- the_hash_algo
->rawsz
- EOIE_SIZE_WITH_HEADER
)
3751 static void write_eoie_extension(struct strbuf
*sb
, git_hash_ctx
*eoie_context
, size_t offset
)
3754 unsigned char hash
[GIT_MAX_RAWSZ
];
3757 put_be32(&buffer
, offset
);
3758 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3761 the_hash_algo
->final_fn(hash
, eoie_context
);
3762 strbuf_add(sb
, hash
, the_hash_algo
->rawsz
);
3765 #define IEOT_VERSION (1)
3767 static struct index_entry_offset_table
*read_ieot_extension(const char *mmap
, size_t mmap_size
, size_t offset
)
3769 const char *index
= NULL
;
3770 uint32_t extsize
, ext_version
;
3771 struct index_entry_offset_table
*ieot
;
3774 /* find the IEOT extension */
3777 while (offset
<= mmap_size
- the_hash_algo
->rawsz
- 8) {
3778 extsize
= get_be32(mmap
+ offset
+ 4);
3779 if (CACHE_EXT((mmap
+ offset
)) == CACHE_EXT_INDEXENTRYOFFSETTABLE
) {
3780 index
= mmap
+ offset
+ 4 + 4;
3789 /* validate the version is IEOT_VERSION */
3790 ext_version
= get_be32(index
);
3791 if (ext_version
!= IEOT_VERSION
) {
3792 error("invalid IEOT version %d", ext_version
);
3795 index
+= sizeof(uint32_t);
3797 /* extension size - version bytes / bytes per entry */
3798 nr
= (extsize
- sizeof(uint32_t)) / (sizeof(uint32_t) + sizeof(uint32_t));
3800 error("invalid number of IEOT entries %d", nr
);
3803 ieot
= xmalloc(sizeof(struct index_entry_offset_table
)
3804 + (nr
* sizeof(struct index_entry_offset
)));
3806 for (i
= 0; i
< nr
; i
++) {
3807 ieot
->entries
[i
].offset
= get_be32(index
);
3808 index
+= sizeof(uint32_t);
3809 ieot
->entries
[i
].nr
= get_be32(index
);
3810 index
+= sizeof(uint32_t);
3816 static void write_ieot_extension(struct strbuf
*sb
, struct index_entry_offset_table
*ieot
)
3822 put_be32(&buffer
, IEOT_VERSION
);
3823 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3826 for (i
= 0; i
< ieot
->nr
; i
++) {
3829 put_be32(&buffer
, ieot
->entries
[i
].offset
);
3830 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3833 put_be32(&buffer
, ieot
->entries
[i
].nr
);
3834 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3838 void prefetch_cache_entries(const struct index_state
*istate
,
3839 must_prefetch_predicate must_prefetch
)
3842 struct oid_array to_fetch
= OID_ARRAY_INIT
;
3844 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3845 struct cache_entry
*ce
= istate
->cache
[i
];
3847 if (S_ISGITLINK(ce
->ce_mode
) || !must_prefetch(ce
))
3849 if (!oid_object_info_extended(the_repository
, &ce
->oid
,
3851 OBJECT_INFO_FOR_PREFETCH
))
3853 oid_array_append(&to_fetch
, &ce
->oid
);
3855 promisor_remote_get_direct(the_repository
,
3856 to_fetch
.oid
, to_fetch
.nr
);
3857 oid_array_clear(&to_fetch
);