]> git.ipfire.org Git - thirdparty/git.git/blob - cache.h
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / cache.h
1 #ifndef CACHE_H
2 #define CACHE_H
3
4 #include "git-compat-util.h"
5 #include "strbuf.h"
6 #include "hashmap.h"
7 #include "list.h"
8 #include "gettext.h"
9 #include "string-list.h"
10 #include "hash.h"
11 #include "path.h"
12 #include "pathspec.h"
13 #include "object.h"
14 #include "repository.h"
15 #include "statinfo.h"
16
17 typedef 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
27 void git_inflate_init(git_zstream *);
28 void git_inflate_init_gzip_only(git_zstream *);
29 void git_inflate_end(git_zstream *);
30 int git_inflate(git_zstream *, int flush);
31
32 void git_deflate_init(git_zstream *, int level);
33 void git_deflate_init_gzip(git_zstream *, int level);
34 void git_deflate_init_raw(git_zstream *, int level);
35 void git_deflate_end(git_zstream *);
36 int git_deflate_abort(git_zstream *);
37 int git_deflate_end_gently(git_zstream *);
38 int git_deflate(git_zstream *, int flush);
39 unsigned long git_deflate_bound(git_zstream *, unsigned long);
40
41 #if defined(DT_UNKNOWN) && !defined(NO_D_TYPE_IN_DIRENT)
42 #define DTYPE(de) ((de)->d_type)
43 #else
44 #undef DT_UNKNOWN
45 #undef DT_DIR
46 #undef DT_REG
47 #undef DT_LNK
48 #define DT_UNKNOWN 0
49 #define DT_DIR 1
50 #define DT_REG 2
51 #define DT_LNK 3
52 #define DTYPE(de) DT_UNKNOWN
53 #endif
54
55 /* unknown mode (impossible combination S_IFIFO|S_IFCHR) */
56 #define S_IFINVALID 0030000
57
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
63 */
64 #define S_IFGITLINK 0160000
65 #define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK)
66
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
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.
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
100 */
101 #define DEFAULT_GIT_PORT 9418
102
103 /*
104 * Basic data structures for the directory cache
105 */
106
107 #define CACHE_SIGNATURE 0x44495243 /* "DIRC" */
108 struct cache_header {
109 uint32_t hdr_signature;
110 uint32_t hdr_version;
111 uint32_t hdr_entries;
112 };
113
114 #define INDEX_FORMAT_LB 2
115 #define INDEX_FORMAT_UB 4
116
117 struct cache_entry {
118 struct hashmap_entry ent;
119 struct stat_data ce_stat_data;
120 unsigned int ce_mode;
121 unsigned int ce_flags;
122 unsigned int mem_pool_allocated;
123 unsigned int ce_namelen;
124 unsigned int index; /* for link extension */
125 struct object_id oid;
126 char name[FLEX_ARRAY]; /* more */
127 };
128
129 #define CE_STAGEMASK (0x3000)
130 #define CE_EXTENDED (0x4000)
131 #define CE_VALID (0x8000)
132 #define CE_STAGESHIFT 12
133
134 /*
135 * Range 0xFFFF0FFF in ce_flags is divided into
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 */
143 #define CE_UPDATE (1 << 16)
144 #define CE_REMOVE (1 << 17)
145 #define CE_UPTODATE (1 << 18)
146 #define CE_ADDED (1 << 19)
147
148 #define CE_HASHED (1 << 20)
149 #define CE_FSMONITOR_VALID (1 << 21)
150 #define CE_WT_REMOVE (1 << 22) /* remove in work directory */
151 #define CE_CONFLICTED (1 << 23)
152
153 #define CE_UNPACKED (1 << 24)
154 #define CE_NEW_SKIP_WORKTREE (1 << 25)
155
156 /* used to temporarily mark paths matched by pathspecs */
157 #define CE_MATCHED (1 << 26)
158
159 #define CE_UPDATE_IN_BASE (1 << 27)
160 #define CE_STRIP_NAME (1 << 28)
161
162 /*
163 * Extended on-disk flags
164 */
165 #define CE_INTENT_TO_ADD (1 << 29)
166 #define CE_SKIP_WORKTREE (1 << 30)
167 /* CE_EXTENDED2 is for future extension */
168 #define CE_EXTENDED2 (1U << 31)
169
170 #define CE_EXTENDED_FLAGS (CE_INTENT_TO_ADD | CE_SKIP_WORKTREE)
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
182 #define S_ISSPARSEDIR(m) ((m) == S_IFDIR)
183
184 /* Forward structure decls */
185 struct pathspec;
186 struct child_process;
187 struct tree;
188
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 */
193 static inline void copy_cache_entry(struct cache_entry *dst,
194 const struct cache_entry *src)
195 {
196 unsigned int state = dst->ce_flags & CE_HASHED;
197 int mem_pool_allocated = dst->mem_pool_allocated;
198
199 /* Don't copy hash chain and name */
200 memcpy(&dst->ce_stat_data, &src->ce_stat_data,
201 offsetof(struct cache_entry, name) -
202 offsetof(struct cache_entry, ce_stat_data));
203
204 /* Restore the hash state */
205 dst->ce_flags = (dst->ce_flags & ~CE_HASHED) | state;
206
207 /* Restore the mem_pool_allocated flag */
208 dst->mem_pool_allocated = mem_pool_allocated;
209 }
210
211 static inline unsigned create_ce_flags(unsigned stage)
212 {
213 return (stage << CE_STAGESHIFT);
214 }
215
216 #define ce_namelen(ce) ((ce)->ce_namelen)
217 #define ce_size(ce) cache_entry_size(ce_namelen(ce))
218 #define ce_stage(ce) ((CE_STAGEMASK & (ce)->ce_flags) >> CE_STAGESHIFT)
219 #define ce_uptodate(ce) ((ce)->ce_flags & CE_UPTODATE)
220 #define ce_skip_worktree(ce) ((ce)->ce_flags & CE_SKIP_WORKTREE)
221 #define ce_mark_uptodate(ce) ((ce)->ce_flags |= CE_UPTODATE)
222 #define ce_intent_to_add(ce) ((ce)->ce_flags & CE_INTENT_TO_ADD)
223
224 #define ce_permissions(mode) (((mode) & 0100) ? 0755 : 0644)
225 static inline unsigned int create_ce_mode(unsigned int mode)
226 {
227 if (S_ISLNK(mode))
228 return S_IFLNK;
229 if (S_ISSPARSEDIR(mode))
230 return S_IFDIR;
231 if (S_ISDIR(mode) || S_ISGITLINK(mode))
232 return S_IFGITLINK;
233 return S_IFREG | ce_permissions(mode);
234 }
235 static inline unsigned int ce_mode_from_stat(const struct cache_entry *ce,
236 unsigned int mode)
237 {
238 extern int trust_executable_bit, has_symlinks;
239 if (!has_symlinks && S_ISREG(mode) &&
240 ce && S_ISLNK(ce->ce_mode))
241 return ce->ce_mode;
242 if (!trust_executable_bit && S_ISREG(mode)) {
243 if (ce && S_ISREG(ce->ce_mode))
244 return ce->ce_mode;
245 return create_ce_mode(0666);
246 }
247 return create_ce_mode(mode);
248 }
249 static 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 }
261 static 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 }
271
272 static 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
281 #define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)
282
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)
287 #define RESOLVE_UNDO_CHANGED (1 << 4)
288 #define CACHE_TREE_CHANGED (1 << 5)
289 #define SPLIT_INDEX_ORDERED (1 << 6)
290 #define UNTRACKED_CHANGED (1 << 7)
291 #define FSMONITOR_CHANGED (1 << 8)
292
293 struct split_index;
294 struct untracked_cache;
295 struct progress;
296 struct pattern_list;
297
298 enum 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
321 struct index_state {
322 struct cache_entry **cache;
323 unsigned int version;
324 unsigned int cache_nr, cache_alloc, cache_changed;
325 struct string_list *resolve_undo;
326 struct cache_tree *cache_tree;
327 struct split_index *split_index;
328 struct cache_time timestamp;
329 unsigned name_hash_initialized : 1,
330 initialized : 1,
331 drop_cache_tree : 1,
332 updated_workdir : 1,
333 updated_skipworktree : 1,
334 fsmonitor_has_run_once : 1;
335 enum sparse_index_mode sparse_index;
336 struct hashmap name_hash;
337 struct hashmap dir_hash;
338 struct object_id oid;
339 struct untracked_cache *untracked;
340 char *fsmonitor_last_update;
341 struct ewah_bitmap *fsmonitor_dirty;
342 struct mem_pool *ce_mem_pool;
343 struct progress *progress;
344 struct repository *repo;
345 struct pattern_list *sparse_checkout_patterns;
346 };
347
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
355 * the end. The discard_index() will use its own "istate->repo" as the
356 * "r" argument to index_state_init() in that case.
357 */
358 #define INDEX_STATE_INIT(r) { \
359 .repo = (r), \
360 }
361 void index_state_init(struct index_state *istate, struct repository *r);
362 void release_index(struct index_state *istate);
363
364 /* Name hashing */
365 int test_lazy_init_name_hash(struct index_state *istate, int try_threaded);
366 void add_name_hash(struct index_state *istate, struct cache_entry *ce);
367 void remove_name_hash(struct index_state *istate, struct cache_entry *ce);
368 void free_name_hash(struct index_state *istate);
369
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 */
377 struct 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
384 struct cache_entry *make_empty_cache_entry(struct index_state *istate,
385 size_t name_len);
386
387 /*
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())`.
393 */
394 struct cache_entry *make_transient_cache_entry(unsigned int mode,
395 const struct object_id *oid,
396 const char *path,
397 int stage,
398 struct mem_pool *ce_mem_pool);
399
400 struct cache_entry *make_empty_transient_cache_entry(size_t len,
401 struct mem_pool *ce_mem_pool);
402
403 /*
404 * Discard cache entry.
405 */
406 void discard_cache_entry(struct cache_entry *ce);
407
408 /*
409 * Check configuration if we should perform extra validation on cache
410 * entries.
411 */
412 int should_validate_cache_entries(void);
413
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 */
419 struct 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 */
426 void validate_cache_entries(const struct index_state *istate);
427
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
431 * repo_has_promisor_remote() returns true.
432 */
433 typedef int (*must_prefetch_predicate)(const struct cache_entry *);
434 void prefetch_cache_entries(const struct index_state *istate,
435 must_prefetch_predicate must_prefetch);
436
437 #ifdef USE_THE_INDEX_VARIABLE
438 extern struct index_state the_index;
439 #endif
440
441 static 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
448 #define INIT_DB_QUIET 0x0001
449 #define INIT_DB_EXIST_OK 0x0002
450
451 int init_db(const char *git_dir, const char *real_git_dir,
452 const char *template_dir, int hash_algo,
453 const char *initial_branch, unsigned int flags);
454 void initialize_repository_version(int hash_algo, int reinit);
455
456 /* Initialize and use the cache information */
457 struct lock_file;
458 void preload_index(struct index_state *index,
459 const struct pathspec *pathspec,
460 unsigned int refresh_flags);
461 int do_read_index(struct index_state *istate, const char *path,
462 int must_exist); /* for testting only! */
463 int read_index_from(struct index_state *, const char *path,
464 const char *gitdir);
465 int is_index_unborn(struct index_state *);
466
467 void ensure_full_index(struct index_state *istate);
468
469 /* For use with `write_locked_index()`. */
470 #define COMMIT_LOCK (1 << 0)
471 #define SKIP_IF_UNCHANGED (1 << 1)
472
473 /*
474 * Write the index while holding an already-taken lock. Close the lock,
475 * and if `COMMIT_LOCK` is given, commit it.
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.
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.
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).
491 */
492 int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);
493
494 void discard_index(struct index_state *);
495 void move_index_extensions(struct index_state *dst, struct index_state *src);
496 int unmerged_index(const struct index_state *);
497
498 /**
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.
504 */
505 int repo_index_has_changes(struct repository *repo,
506 struct tree *tree,
507 struct strbuf *sb);
508
509 int verify_path(const char *path, unsigned mode);
510 int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
511 int index_dir_exists(struct index_state *istate, const char *name, int namelen);
512 void adjust_dirname_case(struct index_state *istate, char *name);
513 struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
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 */
532 int index_name_pos(struct index_state *, const char *name, int namelen);
533
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 */
541 int index_name_pos_sparse(struct index_state *, const char *name, int namelen);
542
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 */
551 int index_entry_exists(struct index_state *, const char *name, int namelen);
552
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 */
559 static 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
566 #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
567 #define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
568 #define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
569 #define ADD_CACHE_JUST_APPEND 8 /* Append only */
570 #define ADD_CACHE_NEW_ONLY 16 /* Do not replace existing ones */
571 #define ADD_CACHE_KEEP_CACHE_TREE 32 /* Do not invalidate cache-tree */
572 #define ADD_CACHE_RENORMALIZE 64 /* Pass along HASH_RENORMALIZE */
573 int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
574 void rename_index_entry_at(struct index_state *, int pos, const char *new_name);
575
576 /* Remove entry, return true if there are more entries to go. */
577 int remove_index_entry_at(struct index_state *, int pos);
578
579 void remove_marked_cache_entries(struct index_state *istate, int invalidate);
580 int remove_file_from_index(struct index_state *, const char *path);
581 #define ADD_CACHE_VERBOSE 1
582 #define ADD_CACHE_PRETEND 2
583 #define ADD_CACHE_IGNORE_ERRORS 4
584 #define ADD_CACHE_IGNORE_REMOVAL 8
585 #define ADD_CACHE_INTENT 16
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 */
595 int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
596 int add_file_to_index(struct index_state *, const char *path, int flags);
597
598 int chmod_index_entry(struct index_state *, struct cache_entry *ce, char flip);
599 int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
600 void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
601 int index_name_is_other(struct index_state *, const char *, int);
602 void *read_blob_data_from_index(struct index_state *, const char *, unsigned long *);
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 */
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
610 /* ignore non-existent files during stat update */
611 #define CE_MATCH_IGNORE_MISSING 0x08
612 /* enable stat refresh */
613 #define CE_MATCH_REFRESH 0x10
614 /* don't refresh_fsmonitor state or do stat comparison even if CE_FSMONITOR_VALID is true */
615 #define CE_MATCH_IGNORE_FSMONITOR 0X20
616 int is_racy_timestamp(const struct index_state *istate,
617 const struct cache_entry *ce);
618 int has_racy_timestamp(struct index_state *istate);
619 int ie_match_stat(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
620 int ie_modified(struct index_state *, const struct cache_entry *, struct stat *, unsigned int);
621
622 #define HASH_WRITE_OBJECT 1
623 #define HASH_FORMAT_CHECK 2
624 #define HASH_RENORMALIZE 4
625 #define HASH_SILENT 8
626 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);
627 int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
628
629 /*
630 * Record to sd the data from st that we use to check whether a file
631 * might have changed.
632 */
633 void fill_stat_data(struct stat_data *sd, struct stat *st);
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 */
641 int match_stat_data(const struct stat_data *sd, struct stat *st);
642 int match_stat_data_racy(const struct index_state *istate,
643 const struct stat_data *sd, struct stat *st);
644
645 void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st);
646
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 */
655 int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
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 */
671 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);
672
673 struct cache_entry *refresh_cache_entry(struct index_state *, struct cache_entry *, unsigned int);
674
675 void set_alternate_index_output(const char *);
676
677 extern int verify_index_checksum;
678 extern int verify_ce_order;
679
680 extern int quote_path_fully;
681
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
688 #define TYPE_CHANGED 0x0040
689
690 /*
691 * Create the directory containing the named path, using care to be
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.
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.
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
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.
711 */
712 enum scld_error {
713 SCLD_OK = 0,
714 SCLD_FAILED = -1,
715 SCLD_PERMS = -2,
716 SCLD_EXISTS = -3,
717 SCLD_VANISHED = -4
718 };
719 enum scld_error safe_create_leading_directories(char *path);
720 enum scld_error safe_create_leading_directories_const(const char *path);
721 enum scld_error safe_create_leading_directories_no_share(char *path);
722
723 int mkdir_in_gitdir(const char *path);
724
725 int git_open_cloexec(const char *name, int flags);
726 #define git_open(name) git_open_cloexec(name, O_RDONLY)
727
728 /**
729 * unpack_loose_header() initializes the data stream needed to unpack
730 * a loose object header.
731 *
732 * Returns:
733 *
734 * - ULHR_OK on success
735 * - ULHR_BAD on error
736 * - ULHR_TOO_LONG if the header was too long
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
742 * with parse_loose_header(), ULHR_TOO_LONG will still be returned
743 * from this function to indicate that the header was too long.
744 */
745 enum unpack_loose_header_result {
746 ULHR_OK,
747 ULHR_BAD,
748 ULHR_TOO_LONG,
749 };
750 enum 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
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 */
765 struct object_info;
766 int parse_loose_header(const char *hdr, struct object_info *oi);
767
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.
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.
774 */
775 int check_object_signature(struct repository *r, const struct object_id *oid,
776 void *map, unsigned long size,
777 enum object_type type);
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 */
784 int stream_object_signature(struct repository *r, const struct object_id *oid);
785
786 int finalize_object_file(const char *tmpfile, const char *filename);
787
788 /* Helper to check and "touch" a file */
789 int check_and_freshen_file(const char *fn, int freshen);
790
791 int base_name_compare(const char *name1, size_t len1, int mode1,
792 const char *name2, size_t len2, int mode2);
793 int df_name_compare(const char *name1, size_t len1, int mode1,
794 const char *name2, size_t len2, int mode2);
795 int name_compare(const char *name1, size_t len1, const char *name2, size_t len2);
796 int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2);
797
798 void *read_object_with_reference(struct repository *r,
799 const struct object_id *oid,
800 enum object_type required_type,
801 unsigned long *size,
802 struct object_id *oid_ret);
803
804 const char *git_editor(void);
805 const char *git_sequence_editor(void);
806 const char *git_pager(int stdout_is_tty);
807 int is_terminal_dumb(void);
808
809 struct cache_def {
810 struct strbuf path;
811 int flags;
812 int track_flags;
813 int prefix_len_stat_func;
814 };
815 #define CACHE_DEF_INIT { \
816 .path = STRBUF_INIT, \
817 }
818 static inline void cache_def_clear(struct cache_def *cache)
819 {
820 strbuf_release(&cache->path);
821 }
822
823 int has_symlink_leading_path(const char *name, int len);
824 int threaded_has_symlink_leading_path(struct cache_def *, const char *, int);
825 int check_leading_path(const char *name, int len, int warn_on_lstat_err);
826 int has_dirs_only_path(const char *name, int len, int prefix_len);
827 void invalidate_lstat_cache(void);
828 void schedule_dir_for_removal(const char *name, int len);
829 void remove_scheduled_dirs(void);
830
831 struct 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
840 struct pack_entry {
841 off_t offset;
842 struct packed_git *p;
843 };
844
845 /*
846 * Set this to 0 to prevent oid_object_info_extended() from fetching missing
847 * blobs. This has a difference only if extensions.partialClone is set.
848 *
849 * Its default value is 1.
850 */
851 extern int fetch_if_missing;
852
853 /* Dumb servers support */
854 int update_server_info(int);
855
856 extern const char *git_mailmap_file;
857 extern const char *git_mailmap_blob;
858
859 #define COPY_READ_ERROR (-2)
860 #define COPY_WRITE_ERROR (-3)
861 int copy_fd(int ifd, int ofd);
862 int copy_file(const char *dst, const char *src, int mode);
863 int copy_file_with_time(const char *dst, const char *src, int mode);
864
865 /* pager.c */
866 void setup_pager(void);
867 int pager_in_use(void);
868 extern int pager_use_color;
869 int term_columns(void);
870 void term_clear_line(void);
871 int decimal_width(uintmax_t);
872 int check_pager_config(const char *cmd);
873 void prepare_pager_args(struct child_process *, const char *pager);
874
875 /* base85 */
876 int decode_85(char *dst, const char *line, int linelen);
877 void encode_85(char *buf, const unsigned char *data, int bytes);
878
879 /* pkt-line.c */
880 void packet_trace_identity(const char *prog);
881
882 /* add */
883 /*
884 * return 0 if success, 1 - if addition of a file failed and
885 * ADD_FILES_IGNORE_ERRORS was specified in flags
886 */
887 int add_files_to_cache(const char *prefix, const struct pathspec *pathspec, int flags);
888
889 /* diff.c */
890 extern int diff_auto_refresh_index;
891
892 /* match-trees.c */
893 void shift_tree(struct repository *, const struct object_id *, const struct object_id *, struct object_id *, int);
894 void shift_tree_by(struct repository *, const struct object_id *, const struct object_id *, struct object_id *, const char *);
895
896 /*
897 * whitespace rules.
898 * used by both diff and apply
899 * last two digits are tab width
900 */
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
907 #define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
908 #define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)
909 #define WS_TAB_WIDTH_MASK 077
910 /* All WS_* -- when extended, adapt diff.c emit_symbol */
911 #define WS_RULE_MASK 07777
912 extern unsigned whitespace_rule_cfg;
913 unsigned whitespace_rule(struct index_state *, const char *);
914 unsigned parse_whitespace_rule(const char *);
915 unsigned ws_check(const char *line, int len, unsigned ws_rule);
916 void ws_check_emit(const char *line, int len, unsigned ws_rule, FILE *stream, const char *set, const char *reset, const char *ws);
917 char *whitespace_error_string(unsigned ws);
918 void ws_fix_copy(struct strbuf *, const char *, int, unsigned, int *);
919 int ws_blank_line(const char *line, int len);
920 #define ws_tab_width(rule) ((rule) & WS_TAB_WIDTH_MASK)
921
922 /* ls-files */
923 void overlay_tree_on_index(struct index_state *istate,
924 const char *tree_name, const char *prefix);
925
926 /* merge.c */
927 struct commit_list;
928 int try_merge_command(struct repository *r,
929 const char *strategy, size_t xopts_nr,
930 const char **xopts, struct commit_list *common,
931 const char *head_arg, struct commit_list *remotes);
932 int checkout_fast_forward(struct repository *r,
933 const struct object_id *from,
934 const struct object_id *to,
935 int overwrite_ignore);
936
937
938 int sane_execvp(const char *file, char *const argv[]);
939
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 */
945 struct stat_validity {
946 struct stat_data *sd;
947 };
948
949 void 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 */
957 int 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 */
965 void stat_validity_update(struct stat_validity *sv, int fd);
966
967 int versioncmp(const char *s1, const char *s2);
968
969 #endif /* CACHE_H */