]> git.proxmox.com Git - libgit2.git/blame - src/index.h
New upstream version 1.3.0+dfsg.1
[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
eae0bfdc
PP
10#include "common.h"
11
22a2d3d5 12#include "futils.h"
817c2820 13#include "filebuf.h"
c4034e63 14#include "vector.h"
af1d5239 15#include "idxmap.h"
b4171320 16#include "tree-cache.h"
44908fe7
VM
17#include "git2/odb.h"
18#include "git2/index.h"
68535125 19
01ad7b3a
BR
20#define GIT_INDEX_FILE "index"
21#define GIT_INDEX_FILE_MODE 0666
22
ac3d33df
JK
23extern bool git_index__enforce_unsaved_safety;
24
68535125 25struct git_index {
9462c471
VM
26 git_refcount rc;
27
68535125 28 char *index_file_path;
8ff0f325 29 git_futils_filestamp stamp;
5e947c91 30 git_oid checksum; /* checksum at the end of the file */
dac16048 31
c4034e63 32 git_vector entries;
af1d5239 33 git_idxmap *entries_map;
68535125 34
dac16048 35 git_vector deleted; /* deleted entries if readers > 0 */
c25aa7cd 36 git_atomic32 readers; /* number of active iterators */
da825c92 37
dac16048 38 unsigned int on_disk:1;
da825c92
RB
39 unsigned int ignore_case:1;
40 unsigned int distrust_filemode:1;
41 unsigned int no_symlinks:1;
ac3d33df 42 unsigned int dirty:1; /* whether we have unsaved changes */
da825c92 43
b4171320 44 git_tree_cache *tree;
19c88310 45 git_pool tree_pool;
4c0b6a6d 46
0462fba5 47 git_vector names;
f45ec1a0 48 git_vector reuc;
ec40b7f9 49
f45ec1a0 50 git_vector_cmp entries_cmp_path;
ec40b7f9 51 git_vector_cmp entries_search;
f45ec1a0
ET
52 git_vector_cmp entries_search_path;
53 git_vector_cmp reuc_search;
5625d86b
DT
54
55 unsigned int version;
68535125
VM
56};
57
ac3d33df
JK
58struct git_index_iterator {
59 git_index *index;
60 git_vector snap;
61 size_t cur;
62};
63
0e0108f7
ET
64struct git_index_conflict_iterator {
65 git_index *index;
66 size_t cur;
67};
68
d2ce27dd 69extern void git_index_entry__init_from_stat(
14997dc5 70 git_index_entry *entry, struct stat *st, bool trust_mode);
7c7ff7d1 71
3b4c401a
RB
72/* Index entry comparison functions for array sorting */
73extern int git_index_entry_cmp(const void *a, const void *b);
74extern int git_index_entry_icmp(const void *a, const void *b);
75
76/* Index entry search functions for search using a search spec */
77extern int git_index_entry_srch(const void *a, const void *b);
78extern int git_index_entry_isrch(const void *a, const void *b);
55cbd05b 79
25e84f95
ET
80/* Index time handling functions */
81GIT_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 */
99GIT_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)
0c09753c 108 if ((int32_t)index->stamp.mtime.tv_sec < entry->mtime.seconds)
25e84f95
ET
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
52bb0476
RB
119/* Search index for `path`, returning GIT_ENOTFOUND if it does not exist
120 * (but not setting an error message).
121 *
3dbee456
RB
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 */
52bb0476 125extern int git_index__find_pos(
3158e2fe 126 size_t *at_pos, git_index *index, const char *path, size_t path_len, int stage);
d2ce27dd 127
879ebab3
VM
128extern int git_index__fill(git_index *index, const git_vector *source_entries);
129
cb53669e 130extern void git_index__set_ignore_case(git_index *index, bool ignore_case);
3f0d0c85 131
05d47768
ET
132extern unsigned int git_index__create_mode(unsigned int mode);
133
db0e7878
RB
134GIT_INLINE(const git_futils_filestamp *) git_index__filestamp(git_index *index)
135{
136 return &index->stamp;
137}
138
624c949f 139extern int git_index__changed_relative_to(git_index *index, const git_oid *checksum);
db0e7878 140
54edbb98
RB
141/* Copy the current entries vector *and* increment the index refcount.
142 * Call `git_index__release_snapshot` when done.
143 */
52bb0476
RB
144extern int git_index_snapshot_new(git_vector *snap, git_index *index);
145extern void git_index_snapshot_release(git_vector *snap, git_index *index);
54edbb98
RB
146
147/* Allow searching in a snapshot; entries must already be sorted! */
52bb0476
RB
148extern int git_index_snapshot_find(
149 size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch,
54edbb98
RB
150 const char *path, size_t path_len, int stage);
151
35d39761
ET
152/* Replace an index with a new index */
153int git_index_read_index(git_index *index, const git_index *new_index);
52bb0476 154
ac3d33df
JK
155GIT_INLINE(int) git_index_is_dirty(git_index *index)
156{
157 return index->dirty;
158}
159
160extern int git_index_read_safely(git_index *index);
161
55798fd1
ET
162typedef struct {
163 git_index *index;
164 git_filebuf file;
41fae48d 165 unsigned int should_write:1;
55798fd1
ET
166} git_indexwriter;
167
168#define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT }
169
170/* Lock the index for eventual writing. */
171extern int git_indexwriter_init(git_indexwriter *writer, git_index *index);
172
41fae48d
ET
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 */
178extern int git_indexwriter_init_for_operation(
179 git_indexwriter *writer,
180 git_repository *repo,
181 unsigned int *checkout_strategy);
182
55798fd1
ET
183/* Write the index and unlock it. */
184extern 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 */
189extern void git_indexwriter_cleanup(git_indexwriter *writer);
190
68535125 191#endif