2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
7 #ifndef INCLUDE_index_h__
8 #define INCLUDE_index_h__
14 #include "tree-cache.h"
16 #include "git2/index.h"
18 #define GIT_INDEX_FILE "index"
19 #define GIT_INDEX_FILE_MODE 0666
24 char *index_file_path
;
25 git_futils_filestamp stamp
;
26 git_oid checksum
; /* checksum at the end of the file */
29 git_idxmap
*entries_map
;
31 git_mutex lock
; /* lock held while entries is being changed */
32 git_vector deleted
; /* deleted entries if readers > 0 */
33 git_atomic readers
; /* number of active iterators */
35 unsigned int on_disk
:1;
36 unsigned int ignore_case
:1;
37 unsigned int distrust_filemode
:1;
38 unsigned int no_symlinks
:1;
46 git_vector_cmp entries_cmp_path
;
47 git_vector_cmp entries_search
;
48 git_vector_cmp entries_search_path
;
49 git_vector_cmp reuc_search
;
52 struct git_index_conflict_iterator
{
57 extern void git_index_entry__init_from_stat(
58 git_index_entry
*entry
, struct stat
*st
, bool trust_mode
);
60 /* Index entry comparison functions for array sorting */
61 extern int git_index_entry_cmp(const void *a
, const void *b
);
62 extern int git_index_entry_icmp(const void *a
, const void *b
);
64 /* Index entry search functions for search using a search spec */
65 extern int git_index_entry_srch(const void *a
, const void *b
);
66 extern int git_index_entry_isrch(const void *a
, const void *b
);
68 /* Index time handling functions */
69 GIT_INLINE(bool) git_index_time_eq(const git_index_time
*one
, const git_index_time
*two
)
71 if (one
->seconds
!= two
->seconds
)
75 if (one
->nanoseconds
!= two
->nanoseconds
)
83 * Test if the given index time is newer than the given existing index entry.
84 * If the timestamps are exactly equivalent, then the given index time is
85 * considered "racily newer" than the existing index entry.
87 GIT_INLINE(bool) git_index_entry_newer_than_index(
88 const git_index_entry
*entry
, git_index
*index
)
90 /* If we never read the index, we can't have this race either */
91 if (!index
|| index
->stamp
.mtime
.tv_sec
== 0)
94 /* If the timestamp is the same or newer than the index, it's racy */
95 #if defined(GIT_USE_NSEC)
96 if ((int32_t)index
->stamp
.tv_sec
< entry
->mtime
.seconds
)
98 else if ((int32_t)index
->stamp
.mtime
.tv_sec
> entry
->mtime
.seconds
)
101 return (uint32_t)index
->stamp
.mtime
.tv_nsec
<= entry
->mtime
.nanoseconds
;
103 return ((int32_t)index
->stamp
.mtime
.tv_sec
) <= entry
->mtime
.seconds
;
107 /* Search index for `path`, returning GIT_ENOTFOUND if it does not exist
108 * (but not setting an error message).
110 * `at_pos` is set to the position where it is or would be inserted.
111 * Pass `path_len` as strlen of path or 0 to call strlen internally.
113 extern int git_index__find_pos(
114 size_t *at_pos
, git_index
*index
, const char *path
, size_t path_len
, int stage
);
116 extern int git_index__fill(git_index
*index
, const git_vector
*source_entries
);
118 extern void git_index__set_ignore_case(git_index
*index
, bool ignore_case
);
120 extern unsigned int git_index__create_mode(unsigned int mode
);
122 GIT_INLINE(const git_futils_filestamp
*) git_index__filestamp(git_index
*index
)
124 return &index
->stamp
;
127 extern int git_index__changed_relative_to(git_index
*index
, const git_oid
*checksum
);
129 /* Copy the current entries vector *and* increment the index refcount.
130 * Call `git_index__release_snapshot` when done.
132 extern int git_index_snapshot_new(git_vector
*snap
, git_index
*index
);
133 extern void git_index_snapshot_release(git_vector
*snap
, git_index
*index
);
135 /* Allow searching in a snapshot; entries must already be sorted! */
136 extern int git_index_snapshot_find(
137 size_t *at_pos
, git_vector
*snap
, git_vector_cmp entry_srch
,
138 const char *path
, size_t path_len
, int stage
);
140 /* Replace an index with a new index */
141 int git_index_read_index(git_index
*index
, const git_index
*new_index
);
146 unsigned int should_write
:1;
149 #define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT }
151 /* Lock the index for eventual writing. */
152 extern int git_indexwriter_init(git_indexwriter
*writer
, git_index
*index
);
154 /* Lock the index for eventual writing by a repository operation: a merge,
155 * revert, cherry-pick or a rebase. Note that the given checkout strategy
156 * will be updated for the operation's use so that checkout will not write
159 extern int git_indexwriter_init_for_operation(
160 git_indexwriter
*writer
,
161 git_repository
*repo
,
162 unsigned int *checkout_strategy
);
164 /* Write the index and unlock it. */
165 extern int git_indexwriter_commit(git_indexwriter
*writer
);
167 /* Cleanup an index writing session, unlocking the file (if it is still
168 * locked and freeing any data structures.
170 extern void git_indexwriter_cleanup(git_indexwriter
*writer
);