index.h 5.65 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8 9
#ifndef INCLUDE_index_h__
#define INCLUDE_index_h__

10 11
#include "common.h"

12
#include "futils.h"
13
#include "filebuf.h"
14
#include "vector.h"
15
#include "idxmap.h"
16
#include "tree-cache.h"
17 18
#include "git2/odb.h"
#include "git2/index.h"
19

20 21 22
#define GIT_INDEX_FILE "index"
#define GIT_INDEX_FILE_MODE 0666

23 24
extern bool git_index__enforce_unsaved_safety;

25
struct git_index {
26 27
	git_refcount rc;

28
	char *index_file_path;
29
	git_futils_filestamp stamp;
30
	unsigned char checksum[GIT_HASH_MAX_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 39
	git_oid_t oid_type;

40
	unsigned int on_disk:1;
41 42 43
	unsigned int ignore_case:1;
	unsigned int distrust_filemode:1;
	unsigned int no_symlinks:1;
44
	unsigned int dirty:1;	/* whether we have unsaved changes */
45

46
	git_tree_cache *tree;
47
	git_pool tree_pool;
48

Edward Thomson committed
49
	git_vector names;
Edward Thomson committed
50
	git_vector reuc;
51

Edward Thomson committed
52
	git_vector_cmp entries_cmp_path;
53
	git_vector_cmp entries_search;
Edward Thomson committed
54 55
	git_vector_cmp entries_search_path;
	git_vector_cmp reuc_search;
David Turner committed
56 57

	unsigned int version;
58 59
};

60 61 62 63 64 65
struct git_index_iterator {
	git_index *index;
	git_vector snap;
	size_t cur;
};

66 67 68 69 70
struct git_index_conflict_iterator {
	git_index *index;
	size_t cur;
};

71
extern void git_index_entry__init_from_stat(
72
	git_index_entry *entry, struct stat *st, bool trust_mode);
73

74 75 76 77 78 79 80
/* Index entry comparison functions for array sorting */
extern int git_index_entry_cmp(const void *a, const void *b);
extern int git_index_entry_icmp(const void *a, const void *b);

/* Index entry search functions for search using a search spec */
extern int git_index_entry_srch(const void *a, const void *b);
extern int git_index_entry_isrch(const void *a, const void *b);
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/* Index time handling functions */
GIT_INLINE(bool) git_index_time_eq(const git_index_time *one, const git_index_time *two)
{
	if (one->seconds != two->seconds)
		return false;

#ifdef GIT_USE_NSEC
	if (one->nanoseconds != two->nanoseconds)
		return false;
#endif

	return true;
}

/*
 * Test if the given index time is newer than the given existing index entry.
 * If the timestamps are exactly equivalent, then the given index time is
 * considered "racily newer" than the existing index entry.
 */
GIT_INLINE(bool) git_index_entry_newer_than_index(
	const git_index_entry *entry, git_index *index)
{
	/* If we never read the index, we can't have this race either */
	if (!index || index->stamp.mtime.tv_sec == 0)
		return false;

	/* If the timestamp is the same or newer than the index, it's racy */
#if defined(GIT_USE_NSEC)
110
	if ((int32_t)index->stamp.mtime.tv_sec < entry->mtime.seconds)
111 112 113 114 115 116 117 118 119 120
		return true;
	else if ((int32_t)index->stamp.mtime.tv_sec > entry->mtime.seconds)
		return false;
	else
		return (uint32_t)index->stamp.mtime.tv_nsec <= entry->mtime.nanoseconds;
#else
	return ((int32_t)index->stamp.mtime.tv_sec) <= entry->mtime.seconds;
#endif
}

121 122 123
/* Search index for `path`, returning GIT_ENOTFOUND if it does not exist
 * (but not setting an error message).
 *
124 125 126
 * `at_pos` is set to the position where it is or would be inserted.
 * Pass `path_len` as strlen of path or 0 to call strlen internally.
 */
127
extern int git_index__find_pos(
128
	size_t *at_pos, git_index *index, const char *path, size_t path_len, int stage);
129

130 131
extern int git_index__fill(git_index *index, const git_vector *source_entries);

132
extern void git_index__set_ignore_case(git_index *index, bool ignore_case);
133

134 135
extern unsigned int git_index__create_mode(unsigned int mode);

136 137
GIT_INLINE(const git_futils_filestamp *) git_index__filestamp(git_index *index)
{
138
	return &index->stamp;
139 140
}

141 142 143 144
GIT_INLINE(unsigned char *) git_index__checksum(git_index *index)
{
	return index->checksum;
}
145

146 147 148 149 150 151 152 153 154 155 156
/* SHA256-aware internal functions */

extern int git_index__new(
	git_index **index_out,
	git_oid_t oid_type);

extern int git_index__open(
	git_index **index_out,
	const char *index_path,
	git_oid_t oid_type);

157 158 159
/* Copy the current entries vector *and* increment the index refcount.
 * Call `git_index__release_snapshot` when done.
 */
160 161
extern int git_index_snapshot_new(git_vector *snap, git_index *index);
extern void git_index_snapshot_release(git_vector *snap, git_index *index);
162 163

/* Allow searching in a snapshot; entries must already be sorted! */
164 165
extern int git_index_snapshot_find(
	size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch,
166 167
	const char *path, size_t path_len, int stage);

168 169
/* Replace an index with a new index */
int git_index_read_index(git_index *index, const git_index *new_index);
170

171 172 173 174 175
GIT_INLINE(int) git_index_is_dirty(git_index *index)
{
	return index->dirty;
}

176 177
extern int git_index_read_safely(git_index *index);

178 179 180
typedef struct {
	git_index *index;
	git_filebuf file;
181
	unsigned int should_write:1;
182 183 184 185 186 187 188
} git_indexwriter;

#define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT }

/* Lock the index for eventual writing. */
extern int git_indexwriter_init(git_indexwriter *writer, git_index *index);

189 190 191 192 193 194 195 196 197 198
/* Lock the index for eventual writing by a repository operation: a merge,
 * revert, cherry-pick or a rebase.  Note that the given checkout strategy
 * will be updated for the operation's use so that checkout will not write
 * the index.
 */
extern int git_indexwriter_init_for_operation(
	git_indexwriter *writer,
	git_repository *repo,
	unsigned int *checkout_strategy);

199 200 201 202 203 204 205 206
/* Write the index and unlock it. */
extern int git_indexwriter_commit(git_indexwriter *writer);

/* Cleanup an index writing session, unlocking the file (if it is still
 * locked and freeing any data structures.
 */
extern void git_indexwriter_cleanup(git_indexwriter *writer);

207
#endif