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 	git_oid checksum;   /* checksum at the end of the file */
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 */
git_index_time_eq(const git_index_time * one,const git_index_time * two)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  */
git_index_entry_newer_than_index(const git_index_entry * entry,git_index * index)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 
git_index__filestamp(git_index * index)134 GIT_INLINE(const git_futils_filestamp *) git_index__filestamp(git_index *index)
135 {
136    return &index->stamp;
137 }
138 
139 extern int git_index__changed_relative_to(git_index *index, const git_oid *checksum);
140 
141 /* Copy the current entries vector *and* increment the index refcount.
142  * Call `git_index__release_snapshot` when done.
143  */
144 extern int git_index_snapshot_new(git_vector *snap, git_index *index);
145 extern void git_index_snapshot_release(git_vector *snap, git_index *index);
146 
147 /* Allow searching in a snapshot; entries must already be sorted! */
148 extern int git_index_snapshot_find(
149 	size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch,
150 	const char *path, size_t path_len, int stage);
151 
152 /* Replace an index with a new index */
153 int git_index_read_index(git_index *index, const git_index *new_index);
154 
git_index_is_dirty(git_index * index)155 GIT_INLINE(int) git_index_is_dirty(git_index *index)
156 {
157 	return index->dirty;
158 }
159 
160 extern int git_index_read_safely(git_index *index);
161 
162 typedef struct {
163 	git_index *index;
164 	git_filebuf file;
165 	unsigned int should_write:1;
166 } git_indexwriter;
167 
168 #define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT }
169 
170 /* Lock the index for eventual writing. */
171 extern int git_indexwriter_init(git_indexwriter *writer, git_index *index);
172 
173 /* Lock the index for eventual writing by a repository operation: a merge,
174  * revert, cherry-pick or a rebase.  Note that the given checkout strategy
175  * will be updated for the operation's use so that checkout will not write
176  * the index.
177  */
178 extern int git_indexwriter_init_for_operation(
179 	git_indexwriter *writer,
180 	git_repository *repo,
181 	unsigned int *checkout_strategy);
182 
183 /* Write the index and unlock it. */
184 extern int git_indexwriter_commit(git_indexwriter *writer);
185 
186 /* Cleanup an index writing session, unlocking the file (if it is still
187  * locked and freeing any data structures.
188  */
189 extern void git_indexwriter_cleanup(git_indexwriter *writer);
190 
191 #endif
192