2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
14 #include "cache-tree.h"
17 #include "object-file.h"
18 #include "object-store.h"
19 #include "oid-array.h"
23 #include "environment.h"
26 #include "object-name.h"
27 #include "resolve-undo.h"
28 #include "run-command.h"
32 #include "split-index.h"
35 #include "fsmonitor.h"
36 #include "thread-utils.h"
38 #include "sparse-index.h"
39 #include "csum-file.h"
40 #include "promisor-remote.h"
44 /* Mask for the name length in ce_flags in the on-disk index */
46 #define CE_NAMEMASK (0x0fff)
50 * The first letter should be 'A'..'Z' for extensions that are not
51 * necessary for a correct operation (i.e. optimization data).
52 * When new extensions are added that _needs_ to be understood in
53 * order to correctly interpret the index file, pick character that
54 * is outside the range, to cause the reader to abort.
57 #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
58 #define CACHE_EXT_TREE 0x54524545 /* "TREE" */
59 #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
60 #define CACHE_EXT_LINK 0x6c696e6b /* "link" */
61 #define CACHE_EXT_UNTRACKED 0x554E5452 /* "UNTR" */
62 #define CACHE_EXT_FSMONITOR 0x46534D4E /* "FSMN" */
63 #define CACHE_EXT_ENDOFINDEXENTRIES 0x454F4945 /* "EOIE" */
64 #define CACHE_EXT_INDEXENTRYOFFSETTABLE 0x49454F54 /* "IEOT" */
65 #define CACHE_EXT_SPARSE_DIRECTORIES 0x73646972 /* "sdir" */
67 /* changes that can be kept in $GIT_DIR/index (basically all extensions) */
68 #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
69 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \
70 SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED | FSMONITOR_CHANGED)
74 * This is an estimate of the pathname length in the index. We use
75 * this for V4 index files to guess the un-deltafied size of the index
76 * in memory because of pathname deltafication. This is not required
77 * for V2/V3 index formats because their pathnames are not compressed.
78 * If the initial amount of memory set aside is not sufficient, the
79 * mem pool will allocate extra memory.
81 #define CACHE_ENTRY_PATH_LENGTH 80
83 enum index_search_mode
{
88 static inline struct cache_entry
*mem_pool__ce_alloc(struct mem_pool
*mem_pool
, size_t len
)
90 struct cache_entry
*ce
;
91 ce
= mem_pool_alloc(mem_pool
, cache_entry_size(len
));
92 ce
->mem_pool_allocated
= 1;
96 static inline struct cache_entry
*mem_pool__ce_calloc(struct mem_pool
*mem_pool
, size_t len
)
98 struct cache_entry
* ce
;
99 ce
= mem_pool_calloc(mem_pool
, 1, cache_entry_size(len
));
100 ce
->mem_pool_allocated
= 1;
104 static struct mem_pool
*find_mem_pool(struct index_state
*istate
)
106 struct mem_pool
**pool_ptr
;
108 if (istate
->split_index
&& istate
->split_index
->base
)
109 pool_ptr
= &istate
->split_index
->base
->ce_mem_pool
;
111 pool_ptr
= &istate
->ce_mem_pool
;
114 *pool_ptr
= xmalloc(sizeof(**pool_ptr
));
115 mem_pool_init(*pool_ptr
, 0);
121 static const char *alternate_index_output
;
123 static void set_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
125 if (S_ISSPARSEDIR(ce
->ce_mode
))
126 istate
->sparse_index
= INDEX_COLLAPSED
;
128 istate
->cache
[nr
] = ce
;
129 add_name_hash(istate
, ce
);
132 static void replace_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
134 struct cache_entry
*old
= istate
->cache
[nr
];
136 replace_index_entry_in_base(istate
, old
, ce
);
137 remove_name_hash(istate
, old
);
138 discard_cache_entry(old
);
139 ce
->ce_flags
&= ~CE_HASHED
;
140 set_index_entry(istate
, nr
, ce
);
141 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
142 mark_fsmonitor_invalid(istate
, ce
);
143 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
146 void rename_index_entry_at(struct index_state
*istate
, int nr
, const char *new_name
)
148 struct cache_entry
*old_entry
= istate
->cache
[nr
], *new_entry
, *refreshed
;
149 int namelen
= strlen(new_name
);
151 new_entry
= make_empty_cache_entry(istate
, namelen
);
152 copy_cache_entry(new_entry
, old_entry
);
153 new_entry
->ce_flags
&= ~CE_HASHED
;
154 new_entry
->ce_namelen
= namelen
;
155 new_entry
->index
= 0;
156 memcpy(new_entry
->name
, new_name
, namelen
+ 1);
158 cache_tree_invalidate_path(istate
, old_entry
->name
);
159 untracked_cache_remove_from_index(istate
, old_entry
->name
);
160 remove_index_entry_at(istate
, nr
);
163 * Refresh the new index entry. Using 'refresh_cache_entry' ensures
164 * we only update stat info if the entry is otherwise up-to-date (i.e.,
165 * the contents/mode haven't changed). This ensures that we reflect the
166 * 'ctime' of the rename in the index without (incorrectly) updating
167 * the cached stat info to reflect unstaged changes on disk.
169 refreshed
= refresh_cache_entry(istate
, new_entry
, CE_MATCH_REFRESH
);
170 if (refreshed
&& refreshed
!= new_entry
) {
171 add_index_entry(istate
, refreshed
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
172 discard_cache_entry(new_entry
);
174 add_index_entry(istate
, new_entry
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
177 void fill_stat_data(struct stat_data
*sd
, struct stat
*st
)
179 sd
->sd_ctime
.sec
= (unsigned int)st
->st_ctime
;
180 sd
->sd_mtime
.sec
= (unsigned int)st
->st_mtime
;
181 sd
->sd_ctime
.nsec
= ST_CTIME_NSEC(*st
);
182 sd
->sd_mtime
.nsec
= ST_MTIME_NSEC(*st
);
183 sd
->sd_dev
= st
->st_dev
;
184 sd
->sd_ino
= st
->st_ino
;
185 sd
->sd_uid
= st
->st_uid
;
186 sd
->sd_gid
= st
->st_gid
;
187 sd
->sd_size
= st
->st_size
;
190 int match_stat_data(const struct stat_data
*sd
, struct stat
*st
)
194 if (sd
->sd_mtime
.sec
!= (unsigned int)st
->st_mtime
)
195 changed
|= MTIME_CHANGED
;
196 if (trust_ctime
&& check_stat
&&
197 sd
->sd_ctime
.sec
!= (unsigned int)st
->st_ctime
)
198 changed
|= CTIME_CHANGED
;
201 if (check_stat
&& sd
->sd_mtime
.nsec
!= ST_MTIME_NSEC(*st
))
202 changed
|= MTIME_CHANGED
;
203 if (trust_ctime
&& check_stat
&&
204 sd
->sd_ctime
.nsec
!= ST_CTIME_NSEC(*st
))
205 changed
|= CTIME_CHANGED
;
209 if (sd
->sd_uid
!= (unsigned int) st
->st_uid
||
210 sd
->sd_gid
!= (unsigned int) st
->st_gid
)
211 changed
|= OWNER_CHANGED
;
212 if (sd
->sd_ino
!= (unsigned int) st
->st_ino
)
213 changed
|= INODE_CHANGED
;
218 * st_dev breaks on network filesystems where different
219 * clients will have different views of what "device"
220 * the filesystem is on
222 if (check_stat
&& sd
->sd_dev
!= (unsigned int) st
->st_dev
)
223 changed
|= INODE_CHANGED
;
226 if (sd
->sd_size
!= (unsigned int) st
->st_size
)
227 changed
|= DATA_CHANGED
;
233 * This only updates the "non-critical" parts of the directory
234 * cache, ie the parts that aren't tracked by GIT, and only used
235 * to validate the cache.
237 void fill_stat_cache_info(struct index_state
*istate
, struct cache_entry
*ce
, struct stat
*st
)
239 fill_stat_data(&ce
->ce_stat_data
, st
);
241 if (assume_unchanged
)
242 ce
->ce_flags
|= CE_VALID
;
244 if (S_ISREG(st
->st_mode
)) {
245 ce_mark_uptodate(ce
);
246 mark_fsmonitor_valid(istate
, ce
);
250 static int ce_compare_data(struct index_state
*istate
,
251 const struct cache_entry
*ce
,
255 int fd
= git_open_cloexec(ce
->name
, O_RDONLY
);
258 struct object_id oid
;
259 if (!index_fd(istate
, &oid
, fd
, st
, OBJ_BLOB
, ce
->name
, 0))
260 match
= !oideq(&oid
, &ce
->oid
);
261 /* index_fd() closed the file descriptor already */
266 static int ce_compare_link(const struct cache_entry
*ce
, size_t expected_size
)
271 enum object_type type
;
272 struct strbuf sb
= STRBUF_INIT
;
274 if (strbuf_readlink(&sb
, ce
->name
, expected_size
))
277 buffer
= repo_read_object_file(the_repository
, &ce
->oid
, &type
, &size
);
280 match
= memcmp(buffer
, sb
.buf
, size
);
287 static int ce_compare_gitlink(const struct cache_entry
*ce
)
289 struct object_id oid
;
292 * We don't actually require that the .git directory
293 * under GITLINK directory be a valid git directory. It
294 * might even be missing (in case nobody populated that
297 * If so, we consider it always to match.
299 if (resolve_gitlink_ref(ce
->name
, "HEAD", &oid
) < 0)
301 return !oideq(&oid
, &ce
->oid
);
304 static int ce_modified_check_fs(struct index_state
*istate
,
305 const struct cache_entry
*ce
,
308 switch (st
->st_mode
& S_IFMT
) {
310 if (ce_compare_data(istate
, ce
, st
))
314 if (ce_compare_link(ce
, xsize_t(st
->st_size
)))
318 if (S_ISGITLINK(ce
->ce_mode
))
319 return ce_compare_gitlink(ce
) ? DATA_CHANGED
: 0;
320 /* else fallthrough */
327 static int ce_match_stat_basic(const struct cache_entry
*ce
, struct stat
*st
)
329 unsigned int changed
= 0;
331 if (ce
->ce_flags
& CE_REMOVE
)
332 return MODE_CHANGED
| DATA_CHANGED
| TYPE_CHANGED
;
334 switch (ce
->ce_mode
& S_IFMT
) {
336 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
337 /* We consider only the owner x bit to be relevant for
340 if (trust_executable_bit
&&
341 (0100 & (ce
->ce_mode
^ st
->st_mode
)))
342 changed
|= MODE_CHANGED
;
345 if (!S_ISLNK(st
->st_mode
) &&
346 (has_symlinks
|| !S_ISREG(st
->st_mode
)))
347 changed
|= TYPE_CHANGED
;
350 /* We ignore most of the st_xxx fields for gitlinks */
351 if (!S_ISDIR(st
->st_mode
))
352 changed
|= TYPE_CHANGED
;
353 else if (ce_compare_gitlink(ce
))
354 changed
|= DATA_CHANGED
;
357 BUG("unsupported ce_mode: %o", ce
->ce_mode
);
360 changed
|= match_stat_data(&ce
->ce_stat_data
, st
);
362 /* Racily smudged entry? */
363 if (!ce
->ce_stat_data
.sd_size
) {
364 if (!is_empty_blob_sha1(ce
->oid
.hash
))
365 changed
|= DATA_CHANGED
;
371 static int is_racy_stat(const struct index_state
*istate
,
372 const struct stat_data
*sd
)
374 return (istate
->timestamp
.sec
&&
376 /* nanosecond timestamped files can also be racy! */
377 (istate
->timestamp
.sec
< sd
->sd_mtime
.sec
||
378 (istate
->timestamp
.sec
== sd
->sd_mtime
.sec
&&
379 istate
->timestamp
.nsec
<= sd
->sd_mtime
.nsec
))
381 istate
->timestamp
.sec
<= sd
->sd_mtime
.sec
386 int is_racy_timestamp(const struct index_state
*istate
,
387 const struct cache_entry
*ce
)
389 return (!S_ISGITLINK(ce
->ce_mode
) &&
390 is_racy_stat(istate
, &ce
->ce_stat_data
));
393 int match_stat_data_racy(const struct index_state
*istate
,
394 const struct stat_data
*sd
, struct stat
*st
)
396 if (is_racy_stat(istate
, sd
))
397 return MTIME_CHANGED
;
398 return match_stat_data(sd
, st
);
401 int ie_match_stat(struct index_state
*istate
,
402 const struct cache_entry
*ce
, struct stat
*st
,
403 unsigned int options
)
405 unsigned int changed
;
406 int ignore_valid
= options
& CE_MATCH_IGNORE_VALID
;
407 int ignore_skip_worktree
= options
& CE_MATCH_IGNORE_SKIP_WORKTREE
;
408 int assume_racy_is_modified
= options
& CE_MATCH_RACY_IS_DIRTY
;
409 int ignore_fsmonitor
= options
& CE_MATCH_IGNORE_FSMONITOR
;
411 if (!ignore_fsmonitor
)
412 refresh_fsmonitor(istate
);
414 * If it's marked as always valid in the index, it's
415 * valid whatever the checked-out copy says.
417 * skip-worktree has the same effect with higher precedence
419 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
421 if (!ignore_valid
&& (ce
->ce_flags
& CE_VALID
))
423 if (!ignore_fsmonitor
&& (ce
->ce_flags
& CE_FSMONITOR_VALID
))
427 * Intent-to-add entries have not been added, so the index entry
428 * by definition never matches what is in the work tree until it
429 * actually gets added.
431 if (ce_intent_to_add(ce
))
432 return DATA_CHANGED
| TYPE_CHANGED
| MODE_CHANGED
;
434 changed
= ce_match_stat_basic(ce
, st
);
437 * Within 1 second of this sequence:
438 * echo xyzzy >file && git-update-index --add file
439 * running this command:
441 * would give a falsely clean cache entry. The mtime and
442 * length match the cache, and other stat fields do not change.
444 * We could detect this at update-index time (the cache entry
445 * being registered/updated records the same time as "now")
446 * and delay the return from git-update-index, but that would
447 * effectively mean we can make at most one commit per second,
448 * which is not acceptable. Instead, we check cache entries
449 * whose mtime are the same as the index file timestamp more
450 * carefully than others.
452 if (!changed
&& is_racy_timestamp(istate
, ce
)) {
453 if (assume_racy_is_modified
)
454 changed
|= DATA_CHANGED
;
456 changed
|= ce_modified_check_fs(istate
, ce
, st
);
462 int ie_modified(struct index_state
*istate
,
463 const struct cache_entry
*ce
,
464 struct stat
*st
, unsigned int options
)
466 int changed
, changed_fs
;
468 changed
= ie_match_stat(istate
, ce
, st
, options
);
472 * If the mode or type has changed, there's no point in trying
473 * to refresh the entry - it's not going to match
475 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
479 * Immediately after read-tree or update-index --cacheinfo,
480 * the length field is zero, as we have never even read the
481 * lstat(2) information once, and we cannot trust DATA_CHANGED
482 * returned by ie_match_stat() which in turn was returned by
483 * ce_match_stat_basic() to signal that the filesize of the
484 * blob changed. We have to actually go to the filesystem to
485 * see if the contents match, and if so, should answer "unchanged".
487 * The logic does not apply to gitlinks, as ce_match_stat_basic()
488 * already has checked the actual HEAD from the filesystem in the
489 * subproject. If ie_match_stat() already said it is different,
490 * then we know it is.
492 if ((changed
& DATA_CHANGED
) &&
493 (S_ISGITLINK(ce
->ce_mode
) || ce
->ce_stat_data
.sd_size
!= 0))
496 changed_fs
= ce_modified_check_fs(istate
, ce
, st
);
498 return changed
| changed_fs
;
502 int base_name_compare(const char *name1
, size_t len1
, int mode1
,
503 const char *name2
, size_t len2
, int mode2
)
505 unsigned char c1
, c2
;
506 size_t len
= len1
< len2
? len1
: len2
;
509 cmp
= memcmp(name1
, name2
, len
);
514 if (!c1
&& S_ISDIR(mode1
))
516 if (!c2
&& S_ISDIR(mode2
))
518 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
522 * df_name_compare() is identical to base_name_compare(), except it
523 * compares conflicting directory/file entries as equal. Note that
524 * while a directory name compares as equal to a regular file, they
525 * then individually compare _differently_ to a filename that has
526 * a dot after the basename (because '\0' < '.' < '/').
528 * This is used by routines that want to traverse the git namespace
529 * but then handle conflicting entries together when possible.
531 int df_name_compare(const char *name1
, size_t len1
, int mode1
,
532 const char *name2
, size_t len2
, int mode2
)
534 unsigned char c1
, c2
;
535 size_t len
= len1
< len2
? len1
: len2
;
538 cmp
= memcmp(name1
, name2
, len
);
541 /* Directories and files compare equal (same length, same name) */
545 if (!c1
&& S_ISDIR(mode1
))
548 if (!c2
&& S_ISDIR(mode2
))
550 if (c1
== '/' && !c2
)
552 if (c2
== '/' && !c1
)
557 int name_compare(const char *name1
, size_t len1
, const char *name2
, size_t len2
)
559 size_t min_len
= (len1
< len2
) ? len1
: len2
;
560 int cmp
= memcmp(name1
, name2
, min_len
);
570 int cache_name_stage_compare(const char *name1
, int len1
, int stage1
, const char *name2
, int len2
, int stage2
)
574 cmp
= name_compare(name1
, len1
, name2
, len2
);
585 static int index_name_stage_pos(struct index_state
*istate
,
586 const char *name
, int namelen
,
588 enum index_search_mode search_mode
)
593 last
= istate
->cache_nr
;
594 while (last
> first
) {
595 int next
= first
+ ((last
- first
) >> 1);
596 struct cache_entry
*ce
= istate
->cache
[next
];
597 int cmp
= cache_name_stage_compare(name
, namelen
, stage
, ce
->name
, ce_namelen(ce
), ce_stage(ce
));
607 if (search_mode
== EXPAND_SPARSE
&& istate
->sparse_index
&&
609 /* Note: first <= istate->cache_nr */
610 struct cache_entry
*ce
= istate
->cache
[first
- 1];
613 * If we are in a sparse-index _and_ the entry before the
614 * insertion position is a sparse-directory entry that is
615 * an ancestor of 'name', then we need to expand the index
616 * and search again. This will only trigger once, because
617 * thereafter the index is fully expanded.
619 if (S_ISSPARSEDIR(ce
->ce_mode
) &&
620 ce_namelen(ce
) < namelen
&&
621 !strncmp(name
, ce
->name
, ce_namelen(ce
))) {
622 ensure_full_index(istate
);
623 return index_name_stage_pos(istate
, name
, namelen
, stage
, search_mode
);
630 int index_name_pos(struct index_state
*istate
, const char *name
, int namelen
)
632 return index_name_stage_pos(istate
, name
, namelen
, 0, EXPAND_SPARSE
);
635 int index_name_pos_sparse(struct index_state
*istate
, const char *name
, int namelen
)
637 return index_name_stage_pos(istate
, name
, namelen
, 0, NO_EXPAND_SPARSE
);
640 int index_entry_exists(struct index_state
*istate
, const char *name
, int namelen
)
642 return index_name_stage_pos(istate
, name
, namelen
, 0, NO_EXPAND_SPARSE
) >= 0;
645 int remove_index_entry_at(struct index_state
*istate
, int pos
)
647 struct cache_entry
*ce
= istate
->cache
[pos
];
649 record_resolve_undo(istate
, ce
);
650 remove_name_hash(istate
, ce
);
651 save_or_free_index_entry(istate
, ce
);
652 istate
->cache_changed
|= CE_ENTRY_REMOVED
;
654 if (pos
>= istate
->cache_nr
)
656 MOVE_ARRAY(istate
->cache
+ pos
, istate
->cache
+ pos
+ 1,
657 istate
->cache_nr
- pos
);
662 * Remove all cache entries marked for removal, that is where
663 * CE_REMOVE is set in ce_flags. This is much more effective than
664 * calling remove_index_entry_at() for each entry to be removed.
666 void remove_marked_cache_entries(struct index_state
*istate
, int invalidate
)
668 struct cache_entry
**ce_array
= istate
->cache
;
671 for (i
= j
= 0; i
< istate
->cache_nr
; i
++) {
672 if (ce_array
[i
]->ce_flags
& CE_REMOVE
) {
674 cache_tree_invalidate_path(istate
,
676 untracked_cache_remove_from_index(istate
,
679 remove_name_hash(istate
, ce_array
[i
]);
680 save_or_free_index_entry(istate
, ce_array
[i
]);
683 ce_array
[j
++] = ce_array
[i
];
685 if (j
== istate
->cache_nr
)
687 istate
->cache_changed
|= CE_ENTRY_REMOVED
;
688 istate
->cache_nr
= j
;
691 int remove_file_from_index(struct index_state
*istate
, const char *path
)
693 int pos
= index_name_pos(istate
, path
, strlen(path
));
696 cache_tree_invalidate_path(istate
, path
);
697 untracked_cache_remove_from_index(istate
, path
);
698 while (pos
< istate
->cache_nr
&& !strcmp(istate
->cache
[pos
]->name
, path
))
699 remove_index_entry_at(istate
, pos
);
703 static int compare_name(struct cache_entry
*ce
, const char *path
, int namelen
)
705 return namelen
!= ce_namelen(ce
) || memcmp(path
, ce
->name
, namelen
);
708 static int index_name_pos_also_unmerged(struct index_state
*istate
,
709 const char *path
, int namelen
)
711 int pos
= index_name_pos(istate
, path
, namelen
);
712 struct cache_entry
*ce
;
717 /* maybe unmerged? */
719 if (pos
>= istate
->cache_nr
||
720 compare_name((ce
= istate
->cache
[pos
]), path
, namelen
))
723 /* order of preference: stage 2, 1, 3 */
724 if (ce_stage(ce
) == 1 && pos
+ 1 < istate
->cache_nr
&&
725 ce_stage((ce
= istate
->cache
[pos
+ 1])) == 2 &&
726 !compare_name(ce
, path
, namelen
))
731 static int different_name(struct cache_entry
*ce
, struct cache_entry
*alias
)
733 int len
= ce_namelen(ce
);
734 return ce_namelen(alias
) != len
|| memcmp(ce
->name
, alias
->name
, len
);
738 * If we add a filename that aliases in the cache, we will use the
739 * name that we already have - but we don't want to update the same
740 * alias twice, because that implies that there were actually two
741 * different files with aliasing names!
743 * So we use the CE_ADDED flag to verify that the alias was an old
744 * one before we accept it as
746 static struct cache_entry
*create_alias_ce(struct index_state
*istate
,
747 struct cache_entry
*ce
,
748 struct cache_entry
*alias
)
751 struct cache_entry
*new_entry
;
753 if (alias
->ce_flags
& CE_ADDED
)
754 die(_("will not add file alias '%s' ('%s' already exists in index)"),
755 ce
->name
, alias
->name
);
757 /* Ok, create the new entry using the name of the existing alias */
758 len
= ce_namelen(alias
);
759 new_entry
= make_empty_cache_entry(istate
, len
);
760 memcpy(new_entry
->name
, alias
->name
, len
);
761 copy_cache_entry(new_entry
, ce
);
762 save_or_free_index_entry(istate
, ce
);
766 void set_object_name_for_intent_to_add_entry(struct cache_entry
*ce
)
768 struct object_id oid
;
769 if (write_object_file("", 0, OBJ_BLOB
, &oid
))
770 die(_("cannot create an empty blob in the object database"));
771 oidcpy(&ce
->oid
, &oid
);
774 int add_to_index(struct index_state
*istate
, const char *path
, struct stat
*st
, int flags
)
776 int namelen
, was_same
;
777 mode_t st_mode
= st
->st_mode
;
778 struct cache_entry
*ce
, *alias
= NULL
;
779 unsigned ce_option
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
|CE_MATCH_RACY_IS_DIRTY
;
780 int verbose
= flags
& (ADD_CACHE_VERBOSE
| ADD_CACHE_PRETEND
);
781 int pretend
= flags
& ADD_CACHE_PRETEND
;
782 int intent_only
= flags
& ADD_CACHE_INTENT
;
783 int add_option
= (ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
|
784 (intent_only
? ADD_CACHE_NEW_ONLY
: 0));
785 unsigned hash_flags
= pretend
? 0 : HASH_WRITE_OBJECT
;
786 struct object_id oid
;
788 if (flags
& ADD_CACHE_RENORMALIZE
)
789 hash_flags
|= HASH_RENORMALIZE
;
791 if (!S_ISREG(st_mode
) && !S_ISLNK(st_mode
) && !S_ISDIR(st_mode
))
792 return error(_("%s: can only add regular files, symbolic links or git-directories"), path
);
794 namelen
= strlen(path
);
795 if (S_ISDIR(st_mode
)) {
796 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
797 return error(_("'%s' does not have a commit checked out"), path
);
798 while (namelen
&& path
[namelen
-1] == '/')
801 ce
= make_empty_cache_entry(istate
, namelen
);
802 memcpy(ce
->name
, path
, namelen
);
803 ce
->ce_namelen
= namelen
;
805 fill_stat_cache_info(istate
, ce
, st
);
807 ce
->ce_flags
|= CE_INTENT_TO_ADD
;
810 if (trust_executable_bit
&& has_symlinks
) {
811 ce
->ce_mode
= create_ce_mode(st_mode
);
813 /* If there is an existing entry, pick the mode bits and type
814 * from it, otherwise assume unexecutable regular file.
816 struct cache_entry
*ent
;
817 int pos
= index_name_pos_also_unmerged(istate
, path
, namelen
);
819 ent
= (0 <= pos
) ? istate
->cache
[pos
] : NULL
;
820 ce
->ce_mode
= ce_mode_from_stat(ent
, st_mode
);
823 /* When core.ignorecase=true, determine if a directory of the same name but differing
824 * case already exists within the Git repository. If it does, ensure the directory
825 * case of the file being added to the repository matches (is folded into) the existing
826 * entry's directory case.
829 adjust_dirname_case(istate
, ce
->name
);
831 if (!(flags
& ADD_CACHE_RENORMALIZE
)) {
832 alias
= index_file_exists(istate
, ce
->name
,
833 ce_namelen(ce
), ignore_case
);
836 !ie_match_stat(istate
, alias
, st
, ce_option
)) {
837 /* Nothing changed, really */
838 if (!S_ISGITLINK(alias
->ce_mode
))
839 ce_mark_uptodate(alias
);
840 alias
->ce_flags
|= CE_ADDED
;
842 discard_cache_entry(ce
);
847 if (index_path(istate
, &ce
->oid
, path
, st
, hash_flags
)) {
848 discard_cache_entry(ce
);
849 return error(_("unable to index file '%s'"), path
);
852 set_object_name_for_intent_to_add_entry(ce
);
854 if (ignore_case
&& alias
&& different_name(ce
, alias
))
855 ce
= create_alias_ce(istate
, ce
, alias
);
856 ce
->ce_flags
|= CE_ADDED
;
858 /* It was suspected to be racily clean, but it turns out to be Ok */
861 oideq(&alias
->oid
, &ce
->oid
) &&
862 ce
->ce_mode
== alias
->ce_mode
);
865 discard_cache_entry(ce
);
866 else if (add_index_entry(istate
, ce
, add_option
)) {
867 discard_cache_entry(ce
);
868 return error(_("unable to add '%s' to index"), path
);
870 if (verbose
&& !was_same
)
871 printf("add '%s'\n", path
);
875 int add_file_to_index(struct index_state
*istate
, const char *path
, int flags
)
878 if (lstat(path
, &st
))
879 die_errno(_("unable to stat '%s'"), path
);
880 return add_to_index(istate
, path
, &st
, flags
);
883 struct cache_entry
*make_empty_cache_entry(struct index_state
*istate
, size_t len
)
885 return mem_pool__ce_calloc(find_mem_pool(istate
), len
);
888 struct cache_entry
*make_empty_transient_cache_entry(size_t len
,
889 struct mem_pool
*ce_mem_pool
)
892 return mem_pool__ce_calloc(ce_mem_pool
, len
);
893 return xcalloc(1, cache_entry_size(len
));
896 enum verify_path_result
{
902 static enum verify_path_result
verify_path_internal(const char *, unsigned);
904 int verify_path(const char *path
, unsigned mode
)
906 return verify_path_internal(path
, mode
) == PATH_OK
;
909 struct cache_entry
*make_cache_entry(struct index_state
*istate
,
911 const struct object_id
*oid
,
914 unsigned int refresh_options
)
916 struct cache_entry
*ce
, *ret
;
919 if (verify_path_internal(path
, mode
) == PATH_INVALID
) {
920 error(_("invalid path '%s'"), path
);
925 ce
= make_empty_cache_entry(istate
, len
);
927 oidcpy(&ce
->oid
, oid
);
928 memcpy(ce
->name
, path
, len
);
929 ce
->ce_flags
= create_ce_flags(stage
);
930 ce
->ce_namelen
= len
;
931 ce
->ce_mode
= create_ce_mode(mode
);
933 ret
= refresh_cache_entry(istate
, ce
, refresh_options
);
935 discard_cache_entry(ce
);
939 struct cache_entry
*make_transient_cache_entry(unsigned int mode
,
940 const struct object_id
*oid
,
943 struct mem_pool
*ce_mem_pool
)
945 struct cache_entry
*ce
;
948 if (!verify_path(path
, mode
)) {
949 error(_("invalid path '%s'"), path
);
954 ce
= make_empty_transient_cache_entry(len
, ce_mem_pool
);
956 oidcpy(&ce
->oid
, oid
);
957 memcpy(ce
->name
, path
, len
);
958 ce
->ce_flags
= create_ce_flags(stage
);
959 ce
->ce_namelen
= len
;
960 ce
->ce_mode
= create_ce_mode(mode
);
966 * Chmod an index entry with either +x or -x.
968 * Returns -1 if the chmod for the particular cache entry failed (if it's
969 * not a regular file), -2 if an invalid flip argument is passed in, 0
972 int chmod_index_entry(struct index_state
*istate
, struct cache_entry
*ce
,
975 if (!S_ISREG(ce
->ce_mode
))
982 ce
->ce_mode
&= ~0111;
987 cache_tree_invalidate_path(istate
, ce
->name
);
988 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
989 mark_fsmonitor_invalid(istate
, ce
);
990 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
995 int ce_same_name(const struct cache_entry
*a
, const struct cache_entry
*b
)
997 int len
= ce_namelen(a
);
998 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
1002 * We fundamentally don't like some paths: we don't want
1003 * dot or dot-dot anywhere, and for obvious reasons don't
1004 * want to recurse into ".git" either.
1006 * Also, we don't want double slashes or slashes at the
1007 * end that can make pathnames ambiguous.
1009 static int verify_dotfile(const char *rest
, unsigned mode
)
1012 * The first character was '.', but that
1013 * has already been discarded, we now test
1017 /* "." is not allowed */
1018 if (*rest
== '\0' || is_dir_sep(*rest
))
1023 * ".git" followed by NUL or slash is bad. Note that we match
1024 * case-insensitively here, even if ignore_case is not set.
1025 * This outlaws ".GIT" everywhere out of an abundance of caution,
1026 * since there's really no good reason to allow it.
1028 * Once we've seen ".git", we can also find ".gitmodules", etc (also
1029 * case-insensitively).
1033 if (rest
[1] != 'i' && rest
[1] != 'I')
1035 if (rest
[2] != 't' && rest
[2] != 'T')
1037 if (rest
[3] == '\0' || is_dir_sep(rest
[3]))
1039 if (S_ISLNK(mode
)) {
1041 if (skip_iprefix(rest
, "modules", &rest
) &&
1042 (*rest
== '\0' || is_dir_sep(*rest
)))
1047 if (rest
[1] == '\0' || is_dir_sep(rest
[1]))
1053 static enum verify_path_result
verify_path_internal(const char *path
,
1058 if (has_dos_drive_prefix(path
))
1059 return PATH_INVALID
;
1061 if (!is_valid_path(path
))
1062 return PATH_INVALID
;
1068 if (is_dir_sep(c
)) {
1072 if (is_hfs_dotgit(path
))
1073 return PATH_INVALID
;
1074 if (S_ISLNK(mode
)) {
1075 if (is_hfs_dotgitmodules(path
))
1076 return PATH_INVALID
;
1080 #if defined GIT_WINDOWS_NATIVE || defined __CYGWIN__
1082 return PATH_INVALID
;
1084 if (is_ntfs_dotgit(path
))
1085 return PATH_INVALID
;
1086 if (S_ISLNK(mode
)) {
1087 if (is_ntfs_dotgitmodules(path
))
1088 return PATH_INVALID
;
1093 if ((c
== '.' && !verify_dotfile(path
, mode
)) ||
1095 return PATH_INVALID
;
1097 * allow terminating directory separators for
1098 * sparse directory entries.
1101 return S_ISDIR(mode
) ? PATH_DIR_WITH_SEP
:
1103 } else if (c
== '\\' && protect_ntfs
) {
1104 if (is_ntfs_dotgit(path
))
1105 return PATH_INVALID
;
1106 if (S_ISLNK(mode
)) {
1107 if (is_ntfs_dotgitmodules(path
))
1108 return PATH_INVALID
;
1117 * Do we have another file that has the beginning components being a
1118 * proper superset of the name we're trying to add?
1120 static int has_file_name(struct index_state
*istate
,
1121 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
1124 int len
= ce_namelen(ce
);
1125 int stage
= ce_stage(ce
);
1126 const char *name
= ce
->name
;
1128 while (pos
< istate
->cache_nr
) {
1129 struct cache_entry
*p
= istate
->cache
[pos
++];
1131 if (len
>= ce_namelen(p
))
1133 if (memcmp(name
, p
->name
, len
))
1135 if (ce_stage(p
) != stage
)
1137 if (p
->name
[len
] != '/')
1139 if (p
->ce_flags
& CE_REMOVE
)
1144 remove_index_entry_at(istate
, --pos
);
1151 * Like strcmp(), but also return the offset of the first change.
1152 * If strings are equal, return the length.
1154 int strcmp_offset(const char *s1
, const char *s2
, size_t *first_change
)
1159 return strcmp(s1
, s2
);
1161 for (k
= 0; s1
[k
] == s2
[k
]; k
++)
1166 return (unsigned char)s1
[k
] - (unsigned char)s2
[k
];
1170 * Do we have another file with a pathname that is a proper
1171 * subset of the name we're trying to add?
1173 * That is, is there another file in the index with a path
1174 * that matches a sub-directory in the given entry?
1176 static int has_dir_name(struct index_state
*istate
,
1177 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
1180 int stage
= ce_stage(ce
);
1181 const char *name
= ce
->name
;
1182 const char *slash
= name
+ ce_namelen(ce
);
1187 * We are frequently called during an iteration on a sorted
1188 * list of pathnames and while building a new index. Therefore,
1189 * there is a high probability that this entry will eventually
1190 * be appended to the index, rather than inserted in the middle.
1191 * If we can confirm that, we can avoid binary searches on the
1192 * components of the pathname.
1194 * Compare the entry's full path with the last path in the index.
1196 if (istate
->cache_nr
> 0) {
1197 cmp_last
= strcmp_offset(name
,
1198 istate
->cache
[istate
->cache_nr
- 1]->name
,
1201 if (len_eq_last
== 0) {
1203 * The entry sorts AFTER the last one in the
1204 * index and their paths have no common prefix,
1205 * so there cannot be a F/D conflict.
1210 * The entry sorts AFTER the last one in the
1211 * index, but has a common prefix. Fall through
1212 * to the loop below to disect the entry's path
1213 * and see where the difference is.
1216 } else if (cmp_last
== 0) {
1218 * The entry exactly matches the last one in the
1219 * index, but because of multiple stage and CE_REMOVE
1220 * items, we fall through and let the regular search
1230 if (*--slash
== '/')
1232 if (slash
<= ce
->name
)
1239 * (len + 1) is a directory boundary (including
1240 * the trailing slash). And since the loop is
1241 * decrementing "slash", the first iteration is
1242 * the longest directory prefix; subsequent
1243 * iterations consider parent directories.
1246 if (len
+ 1 <= len_eq_last
) {
1248 * The directory prefix (including the trailing
1249 * slash) also appears as a prefix in the last
1250 * entry, so the remainder cannot collide (because
1251 * strcmp said the whole path was greater).
1256 * LT: last: xxx/file_A
1262 if (len
> len_eq_last
) {
1264 * This part of the directory prefix (excluding
1265 * the trailing slash) is longer than the known
1266 * equal portions, so this sub-directory cannot
1267 * collide with a file.
1276 * This is a possible collision. Fall through and
1277 * let the regular search code handle it.
1284 pos
= index_name_stage_pos(istate
, name
, len
, stage
, EXPAND_SPARSE
);
1287 * Found one, but not so fast. This could
1288 * be a marker that says "I was here, but
1289 * I am being removed". Such an entry is
1290 * not a part of the resulting tree, and
1291 * it is Ok to have a directory at the same
1294 if (!(istate
->cache
[pos
]->ce_flags
& CE_REMOVE
)) {
1298 remove_index_entry_at(istate
, pos
);
1306 * Trivial optimization: if we find an entry that
1307 * already matches the sub-directory, then we know
1308 * we're ok, and we can exit.
1310 while (pos
< istate
->cache_nr
) {
1311 struct cache_entry
*p
= istate
->cache
[pos
];
1312 if ((ce_namelen(p
) <= len
) ||
1313 (p
->name
[len
] != '/') ||
1314 memcmp(p
->name
, name
, len
))
1315 break; /* not our subdirectory */
1316 if (ce_stage(p
) == stage
&& !(p
->ce_flags
& CE_REMOVE
))
1318 * p is at the same stage as our entry, and
1319 * is a subdirectory of what we are looking
1320 * at, so we cannot have conflicts at our
1321 * level or anything shorter.
1330 /* We may be in a situation where we already have path/file and path
1331 * is being added, or we already have path and path/file is being
1332 * added. Either one would result in a nonsense tree that has path
1333 * twice when git-write-tree tries to write it out. Prevent it.
1335 * If ok-to-replace is specified, we remove the conflicting entries
1336 * from the cache so the caller should recompute the insert position.
1337 * When this happens, we return non-zero.
1339 static int check_file_directory_conflict(struct index_state
*istate
,
1340 const struct cache_entry
*ce
,
1341 int pos
, int ok_to_replace
)
1346 * When ce is an "I am going away" entry, we allow it to be added
1348 if (ce
->ce_flags
& CE_REMOVE
)
1352 * We check if the path is a sub-path of a subsequent pathname
1353 * first, since removing those will not change the position
1356 retval
= has_file_name(istate
, ce
, pos
, ok_to_replace
);
1359 * Then check if the path might have a clashing sub-directory
1362 return retval
+ has_dir_name(istate
, ce
, pos
, ok_to_replace
);
1365 static int add_index_entry_with_check(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
1368 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
1369 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
1370 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
1371 int new_only
= option
& ADD_CACHE_NEW_ONLY
;
1374 * If this entry's path sorts after the last entry in the index,
1375 * we can avoid searching for it.
1377 if (istate
->cache_nr
> 0 &&
1378 strcmp(ce
->name
, istate
->cache
[istate
->cache_nr
- 1]->name
) > 0)
1379 pos
= index_pos_to_insert_pos(istate
->cache_nr
);
1381 pos
= index_name_stage_pos(istate
, ce
->name
, ce_namelen(ce
), ce_stage(ce
), EXPAND_SPARSE
);
1384 * Cache tree path should be invalidated only after index_name_stage_pos,
1385 * in case it expands a sparse index.
1387 if (!(option
& ADD_CACHE_KEEP_CACHE_TREE
))
1388 cache_tree_invalidate_path(istate
, ce
->name
);
1390 /* existing match? Just replace it. */
1393 replace_index_entry(istate
, pos
, ce
);
1398 if (!(option
& ADD_CACHE_KEEP_CACHE_TREE
))
1399 untracked_cache_add_to_index(istate
, ce
->name
);
1402 * Inserting a merged entry ("stage 0") into the index
1403 * will always replace all non-merged entries..
1405 if (pos
< istate
->cache_nr
&& ce_stage(ce
) == 0) {
1406 while (ce_same_name(istate
->cache
[pos
], ce
)) {
1408 if (!remove_index_entry_at(istate
, pos
))
1415 if (verify_path_internal(ce
->name
, ce
->ce_mode
) == PATH_INVALID
)
1416 return error(_("invalid path '%s'"), ce
->name
);
1418 if (!skip_df_check
&&
1419 check_file_directory_conflict(istate
, ce
, pos
, ok_to_replace
)) {
1421 return error(_("'%s' appears as both a file and as a directory"),
1423 pos
= index_name_stage_pos(istate
, ce
->name
, ce_namelen(ce
), ce_stage(ce
), EXPAND_SPARSE
);
1429 int add_index_entry(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
1433 if (option
& ADD_CACHE_JUST_APPEND
)
1434 pos
= istate
->cache_nr
;
1437 ret
= add_index_entry_with_check(istate
, ce
, option
);
1443 /* Make sure the array is big enough .. */
1444 ALLOC_GROW(istate
->cache
, istate
->cache_nr
+ 1, istate
->cache_alloc
);
1448 if (istate
->cache_nr
> pos
+ 1)
1449 MOVE_ARRAY(istate
->cache
+ pos
+ 1, istate
->cache
+ pos
,
1450 istate
->cache_nr
- pos
- 1);
1451 set_index_entry(istate
, pos
, ce
);
1452 istate
->cache_changed
|= CE_ENTRY_ADDED
;
1457 * "refresh" does not calculate a new sha1 file or bring the
1458 * cache up-to-date for mode/content changes. But what it
1459 * _does_ do is to "re-match" the stat information of a file
1460 * with the cache, so that you can refresh the cache for a
1461 * file that hasn't been changed but where the stat entry is
1464 * For example, you'd want to do this after doing a "git-read-tree",
1465 * to link up the stat cache details with the proper files.
1467 static struct cache_entry
*refresh_cache_ent(struct index_state
*istate
,
1468 struct cache_entry
*ce
,
1469 unsigned int options
, int *err
,
1475 struct cache_entry
*updated
;
1477 int refresh
= options
& CE_MATCH_REFRESH
;
1478 int ignore_valid
= options
& CE_MATCH_IGNORE_VALID
;
1479 int ignore_skip_worktree
= options
& CE_MATCH_IGNORE_SKIP_WORKTREE
;
1480 int ignore_missing
= options
& CE_MATCH_IGNORE_MISSING
;
1481 int ignore_fsmonitor
= options
& CE_MATCH_IGNORE_FSMONITOR
;
1483 if (!refresh
|| ce_uptodate(ce
))
1486 if (!ignore_fsmonitor
)
1487 refresh_fsmonitor(istate
);
1489 * CE_VALID or CE_SKIP_WORKTREE means the user promised us
1490 * that the change to the work tree does not matter and told
1493 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
)) {
1494 ce_mark_uptodate(ce
);
1497 if (!ignore_valid
&& (ce
->ce_flags
& CE_VALID
)) {
1498 ce_mark_uptodate(ce
);
1501 if (!ignore_fsmonitor
&& (ce
->ce_flags
& CE_FSMONITOR_VALID
)) {
1502 ce_mark_uptodate(ce
);
1506 if (has_symlink_leading_path(ce
->name
, ce_namelen(ce
))) {
1516 if (lstat(ce
->name
, &st
) < 0) {
1517 if (ignore_missing
&& errno
== ENOENT
)
1524 changed
= ie_match_stat(istate
, ce
, &st
, options
);
1526 *changed_ret
= changed
;
1529 * The path is unchanged. If we were told to ignore
1530 * valid bit, then we did the actual stat check and
1531 * found that the entry is unmodified. If the entry
1532 * is not marked VALID, this is the place to mark it
1533 * valid again, under "assume unchanged" mode.
1535 if (ignore_valid
&& assume_unchanged
&&
1536 !(ce
->ce_flags
& CE_VALID
))
1537 ; /* mark this one VALID again */
1540 * We do not mark the index itself "modified"
1541 * because CE_UPTODATE flag is in-core only;
1542 * we are not going to write this change out.
1544 if (!S_ISGITLINK(ce
->ce_mode
)) {
1545 ce_mark_uptodate(ce
);
1546 mark_fsmonitor_valid(istate
, ce
);
1554 if (ie_modified(istate
, ce
, &st
, options
)) {
1560 updated
= make_empty_cache_entry(istate
, ce_namelen(ce
));
1561 copy_cache_entry(updated
, ce
);
1562 memcpy(updated
->name
, ce
->name
, ce
->ce_namelen
+ 1);
1563 fill_stat_cache_info(istate
, updated
, &st
);
1565 * If ignore_valid is not set, we should leave CE_VALID bit
1566 * alone. Otherwise, paths marked with --no-assume-unchanged
1567 * (i.e. things to be edited) will reacquire CE_VALID bit
1568 * automatically, which is not really what we want.
1570 if (!ignore_valid
&& assume_unchanged
&&
1571 !(ce
->ce_flags
& CE_VALID
))
1572 updated
->ce_flags
&= ~CE_VALID
;
1574 /* istate->cache_changed is updated in the caller */
1578 static void show_file(const char * fmt
, const char * name
, int in_porcelain
,
1579 int * first
, const char *header_msg
)
1581 if (in_porcelain
&& *first
&& header_msg
) {
1582 printf("%s\n", header_msg
);
1588 int repo_refresh_and_write_index(struct repository
*repo
,
1589 unsigned int refresh_flags
,
1590 unsigned int write_flags
,
1592 const struct pathspec
*pathspec
,
1593 char *seen
, const char *header_msg
)
1595 struct lock_file lock_file
= LOCK_INIT
;
1598 fd
= repo_hold_locked_index(repo
, &lock_file
, 0);
1599 if (!gentle
&& fd
< 0)
1601 if (refresh_index(repo
->index
, refresh_flags
, pathspec
, seen
, header_msg
))
1603 if (0 <= fd
&& write_locked_index(repo
->index
, &lock_file
, COMMIT_LOCK
| write_flags
))
1609 int refresh_index(struct index_state
*istate
, unsigned int flags
,
1610 const struct pathspec
*pathspec
,
1611 char *seen
, const char *header_msg
)
1615 int really
= (flags
& REFRESH_REALLY
) != 0;
1616 int allow_unmerged
= (flags
& REFRESH_UNMERGED
) != 0;
1617 int quiet
= (flags
& REFRESH_QUIET
) != 0;
1618 int not_new
= (flags
& REFRESH_IGNORE_MISSING
) != 0;
1619 int ignore_submodules
= (flags
& REFRESH_IGNORE_SUBMODULES
) != 0;
1620 int ignore_skip_worktree
= (flags
& REFRESH_IGNORE_SKIP_WORKTREE
) != 0;
1622 int in_porcelain
= (flags
& REFRESH_IN_PORCELAIN
);
1623 unsigned int options
= (CE_MATCH_REFRESH
|
1624 (really
? CE_MATCH_IGNORE_VALID
: 0) |
1625 (not_new
? CE_MATCH_IGNORE_MISSING
: 0));
1626 const char *modified_fmt
;
1627 const char *deleted_fmt
;
1628 const char *typechange_fmt
;
1629 const char *added_fmt
;
1630 const char *unmerged_fmt
;
1631 struct progress
*progress
= NULL
;
1632 int t2_sum_lstat
= 0;
1633 int t2_sum_scan
= 0;
1635 if (flags
& REFRESH_PROGRESS
&& isatty(2))
1636 progress
= start_delayed_progress(_("Refresh index"),
1639 trace_performance_enter();
1640 modified_fmt
= in_porcelain
? "M\t%s\n" : "%s: needs update\n";
1641 deleted_fmt
= in_porcelain
? "D\t%s\n" : "%s: needs update\n";
1642 typechange_fmt
= in_porcelain
? "T\t%s\n" : "%s: needs update\n";
1643 added_fmt
= in_porcelain
? "A\t%s\n" : "%s: needs update\n";
1644 unmerged_fmt
= in_porcelain
? "U\t%s\n" : "%s: needs merge\n";
1646 * Use the multi-threaded preload_index() to refresh most of the
1647 * cache entries quickly then in the single threaded loop below,
1648 * we only have to do the special cases that are left.
1650 preload_index(istate
, pathspec
, 0);
1651 trace2_region_enter("index", "refresh", NULL
);
1653 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1654 struct cache_entry
*ce
, *new_entry
;
1655 int cache_errno
= 0;
1658 int t2_did_lstat
= 0;
1659 int t2_did_scan
= 0;
1661 ce
= istate
->cache
[i
];
1662 if (ignore_submodules
&& S_ISGITLINK(ce
->ce_mode
))
1664 if (ignore_skip_worktree
&& ce_skip_worktree(ce
))
1668 * If this entry is a sparse directory, then there isn't
1669 * any stat() information to update. Ignore the entry.
1671 if (S_ISSPARSEDIR(ce
->ce_mode
))
1674 if (pathspec
&& !ce_path_match(istate
, ce
, pathspec
, seen
))
1678 while ((i
< istate
->cache_nr
) &&
1679 ! strcmp(istate
->cache
[i
]->name
, ce
->name
))
1685 show_file(unmerged_fmt
, ce
->name
, in_porcelain
,
1686 &first
, header_msg
);
1694 new_entry
= refresh_cache_ent(istate
, ce
, options
,
1695 &cache_errno
, &changed
,
1696 &t2_did_lstat
, &t2_did_scan
);
1697 t2_sum_lstat
+= t2_did_lstat
;
1698 t2_sum_scan
+= t2_did_scan
;
1699 if (new_entry
== ce
)
1701 display_progress(progress
, i
);
1705 if (really
&& cache_errno
== EINVAL
) {
1706 /* If we are doing --really-refresh that
1707 * means the index is not valid anymore.
1709 ce
->ce_flags
&= ~CE_VALID
;
1710 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
1711 mark_fsmonitor_invalid(istate
, ce
);
1712 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
1717 if (cache_errno
== ENOENT
)
1719 else if (ce_intent_to_add(ce
))
1720 fmt
= added_fmt
; /* must be before other checks */
1721 else if (changed
& TYPE_CHANGED
)
1722 fmt
= typechange_fmt
;
1726 ce
->name
, in_porcelain
, &first
, header_msg
);
1731 replace_index_entry(istate
, i
, new_entry
);
1733 trace2_data_intmax("index", NULL
, "refresh/sum_lstat", t2_sum_lstat
);
1734 trace2_data_intmax("index", NULL
, "refresh/sum_scan", t2_sum_scan
);
1735 trace2_region_leave("index", "refresh", NULL
);
1736 display_progress(progress
, istate
->cache_nr
);
1737 stop_progress(&progress
);
1738 trace_performance_leave("refresh index");
1742 struct cache_entry
*refresh_cache_entry(struct index_state
*istate
,
1743 struct cache_entry
*ce
,
1744 unsigned int options
)
1746 return refresh_cache_ent(istate
, ce
, options
, NULL
, NULL
, NULL
, NULL
);
1750 /*****************************************************************
1752 *****************************************************************/
1754 #define INDEX_FORMAT_DEFAULT 3
1756 static unsigned int get_index_format_default(struct repository
*r
)
1758 char *envversion
= getenv("GIT_INDEX_VERSION");
1760 unsigned int version
= INDEX_FORMAT_DEFAULT
;
1763 prepare_repo_settings(r
);
1765 if (r
->settings
.index_version
>= 0)
1766 version
= r
->settings
.index_version
;
1767 if (version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< version
) {
1768 warning(_("index.version set, but the value is invalid.\n"
1769 "Using version %i"), INDEX_FORMAT_DEFAULT
);
1770 return INDEX_FORMAT_DEFAULT
;
1775 version
= strtoul(envversion
, &endp
, 10);
1777 version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< version
) {
1778 warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1779 "Using version %i"), INDEX_FORMAT_DEFAULT
);
1780 version
= INDEX_FORMAT_DEFAULT
;
1786 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
1787 * Again - this is just a (very strong in practice) heuristic that
1788 * the inode hasn't changed.
1790 * We save the fields in big-endian order to allow using the
1791 * index file over NFS transparently.
1793 struct ondisk_cache_entry
{
1794 struct cache_time ctime
;
1795 struct cache_time mtime
;
1803 * unsigned char hash[hashsz];
1805 * if (flags & CE_EXTENDED)
1808 unsigned char data
[GIT_MAX_RAWSZ
+ 2 * sizeof(uint16_t)];
1809 char name
[FLEX_ARRAY
];
1812 /* These are only used for v3 or lower */
1813 #define align_padding_size(size, len) ((size + (len) + 8) & ~7) - (size + len)
1814 #define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,data) + (len) + 8) & ~7)
1815 #define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)
1816 #define ondisk_data_size(flags, len) (the_hash_algo->rawsz + \
1817 ((flags & CE_EXTENDED) ? 2 : 1) * sizeof(uint16_t) + len)
1818 #define ondisk_data_size_max(len) (ondisk_data_size(CE_EXTENDED, len))
1819 #define ondisk_ce_size(ce) (ondisk_cache_entry_size(ondisk_data_size((ce)->ce_flags, ce_namelen(ce))))
1821 /* Allow fsck to force verification of the index checksum. */
1822 int verify_index_checksum
;
1824 /* Allow fsck to force verification of the cache entry order. */
1825 int verify_ce_order
;
1827 static int verify_hdr(const struct cache_header
*hdr
, unsigned long size
)
1830 unsigned char hash
[GIT_MAX_RAWSZ
];
1832 unsigned char *start
, *end
;
1833 struct object_id oid
;
1835 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
1836 return error(_("bad signature 0x%08x"), hdr
->hdr_signature
);
1837 hdr_version
= ntohl(hdr
->hdr_version
);
1838 if (hdr_version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< hdr_version
)
1839 return error(_("bad index version %d"), hdr_version
);
1841 if (!verify_index_checksum
)
1844 end
= (unsigned char *)hdr
+ size
;
1845 start
= end
- the_hash_algo
->rawsz
;
1846 oidread(&oid
, start
);
1847 if (oideq(&oid
, null_oid()))
1850 the_hash_algo
->init_fn(&c
);
1851 the_hash_algo
->update_fn(&c
, hdr
, size
- the_hash_algo
->rawsz
);
1852 the_hash_algo
->final_fn(hash
, &c
);
1853 if (!hasheq(hash
, start
))
1854 return error(_("bad index file sha1 signature"));
1858 static int read_index_extension(struct index_state
*istate
,
1859 const char *ext
, const char *data
, unsigned long sz
)
1861 switch (CACHE_EXT(ext
)) {
1862 case CACHE_EXT_TREE
:
1863 istate
->cache_tree
= cache_tree_read(data
, sz
);
1865 case CACHE_EXT_RESOLVE_UNDO
:
1866 istate
->resolve_undo
= resolve_undo_read(data
, sz
);
1868 case CACHE_EXT_LINK
:
1869 if (read_link_extension(istate
, data
, sz
))
1872 case CACHE_EXT_UNTRACKED
:
1873 istate
->untracked
= read_untracked_extension(data
, sz
);
1875 case CACHE_EXT_FSMONITOR
:
1876 read_fsmonitor_extension(istate
, data
, sz
);
1878 case CACHE_EXT_ENDOFINDEXENTRIES
:
1879 case CACHE_EXT_INDEXENTRYOFFSETTABLE
:
1880 /* already handled in do_read_index() */
1882 case CACHE_EXT_SPARSE_DIRECTORIES
:
1883 /* no content, only an indicator */
1884 istate
->sparse_index
= INDEX_COLLAPSED
;
1887 if (*ext
< 'A' || 'Z' < *ext
)
1888 return error(_("index uses %.4s extension, which we do not understand"),
1890 fprintf_ln(stderr
, _("ignoring %.4s extension"), ext
);
1897 * Parses the contents of the cache entry contained within the 'ondisk' buffer
1898 * into a new incore 'cache_entry'.
1900 * Note that 'char *ondisk' may not be aligned to a 4-byte address interval in
1901 * index v4, so we cannot cast it to 'struct ondisk_cache_entry *' and access
1902 * its members. Instead, we use the byte offsets of members within the struct to
1903 * identify where 'get_be16()', 'get_be32()', and 'oidread()' (which can all
1904 * read from an unaligned memory buffer) should read from the 'ondisk' buffer
1905 * into the corresponding incore 'cache_entry' members.
1907 static struct cache_entry
*create_from_disk(struct mem_pool
*ce_mem_pool
,
1908 unsigned int version
,
1910 unsigned long *ent_size
,
1911 const struct cache_entry
*previous_ce
)
1913 struct cache_entry
*ce
;
1916 const unsigned hashsz
= the_hash_algo
->rawsz
;
1917 const char *flagsp
= ondisk
+ offsetof(struct ondisk_cache_entry
, data
) + hashsz
;
1919 size_t copy_len
= 0;
1921 * Adjacent cache entries tend to share the leading paths, so it makes
1922 * sense to only store the differences in later entries. In the v4
1923 * on-disk format of the index, each on-disk cache entry stores the
1924 * number of bytes to be stripped from the end of the previous name,
1925 * and the bytes to append to the result, to come up with its name.
1927 int expand_name_field
= version
== 4;
1929 /* On-disk flags are just 16 bits */
1930 flags
= get_be16(flagsp
);
1931 len
= flags
& CE_NAMEMASK
;
1933 if (flags
& CE_EXTENDED
) {
1935 extended_flags
= get_be16(flagsp
+ sizeof(uint16_t)) << 16;
1936 /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1937 if (extended_flags
& ~CE_EXTENDED_FLAGS
)
1938 die(_("unknown index entry format 0x%08x"), extended_flags
);
1939 flags
|= extended_flags
;
1940 name
= (const char *)(flagsp
+ 2 * sizeof(uint16_t));
1943 name
= (const char *)(flagsp
+ sizeof(uint16_t));
1945 if (expand_name_field
) {
1946 const unsigned char *cp
= (const unsigned char *)name
;
1947 size_t strip_len
, previous_len
;
1949 /* If we're at the beginning of a block, ignore the previous name */
1950 strip_len
= decode_varint(&cp
);
1952 previous_len
= previous_ce
->ce_namelen
;
1953 if (previous_len
< strip_len
)
1954 die(_("malformed name field in the index, near path '%s'"),
1956 copy_len
= previous_len
- strip_len
;
1958 name
= (const char *)cp
;
1961 if (len
== CE_NAMEMASK
) {
1963 if (expand_name_field
)
1967 ce
= mem_pool__ce_alloc(ce_mem_pool
, len
);
1970 * NEEDSWORK: using 'offsetof()' is cumbersome and should be replaced
1971 * with something more akin to 'load_bitmap_entries_v1()'s use of
1972 * 'read_be16'/'read_be32'. For consistency with the corresponding
1973 * ondisk entry write function ('copy_cache_entry_to_ondisk()'), this
1974 * should be done at the same time as removing references to
1975 * 'ondisk_cache_entry' there.
1977 ce
->ce_stat_data
.sd_ctime
.sec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, ctime
)
1978 + offsetof(struct cache_time
, sec
));
1979 ce
->ce_stat_data
.sd_mtime
.sec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, mtime
)
1980 + offsetof(struct cache_time
, sec
));
1981 ce
->ce_stat_data
.sd_ctime
.nsec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, ctime
)
1982 + offsetof(struct cache_time
, nsec
));
1983 ce
->ce_stat_data
.sd_mtime
.nsec
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, mtime
)
1984 + offsetof(struct cache_time
, nsec
));
1985 ce
->ce_stat_data
.sd_dev
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, dev
));
1986 ce
->ce_stat_data
.sd_ino
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, ino
));
1987 ce
->ce_mode
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, mode
));
1988 ce
->ce_stat_data
.sd_uid
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, uid
));
1989 ce
->ce_stat_data
.sd_gid
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, gid
));
1990 ce
->ce_stat_data
.sd_size
= get_be32(ondisk
+ offsetof(struct ondisk_cache_entry
, size
));
1991 ce
->ce_flags
= flags
& ~CE_NAMEMASK
;
1992 ce
->ce_namelen
= len
;
1994 oidread(&ce
->oid
, (const unsigned char *)ondisk
+ offsetof(struct ondisk_cache_entry
, data
));
1996 if (expand_name_field
) {
1998 memcpy(ce
->name
, previous_ce
->name
, copy_len
);
1999 memcpy(ce
->name
+ copy_len
, name
, len
+ 1 - copy_len
);
2000 *ent_size
= (name
- ((char *)ondisk
)) + len
+ 1 - copy_len
;
2002 memcpy(ce
->name
, name
, len
+ 1);
2003 *ent_size
= ondisk_ce_size(ce
);
2008 static void check_ce_order(struct index_state
*istate
)
2012 if (!verify_ce_order
)
2015 for (i
= 1; i
< istate
->cache_nr
; i
++) {
2016 struct cache_entry
*ce
= istate
->cache
[i
- 1];
2017 struct cache_entry
*next_ce
= istate
->cache
[i
];
2018 int name_compare
= strcmp(ce
->name
, next_ce
->name
);
2020 if (0 < name_compare
)
2021 die(_("unordered stage entries in index"));
2022 if (!name_compare
) {
2024 die(_("multiple stage entries for merged file '%s'"),
2026 if (ce_stage(ce
) > ce_stage(next_ce
))
2027 die(_("unordered stage entries for '%s'"),
2033 static void tweak_untracked_cache(struct index_state
*istate
)
2035 struct repository
*r
= the_repository
;
2037 prepare_repo_settings(r
);
2039 switch (r
->settings
.core_untracked_cache
) {
2040 case UNTRACKED_CACHE_REMOVE
:
2041 remove_untracked_cache(istate
);
2043 case UNTRACKED_CACHE_WRITE
:
2044 add_untracked_cache(istate
);
2046 case UNTRACKED_CACHE_KEEP
:
2048 * Either an explicit "core.untrackedCache=keep", the
2049 * default if "core.untrackedCache" isn't configured,
2050 * or a fallback on an unknown "core.untrackedCache"
2057 static void tweak_split_index(struct index_state
*istate
)
2059 switch (git_config_get_split_index()) {
2060 case -1: /* unset: do nothing */
2063 remove_split_index(istate
);
2066 add_split_index(istate
);
2068 default: /* unknown value: do nothing */
2073 static void post_read_index_from(struct index_state
*istate
)
2075 check_ce_order(istate
);
2076 tweak_untracked_cache(istate
);
2077 tweak_split_index(istate
);
2078 tweak_fsmonitor(istate
);
2081 static size_t estimate_cache_size_from_compressed(unsigned int entries
)
2083 return entries
* (sizeof(struct cache_entry
) + CACHE_ENTRY_PATH_LENGTH
);
2086 static size_t estimate_cache_size(size_t ondisk_size
, unsigned int entries
)
2088 long per_entry
= sizeof(struct cache_entry
) - sizeof(struct ondisk_cache_entry
);
2091 * Account for potential alignment differences.
2093 per_entry
+= align_padding_size(per_entry
, 0);
2094 return ondisk_size
+ entries
* per_entry
;
2097 struct index_entry_offset
2099 /* starting byte offset into index file, count of index entries in this block */
2103 struct index_entry_offset_table
2106 struct index_entry_offset entries
[FLEX_ARRAY
];
2109 static struct index_entry_offset_table
*read_ieot_extension(const char *mmap
, size_t mmap_size
, size_t offset
);
2110 static void write_ieot_extension(struct strbuf
*sb
, struct index_entry_offset_table
*ieot
);
2112 static size_t read_eoie_extension(const char *mmap
, size_t mmap_size
);
2113 static void write_eoie_extension(struct strbuf
*sb
, git_hash_ctx
*eoie_context
, size_t offset
);
2115 struct load_index_extensions
2118 struct index_state
*istate
;
2121 unsigned long src_offset
;
2124 static void *load_index_extensions(void *_data
)
2126 struct load_index_extensions
*p
= _data
;
2127 unsigned long src_offset
= p
->src_offset
;
2129 while (src_offset
<= p
->mmap_size
- the_hash_algo
->rawsz
- 8) {
2130 /* After an array of active_nr index entries,
2131 * there can be arbitrary number of extended
2132 * sections, each of which is prefixed with
2133 * extension name (4-byte) and section length
2134 * in 4-byte network byte order.
2136 uint32_t extsize
= get_be32(p
->mmap
+ src_offset
+ 4);
2137 if (read_index_extension(p
->istate
,
2138 p
->mmap
+ src_offset
,
2139 p
->mmap
+ src_offset
+ 8,
2141 munmap((void *)p
->mmap
, p
->mmap_size
);
2142 die(_("index file corrupt"));
2145 src_offset
+= extsize
;
2152 * A helper function that will load the specified range of cache entries
2153 * from the memory mapped file and add them to the given index.
2155 static unsigned long load_cache_entry_block(struct index_state
*istate
,
2156 struct mem_pool
*ce_mem_pool
, int offset
, int nr
, const char *mmap
,
2157 unsigned long start_offset
, const struct cache_entry
*previous_ce
)
2160 unsigned long src_offset
= start_offset
;
2162 for (i
= offset
; i
< offset
+ nr
; i
++) {
2163 struct cache_entry
*ce
;
2164 unsigned long consumed
;
2166 ce
= create_from_disk(ce_mem_pool
, istate
->version
,
2168 &consumed
, previous_ce
);
2169 set_index_entry(istate
, i
, ce
);
2171 src_offset
+= consumed
;
2174 return src_offset
- start_offset
;
2177 static unsigned long load_all_cache_entries(struct index_state
*istate
,
2178 const char *mmap
, size_t mmap_size
, unsigned long src_offset
)
2180 unsigned long consumed
;
2182 istate
->ce_mem_pool
= xmalloc(sizeof(*istate
->ce_mem_pool
));
2183 if (istate
->version
== 4) {
2184 mem_pool_init(istate
->ce_mem_pool
,
2185 estimate_cache_size_from_compressed(istate
->cache_nr
));
2187 mem_pool_init(istate
->ce_mem_pool
,
2188 estimate_cache_size(mmap_size
, istate
->cache_nr
));
2191 consumed
= load_cache_entry_block(istate
, istate
->ce_mem_pool
,
2192 0, istate
->cache_nr
, mmap
, src_offset
, NULL
);
2197 * Mostly randomly chosen maximum thread counts: we
2198 * cap the parallelism to online_cpus() threads, and we want
2199 * to have at least 10000 cache entries per thread for it to
2200 * be worth starting a thread.
2203 #define THREAD_COST (10000)
2205 struct load_cache_entries_thread_data
2208 struct index_state
*istate
;
2209 struct mem_pool
*ce_mem_pool
;
2212 struct index_entry_offset_table
*ieot
;
2213 int ieot_start
; /* starting index into the ieot array */
2214 int ieot_blocks
; /* count of ieot entries to process */
2215 unsigned long consumed
; /* return # of bytes in index file processed */
2219 * A thread proc to run the load_cache_entries() computation
2220 * across multiple background threads.
2222 static void *load_cache_entries_thread(void *_data
)
2224 struct load_cache_entries_thread_data
*p
= _data
;
2227 /* iterate across all ieot blocks assigned to this thread */
2228 for (i
= p
->ieot_start
; i
< p
->ieot_start
+ p
->ieot_blocks
; i
++) {
2229 p
->consumed
+= load_cache_entry_block(p
->istate
, p
->ce_mem_pool
,
2230 p
->offset
, p
->ieot
->entries
[i
].nr
, p
->mmap
, p
->ieot
->entries
[i
].offset
, NULL
);
2231 p
->offset
+= p
->ieot
->entries
[i
].nr
;
2236 static unsigned long load_cache_entries_threaded(struct index_state
*istate
, const char *mmap
, size_t mmap_size
,
2237 int nr_threads
, struct index_entry_offset_table
*ieot
)
2239 int i
, offset
, ieot_blocks
, ieot_start
, err
;
2240 struct load_cache_entries_thread_data
*data
;
2241 unsigned long consumed
= 0;
2243 /* a little sanity checking */
2244 if (istate
->name_hash_initialized
)
2245 BUG("the name hash isn't thread safe");
2247 istate
->ce_mem_pool
= xmalloc(sizeof(*istate
->ce_mem_pool
));
2248 mem_pool_init(istate
->ce_mem_pool
, 0);
2250 /* ensure we have no more threads than we have blocks to process */
2251 if (nr_threads
> ieot
->nr
)
2252 nr_threads
= ieot
->nr
;
2253 CALLOC_ARRAY(data
, nr_threads
);
2255 offset
= ieot_start
= 0;
2256 ieot_blocks
= DIV_ROUND_UP(ieot
->nr
, nr_threads
);
2257 for (i
= 0; i
< nr_threads
; i
++) {
2258 struct load_cache_entries_thread_data
*p
= &data
[i
];
2261 if (ieot_start
+ ieot_blocks
> ieot
->nr
)
2262 ieot_blocks
= ieot
->nr
- ieot_start
;
2268 p
->ieot_start
= ieot_start
;
2269 p
->ieot_blocks
= ieot_blocks
;
2271 /* create a mem_pool for each thread */
2273 for (j
= p
->ieot_start
; j
< p
->ieot_start
+ p
->ieot_blocks
; j
++)
2274 nr
+= p
->ieot
->entries
[j
].nr
;
2275 p
->ce_mem_pool
= xmalloc(sizeof(*istate
->ce_mem_pool
));
2276 if (istate
->version
== 4) {
2277 mem_pool_init(p
->ce_mem_pool
,
2278 estimate_cache_size_from_compressed(nr
));
2280 mem_pool_init(p
->ce_mem_pool
,
2281 estimate_cache_size(mmap_size
, nr
));
2284 err
= pthread_create(&p
->pthread
, NULL
, load_cache_entries_thread
, p
);
2286 die(_("unable to create load_cache_entries thread: %s"), strerror(err
));
2288 /* increment by the number of cache entries in the ieot block being processed */
2289 for (j
= 0; j
< ieot_blocks
; j
++)
2290 offset
+= ieot
->entries
[ieot_start
+ j
].nr
;
2291 ieot_start
+= ieot_blocks
;
2294 for (i
= 0; i
< nr_threads
; i
++) {
2295 struct load_cache_entries_thread_data
*p
= &data
[i
];
2297 err
= pthread_join(p
->pthread
, NULL
);
2299 die(_("unable to join load_cache_entries thread: %s"), strerror(err
));
2300 mem_pool_combine(istate
->ce_mem_pool
, p
->ce_mem_pool
);
2301 consumed
+= p
->consumed
;
2309 static void set_new_index_sparsity(struct index_state
*istate
)
2312 * If the index's repo exists, mark it sparse according to
2315 prepare_repo_settings(istate
->repo
);
2316 if (!istate
->repo
->settings
.command_requires_full_index
&&
2317 is_sparse_index_allowed(istate
, 0))
2318 istate
->sparse_index
= 1;
2321 /* remember to discard_cache() before reading a different cache! */
2322 int do_read_index(struct index_state
*istate
, const char *path
, int must_exist
)
2326 unsigned long src_offset
;
2327 const struct cache_header
*hdr
;
2330 struct load_index_extensions p
;
2331 size_t extension_offset
= 0;
2332 int nr_threads
, cpus
;
2333 struct index_entry_offset_table
*ieot
= NULL
;
2335 if (istate
->initialized
)
2336 return istate
->cache_nr
;
2338 istate
->timestamp
.sec
= 0;
2339 istate
->timestamp
.nsec
= 0;
2340 fd
= open(path
, O_RDONLY
);
2342 if (!must_exist
&& errno
== ENOENT
) {
2343 set_new_index_sparsity(istate
);
2346 die_errno(_("%s: index file open failed"), path
);
2350 die_errno(_("%s: cannot stat the open index"), path
);
2352 mmap_size
= xsize_t(st
.st_size
);
2353 if (mmap_size
< sizeof(struct cache_header
) + the_hash_algo
->rawsz
)
2354 die(_("%s: index file smaller than expected"), path
);
2356 mmap
= xmmap_gently(NULL
, mmap_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2357 if (mmap
== MAP_FAILED
)
2358 die_errno(_("%s: unable to map index file%s"), path
,
2362 hdr
= (const struct cache_header
*)mmap
;
2363 if (verify_hdr(hdr
, mmap_size
) < 0)
2366 oidread(&istate
->oid
, (const unsigned char *)hdr
+ mmap_size
- the_hash_algo
->rawsz
);
2367 istate
->version
= ntohl(hdr
->hdr_version
);
2368 istate
->cache_nr
= ntohl(hdr
->hdr_entries
);
2369 istate
->cache_alloc
= alloc_nr(istate
->cache_nr
);
2370 CALLOC_ARRAY(istate
->cache
, istate
->cache_alloc
);
2371 istate
->initialized
= 1;
2375 p
.mmap_size
= mmap_size
;
2377 src_offset
= sizeof(*hdr
);
2379 if (git_config_get_index_threads(&nr_threads
))
2382 /* TODO: does creating more threads than cores help? */
2384 nr_threads
= istate
->cache_nr
/ THREAD_COST
;
2385 cpus
= online_cpus();
2386 if (nr_threads
> cpus
)
2393 if (nr_threads
> 1) {
2394 extension_offset
= read_eoie_extension(mmap
, mmap_size
);
2395 if (extension_offset
) {
2398 p
.src_offset
= extension_offset
;
2399 err
= pthread_create(&p
.pthread
, NULL
, load_index_extensions
, &p
);
2401 die(_("unable to create load_index_extensions thread: %s"), strerror(err
));
2408 * Locate and read the index entry offset table so that we can use it
2409 * to multi-thread the reading of the cache entries.
2411 if (extension_offset
&& nr_threads
> 1)
2412 ieot
= read_ieot_extension(mmap
, mmap_size
, extension_offset
);
2415 src_offset
+= load_cache_entries_threaded(istate
, mmap
, mmap_size
, nr_threads
, ieot
);
2418 src_offset
+= load_all_cache_entries(istate
, mmap
, mmap_size
, src_offset
);
2421 istate
->timestamp
.sec
= st
.st_mtime
;
2422 istate
->timestamp
.nsec
= ST_MTIME_NSEC(st
);
2424 /* if we created a thread, join it otherwise load the extensions on the primary thread */
2425 if (extension_offset
) {
2426 int ret
= pthread_join(p
.pthread
, NULL
);
2428 die(_("unable to join load_index_extensions thread: %s"), strerror(ret
));
2430 p
.src_offset
= src_offset
;
2431 load_index_extensions(&p
);
2433 munmap((void *)mmap
, mmap_size
);
2436 * TODO trace2: replace "the_repository" with the actual repo instance
2437 * that is associated with the given "istate".
2439 trace2_data_intmax("index", the_repository
, "read/version",
2441 trace2_data_intmax("index", the_repository
, "read/cache_nr",
2445 * If the command explicitly requires a full index, force it
2446 * to be full. Otherwise, correct the sparsity based on repository
2447 * settings and other properties of the index (if necessary).
2449 prepare_repo_settings(istate
->repo
);
2450 if (istate
->repo
->settings
.command_requires_full_index
)
2451 ensure_full_index(istate
);
2453 ensure_correct_sparsity(istate
);
2455 return istate
->cache_nr
;
2458 munmap((void *)mmap
, mmap_size
);
2459 die(_("index file corrupt"));
2463 * Signal that the shared index is used by updating its mtime.
2465 * This way, shared index can be removed if they have not been used
2468 static void freshen_shared_index(const char *shared_index
, int warn
)
2470 if (!check_and_freshen_file(shared_index
, 1) && warn
)
2471 warning(_("could not freshen shared index '%s'"), shared_index
);
2474 int read_index_from(struct index_state
*istate
, const char *path
,
2477 struct split_index
*split_index
;
2482 /* istate->initialized covers both .git/index and .git/sharedindex.xxx */
2483 if (istate
->initialized
)
2484 return istate
->cache_nr
;
2487 * TODO trace2: replace "the_repository" with the actual repo instance
2488 * that is associated with the given "istate".
2490 trace2_region_enter_printf("index", "do_read_index", the_repository
,
2492 trace_performance_enter();
2493 ret
= do_read_index(istate
, path
, 0);
2494 trace_performance_leave("read cache %s", path
);
2495 trace2_region_leave_printf("index", "do_read_index", the_repository
,
2498 split_index
= istate
->split_index
;
2499 if (!split_index
|| is_null_oid(&split_index
->base_oid
)) {
2500 post_read_index_from(istate
);
2504 trace_performance_enter();
2505 if (split_index
->base
)
2506 release_index(split_index
->base
);
2508 ALLOC_ARRAY(split_index
->base
, 1);
2509 index_state_init(split_index
->base
, istate
->repo
);
2511 base_oid_hex
= oid_to_hex(&split_index
->base_oid
);
2512 base_path
= xstrfmt("%s/sharedindex.%s", gitdir
, base_oid_hex
);
2513 trace2_region_enter_printf("index", "shared/do_read_index",
2514 the_repository
, "%s", base_path
);
2515 ret
= do_read_index(split_index
->base
, base_path
, 0);
2516 trace2_region_leave_printf("index", "shared/do_read_index",
2517 the_repository
, "%s", base_path
);
2519 char *path_copy
= xstrdup(path
);
2520 char *base_path2
= xstrfmt("%s/sharedindex.%s",
2521 dirname(path_copy
), base_oid_hex
);
2523 trace2_region_enter_printf("index", "shared/do_read_index",
2524 the_repository
, "%s", base_path2
);
2525 ret
= do_read_index(split_index
->base
, base_path2
, 1);
2526 trace2_region_leave_printf("index", "shared/do_read_index",
2527 the_repository
, "%s", base_path2
);
2530 if (!oideq(&split_index
->base_oid
, &split_index
->base
->oid
))
2531 die(_("broken index, expect %s in %s, got %s"),
2532 base_oid_hex
, base_path
,
2533 oid_to_hex(&split_index
->base
->oid
));
2535 freshen_shared_index(base_path
, 0);
2536 merge_base_index(istate
);
2537 post_read_index_from(istate
);
2538 trace_performance_leave("read cache %s", base_path
);
2543 int is_index_unborn(struct index_state
*istate
)
2545 return (!istate
->cache_nr
&& !istate
->timestamp
.sec
);
2548 void index_state_init(struct index_state
*istate
, struct repository
*r
)
2550 struct index_state blank
= INDEX_STATE_INIT(r
);
2551 memcpy(istate
, &blank
, sizeof(*istate
));
2554 void release_index(struct index_state
*istate
)
2557 * Cache entries in istate->cache[] should have been allocated
2558 * from the memory pool associated with this index, or from an
2559 * associated split_index. There is no need to free individual
2560 * cache entries. validate_cache_entries can detect when this
2561 * assertion does not hold.
2563 validate_cache_entries(istate
);
2565 resolve_undo_clear_index(istate
);
2566 free_name_hash(istate
);
2567 cache_tree_free(&(istate
->cache_tree
));
2568 free(istate
->fsmonitor_last_update
);
2569 free(istate
->cache
);
2570 discard_split_index(istate
);
2571 free_untracked_cache(istate
->untracked
);
2573 if (istate
->sparse_checkout_patterns
) {
2574 clear_pattern_list(istate
->sparse_checkout_patterns
);
2575 FREE_AND_NULL(istate
->sparse_checkout_patterns
);
2578 if (istate
->ce_mem_pool
) {
2579 mem_pool_discard(istate
->ce_mem_pool
, should_validate_cache_entries());
2580 FREE_AND_NULL(istate
->ce_mem_pool
);
2584 void discard_index(struct index_state
*istate
)
2586 release_index(istate
);
2587 index_state_init(istate
, istate
->repo
);
2591 * Validate the cache entries of this index.
2592 * All cache entries associated with this index
2593 * should have been allocated by the memory pool
2594 * associated with this index, or by a referenced
2597 void validate_cache_entries(const struct index_state
*istate
)
2601 if (!should_validate_cache_entries() ||!istate
|| !istate
->initialized
)
2604 for (i
= 0; i
< istate
->cache_nr
; i
++) {
2606 BUG("cache entry is not allocated from expected memory pool");
2607 } else if (!istate
->ce_mem_pool
||
2608 !mem_pool_contains(istate
->ce_mem_pool
, istate
->cache
[i
])) {
2609 if (!istate
->split_index
||
2610 !istate
->split_index
->base
||
2611 !istate
->split_index
->base
->ce_mem_pool
||
2612 !mem_pool_contains(istate
->split_index
->base
->ce_mem_pool
, istate
->cache
[i
])) {
2613 BUG("cache entry is not allocated from expected memory pool");
2618 if (istate
->split_index
)
2619 validate_cache_entries(istate
->split_index
->base
);
2622 int unmerged_index(const struct index_state
*istate
)
2625 for (i
= 0; i
< istate
->cache_nr
; i
++) {
2626 if (ce_stage(istate
->cache
[i
]))
2632 int repo_index_has_changes(struct repository
*repo
,
2636 struct index_state
*istate
= repo
->index
;
2637 struct object_id cmp
;
2641 cmp
= tree
->object
.oid
;
2642 if (tree
|| !repo_get_oid_tree(repo
, "HEAD", &cmp
)) {
2643 struct diff_options opt
;
2645 repo_diff_setup(repo
, &opt
);
2646 opt
.flags
.exit_with_status
= 1;
2648 opt
.flags
.quick
= 1;
2649 diff_setup_done(&opt
);
2650 do_diff_cache(&cmp
, &opt
);
2652 for (i
= 0; sb
&& i
< diff_queued_diff
.nr
; i
++) {
2654 strbuf_addch(sb
, ' ');
2655 strbuf_addstr(sb
, diff_queued_diff
.queue
[i
]->two
->path
);
2658 return opt
.flags
.has_changes
!= 0;
2660 /* TODO: audit for interaction with sparse-index. */
2661 ensure_full_index(istate
);
2662 for (i
= 0; sb
&& i
< istate
->cache_nr
; i
++) {
2664 strbuf_addch(sb
, ' ');
2665 strbuf_addstr(sb
, istate
->cache
[i
]->name
);
2667 return !!istate
->cache_nr
;
2671 static int write_index_ext_header(struct hashfile
*f
,
2672 git_hash_ctx
*eoie_f
,
2676 hashwrite_be32(f
, ext
);
2677 hashwrite_be32(f
, sz
);
2682 the_hash_algo
->update_fn(eoie_f
, &ext
, sizeof(ext
));
2683 the_hash_algo
->update_fn(eoie_f
, &sz
, sizeof(sz
));
2688 static void ce_smudge_racily_clean_entry(struct index_state
*istate
,
2689 struct cache_entry
*ce
)
2692 * The only thing we care about in this function is to smudge the
2693 * falsely clean entry due to touch-update-touch race, so we leave
2694 * everything else as they are. We are called for entries whose
2695 * ce_stat_data.sd_mtime match the index file mtime.
2697 * Note that this actually does not do much for gitlinks, for
2698 * which ce_match_stat_basic() always goes to the actual
2699 * contents. The caller checks with is_racy_timestamp() which
2700 * always says "no" for gitlinks, so we are not called for them ;-)
2704 if (lstat(ce
->name
, &st
) < 0)
2706 if (ce_match_stat_basic(ce
, &st
))
2708 if (ce_modified_check_fs(istate
, ce
, &st
)) {
2709 /* This is "racily clean"; smudge it. Note that this
2710 * is a tricky code. At first glance, it may appear
2711 * that it can break with this sequence:
2713 * $ echo xyzzy >frotz
2714 * $ git-update-index --add frotz
2717 * $ echo filfre >nitfol
2718 * $ git-update-index --add nitfol
2720 * but it does not. When the second update-index runs,
2721 * it notices that the entry "frotz" has the same timestamp
2722 * as index, and if we were to smudge it by resetting its
2723 * size to zero here, then the object name recorded
2724 * in index is the 6-byte file but the cached stat information
2725 * becomes zero --- which would then match what we would
2726 * obtain from the filesystem next time we stat("frotz").
2728 * However, the second update-index, before calling
2729 * this function, notices that the cached size is 6
2730 * bytes and what is on the filesystem is an empty
2731 * file, and never calls us, so the cached size information
2732 * for "frotz" stays 6 which does not match the filesystem.
2734 ce
->ce_stat_data
.sd_size
= 0;
2738 /* Copy miscellaneous fields but not the name */
2739 static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry
*ondisk
,
2740 struct cache_entry
*ce
)
2743 const unsigned hashsz
= the_hash_algo
->rawsz
;
2744 uint16_t *flagsp
= (uint16_t *)(ondisk
->data
+ hashsz
);
2746 ondisk
->ctime
.sec
= htonl(ce
->ce_stat_data
.sd_ctime
.sec
);
2747 ondisk
->mtime
.sec
= htonl(ce
->ce_stat_data
.sd_mtime
.sec
);
2748 ondisk
->ctime
.nsec
= htonl(ce
->ce_stat_data
.sd_ctime
.nsec
);
2749 ondisk
->mtime
.nsec
= htonl(ce
->ce_stat_data
.sd_mtime
.nsec
);
2750 ondisk
->dev
= htonl(ce
->ce_stat_data
.sd_dev
);
2751 ondisk
->ino
= htonl(ce
->ce_stat_data
.sd_ino
);
2752 ondisk
->mode
= htonl(ce
->ce_mode
);
2753 ondisk
->uid
= htonl(ce
->ce_stat_data
.sd_uid
);
2754 ondisk
->gid
= htonl(ce
->ce_stat_data
.sd_gid
);
2755 ondisk
->size
= htonl(ce
->ce_stat_data
.sd_size
);
2756 hashcpy(ondisk
->data
, ce
->oid
.hash
);
2758 flags
= ce
->ce_flags
& ~CE_NAMEMASK
;
2759 flags
|= (ce_namelen(ce
) >= CE_NAMEMASK
? CE_NAMEMASK
: ce_namelen(ce
));
2760 flagsp
[0] = htons(flags
);
2761 if (ce
->ce_flags
& CE_EXTENDED
) {
2762 flagsp
[1] = htons((ce
->ce_flags
& CE_EXTENDED_FLAGS
) >> 16);
2766 static int ce_write_entry(struct hashfile
*f
, struct cache_entry
*ce
,
2767 struct strbuf
*previous_name
, struct ondisk_cache_entry
*ondisk
)
2770 unsigned int saved_namelen
;
2771 int stripped_name
= 0;
2772 static unsigned char padding
[8] = { 0x00 };
2774 if (ce
->ce_flags
& CE_STRIP_NAME
) {
2775 saved_namelen
= ce_namelen(ce
);
2780 size
= offsetof(struct ondisk_cache_entry
,data
) + ondisk_data_size(ce
->ce_flags
, 0);
2782 if (!previous_name
) {
2783 int len
= ce_namelen(ce
);
2784 copy_cache_entry_to_ondisk(ondisk
, ce
);
2785 hashwrite(f
, ondisk
, size
);
2786 hashwrite(f
, ce
->name
, len
);
2787 hashwrite(f
, padding
, align_padding_size(size
, len
));
2789 int common
, to_remove
, prefix_size
;
2790 unsigned char to_remove_vi
[16];
2792 (ce
->name
[common
] &&
2793 common
< previous_name
->len
&&
2794 ce
->name
[common
] == previous_name
->buf
[common
]);
2796 ; /* still matching */
2797 to_remove
= previous_name
->len
- common
;
2798 prefix_size
= encode_varint(to_remove
, to_remove_vi
);
2800 copy_cache_entry_to_ondisk(ondisk
, ce
);
2801 hashwrite(f
, ondisk
, size
);
2802 hashwrite(f
, to_remove_vi
, prefix_size
);
2803 hashwrite(f
, ce
->name
+ common
, ce_namelen(ce
) - common
);
2804 hashwrite(f
, padding
, 1);
2806 strbuf_splice(previous_name
, common
, to_remove
,
2807 ce
->name
+ common
, ce_namelen(ce
) - common
);
2809 if (stripped_name
) {
2810 ce
->ce_namelen
= saved_namelen
;
2811 ce
->ce_flags
&= ~CE_STRIP_NAME
;
2818 * This function verifies if index_state has the correct sha1 of the
2819 * index file. Don't die if we have any other failure, just return 0.
2821 static int verify_index_from(const struct index_state
*istate
, const char *path
)
2826 unsigned char hash
[GIT_MAX_RAWSZ
];
2828 if (!istate
->initialized
)
2831 fd
= open(path
, O_RDONLY
);
2838 if (st
.st_size
< sizeof(struct cache_header
) + the_hash_algo
->rawsz
)
2841 n
= pread_in_full(fd
, hash
, the_hash_algo
->rawsz
, st
.st_size
- the_hash_algo
->rawsz
);
2842 if (n
!= the_hash_algo
->rawsz
)
2845 if (!hasheq(istate
->oid
.hash
, hash
))
2856 static int repo_verify_index(struct repository
*repo
)
2858 return verify_index_from(repo
->index
, repo
->index_file
);
2861 int has_racy_timestamp(struct index_state
*istate
)
2863 int entries
= istate
->cache_nr
;
2866 for (i
= 0; i
< entries
; i
++) {
2867 struct cache_entry
*ce
= istate
->cache
[i
];
2868 if (is_racy_timestamp(istate
, ce
))
2874 void repo_update_index_if_able(struct repository
*repo
,
2875 struct lock_file
*lockfile
)
2877 if ((repo
->index
->cache_changed
||
2878 has_racy_timestamp(repo
->index
)) &&
2879 repo_verify_index(repo
))
2880 write_locked_index(repo
->index
, lockfile
, COMMIT_LOCK
);
2882 rollback_lock_file(lockfile
);
2885 static int record_eoie(void)
2889 if (!git_config_get_bool("index.recordendofindexentries", &val
))
2893 * As a convenience, the end of index entries extension
2894 * used for threading is written by default if the user
2895 * explicitly requested threaded index reads.
2897 return !git_config_get_index_threads(&val
) && val
!= 1;
2900 static int record_ieot(void)
2904 if (!git_config_get_bool("index.recordoffsettable", &val
))
2908 * As a convenience, the offset table used for threading is
2909 * written by default if the user explicitly requested
2910 * threaded index reads.
2912 return !git_config_get_index_threads(&val
) && val
!= 1;
2916 * On success, `tempfile` is closed. If it is the temporary file
2917 * of a `struct lock_file`, we will therefore effectively perform
2918 * a 'close_lock_file_gently()`. Since that is an implementation
2919 * detail of lockfiles, callers of `do_write_index()` should not
2922 static int do_write_index(struct index_state
*istate
, struct tempfile
*tempfile
,
2923 int strip_extensions
, unsigned flags
)
2925 uint64_t start
= getnanotime();
2927 git_hash_ctx
*eoie_c
= NULL
;
2928 struct cache_header hdr
;
2929 int i
, err
= 0, removed
, extended
, hdr_version
;
2930 struct cache_entry
**cache
= istate
->cache
;
2931 int entries
= istate
->cache_nr
;
2933 struct ondisk_cache_entry ondisk
;
2934 struct strbuf previous_name_buf
= STRBUF_INIT
, *previous_name
;
2935 int drop_cache_tree
= istate
->drop_cache_tree
;
2937 int csum_fsync_flag
;
2938 int ieot_entries
= 1;
2939 struct index_entry_offset_table
*ieot
= NULL
;
2941 struct repository
*r
= istate
->repo
;
2943 f
= hashfd(tempfile
->fd
, tempfile
->filename
.buf
);
2945 prepare_repo_settings(r
);
2946 f
->skip_hash
= r
->settings
.index_skip_hash
;
2948 for (i
= removed
= extended
= 0; i
< entries
; i
++) {
2949 if (cache
[i
]->ce_flags
& CE_REMOVE
)
2952 /* reduce extended entries if possible */
2953 cache
[i
]->ce_flags
&= ~CE_EXTENDED
;
2954 if (cache
[i
]->ce_flags
& CE_EXTENDED_FLAGS
) {
2956 cache
[i
]->ce_flags
|= CE_EXTENDED
;
2960 if (!istate
->version
)
2961 istate
->version
= get_index_format_default(r
);
2963 /* demote version 3 to version 2 when the latter suffices */
2964 if (istate
->version
== 3 || istate
->version
== 2)
2965 istate
->version
= extended
? 3 : 2;
2967 hdr_version
= istate
->version
;
2969 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
2970 hdr
.hdr_version
= htonl(hdr_version
);
2971 hdr
.hdr_entries
= htonl(entries
- removed
);
2973 hashwrite(f
, &hdr
, sizeof(hdr
));
2975 if (!HAVE_THREADS
|| git_config_get_index_threads(&nr_threads
))
2978 if (nr_threads
!= 1 && record_ieot()) {
2979 int ieot_blocks
, cpus
;
2982 * ensure default number of ieot blocks maps evenly to the
2983 * default number of threads that will process them leaving
2984 * room for the thread to load the index extensions.
2987 ieot_blocks
= istate
->cache_nr
/ THREAD_COST
;
2988 cpus
= online_cpus();
2989 if (ieot_blocks
> cpus
- 1)
2990 ieot_blocks
= cpus
- 1;
2992 ieot_blocks
= nr_threads
;
2993 if (ieot_blocks
> istate
->cache_nr
)
2994 ieot_blocks
= istate
->cache_nr
;
2998 * no reason to write out the IEOT extension if we don't
2999 * have enough blocks to utilize multi-threading
3001 if (ieot_blocks
> 1) {
3002 ieot
= xcalloc(1, sizeof(struct index_entry_offset_table
)
3003 + (ieot_blocks
* sizeof(struct index_entry_offset
)));
3004 ieot_entries
= DIV_ROUND_UP(entries
, ieot_blocks
);
3008 offset
= hashfile_total(f
);
3011 previous_name
= (hdr_version
== 4) ? &previous_name_buf
: NULL
;
3013 for (i
= 0; i
< entries
; i
++) {
3014 struct cache_entry
*ce
= cache
[i
];
3015 if (ce
->ce_flags
& CE_REMOVE
)
3017 if (!ce_uptodate(ce
) && is_racy_timestamp(istate
, ce
))
3018 ce_smudge_racily_clean_entry(istate
, ce
);
3019 if (is_null_oid(&ce
->oid
)) {
3020 static const char msg
[] = "cache entry has null sha1: %s";
3021 static int allow
= -1;
3024 allow
= git_env_bool("GIT_ALLOW_NULL_SHA1", 0);
3026 warning(msg
, ce
->name
);
3028 err
= error(msg
, ce
->name
);
3030 drop_cache_tree
= 1;
3032 if (ieot
&& i
&& (i
% ieot_entries
== 0)) {
3033 ieot
->entries
[ieot
->nr
].nr
= nr
;
3034 ieot
->entries
[ieot
->nr
].offset
= offset
;
3037 * If we have a V4 index, set the first byte to an invalid
3038 * character to ensure there is nothing common with the previous
3042 previous_name
->buf
[0] = 0;
3045 offset
= hashfile_total(f
);
3047 if (ce_write_entry(f
, ce
, previous_name
, (struct ondisk_cache_entry
*)&ondisk
) < 0)
3055 ieot
->entries
[ieot
->nr
].nr
= nr
;
3056 ieot
->entries
[ieot
->nr
].offset
= offset
;
3059 strbuf_release(&previous_name_buf
);
3066 offset
= hashfile_total(f
);
3069 * The extension headers must be hashed on their own for the
3070 * EOIE extension. Create a hashfile here to compute that hash.
3072 if (offset
&& record_eoie()) {
3073 CALLOC_ARRAY(eoie_c
, 1);
3074 the_hash_algo
->init_fn(eoie_c
);
3078 * Lets write out CACHE_EXT_INDEXENTRYOFFSETTABLE first so that we
3079 * can minimize the number of extensions we have to scan through to
3080 * find it during load. Write it out regardless of the
3081 * strip_extensions parameter as we need it when loading the shared
3085 struct strbuf sb
= STRBUF_INIT
;
3087 write_ieot_extension(&sb
, ieot
);
3088 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_INDEXENTRYOFFSETTABLE
, sb
.len
) < 0;
3089 hashwrite(f
, sb
.buf
, sb
.len
);
3090 strbuf_release(&sb
);
3096 if (!strip_extensions
&& istate
->split_index
&&
3097 !is_null_oid(&istate
->split_index
->base_oid
)) {
3098 struct strbuf sb
= STRBUF_INIT
;
3100 if (istate
->sparse_index
)
3101 die(_("cannot write split index for a sparse index"));
3103 err
= write_link_extension(&sb
, istate
) < 0 ||
3104 write_index_ext_header(f
, eoie_c
, CACHE_EXT_LINK
,
3106 hashwrite(f
, sb
.buf
, sb
.len
);
3107 strbuf_release(&sb
);
3111 if (!strip_extensions
&& !drop_cache_tree
&& istate
->cache_tree
) {
3112 struct strbuf sb
= STRBUF_INIT
;
3114 cache_tree_write(&sb
, istate
->cache_tree
);
3115 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_TREE
, sb
.len
) < 0;
3116 hashwrite(f
, sb
.buf
, sb
.len
);
3117 strbuf_release(&sb
);
3121 if (!strip_extensions
&& istate
->resolve_undo
) {
3122 struct strbuf sb
= STRBUF_INIT
;
3124 resolve_undo_write(&sb
, istate
->resolve_undo
);
3125 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_RESOLVE_UNDO
,
3127 hashwrite(f
, sb
.buf
, sb
.len
);
3128 strbuf_release(&sb
);
3132 if (!strip_extensions
&& istate
->untracked
) {
3133 struct strbuf sb
= STRBUF_INIT
;
3135 write_untracked_extension(&sb
, istate
->untracked
);
3136 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_UNTRACKED
,
3138 hashwrite(f
, sb
.buf
, sb
.len
);
3139 strbuf_release(&sb
);
3143 if (!strip_extensions
&& istate
->fsmonitor_last_update
) {
3144 struct strbuf sb
= STRBUF_INIT
;
3146 write_fsmonitor_extension(&sb
, istate
);
3147 err
= write_index_ext_header(f
, eoie_c
, CACHE_EXT_FSMONITOR
, sb
.len
) < 0;
3148 hashwrite(f
, sb
.buf
, sb
.len
);
3149 strbuf_release(&sb
);
3153 if (istate
->sparse_index
) {
3154 if (write_index_ext_header(f
, eoie_c
, CACHE_EXT_SPARSE_DIRECTORIES
, 0) < 0)
3159 * CACHE_EXT_ENDOFINDEXENTRIES must be written as the last entry before the SHA1
3160 * so that it can be found and processed before all the index entries are
3161 * read. Write it out regardless of the strip_extensions parameter as we need it
3162 * when loading the shared index.
3165 struct strbuf sb
= STRBUF_INIT
;
3167 write_eoie_extension(&sb
, eoie_c
, offset
);
3168 err
= write_index_ext_header(f
, NULL
, CACHE_EXT_ENDOFINDEXENTRIES
, sb
.len
) < 0;
3169 hashwrite(f
, sb
.buf
, sb
.len
);
3170 strbuf_release(&sb
);
3175 csum_fsync_flag
= 0;
3176 if (!alternate_index_output
&& (flags
& COMMIT_LOCK
))
3177 csum_fsync_flag
= CSUM_FSYNC
;
3179 finalize_hashfile(f
, istate
->oid
.hash
, FSYNC_COMPONENT_INDEX
,
3180 CSUM_HASH_IN_STREAM
| csum_fsync_flag
);
3182 if (close_tempfile_gently(tempfile
)) {
3183 error(_("could not close '%s'"), get_tempfile_path(tempfile
));
3186 if (stat(get_tempfile_path(tempfile
), &st
))
3188 istate
->timestamp
.sec
= (unsigned int)st
.st_mtime
;
3189 istate
->timestamp
.nsec
= ST_MTIME_NSEC(st
);
3190 trace_performance_since(start
, "write index, changed mask = %x", istate
->cache_changed
);
3193 * TODO trace2: replace "the_repository" with the actual repo instance
3194 * that is associated with the given "istate".
3196 trace2_data_intmax("index", the_repository
, "write/version",
3198 trace2_data_intmax("index", the_repository
, "write/cache_nr",
3204 void set_alternate_index_output(const char *name
)
3206 alternate_index_output
= name
;
3209 static int commit_locked_index(struct lock_file
*lk
)
3211 if (alternate_index_output
)
3212 return commit_lock_file_to(lk
, alternate_index_output
);
3214 return commit_lock_file(lk
);
3217 static int do_write_locked_index(struct index_state
*istate
, struct lock_file
*lock
,
3221 int was_full
= istate
->sparse_index
== INDEX_EXPANDED
;
3223 ret
= convert_to_sparse(istate
, 0);
3226 warning(_("failed to convert to a sparse-index"));
3231 * TODO trace2: replace "the_repository" with the actual repo instance
3232 * that is associated with the given "istate".
3234 trace2_region_enter_printf("index", "do_write_index", the_repository
,
3235 "%s", get_lock_file_path(lock
));
3236 ret
= do_write_index(istate
, lock
->tempfile
, 0, flags
);
3237 trace2_region_leave_printf("index", "do_write_index", the_repository
,
3238 "%s", get_lock_file_path(lock
));
3241 ensure_full_index(istate
);
3245 if (flags
& COMMIT_LOCK
)
3246 ret
= commit_locked_index(lock
);
3248 ret
= close_lock_file_gently(lock
);
3250 run_hooks_l("post-index-change",
3251 istate
->updated_workdir
? "1" : "0",
3252 istate
->updated_skipworktree
? "1" : "0", NULL
);
3253 istate
->updated_workdir
= 0;
3254 istate
->updated_skipworktree
= 0;
3259 static int write_split_index(struct index_state
*istate
,
3260 struct lock_file
*lock
,
3264 prepare_to_write_split_index(istate
);
3265 ret
= do_write_locked_index(istate
, lock
, flags
);
3266 finish_writing_split_index(istate
);
3270 static const char *shared_index_expire
= "2.weeks.ago";
3272 static unsigned long get_shared_index_expire_date(void)
3274 static unsigned long shared_index_expire_date
;
3275 static int shared_index_expire_date_prepared
;
3277 if (!shared_index_expire_date_prepared
) {
3278 git_config_get_expiry("splitindex.sharedindexexpire",
3279 &shared_index_expire
);
3280 shared_index_expire_date
= approxidate(shared_index_expire
);
3281 shared_index_expire_date_prepared
= 1;
3284 return shared_index_expire_date
;
3287 static int should_delete_shared_index(const char *shared_index_path
)
3290 unsigned long expiration
;
3292 /* Check timestamp */
3293 expiration
= get_shared_index_expire_date();
3296 if (stat(shared_index_path
, &st
))
3297 return error_errno(_("could not stat '%s'"), shared_index_path
);
3298 if (st
.st_mtime
> expiration
)
3304 static int clean_shared_index_files(const char *current_hex
)
3307 DIR *dir
= opendir(get_git_dir());
3310 return error_errno(_("unable to open git dir: %s"), get_git_dir());
3312 while ((de
= readdir(dir
)) != NULL
) {
3313 const char *sha1_hex
;
3314 const char *shared_index_path
;
3315 if (!skip_prefix(de
->d_name
, "sharedindex.", &sha1_hex
))
3317 if (!strcmp(sha1_hex
, current_hex
))
3319 shared_index_path
= git_path("%s", de
->d_name
);
3320 if (should_delete_shared_index(shared_index_path
) > 0 &&
3321 unlink(shared_index_path
))
3322 warning_errno(_("unable to unlink: %s"), shared_index_path
);
3329 static int write_shared_index(struct index_state
*istate
,
3330 struct tempfile
**temp
, unsigned flags
)
3332 struct split_index
*si
= istate
->split_index
;
3333 int ret
, was_full
= !istate
->sparse_index
;
3335 move_cache_to_base_index(istate
);
3336 convert_to_sparse(istate
, 0);
3338 trace2_region_enter_printf("index", "shared/do_write_index",
3339 the_repository
, "%s", get_tempfile_path(*temp
));
3340 ret
= do_write_index(si
->base
, *temp
, 1, flags
);
3341 trace2_region_leave_printf("index", "shared/do_write_index",
3342 the_repository
, "%s", get_tempfile_path(*temp
));
3345 ensure_full_index(istate
);
3349 ret
= adjust_shared_perm(get_tempfile_path(*temp
));
3351 error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp
));
3354 ret
= rename_tempfile(temp
,
3355 git_path("sharedindex.%s", oid_to_hex(&si
->base
->oid
)));
3357 oidcpy(&si
->base_oid
, &si
->base
->oid
);
3358 clean_shared_index_files(oid_to_hex(&si
->base
->oid
));
3364 static const int default_max_percent_split_change
= 20;
3366 static int too_many_not_shared_entries(struct index_state
*istate
)
3368 int i
, not_shared
= 0;
3369 int max_split
= git_config_get_max_percent_split_change();
3371 switch (max_split
) {
3373 /* not or badly configured: use the default value */
3374 max_split
= default_max_percent_split_change
;
3377 return 1; /* 0% means always write a new shared index */
3379 return 0; /* 100% means never write a new shared index */
3381 break; /* just use the configured value */
3384 /* Count not shared entries */
3385 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3386 struct cache_entry
*ce
= istate
->cache
[i
];
3391 return (int64_t)istate
->cache_nr
* max_split
< (int64_t)not_shared
* 100;
3394 int write_locked_index(struct index_state
*istate
, struct lock_file
*lock
,
3397 int new_shared_index
, ret
, test_split_index_env
;
3398 struct split_index
*si
= istate
->split_index
;
3400 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
3401 cache_tree_verify(the_repository
, istate
);
3403 if ((flags
& SKIP_IF_UNCHANGED
) && !istate
->cache_changed
) {
3404 if (flags
& COMMIT_LOCK
)
3405 rollback_lock_file(lock
);
3409 if (istate
->fsmonitor_last_update
)
3410 fill_fsmonitor_bitmap(istate
);
3412 test_split_index_env
= git_env_bool("GIT_TEST_SPLIT_INDEX", 0);
3414 if ((!si
&& !test_split_index_env
) ||
3415 alternate_index_output
||
3416 (istate
->cache_changed
& ~EXTMASK
)) {
3418 oidclr(&si
->base_oid
);
3419 ret
= do_write_locked_index(istate
, lock
, flags
);
3423 if (test_split_index_env
) {
3425 si
= init_split_index(istate
);
3426 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
3428 int v
= si
->base_oid
.hash
[0];
3430 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
3433 if (too_many_not_shared_entries(istate
))
3434 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
3436 new_shared_index
= istate
->cache_changed
& SPLIT_INDEX_ORDERED
;
3438 if (new_shared_index
) {
3439 struct tempfile
*temp
;
3442 /* Same initial permissions as the main .git/index file */
3443 temp
= mks_tempfile_sm(git_path("sharedindex_XXXXXX"), 0, 0666);
3445 oidclr(&si
->base_oid
);
3446 ret
= do_write_locked_index(istate
, lock
, flags
);
3449 ret
= write_shared_index(istate
, &temp
, flags
);
3451 saved_errno
= errno
;
3452 if (is_tempfile_active(temp
))
3453 delete_tempfile(&temp
);
3454 errno
= saved_errno
;
3460 ret
= write_split_index(istate
, lock
, flags
);
3462 /* Freshen the shared index only if the split-index was written */
3463 if (!ret
&& !new_shared_index
&& !is_null_oid(&si
->base_oid
)) {
3464 const char *shared_index
= git_path("sharedindex.%s",
3465 oid_to_hex(&si
->base_oid
));
3466 freshen_shared_index(shared_index
, 1);
3470 if (flags
& COMMIT_LOCK
)
3471 rollback_lock_file(lock
);
3476 * Read the index file that is potentially unmerged into given
3477 * index_state, dropping any unmerged entries to stage #0 (potentially
3478 * resulting in a path appearing as both a file and a directory in the
3479 * index; the caller is responsible to clear out the extra entries
3480 * before writing the index to a tree). Returns true if the index is
3481 * unmerged. Callers who want to refuse to work from an unmerged
3482 * state can call this and check its return value, instead of calling
3485 int repo_read_index_unmerged(struct repository
*repo
)
3487 struct index_state
*istate
;
3491 repo_read_index(repo
);
3492 istate
= repo
->index
;
3493 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3494 struct cache_entry
*ce
= istate
->cache
[i
];
3495 struct cache_entry
*new_ce
;
3501 len
= ce_namelen(ce
);
3502 new_ce
= make_empty_cache_entry(istate
, len
);
3503 memcpy(new_ce
->name
, ce
->name
, len
);
3504 new_ce
->ce_flags
= create_ce_flags(0) | CE_CONFLICTED
;
3505 new_ce
->ce_namelen
= len
;
3506 new_ce
->ce_mode
= ce
->ce_mode
;
3507 if (add_index_entry(istate
, new_ce
, ADD_CACHE_SKIP_DFCHECK
))
3508 return error(_("%s: cannot drop to stage #0"),
3515 * Returns 1 if the path is an "other" path with respect to
3516 * the index; that is, the path is not mentioned in the index at all,
3517 * either as a file, a directory with some files in the index,
3518 * or as an unmerged entry.
3520 * We helpfully remove a trailing "/" from directories so that
3521 * the output of read_directory can be used as-is.
3523 int index_name_is_other(struct index_state
*istate
, const char *name
,
3527 if (namelen
&& name
[namelen
- 1] == '/')
3529 pos
= index_name_pos(istate
, name
, namelen
);
3531 return 0; /* exact match */
3533 if (pos
< istate
->cache_nr
) {
3534 struct cache_entry
*ce
= istate
->cache
[pos
];
3535 if (ce_namelen(ce
) == namelen
&&
3536 !memcmp(ce
->name
, name
, namelen
))
3537 return 0; /* Yup, this one exists unmerged */
3542 void *read_blob_data_from_index(struct index_state
*istate
,
3543 const char *path
, unsigned long *size
)
3547 enum object_type type
;
3551 pos
= index_name_pos(istate
, path
, len
);
3554 * We might be in the middle of a merge, in which
3555 * case we would read stage #2 (ours).
3559 (pos
< 0 && i
< istate
->cache_nr
&&
3560 !strcmp(istate
->cache
[i
]->name
, path
));
3562 if (ce_stage(istate
->cache
[i
]) == 2)
3567 data
= repo_read_object_file(the_repository
, &istate
->cache
[pos
]->oid
,
3569 if (!data
|| type
!= OBJ_BLOB
) {
3578 void stat_validity_clear(struct stat_validity
*sv
)
3580 FREE_AND_NULL(sv
->sd
);
3583 int stat_validity_check(struct stat_validity
*sv
, const char *path
)
3587 if (stat(path
, &st
) < 0)
3588 return sv
->sd
== NULL
;
3591 return S_ISREG(st
.st_mode
) && !match_stat_data(sv
->sd
, &st
);
3594 void stat_validity_update(struct stat_validity
*sv
, int fd
)
3598 if (fstat(fd
, &st
) < 0 || !S_ISREG(st
.st_mode
))
3599 stat_validity_clear(sv
);
3602 CALLOC_ARRAY(sv
->sd
, 1);
3603 fill_stat_data(sv
->sd
, &st
);
3607 void move_index_extensions(struct index_state
*dst
, struct index_state
*src
)
3609 dst
->untracked
= src
->untracked
;
3610 src
->untracked
= NULL
;
3611 dst
->cache_tree
= src
->cache_tree
;
3612 src
->cache_tree
= NULL
;
3615 struct cache_entry
*dup_cache_entry(const struct cache_entry
*ce
,
3616 struct index_state
*istate
)
3618 unsigned int size
= ce_size(ce
);
3619 int mem_pool_allocated
;
3620 struct cache_entry
*new_entry
= make_empty_cache_entry(istate
, ce_namelen(ce
));
3621 mem_pool_allocated
= new_entry
->mem_pool_allocated
;
3623 memcpy(new_entry
, ce
, size
);
3624 new_entry
->mem_pool_allocated
= mem_pool_allocated
;
3628 void discard_cache_entry(struct cache_entry
*ce
)
3630 if (ce
&& should_validate_cache_entries())
3631 memset(ce
, 0xCD, cache_entry_size(ce
->ce_namelen
));
3633 if (ce
&& ce
->mem_pool_allocated
)
3639 int should_validate_cache_entries(void)
3641 static int validate_index_cache_entries
= -1;
3643 if (validate_index_cache_entries
< 0) {
3644 if (getenv("GIT_TEST_VALIDATE_INDEX_CACHE_ENTRIES"))
3645 validate_index_cache_entries
= 1;
3647 validate_index_cache_entries
= 0;
3650 return validate_index_cache_entries
;
3653 #define EOIE_SIZE (4 + GIT_SHA1_RAWSZ) /* <4-byte offset> + <20-byte hash> */
3654 #define EOIE_SIZE_WITH_HEADER (4 + 4 + EOIE_SIZE) /* <4-byte signature> + <4-byte length> + EOIE_SIZE */
3656 static size_t read_eoie_extension(const char *mmap
, size_t mmap_size
)
3659 * The end of index entries (EOIE) extension is guaranteed to be last
3660 * so that it can be found by scanning backwards from the EOF.
3667 const char *index
, *eoie
;
3669 size_t offset
, src_offset
;
3670 unsigned char hash
[GIT_MAX_RAWSZ
];
3673 /* ensure we have an index big enough to contain an EOIE extension */
3674 if (mmap_size
< sizeof(struct cache_header
) + EOIE_SIZE_WITH_HEADER
+ the_hash_algo
->rawsz
)
3677 /* validate the extension signature */
3678 index
= eoie
= mmap
+ mmap_size
- EOIE_SIZE_WITH_HEADER
- the_hash_algo
->rawsz
;
3679 if (CACHE_EXT(index
) != CACHE_EXT_ENDOFINDEXENTRIES
)
3681 index
+= sizeof(uint32_t);
3683 /* validate the extension size */
3684 extsize
= get_be32(index
);
3685 if (extsize
!= EOIE_SIZE
)
3687 index
+= sizeof(uint32_t);
3690 * Validate the offset we're going to look for the first extension
3691 * signature is after the index header and before the eoie extension.
3693 offset
= get_be32(index
);
3694 if (mmap
+ offset
< mmap
+ sizeof(struct cache_header
))
3696 if (mmap
+ offset
>= eoie
)
3698 index
+= sizeof(uint32_t);
3701 * The hash is computed over extension types and their sizes (but not
3702 * their contents). E.g. if we have "TREE" extension that is N-bytes
3703 * long, "REUC" extension that is M-bytes long, followed by "EOIE",
3704 * then the hash would be:
3706 * SHA-1("TREE" + <binary representation of N> +
3707 * "REUC" + <binary representation of M>)
3709 src_offset
= offset
;
3710 the_hash_algo
->init_fn(&c
);
3711 while (src_offset
< mmap_size
- the_hash_algo
->rawsz
- EOIE_SIZE_WITH_HEADER
) {
3712 /* After an array of active_nr index entries,
3713 * there can be arbitrary number of extended
3714 * sections, each of which is prefixed with
3715 * extension name (4-byte) and section length
3716 * in 4-byte network byte order.
3719 memcpy(&extsize
, mmap
+ src_offset
+ 4, 4);
3720 extsize
= ntohl(extsize
);
3722 /* verify the extension size isn't so large it will wrap around */
3723 if (src_offset
+ 8 + extsize
< src_offset
)
3726 the_hash_algo
->update_fn(&c
, mmap
+ src_offset
, 8);
3729 src_offset
+= extsize
;
3731 the_hash_algo
->final_fn(hash
, &c
);
3732 if (!hasheq(hash
, (const unsigned char *)index
))
3735 /* Validate that the extension offsets returned us back to the eoie extension. */
3736 if (src_offset
!= mmap_size
- the_hash_algo
->rawsz
- EOIE_SIZE_WITH_HEADER
)
3742 static void write_eoie_extension(struct strbuf
*sb
, git_hash_ctx
*eoie_context
, size_t offset
)
3745 unsigned char hash
[GIT_MAX_RAWSZ
];
3748 put_be32(&buffer
, offset
);
3749 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3752 the_hash_algo
->final_fn(hash
, eoie_context
);
3753 strbuf_add(sb
, hash
, the_hash_algo
->rawsz
);
3756 #define IEOT_VERSION (1)
3758 static struct index_entry_offset_table
*read_ieot_extension(const char *mmap
, size_t mmap_size
, size_t offset
)
3760 const char *index
= NULL
;
3761 uint32_t extsize
, ext_version
;
3762 struct index_entry_offset_table
*ieot
;
3765 /* find the IEOT extension */
3768 while (offset
<= mmap_size
- the_hash_algo
->rawsz
- 8) {
3769 extsize
= get_be32(mmap
+ offset
+ 4);
3770 if (CACHE_EXT((mmap
+ offset
)) == CACHE_EXT_INDEXENTRYOFFSETTABLE
) {
3771 index
= mmap
+ offset
+ 4 + 4;
3780 /* validate the version is IEOT_VERSION */
3781 ext_version
= get_be32(index
);
3782 if (ext_version
!= IEOT_VERSION
) {
3783 error("invalid IEOT version %d", ext_version
);
3786 index
+= sizeof(uint32_t);
3788 /* extension size - version bytes / bytes per entry */
3789 nr
= (extsize
- sizeof(uint32_t)) / (sizeof(uint32_t) + sizeof(uint32_t));
3791 error("invalid number of IEOT entries %d", nr
);
3794 ieot
= xmalloc(sizeof(struct index_entry_offset_table
)
3795 + (nr
* sizeof(struct index_entry_offset
)));
3797 for (i
= 0; i
< nr
; i
++) {
3798 ieot
->entries
[i
].offset
= get_be32(index
);
3799 index
+= sizeof(uint32_t);
3800 ieot
->entries
[i
].nr
= get_be32(index
);
3801 index
+= sizeof(uint32_t);
3807 static void write_ieot_extension(struct strbuf
*sb
, struct index_entry_offset_table
*ieot
)
3813 put_be32(&buffer
, IEOT_VERSION
);
3814 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3817 for (i
= 0; i
< ieot
->nr
; i
++) {
3820 put_be32(&buffer
, ieot
->entries
[i
].offset
);
3821 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3824 put_be32(&buffer
, ieot
->entries
[i
].nr
);
3825 strbuf_add(sb
, &buffer
, sizeof(uint32_t));
3829 void prefetch_cache_entries(const struct index_state
*istate
,
3830 must_prefetch_predicate must_prefetch
)
3833 struct oid_array to_fetch
= OID_ARRAY_INIT
;
3835 for (i
= 0; i
< istate
->cache_nr
; i
++) {
3836 struct cache_entry
*ce
= istate
->cache
[i
];
3838 if (S_ISGITLINK(ce
->ce_mode
) || !must_prefetch(ce
))
3840 if (!oid_object_info_extended(the_repository
, &ce
->oid
,
3842 OBJECT_INFO_FOR_PREFETCH
))
3844 oid_array_append(&to_fetch
, &ce
->oid
);
3846 promisor_remote_get_direct(the_repository
,
3847 to_fetch
.oid
, to_fetch
.nr
);
3848 oid_array_clear(&to_fetch
);