t2400: rewrite regex to avoid unintentional PCRE
[git/debian.git] / cache.h
blob82d7b112b4e71cc2d707f637178841a327217584
1 #ifndef CACHE_H
2 #define CACHE_H
4 #include "git-compat-util.h"
5 #include "strbuf.h"
6 #include "hashmap.h"
7 #include "list.h"
8 #include "advice.h"
9 #include "gettext.h"
10 #include "convert.h"
11 #include "trace.h"
12 #include "trace2.h"
13 #include "string-list.h"
14 #include "pack-revindex.h"
15 #include "hash.h"
16 #include "path.h"
17 #include "pathspec.h"
18 #include "object.h"
19 #include "oid-array.h"
20 #include "repository.h"
21 #include "statinfo.h"
22 #include "mem-pool.h"
24 typedef struct git_zstream {
25 z_stream z;
26 unsigned long avail_in;
27 unsigned long avail_out;
28 unsigned long total_in;
29 unsigned long total_out;
30 unsigned char *next_in;
31 unsigned char *next_out;
32 } git_zstream;
34 void git_inflate_init(git_zstream *);
35 void git_inflate_init_gzip_only(git_zstream *);
36 void git_inflate_end(git_zstream *);
37 int git_inflate(git_zstream *, int flush);
39 void git_deflate_init(git_zstream *, int level);
40 void git_deflate_init_gzip(git_zstream *, int level);
41 void git_deflate_init_raw(git_zstream *, int level);
42 void git_deflate_end(git_zstream *);
43 int git_deflate_abort(git_zstream *);
44 int git_deflate_end_gently(git_zstream *);
45 int git_deflate(git_zstream *, int flush);
46 unsigned long git_deflate_bound(git_zstream *, unsigned long);
48 #if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
49 #define DTYPE(de) ((de)->d_type)
50 #else
51 #undef DT_UNKNOWN
52 #undef DT_DIR
53 #undef DT_REG
54 #undef DT_LNK
55 #define DT_UNKNOWN 0
56 #define DT_DIR 1
57 #define DT_REG 2
58 #define DT_LNK 3
59 #define DTYPE(de) DT_UNKNOWN
60 #endif
62 /* unknown mode (impossible combination S_IFIFO|S_IFCHR) */
63 #define S_IFINVALID 0030000
66 * A "directory link" is a link to another git directory.
68 * The value 0160000 is not normally a valid mode, and
69 * also just happens to be S_IFDIR + S_IFLNK
71 #define S_IFGITLINK 0160000
72 #define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK)
75 * Some mode bits are also used internally for computations.
77 * They *must* not overlap with any valid modes, and they *must* not be emitted
78 * to outside world - i.e. appear on disk or network. In other words, it's just
79 * temporary fields, which we internally use, but they have to stay in-house.
81 * ( such approach is valid, as standard S_IF* fits into 16 bits, and in Git
82 * codebase mode is `unsigned int` which is assumed to be at least 32 bits )
85 /* used internally in tree-diff */
86 #define S_DIFFTREE_IFXMIN_NEQ 0x80000000
90 * Intensive research over the course of many years has shown that
91 * port 9418 is totally unused by anything else. Or
93 * Your search - "port 9418" - did not match any documents.
95 * as www.google.com puts it.
97 * This port has been properly assigned for git use by IANA:
98 * git (Assigned-9418) [I06-050728-0001].
100 * git 9418/tcp git pack transfer service
101 * git 9418/udp git pack transfer service
103 * with Linus Torvalds <torvalds@osdl.org> as the point of
104 * contact. September 2005.
106 * See http://www.iana.org/assignments/port-numbers
108 #define DEFAULT_GIT_PORT 9418
111 * Basic data structures for the directory cache
114 #define CACHE_SIGNATURE 0x44495243 /* "DIRC" */
115 struct cache_header {
116 uint32_t hdr_signature;
117 uint32_t hdr_version;
118 uint32_t hdr_entries;
121 #define INDEX_FORMAT_LB 2
122 #define INDEX_FORMAT_UB 4
124 struct cache_entry {
125 struct hashmap_entry ent;
126 struct stat_data ce_stat_data;
127 unsigned int ce_mode;
128 unsigned int ce_flags;
129 unsigned int mem_pool_allocated;
130 unsigned int ce_namelen;
131 unsigned int index; /* for link extension */
132 struct object_id oid;
133 char name[FLEX_ARRAY]; /* more */
136 #define CE_STAGEMASK (0x3000)
137 #define CE_EXTENDED (0x4000)
138 #define CE_VALID (0x8000)
139 #define CE_STAGESHIFT 12
142 * Range 0xFFFF0FFF in ce_flags is divided into
143 * two parts: in-memory flags and on-disk ones.
144 * Flags in CE_EXTENDED_FLAGS will get saved on-disk
145 * if you want to save a new flag, add it in
146 * CE_EXTENDED_FLAGS
148 * In-memory only flags
150 #define CE_UPDATE (1 << 16)
151 #define CE_REMOVE (1 << 17)
152 #define CE_UPTODATE (1 << 18)
153 #define CE_ADDED (1 << 19)
155 #define CE_HASHED (1 << 20)
156 #define CE_FSMONITOR_VALID (1 << 21)
157 #define CE_WT_REMOVE (1 << 22) /* remove in work directory */
158 #define CE_CONFLICTED (1 << 23)
160 #define CE_UNPACKED (1 << 24)
161 #define CE_NEW_SKIP_WORKTREE (1 << 25)
163 /* used to temporarily mark paths matched by pathspecs */
164 #define CE_MATCHED (1 << 26)
166 #define CE_UPDATE_IN_BASE (1 << 27)
167 #define CE_STRIP_NAME (1 << 28)
170 * Extended on-disk flags
172 #define CE_INTENT_TO_ADD (1 << 29)
173 #define CE_SKIP_WORKTREE (1 << 30)
174 /* CE_EXTENDED2 is for future extension */
175 #define CE_EXTENDED2 (1U << 31)
177 #define CE_EXTENDED_FLAGS (CE_INTENT_TO_ADD | CE_SKIP_WORKTREE)
180 * Safeguard to avoid saving wrong flags:
181 * - CE_EXTENDED2 won't get saved until its semantic is known
182 * - Bits in 0x0000FFFF have been saved in ce_flags already
183 * - Bits in 0x003F0000 are currently in-memory flags
185 #if CE_EXTENDED_FLAGS & 0x803FFFFF
186 #error "CE_EXTENDED_FLAGS out of range"
187 #endif
189 #define S_ISSPARSEDIR(m) ((m) == S_IFDIR)
191 /* Forward structure decls */
192 struct pathspec;
193 struct child_process;
194 struct tree;
197 * Copy the sha1 and stat state of a cache entry from one to
198 * another. But we never change the name, or the hash state!
200 static inline void copy_cache_entry(struct cache_entry *dst,
201 const struct cache_entry *src)
203 unsigned int state = dst->ce_flags & CE_HASHED;
204 int mem_pool_allocated = dst->mem_pool_allocated;
206 /* Don't copy hash chain and name */
207 memcpy(&dst->ce_stat_data, &src->ce_stat_data,
208 offsetof(struct cache_entry, name) -
209 offsetof(struct cache_entry, ce_stat_data));
211 /* Restore the hash state */
212 dst->ce_flags = (dst->ce_flags & ~CE_HASHED) | state;
214 /* Restore the mem_pool_allocated flag */
215 dst->mem_pool_allocated = mem_pool_allocated;
218 static inline unsigned create_ce_flags(unsigned stage)
220 return (stage << CE_STAGESHIFT);
223 #define ce_namelen(ce) ((ce)->ce_namelen)
224 #define ce_size(ce) cache_entry_size(ce_namelen(ce))
225 #define ce_stage(ce) ((CE_STAGEMASK & (ce)->ce_flags) >> CE_STAGESHIFT)
226 #define ce_uptodate(ce) ((ce)->ce_flags & CE_UPTODATE)
227 #define ce_skip_worktree(ce) ((ce)->ce_flags & CE_SKIP_WORKTREE)
228 #define ce_mark_uptodate(ce) ((ce)->ce_flags |= CE_UPTODATE)
229 #define ce_intent_to_add(ce) ((ce)->ce_flags & CE_INTENT_TO_ADD)
231 #define ce_permissions(mode) (((mode) & 0100) ? 0755 : 0644)
232 static inline unsigned int create_ce_mode(unsigned int mode)
234 if (S_ISLNK(mode))
235 return S_IFLNK;
236 if (S_ISSPARSEDIR(mode))
237 return S_IFDIR;
238 if (S_ISDIR(mode) || S_ISGITLINK(mode))
239 return S_IFGITLINK;
240 return S_IFREG | ce_permissions(mode);
242 static inline unsigned int ce_mode_from_stat(const struct cache_entry *ce,
243 unsigned int mode)
245 extern int trust_executable_bit, has_symlinks;
246 if (!has_symlinks && S_ISREG(mode) &&
247 ce && S_ISLNK(ce->ce_mode))
248 return ce->ce_mode;
249 if (!trust_executable_bit && S_ISREG(mode)) {
250 if (ce && S_ISREG(ce->ce_mode))
251 return ce->ce_mode;
252 return create_ce_mode(0666);
254 return create_ce_mode(mode);
256 static inline int ce_to_dtype(const struct cache_entry *ce)
258 unsigned ce_mode = ntohl(ce->ce_mode);
259 if (S_ISREG(ce_mode))
260 return DT_REG;
261 else if (S_ISDIR(ce_mode) || S_ISGITLINK(ce_mode))
262 return DT_DIR;
263 else if (S_ISLNK(ce_mode))
264 return DT_LNK;
265 else
266 return DT_UNKNOWN;
268 static inline unsigned int canon_mode(unsigned int mode)
270 if (S_ISREG(mode))
271 return S_IFREG | ce_permissions(mode);
272 if (S_ISLNK(mode))
273 return S_IFLNK;
274 if (S_ISDIR(mode))
275 return S_IFDIR;
276 return S_IFGITLINK;
279 static inline int ce_path_match(struct index_state *istate,
280 const struct cache_entry *ce,
281 const struct pathspec *pathspec,
282 char *seen)
284 return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
285 S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
288 #define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)
290 #define SOMETHING_CHANGED (1 << 0) /* unclassified changes go here */
291 #define CE_ENTRY_CHANGED (1 << 1)
292 #define CE_ENTRY_REMOVED (1 << 2)
293 #define CE_ENTRY_ADDED (1 << 3)
294 #define RESOLVE_UNDO_CHANGED (1 << 4)
295 #define CACHE_TREE_CHANGED (1 << 5)
296 #define SPLIT_INDEX_ORDERED (1 << 6)
297 #define UNTRACKED_CHANGED (1 << 7)
298 #define FSMONITOR_CHANGED (1 << 8)
300 struct split_index;
301 struct untracked_cache;
302 struct progress;
303 struct pattern_list;
305 enum sparse_index_mode {
307 * There are no sparse directories in the index at all.
309 * Repositories that don't use cone-mode sparse-checkout will
310 * always have their indexes in this mode.
312 INDEX_EXPANDED = 0,
315 * The index has already been collapsed to sparse directories
316 * whereever possible.
318 INDEX_COLLAPSED,
321 * The sparse directories that exist are outside the
322 * sparse-checkout boundary, but it is possible that some file
323 * entries could collapse to sparse directory entries.
325 INDEX_PARTIALLY_SPARSE,
328 struct index_state {
329 struct cache_entry **cache;
330 unsigned int version;
331 unsigned int cache_nr, cache_alloc, cache_changed;
332 struct string_list *resolve_undo;
333 struct cache_tree *cache_tree;
334 struct split_index *split_index;
335 struct cache_time timestamp;
336 unsigned name_hash_initialized : 1,
337 initialized : 1,
338 drop_cache_tree : 1,
339 updated_workdir : 1,
340 updated_skipworktree : 1,
341 fsmonitor_has_run_once : 1;
342 enum sparse_index_mode sparse_index;
343 struct hashmap name_hash;
344 struct hashmap dir_hash;
345 struct object_id oid;
346 struct untracked_cache *untracked;
347 char *fsmonitor_last_update;
348 struct ewah_bitmap *fsmonitor_dirty;
349 struct mem_pool *ce_mem_pool;
350 struct progress *progress;
351 struct repository *repo;
352 struct pattern_list *sparse_checkout_patterns;
356 * A "struct index_state istate" must be initialized with
357 * INDEX_STATE_INIT or the corresponding index_state_init().
359 * If the variable won't be used again, use release_index() to free()
360 * its resources. If it needs to be used again use discard_index(),
361 * which does the same thing, but will use use index_state_init() at
362 * the end. The discard_index() will use its own "istate->repo" as the
363 * "r" argument to index_state_init() in that case.
365 #define INDEX_STATE_INIT(r) { \
366 .repo = (r), \
368 void index_state_init(struct index_state *istate, struct repository *r);
369 void release_index(struct index_state *istate);
371 /* Name hashing */
372 int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
373 void add_name_hash(struct index_state *istate, struct cache_entry *ce);
374 void remove_name_hash(struct index_state *istate, struct cache_entry *ce);
375 void free_name_hash(struct index_state *istate);
377 /* Cache entry creation and cleanup */
380 * Create cache_entry intended for use in the specified index. Caller
381 * is responsible for discarding the cache_entry with
382 * `discard_cache_entry`.
384 struct cache_entry *make_cache_entry(struct index_state *istate,
385 unsigned int mode,
386 const struct object_id *oid,
387 const char *path,
388 int stage,
389 unsigned int refresh_options);
391 struct cache_entry *make_empty_cache_entry(struct index_state *istate,
392 size_t name_len);
395 * Create a cache_entry that is not intended to be added to an index. If
396 * `ce_mem_pool` is not NULL, the entry is allocated within the given memory
397 * pool. Caller is responsible for discarding "loose" entries with
398 * `discard_cache_entry()` and the memory pool with
399 * `mem_pool_discard(ce_mem_pool, should_validate_cache_entries())`.
401 struct cache_entry *make_transient_cache_entry(unsigned int mode,
402 const struct object_id *oid,
403 const char *path,
404 int stage,
405 struct mem_pool *ce_mem_pool);
407 struct cache_entry *make_empty_transient_cache_entry(size_t len,
408 struct mem_pool *ce_mem_pool);
411 * Discard cache entry.
413 void discard_cache_entry(struct cache_entry *ce);
416 * Check configuration if we should perform extra validation on cache
417 * entries.
419 int should_validate_cache_entries(void);
422 * Duplicate a cache_entry. Allocate memory for the new entry from a
423 * memory_pool. Takes into account cache_entry fields that are meant
424 * for managing the underlying memory allocation of the cache_entry.
426 struct cache_entry *dup_cache_entry(const struct cache_entry *ce, struct index_state *istate);
429 * Validate the cache entries in the index. This is an internal
430 * consistency check that the cache_entry structs are allocated from
431 * the expected memory pool.
433 void validate_cache_entries(const struct index_state *istate);
436 * Bulk prefetch all missing cache entries that are not GITLINKs and that match
437 * the given predicate. This function should only be called if
438 * repo_has_promisor_remote() returns true.
440 typedef int (*must_prefetch_predicate)(const struct cache_entry *);
441 void prefetch_cache_entries(const struct index_state *istate,
442 must_prefetch_predicate must_prefetch);
444 #ifdef USE_THE_INDEX_VARIABLE
445 extern struct index_state the_index;
446 #endif
448 static inline enum object_type object_type(unsigned int mode)
450 return S_ISDIR(mode) ? OBJ_TREE :
451 S_ISGITLINK(mode) ? OBJ_COMMIT :
452 OBJ_BLOB;
455 #define INIT_DB_QUIET 0x0001
456 #define INIT_DB_EXIST_OK 0x0002
458 int init_db(const char *git_dir, const char *real_git_dir,
459 const char *template_dir, int hash_algo,
460 const char *initial_branch, unsigned int flags);
461 void initialize_repository_version(int hash_algo, int reinit);
463 /* Initialize and use the cache information */
464 struct lock_file;
465 void preload_index(struct index_state *index,
466 const struct pathspec *pathspec,
467 unsigned int refresh_flags);
468 int do_read_index(struct index_state *istate, const char *path,
469 int must_exist); /* for testting only! */
470 int read_index_from(struct index_state *, const char *path,
471 const char *gitdir);
472 int is_index_unborn(struct index_state *);
474 void ensure_full_index(struct index_state *istate);
476 /* For use with `write_locked_index()`. */
477 #define COMMIT_LOCK (1 << 0)
478 #define SKIP_IF_UNCHANGED (1 << 1)
481 * Write the index while holding an already-taken lock. Close the lock,
482 * and if `COMMIT_LOCK` is given, commit it.
484 * Unless a split index is in use, write the index into the lockfile.
486 * With a split index, write the shared index to a temporary file,
487 * adjust its permissions and rename it into place, then write the
488 * split index to the lockfile. If the temporary file for the shared
489 * index cannot be created, fall back to the behavior described in
490 * the previous paragraph.
492 * With `COMMIT_LOCK`, the lock is always committed or rolled back.
493 * Without it, the lock is closed, but neither committed nor rolled
494 * back.
496 * If `SKIP_IF_UNCHANGED` is given and the index is unchanged, nothing
497 * is written (and the lock is rolled back if `COMMIT_LOCK` is given).
499 int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
501 void discard_index(struct index_state *);
502 void move_index_extensions(struct index_state *dst, struct index_state *src);
503 int unmerged_index(const struct index_state *);
506 * Returns 1 if istate differs from tree, 0 otherwise. If tree is NULL,
507 * compares istate to HEAD. If tree is NULL and on an unborn branch,
508 * returns 1 if there are entries in istate, 0 otherwise. If an strbuf is
509 * provided, the space-separated list of files that differ will be appended
510 * to it.
512 int repo_index_has_changes(struct repository *repo,
513 struct tree *tree,
514 struct strbuf *sb);
516 int verify_path(const char *path, unsigned mode);
517 int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
518 int index_dir_exists(struct index_state *istate, const char *name, int namelen);
519 void adjust_dirname_case(struct index_state *istate, char *name);
520 struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
523 * Searches for an entry defined by name and namelen in the given index.
524 * If the return value is positive (including 0) it is the position of an
525 * exact match. If the return value is negative, the negated value minus 1
526 * is the position where the entry would be inserted.
527 * Example: The current index consists of these files and its stages:
529 * b#0, d#0, f#1, f#3
531 * index_name_pos(&index, "a", 1) -> -1
532 * index_name_pos(&index, "b", 1) -> 0
533 * index_name_pos(&index, "c", 1) -> -2
534 * index_name_pos(&index, "d", 1) -> 1
535 * index_name_pos(&index, "e", 1) -> -3
536 * index_name_pos(&index, "f", 1) -> -3
537 * index_name_pos(&index, "g", 1) -> -5
539 int index_name_pos(struct index_state *, const char *name, int namelen);
542 * Like index_name_pos, returns the position of an entry of the given name in
543 * the index if one exists, otherwise returns a negative value where the negated
544 * value minus 1 is the position where the index entry would be inserted. Unlike
545 * index_name_pos, however, a sparse index is not expanded to find an entry
546 * inside a sparse directory.
548 int index_name_pos_sparse(struct index_state *, const char *name, int namelen);
551 * Determines whether an entry with the given name exists within the
552 * given index. The return value is 1 if an exact match is found, otherwise
553 * it is 0. Note that, unlike index_name_pos, this function does not expand
554 * the index if it is sparse. If an item exists within the full index but it
555 * is contained within a sparse directory (and not in the sparse index), 0 is
556 * returned.
558 int index_entry_exists(struct index_state *, const char *name, int namelen);
561 * Some functions return the negative complement of an insert position when a
562 * precise match was not found but a position was found where the entry would
563 * need to be inserted. This helper protects that logic from any integer
564 * underflow.
566 static inline int index_pos_to_insert_pos(uintmax_t pos)
568 if (pos > INT_MAX)
569 die("overflow: -1 - %"PRIuMAX, pos);
570 return -1 - (int)pos;
573 #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
574 #define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
575 #define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
576 #define ADD_CACHE_JUST_APPEND 8 /* Append only */
577 #define ADD_CACHE_NEW_ONLY 16 /* Do not replace existing ones */
578 #define ADD_CACHE_KEEP_CACHE_TREE 32 /* Do not invalidate cache-tree */
579 #define ADD_CACHE_RENORMALIZE 64 /* Pass along HASH_RENORMALIZE */
580 int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
581 void rename_index_entry_at(struct index_state *, int pos, const char *new_name);
583 /* Remove entry, return true if there are more entries to go. */
584 int remove_index_entry_at(struct index_state *, int pos);
586 void remove_marked_cache_entries(struct index_state *istate, int invalidate);
587 int remove_file_from_index(struct index_state *, const char *path);
588 #define ADD_CACHE_VERBOSE 1
589 #define ADD_CACHE_PRETEND 2
590 #define ADD_CACHE_IGNORE_ERRORS 4
591 #define ADD_CACHE_IGNORE_REMOVAL 8
592 #define ADD_CACHE_INTENT 16
594 * These two are used to add the contents of the file at path
595 * to the index, marking the working tree up-to-date by storing
596 * the cached stat info in the resulting cache entry. A caller
597 * that has already run lstat(2) on the path can call
598 * add_to_index(), and all others can call add_file_to_index();
599 * the latter will do necessary lstat(2) internally before
600 * calling the former.
602 int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
603 int add_file_to_index(struct index_state *, const char *path, int flags);
605 int chmod_index_entry(struct index_state *, struct cache_entry *ce, char flip);
606 int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
607 void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
608 int index_name_is_other(struct index_state *, const char *, int);
609 void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);
611 /* do stat comparison even if CE_VALID is true */
612 #define CE_MATCH_IGNORE_VALID 01
613 /* do not check the contents but report dirty on racily-clean entries */
614 #define CE_MATCH_RACY_IS_DIRTY 02
615 /* do stat comparison even if CE_SKIP_WORKTREE is true */
616 #define CE_MATCH_IGNORE_SKIP_WORKTREE 04
617 /* ignore non-existent files during stat update */
618 #define CE_MATCH_IGNORE_MISSING 0x08
619 /* enable stat refresh */
620 #define CE_MATCH_REFRESH 0x10
621 /* don't refresh_fsmonitor state or do stat comparison even if CE_FSMONITOR_VALID is true */
622 #define CE_MATCH_IGNORE_FSMONITOR 0X20
623 int is_racy_timestamp(const struct index_state *istate,
624 const struct cache_entry *ce);
625 int has_racy_timestamp(struct index_state *istate);
626 int ie_match_stat(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
627 int ie_modified(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
629 #define HASH_WRITE_OBJECT 1
630 #define HASH_FORMAT_CHECK 2
631 #define HASH_RENORMALIZE 4
632 #define HASH_SILENT 8
633 int index_fd(struct index_state *istate, struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
634 int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
637 * Record to sd the data from st that we use to check whether a file
638 * might have changed.
640 void fill_stat_data(struct stat_data *sd, struct stat *st);
643 * Return 0 if st is consistent with a file not having been changed
644 * since sd was filled. If there are differences, return a
645 * combination of MTIME_CHANGED, CTIME_CHANGED, OWNER_CHANGED,
646 * INODE_CHANGED, and DATA_CHANGED.
648 int match_stat_data(const struct stat_data *sd, struct stat *st);
649 int match_stat_data_racy(const struct index_state *istate,
650 const struct stat_data *sd, struct stat *st);
652 void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st);
654 #define REFRESH_REALLY (1 << 0) /* ignore_valid */
655 #define REFRESH_UNMERGED (1 << 1) /* allow unmerged */
656 #define REFRESH_QUIET (1 << 2) /* be quiet about it */
657 #define REFRESH_IGNORE_MISSING (1 << 3) /* ignore non-existent */
658 #define REFRESH_IGNORE_SUBMODULES (1 << 4) /* ignore submodules */
659 #define REFRESH_IN_PORCELAIN (1 << 5) /* user friendly output, not "needs update" */
660 #define REFRESH_PROGRESS (1 << 6) /* show progress bar if stderr is tty */
661 #define REFRESH_IGNORE_SKIP_WORKTREE (1 << 7) /* ignore skip_worktree entries */
662 int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
664 * Refresh the index and write it to disk.
666 * 'refresh_flags' is passed directly to 'refresh_index()', while
667 * 'COMMIT_LOCK | write_flags' is passed to 'write_locked_index()', so
668 * the lockfile is always either committed or rolled back.
670 * If 'gentle' is passed, errors locking the index are ignored.
672 * Return 1 if refreshing the index returns an error, -1 if writing
673 * the index to disk fails, 0 on success.
675 * Note that if refreshing the index returns an error, we still write
676 * out the index (unless locking fails).
678 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);
680 struct cache_entry *refresh_cache_entry(struct index_state *, struct cache_entry *, unsigned int);
682 void set_alternate_index_output(const char *);
684 extern int verify_index_checksum;
685 extern int verify_ce_order;
687 extern int quote_path_fully;
689 #define MTIME_CHANGED 0x0001
690 #define CTIME_CHANGED 0x0002
691 #define OWNER_CHANGED 0x0004
692 #define MODE_CHANGED 0x0008
693 #define INODE_CHANGED 0x0010
694 #define DATA_CHANGED 0x0020
695 #define TYPE_CHANGED 0x0040
698 * Return an abbreviated sha1 unique within this repository's object database.
699 * The result will be at least `len` characters long, and will be NUL
700 * terminated.
702 * The non-`_r` version returns a static buffer which remains valid until 4
703 * more calls to repo_find_unique_abbrev are made.
705 * The `_r` variant writes to a buffer supplied by the caller, which must be at
706 * least `GIT_MAX_HEXSZ + 1` bytes. The return value is the number of bytes
707 * written (excluding the NUL terminator).
709 * Note that while this version avoids the static buffer, it is not fully
710 * reentrant, as it calls into other non-reentrant git code.
712 const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
713 int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
716 * Create the directory containing the named path, using care to be
717 * somewhat safe against races. Return one of the scld_error values to
718 * indicate success/failure. On error, set errno to describe the
719 * problem.
721 * SCLD_VANISHED indicates that one of the ancestor directories of the
722 * path existed at one point during the function call and then
723 * suddenly vanished, probably because another process pruned the
724 * directory while we were working. To be robust against this kind of
725 * race, callers might want to try invoking the function again when it
726 * returns SCLD_VANISHED.
728 * safe_create_leading_directories() temporarily changes path while it
729 * is working but restores it before returning.
730 * safe_create_leading_directories_const() doesn't modify path, even
731 * temporarily. Both these variants adjust the permissions of the
732 * created directories to honor core.sharedRepository, so they are best
733 * suited for files inside the git dir. For working tree files, use
734 * safe_create_leading_directories_no_share() instead, as it ignores
735 * the core.sharedRepository setting.
737 enum scld_error {
738 SCLD_OK = 0,
739 SCLD_FAILED = -1,
740 SCLD_PERMS = -2,
741 SCLD_EXISTS = -3,
742 SCLD_VANISHED = -4
744 enum scld_error safe_create_leading_directories(char *path);
745 enum scld_error safe_create_leading_directories_const(const char *path);
746 enum scld_error safe_create_leading_directories_no_share(char *path);
748 int mkdir_in_gitdir(const char *path);
750 int git_open_cloexec(const char *name, int flags);
751 #define git_open(name) git_open_cloexec(name, O_RDONLY)
754 * unpack_loose_header() initializes the data stream needed to unpack
755 * a loose object header.
757 * Returns:
759 * - ULHR_OK on success
760 * - ULHR_BAD on error
761 * - ULHR_TOO_LONG if the header was too long
763 * It will only parse up to MAX_HEADER_LEN bytes unless an optional
764 * "hdrbuf" argument is non-NULL. This is intended for use with
765 * OBJECT_INFO_ALLOW_UNKNOWN_TYPE to extract the bad type for (error)
766 * reporting. The full header will be extracted to "hdrbuf" for use
767 * with parse_loose_header(), ULHR_TOO_LONG will still be returned
768 * from this function to indicate that the header was too long.
770 enum unpack_loose_header_result {
771 ULHR_OK,
772 ULHR_BAD,
773 ULHR_TOO_LONG,
775 enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
776 unsigned char *map,
777 unsigned long mapsize,
778 void *buffer,
779 unsigned long bufsiz,
780 struct strbuf *hdrbuf);
783 * parse_loose_header() parses the starting "<type> <len>\0" of an
784 * object. If it doesn't follow that format -1 is returned. To check
785 * the validity of the <type> populate the "typep" in the "struct
786 * object_info". It will be OBJ_BAD if the object type is unknown. The
787 * parsed <len> can be retrieved via "oi->sizep", and from there
788 * passed to unpack_loose_rest().
790 struct object_info;
791 int parse_loose_header(const char *hdr, struct object_info *oi);
794 * With in-core object data in "buf", rehash it to make sure the
795 * object name actually matches "oid" to detect object corruption.
797 * A negative value indicates an error, usually that the OID is not
798 * what we expected, but it might also indicate another error.
800 int check_object_signature(struct repository *r, const struct object_id *oid,
801 void *map, unsigned long size,
802 enum object_type type);
805 * A streaming version of check_object_signature().
806 * Try reading the object named with "oid" using
807 * the streaming interface and rehash it to do the same.
809 int stream_object_signature(struct repository *r, const struct object_id *oid);
811 int finalize_object_file(const char *tmpfile, const char *filename);
813 /* Helper to check and "touch" a file */
814 int check_and_freshen_file(const char *fn, int freshen);
816 /* Convert to/from hex/sha1 representation */
817 #define MINIMUM_ABBREV minimum_abbrev
818 #define DEFAULT_ABBREV default_abbrev
820 /* used when the code does not know or care what the default abbrev is */
821 #define FALLBACK_DEFAULT_ABBREV 7
823 struct object_context {
824 unsigned short mode;
826 * symlink_path is only used by get_tree_entry_follow_symlinks,
827 * and only for symlinks that point outside the repository.
829 struct strbuf symlink_path;
831 * If GET_OID_RECORD_PATH is set, this will record path (if any)
832 * found when resolving the name. The caller is responsible for
833 * releasing the memory.
835 char *path;
838 int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
839 __attribute__((format (printf, 2, 3)))
840 int get_oidf(struct object_id *oid, const char *fmt, ...);
841 int repo_get_oid_commit(struct repository *r, const char *str, struct object_id *oid);
842 int repo_get_oid_committish(struct repository *r, const char *str, struct object_id *oid);
843 int repo_get_oid_tree(struct repository *r, const char *str, struct object_id *oid);
844 int repo_get_oid_treeish(struct repository *r, const char *str, struct object_id *oid);
845 int repo_get_oid_blob(struct repository *r, const char *str, struct object_id *oid);
846 int repo_get_oid_mb(struct repository *r, const char *str, struct object_id *oid);
847 void maybe_die_on_misspelt_object_name(struct repository *repo,
848 const char *name,
849 const char *prefix);
850 enum get_oid_result get_oid_with_context(struct repository *repo, const char *str,
851 unsigned flags, struct object_id *oid,
852 struct object_context *oc);
854 typedef int each_abbrev_fn(const struct object_id *oid, void *);
855 int repo_for_each_abbrev(struct repository *r, const char *prefix, each_abbrev_fn, void *);
857 int set_disambiguate_hint_config(const char *var, const char *value);
860 * This reads short-hand syntax that not only evaluates to a commit
861 * object name, but also can act as if the end user spelled the name
862 * of the branch from the command line.
864 * - "@{-N}" finds the name of the Nth previous branch we were on, and
865 * places the name of the branch in the given buf and returns the
866 * number of characters parsed if successful.
868 * - "<branch>@{upstream}" finds the name of the other ref that
869 * <branch> is configured to merge with (missing <branch> defaults
870 * to the current branch), and places the name of the branch in the
871 * given buf and returns the number of characters parsed if
872 * successful.
874 * If the input is not of the accepted format, it returns a negative
875 * number to signal an error.
877 * If the input was ok but there are not N branch switches in the
878 * reflog, it returns 0.
880 #define INTERPRET_BRANCH_LOCAL (1<<0)
881 #define INTERPRET_BRANCH_REMOTE (1<<1)
882 #define INTERPRET_BRANCH_HEAD (1<<2)
883 struct interpret_branch_name_options {
885 * If "allowed" is non-zero, it is a treated as a bitfield of allowable
886 * expansions: local branches ("refs/heads/"), remote branches
887 * ("refs/remotes/"), or "HEAD". If no "allowed" bits are set, any expansion is
888 * allowed, even ones to refs outside of those namespaces.
890 unsigned allowed;
893 * If ^{upstream} or ^{push} (or equivalent) is requested, and the
894 * branch in question does not have such a reference, return -1 instead
895 * of die()-ing.
897 unsigned nonfatal_dangling_mark : 1;
899 int repo_interpret_branch_name(struct repository *r,
900 const char *str, int len,
901 struct strbuf *buf,
902 const struct interpret_branch_name_options *options);
904 int base_name_compare(const char *name1, size_t len1, int mode1,
905 const char *name2, size_t len2, int mode2);
906 int df_name_compare(const char *name1, size_t len1, int mode1,
907 const char *name2, size_t len2, int mode2);
908 int name_compare(const char *name1, size_t len1, const char *name2, size_t len2);
909 int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2);
911 void *read_object_with_reference(struct repository *r,
912 const struct object_id *oid,
913 enum object_type required_type,
914 unsigned long *size,
915 struct object_id *oid_ret);
917 struct object *repo_peel_to_type(struct repository *r,
918 const char *name, int namelen,
919 struct object *o, enum object_type);
921 const char *git_editor(void);
922 const char *git_sequence_editor(void);
923 const char *git_pager(int stdout_is_tty);
924 int is_terminal_dumb(void);
926 struct cache_def {
927 struct strbuf path;
928 int flags;
929 int track_flags;
930 int prefix_len_stat_func;
932 #define CACHE_DEF_INIT { \
933 .path = STRBUF_INIT, \
935 static inline void cache_def_clear(struct cache_def *cache)
937 strbuf_release(&cache->path);
940 int has_symlink_leading_path(const char *name, int len);
941 int threaded_has_symlink_leading_path(struct cache_def *, const char *, int);
942 int check_leading_path(const char *name, int len, int warn_on_lstat_err);
943 int has_dirs_only_path(const char *name, int len, int prefix_len);
944 void invalidate_lstat_cache(void);
945 void schedule_dir_for_removal(const char *name, int len);
946 void remove_scheduled_dirs(void);
948 struct pack_window {
949 struct pack_window *next;
950 unsigned char *base;
951 off_t offset;
952 size_t len;
953 unsigned int last_used;
954 unsigned int inuse_cnt;
957 struct pack_entry {
958 off_t offset;
959 struct packed_git *p;
963 * Set this to 0 to prevent oid_object_info_extended() from fetching missing
964 * blobs. This has a difference only if extensions.partialClone is set.
966 * Its default value is 1.
968 extern int fetch_if_missing;
970 /* Dumb servers support */
971 int update_server_info(int);
973 extern const char *git_mailmap_file;
974 extern const char *git_mailmap_blob;
976 #define COPY_READ_ERROR (-2)
977 #define COPY_WRITE_ERROR (-3)
978 int copy_fd(int ifd, int ofd);
979 int copy_file(const char *dst, const char *src, int mode);
980 int copy_file_with_time(const char *dst, const char *src, int mode);
982 /* pager.c */
983 void setup_pager(void);
984 int pager_in_use(void);
985 extern int pager_use_color;
986 int term_columns(void);
987 void term_clear_line(void);
988 int decimal_width(uintmax_t);
989 int check_pager_config(const char *cmd);
990 void prepare_pager_args(struct child_process *, const char *pager);
992 /* base85 */
993 int decode_85(char *dst, const char *line, int linelen);
994 void encode_85(char *buf, const unsigned char *data, int bytes);
996 /* pkt-line.c */
997 void packet_trace_identity(const char *prog);
999 /* add */
1001 * return 0 if success, 1 - if addition of a file failed and
1002 * ADD_FILES_IGNORE_ERRORS was specified in flags
1004 int add_files_to_cache(const char *prefix, const struct pathspec *pathspec, int flags);
1006 /* diff.c */
1007 extern int diff_auto_refresh_index;
1009 /* match-trees.c */
1010 void shift_tree(struct repository *, const struct object_id *, const struct object_id *, struct object_id *, int);
1011 void shift_tree_by(struct repository *, const struct object_id *, const struct object_id *, struct object_id *, const char *);
1014 * whitespace rules.
1015 * used by both diff and apply
1016 * last two digits are tab width
1018 #define WS_BLANK_AT_EOL 0100
1019 #define WS_SPACE_BEFORE_TAB 0200
1020 #define WS_INDENT_WITH_NON_TAB 0400
1021 #define WS_CR_AT_EOL 01000
1022 #define WS_BLANK_AT_EOF 02000
1023 #define WS_TAB_IN_INDENT 04000
1024 #define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
1025 #define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)
1026 #define WS_TAB_WIDTH_MASK 077
1027 /* All WS_* -- when extended, adapt diff.c emit_symbol */
1028 #define WS_RULE_MASK 07777
1029 extern unsigned whitespace_rule_cfg;
1030 unsigned whitespace_rule(struct index_state *, const char *);
1031 unsigned parse_whitespace_rule(const char *);
1032 unsigned ws_check(const char *line, int len, unsigned ws_rule);
1033 void ws_check_emit(const char *line, int len, unsigned ws_rule, FILE *stream, const char *set, const char *reset, const char *ws);
1034 char *whitespace_error_string(unsigned ws);
1035 void ws_fix_copy(struct strbuf *, const char *, int, unsigned, int *);
1036 int ws_blank_line(const char *line, int len);
1037 #define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK)
1039 /* ls-files */
1040 void overlay_tree_on_index(struct index_state *istate,
1041 const char *tree_name, const char *prefix);
1043 /* merge.c */
1044 struct commit_list;
1045 int try_merge_command(struct repository *r,
1046 const char *strategy, size_t xopts_nr,
1047 const char **xopts, struct commit_list *common,
1048 const char *head_arg, struct commit_list *remotes);
1049 int checkout_fast_forward(struct repository *r,
1050 const struct object_id *from,
1051 const struct object_id *to,
1052 int overwrite_ignore);
1055 int sane_execvp(const char *file, char *const argv[]);
1058 * A struct to encapsulate the concept of whether a file has changed
1059 * since we last checked it. This uses criteria similar to those used
1060 * for the index.
1062 struct stat_validity {
1063 struct stat_data *sd;
1066 void stat_validity_clear(struct stat_validity *sv);
1069 * Returns 1 if the path is a regular file (or a symlink to a regular
1070 * file) and matches the saved stat_validity, 0 otherwise. A missing
1071 * or inaccessible file is considered a match if the struct was just
1072 * initialized, or if the previous update found an inaccessible file.
1074 int stat_validity_check(struct stat_validity *sv, const char *path);
1077 * Update the stat_validity from a file opened at descriptor fd. If
1078 * the file is missing, inaccessible, or not a regular file, then
1079 * future calls to stat_validity_check will match iff one of those
1080 * conditions continues to be true.
1082 void stat_validity_update(struct stat_validity *sv, int fd);
1084 int versioncmp(const char *s1, const char *s2);
1086 #endif /* CACHE_H */