]> git.ipfire.org Git - thirdparty/git.git/blame - cache.h
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / cache.h
CommitLineData
e83c5163
LT
1#ifndef CACHE_H
2#define CACHE_H
3
4050c0df 4#include "git-compat-util.h"
5ecd293d 5#include "strbuf.h"
e05881a4 6#include "hashmap.h"
ec2dd32c 7#include "list.h"
65784830 8#include "gettext.h"
155ef25f 9#include "string-list.h"
f18f816c 10#include "hash.h"
e7d72d07 11#include "path.h"
ac48adf4 12#include "pathspec.h"
a64215b6 13#include "object.h"
78a67668 14#include "repository.h"
ac48adf4 15#include "statinfo.h"
e83c5163 16
ef49a7a0
JH
17typedef struct git_zstream {
18 z_stream z;
19 unsigned long avail_in;
20 unsigned long avail_out;
21 unsigned long total_in;
22 unsigned long total_out;
23 unsigned char *next_in;
24 unsigned char *next_out;
25} git_zstream;
26
27void git_inflate_init(git_zstream *);
28void git_inflate_init_gzip_only(git_zstream *);
29void git_inflate_end(git_zstream *);
30int git_inflate(git_zstream *, int flush);
31
32void git_deflate_init(git_zstream *, int level);
33void git_deflate_init_gzip(git_zstream *, int level);
c3c2e1a0 34void git_deflate_init_raw(git_zstream *, int level);
ef49a7a0 35void git_deflate_end(git_zstream *);
568508e7 36int git_deflate_abort(git_zstream *);
ef49a7a0
JH
37int git_deflate_end_gently(git_zstream *);
38int git_deflate(git_zstream *, int flush);
39unsigned long git_deflate_bound(git_zstream *, unsigned long);
39c68542 40
962554c6 41#if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
b6829693
ET
42#define DTYPE(de) ((de)->d_type)
43#else
0bdd79af
JH
44#undef DT_UNKNOWN
45#undef DT_DIR
46#undef DT_REG
47#undef DT_LNK
b6829693
ET
48#define DT_UNKNOWN 0
49#define DT_DIR 1
50#define DT_REG 2
a15c1c60 51#define DT_LNK 3
b6829693
ET
52#define DTYPE(de) DT_UNKNOWN
53#endif
54
40689ae1
MK
55/* unknown mode (impossible combination S_IFIFO|S_IFCHR) */
56#define S_IFINVALID 0030000
57
9eec4795
LT
58/*
59 * A "directory link" is a link to another git directory.
60 *
61 * The value 0160000 is not normally a valid mode, and
62 * also just happens to be S_IFDIR + S_IFLNK
9eec4795 63 */
302b9282
MW
64#define S_IFGITLINK 0160000
65#define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK)
9eec4795 66
72441af7
KS
67/*
68 * Some mode bits are also used internally for computations.
69 *
70 * They *must* not overlap with any valid modes, and they *must* not be emitted
71 * to outside world - i.e. appear on disk or network. In other words, it's just
72 * temporary fields, which we internally use, but they have to stay in-house.
73 *
74 * ( such approach is valid, as standard S_IF* fits into 16 bits, and in Git
75 * codebase mode is `unsigned int` which is assumed to be at least 32 bits )
76 */
77
78/* used internally in tree-diff */
79#define S_DIFFTREE_IFXMIN_NEQ 0x80000000
80
81
2386d658
LT
82/*
83 * Intensive research over the course of many years has shown that
84 * port 9418 is totally unused by anything else. Or
85 *
86 * Your search - "port 9418" - did not match any documents.
87 *
88 * as www.google.com puts it.
ba8a4970
LT
89 *
90 * This port has been properly assigned for git use by IANA:
91 * git (Assigned-9418) [I06-050728-0001].
92 *
93 * git 9418/tcp git pack transfer service
94 * git 9418/udp git pack transfer service
95 *
96 * with Linus Torvalds <torvalds@osdl.org> as the point of
97 * contact. September 2005.
98 *
99 * See http://www.iana.org/assignments/port-numbers
2386d658
LT
100 */
101#define DEFAULT_GIT_PORT 9418
102
e83c5163
LT
103/*
104 * Basic data structures for the directory cache
e83c5163
LT
105 */
106
107#define CACHE_SIGNATURE 0x44495243 /* "DIRC" */
108struct cache_header {
7800c1eb
TG
109 uint32_t hdr_signature;
110 uint32_t hdr_version;
111 uint32_t hdr_entries;
e83c5163
LT
112};
113
9d227781
JH
114#define INDEX_FORMAT_LB 2
115#define INDEX_FORMAT_UB 4
116
e83c5163 117struct cache_entry {
8b013788 118 struct hashmap_entry ent;
c21d39d7 119 struct stat_data ce_stat_data;
ccc4feb5 120 unsigned int ce_mode;
7a51ed66 121 unsigned int ce_flags;
8e72d675 122 unsigned int mem_pool_allocated;
b60e188c 123 unsigned int ce_namelen;
5fc2fc8f 124 unsigned int index; /* for link extension */
99d1a986 125 struct object_id oid;
8f1d2e6f 126 char name[FLEX_ARRAY]; /* more */
e83c5163
LT
127};
128
95fd5bf8 129#define CE_STAGEMASK (0x3000)
16ce2e4c 130#define CE_EXTENDED (0x4000)
5f73076c 131#define CE_VALID (0x8000)
aee46198 132#define CE_STAGESHIFT 12
95fd5bf8 133
06aaaa0b 134/*
ce51bf09 135 * Range 0xFFFF0FFF in ce_flags is divided into
06aaaa0b
NTND
136 * two parts: in-memory flags and on-disk ones.
137 * Flags in CE_EXTENDED_FLAGS will get saved on-disk
138 * if you want to save a new flag, add it in
139 * CE_EXTENDED_FLAGS
140 *
141 * In-memory only flags
142 */
2977ffbb
NTND
143#define CE_UPDATE (1 << 16)
144#define CE_REMOVE (1 << 17)
145#define CE_UPTODATE (1 << 18)
146#define CE_ADDED (1 << 19)
a22c6371 147
2977ffbb 148#define CE_HASHED (1 << 20)
883e248b 149#define CE_FSMONITOR_VALID (1 << 21)
2977ffbb
NTND
150#define CE_WT_REMOVE (1 << 22) /* remove in work directory */
151#define CE_CONFLICTED (1 << 23)
7a51ed66 152
2977ffbb 153#define CE_UNPACKED (1 << 24)
2431afbf 154#define CE_NEW_SKIP_WORKTREE (1 << 25)
da165f47 155
e721c154
NTND
156/* used to temporarily mark paths matched by pathspecs */
157#define CE_MATCHED (1 << 26)
158
078a58e8 159#define CE_UPDATE_IN_BASE (1 << 27)
b3c96fb1 160#define CE_STRIP_NAME (1 << 28)
078a58e8 161
06aaaa0b
NTND
162/*
163 * Extended on-disk flags
164 */
2977ffbb
NTND
165#define CE_INTENT_TO_ADD (1 << 29)
166#define CE_SKIP_WORKTREE (1 << 30)
06aaaa0b 167/* CE_EXTENDED2 is for future extension */
9a93c668 168#define CE_EXTENDED2 (1U << 31)
06aaaa0b 169
44a36913 170#define CE_EXTENDED_FLAGS (CE_INTENT_TO_ADD | CE_SKIP_WORKTREE)
06aaaa0b
NTND
171
172/*
173 * Safeguard to avoid saving wrong flags:
174 * - CE_EXTENDED2 won't get saved until its semantic is known
175 * - Bits in 0x0000FFFF have been saved in ce_flags already
176 * - Bits in 0x003F0000 are currently in-memory flags
177 */
178#if CE_EXTENDED_FLAGS & 0x803FFFFF
179#error "CE_EXTENDED_FLAGS out of range"
180#endif
181
4300f844
DS
182#define S_ISSPARSEDIR(m) ((m) == S_IFDIR)
183
3e3a4a41 184/* Forward structure decls */
64acde94 185struct pathspec;
3e3a4a41 186struct child_process;
e1f8694f 187struct tree;
64acde94 188
eb7a2f1d
LT
189/*
190 * Copy the sha1 and stat state of a cache entry from one to
191 * another. But we never change the name, or the hash state!
192 */
20d142b4
RS
193static inline void copy_cache_entry(struct cache_entry *dst,
194 const struct cache_entry *src)
eb7a2f1d 195{
419a597f 196 unsigned int state = dst->ce_flags & CE_HASHED;
8e72d675 197 int mem_pool_allocated = dst->mem_pool_allocated;
eb7a2f1d
LT
198
199 /* Don't copy hash chain and name */
8b013788
KB
200 memcpy(&dst->ce_stat_data, &src->ce_stat_data,
201 offsetof(struct cache_entry, name) -
202 offsetof(struct cache_entry, ce_stat_data));
eb7a2f1d
LT
203
204 /* Restore the hash state */
419a597f 205 dst->ce_flags = (dst->ce_flags & ~CE_HASHED) | state;
8e72d675
JM
206
207 /* Restore the mem_pool_allocated flag */
208 dst->mem_pool_allocated = mem_pool_allocated;
eb7a2f1d
LT
209}
210
b60e188c 211static inline unsigned create_ce_flags(unsigned stage)
7fec10b7 212{
b60e188c 213 return (stage << CE_STAGESHIFT);
7fec10b7
JH
214}
215
b60e188c 216#define ce_namelen(ce) ((ce)->ce_namelen)
aee46198 217#define ce_size(ce) cache_entry_size(ce_namelen(ce))
7a51ed66 218#define ce_stage(ce) ((CE_STAGEMASK & (ce)->ce_flags) >> CE_STAGESHIFT)
eadb5831 219#define ce_uptodate(ce) ((ce)->ce_flags & CE_UPTODATE)
44a36913 220#define ce_skip_worktree(ce) ((ce)->ce_flags & CE_SKIP_WORKTREE)
eadb5831 221#define ce_mark_uptodate(ce) ((ce)->ce_flags |= CE_UPTODATE)
895ff3b2 222#define ce_intent_to_add(ce) ((ce)->ce_flags & CE_INTENT_TO_ADD)
aee46198 223
e4479470 224#define ce_permissions(mode) (((mode) & 0100) ? 0755 : 0644)
8ae0a8c5
KS
225static inline unsigned int create_ce_mode(unsigned int mode)
226{
8ae0a8c5 227 if (S_ISLNK(mode))
7a51ed66 228 return S_IFLNK;
6e773527
DS
229 if (S_ISSPARSEDIR(mode))
230 return S_IFDIR;
302b9282 231 if (S_ISDIR(mode) || S_ISGITLINK(mode))
7a51ed66
LT
232 return S_IFGITLINK;
233 return S_IFREG | ce_permissions(mode);
8ae0a8c5 234}
20d142b4
RS
235static inline unsigned int ce_mode_from_stat(const struct cache_entry *ce,
236 unsigned int mode)
185c975f 237{
78a8d641
JS
238 extern int trust_executable_bit, has_symlinks;
239 if (!has_symlinks && S_ISREG(mode) &&
7a51ed66 240 ce && S_ISLNK(ce->ce_mode))
78a8d641 241 return ce->ce_mode;
185c975f 242 if (!trust_executable_bit && S_ISREG(mode)) {
7a51ed66 243 if (ce && S_ISREG(ce->ce_mode))
185c975f
JH
244 return ce->ce_mode;
245 return create_ce_mode(0666);
246 }
247 return create_ce_mode(mode);
248}
d6b8fc30
JH
249static inline int ce_to_dtype(const struct cache_entry *ce)
250{
251 unsigned ce_mode = ntohl(ce->ce_mode);
252 if (S_ISREG(ce_mode))
253 return DT_REG;
254 else if (S_ISDIR(ce_mode) || S_ISGITLINK(ce_mode))
255 return DT_DIR;
256 else if (S_ISLNK(ce_mode))
257 return DT_LNK;
258 else
259 return DT_UNKNOWN;
260}
b90d9b88
RS
261static inline unsigned int canon_mode(unsigned int mode)
262{
263 if (S_ISREG(mode))
264 return S_IFREG | ce_permissions(mode);
265 if (S_ISLNK(mode))
266 return S_IFLNK;
267 if (S_ISDIR(mode))
268 return S_IFDIR;
269 return S_IFGITLINK;
270}
e4479470 271
ac48adf4
EN
272static inline int ce_path_match(struct index_state *istate,
273 const struct cache_entry *ce,
274 const struct pathspec *pathspec,
275 char *seen)
276{
277 return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
278 S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
279}
280
ee7825b5 281#define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)
f5cabd13 282
e636a7b4
NTND
283#define SOMETHING_CHANGED (1 << 0) /* unclassified changes go here */
284#define CE_ENTRY_CHANGED (1 << 1)
285#define CE_ENTRY_REMOVED (1 << 2)
286#define CE_ENTRY_ADDED (1 << 3)
6c306a34 287#define RESOLVE_UNDO_CHANGED (1 << 4)
a5400efe 288#define CACHE_TREE_CHANGED (1 << 5)
c18b80a0 289#define SPLIT_INDEX_ORDERED (1 << 6)
1bbb3dba 290#define UNTRACKED_CHANGED (1 << 7)
883e248b 291#define FSMONITOR_CHANGED (1 << 8)
e636a7b4 292
5fc2fc8f 293struct split_index;
83c094ad 294struct untracked_cache;
4dcd4def 295struct progress;
836e25c5 296struct pattern_list;
83c094ad 297
9fadb373
DS
298enum sparse_index_mode {
299 /*
300 * There are no sparse directories in the index at all.
301 *
302 * Repositories that don't use cone-mode sparse-checkout will
303 * always have their indexes in this mode.
304 */
305 INDEX_EXPANDED = 0,
306
307 /*
308 * The index has already been collapsed to sparse directories
309 * whereever possible.
310 */
311 INDEX_COLLAPSED,
312
313 /*
314 * The sparse directories that exist are outside the
315 * sparse-checkout boundary, but it is possible that some file
316 * entries could collapse to sparse directory entries.
317 */
318 INDEX_PARTIALLY_SPARSE,
319};
320
228e94f9
JH
321struct index_state {
322 struct cache_entry **cache;
9d227781 323 unsigned int version;
228e94f9 324 unsigned int cache_nr, cache_alloc, cache_changed;
cfc5789a 325 struct string_list *resolve_undo;
228e94f9 326 struct cache_tree *cache_tree;
5fc2fc8f 327 struct split_index *split_index;
fba2f38a 328 struct cache_time timestamp;
913e0e99 329 unsigned name_hash_initialized : 1,
4bddd983 330 initialized : 1,
1956ecd0
BP
331 drop_cache_tree : 1,
332 updated_workdir : 1,
cfd635c7 333 updated_skipworktree : 1,
9fadb373
DS
334 fsmonitor_has_run_once : 1;
335 enum sparse_index_mode sparse_index;
8b013788 336 struct hashmap name_hash;
e05881a4 337 struct hashmap dir_hash;
75691ea3 338 struct object_id oid;
83c094ad 339 struct untracked_cache *untracked;
56c69100 340 char *fsmonitor_last_update;
ba1b9cac 341 struct ewah_bitmap *fsmonitor_dirty;
8e72d675 342 struct mem_pool *ce_mem_pool;
4dcd4def 343 struct progress *progress;
1fd9ae51 344 struct repository *repo;
836e25c5 345 struct pattern_list *sparse_checkout_patterns;
228e94f9
JH
346};
347
2f6b1eb7
ÆAB
348/**
349 * A "struct index_state istate" must be initialized with
350 * INDEX_STATE_INIT or the corresponding index_state_init().
351 *
352 * If the variable won't be used again, use release_index() to free()
353 * its resources. If it needs to be used again use discard_index(),
354 * which does the same thing, but will use use index_state_init() at
6269f8ea
ÆAB
355 * the end. The discard_index() will use its own "istate->repo" as the
356 * "r" argument to index_state_init() in that case.
2f6b1eb7 357 */
6269f8ea
ÆAB
358#define INDEX_STATE_INIT(r) { \
359 .repo = (r), \
360}
361void index_state_init(struct index_state *istate, struct repository *r);
2f6b1eb7
ÆAB
362void release_index(struct index_state *istate);
363
96872bc2 364/* Name hashing */
55454427
DL
365int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
366void add_name_hash(struct index_state *istate, struct cache_entry *ce);
367void remove_name_hash(struct index_state *istate, struct cache_entry *ce);
368void free_name_hash(struct index_state *istate);
96872bc2 369
a849735b
JM
370/* Cache entry creation and cleanup */
371
372/*
373 * Create cache_entry intended for use in the specified index. Caller
374 * is responsible for discarding the cache_entry with
375 * `discard_cache_entry`.
376 */
377struct cache_entry *make_cache_entry(struct index_state *istate,
378 unsigned int mode,
379 const struct object_id *oid,
380 const char *path,
381 int stage,
382 unsigned int refresh_options);
383
384struct cache_entry *make_empty_cache_entry(struct index_state *istate,
385 size_t name_len);
386
387/*
96168827
MT
388 * Create a cache_entry that is not intended to be added to an index. If
389 * `ce_mem_pool` is not NULL, the entry is allocated within the given memory
390 * pool. Caller is responsible for discarding "loose" entries with
391 * `discard_cache_entry()` and the memory pool with
392 * `mem_pool_discard(ce_mem_pool, should_validate_cache_entries())`.
a849735b
JM
393 */
394struct cache_entry *make_transient_cache_entry(unsigned int mode,
395 const struct object_id *oid,
396 const char *path,
96168827
MT
397 int stage,
398 struct mem_pool *ce_mem_pool);
a849735b 399
96168827
MT
400struct cache_entry *make_empty_transient_cache_entry(size_t len,
401 struct mem_pool *ce_mem_pool);
a849735b
JM
402
403/*
404 * Discard cache entry.
405 */
406void discard_cache_entry(struct cache_entry *ce);
407
8616a2d0
JM
408/*
409 * Check configuration if we should perform extra validation on cache
410 * entries.
411 */
412int should_validate_cache_entries(void);
413
8e72d675
JM
414/*
415 * Duplicate a cache_entry. Allocate memory for the new entry from a
416 * memory_pool. Takes into account cache_entry fields that are meant
417 * for managing the underlying memory allocation of the cache_entry.
418 */
419struct cache_entry *dup_cache_entry(const struct cache_entry *ce, struct index_state *istate);
420
421/*
422 * Validate the cache entries in the index. This is an internal
423 * consistency check that the cache_entry structs are allocated from
424 * the expected memory pool.
425 */
426void validate_cache_entries(const struct index_state *istate);
427
b2896d27
JT
428/*
429 * Bulk prefetch all missing cache entries that are not GITLINKs and that match
430 * the given predicate. This function should only be called if
c7c33f50 431 * repo_has_promisor_remote() returns true.
b2896d27
JT
432 */
433typedef int (*must_prefetch_predicate)(const struct cache_entry *);
434void prefetch_cache_entries(const struct index_state *istate,
435 must_prefetch_predicate must_prefetch);
436
dfd0a893 437#ifdef USE_THE_INDEX_VARIABLE
f8adbec9 438extern struct index_state the_index;
666f53eb 439#endif
e83c5163 440
b45563a2
JH
441static inline enum object_type object_type(unsigned int mode)
442{
443 return S_ISDIR(mode) ? OBJ_TREE :
444 S_ISGITLINK(mode) ? OBJ_COMMIT :
445 OBJ_BLOB;
446}
447
f225aeb2 448#define INIT_DB_QUIET 0x0001
33158701 449#define INIT_DB_EXIST_OK 0x0002
f225aeb2 450
55454427 451int init_db(const char *git_dir, const char *real_git_dir,
8b8f7189 452 const char *template_dir, int hash_algo,
32ba12da 453 const char *initial_branch, unsigned int flags);
47ac9703 454void initialize_repository_version(int hash_algo, int reinit);
f225aeb2 455
734aab75 456/* Initialize and use the cache information */
03b86647 457struct lock_file;
55454427 458void preload_index(struct index_state *index,
ad6dad09
DL
459 const struct pathspec *pathspec,
460 unsigned int refresh_flags);
55454427 461int do_read_index(struct index_state *istate, const char *path,
ad6dad09 462 int must_exist); /* for testting only! */
55454427 463int read_index_from(struct index_state *, const char *path,
ad6dad09 464 const char *gitdir);
55454427 465int is_index_unborn(struct index_state *);
8dc38346 466
4300f844
DS
467void ensure_full_index(struct index_state *istate);
468
8dc38346 469/* For use with `write_locked_index()`. */
03b86647 470#define COMMIT_LOCK (1 << 0)
61000814 471#define SKIP_IF_UNCHANGED (1 << 1)
8dc38346
472
473/*
812d6b00
474 * Write the index while holding an already-taken lock. Close the lock,
475 * and if `COMMIT_LOCK` is given, commit it.
8dc38346
476 *
477 * Unless a split index is in use, write the index into the lockfile.
478 *
479 * With a split index, write the shared index to a temporary file,
480 * adjust its permissions and rename it into place, then write the
481 * split index to the lockfile. If the temporary file for the shared
482 * index cannot be created, fall back to the behavior described in
483 * the previous paragraph.
df60cf57
484 *
485 * With `COMMIT_LOCK`, the lock is always committed or rolled back.
486 * Without it, the lock is closed, but neither committed nor rolled
487 * back.
61000814
488 *
489 * If `SKIP_IF_UNCHANGED` is given and the index is unchanged, nothing
490 * is written (and the lock is rolled back if `COMMIT_LOCK` is given).
8dc38346 491 */
55454427 492int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
8dc38346 493
9c5f3ee3 494void discard_index(struct index_state *);
55454427
DL
495void move_index_extensions(struct index_state *dst, struct index_state *src);
496int unmerged_index(const struct index_state *);
b101793c
EN
497
498/**
e1f8694f
EN
499 * Returns 1 if istate differs from tree, 0 otherwise. If tree is NULL,
500 * compares istate to HEAD. If tree is NULL and on an unborn branch,
501 * returns 1 if there are entries in istate, 0 otherwise. If an strbuf is
502 * provided, the space-separated list of files that differ will be appended
503 * to it.
b101793c 504 */
55454427 505int repo_index_has_changes(struct repository *repo,
ad6dad09
DL
506 struct tree *tree,
507 struct strbuf *sb);
b101793c 508
55454427
DL
509int verify_path(const char *path, unsigned mode);
510int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
511int index_dir_exists(struct index_state *istate, const char *name, int namelen);
512void adjust_dirname_case(struct index_state *istate, char *name);
513struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
12733e9d
SB
514
515/*
516 * Searches for an entry defined by name and namelen in the given index.
517 * If the return value is positive (including 0) it is the position of an
518 * exact match. If the return value is negative, the negated value minus 1
519 * is the position where the entry would be inserted.
520 * Example: The current index consists of these files and its stages:
521 *
522 * b#0, d#0, f#1, f#3
523 *
524 * index_name_pos(&index, "a", 1) -> -1
525 * index_name_pos(&index, "b", 1) -> 0
526 * index_name_pos(&index, "c", 1) -> -2
527 * index_name_pos(&index, "d", 1) -> 1
528 * index_name_pos(&index, "e", 1) -> -3
529 * index_name_pos(&index, "f", 1) -> -3
530 * index_name_pos(&index, "g", 1) -> -5
531 */
847a9e5d 532int index_name_pos(struct index_state *, const char *name, int namelen);
12733e9d 533
9553aa0f
VD
534/*
535 * Like index_name_pos, returns the position of an entry of the given name in
536 * the index if one exists, otherwise returns a negative value where the negated
537 * value minus 1 is the position where the index entry would be inserted. Unlike
538 * index_name_pos, however, a sparse index is not expanded to find an entry
539 * inside a sparse directory.
540 */
541int index_name_pos_sparse(struct index_state *, const char *name, int namelen);
542
20ec2d03
VD
543/*
544 * Determines whether an entry with the given name exists within the
545 * given index. The return value is 1 if an exact match is found, otherwise
546 * it is 0. Note that, unlike index_name_pos, this function does not expand
547 * the index if it is sparse. If an item exists within the full index but it
548 * is contained within a sparse directory (and not in the sparse index), 0 is
549 * returned.
550 */
551int index_entry_exists(struct index_state *, const char *name, int namelen);
552
c097b95a
JS
553/*
554 * Some functions return the negative complement of an insert position when a
555 * precise match was not found but a position was found where the entry would
556 * need to be inserted. This helper protects that logic from any integer
557 * underflow.
558 */
559static inline int index_pos_to_insert_pos(uintmax_t pos)
560{
561 if (pos > INT_MAX)
562 die("overflow: -1 - %"PRIuMAX, pos);
563 return -1 - (int)pos;
564}
565
192268c1
JH
566#define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
567#define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
b155725d 568#define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
eefadd18 569#define ADD_CACHE_JUST_APPEND 8 /* Append only */
39425819 570#define ADD_CACHE_NEW_ONLY 16 /* Do not replace existing ones */
ce7c614b 571#define ADD_CACHE_KEEP_CACHE_TREE 32 /* Do not invalidate cache-tree */
9e5da3d0 572#define ADD_CACHE_RENORMALIZE 64 /* Pass along HASH_RENORMALIZE */
55454427
DL
573int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
574void rename_index_entry_at(struct index_state *, int pos, const char *new_name);
3bd72adf
SB
575
576/* Remove entry, return true if there are more entries to go. */
55454427 577int remove_index_entry_at(struct index_state *, int pos);
3bd72adf 578
55454427
DL
579void remove_marked_cache_entries(struct index_state *istate, int invalidate);
580int remove_file_from_index(struct index_state *, const char *path);
38ed1d89
JH
581#define ADD_CACHE_VERBOSE 1
582#define ADD_CACHE_PRETEND 2
01665924 583#define ADD_CACHE_IGNORE_ERRORS 4
041aee31 584#define ADD_CACHE_IGNORE_REMOVAL 8
39425819 585#define ADD_CACHE_INTENT 16
20cf41d0
SB
586/*
587 * These two are used to add the contents of the file at path
588 * to the index, marking the working tree up-to-date by storing
589 * the cached stat info in the resulting cache entry. A caller
590 * that has already run lstat(2) on the path can call
591 * add_to_index(), and all others can call add_file_to_index();
592 * the latter will do necessary lstat(2) internally before
593 * calling the former.
594 */
55454427
DL
595int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
596int add_file_to_index(struct index_state *, const char *path, int flags);
20cf41d0 597
55454427
DL
598int chmod_index_entry(struct index_state *, struct cache_entry *ce, char flip);
599int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
600void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
847a9e5d
DS
601int index_name_is_other(struct index_state *, const char *, int);
602void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);
4bd5b7da
JH
603
604/* do stat comparison even if CE_VALID is true */
605#define CE_MATCH_IGNORE_VALID 01
606/* do not check the contents but report dirty on racily-clean entries */
56cac48c
NTND
607#define CE_MATCH_RACY_IS_DIRTY 02
608/* do stat comparison even if CE_SKIP_WORKTREE is true */
609#define CE_MATCH_IGNORE_SKIP_WORKTREE 04
2e2e7ec1
BK
610/* ignore non-existent files during stat update */
611#define CE_MATCH_IGNORE_MISSING 0x08
25762726
BK
612/* enable stat refresh */
613#define CE_MATCH_REFRESH 0x10
883e248b
BP
614/* don't refresh_fsmonitor state or do stat comparison even if CE_FSMONITOR_VALID is true */
615#define CE_MATCH_IGNORE_FSMONITOR 0X20
55454427 616int is_racy_timestamp(const struct index_state *istate,
ad6dad09 617 const struct cache_entry *ce);
2ede073f 618int has_racy_timestamp(struct index_state *istate);
55454427
DL
619int ie_match_stat(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
620int ie_modified(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
4bd5b7da 621
c4ce46fc
JH
622#define HASH_WRITE_OBJECT 1
623#define HASH_FORMAT_CHECK 2
9472935d 624#define HASH_RENORMALIZE 4
4ef91a2d 625#define HASH_SILENT 8
55454427
DL
626int index_fd(struct index_state *istate, struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
627int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
c21d39d7
MH
628
629/*
630 * Record to sd the data from st that we use to check whether a file
631 * might have changed.
632 */
55454427 633void fill_stat_data(struct stat_data *sd, struct stat *st);
c21d39d7
MH
634
635/*
636 * Return 0 if st is consistent with a file not having been changed
637 * since sd was filled. If there are differences, return a
638 * combination of MTIME_CHANGED, CTIME_CHANGED, OWNER_CHANGED,
639 * INODE_CHANGED, and DATA_CHANGED.
640 */
55454427
DL
641int match_stat_data(const struct stat_data *sd, struct stat *st);
642int match_stat_data_racy(const struct index_state *istate,
ad6dad09 643 const struct stat_data *sd, struct stat *st);
c21d39d7 644
d4c0a3ac 645void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st);
415e96c8 646
b243012c
MT
647#define REFRESH_REALLY (1 << 0) /* ignore_valid */
648#define REFRESH_UNMERGED (1 << 1) /* allow unmerged */
649#define REFRESH_QUIET (1 << 2) /* be quiet about it */
650#define REFRESH_IGNORE_MISSING (1 << 3) /* ignore non-existent */
651#define REFRESH_IGNORE_SUBMODULES (1 << 4) /* ignore submodules */
652#define REFRESH_IN_PORCELAIN (1 << 5) /* user friendly output, not "needs update" */
653#define REFRESH_PROGRESS (1 << 6) /* show progress bar if stderr is tty */
654#define REFRESH_IGNORE_SKIP_WORKTREE (1 << 7) /* ignore skip_worktree entries */
55454427 655int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
22184497
TG
656/*
657 * Refresh the index and write it to disk.
658 *
659 * 'refresh_flags' is passed directly to 'refresh_index()', while
660 * 'COMMIT_LOCK | write_flags' is passed to 'write_locked_index()', so
661 * the lockfile is always either committed or rolled back.
662 *
663 * If 'gentle' is passed, errors locking the index are ignored.
664 *
665 * Return 1 if refreshing the index returns an error, -1 if writing
666 * the index to disk fails, 0 on success.
667 *
668 * Note that if refreshing the index returns an error, we still write
669 * out the index (unless locking fails).
670 */
671int 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);
672
55454427 673struct cache_entry *refresh_cache_entry(struct index_state *, struct cache_entry *, unsigned int);
405e5b2f 674
55454427 675void set_alternate_index_output(const char *);
697cc8ef 676
a33fc72f 677extern int verify_index_checksum;
00ec50e5 678extern int verify_ce_order;
a33fc72f 679
9378c161 680extern int quote_path_fully;
7875acb6 681
734aab75
LT
682#define MTIME_CHANGED 0x0001
683#define CTIME_CHANGED 0x0002
684#define OWNER_CHANGED 0x0004
685#define MODE_CHANGED 0x0008
686#define INODE_CHANGED 0x0010
687#define DATA_CHANGED 0x0020
8ae0a8c5 688#define TYPE_CHANGED 0x0040
e83c5163 689
0be0521b
MH
690/*
691 * Create the directory containing the named path, using care to be
204a047f
MH
692 * somewhat safe against races. Return one of the scld_error values to
693 * indicate success/failure. On error, set errno to describe the
694 * problem.
18d37e86
MH
695 *
696 * SCLD_VANISHED indicates that one of the ancestor directories of the
697 * path existed at one point during the function call and then
698 * suddenly vanished, probably because another process pruned the
699 * directory while we were working. To be robust against this kind of
700 * race, callers might want to try invoking the function again when it
701 * returns SCLD_VANISHED.
e95792e5
MH
702 *
703 * safe_create_leading_directories() temporarily changes path while it
704 * is working but restores it before returning.
705 * safe_create_leading_directories_const() doesn't modify path, even
eb3c027e
MT
706 * temporarily. Both these variants adjust the permissions of the
707 * created directories to honor core.sharedRepository, so they are best
708 * suited for files inside the git dir. For working tree files, use
709 * safe_create_leading_directories_no_share() instead, as it ignores
710 * the core.sharedRepository setting.
0be0521b
MH
711 */
712enum scld_error {
713 SCLD_OK = 0,
714 SCLD_FAILED = -1,
715 SCLD_PERMS = -2,
18d37e86
MH
716 SCLD_EXISTS = -3,
717 SCLD_VANISHED = -4
0be0521b
MH
718};
719enum scld_error safe_create_leading_directories(char *path);
720enum scld_error safe_create_leading_directories_const(const char *path);
eb3c027e 721enum scld_error safe_create_leading_directories_no_share(char *path);
0be0521b 722
90a6464b 723int mkdir_in_gitdir(const char *path);
e7f136bf 724
55454427 725int git_open_cloexec(const char *name, int flags);
b4d065df 726#define git_open(name) git_open_cloexec(name, O_RDONLY)
01cab976
ÆAB
727
728/**
729 * unpack_loose_header() initializes the data stream needed to unpack
730 * a loose object header.
731 *
3b6a8db3
ÆAB
732 * Returns:
733 *
734 * - ULHR_OK on success
735 * - ULHR_BAD on error
5848fb11 736 * - ULHR_TOO_LONG if the header was too long
01cab976
ÆAB
737 *
738 * It will only parse up to MAX_HEADER_LEN bytes unless an optional
739 * "hdrbuf" argument is non-NULL. This is intended for use with
740 * OBJECT_INFO_ALLOW_UNKNOWN_TYPE to extract the bad type for (error)
741 * reporting. The full header will be extracted to "hdrbuf" for use
5848fb11
ÆAB
742 * with parse_loose_header(), ULHR_TOO_LONG will still be returned
743 * from this function to indicate that the header was too long.
01cab976 744 */
3b6a8db3
ÆAB
745enum unpack_loose_header_result {
746 ULHR_OK,
747 ULHR_BAD,
5848fb11 748 ULHR_TOO_LONG,
3b6a8db3
ÆAB
749};
750enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
751 unsigned char *map,
752 unsigned long mapsize,
753 void *buffer,
754 unsigned long bufsiz,
755 struct strbuf *hdrbuf);
756
dccb32bf
ÆAB
757/**
758 * parse_loose_header() parses the starting "<type> <len>\0" of an
759 * object. If it doesn't follow that format -1 is returned. To check
760 * the validity of the <type> populate the "typep" in the "struct
761 * object_info". It will be OBJ_BAD if the object type is unknown. The
762 * parsed <len> can be retrieved via "oi->sizep", and from there
763 * passed to unpack_loose_rest().
764 */
ddb3474b 765struct object_info;
dccb32bf 766int parse_loose_header(const char *hdr, struct object_info *oi);
8237b185 767
cdcaaec9
ÆAB
768/**
769 * With in-core object data in "buf", rehash it to make sure the
770 * object name actually matches "oid" to detect object corruption.
ee213de2
ÆAB
771 *
772 * A negative value indicates an error, usually that the OID is not
773 * what we expected, but it might also indicate another error.
cdcaaec9 774 */
b98d1885 775int check_object_signature(struct repository *r, const struct object_id *oid,
44439c1c
ÆAB
776 void *map, unsigned long size,
777 enum object_type type);
0f156dbb
ÆAB
778
779/**
780 * A streaming version of check_object_signature().
781 * Try reading the object named with "oid" using
782 * the streaming interface and rehash it to do the same.
783 */
784int stream_object_signature(struct repository *r, const struct object_id *oid);
e83c5163 785
55454427 786int finalize_object_file(const char *tmpfile, const char *filename);
8237b185 787
6a5e6f5e 788/* Helper to check and "touch" a file */
55454427 789int check_and_freshen_file(const char *fn, int freshen);
6a5e6f5e 790
1b4a38d7
RS
791int base_name_compare(const char *name1, size_t len1, int mode1,
792 const char *name2, size_t len2, int mode2);
793int df_name_compare(const char *name1, size_t len1, int mode1,
794 const char *name2, size_t len2, int mode2);
55454427
DL
795int name_compare(const char *name1, size_t len1, const char *name2, size_t len2);
796int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2);
e83c5163 797
d3b4705a
NTND
798void *read_object_with_reference(struct repository *r,
799 const struct object_id *oid,
6aea6bae 800 enum object_type required_type,
ad6dad09
DL
801 unsigned long *size,
802 struct object_id *oid_ret);
f4913f91 803
55454427
DL
804const char *git_editor(void);
805const char *git_sequence_editor(void);
806const char *git_pager(int stdout_is_tty);
807int is_terminal_dumb(void);
662cc30c 808
b9fd2846 809struct cache_def {
e7c73053 810 struct strbuf path;
b9fd2846
LT
811 int flags;
812 int track_flags;
813 int prefix_len_stat_func;
814};
f69a6e4f
ÆAB
815#define CACHE_DEF_INIT { \
816 .path = STRBUF_INIT, \
817}
2a608391 818static inline void cache_def_clear(struct cache_def *cache)
e7c73053
KB
819{
820 strbuf_release(&cache->path);
821}
b9fd2846 822
55454427
DL
823int has_symlink_leading_path(const char *name, int len);
824int threaded_has_symlink_leading_path(struct cache_def *, const char *, int);
fab78a0c 825int check_leading_path(const char *name, int len, int warn_on_lstat_err);
55454427 826int has_dirs_only_path(const char *name, int len, int prefix_len);
bcf08f33 827void invalidate_lstat_cache(void);
55454427
DL
828void schedule_dir_for_removal(const char *name, int len);
829void remove_scheduled_dirs(void);
12dccc16 830
c41ee586
SP
831struct pack_window {
832 struct pack_window *next;
833 unsigned char *base;
834 off_t offset;
835 size_t len;
836 unsigned int last_used;
837 unsigned int inuse_cnt;
838};
839
f3bf9224 840struct pack_entry {
c4001d92 841 off_t offset;
f3bf9224
JH
842 struct packed_git *p;
843};
844
8b4c0103 845/*
c93206b4 846 * Set this to 0 to prevent oid_object_info_extended() from fetching missing
8b4c0103
JT
847 * blobs. This has a difference only if extensions.partialClone is set.
848 *
849 * Its default value is 1.
850 */
851extern int fetch_if_missing;
852
8f3f9b09 853/* Dumb servers support */
55454427 854int update_server_info(int);
8f3f9b09 855
d551a488 856extern const char *git_mailmap_file;
08610900 857extern const char *git_mailmap_blob;
4e72dcec 858
00b7cbfc
JH
859#define COPY_READ_ERROR (-2)
860#define COPY_WRITE_ERROR (-3)
55454427
DL
861int copy_fd(int ifd, int ofd);
862int copy_file(const char *dst, const char *src, int mode);
863int copy_file_with_time(const char *dst, const char *src, int mode);
00b7cbfc 864
f67b45f8 865/* pager.c */
55454427
DL
866void setup_pager(void);
867int pager_in_use(void);
aa086eb8 868extern int pager_use_color;
55454427 869int term_columns(void);
cd1096b2 870void term_clear_line(void);
55454427
DL
871int decimal_width(uintmax_t);
872int check_pager_config(const char *cmd);
873void prepare_pager_args(struct child_process *, const char *pager);
f67b45f8 874
051308f6 875/* base85 */
f9815772
JM
876int decode_85(char *dst, const char *line, int linelen);
877void encode_85(char *buf, const unsigned char *data, int bytes);
051308f6 878
5991a55c 879/* pkt-line.c */
bbc30f99 880void packet_trace_identity(const char *prog);
6ce4e61f 881
b6ec1d61 882/* add */
7ae02a30
AR
883/*
884 * return 0 if success, 1 - if addition of a file failed and
885 * ADD_FILES_IGNORE_ERRORS was specified in flags
886 */
610d55af 887int add_files_to_cache(const char *prefix, const struct pathspec *pathspec, int flags);
b6ec1d61 888
aecbf914
JH
889/* diff.c */
890extern int diff_auto_refresh_index;
891
68faf689 892/* match-trees.c */
90d34051
NTND
893void shift_tree(struct repository *, const struct object_id *, const struct object_id *, struct object_id *, int);
894void shift_tree_by(struct repository *, const struct object_id *, const struct object_id *, struct object_id *, const char *);
68faf689 895
a9cc857a
JH
896/*
897 * whitespace rules.
898 * used by both diff and apply
f4b05a49 899 * last two digits are tab width
a9cc857a 900 */
f4b05a49
JS
901#define WS_BLANK_AT_EOL 0100
902#define WS_SPACE_BEFORE_TAB 0200
903#define WS_INDENT_WITH_NON_TAB 0400
904#define WS_CR_AT_EOL 01000
905#define WS_BLANK_AT_EOF 02000
906#define WS_TAB_IN_INDENT 04000
aeb84b05 907#define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
f4b05a49
JS
908#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)
909#define WS_TAB_WIDTH_MASK 077
091f8e28
SB
910/* All WS_* -- when extended, adapt diff.c emit_symbol */
911#define WS_RULE_MASK 07777
cf1b7869 912extern unsigned whitespace_rule_cfg;
55454427
DL
913unsigned whitespace_rule(struct index_state *, const char *);
914unsigned parse_whitespace_rule(const char *);
915unsigned ws_check(const char *line, int len, unsigned ws_rule);
916void ws_check_emit(const char *line, int len, unsigned ws_rule, FILE *stream, const char *set, const char *reset, const char *ws);
917char *whitespace_error_string(unsigned ws);
918void ws_fix_copy(struct strbuf *, const char *, int, unsigned, int *);
c5224f0f 919int ws_blank_line(const char *line, int len);
f4b05a49 920#define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK)
a9cc857a 921
ee425e46 922/* ls-files */
312c984a
BW
923void overlay_tree_on_index(struct index_state *istate,
924 const char *tree_name, const char *prefix);
ee425e46 925
db699a8a
NTND
926/* merge.c */
927struct commit_list;
7e196c3a
NTND
928int try_merge_command(struct repository *r,
929 const char *strategy, size_t xopts_nr,
db699a8a
NTND
930 const char **xopts, struct commit_list *common,
931 const char *head_arg, struct commit_list *remotes);
7e196c3a
NTND
932int checkout_fast_forward(struct repository *r,
933 const struct object_id *from,
f06e90da 934 const struct object_id *to,
db699a8a
NTND
935 int overwrite_ignore);
936
cac42b26 937
38f865c2
JK
938int sane_execvp(const char *file, char *const argv[]);
939
38612532
MH
940/*
941 * A struct to encapsulate the concept of whether a file has changed
942 * since we last checked it. This uses criteria similar to those used
943 * for the index.
944 */
945struct stat_validity {
946 struct stat_data *sd;
947};
948
949void stat_validity_clear(struct stat_validity *sv);
950
951/*
952 * Returns 1 if the path is a regular file (or a symlink to a regular
953 * file) and matches the saved stat_validity, 0 otherwise. A missing
954 * or inaccessible file is considered a match if the struct was just
955 * initialized, or if the previous update found an inaccessible file.
956 */
957int stat_validity_check(struct stat_validity *sv, const char *path);
958
959/*
960 * Update the stat_validity from a file opened at descriptor fd. If
961 * the file is missing, inaccessible, or not a regular file, then
962 * future calls to stat_validity_check will match iff one of those
963 * conditions continues to be true.
964 */
965void stat_validity_update(struct stat_validity *sv, int fd);
966
9ef176b5
NTND
967int versioncmp(const char *s1, const char *s2);
968
e83c5163 969#endif /* CACHE_H */