2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
7 #include "bulk-checkin.h"
15 #include "cache-tree.h"
18 #include "object-file.h"
19 #include "object-store-ll.h"
20 #include "oid-array.h"
23 #include "environment.h"
26 #include "name-hash.h"
27 #include "object-name.h"
29 #include "preload-index.h"
30 #include "read-cache.h"
31 #include "resolve-undo.h"
36 #include "split-index.h"
39 #include "fsmonitor.h"
40 #include "thread-utils.h"
42 #include "sparse-index.h"
43 #include "csum-file.h"
44 #include "promisor-remote.h"
47 /* Mask for the name length in ce_flags in the on-disk index */
49 #define CE_NAMEMASK (0x0fff)
53 * The first letter should be 'A'..'Z' for extensions that are not
54 * necessary for a correct operation (i.e. optimization data).
55 * When new extensions are added that _needs_ to be understood in
56 * order to correctly interpret the index file, pick character that
57 * is outside the range, to cause the reader to abort.
60 #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
61 #define CACHE_EXT_TREE 0x54524545 /* "TREE" */
62 #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
63 #define CACHE_EXT_LINK 0x6c696e6b /* "link" */
64 #define CACHE_EXT_UNTRACKED 0x554E5452 /* "UNTR" */
65 #define CACHE_EXT_FSMONITOR 0x46534D4E /* "FSMN" */
66 #define CACHE_EXT_ENDOFINDEXENTRIES 0x454F4945 /* "EOIE" */
67 #define CACHE_EXT_INDEXENTRYOFFSETTABLE 0x49454F54 /* "IEOT" */
68 #define CACHE_EXT_SPARSE_DIRECTORIES 0x73646972 /* "sdir" */
70 /* changes that can be kept in $GIT_DIR/index (basically all extensions) */
71 #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
72 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \
73 SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED | FSMONITOR_CHANGED)
77 * This is an estimate of the pathname length in the index. We use
78 * this for V4 index files to guess the un-deltafied size of the index
79 * in memory because of pathname deltafication. This is not required
80 * for V2/V3 index formats because their pathnames are not compressed.
81 * If the initial amount of memory set aside is not sufficient, the
82 * mem pool will allocate extra memory.
84 #define CACHE_ENTRY_PATH_LENGTH 80
86 enum index_search_mode
{
91 static inline struct cache_entry
*mem_pool__ce_alloc(struct mem_pool
*mem_pool
, size_t len
)
93 struct cache_entry
*ce
;
94 ce
= mem_pool_alloc(mem_pool
, cache_entry_size(len
));
95 ce
->mem_pool_allocated
= 1;
99 static inline struct cache_entry
*mem_pool__ce_calloc(struct mem_pool
*mem_pool
, size_t len
)
101 struct cache_entry
* ce
;
102 ce
= mem_pool_calloc(mem_pool
, 1, cache_entry_size(len
));
103 ce
->mem_pool_allocated
= 1;
107 static struct mem_pool
*find_mem_pool(struct index_state
*istate
)
109 struct mem_pool
**pool_ptr
;
111 if (istate
->split_index
&& istate
->split_index
->base
)
112 pool_ptr
= &istate
->split_index
->base
->ce_mem_pool
;
114 pool_ptr
= &istate
->ce_mem_pool
;
117 *pool_ptr
= xmalloc(sizeof(**pool_ptr
));
118 mem_pool_init(*pool_ptr
, 0);
124 static const char *alternate_index_output
;
126 static void set_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
128 if (S_ISSPARSEDIR(ce
->ce_mode
))
129 istate
->sparse_index
= INDEX_COLLAPSED
;
131 istate
->cache
[nr
] = ce
;
132 add_name_hash(istate
, ce
);
135 static void replace_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
137 struct cache_entry
*old
= istate
->cache
[nr
];
139 replace_index_entry_in_base(istate
, old
, ce
);
140 remove_name_hash(istate
, old
);
141 discard_cache_entry(old
);
142 ce
->ce_flags
&= ~CE_HASHED
;
143 set_index_entry(istate
, nr
, ce
);
144 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
145 mark_fsmonitor_invalid(istate
, ce
);
146 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
149 void rename_index_entry_at(struct index_state
*istate
, int nr
, const char *new_name
)
151 struct cache_entry
*old_entry
= istate
->cache
[nr
], *new_entry
, *refreshed
;
152 int namelen
= strlen(new_name
);
154 new_entry
= make_empty_cache_entry(istate
, namelen
);
155 copy_cache_entry(new_entry
, old_entry
);
156 new_entry
->ce_flags
&= ~CE_HASHED
;
157 new_entry
->ce_namelen
= namelen
;
158 new_entry
->index
= 0;
159 memcpy(new_entry
->name
, new_name
, namelen
+ 1);
161 cache_tree_invalidate_path(istate
, old_entry
->name
);
162 untracked_cache_remove_from_index(istate
, old_entry
->name
);
163 remove_index_entry_at(istate
, nr
);
166 * Refresh the new index entry. Using 'refresh_cache_entry' ensures
167 * we only update stat info if the entry is otherwise up-to-date (i.e.,
168 * the contents/mode haven't changed). This ensures that we reflect the
169 * 'ctime' of the rename in the index without (incorrectly) updating
170 * the cached stat info to reflect unstaged changes on disk.
172 refreshed
= refresh_cache_entry(istate
, new_entry
, CE_MATCH_REFRESH
);
173 if (refreshed
&& refreshed
!= new_entry
) {
174 add_index_entry(istate
, refreshed
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
175 discard_cache_entry(new_entry
);
177 add_index_entry(istate
, new_entry
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
181 * This only updates the "non-critical" parts of the directory
182 * cache, ie the parts that aren't tracked by GIT, and only used
183 * to validate the cache.
185 void fill_stat_cache_info(struct index_state
*istate
, struct cache_entry
*ce
, struct stat
*st
)
187 fill_stat_data(&ce
->ce_stat_data
, st
);
189 if (assume_unchanged
)
190 ce
->ce_flags
|= CE_VALID
;
192 if (S_ISREG(st
->st_mode
)) {
193 ce_mark_uptodate(ce
);
194 mark_fsmonitor_valid(istate
, ce
);
198 static unsigned int st_mode_from_ce(const struct cache_entry
*ce
)
200 extern int trust_executable_bit
, has_symlinks
;
202 switch (ce
->ce_mode
& S_IFMT
) {
204 return has_symlinks
? S_IFLNK
: (S_IFREG
| 0644);
206 return (ce
->ce_mode
& (trust_executable_bit
? 0755 : 0644)) | S_IFREG
;
208 return S_IFDIR
| 0755;
212 BUG("unsupported ce_mode: %o", ce
->ce_mode
);
216 int fake_lstat(const struct cache_entry
*ce
, struct stat
*st
)
218 fake_lstat_data(&ce
->ce_stat_data
, st
);
219 st
->st_mode
= st_mode_from_ce(ce
);
221 /* always succeed as lstat() replacement */
225 static int ce_compare_data(struct index_state
*istate
,
226 const struct cache_entry
*ce
,
230 int fd
= git_open_cloexec(ce
->name
, O_RDONLY
);
233 struct object_id oid
;
234 if (!index_fd(istate
, &oid
, fd
, st
, OBJ_BLOB
, ce
->name
, 0))
235 match
= !oideq(&oid
, &ce
->oid
);
236 /* index_fd() closed the file descriptor already */
241 static int ce_compare_link(const struct cache_entry
*ce
, size_t expected_size
)
246 enum object_type type
;
247 struct strbuf sb
= STRBUF_INIT
;
249 if (strbuf_readlink(&sb
, ce
->name
, expected_size
))
252 buffer
= repo_read_object_file(the_repository
, &ce
->oid
, &type
, &size
);
255 match
= memcmp(buffer
, sb
.buf
, size
);
262 static int ce_compare_gitlink(const struct cache_entry
*ce
)
264 struct object_id oid
;
267 * We don't actually require that the .git directory
268 * under GITLINK directory be a valid git directory. It
269 * might even be missing (in case nobody populated that
272 * If so, we consider it always to match.
274 if (resolve_gitlink_ref(ce
->name
, "HEAD", &oid
) < 0)
276 return !oideq(&oid
, &ce
->oid
);
279 static int ce_modified_check_fs(struct index_state
*istate
,
280 const struct cache_entry
*ce
,
283 switch (st
->st_mode
& S_IFMT
) {
285 if (ce_compare_data(istate
, ce
, st
))
289 if (ce_compare_link(ce
, xsize_t(st
->st_size
)))
293 if (S_ISGITLINK(ce
->ce_mode
))
294 return ce_compare_gitlink(ce
) ? DATA_CHANGED
: 0;
295 /* else fallthrough */
302 static int ce_match_stat_basic(const struct cache_entry
*ce
, struct stat
*st
)
304 unsigned int changed
= 0;
306 if (ce
->ce_flags
& CE_REMOVE
)
307 return MODE_CHANGED
| DATA_CHANGED
| TYPE_CHANGED
;
309 switch (ce
->ce_mode
& S_IFMT
) {
311 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
312 /* We consider only the owner x bit to be relevant for
315 if (trust_executable_bit
&&
316 (0100 & (ce
->ce_mode
^ st
->st_mode
)))
317 changed
|= MODE_CHANGED
;
320 if (!S_ISLNK(st
->st_mode
) &&
321 (has_symlinks
|| !S_ISREG(st
->st_mode
)))
322 changed
|= TYPE_CHANGED
;
325 /* We ignore most of the st_xxx fields for gitlinks */
326 if (!S_ISDIR(st
->st_mode
))
327 changed
|= TYPE_CHANGED
;
328 else if (ce_compare_gitlink(ce
))
329 changed
|= DATA_CHANGED
;
332 BUG("unsupported ce_mode: %o", ce
->ce_mode
);
335 changed
|= match_stat_data(&ce
->ce_stat_data
, st
);
337 /* Racily smudged entry? */
338 if (!ce
->ce_stat_data
.sd_size
) {
339 if (!is_empty_blob_sha1(ce
->oid
.hash
))
340 changed
|= DATA_CHANGED
;
346 static int is_racy_stat(const struct index_state
*istate
,
347 const struct stat_data
*sd
)
349 return (istate
->timestamp
.sec
&&
351 /* nanosecond timestamped files can also be racy! */
352 (istate
->timestamp
.sec
< sd
->sd_mtime
.sec
||
353 (istate
->timestamp
.sec
== sd
->sd_mtime
.sec
&&
354 istate
->timestamp
.nsec
<= sd
->sd_mtime
.nsec
))
356 istate
->timestamp
.sec
<= sd
->sd_mtime
.sec
361 int is_racy_timestamp(const struct index_state
*istate
,
362 const struct cache_entry
*ce
)
364 return (!S_ISGITLINK(ce
->ce_mode
) &&
365 is_racy_stat(istate
, &ce
->ce_stat_data
));
368 int match_stat_data_racy(const struct index_state
*istate
,
369 const struct stat_data
*sd
, struct stat
*st
)
371 if (is_racy_stat(istate
, sd
))
372 return MTIME_CHANGED
;
373 return match_stat_data(sd
, st
);
376 int ie_match_stat(struct index_state
*istate
,
377 const struct cache_entry
*ce
, struct stat
*st
,
378 unsigned int options
)
380 unsigned int changed
;
381 int ignore_valid
= options
& CE_MATCH_IGNORE_VALID
;
382 int ignore_skip_worktree
= options
& CE_MATCH_IGNORE_SKIP_WORKTREE
;
383 int assume_racy_is_modified
= options
& CE_MATCH_RACY_IS_DIRTY
;
384 int ignore_fsmonitor
= options
& CE_MATCH_IGNORE_FSMONITOR
;
386 if (!ignore_fsmonitor
)
387 refresh_fsmonitor(istate
);
389 * If it's marked as always valid in the index, it's
390 * valid whatever the checked-out copy says.
392 * skip-worktree has the same effect with higher precedence
394 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
396 if (!ignore_valid
&& (ce
->ce_flags
& CE_VALID
))
398 if (!ignore_fsmonitor
&& (ce
->ce_flags
& CE_FSMONITOR_VALID
))
402 * Intent-to-add entries have not been added, so the index entry
403 * by definition never matches what is in the work tree until it
404 * actually gets added.
406 if (ce_intent_to_add(ce
))
407 return DATA_CHANGED
| TYPE_CHANGED
| MODE_CHANGED
;
409 changed
= ce_match_stat_basic(ce
, st
);
412 * Within 1 second of this sequence:
413 * echo xyzzy >file && git-update-index --add file
414 * running this command:
416 * would give a falsely clean cache entry. The mtime and
417 * length match the cache, and other stat fields do not change.
419 * We could detect this at update-index time (the cache entry
420 * being registered/updated records the same time as "now")
421 * and delay the return from git-update-index, but that would
422 * effectively mean we can make at most one commit per second,
423 * which is not acceptable. Instead, we check cache entries
424 * whose mtime are the same as the index file timestamp more
425 * carefully than others.
427 if (!changed
&& is_racy_timestamp(istate
, ce
)) {
428 if (assume_racy_is_modified
)
429 changed
|= DATA_CHANGED
;
431 changed
|= ce_modified_check_fs(istate
, ce
, st
);
437 int ie_modified(struct index_state
*istate
,
438 const struct cache_entry
*ce
,
439 struct stat
*st
, unsigned int options
)
441 int changed
, changed_fs
;
443 changed
= ie_match_stat(istate
, ce
, st
, options
);
447 * If the mode or type has changed, there's no point in trying
448 * to refresh the entry - it's not going to match
450 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
454 * Immediately after read-tree or update-index --cacheinfo,
455 * the length field is zero, as we have never even read the
456 * lstat(2) information once, and we cannot trust DATA_CHANGED
457 * returned by ie_match_stat() which in turn was returned by
458 * ce_match_stat_basic() to signal that the filesize of the
459 * blob changed. We have to actually go to the filesystem to
460 * see if the contents match, and if so, should answer "unchanged".
462 * The logic does not apply to gitlinks, as ce_match_stat_basic()
463 * already has checked the actual HEAD from the filesystem in the
464 * subproject. If ie_match_stat() already said it is different,
465 * then we know it is.
467 if ((changed
& DATA_CHANGED
) &&
468 (S_ISGITLINK(ce
->ce_mode
) || ce
->ce_stat_data
.sd_size
!= 0))
471 changed_fs
= ce_modified_check_fs(istate
, ce
, st
);
473 return changed
| changed_fs
;
477 static int cache_name_stage_compare(const char *name1
, int len1
, int stage1
,
478 const char *name2
, int len2
, int stage2
)
482 cmp
= name_compare(name1
, len1
, name2
, len2
);
493 int cmp_cache_name_compare(const void *a_
, const void *b_
)
495 const struct cache_entry
*ce1
, *ce2
;
497 ce1
= *((const struct cache_entry
**)a_
);
498 ce2
= *((const struct cache_entry
**)b_
);
499 return cache_name_stage_compare(ce1
->name
, ce1
->ce_namelen
, ce_stage(ce1
),
500 ce2
->name
, ce2
->ce_namelen
, ce_stage(ce2
));
503 static int index_name_stage_pos(struct index_state
*istate
,
504 const char *name
, int namelen
,
506 enum index_search_mode search_mode
)
511 last
= istate
->cache_nr
;
512 while (last
> first
) {
513 int next
= first
+ ((last
- first
) >> 1);
514 struct cache_entry
*ce
= istate
->cache
[next
];
515 int cmp
= cache_name_stage_compare(name
, namelen
, stage
, ce
->name
, ce_namelen(ce
), ce_stage(ce
));
525 if (search_mode
== EXPAND_SPARSE
&& istate
->sparse_index
&&
527 /* Note: first <= istate->cache_nr */
528 struct cache_entry
*ce
= istate
->cache
[first
- 1];
531 * If we are in a sparse-index _and_ the entry before the
532 * insertion position is a sparse-directory entry that is
533 * an ancestor of 'name', then we need to expand the index
534 * and search again. This will only trigger once, because
535 * thereafter the index is fully expanded.
537 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
538 ce_namelen(ce
) < namelen
&&
539 !strncmp(name
, ce
->name
, ce_namelen(ce
))) {
540 ensure_full_index(istate
);
541 return index_name_stage_pos(istate
, name
, namelen
, stage
, search_mode
);
548 int index_name_pos(struct index_state
*istate
, const char *name
, int namelen
)
550 return index_name_stage_pos(istate
, name
, namelen
, 0, EXPAND_SPARSE
);
553 int index_name_pos_sparse(struct index_state
*istate
, const char *name
, int namelen
)
555 return index_name_stage_pos(istate
, name
, namelen
, 0, NO_EXPAND_SPARSE
);
558 int index_entry_exists(struct index_state
*istate
, const char *name
, int namelen
)
560 return index_name_stage_pos(istate
, name
, namelen
, 0, NO_EXPAND_SPARSE
) >= 0;
563 int remove_index_entry_at(struct index_state
*istate
, int pos
)
565 struct cache_entry
*ce
= istate
->cache
[pos
];
567 record_resolve_undo(istate
, ce
);
568 remove_name_hash(istate
, ce
);
569 save_or_free_index_entry(istate
, ce
);
570 istate
->cache_changed
|= CE_ENTRY_REMOVED
;
572 if (pos
>= istate
->cache_nr
)
574 MOVE_ARRAY(istate
->cache
+ pos
, istate
->cache
+ pos
+ 1,
575 istate
->cache_nr
- pos
);
580 * Remove all cache entries marked for removal, that is where
581 * CE_REMOVE is set in ce_flags. This is much more effective than
582 * calling remove_index_entry_at() for each entry to be removed.
584 void remove_marked_cache_entries(struct index_state
*istate
, int invalidate
)
586 struct cache_entry
**ce_array
= istate
->cache
;
589 for (i
= j
= 0; i
< istate
->cache_nr
; i
++) {
590 if (ce_array
[i
]->ce_flags
& CE_REMOVE
) {
592 cache_tree_invalidate_path(istate
,
594 untracked_cache_remove_from_index(istate
,
597 remove_name_hash(istate
, ce_array
[i
]);
598 save_or_free_index_entry(istate
, ce_array
[i
]);
601 ce_array
[j
++] = ce_array
[i
];
603 if (j
== istate
->cache_nr
)
605 istate
->cache_changed
|= CE_ENTRY_REMOVED
;
606 istate
->cache_nr
= j
;
609 int remove_file_from_index(struct index_state
*istate
, const char *path
)
611 int pos
= index_name_pos(istate
, path
, strlen(path
));
614 cache_tree_invalidate_path(istate
, path
);
615 untracked_cache_remove_from_index(istate
, path
);
616 while (pos
< istate
->cache_nr
&& !strcmp(istate
->cache
[pos
]->name
, path
))
617 remove_index_entry_at(istate
, pos
);
621 static int compare_name(struct cache_entry
*ce
, const char *path
, int namelen
)
623 return namelen
!= ce_namelen(ce
) || memcmp(path
, ce
->name
, namelen
);
626 static int index_name_pos_also_unmerged(struct index_state
*istate
,
627 const char *path
, int namelen
)
629 int pos
= index_name_pos(istate
, path
, namelen
);
630 struct cache_entry
*ce
;
635 /* maybe unmerged? */
637 if (pos
>= istate
->cache_nr
||
638 compare_name((ce
= istate
->cache
[pos
]), path
, namelen
))
641 /* order of preference: stage 2, 1, 3 */
642 if (ce_stage(ce
) == 1 && pos
+ 1 < istate
->cache_nr
&&
643 ce_stage((ce
= istate
->cache
[pos
+ 1])) == 2 &&
644 !compare_name(ce
, path
, namelen
))
649 static int different_name(struct cache_entry
*ce
, struct cache_entry
*alias
)
651 int len
= ce_namelen(ce
);
652 return ce_namelen(alias
) != len
|| memcmp(ce
->name
, alias
->name
, len
);
656 * If we add a filename that aliases in the cache, we will use the
657 * name that we already have - but we don't want to update the same
658 * alias twice, because that implies that there were actually two
659 * different files with aliasing names!
661 * So we use the CE_ADDED flag to verify that the alias was an old
662 * one before we accept it as
664 static struct cache_entry
*create_alias_ce(struct index_state
*istate
,
665 struct cache_entry
*ce
,
666 struct cache_entry
*alias
)
669 struct cache_entry
*new_entry
;
671 if (alias
->ce_flags
& CE_ADDED
)
672 die(_("will not add file alias '%s' ('%s' already exists in index)"),
673 ce
->name
, alias
->name
);
675 /* Ok, create the new entry using the name of the existing alias */
676 len
= ce_namelen(alias
);
677 new_entry
= make_empty_cache_entry(istate
, len
);
678 memcpy(new_entry
->name
, alias
->name
, len
);
679 copy_cache_entry(new_entry
, ce
);
680 save_or_free_index_entry(istate
, ce
);
684 void set_object_name_for_intent_to_add_entry(struct cache_entry
*ce
)
686 struct object_id oid
;
687 if (write_object_file("", 0, OBJ_BLOB
, &oid
))
688 die(_("cannot create an empty blob in the object database"));
689 oidcpy(&ce
->oid
, &oid
);
692 int add_to_index(struct index_state
*istate
, const char *path
, struct stat
*st
, int flags
)
694 int namelen
, was_same
;
695 mode_t st_mode
= st
->st_mode
;
696 struct cache_entry
*ce
, *alias
= NULL
;
697 unsigned ce_option
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
|CE_MATCH_RACY_IS_DIRTY
;
698 int verbose
= flags
& (ADD_CACHE_VERBOSE
| ADD_CACHE_PRETEND
);
699 int pretend
= flags
& ADD_CACHE_PRETEND
;
700 int intent_only
= flags
& ADD_CACHE_INTENT
;
701 int add_option
= (ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
|
702 (intent_only
? ADD_CACHE_NEW_ONLY
: 0));
703 unsigned hash_flags
= pretend
? 0 : HASH_WRITE_OBJECT
;
704 struct object_id oid
;
706 if (flags
& ADD_CACHE_RENORMALIZE
)
707 hash_flags
|= HASH_RENORMALIZE
;
709 if (!S_ISREG(st_mode
) && !S_ISLNK(st_mode
) && !S_ISDIR(st_mode
))
710 return error(_("%s: can only add regular files, symbolic links or git-directories"), path
);
712 namelen
= strlen(path
);
713 if (S_ISDIR(st_mode
)) {
714 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
715 return error(_("'%s' does not have a commit checked out"), path
);
716 while (namelen
&& path
[namelen
-1] == '/')
719 ce
= make_empty_cache_entry(istate
, namelen
);
720 memcpy(ce
->name
, path
, namelen
);
721 ce
->ce_namelen
= namelen
;
723 fill_stat_cache_info(istate
, ce
, st
);
725 ce
->ce_flags
|= CE_INTENT_TO_ADD
;
728 if (trust_executable_bit
&& has_symlinks
) {
729 ce
->ce_mode
= create_ce_mode(st_mode
);
731 /* If there is an existing entry, pick the mode bits and type
732 * from it, otherwise assume unexecutable regular file.
734 struct cache_entry
*ent
;
735 int pos
= index_name_pos_also_unmerged(istate
, path
, namelen
);
737 ent
= (0 <= pos
) ? istate
->cache
[pos
] : NULL
;
738 ce
->ce_mode
= ce_mode_from_stat(ent
, st_mode
);
741 /* When core.ignorecase=true, determine if a directory of the same name but differing
742 * case already exists within the Git repository. If it does, ensure the directory
743 * case of the file being added to the repository matches (is folded into) the existing
744 * entry's directory case.
747 adjust_dirname_case(istate
, ce
->name
);
749 if (!(flags
& ADD_CACHE_RENORMALIZE
)) {
750 alias
= index_file_exists(istate
, ce
->name
,
751 ce_namelen(ce
), ignore_case
);
754 !ie_match_stat(istate
, alias
, st
, ce_option
)) {
755 /* Nothing changed, really */
756 if (!S_ISGITLINK(alias
->ce_mode
))
757 ce_mark_uptodate(alias
);
758 alias
->ce_flags
|= CE_ADDED
;
760 discard_cache_entry(ce
);
765 if (index_path(istate
, &ce
->oid
, path
, st
, hash_flags
)) {
766 discard_cache_entry(ce
);
767 return error(_("unable to index file '%s'"), path
);
770 set_object_name_for_intent_to_add_entry(ce
);
772 if (ignore_case
&& alias
&& different_name(ce
, alias
))
773 ce
= create_alias_ce(istate
, ce
, alias
);
774 ce
->ce_flags
|= CE_ADDED
;
776 /* It was suspected to be racily clean, but it turns out to be Ok */
779 oideq(&alias
->oid
, &ce
->oid
) &&
780 ce
->ce_mode
== alias
->ce_mode
);
783 discard_cache_entry(ce
);
784 else if (add_index_entry(istate
, ce
, add_option
)) {
785 discard_cache_entry(ce
);
786 return error(_("unable to add '%s' to index"), path
);
788 if (verbose
&& !was_same
)
789 printf("add '%s'\n", path
);
793 int add_file_to_index(struct index_state
*istate
, const char *path
, int flags
)
796 if (lstat(path
, &st
))
797 die_errno(_("unable to stat '%s'"), path
);
798 return add_to_index(istate
, path
, &st
, flags
);
801 struct cache_entry
*make_empty_cache_entry(struct index_state
*istate
, size_t len
)
803 return mem_pool__ce_calloc(find_mem_pool(istate
), len
);
806 struct cache_entry
*make_empty_transient_cache_entry(size_t len
,
807 struct mem_pool
*ce_mem_pool
)
810 return mem_pool__ce_calloc(ce_mem_pool
, len
);
811 return xcalloc(1, cache_entry_size(len
));
814 enum verify_path_result
{
820 static enum verify_path_result
verify_path_internal(const char *, unsigned);
822 int verify_path(const char *path
, unsigned mode
)
824 return verify_path_internal(path
, mode
) == PATH_OK
;
827 struct cache_entry
*make_cache_entry(struct index_state
*istate
,
829 const struct object_id
*oid
,
832 unsigned int refresh_options
)
834 struct cache_entry
*ce
, *ret
;
837 if (verify_path_internal(path
, mode
) == PATH_INVALID
) {
838 error(_("invalid path '%s'"), path
);
843 ce
= make_empty_cache_entry(istate
, len
);
845 oidcpy(&ce
->oid
, oid
);
846 memcpy(ce
->name
, path
, len
);
847 ce
->ce_flags
= create_ce_flags(stage
);
848 ce
->ce_namelen
= len
;
849 ce
->ce_mode
= create_ce_mode(mode
);
851 ret
= refresh_cache_entry(istate
, ce
, refresh_options
);
853 discard_cache_entry(ce
);
857 struct cache_entry
*make_transient_cache_entry(unsigned int mode
,
858 const struct object_id
*oid
,
861 struct mem_pool
*ce_mem_pool
)
863 struct cache_entry
*ce
;
866 if (!verify_path(path
, mode
)) {
867 error(_("invalid path '%s'"), path
);
872 ce
= make_empty_transient_cache_entry(len
, ce_mem_pool
);
874 oidcpy(&ce
->oid
, oid
);
875 memcpy(ce
->name
, path
, len
);
876 ce
->ce_flags
= create_ce_flags(stage
);
877 ce
->ce_namelen
= len
;
878 ce
->ce_mode
= create_ce_mode(mode
);
884 * Chmod an index entry with either +x or -x.
886 * Returns -1 if the chmod for the particular cache entry failed (if it's
887 * not a regular file), -2 if an invalid flip argument is passed in, 0
890 int chmod_index_entry(struct index_state
*istate
, struct cache_entry
*ce
,
893 if (!S_ISREG(ce
->ce_mode
))
900 ce
->ce_mode
&= ~0111;
905 cache_tree_invalidate_path(istate
, ce
->name
);
906 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
907 mark_fsmonitor_invalid(istate
, ce
);
908 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
913 int ce_same_name(const struct cache_entry
*a
, const struct cache_entry
*b
)
915 int len
= ce_namelen(a
);
916 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
920 * We fundamentally don't like some paths: we don't want
921 * dot or dot-dot anywhere, and for obvious reasons don't
922 * want to recurse into ".git" either.
924 * Also, we don't want double slashes or slashes at the
925 * end that can make pathnames ambiguous.
927 static int verify_dotfile(const char *rest
, unsigned mode
)
930 * The first character was '.', but that
931 * has already been discarded, we now test
935 /* "." is not allowed */
936 if (*rest
== '\0' || is_dir_sep(*rest
))
941 * ".git" followed by NUL or slash is bad. Note that we match
942 * case-insensitively here, even if ignore_case is not set.
943 * This outlaws ".GIT" everywhere out of an abundance of caution,
944 * since there's really no good reason to allow it.
946 * Once we've seen ".git", we can also find ".gitmodules", etc (also
947 * case-insensitively).
951 if (rest
[1] != 'i' && rest
[1] != 'I')
953 if (rest
[2] != 't' && rest
[2] != 'T')
955 if (rest
[3] == '\0' || is_dir_sep(rest
[3]))
959 if (skip_iprefix(rest
, "modules", &rest
) &&
960 (*rest
== '\0' || is_dir_sep(*rest
)))
965 if (rest
[1] == '\0' || is_dir_sep(rest
[1]))
971 static enum verify_path_result
verify_path_internal(const char *path
,
976 if (has_dos_drive_prefix(path
))
979 if (!is_valid_path(path
))
990 if (is_hfs_dotgit(path
))
993 if (is_hfs_dotgitmodules(path
))
998 #if defined GIT_WINDOWS_NATIVE || defined __CYGWIN__
1000 return PATH_INVALID
;
1002 if (is_ntfs_dotgit(path
))
1003 return PATH_INVALID
;
1004 if (S_ISLNK(mode
)) {
1005 if (is_ntfs_dotgitmodules(path
))
1006 return PATH_INVALID
;
1011 if ((c
== '.' && !verify_dotfile(path
, mode
)) ||
1013 return PATH_INVALID
;
1015 * allow terminating directory separators for
1016 * sparse directory entries.
1019 return S_ISDIR(mode
) ? PATH_DIR_WITH_SEP
:
1021 } else if (c
== '\\' && protect_ntfs
) {
1022 if (is_ntfs_dotgit(path
))
1023 return PATH_INVALID
;
1024 if (S_ISLNK(mode
)) {
1025 if (is_ntfs_dotgitmodules(path
))
1026 return PATH_INVALID
;
1035 * Do we have another file that has the beginning components being a
1036 * proper superset of the name we're trying to add?
1038 static int has_file_name(struct index_state
*istate
,
1039 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
1042 int len
= ce_namelen(ce
);
1043 int stage
= ce_stage(ce
);
1044 const char *name
= ce
->name
;
1046 while (pos
< istate
->cache_nr
) {
1047 struct cache_entry
*p
= istate
->cache
[pos
++];
1049 if (len
>= ce_namelen(p
))
1051 if (memcmp(name
, p
->name
, len
))
1053 if (ce_stage(p
) != stage
)
1055 if (p
->name
[len
] != '/')
1057 if (p
->ce_flags
& CE_REMOVE
)
1062 remove_index_entry_at(istate
, --pos
);
1069 * Like strcmp(), but also return the offset of the first change.
1070 * If strings are equal, return the length.
1072 int strcmp_offset(const char *s1
, const char *s2
, size_t *first_change
)
1077 return strcmp(s1
, s2
);
1079 for (k
= 0; s1
[k
] == s2
[k
]; k
++)
1084 return (unsigned char)s1
[k
] - (unsigned char)s2
[k
];
1088 * Do we have another file with a pathname that is a proper
1089 * subset of the name we're trying to add?
1091 * That is, is there another file in the index with a path
1092 * that matches a sub-directory in the given entry?
1094 static int has_dir_name(struct index_state
*istate
,
1095 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
1098 int stage
= ce_stage(ce
);
1099 const char *name
= ce
->name
;
1100 const char *slash
= name
+ ce_namelen(ce
);
1105 * We are frequently called during an iteration on a sorted
1106 * list of pathnames and while building a new index. Therefore,
1107 * there is a high probability that this entry will eventually
1108 * be appended to the index, rather than inserted in the middle.
1109 * If we can confirm that, we can avoid binary searches on the
1110 * components of the pathname.
1112 * Compare the entry's full path with the last path in the index.
1114 if (istate
->cache_nr
> 0) {
1115 cmp_last
= strcmp_offset(name
,
1116 istate
->cache
[istate
->cache_nr
- 1]->name
,
1119 if (name
[len_eq_last
] != '/') {
1121 * The entry sorts AFTER the last one in the
1124 * If there were a conflict with "file", then our
1125 * name would start with "file/" and the last index
1126 * entry would start with "file" but not "file/".
1128 * The next character after common prefix is
1129 * not '/', so there can be no conflict.
1134 * The entry sorts AFTER the last one in the
1135 * index, and the next character after common
1138 * Either the last index entry is a file in
1139 * conflict with this entry, or it has a name
1140 * which sorts between this entry and the
1141 * potential conflicting file.
1143 * In both cases, we fall through to the loop
1144 * below and let the regular search code handle it.
1147 } else if (cmp_last
== 0) {
1149 * The entry exactly matches the last one in the
1150 * index, but because of multiple stage and CE_REMOVE
1151 * items, we fall through and let the regular search
1161 if (*--slash
== '/')
1163 if (slash
<= ce
->name
)
1168 pos
= index_name_stage_pos(istate
, name
, len
, stage
, EXPAND_SPARSE
);
1171 * Found one, but not so fast. This could
1172 * be a marker that says "I was here, but
1173 * I am being removed". Such an entry is
1174 * not a part of the resulting tree, and
1175 * it is Ok to have a directory at the same
1178 if (!(istate
->cache
[pos
]->ce_flags
& CE_REMOVE
)) {
1182 remove_index_entry_at(istate
, pos
);
1190 * Trivial optimization: if we find an entry that
1191 * already matches the sub-directory, then we know
1192 * we're ok, and we can exit.
1194 while (pos
< istate
->cache_nr
) {
1195 struct cache_entry
*p
= istate
->cache
[pos
];
1196 if ((ce_namelen(p
) <= len
) ||
1197 (p
->name
[len
] != '/') ||
1198 memcmp(p
->name
, name
, len
))
1199 break; /* not our subdirectory */
1200 if (ce_stage(p
) == stage
&& !(p
->ce_flags
& CE_REMOVE
))
1202 * p is at the same stage as our entry, and
1203 * is a subdirectory of what we are looking
1204 * at, so we cannot have conflicts at our
1205 * level or anything shorter.
1214 /* We may be in a situation where we already have path/file and path
1215 * is being added, or we already have path and path/file is being
1216 * added. Either one would result in a nonsense tree that has path
1217 * twice when git-write-tree tries to write it out. Prevent it.
1219 * If ok-to-replace is specified, we remove the conflicting entries
1220 * from the cache so the caller should recompute the insert position.
1221 * When this happens, we return non-zero.
1223 static int check_file_directory_conflict(struct index_state
*istate
,
1224 const struct cache_entry
*ce
,
1225 int pos
, int ok_to_replace
)
1230 * When ce is an "I am going away" entry, we allow it to be added
1232 if (ce
->ce_flags
& CE_REMOVE
)
1236 * We check if the path is a sub-path of a subsequent pathname
1237 * first, since removing those will not change the position
1240 retval
= has_file_name(istate
, ce
, pos
, ok_to_replace
);
1243 * Then check if the path might have a clashing sub-directory
1246 return retval
+ has_dir_name(istate
, ce
, pos
, ok_to_replace
);
1249 static int add_index_entry_with_check(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
1252 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
1253 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
1254 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
1255 int new_only
= option
& ADD_CACHE_NEW_ONLY
;
1258 * If this entry's path sorts after the last entry in the index,
1259 * we can avoid searching for it.
1261 if (istate
->cache_nr
> 0 &&
1262 strcmp(ce
->name
, istate
->cache
[istate
->cache_nr
- 1]->name
) > 0)
1263 pos
= index_pos_to_insert_pos(istate
->cache_nr
);
1265 pos
= index_name_stage_pos(istate
, ce
->name
, ce_namelen(ce
), ce_stage(ce
), EXPAND_SPARSE
);
1268 * Cache tree path should be invalidated only after index_name_stage_pos,
1269 * in case it expands a sparse index.
1271 if (!(option
& ADD_CACHE_KEEP_CACHE_TREE
))
1272 cache_tree_invalidate_path(istate
, ce
->name
);
1274 /* existing match? Just replace it. */
1277 replace_index_entry(istate
, pos
, ce
);
1282 if (!(option
& ADD_CACHE_KEEP_CACHE_TREE
))
1283 untracked_cache_add_to_index(istate
, ce
->name
);
1286 * Inserting a merged entry ("stage 0") into the index
1287 * will always replace all non-merged entries..
1289 if (pos
< istate
->cache_nr
&& ce_stage(ce
) == 0) {
1290 while (ce_same_name(istate
->cache
[pos
], ce
)) {
1292 if (!remove_index_entry_at(istate
, pos
))
1299 if (verify_path_internal(ce
->name
, ce
->ce_mode
) == PATH_INVALID
)
1300 return error(_("invalid path '%s'"), ce
->name
);
1302 if (!skip_df_check
&&
1303 check_file_directory_conflict(istate
, ce
, pos
, ok_to_replace
)) {
1305 return error(_("'%s' appears as both a file and as a directory"),
1307 pos
= index_name_stage_pos(istate
, ce
->name
, ce_namelen(ce
), ce_stage(ce
), EXPAND_SPARSE
);
1313 int add_index_entry(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
1317 if (option
& ADD_CACHE_JUST_APPEND
)
1318 pos
= istate
->cache_nr
;
1321 ret
= add_index_entry_with_check(istate
, ce
, option
);
1327 /* Make sure the array is big enough .. */
1328 ALLOC_GROW(istate
->cache
, istate
->cache_nr
+ 1, istate
->cache_alloc
);
1332 if (istate
->cache_nr
> pos
+ 1)
1333 MOVE_ARRAY(istate
->cache
+ pos
+ 1, istate
->cache
+ pos
,
1334 istate
->cache_nr
- pos
- 1);
1335 set_index_entry(istate
, pos
, ce
);
1336 istate
->cache_changed
|= CE_ENTRY_ADDED
;
1341 * "refresh" does not calculate a new sha1 file or bring the
1342 * cache up-to-date for mode/content changes. But what it
1343 * _does_ do is to "re-match" the stat information of a file
1344 * with the cache, so that you can refresh the cache for a
1345 * file that hasn't been changed but where the stat entry is
1348 * For example, you'd want to do this after doing a "git-read-tree",
1349 * to link up the stat cache details with the proper files.
1351 static struct cache_entry
*refresh_cache_ent(struct index_state
*istate
,
1352 struct cache_entry
*ce
,
1353 unsigned int options
, int *err
,
1359 struct cache_entry
*updated
;
1361 int refresh
= options
& CE_MATCH_REFRESH
;
1362 int ignore_valid
= options
& CE_MATCH_IGNORE_VALID
;
1363 int ignore_skip_worktree
= options
& CE_MATCH_IGNORE_SKIP_WORKTREE
;
1364 int ignore_missing
= options
& CE_MATCH_IGNORE_MISSING
;
1365 int ignore_fsmonitor
= options
& CE_MATCH_IGNORE_FSMONITOR
;
1367 if (!refresh
|| ce_uptodate(ce
))
1370 if (!ignore_fsmonitor
)
1371 refresh_fsmonitor(istate
);
1373 * CE_VALID or CE_SKIP_WORKTREE means the user promised us
1374 * that the change to the work tree does not matter and told
1377 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
)) {
1378 ce_mark_uptodate(ce
);
1381 if (!ignore_valid
&& (ce
->ce_flags
& CE_VALID
)) {
1382 ce_mark_uptodate(ce
);
1385 if (!ignore_fsmonitor
&& (ce
->ce_flags
& CE_FSMONITOR_VALID
)) {
1386 ce_mark_uptodate(ce
);
1390 if (has_symlink_leading_path(ce
->name
, ce_namelen(ce
))) {
1400 if (lstat(ce
->name
, &st
) < 0) {
1401 if (ignore_missing
&& errno
== ENOENT
)
1408 changed
= ie_match_stat(istate
, ce
, &st
, options
);
1410 *changed_ret
= changed
;
1413 * The path is unchanged. If we were told to ignore
1414 * valid bit, then we did the actual stat check and
1415 * found that the entry is unmodified. If the entry
1416 * is not marked VALID, this is the place to mark it
1417 * valid again, under "assume unchanged" mode.
1419 if (ignore_valid
&& assume_unchanged
&&
1420 !(ce
->ce_flags
& CE_VALID
))
1421 ; /* mark this one VALID again */
1424 * We do not mark the index itself "modified"
1425 * because CE_UPTODATE flag is in-core only;
1426 * we are not going to write this change out.
1428 if (!S_ISGITLINK(ce
->ce_mode
)) {
1429 ce_mark_uptodate(ce
);
1430 mark_fsmonitor_valid(istate
, ce
);
1438 if (ie_modified(istate
, ce
, &st
, options
)) {
1444 updated
= make_empty_cache_entry(istate
, ce_namelen(ce
));
1445 copy_cache_entry(updated
, ce
);
1446 memcpy(updated
->name
, ce
->name
, ce
->ce_namelen
+ 1);
1447 fill_stat_cache_info(istate
, updated
, &st
);
1449 * If ignore_valid is not set, we should leave CE_VALID bit
1450 * alone. Otherwise, paths marked with --no-assume-unchanged
1451 * (i.e. things to be edited) will reacquire CE_VALID bit
1452 * automatically, which is not really what we want.
1454 if (!ignore_valid
&& assume_unchanged
&&
1455 !(ce
->ce_flags
& CE_VALID
))
1456 updated
->ce_flags
&= ~CE_VALID
;
1458 /* istate->cache_changed is updated in the caller */
1462 static void show_file(const char * fmt
, const char * name
, int in_porcelain
,
1463 int * first
, const char *header_msg
)
1465 if (in_porcelain
&& *first
&& header_msg
) {
1466 printf("%s\n", header_msg
);
1472 int repo_refresh_and_write_index(struct repository
*repo
,
1473 unsigned int refresh_flags
,
1474 unsigned int write_flags
,
1476 const struct pathspec
*pathspec
,
1477 char *seen
, const char *header_msg
)
1479 struct lock_file lock_file
= LOCK_INIT
;
1482 fd
= repo_hold_locked_index(repo
, &lock_file
, 0);
1483 if (!gentle
&& fd
< 0)
1485 if (refresh_index(repo
->index
, refresh_flags
, pathspec
, seen
, header_msg
))
1487 if (0 <= fd
&& write_locked_index(repo
->index
, &lock_file
, COMMIT_LOCK
| write_flags
))
1493 int refresh_index(struct index_state
*istate
, unsigned int flags
,
1494 const struct pathspec
*pathspec
,
1495 char *seen
, const char *header_msg
)
1499 int really
= (flags
& REFRESH_REALLY
) != 0;
1500 int allow_unmerged
= (flags
& REFRESH_UNMERGED
) != 0;
1501 int quiet
= (flags
& REFRESH_QUIET
) != 0;
1502 int not_new
= (flags
& REFRESH_IGNORE_MISSING
) != 0;
1503 int ignore_submodules
= (flags
& REFRESH_IGNORE_SUBMODULES
) != 0;
1504 int ignore_skip_worktree
= (flags
& REFRESH_IGNORE_SKIP_WORKTREE
) != 0;
1506 int in_porcelain
= (flags
& REFRESH_IN_PORCELAIN
);
1507 unsigned int options
= (CE_MATCH_REFRESH
|
1508 (really
? CE_MATCH_IGNORE_VALID
: 0) |
1509 (not_new
? CE_MATCH_IGNORE_MISSING
: 0));
1510 const char *modified_fmt
;
1511 const char *deleted_fmt
;
1512 const char *typechange_fmt
;
1513 const char *added_fmt
;
1514 const char *unmerged_fmt
;
1515 struct progress
*progress
= NULL
;
1516 int t2_sum_lstat
= 0;
1517 int t2_sum_scan
= 0;
1519 if (flags
& REFRESH_PROGRESS
&& isatty(2))
1520 progress
= start_delayed_progress(_("Refresh index"),
1523 trace_performance_enter();
1524 modified_fmt
= in_porcelain
? "M\t%s\n" : "%s: needs update\n";
1525 deleted_fmt
= in_porcelain
? "D\t%s\n" : "%s: needs update\n";
1526 typechange_fmt
= in_porcelain
? "T\t%s\n" : "%s: needs update\n";
1527 added_fmt
= in_porcelain
? "A\t%s\n" : "%s: needs update\n";
1528 unmerged_fmt
= in_porcelain
? "U\t%s\n" : "%s: needs merge\n";
1530 * Use the multi-threaded preload_index() to refresh most of the
1531 * cache entries quickly then in the single threaded loop below,
1532 * we only have to do the special cases that are left.
1534 preload_index(istate
, pathspec
, 0);
1535 trace2_region_enter("index", "refresh", NULL
);
1537 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1538 struct cache_entry
*ce
, *new_entry
;
1539 int cache_errno
= 0;
1542 int t2_did_lstat
= 0;
1543 int t2_did_scan
= 0;
1545 ce
= istate
->cache
[i
];
1546 if (ignore_submodules
&& S_ISGITLINK(ce
->ce_mode
))
1548 if (ignore_skip_worktree
&& ce_skip_worktree(ce
))
1552 * If this entry is a sparse directory, then there isn't
1553 * any stat() information to update. Ignore the entry.
1555 if (S_ISSPARSEDIR(ce
->ce_mode
))
1558 if (pathspec
&& !ce_path_match(istate
, ce
, pathspec
, seen
))
1562 while ((i
< istate
->cache_nr
) &&
1563 ! strcmp(istate
->cache
[i
]->name
, ce
->name
))
1569 show_file(unmerged_fmt
, ce
->name
, in_porcelain
,
1570 &first
, header_msg
);
1578 new_entry
= refresh_cache_ent(istate
, ce
, options
,
1579 &cache_errno
, &changed
,
1580 &t2_did_lstat
, &t2_did_scan
);
1581 t2_sum_lstat
+= t2_did_lstat
;
1582 t2_sum_scan
+= t2_did_scan
;
1583 if (new_entry
== ce
)
1585 display_progress(progress
, i
);
1589 if (really
&& cache_errno
== EINVAL
) {
1590 /* If we are doing --really-refresh that
1591 * means the index is not valid anymore.
1593 ce
->ce_flags
&= ~CE_VALID
;
1594 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
1595 mark_fsmonitor_invalid(istate
, ce
);
1596 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
1601 if (cache_errno
== ENOENT
)
1603 else if (ce_intent_to_add(ce
))
1604 fmt
= added_fmt
; /* must be before other checks */
1605 else if (changed
& TYPE_CHANGED
)
1606 fmt
= typechange_fmt
;
1610 ce
->name
, in_porcelain
, &first
, header_msg
);
1615 replace_index_entry(istate
, i
, new_entry
);
1617 trace2_data_intmax("index", NULL
, "refresh/sum_lstat", t2_sum_lstat
);
1618 trace2_data_intmax("index", NULL
, "refresh/sum_scan", t2_sum_scan
);
1619 trace2_region_leave("index", "refresh", NULL
);
1620 display_progress(progress
, istate
->cache_nr
);
1621 stop_progress(&progress
);
1622 trace_performance_leave("refresh index");
1626 struct cache_entry
*refresh_cache_entry(struct index_state
*istate
,
1627 struct cache_entry
*ce
,
1628 unsigned int options
)
1630 return refresh_cache_ent(istate
, ce
, options
, NULL
, NULL
, NULL
, NULL
);
1634 /*****************************************************************
1636 *****************************************************************/
1638 #define INDEX_FORMAT_DEFAULT 3
1640 static unsigned int get_index_format_default(struct repository
*r
)
1642 char *envversion
= getenv("GIT_INDEX_VERSION");
1644 unsigned int version
= INDEX_FORMAT_DEFAULT
;
1647 prepare_repo_settings(r
);
1649 if (r
->settings
.index_version
>= 0)
1650 version
= r
->settings
.index_version
;
1651 if (version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< version
) {
1652 warning(_("index.version set, but the value is invalid.\n"
1653 "Using version %i"), INDEX_FORMAT_DEFAULT
);
1654 return INDEX_FORMAT_DEFAULT
;
1659 version
= strtoul(envversion
, &endp
, 10);
1661 version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< version
) {
1662 warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1663 "Using version %i"), INDEX_FORMAT_DEFAULT
);
1664 version
= INDEX_FORMAT_DEFAULT
;
1670 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
1671 * Again - this is just a (very strong in practice) heuristic that
1672 * the inode hasn't changed.
1674 * We save the fields in big-endian order to allow using the
1675 * index file over NFS transparently.
1677 struct ondisk_cache_entry
{
1678 struct cache_time ctime
;
1679 struct cache_time mtime
;
1687 * unsigned char hash[hashsz];
1689 * if (flags & CE_EXTENDED)
1692 unsigned char data
[GIT_MAX_RAWSZ
+ 2 * sizeof(uint16_t)];
1693 char name
[FLEX_ARRAY
];
1696 /* These are only used for v3 or lower */
1697 #define align_padding_size(size, len) ((size + (len) + 8) & ~7) - (size + len)
1698 #define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,data) + (len) + 8) & ~7)
1699 #define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)
1700 #define ondisk_data_size(flags, len) (the_hash_algo->rawsz + \
1701 ((flags & CE_EXTENDED) ? 2 : 1) * sizeof(uint16_t) + len)
1702 #define ondisk_data_size_max(len) (ondisk_data_size(CE_EXTENDED, len))
1703 #define ondisk_ce_size(ce) (ondisk_cache_entry_size(ondisk_data_size((ce)->ce_flags, ce_namelen(ce))))
1705 /* Allow fsck to force verification of the index checksum. */
1706 int verify_index_checksum
;
1708 /* Allow fsck to force verification of the cache entry order. */
1709 int verify_ce_order
;
1711 static int verify_hdr(const struct cache_header
*hdr
, unsigned long size
)
1714 unsigned char hash
[GIT_MAX_RAWSZ
];
1716 unsigned char *start
, *end
;
1717 struct object_id oid
;
1719 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
1720 return error(_("bad signature 0x%08x"), hdr
->hdr_signature
);
1721 hdr_version
= ntohl(hdr
->hdr_version
);
1722 if (hdr_version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< hdr_version
)
1723 return error(_("bad index version %d"), hdr_version
);
1725 if (!verify_index_checksum
)
1728 end
= (unsigned char *)hdr
+ size
;
1729 start
= end
- the_hash_algo
->rawsz
;
1730 oidread(&oid
, start
);
1731 if (oideq(&oid
, null_oid()))
1734 the_hash_algo
->init_fn(&c
);
1735 the_hash_algo
->update_fn(&c
, hdr
, size
- the_hash_algo
->rawsz
);
1736 the_hash_algo
->final_fn(hash
, &c
);
1737 if (!hasheq(hash
, start
))
1738 return error(_("bad index file sha1 signature"));
1742 static int read_index_extension(struct index_state
*istate
,
1743 const char *ext
, const char *data
, unsigned long sz
)
1745 switch (CACHE_EXT(ext
)) {
1746 case CACHE_EXT_TREE
:
1747 istate
->cache_tree
= cache_tree_read(data
, sz
);
1749 case CACHE_EXT_RESOLVE_UNDO
:
1750 istate
->resolve_undo
= resolve_undo_read(data
, sz
);
1752 case CACHE_EXT_LINK
:
1753 if (read_link_extension(istate
, data
, sz
))
1756 case CACHE_EXT_UNTRACKED
:
1757 istate
->untracked
= read_untracked_extension(data
, sz
);
1759 case CACHE_EXT_FSMONITOR
:
1760 read_fsmonitor_extension(istate
, data
, sz
);
1762 case CACHE_EXT_ENDOFINDEXENTRIES
:
1763 case CACHE_EXT_INDEXENTRYOFFSETTABLE
:
1764 /* already handled in do_read_index() */
1766 case CACHE_EXT_SPARSE_DIRECTORIES
:
1767 /* no content, only an indicator */
1768 istate
->sparse_index
= INDEX_COLLAPSED
;
1771 if (*ext
< 'A' || 'Z' < *ext
)
1772 return error(_("index uses %.4s extension, which we do not understand"),
1774 fprintf_ln(stderr
, _("ignoring %.4s extension"), ext
);
1781 * Parses the contents of the cache entry contained within the 'ondisk' buffer
1782 * into a new incore 'cache_entry'.
1784 * Note that 'char *ondisk' may not be aligned to a 4-byte address interval in
1785 * index v4, so we cannot cast it to 'struct ondisk_cache_entry *' and access
1786 * its members. Instead, we use the byte offsets of members within the struct to
1787 * identify where 'get_be16()', 'get_be32()', and 'oidread()' (which can all
1788 * read from an unaligned memory buffer) should read from the 'ondisk' buffer
1789 * into the corresponding incore 'cache_entry' members.
1791 static struct cache_entry
*create_from_disk(struct mem_pool
*ce_mem_pool
,
1792 unsigned int version
,
1794 unsigned long *ent_size
,
1795 const struct cache_entry
*previous_ce
)
1797 struct cache_entry
*ce
;
1800 const unsigned hashsz
= the_hash_algo
->rawsz
;
1801 const char *flagsp
= ondisk
+ offsetof(struct ondisk_cache_entry
, data
) + hashsz
;
1803 size_t copy_len
= 0;
1805 * Adjacent cache entries tend to share the leading paths, so it makes
1806 * sense to only store the differences in later entries. In the v4
1807 * on-disk format of the index, each on-disk cache entry stores the
1808 * number of bytes to be stripped from the end of the previous name,
1809 * and the bytes to append to the result, to come up with its name.
1811 int expand_name_field
= version
== 4;
1813 /* On-disk flags are just 16 bits */
1814 flags
= get_be16(flagsp
);
1815 len
= flags
& CE_NAMEMASK
;
1817 if (flags
& CE_EXTENDED
) {
1819 extended_flags
= get_be16(flagsp
+ sizeof(uint16_t)) << 16;
1820 /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1821 if (extended_flags
& ~CE_EXTENDED_FLAGS
)
1822 die(_("unknown index entry format 0x%08x"), extended_flags
);
1823 flags
|= extended_flags
;
1824 name
= (const char *)(flagsp
+ 2 * sizeof(uint16_t));
1827 name
= (const char *)(flagsp
+ sizeof(uint16_t));
1829 if (expand_name_field
) {
1830 const unsigned char *cp
= (const unsigned char *)name
;
1831 size_t strip_len
, previous_len
;
1833 /* If we're at the beginning of a block, ignore the previous name */
1834 strip_len
= decode_varint(&cp
);
1836 previous_len
= previous_ce
->ce_namelen
;
1837 if (previous_len
< strip_len
)
1838 die(_("malformed name field in the index, near path '%s'"),
1840 copy_len
= previous_len
- strip_len
;
1842 name
= (const char *)cp
;
1845 if (len
== CE_NAMEMASK
) {
1847 if (expand_name_field
)
1851 ce
= mem_pool__ce_alloc(ce_mem_pool
, len
);
1854 * NEEDSWORK: using 'offsetof()' is cumbersome and should be replaced
1855 * with something more akin to 'load_bitmap_entries_v1()'s use of
1856 * 'read_be16'/'read_be32'. For consistency with the corresponding
1857 * ondisk entry write function ('copy_cache_entry_to_ondisk()'), this
1858 * should be done at the same time as removing references to
1859 * 'ondisk_cache_entry' there.
1861 ce
->ce_stat_data
.sd_ctime
.sec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, ctime
)
1862 + offsetof(struct cache_time
, sec
));
1863 ce
->ce_stat_data
.sd_mtime
.sec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, mtime
)
1864 + offsetof(struct cache_time
, sec
));
1865 ce
->ce_stat_data
.sd_ctime
.nsec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, ctime
)
1866 + offsetof(struct cache_time
, nsec
));
1867 ce
->ce_stat_data
.sd_mtime
.nsec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, mtime
)
1868 + offsetof(struct cache_time
, nsec
));
1869 ce
->ce_stat_data
.sd_dev
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, dev
));
1870 ce
->ce_stat_data
.sd_ino
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, ino
));
1871 ce
->ce_mode
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, mode
));
1872 ce
->ce_stat_data
.sd_uid
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, uid
));
1873 ce
->ce_stat_data
.sd_gid
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, gid
));
1874 ce
->ce_stat_data
.sd_size
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, size
));
1875 ce
->ce_flags
= flags
& ~CE_NAMEMASK
;
1876 ce
->ce_namelen
= len
;
1878 oidread(&ce
->oid
, (const unsigned char *)ondisk
+ offsetof(struct ondisk_cache_entry
, data
));
1880 if (expand_name_field
) {
1882 memcpy(ce
->name
, previous_ce
->name
, copy_len
);
1883 memcpy(ce
->name
+ copy_len
, name
, len
+ 1 - copy_len
);
1884 *ent_size
= (name
- ((char *)ondisk
)) + len
+ 1 - copy_len
;
1886 memcpy(ce
->name
, name
, len
+ 1);
1887 *ent_size
= ondisk_ce_size(ce
);
1892 static void check_ce_order(struct index_state
*istate
)
1896 if (!verify_ce_order
)
1899 for (i
= 1; i
< istate
->cache_nr
; i
++) {
1900 struct cache_entry
*ce
= istate
->cache
[i
- 1];
1901 struct cache_entry
*next_ce
= istate
->cache
[i
];
1902 int name_compare
= strcmp(ce
->name
, next_ce
->name
);
1904 if (0 < name_compare
)
1905 die(_("unordered stage entries in index"));
1906 if (!name_compare
) {
1908 die(_("multiple stage entries for merged file '%s'"),
1910 if (ce_stage(ce
) > ce_stage(next_ce
))
1911 die(_("unordered stage entries for '%s'"),
1917 static void tweak_untracked_cache(struct index_state
*istate
)
1919 struct repository
*r
= the_repository
;
1921 prepare_repo_settings(r
);
1923 switch (r
->settings
.core_untracked_cache
) {
1924 case UNTRACKED_CACHE_REMOVE
:
1925 remove_untracked_cache(istate
);
1927 case UNTRACKED_CACHE_WRITE
:
1928 add_untracked_cache(istate
);
1930 case UNTRACKED_CACHE_KEEP
:
1932 * Either an explicit "core.untrackedCache=keep", the
1933 * default if "core.untrackedCache" isn't configured,
1934 * or a fallback on an unknown "core.untrackedCache"
1941 static void tweak_split_index(struct index_state
*istate
)
1943 switch (git_config_get_split_index()) {
1944 case -1: /* unset: do nothing */
1947 remove_split_index(istate
);
1950 add_split_index(istate
);
1952 default: /* unknown value: do nothing */
1957 static void post_read_index_from(struct index_state
*istate
)
1959 check_ce_order(istate
);
1960 tweak_untracked_cache(istate
);
1961 tweak_split_index(istate
);
1962 tweak_fsmonitor(istate
);
1965 static size_t estimate_cache_size_from_compressed(unsigned int entries
)
1967 return entries
* (sizeof(struct cache_entry
) + CACHE_ENTRY_PATH_LENGTH
);
1970 static size_t estimate_cache_size(size_t ondisk_size
, unsigned int entries
)
1972 long per_entry
= sizeof(struct cache_entry
) - sizeof(struct ondisk_cache_entry
);
1975 * Account for potential alignment differences.
1977 per_entry
+= align_padding_size(per_entry
, 0);
1978 return ondisk_size
+ entries
* per_entry
;
1981 struct index_entry_offset
1983 /* starting byte offset into index file, count of index entries in this block */
1987 struct index_entry_offset_table
1990 struct index_entry_offset entries
[FLEX_ARRAY
];
1993 static struct index_entry_offset_table
*read_ieot_extension(const char *mmap
, size_t mmap_size
, size_t offset
);
1994 static void write_ieot_extension(struct strbuf
*sb
, struct index_entry_offset_table
*ieot
);
1996 static size_t read_eoie_extension(const char *mmap
, size_t mmap_size
);
1997 static void write_eoie_extension(struct strbuf
*sb
, git_hash_ctx
*eoie_context
, size_t offset
);
1999 struct load_index_extensions
2002 struct index_state
*istate
;
2005 unsigned long src_offset
;
2008 static void *load_index_extensions(void *_data
)
2010 struct load_index_extensions
*p
= _data
;
2011 unsigned long src_offset
= p
->src_offset
;
2013 while (src_offset
<= p
->mmap_size
- the_hash_algo
->rawsz
- 8) {
2014 /* After an array of active_nr index entries,
2015 * there can be arbitrary number of extended
2016 * sections, each of which is prefixed with
2017 * extension name (4-byte) and section length
2018 * in 4-byte network byte order.
2020 uint32_t extsize
= get_be32(p
->mmap
+ src_offset
+ 4);
2021 if (read_index_extension(p
->istate
,
2022 p
->mmap
+ src_offset
,
2023 p
->mmap
+ src_offset
+ 8,
2025 munmap((void *)p
->mmap
, p
->mmap_size
);
2026 die(_("index file corrupt"));
2029 src_offset
+= extsize
;
2036 * A helper function that will load the specified range of cache entries
2037 * from the memory mapped file and add them to the given index.
2039 static unsigned long load_cache_entry_block(struct index_state
*istate
,
2040 struct mem_pool
*ce_mem_pool
, int offset
, int nr
, const char *mmap
,
2041 unsigned long start_offset
, const struct cache_entry
*previous_ce
)
2044 unsigned long src_offset
= start_offset
;
2046 for (i
= offset
; i
< offset
+ nr
; i
++) {
2047 struct cache_entry
*ce
;
2048 unsigned long consumed
;
2050 ce
= create_from_disk(ce_mem_pool
, istate
->version
,
2052 &consumed
, previous_ce
);
2053 set_index_entry(istate
, i
, ce
);
2055 src_offset
+= consumed
;
2058 return src_offset
- start_offset
;
2061 static unsigned long load_all_cache_entries(struct index_state
*istate
,
2062 const char *mmap
, size_t mmap_size
, unsigned long src_offset
)
2064 unsigned long consumed
;
2066 istate
->ce_mem_pool
= xmalloc(sizeof(*istate
->ce_mem_pool
));
2067 if (istate
->version
== 4) {
2068 mem_pool_init(istate
->ce_mem_pool
,
2069 estimate_cache_size_from_compressed(istate
->cache_nr
));
2071 mem_pool_init(istate
->ce_mem_pool
,
2072 estimate_cache_size(mmap_size
, istate
->cache_nr
));
2075 consumed
= load_cache_entry_block(istate
, istate
->ce_mem_pool
,
2076 0, istate
->cache_nr
, mmap
, src_offset
, NULL
);
2081 * Mostly randomly chosen maximum thread counts: we
2082 * cap the parallelism to online_cpus() threads, and we want
2083 * to have at least 10000 cache entries per thread for it to
2084 * be worth starting a thread.
2087 #define THREAD_COST (10000)
2089 struct load_cache_entries_thread_data
2092 struct index_state
*istate
;
2093 struct mem_pool
*ce_mem_pool
;
2096 struct index_entry_offset_table
*ieot
;
2097 int ieot_start
; /* starting index into the ieot array */
2098 int ieot_blocks
; /* count of ieot entries to process */
2099 unsigned long consumed
; /* return # of bytes in index file processed */
2103 * A thread proc to run the load_cache_entries() computation
2104 * across multiple background threads.
2106 static void *load_cache_entries_thread(void *_data
)
2108 struct load_cache_entries_thread_data
*p
= _data
;
2111 /* iterate across all ieot blocks assigned to this thread */
2112 for (i
= p
->ieot_start
; i
< p
->ieot_start
+ p
->ieot_blocks
; i
++) {
2113 p
->consumed
+= load_cache_entry_block(p
->istate
, p
->ce_mem_pool
,
2114 p
->offset
, p
->ieot
->entries
[i
].nr
, p
->mmap
, p
->ieot
->entries
[i
].offset
, NULL
);
2115 p
->offset
+= p
->ieot
->entries
[i
].nr
;
2120 static unsigned long load_cache_entries_threaded(struct index_state
*istate
, const char *mmap
, size_t mmap_size
,
2121 int nr_threads
, struct index_entry_offset_table
*ieot
)
2123 int i
, offset
, ieot_blocks
, ieot_start
, err
;
2124 struct load_cache_entries_thread_data
*data
;
2125 unsigned long consumed
= 0;
2127 /* a little sanity checking */
2128 if (istate
->name_hash_initialized
)
2129 BUG("the name hash isn't thread safe");
2131 istate
->ce_mem_pool
= xmalloc(sizeof(*istate
->ce_mem_pool
));
2132 mem_pool_init(istate
->ce_mem_pool
, 0);
2134 /* ensure we have no more threads than we have blocks to process */
2135 if (nr_threads
> ieot
->nr
)
2136 nr_threads
= ieot
->nr
;
2137 CALLOC_ARRAY(data
, nr_threads
);
2139 offset
= ieot_start
= 0;
2140 ieot_blocks
= DIV_ROUND_UP(ieot
->nr
, nr_threads
);
2141 for (i
= 0; i
< nr_threads
; i
++) {
2142 struct load_cache_entries_thread_data
*p
= &data
[i
];
2145 if (ieot_start
+ ieot_blocks
> ieot
->nr
)
2146 ieot_blocks
= ieot
->nr
- ieot_start
;
2152 p
->ieot_start
= ieot_start
;
2153 p
->ieot_blocks
= ieot_blocks
;
2155 /* create a mem_pool for each thread */
2157 for (j
= p
->ieot_start
; j
< p
->ieot_start
+ p
->ieot_blocks
; j
++)
2158 nr
+= p
->ieot
->entries
[j
].nr
;
2159 p
->ce_mem_pool
= xmalloc(sizeof(*istate
->ce_mem_pool
));
2160 if (istate
->version
== 4) {
2161 mem_pool_init(p
->ce_mem_pool
,
2162 estimate_cache_size_from_compressed(nr
));
2164 mem_pool_init(p
->ce_mem_pool
,
2165 estimate_cache_size(mmap_size
, nr
));
2168 err
= pthread_create(&p
->pthread
, NULL
, load_cache_entries_thread
, p
);
2170 die(_("unable to create load_cache_entries thread: %s"), strerror(err
));
2172 /* increment by the number of cache entries in the ieot block being processed */
2173 for (j
= 0; j
< ieot_blocks
; j
++)
2174 offset
+= ieot
->entries
[ieot_start
+ j
].nr
;
2175 ieot_start
+= ieot_blocks
;
2178 for (i
= 0; i
< nr_threads
; i
++) {
2179 struct load_cache_entries_thread_data
*p
= &data
[i
];
2181 err
= pthread_join(p
->pthread
, NULL
);
2183 die(_("unable to join load_cache_entries thread: %s"), strerror(err
));
2184 mem_pool_combine(istate
->ce_mem_pool
, p
->ce_mem_pool
);
2185 consumed
+= p
->consumed
;
2193 static void set_new_index_sparsity(struct index_state
*istate
)
2196 * If the index's repo exists, mark it sparse according to
2199 prepare_repo_settings(istate
->repo
);
2200 if (!istate
->repo
->settings
.command_requires_full_index
&&
2201 is_sparse_index_allowed(istate
, 0))
2202 istate
->sparse_index
= 1;
2205 /* remember to discard_cache() before reading a different cache! */
2206 int do_read_index(struct index_state
*istate
, const char *path
, int must_exist
)
2210 unsigned long src_offset
;
2211 const struct cache_header
*hdr
;
2214 struct load_index_extensions p
;
2215 size_t extension_offset
= 0;
2216 int nr_threads
, cpus
;
2217 struct index_entry_offset_table
*ieot
= NULL
;
2219 if (istate
->initialized
)
2220 return istate
->cache_nr
;
2222 istate
->timestamp
.sec
= 0;
2223 istate
->timestamp
.nsec
= 0;
2224 fd
= open(path
, O_RDONLY
);
2226 if (!must_exist
&& errno
== ENOENT
) {
2227 set_new_index_sparsity(istate
);
2228 istate
->initialized
= 1;
2231 die_errno(_("%s: index file open failed"), path
);
2235 die_errno(_("%s: cannot stat the open index"), path
);
2237 mmap_size
= xsize_t(st
.st_size
);
2238 if (mmap_size
< sizeof(struct cache_header
) + the_hash_algo
->rawsz
)
2239 die(_("%s: index file smaller than expected"), path
);
2241 mmap
= xmmap_gently(NULL
, mmap_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2242 if (mmap
== MAP_FAILED
)
2243 die_errno(_("%s: unable to map index file%s"), path
,
2247 hdr
= (const struct cache_header
*)mmap
;
2248 if (verify_hdr(hdr
, mmap_size
) < 0)
2251 oidread(&istate
->oid
, (const unsigned char *)hdr
+ mmap_size
- the_hash_algo
->rawsz
);
2252 istate
->version
= ntohl(hdr
->hdr_version
);
2253 istate
->cache_nr
= ntohl(hdr
->hdr_entries
);
2254 istate
->cache_alloc
= alloc_nr(istate
->cache_nr
);
2255 CALLOC_ARRAY(istate
->cache
, istate
->cache_alloc
);
2256 istate
->initialized
= 1;
2260 p
.mmap_size
= mmap_size
;
2262 src_offset
= sizeof(*hdr
);
2264 if (git_config_get_index_threads(&nr_threads
))
2267 /* TODO: does creating more threads than cores help? */
2269 nr_threads
= istate
->cache_nr
/ THREAD_COST
;
2270 cpus
= online_cpus();
2271 if (nr_threads
> cpus
)
2278 if (nr_threads
> 1) {
2279 extension_offset
= read_eoie_extension(mmap
, mmap_size
);
2280 if (extension_offset
) {
2283 p
.src_offset
= extension_offset
;
2284 err
= pthread_create(&p
.pthread
, NULL
, load_index_extensions
, &p
);
2286 die(_("unable to create load_index_extensions thread: %s"), strerror(err
));
2293 * Locate and read the index entry offset table so that we can use it
2294 * to multi-thread the reading of the cache entries.
2296 if (extension_offset
&& nr_threads
> 1)
2297 ieot
= read_ieot_extension(mmap
, mmap_size
, extension_offset
);
2300 src_offset
+= load_cache_entries_threaded(istate
, mmap
, mmap_size
, nr_threads
, ieot
);
2303 src_offset
+= load_all_cache_entries(istate
, mmap
, mmap_size
, src_offset
);
2306 istate
->timestamp
.sec
= st
.st_mtime
;
2307 istate
->timestamp
.nsec
= ST_MTIME_NSEC(st
);
2309 /* if we created a thread, join it otherwise load the extensions on the primary thread */
2310 if (extension_offset
) {
2311 int ret
= pthread_join(p
.pthread
, NULL
);
2313 die(_("unable to join load_index_extensions thread: %s"), strerror(ret
));
2315 p
.src_offset
= src_offset
;
2316 load_index_extensions(&p
);
2318 munmap((void *)mmap
, mmap_size
);
2321 * TODO trace2: replace "the_repository" with the actual repo instance
2322 * that is associated with the given "istate".
2324 trace2_data_intmax("index", the_repository
, "read/version",
2326 trace2_data_intmax("index", the_repository
, "read/cache_nr",
2330 * If the command explicitly requires a full index, force it
2331 * to be full. Otherwise, correct the sparsity based on repository
2332 * settings and other properties of the index (if necessary).
2334 prepare_repo_settings(istate
->repo
);
2335 if (istate
->repo
->settings
.command_requires_full_index
)
2336 ensure_full_index(istate
);
2338 ensure_correct_sparsity(istate
);
2340 return istate
->cache_nr
;
2343 munmap((void *)mmap
, mmap_size
);
2344 die(_("index file corrupt"));
2348 * Signal that the shared index is used by updating its mtime.
2350 * This way, shared index can be removed if they have not been used
2353 static void freshen_shared_index(const char *shared_index
, int warn
)
2355 if (!check_and_freshen_file(shared_index
, 1) && warn
)
2356 warning(_("could not freshen shared index '%s'"), shared_index
);
2359 int read_index_from(struct index_state
*istate
, const char *path
,
2362 struct split_index
*split_index
;
2367 /* istate->initialized covers both .git/index and .git/sharedindex.xxx */
2368 if (istate
->initialized
)
2369 return istate
->cache_nr
;
2372 * TODO trace2: replace "the_repository" with the actual repo instance
2373 * that is associated with the given "istate".
2375 trace2_region_enter_printf("index", "do_read_index", the_repository
,
2377 trace_performance_enter();
2378 ret
= do_read_index(istate
, path
, 0);
2379 trace_performance_leave("read cache %s", path
);
2380 trace2_region_leave_printf("index", "do_read_index", the_repository
,
2383 split_index
= istate
->split_index
;
2384 if (!split_index
|| is_null_oid(&split_index
->base_oid
)) {
2385 post_read_index_from(istate
);
2389 trace_performance_enter();
2390 if (split_index
->base
)
2391 release_index(split_index
->base
);
2393 ALLOC_ARRAY(split_index
->base
, 1);
2394 index_state_init(split_index
->base
, istate
->repo
);
2396 base_oid_hex
= oid_to_hex(&split_index
->base_oid
);
2397 base_path
= xstrfmt("%s/sharedindex.%s", gitdir
, base_oid_hex
);
2398 if (file_exists(base_path
)) {
2399 trace2_region_enter_printf("index", "shared/do_read_index",
2400 the_repository
, "%s", base_path
);
2402 ret
= do_read_index(split_index
->base
, base_path
, 0);
2403 trace2_region_leave_printf("index", "shared/do_read_index",
2404 the_repository
, "%s", base_path
);
2406 char *path_copy
= xstrdup(path
);
2407 char *base_path2
= xstrfmt("%s/sharedindex.%s",
2408 dirname(path_copy
), base_oid_hex
);
2410 trace2_region_enter_printf("index", "shared/do_read_index",
2411 the_repository
, "%s", base_path2
);
2412 ret
= do_read_index(split_index
->base
, base_path2
, 1);
2413 trace2_region_leave_printf("index", "shared/do_read_index",
2414 the_repository
, "%s", base_path2
);
2417 if (!oideq(&split_index
->base_oid
, &split_index
->base
->oid
))
2418 die(_("broken index, expect %s in %s, got %s"),
2419 base_oid_hex
, base_path
,
2420 oid_to_hex(&split_index
->base
->oid
));
2422 freshen_shared_index(base_path
, 0);
2423 merge_base_index(istate
);
2424 post_read_index_from(istate
);
2425 trace_performance_leave("read cache %s", base_path
);
2430 int is_index_unborn(struct index_state
*istate
)
2432 return (!istate
->cache_nr
&& !istate
->timestamp
.sec
);
2435 void index_state_init(struct index_state
*istate
, struct repository
*r
)
2437 struct index_state blank
= INDEX_STATE_INIT(r
);
2438 memcpy(istate
, &blank
, sizeof(*istate
));
2441 void release_index(struct index_state
*istate
)
2444 * Cache entries in istate->cache[] should have been allocated
2445 * from the memory pool associated with this index, or from an
2446 * associated split_index. There is no need to free individual
2447 * cache entries. validate_cache_entries can detect when this
2448 * assertion does not hold.
2450 validate_cache_entries(istate
);
2452 resolve_undo_clear_index(istate
);
2453 free_name_hash(istate
);
2454 cache_tree_free(&(istate
->cache_tree
));
2455 free(istate
->fsmonitor_last_update
);
2456 free(istate
->cache
);
2457 discard_split_index(istate
);
2458 free_untracked_cache(istate
->untracked
);
2460 if (istate
->sparse_checkout_patterns
) {
2461 clear_pattern_list(istate
->sparse_checkout_patterns
);
2462 FREE_AND_NULL(istate
->sparse_checkout_patterns
);
2465 if (istate
->ce_mem_pool
) {
2466 mem_pool_discard(istate
->ce_mem_pool
, should_validate_cache_entries());
2467 FREE_AND_NULL(istate
->ce_mem_pool
);
2471 void discard_index(struct index_state
*istate
)
2473 release_index(istate
);
2474 index_state_init(istate
, istate
->repo
);
2478 * Validate the cache entries of this index.
2479 * All cache entries associated with this index
2480 * should have been allocated by the memory pool
2481 * associated with this index, or by a referenced
2484 void validate_cache_entries(const struct index_state
*istate
)
2488 if (!should_validate_cache_entries() ||!istate
|| !istate
->initialized
)
2491 for (i
= 0; i
< istate
->cache_nr
; i
++) {
2493 BUG("cache entry is not allocated from expected memory pool");
2494 } else if (!istate
->ce_mem_pool
||
2495 !mem_pool_contains(istate
->ce_mem_pool
, istate
->cache
[i
])) {
2496 if (!istate
->split_index
||
2497 !istate
->split_index
->base
||
2498 !istate
->split_index
->base
->ce_mem_pool
||
2499 !mem_pool_contains(istate
->split_index
->base
->ce_mem_pool
, istate
->cache
[i
])) {
2500 BUG("cache entry is not allocated from expected memory pool");
2505 if (istate
->split_index
)
2506 validate_cache_entries(istate
->split_index
->base
);
2509 int unmerged_index(const struct index_state
*istate
)
2512 for (i
= 0; i
< istate
->cache_nr
; i
++) {
2513 if (ce_stage(istate
->cache
[i
]))
2519 int repo_index_has_changes(struct repository
*repo
,
2523 struct index_state
*istate
= repo
->index
;
2524 struct object_id cmp
;
2528 cmp
= tree
->object
.oid
;
2529 if (tree
|| !repo_get_oid_tree(repo
, "HEAD", &cmp
)) {
2530 struct diff_options opt
;
2532 repo_diff_setup(repo
, &opt
);
2533 opt
.flags
.exit_with_status
= 1;
2535 opt
.flags
.quick
= 1;
2536 diff_setup_done(&opt
);
2537 do_diff_cache(&cmp
, &opt
);
2539 for (i
= 0; sb
&& i
< diff_queued_diff
.nr
; i
++) {
2541 strbuf_addch(sb
, ' ');
2542 strbuf_addstr(sb
, diff_queued_diff
.queue
[i
]->two
->path
);
2545 return opt
.flags
.has_changes
!= 0;
2547 /* TODO: audit for interaction with sparse-index. */
2548 ensure_full_index(istate
);
2549 for (i
= 0; sb
&& i
< istate
->cache_nr
; i
++) {
2551 strbuf_addch(sb
, ' ');
2552 strbuf_addstr(sb
, istate
->cache
[i
]->name
);
2554 return !!istate
->cache_nr
;
2558 static int write_index_ext_header(struct hashfile
*f
,
2559 git_hash_ctx
*eoie_f
,
2563 hashwrite_be32(f
, ext
);
2564 hashwrite_be32(f
, sz
);
2569 the_hash_algo
->update_fn(eoie_f
, &ext
, sizeof(ext
));
2570 the_hash_algo
->update_fn(eoie_f
, &sz
, sizeof(sz
));
2575 static void ce_smudge_racily_clean_entry(struct index_state
*istate
,
2576 struct cache_entry
*ce
)
2579 * The only thing we care about in this function is to smudge the
2580 * falsely clean entry due to touch-update-touch race, so we leave
2581 * everything else as they are. We are called for entries whose
2582 * ce_stat_data.sd_mtime match the index file mtime.
2584 * Note that this actually does not do much for gitlinks, for
2585 * which ce_match_stat_basic() always goes to the actual
2586 * contents. The caller checks with is_racy_timestamp() which
2587 * always says "no" for gitlinks, so we are not called for them ;-)
2591 if (lstat(ce
->name
, &st
) < 0)
2593 if (ce_match_stat_basic(ce
, &st
))
2595 if (ce_modified_check_fs(istate
, ce
, &st
)) {
2596 /* This is "racily clean"; smudge it. Note that this
2597 * is a tricky code. At first glance, it may appear
2598 * that it can break with this sequence:
2600 * $ echo xyzzy >frotz
2601 * $ git-update-index --add frotz
2604 * $ echo filfre >nitfol
2605 * $ git-update-index --add nitfol
2607 * but it does not. When the second update-index runs,
2608 * it notices that the entry "frotz" has the same timestamp
2609 * as index, and if we were to smudge it by resetting its
2610 * size to zero here, then the object name recorded
2611 * in index is the 6-byte file but the cached stat information
2612 * becomes zero --- which would then match what we would
2613 * obtain from the filesystem next time we stat("frotz").
2615 * However, the second update-index, before calling
2616 * this function, notices that the cached size is 6
2617 * bytes and what is on the filesystem is an empty
2618 * file, and never calls us, so the cached size information
2619 * for "frotz" stays 6 which does not match the filesystem.
2621 ce
->ce_stat_data
.sd_size
= 0;
2625 /* Copy miscellaneous fields but not the name */
2626 static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry
*ondisk
,
2627 struct cache_entry
*ce
)
2630 const unsigned hashsz
= the_hash_algo
->rawsz
;
2631 uint16_t *flagsp
= (uint16_t *)(ondisk
->data
+ hashsz
);
2633 ondisk
->ctime
.sec
= htonl(ce
->ce_stat_data
.sd_ctime
.sec
);
2634 ondisk
->mtime
.sec
= htonl(ce
->ce_stat_data
.sd_mtime
.sec
);
2635 ondisk
->ctime
.nsec
= htonl(ce
->ce_stat_data
.sd_ctime
.nsec
);
2636 ondisk
->mtime
.nsec
= htonl(ce
->ce_stat_data
.sd_mtime
.nsec
);
2637 ondisk
->dev
= htonl(ce
->ce_stat_data
.sd_dev
);
2638 ondisk
->ino
= htonl(ce
->ce_stat_data
.sd_ino
);
2639 ondisk
->mode
= htonl(ce
->ce_mode
);
2640 ondisk
->uid
= htonl(ce
->ce_stat_data
.sd_uid
);
2641 ondisk
->gid
= htonl(ce
->ce_stat_data
.sd_gid
);
2642 ondisk
->size
= htonl(ce
->ce_stat_data
.sd_size
);
2643 hashcpy(ondisk
->data
, ce
->oid
.hash
);
2645 flags
= ce
->ce_flags
& ~CE_NAMEMASK
;
2646 flags
|= (ce_namelen(ce
) >= CE_NAMEMASK
? CE_NAMEMASK
: ce_namelen(ce
));
2647 flagsp
[0] = htons(flags
);
2648 if (ce
->ce_flags
& CE_EXTENDED
) {
2649 flagsp
[1] = htons((ce
->ce_flags
& CE_EXTENDED_FLAGS
) >> 16);
2653 static int ce_write_entry(struct hashfile
*f
, struct cache_entry
*ce
,
2654 struct strbuf
*previous_name
, struct ondisk_cache_entry
*ondisk
)
2657 unsigned int saved_namelen
;
2658 int stripped_name
= 0;
2659 static unsigned char padding
[8] = { 0x00 };
2661 if (ce
->ce_flags
& CE_STRIP_NAME
) {
2662 saved_namelen
= ce_namelen(ce
);
2667 size
= offsetof(struct ondisk_cache_entry
,data
) + ondisk_data_size(ce
->ce_flags
, 0);
2669 if (!previous_name
) {
2670 int len
= ce_namelen(ce
);
2671 copy_cache_entry_to_ondisk(ondisk
, ce
);
2672 hashwrite(f
, ondisk
, size
);
2673 hashwrite(f
, ce
->name
, len
);
2674 hashwrite(f
, padding
, align_padding_size(size
, len
));
2676 int common
, to_remove
, prefix_size
;
2677 unsigned char to_remove_vi
[16];
2679 (ce
->name
[common
] &&
2680 common
< previous_name
->len
&&
2681 ce
->name
[common
] == previous_name
->buf
[common
]);
2683 ; /* still matching */
2684 to_remove
= previous_name
->len
- common
;
2685 prefix_size
= encode_varint(to_remove
, to_remove_vi
);
2687 copy_cache_entry_to_ondisk(ondisk
, ce
);
2688 hashwrite(f
, ondisk
, size
);
2689 hashwrite(f
, to_remove_vi
, prefix_size
);
2690 hashwrite(f
, ce
->name
+ common
, ce_namelen(ce
) - common
);
2691 hashwrite(f
, padding
, 1);
2693 strbuf_splice(previous_name
, common
, to_remove
,
2694 ce
->name
+ common
, ce_namelen(ce
) - common
);
2696 if (stripped_name
) {
2697 ce
->ce_namelen
= saved_namelen
;
2698 ce
->ce_flags
&= ~CE_STRIP_NAME
;
2705 * This function verifies if index_state has the correct sha1 of the
2706 * index file. Don't die if we have any other failure, just return 0.
2708 static int verify_index_from(const struct index_state
*istate
, const char *path
)
2713 unsigned char hash
[GIT_MAX_RAWSZ
];
2715 if (!istate
->initialized
)
2718 fd
= open(path
, O_RDONLY
);
2725 if (st
.st_size
< sizeof(struct cache_header
) + the_hash_algo
->rawsz
)
2728 n
= pread_in_full(fd
, hash
, the_hash_algo
->rawsz
, st
.st_size
- the_hash_algo
->rawsz
);
2729 if (n
!= the_hash_algo
->rawsz
)
2732 if (!hasheq(istate
->oid
.hash
, hash
))
2743 static int repo_verify_index(struct repository
*repo
)
2745 return verify_index_from(repo
->index
, repo
->index_file
);
2748 int has_racy_timestamp(struct index_state
*istate
)
2750 int entries
= istate
->cache_nr
;
2753 for (i
= 0; i
< entries
; i
++) {
2754 struct cache_entry
*ce
= istate
->cache
[i
];
2755 if (is_racy_timestamp(istate
, ce
))
2761 void repo_update_index_if_able(struct repository
*repo
,
2762 struct lock_file
*lockfile
)
2764 if ((repo
->index
->cache_changed
||
2765 has_racy_timestamp(repo
->index
)) &&
2766 repo_verify_index(repo
))
2767 write_locked_index(repo
->index
, lockfile
, COMMIT_LOCK
);
2769 rollback_lock_file(lockfile
);
2772 static int record_eoie(void)
2776 if (!git_config_get_bool("index.recordendofindexentries", &val
))
2780 * As a convenience, the end of index entries extension
2781 * used for threading is written by default if the user
2782 * explicitly requested threaded index reads.
2784 return !git_config_get_index_threads(&val
) && val
!= 1;
2787 static int record_ieot(void)
2791 if (!git_config_get_bool("index.recordoffsettable", &val
))
2795 * As a convenience, the offset table used for threading is
2796 * written by default if the user explicitly requested
2797 * threaded index reads.
2799 return !git_config_get_index_threads(&val
) && val
!= 1;
2802 enum write_extensions
{
2803 WRITE_NO_EXTENSION
= 0,
2804 WRITE_SPLIT_INDEX_EXTENSION
= 1<<0,
2805 WRITE_CACHE_TREE_EXTENSION
= 1<<1,
2806 WRITE_RESOLVE_UNDO_EXTENSION
= 1<<2,
2807 WRITE_UNTRACKED_CACHE_EXTENSION
= 1<<3,
2808 WRITE_FSMONITOR_EXTENSION
= 1<<4,
2810 #define WRITE_ALL_EXTENSIONS ((enum write_extensions)-1)
2813 * On success, `tempfile` is closed. If it is the temporary file
2814 * of a `struct lock_file`, we will therefore effectively perform
2815 * a 'close_lock_file_gently()`. Since that is an implementation
2816 * detail of lockfiles, callers of `do_write_index()` should not
2819 static int do_write_index(struct index_state
*istate
, struct tempfile
*tempfile
,
2820 enum write_extensions write_extensions
, unsigned flags
)
2822 uint64_t start
= getnanotime();
2824 git_hash_ctx
*eoie_c
= NULL
;
2825 struct cache_header hdr
;
2826 int i
, err
= 0, removed
, extended
, hdr_version
;
2827 struct cache_entry
**cache
= istate
->cache
;
2828 int entries
= istate
->cache_nr
;
2830 struct ondisk_cache_entry ondisk
;
2831 struct strbuf previous_name_buf
= STRBUF_INIT
, *previous_name
;
2832 int drop_cache_tree
= istate
->drop_cache_tree
;
2834 int csum_fsync_flag
;
2835 int ieot_entries
= 1;
2836 struct index_entry_offset_table
*ieot
= NULL
;
2838 struct repository
*r
= istate
->repo
;
2840 f
= hashfd(tempfile
->fd
, tempfile
->filename
.buf
);
2842 prepare_repo_settings(r
);
2843 f
->skip_hash
= r
->settings
.index_skip_hash
;
2845 for (i
= removed
= extended
= 0; i
< entries
; i
++) {
2846 if (cache
[i
]->ce_flags
& CE_REMOVE
)
2849 /* reduce extended entries if possible */
2850 cache
[i
]->ce_flags
&= ~CE_EXTENDED
;
2851 if (cache
[i
]->ce_flags
& CE_EXTENDED_FLAGS
) {
2853 cache
[i
]->ce_flags
|= CE_EXTENDED
;
2857 if (!istate
->version
)
2858 istate
->version
= get_index_format_default(r
);
2860 /* demote version 3 to version 2 when the latter suffices */
2861 if (istate
->version
== 3 || istate
->version
== 2)
2862 istate
->version
= extended
? 3 : 2;
2864 hdr_version
= istate
->version
;
2866 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
2867 hdr
.hdr_version
= htonl(hdr_version
);
2868 hdr
.hdr_entries
= htonl(entries
- removed
);
2870 hashwrite(f
, &hdr
, sizeof(hdr
));
2872 if (!HAVE_THREADS
|| git_config_get_index_threads(&nr_threads
))
2875 if (nr_threads
!= 1 && record_ieot()) {
2876 int ieot_blocks
, cpus
;
2879 * ensure default number of ieot blocks maps evenly to the
2880 * default number of threads that will process them leaving
2881 * room for the thread to load the index extensions.
2884 ieot_blocks
= istate
->cache_nr
/ THREAD_COST
;
2885 cpus
= online_cpus();
2886 if (ieot_blocks
> cpus
- 1)
2887 ieot_blocks
= cpus
- 1;
2889 ieot_blocks
= nr_threads
;
2890 if (ieot_blocks
> istate
->cache_nr
)
2891 ieot_blocks
= istate
->cache_nr
;
2895 * no reason to write out the IEOT extension if we don't
2896 * have enough blocks to utilize multi-threading
2898 if (ieot_blocks
> 1) {
2899 ieot
= xcalloc(1, sizeof(struct index_entry_offset_table
)
2900 + (ieot_blocks
* sizeof(struct index_entry_offset
)));
2901 ieot_entries
= DIV_ROUND_UP(entries
, ieot_blocks
);
2905 offset
= hashfile_total(f
);
2908 previous_name
= (hdr_version
== 4) ? &previous_name_buf
: NULL
;
2910 for (i
= 0; i
< entries
; i
++) {
2911 struct cache_entry
*ce
= cache
[i
];
2912 if (ce
->ce_flags
& CE_REMOVE
)
2914 if (!ce_uptodate(ce
) && is_racy_timestamp(istate
, ce
))
2915 ce_smudge_racily_clean_entry(istate
, ce
);
2916 if (is_null_oid(&ce
->oid
)) {
2917 static const char msg
[] = "cache entry has null sha1: %s";
2918 static int allow
= -1;
2921 allow
= git_env_bool("GIT_ALLOW_NULL_SHA1", 0);
2923 warning(msg
, ce
->name
);
2925 err
= error(msg
, ce
->name
);
2927 drop_cache_tree
= 1;
2929 if (ieot
&& i
&& (i
% ieot_entries
== 0)) {
2930 ieot
->entries
[ieot
->nr
].nr
= nr
;
2931 ieot
->entries
[ieot
->nr
].offset
= offset
;
2934 * If we have a V4 index, set the first byte to an invalid
2935 * character to ensure there is nothing common with the previous
2939 previous_name
->buf
[0] = 0;
2942 offset
= hashfile_total(f
);
2944 if (ce_write_entry(f
, ce
, previous_name
, (struct ondisk_cache_entry
*)&ondisk
) < 0)
2952 ieot
->entries
[ieot
->nr
].nr
= nr
;
2953 ieot
->entries
[ieot
->nr
].offset
= offset
;
2956 strbuf_release(&previous_name_buf
);
2963 offset
= hashfile_total(f
);
2966 * The extension headers must be hashed on their own for the
2967 * EOIE extension. Create a hashfile here to compute that hash.
2969 if (offset
&& record_eoie()) {
2970 CALLOC_ARRAY(eoie_c
, 1);
2971 the_hash_algo
->init_fn(eoie_c
);
2975 * Lets write out CACHE_EXT_INDEXENTRYOFFSETTABLE first so that we
2976 * can minimize the number of extensions we have to scan through to
2977 * find it during load. Write it out regardless of the
2978 * strip_extensions parameter as we need it when loading the shared
2982 struct strbuf sb
= STRBUF_INIT
;
2984 write_ieot_extension(&sb
, ieot
);
2985 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_INDEXENTRYOFFSETTABLE
, sb
.len
) < 0;
2986 hashwrite(f
, sb
.buf
, sb
.len
);
2987 strbuf_release(&sb
);
2993 if (write_extensions
& WRITE_SPLIT_INDEX_EXTENSION
&&
2994 istate
->split_index
) {
2995 struct strbuf sb
= STRBUF_INIT
;
2997 if (istate
->sparse_index
)
2998 die(_("cannot write split index for a sparse index"));
3000 err
= write_link_extension(&sb
, istate
) < 0 ||
3001 write_index_ext_header(f
, eoie_c
, CACHE_EXT_LINK
,
3003 hashwrite(f
, sb
.buf
, sb
.len
);
3004 strbuf_release(&sb
);
3008 if (write_extensions
& WRITE_CACHE_TREE_EXTENSION
&&
3009 !drop_cache_tree
&& istate
->cache_tree
) {
3010 struct strbuf sb
= STRBUF_INIT
;
3012 cache_tree_write(&sb
, istate
->cache_tree
);
3013 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_TREE
, sb
.len
) < 0;
3014 hashwrite(f
, sb
.buf
, sb
.len
);
3015 strbuf_release(&sb
);
3019 if (write_extensions
& WRITE_RESOLVE_UNDO_EXTENSION
&&
3020 istate
->resolve_undo
) {
3021 struct strbuf sb
= STRBUF_INIT
;
3023 resolve_undo_write(&sb
, istate
->resolve_undo
);
3024 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_RESOLVE_UNDO
,
3026 hashwrite(f
, sb
.buf
, sb
.len
);
3027 strbuf_release(&sb
);
3031 if (write_extensions
& WRITE_UNTRACKED_CACHE_EXTENSION
&&
3032 istate
->untracked
) {
3033 struct strbuf sb
= STRBUF_INIT
;
3035 write_untracked_extension(&sb
, istate
->untracked
);
3036 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_UNTRACKED
,
3038 hashwrite(f
, sb
.buf
, sb
.len
);
3039 strbuf_release(&sb
);
3043 if (write_extensions
& WRITE_FSMONITOR_EXTENSION
&&
3044 istate
->fsmonitor_last_update
) {
3045 struct strbuf sb
= STRBUF_INIT
;
3047 write_fsmonitor_extension(&sb
, istate
);
3048 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_FSMONITOR
, sb
.len
) < 0;
3049 hashwrite(f
, sb
.buf
, sb
.len
);
3050 strbuf_release(&sb
);
3054 if (istate
->sparse_index
) {
3055 if (write_index_ext_header(f
, eoie_c
, CACHE_EXT_SPARSE_DIRECTORIES
, 0) < 0)
3060 * CACHE_EXT_ENDOFINDEXENTRIES must be written as the last entry before the SHA1
3061 * so that it can be found and processed before all the index entries are
3062 * read. Write it out regardless of the strip_extensions parameter as we need it
3063 * when loading the shared index.
3066 struct strbuf sb
= STRBUF_INIT
;
3068 write_eoie_extension(&sb
, eoie_c
, offset
);
3069 err
= write_index_ext_header(f
, NULL
, CACHE_EXT_ENDOFINDEXENTRIES
, sb
.len
) < 0;
3070 hashwrite(f
, sb
.buf
, sb
.len
);
3071 strbuf_release(&sb
);
3076 csum_fsync_flag
= 0;
3077 if (!alternate_index_output
&& (flags
& COMMIT_LOCK
))
3078 csum_fsync_flag
= CSUM_FSYNC
;
3080 finalize_hashfile(f
, istate
->oid
.hash
, FSYNC_COMPONENT_INDEX
,
3081 CSUM_HASH_IN_STREAM
| csum_fsync_flag
);
3083 if (close_tempfile_gently(tempfile
)) {
3084 error(_("could not close '%s'"), get_tempfile_path(tempfile
));
3087 if (stat(get_tempfile_path(tempfile
), &st
))
3089 istate
->timestamp
.sec
= (unsigned int)st
.st_mtime
;
3090 istate
->timestamp
.nsec
= ST_MTIME_NSEC(st
);
3091 trace_performance_since(start
, "write index, changed mask = %x", istate
->cache_changed
);
3094 * TODO trace2: replace "the_repository" with the actual repo instance
3095 * that is associated with the given "istate".
3097 trace2_data_intmax("index", the_repository
, "write/version",
3099 trace2_data_intmax("index", the_repository
, "write/cache_nr",
3105 void set_alternate_index_output(const char *name
)
3107 alternate_index_output
= name
;
3110 static int commit_locked_index(struct lock_file
*lk
)
3112 if (alternate_index_output
)
3113 return commit_lock_file_to(lk
, alternate_index_output
);
3115 return commit_lock_file(lk
);
3118 static int do_write_locked_index(struct index_state
*istate
,
3119 struct lock_file
*lock
,
3121 enum write_extensions write_extensions
)
3124 int was_full
= istate
->sparse_index
== INDEX_EXPANDED
;
3126 ret
= convert_to_sparse(istate
, 0);
3129 warning(_("failed to convert to a sparse-index"));
3134 * TODO trace2: replace "the_repository" with the actual repo instance
3135 * that is associated with the given "istate".
3137 trace2_region_enter_printf("index", "do_write_index", the_repository
,
3138 "%s", get_lock_file_path(lock
));
3139 ret
= do_write_index(istate
, lock
->tempfile
, write_extensions
, flags
);
3140 trace2_region_leave_printf("index", "do_write_index", the_repository
,
3141 "%s", get_lock_file_path(lock
));
3144 ensure_full_index(istate
);
3148 if (flags
& COMMIT_LOCK
)
3149 ret
= commit_locked_index(lock
);
3151 ret
= close_lock_file_gently(lock
);
3153 run_hooks_l("post-index-change",
3154 istate
->updated_workdir
? "1" : "0",
3155 istate
->updated_skipworktree
? "1" : "0", NULL
);
3156 istate
->updated_workdir
= 0;
3157 istate
->updated_skipworktree
= 0;
3162 static int write_split_index(struct index_state
*istate
,
3163 struct lock_file
*lock
,
3167 prepare_to_write_split_index(istate
);
3168 ret
= do_write_locked_index(istate
, lock
, flags
, WRITE_ALL_EXTENSIONS
);
3169 finish_writing_split_index(istate
);
3173 static const char *shared_index_expire
= "2.weeks.ago";
3175 static unsigned long get_shared_index_expire_date(void)
3177 static unsigned long shared_index_expire_date
;
3178 static int shared_index_expire_date_prepared
;
3180 if (!shared_index_expire_date_prepared
) {
3181 git_config_get_expiry("splitindex.sharedindexexpire",
3182 &shared_index_expire
);
3183 shared_index_expire_date
= approxidate(shared_index_expire
);
3184 shared_index_expire_date_prepared
= 1;
3187 return shared_index_expire_date
;
3190 static int should_delete_shared_index(const char *shared_index_path
)
3193 unsigned long expiration
;
3195 /* Check timestamp */
3196 expiration
= get_shared_index_expire_date();
3199 if (stat(shared_index_path
, &st
))
3200 return error_errno(_("could not stat '%s'"), shared_index_path
);
3201 if (st
.st_mtime
> expiration
)
3207 static int clean_shared_index_files(const char *current_hex
)
3210 DIR *dir
= opendir(get_git_dir());
3213 return error_errno(_("unable to open git dir: %s"), get_git_dir());
3215 while ((de
= readdir(dir
)) != NULL
) {
3216 const char *sha1_hex
;
3217 const char *shared_index_path
;
3218 if (!skip_prefix(de
->d_name
, "sharedindex.", &sha1_hex
))
3220 if (!strcmp(sha1_hex
, current_hex
))
3222 shared_index_path
= git_path("%s", de
->d_name
);
3223 if (should_delete_shared_index(shared_index_path
) > 0 &&
3224 unlink(shared_index_path
))
3225 warning_errno(_("unable to unlink: %s"), shared_index_path
);
3232 static int write_shared_index(struct index_state
*istate
,
3233 struct tempfile
**temp
, unsigned flags
)
3235 struct split_index
*si
= istate
->split_index
;
3236 int ret
, was_full
= !istate
->sparse_index
;
3238 move_cache_to_base_index(istate
);
3239 convert_to_sparse(istate
, 0);
3241 trace2_region_enter_printf("index", "shared/do_write_index",
3242 the_repository
, "%s", get_tempfile_path(*temp
));
3243 ret
= do_write_index(si
->base
, *temp
, WRITE_NO_EXTENSION
, flags
);
3244 trace2_region_leave_printf("index", "shared/do_write_index",
3245 the_repository
, "%s", get_tempfile_path(*temp
));
3248 ensure_full_index(istate
);
3252 ret
= adjust_shared_perm(get_tempfile_path(*temp
));
3254 error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp
));
3257 ret
= rename_tempfile(temp
,
3258 git_path("sharedindex.%s", oid_to_hex(&si
->base
->oid
)));
3260 oidcpy(&si
->base_oid
, &si
->base
->oid
);
3261 clean_shared_index_files(oid_to_hex(&si
->base
->oid
));
3267 static const int default_max_percent_split_change
= 20;
3269 static int too_many_not_shared_entries(struct index_state
*istate
)
3271 int i
, not_shared
= 0;
3272 int max_split
= git_config_get_max_percent_split_change();
3274 switch (max_split
) {
3276 /* not or badly configured: use the default value */
3277 max_split
= default_max_percent_split_change
;
3280 return 1; /* 0% means always write a new shared index */
3282 return 0; /* 100% means never write a new shared index */
3284 break; /* just use the configured value */
3287 /* Count not shared entries */
3288 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3289 struct cache_entry
*ce
= istate
->cache
[i
];
3294 return (int64_t)istate
->cache_nr
* max_split
< (int64_t)not_shared
* 100;
3297 int write_locked_index(struct index_state
*istate
, struct lock_file
*lock
,
3300 int new_shared_index
, ret
, test_split_index_env
;
3301 struct split_index
*si
= istate
->split_index
;
3303 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
3304 cache_tree_verify(the_repository
, istate
);
3306 if ((flags
& SKIP_IF_UNCHANGED
) && !istate
->cache_changed
) {
3307 if (flags
& COMMIT_LOCK
)
3308 rollback_lock_file(lock
);
3312 if (istate
->fsmonitor_last_update
)
3313 fill_fsmonitor_bitmap(istate
);
3315 test_split_index_env
= git_env_bool("GIT_TEST_SPLIT_INDEX", 0);
3317 if ((!si
&& !test_split_index_env
) ||
3318 alternate_index_output
||
3319 (istate
->cache_changed
& ~EXTMASK
)) {
3320 ret
= do_write_locked_index(istate
, lock
, flags
,
3321 ~WRITE_SPLIT_INDEX_EXTENSION
);
3325 if (test_split_index_env
) {
3327 si
= init_split_index(istate
);
3328 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
3330 int v
= si
->base_oid
.hash
[0];
3332 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
3335 if (too_many_not_shared_entries(istate
))
3336 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
3338 new_shared_index
= istate
->cache_changed
& SPLIT_INDEX_ORDERED
;
3340 if (new_shared_index
) {
3341 struct tempfile
*temp
;
3344 /* Same initial permissions as the main .git/index file */
3345 temp
= mks_tempfile_sm(git_path("sharedindex_XXXXXX"), 0, 0666);
3347 ret
= do_write_locked_index(istate
, lock
, flags
,
3348 ~WRITE_SPLIT_INDEX_EXTENSION
);
3351 ret
= write_shared_index(istate
, &temp
, flags
);
3353 saved_errno
= errno
;
3354 if (is_tempfile_active(temp
))
3355 delete_tempfile(&temp
);
3356 errno
= saved_errno
;
3362 ret
= write_split_index(istate
, lock
, flags
);
3364 /* Freshen the shared index only if the split-index was written */
3365 if (!ret
&& !new_shared_index
&& !is_null_oid(&si
->base_oid
)) {
3366 const char *shared_index
= git_path("sharedindex.%s",
3367 oid_to_hex(&si
->base_oid
));
3368 freshen_shared_index(shared_index
, 1);
3372 if (flags
& COMMIT_LOCK
)
3373 rollback_lock_file(lock
);
3378 * Read the index file that is potentially unmerged into given
3379 * index_state, dropping any unmerged entries to stage #0 (potentially
3380 * resulting in a path appearing as both a file and a directory in the
3381 * index; the caller is responsible to clear out the extra entries
3382 * before writing the index to a tree). Returns true if the index is
3383 * unmerged. Callers who want to refuse to work from an unmerged
3384 * state can call this and check its return value, instead of calling
3387 int repo_read_index_unmerged(struct repository
*repo
)
3389 struct index_state
*istate
;
3393 repo_read_index(repo
);
3394 istate
= repo
->index
;
3395 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3396 struct cache_entry
*ce
= istate
->cache
[i
];
3397 struct cache_entry
*new_ce
;
3403 len
= ce_namelen(ce
);
3404 new_ce
= make_empty_cache_entry(istate
, len
);
3405 memcpy(new_ce
->name
, ce
->name
, len
);
3406 new_ce
->ce_flags
= create_ce_flags(0) | CE_CONFLICTED
;
3407 new_ce
->ce_namelen
= len
;
3408 new_ce
->ce_mode
= ce
->ce_mode
;
3409 if (add_index_entry(istate
, new_ce
, ADD_CACHE_SKIP_DFCHECK
))
3410 return error(_("%s: cannot drop to stage #0"),
3417 * Returns 1 if the path is an "other" path with respect to
3418 * the index; that is, the path is not mentioned in the index at all,
3419 * either as a file, a directory with some files in the index,
3420 * or as an unmerged entry.
3422 * We helpfully remove a trailing "/" from directories so that
3423 * the output of read_directory can be used as-is.
3425 int index_name_is_other(struct index_state
*istate
, const char *name
,
3429 if (namelen
&& name
[namelen
- 1] == '/')
3431 pos
= index_name_pos(istate
, name
, namelen
);
3433 return 0; /* exact match */
3435 if (pos
< istate
->cache_nr
) {
3436 struct cache_entry
*ce
= istate
->cache
[pos
];
3437 if (ce_namelen(ce
) == namelen
&&
3438 !memcmp(ce
->name
, name
, namelen
))
3439 return 0; /* Yup, this one exists unmerged */
3444 void *read_blob_data_from_index(struct index_state
*istate
,
3445 const char *path
, unsigned long *size
)
3449 enum object_type type
;
3453 pos
= index_name_pos(istate
, path
, len
);
3456 * We might be in the middle of a merge, in which
3457 * case we would read stage #2 (ours).
3461 (pos
< 0 && i
< istate
->cache_nr
&&
3462 !strcmp(istate
->cache
[i
]->name
, path
));
3464 if (ce_stage(istate
->cache
[i
]) == 2)
3469 data
= repo_read_object_file(the_repository
, &istate
->cache
[pos
]->oid
,
3471 if (!data
|| type
!= OBJ_BLOB
) {
3480 void move_index_extensions(struct index_state
*dst
, struct index_state
*src
)
3482 dst
->untracked
= src
->untracked
;
3483 src
->untracked
= NULL
;
3484 dst
->cache_tree
= src
->cache_tree
;
3485 src
->cache_tree
= NULL
;
3488 struct cache_entry
*dup_cache_entry(const struct cache_entry
*ce
,
3489 struct index_state
*istate
)
3491 unsigned int size
= ce_size(ce
);
3492 int mem_pool_allocated
;
3493 struct cache_entry
*new_entry
= make_empty_cache_entry(istate
, ce_namelen(ce
));
3494 mem_pool_allocated
= new_entry
->mem_pool_allocated
;
3496 memcpy(new_entry
, ce
, size
);
3497 new_entry
->mem_pool_allocated
= mem_pool_allocated
;
3501 void discard_cache_entry(struct cache_entry
*ce
)
3503 if (ce
&& should_validate_cache_entries())
3504 memset(ce
, 0xCD, cache_entry_size(ce
->ce_namelen
));
3506 if (ce
&& ce
->mem_pool_allocated
)
3512 int should_validate_cache_entries(void)
3514 static int validate_index_cache_entries
= -1;
3516 if (validate_index_cache_entries
< 0) {
3517 if (getenv("GIT_TEST_VALIDATE_INDEX_CACHE_ENTRIES"))
3518 validate_index_cache_entries
= 1;
3520 validate_index_cache_entries
= 0;
3523 return validate_index_cache_entries
;
3526 #define EOIE_SIZE (4 + GIT_SHA1_RAWSZ) /* <4-byte offset> + <20-byte hash> */
3527 #define EOIE_SIZE_WITH_HEADER (4 + 4 + EOIE_SIZE) /* <4-byte signature> + <4-byte length> + EOIE_SIZE */
3529 static size_t read_eoie_extension(const char *mmap
, size_t mmap_size
)
3532 * The end of index entries (EOIE) extension is guaranteed to be last
3533 * so that it can be found by scanning backwards from the EOF.
3540 const char *index
, *eoie
;
3542 size_t offset
, src_offset
;
3543 unsigned char hash
[GIT_MAX_RAWSZ
];
3546 /* ensure we have an index big enough to contain an EOIE extension */
3547 if (mmap_size
< sizeof(struct cache_header
) + EOIE_SIZE_WITH_HEADER
+ the_hash_algo
->rawsz
)
3550 /* validate the extension signature */
3551 index
= eoie
= mmap
+ mmap_size
- EOIE_SIZE_WITH_HEADER
- the_hash_algo
->rawsz
;
3552 if (CACHE_EXT(index
) != CACHE_EXT_ENDOFINDEXENTRIES
)
3554 index
+= sizeof(uint32_t);
3556 /* validate the extension size */
3557 extsize
= get_be32(index
);
3558 if (extsize
!= EOIE_SIZE
)
3560 index
+= sizeof(uint32_t);
3563 * Validate the offset we're going to look for the first extension
3564 * signature is after the index header and before the eoie extension.
3566 offset
= get_be32(index
);
3567 if (mmap
+ offset
< mmap
+ sizeof(struct cache_header
))
3569 if (mmap
+ offset
>= eoie
)
3571 index
+= sizeof(uint32_t);
3574 * The hash is computed over extension types and their sizes (but not
3575 * their contents). E.g. if we have "TREE" extension that is N-bytes
3576 * long, "REUC" extension that is M-bytes long, followed by "EOIE",
3577 * then the hash would be:
3579 * SHA-1("TREE" + <binary representation of N> +
3580 * "REUC" + <binary representation of M>)
3582 src_offset
= offset
;
3583 the_hash_algo
->init_fn(&c
);
3584 while (src_offset
< mmap_size
- the_hash_algo
->rawsz
- EOIE_SIZE_WITH_HEADER
) {
3585 /* After an array of active_nr index entries,
3586 * there can be arbitrary number of extended
3587 * sections, each of which is prefixed with
3588 * extension name (4-byte) and section length
3589 * in 4-byte network byte order.
3592 memcpy(&extsize
, mmap
+ src_offset
+ 4, 4);
3593 extsize
= ntohl(extsize
);
3595 /* verify the extension size isn't so large it will wrap around */
3596 if (src_offset
+ 8 + extsize
< src_offset
)
3599 the_hash_algo
->update_fn(&c
, mmap
+ src_offset
, 8);
3602 src_offset
+= extsize
;
3604 the_hash_algo
->final_fn(hash
, &c
);
3605 if (!hasheq(hash
, (const unsigned char *)index
))
3608 /* Validate that the extension offsets returned us back to the eoie extension. */
3609 if (src_offset
!= mmap_size
- the_hash_algo
->rawsz
- EOIE_SIZE_WITH_HEADER
)
3615 static void write_eoie_extension(struct strbuf
*sb
, git_hash_ctx
*eoie_context
, size_t offset
)
3618 unsigned char hash
[GIT_MAX_RAWSZ
];
3621 put_be32(&buffer
, offset
);
3622 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3625 the_hash_algo
->final_fn(hash
, eoie_context
);
3626 strbuf_add(sb
, hash
, the_hash_algo
->rawsz
);
3629 #define IEOT_VERSION (1)
3631 static struct index_entry_offset_table
*read_ieot_extension(const char *mmap
, size_t mmap_size
, size_t offset
)
3633 const char *index
= NULL
;
3634 uint32_t extsize
, ext_version
;
3635 struct index_entry_offset_table
*ieot
;
3638 /* find the IEOT extension */
3641 while (offset
<= mmap_size
- the_hash_algo
->rawsz
- 8) {
3642 extsize
= get_be32(mmap
+ offset
+ 4);
3643 if (CACHE_EXT((mmap
+ offset
)) == CACHE_EXT_INDEXENTRYOFFSETTABLE
) {
3644 index
= mmap
+ offset
+ 4 + 4;
3653 /* validate the version is IEOT_VERSION */
3654 ext_version
= get_be32(index
);
3655 if (ext_version
!= IEOT_VERSION
) {
3656 error("invalid IEOT version %d", ext_version
);
3659 index
+= sizeof(uint32_t);
3661 /* extension size - version bytes / bytes per entry */
3662 nr
= (extsize
- sizeof(uint32_t)) / (sizeof(uint32_t) + sizeof(uint32_t));
3664 error("invalid number of IEOT entries %d", nr
);
3667 ieot
= xmalloc(sizeof(struct index_entry_offset_table
)
3668 + (nr
* sizeof(struct index_entry_offset
)));
3670 for (i
= 0; i
< nr
; i
++) {
3671 ieot
->entries
[i
].offset
= get_be32(index
);
3672 index
+= sizeof(uint32_t);
3673 ieot
->entries
[i
].nr
= get_be32(index
);
3674 index
+= sizeof(uint32_t);
3680 static void write_ieot_extension(struct strbuf
*sb
, struct index_entry_offset_table
*ieot
)
3686 put_be32(&buffer
, IEOT_VERSION
);
3687 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3690 for (i
= 0; i
< ieot
->nr
; i
++) {
3693 put_be32(&buffer
, ieot
->entries
[i
].offset
);
3694 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3697 put_be32(&buffer
, ieot
->entries
[i
].nr
);
3698 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3702 void prefetch_cache_entries(const struct index_state
*istate
,
3703 must_prefetch_predicate must_prefetch
)
3706 struct oid_array to_fetch
= OID_ARRAY_INIT
;
3708 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3709 struct cache_entry
*ce
= istate
->cache
[i
];
3711 if (S_ISGITLINK(ce
->ce_mode
) || !must_prefetch(ce
))
3713 if (!oid_object_info_extended(the_repository
, &ce
->oid
,
3715 OBJECT_INFO_FOR_PREFETCH
))
3717 oid_array_append(&to_fetch
, &ce
->oid
);
3719 promisor_remote_get_direct(the_repository
,
3720 to_fetch
.oid
, to_fetch
.nr
);
3721 oid_array_clear(&to_fetch
);
3724 static int read_one_entry_opt(struct index_state
*istate
,
3725 const struct object_id
*oid
,
3726 struct strbuf
*base
,
3727 const char *pathname
,
3728 unsigned mode
, int opt
)
3731 struct cache_entry
*ce
;
3734 return READ_TREE_RECURSIVE
;
3736 len
= strlen(pathname
);
3737 ce
= make_empty_cache_entry(istate
, base
->len
+ len
);
3739 ce
->ce_mode
= create_ce_mode(mode
);
3740 ce
->ce_flags
= create_ce_flags(1);
3741 ce
->ce_namelen
= base
->len
+ len
;
3742 memcpy(ce
->name
, base
->buf
, base
->len
);
3743 memcpy(ce
->name
+ base
->len
, pathname
, len
+1);
3744 oidcpy(&ce
->oid
, oid
);
3745 return add_index_entry(istate
, ce
, opt
);
3748 static int read_one_entry(const struct object_id
*oid
, struct strbuf
*base
,
3749 const char *pathname
, unsigned mode
,
3752 struct index_state
*istate
= context
;
3753 return read_one_entry_opt(istate
, oid
, base
, pathname
,
3755 ADD_CACHE_OK_TO_ADD
|ADD_CACHE_SKIP_DFCHECK
);
3759 * This is used when the caller knows there is no existing entries at
3760 * the stage that will conflict with the entry being added.
3762 static int read_one_entry_quick(const struct object_id
*oid
, struct strbuf
*base
,
3763 const char *pathname
, unsigned mode
,
3766 struct index_state
*istate
= context
;
3767 return read_one_entry_opt(istate
, oid
, base
, pathname
,
3768 mode
, ADD_CACHE_JUST_APPEND
);
3772 * Read the tree specified with --with-tree option
3773 * (typically, HEAD) into stage #1 and then
3774 * squash them down to stage #0. This is used for
3775 * --error-unmatch to list and check the path patterns
3776 * that were given from the command line. We are not
3777 * going to write this index out.
3779 void overlay_tree_on_index(struct index_state
*istate
,
3780 const char *tree_name
, const char *prefix
)
3783 struct object_id oid
;
3784 struct pathspec pathspec
;
3785 struct cache_entry
*last_stage0
= NULL
;
3787 read_tree_fn_t fn
= NULL
;
3790 if (repo_get_oid(the_repository
, tree_name
, &oid
))
3791 die("tree-ish %s not found.", tree_name
);
3792 tree
= parse_tree_indirect(&oid
);
3794 die("bad tree-ish %s", tree_name
);
3796 /* Hoist the unmerged entries up to stage #3 to make room */
3797 /* TODO: audit for interaction with sparse-index. */
3798 ensure_full_index(istate
);
3799 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3800 struct cache_entry
*ce
= istate
->cache
[i
];
3803 ce
->ce_flags
|= CE_STAGEMASK
;
3807 static const char *(matchbuf
[1]);
3809 parse_pathspec(&pathspec
, PATHSPEC_ALL_MAGIC
,
3810 PATHSPEC_PREFER_CWD
, prefix
, matchbuf
);
3812 memset(&pathspec
, 0, sizeof(pathspec
));
3815 * See if we have cache entry at the stage. If so,
3816 * do it the original slow way, otherwise, append and then
3819 for (i
= 0; !fn
&& i
< istate
->cache_nr
; i
++) {
3820 const struct cache_entry
*ce
= istate
->cache
[i
];
3821 if (ce_stage(ce
) == 1)
3822 fn
= read_one_entry
;
3826 fn
= read_one_entry_quick
;
3827 err
= read_tree(the_repository
, tree
, &pathspec
, fn
, istate
);
3828 clear_pathspec(&pathspec
);
3830 die("unable to read tree entries %s", tree_name
);
3833 * Sort the cache entry -- we need to nuke the cache tree, though.
3835 if (fn
== read_one_entry_quick
) {
3836 cache_tree_free(&istate
->cache_tree
);
3837 QSORT(istate
->cache
, istate
->cache_nr
, cmp_cache_name_compare
);
3840 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3841 struct cache_entry
*ce
= istate
->cache
[i
];
3842 switch (ce_stage(ce
)) {
3850 * If there is stage #0 entry for this, we do not
3851 * need to show it. We use CE_UPDATE bit to mark
3855 !strcmp(last_stage0
->name
, ce
->name
))
3856 ce
->ce_flags
|= CE_UPDATE
;
3861 struct update_callback_data
{
3862 struct index_state
*index
;
3868 static int fix_unmerged_status(struct diff_filepair
*p
,
3869 struct update_callback_data
*data
)
3871 if (p
->status
!= DIFF_STATUS_UNMERGED
)
3873 if (!(data
->flags
& ADD_CACHE_IGNORE_REMOVAL
) && !p
->two
->mode
)
3875 * This is not an explicit add request, and the
3876 * path is missing from the working tree (deleted)
3878 return DIFF_STATUS_DELETED
;
3881 * Either an explicit add request, or path exists
3882 * in the working tree. An attempt to explicitly
3883 * add a path that does not exist in the working tree
3884 * will be caught as an error by the caller immediately.
3886 return DIFF_STATUS_MODIFIED
;
3889 static void update_callback(struct diff_queue_struct
*q
,
3890 struct diff_options
*opt UNUSED
, void *cbdata
)
3893 struct update_callback_data
*data
= cbdata
;
3895 for (i
= 0; i
< q
->nr
; i
++) {
3896 struct diff_filepair
*p
= q
->queue
[i
];
3897 const char *path
= p
->one
->path
;
3899 if (!data
->include_sparse
&&
3900 !path_in_sparse_checkout(path
, data
->index
))
3903 switch (fix_unmerged_status(p
, data
)) {
3905 die(_("unexpected diff status %c"), p
->status
);
3906 case DIFF_STATUS_MODIFIED
:
3907 case DIFF_STATUS_TYPE_CHANGED
:
3908 if (add_file_to_index(data
->index
, path
, data
->flags
)) {
3909 if (!(data
->flags
& ADD_CACHE_IGNORE_ERRORS
))
3910 die(_("updating files failed"));
3914 case DIFF_STATUS_DELETED
:
3915 if (data
->flags
& ADD_CACHE_IGNORE_REMOVAL
)
3917 if (!(data
->flags
& ADD_CACHE_PRETEND
))
3918 remove_file_from_index(data
->index
, path
);
3919 if (data
->flags
& (ADD_CACHE_PRETEND
|ADD_CACHE_VERBOSE
))
3920 printf(_("remove '%s'\n"), path
);
3926 int add_files_to_cache(struct repository
*repo
, const char *prefix
,
3927 const struct pathspec
*pathspec
, char *ps_matched
,
3928 int include_sparse
, int flags
)
3930 struct update_callback_data data
;
3931 struct rev_info rev
;
3933 memset(&data
, 0, sizeof(data
));
3934 data
.index
= repo
->index
;
3935 data
.include_sparse
= include_sparse
;
3938 repo_init_revisions(repo
, &rev
, prefix
);
3939 setup_revisions(0, NULL
, &rev
, NULL
);
3941 copy_pathspec(&rev
.prune_data
, pathspec
);
3942 rev
.ps_matched
= ps_matched
;
3944 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
3945 rev
.diffopt
.format_callback
= update_callback
;
3946 rev
.diffopt
.format_callback_data
= &data
;
3947 rev
.diffopt
.flags
.override_submodule_config
= 1;
3948 rev
.max_count
= 0; /* do not compare unmerged paths with stage #2 */
3951 * Use an ODB transaction to optimize adding multiple objects.
3952 * This function is invoked from commands other than 'add', which
3953 * may not have their own transaction active.
3955 begin_odb_transaction();
3956 run_diff_files(&rev
, DIFF_RACY_IS_MODIFIED
);
3957 end_odb_transaction();
3959 release_revisions(&rev
);
3960 return !!data
.add_errors
;