]> git.proxmox.com Git - libgit2.git/blob - src/index.h
Merge branch 'pr/3809'
[libgit2.git] / src / index.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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 */
7 #ifndef INCLUDE_index_h__
8 #define INCLUDE_index_h__
9
10 #include "fileops.h"
11 #include "filebuf.h"
12 #include "vector.h"
13 #include "idxmap.h"
14 #include "tree-cache.h"
15 #include "git2/odb.h"
16 #include "git2/index.h"
17
18 #define GIT_INDEX_FILE "index"
19 #define GIT_INDEX_FILE_MODE 0666
20
21 struct git_index {
22 git_refcount rc;
23
24 char *index_file_path;
25 git_futils_filestamp stamp;
26 git_oid checksum; /* checksum at the end of the file */
27
28 git_vector entries;
29 git_idxmap *entries_map;
30
31 git_vector deleted; /* deleted entries if readers > 0 */
32 git_atomic readers; /* number of active iterators */
33
34 unsigned int on_disk:1;
35 unsigned int ignore_case:1;
36 unsigned int distrust_filemode:1;
37 unsigned int no_symlinks:1;
38
39 git_tree_cache *tree;
40 git_pool tree_pool;
41
42 git_vector names;
43 git_vector reuc;
44
45 git_vector_cmp entries_cmp_path;
46 git_vector_cmp entries_search;
47 git_vector_cmp entries_search_path;
48 git_vector_cmp reuc_search;
49
50 unsigned int version;
51 };
52
53 struct git_index_conflict_iterator {
54 git_index *index;
55 size_t cur;
56 };
57
58 extern void git_index_entry__init_from_stat(
59 git_index_entry *entry, struct stat *st, bool trust_mode);
60
61 /* Index entry comparison functions for array sorting */
62 extern int git_index_entry_cmp(const void *a, const void *b);
63 extern int git_index_entry_icmp(const void *a, const void *b);
64
65 /* Index entry search functions for search using a search spec */
66 extern int git_index_entry_srch(const void *a, const void *b);
67 extern int git_index_entry_isrch(const void *a, const void *b);
68
69 /* Index time handling functions */
70 GIT_INLINE(bool) git_index_time_eq(const git_index_time *one, const git_index_time *two)
71 {
72 if (one->seconds != two->seconds)
73 return false;
74
75 #ifdef GIT_USE_NSEC
76 if (one->nanoseconds != two->nanoseconds)
77 return false;
78 #endif
79
80 return true;
81 }
82
83 /*
84 * Test if the given index time is newer than the given existing index entry.
85 * If the timestamps are exactly equivalent, then the given index time is
86 * considered "racily newer" than the existing index entry.
87 */
88 GIT_INLINE(bool) git_index_entry_newer_than_index(
89 const git_index_entry *entry, git_index *index)
90 {
91 /* If we never read the index, we can't have this race either */
92 if (!index || index->stamp.mtime.tv_sec == 0)
93 return false;
94
95 /* If the timestamp is the same or newer than the index, it's racy */
96 #if defined(GIT_USE_NSEC)
97 if ((int32_t)index->stamp.mtime.tv_sec < entry->mtime.seconds)
98 return true;
99 else if ((int32_t)index->stamp.mtime.tv_sec > entry->mtime.seconds)
100 return false;
101 else
102 return (uint32_t)index->stamp.mtime.tv_nsec <= entry->mtime.nanoseconds;
103 #else
104 return ((int32_t)index->stamp.mtime.tv_sec) <= entry->mtime.seconds;
105 #endif
106 }
107
108 /* Search index for `path`, returning GIT_ENOTFOUND if it does not exist
109 * (but not setting an error message).
110 *
111 * `at_pos` is set to the position where it is or would be inserted.
112 * Pass `path_len` as strlen of path or 0 to call strlen internally.
113 */
114 extern int git_index__find_pos(
115 size_t *at_pos, git_index *index, const char *path, size_t path_len, int stage);
116
117 extern int git_index__fill(git_index *index, const git_vector *source_entries);
118
119 extern void git_index__set_ignore_case(git_index *index, bool ignore_case);
120
121 extern unsigned int git_index__create_mode(unsigned int mode);
122
123 GIT_INLINE(const git_futils_filestamp *) git_index__filestamp(git_index *index)
124 {
125 return &index->stamp;
126 }
127
128 extern int git_index__changed_relative_to(git_index *index, const git_oid *checksum);
129
130 /* Copy the current entries vector *and* increment the index refcount.
131 * Call `git_index__release_snapshot` when done.
132 */
133 extern int git_index_snapshot_new(git_vector *snap, git_index *index);
134 extern void git_index_snapshot_release(git_vector *snap, git_index *index);
135
136 /* Allow searching in a snapshot; entries must already be sorted! */
137 extern int git_index_snapshot_find(
138 size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch,
139 const char *path, size_t path_len, int stage);
140
141 /* Replace an index with a new index */
142 int git_index_read_index(git_index *index, const git_index *new_index);
143
144 typedef struct {
145 git_index *index;
146 git_filebuf file;
147 unsigned int should_write:1;
148 } git_indexwriter;
149
150 #define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT }
151
152 /* Lock the index for eventual writing. */
153 extern int git_indexwriter_init(git_indexwriter *writer, git_index *index);
154
155 /* Lock the index for eventual writing by a repository operation: a merge,
156 * revert, cherry-pick or a rebase. Note that the given checkout strategy
157 * will be updated for the operation's use so that checkout will not write
158 * the index.
159 */
160 extern int git_indexwriter_init_for_operation(
161 git_indexwriter *writer,
162 git_repository *repo,
163 unsigned int *checkout_strategy);
164
165 /* Write the index and unlock it. */
166 extern int git_indexwriter_commit(git_indexwriter *writer);
167
168 /* Cleanup an index writing session, unlocking the file (if it is still
169 * locked and freeing any data structures.
170 */
171 extern void git_indexwriter_cleanup(git_indexwriter *writer);
172
173 #endif