]>
Commit | Line | Data |
---|---|---|
bb742ede | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
bb742ede VM |
3 | * |
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. | |
6 | */ | |
68535125 VM |
7 | #ifndef INCLUDE_index_h__ |
8 | #define INCLUDE_index_h__ | |
9 | ||
eae0bfdc PP |
10 | #include "common.h" |
11 | ||
22a2d3d5 | 12 | #include "futils.h" |
817c2820 | 13 | #include "filebuf.h" |
c4034e63 | 14 | #include "vector.h" |
af1d5239 | 15 | #include "idxmap.h" |
b4171320 | 16 | #include "tree-cache.h" |
44908fe7 VM |
17 | #include "git2/odb.h" |
18 | #include "git2/index.h" | |
68535125 | 19 | |
01ad7b3a BR |
20 | #define GIT_INDEX_FILE "index" |
21 | #define GIT_INDEX_FILE_MODE 0666 | |
22 | ||
ac3d33df JK |
23 | extern bool git_index__enforce_unsaved_safety; |
24 | ||
68535125 | 25 | struct git_index { |
9462c471 VM |
26 | git_refcount rc; |
27 | ||
68535125 | 28 | char *index_file_path; |
8ff0f325 | 29 | git_futils_filestamp stamp; |
e579e0f7 | 30 | unsigned char checksum[GIT_HASH_SHA1_SIZE]; |
dac16048 | 31 | |
c4034e63 | 32 | git_vector entries; |
af1d5239 | 33 | git_idxmap *entries_map; |
68535125 | 34 | |
dac16048 | 35 | git_vector deleted; /* deleted entries if readers > 0 */ |
c25aa7cd | 36 | git_atomic32 readers; /* number of active iterators */ |
da825c92 | 37 | |
dac16048 | 38 | unsigned int on_disk:1; |
da825c92 RB |
39 | unsigned int ignore_case:1; |
40 | unsigned int distrust_filemode:1; | |
41 | unsigned int no_symlinks:1; | |
ac3d33df | 42 | unsigned int dirty:1; /* whether we have unsaved changes */ |
da825c92 | 43 | |
b4171320 | 44 | git_tree_cache *tree; |
19c88310 | 45 | git_pool tree_pool; |
4c0b6a6d | 46 | |
0462fba5 | 47 | git_vector names; |
f45ec1a0 | 48 | git_vector reuc; |
ec40b7f9 | 49 | |
f45ec1a0 | 50 | git_vector_cmp entries_cmp_path; |
ec40b7f9 | 51 | git_vector_cmp entries_search; |
f45ec1a0 ET |
52 | git_vector_cmp entries_search_path; |
53 | git_vector_cmp reuc_search; | |
5625d86b DT |
54 | |
55 | unsigned int version; | |
68535125 VM |
56 | }; |
57 | ||
ac3d33df JK |
58 | struct git_index_iterator { |
59 | git_index *index; | |
60 | git_vector snap; | |
61 | size_t cur; | |
62 | }; | |
63 | ||
0e0108f7 ET |
64 | struct git_index_conflict_iterator { |
65 | git_index *index; | |
66 | size_t cur; | |
67 | }; | |
68 | ||
d2ce27dd | 69 | extern void git_index_entry__init_from_stat( |
14997dc5 | 70 | git_index_entry *entry, struct stat *st, bool trust_mode); |
7c7ff7d1 | 71 | |
3b4c401a RB |
72 | /* Index entry comparison functions for array sorting */ |
73 | extern int git_index_entry_cmp(const void *a, const void *b); | |
74 | extern int git_index_entry_icmp(const void *a, const void *b); | |
75 | ||
76 | /* Index entry search functions for search using a search spec */ | |
77 | extern int git_index_entry_srch(const void *a, const void *b); | |
78 | extern int git_index_entry_isrch(const void *a, const void *b); | |
55cbd05b | 79 | |
25e84f95 ET |
80 | /* Index time handling functions */ |
81 | GIT_INLINE(bool) git_index_time_eq(const git_index_time *one, const git_index_time *two) | |
82 | { | |
83 | if (one->seconds != two->seconds) | |
84 | return false; | |
85 | ||
86 | #ifdef GIT_USE_NSEC | |
87 | if (one->nanoseconds != two->nanoseconds) | |
88 | return false; | |
89 | #endif | |
90 | ||
91 | return true; | |
92 | } | |
93 | ||
94 | /* | |
95 | * Test if the given index time is newer than the given existing index entry. | |
96 | * If the timestamps are exactly equivalent, then the given index time is | |
97 | * considered "racily newer" than the existing index entry. | |
98 | */ | |
99 | GIT_INLINE(bool) git_index_entry_newer_than_index( | |
100 | const git_index_entry *entry, git_index *index) | |
101 | { | |
102 | /* If we never read the index, we can't have this race either */ | |
103 | if (!index || index->stamp.mtime.tv_sec == 0) | |
104 | return false; | |
105 | ||
106 | /* If the timestamp is the same or newer than the index, it's racy */ | |
107 | #if defined(GIT_USE_NSEC) | |
0c09753c | 108 | if ((int32_t)index->stamp.mtime.tv_sec < entry->mtime.seconds) |
25e84f95 ET |
109 | return true; |
110 | else if ((int32_t)index->stamp.mtime.tv_sec > entry->mtime.seconds) | |
111 | return false; | |
112 | else | |
113 | return (uint32_t)index->stamp.mtime.tv_nsec <= entry->mtime.nanoseconds; | |
114 | #else | |
115 | return ((int32_t)index->stamp.mtime.tv_sec) <= entry->mtime.seconds; | |
116 | #endif | |
117 | } | |
118 | ||
52bb0476 RB |
119 | /* Search index for `path`, returning GIT_ENOTFOUND if it does not exist |
120 | * (but not setting an error message). | |
121 | * | |
3dbee456 RB |
122 | * `at_pos` is set to the position where it is or would be inserted. |
123 | * Pass `path_len` as strlen of path or 0 to call strlen internally. | |
124 | */ | |
52bb0476 | 125 | extern int git_index__find_pos( |
3158e2fe | 126 | size_t *at_pos, git_index *index, const char *path, size_t path_len, int stage); |
d2ce27dd | 127 | |
879ebab3 VM |
128 | extern int git_index__fill(git_index *index, const git_vector *source_entries); |
129 | ||
cb53669e | 130 | extern void git_index__set_ignore_case(git_index *index, bool ignore_case); |
3f0d0c85 | 131 | |
05d47768 ET |
132 | extern unsigned int git_index__create_mode(unsigned int mode); |
133 | ||
db0e7878 RB |
134 | GIT_INLINE(const git_futils_filestamp *) git_index__filestamp(git_index *index) |
135 | { | |
e579e0f7 | 136 | return &index->stamp; |
db0e7878 RB |
137 | } |
138 | ||
e579e0f7 MB |
139 | GIT_INLINE(unsigned char *) git_index__checksum(git_index *index) |
140 | { | |
141 | return index->checksum; | |
142 | } | |
db0e7878 | 143 | |
54edbb98 RB |
144 | /* Copy the current entries vector *and* increment the index refcount. |
145 | * Call `git_index__release_snapshot` when done. | |
146 | */ | |
52bb0476 RB |
147 | extern int git_index_snapshot_new(git_vector *snap, git_index *index); |
148 | extern void git_index_snapshot_release(git_vector *snap, git_index *index); | |
54edbb98 RB |
149 | |
150 | /* Allow searching in a snapshot; entries must already be sorted! */ | |
52bb0476 RB |
151 | extern int git_index_snapshot_find( |
152 | size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch, | |
54edbb98 RB |
153 | const char *path, size_t path_len, int stage); |
154 | ||
35d39761 ET |
155 | /* Replace an index with a new index */ |
156 | int git_index_read_index(git_index *index, const git_index *new_index); | |
52bb0476 | 157 | |
ac3d33df JK |
158 | GIT_INLINE(int) git_index_is_dirty(git_index *index) |
159 | { | |
160 | return index->dirty; | |
161 | } | |
162 | ||
163 | extern int git_index_read_safely(git_index *index); | |
164 | ||
55798fd1 ET |
165 | typedef struct { |
166 | git_index *index; | |
167 | git_filebuf file; | |
41fae48d | 168 | unsigned int should_write:1; |
55798fd1 ET |
169 | } git_indexwriter; |
170 | ||
171 | #define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT } | |
172 | ||
173 | /* Lock the index for eventual writing. */ | |
174 | extern int git_indexwriter_init(git_indexwriter *writer, git_index *index); | |
175 | ||
41fae48d ET |
176 | /* Lock the index for eventual writing by a repository operation: a merge, |
177 | * revert, cherry-pick or a rebase. Note that the given checkout strategy | |
178 | * will be updated for the operation's use so that checkout will not write | |
179 | * the index. | |
180 | */ | |
181 | extern int git_indexwriter_init_for_operation( | |
182 | git_indexwriter *writer, | |
183 | git_repository *repo, | |
184 | unsigned int *checkout_strategy); | |
185 | ||
55798fd1 ET |
186 | /* Write the index and unlock it. */ |
187 | extern int git_indexwriter_commit(git_indexwriter *writer); | |
188 | ||
189 | /* Cleanup an index writing session, unlocking the file (if it is still | |
190 | * locked and freeing any data structures. | |
191 | */ | |
192 | extern void git_indexwriter_cleanup(git_indexwriter *writer); | |
193 | ||
68535125 | 194 | #endif |