surround %s with quotes when failed to lookup commit
[git/debian.git] / cache.h
blobbdedb87e83b8bc8b5fd596b00552072191f96bb2
1 #ifndef CACHE_H
2 #define CACHE_H
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"
12 * Basic data structures for the directory cache
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;
22 #define INDEX_FORMAT_LB 2
23 #define INDEX_FORMAT_UB 4
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 */
37 #define CE_STAGEMASK (0x3000)
38 #define CE_EXTENDED (0x4000)
39 #define CE_VALID (0x8000)
40 #define CE_STAGESHIFT 12
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
49 * In-memory only flags
51 #define CE_UPDATE (1 << 16)
52 #define CE_REMOVE (1 << 17)
53 #define CE_UPTODATE (1 << 18)
54 #define CE_ADDED (1 << 19)
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)
61 #define CE_UNPACKED (1 << 24)
62 #define CE_NEW_SKIP_WORKTREE (1 << 25)
64 /* used to temporarily mark paths matched by pathspecs */
65 #define CE_MATCHED (1 << 26)
67 #define CE_UPDATE_IN_BASE (1 << 27)
68 #define CE_STRIP_NAME (1 << 28)
71 * Extended on-disk flags
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)
78 #define CE_EXTENDED_FLAGS (CE_INTENT_TO_ADD | CE_SKIP_WORKTREE)
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
86 #if CE_EXTENDED_FLAGS & 0x803FFFFF
87 #error "CE_EXTENDED_FLAGS out of range"
88 #endif
90 /* Forward structure decls */
91 struct pathspec;
92 struct tree;
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!
98 static inline void copy_cache_entry(struct cache_entry *dst,
99 const struct cache_entry *src)
101 unsigned int state = dst->ce_flags & CE_HASHED;
102 int mem_pool_allocated = dst->mem_pool_allocated;
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));
109 /* Restore the hash state */
110 dst->ce_flags = (dst->ce_flags & ~CE_HASHED) | state;
112 /* Restore the mem_pool_allocated flag */
113 dst->mem_pool_allocated = mem_pool_allocated;
116 static inline unsigned create_ce_flags(unsigned stage)
118 return (stage << CE_STAGESHIFT);
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)
129 static inline unsigned int ce_mode_from_stat(const struct cache_entry *ce,
130 unsigned int mode)
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);
141 return create_ce_mode(mode);
143 static inline int ce_to_dtype(const struct cache_entry *ce)
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;
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)
161 return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
162 S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
165 #define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)
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)
177 struct split_index;
178 struct untracked_cache;
179 struct progress;
180 struct pattern_list;
182 enum sparse_index_mode {
184 * There are no sparse directories in the index at all.
186 * Repositories that don't use cone-mode sparse-checkout will
187 * always have their indexes in this mode.
189 INDEX_EXPANDED = 0,
192 * The index has already been collapsed to sparse directories
193 * whereever possible.
195 INDEX_COLLAPSED,
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.
202 INDEX_PARTIALLY_SPARSE,
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;
233 * A "struct index_state istate" must be initialized with
234 * INDEX_STATE_INIT or the corresponding index_state_init().
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.
242 #define INDEX_STATE_INIT(r) { \
243 .repo = (r), \
245 void index_state_init(struct index_state *istate, struct repository *r);
246 void release_index(struct index_state *istate);
248 /* Name hashing */
249 int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
250 void add_name_hash(struct index_state *istate, struct cache_entry *ce);
251 void remove_name_hash(struct index_state *istate, struct cache_entry *ce);
252 void free_name_hash(struct index_state *istate);
254 /* Cache entry creation and cleanup */
257 * Create cache_entry intended for use in the specified index. Caller
258 * is responsible for discarding the cache_entry with
259 * `discard_cache_entry`.
261 struct cache_entry *make_cache_entry(struct index_state *istate,
262 unsigned int mode,
263 const struct object_id *oid,
264 const char *path,
265 int stage,
266 unsigned int refresh_options);
268 struct cache_entry *make_empty_cache_entry(struct index_state *istate,
269 size_t name_len);
272 * Create a cache_entry that is not intended to be added to an index. If
273 * `ce_mem_pool` is not NULL, the entry is allocated within the given memory
274 * pool. Caller is responsible for discarding "loose" entries with
275 * `discard_cache_entry()` and the memory pool with
276 * `mem_pool_discard(ce_mem_pool, should_validate_cache_entries())`.
278 struct cache_entry *make_transient_cache_entry(unsigned int mode,
279 const struct object_id *oid,
280 const char *path,
281 int stage,
282 struct mem_pool *ce_mem_pool);
284 struct cache_entry *make_empty_transient_cache_entry(size_t len,
285 struct mem_pool *ce_mem_pool);
288 * Discard cache entry.
290 void discard_cache_entry(struct cache_entry *ce);
293 * Check configuration if we should perform extra validation on cache
294 * entries.
296 int should_validate_cache_entries(void);
299 * Duplicate a cache_entry. Allocate memory for the new entry from a
300 * memory_pool. Takes into account cache_entry fields that are meant
301 * for managing the underlying memory allocation of the cache_entry.
303 struct cache_entry *dup_cache_entry(const struct cache_entry *ce, struct index_state *istate);
306 * Validate the cache entries in the index. This is an internal
307 * consistency check that the cache_entry structs are allocated from
308 * the expected memory pool.
310 void validate_cache_entries(const struct index_state *istate);
313 * Bulk prefetch all missing cache entries that are not GITLINKs and that match
314 * the given predicate. This function should only be called if
315 * repo_has_promisor_remote() returns true.
317 typedef int (*must_prefetch_predicate)(const struct cache_entry *);
318 void prefetch_cache_entries(const struct index_state *istate,
319 must_prefetch_predicate must_prefetch);
321 #ifdef USE_THE_INDEX_VARIABLE
322 extern struct index_state the_index;
323 #endif
325 #define INIT_DB_QUIET 0x0001
326 #define INIT_DB_EXIST_OK 0x0002
328 int init_db(const char *git_dir, const char *real_git_dir,
329 const char *template_dir, int hash_algo,
330 const char *initial_branch, unsigned int flags);
331 void initialize_repository_version(int hash_algo, int reinit);
333 /* Initialize and use the cache information */
334 struct lock_file;
335 void preload_index(struct index_state *index,
336 const struct pathspec *pathspec,
337 unsigned int refresh_flags);
338 int do_read_index(struct index_state *istate, const char *path,
339 int must_exist); /* for testting only! */
340 int read_index_from(struct index_state *, const char *path,
341 const char *gitdir);
342 int is_index_unborn(struct index_state *);
344 void ensure_full_index(struct index_state *istate);
346 /* For use with `write_locked_index()`. */
347 #define COMMIT_LOCK (1 << 0)
348 #define SKIP_IF_UNCHANGED (1 << 1)
351 * Write the index while holding an already-taken lock. Close the lock,
352 * and if `COMMIT_LOCK` is given, commit it.
354 * Unless a split index is in use, write the index into the lockfile.
356 * With a split index, write the shared index to a temporary file,
357 * adjust its permissions and rename it into place, then write the
358 * split index to the lockfile. If the temporary file for the shared
359 * index cannot be created, fall back to the behavior described in
360 * the previous paragraph.
362 * With `COMMIT_LOCK`, the lock is always committed or rolled back.
363 * Without it, the lock is closed, but neither committed nor rolled
364 * back.
366 * If `SKIP_IF_UNCHANGED` is given and the index is unchanged, nothing
367 * is written (and the lock is rolled back if `COMMIT_LOCK` is given).
369 int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
371 void discard_index(struct index_state *);
372 void move_index_extensions(struct index_state *dst, struct index_state *src);
373 int unmerged_index(const struct index_state *);
376 * Returns 1 if istate differs from tree, 0 otherwise. If tree is NULL,
377 * compares istate to HEAD. If tree is NULL and on an unborn branch,
378 * returns 1 if there are entries in istate, 0 otherwise. If an strbuf is
379 * provided, the space-separated list of files that differ will be appended
380 * to it.
382 int repo_index_has_changes(struct repository *repo,
383 struct tree *tree,
384 struct strbuf *sb);
386 int verify_path(const char *path, unsigned mode);
387 int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
388 int index_dir_exists(struct index_state *istate, const char *name, int namelen);
389 void adjust_dirname_case(struct index_state *istate, char *name);
390 struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
393 * Searches for an entry defined by name and namelen in the given index.
394 * If the return value is positive (including 0) it is the position of an
395 * exact match. If the return value is negative, the negated value minus 1
396 * is the position where the entry would be inserted.
397 * Example: The current index consists of these files and its stages:
399 * b#0, d#0, f#1, f#3
401 * index_name_pos(&index, "a", 1) -> -1
402 * index_name_pos(&index, "b", 1) -> 0
403 * index_name_pos(&index, "c", 1) -> -2
404 * index_name_pos(&index, "d", 1) -> 1
405 * index_name_pos(&index, "e", 1) -> -3
406 * index_name_pos(&index, "f", 1) -> -3
407 * index_name_pos(&index, "g", 1) -> -5
409 int index_name_pos(struct index_state *, const char *name, int namelen);
412 * Like index_name_pos, returns the position of an entry of the given name in
413 * the index if one exists, otherwise returns a negative value where the negated
414 * value minus 1 is the position where the index entry would be inserted. Unlike
415 * index_name_pos, however, a sparse index is not expanded to find an entry
416 * inside a sparse directory.
418 int index_name_pos_sparse(struct index_state *, const char *name, int namelen);
421 * Determines whether an entry with the given name exists within the
422 * given index. The return value is 1 if an exact match is found, otherwise
423 * it is 0. Note that, unlike index_name_pos, this function does not expand
424 * the index if it is sparse. If an item exists within the full index but it
425 * is contained within a sparse directory (and not in the sparse index), 0 is
426 * returned.
428 int index_entry_exists(struct index_state *, const char *name, int namelen);
431 * Some functions return the negative complement of an insert position when a
432 * precise match was not found but a position was found where the entry would
433 * need to be inserted. This helper protects that logic from any integer
434 * underflow.
436 static inline int index_pos_to_insert_pos(uintmax_t pos)
438 if (pos > INT_MAX)
439 die("overflow: -1 - %"PRIuMAX, pos);
440 return -1 - (int)pos;
443 #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
444 #define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
445 #define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
446 #define ADD_CACHE_JUST_APPEND 8 /* Append only */
447 #define ADD_CACHE_NEW_ONLY 16 /* Do not replace existing ones */
448 #define ADD_CACHE_KEEP_CACHE_TREE 32 /* Do not invalidate cache-tree */
449 #define ADD_CACHE_RENORMALIZE 64 /* Pass along HASH_RENORMALIZE */
450 int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
451 void rename_index_entry_at(struct index_state *, int pos, const char *new_name);
453 /* Remove entry, return true if there are more entries to go. */
454 int remove_index_entry_at(struct index_state *, int pos);
456 void remove_marked_cache_entries(struct index_state *istate, int invalidate);
457 int remove_file_from_index(struct index_state *, const char *path);
458 #define ADD_CACHE_VERBOSE 1
459 #define ADD_CACHE_PRETEND 2
460 #define ADD_CACHE_IGNORE_ERRORS 4
461 #define ADD_CACHE_IGNORE_REMOVAL 8
462 #define ADD_CACHE_INTENT 16
464 * These two are used to add the contents of the file at path
465 * to the index, marking the working tree up-to-date by storing
466 * the cached stat info in the resulting cache entry. A caller
467 * that has already run lstat(2) on the path can call
468 * add_to_index(), and all others can call add_file_to_index();
469 * the latter will do necessary lstat(2) internally before
470 * calling the former.
472 int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
473 int add_file_to_index(struct index_state *, const char *path, int flags);
475 int chmod_index_entry(struct index_state *, struct cache_entry *ce, char flip);
476 int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
477 void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
478 int index_name_is_other(struct index_state *, const char *, int);
479 void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);
481 /* do stat comparison even if CE_VALID is true */
482 #define CE_MATCH_IGNORE_VALID 01
483 /* do not check the contents but report dirty on racily-clean entries */
484 #define CE_MATCH_RACY_IS_DIRTY 02
485 /* do stat comparison even if CE_SKIP_WORKTREE is true */
486 #define CE_MATCH_IGNORE_SKIP_WORKTREE 04
487 /* ignore non-existent files during stat update */
488 #define CE_MATCH_IGNORE_MISSING 0x08
489 /* enable stat refresh */
490 #define CE_MATCH_REFRESH 0x10
491 /* don't refresh_fsmonitor state or do stat comparison even if CE_FSMONITOR_VALID is true */
492 #define CE_MATCH_IGNORE_FSMONITOR 0X20
493 int is_racy_timestamp(const struct index_state *istate,
494 const struct cache_entry *ce);
495 int has_racy_timestamp(struct index_state *istate);
496 int ie_match_stat(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
497 int ie_modified(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
500 * Record to sd the data from st that we use to check whether a file
501 * might have changed.
503 void fill_stat_data(struct stat_data *sd, struct stat *st);
506 * Return 0 if st is consistent with a file not having been changed
507 * since sd was filled. If there are differences, return a
508 * combination of MTIME_CHANGED, CTIME_CHANGED, OWNER_CHANGED,
509 * INODE_CHANGED, and DATA_CHANGED.
511 int match_stat_data(const struct stat_data *sd, struct stat *st);
512 int match_stat_data_racy(const struct index_state *istate,
513 const struct stat_data *sd, struct stat *st);
515 void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st);
517 #define REFRESH_REALLY (1 << 0) /* ignore_valid */
518 #define REFRESH_UNMERGED (1 << 1) /* allow unmerged */
519 #define REFRESH_QUIET (1 << 2) /* be quiet about it */
520 #define REFRESH_IGNORE_MISSING (1 << 3) /* ignore non-existent */
521 #define REFRESH_IGNORE_SUBMODULES (1 << 4) /* ignore submodules */
522 #define REFRESH_IN_PORCELAIN (1 << 5) /* user friendly output, not "needs update" */
523 #define REFRESH_PROGRESS (1 << 6) /* show progress bar if stderr is tty */
524 #define REFRESH_IGNORE_SKIP_WORKTREE (1 << 7) /* ignore skip_worktree entries */
525 int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
527 * Refresh the index and write it to disk.
529 * 'refresh_flags' is passed directly to 'refresh_index()', while
530 * 'COMMIT_LOCK | write_flags' is passed to 'write_locked_index()', so
531 * the lockfile is always either committed or rolled back.
533 * If 'gentle' is passed, errors locking the index are ignored.
535 * Return 1 if refreshing the index returns an error, -1 if writing
536 * the index to disk fails, 0 on success.
538 * Note that if refreshing the index returns an error, we still write
539 * out the index (unless locking fails).
541 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);
543 struct cache_entry *refresh_cache_entry(struct index_state *, struct cache_entry *, unsigned int);
545 void set_alternate_index_output(const char *);
547 extern int verify_index_checksum;
548 extern int verify_ce_order;
550 #define MTIME_CHANGED 0x0001
551 #define CTIME_CHANGED 0x0002
552 #define OWNER_CHANGED 0x0004
553 #define MODE_CHANGED 0x0008
554 #define INODE_CHANGED 0x0010
555 #define DATA_CHANGED 0x0020
556 #define TYPE_CHANGED 0x0040
558 int cmp_cache_name_compare(const void *a_, const void *b_);
560 /* add */
562 * return 0 if success, 1 - if addition of a file failed and
563 * ADD_FILES_IGNORE_ERRORS was specified in flags
565 int add_files_to_cache(const char *prefix, const struct pathspec *pathspec, int flags);
567 /* diff.c */
568 extern int diff_auto_refresh_index;
570 /* ls-files */
571 void overlay_tree_on_index(struct index_state *istate,
572 const char *tree_name, const char *prefix);
574 /* merge.c */
575 struct commit_list;
576 int try_merge_command(struct repository *r,
577 const char *strategy, size_t xopts_nr,
578 const char **xopts, struct commit_list *common,
579 const char *head_arg, struct commit_list *remotes);
580 int checkout_fast_forward(struct repository *r,
581 const struct object_id *from,
582 const struct object_id *to,
583 int overwrite_ignore);
586 int sane_execvp(const char *file, char *const argv[]);
589 * A struct to encapsulate the concept of whether a file has changed
590 * since we last checked it. This uses criteria similar to those used
591 * for the index.
593 struct stat_validity {
594 struct stat_data *sd;
597 void stat_validity_clear(struct stat_validity *sv);
600 * Returns 1 if the path is a regular file (or a symlink to a regular
601 * file) and matches the saved stat_validity, 0 otherwise. A missing
602 * or inaccessible file is considered a match if the struct was just
603 * initialized, or if the previous update found an inaccessible file.
605 int stat_validity_check(struct stat_validity *sv, const char *path);
608 * Update the stat_validity from a file opened at descriptor fd. If
609 * the file is missing, inaccessible, or not a regular file, then
610 * future calls to stat_validity_check will match iff one of those
611 * conditions continues to be true.
613 void stat_validity_update(struct stat_validity *sv, int fd);
615 #endif /* CACHE_H */