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__
16 #include "tree-cache.h"
18 #include "git2/index.h"
20 #define GIT_INDEX_FILE "index"
21 #define GIT_INDEX_FILE_MODE 0666
23 extern bool git_index__enforce_unsaved_safety
;
28 char *index_file_path
;
29 git_futils_filestamp stamp
;
30 unsigned char checksum
[GIT_HASH_SHA1_SIZE
];
33 git_idxmap
*entries_map
;
35 git_vector deleted
; /* deleted entries if readers > 0 */
36 git_atomic32 readers
; /* number of active iterators */
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 */
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
;
58 struct git_index_iterator
{
64 struct git_index_conflict_iterator
{
69 extern void git_index_entry__init_from_stat(
70 git_index_entry
*entry
, struct stat
*st
, bool trust_mode
);
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
);
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
);
80 /* Index time handling functions */
81 GIT_INLINE(bool) git_index_time_eq(const git_index_time
*one
, const git_index_time
*two
)
83 if (one
->seconds
!= two
->seconds
)
87 if (one
->nanoseconds
!= two
->nanoseconds
)
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.
99 GIT_INLINE(bool) git_index_entry_newer_than_index(
100 const git_index_entry
*entry
, git_index
*index
)
102 /* If we never read the index, we can't have this race either */
103 if (!index
|| index
->stamp
.mtime
.tv_sec
== 0)
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
)
110 else if ((int32_t)index
->stamp
.mtime
.tv_sec
> entry
->mtime
.seconds
)
113 return (uint32_t)index
->stamp
.mtime
.tv_nsec
<= entry
->mtime
.nanoseconds
;
115 return ((int32_t)index
->stamp
.mtime
.tv_sec
) <= entry
->mtime
.seconds
;
119 /* Search index for `path`, returning GIT_ENOTFOUND if it does not exist
120 * (but not setting an error message).
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.
125 extern int git_index__find_pos(
126 size_t *at_pos
, git_index
*index
, const char *path
, size_t path_len
, int stage
);
128 extern int git_index__fill(git_index
*index
, const git_vector
*source_entries
);
130 extern void git_index__set_ignore_case(git_index
*index
, bool ignore_case
);
132 extern unsigned int git_index__create_mode(unsigned int mode
);
134 GIT_INLINE(const git_futils_filestamp
*) git_index__filestamp(git_index
*index
)
136 return &index
->stamp
;
139 GIT_INLINE(unsigned char *) git_index__checksum(git_index
*index
)
141 return index
->checksum
;
144 /* Copy the current entries vector *and* increment the index refcount.
145 * Call `git_index__release_snapshot` when done.
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
);
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
);
155 /* Replace an index with a new index */
156 int git_index_read_index(git_index
*index
, const git_index
*new_index
);
158 GIT_INLINE(int) git_index_is_dirty(git_index
*index
)
163 extern int git_index_read_safely(git_index
*index
);
168 unsigned int should_write
:1;
171 #define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT }
173 /* Lock the index for eventual writing. */
174 extern int git_indexwriter_init(git_indexwriter
*writer
, git_index
*index
);
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
181 extern int git_indexwriter_init_for_operation(
182 git_indexwriter
*writer
,
183 git_repository
*repo
,
184 unsigned int *checkout_strategy
);
186 /* Write the index and unlock it. */
187 extern int git_indexwriter_commit(git_indexwriter
*writer
);
189 /* Cleanup an index writing session, unlocking the file (if it is still
190 * locked and freeing any data structures.
192 extern void git_indexwriter_cleanup(git_indexwriter
*writer
);