]> git.proxmox.com Git - libgit2.git/blob - src/libgit2/index.h
New upstream version 1.5.0+ds
[libgit2.git] / src / libgit2 / 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 "common.h"
11
12 #include "futils.h"
13 #include "filebuf.h"
14 #include "vector.h"
15 #include "idxmap.h"
16 #include "tree-cache.h"
17 #include "git2/odb.h"
18 #include "git2/index.h"
19
20 #define GIT_INDEX_FILE "index"
21 #define GIT_INDEX_FILE_MODE 0666
22
23 extern bool git_index__enforce_unsaved_safety;
24
25 struct git_index {
26 git_refcount rc;
27
28 char *index_file_path;
29 git_futils_filestamp stamp;
30 unsigned char checksum[GIT_HASH_SHA1_SIZE];
31
32 git_vector entries;
33 git_idxmap *entries_map;
34
35 git_vector deleted; /* deleted entries if readers > 0 */
36 git_atomic32 readers; /* number of active iterators */
37
38 unsigned int on_disk:1;
39 unsigned int ignore_case:1;
40 unsigned int distrust_filemode:1;
41 unsigned int no_symlinks:1;
42 unsigned int dirty:1; /* whether we have unsaved changes */
43
44 git_tree_cache *tree;
45 git_pool tree_pool;
46
47 git_vector names;
48 git_vector reuc;
49
50 git_vector_cmp entries_cmp_path;
51 git_vector_cmp entries_search;
52 git_vector_cmp entries_search_path;
53 git_vector_cmp reuc_search;
54
55 unsigned int version;
56 };
57
58 struct git_index_iterator {
59 git_index *index;
60 git_vector snap;
61 size_t cur;
62 };
63
64 struct git_index_conflict_iterator {
65 git_index *index;
66 size_t cur;
67 };
68
69 extern void git_index_entry__init_from_stat(
70 git_index_entry *entry, struct stat *st, bool trust_mode);
71
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);
79
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)
108 if ((int32_t)index->stamp.mtime.tv_sec < entry->mtime.seconds)
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
119 /* Search index for `path`, returning GIT_ENOTFOUND if it does not exist
120 * (but not setting an error message).
121 *
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 */
125 extern int git_index__find_pos(
126 size_t *at_pos, git_index *index, const char *path, size_t path_len, int stage);
127
128 extern int git_index__fill(git_index *index, const git_vector *source_entries);
129
130 extern void git_index__set_ignore_case(git_index *index, bool ignore_case);
131
132 extern unsigned int git_index__create_mode(unsigned int mode);
133
134 GIT_INLINE(const git_futils_filestamp *) git_index__filestamp(git_index *index)
135 {
136 return &index->stamp;
137 }
138
139 GIT_INLINE(unsigned char *) git_index__checksum(git_index *index)
140 {
141 return index->checksum;
142 }
143
144 /* Copy the current entries vector *and* increment the index refcount.
145 * Call `git_index__release_snapshot` when done.
146 */
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);
149
150 /* Allow searching in a snapshot; entries must already be sorted! */
151 extern int git_index_snapshot_find(
152 size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch,
153 const char *path, size_t path_len, int stage);
154
155 /* Replace an index with a new index */
156 int git_index_read_index(git_index *index, const git_index *new_index);
157
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
165 typedef struct {
166 git_index *index;
167 git_filebuf file;
168 unsigned int should_write:1;
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
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
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
194 #endif