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