read-cache*.h: move declarations for read-cache.c functions from cache.h

For the functions defined in read-cache.c, move their declarations from cache.h to a new header, read-cache-ll.h. Also move some related inline functions from cache.h to read-cache.h. The purpose of the read-cache-ll.h/read-cache.h split is that about 70% of the sites don't need the inline functions and the extra headers they include. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed May 16, 2023 at 06:33 UTC 08c46a499aec5b6459fb1d55ff90403c7dc2ee5a
77 files changed +603 -524
add-interactive.c
+1
@@ -6,6 +6,7 @@
6 #include "gettext.h"
7 #include "hex.h"
8 #include "preload-index.h"
9 +#include "read-cache-ll.h"
10 #include "revision.h"
11 #include "refs.h"
12 #include "string-list.h"
add-patch.c
+1
@@ -6,6 +6,7 @@
6 #include "environment.h"
7 #include "gettext.h"
8 #include "object-name.h"
9 +#include "read-cache-ll.h"
10 #include "strbuf.h"
11 #include "run-command.h"
12 #include "strvec.h"
apply.c
+1
@@ -28,6 +28,7 @@
28 #include "object-file.h"
29 #include "parse-options.h"
30 #include "quote.h"
31 +#include "read-cache.h"
32 #include "rerere.h"
33 #include "apply.h"
34 #include "entry.h"
attr.c
+1
@@ -16,6 +16,7 @@
16 #include "gettext.h"
17 #include "utf8.h"
18 #include "quote.h"
19 +#include "read-cache-ll.h"
20 #include "revision.h"
21 #include "object-store.h"
22 #include "setup.h"
blame.c
+1
@@ -8,6 +8,7 @@
8 #include "diffcore.h"
9 #include "gettext.h"
10 #include "hex.h"
11 +#include "read-cache.h"
12 #include "setup.h"
13 #include "tag.h"
14 #include "trace2.h"
builtin/add.c
+1
@@ -20,6 +20,7 @@
20 #include "preload-index.h"
21 #include "diff.h"
22 #include "diffcore.h"
23 +#include "read-cache.h"
24 #include "revision.h"
25 #include "bulk-checkin.h"
26 #include "strvec.h"
builtin/checkout--worker.c
+1
@@ -6,6 +6,7 @@
6 #include "parallel-checkout.h"
7 #include "parse-options.h"
8 #include "pkt-line.h"
9 +#include "read-cache-ll.h"
10
11 static void packet_to_pc_item(const char *buffer, int len,
12 struct parallel_checkout_item *pc_item)
builtin/checkout-index.c
+1
@@ -16,6 +16,7 @@
16 #include "parse-options.h"
17 #include "entry.h"
18 #include "parallel-checkout.h"
19 +#include "read-cache-ll.h"
20 #include "setup.h"
21 #include "sparse-index.h"
22
builtin/checkout.c
+1
@@ -21,6 +21,7 @@
21 #include "object-store.h"
22 #include "parse-options.h"
23 #include "preload-index.h"
24 +#include "read-cache.h"
25 #include "refs.h"
26 #include "remote.h"
27 #include "resolve-undo.h"
builtin/clean.c
+1
@@ -14,6 +14,7 @@
14 #include "dir.h"
15 #include "gettext.h"
16 #include "parse-options.h"
17 +#include "read-cache-ll.h"
18 #include "repository.h"
19 #include "setup.h"
20 #include "string-list.h"
builtin/commit.c
+1
@@ -31,6 +31,7 @@
31 #include "object-name.h"
32 #include "parse-options.h"
33 #include "preload-index.h"
34 +#include "read-cache.h"
35 #include "string-list.h"
36 #include "rerere.h"
37 #include "unpack-trees.h"
builtin/describe.c
+1
@@ -13,6 +13,7 @@
13 #include "exec-cmd.h"
14 #include "object-name.h"
15 #include "parse-options.h"
16 +#include "read-cache-ll.h"
17 #include "revision.h"
18 #include "diff.h"
19 #include "hashmap.h"
builtin/diff-tree.c
+1
@@ -8,6 +8,7 @@
8 #include "log-tree.h"
9 #include "builtin.h"
10 #include "submodule.h"
11 +#include "read-cache-ll.h"
12 #include "repository.h"
13 #include "tree.h"
14
builtin/diff.c
+1
@@ -17,6 +17,7 @@
17 #include "diff-merges.h"
18 #include "diffcore.h"
19 #include "preload-index.h"
20 +#include "read-cache-ll.h"
21 #include "revision.h"
22 #include "log-tree.h"
23 #include "builtin.h"
builtin/difftool.c
+1
@@ -23,6 +23,7 @@
23 #include "gettext.h"
24 #include "hex.h"
25 #include "parse-options.h"
26 +#include "read-cache-ll.h"
27 #include "sparse-index.h"
28 #include "strvec.h"
29 #include "strbuf.h"
builtin/fsck.c
+1
@@ -22,6 +22,7 @@
22 #include "object-file.h"
23 #include "object-name.h"
24 #include "object-store.h"
25 +#include "read-cache-ll.h"
26 #include "replace-object.h"
27 #include "resolve-undo.h"
28 #include "run-command.h"
builtin/grep.c
+1
@@ -31,6 +31,7 @@
31 #include "object-store.h"
32 #include "packfile.h"
33 #include "pager.h"
34 +#include "read-cache-ll.h"
35 #include "write-or-die.h"
36
37 static const char *grep_prefix;
builtin/ls-files.c
+1
@@ -21,6 +21,7 @@
21 #include "resolve-undo.h"
22 #include "string-list.h"
23 #include "pathspec.h"
24 +#include "read-cache.h"
25 #include "run-command.h"
26 #include "setup.h"
27 #include "sparse-index.h"
builtin/merge-index.c
+1
@@ -1,6 +1,7 @@
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "hex.h"
4 +#include "read-cache-ll.h"
5 #include "repository.h"
6 #include "run-command.h"
7 #include "sparse-index.h"
builtin/mv.c
+1
@@ -19,6 +19,7 @@
19 #include "cache-tree.h"
20 #include "string-list.h"
21 #include "parse-options.h"
22 +#include "read-cache-ll.h"
23 #include "repository.h"
24 #include "setup.h"
25 #include "submodule.h"
builtin/pull.c
+1
@@ -20,6 +20,7 @@
20 #include "oid-array.h"
21 #include "remote.h"
22 #include "dir.h"
23 +#include "read-cache-ll.h"
24 #include "rebase.h"
25 #include "refs.h"
26 #include "refspec.h"
builtin/rev-parse.c
+1
@@ -18,6 +18,7 @@
18 #include "object-name.h"
19 #include "parse-options.h"
20 #include "diff.h"
21 +#include "read-cache-ll.h"
22 #include "revision.h"
23 #include "setup.h"
24 #include "split-index.h"
builtin/rm.c
+1
@@ -16,6 +16,7 @@
16 #include "tree-walk.h"
17 #include "object-name.h"
18 #include "parse-options.h"
19 +#include "read-cache.h"
20 #include "repository.h"
21 #include "string-list.h"
22 #include "setup.h"
builtin/stash.c
+1
@@ -18,6 +18,7 @@
18 #include "dir.h"
19 #include "entry.h"
20 #include "preload-index.h"
21 +#include "read-cache.h"
22 #include "rerere.h"
23 #include "revision.h"
24 #include "setup.h"
builtin/submodule--helper.c
+1
@@ -13,6 +13,7 @@
13 #include "pathspec.h"
14 #include "preload-index.h"
15 #include "dir.h"
16 +#include "read-cache.h"
17 #include "setup.h"
18 #include "sparse-index.h"
19 #include "submodule.h"
builtin/update-index.c
+1
@@ -22,6 +22,7 @@
22 #include "parse-options.h"
23 #include "pathspec.h"
24 #include "dir.h"
25 +#include "read-cache.h"
26 #include "repository.h"
27 #include "setup.h"
28 #include "sparse-index.h"
builtin/worktree.c
+1
@@ -13,6 +13,7 @@
13 #include "parse-options.h"
14 #include "strvec.h"
15 #include "branch.h"
16 +#include "read-cache-ll.h"
17 #include "refs.h"
18 #include "repository.h"
19 #include "run-command.h"
cache-tree.c
+1
@@ -9,6 +9,7 @@
9 #include "bulk-checkin.h"
10 #include "object-file.h"
11 #include "object-store.h"
12 +#include "read-cache-ll.h"
13 #include "replace-object.h"
14 #include "promisor-remote.h"
15 #include "sparse-index.h"
cache.h
-520
@@ -2,525 +2,5 @@
2 #define CACHE_H
3
4 #include "git-compat-util.h"
5 -#include "strbuf.h"
6 -#include "hashmap.h"
7 -#include "pathspec.h"
8 -#include "object.h"
9 -#include "statinfo.h"
10 -
11 -/*
12 - * Basic data structures for the directory cache
13 - */
14 -
15 -#define CACHE_SIGNATURE 0x44495243 /* "DIRC" */
16 -struct cache_header {
17 - uint32_t hdr_signature;
18 - uint32_t hdr_version;
19 - uint32_t hdr_entries;
20 -};
21 -
22 -#define INDEX_FORMAT_LB 2
23 -#define INDEX_FORMAT_UB 4
24 -
25 -struct cache_entry {
26 - struct hashmap_entry ent;
27 - struct stat_data ce_stat_data;
28 - unsigned int ce_mode;
29 - unsigned int ce_flags;
30 - unsigned int mem_pool_allocated;
31 - unsigned int ce_namelen;
32 - unsigned int index; /* for link extension */
33 - struct object_id oid;
34 - char name[FLEX_ARRAY]; /* more */
35 -};
36 -
37 -#define CE_STAGEMASK (0x3000)
38 -#define CE_EXTENDED (0x4000)
39 -#define CE_VALID (0x8000)
40 -#define CE_STAGESHIFT 12
41 -
42 -/*
43 - * Range 0xFFFF0FFF in ce_flags is divided into
44 - * two parts: in-memory flags and on-disk ones.
45 - * Flags in CE_EXTENDED_FLAGS will get saved on-disk
46 - * if you want to save a new flag, add it in
47 - * CE_EXTENDED_FLAGS
48 - *
49 - * In-memory only flags
50 - */
51 -#define CE_UPDATE (1 << 16)
52 -#define CE_REMOVE (1 << 17)
53 -#define CE_UPTODATE (1 << 18)
54 -#define CE_ADDED (1 << 19)
55 -
56 -#define CE_HASHED (1 << 20)
57 -#define CE_FSMONITOR_VALID (1 << 21)
58 -#define CE_WT_REMOVE (1 << 22) /* remove in work directory */
59 -#define CE_CONFLICTED (1 << 23)
60 -
61 -#define CE_UNPACKED (1 << 24)
62 -#define CE_NEW_SKIP_WORKTREE (1 << 25)
63 -
64 -/* used to temporarily mark paths matched by pathspecs */
65 -#define CE_MATCHED (1 << 26)
66 -
67 -#define CE_UPDATE_IN_BASE (1 << 27)
68 -#define CE_STRIP_NAME (1 << 28)
69 -
70 -/*
71 - * Extended on-disk flags
72 - */
73 -#define CE_INTENT_TO_ADD (1 << 29)
74 -#define CE_SKIP_WORKTREE (1 << 30)
75 -/* CE_EXTENDED2 is for future extension */
76 -#define CE_EXTENDED2 (1U << 31)
77 -
78 -#define CE_EXTENDED_FLAGS (CE_INTENT_TO_ADD | CE_SKIP_WORKTREE)
79 -
80 -/*
81 - * Safeguard to avoid saving wrong flags:
82 - * - CE_EXTENDED2 won't get saved until its semantic is known
83 - * - Bits in 0x0000FFFF have been saved in ce_flags already
84 - * - Bits in 0x003F0000 are currently in-memory flags
85 - */
86 -#if CE_EXTENDED_FLAGS & 0x803FFFFF
87 -#error "CE_EXTENDED_FLAGS out of range"
88 -#endif
89 -
90 -/* Forward structure decls */
91 -struct pathspec;
92 -struct tree;
93 -
94 -/*
95 - * Copy the sha1 and stat state of a cache entry from one to
96 - * another. But we never change the name, or the hash state!
97 - */
98 -static inline void copy_cache_entry(struct cache_entry *dst,
99 - const struct cache_entry *src)
100 -{
101 - unsigned int state = dst->ce_flags & CE_HASHED;
102 - int mem_pool_allocated = dst->mem_pool_allocated;
103 -
104 - /* Don't copy hash chain and name */
105 - memcpy(&dst->ce_stat_data, &src->ce_stat_data,
106 - offsetof(struct cache_entry, name) -
107 - offsetof(struct cache_entry, ce_stat_data));
108 -
109 - /* Restore the hash state */
110 - dst->ce_flags = (dst->ce_flags & ~CE_HASHED) | state;
111 -
112 - /* Restore the mem_pool_allocated flag */
113 - dst->mem_pool_allocated = mem_pool_allocated;
114 -}
115 -
116 -static inline unsigned create_ce_flags(unsigned stage)
117 -{
118 - return (stage << CE_STAGESHIFT);
119 -}
120 -
121 -#define ce_namelen(ce) ((ce)->ce_namelen)
122 -#define ce_size(ce) cache_entry_size(ce_namelen(ce))
123 -#define ce_stage(ce) ((CE_STAGEMASK & (ce)->ce_flags) >> CE_STAGESHIFT)
124 -#define ce_uptodate(ce) ((ce)->ce_flags & CE_UPTODATE)
125 -#define ce_skip_worktree(ce) ((ce)->ce_flags & CE_SKIP_WORKTREE)
126 -#define ce_mark_uptodate(ce) ((ce)->ce_flags |= CE_UPTODATE)
127 -#define ce_intent_to_add(ce) ((ce)->ce_flags & CE_INTENT_TO_ADD)
128 -
129 -static inline unsigned int ce_mode_from_stat(const struct cache_entry *ce,
130 - unsigned int mode)
131 -{
132 - extern int trust_executable_bit, has_symlinks;
133 - if (!has_symlinks && S_ISREG(mode) &&
134 - ce && S_ISLNK(ce->ce_mode))
135 - return ce->ce_mode;
136 - if (!trust_executable_bit && S_ISREG(mode)) {
137 - if (ce && S_ISREG(ce->ce_mode))
138 - return ce->ce_mode;
139 - return create_ce_mode(0666);
140 - }
141 - return create_ce_mode(mode);
142 -}
143 -static inline int ce_to_dtype(const struct cache_entry *ce)
144 -{
145 - unsigned ce_mode = ntohl(ce->ce_mode);
146 - if (S_ISREG(ce_mode))
147 - return DT_REG;
148 - else if (S_ISDIR(ce_mode) || S_ISGITLINK(ce_mode))
149 - return DT_DIR;
150 - else if (S_ISLNK(ce_mode))
151 - return DT_LNK;
152 - else
153 - return DT_UNKNOWN;
154 -}
155 -
156 -static inline int ce_path_match(struct index_state *istate,
157 - const struct cache_entry *ce,
158 - const struct pathspec *pathspec,
159 - char *seen)
160 -{
161 - return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
162 - S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
163 -}
164 -
165 -#define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)
166 -
167 -#define SOMETHING_CHANGED (1 << 0) /* unclassified changes go here */
168 -#define CE_ENTRY_CHANGED (1 << 1)
169 -#define CE_ENTRY_REMOVED (1 << 2)
170 -#define CE_ENTRY_ADDED (1 << 3)
171 -#define RESOLVE_UNDO_CHANGED (1 << 4)
172 -#define CACHE_TREE_CHANGED (1 << 5)
173 -#define SPLIT_INDEX_ORDERED (1 << 6)
174 -#define UNTRACKED_CHANGED (1 << 7)
175 -#define FSMONITOR_CHANGED (1 << 8)
176 -
177 -struct split_index;
178 -struct untracked_cache;
179 -struct progress;
180 -struct pattern_list;
181 -
182 -enum sparse_index_mode {
183 - /*
184 - * There are no sparse directories in the index at all.
185 - *
186 - * Repositories that don't use cone-mode sparse-checkout will
187 - * always have their indexes in this mode.
188 - */
189 - INDEX_EXPANDED = 0,
190 -
191 - /*
192 - * The index has already been collapsed to sparse directories
193 - * whereever possible.
194 - */
195 - INDEX_COLLAPSED,
196 -
197 - /*
198 - * The sparse directories that exist are outside the
199 - * sparse-checkout boundary, but it is possible that some file
200 - * entries could collapse to sparse directory entries.
201 - */
202 - INDEX_PARTIALLY_SPARSE,
203 -};
204 -
205 -struct index_state {
206 - struct cache_entry **cache;
207 - unsigned int version;
208 - unsigned int cache_nr, cache_alloc, cache_changed;
209 - struct string_list *resolve_undo;
210 - struct cache_tree *cache_tree;
211 - struct split_index *split_index;
212 - struct cache_time timestamp;
213 - unsigned name_hash_initialized : 1,
214 - initialized : 1,
215 - drop_cache_tree : 1,
216 - updated_workdir : 1,
217 - updated_skipworktree : 1,
218 - fsmonitor_has_run_once : 1;
219 - enum sparse_index_mode sparse_index;
220 - struct hashmap name_hash;
221 - struct hashmap dir_hash;
222 - struct object_id oid;
223 - struct untracked_cache *untracked;
224 - char *fsmonitor_last_update;
225 - struct ewah_bitmap *fsmonitor_dirty;
226 - struct mem_pool *ce_mem_pool;
227 - struct progress *progress;
228 - struct repository *repo;
229 - struct pattern_list *sparse_checkout_patterns;
230 -};
231 -
232 -/**
233 - * A "struct index_state istate" must be initialized with
234 - * INDEX_STATE_INIT or the corresponding index_state_init().
235 - *
236 - * If the variable won't be used again, use release_index() to free()
237 - * its resources. If it needs to be used again use discard_index(),
238 - * which does the same thing, but will use use index_state_init() at
239 - * the end. The discard_index() will use its own "istate->repo" as the
240 - * "r" argument to index_state_init() in that case.
241 - */
242 -#define INDEX_STATE_INIT(r) { \
243 - .repo = (r), \
244 -}
245 -void index_state_init(struct index_state *istate, struct repository *r);
246 -void release_index(struct index_state *istate);
247 -
248 -/* Cache entry creation and cleanup */
249 -
250 -/*
251 - * Create cache_entry intended for use in the specified index. Caller
252 - * is responsible for discarding the cache_entry with
253 - * `discard_cache_entry`.
254 - */
255 -struct cache_entry *make_cache_entry(struct index_state *istate,
256 - unsigned int mode,
257 - const struct object_id *oid,
258 - const char *path,
259 - int stage,
260 - unsigned int refresh_options);
261 -
262 -struct cache_entry *make_empty_cache_entry(struct index_state *istate,
263 - size_t name_len);
264 -
265 -/*
266 - * Create a cache_entry that is not intended to be added to an index. If
267 - * `ce_mem_pool` is not NULL, the entry is allocated within the given memory
268 - * pool. Caller is responsible for discarding "loose" entries with
269 - * `discard_cache_entry()` and the memory pool with
270 - * `mem_pool_discard(ce_mem_pool, should_validate_cache_entries())`.
271 - */
272 -struct cache_entry *make_transient_cache_entry(unsigned int mode,
273 - const struct object_id *oid,
274 - const char *path,
275 - int stage,
276 - struct mem_pool *ce_mem_pool);
277 -
278 -struct cache_entry *make_empty_transient_cache_entry(size_t len,
279 - struct mem_pool *ce_mem_pool);
280 -
281 -/*
282 - * Discard cache entry.
283 - */
284 -void discard_cache_entry(struct cache_entry *ce);
285 -
286 -/*
287 - * Check configuration if we should perform extra validation on cache
288 - * entries.
289 - */
290 -int should_validate_cache_entries(void);
291 -
292 -/*
293 - * Duplicate a cache_entry. Allocate memory for the new entry from a
294 - * memory_pool. Takes into account cache_entry fields that are meant
295 - * for managing the underlying memory allocation of the cache_entry.
296 - */
297 -struct cache_entry *dup_cache_entry(const struct cache_entry *ce, struct index_state *istate);
298 -
299 -/*
300 - * Validate the cache entries in the index. This is an internal
301 - * consistency check that the cache_entry structs are allocated from
302 - * the expected memory pool.
303 - */
304 -void validate_cache_entries(const struct index_state *istate);
305 -
306 -/*
307 - * Bulk prefetch all missing cache entries that are not GITLINKs and that match
308 - * the given predicate. This function should only be called if
309 - * repo_has_promisor_remote() returns true.
310 - */
311 -typedef int (*must_prefetch_predicate)(const struct cache_entry *);
312 -void prefetch_cache_entries(const struct index_state *istate,
313 - must_prefetch_predicate must_prefetch);
314 -
315 -/* Initialize and use the cache information */
316 -struct lock_file;
317 -int do_read_index(struct index_state *istate, const char *path,
318 - int must_exist); /* for testting only! */
319 -int read_index_from(struct index_state *, const char *path,
320 - const char *gitdir);
321 -int is_index_unborn(struct index_state *);
322 -
323 -/* For use with `write_locked_index()`. */
324 -#define COMMIT_LOCK (1 << 0)
325 -#define SKIP_IF_UNCHANGED (1 << 1)
326 -
327 -/*
328 - * Write the index while holding an already-taken lock. Close the lock,
329 - * and if `COMMIT_LOCK` is given, commit it.
330 - *
331 - * Unless a split index is in use, write the index into the lockfile.
332 - *
333 - * With a split index, write the shared index to a temporary file,
334 - * adjust its permissions and rename it into place, then write the
335 - * split index to the lockfile. If the temporary file for the shared
336 - * index cannot be created, fall back to the behavior described in
337 - * the previous paragraph.
338 - *
339 - * With `COMMIT_LOCK`, the lock is always committed or rolled back.
340 - * Without it, the lock is closed, but neither committed nor rolled
341 - * back.
342 - *
343 - * If `SKIP_IF_UNCHANGED` is given and the index is unchanged, nothing
344 - * is written (and the lock is rolled back if `COMMIT_LOCK` is given).
345 - */
346 -int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
347 -
348 -void discard_index(struct index_state *);
349 -void move_index_extensions(struct index_state *dst, struct index_state *src);
350 -int unmerged_index(const struct index_state *);
351 -
352 -/**
353 - * Returns 1 if istate differs from tree, 0 otherwise. If tree is NULL,
354 - * compares istate to HEAD. If tree is NULL and on an unborn branch,
355 - * returns 1 if there are entries in istate, 0 otherwise. If an strbuf is
356 - * provided, the space-separated list of files that differ will be appended
357 - * to it.
358 - */
359 -int repo_index_has_changes(struct repository *repo,
360 - struct tree *tree,
361 - struct strbuf *sb);
362 -
363 -int verify_path(const char *path, unsigned mode);
364 -int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
365 -
366 -/*
367 - * Searches for an entry defined by name and namelen in the given index.
368 - * If the return value is positive (including 0) it is the position of an
369 - * exact match. If the return value is negative, the negated value minus 1
370 - * is the position where the entry would be inserted.
371 - * Example: The current index consists of these files and its stages:
372 - *
373 - * b#0, d#0, f#1, f#3
374 - *
375 - * index_name_pos(&index, "a", 1) -> -1
376 - * index_name_pos(&index, "b", 1) -> 0
377 - * index_name_pos(&index, "c", 1) -> -2
378 - * index_name_pos(&index, "d", 1) -> 1
379 - * index_name_pos(&index, "e", 1) -> -3
380 - * index_name_pos(&index, "f", 1) -> -3
381 - * index_name_pos(&index, "g", 1) -> -5
382 - */
383 -int index_name_pos(struct index_state *, const char *name, int namelen);
384 -
385 -/*
386 - * Like index_name_pos, returns the position of an entry of the given name in
387 - * the index if one exists, otherwise returns a negative value where the negated
388 - * value minus 1 is the position where the index entry would be inserted. Unlike
389 - * index_name_pos, however, a sparse index is not expanded to find an entry
390 - * inside a sparse directory.
391 - */
392 -int index_name_pos_sparse(struct index_state *, const char *name, int namelen);
393 -
394 -/*
395 - * Determines whether an entry with the given name exists within the
396 - * given index. The return value is 1 if an exact match is found, otherwise
397 - * it is 0. Note that, unlike index_name_pos, this function does not expand
398 - * the index if it is sparse. If an item exists within the full index but it
399 - * is contained within a sparse directory (and not in the sparse index), 0 is
400 - * returned.
401 - */
402 -int index_entry_exists(struct index_state *, const char *name, int namelen);
403 -
404 -/*
405 - * Some functions return the negative complement of an insert position when a
406 - * precise match was not found but a position was found where the entry would
407 - * need to be inserted. This helper protects that logic from any integer
408 - * underflow.
409 - */
410 -static inline int index_pos_to_insert_pos(uintmax_t pos)
411 -{
412 - if (pos > INT_MAX)
413 - die("overflow: -1 - %"PRIuMAX, pos);
414 - return -1 - (int)pos;
415 -}
416 -
417 -#define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
418 -#define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
419 -#define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
420 -#define ADD_CACHE_JUST_APPEND 8 /* Append only */
421 -#define ADD_CACHE_NEW_ONLY 16 /* Do not replace existing ones */
422 -#define ADD_CACHE_KEEP_CACHE_TREE 32 /* Do not invalidate cache-tree */
423 -#define ADD_CACHE_RENORMALIZE 64 /* Pass along HASH_RENORMALIZE */
424 -int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
425 -void rename_index_entry_at(struct index_state *, int pos, const char *new_name);
426 -
427 -/* Remove entry, return true if there are more entries to go. */
428 -int remove_index_entry_at(struct index_state *, int pos);
429 -
430 -void remove_marked_cache_entries(struct index_state *istate, int invalidate);
431 -int remove_file_from_index(struct index_state *, const char *path);
432 -#define ADD_CACHE_VERBOSE 1
433 -#define ADD_CACHE_PRETEND 2
434 -#define ADD_CACHE_IGNORE_ERRORS 4
435 -#define ADD_CACHE_IGNORE_REMOVAL 8
436 -#define ADD_CACHE_INTENT 16
437 -/*
438 - * These two are used to add the contents of the file at path
439 - * to the index, marking the working tree up-to-date by storing
440 - * the cached stat info in the resulting cache entry. A caller
441 - * that has already run lstat(2) on the path can call
442 - * add_to_index(), and all others can call add_file_to_index();
443 - * the latter will do necessary lstat(2) internally before
444 - * calling the former.
445 - */
446 -int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
447 -int add_file_to_index(struct index_state *, const char *path, int flags);
448 -
449 -int chmod_index_entry(struct index_state *, struct cache_entry *ce, char flip);
450 -int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
451 -void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
452 -int index_name_is_other(struct index_state *, const char *, int);
453 -void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);
454 -
455 -/* do stat comparison even if CE_VALID is true */
456 -#define CE_MATCH_IGNORE_VALID 01
457 -/* do not check the contents but report dirty on racily-clean entries */
458 -#define CE_MATCH_RACY_IS_DIRTY 02
459 -/* do stat comparison even if CE_SKIP_WORKTREE is true */
460 -#define CE_MATCH_IGNORE_SKIP_WORKTREE 04
461 -/* ignore non-existent files during stat update */
462 -#define CE_MATCH_IGNORE_MISSING 0x08
463 -/* enable stat refresh */
464 -#define CE_MATCH_REFRESH 0x10
465 -/* don't refresh_fsmonitor state or do stat comparison even if CE_FSMONITOR_VALID is true */
466 -#define CE_MATCH_IGNORE_FSMONITOR 0X20
467 -int is_racy_timestamp(const struct index_state *istate,
468 - const struct cache_entry *ce);
469 -int has_racy_timestamp(struct index_state *istate);
470 -int ie_match_stat(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
471 -int ie_modified(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
472 -
473 -int match_stat_data_racy(const struct index_state *istate,
474 - const struct stat_data *sd, struct stat *st);
475 -
476 -void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st);
477 -
478 -#define REFRESH_REALLY (1 << 0) /* ignore_valid */
479 -#define REFRESH_UNMERGED (1 << 1) /* allow unmerged */
480 -#define REFRESH_QUIET (1 << 2) /* be quiet about it */
481 -#define REFRESH_IGNORE_MISSING (1 << 3) /* ignore non-existent */
482 -#define REFRESH_IGNORE_SUBMODULES (1 << 4) /* ignore submodules */
483 -#define REFRESH_IN_PORCELAIN (1 << 5) /* user friendly output, not "needs update" */
484 -#define REFRESH_PROGRESS (1 << 6) /* show progress bar if stderr is tty */
485 -#define REFRESH_IGNORE_SKIP_WORKTREE (1 << 7) /* ignore skip_worktree entries */
486 -int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
487 -/*
488 - * Refresh the index and write it to disk.
489 - *
490 - * 'refresh_flags' is passed directly to 'refresh_index()', while
491 - * 'COMMIT_LOCK | write_flags' is passed to 'write_locked_index()', so
492 - * the lockfile is always either committed or rolled back.
493 - *
494 - * If 'gentle' is passed, errors locking the index are ignored.
495 - *
496 - * Return 1 if refreshing the index returns an error, -1 if writing
497 - * the index to disk fails, 0 on success.
498 - *
499 - * Note that if refreshing the index returns an error, we still write
500 - * out the index (unless locking fails).
501 - */
502 -int repo_refresh_and_write_index(struct repository*, unsigned int refresh_flags, unsigned int write_flags, int gentle, const struct pathspec *, char *seen, const char *header_msg);
503 -
504 -struct cache_entry *refresh_cache_entry(struct index_state *, struct cache_entry *, unsigned int);
505 -
506 -void set_alternate_index_output(const char *);
507 -
508 -extern int verify_index_checksum;
509 -extern int verify_ce_order;
510 -
511 -int cmp_cache_name_compare(const void *a_, const void *b_);
512 -
513 -/* add */
514 -/*
515 - * return 0 if success, 1 - if addition of a file failed and
516 - * ADD_FILES_IGNORE_ERRORS was specified in flags
517 - */
518 -int add_files_to_cache(struct repository *repo, const char *prefix,
519 - const struct pathspec *pathspec, int include_sparse,
520 - int flags);
521 -
522 -/* ls-files */
523 -void overlay_tree_on_index(struct index_state *istate,
524 - const char *tree_name, const char *prefix);
5
6 #endif /* CACHE_H */
compat/precompose_utf8.c
+1
@@ -10,6 +10,7 @@
10 #include "environment.h"
11 #include "gettext.h"
12 #include "path.h"
13 +#include "strbuf.h"
14 #include "utf8.h"
15 #include "precompose_utf8.h"
16
compat/sha1-chunked.c
+1
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "hash-ll.h"
3
4 int git_SHA1_Update_Chunked(platform_SHA_CTX *c, const void *data, size_t len)
5 {
config.c
+1
@@ -33,6 +33,7 @@
33 #include "replace-object.h"
34 #include "refs.h"
35 #include "setup.h"
36 +#include "strvec.h"
37 #include "trace2.h"
38 #include "worktree.h"
39 #include "ws.h"
convert.c
+1
@@ -9,6 +9,7 @@
9 #include "attr.h"
10 #include "run-command.h"
11 #include "quote.h"
12 +#include "read-cache-ll.h"
13 #include "sigchain.h"
14 #include "pkt-line.h"
15 #include "sub-process.h"
diff-lib.c
+1
@@ -9,6 +9,7 @@
9 #include "gettext.h"
10 #include "hex.h"
11 #include "object-name.h"
12 +#include "read-cache.h"
13 #include "revision.h"
14 #include "cache-tree.h"
15 #include "unpack-trees.h"
diff.c
+1
@@ -39,6 +39,7 @@
39 #include "dir.h"
40 #include "object-file.h"
41 #include "object-name.h"
42 +#include "read-cache-ll.h"
43 #include "setup.h"
44 #include "strmap.h"
45 #include "ws.h"
diffcore-break.c
+2 -1
@@ -1,9 +1,10 @@
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 -#include "cache.h"
4 +#include "git-compat-util.h"
5 #include "diff.h"
6 #include "diffcore.h"
7 +#include "object.h"
8 #include "promisor-remote.h"
9
10 static int should_break(struct repository *r,
environment.h
+1 -2
@@ -1,9 +1,8 @@
1 #ifndef ENVIRONMENT_H
2 #define ENVIRONMENT_H
3
4 -#include "strvec.h"
5 -
4 struct repository;
5 +struct strvec;
6
7 /*
8 * The character that begins a commented line in user-editable file
fsmonitor.h
+2
@@ -4,6 +4,8 @@
4 #include "cache.h"
5 #include "dir.h"
6 #include "fsmonitor-settings.h"
7 +#include "object.h"
8 +#include "read-cache-ll.h"
9 #include "trace.h"
10
11 extern struct trace_key trace_fsmonitor;
git.c
+1
@@ -5,6 +5,7 @@
5 #include "gettext.h"
6 #include "help.h"
7 #include "pager.h"
8 +#include "read-cache-ll.h"
9 #include "run-command.h"
10 #include "alias.h"
11 #include "replace-object.h"
hash-lookup.c
+1
@@ -1,6 +1,7 @@
1 #include "cache.h"
2 #include "hash.h"
3 #include "hash-lookup.h"
4 +#include "read-cache-ll.h"
5
6 static uint32_t take2(const struct object_id *oid, size_t ofs)
7 {
merge-ort-wrappers.c
+1
@@ -3,6 +3,7 @@
3 #include "hash.h"
4 #include "merge-ort.h"
5 #include "merge-ort-wrappers.h"
6 +#include "read-cache-ll.h"
7 #include "tree.h"
8
9 #include "commit.h"
merge-ort.c
+1
@@ -37,6 +37,7 @@
37 #include "object-store.h"
38 #include "oid-array.h"
39 #include "promisor-remote.h"
40 +#include "read-cache-ll.h"
41 #include "revision.h"
42 #include "sparse-index.h"
43 #include "strmap.h"
name-hash.c
+2
@@ -9,6 +9,8 @@
9 #include "environment.h"
10 #include "gettext.h"
11 #include "name-hash.h"
12 +#include "object.h"
13 +#include "read-cache-ll.h"
14 #include "thread-utils.h"
15 #include "trace.h"
16 #include "trace2.h"
object-name.c
+1
@@ -18,6 +18,7 @@
18 #include "packfile.h"
19 #include "pretty.h"
20 #include "object-store.h"
21 +#include "read-cache-ll.h"
22 #include "repository.h"
23 #include "setup.h"
24 #include "submodule.h"
oidmap.h
+1
@@ -1,6 +1,7 @@
1 #ifndef OIDMAP_H
2 #define OIDMAP_H
3
4 +#include "hash-ll.h"
5 #include "hashmap.h"
6
7 /*
parallel-checkout.c
+1
@@ -8,6 +8,7 @@
8 #include "parallel-checkout.h"
9 #include "pkt-line.h"
10 #include "progress.h"
11 +#include "read-cache-ll.h"
12 #include "run-command.h"
13 #include "sigchain.h"
14 #include "streaming.h"
pathspec.c
+1
@@ -6,6 +6,7 @@
6 #include "gettext.h"
7 #include "pathspec.h"
8 #include "attr.h"
9 +#include "read-cache.h"
10 #include "repository.h"
11 #include "setup.h"
12 #include "strvec.h"
preload-index.c
+1
@@ -10,6 +10,7 @@
10 #include "config.h"
11 #include "preload-index.h"
12 #include "progress.h"
13 +#include "read-cache.h"
14 #include "thread-utils.h"
15 #include "repository.h"
16 #include "symlinks.h"
read-cache-ll.h new
+481
@@ -0,0 +1,481 @@
1 +#ifndef READ_CACHE_LL_H
2 +#define READ_CACHE_LL_H
3 +
4 +#include "hash-ll.h"
5 +#include "hashmap.h"
6 +#include "statinfo.h"
7 +
8 +/*
9 + * Basic data structures for the directory cache
10 + */
11 +
12 +#define CACHE_SIGNATURE 0x44495243 /* "DIRC" */
13 +struct cache_header {
14 + uint32_t hdr_signature;
15 + uint32_t hdr_version;
16 + uint32_t hdr_entries;
17 +};
18 +
19 +#define INDEX_FORMAT_LB 2
20 +#define INDEX_FORMAT_UB 4
21 +
22 +struct cache_entry {
23 + struct hashmap_entry ent;
24 + struct stat_data ce_stat_data;
25 + unsigned int ce_mode;
26 + unsigned int ce_flags;
27 + unsigned int mem_pool_allocated;
28 + unsigned int ce_namelen;
29 + unsigned int index; /* for link extension */
30 + struct object_id oid;
31 + char name[FLEX_ARRAY]; /* more */
32 +};
33 +
34 +#define CE_STAGEMASK (0x3000)
35 +#define CE_EXTENDED (0x4000)
36 +#define CE_VALID (0x8000)
37 +#define CE_STAGESHIFT 12
38 +
39 +/*
40 + * Range 0xFFFF0FFF in ce_flags is divided into
41 + * two parts: in-memory flags and on-disk ones.
42 + * Flags in CE_EXTENDED_FLAGS will get saved on-disk
43 + * if you want to save a new flag, add it in
44 + * CE_EXTENDED_FLAGS
45 + *
46 + * In-memory only flags
47 + */
48 +#define CE_UPDATE (1 << 16)
49 +#define CE_REMOVE (1 << 17)
50 +#define CE_UPTODATE (1 << 18)
51 +#define CE_ADDED (1 << 19)
52 +
53 +#define CE_HASHED (1 << 20)
54 +#define CE_FSMONITOR_VALID (1 << 21)
55 +#define CE_WT_REMOVE (1 << 22) /* remove in work directory */
56 +#define CE_CONFLICTED (1 << 23)
57 +
58 +#define CE_UNPACKED (1 << 24)
59 +#define CE_NEW_SKIP_WORKTREE (1 << 25)
60 +
61 +/* used to temporarily mark paths matched by pathspecs */
62 +#define CE_MATCHED (1 << 26)
63 +
64 +#define CE_UPDATE_IN_BASE (1 << 27)
65 +#define CE_STRIP_NAME (1 << 28)
66 +
67 +/*
68 + * Extended on-disk flags
69 + */
70 +#define CE_INTENT_TO_ADD (1 << 29)
71 +#define CE_SKIP_WORKTREE (1 << 30)
72 +/* CE_EXTENDED2 is for future extension */
73 +#define CE_EXTENDED2 (1U << 31)
74 +
75 +#define CE_EXTENDED_FLAGS (CE_INTENT_TO_ADD | CE_SKIP_WORKTREE)
76 +
77 +/*
78 + * Safeguard to avoid saving wrong flags:
79 + * - CE_EXTENDED2 won't get saved until its semantic is known
80 + * - Bits in 0x0000FFFF have been saved in ce_flags already
81 + * - Bits in 0x003F0000 are currently in-memory flags
82 + */
83 +#if CE_EXTENDED_FLAGS & 0x803FFFFF
84 +#error "CE_EXTENDED_FLAGS out of range"
85 +#endif
86 +
87 +/* Forward structure decls */
88 +struct pathspec;
89 +struct tree;
90 +
91 +/*
92 + * Copy the sha1 and stat state of a cache entry from one to
93 + * another. But we never change the name, or the hash state!
94 + */
95 +static inline void copy_cache_entry(struct cache_entry *dst,
96 + const struct cache_entry *src)
97 +{
98 + unsigned int state = dst->ce_flags & CE_HASHED;
99 + int mem_pool_allocated = dst->mem_pool_allocated;
100 +
101 + /* Don't copy hash chain and name */
102 + memcpy(&dst->ce_stat_data, &src->ce_stat_data,
103 + offsetof(struct cache_entry, name) -
104 + offsetof(struct cache_entry, ce_stat_data));
105 +
106 + /* Restore the hash state */
107 + dst->ce_flags = (dst->ce_flags & ~CE_HASHED) | state;
108 +
109 + /* Restore the mem_pool_allocated flag */
110 + dst->mem_pool_allocated = mem_pool_allocated;
111 +}
112 +
113 +static inline unsigned create_ce_flags(unsigned stage)
114 +{
115 + return (stage << CE_STAGESHIFT);
116 +}
117 +
118 +#define ce_namelen(ce) ((ce)->ce_namelen)
119 +#define ce_size(ce) cache_entry_size(ce_namelen(ce))
120 +#define ce_stage(ce) ((CE_STAGEMASK & (ce)->ce_flags) >> CE_STAGESHIFT)
121 +#define ce_uptodate(ce) ((ce)->ce_flags & CE_UPTODATE)
122 +#define ce_skip_worktree(ce) ((ce)->ce_flags & CE_SKIP_WORKTREE)
123 +#define ce_mark_uptodate(ce) ((ce)->ce_flags |= CE_UPTODATE)
124 +#define ce_intent_to_add(ce) ((ce)->ce_flags & CE_INTENT_TO_ADD)
125 +
126 +#define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)
127 +
128 +#define SOMETHING_CHANGED (1 << 0) /* unclassified changes go here */
129 +#define CE_ENTRY_CHANGED (1 << 1)
130 +#define CE_ENTRY_REMOVED (1 << 2)
131 +#define CE_ENTRY_ADDED (1 << 3)
132 +#define RESOLVE_UNDO_CHANGED (1 << 4)
133 +#define CACHE_TREE_CHANGED (1 << 5)
134 +#define SPLIT_INDEX_ORDERED (1 << 6)
135 +#define UNTRACKED_CHANGED (1 << 7)
136 +#define FSMONITOR_CHANGED (1 << 8)
137 +
138 +struct split_index;
139 +struct untracked_cache;
140 +struct progress;
141 +struct pattern_list;
142 +
143 +enum sparse_index_mode {
144 + /*
145 + * There are no sparse directories in the index at all.
146 + *
147 + * Repositories that don't use cone-mode sparse-checkout will
148 + * always have their indexes in this mode.
149 + */
150 + INDEX_EXPANDED = 0,
151 +
152 + /*
153 + * The index has already been collapsed to sparse directories
154 + * whereever possible.
155 + */
156 + INDEX_COLLAPSED,
157 +
158 + /*
159 + * The sparse directories that exist are outside the
160 + * sparse-checkout boundary, but it is possible that some file
161 + * entries could collapse to sparse directory entries.
162 + */
163 + INDEX_PARTIALLY_SPARSE,
164 +};
165 +
166 +struct index_state {
167 + struct cache_entry **cache;
168 + unsigned int version;
169 + unsigned int cache_nr, cache_alloc, cache_changed;
170 + struct string_list *resolve_undo;
171 + struct cache_tree *cache_tree;
172 + struct split_index *split_index;
173 + struct cache_time timestamp;
174 + unsigned name_hash_initialized : 1,
175 + initialized : 1,
176 + drop_cache_tree : 1,
177 + updated_workdir : 1,
178 + updated_skipworktree : 1,
179 + fsmonitor_has_run_once : 1;
180 + enum sparse_index_mode sparse_index;
181 + struct hashmap name_hash;
182 + struct hashmap dir_hash;
183 + struct object_id oid;
184 + struct untracked_cache *untracked;
185 + char *fsmonitor_last_update;
186 + struct ewah_bitmap *fsmonitor_dirty;
187 + struct mem_pool *ce_mem_pool;
188 + struct progress *progress;
189 + struct repository *repo;
190 + struct pattern_list *sparse_checkout_patterns;
191 +};
192 +
193 +/**
194 + * A "struct index_state istate" must be initialized with
195 + * INDEX_STATE_INIT or the corresponding index_state_init().
196 + *
197 + * If the variable won't be used again, use release_index() to free()
198 + * its resources. If it needs to be used again use discard_index(),
199 + * which does the same thing, but will use use index_state_init() at
200 + * the end. The discard_index() will use its own "istate->repo" as the
201 + * "r" argument to index_state_init() in that case.
202 + */
203 +#define INDEX_STATE_INIT(r) { \
204 + .repo = (r), \
205 +}
206 +void index_state_init(struct index_state *istate, struct repository *r);
207 +void release_index(struct index_state *istate);
208 +
209 +/* Cache entry creation and cleanup */
210 +
211 +/*
212 + * Create cache_entry intended for use in the specified index. Caller
213 + * is responsible for discarding the cache_entry with
214 + * `discard_cache_entry`.
215 + */
216 +struct cache_entry *make_cache_entry(struct index_state *istate,
217 + unsigned int mode,
218 + const struct object_id *oid,
219 + const char *path,
220 + int stage,
221 + unsigned int refresh_options);
222 +
223 +struct cache_entry *make_empty_cache_entry(struct index_state *istate,
224 + size_t name_len);
225 +
226 +/*
227 + * Create a cache_entry that is not intended to be added to an index. If
228 + * `ce_mem_pool` is not NULL, the entry is allocated within the given memory
229 + * pool. Caller is responsible for discarding "loose" entries with
230 + * `discard_cache_entry()` and the memory pool with
231 + * `mem_pool_discard(ce_mem_pool, should_validate_cache_entries())`.
232 + */
233 +struct cache_entry *make_transient_cache_entry(unsigned int mode,
234 + const struct object_id *oid,
235 + const char *path,
236 + int stage,
237 + struct mem_pool *ce_mem_pool);
238 +
239 +struct cache_entry *make_empty_transient_cache_entry(size_t len,
240 + struct mem_pool *ce_mem_pool);
241 +
242 +/*
243 + * Discard cache entry.
244 + */
245 +void discard_cache_entry(struct cache_entry *ce);
246 +
247 +/*
248 + * Check configuration if we should perform extra validation on cache
249 + * entries.
250 + */
251 +int should_validate_cache_entries(void);
252 +
253 +/*
254 + * Duplicate a cache_entry. Allocate memory for the new entry from a
255 + * memory_pool. Takes into account cache_entry fields that are meant
256 + * for managing the underlying memory allocation of the cache_entry.
257 + */
258 +struct cache_entry *dup_cache_entry(const struct cache_entry *ce, struct index_state *istate);
259 +
260 +/*
261 + * Validate the cache entries in the index. This is an internal
262 + * consistency check that the cache_entry structs are allocated from
263 + * the expected memory pool.
264 + */
265 +void validate_cache_entries(const struct index_state *istate);
266 +
267 +/*
268 + * Bulk prefetch all missing cache entries that are not GITLINKs and that match
269 + * the given predicate. This function should only be called if
270 + * repo_has_promisor_remote() returns true.
271 + */
272 +typedef int (*must_prefetch_predicate)(const struct cache_entry *);
273 +void prefetch_cache_entries(const struct index_state *istate,
274 + must_prefetch_predicate must_prefetch);
275 +
276 +/* Initialize and use the cache information */
277 +struct lock_file;
278 +int do_read_index(struct index_state *istate, const char *path,
279 + int must_exist); /* for testting only! */
280 +int read_index_from(struct index_state *, const char *path,
281 + const char *gitdir);
282 +int is_index_unborn(struct index_state *);
283 +
284 +/* For use with `write_locked_index()`. */
285 +#define COMMIT_LOCK (1 << 0)
286 +#define SKIP_IF_UNCHANGED (1 << 1)
287 +
288 +/*
289 + * Write the index while holding an already-taken lock. Close the lock,
290 + * and if `COMMIT_LOCK` is given, commit it.
291 + *
292 + * Unless a split index is in use, write the index into the lockfile.
293 + *
294 + * With a split index, write the shared index to a temporary file,
295 + * adjust its permissions and rename it into place, then write the
296 + * split index to the lockfile. If the temporary file for the shared
297 + * index cannot be created, fall back to the behavior described in
298 + * the previous paragraph.
299 + *
300 + * With `COMMIT_LOCK`, the lock is always committed or rolled back.
301 + * Without it, the lock is closed, but neither committed nor rolled
302 + * back.
303 + *
304 + * If `SKIP_IF_UNCHANGED` is given and the index is unchanged, nothing
305 + * is written (and the lock is rolled back if `COMMIT_LOCK` is given).
306 + */
307 +int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
308 +
309 +void discard_index(struct index_state *);
310 +void move_index_extensions(struct index_state *dst, struct index_state *src);
311 +int unmerged_index(const struct index_state *);
312 +
313 +/**
314 + * Returns 1 if istate differs from tree, 0 otherwise. If tree is NULL,
315 + * compares istate to HEAD. If tree is NULL and on an unborn branch,
316 + * returns 1 if there are entries in istate, 0 otherwise. If an strbuf is
317 + * provided, the space-separated list of files that differ will be appended
318 + * to it.
319 + */
320 +int repo_index_has_changes(struct repository *repo,
321 + struct tree *tree,
322 + struct strbuf *sb);
323 +
324 +int verify_path(const char *path, unsigned mode);
325 +int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
326 +
327 +/*
328 + * Searches for an entry defined by name and namelen in the given index.
329 + * If the return value is positive (including 0) it is the position of an
330 + * exact match. If the return value is negative, the negated value minus 1
331 + * is the position where the entry would be inserted.
332 + * Example: The current index consists of these files and its stages:
333 + *
334 + * b#0, d#0, f#1, f#3
335 + *
336 + * index_name_pos(&index, "a", 1) -> -1
337 + * index_name_pos(&index, "b", 1) -> 0
338 + * index_name_pos(&index, "c", 1) -> -2
339 + * index_name_pos(&index, "d", 1) -> 1
340 + * index_name_pos(&index, "e", 1) -> -3
341 + * index_name_pos(&index, "f", 1) -> -3
342 + * index_name_pos(&index, "g", 1) -> -5
343 + */
344 +int index_name_pos(struct index_state *, const char *name, int namelen);
345 +
346 +/*
347 + * Like index_name_pos, returns the position of an entry of the given name in
348 + * the index if one exists, otherwise returns a negative value where the negated
349 + * value minus 1 is the position where the index entry would be inserted. Unlike
350 + * index_name_pos, however, a sparse index is not expanded to find an entry
351 + * inside a sparse directory.
352 + */
353 +int index_name_pos_sparse(struct index_state *, const char *name, int namelen);
354 +
355 +/*
356 + * Determines whether an entry with the given name exists within the
357 + * given index. The return value is 1 if an exact match is found, otherwise
358 + * it is 0. Note that, unlike index_name_pos, this function does not expand
359 + * the index if it is sparse. If an item exists within the full index but it
360 + * is contained within a sparse directory (and not in the sparse index), 0 is
361 + * returned.
362 + */
363 +int index_entry_exists(struct index_state *, const char *name, int namelen);
364 +
365 +/*
366 + * Some functions return the negative complement of an insert position when a
367 + * precise match was not found but a position was found where the entry would
368 + * need to be inserted. This helper protects that logic from any integer
369 + * underflow.
370 + */
371 +static inline int index_pos_to_insert_pos(uintmax_t pos)
372 +{
373 + if (pos > INT_MAX)
374 + die("overflow: -1 - %"PRIuMAX, pos);
375 + return -1 - (int)pos;
376 +}
377 +
378 +#define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
379 +#define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
380 +#define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
381 +#define ADD_CACHE_JUST_APPEND 8 /* Append only */
382 +#define ADD_CACHE_NEW_ONLY 16 /* Do not replace existing ones */
383 +#define ADD_CACHE_KEEP_CACHE_TREE 32 /* Do not invalidate cache-tree */
384 +#define ADD_CACHE_RENORMALIZE 64 /* Pass along HASH_RENORMALIZE */
385 +int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
386 +void rename_index_entry_at(struct index_state *, int pos, const char *new_name);
387 +
388 +/* Remove entry, return true if there are more entries to go. */
389 +int remove_index_entry_at(struct index_state *, int pos);
390 +
391 +void remove_marked_cache_entries(struct index_state *istate, int invalidate);
392 +int remove_file_from_index(struct index_state *, const char *path);
393 +#define ADD_CACHE_VERBOSE 1
394 +#define ADD_CACHE_PRETEND 2
395 +#define ADD_CACHE_IGNORE_ERRORS 4
396 +#define ADD_CACHE_IGNORE_REMOVAL 8
397 +#define ADD_CACHE_INTENT 16
398 +/*
399 + * These two are used to add the contents of the file at path
400 + * to the index, marking the working tree up-to-date by storing
401 + * the cached stat info in the resulting cache entry. A caller
402 + * that has already run lstat(2) on the path can call
403 + * add_to_index(), and all others can call add_file_to_index();
404 + * the latter will do necessary lstat(2) internally before
405 + * calling the former.
406 + */
407 +int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
408 +int add_file_to_index(struct index_state *, const char *path, int flags);
409 +
410 +int chmod_index_entry(struct index_state *, struct cache_entry *ce, char flip);
411 +int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
412 +void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
413 +int index_name_is_other(struct index_state *, const char *, int);
414 +void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);
415 +
416 +/* do stat comparison even if CE_VALID is true */
417 +#define CE_MATCH_IGNORE_VALID 01
418 +/* do not check the contents but report dirty on racily-clean entries */
419 +#define CE_MATCH_RACY_IS_DIRTY 02
420 +/* do stat comparison even if CE_SKIP_WORKTREE is true */
421 +#define CE_MATCH_IGNORE_SKIP_WORKTREE 04
422 +/* ignore non-existent files during stat update */
423 +#define CE_MATCH_IGNORE_MISSING 0x08
424 +/* enable stat refresh */
425 +#define CE_MATCH_REFRESH 0x10
426 +/* don't refresh_fsmonitor state or do stat comparison even if CE_FSMONITOR_VALID is true */
427 +#define CE_MATCH_IGNORE_FSMONITOR 0X20
428 +int is_racy_timestamp(const struct index_state *istate,
429 + const struct cache_entry *ce);
430 +int has_racy_timestamp(struct index_state *istate);
431 +int ie_match_stat(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
432 +int ie_modified(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
433 +
434 +int match_stat_data_racy(const struct index_state *istate,
435 + const struct stat_data *sd, struct stat *st);
436 +
437 +void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st);
438 +
439 +#define REFRESH_REALLY (1 << 0) /* ignore_valid */
440 +#define REFRESH_UNMERGED (1 << 1) /* allow unmerged */
441 +#define REFRESH_QUIET (1 << 2) /* be quiet about it */
442 +#define REFRESH_IGNORE_MISSING (1 << 3) /* ignore non-existent */
443 +#define REFRESH_IGNORE_SUBMODULES (1 << 4) /* ignore submodules */
444 +#define REFRESH_IN_PORCELAIN (1 << 5) /* user friendly output, not "needs update" */
445 +#define REFRESH_PROGRESS (1 << 6) /* show progress bar if stderr is tty */
446 +#define REFRESH_IGNORE_SKIP_WORKTREE (1 << 7) /* ignore skip_worktree entries */
447 +int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
448 +/*
449 + * Refresh the index and write it to disk.
450 + *
451 + * 'refresh_flags' is passed directly to 'refresh_index()', while
452 + * 'COMMIT_LOCK | write_flags' is passed to 'write_locked_index()', so
453 + * the lockfile is always either committed or rolled back.
454 + *
455 + * If 'gentle' is passed, errors locking the index are ignored.
456 + *
457 + * Return 1 if refreshing the index returns an error, -1 if writing
458 + * the index to disk fails, 0 on success.
459 + *
460 + * Note that if refreshing the index returns an error, we still write
461 + * out the index (unless locking fails).
462 + */
463 +int repo_refresh_and_write_index(struct repository*, unsigned int refresh_flags, unsigned int write_flags, int gentle, const struct pathspec *, char *seen, const char *header_msg);
464 +
465 +struct cache_entry *refresh_cache_entry(struct index_state *, struct cache_entry *, unsigned int);
466 +
467 +void set_alternate_index_output(const char *);
468 +
469 +extern int verify_index_checksum;
470 +extern int verify_ce_order;
471 +
472 +int cmp_cache_name_compare(const void *a_, const void *b_);
473 +
474 +int add_files_to_cache(struct repository *repo, const char *prefix,
475 + const struct pathspec *pathspec, int include_sparse,
476 + int flags);
477 +
478 +void overlay_tree_on_index(struct index_state *istate,
479 + const char *tree_name, const char *prefix);
480 +
481 +#endif /* READ_CACHE_LL_H */
read-cache.c
+1
@@ -28,6 +28,7 @@
28 #include "name-hash.h"
29 #include "object-name.h"
30 #include "preload-index.h"
31 +#include "read-cache.h"
32 #include "resolve-undo.h"
33 #include "revision.h"
34 #include "run-command.h"
read-cache.h new
+45
@@ -0,0 +1,45 @@
1 +#ifndef READ_CACHE_H
2 +#define READ_CACHE_H
3 +
4 +#include "read-cache-ll.h"
5 +#include "object.h"
6 +#include "pathspec.h"
7 +
8 +static inline unsigned int ce_mode_from_stat(const struct cache_entry *ce,
9 + unsigned int mode)
10 +{
11 + extern int trust_executable_bit, has_symlinks;
12 + if (!has_symlinks && S_ISREG(mode) &&
13 + ce && S_ISLNK(ce->ce_mode))
14 + return ce->ce_mode;
15 + if (!trust_executable_bit && S_ISREG(mode)) {
16 + if (ce && S_ISREG(ce->ce_mode))
17 + return ce->ce_mode;
18 + return create_ce_mode(0666);
19 + }
20 + return create_ce_mode(mode);
21 +}
22 +
23 +static inline int ce_to_dtype(const struct cache_entry *ce)
24 +{
25 + unsigned ce_mode = ntohl(ce->ce_mode);
26 + if (S_ISREG(ce_mode))
27 + return DT_REG;
28 + else if (S_ISDIR(ce_mode) || S_ISGITLINK(ce_mode))
29 + return DT_DIR;
30 + else if (S_ISLNK(ce_mode))
31 + return DT_LNK;
32 + else
33 + return DT_UNKNOWN;
34 +}
35 +
36 +static inline int ce_path_match(struct index_state *istate,
37 + const struct cache_entry *ce,
38 + const struct pathspec *pathspec,
39 + char *seen)
40 +{
41 + return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
42 + S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
43 +}
44 +
45 +#endif /* READ_CACHE_H */
repository.c
+1
@@ -10,6 +10,7 @@
10 #include "config.h"
11 #include "object.h"
12 #include "lockfile.h"
13 +#include "read-cache-ll.h"
14 #include "remote.h"
15 #include "setup.h"
16 #include "submodule-config.h"
rerere.c
+1
@@ -7,6 +7,7 @@
7 #include "hex.h"
8 #include "lockfile.h"
9 #include "string-list.h"
10 +#include "read-cache-ll.h"
11 #include "rerere.h"
12 #include "xdiff-interface.h"
13 #include "dir.h"
resolve-undo.c
+1
@@ -1,6 +1,7 @@
1 #include "cache.h"
2 #include "dir.h"
3 #include "hash.h"
4 +#include "read-cache.h"
5 #include "resolve-undo.h"
6 #include "sparse-index.h"
7 #include "string-list.h"
revision.c
+1
@@ -31,6 +31,7 @@
31 #include "bisect.h"
32 #include "packfile.h"
33 #include "worktree.h"
34 +#include "read-cache.h"
35 #include "setup.h"
36 #include "sparse-index.h"
37 #include "strvec.h"
split-index.c
+1
@@ -3,6 +3,7 @@
3 #include "gettext.h"
4 #include "hash.h"
5 #include "mem-pool.h"
6 +#include "read-cache-ll.h"
7 #include "split-index.h"
8 #include "strbuf.h"
9 #include "ewah/ewok.h"
submodule.c
+1
@@ -28,6 +28,7 @@
28 #include "object-name.h"
29 #include "object-store.h"
30 #include "commit-reach.h"
31 +#include "read-cache-ll.h"
32 #include "setup.h"
33 #include "shallow.h"
34 #include "trace2.h"
t/helper/test-cache-tree.c
+1
@@ -6,6 +6,7 @@
6 #include "tree.h"
7 #include "cache-tree.h"
8 #include "parse-options.h"
9 +#include "read-cache-ll.h"
10 #include "repository.h"
11 #include "setup.h"
12
t/helper/test-dump-cache-tree.c
+1
@@ -5,6 +5,7 @@
5 #include "hex.h"
6 #include "tree.h"
7 #include "cache-tree.h"
8 +#include "read-cache-ll.h"
9 #include "repository.h"
10 #include "setup.h"
11
t/helper/test-dump-fsmonitor.c
+1
@@ -1,5 +1,6 @@
1 #include "test-tool.h"
2 #include "cache.h"
3 +#include "read-cache-ll.h"
4 #include "repository.h"
5 #include "setup.h"
6
t/helper/test-dump-split-index.c
+1
@@ -2,6 +2,7 @@
2 #include "test-tool.h"
3 #include "cache.h"
4 #include "hex.h"
5 +#include "read-cache-ll.h"
6 #include "repository.h"
7 #include "setup.h"
8 #include "split-index.h"
t/helper/test-dump-untracked-cache.c
+1
@@ -3,6 +3,7 @@
3 #include "cache.h"
4 #include "dir.h"
5 #include "hex.h"
6 +#include "read-cache-ll.h"
7 #include "repository.h"
8 #include "setup.h"
9
t/helper/test-fast-rebase.c
+1
@@ -21,6 +21,7 @@
21 #include "lockfile.h"
22 #include "merge-ort.h"
23 #include "object-name.h"
24 +#include "read-cache-ll.h"
25 #include "refs.h"
26 #include "revision.h"
27 #include "sequencer.h"
t/helper/test-fsmonitor-client.c
+1
@@ -7,6 +7,7 @@
7 #include "cache.h"
8 #include "parse-options.h"
9 #include "fsmonitor-ipc.h"
10 +#include "read-cache-ll.h"
11 #include "repository.h"
12 #include "setup.h"
13 #include "thread-utils.h"
t/helper/test-hash-speed.c
+1
@@ -1,5 +1,6 @@
1 #include "test-tool.h"
2 #include "cache.h"
3 +#include "hash-ll.h"
4
5 #define NUM_SECONDS 3
6
t/helper/test-index-version.c
+1
@@ -1,5 +1,6 @@
1 #include "test-tool.h"
2 #include "cache.h"
3 +#include "read-cache-ll.h"
4
5 int cmd__index_version(int argc UNUSED, const char **argv UNUSED)
6 {
t/helper/test-lazy-init-name-hash.c
+1
@@ -4,6 +4,7 @@
4 #include "environment.h"
5 #include "name-hash.h"
6 #include "parse-options.h"
7 +#include "read-cache-ll.h"
8 #include "repository.h"
9 #include "setup.h"
10 #include "trace.h"
t/helper/test-path-utils.c
+1
@@ -3,6 +3,7 @@
3 #include "abspath.h"
4 #include "environment.h"
5 #include "path.h"
6 +#include "read-cache-ll.h"
7 #include "setup.h"
8 #include "string-list.h"
9 #include "trace.h"
t/helper/test-read-cache.c
+1
@@ -2,6 +2,7 @@
2 #include "test-tool.h"
3 #include "cache.h"
4 #include "config.h"
5 +#include "read-cache-ll.h"
6 #include "repository.h"
7 #include "setup.h"
8 #include "wrapper.h"
t/helper/test-scrap-cache-tree.c
+1
@@ -2,6 +2,7 @@
2 #include "test-tool.h"
3 #include "cache.h"
4 #include "lockfile.h"
5 +#include "read-cache-ll.h"
6 #include "repository.h"
7 #include "setup.h"
8 #include "tree.h"
t/helper/test-sha1.c
+1
@@ -1,5 +1,6 @@
1 #include "test-tool.h"
2 #include "cache.h"
3 +#include "hash-ll.h"
4
5 int cmd__sha1(int ac, const char **av)
6 {
t/helper/test-sha256.c
+1
@@ -1,5 +1,6 @@
1 #include "test-tool.h"
2 #include "cache.h"
3 +#include "hash-ll.h"
4
5 int cmd__sha256(int ac, const char **av)
6 {
t/helper/test-strcmp-offset.c
+1
@@ -1,5 +1,6 @@
1 #include "test-tool.h"
2 #include "cache.h"
3 +#include "read-cache-ll.h"
4
5 int cmd__strcmp_offset(int argc UNUSED, const char **argv)
6 {
t/helper/test-write-cache.c
+1
@@ -2,6 +2,7 @@
2 #include "test-tool.h"
3 #include "cache.h"
4 #include "lockfile.h"
5 +#include "read-cache-ll.h"
6 #include "repository.h"
7 #include "setup.h"
8
unpack-trees.c
+1
@@ -15,6 +15,7 @@
15 #include "progress.h"
16 #include "refs.h"
17 #include "attr.h"
18 +#include "read-cache.h"
19 #include "split-index.h"
20 #include "sparse-index.h"
21 #include "submodule.h"
unpack-trees.h
+1 -1
@@ -1,8 +1,8 @@
1 #ifndef UNPACK_TREES_H
2 #define UNPACK_TREES_H
3
4 -#include "cache.h"
4 #include "convert.h"
5 +#include "read-cache-ll.h"
6 #include "strvec.h"
7 #include "string-list.h"
8 #include "tree-walk.h"
wt-status.c
+1
@@ -18,6 +18,7 @@
18 #include "refs.h"
19 #include "submodule.h"
20 #include "column.h"
21 +#include "read-cache.h"
22 #include "setup.h"
23 #include "strbuf.h"
24 #include "trace.h"