line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#include "index.h" |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
#include "repository.h" |
13
|
|
|
|
|
|
|
#include "tree.h" |
14
|
|
|
|
|
|
|
#include "tree-cache.h" |
15
|
|
|
|
|
|
|
#include "hash.h" |
16
|
|
|
|
|
|
|
#include "iterator.h" |
17
|
|
|
|
|
|
|
#include "pathspec.h" |
18
|
|
|
|
|
|
|
#include "ignore.h" |
19
|
|
|
|
|
|
|
#include "blob.h" |
20
|
|
|
|
|
|
|
#include "idxmap.h" |
21
|
|
|
|
|
|
|
#include "diff.h" |
22
|
|
|
|
|
|
|
#include "varint.h" |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#include "git2/odb.h" |
25
|
|
|
|
|
|
|
#include "git2/oid.h" |
26
|
|
|
|
|
|
|
#include "git2/blob.h" |
27
|
|
|
|
|
|
|
#include "git2/config.h" |
28
|
|
|
|
|
|
|
#include "git2/sys/index.h" |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths, |
31
|
|
|
|
|
|
|
unsigned int flags, |
32
|
|
|
|
|
|
|
git_index_matched_path_cb cb, void *payload); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
#define minimal_entry_size (offsetof(struct entry_short, path)) |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
static const size_t INDEX_FOOTER_SIZE = GIT_OID_RAWSZ; |
37
|
|
|
|
|
|
|
static const size_t INDEX_HEADER_SIZE = 12; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
static const unsigned int INDEX_VERSION_NUMBER_DEFAULT = 2; |
40
|
|
|
|
|
|
|
static const unsigned int INDEX_VERSION_NUMBER_LB = 2; |
41
|
|
|
|
|
|
|
static const unsigned int INDEX_VERSION_NUMBER_EXT = 3; |
42
|
|
|
|
|
|
|
static const unsigned int INDEX_VERSION_NUMBER_COMP = 4; |
43
|
|
|
|
|
|
|
static const unsigned int INDEX_VERSION_NUMBER_UB = 4; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
static const unsigned int INDEX_HEADER_SIG = 0x44495243; |
46
|
|
|
|
|
|
|
static const char INDEX_EXT_TREECACHE_SIG[] = {'T', 'R', 'E', 'E'}; |
47
|
|
|
|
|
|
|
static const char INDEX_EXT_UNMERGED_SIG[] = {'R', 'E', 'U', 'C'}; |
48
|
|
|
|
|
|
|
static const char INDEX_EXT_CONFLICT_NAME_SIG[] = {'N', 'A', 'M', 'E'}; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
#define INDEX_OWNER(idx) ((git_repository *)(GIT_REFCOUNT_OWNER(idx))) |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
struct index_header { |
53
|
|
|
|
|
|
|
uint32_t signature; |
54
|
|
|
|
|
|
|
uint32_t version; |
55
|
|
|
|
|
|
|
uint32_t entry_count; |
56
|
|
|
|
|
|
|
}; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
struct index_extension { |
59
|
|
|
|
|
|
|
char signature[4]; |
60
|
|
|
|
|
|
|
uint32_t extension_size; |
61
|
|
|
|
|
|
|
}; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
struct entry_time { |
64
|
|
|
|
|
|
|
uint32_t seconds; |
65
|
|
|
|
|
|
|
uint32_t nanoseconds; |
66
|
|
|
|
|
|
|
}; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
struct entry_short { |
69
|
|
|
|
|
|
|
struct entry_time ctime; |
70
|
|
|
|
|
|
|
struct entry_time mtime; |
71
|
|
|
|
|
|
|
uint32_t dev; |
72
|
|
|
|
|
|
|
uint32_t ino; |
73
|
|
|
|
|
|
|
uint32_t mode; |
74
|
|
|
|
|
|
|
uint32_t uid; |
75
|
|
|
|
|
|
|
uint32_t gid; |
76
|
|
|
|
|
|
|
uint32_t file_size; |
77
|
|
|
|
|
|
|
git_oid oid; |
78
|
|
|
|
|
|
|
uint16_t flags; |
79
|
|
|
|
|
|
|
char path[1]; /* arbitrary length */ |
80
|
|
|
|
|
|
|
}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
struct entry_long { |
83
|
|
|
|
|
|
|
struct entry_time ctime; |
84
|
|
|
|
|
|
|
struct entry_time mtime; |
85
|
|
|
|
|
|
|
uint32_t dev; |
86
|
|
|
|
|
|
|
uint32_t ino; |
87
|
|
|
|
|
|
|
uint32_t mode; |
88
|
|
|
|
|
|
|
uint32_t uid; |
89
|
|
|
|
|
|
|
uint32_t gid; |
90
|
|
|
|
|
|
|
uint32_t file_size; |
91
|
|
|
|
|
|
|
git_oid oid; |
92
|
|
|
|
|
|
|
uint16_t flags; |
93
|
|
|
|
|
|
|
uint16_t flags_extended; |
94
|
|
|
|
|
|
|
char path[1]; /* arbitrary length */ |
95
|
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
struct entry_srch_key { |
98
|
|
|
|
|
|
|
const char *path; |
99
|
|
|
|
|
|
|
size_t pathlen; |
100
|
|
|
|
|
|
|
int stage; |
101
|
|
|
|
|
|
|
}; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
struct entry_internal { |
104
|
|
|
|
|
|
|
git_index_entry entry; |
105
|
|
|
|
|
|
|
size_t pathlen; |
106
|
|
|
|
|
|
|
char path[GIT_FLEX_ARRAY]; |
107
|
|
|
|
|
|
|
}; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
struct reuc_entry_internal { |
110
|
|
|
|
|
|
|
git_index_reuc_entry entry; |
111
|
|
|
|
|
|
|
size_t pathlen; |
112
|
|
|
|
|
|
|
char path[GIT_FLEX_ARRAY]; |
113
|
|
|
|
|
|
|
}; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
bool git_index__enforce_unsaved_safety = false; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
/* local declarations */ |
118
|
|
|
|
|
|
|
static int read_extension(size_t *read_len, git_index *index, const char *buffer, size_t buffer_size); |
119
|
|
|
|
|
|
|
static int read_header(struct index_header *dest, const void *buffer); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
static int parse_index(git_index *index, const char *buffer, size_t buffer_size); |
122
|
|
|
|
|
|
|
static bool is_index_extended(git_index *index); |
123
|
|
|
|
|
|
|
static int write_index(git_oid *checksum, git_index *index, git_filebuf *file); |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
static void index_entry_free(git_index_entry *entry); |
126
|
|
|
|
|
|
|
static void index_entry_reuc_free(git_index_reuc_entry *reuc); |
127
|
|
|
|
|
|
|
|
128
|
173
|
|
|
|
|
|
GIT_INLINE(int) index_map_set(git_idxmap *map, git_index_entry *e, bool ignore_case) |
129
|
|
|
|
|
|
|
{ |
130
|
173
|
50
|
|
|
|
|
if (ignore_case) |
131
|
0
|
|
|
|
|
|
return git_idxmap_icase_set((git_idxmap_icase *) map, e, e); |
132
|
|
|
|
|
|
|
else |
133
|
173
|
|
|
|
|
|
return git_idxmap_set(map, e, e); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
217
|
|
|
|
|
|
GIT_INLINE(int) index_map_delete(git_idxmap *map, git_index_entry *e, bool ignore_case) |
137
|
|
|
|
|
|
|
{ |
138
|
217
|
50
|
|
|
|
|
if (ignore_case) |
139
|
0
|
|
|
|
|
|
return git_idxmap_icase_delete((git_idxmap_icase *) map, e); |
140
|
|
|
|
|
|
|
else |
141
|
217
|
|
|
|
|
|
return git_idxmap_delete(map, e); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
43
|
|
|
|
|
|
GIT_INLINE(int) index_map_resize(git_idxmap *map, size_t count, bool ignore_case) |
145
|
|
|
|
|
|
|
{ |
146
|
43
|
50
|
|
|
|
|
if (ignore_case) |
147
|
0
|
|
|
|
|
|
return git_idxmap_icase_resize((git_idxmap_icase *) map, count); |
148
|
|
|
|
|
|
|
else |
149
|
43
|
|
|
|
|
|
return git_idxmap_resize(map, count); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
2925
|
|
|
|
|
|
int git_index_entry_srch(const void *key, const void *array_member) |
153
|
|
|
|
|
|
|
{ |
154
|
2925
|
|
|
|
|
|
const struct entry_srch_key *srch_key = key; |
155
|
2925
|
|
|
|
|
|
const struct entry_internal *entry = array_member; |
156
|
|
|
|
|
|
|
int cmp; |
157
|
|
|
|
|
|
|
size_t len1, len2, len; |
158
|
|
|
|
|
|
|
|
159
|
2925
|
|
|
|
|
|
len1 = srch_key->pathlen; |
160
|
2925
|
|
|
|
|
|
len2 = entry->pathlen; |
161
|
2925
|
|
|
|
|
|
len = len1 < len2 ? len1 : len2; |
162
|
|
|
|
|
|
|
|
163
|
2925
|
|
|
|
|
|
cmp = memcmp(srch_key->path, entry->path, len); |
164
|
2925
|
100
|
|
|
|
|
if (cmp) |
165
|
2594
|
|
|
|
|
|
return cmp; |
166
|
331
|
100
|
|
|
|
|
if (len1 < len2) |
167
|
176
|
|
|
|
|
|
return -1; |
168
|
155
|
100
|
|
|
|
|
if (len1 > len2) |
169
|
39
|
|
|
|
|
|
return 1; |
170
|
|
|
|
|
|
|
|
171
|
116
|
100
|
|
|
|
|
if (srch_key->stage != GIT_INDEX_STAGE_ANY) |
172
|
112
|
|
|
|
|
|
return srch_key->stage - GIT_INDEX_ENTRY_STAGE(&entry->entry); |
173
|
|
|
|
|
|
|
|
174
|
4
|
|
|
|
|
|
return 0; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
0
|
|
|
|
|
|
int git_index_entry_isrch(const void *key, const void *array_member) |
178
|
|
|
|
|
|
|
{ |
179
|
0
|
|
|
|
|
|
const struct entry_srch_key *srch_key = key; |
180
|
0
|
|
|
|
|
|
const struct entry_internal *entry = array_member; |
181
|
|
|
|
|
|
|
int cmp; |
182
|
|
|
|
|
|
|
size_t len1, len2, len; |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
len1 = srch_key->pathlen; |
185
|
0
|
|
|
|
|
|
len2 = entry->pathlen; |
186
|
0
|
|
|
|
|
|
len = len1 < len2 ? len1 : len2; |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
cmp = strncasecmp(srch_key->path, entry->path, len); |
189
|
|
|
|
|
|
|
|
190
|
0
|
0
|
|
|
|
|
if (cmp) |
191
|
0
|
|
|
|
|
|
return cmp; |
192
|
0
|
0
|
|
|
|
|
if (len1 < len2) |
193
|
0
|
|
|
|
|
|
return -1; |
194
|
0
|
0
|
|
|
|
|
if (len1 > len2) |
195
|
0
|
|
|
|
|
|
return 1; |
196
|
|
|
|
|
|
|
|
197
|
0
|
0
|
|
|
|
|
if (srch_key->stage != GIT_INDEX_STAGE_ANY) |
198
|
0
|
|
|
|
|
|
return srch_key->stage - GIT_INDEX_ENTRY_STAGE(&entry->entry); |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
return 0; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
116
|
|
|
|
|
|
static int index_entry_srch_path(const void *path, const void *array_member) |
204
|
|
|
|
|
|
|
{ |
205
|
116
|
|
|
|
|
|
const git_index_entry *entry = array_member; |
206
|
|
|
|
|
|
|
|
207
|
116
|
|
|
|
|
|
return strcmp((const char *)path, entry->path); |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
0
|
|
|
|
|
|
static int index_entry_isrch_path(const void *path, const void *array_member) |
211
|
|
|
|
|
|
|
{ |
212
|
0
|
|
|
|
|
|
const git_index_entry *entry = array_member; |
213
|
|
|
|
|
|
|
|
214
|
0
|
|
|
|
|
|
return strcasecmp((const char *)path, entry->path); |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
239
|
|
|
|
|
|
int git_index_entry_cmp(const void *a, const void *b) |
218
|
|
|
|
|
|
|
{ |
219
|
|
|
|
|
|
|
int diff; |
220
|
239
|
|
|
|
|
|
const git_index_entry *entry_a = a; |
221
|
239
|
|
|
|
|
|
const git_index_entry *entry_b = b; |
222
|
|
|
|
|
|
|
|
223
|
239
|
|
|
|
|
|
diff = strcmp(entry_a->path, entry_b->path); |
224
|
|
|
|
|
|
|
|
225
|
239
|
100
|
|
|
|
|
if (diff == 0) |
226
|
118
|
|
|
|
|
|
diff = (GIT_INDEX_ENTRY_STAGE(entry_a) - GIT_INDEX_ENTRY_STAGE(entry_b)); |
227
|
|
|
|
|
|
|
|
228
|
239
|
|
|
|
|
|
return diff; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
int git_index_entry_icmp(const void *a, const void *b) |
232
|
|
|
|
|
|
|
{ |
233
|
|
|
|
|
|
|
int diff; |
234
|
0
|
|
|
|
|
|
const git_index_entry *entry_a = a; |
235
|
0
|
|
|
|
|
|
const git_index_entry *entry_b = b; |
236
|
|
|
|
|
|
|
|
237
|
0
|
|
|
|
|
|
diff = strcasecmp(entry_a->path, entry_b->path); |
238
|
|
|
|
|
|
|
|
239
|
0
|
0
|
|
|
|
|
if (diff == 0) |
240
|
0
|
|
|
|
|
|
diff = (GIT_INDEX_ENTRY_STAGE(entry_a) - GIT_INDEX_ENTRY_STAGE(entry_b)); |
241
|
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
|
return diff; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
0
|
|
|
|
|
|
static int conflict_name_cmp(const void *a, const void *b) |
246
|
|
|
|
|
|
|
{ |
247
|
0
|
|
|
|
|
|
const git_index_name_entry *name_a = a; |
248
|
0
|
|
|
|
|
|
const git_index_name_entry *name_b = b; |
249
|
|
|
|
|
|
|
|
250
|
0
|
0
|
|
|
|
|
if (name_a->ancestor && !name_b->ancestor) |
|
|
0
|
|
|
|
|
|
251
|
0
|
|
|
|
|
|
return 1; |
252
|
|
|
|
|
|
|
|
253
|
0
|
0
|
|
|
|
|
if (!name_a->ancestor && name_b->ancestor) |
|
|
0
|
|
|
|
|
|
254
|
0
|
|
|
|
|
|
return -1; |
255
|
|
|
|
|
|
|
|
256
|
0
|
0
|
|
|
|
|
if (name_a->ancestor) |
257
|
0
|
|
|
|
|
|
return strcmp(name_a->ancestor, name_b->ancestor); |
258
|
|
|
|
|
|
|
|
259
|
0
|
0
|
|
|
|
|
if (!name_a->ours || !name_b->ours) |
|
|
0
|
|
|
|
|
|
260
|
0
|
|
|
|
|
|
return 0; |
261
|
|
|
|
|
|
|
|
262
|
0
|
|
|
|
|
|
return strcmp(name_a->ours, name_b->ours); |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
/** |
266
|
|
|
|
|
|
|
* TODO: enable this when resolving case insensitive conflicts |
267
|
|
|
|
|
|
|
*/ |
268
|
|
|
|
|
|
|
#if 0 |
269
|
|
|
|
|
|
|
static int conflict_name_icmp(const void *a, const void *b) |
270
|
|
|
|
|
|
|
{ |
271
|
|
|
|
|
|
|
const git_index_name_entry *name_a = a; |
272
|
|
|
|
|
|
|
const git_index_name_entry *name_b = b; |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
if (name_a->ancestor && !name_b->ancestor) |
275
|
|
|
|
|
|
|
return 1; |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
if (!name_a->ancestor && name_b->ancestor) |
278
|
|
|
|
|
|
|
return -1; |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
if (name_a->ancestor) |
281
|
|
|
|
|
|
|
return strcasecmp(name_a->ancestor, name_b->ancestor); |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
if (!name_a->ours || !name_b->ours) |
284
|
|
|
|
|
|
|
return 0; |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
return strcasecmp(name_a->ours, name_b->ours); |
287
|
|
|
|
|
|
|
} |
288
|
|
|
|
|
|
|
#endif |
289
|
|
|
|
|
|
|
|
290
|
4
|
|
|
|
|
|
static int reuc_srch(const void *key, const void *array_member) |
291
|
|
|
|
|
|
|
{ |
292
|
4
|
|
|
|
|
|
const git_index_reuc_entry *reuc = array_member; |
293
|
|
|
|
|
|
|
|
294
|
4
|
|
|
|
|
|
return strcmp(key, reuc->path); |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
0
|
|
|
|
|
|
static int reuc_isrch(const void *key, const void *array_member) |
298
|
|
|
|
|
|
|
{ |
299
|
0
|
|
|
|
|
|
const git_index_reuc_entry *reuc = array_member; |
300
|
|
|
|
|
|
|
|
301
|
0
|
|
|
|
|
|
return strcasecmp(key, reuc->path); |
302
|
|
|
|
|
|
|
} |
303
|
|
|
|
|
|
|
|
304
|
4
|
|
|
|
|
|
static int reuc_cmp(const void *a, const void *b) |
305
|
|
|
|
|
|
|
{ |
306
|
4
|
|
|
|
|
|
const git_index_reuc_entry *info_a = a; |
307
|
4
|
|
|
|
|
|
const git_index_reuc_entry *info_b = b; |
308
|
|
|
|
|
|
|
|
309
|
4
|
|
|
|
|
|
return strcmp(info_a->path, info_b->path); |
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
|
312
|
0
|
|
|
|
|
|
static int reuc_icmp(const void *a, const void *b) |
313
|
|
|
|
|
|
|
{ |
314
|
0
|
|
|
|
|
|
const git_index_reuc_entry *info_a = a; |
315
|
0
|
|
|
|
|
|
const git_index_reuc_entry *info_b = b; |
316
|
|
|
|
|
|
|
|
317
|
0
|
|
|
|
|
|
return strcasecmp(info_a->path, info_b->path); |
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
|
320
|
10
|
|
|
|
|
|
static void index_entry_reuc_free(git_index_reuc_entry *reuc) |
321
|
|
|
|
|
|
|
{ |
322
|
10
|
|
|
|
|
|
git__free(reuc); |
323
|
10
|
|
|
|
|
|
} |
324
|
|
|
|
|
|
|
|
325
|
228
|
|
|
|
|
|
static void index_entry_free(git_index_entry *entry) |
326
|
|
|
|
|
|
|
{ |
327
|
228
|
50
|
|
|
|
|
if (!entry) |
328
|
0
|
|
|
|
|
|
return; |
329
|
|
|
|
|
|
|
|
330
|
228
|
|
|
|
|
|
memset(&entry->id, 0, sizeof(entry->id)); |
331
|
228
|
|
|
|
|
|
git__free(entry); |
332
|
|
|
|
|
|
|
} |
333
|
|
|
|
|
|
|
|
334
|
280
|
|
|
|
|
|
unsigned int git_index__create_mode(unsigned int mode) |
335
|
|
|
|
|
|
|
{ |
336
|
280
|
100
|
|
|
|
|
if (S_ISLNK(mode)) |
337
|
1
|
|
|
|
|
|
return S_IFLNK; |
338
|
|
|
|
|
|
|
|
339
|
279
|
50
|
|
|
|
|
if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR)) |
|
|
50
|
|
|
|
|
|
340
|
0
|
|
|
|
|
|
return (S_IFLNK | S_IFDIR); |
341
|
|
|
|
|
|
|
|
342
|
279
|
100
|
|
|
|
|
return S_IFREG | GIT_PERMS_CANONICAL(mode); |
343
|
|
|
|
|
|
|
} |
344
|
|
|
|
|
|
|
|
345
|
50
|
|
|
|
|
|
static unsigned int index_merge_mode( |
346
|
|
|
|
|
|
|
git_index *index, git_index_entry *existing, unsigned int mode) |
347
|
|
|
|
|
|
|
{ |
348
|
50
|
50
|
|
|
|
|
if (index->no_symlinks && S_ISREG(mode) && |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
349
|
0
|
0
|
|
|
|
|
existing && S_ISLNK(existing->mode)) |
350
|
0
|
|
|
|
|
|
return existing->mode; |
351
|
|
|
|
|
|
|
|
352
|
50
|
50
|
|
|
|
|
if (index->distrust_filemode && S_ISREG(mode)) |
|
|
0
|
|
|
|
|
|
353
|
0
|
0
|
|
|
|
|
return (existing && S_ISREG(existing->mode)) ? |
354
|
0
|
0
|
|
|
|
|
existing->mode : git_index__create_mode(0666); |
355
|
|
|
|
|
|
|
|
356
|
50
|
|
|
|
|
|
return git_index__create_mode(mode); |
357
|
|
|
|
|
|
|
} |
358
|
|
|
|
|
|
|
|
359
|
1597
|
|
|
|
|
|
GIT_INLINE(int) index_find_in_entries( |
360
|
|
|
|
|
|
|
size_t *out, git_vector *entries, git_vector_cmp entry_srch, |
361
|
|
|
|
|
|
|
const char *path, size_t path_len, int stage) |
362
|
|
|
|
|
|
|
{ |
363
|
|
|
|
|
|
|
struct entry_srch_key srch_key; |
364
|
1597
|
|
|
|
|
|
srch_key.path = path; |
365
|
1597
|
100
|
|
|
|
|
srch_key.pathlen = !path_len ? strlen(path) : path_len; |
366
|
1597
|
|
|
|
|
|
srch_key.stage = stage; |
367
|
1597
|
|
|
|
|
|
return git_vector_bsearch2(out, entries, entry_srch, &srch_key); |
368
|
|
|
|
|
|
|
} |
369
|
|
|
|
|
|
|
|
370
|
1382
|
|
|
|
|
|
GIT_INLINE(int) index_find( |
371
|
|
|
|
|
|
|
size_t *out, git_index *index, |
372
|
|
|
|
|
|
|
const char *path, size_t path_len, int stage) |
373
|
|
|
|
|
|
|
{ |
374
|
1382
|
|
|
|
|
|
git_vector_sort(&index->entries); |
375
|
|
|
|
|
|
|
|
376
|
1382
|
|
|
|
|
|
return index_find_in_entries( |
377
|
|
|
|
|
|
|
out, &index->entries, index->entries_search, path, path_len, stage); |
378
|
|
|
|
|
|
|
} |
379
|
|
|
|
|
|
|
|
380
|
6
|
|
|
|
|
|
void git_index__set_ignore_case(git_index *index, bool ignore_case) |
381
|
|
|
|
|
|
|
{ |
382
|
6
|
|
|
|
|
|
index->ignore_case = ignore_case; |
383
|
|
|
|
|
|
|
|
384
|
6
|
50
|
|
|
|
|
if (ignore_case) { |
385
|
0
|
|
|
|
|
|
index->entries_cmp_path = git__strcasecmp_cb; |
386
|
0
|
|
|
|
|
|
index->entries_search = git_index_entry_isrch; |
387
|
0
|
|
|
|
|
|
index->entries_search_path = index_entry_isrch_path; |
388
|
0
|
|
|
|
|
|
index->reuc_search = reuc_isrch; |
389
|
|
|
|
|
|
|
} else { |
390
|
6
|
|
|
|
|
|
index->entries_cmp_path = git__strcmp_cb; |
391
|
6
|
|
|
|
|
|
index->entries_search = git_index_entry_srch; |
392
|
6
|
|
|
|
|
|
index->entries_search_path = index_entry_srch_path; |
393
|
6
|
|
|
|
|
|
index->reuc_search = reuc_srch; |
394
|
|
|
|
|
|
|
} |
395
|
|
|
|
|
|
|
|
396
|
6
|
50
|
|
|
|
|
git_vector_set_cmp(&index->entries, |
397
|
|
|
|
|
|
|
ignore_case ? git_index_entry_icmp : git_index_entry_cmp); |
398
|
6
|
|
|
|
|
|
git_vector_sort(&index->entries); |
399
|
|
|
|
|
|
|
|
400
|
6
|
50
|
|
|
|
|
git_vector_set_cmp(&index->reuc, ignore_case ? reuc_icmp : reuc_cmp); |
401
|
6
|
|
|
|
|
|
git_vector_sort(&index->reuc); |
402
|
6
|
|
|
|
|
|
} |
403
|
|
|
|
|
|
|
|
404
|
47
|
|
|
|
|
|
int git_index_open(git_index **index_out, const char *index_path) |
405
|
|
|
|
|
|
|
{ |
406
|
|
|
|
|
|
|
git_index *index; |
407
|
47
|
|
|
|
|
|
int error = -1; |
408
|
|
|
|
|
|
|
|
409
|
47
|
50
|
|
|
|
|
assert(index_out); |
410
|
|
|
|
|
|
|
|
411
|
47
|
|
|
|
|
|
index = git__calloc(1, sizeof(git_index)); |
412
|
47
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(index); |
413
|
|
|
|
|
|
|
|
414
|
47
|
|
|
|
|
|
git_pool_init(&index->tree_pool, 1); |
415
|
|
|
|
|
|
|
|
416
|
47
|
100
|
|
|
|
|
if (index_path != NULL) { |
417
|
16
|
|
|
|
|
|
index->index_file_path = git__strdup(index_path); |
418
|
16
|
50
|
|
|
|
|
if (!index->index_file_path) |
419
|
0
|
|
|
|
|
|
goto fail; |
420
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
/* Check if index file is stored on disk already */ |
422
|
16
|
100
|
|
|
|
|
if (git_path_exists(index->index_file_path) == true) |
423
|
10
|
|
|
|
|
|
index->on_disk = 1; |
424
|
|
|
|
|
|
|
} |
425
|
|
|
|
|
|
|
|
426
|
94
|
|
|
|
|
|
if (git_vector_init(&index->entries, 32, git_index_entry_cmp) < 0 || |
427
|
94
|
50
|
|
|
|
|
git_idxmap_new(&index->entries_map) < 0 || |
428
|
94
|
50
|
|
|
|
|
git_vector_init(&index->names, 8, conflict_name_cmp) < 0 || |
429
|
94
|
50
|
|
|
|
|
git_vector_init(&index->reuc, 8, reuc_cmp) < 0 || |
430
|
47
|
|
|
|
|
|
git_vector_init(&index->deleted, 8, git_index_entry_cmp) < 0) |
431
|
|
|
|
|
|
|
goto fail; |
432
|
|
|
|
|
|
|
|
433
|
47
|
|
|
|
|
|
index->entries_cmp_path = git__strcmp_cb; |
434
|
47
|
|
|
|
|
|
index->entries_search = git_index_entry_srch; |
435
|
47
|
|
|
|
|
|
index->entries_search_path = index_entry_srch_path; |
436
|
47
|
|
|
|
|
|
index->reuc_search = reuc_srch; |
437
|
47
|
|
|
|
|
|
index->version = INDEX_VERSION_NUMBER_DEFAULT; |
438
|
|
|
|
|
|
|
|
439
|
47
|
100
|
|
|
|
|
if (index_path != NULL && (error = git_index_read(index, true)) < 0) |
|
|
50
|
|
|
|
|
|
440
|
0
|
|
|
|
|
|
goto fail; |
441
|
|
|
|
|
|
|
|
442
|
47
|
|
|
|
|
|
*index_out = index; |
443
|
47
|
|
|
|
|
|
GIT_REFCOUNT_INC(index); |
444
|
|
|
|
|
|
|
|
445
|
47
|
|
|
|
|
|
return 0; |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
fail: |
448
|
0
|
|
|
|
|
|
git_pool_clear(&index->tree_pool); |
449
|
0
|
|
|
|
|
|
git_index_free(index); |
450
|
0
|
|
|
|
|
|
return error; |
451
|
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
|
453
|
31
|
|
|
|
|
|
int git_index_new(git_index **out) |
454
|
|
|
|
|
|
|
{ |
455
|
31
|
|
|
|
|
|
return git_index_open(out, NULL); |
456
|
|
|
|
|
|
|
} |
457
|
|
|
|
|
|
|
|
458
|
47
|
|
|
|
|
|
static void index_free(git_index *index) |
459
|
|
|
|
|
|
|
{ |
460
|
|
|
|
|
|
|
/* index iterators increment the refcount of the index, so if we |
461
|
|
|
|
|
|
|
* get here then there should be no outstanding iterators. |
462
|
|
|
|
|
|
|
*/ |
463
|
47
|
50
|
|
|
|
|
assert(!git_atomic_get(&index->readers)); |
464
|
|
|
|
|
|
|
|
465
|
47
|
|
|
|
|
|
git_index_clear(index); |
466
|
47
|
|
|
|
|
|
git_idxmap_free(index->entries_map); |
467
|
47
|
|
|
|
|
|
git_vector_free(&index->entries); |
468
|
47
|
|
|
|
|
|
git_vector_free(&index->names); |
469
|
47
|
|
|
|
|
|
git_vector_free(&index->reuc); |
470
|
47
|
|
|
|
|
|
git_vector_free(&index->deleted); |
471
|
|
|
|
|
|
|
|
472
|
47
|
|
|
|
|
|
git__free(index->index_file_path); |
473
|
|
|
|
|
|
|
|
474
|
47
|
|
|
|
|
|
git__memzero(index, sizeof(*index)); |
475
|
47
|
|
|
|
|
|
git__free(index); |
476
|
47
|
|
|
|
|
|
} |
477
|
|
|
|
|
|
|
|
478
|
923
|
|
|
|
|
|
void git_index_free(git_index *index) |
479
|
|
|
|
|
|
|
{ |
480
|
923
|
100
|
|
|
|
|
if (index == NULL) |
481
|
109
|
|
|
|
|
|
return; |
482
|
|
|
|
|
|
|
|
483
|
814
|
100
|
|
|
|
|
GIT_REFCOUNT_DEC(index, index_free); |
|
|
50
|
|
|
|
|
|
484
|
|
|
|
|
|
|
} |
485
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
/* call with locked index */ |
487
|
71
|
|
|
|
|
|
static void index_free_deleted(git_index *index) |
488
|
|
|
|
|
|
|
{ |
489
|
71
|
|
|
|
|
|
int readers = (int)git_atomic_get(&index->readers); |
490
|
|
|
|
|
|
|
size_t i; |
491
|
|
|
|
|
|
|
|
492
|
71
|
50
|
|
|
|
|
if (readers > 0 || !index->deleted.length) |
|
|
100
|
|
|
|
|
|
493
|
65
|
|
|
|
|
|
return; |
494
|
|
|
|
|
|
|
|
495
|
12
|
100
|
|
|
|
|
for (i = 0; i < index->deleted.length; ++i) { |
496
|
6
|
|
|
|
|
|
git_index_entry *ie = git__swap(index->deleted.contents[i], NULL); |
497
|
6
|
|
|
|
|
|
index_entry_free(ie); |
498
|
|
|
|
|
|
|
} |
499
|
|
|
|
|
|
|
|
500
|
6
|
|
|
|
|
|
git_vector_clear(&index->deleted); |
501
|
|
|
|
|
|
|
} |
502
|
|
|
|
|
|
|
|
503
|
|
|
|
|
|
|
/* call with locked index */ |
504
|
167
|
|
|
|
|
|
static int index_remove_entry(git_index *index, size_t pos) |
505
|
|
|
|
|
|
|
{ |
506
|
167
|
|
|
|
|
|
int error = 0; |
507
|
167
|
|
|
|
|
|
git_index_entry *entry = git_vector_get(&index->entries, pos); |
508
|
|
|
|
|
|
|
|
509
|
167
|
50
|
|
|
|
|
if (entry != NULL) { |
510
|
167
|
|
|
|
|
|
git_tree_cache_invalidate_path(index->tree, entry->path); |
511
|
167
|
|
|
|
|
|
index_map_delete(index->entries_map, entry, index->ignore_case); |
512
|
|
|
|
|
|
|
} |
513
|
|
|
|
|
|
|
|
514
|
167
|
|
|
|
|
|
error = git_vector_remove(&index->entries, pos); |
515
|
|
|
|
|
|
|
|
516
|
167
|
50
|
|
|
|
|
if (!error) { |
517
|
167
|
100
|
|
|
|
|
if (git_atomic_get(&index->readers) > 0) { |
518
|
6
|
|
|
|
|
|
error = git_vector_insert(&index->deleted, entry); |
519
|
|
|
|
|
|
|
} else { |
520
|
161
|
|
|
|
|
|
index_entry_free(entry); |
521
|
|
|
|
|
|
|
} |
522
|
|
|
|
|
|
|
|
523
|
167
|
|
|
|
|
|
index->dirty = 1; |
524
|
|
|
|
|
|
|
} |
525
|
|
|
|
|
|
|
|
526
|
167
|
|
|
|
|
|
return error; |
527
|
|
|
|
|
|
|
} |
528
|
|
|
|
|
|
|
|
529
|
71
|
|
|
|
|
|
int git_index_clear(git_index *index) |
530
|
|
|
|
|
|
|
{ |
531
|
71
|
|
|
|
|
|
int error = 0; |
532
|
|
|
|
|
|
|
|
533
|
71
|
50
|
|
|
|
|
assert(index); |
534
|
|
|
|
|
|
|
|
535
|
71
|
|
|
|
|
|
index->dirty = 1; |
536
|
71
|
|
|
|
|
|
index->tree = NULL; |
537
|
71
|
|
|
|
|
|
git_pool_clear(&index->tree_pool); |
538
|
|
|
|
|
|
|
|
539
|
71
|
|
|
|
|
|
git_idxmap_clear(index->entries_map); |
540
|
211
|
50
|
|
|
|
|
while (!error && index->entries.length > 0) |
|
|
100
|
|
|
|
|
|
541
|
140
|
|
|
|
|
|
error = index_remove_entry(index, index->entries.length - 1); |
542
|
|
|
|
|
|
|
|
543
|
71
|
50
|
|
|
|
|
if (error) |
544
|
0
|
|
|
|
|
|
goto done; |
545
|
|
|
|
|
|
|
|
546
|
71
|
|
|
|
|
|
index_free_deleted(index); |
547
|
|
|
|
|
|
|
|
548
|
71
|
50
|
|
|
|
|
if ((error = git_index_name_clear(index)) < 0 || |
|
|
50
|
|
|
|
|
|
549
|
|
|
|
|
|
|
(error = git_index_reuc_clear(index)) < 0) |
550
|
|
|
|
|
|
|
goto done; |
551
|
|
|
|
|
|
|
|
552
|
71
|
|
|
|
|
|
git_futils_filestamp_set(&index->stamp, NULL); |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
done: |
555
|
71
|
|
|
|
|
|
return error; |
556
|
|
|
|
|
|
|
} |
557
|
|
|
|
|
|
|
|
558
|
0
|
|
|
|
|
|
static int create_index_error(int error, const char *msg) |
559
|
|
|
|
|
|
|
{ |
560
|
0
|
|
|
|
|
|
git_error_set_str(GIT_ERROR_INDEX, msg); |
561
|
0
|
|
|
|
|
|
return error; |
562
|
|
|
|
|
|
|
} |
563
|
|
|
|
|
|
|
|
564
|
16
|
|
|
|
|
|
int git_index_set_caps(git_index *index, int caps) |
565
|
|
|
|
|
|
|
{ |
566
|
|
|
|
|
|
|
unsigned int old_ignore_case; |
567
|
|
|
|
|
|
|
|
568
|
16
|
50
|
|
|
|
|
assert(index); |
569
|
|
|
|
|
|
|
|
570
|
16
|
|
|
|
|
|
old_ignore_case = index->ignore_case; |
571
|
|
|
|
|
|
|
|
572
|
16
|
50
|
|
|
|
|
if (caps == GIT_INDEX_CAPABILITY_FROM_OWNER) { |
573
|
16
|
|
|
|
|
|
git_repository *repo = INDEX_OWNER(index); |
574
|
|
|
|
|
|
|
int val; |
575
|
|
|
|
|
|
|
|
576
|
16
|
50
|
|
|
|
|
if (!repo) |
577
|
0
|
|
|
|
|
|
return create_index_error( |
578
|
|
|
|
|
|
|
-1, "cannot access repository to set index caps"); |
579
|
|
|
|
|
|
|
|
580
|
16
|
50
|
|
|
|
|
if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_IGNORECASE)) |
581
|
16
|
|
|
|
|
|
index->ignore_case = (val != 0); |
582
|
16
|
50
|
|
|
|
|
if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_FILEMODE)) |
583
|
16
|
|
|
|
|
|
index->distrust_filemode = (val == 0); |
584
|
16
|
50
|
|
|
|
|
if (!git_repository__configmap_lookup(&val, repo, GIT_CONFIGMAP_SYMLINKS)) |
585
|
16
|
|
|
|
|
|
index->no_symlinks = (val == 0); |
586
|
|
|
|
|
|
|
} |
587
|
|
|
|
|
|
|
else { |
588
|
0
|
|
|
|
|
|
index->ignore_case = ((caps & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0); |
589
|
0
|
|
|
|
|
|
index->distrust_filemode = ((caps & GIT_INDEX_CAPABILITY_NO_FILEMODE) != 0); |
590
|
0
|
|
|
|
|
|
index->no_symlinks = ((caps & GIT_INDEX_CAPABILITY_NO_SYMLINKS) != 0); |
591
|
|
|
|
|
|
|
} |
592
|
|
|
|
|
|
|
|
593
|
16
|
50
|
|
|
|
|
if (old_ignore_case != index->ignore_case) { |
594
|
0
|
|
|
|
|
|
git_index__set_ignore_case(index, (bool)index->ignore_case); |
595
|
|
|
|
|
|
|
} |
596
|
|
|
|
|
|
|
|
597
|
16
|
|
|
|
|
|
return 0; |
598
|
|
|
|
|
|
|
} |
599
|
|
|
|
|
|
|
|
600
|
1
|
|
|
|
|
|
int git_index_caps(const git_index *index) |
601
|
|
|
|
|
|
|
{ |
602
|
1
|
|
|
|
|
|
return ((index->ignore_case ? GIT_INDEX_CAPABILITY_IGNORE_CASE : 0) | |
603
|
1
|
50
|
|
|
|
|
(index->distrust_filemode ? GIT_INDEX_CAPABILITY_NO_FILEMODE : 0) | |
604
|
1
|
50
|
|
|
|
|
(index->no_symlinks ? GIT_INDEX_CAPABILITY_NO_SYMLINKS : 0)); |
605
|
|
|
|
|
|
|
} |
606
|
|
|
|
|
|
|
|
607
|
1
|
|
|
|
|
|
const git_oid *git_index_checksum(git_index *index) |
608
|
|
|
|
|
|
|
{ |
609
|
1
|
|
|
|
|
|
return &index->checksum; |
610
|
|
|
|
|
|
|
} |
611
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
/** |
613
|
|
|
|
|
|
|
* Returns 1 for changed, 0 for not changed and <0 for errors |
614
|
|
|
|
|
|
|
*/ |
615
|
135
|
|
|
|
|
|
static int compare_checksum(git_index *index) |
616
|
|
|
|
|
|
|
{ |
617
|
|
|
|
|
|
|
int fd; |
618
|
|
|
|
|
|
|
ssize_t bytes_read; |
619
|
135
|
|
|
|
|
|
git_oid checksum = {{ 0 }}; |
620
|
|
|
|
|
|
|
|
621
|
135
|
50
|
|
|
|
|
if ((fd = p_open(index->index_file_path, O_RDONLY)) < 0) |
622
|
0
|
|
|
|
|
|
return fd; |
623
|
|
|
|
|
|
|
|
624
|
135
|
50
|
|
|
|
|
if (p_lseek(fd, -20, SEEK_END) < 0) { |
625
|
0
|
|
|
|
|
|
p_close(fd); |
626
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to seek to end of file"); |
627
|
0
|
|
|
|
|
|
return -1; |
628
|
|
|
|
|
|
|
} |
629
|
|
|
|
|
|
|
|
630
|
135
|
|
|
|
|
|
bytes_read = p_read(fd, &checksum, GIT_OID_RAWSZ); |
631
|
135
|
|
|
|
|
|
p_close(fd); |
632
|
|
|
|
|
|
|
|
633
|
135
|
50
|
|
|
|
|
if (bytes_read < 0) |
634
|
0
|
|
|
|
|
|
return -1; |
635
|
|
|
|
|
|
|
|
636
|
135
|
|
|
|
|
|
return !!git_oid_cmp(&checksum, &index->checksum); |
637
|
|
|
|
|
|
|
} |
638
|
|
|
|
|
|
|
|
639
|
152
|
|
|
|
|
|
int git_index_read(git_index *index, int force) |
640
|
|
|
|
|
|
|
{ |
641
|
152
|
|
|
|
|
|
int error = 0, updated; |
642
|
152
|
|
|
|
|
|
git_buf buffer = GIT_BUF_INIT; |
643
|
152
|
|
|
|
|
|
git_futils_filestamp stamp = index->stamp; |
644
|
|
|
|
|
|
|
|
645
|
152
|
50
|
|
|
|
|
if (!index->index_file_path) |
646
|
0
|
|
|
|
|
|
return create_index_error(-1, |
647
|
|
|
|
|
|
|
"failed to read index: The index is in-memory only"); |
648
|
|
|
|
|
|
|
|
649
|
152
|
|
|
|
|
|
index->on_disk = git_path_exists(index->index_file_path); |
650
|
|
|
|
|
|
|
|
651
|
152
|
100
|
|
|
|
|
if (!index->on_disk) { |
652
|
17
|
100
|
|
|
|
|
if (force && (error = git_index_clear(index)) < 0) |
|
|
50
|
|
|
|
|
|
653
|
0
|
|
|
|
|
|
return error; |
654
|
|
|
|
|
|
|
|
655
|
17
|
|
|
|
|
|
index->dirty = 0; |
656
|
17
|
|
|
|
|
|
return 0; |
657
|
|
|
|
|
|
|
} |
658
|
|
|
|
|
|
|
|
659
|
135
|
50
|
|
|
|
|
if ((updated = git_futils_filestamp_check(&stamp, index->index_file_path) < 0) || |
|
|
50
|
|
|
|
|
|
660
|
|
|
|
|
|
|
((updated = compare_checksum(index)) < 0)) { |
661
|
0
|
|
|
|
|
|
git_error_set( |
662
|
|
|
|
|
|
|
GIT_ERROR_INDEX, |
663
|
|
|
|
|
|
|
"failed to read index: '%s' no longer exists", |
664
|
|
|
|
|
|
|
index->index_file_path); |
665
|
0
|
|
|
|
|
|
return updated; |
666
|
|
|
|
|
|
|
} |
667
|
|
|
|
|
|
|
|
668
|
135
|
100
|
|
|
|
|
if (!updated && !force) |
|
|
100
|
|
|
|
|
|
669
|
124
|
|
|
|
|
|
return 0; |
670
|
|
|
|
|
|
|
|
671
|
11
|
|
|
|
|
|
error = git_futils_readbuffer(&buffer, index->index_file_path); |
672
|
11
|
50
|
|
|
|
|
if (error < 0) |
673
|
0
|
|
|
|
|
|
return error; |
674
|
|
|
|
|
|
|
|
675
|
11
|
|
|
|
|
|
index->tree = NULL; |
676
|
11
|
|
|
|
|
|
git_pool_clear(&index->tree_pool); |
677
|
|
|
|
|
|
|
|
678
|
11
|
|
|
|
|
|
error = git_index_clear(index); |
679
|
|
|
|
|
|
|
|
680
|
11
|
50
|
|
|
|
|
if (!error) |
681
|
11
|
|
|
|
|
|
error = parse_index(index, buffer.ptr, buffer.size); |
682
|
|
|
|
|
|
|
|
683
|
11
|
50
|
|
|
|
|
if (!error) { |
684
|
11
|
|
|
|
|
|
git_futils_filestamp_set(&index->stamp, &stamp); |
685
|
11
|
|
|
|
|
|
index->dirty = 0; |
686
|
|
|
|
|
|
|
} |
687
|
|
|
|
|
|
|
|
688
|
11
|
|
|
|
|
|
git_buf_dispose(&buffer); |
689
|
152
|
|
|
|
|
|
return error; |
690
|
|
|
|
|
|
|
} |
691
|
|
|
|
|
|
|
|
692
|
89
|
|
|
|
|
|
int git_index_read_safely(git_index *index) |
693
|
|
|
|
|
|
|
{ |
694
|
89
|
50
|
|
|
|
|
if (git_index__enforce_unsaved_safety && index->dirty) { |
|
|
0
|
|
|
|
|
|
695
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, |
696
|
|
|
|
|
|
|
"the index has unsaved changes that would be overwritten by this operation"); |
697
|
0
|
|
|
|
|
|
return GIT_EINDEXDIRTY; |
698
|
|
|
|
|
|
|
} |
699
|
|
|
|
|
|
|
|
700
|
89
|
|
|
|
|
|
return git_index_read(index, false); |
701
|
|
|
|
|
|
|
} |
702
|
|
|
|
|
|
|
|
703
|
0
|
|
|
|
|
|
int git_index__changed_relative_to( |
704
|
|
|
|
|
|
|
git_index *index, const git_oid *checksum) |
705
|
|
|
|
|
|
|
{ |
706
|
|
|
|
|
|
|
/* attempt to update index (ignoring errors) */ |
707
|
0
|
0
|
|
|
|
|
if (git_index_read(index, false) < 0) |
708
|
0
|
|
|
|
|
|
git_error_clear(); |
709
|
|
|
|
|
|
|
|
710
|
0
|
|
|
|
|
|
return !!git_oid_cmp(&index->checksum, checksum); |
711
|
|
|
|
|
|
|
} |
712
|
|
|
|
|
|
|
|
713
|
111
|
|
|
|
|
|
static bool is_racy_entry(git_index *index, const git_index_entry *entry) |
714
|
|
|
|
|
|
|
{ |
715
|
|
|
|
|
|
|
/* Git special-cases submodules in the check */ |
716
|
111
|
50
|
|
|
|
|
if (S_ISGITLINK(entry->mode)) |
717
|
0
|
|
|
|
|
|
return false; |
718
|
|
|
|
|
|
|
|
719
|
111
|
|
|
|
|
|
return git_index_entry_newer_than_index(entry, index); |
720
|
|
|
|
|
|
|
} |
721
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
/* |
723
|
|
|
|
|
|
|
* Force the next diff to take a look at those entries which have the |
724
|
|
|
|
|
|
|
* same timestamp as the current index. |
725
|
|
|
|
|
|
|
*/ |
726
|
83
|
|
|
|
|
|
static int truncate_racily_clean(git_index *index) |
727
|
|
|
|
|
|
|
{ |
728
|
|
|
|
|
|
|
size_t i; |
729
|
|
|
|
|
|
|
int error; |
730
|
|
|
|
|
|
|
git_index_entry *entry; |
731
|
83
|
|
|
|
|
|
git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; |
732
|
83
|
|
|
|
|
|
git_diff *diff = NULL; |
733
|
83
|
|
|
|
|
|
git_vector paths = GIT_VECTOR_INIT; |
734
|
|
|
|
|
|
|
git_diff_delta *delta; |
735
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
/* Nothing to do if there's no repo to talk about */ |
737
|
83
|
50
|
|
|
|
|
if (!INDEX_OWNER(index)) |
738
|
0
|
|
|
|
|
|
return 0; |
739
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
/* If there's no workdir, we can't know where to even check */ |
741
|
83
|
50
|
|
|
|
|
if (!git_repository_workdir(INDEX_OWNER(index))) |
742
|
0
|
|
|
|
|
|
return 0; |
743
|
|
|
|
|
|
|
|
744
|
83
|
|
|
|
|
|
diff_opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE | GIT_DIFF_IGNORE_SUBMODULES | GIT_DIFF_DISABLE_PATHSPEC_MATCH; |
745
|
254
|
100
|
|
|
|
|
git_vector_foreach(&index->entries, i, entry) { |
746
|
282
|
|
|
|
|
|
if ((entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE) == 0 && |
747
|
111
|
|
|
|
|
|
is_racy_entry(index, entry)) |
748
|
27
|
|
|
|
|
|
git_vector_insert(&paths, (char *)entry->path); |
749
|
|
|
|
|
|
|
} |
750
|
|
|
|
|
|
|
|
751
|
83
|
100
|
|
|
|
|
if (paths.length == 0) |
752
|
61
|
|
|
|
|
|
goto done; |
753
|
|
|
|
|
|
|
|
754
|
22
|
|
|
|
|
|
diff_opts.pathspec.count = paths.length; |
755
|
22
|
|
|
|
|
|
diff_opts.pathspec.strings = (char **)paths.contents; |
756
|
|
|
|
|
|
|
|
757
|
22
|
50
|
|
|
|
|
if ((error = git_diff_index_to_workdir(&diff, INDEX_OWNER(index), index, &diff_opts)) < 0) |
758
|
0
|
|
|
|
|
|
return error; |
759
|
|
|
|
|
|
|
|
760
|
22
|
50
|
|
|
|
|
git_vector_foreach(&diff->deltas, i, delta) { |
761
|
0
|
|
|
|
|
|
entry = (git_index_entry *)git_index_get_bypath(index, delta->old_file.path, 0); |
762
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
/* Ensure that we have a stage 0 for this file (ie, it's not a |
764
|
|
|
|
|
|
|
* conflict), otherwise smudging it is quite pointless. |
765
|
|
|
|
|
|
|
*/ |
766
|
0
|
0
|
|
|
|
|
if (entry) { |
767
|
0
|
|
|
|
|
|
entry->file_size = 0; |
768
|
0
|
|
|
|
|
|
index->dirty = 1; |
769
|
|
|
|
|
|
|
} |
770
|
|
|
|
|
|
|
} |
771
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
done: |
773
|
83
|
|
|
|
|
|
git_diff_free(diff); |
774
|
83
|
|
|
|
|
|
git_vector_free(&paths); |
775
|
83
|
|
|
|
|
|
return 0; |
776
|
|
|
|
|
|
|
} |
777
|
|
|
|
|
|
|
|
778
|
3
|
|
|
|
|
|
unsigned git_index_version(git_index *index) |
779
|
|
|
|
|
|
|
{ |
780
|
3
|
50
|
|
|
|
|
assert(index); |
781
|
|
|
|
|
|
|
|
782
|
3
|
|
|
|
|
|
return index->version; |
783
|
|
|
|
|
|
|
} |
784
|
|
|
|
|
|
|
|
785
|
1
|
|
|
|
|
|
int git_index_set_version(git_index *index, unsigned int version) |
786
|
|
|
|
|
|
|
{ |
787
|
1
|
50
|
|
|
|
|
assert(index); |
788
|
|
|
|
|
|
|
|
789
|
1
|
50
|
|
|
|
|
if (version < INDEX_VERSION_NUMBER_LB || |
|
|
50
|
|
|
|
|
|
790
|
1
|
|
|
|
|
|
version > INDEX_VERSION_NUMBER_UB) { |
791
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "invalid version number"); |
792
|
0
|
|
|
|
|
|
return -1; |
793
|
|
|
|
|
|
|
} |
794
|
|
|
|
|
|
|
|
795
|
1
|
|
|
|
|
|
index->version = version; |
796
|
|
|
|
|
|
|
|
797
|
1
|
|
|
|
|
|
return 0; |
798
|
|
|
|
|
|
|
} |
799
|
|
|
|
|
|
|
|
800
|
83
|
|
|
|
|
|
int git_index_write(git_index *index) |
801
|
|
|
|
|
|
|
{ |
802
|
83
|
|
|
|
|
|
git_indexwriter writer = GIT_INDEXWRITER_INIT; |
803
|
|
|
|
|
|
|
int error; |
804
|
|
|
|
|
|
|
|
805
|
83
|
|
|
|
|
|
truncate_racily_clean(index); |
806
|
|
|
|
|
|
|
|
807
|
83
|
50
|
|
|
|
|
if ((error = git_indexwriter_init(&writer, index)) == 0 && |
|
|
50
|
|
|
|
|
|
808
|
|
|
|
|
|
|
(error = git_indexwriter_commit(&writer)) == 0) |
809
|
83
|
|
|
|
|
|
index->dirty = 0; |
810
|
|
|
|
|
|
|
|
811
|
83
|
|
|
|
|
|
git_indexwriter_cleanup(&writer); |
812
|
|
|
|
|
|
|
|
813
|
83
|
|
|
|
|
|
return error; |
814
|
|
|
|
|
|
|
} |
815
|
|
|
|
|
|
|
|
816
|
3
|
|
|
|
|
|
const char * git_index_path(const git_index *index) |
817
|
|
|
|
|
|
|
{ |
818
|
3
|
50
|
|
|
|
|
assert(index); |
819
|
3
|
|
|
|
|
|
return index->index_file_path; |
820
|
|
|
|
|
|
|
} |
821
|
|
|
|
|
|
|
|
822
|
38
|
|
|
|
|
|
int git_index_write_tree(git_oid *oid, git_index *index) |
823
|
|
|
|
|
|
|
{ |
824
|
|
|
|
|
|
|
git_repository *repo; |
825
|
|
|
|
|
|
|
|
826
|
38
|
50
|
|
|
|
|
assert(oid && index); |
|
|
50
|
|
|
|
|
|
827
|
|
|
|
|
|
|
|
828
|
38
|
|
|
|
|
|
repo = INDEX_OWNER(index); |
829
|
|
|
|
|
|
|
|
830
|
38
|
50
|
|
|
|
|
if (repo == NULL) |
831
|
0
|
|
|
|
|
|
return create_index_error(-1, "Failed to write tree. " |
832
|
|
|
|
|
|
|
"the index file is not backed up by an existing repository"); |
833
|
|
|
|
|
|
|
|
834
|
38
|
|
|
|
|
|
return git_tree__write_index(oid, index, repo); |
835
|
|
|
|
|
|
|
} |
836
|
|
|
|
|
|
|
|
837
|
18
|
|
|
|
|
|
int git_index_write_tree_to( |
838
|
|
|
|
|
|
|
git_oid *oid, git_index *index, git_repository *repo) |
839
|
|
|
|
|
|
|
{ |
840
|
18
|
50
|
|
|
|
|
assert(oid && index && repo); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
841
|
18
|
|
|
|
|
|
return git_tree__write_index(oid, index, repo); |
842
|
|
|
|
|
|
|
} |
843
|
|
|
|
|
|
|
|
844
|
270
|
|
|
|
|
|
size_t git_index_entrycount(const git_index *index) |
845
|
|
|
|
|
|
|
{ |
846
|
270
|
50
|
|
|
|
|
assert(index); |
847
|
270
|
|
|
|
|
|
return index->entries.length; |
848
|
|
|
|
|
|
|
} |
849
|
|
|
|
|
|
|
|
850
|
419
|
|
|
|
|
|
const git_index_entry *git_index_get_byindex( |
851
|
|
|
|
|
|
|
git_index *index, size_t n) |
852
|
|
|
|
|
|
|
{ |
853
|
419
|
50
|
|
|
|
|
assert(index); |
854
|
419
|
|
|
|
|
|
git_vector_sort(&index->entries); |
855
|
419
|
|
|
|
|
|
return git_vector_get(&index->entries, n); |
856
|
|
|
|
|
|
|
} |
857
|
|
|
|
|
|
|
|
858
|
170
|
|
|
|
|
|
const git_index_entry *git_index_get_bypath( |
859
|
|
|
|
|
|
|
git_index *index, const char *path, int stage) |
860
|
|
|
|
|
|
|
{ |
861
|
170
|
|
|
|
|
|
git_index_entry key = {{ 0 }}; |
862
|
|
|
|
|
|
|
git_index_entry *value; |
863
|
|
|
|
|
|
|
|
864
|
170
|
50
|
|
|
|
|
assert(index); |
865
|
|
|
|
|
|
|
|
866
|
170
|
|
|
|
|
|
key.path = path; |
867
|
170
|
|
|
|
|
|
GIT_INDEX_ENTRY_STAGE_SET(&key, stage); |
868
|
|
|
|
|
|
|
|
869
|
170
|
50
|
|
|
|
|
if (index->ignore_case) |
870
|
0
|
|
|
|
|
|
value = git_idxmap_icase_get((git_idxmap_icase *) index->entries_map, &key); |
871
|
|
|
|
|
|
|
else |
872
|
170
|
|
|
|
|
|
value = git_idxmap_get(index->entries_map, &key); |
873
|
|
|
|
|
|
|
|
874
|
170
|
100
|
|
|
|
|
if (!value) { |
875
|
23
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "index does not contain '%s'", path); |
876
|
23
|
|
|
|
|
|
return NULL; |
877
|
|
|
|
|
|
|
} |
878
|
|
|
|
|
|
|
|
879
|
170
|
|
|
|
|
|
return value; |
880
|
|
|
|
|
|
|
} |
881
|
|
|
|
|
|
|
|
882
|
91
|
|
|
|
|
|
void git_index_entry__init_from_stat( |
883
|
|
|
|
|
|
|
git_index_entry *entry, struct stat *st, bool trust_mode) |
884
|
|
|
|
|
|
|
{ |
885
|
91
|
|
|
|
|
|
entry->ctime.seconds = (int32_t)st->st_ctime; |
886
|
91
|
|
|
|
|
|
entry->mtime.seconds = (int32_t)st->st_mtime; |
887
|
|
|
|
|
|
|
#if defined(GIT_USE_NSEC) |
888
|
|
|
|
|
|
|
entry->mtime.nanoseconds = st->st_mtime_nsec; |
889
|
|
|
|
|
|
|
entry->ctime.nanoseconds = st->st_ctime_nsec; |
890
|
|
|
|
|
|
|
#endif |
891
|
91
|
|
|
|
|
|
entry->dev = st->st_rdev; |
892
|
91
|
|
|
|
|
|
entry->ino = st->st_ino; |
893
|
0
|
0
|
|
|
|
|
entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ? |
894
|
91
|
50
|
|
|
|
|
git_index__create_mode(0666) : git_index__create_mode(st->st_mode); |
895
|
91
|
|
|
|
|
|
entry->uid = st->st_uid; |
896
|
91
|
|
|
|
|
|
entry->gid = st->st_gid; |
897
|
91
|
|
|
|
|
|
entry->file_size = (uint32_t)st->st_size; |
898
|
91
|
|
|
|
|
|
} |
899
|
|
|
|
|
|
|
|
900
|
200
|
|
|
|
|
|
static void index_entry_adjust_namemask( |
901
|
|
|
|
|
|
|
git_index_entry *entry, |
902
|
|
|
|
|
|
|
size_t path_length) |
903
|
|
|
|
|
|
|
{ |
904
|
200
|
|
|
|
|
|
entry->flags &= ~GIT_INDEX_ENTRY_NAMEMASK; |
905
|
|
|
|
|
|
|
|
906
|
200
|
50
|
|
|
|
|
if (path_length < GIT_INDEX_ENTRY_NAMEMASK) |
907
|
200
|
|
|
|
|
|
entry->flags |= path_length & GIT_INDEX_ENTRY_NAMEMASK; |
908
|
|
|
|
|
|
|
else |
909
|
0
|
|
|
|
|
|
entry->flags |= GIT_INDEX_ENTRY_NAMEMASK; |
910
|
200
|
|
|
|
|
|
} |
911
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
/* When `from_workdir` is true, we will validate the paths to avoid placing |
913
|
|
|
|
|
|
|
* paths that are invalid for the working directory on the current filesystem |
914
|
|
|
|
|
|
|
* (eg, on Windows, we will disallow `GIT~1`, `AUX`, `COM1`, etc). This |
915
|
|
|
|
|
|
|
* function will *always* prevent `.git` and directory traversal `../` from |
916
|
|
|
|
|
|
|
* being added to the index. |
917
|
|
|
|
|
|
|
*/ |
918
|
228
|
|
|
|
|
|
static int index_entry_create( |
919
|
|
|
|
|
|
|
git_index_entry **out, |
920
|
|
|
|
|
|
|
git_repository *repo, |
921
|
|
|
|
|
|
|
const char *path, |
922
|
|
|
|
|
|
|
struct stat *st, |
923
|
|
|
|
|
|
|
bool from_workdir) |
924
|
|
|
|
|
|
|
{ |
925
|
228
|
|
|
|
|
|
size_t pathlen = strlen(path), alloclen; |
926
|
|
|
|
|
|
|
struct entry_internal *entry; |
927
|
228
|
|
|
|
|
|
unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS; |
928
|
228
|
|
|
|
|
|
uint16_t mode = 0; |
929
|
|
|
|
|
|
|
|
930
|
|
|
|
|
|
|
/* always reject placing `.git` in the index and directory traversal. |
931
|
|
|
|
|
|
|
* when requested, disallow platform-specific filenames and upgrade to |
932
|
|
|
|
|
|
|
* the platform-specific `.git` tests (eg, `git~1`, etc). |
933
|
|
|
|
|
|
|
*/ |
934
|
228
|
100
|
|
|
|
|
if (from_workdir) |
935
|
52
|
|
|
|
|
|
path_valid_flags |= GIT_PATH_REJECT_WORKDIR_DEFAULTS; |
936
|
228
|
100
|
|
|
|
|
if (st) |
937
|
52
|
|
|
|
|
|
mode = st->st_mode; |
938
|
|
|
|
|
|
|
|
939
|
228
|
50
|
|
|
|
|
if (!git_path_isvalid(repo, path, mode, path_valid_flags)) { |
940
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "invalid path: '%s'", path); |
941
|
0
|
|
|
|
|
|
return -1; |
942
|
|
|
|
|
|
|
} |
943
|
|
|
|
|
|
|
|
944
|
228
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(struct entry_internal), pathlen); |
|
|
50
|
|
|
|
|
|
945
|
228
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); |
|
|
50
|
|
|
|
|
|
946
|
228
|
|
|
|
|
|
entry = git__calloc(1, alloclen); |
947
|
228
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(entry); |
948
|
|
|
|
|
|
|
|
949
|
228
|
|
|
|
|
|
entry->pathlen = pathlen; |
950
|
228
|
|
|
|
|
|
memcpy(entry->path, path, pathlen); |
951
|
228
|
|
|
|
|
|
entry->entry.path = entry->path; |
952
|
|
|
|
|
|
|
|
953
|
228
|
|
|
|
|
|
*out = (git_index_entry *)entry; |
954
|
228
|
|
|
|
|
|
return 0; |
955
|
|
|
|
|
|
|
} |
956
|
|
|
|
|
|
|
|
957
|
52
|
|
|
|
|
|
static int index_entry_init( |
958
|
|
|
|
|
|
|
git_index_entry **entry_out, |
959
|
|
|
|
|
|
|
git_index *index, |
960
|
|
|
|
|
|
|
const char *rel_path) |
961
|
|
|
|
|
|
|
{ |
962
|
52
|
|
|
|
|
|
int error = 0; |
963
|
52
|
|
|
|
|
|
git_index_entry *entry = NULL; |
964
|
52
|
|
|
|
|
|
git_buf path = GIT_BUF_INIT; |
965
|
|
|
|
|
|
|
struct stat st; |
966
|
|
|
|
|
|
|
git_oid oid; |
967
|
|
|
|
|
|
|
git_repository *repo; |
968
|
|
|
|
|
|
|
|
969
|
52
|
50
|
|
|
|
|
if (INDEX_OWNER(index) == NULL) |
970
|
0
|
|
|
|
|
|
return create_index_error(-1, |
971
|
|
|
|
|
|
|
"could not initialize index entry. " |
972
|
|
|
|
|
|
|
"Index is not backed up by an existing repository."); |
973
|
|
|
|
|
|
|
|
974
|
|
|
|
|
|
|
/* |
975
|
|
|
|
|
|
|
* FIXME: this is duplicated with the work in |
976
|
|
|
|
|
|
|
* git_blob__create_from_paths. It should accept an optional stat |
977
|
|
|
|
|
|
|
* structure so we can pass in the one we have to do here. |
978
|
|
|
|
|
|
|
*/ |
979
|
52
|
|
|
|
|
|
repo = INDEX_OWNER(index); |
980
|
52
|
50
|
|
|
|
|
if (git_repository__ensure_not_bare(repo, "create blob from file") < 0) |
981
|
0
|
|
|
|
|
|
return GIT_EBAREREPO; |
982
|
|
|
|
|
|
|
|
983
|
52
|
50
|
|
|
|
|
if (git_buf_joinpath(&path, git_repository_workdir(repo), rel_path) < 0) |
984
|
0
|
|
|
|
|
|
return -1; |
985
|
|
|
|
|
|
|
|
986
|
52
|
|
|
|
|
|
error = git_path_lstat(path.ptr, &st); |
987
|
52
|
|
|
|
|
|
git_buf_dispose(&path); |
988
|
|
|
|
|
|
|
|
989
|
52
|
50
|
|
|
|
|
if (error < 0) |
990
|
0
|
|
|
|
|
|
return error; |
991
|
|
|
|
|
|
|
|
992
|
52
|
50
|
|
|
|
|
if (index_entry_create(&entry, INDEX_OWNER(index), rel_path, &st, true) < 0) |
993
|
0
|
|
|
|
|
|
return -1; |
994
|
|
|
|
|
|
|
|
995
|
|
|
|
|
|
|
/* write the blob to disk and get the oid and stat info */ |
996
|
52
|
|
|
|
|
|
error = git_blob__create_from_paths( |
997
|
52
|
|
|
|
|
|
&oid, &st, INDEX_OWNER(index), NULL, rel_path, 0, true); |
998
|
|
|
|
|
|
|
|
999
|
52
|
100
|
|
|
|
|
if (error < 0) { |
1000
|
2
|
|
|
|
|
|
index_entry_free(entry); |
1001
|
2
|
|
|
|
|
|
return error; |
1002
|
|
|
|
|
|
|
} |
1003
|
|
|
|
|
|
|
|
1004
|
50
|
|
|
|
|
|
entry->id = oid; |
1005
|
50
|
|
|
|
|
|
git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode); |
1006
|
|
|
|
|
|
|
|
1007
|
50
|
|
|
|
|
|
*entry_out = (git_index_entry *)entry; |
1008
|
52
|
|
|
|
|
|
return 0; |
1009
|
|
|
|
|
|
|
} |
1010
|
|
|
|
|
|
|
|
1011
|
10
|
|
|
|
|
|
static git_index_reuc_entry *reuc_entry_alloc(const char *path) |
1012
|
|
|
|
|
|
|
{ |
1013
|
10
|
|
|
|
|
|
size_t pathlen = strlen(path), |
1014
|
10
|
|
|
|
|
|
structlen = sizeof(struct reuc_entry_internal), |
1015
|
|
|
|
|
|
|
alloclen; |
1016
|
|
|
|
|
|
|
struct reuc_entry_internal *entry; |
1017
|
|
|
|
|
|
|
|
1018
|
20
|
50
|
|
|
|
|
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, structlen, pathlen) || |
|
|
50
|
|
|
|
|
|
1019
|
10
|
|
|
|
|
|
GIT_ADD_SIZET_OVERFLOW(&alloclen, alloclen, 1)) |
1020
|
0
|
|
|
|
|
|
return NULL; |
1021
|
|
|
|
|
|
|
|
1022
|
10
|
|
|
|
|
|
entry = git__calloc(1, alloclen); |
1023
|
10
|
50
|
|
|
|
|
if (!entry) |
1024
|
0
|
|
|
|
|
|
return NULL; |
1025
|
|
|
|
|
|
|
|
1026
|
10
|
|
|
|
|
|
entry->pathlen = pathlen; |
1027
|
10
|
|
|
|
|
|
memcpy(entry->path, path, pathlen); |
1028
|
10
|
|
|
|
|
|
entry->entry.path = entry->path; |
1029
|
|
|
|
|
|
|
|
1030
|
10
|
|
|
|
|
|
return (git_index_reuc_entry *)entry; |
1031
|
|
|
|
|
|
|
} |
1032
|
|
|
|
|
|
|
|
1033
|
10
|
|
|
|
|
|
static int index_entry_reuc_init(git_index_reuc_entry **reuc_out, |
1034
|
|
|
|
|
|
|
const char *path, |
1035
|
|
|
|
|
|
|
int ancestor_mode, const git_oid *ancestor_oid, |
1036
|
|
|
|
|
|
|
int our_mode, const git_oid *our_oid, |
1037
|
|
|
|
|
|
|
int their_mode, const git_oid *their_oid) |
1038
|
|
|
|
|
|
|
{ |
1039
|
10
|
|
|
|
|
|
git_index_reuc_entry *reuc = NULL; |
1040
|
|
|
|
|
|
|
|
1041
|
10
|
50
|
|
|
|
|
assert(reuc_out && path); |
|
|
50
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
|
1043
|
10
|
|
|
|
|
|
*reuc_out = reuc = reuc_entry_alloc(path); |
1044
|
10
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(reuc); |
1045
|
|
|
|
|
|
|
|
1046
|
10
|
50
|
|
|
|
|
if ((reuc->mode[0] = ancestor_mode) > 0) { |
1047
|
10
|
50
|
|
|
|
|
assert(ancestor_oid); |
1048
|
10
|
|
|
|
|
|
git_oid_cpy(&reuc->oid[0], ancestor_oid); |
1049
|
|
|
|
|
|
|
} |
1050
|
|
|
|
|
|
|
|
1051
|
10
|
100
|
|
|
|
|
if ((reuc->mode[1] = our_mode) > 0) { |
1052
|
8
|
50
|
|
|
|
|
assert(our_oid); |
1053
|
8
|
|
|
|
|
|
git_oid_cpy(&reuc->oid[1], our_oid); |
1054
|
|
|
|
|
|
|
} |
1055
|
|
|
|
|
|
|
|
1056
|
10
|
100
|
|
|
|
|
if ((reuc->mode[2] = their_mode) > 0) { |
1057
|
6
|
50
|
|
|
|
|
assert(their_oid); |
1058
|
6
|
|
|
|
|
|
git_oid_cpy(&reuc->oid[2], their_oid); |
1059
|
|
|
|
|
|
|
} |
1060
|
|
|
|
|
|
|
|
1061
|
10
|
|
|
|
|
|
return 0; |
1062
|
|
|
|
|
|
|
} |
1063
|
|
|
|
|
|
|
|
1064
|
231
|
|
|
|
|
|
static void index_entry_cpy( |
1065
|
|
|
|
|
|
|
git_index_entry *tgt, |
1066
|
|
|
|
|
|
|
const git_index_entry *src) |
1067
|
|
|
|
|
|
|
{ |
1068
|
231
|
|
|
|
|
|
const char *tgt_path = tgt->path; |
1069
|
231
|
|
|
|
|
|
memcpy(tgt, src, sizeof(*tgt)); |
1070
|
231
|
|
|
|
|
|
tgt->path = tgt_path; |
1071
|
231
|
|
|
|
|
|
} |
1072
|
|
|
|
|
|
|
|
1073
|
165
|
|
|
|
|
|
static int index_entry_dup( |
1074
|
|
|
|
|
|
|
git_index_entry **out, |
1075
|
|
|
|
|
|
|
git_index *index, |
1076
|
|
|
|
|
|
|
const git_index_entry *src) |
1077
|
|
|
|
|
|
|
{ |
1078
|
165
|
50
|
|
|
|
|
if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0) |
1079
|
0
|
|
|
|
|
|
return -1; |
1080
|
|
|
|
|
|
|
|
1081
|
165
|
|
|
|
|
|
index_entry_cpy(*out, src); |
1082
|
165
|
|
|
|
|
|
return 0; |
1083
|
|
|
|
|
|
|
} |
1084
|
|
|
|
|
|
|
|
1085
|
2
|
|
|
|
|
|
static void index_entry_cpy_nocache( |
1086
|
|
|
|
|
|
|
git_index_entry *tgt, |
1087
|
|
|
|
|
|
|
const git_index_entry *src) |
1088
|
|
|
|
|
|
|
{ |
1089
|
2
|
|
|
|
|
|
git_oid_cpy(&tgt->id, &src->id); |
1090
|
2
|
|
|
|
|
|
tgt->mode = src->mode; |
1091
|
2
|
|
|
|
|
|
tgt->flags = src->flags; |
1092
|
2
|
|
|
|
|
|
tgt->flags_extended = (src->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS); |
1093
|
2
|
|
|
|
|
|
} |
1094
|
|
|
|
|
|
|
|
1095
|
2
|
|
|
|
|
|
static int index_entry_dup_nocache( |
1096
|
|
|
|
|
|
|
git_index_entry **out, |
1097
|
|
|
|
|
|
|
git_index *index, |
1098
|
|
|
|
|
|
|
const git_index_entry *src) |
1099
|
|
|
|
|
|
|
{ |
1100
|
2
|
50
|
|
|
|
|
if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0) |
1101
|
0
|
|
|
|
|
|
return -1; |
1102
|
|
|
|
|
|
|
|
1103
|
2
|
|
|
|
|
|
index_entry_cpy_nocache(*out, src); |
1104
|
2
|
|
|
|
|
|
return 0; |
1105
|
|
|
|
|
|
|
} |
1106
|
|
|
|
|
|
|
|
1107
|
126
|
|
|
|
|
|
static int has_file_name(git_index *index, |
1108
|
|
|
|
|
|
|
const git_index_entry *entry, size_t pos, int ok_to_replace) |
1109
|
|
|
|
|
|
|
{ |
1110
|
126
|
|
|
|
|
|
size_t len = strlen(entry->path); |
1111
|
126
|
|
|
|
|
|
int stage = GIT_INDEX_ENTRY_STAGE(entry); |
1112
|
126
|
|
|
|
|
|
const char *name = entry->path; |
1113
|
|
|
|
|
|
|
|
1114
|
127
|
100
|
|
|
|
|
while (pos < index->entries.length) { |
1115
|
109
|
|
|
|
|
|
struct entry_internal *p = index->entries.contents[pos++]; |
1116
|
|
|
|
|
|
|
|
1117
|
109
|
100
|
|
|
|
|
if (len >= p->pathlen) |
1118
|
105
|
|
|
|
|
|
break; |
1119
|
4
|
100
|
|
|
|
|
if (memcmp(name, p->path, len)) |
1120
|
3
|
|
|
|
|
|
break; |
1121
|
1
|
50
|
|
|
|
|
if (GIT_INDEX_ENTRY_STAGE(&p->entry) != stage) |
1122
|
0
|
|
|
|
|
|
continue; |
1123
|
1
|
50
|
|
|
|
|
if (p->path[len] != '/') |
1124
|
1
|
|
|
|
|
|
continue; |
1125
|
0
|
0
|
|
|
|
|
if (!ok_to_replace) |
1126
|
0
|
|
|
|
|
|
return -1; |
1127
|
|
|
|
|
|
|
|
1128
|
0
|
0
|
|
|
|
|
if (index_remove_entry(index, --pos) < 0) |
1129
|
0
|
|
|
|
|
|
break; |
1130
|
|
|
|
|
|
|
} |
1131
|
126
|
|
|
|
|
|
return 0; |
1132
|
|
|
|
|
|
|
} |
1133
|
|
|
|
|
|
|
|
1134
|
|
|
|
|
|
|
/* |
1135
|
|
|
|
|
|
|
* Do we have another file with a pathname that is a proper |
1136
|
|
|
|
|
|
|
* subset of the name we're trying to add? |
1137
|
|
|
|
|
|
|
*/ |
1138
|
126
|
|
|
|
|
|
static int has_dir_name(git_index *index, |
1139
|
|
|
|
|
|
|
const git_index_entry *entry, int ok_to_replace) |
1140
|
|
|
|
|
|
|
{ |
1141
|
126
|
|
|
|
|
|
int stage = GIT_INDEX_ENTRY_STAGE(entry); |
1142
|
126
|
|
|
|
|
|
const char *name = entry->path; |
1143
|
126
|
|
|
|
|
|
const char *slash = name + strlen(name); |
1144
|
|
|
|
|
|
|
|
1145
|
|
|
|
|
|
|
for (;;) { |
1146
|
|
|
|
|
|
|
size_t len, pos; |
1147
|
|
|
|
|
|
|
|
1148
|
|
|
|
|
|
|
for (;;) { |
1149
|
847
|
100
|
|
|
|
|
if (*--slash == '/') |
1150
|
13
|
|
|
|
|
|
break; |
1151
|
834
|
100
|
|
|
|
|
if (slash <= entry->path) |
1152
|
126
|
|
|
|
|
|
return 0; |
1153
|
710
|
|
|
|
|
|
} |
1154
|
13
|
|
|
|
|
|
len = slash - name; |
1155
|
|
|
|
|
|
|
|
1156
|
13
|
50
|
|
|
|
|
if (!index_find(&pos, index, name, len, stage)) { |
1157
|
0
|
0
|
|
|
|
|
if (!ok_to_replace) |
1158
|
0
|
|
|
|
|
|
return -1; |
1159
|
|
|
|
|
|
|
|
1160
|
0
|
0
|
|
|
|
|
if (index_remove_entry(index, pos) < 0) |
1161
|
0
|
|
|
|
|
|
break; |
1162
|
0
|
|
|
|
|
|
continue; |
1163
|
|
|
|
|
|
|
} |
1164
|
|
|
|
|
|
|
|
1165
|
|
|
|
|
|
|
/* |
1166
|
|
|
|
|
|
|
* Trivial optimization: if we find an entry that |
1167
|
|
|
|
|
|
|
* already matches the sub-directory, then we know |
1168
|
|
|
|
|
|
|
* we're ok, and we can exit. |
1169
|
|
|
|
|
|
|
*/ |
1170
|
18
|
100
|
|
|
|
|
for (; pos < index->entries.length; ++pos) { |
1171
|
7
|
|
|
|
|
|
struct entry_internal *p = index->entries.contents[pos]; |
1172
|
|
|
|
|
|
|
|
1173
|
7
|
50
|
|
|
|
|
if (p->pathlen <= len || |
|
|
100
|
|
|
|
|
|
1174
|
2
|
50
|
|
|
|
|
p->path[len] != '/' || |
1175
|
2
|
|
|
|
|
|
memcmp(p->path, name, len)) |
1176
|
|
|
|
|
|
|
break; /* not our subdirectory */ |
1177
|
|
|
|
|
|
|
|
1178
|
2
|
50
|
|
|
|
|
if (GIT_INDEX_ENTRY_STAGE(&p->entry) == stage) |
1179
|
2
|
|
|
|
|
|
return 0; |
1180
|
|
|
|
|
|
|
} |
1181
|
11
|
|
|
|
|
|
} |
1182
|
|
|
|
|
|
|
|
1183
|
0
|
|
|
|
|
|
return 0; |
1184
|
|
|
|
|
|
|
} |
1185
|
|
|
|
|
|
|
|
1186
|
126
|
|
|
|
|
|
static int check_file_directory_collision(git_index *index, |
1187
|
|
|
|
|
|
|
git_index_entry *entry, size_t pos, int ok_to_replace) |
1188
|
|
|
|
|
|
|
{ |
1189
|
252
|
|
|
|
|
|
if (has_file_name(index, entry, pos, ok_to_replace) < 0 || |
1190
|
126
|
|
|
|
|
|
has_dir_name(index, entry, ok_to_replace) < 0) { |
1191
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, |
1192
|
|
|
|
|
|
|
"'%s' appears as both a file and a directory", entry->path); |
1193
|
0
|
|
|
|
|
|
return -1; |
1194
|
|
|
|
|
|
|
} |
1195
|
|
|
|
|
|
|
|
1196
|
126
|
|
|
|
|
|
return 0; |
1197
|
|
|
|
|
|
|
} |
1198
|
|
|
|
|
|
|
|
1199
|
50
|
|
|
|
|
|
static int canonicalize_directory_path( |
1200
|
|
|
|
|
|
|
git_index *index, |
1201
|
|
|
|
|
|
|
git_index_entry *entry, |
1202
|
|
|
|
|
|
|
git_index_entry *existing) |
1203
|
|
|
|
|
|
|
{ |
1204
|
50
|
|
|
|
|
|
const git_index_entry *match, *best = NULL; |
1205
|
|
|
|
|
|
|
char *search, *sep; |
1206
|
|
|
|
|
|
|
size_t pos, search_len, best_len; |
1207
|
|
|
|
|
|
|
|
1208
|
50
|
50
|
|
|
|
|
if (!index->ignore_case) |
1209
|
50
|
|
|
|
|
|
return 0; |
1210
|
|
|
|
|
|
|
|
1211
|
|
|
|
|
|
|
/* item already exists in the index, simply re-use the existing case */ |
1212
|
0
|
0
|
|
|
|
|
if (existing) { |
1213
|
0
|
|
|
|
|
|
memcpy((char *)entry->path, existing->path, strlen(existing->path)); |
1214
|
0
|
|
|
|
|
|
return 0; |
1215
|
|
|
|
|
|
|
} |
1216
|
|
|
|
|
|
|
|
1217
|
|
|
|
|
|
|
/* nothing to do */ |
1218
|
0
|
0
|
|
|
|
|
if (strchr(entry->path, '/') == NULL) |
1219
|
0
|
|
|
|
|
|
return 0; |
1220
|
|
|
|
|
|
|
|
1221
|
0
|
0
|
|
|
|
|
if ((search = git__strdup(entry->path)) == NULL) |
1222
|
0
|
|
|
|
|
|
return -1; |
1223
|
|
|
|
|
|
|
|
1224
|
|
|
|
|
|
|
/* starting at the parent directory and descending to the root, find the |
1225
|
|
|
|
|
|
|
* common parent directory. |
1226
|
|
|
|
|
|
|
*/ |
1227
|
0
|
0
|
|
|
|
|
while (!best && (sep = strrchr(search, '/'))) { |
|
|
0
|
|
|
|
|
|
1228
|
0
|
|
|
|
|
|
sep[1] = '\0'; |
1229
|
|
|
|
|
|
|
|
1230
|
0
|
|
|
|
|
|
search_len = strlen(search); |
1231
|
|
|
|
|
|
|
|
1232
|
0
|
|
|
|
|
|
git_vector_bsearch2( |
1233
|
|
|
|
|
|
|
&pos, &index->entries, index->entries_search_path, search); |
1234
|
|
|
|
|
|
|
|
1235
|
0
|
0
|
|
|
|
|
while ((match = git_vector_get(&index->entries, pos))) { |
1236
|
0
|
0
|
|
|
|
|
if (GIT_INDEX_ENTRY_STAGE(match) != 0) { |
1237
|
|
|
|
|
|
|
/* conflicts do not contribute to canonical paths */ |
1238
|
0
|
0
|
|
|
|
|
} else if (strncmp(search, match->path, search_len) == 0) { |
1239
|
|
|
|
|
|
|
/* prefer an exact match to the input filename */ |
1240
|
0
|
|
|
|
|
|
best = match; |
1241
|
0
|
|
|
|
|
|
best_len = search_len; |
1242
|
0
|
|
|
|
|
|
break; |
1243
|
0
|
0
|
|
|
|
|
} else if (strncasecmp(search, match->path, search_len) == 0) { |
1244
|
|
|
|
|
|
|
/* continue walking, there may be a path with an exact |
1245
|
|
|
|
|
|
|
* (case sensitive) match later in the index, but use this |
1246
|
|
|
|
|
|
|
* as the best match until that happens. |
1247
|
|
|
|
|
|
|
*/ |
1248
|
0
|
0
|
|
|
|
|
if (!best) { |
1249
|
0
|
|
|
|
|
|
best = match; |
1250
|
0
|
|
|
|
|
|
best_len = search_len; |
1251
|
|
|
|
|
|
|
} |
1252
|
|
|
|
|
|
|
} else { |
1253
|
0
|
|
|
|
|
|
break; |
1254
|
|
|
|
|
|
|
} |
1255
|
|
|
|
|
|
|
|
1256
|
0
|
|
|
|
|
|
pos++; |
1257
|
|
|
|
|
|
|
} |
1258
|
|
|
|
|
|
|
|
1259
|
0
|
|
|
|
|
|
sep[0] = '\0'; |
1260
|
|
|
|
|
|
|
} |
1261
|
|
|
|
|
|
|
|
1262
|
0
|
0
|
|
|
|
|
if (best) |
1263
|
0
|
|
|
|
|
|
memcpy((char *)entry->path, best->path, best_len); |
1264
|
|
|
|
|
|
|
|
1265
|
0
|
|
|
|
|
|
git__free(search); |
1266
|
50
|
|
|
|
|
|
return 0; |
1267
|
|
|
|
|
|
|
} |
1268
|
|
|
|
|
|
|
|
1269
|
0
|
|
|
|
|
|
static int index_no_dups(void **old, void *new) |
1270
|
|
|
|
|
|
|
{ |
1271
|
0
|
|
|
|
|
|
const git_index_entry *entry = new; |
1272
|
|
|
|
|
|
|
GIT_UNUSED(old); |
1273
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "'%s' appears multiple times at stage %d", |
1274
|
0
|
|
|
|
|
|
entry->path, GIT_INDEX_ENTRY_STAGE(entry)); |
1275
|
0
|
|
|
|
|
|
return GIT_EEXISTS; |
1276
|
|
|
|
|
|
|
} |
1277
|
|
|
|
|
|
|
|
1278
|
126
|
|
|
|
|
|
static void index_existing_and_best( |
1279
|
|
|
|
|
|
|
git_index_entry **existing, |
1280
|
|
|
|
|
|
|
size_t *existing_position, |
1281
|
|
|
|
|
|
|
git_index_entry **best, |
1282
|
|
|
|
|
|
|
git_index *index, |
1283
|
|
|
|
|
|
|
const git_index_entry *entry) |
1284
|
|
|
|
|
|
|
{ |
1285
|
|
|
|
|
|
|
git_index_entry *e; |
1286
|
|
|
|
|
|
|
size_t pos; |
1287
|
|
|
|
|
|
|
int error; |
1288
|
|
|
|
|
|
|
|
1289
|
126
|
|
|
|
|
|
error = index_find(&pos, |
1290
|
126
|
|
|
|
|
|
index, entry->path, 0, GIT_INDEX_ENTRY_STAGE(entry)); |
1291
|
|
|
|
|
|
|
|
1292
|
126
|
100
|
|
|
|
|
if (error == 0) { |
1293
|
57
|
|
|
|
|
|
*existing = index->entries.contents[pos]; |
1294
|
57
|
|
|
|
|
|
*existing_position = pos; |
1295
|
57
|
|
|
|
|
|
*best = index->entries.contents[pos]; |
1296
|
57
|
|
|
|
|
|
return; |
1297
|
|
|
|
|
|
|
} |
1298
|
|
|
|
|
|
|
|
1299
|
69
|
|
|
|
|
|
*existing = NULL; |
1300
|
69
|
|
|
|
|
|
*existing_position = 0; |
1301
|
69
|
|
|
|
|
|
*best = NULL; |
1302
|
|
|
|
|
|
|
|
1303
|
69
|
100
|
|
|
|
|
if (GIT_INDEX_ENTRY_STAGE(entry) == 0) { |
1304
|
88
|
100
|
|
|
|
|
for (; pos < index->entries.length; pos++) { |
1305
|
19
|
|
|
|
|
|
int (*strcomp)(const char *a, const char *b) = |
1306
|
19
|
50
|
|
|
|
|
index->ignore_case ? git__strcasecmp : git__strcmp; |
1307
|
|
|
|
|
|
|
|
1308
|
19
|
|
|
|
|
|
e = index->entries.contents[pos]; |
1309
|
|
|
|
|
|
|
|
1310
|
19
|
100
|
|
|
|
|
if (strcomp(entry->path, e->path) != 0) |
1311
|
15
|
|
|
|
|
|
break; |
1312
|
|
|
|
|
|
|
|
1313
|
4
|
100
|
|
|
|
|
if (GIT_INDEX_ENTRY_STAGE(e) == GIT_INDEX_STAGE_ANCESTOR) { |
1314
|
2
|
|
|
|
|
|
*best = e; |
1315
|
2
|
|
|
|
|
|
continue; |
1316
|
|
|
|
|
|
|
} else { |
1317
|
2
|
|
|
|
|
|
*best = e; |
1318
|
2
|
|
|
|
|
|
break; |
1319
|
|
|
|
|
|
|
} |
1320
|
|
|
|
|
|
|
} |
1321
|
|
|
|
|
|
|
} |
1322
|
|
|
|
|
|
|
} |
1323
|
|
|
|
|
|
|
|
1324
|
|
|
|
|
|
|
/* index_insert takes ownership of the new entry - if it can't insert |
1325
|
|
|
|
|
|
|
* it, then it will return an error **and also free the entry**. When |
1326
|
|
|
|
|
|
|
* it replaces an existing entry, it will update the entry_ptr with the |
1327
|
|
|
|
|
|
|
* actual entry in the index (and free the passed in one). |
1328
|
|
|
|
|
|
|
* |
1329
|
|
|
|
|
|
|
* trust_path is whether we use the given path, or whether (on case |
1330
|
|
|
|
|
|
|
* insensitive systems only) we try to canonicalize the given path to |
1331
|
|
|
|
|
|
|
* be within an existing directory. |
1332
|
|
|
|
|
|
|
* |
1333
|
|
|
|
|
|
|
* trust_mode is whether we trust the mode in entry_ptr. |
1334
|
|
|
|
|
|
|
* |
1335
|
|
|
|
|
|
|
* trust_id is whether we trust the id or it should be validated. |
1336
|
|
|
|
|
|
|
*/ |
1337
|
126
|
|
|
|
|
|
static int index_insert( |
1338
|
|
|
|
|
|
|
git_index *index, |
1339
|
|
|
|
|
|
|
git_index_entry **entry_ptr, |
1340
|
|
|
|
|
|
|
int replace, |
1341
|
|
|
|
|
|
|
bool trust_path, |
1342
|
|
|
|
|
|
|
bool trust_mode, |
1343
|
|
|
|
|
|
|
bool trust_id) |
1344
|
|
|
|
|
|
|
{ |
1345
|
|
|
|
|
|
|
git_index_entry *existing, *best, *entry; |
1346
|
|
|
|
|
|
|
size_t path_length, position; |
1347
|
|
|
|
|
|
|
int error; |
1348
|
|
|
|
|
|
|
|
1349
|
126
|
50
|
|
|
|
|
assert(index && entry_ptr); |
|
|
50
|
|
|
|
|
|
1350
|
|
|
|
|
|
|
|
1351
|
126
|
|
|
|
|
|
entry = *entry_ptr; |
1352
|
|
|
|
|
|
|
|
1353
|
|
|
|
|
|
|
/* Make sure that the path length flag is correct */ |
1354
|
126
|
|
|
|
|
|
path_length = ((struct entry_internal *)entry)->pathlen; |
1355
|
126
|
|
|
|
|
|
index_entry_adjust_namemask(entry, path_length); |
1356
|
|
|
|
|
|
|
|
1357
|
|
|
|
|
|
|
/* This entry is now up-to-date and should not be checked for raciness */ |
1358
|
126
|
|
|
|
|
|
entry->flags_extended |= GIT_INDEX_ENTRY_UPTODATE; |
1359
|
|
|
|
|
|
|
|
1360
|
126
|
|
|
|
|
|
git_vector_sort(&index->entries); |
1361
|
|
|
|
|
|
|
|
1362
|
|
|
|
|
|
|
/* |
1363
|
|
|
|
|
|
|
* Look if an entry with this path already exists, either staged, or (if |
1364
|
|
|
|
|
|
|
* this entry is a regular staged item) as the "ours" side of a conflict. |
1365
|
|
|
|
|
|
|
*/ |
1366
|
126
|
|
|
|
|
|
index_existing_and_best(&existing, &position, &best, index, entry); |
1367
|
|
|
|
|
|
|
|
1368
|
|
|
|
|
|
|
/* Update the file mode */ |
1369
|
126
|
|
|
|
|
|
entry->mode = trust_mode ? |
1370
|
126
|
100
|
|
|
|
|
git_index__create_mode(entry->mode) : |
1371
|
50
|
|
|
|
|
|
index_merge_mode(index, best, entry->mode); |
1372
|
|
|
|
|
|
|
|
1373
|
|
|
|
|
|
|
/* Canonicalize the directory name */ |
1374
|
126
|
100
|
|
|
|
|
if (!trust_path && (error = canonicalize_directory_path(index, entry, best)) < 0) |
|
|
50
|
|
|
|
|
|
1375
|
0
|
|
|
|
|
|
goto out; |
1376
|
|
|
|
|
|
|
|
1377
|
|
|
|
|
|
|
/* Ensure that the given id exists (unless it's a submodule) */ |
1378
|
126
|
100
|
|
|
|
|
if (!trust_id && INDEX_OWNER(index) && |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
1379
|
43
|
|
|
|
|
|
(entry->mode & GIT_FILEMODE_COMMIT) != GIT_FILEMODE_COMMIT) { |
1380
|
|
|
|
|
|
|
|
1381
|
43
|
50
|
|
|
|
|
if (!git_object__is_valid(INDEX_OWNER(index), &entry->id, |
1382
|
43
|
|
|
|
|
|
git_object__type_from_filemode(entry->mode))) { |
1383
|
0
|
|
|
|
|
|
error = -1; |
1384
|
0
|
|
|
|
|
|
goto out; |
1385
|
|
|
|
|
|
|
} |
1386
|
|
|
|
|
|
|
} |
1387
|
|
|
|
|
|
|
|
1388
|
|
|
|
|
|
|
/* Look for tree / blob name collisions, removing conflicts if requested */ |
1389
|
126
|
50
|
|
|
|
|
if ((error = check_file_directory_collision(index, entry, position, replace)) < 0) |
1390
|
0
|
|
|
|
|
|
goto out; |
1391
|
|
|
|
|
|
|
|
1392
|
|
|
|
|
|
|
/* |
1393
|
|
|
|
|
|
|
* If we are replacing an existing item, overwrite the existing entry |
1394
|
|
|
|
|
|
|
* and return it in place of the passed in one. |
1395
|
|
|
|
|
|
|
*/ |
1396
|
126
|
100
|
|
|
|
|
if (existing) { |
1397
|
57
|
50
|
|
|
|
|
if (replace) { |
1398
|
57
|
|
|
|
|
|
index_entry_cpy(existing, entry); |
1399
|
|
|
|
|
|
|
|
1400
|
57
|
100
|
|
|
|
|
if (trust_path) |
1401
|
30
|
|
|
|
|
|
memcpy((char *)existing->path, entry->path, strlen(entry->path)); |
1402
|
|
|
|
|
|
|
} |
1403
|
|
|
|
|
|
|
|
1404
|
57
|
|
|
|
|
|
index_entry_free(entry); |
1405
|
57
|
|
|
|
|
|
*entry_ptr = existing; |
1406
|
|
|
|
|
|
|
} else { |
1407
|
|
|
|
|
|
|
/* |
1408
|
|
|
|
|
|
|
* If replace is not requested or no existing entry exists, insert |
1409
|
|
|
|
|
|
|
* at the sorted position. (Since we re-sort after each insert to |
1410
|
|
|
|
|
|
|
* check for dups, this is actually cheaper in the long run.) |
1411
|
|
|
|
|
|
|
*/ |
1412
|
69
|
50
|
|
|
|
|
if ((error = git_vector_insert_sorted(&index->entries, entry, index_no_dups)) < 0 || |
|
|
50
|
|
|
|
|
|
1413
|
69
|
|
|
|
|
|
(error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0) |
1414
|
|
|
|
|
|
|
goto out; |
1415
|
|
|
|
|
|
|
} |
1416
|
|
|
|
|
|
|
|
1417
|
126
|
|
|
|
|
|
index->dirty = 1; |
1418
|
|
|
|
|
|
|
|
1419
|
|
|
|
|
|
|
out: |
1420
|
126
|
50
|
|
|
|
|
if (error < 0) { |
1421
|
0
|
|
|
|
|
|
index_entry_free(*entry_ptr); |
1422
|
0
|
|
|
|
|
|
*entry_ptr = NULL; |
1423
|
|
|
|
|
|
|
} |
1424
|
|
|
|
|
|
|
|
1425
|
126
|
|
|
|
|
|
return error; |
1426
|
|
|
|
|
|
|
} |
1427
|
|
|
|
|
|
|
|
1428
|
63
|
|
|
|
|
|
static int index_conflict_to_reuc(git_index *index, const char *path) |
1429
|
|
|
|
|
|
|
{ |
1430
|
|
|
|
|
|
|
const git_index_entry *conflict_entries[3]; |
1431
|
|
|
|
|
|
|
int ancestor_mode, our_mode, their_mode; |
1432
|
|
|
|
|
|
|
git_oid const *ancestor_oid, *our_oid, *their_oid; |
1433
|
|
|
|
|
|
|
int ret; |
1434
|
|
|
|
|
|
|
|
1435
|
63
|
100
|
|
|
|
|
if ((ret = git_index_conflict_get(&conflict_entries[0], |
1436
|
|
|
|
|
|
|
&conflict_entries[1], &conflict_entries[2], index, path)) < 0) |
1437
|
61
|
|
|
|
|
|
return ret; |
1438
|
|
|
|
|
|
|
|
1439
|
2
|
50
|
|
|
|
|
ancestor_mode = conflict_entries[0] == NULL ? 0 : conflict_entries[0]->mode; |
1440
|
2
|
50
|
|
|
|
|
our_mode = conflict_entries[1] == NULL ? 0 : conflict_entries[1]->mode; |
1441
|
2
|
50
|
|
|
|
|
their_mode = conflict_entries[2] == NULL ? 0 : conflict_entries[2]->mode; |
1442
|
|
|
|
|
|
|
|
1443
|
2
|
50
|
|
|
|
|
ancestor_oid = conflict_entries[0] == NULL ? NULL : &conflict_entries[0]->id; |
1444
|
2
|
50
|
|
|
|
|
our_oid = conflict_entries[1] == NULL ? NULL : &conflict_entries[1]->id; |
1445
|
2
|
50
|
|
|
|
|
their_oid = conflict_entries[2] == NULL ? NULL : &conflict_entries[2]->id; |
1446
|
|
|
|
|
|
|
|
1447
|
2
|
50
|
|
|
|
|
if ((ret = git_index_reuc_add(index, path, ancestor_mode, ancestor_oid, |
1448
|
|
|
|
|
|
|
our_mode, our_oid, their_mode, their_oid)) >= 0) |
1449
|
2
|
|
|
|
|
|
ret = git_index_conflict_remove(index, path); |
1450
|
|
|
|
|
|
|
|
1451
|
63
|
|
|
|
|
|
return ret; |
1452
|
|
|
|
|
|
|
} |
1453
|
|
|
|
|
|
|
|
1454
|
78
|
|
|
|
|
|
GIT_INLINE(bool) is_file_or_link(const int filemode) |
1455
|
|
|
|
|
|
|
{ |
1456
|
82
|
100
|
|
|
|
|
return (filemode == GIT_FILEMODE_BLOB || |
1457
|
82
|
100
|
|
|
|
|
filemode == GIT_FILEMODE_BLOB_EXECUTABLE || |
|
|
100
|
|
|
|
|
|
1458
|
|
|
|
|
|
|
filemode == GIT_FILEMODE_LINK); |
1459
|
|
|
|
|
|
|
} |
1460
|
|
|
|
|
|
|
|
1461
|
69
|
|
|
|
|
|
GIT_INLINE(bool) valid_filemode(const int filemode) |
1462
|
|
|
|
|
|
|
{ |
1463
|
69
|
50
|
|
|
|
|
return (is_file_or_link(filemode) || filemode == GIT_FILEMODE_COMMIT); |
|
|
0
|
|
|
|
|
|
1464
|
|
|
|
|
|
|
} |
1465
|
|
|
|
|
|
|
|
1466
|
9
|
|
|
|
|
|
int git_index_add_from_buffer( |
1467
|
|
|
|
|
|
|
git_index *index, const git_index_entry *source_entry, |
1468
|
|
|
|
|
|
|
const void *buffer, size_t len) |
1469
|
|
|
|
|
|
|
{ |
1470
|
9
|
|
|
|
|
|
git_index_entry *entry = NULL; |
1471
|
9
|
|
|
|
|
|
int error = 0; |
1472
|
|
|
|
|
|
|
git_oid id; |
1473
|
|
|
|
|
|
|
|
1474
|
9
|
50
|
|
|
|
|
assert(index && source_entry->path); |
|
|
50
|
|
|
|
|
|
1475
|
|
|
|
|
|
|
|
1476
|
9
|
50
|
|
|
|
|
if (INDEX_OWNER(index) == NULL) |
1477
|
0
|
|
|
|
|
|
return create_index_error(-1, |
1478
|
|
|
|
|
|
|
"could not initialize index entry. " |
1479
|
|
|
|
|
|
|
"Index is not backed up by an existing repository."); |
1480
|
|
|
|
|
|
|
|
1481
|
9
|
100
|
|
|
|
|
if (!is_file_or_link(source_entry->mode)) { |
1482
|
2
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "invalid filemode"); |
1483
|
2
|
|
|
|
|
|
return -1; |
1484
|
|
|
|
|
|
|
} |
1485
|
|
|
|
|
|
|
|
1486
|
7
|
50
|
|
|
|
|
if (len > UINT32_MAX) { |
1487
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "buffer is too large"); |
1488
|
0
|
|
|
|
|
|
return -1; |
1489
|
|
|
|
|
|
|
} |
1490
|
|
|
|
|
|
|
|
1491
|
7
|
50
|
|
|
|
|
if (index_entry_dup(&entry, index, source_entry) < 0) |
1492
|
0
|
|
|
|
|
|
return -1; |
1493
|
|
|
|
|
|
|
|
1494
|
7
|
|
|
|
|
|
error = git_blob_create_from_buffer(&id, INDEX_OWNER(index), buffer, len); |
1495
|
7
|
50
|
|
|
|
|
if (error < 0) { |
1496
|
0
|
|
|
|
|
|
index_entry_free(entry); |
1497
|
0
|
|
|
|
|
|
return error; |
1498
|
|
|
|
|
|
|
} |
1499
|
|
|
|
|
|
|
|
1500
|
7
|
|
|
|
|
|
git_oid_cpy(&entry->id, &id); |
1501
|
7
|
|
|
|
|
|
entry->file_size = (uint32_t)len; |
1502
|
|
|
|
|
|
|
|
1503
|
7
|
50
|
|
|
|
|
if ((error = index_insert(index, &entry, 1, true, true, true)) < 0) |
1504
|
0
|
|
|
|
|
|
return error; |
1505
|
|
|
|
|
|
|
|
1506
|
|
|
|
|
|
|
/* Adding implies conflict was resolved, move conflict entries to REUC */ |
1507
|
7
|
50
|
|
|
|
|
if ((error = index_conflict_to_reuc(index, entry->path)) < 0 && error != GIT_ENOTFOUND) |
|
|
50
|
|
|
|
|
|
1508
|
0
|
|
|
|
|
|
return error; |
1509
|
|
|
|
|
|
|
|
1510
|
7
|
|
|
|
|
|
git_tree_cache_invalidate_path(index->tree, entry->path); |
1511
|
9
|
|
|
|
|
|
return 0; |
1512
|
|
|
|
|
|
|
} |
1513
|
|
|
|
|
|
|
|
1514
|
0
|
|
|
|
|
|
static int add_repo_as_submodule(git_index_entry **out, git_index *index, const char *path) |
1515
|
|
|
|
|
|
|
{ |
1516
|
|
|
|
|
|
|
git_repository *sub; |
1517
|
0
|
|
|
|
|
|
git_buf abspath = GIT_BUF_INIT; |
1518
|
0
|
|
|
|
|
|
git_repository *repo = INDEX_OWNER(index); |
1519
|
|
|
|
|
|
|
git_reference *head; |
1520
|
|
|
|
|
|
|
git_index_entry *entry; |
1521
|
|
|
|
|
|
|
struct stat st; |
1522
|
|
|
|
|
|
|
int error; |
1523
|
|
|
|
|
|
|
|
1524
|
0
|
0
|
|
|
|
|
if ((error = git_buf_joinpath(&abspath, git_repository_workdir(repo), path)) < 0) |
1525
|
0
|
|
|
|
|
|
return error; |
1526
|
|
|
|
|
|
|
|
1527
|
0
|
0
|
|
|
|
|
if ((error = p_stat(abspath.ptr, &st)) < 0) { |
1528
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to stat repository dir"); |
1529
|
0
|
|
|
|
|
|
return -1; |
1530
|
|
|
|
|
|
|
} |
1531
|
|
|
|
|
|
|
|
1532
|
0
|
0
|
|
|
|
|
if (index_entry_create(&entry, INDEX_OWNER(index), path, &st, true) < 0) |
1533
|
0
|
|
|
|
|
|
return -1; |
1534
|
|
|
|
|
|
|
|
1535
|
0
|
|
|
|
|
|
git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode); |
1536
|
|
|
|
|
|
|
|
1537
|
0
|
0
|
|
|
|
|
if ((error = git_repository_open(&sub, abspath.ptr)) < 0) |
1538
|
0
|
|
|
|
|
|
return error; |
1539
|
|
|
|
|
|
|
|
1540
|
0
|
0
|
|
|
|
|
if ((error = git_repository_head(&head, sub)) < 0) |
1541
|
0
|
|
|
|
|
|
return error; |
1542
|
|
|
|
|
|
|
|
1543
|
0
|
|
|
|
|
|
git_oid_cpy(&entry->id, git_reference_target(head)); |
1544
|
0
|
|
|
|
|
|
entry->mode = GIT_FILEMODE_COMMIT; |
1545
|
|
|
|
|
|
|
|
1546
|
0
|
|
|
|
|
|
git_reference_free(head); |
1547
|
0
|
|
|
|
|
|
git_repository_free(sub); |
1548
|
0
|
|
|
|
|
|
git_buf_dispose(&abspath); |
1549
|
|
|
|
|
|
|
|
1550
|
0
|
|
|
|
|
|
*out = entry; |
1551
|
0
|
|
|
|
|
|
return 0; |
1552
|
|
|
|
|
|
|
} |
1553
|
|
|
|
|
|
|
|
1554
|
52
|
|
|
|
|
|
int git_index_add_bypath(git_index *index, const char *path) |
1555
|
|
|
|
|
|
|
{ |
1556
|
52
|
|
|
|
|
|
git_index_entry *entry = NULL; |
1557
|
|
|
|
|
|
|
int ret; |
1558
|
|
|
|
|
|
|
|
1559
|
52
|
50
|
|
|
|
|
assert(index && path); |
|
|
50
|
|
|
|
|
|
1560
|
|
|
|
|
|
|
|
1561
|
52
|
100
|
|
|
|
|
if ((ret = index_entry_init(&entry, index, path)) == 0) |
1562
|
50
|
|
|
|
|
|
ret = index_insert(index, &entry, 1, false, false, true); |
1563
|
|
|
|
|
|
|
|
1564
|
|
|
|
|
|
|
/* If we were given a directory, let's see if it's a submodule */ |
1565
|
52
|
100
|
|
|
|
|
if (ret < 0 && ret != GIT_EDIRECTORY) |
|
|
50
|
|
|
|
|
|
1566
|
2
|
|
|
|
|
|
return ret; |
1567
|
|
|
|
|
|
|
|
1568
|
50
|
50
|
|
|
|
|
if (ret == GIT_EDIRECTORY) { |
1569
|
|
|
|
|
|
|
git_submodule *sm; |
1570
|
|
|
|
|
|
|
git_error_state err; |
1571
|
|
|
|
|
|
|
|
1572
|
0
|
|
|
|
|
|
git_error_state_capture(&err, ret); |
1573
|
|
|
|
|
|
|
|
1574
|
0
|
|
|
|
|
|
ret = git_submodule_lookup(&sm, INDEX_OWNER(index), path); |
1575
|
0
|
0
|
|
|
|
|
if (ret == GIT_ENOTFOUND) |
1576
|
0
|
|
|
|
|
|
return git_error_state_restore(&err); |
1577
|
|
|
|
|
|
|
|
1578
|
0
|
|
|
|
|
|
git_error_state_free(&err); |
1579
|
|
|
|
|
|
|
|
1580
|
|
|
|
|
|
|
/* |
1581
|
|
|
|
|
|
|
* EEXISTS means that there is a repository at that path, but it's not known |
1582
|
|
|
|
|
|
|
* as a submodule. We add its HEAD as an entry and don't register it. |
1583
|
|
|
|
|
|
|
*/ |
1584
|
0
|
0
|
|
|
|
|
if (ret == GIT_EEXISTS) { |
1585
|
0
|
0
|
|
|
|
|
if ((ret = add_repo_as_submodule(&entry, index, path)) < 0) |
1586
|
0
|
|
|
|
|
|
return ret; |
1587
|
|
|
|
|
|
|
|
1588
|
0
|
0
|
|
|
|
|
if ((ret = index_insert(index, &entry, 1, false, false, true)) < 0) |
1589
|
0
|
|
|
|
|
|
return ret; |
1590
|
0
|
0
|
|
|
|
|
} else if (ret < 0) { |
1591
|
0
|
|
|
|
|
|
return ret; |
1592
|
|
|
|
|
|
|
} else { |
1593
|
0
|
|
|
|
|
|
ret = git_submodule_add_to_index(sm, false); |
1594
|
0
|
|
|
|
|
|
git_submodule_free(sm); |
1595
|
0
|
|
|
|
|
|
return ret; |
1596
|
|
|
|
|
|
|
} |
1597
|
|
|
|
|
|
|
} |
1598
|
|
|
|
|
|
|
|
1599
|
|
|
|
|
|
|
/* Adding implies conflict was resolved, move conflict entries to REUC */ |
1600
|
50
|
100
|
|
|
|
|
if ((ret = index_conflict_to_reuc(index, path)) < 0 && ret != GIT_ENOTFOUND) |
|
|
50
|
|
|
|
|
|
1601
|
0
|
|
|
|
|
|
return ret; |
1602
|
|
|
|
|
|
|
|
1603
|
50
|
|
|
|
|
|
git_tree_cache_invalidate_path(index->tree, entry->path); |
1604
|
52
|
|
|
|
|
|
return 0; |
1605
|
|
|
|
|
|
|
} |
1606
|
|
|
|
|
|
|
|
1607
|
6
|
|
|
|
|
|
int git_index_remove_bypath(git_index *index, const char *path) |
1608
|
|
|
|
|
|
|
{ |
1609
|
|
|
|
|
|
|
int ret; |
1610
|
|
|
|
|
|
|
|
1611
|
6
|
50
|
|
|
|
|
assert(index && path); |
|
|
50
|
|
|
|
|
|
1612
|
|
|
|
|
|
|
|
1613
|
6
|
50
|
|
|
|
|
if (((ret = git_index_remove(index, path, 0)) < 0 && |
|
|
0
|
|
|
|
|
|
1614
|
6
|
50
|
|
|
|
|
ret != GIT_ENOTFOUND) || |
1615
|
6
|
50
|
|
|
|
|
((ret = index_conflict_to_reuc(index, path)) < 0 && |
1616
|
|
|
|
|
|
|
ret != GIT_ENOTFOUND)) |
1617
|
0
|
|
|
|
|
|
return ret; |
1618
|
|
|
|
|
|
|
|
1619
|
6
|
50
|
|
|
|
|
if (ret == GIT_ENOTFOUND) |
1620
|
6
|
|
|
|
|
|
git_error_clear(); |
1621
|
|
|
|
|
|
|
|
1622
|
6
|
|
|
|
|
|
return 0; |
1623
|
|
|
|
|
|
|
} |
1624
|
|
|
|
|
|
|
|
1625
|
28
|
|
|
|
|
|
int git_index__fill(git_index *index, const git_vector *source_entries) |
1626
|
|
|
|
|
|
|
{ |
1627
|
28
|
|
|
|
|
|
const git_index_entry *source_entry = NULL; |
1628
|
28
|
|
|
|
|
|
int error = 0; |
1629
|
|
|
|
|
|
|
size_t i; |
1630
|
|
|
|
|
|
|
|
1631
|
28
|
50
|
|
|
|
|
assert(index); |
1632
|
|
|
|
|
|
|
|
1633
|
28
|
100
|
|
|
|
|
if (!source_entries->length) |
1634
|
3
|
|
|
|
|
|
return 0; |
1635
|
|
|
|
|
|
|
|
1636
|
50
|
|
|
|
|
|
if (git_vector_size_hint(&index->entries, source_entries->length) < 0 || |
1637
|
25
|
|
|
|
|
|
index_map_resize(index->entries_map, (size_t)(source_entries->length * 1.3), |
1638
|
25
|
|
|
|
|
|
index->ignore_case) < 0) |
1639
|
0
|
|
|
|
|
|
return -1; |
1640
|
|
|
|
|
|
|
|
1641
|
88
|
100
|
|
|
|
|
git_vector_foreach(source_entries, i, source_entry) { |
1642
|
63
|
|
|
|
|
|
git_index_entry *entry = NULL; |
1643
|
|
|
|
|
|
|
|
1644
|
63
|
50
|
|
|
|
|
if ((error = index_entry_dup(&entry, index, source_entry)) < 0) |
1645
|
0
|
|
|
|
|
|
break; |
1646
|
|
|
|
|
|
|
|
1647
|
63
|
|
|
|
|
|
index_entry_adjust_namemask(entry, ((struct entry_internal *)entry)->pathlen); |
1648
|
63
|
|
|
|
|
|
entry->flags_extended |= GIT_INDEX_ENTRY_UPTODATE; |
1649
|
63
|
|
|
|
|
|
entry->mode = git_index__create_mode(entry->mode); |
1650
|
|
|
|
|
|
|
|
1651
|
63
|
50
|
|
|
|
|
if ((error = git_vector_insert(&index->entries, entry)) < 0) |
1652
|
0
|
|
|
|
|
|
break; |
1653
|
|
|
|
|
|
|
|
1654
|
63
|
50
|
|
|
|
|
if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0) |
1655
|
0
|
|
|
|
|
|
break; |
1656
|
|
|
|
|
|
|
|
1657
|
63
|
|
|
|
|
|
index->dirty = 1; |
1658
|
|
|
|
|
|
|
} |
1659
|
|
|
|
|
|
|
|
1660
|
25
|
50
|
|
|
|
|
if (!error) |
1661
|
25
|
|
|
|
|
|
git_vector_sort(&index->entries); |
1662
|
|
|
|
|
|
|
|
1663
|
25
|
|
|
|
|
|
return error; |
1664
|
|
|
|
|
|
|
} |
1665
|
|
|
|
|
|
|
|
1666
|
|
|
|
|
|
|
|
1667
|
52
|
|
|
|
|
|
int git_index_add(git_index *index, const git_index_entry *source_entry) |
1668
|
|
|
|
|
|
|
{ |
1669
|
52
|
|
|
|
|
|
git_index_entry *entry = NULL; |
1670
|
|
|
|
|
|
|
int ret; |
1671
|
|
|
|
|
|
|
|
1672
|
52
|
50
|
|
|
|
|
assert(index && source_entry && source_entry->path); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
1673
|
|
|
|
|
|
|
|
1674
|
52
|
50
|
|
|
|
|
if (!valid_filemode(source_entry->mode)) { |
1675
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "invalid entry mode"); |
1676
|
0
|
|
|
|
|
|
return -1; |
1677
|
|
|
|
|
|
|
} |
1678
|
|
|
|
|
|
|
|
1679
|
52
|
50
|
|
|
|
|
if ((ret = index_entry_dup(&entry, index, source_entry)) < 0 || |
|
|
50
|
|
|
|
|
|
1680
|
|
|
|
|
|
|
(ret = index_insert(index, &entry, 1, true, true, false)) < 0) |
1681
|
0
|
|
|
|
|
|
return ret; |
1682
|
|
|
|
|
|
|
|
1683
|
52
|
|
|
|
|
|
git_tree_cache_invalidate_path(index->tree, entry->path); |
1684
|
52
|
|
|
|
|
|
return 0; |
1685
|
|
|
|
|
|
|
} |
1686
|
|
|
|
|
|
|
|
1687
|
50
|
|
|
|
|
|
int git_index_remove(git_index *index, const char *path, int stage) |
1688
|
|
|
|
|
|
|
{ |
1689
|
|
|
|
|
|
|
int error; |
1690
|
|
|
|
|
|
|
size_t position; |
1691
|
50
|
|
|
|
|
|
git_index_entry remove_key = {{ 0 }}; |
1692
|
|
|
|
|
|
|
|
1693
|
50
|
|
|
|
|
|
remove_key.path = path; |
1694
|
50
|
|
|
|
|
|
GIT_INDEX_ENTRY_STAGE_SET(&remove_key, stage); |
1695
|
|
|
|
|
|
|
|
1696
|
50
|
|
|
|
|
|
index_map_delete(index->entries_map, &remove_key, index->ignore_case); |
1697
|
|
|
|
|
|
|
|
1698
|
50
|
100
|
|
|
|
|
if (index_find(&position, index, path, 0, stage) < 0) { |
1699
|
35
|
|
|
|
|
|
git_error_set( |
1700
|
|
|
|
|
|
|
GIT_ERROR_INDEX, "index does not contain %s at stage %d", path, stage); |
1701
|
35
|
|
|
|
|
|
error = GIT_ENOTFOUND; |
1702
|
|
|
|
|
|
|
} else { |
1703
|
15
|
|
|
|
|
|
error = index_remove_entry(index, position); |
1704
|
|
|
|
|
|
|
} |
1705
|
|
|
|
|
|
|
|
1706
|
50
|
|
|
|
|
|
return error; |
1707
|
|
|
|
|
|
|
} |
1708
|
|
|
|
|
|
|
|
1709
|
1
|
|
|
|
|
|
int git_index_remove_directory(git_index *index, const char *dir, int stage) |
1710
|
|
|
|
|
|
|
{ |
1711
|
1
|
|
|
|
|
|
git_buf pfx = GIT_BUF_INIT; |
1712
|
1
|
|
|
|
|
|
int error = 0; |
1713
|
|
|
|
|
|
|
size_t pos; |
1714
|
|
|
|
|
|
|
git_index_entry *entry; |
1715
|
|
|
|
|
|
|
|
1716
|
1
|
50
|
|
|
|
|
if (!(error = git_buf_sets(&pfx, dir)) && |
|
|
50
|
|
|
|
|
|
1717
|
|
|
|
|
|
|
!(error = git_path_to_dir(&pfx))) |
1718
|
1
|
|
|
|
|
|
index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY); |
1719
|
|
|
|
|
|
|
|
1720
|
1
|
50
|
|
|
|
|
while (!error) { |
1721
|
1
|
|
|
|
|
|
entry = git_vector_get(&index->entries, pos); |
1722
|
1
|
50
|
|
|
|
|
if (!entry || git__prefixcmp(entry->path, pfx.ptr) != 0) |
|
|
50
|
|
|
|
|
|
1723
|
|
|
|
|
|
|
break; |
1724
|
|
|
|
|
|
|
|
1725
|
0
|
0
|
|
|
|
|
if (GIT_INDEX_ENTRY_STAGE(entry) != stage) { |
1726
|
0
|
|
|
|
|
|
++pos; |
1727
|
0
|
|
|
|
|
|
continue; |
1728
|
|
|
|
|
|
|
} |
1729
|
|
|
|
|
|
|
|
1730
|
0
|
|
|
|
|
|
error = index_remove_entry(index, pos); |
1731
|
|
|
|
|
|
|
|
1732
|
|
|
|
|
|
|
/* removed entry at 'pos' so we don't need to increment */ |
1733
|
|
|
|
|
|
|
} |
1734
|
|
|
|
|
|
|
|
1735
|
1
|
|
|
|
|
|
git_buf_dispose(&pfx); |
1736
|
|
|
|
|
|
|
|
1737
|
1
|
|
|
|
|
|
return error; |
1738
|
|
|
|
|
|
|
} |
1739
|
|
|
|
|
|
|
|
1740
|
0
|
|
|
|
|
|
int git_index_find_prefix(size_t *at_pos, git_index *index, const char *prefix) |
1741
|
|
|
|
|
|
|
{ |
1742
|
0
|
|
|
|
|
|
int error = 0; |
1743
|
|
|
|
|
|
|
size_t pos; |
1744
|
|
|
|
|
|
|
const git_index_entry *entry; |
1745
|
|
|
|
|
|
|
|
1746
|
0
|
|
|
|
|
|
index_find(&pos, index, prefix, strlen(prefix), GIT_INDEX_STAGE_ANY); |
1747
|
0
|
|
|
|
|
|
entry = git_vector_get(&index->entries, pos); |
1748
|
0
|
0
|
|
|
|
|
if (!entry || git__prefixcmp(entry->path, prefix) != 0) |
|
|
0
|
|
|
|
|
|
1749
|
0
|
|
|
|
|
|
error = GIT_ENOTFOUND; |
1750
|
|
|
|
|
|
|
|
1751
|
0
|
0
|
|
|
|
|
if (!error && at_pos) |
|
|
0
|
|
|
|
|
|
1752
|
0
|
|
|
|
|
|
*at_pos = pos; |
1753
|
|
|
|
|
|
|
|
1754
|
0
|
|
|
|
|
|
return error; |
1755
|
|
|
|
|
|
|
} |
1756
|
|
|
|
|
|
|
|
1757
|
1192
|
|
|
|
|
|
int git_index__find_pos( |
1758
|
|
|
|
|
|
|
size_t *out, git_index *index, const char *path, size_t path_len, int stage) |
1759
|
|
|
|
|
|
|
{ |
1760
|
1192
|
50
|
|
|
|
|
assert(index && path); |
|
|
50
|
|
|
|
|
|
1761
|
1192
|
|
|
|
|
|
return index_find(out, index, path, path_len, stage); |
1762
|
|
|
|
|
|
|
} |
1763
|
|
|
|
|
|
|
|
1764
|
71
|
|
|
|
|
|
int git_index_find(size_t *at_pos, git_index *index, const char *path) |
1765
|
|
|
|
|
|
|
{ |
1766
|
|
|
|
|
|
|
size_t pos; |
1767
|
|
|
|
|
|
|
|
1768
|
71
|
50
|
|
|
|
|
assert(index && path); |
|
|
50
|
|
|
|
|
|
1769
|
|
|
|
|
|
|
|
1770
|
71
|
100
|
|
|
|
|
if (git_vector_bsearch2( |
1771
|
|
|
|
|
|
|
&pos, &index->entries, index->entries_search_path, path) < 0) { |
1772
|
7
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "index does not contain %s", path); |
1773
|
7
|
|
|
|
|
|
return GIT_ENOTFOUND; |
1774
|
|
|
|
|
|
|
} |
1775
|
|
|
|
|
|
|
|
1776
|
|
|
|
|
|
|
/* Since our binary search only looked at path, we may be in the |
1777
|
|
|
|
|
|
|
* middle of a list of stages. |
1778
|
|
|
|
|
|
|
*/ |
1779
|
77
|
100
|
|
|
|
|
for (; pos > 0; --pos) { |
1780
|
28
|
|
|
|
|
|
const git_index_entry *prev = git_vector_get(&index->entries, pos - 1); |
1781
|
|
|
|
|
|
|
|
1782
|
28
|
100
|
|
|
|
|
if (index->entries_cmp_path(prev->path, path) != 0) |
1783
|
15
|
|
|
|
|
|
break; |
1784
|
|
|
|
|
|
|
} |
1785
|
|
|
|
|
|
|
|
1786
|
64
|
50
|
|
|
|
|
if (at_pos) |
1787
|
64
|
|
|
|
|
|
*at_pos = pos; |
1788
|
|
|
|
|
|
|
|
1789
|
71
|
|
|
|
|
|
return 0; |
1790
|
|
|
|
|
|
|
} |
1791
|
|
|
|
|
|
|
|
1792
|
6
|
|
|
|
|
|
int git_index_conflict_add(git_index *index, |
1793
|
|
|
|
|
|
|
const git_index_entry *ancestor_entry, |
1794
|
|
|
|
|
|
|
const git_index_entry *our_entry, |
1795
|
|
|
|
|
|
|
const git_index_entry *their_entry) |
1796
|
|
|
|
|
|
|
{ |
1797
|
6
|
|
|
|
|
|
git_index_entry *entries[3] = { 0 }; |
1798
|
|
|
|
|
|
|
unsigned short i; |
1799
|
6
|
|
|
|
|
|
int ret = 0; |
1800
|
|
|
|
|
|
|
|
1801
|
6
|
50
|
|
|
|
|
assert (index); |
1802
|
|
|
|
|
|
|
|
1803
|
6
|
50
|
|
|
|
|
if ((ancestor_entry && |
|
|
50
|
|
|
|
|
|
1804
|
6
|
100
|
|
|
|
|
(ret = index_entry_dup(&entries[0], index, ancestor_entry)) < 0) || |
1805
|
5
|
50
|
|
|
|
|
(our_entry && |
1806
|
6
|
50
|
|
|
|
|
(ret = index_entry_dup(&entries[1], index, our_entry)) < 0) || |
1807
|
6
|
50
|
|
|
|
|
(their_entry && |
1808
|
|
|
|
|
|
|
(ret = index_entry_dup(&entries[2], index, their_entry)) < 0)) |
1809
|
|
|
|
|
|
|
goto on_error; |
1810
|
|
|
|
|
|
|
|
1811
|
|
|
|
|
|
|
/* Validate entries */ |
1812
|
24
|
100
|
|
|
|
|
for (i = 0; i < 3; i++) { |
1813
|
18
|
100
|
|
|
|
|
if (entries[i] && !valid_filemode(entries[i]->mode)) { |
|
|
50
|
|
|
|
|
|
1814
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "invalid filemode for stage %d entry", |
1815
|
|
|
|
|
|
|
i + 1); |
1816
|
0
|
|
|
|
|
|
ret = -1; |
1817
|
0
|
|
|
|
|
|
goto on_error; |
1818
|
|
|
|
|
|
|
} |
1819
|
|
|
|
|
|
|
} |
1820
|
|
|
|
|
|
|
|
1821
|
|
|
|
|
|
|
/* Remove existing index entries for each path */ |
1822
|
24
|
100
|
|
|
|
|
for (i = 0; i < 3; i++) { |
1823
|
18
|
100
|
|
|
|
|
if (entries[i] == NULL) |
1824
|
1
|
|
|
|
|
|
continue; |
1825
|
|
|
|
|
|
|
|
1826
|
17
|
50
|
|
|
|
|
if ((ret = git_index_remove(index, entries[i]->path, 0)) != 0) { |
1827
|
17
|
50
|
|
|
|
|
if (ret != GIT_ENOTFOUND) |
1828
|
0
|
|
|
|
|
|
goto on_error; |
1829
|
|
|
|
|
|
|
|
1830
|
17
|
|
|
|
|
|
git_error_clear(); |
1831
|
17
|
|
|
|
|
|
ret = 0; |
1832
|
|
|
|
|
|
|
} |
1833
|
|
|
|
|
|
|
} |
1834
|
|
|
|
|
|
|
|
1835
|
|
|
|
|
|
|
/* Add the conflict entries */ |
1836
|
24
|
100
|
|
|
|
|
for (i = 0; i < 3; i++) { |
1837
|
18
|
100
|
|
|
|
|
if (entries[i] == NULL) |
1838
|
1
|
|
|
|
|
|
continue; |
1839
|
|
|
|
|
|
|
|
1840
|
|
|
|
|
|
|
/* Make sure stage is correct */ |
1841
|
17
|
|
|
|
|
|
GIT_INDEX_ENTRY_STAGE_SET(entries[i], i + 1); |
1842
|
|
|
|
|
|
|
|
1843
|
17
|
50
|
|
|
|
|
if ((ret = index_insert(index, &entries[i], 1, true, true, false)) < 0) |
1844
|
0
|
|
|
|
|
|
goto on_error; |
1845
|
|
|
|
|
|
|
|
1846
|
17
|
|
|
|
|
|
entries[i] = NULL; /* don't free if later entry fails */ |
1847
|
|
|
|
|
|
|
} |
1848
|
|
|
|
|
|
|
|
1849
|
6
|
|
|
|
|
|
return 0; |
1850
|
|
|
|
|
|
|
|
1851
|
|
|
|
|
|
|
on_error: |
1852
|
0
|
0
|
|
|
|
|
for (i = 0; i < 3; i++) { |
1853
|
0
|
0
|
|
|
|
|
if (entries[i] != NULL) |
1854
|
0
|
|
|
|
|
|
index_entry_free(entries[i]); |
1855
|
|
|
|
|
|
|
} |
1856
|
|
|
|
|
|
|
|
1857
|
6
|
|
|
|
|
|
return ret; |
1858
|
|
|
|
|
|
|
} |
1859
|
|
|
|
|
|
|
|
1860
|
62
|
|
|
|
|
|
static int index_conflict__get_byindex( |
1861
|
|
|
|
|
|
|
const git_index_entry **ancestor_out, |
1862
|
|
|
|
|
|
|
const git_index_entry **our_out, |
1863
|
|
|
|
|
|
|
const git_index_entry **their_out, |
1864
|
|
|
|
|
|
|
git_index *index, |
1865
|
|
|
|
|
|
|
size_t n) |
1866
|
|
|
|
|
|
|
{ |
1867
|
|
|
|
|
|
|
const git_index_entry *conflict_entry; |
1868
|
62
|
|
|
|
|
|
const char *path = NULL; |
1869
|
|
|
|
|
|
|
size_t count; |
1870
|
62
|
|
|
|
|
|
int stage, len = 0; |
1871
|
|
|
|
|
|
|
|
1872
|
62
|
50
|
|
|
|
|
assert(ancestor_out && our_out && their_out && index); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
1873
|
|
|
|
|
|
|
|
1874
|
62
|
|
|
|
|
|
*ancestor_out = NULL; |
1875
|
62
|
|
|
|
|
|
*our_out = NULL; |
1876
|
62
|
|
|
|
|
|
*their_out = NULL; |
1877
|
|
|
|
|
|
|
|
1878
|
140
|
100
|
|
|
|
|
for (count = git_index_entrycount(index); n < count; ++n) { |
1879
|
108
|
|
|
|
|
|
conflict_entry = git_vector_get(&index->entries, n); |
1880
|
|
|
|
|
|
|
|
1881
|
108
|
100
|
|
|
|
|
if (path && index->entries_cmp_path(conflict_entry->path, path) != 0) |
|
|
100
|
|
|
|
|
|
1882
|
30
|
|
|
|
|
|
break; |
1883
|
|
|
|
|
|
|
|
1884
|
78
|
|
|
|
|
|
stage = GIT_INDEX_ENTRY_STAGE(conflict_entry); |
1885
|
78
|
|
|
|
|
|
path = conflict_entry->path; |
1886
|
|
|
|
|
|
|
|
1887
|
78
|
|
|
|
|
|
switch (stage) { |
1888
|
|
|
|
|
|
|
case 3: |
1889
|
7
|
|
|
|
|
|
*their_out = conflict_entry; |
1890
|
7
|
|
|
|
|
|
len++; |
1891
|
7
|
|
|
|
|
|
break; |
1892
|
|
|
|
|
|
|
case 2: |
1893
|
7
|
|
|
|
|
|
*our_out = conflict_entry; |
1894
|
7
|
|
|
|
|
|
len++; |
1895
|
7
|
|
|
|
|
|
break; |
1896
|
|
|
|
|
|
|
case 1: |
1897
|
7
|
|
|
|
|
|
*ancestor_out = conflict_entry; |
1898
|
7
|
|
|
|
|
|
len++; |
1899
|
7
|
|
|
|
|
|
break; |
1900
|
|
|
|
|
|
|
default: |
1901
|
57
|
|
|
|
|
|
break; |
1902
|
|
|
|
|
|
|
}; |
1903
|
|
|
|
|
|
|
} |
1904
|
|
|
|
|
|
|
|
1905
|
62
|
|
|
|
|
|
return len; |
1906
|
|
|
|
|
|
|
} |
1907
|
|
|
|
|
|
|
|
1908
|
64
|
|
|
|
|
|
int git_index_conflict_get( |
1909
|
|
|
|
|
|
|
const git_index_entry **ancestor_out, |
1910
|
|
|
|
|
|
|
const git_index_entry **our_out, |
1911
|
|
|
|
|
|
|
const git_index_entry **their_out, |
1912
|
|
|
|
|
|
|
git_index *index, |
1913
|
|
|
|
|
|
|
const char *path) |
1914
|
|
|
|
|
|
|
{ |
1915
|
|
|
|
|
|
|
size_t pos; |
1916
|
64
|
|
|
|
|
|
int len = 0; |
1917
|
|
|
|
|
|
|
|
1918
|
64
|
50
|
|
|
|
|
assert(ancestor_out && our_out && their_out && index && path); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
1919
|
|
|
|
|
|
|
|
1920
|
64
|
|
|
|
|
|
*ancestor_out = NULL; |
1921
|
64
|
|
|
|
|
|
*our_out = NULL; |
1922
|
64
|
|
|
|
|
|
*their_out = NULL; |
1923
|
|
|
|
|
|
|
|
1924
|
64
|
100
|
|
|
|
|
if (git_index_find(&pos, index, path) < 0) |
1925
|
6
|
|
|
|
|
|
return GIT_ENOTFOUND; |
1926
|
|
|
|
|
|
|
|
1927
|
58
|
50
|
|
|
|
|
if ((len = index_conflict__get_byindex( |
1928
|
|
|
|
|
|
|
ancestor_out, our_out, their_out, index, pos)) < 0) |
1929
|
0
|
|
|
|
|
|
return len; |
1930
|
58
|
100
|
|
|
|
|
else if (len == 0) |
1931
|
55
|
|
|
|
|
|
return GIT_ENOTFOUND; |
1932
|
|
|
|
|
|
|
|
1933
|
64
|
|
|
|
|
|
return 0; |
1934
|
|
|
|
|
|
|
} |
1935
|
|
|
|
|
|
|
|
1936
|
6
|
|
|
|
|
|
static int index_conflict_remove(git_index *index, const char *path) |
1937
|
|
|
|
|
|
|
{ |
1938
|
6
|
|
|
|
|
|
size_t pos = 0; |
1939
|
|
|
|
|
|
|
git_index_entry *conflict_entry; |
1940
|
6
|
|
|
|
|
|
int error = 0; |
1941
|
|
|
|
|
|
|
|
1942
|
6
|
100
|
|
|
|
|
if (path != NULL && git_index_find(&pos, index, path) < 0) |
|
|
50
|
|
|
|
|
|
1943
|
0
|
|
|
|
|
|
return GIT_ENOTFOUND; |
1944
|
|
|
|
|
|
|
|
1945
|
21
|
100
|
|
|
|
|
while ((conflict_entry = git_vector_get(&index->entries, pos)) != NULL) { |
1946
|
|
|
|
|
|
|
|
1947
|
32
|
|
|
|
|
|
if (path != NULL && |
1948
|
16
|
|
|
|
|
|
index->entries_cmp_path(conflict_entry->path, path) != 0) |
1949
|
1
|
|
|
|
|
|
break; |
1950
|
|
|
|
|
|
|
|
1951
|
15
|
100
|
|
|
|
|
if (GIT_INDEX_ENTRY_STAGE(conflict_entry) == 0) { |
1952
|
3
|
|
|
|
|
|
pos++; |
1953
|
3
|
|
|
|
|
|
continue; |
1954
|
|
|
|
|
|
|
} |
1955
|
|
|
|
|
|
|
|
1956
|
12
|
50
|
|
|
|
|
if ((error = index_remove_entry(index, pos)) < 0) |
1957
|
0
|
|
|
|
|
|
break; |
1958
|
|
|
|
|
|
|
} |
1959
|
|
|
|
|
|
|
|
1960
|
6
|
|
|
|
|
|
return error; |
1961
|
|
|
|
|
|
|
} |
1962
|
|
|
|
|
|
|
|
1963
|
5
|
|
|
|
|
|
int git_index_conflict_remove(git_index *index, const char *path) |
1964
|
|
|
|
|
|
|
{ |
1965
|
5
|
50
|
|
|
|
|
assert(index && path); |
|
|
50
|
|
|
|
|
|
1966
|
5
|
|
|
|
|
|
return index_conflict_remove(index, path); |
1967
|
|
|
|
|
|
|
} |
1968
|
|
|
|
|
|
|
|
1969
|
1
|
|
|
|
|
|
int git_index_conflict_cleanup(git_index *index) |
1970
|
|
|
|
|
|
|
{ |
1971
|
1
|
50
|
|
|
|
|
assert(index); |
1972
|
1
|
|
|
|
|
|
return index_conflict_remove(index, NULL); |
1973
|
|
|
|
|
|
|
} |
1974
|
|
|
|
|
|
|
|
1975
|
121
|
|
|
|
|
|
int git_index_has_conflicts(const git_index *index) |
1976
|
|
|
|
|
|
|
{ |
1977
|
|
|
|
|
|
|
size_t i; |
1978
|
|
|
|
|
|
|
git_index_entry *entry; |
1979
|
|
|
|
|
|
|
|
1980
|
121
|
50
|
|
|
|
|
assert(index); |
1981
|
|
|
|
|
|
|
|
1982
|
404
|
100
|
|
|
|
|
git_vector_foreach(&index->entries, i, entry) { |
1983
|
291
|
100
|
|
|
|
|
if (GIT_INDEX_ENTRY_STAGE(entry) > 0) |
1984
|
8
|
|
|
|
|
|
return 1; |
1985
|
|
|
|
|
|
|
} |
1986
|
|
|
|
|
|
|
|
1987
|
113
|
|
|
|
|
|
return 0; |
1988
|
|
|
|
|
|
|
} |
1989
|
|
|
|
|
|
|
|
1990
|
0
|
|
|
|
|
|
int git_index_iterator_new( |
1991
|
|
|
|
|
|
|
git_index_iterator **iterator_out, |
1992
|
|
|
|
|
|
|
git_index *index) |
1993
|
|
|
|
|
|
|
{ |
1994
|
|
|
|
|
|
|
git_index_iterator *it; |
1995
|
|
|
|
|
|
|
int error; |
1996
|
|
|
|
|
|
|
|
1997
|
0
|
0
|
|
|
|
|
assert(iterator_out && index); |
|
|
0
|
|
|
|
|
|
1998
|
|
|
|
|
|
|
|
1999
|
0
|
|
|
|
|
|
it = git__calloc(1, sizeof(git_index_iterator)); |
2000
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(it); |
2001
|
|
|
|
|
|
|
|
2002
|
0
|
0
|
|
|
|
|
if ((error = git_index_snapshot_new(&it->snap, index)) < 0) { |
2003
|
0
|
|
|
|
|
|
git__free(it); |
2004
|
0
|
|
|
|
|
|
return error; |
2005
|
|
|
|
|
|
|
} |
2006
|
|
|
|
|
|
|
|
2007
|
0
|
|
|
|
|
|
it->index = index; |
2008
|
|
|
|
|
|
|
|
2009
|
0
|
|
|
|
|
|
*iterator_out = it; |
2010
|
0
|
|
|
|
|
|
return 0; |
2011
|
|
|
|
|
|
|
} |
2012
|
|
|
|
|
|
|
|
2013
|
0
|
|
|
|
|
|
int git_index_iterator_next( |
2014
|
|
|
|
|
|
|
const git_index_entry **out, |
2015
|
|
|
|
|
|
|
git_index_iterator *it) |
2016
|
|
|
|
|
|
|
{ |
2017
|
0
|
0
|
|
|
|
|
assert(out && it); |
|
|
0
|
|
|
|
|
|
2018
|
|
|
|
|
|
|
|
2019
|
0
|
0
|
|
|
|
|
if (it->cur >= git_vector_length(&it->snap)) |
2020
|
0
|
|
|
|
|
|
return GIT_ITEROVER; |
2021
|
|
|
|
|
|
|
|
2022
|
0
|
|
|
|
|
|
*out = (git_index_entry *)git_vector_get(&it->snap, it->cur++); |
2023
|
0
|
|
|
|
|
|
return 0; |
2024
|
|
|
|
|
|
|
} |
2025
|
|
|
|
|
|
|
|
2026
|
0
|
|
|
|
|
|
void git_index_iterator_free(git_index_iterator *it) |
2027
|
|
|
|
|
|
|
{ |
2028
|
0
|
0
|
|
|
|
|
if (it == NULL) |
2029
|
0
|
|
|
|
|
|
return; |
2030
|
|
|
|
|
|
|
|
2031
|
0
|
|
|
|
|
|
git_index_snapshot_release(&it->snap, it->index); |
2032
|
0
|
|
|
|
|
|
git__free(it); |
2033
|
|
|
|
|
|
|
} |
2034
|
|
|
|
|
|
|
|
2035
|
69
|
|
|
|
|
|
int git_index_conflict_iterator_new( |
2036
|
|
|
|
|
|
|
git_index_conflict_iterator **iterator_out, |
2037
|
|
|
|
|
|
|
git_index *index) |
2038
|
|
|
|
|
|
|
{ |
2039
|
69
|
|
|
|
|
|
git_index_conflict_iterator *it = NULL; |
2040
|
|
|
|
|
|
|
|
2041
|
69
|
50
|
|
|
|
|
assert(iterator_out && index); |
|
|
50
|
|
|
|
|
|
2042
|
|
|
|
|
|
|
|
2043
|
69
|
|
|
|
|
|
it = git__calloc(1, sizeof(git_index_conflict_iterator)); |
2044
|
69
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(it); |
2045
|
|
|
|
|
|
|
|
2046
|
69
|
|
|
|
|
|
it->index = index; |
2047
|
|
|
|
|
|
|
|
2048
|
69
|
|
|
|
|
|
*iterator_out = it; |
2049
|
69
|
|
|
|
|
|
return 0; |
2050
|
|
|
|
|
|
|
} |
2051
|
|
|
|
|
|
|
|
2052
|
73
|
|
|
|
|
|
int git_index_conflict_next( |
2053
|
|
|
|
|
|
|
const git_index_entry **ancestor_out, |
2054
|
|
|
|
|
|
|
const git_index_entry **our_out, |
2055
|
|
|
|
|
|
|
const git_index_entry **their_out, |
2056
|
|
|
|
|
|
|
git_index_conflict_iterator *iterator) |
2057
|
|
|
|
|
|
|
{ |
2058
|
|
|
|
|
|
|
const git_index_entry *entry; |
2059
|
|
|
|
|
|
|
int len; |
2060
|
|
|
|
|
|
|
|
2061
|
73
|
50
|
|
|
|
|
assert(ancestor_out && our_out && their_out && iterator); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
2062
|
|
|
|
|
|
|
|
2063
|
73
|
|
|
|
|
|
*ancestor_out = NULL; |
2064
|
73
|
|
|
|
|
|
*our_out = NULL; |
2065
|
73
|
|
|
|
|
|
*their_out = NULL; |
2066
|
|
|
|
|
|
|
|
2067
|
213
|
100
|
|
|
|
|
while (iterator->cur < iterator->index->entries.length) { |
2068
|
144
|
|
|
|
|
|
entry = git_index_get_byindex(iterator->index, iterator->cur); |
2069
|
|
|
|
|
|
|
|
2070
|
144
|
100
|
|
|
|
|
if (git_index_entry_is_conflict(entry)) { |
2071
|
4
|
50
|
|
|
|
|
if ((len = index_conflict__get_byindex( |
2072
|
|
|
|
|
|
|
ancestor_out, |
2073
|
|
|
|
|
|
|
our_out, |
2074
|
|
|
|
|
|
|
their_out, |
2075
|
|
|
|
|
|
|
iterator->index, |
2076
|
|
|
|
|
|
|
iterator->cur)) < 0) |
2077
|
0
|
|
|
|
|
|
return len; |
2078
|
|
|
|
|
|
|
|
2079
|
4
|
|
|
|
|
|
iterator->cur += len; |
2080
|
4
|
|
|
|
|
|
return 0; |
2081
|
|
|
|
|
|
|
} |
2082
|
|
|
|
|
|
|
|
2083
|
140
|
|
|
|
|
|
iterator->cur++; |
2084
|
|
|
|
|
|
|
} |
2085
|
|
|
|
|
|
|
|
2086
|
69
|
|
|
|
|
|
return GIT_ITEROVER; |
2087
|
|
|
|
|
|
|
} |
2088
|
|
|
|
|
|
|
|
2089
|
69
|
|
|
|
|
|
void git_index_conflict_iterator_free(git_index_conflict_iterator *iterator) |
2090
|
|
|
|
|
|
|
{ |
2091
|
69
|
50
|
|
|
|
|
if (iterator == NULL) |
2092
|
0
|
|
|
|
|
|
return; |
2093
|
|
|
|
|
|
|
|
2094
|
69
|
|
|
|
|
|
git__free(iterator); |
2095
|
|
|
|
|
|
|
} |
2096
|
|
|
|
|
|
|
|
2097
|
17
|
|
|
|
|
|
size_t git_index_name_entrycount(git_index *index) |
2098
|
|
|
|
|
|
|
{ |
2099
|
17
|
50
|
|
|
|
|
assert(index); |
2100
|
17
|
|
|
|
|
|
return index->names.length; |
2101
|
|
|
|
|
|
|
} |
2102
|
|
|
|
|
|
|
|
2103
|
0
|
|
|
|
|
|
const git_index_name_entry *git_index_name_get_byindex( |
2104
|
|
|
|
|
|
|
git_index *index, size_t n) |
2105
|
|
|
|
|
|
|
{ |
2106
|
0
|
0
|
|
|
|
|
assert(index); |
2107
|
|
|
|
|
|
|
|
2108
|
0
|
|
|
|
|
|
git_vector_sort(&index->names); |
2109
|
0
|
|
|
|
|
|
return git_vector_get(&index->names, n); |
2110
|
|
|
|
|
|
|
} |
2111
|
|
|
|
|
|
|
|
2112
|
0
|
|
|
|
|
|
static void index_name_entry_free(git_index_name_entry *ne) |
2113
|
|
|
|
|
|
|
{ |
2114
|
0
|
0
|
|
|
|
|
if (!ne) |
2115
|
0
|
|
|
|
|
|
return; |
2116
|
0
|
|
|
|
|
|
git__free(ne->ancestor); |
2117
|
0
|
|
|
|
|
|
git__free(ne->ours); |
2118
|
0
|
|
|
|
|
|
git__free(ne->theirs); |
2119
|
0
|
|
|
|
|
|
git__free(ne); |
2120
|
|
|
|
|
|
|
} |
2121
|
|
|
|
|
|
|
|
2122
|
0
|
|
|
|
|
|
int git_index_name_add(git_index *index, |
2123
|
|
|
|
|
|
|
const char *ancestor, const char *ours, const char *theirs) |
2124
|
|
|
|
|
|
|
{ |
2125
|
|
|
|
|
|
|
git_index_name_entry *conflict_name; |
2126
|
|
|
|
|
|
|
|
2127
|
0
|
0
|
|
|
|
|
assert((ancestor && ours) || (ancestor && theirs) || (ours && theirs)); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
2128
|
|
|
|
|
|
|
|
2129
|
0
|
|
|
|
|
|
conflict_name = git__calloc(1, sizeof(git_index_name_entry)); |
2130
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(conflict_name); |
2131
|
|
|
|
|
|
|
|
2132
|
0
|
0
|
|
|
|
|
if ((ancestor && !(conflict_name->ancestor = git__strdup(ancestor))) || |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
2133
|
0
|
0
|
|
|
|
|
(ours && !(conflict_name->ours = git__strdup(ours))) || |
|
|
0
|
|
|
|
|
|
2134
|
0
|
|
|
|
|
|
(theirs && !(conflict_name->theirs = git__strdup(theirs))) || |
2135
|
0
|
|
|
|
|
|
git_vector_insert(&index->names, conflict_name) < 0) |
2136
|
|
|
|
|
|
|
{ |
2137
|
0
|
|
|
|
|
|
index_name_entry_free(conflict_name); |
2138
|
0
|
|
|
|
|
|
return -1; |
2139
|
|
|
|
|
|
|
} |
2140
|
|
|
|
|
|
|
|
2141
|
0
|
|
|
|
|
|
index->dirty = 1; |
2142
|
0
|
|
|
|
|
|
return 0; |
2143
|
|
|
|
|
|
|
} |
2144
|
|
|
|
|
|
|
|
2145
|
123
|
|
|
|
|
|
int git_index_name_clear(git_index *index) |
2146
|
|
|
|
|
|
|
{ |
2147
|
|
|
|
|
|
|
size_t i; |
2148
|
|
|
|
|
|
|
git_index_name_entry *conflict_name; |
2149
|
|
|
|
|
|
|
|
2150
|
123
|
50
|
|
|
|
|
assert(index); |
2151
|
|
|
|
|
|
|
|
2152
|
123
|
50
|
|
|
|
|
git_vector_foreach(&index->names, i, conflict_name) |
2153
|
0
|
|
|
|
|
|
index_name_entry_free(conflict_name); |
2154
|
|
|
|
|
|
|
|
2155
|
123
|
|
|
|
|
|
git_vector_clear(&index->names); |
2156
|
|
|
|
|
|
|
|
2157
|
123
|
|
|
|
|
|
index->dirty = 1; |
2158
|
|
|
|
|
|
|
|
2159
|
123
|
|
|
|
|
|
return 0; |
2160
|
|
|
|
|
|
|
} |
2161
|
|
|
|
|
|
|
|
2162
|
0
|
|
|
|
|
|
size_t git_index_reuc_entrycount(git_index *index) |
2163
|
|
|
|
|
|
|
{ |
2164
|
0
|
0
|
|
|
|
|
assert(index); |
2165
|
0
|
|
|
|
|
|
return index->reuc.length; |
2166
|
|
|
|
|
|
|
} |
2167
|
|
|
|
|
|
|
|
2168
|
4
|
|
|
|
|
|
static int index_reuc_on_dup(void **old, void *new) |
2169
|
|
|
|
|
|
|
{ |
2170
|
4
|
|
|
|
|
|
index_entry_reuc_free(*old); |
2171
|
4
|
|
|
|
|
|
*old = new; |
2172
|
4
|
|
|
|
|
|
return GIT_EEXISTS; |
2173
|
|
|
|
|
|
|
} |
2174
|
|
|
|
|
|
|
|
2175
|
10
|
|
|
|
|
|
static int index_reuc_insert( |
2176
|
|
|
|
|
|
|
git_index *index, |
2177
|
|
|
|
|
|
|
git_index_reuc_entry *reuc) |
2178
|
|
|
|
|
|
|
{ |
2179
|
|
|
|
|
|
|
int res; |
2180
|
|
|
|
|
|
|
|
2181
|
10
|
50
|
|
|
|
|
assert(index && reuc && reuc->path != NULL); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
2182
|
10
|
50
|
|
|
|
|
assert(git_vector_is_sorted(&index->reuc)); |
2183
|
|
|
|
|
|
|
|
2184
|
10
|
|
|
|
|
|
res = git_vector_insert_sorted(&index->reuc, reuc, &index_reuc_on_dup); |
2185
|
10
|
|
|
|
|
|
index->dirty = 1; |
2186
|
|
|
|
|
|
|
|
2187
|
10
|
100
|
|
|
|
|
return res == GIT_EEXISTS ? 0 : res; |
2188
|
|
|
|
|
|
|
} |
2189
|
|
|
|
|
|
|
|
2190
|
10
|
|
|
|
|
|
int git_index_reuc_add(git_index *index, const char *path, |
2191
|
|
|
|
|
|
|
int ancestor_mode, const git_oid *ancestor_oid, |
2192
|
|
|
|
|
|
|
int our_mode, const git_oid *our_oid, |
2193
|
|
|
|
|
|
|
int their_mode, const git_oid *their_oid) |
2194
|
|
|
|
|
|
|
{ |
2195
|
10
|
|
|
|
|
|
git_index_reuc_entry *reuc = NULL; |
2196
|
10
|
|
|
|
|
|
int error = 0; |
2197
|
|
|
|
|
|
|
|
2198
|
10
|
50
|
|
|
|
|
assert(index && path); |
|
|
50
|
|
|
|
|
|
2199
|
|
|
|
|
|
|
|
2200
|
10
|
50
|
|
|
|
|
if ((error = index_entry_reuc_init(&reuc, path, ancestor_mode, |
2201
|
10
|
50
|
|
|
|
|
ancestor_oid, our_mode, our_oid, their_mode, their_oid)) < 0 || |
2202
|
10
|
|
|
|
|
|
(error = index_reuc_insert(index, reuc)) < 0) |
2203
|
0
|
|
|
|
|
|
index_entry_reuc_free(reuc); |
2204
|
|
|
|
|
|
|
|
2205
|
10
|
|
|
|
|
|
return error; |
2206
|
|
|
|
|
|
|
} |
2207
|
|
|
|
|
|
|
|
2208
|
4
|
|
|
|
|
|
int git_index_reuc_find(size_t *at_pos, git_index *index, const char *path) |
2209
|
|
|
|
|
|
|
{ |
2210
|
4
|
|
|
|
|
|
return git_vector_bsearch2(at_pos, &index->reuc, index->reuc_search, path); |
2211
|
|
|
|
|
|
|
} |
2212
|
|
|
|
|
|
|
|
2213
|
6
|
|
|
|
|
|
const git_index_reuc_entry *git_index_reuc_get_bypath( |
2214
|
|
|
|
|
|
|
git_index *index, const char *path) |
2215
|
|
|
|
|
|
|
{ |
2216
|
|
|
|
|
|
|
size_t pos; |
2217
|
6
|
50
|
|
|
|
|
assert(index && path); |
|
|
50
|
|
|
|
|
|
2218
|
|
|
|
|
|
|
|
2219
|
6
|
100
|
|
|
|
|
if (!index->reuc.length) |
2220
|
2
|
|
|
|
|
|
return NULL; |
2221
|
|
|
|
|
|
|
|
2222
|
4
|
50
|
|
|
|
|
assert(git_vector_is_sorted(&index->reuc)); |
2223
|
|
|
|
|
|
|
|
2224
|
4
|
50
|
|
|
|
|
if (git_index_reuc_find(&pos, index, path) < 0) |
2225
|
0
|
|
|
|
|
|
return NULL; |
2226
|
|
|
|
|
|
|
|
2227
|
6
|
|
|
|
|
|
return git_vector_get(&index->reuc, pos); |
2228
|
|
|
|
|
|
|
} |
2229
|
|
|
|
|
|
|
|
2230
|
0
|
|
|
|
|
|
const git_index_reuc_entry *git_index_reuc_get_byindex( |
2231
|
|
|
|
|
|
|
git_index *index, size_t n) |
2232
|
|
|
|
|
|
|
{ |
2233
|
0
|
0
|
|
|
|
|
assert(index); |
2234
|
0
|
0
|
|
|
|
|
assert(git_vector_is_sorted(&index->reuc)); |
2235
|
|
|
|
|
|
|
|
2236
|
0
|
|
|
|
|
|
return git_vector_get(&index->reuc, n); |
2237
|
|
|
|
|
|
|
} |
2238
|
|
|
|
|
|
|
|
2239
|
0
|
|
|
|
|
|
int git_index_reuc_remove(git_index *index, size_t position) |
2240
|
|
|
|
|
|
|
{ |
2241
|
|
|
|
|
|
|
int error; |
2242
|
|
|
|
|
|
|
git_index_reuc_entry *reuc; |
2243
|
|
|
|
|
|
|
|
2244
|
0
|
0
|
|
|
|
|
assert(git_vector_is_sorted(&index->reuc)); |
2245
|
|
|
|
|
|
|
|
2246
|
0
|
|
|
|
|
|
reuc = git_vector_get(&index->reuc, position); |
2247
|
0
|
|
|
|
|
|
error = git_vector_remove(&index->reuc, position); |
2248
|
|
|
|
|
|
|
|
2249
|
0
|
0
|
|
|
|
|
if (!error) |
2250
|
0
|
|
|
|
|
|
index_entry_reuc_free(reuc); |
2251
|
|
|
|
|
|
|
|
2252
|
0
|
|
|
|
|
|
index->dirty = 1; |
2253
|
0
|
|
|
|
|
|
return error; |
2254
|
|
|
|
|
|
|
} |
2255
|
|
|
|
|
|
|
|
2256
|
123
|
|
|
|
|
|
int git_index_reuc_clear(git_index *index) |
2257
|
|
|
|
|
|
|
{ |
2258
|
|
|
|
|
|
|
size_t i; |
2259
|
|
|
|
|
|
|
|
2260
|
123
|
50
|
|
|
|
|
assert(index); |
2261
|
|
|
|
|
|
|
|
2262
|
129
|
100
|
|
|
|
|
for (i = 0; i < index->reuc.length; ++i) |
2263
|
6
|
|
|
|
|
|
index_entry_reuc_free(git__swap(index->reuc.contents[i], NULL)); |
2264
|
|
|
|
|
|
|
|
2265
|
123
|
|
|
|
|
|
git_vector_clear(&index->reuc); |
2266
|
|
|
|
|
|
|
|
2267
|
123
|
|
|
|
|
|
index->dirty = 1; |
2268
|
|
|
|
|
|
|
|
2269
|
123
|
|
|
|
|
|
return 0; |
2270
|
|
|
|
|
|
|
} |
2271
|
|
|
|
|
|
|
|
2272
|
0
|
|
|
|
|
|
static int index_error_invalid(const char *message) |
2273
|
|
|
|
|
|
|
{ |
2274
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "invalid data in index - %s", message); |
2275
|
0
|
|
|
|
|
|
return -1; |
2276
|
|
|
|
|
|
|
} |
2277
|
|
|
|
|
|
|
|
2278
|
0
|
|
|
|
|
|
static int read_reuc(git_index *index, const char *buffer, size_t size) |
2279
|
|
|
|
|
|
|
{ |
2280
|
|
|
|
|
|
|
const char *endptr; |
2281
|
|
|
|
|
|
|
size_t len; |
2282
|
|
|
|
|
|
|
int i; |
2283
|
|
|
|
|
|
|
|
2284
|
|
|
|
|
|
|
/* If called multiple times, the vector might already be initialized */ |
2285
|
0
|
|
|
|
|
|
if (index->reuc._alloc_size == 0 && |
2286
|
0
|
|
|
|
|
|
git_vector_init(&index->reuc, 16, reuc_cmp) < 0) |
2287
|
0
|
|
|
|
|
|
return -1; |
2288
|
|
|
|
|
|
|
|
2289
|
0
|
0
|
|
|
|
|
while (size) { |
2290
|
|
|
|
|
|
|
git_index_reuc_entry *lost; |
2291
|
|
|
|
|
|
|
|
2292
|
0
|
|
|
|
|
|
len = p_strnlen(buffer, size) + 1; |
2293
|
0
|
0
|
|
|
|
|
if (size <= len) |
2294
|
0
|
|
|
|
|
|
return index_error_invalid("reading reuc entries"); |
2295
|
|
|
|
|
|
|
|
2296
|
0
|
|
|
|
|
|
lost = reuc_entry_alloc(buffer); |
2297
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(lost); |
2298
|
|
|
|
|
|
|
|
2299
|
0
|
|
|
|
|
|
size -= len; |
2300
|
0
|
|
|
|
|
|
buffer += len; |
2301
|
|
|
|
|
|
|
|
2302
|
|
|
|
|
|
|
/* read 3 ASCII octal numbers for stage entries */ |
2303
|
0
|
0
|
|
|
|
|
for (i = 0; i < 3; i++) { |
2304
|
|
|
|
|
|
|
int64_t tmp; |
2305
|
|
|
|
|
|
|
|
2306
|
0
|
0
|
|
|
|
|
if (git__strntol64(&tmp, buffer, size, &endptr, 8) < 0 || |
|
|
0
|
|
|
|
|
|
2307
|
0
|
0
|
|
|
|
|
!endptr || endptr == buffer || *endptr || |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
2308
|
0
|
0
|
|
|
|
|
tmp < 0 || tmp > UINT32_MAX) { |
2309
|
0
|
|
|
|
|
|
index_entry_reuc_free(lost); |
2310
|
0
|
|
|
|
|
|
return index_error_invalid("reading reuc entry stage"); |
2311
|
|
|
|
|
|
|
} |
2312
|
|
|
|
|
|
|
|
2313
|
0
|
|
|
|
|
|
lost->mode[i] = (uint32_t)tmp; |
2314
|
|
|
|
|
|
|
|
2315
|
0
|
|
|
|
|
|
len = (endptr + 1) - buffer; |
2316
|
0
|
0
|
|
|
|
|
if (size <= len) { |
2317
|
0
|
|
|
|
|
|
index_entry_reuc_free(lost); |
2318
|
0
|
|
|
|
|
|
return index_error_invalid("reading reuc entry stage"); |
2319
|
|
|
|
|
|
|
} |
2320
|
|
|
|
|
|
|
|
2321
|
0
|
|
|
|
|
|
size -= len; |
2322
|
0
|
|
|
|
|
|
buffer += len; |
2323
|
|
|
|
|
|
|
} |
2324
|
|
|
|
|
|
|
|
2325
|
|
|
|
|
|
|
/* read up to 3 OIDs for stage entries */ |
2326
|
0
|
0
|
|
|
|
|
for (i = 0; i < 3; i++) { |
2327
|
0
|
0
|
|
|
|
|
if (!lost->mode[i]) |
2328
|
0
|
|
|
|
|
|
continue; |
2329
|
0
|
0
|
|
|
|
|
if (size < 20) { |
2330
|
0
|
|
|
|
|
|
index_entry_reuc_free(lost); |
2331
|
0
|
|
|
|
|
|
return index_error_invalid("reading reuc entry oid"); |
2332
|
|
|
|
|
|
|
} |
2333
|
|
|
|
|
|
|
|
2334
|
0
|
|
|
|
|
|
git_oid_fromraw(&lost->oid[i], (const unsigned char *) buffer); |
2335
|
0
|
|
|
|
|
|
size -= 20; |
2336
|
0
|
|
|
|
|
|
buffer += 20; |
2337
|
|
|
|
|
|
|
} |
2338
|
|
|
|
|
|
|
|
2339
|
|
|
|
|
|
|
/* entry was read successfully - insert into reuc vector */ |
2340
|
0
|
0
|
|
|
|
|
if (git_vector_insert(&index->reuc, lost) < 0) |
2341
|
0
|
|
|
|
|
|
return -1; |
2342
|
|
|
|
|
|
|
} |
2343
|
|
|
|
|
|
|
|
2344
|
|
|
|
|
|
|
/* entries are guaranteed to be sorted on-disk */ |
2345
|
0
|
|
|
|
|
|
git_vector_set_sorted(&index->reuc, true); |
2346
|
|
|
|
|
|
|
|
2347
|
0
|
|
|
|
|
|
return 0; |
2348
|
|
|
|
|
|
|
} |
2349
|
|
|
|
|
|
|
|
2350
|
|
|
|
|
|
|
|
2351
|
0
|
|
|
|
|
|
static int read_conflict_names(git_index *index, const char *buffer, size_t size) |
2352
|
|
|
|
|
|
|
{ |
2353
|
|
|
|
|
|
|
size_t len; |
2354
|
|
|
|
|
|
|
|
2355
|
|
|
|
|
|
|
/* This gets called multiple times, the vector might already be initialized */ |
2356
|
0
|
|
|
|
|
|
if (index->names._alloc_size == 0 && |
2357
|
0
|
|
|
|
|
|
git_vector_init(&index->names, 16, conflict_name_cmp) < 0) |
2358
|
0
|
|
|
|
|
|
return -1; |
2359
|
|
|
|
|
|
|
|
2360
|
|
|
|
|
|
|
#define read_conflict_name(ptr) \ |
2361
|
|
|
|
|
|
|
len = p_strnlen(buffer, size) + 1; \ |
2362
|
|
|
|
|
|
|
if (size < len) { \ |
2363
|
|
|
|
|
|
|
index_error_invalid("reading conflict name entries"); \ |
2364
|
|
|
|
|
|
|
goto out_err; \ |
2365
|
|
|
|
|
|
|
} \ |
2366
|
|
|
|
|
|
|
if (len == 1) \ |
2367
|
|
|
|
|
|
|
ptr = NULL; \ |
2368
|
|
|
|
|
|
|
else { \ |
2369
|
|
|
|
|
|
|
ptr = git__malloc(len); \ |
2370
|
|
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(ptr); \ |
2371
|
|
|
|
|
|
|
memcpy(ptr, buffer, len); \ |
2372
|
|
|
|
|
|
|
} \ |
2373
|
|
|
|
|
|
|
\ |
2374
|
|
|
|
|
|
|
buffer += len; \ |
2375
|
|
|
|
|
|
|
size -= len; |
2376
|
|
|
|
|
|
|
|
2377
|
0
|
0
|
|
|
|
|
while (size) { |
2378
|
0
|
|
|
|
|
|
git_index_name_entry *conflict_name = git__calloc(1, sizeof(git_index_name_entry)); |
2379
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(conflict_name); |
2380
|
|
|
|
|
|
|
|
2381
|
0
|
0
|
|
|
|
|
read_conflict_name(conflict_name->ancestor); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
2382
|
0
|
0
|
|
|
|
|
read_conflict_name(conflict_name->ours); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
2383
|
0
|
0
|
|
|
|
|
read_conflict_name(conflict_name->theirs); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
2384
|
|
|
|
|
|
|
|
2385
|
0
|
0
|
|
|
|
|
if (git_vector_insert(&index->names, conflict_name) < 0) |
2386
|
0
|
|
|
|
|
|
goto out_err; |
2387
|
|
|
|
|
|
|
|
2388
|
0
|
|
|
|
|
|
continue; |
2389
|
|
|
|
|
|
|
|
2390
|
|
|
|
|
|
|
out_err: |
2391
|
0
|
|
|
|
|
|
git__free(conflict_name->ancestor); |
2392
|
0
|
|
|
|
|
|
git__free(conflict_name->ours); |
2393
|
0
|
|
|
|
|
|
git__free(conflict_name->theirs); |
2394
|
0
|
|
|
|
|
|
git__free(conflict_name); |
2395
|
0
|
|
|
|
|
|
return -1; |
2396
|
|
|
|
|
|
|
} |
2397
|
|
|
|
|
|
|
|
2398
|
|
|
|
|
|
|
#undef read_conflict_name |
2399
|
|
|
|
|
|
|
|
2400
|
|
|
|
|
|
|
/* entries are guaranteed to be sorted on-disk */ |
2401
|
0
|
|
|
|
|
|
git_vector_set_sorted(&index->names, true); |
2402
|
|
|
|
|
|
|
|
2403
|
0
|
|
|
|
|
|
return 0; |
2404
|
|
|
|
|
|
|
} |
2405
|
|
|
|
|
|
|
|
2406
|
232
|
|
|
|
|
|
static size_t index_entry_size(size_t path_len, size_t varint_len, uint32_t flags) |
2407
|
|
|
|
|
|
|
{ |
2408
|
232
|
50
|
|
|
|
|
if (varint_len) { |
2409
|
0
|
0
|
|
|
|
|
if (flags & GIT_INDEX_ENTRY_EXTENDED) |
2410
|
0
|
|
|
|
|
|
return offsetof(struct entry_long, path) + path_len + 1 + varint_len; |
2411
|
|
|
|
|
|
|
else |
2412
|
0
|
|
|
|
|
|
return offsetof(struct entry_short, path) + path_len + 1 + varint_len; |
2413
|
|
|
|
|
|
|
} else { |
2414
|
|
|
|
|
|
|
#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7) |
2415
|
232
|
50
|
|
|
|
|
if (flags & GIT_INDEX_ENTRY_EXTENDED) |
2416
|
0
|
|
|
|
|
|
return entry_size(struct entry_long, path_len); |
2417
|
|
|
|
|
|
|
else |
2418
|
232
|
|
|
|
|
|
return entry_size(struct entry_short, path_len); |
2419
|
|
|
|
|
|
|
#undef entry_size |
2420
|
|
|
|
|
|
|
} |
2421
|
|
|
|
|
|
|
} |
2422
|
|
|
|
|
|
|
|
2423
|
26
|
|
|
|
|
|
static int read_entry( |
2424
|
|
|
|
|
|
|
git_index_entry **out, |
2425
|
|
|
|
|
|
|
size_t *out_size, |
2426
|
|
|
|
|
|
|
git_index *index, |
2427
|
|
|
|
|
|
|
const void *buffer, |
2428
|
|
|
|
|
|
|
size_t buffer_size, |
2429
|
|
|
|
|
|
|
const char *last) |
2430
|
|
|
|
|
|
|
{ |
2431
|
|
|
|
|
|
|
size_t path_length, entry_size; |
2432
|
|
|
|
|
|
|
const char *path_ptr; |
2433
|
|
|
|
|
|
|
struct entry_short source; |
2434
|
26
|
|
|
|
|
|
git_index_entry entry = {{0}}; |
2435
|
26
|
|
|
|
|
|
bool compressed = index->version >= INDEX_VERSION_NUMBER_COMP; |
2436
|
26
|
|
|
|
|
|
char *tmp_path = NULL; |
2437
|
|
|
|
|
|
|
|
2438
|
26
|
50
|
|
|
|
|
if (INDEX_FOOTER_SIZE + minimal_entry_size > buffer_size) |
2439
|
0
|
|
|
|
|
|
return -1; |
2440
|
|
|
|
|
|
|
|
2441
|
|
|
|
|
|
|
/* buffer is not guaranteed to be aligned */ |
2442
|
26
|
|
|
|
|
|
memcpy(&source, buffer, sizeof(struct entry_short)); |
2443
|
|
|
|
|
|
|
|
2444
|
26
|
|
|
|
|
|
entry.ctime.seconds = (git_time_t)ntohl(source.ctime.seconds); |
2445
|
26
|
|
|
|
|
|
entry.ctime.nanoseconds = ntohl(source.ctime.nanoseconds); |
2446
|
26
|
|
|
|
|
|
entry.mtime.seconds = (git_time_t)ntohl(source.mtime.seconds); |
2447
|
26
|
|
|
|
|
|
entry.mtime.nanoseconds = ntohl(source.mtime.nanoseconds); |
2448
|
26
|
|
|
|
|
|
entry.dev = ntohl(source.dev); |
2449
|
26
|
|
|
|
|
|
entry.ino = ntohl(source.ino); |
2450
|
26
|
|
|
|
|
|
entry.mode = ntohl(source.mode); |
2451
|
26
|
|
|
|
|
|
entry.uid = ntohl(source.uid); |
2452
|
26
|
|
|
|
|
|
entry.gid = ntohl(source.gid); |
2453
|
26
|
|
|
|
|
|
entry.file_size = ntohl(source.file_size); |
2454
|
26
|
|
|
|
|
|
git_oid_cpy(&entry.id, &source.oid); |
2455
|
26
|
|
|
|
|
|
entry.flags = ntohs(source.flags); |
2456
|
|
|
|
|
|
|
|
2457
|
26
|
50
|
|
|
|
|
if (entry.flags & GIT_INDEX_ENTRY_EXTENDED) { |
2458
|
|
|
|
|
|
|
uint16_t flags_raw; |
2459
|
|
|
|
|
|
|
size_t flags_offset; |
2460
|
|
|
|
|
|
|
|
2461
|
0
|
|
|
|
|
|
flags_offset = offsetof(struct entry_long, flags_extended); |
2462
|
0
|
|
|
|
|
|
memcpy(&flags_raw, (const char *) buffer + flags_offset, |
2463
|
|
|
|
|
|
|
sizeof(flags_raw)); |
2464
|
0
|
|
|
|
|
|
flags_raw = ntohs(flags_raw); |
2465
|
|
|
|
|
|
|
|
2466
|
0
|
|
|
|
|
|
memcpy(&entry.flags_extended, &flags_raw, sizeof(flags_raw)); |
2467
|
0
|
|
|
|
|
|
path_ptr = (const char *) buffer + offsetof(struct entry_long, path); |
2468
|
|
|
|
|
|
|
} else |
2469
|
26
|
|
|
|
|
|
path_ptr = (const char *) buffer + offsetof(struct entry_short, path); |
2470
|
|
|
|
|
|
|
|
2471
|
26
|
50
|
|
|
|
|
if (!compressed) { |
2472
|
26
|
|
|
|
|
|
path_length = entry.flags & GIT_INDEX_ENTRY_NAMEMASK; |
2473
|
|
|
|
|
|
|
|
2474
|
|
|
|
|
|
|
/* if this is a very long string, we must find its |
2475
|
|
|
|
|
|
|
* real length without overflowing */ |
2476
|
26
|
50
|
|
|
|
|
if (path_length == 0xFFF) { |
2477
|
|
|
|
|
|
|
const char *path_end; |
2478
|
|
|
|
|
|
|
|
2479
|
0
|
|
|
|
|
|
path_end = memchr(path_ptr, '\0', buffer_size); |
2480
|
0
|
0
|
|
|
|
|
if (path_end == NULL) |
2481
|
0
|
|
|
|
|
|
return -1; |
2482
|
|
|
|
|
|
|
|
2483
|
0
|
|
|
|
|
|
path_length = path_end - path_ptr; |
2484
|
|
|
|
|
|
|
} |
2485
|
|
|
|
|
|
|
|
2486
|
26
|
|
|
|
|
|
entry_size = index_entry_size(path_length, 0, entry.flags); |
2487
|
26
|
|
|
|
|
|
entry.path = (char *)path_ptr; |
2488
|
|
|
|
|
|
|
} else { |
2489
|
|
|
|
|
|
|
size_t varint_len, last_len, prefix_len, suffix_len, path_len; |
2490
|
|
|
|
|
|
|
uintmax_t strip_len; |
2491
|
|
|
|
|
|
|
|
2492
|
0
|
|
|
|
|
|
strip_len = git_decode_varint((const unsigned char *)path_ptr, &varint_len); |
2493
|
0
|
|
|
|
|
|
last_len = strlen(last); |
2494
|
|
|
|
|
|
|
|
2495
|
0
|
0
|
|
|
|
|
if (varint_len == 0 || last_len < strip_len) |
|
|
0
|
|
|
|
|
|
2496
|
0
|
|
|
|
|
|
return index_error_invalid("incorrect prefix length"); |
2497
|
|
|
|
|
|
|
|
2498
|
0
|
|
|
|
|
|
prefix_len = last_len - (size_t)strip_len; |
2499
|
0
|
|
|
|
|
|
suffix_len = strlen(path_ptr + varint_len); |
2500
|
|
|
|
|
|
|
|
2501
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&path_len, prefix_len, suffix_len); |
|
|
0
|
|
|
|
|
|
2502
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&path_len, path_len, 1); |
|
|
0
|
|
|
|
|
|
2503
|
|
|
|
|
|
|
|
2504
|
0
|
0
|
|
|
|
|
if (path_len > GIT_PATH_MAX) |
2505
|
0
|
|
|
|
|
|
return index_error_invalid("unreasonable path length"); |
2506
|
|
|
|
|
|
|
|
2507
|
0
|
|
|
|
|
|
tmp_path = git__malloc(path_len); |
2508
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(tmp_path); |
2509
|
|
|
|
|
|
|
|
2510
|
0
|
|
|
|
|
|
memcpy(tmp_path, last, prefix_len); |
2511
|
0
|
|
|
|
|
|
memcpy(tmp_path + prefix_len, path_ptr + varint_len, suffix_len + 1); |
2512
|
0
|
|
|
|
|
|
entry_size = index_entry_size(suffix_len, varint_len, entry.flags); |
2513
|
0
|
|
|
|
|
|
entry.path = tmp_path; |
2514
|
|
|
|
|
|
|
} |
2515
|
|
|
|
|
|
|
|
2516
|
26
|
50
|
|
|
|
|
if (entry_size == 0) |
2517
|
0
|
|
|
|
|
|
return -1; |
2518
|
|
|
|
|
|
|
|
2519
|
26
|
50
|
|
|
|
|
if (INDEX_FOOTER_SIZE + entry_size > buffer_size) |
2520
|
0
|
|
|
|
|
|
return -1; |
2521
|
|
|
|
|
|
|
|
2522
|
26
|
50
|
|
|
|
|
if (index_entry_dup(out, index, &entry) < 0) { |
2523
|
0
|
|
|
|
|
|
git__free(tmp_path); |
2524
|
0
|
|
|
|
|
|
return -1; |
2525
|
|
|
|
|
|
|
} |
2526
|
|
|
|
|
|
|
|
2527
|
26
|
|
|
|
|
|
git__free(tmp_path); |
2528
|
26
|
|
|
|
|
|
*out_size = entry_size; |
2529
|
26
|
|
|
|
|
|
return 0; |
2530
|
|
|
|
|
|
|
} |
2531
|
|
|
|
|
|
|
|
2532
|
11
|
|
|
|
|
|
static int read_header(struct index_header *dest, const void *buffer) |
2533
|
|
|
|
|
|
|
{ |
2534
|
11
|
|
|
|
|
|
const struct index_header *source = buffer; |
2535
|
|
|
|
|
|
|
|
2536
|
11
|
|
|
|
|
|
dest->signature = ntohl(source->signature); |
2537
|
11
|
50
|
|
|
|
|
if (dest->signature != INDEX_HEADER_SIG) |
2538
|
0
|
|
|
|
|
|
return index_error_invalid("incorrect header signature"); |
2539
|
|
|
|
|
|
|
|
2540
|
11
|
|
|
|
|
|
dest->version = ntohl(source->version); |
2541
|
11
|
50
|
|
|
|
|
if (dest->version < INDEX_VERSION_NUMBER_LB || |
|
|
50
|
|
|
|
|
|
2542
|
11
|
|
|
|
|
|
dest->version > INDEX_VERSION_NUMBER_UB) |
2543
|
0
|
|
|
|
|
|
return index_error_invalid("incorrect header version"); |
2544
|
|
|
|
|
|
|
|
2545
|
11
|
|
|
|
|
|
dest->entry_count = ntohl(source->entry_count); |
2546
|
11
|
|
|
|
|
|
return 0; |
2547
|
|
|
|
|
|
|
} |
2548
|
|
|
|
|
|
|
|
2549
|
11
|
|
|
|
|
|
static int read_extension(size_t *read_len, git_index *index, const char *buffer, size_t buffer_size) |
2550
|
|
|
|
|
|
|
{ |
2551
|
|
|
|
|
|
|
struct index_extension dest; |
2552
|
|
|
|
|
|
|
size_t total_size; |
2553
|
|
|
|
|
|
|
|
2554
|
|
|
|
|
|
|
/* buffer is not guaranteed to be aligned */ |
2555
|
11
|
|
|
|
|
|
memcpy(&dest, buffer, sizeof(struct index_extension)); |
2556
|
11
|
|
|
|
|
|
dest.extension_size = ntohl(dest.extension_size); |
2557
|
|
|
|
|
|
|
|
2558
|
11
|
|
|
|
|
|
total_size = dest.extension_size + sizeof(struct index_extension); |
2559
|
|
|
|
|
|
|
|
2560
|
11
|
50
|
|
|
|
|
if (dest.extension_size > total_size || |
|
|
50
|
|
|
|
|
|
2561
|
11
|
50
|
|
|
|
|
buffer_size < total_size || |
2562
|
11
|
|
|
|
|
|
buffer_size - total_size < INDEX_FOOTER_SIZE) { |
2563
|
0
|
|
|
|
|
|
index_error_invalid("extension is truncated"); |
2564
|
0
|
|
|
|
|
|
return -1; |
2565
|
|
|
|
|
|
|
} |
2566
|
|
|
|
|
|
|
|
2567
|
|
|
|
|
|
|
/* optional extension */ |
2568
|
11
|
50
|
|
|
|
|
if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') { |
|
|
50
|
|
|
|
|
|
2569
|
|
|
|
|
|
|
/* tree cache */ |
2570
|
22
|
50
|
|
|
|
|
if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) { |
2571
|
11
|
50
|
|
|
|
|
if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, &index->tree_pool) < 0) |
2572
|
0
|
|
|
|
|
|
return -1; |
2573
|
0
|
0
|
|
|
|
|
} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) { |
2574
|
0
|
0
|
|
|
|
|
if (read_reuc(index, buffer + 8, dest.extension_size) < 0) |
2575
|
0
|
|
|
|
|
|
return -1; |
2576
|
0
|
0
|
|
|
|
|
} else if (memcmp(dest.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4) == 0) { |
2577
|
0
|
0
|
|
|
|
|
if (read_conflict_names(index, buffer + 8, dest.extension_size) < 0) |
2578
|
0
|
|
|
|
|
|
return -1; |
2579
|
|
|
|
|
|
|
} |
2580
|
|
|
|
|
|
|
/* else, unsupported extension. We cannot parse this, but we can skip |
2581
|
|
|
|
|
|
|
* it by returning `total_size */ |
2582
|
|
|
|
|
|
|
} else { |
2583
|
|
|
|
|
|
|
/* we cannot handle non-ignorable extensions; |
2584
|
|
|
|
|
|
|
* in fact they aren't even defined in the standard */ |
2585
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "unsupported mandatory extension: '%.4s'", dest.signature); |
2586
|
0
|
|
|
|
|
|
return -1; |
2587
|
|
|
|
|
|
|
} |
2588
|
|
|
|
|
|
|
|
2589
|
11
|
|
|
|
|
|
*read_len = total_size; |
2590
|
|
|
|
|
|
|
|
2591
|
11
|
|
|
|
|
|
return 0; |
2592
|
|
|
|
|
|
|
} |
2593
|
|
|
|
|
|
|
|
2594
|
11
|
|
|
|
|
|
static int parse_index(git_index *index, const char *buffer, size_t buffer_size) |
2595
|
|
|
|
|
|
|
{ |
2596
|
11
|
|
|
|
|
|
int error = 0; |
2597
|
|
|
|
|
|
|
unsigned int i; |
2598
|
11
|
|
|
|
|
|
struct index_header header = { 0 }; |
2599
|
|
|
|
|
|
|
git_oid checksum_calculated, checksum_expected; |
2600
|
11
|
|
|
|
|
|
const char *last = NULL; |
2601
|
11
|
|
|
|
|
|
const char *empty = ""; |
2602
|
|
|
|
|
|
|
|
2603
|
|
|
|
|
|
|
#define seek_forward(_increase) { \ |
2604
|
|
|
|
|
|
|
if (_increase >= buffer_size) { \ |
2605
|
|
|
|
|
|
|
error = index_error_invalid("ran out of data while parsing"); \ |
2606
|
|
|
|
|
|
|
goto done; } \ |
2607
|
|
|
|
|
|
|
buffer += _increase; \ |
2608
|
|
|
|
|
|
|
buffer_size -= _increase;\ |
2609
|
|
|
|
|
|
|
} |
2610
|
|
|
|
|
|
|
|
2611
|
11
|
50
|
|
|
|
|
if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE) |
2612
|
0
|
|
|
|
|
|
return index_error_invalid("insufficient buffer space"); |
2613
|
|
|
|
|
|
|
|
2614
|
|
|
|
|
|
|
/* Precalculate the SHA1 of the files's contents -- we'll match it to |
2615
|
|
|
|
|
|
|
* the provided SHA1 in the footer */ |
2616
|
11
|
|
|
|
|
|
git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE); |
2617
|
|
|
|
|
|
|
|
2618
|
|
|
|
|
|
|
/* Parse header */ |
2619
|
11
|
50
|
|
|
|
|
if ((error = read_header(&header, buffer)) < 0) |
2620
|
0
|
|
|
|
|
|
return error; |
2621
|
|
|
|
|
|
|
|
2622
|
11
|
|
|
|
|
|
index->version = header.version; |
2623
|
11
|
50
|
|
|
|
|
if (index->version >= INDEX_VERSION_NUMBER_COMP) |
2624
|
0
|
|
|
|
|
|
last = empty; |
2625
|
|
|
|
|
|
|
|
2626
|
11
|
50
|
|
|
|
|
seek_forward(INDEX_HEADER_SIZE); |
2627
|
|
|
|
|
|
|
|
2628
|
11
|
50
|
|
|
|
|
assert(!index->entries.length); |
2629
|
|
|
|
|
|
|
|
2630
|
11
|
50
|
|
|
|
|
if ((error = index_map_resize(index->entries_map, header.entry_count, index->ignore_case)) < 0) |
2631
|
0
|
|
|
|
|
|
return error; |
2632
|
|
|
|
|
|
|
|
2633
|
|
|
|
|
|
|
/* Parse all the entries */ |
2634
|
37
|
100
|
|
|
|
|
for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) { |
|
|
50
|
|
|
|
|
|
2635
|
26
|
|
|
|
|
|
git_index_entry *entry = NULL; |
2636
|
|
|
|
|
|
|
size_t entry_size; |
2637
|
|
|
|
|
|
|
|
2638
|
26
|
50
|
|
|
|
|
if ((error = read_entry(&entry, &entry_size, index, buffer, buffer_size, last)) < 0) { |
2639
|
0
|
|
|
|
|
|
error = index_error_invalid("invalid entry"); |
2640
|
0
|
|
|
|
|
|
goto done; |
2641
|
|
|
|
|
|
|
} |
2642
|
|
|
|
|
|
|
|
2643
|
26
|
50
|
|
|
|
|
if ((error = git_vector_insert(&index->entries, entry)) < 0) { |
2644
|
0
|
|
|
|
|
|
index_entry_free(entry); |
2645
|
0
|
|
|
|
|
|
goto done; |
2646
|
|
|
|
|
|
|
} |
2647
|
|
|
|
|
|
|
|
2648
|
26
|
50
|
|
|
|
|
if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0) { |
2649
|
0
|
|
|
|
|
|
index_entry_free(entry); |
2650
|
0
|
|
|
|
|
|
goto done; |
2651
|
|
|
|
|
|
|
} |
2652
|
26
|
|
|
|
|
|
error = 0; |
2653
|
|
|
|
|
|
|
|
2654
|
26
|
50
|
|
|
|
|
if (index->version >= INDEX_VERSION_NUMBER_COMP) |
2655
|
0
|
|
|
|
|
|
last = entry->path; |
2656
|
|
|
|
|
|
|
|
2657
|
26
|
50
|
|
|
|
|
seek_forward(entry_size); |
2658
|
|
|
|
|
|
|
} |
2659
|
|
|
|
|
|
|
|
2660
|
11
|
50
|
|
|
|
|
if (i != header.entry_count) { |
2661
|
0
|
|
|
|
|
|
error = index_error_invalid("header entries changed while parsing"); |
2662
|
0
|
|
|
|
|
|
goto done; |
2663
|
|
|
|
|
|
|
} |
2664
|
|
|
|
|
|
|
|
2665
|
|
|
|
|
|
|
/* There's still space for some extensions! */ |
2666
|
22
|
100
|
|
|
|
|
while (buffer_size > INDEX_FOOTER_SIZE) { |
2667
|
|
|
|
|
|
|
size_t extension_size; |
2668
|
|
|
|
|
|
|
|
2669
|
11
|
50
|
|
|
|
|
if ((error = read_extension(&extension_size, index, buffer, buffer_size)) < 0) { |
2670
|
0
|
|
|
|
|
|
goto done; |
2671
|
|
|
|
|
|
|
} |
2672
|
|
|
|
|
|
|
|
2673
|
11
|
50
|
|
|
|
|
seek_forward(extension_size); |
2674
|
|
|
|
|
|
|
} |
2675
|
|
|
|
|
|
|
|
2676
|
11
|
50
|
|
|
|
|
if (buffer_size != INDEX_FOOTER_SIZE) { |
2677
|
0
|
|
|
|
|
|
error = index_error_invalid( |
2678
|
|
|
|
|
|
|
"buffer size does not match index footer size"); |
2679
|
0
|
|
|
|
|
|
goto done; |
2680
|
|
|
|
|
|
|
} |
2681
|
|
|
|
|
|
|
|
2682
|
|
|
|
|
|
|
/* 160-bit SHA-1 over the content of the index file before this checksum. */ |
2683
|
11
|
|
|
|
|
|
git_oid_fromraw(&checksum_expected, (const unsigned char *)buffer); |
2684
|
|
|
|
|
|
|
|
2685
|
11
|
50
|
|
|
|
|
if (git_oid__cmp(&checksum_calculated, &checksum_expected) != 0) { |
2686
|
0
|
|
|
|
|
|
error = index_error_invalid( |
2687
|
|
|
|
|
|
|
"calculated checksum does not match expected"); |
2688
|
0
|
|
|
|
|
|
goto done; |
2689
|
|
|
|
|
|
|
} |
2690
|
|
|
|
|
|
|
|
2691
|
11
|
|
|
|
|
|
git_oid_cpy(&index->checksum, &checksum_calculated); |
2692
|
|
|
|
|
|
|
|
2693
|
|
|
|
|
|
|
#undef seek_forward |
2694
|
|
|
|
|
|
|
|
2695
|
|
|
|
|
|
|
/* Entries are stored case-sensitively on disk, so re-sort now if |
2696
|
|
|
|
|
|
|
* in-memory index is supposed to be case-insensitive |
2697
|
|
|
|
|
|
|
*/ |
2698
|
11
|
50
|
|
|
|
|
git_vector_set_sorted(&index->entries, !index->ignore_case); |
2699
|
11
|
|
|
|
|
|
git_vector_sort(&index->entries); |
2700
|
|
|
|
|
|
|
|
2701
|
11
|
|
|
|
|
|
index->dirty = 0; |
2702
|
|
|
|
|
|
|
done: |
2703
|
11
|
|
|
|
|
|
return error; |
2704
|
|
|
|
|
|
|
} |
2705
|
|
|
|
|
|
|
|
2706
|
97
|
|
|
|
|
|
static bool is_index_extended(git_index *index) |
2707
|
|
|
|
|
|
|
{ |
2708
|
|
|
|
|
|
|
size_t i, extended; |
2709
|
|
|
|
|
|
|
git_index_entry *entry; |
2710
|
|
|
|
|
|
|
|
2711
|
97
|
|
|
|
|
|
extended = 0; |
2712
|
|
|
|
|
|
|
|
2713
|
303
|
100
|
|
|
|
|
git_vector_foreach(&index->entries, i, entry) { |
2714
|
206
|
|
|
|
|
|
entry->flags &= ~GIT_INDEX_ENTRY_EXTENDED; |
2715
|
206
|
50
|
|
|
|
|
if (entry->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS) { |
2716
|
0
|
|
|
|
|
|
extended++; |
2717
|
0
|
|
|
|
|
|
entry->flags |= GIT_INDEX_ENTRY_EXTENDED; |
2718
|
|
|
|
|
|
|
} |
2719
|
|
|
|
|
|
|
} |
2720
|
|
|
|
|
|
|
|
2721
|
97
|
|
|
|
|
|
return (extended > 0); |
2722
|
|
|
|
|
|
|
} |
2723
|
|
|
|
|
|
|
|
2724
|
206
|
|
|
|
|
|
static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char *last) |
2725
|
|
|
|
|
|
|
{ |
2726
|
206
|
|
|
|
|
|
void *mem = NULL; |
2727
|
|
|
|
|
|
|
struct entry_short ondisk; |
2728
|
|
|
|
|
|
|
size_t path_len, disk_size; |
2729
|
206
|
|
|
|
|
|
int varint_len = 0; |
2730
|
|
|
|
|
|
|
char *path; |
2731
|
206
|
|
|
|
|
|
const char *path_start = entry->path; |
2732
|
206
|
|
|
|
|
|
size_t same_len = 0; |
2733
|
|
|
|
|
|
|
|
2734
|
206
|
|
|
|
|
|
path_len = ((struct entry_internal *)entry)->pathlen; |
2735
|
|
|
|
|
|
|
|
2736
|
206
|
50
|
|
|
|
|
if (last) { |
2737
|
0
|
|
|
|
|
|
const char *last_c = last; |
2738
|
|
|
|
|
|
|
|
2739
|
0
|
0
|
|
|
|
|
while (*path_start == *last_c) { |
2740
|
0
|
0
|
|
|
|
|
if (!*path_start || !*last_c) |
|
|
0
|
|
|
|
|
|
2741
|
|
|
|
|
|
|
break; |
2742
|
0
|
|
|
|
|
|
++path_start; |
2743
|
0
|
|
|
|
|
|
++last_c; |
2744
|
0
|
|
|
|
|
|
++same_len; |
2745
|
|
|
|
|
|
|
} |
2746
|
0
|
|
|
|
|
|
path_len -= same_len; |
2747
|
0
|
|
|
|
|
|
varint_len = git_encode_varint(NULL, 0, same_len); |
2748
|
|
|
|
|
|
|
} |
2749
|
|
|
|
|
|
|
|
2750
|
206
|
|
|
|
|
|
disk_size = index_entry_size(path_len, varint_len, entry->flags); |
2751
|
|
|
|
|
|
|
|
2752
|
206
|
50
|
|
|
|
|
if (git_filebuf_reserve(file, &mem, disk_size) < 0) |
2753
|
0
|
|
|
|
|
|
return -1; |
2754
|
|
|
|
|
|
|
|
2755
|
206
|
|
|
|
|
|
memset(mem, 0x0, disk_size); |
2756
|
|
|
|
|
|
|
|
2757
|
|
|
|
|
|
|
/** |
2758
|
|
|
|
|
|
|
* Yes, we have to truncate. |
2759
|
|
|
|
|
|
|
* |
2760
|
|
|
|
|
|
|
* The on-disk format for Index entries clearly defines |
2761
|
|
|
|
|
|
|
* the time and size fields to be 4 bytes each -- so even if |
2762
|
|
|
|
|
|
|
* we store these values with 8 bytes on-memory, they must |
2763
|
|
|
|
|
|
|
* be truncated to 4 bytes before writing to disk. |
2764
|
|
|
|
|
|
|
* |
2765
|
|
|
|
|
|
|
* In 2038 I will be either too dead or too rich to care about this |
2766
|
|
|
|
|
|
|
*/ |
2767
|
206
|
|
|
|
|
|
ondisk.ctime.seconds = htonl((uint32_t)entry->ctime.seconds); |
2768
|
206
|
|
|
|
|
|
ondisk.mtime.seconds = htonl((uint32_t)entry->mtime.seconds); |
2769
|
206
|
|
|
|
|
|
ondisk.ctime.nanoseconds = htonl(entry->ctime.nanoseconds); |
2770
|
206
|
|
|
|
|
|
ondisk.mtime.nanoseconds = htonl(entry->mtime.nanoseconds); |
2771
|
206
|
|
|
|
|
|
ondisk.dev = htonl(entry->dev); |
2772
|
206
|
|
|
|
|
|
ondisk.ino = htonl(entry->ino); |
2773
|
206
|
|
|
|
|
|
ondisk.mode = htonl(entry->mode); |
2774
|
206
|
|
|
|
|
|
ondisk.uid = htonl(entry->uid); |
2775
|
206
|
|
|
|
|
|
ondisk.gid = htonl(entry->gid); |
2776
|
206
|
|
|
|
|
|
ondisk.file_size = htonl((uint32_t)entry->file_size); |
2777
|
|
|
|
|
|
|
|
2778
|
206
|
|
|
|
|
|
git_oid_cpy(&ondisk.oid, &entry->id); |
2779
|
|
|
|
|
|
|
|
2780
|
206
|
|
|
|
|
|
ondisk.flags = htons(entry->flags); |
2781
|
|
|
|
|
|
|
|
2782
|
206
|
50
|
|
|
|
|
if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) { |
2783
|
|
|
|
|
|
|
struct entry_long ondisk_ext; |
2784
|
0
|
|
|
|
|
|
memcpy(&ondisk_ext, &ondisk, sizeof(struct entry_short)); |
2785
|
0
|
|
|
|
|
|
ondisk_ext.flags_extended = htons(entry->flags_extended & |
2786
|
|
|
|
|
|
|
GIT_INDEX_ENTRY_EXTENDED_FLAGS); |
2787
|
0
|
|
|
|
|
|
memcpy(mem, &ondisk_ext, offsetof(struct entry_long, path)); |
2788
|
0
|
|
|
|
|
|
path = ((struct entry_long*)mem)->path; |
2789
|
0
|
|
|
|
|
|
disk_size -= offsetof(struct entry_long, path); |
2790
|
|
|
|
|
|
|
} else { |
2791
|
206
|
|
|
|
|
|
memcpy(mem, &ondisk, offsetof(struct entry_short, path)); |
2792
|
206
|
|
|
|
|
|
path = ((struct entry_short*)mem)->path; |
2793
|
206
|
|
|
|
|
|
disk_size -= offsetof(struct entry_short, path); |
2794
|
|
|
|
|
|
|
} |
2795
|
|
|
|
|
|
|
|
2796
|
206
|
50
|
|
|
|
|
if (last) { |
2797
|
0
|
|
|
|
|
|
varint_len = git_encode_varint((unsigned char *) path, |
2798
|
|
|
|
|
|
|
disk_size, same_len); |
2799
|
0
|
0
|
|
|
|
|
assert(varint_len > 0); |
2800
|
0
|
|
|
|
|
|
path += varint_len; |
2801
|
0
|
|
|
|
|
|
disk_size -= varint_len; |
2802
|
|
|
|
|
|
|
|
2803
|
|
|
|
|
|
|
/* |
2804
|
|
|
|
|
|
|
* If using path compression, we are not allowed |
2805
|
|
|
|
|
|
|
* to have additional trailing NULs. |
2806
|
|
|
|
|
|
|
*/ |
2807
|
0
|
0
|
|
|
|
|
assert(disk_size == path_len + 1); |
2808
|
|
|
|
|
|
|
} else { |
2809
|
|
|
|
|
|
|
/* |
2810
|
|
|
|
|
|
|
* If no path compression is used, we do have |
2811
|
|
|
|
|
|
|
* NULs as padding. As such, simply assert that |
2812
|
|
|
|
|
|
|
* we have enough space left to write the path. |
2813
|
|
|
|
|
|
|
*/ |
2814
|
206
|
50
|
|
|
|
|
assert(disk_size > path_len); |
2815
|
|
|
|
|
|
|
} |
2816
|
|
|
|
|
|
|
|
2817
|
206
|
|
|
|
|
|
memcpy(path, path_start, path_len + 1); |
2818
|
|
|
|
|
|
|
|
2819
|
206
|
|
|
|
|
|
return 0; |
2820
|
|
|
|
|
|
|
} |
2821
|
|
|
|
|
|
|
|
2822
|
97
|
|
|
|
|
|
static int write_entries(git_index *index, git_filebuf *file) |
2823
|
|
|
|
|
|
|
{ |
2824
|
97
|
|
|
|
|
|
int error = 0; |
2825
|
|
|
|
|
|
|
size_t i; |
2826
|
|
|
|
|
|
|
git_vector case_sorted, *entries; |
2827
|
|
|
|
|
|
|
git_index_entry *entry; |
2828
|
97
|
|
|
|
|
|
const char *last = NULL; |
2829
|
|
|
|
|
|
|
|
2830
|
|
|
|
|
|
|
/* If index->entries is sorted case-insensitively, then we need |
2831
|
|
|
|
|
|
|
* to re-sort it case-sensitively before writing */ |
2832
|
97
|
50
|
|
|
|
|
if (index->ignore_case) { |
2833
|
0
|
|
|
|
|
|
git_vector_dup(&case_sorted, &index->entries, git_index_entry_cmp); |
2834
|
0
|
|
|
|
|
|
git_vector_sort(&case_sorted); |
2835
|
0
|
|
|
|
|
|
entries = &case_sorted; |
2836
|
|
|
|
|
|
|
} else { |
2837
|
97
|
|
|
|
|
|
entries = &index->entries; |
2838
|
|
|
|
|
|
|
} |
2839
|
|
|
|
|
|
|
|
2840
|
97
|
50
|
|
|
|
|
if (index->version >= INDEX_VERSION_NUMBER_COMP) |
2841
|
0
|
|
|
|
|
|
last = ""; |
2842
|
|
|
|
|
|
|
|
2843
|
303
|
100
|
|
|
|
|
git_vector_foreach(entries, i, entry) { |
2844
|
206
|
50
|
|
|
|
|
if ((error = write_disk_entry(file, entry, last)) < 0) |
2845
|
0
|
|
|
|
|
|
break; |
2846
|
206
|
50
|
|
|
|
|
if (index->version >= INDEX_VERSION_NUMBER_COMP) |
2847
|
0
|
|
|
|
|
|
last = entry->path; |
2848
|
|
|
|
|
|
|
} |
2849
|
|
|
|
|
|
|
|
2850
|
97
|
50
|
|
|
|
|
if (index->ignore_case) |
2851
|
0
|
|
|
|
|
|
git_vector_free(&case_sorted); |
2852
|
|
|
|
|
|
|
|
2853
|
97
|
|
|
|
|
|
return error; |
2854
|
|
|
|
|
|
|
} |
2855
|
|
|
|
|
|
|
|
2856
|
97
|
|
|
|
|
|
static int write_extension(git_filebuf *file, struct index_extension *header, git_buf *data) |
2857
|
|
|
|
|
|
|
{ |
2858
|
|
|
|
|
|
|
struct index_extension ondisk; |
2859
|
|
|
|
|
|
|
|
2860
|
97
|
|
|
|
|
|
memset(&ondisk, 0x0, sizeof(struct index_extension)); |
2861
|
97
|
|
|
|
|
|
memcpy(&ondisk, header, 4); |
2862
|
97
|
|
|
|
|
|
ondisk.extension_size = htonl(header->extension_size); |
2863
|
|
|
|
|
|
|
|
2864
|
97
|
|
|
|
|
|
git_filebuf_write(file, &ondisk, sizeof(struct index_extension)); |
2865
|
97
|
|
|
|
|
|
return git_filebuf_write(file, data->ptr, data->size); |
2866
|
|
|
|
|
|
|
} |
2867
|
|
|
|
|
|
|
|
2868
|
0
|
|
|
|
|
|
static int create_name_extension_data(git_buf *name_buf, git_index_name_entry *conflict_name) |
2869
|
|
|
|
|
|
|
{ |
2870
|
0
|
|
|
|
|
|
int error = 0; |
2871
|
|
|
|
|
|
|
|
2872
|
0
|
0
|
|
|
|
|
if (conflict_name->ancestor == NULL) |
2873
|
0
|
|
|
|
|
|
error = git_buf_put(name_buf, "\0", 1); |
2874
|
|
|
|
|
|
|
else |
2875
|
0
|
|
|
|
|
|
error = git_buf_put(name_buf, conflict_name->ancestor, strlen(conflict_name->ancestor) + 1); |
2876
|
|
|
|
|
|
|
|
2877
|
0
|
0
|
|
|
|
|
if (error != 0) |
2878
|
0
|
|
|
|
|
|
goto on_error; |
2879
|
|
|
|
|
|
|
|
2880
|
0
|
0
|
|
|
|
|
if (conflict_name->ours == NULL) |
2881
|
0
|
|
|
|
|
|
error = git_buf_put(name_buf, "\0", 1); |
2882
|
|
|
|
|
|
|
else |
2883
|
0
|
|
|
|
|
|
error = git_buf_put(name_buf, conflict_name->ours, strlen(conflict_name->ours) + 1); |
2884
|
|
|
|
|
|
|
|
2885
|
0
|
0
|
|
|
|
|
if (error != 0) |
2886
|
0
|
|
|
|
|
|
goto on_error; |
2887
|
|
|
|
|
|
|
|
2888
|
0
|
0
|
|
|
|
|
if (conflict_name->theirs == NULL) |
2889
|
0
|
|
|
|
|
|
error = git_buf_put(name_buf, "\0", 1); |
2890
|
|
|
|
|
|
|
else |
2891
|
0
|
|
|
|
|
|
error = git_buf_put(name_buf, conflict_name->theirs, strlen(conflict_name->theirs) + 1); |
2892
|
|
|
|
|
|
|
|
2893
|
|
|
|
|
|
|
on_error: |
2894
|
0
|
|
|
|
|
|
return error; |
2895
|
|
|
|
|
|
|
} |
2896
|
|
|
|
|
|
|
|
2897
|
0
|
|
|
|
|
|
static int write_name_extension(git_index *index, git_filebuf *file) |
2898
|
|
|
|
|
|
|
{ |
2899
|
0
|
|
|
|
|
|
git_buf name_buf = GIT_BUF_INIT; |
2900
|
0
|
|
|
|
|
|
git_vector *out = &index->names; |
2901
|
|
|
|
|
|
|
git_index_name_entry *conflict_name; |
2902
|
|
|
|
|
|
|
struct index_extension extension; |
2903
|
|
|
|
|
|
|
size_t i; |
2904
|
0
|
|
|
|
|
|
int error = 0; |
2905
|
|
|
|
|
|
|
|
2906
|
0
|
0
|
|
|
|
|
git_vector_foreach(out, i, conflict_name) { |
2907
|
0
|
0
|
|
|
|
|
if ((error = create_name_extension_data(&name_buf, conflict_name)) < 0) |
2908
|
0
|
|
|
|
|
|
goto done; |
2909
|
|
|
|
|
|
|
} |
2910
|
|
|
|
|
|
|
|
2911
|
0
|
|
|
|
|
|
memset(&extension, 0x0, sizeof(struct index_extension)); |
2912
|
0
|
|
|
|
|
|
memcpy(&extension.signature, INDEX_EXT_CONFLICT_NAME_SIG, 4); |
2913
|
0
|
|
|
|
|
|
extension.extension_size = (uint32_t)name_buf.size; |
2914
|
|
|
|
|
|
|
|
2915
|
0
|
|
|
|
|
|
error = write_extension(file, &extension, &name_buf); |
2916
|
|
|
|
|
|
|
|
2917
|
0
|
|
|
|
|
|
git_buf_dispose(&name_buf); |
2918
|
|
|
|
|
|
|
|
2919
|
|
|
|
|
|
|
done: |
2920
|
0
|
|
|
|
|
|
return error; |
2921
|
|
|
|
|
|
|
} |
2922
|
|
|
|
|
|
|
|
2923
|
5
|
|
|
|
|
|
static int create_reuc_extension_data(git_buf *reuc_buf, git_index_reuc_entry *reuc) |
2924
|
|
|
|
|
|
|
{ |
2925
|
|
|
|
|
|
|
int i; |
2926
|
5
|
|
|
|
|
|
int error = 0; |
2927
|
|
|
|
|
|
|
|
2928
|
5
|
50
|
|
|
|
|
if ((error = git_buf_put(reuc_buf, reuc->path, strlen(reuc->path) + 1)) < 0) |
2929
|
0
|
|
|
|
|
|
return error; |
2930
|
|
|
|
|
|
|
|
2931
|
20
|
100
|
|
|
|
|
for (i = 0; i < 3; i++) { |
2932
|
15
|
50
|
|
|
|
|
if ((error = git_buf_printf(reuc_buf, "%o", reuc->mode[i])) < 0 || |
|
|
50
|
|
|
|
|
|
2933
|
|
|
|
|
|
|
(error = git_buf_put(reuc_buf, "\0", 1)) < 0) |
2934
|
0
|
|
|
|
|
|
return error; |
2935
|
|
|
|
|
|
|
} |
2936
|
|
|
|
|
|
|
|
2937
|
20
|
100
|
|
|
|
|
for (i = 0; i < 3; i++) { |
2938
|
15
|
50
|
|
|
|
|
if (reuc->mode[i] && (error = git_buf_put(reuc_buf, (char *)&reuc->oid[i].id, GIT_OID_RAWSZ)) < 0) |
|
|
50
|
|
|
|
|
|
2939
|
0
|
|
|
|
|
|
return error; |
2940
|
|
|
|
|
|
|
} |
2941
|
|
|
|
|
|
|
|
2942
|
5
|
|
|
|
|
|
return 0; |
2943
|
|
|
|
|
|
|
} |
2944
|
|
|
|
|
|
|
|
2945
|
5
|
|
|
|
|
|
static int write_reuc_extension(git_index *index, git_filebuf *file) |
2946
|
|
|
|
|
|
|
{ |
2947
|
5
|
|
|
|
|
|
git_buf reuc_buf = GIT_BUF_INIT; |
2948
|
5
|
|
|
|
|
|
git_vector *out = &index->reuc; |
2949
|
|
|
|
|
|
|
git_index_reuc_entry *reuc; |
2950
|
|
|
|
|
|
|
struct index_extension extension; |
2951
|
|
|
|
|
|
|
size_t i; |
2952
|
5
|
|
|
|
|
|
int error = 0; |
2953
|
|
|
|
|
|
|
|
2954
|
10
|
100
|
|
|
|
|
git_vector_foreach(out, i, reuc) { |
2955
|
5
|
50
|
|
|
|
|
if ((error = create_reuc_extension_data(&reuc_buf, reuc)) < 0) |
2956
|
0
|
|
|
|
|
|
goto done; |
2957
|
|
|
|
|
|
|
} |
2958
|
|
|
|
|
|
|
|
2959
|
5
|
|
|
|
|
|
memset(&extension, 0x0, sizeof(struct index_extension)); |
2960
|
5
|
|
|
|
|
|
memcpy(&extension.signature, INDEX_EXT_UNMERGED_SIG, 4); |
2961
|
5
|
|
|
|
|
|
extension.extension_size = (uint32_t)reuc_buf.size; |
2962
|
|
|
|
|
|
|
|
2963
|
5
|
|
|
|
|
|
error = write_extension(file, &extension, &reuc_buf); |
2964
|
|
|
|
|
|
|
|
2965
|
5
|
|
|
|
|
|
git_buf_dispose(&reuc_buf); |
2966
|
|
|
|
|
|
|
|
2967
|
|
|
|
|
|
|
done: |
2968
|
5
|
|
|
|
|
|
return error; |
2969
|
|
|
|
|
|
|
} |
2970
|
|
|
|
|
|
|
|
2971
|
92
|
|
|
|
|
|
static int write_tree_extension(git_index *index, git_filebuf *file) |
2972
|
|
|
|
|
|
|
{ |
2973
|
|
|
|
|
|
|
struct index_extension extension; |
2974
|
92
|
|
|
|
|
|
git_buf buf = GIT_BUF_INIT; |
2975
|
|
|
|
|
|
|
int error; |
2976
|
|
|
|
|
|
|
|
2977
|
92
|
50
|
|
|
|
|
if (index->tree == NULL) |
2978
|
0
|
|
|
|
|
|
return 0; |
2979
|
|
|
|
|
|
|
|
2980
|
92
|
50
|
|
|
|
|
if ((error = git_tree_cache_write(&buf, index->tree)) < 0) |
2981
|
0
|
|
|
|
|
|
return error; |
2982
|
|
|
|
|
|
|
|
2983
|
92
|
|
|
|
|
|
memset(&extension, 0x0, sizeof(struct index_extension)); |
2984
|
92
|
|
|
|
|
|
memcpy(&extension.signature, INDEX_EXT_TREECACHE_SIG, 4); |
2985
|
92
|
|
|
|
|
|
extension.extension_size = (uint32_t)buf.size; |
2986
|
|
|
|
|
|
|
|
2987
|
92
|
|
|
|
|
|
error = write_extension(file, &extension, &buf); |
2988
|
|
|
|
|
|
|
|
2989
|
92
|
|
|
|
|
|
git_buf_dispose(&buf); |
2990
|
|
|
|
|
|
|
|
2991
|
92
|
|
|
|
|
|
return error; |
2992
|
|
|
|
|
|
|
} |
2993
|
|
|
|
|
|
|
|
2994
|
99
|
|
|
|
|
|
static void clear_uptodate(git_index *index) |
2995
|
|
|
|
|
|
|
{ |
2996
|
|
|
|
|
|
|
git_index_entry *entry; |
2997
|
|
|
|
|
|
|
size_t i; |
2998
|
|
|
|
|
|
|
|
2999
|
311
|
100
|
|
|
|
|
git_vector_foreach(&index->entries, i, entry) |
3000
|
212
|
|
|
|
|
|
entry->flags_extended &= ~GIT_INDEX_ENTRY_UPTODATE; |
3001
|
99
|
|
|
|
|
|
} |
3002
|
|
|
|
|
|
|
|
3003
|
97
|
|
|
|
|
|
static int write_index(git_oid *checksum, git_index *index, git_filebuf *file) |
3004
|
|
|
|
|
|
|
{ |
3005
|
|
|
|
|
|
|
git_oid hash_final; |
3006
|
|
|
|
|
|
|
struct index_header header; |
3007
|
|
|
|
|
|
|
bool is_extended; |
3008
|
|
|
|
|
|
|
uint32_t index_version_number; |
3009
|
|
|
|
|
|
|
|
3010
|
97
|
50
|
|
|
|
|
assert(index && file); |
|
|
50
|
|
|
|
|
|
3011
|
|
|
|
|
|
|
|
3012
|
97
|
50
|
|
|
|
|
if (index->version <= INDEX_VERSION_NUMBER_EXT) { |
3013
|
97
|
|
|
|
|
|
is_extended = is_index_extended(index); |
3014
|
97
|
50
|
|
|
|
|
index_version_number = is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER_LB; |
3015
|
|
|
|
|
|
|
} else { |
3016
|
0
|
|
|
|
|
|
index_version_number = index->version; |
3017
|
|
|
|
|
|
|
} |
3018
|
|
|
|
|
|
|
|
3019
|
97
|
|
|
|
|
|
header.signature = htonl(INDEX_HEADER_SIG); |
3020
|
97
|
|
|
|
|
|
header.version = htonl(index_version_number); |
3021
|
97
|
|
|
|
|
|
header.entry_count = htonl((uint32_t)index->entries.length); |
3022
|
|
|
|
|
|
|
|
3023
|
97
|
50
|
|
|
|
|
if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0) |
3024
|
0
|
|
|
|
|
|
return -1; |
3025
|
|
|
|
|
|
|
|
3026
|
97
|
50
|
|
|
|
|
if (write_entries(index, file) < 0) |
3027
|
0
|
|
|
|
|
|
return -1; |
3028
|
|
|
|
|
|
|
|
3029
|
|
|
|
|
|
|
/* write the tree cache extension */ |
3030
|
97
|
100
|
|
|
|
|
if (index->tree != NULL && write_tree_extension(index, file) < 0) |
|
|
50
|
|
|
|
|
|
3031
|
0
|
|
|
|
|
|
return -1; |
3032
|
|
|
|
|
|
|
|
3033
|
|
|
|
|
|
|
/* write the rename conflict extension */ |
3034
|
97
|
50
|
|
|
|
|
if (index->names.length > 0 && write_name_extension(index, file) < 0) |
|
|
0
|
|
|
|
|
|
3035
|
0
|
|
|
|
|
|
return -1; |
3036
|
|
|
|
|
|
|
|
3037
|
|
|
|
|
|
|
/* write the reuc extension */ |
3038
|
97
|
100
|
|
|
|
|
if (index->reuc.length > 0 && write_reuc_extension(index, file) < 0) |
|
|
50
|
|
|
|
|
|
3039
|
0
|
|
|
|
|
|
return -1; |
3040
|
|
|
|
|
|
|
|
3041
|
|
|
|
|
|
|
/* get out the hash for all the contents we've appended to the file */ |
3042
|
97
|
|
|
|
|
|
git_filebuf_hash(&hash_final, file); |
3043
|
97
|
|
|
|
|
|
git_oid_cpy(checksum, &hash_final); |
3044
|
|
|
|
|
|
|
|
3045
|
|
|
|
|
|
|
/* write it at the end of the file */ |
3046
|
97
|
50
|
|
|
|
|
if (git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ) < 0) |
3047
|
0
|
|
|
|
|
|
return -1; |
3048
|
|
|
|
|
|
|
|
3049
|
|
|
|
|
|
|
/* file entries are no longer up to date */ |
3050
|
97
|
|
|
|
|
|
clear_uptodate(index); |
3051
|
|
|
|
|
|
|
|
3052
|
97
|
|
|
|
|
|
return 0; |
3053
|
|
|
|
|
|
|
} |
3054
|
|
|
|
|
|
|
|
3055
|
2
|
|
|
|
|
|
int git_index_entry_stage(const git_index_entry *entry) |
3056
|
|
|
|
|
|
|
{ |
3057
|
2
|
|
|
|
|
|
return GIT_INDEX_ENTRY_STAGE(entry); |
3058
|
|
|
|
|
|
|
} |
3059
|
|
|
|
|
|
|
|
3060
|
2802
|
|
|
|
|
|
int git_index_entry_is_conflict(const git_index_entry *entry) |
3061
|
|
|
|
|
|
|
{ |
3062
|
2802
|
|
|
|
|
|
return (GIT_INDEX_ENTRY_STAGE(entry) > 0); |
3063
|
|
|
|
|
|
|
} |
3064
|
|
|
|
|
|
|
|
3065
|
|
|
|
|
|
|
typedef struct read_tree_data { |
3066
|
|
|
|
|
|
|
git_index *index; |
3067
|
|
|
|
|
|
|
git_vector *old_entries; |
3068
|
|
|
|
|
|
|
git_vector *new_entries; |
3069
|
|
|
|
|
|
|
git_vector_cmp entry_cmp; |
3070
|
|
|
|
|
|
|
git_tree_cache *tree; |
3071
|
|
|
|
|
|
|
} read_tree_data; |
3072
|
|
|
|
|
|
|
|
3073
|
13
|
|
|
|
|
|
static int read_tree_cb( |
3074
|
|
|
|
|
|
|
const char *root, const git_tree_entry *tentry, void *payload) |
3075
|
|
|
|
|
|
|
{ |
3076
|
13
|
|
|
|
|
|
read_tree_data *data = payload; |
3077
|
13
|
|
|
|
|
|
git_index_entry *entry = NULL, *old_entry; |
3078
|
13
|
|
|
|
|
|
git_buf path = GIT_BUF_INIT; |
3079
|
|
|
|
|
|
|
size_t pos; |
3080
|
|
|
|
|
|
|
|
3081
|
13
|
100
|
|
|
|
|
if (git_tree_entry__is_tree(tentry)) |
3082
|
4
|
|
|
|
|
|
return 0; |
3083
|
|
|
|
|
|
|
|
3084
|
9
|
50
|
|
|
|
|
if (git_buf_joinpath(&path, root, tentry->filename) < 0) |
3085
|
0
|
|
|
|
|
|
return -1; |
3086
|
|
|
|
|
|
|
|
3087
|
9
|
50
|
|
|
|
|
if (index_entry_create(&entry, INDEX_OWNER(data->index), path.ptr, NULL, false) < 0) |
3088
|
0
|
|
|
|
|
|
return -1; |
3089
|
|
|
|
|
|
|
|
3090
|
9
|
|
|
|
|
|
entry->mode = tentry->attr; |
3091
|
9
|
|
|
|
|
|
git_oid_cpy(&entry->id, git_tree_entry_id(tentry)); |
3092
|
|
|
|
|
|
|
|
3093
|
|
|
|
|
|
|
/* look for corresponding old entry and copy data to new entry */ |
3094
|
18
|
|
|
|
|
|
if (data->old_entries != NULL && |
3095
|
9
|
|
|
|
|
|
!index_find_in_entries( |
3096
|
9
|
50
|
|
|
|
|
&pos, data->old_entries, data->entry_cmp, path.ptr, 0, 0) && |
3097
|
9
|
50
|
|
|
|
|
(old_entry = git_vector_get(data->old_entries, pos)) != NULL && |
3098
|
9
|
50
|
|
|
|
|
entry->mode == old_entry->mode && |
3099
|
9
|
|
|
|
|
|
git_oid_equal(&entry->id, &old_entry->id)) |
3100
|
|
|
|
|
|
|
{ |
3101
|
9
|
|
|
|
|
|
index_entry_cpy(entry, old_entry); |
3102
|
9
|
|
|
|
|
|
entry->flags_extended = 0; |
3103
|
|
|
|
|
|
|
} |
3104
|
|
|
|
|
|
|
|
3105
|
9
|
|
|
|
|
|
index_entry_adjust_namemask(entry, path.size); |
3106
|
9
|
|
|
|
|
|
git_buf_dispose(&path); |
3107
|
|
|
|
|
|
|
|
3108
|
9
|
50
|
|
|
|
|
if (git_vector_insert(data->new_entries, entry) < 0) { |
3109
|
0
|
|
|
|
|
|
index_entry_free(entry); |
3110
|
0
|
|
|
|
|
|
return -1; |
3111
|
|
|
|
|
|
|
} |
3112
|
|
|
|
|
|
|
|
3113
|
13
|
|
|
|
|
|
return 0; |
3114
|
|
|
|
|
|
|
} |
3115
|
|
|
|
|
|
|
|
3116
|
5
|
|
|
|
|
|
int git_index_read_tree(git_index *index, const git_tree *tree) |
3117
|
|
|
|
|
|
|
{ |
3118
|
5
|
|
|
|
|
|
int error = 0; |
3119
|
5
|
|
|
|
|
|
git_vector entries = GIT_VECTOR_INIT; |
3120
|
|
|
|
|
|
|
git_idxmap *entries_map; |
3121
|
|
|
|
|
|
|
read_tree_data data; |
3122
|
|
|
|
|
|
|
size_t i; |
3123
|
|
|
|
|
|
|
git_index_entry *e; |
3124
|
|
|
|
|
|
|
|
3125
|
5
|
50
|
|
|
|
|
if (git_idxmap_new(&entries_map) < 0) |
3126
|
0
|
|
|
|
|
|
return -1; |
3127
|
|
|
|
|
|
|
|
3128
|
5
|
|
|
|
|
|
git_vector_set_cmp(&entries, index->entries._cmp); /* match sort */ |
3129
|
|
|
|
|
|
|
|
3130
|
5
|
|
|
|
|
|
data.index = index; |
3131
|
5
|
|
|
|
|
|
data.old_entries = &index->entries; |
3132
|
5
|
|
|
|
|
|
data.new_entries = &entries; |
3133
|
5
|
|
|
|
|
|
data.entry_cmp = index->entries_search; |
3134
|
|
|
|
|
|
|
|
3135
|
5
|
|
|
|
|
|
index->tree = NULL; |
3136
|
5
|
|
|
|
|
|
git_pool_clear(&index->tree_pool); |
3137
|
|
|
|
|
|
|
|
3138
|
5
|
|
|
|
|
|
git_vector_sort(&index->entries); |
3139
|
|
|
|
|
|
|
|
3140
|
5
|
50
|
|
|
|
|
if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0) |
3141
|
0
|
|
|
|
|
|
goto cleanup; |
3142
|
|
|
|
|
|
|
|
3143
|
5
|
50
|
|
|
|
|
if ((error = index_map_resize(entries_map, entries.length, index->ignore_case)) < 0) |
3144
|
0
|
|
|
|
|
|
goto cleanup; |
3145
|
|
|
|
|
|
|
|
3146
|
14
|
100
|
|
|
|
|
git_vector_foreach(&entries, i, e) { |
3147
|
9
|
50
|
|
|
|
|
if ((error = index_map_set(entries_map, e, index->ignore_case)) < 0) { |
3148
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "failed to insert entry into map"); |
3149
|
0
|
|
|
|
|
|
return error; |
3150
|
|
|
|
|
|
|
} |
3151
|
|
|
|
|
|
|
} |
3152
|
|
|
|
|
|
|
|
3153
|
5
|
|
|
|
|
|
error = 0; |
3154
|
|
|
|
|
|
|
|
3155
|
5
|
|
|
|
|
|
git_vector_sort(&entries); |
3156
|
|
|
|
|
|
|
|
3157
|
5
|
50
|
|
|
|
|
if ((error = git_index_clear(index)) < 0) { |
3158
|
|
|
|
|
|
|
/* well, this isn't good */; |
3159
|
|
|
|
|
|
|
} else { |
3160
|
5
|
|
|
|
|
|
git_vector_swap(&entries, &index->entries); |
3161
|
5
|
|
|
|
|
|
entries_map = git__swap(index->entries_map, entries_map); |
3162
|
|
|
|
|
|
|
} |
3163
|
|
|
|
|
|
|
|
3164
|
5
|
|
|
|
|
|
index->dirty = 1; |
3165
|
|
|
|
|
|
|
|
3166
|
|
|
|
|
|
|
cleanup: |
3167
|
5
|
|
|
|
|
|
git_vector_free(&entries); |
3168
|
5
|
|
|
|
|
|
git_idxmap_free(entries_map); |
3169
|
5
|
50
|
|
|
|
|
if (error < 0) |
3170
|
0
|
|
|
|
|
|
return error; |
3171
|
|
|
|
|
|
|
|
3172
|
5
|
|
|
|
|
|
error = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool); |
3173
|
|
|
|
|
|
|
|
3174
|
5
|
|
|
|
|
|
return error; |
3175
|
|
|
|
|
|
|
} |
3176
|
|
|
|
|
|
|
|
3177
|
2
|
|
|
|
|
|
static int git_index_read_iterator( |
3178
|
|
|
|
|
|
|
git_index *index, |
3179
|
|
|
|
|
|
|
git_iterator *new_iterator, |
3180
|
|
|
|
|
|
|
size_t new_length_hint) |
3181
|
|
|
|
|
|
|
{ |
3182
|
2
|
|
|
|
|
|
git_vector new_entries = GIT_VECTOR_INIT, |
3183
|
2
|
|
|
|
|
|
remove_entries = GIT_VECTOR_INIT; |
3184
|
2
|
|
|
|
|
|
git_idxmap *new_entries_map = NULL; |
3185
|
2
|
|
|
|
|
|
git_iterator *index_iterator = NULL; |
3186
|
2
|
|
|
|
|
|
git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT; |
3187
|
|
|
|
|
|
|
const git_index_entry *old_entry, *new_entry; |
3188
|
|
|
|
|
|
|
git_index_entry *entry; |
3189
|
|
|
|
|
|
|
size_t i; |
3190
|
|
|
|
|
|
|
int error; |
3191
|
|
|
|
|
|
|
|
3192
|
2
|
50
|
|
|
|
|
assert((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE)); |
3193
|
|
|
|
|
|
|
|
3194
|
2
|
50
|
|
|
|
|
if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 || |
|
|
50
|
|
|
|
|
|
3195
|
2
|
50
|
|
|
|
|
(error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 || |
3196
|
|
|
|
|
|
|
(error = git_idxmap_new(&new_entries_map)) < 0) |
3197
|
|
|
|
|
|
|
goto done; |
3198
|
|
|
|
|
|
|
|
3199
|
2
|
50
|
|
|
|
|
if (new_length_hint && (error = index_map_resize(new_entries_map, new_length_hint, |
|
|
50
|
|
|
|
|
|
3200
|
2
|
|
|
|
|
|
index->ignore_case)) < 0) |
3201
|
0
|
|
|
|
|
|
goto done; |
3202
|
|
|
|
|
|
|
|
3203
|
2
|
|
|
|
|
|
opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | |
3204
|
|
|
|
|
|
|
GIT_ITERATOR_INCLUDE_CONFLICTS; |
3205
|
|
|
|
|
|
|
|
3206
|
2
|
50
|
|
|
|
|
if ((error = git_iterator_for_index(&index_iterator, |
3207
|
2
|
50
|
|
|
|
|
git_index_owner(index), index, &opts)) < 0 || |
3208
|
0
|
0
|
|
|
|
|
((error = git_iterator_current(&old_entry, index_iterator)) < 0 && |
3209
|
2
|
50
|
|
|
|
|
error != GIT_ITEROVER) || |
3210
|
0
|
0
|
|
|
|
|
((error = git_iterator_current(&new_entry, new_iterator)) < 0 && |
3211
|
|
|
|
|
|
|
error != GIT_ITEROVER)) |
3212
|
|
|
|
|
|
|
goto done; |
3213
|
|
|
|
|
|
|
|
3214
|
|
|
|
|
|
|
while (true) { |
3215
|
|
|
|
|
|
|
git_index_entry |
3216
|
8
|
|
|
|
|
|
*dup_entry = NULL, |
3217
|
8
|
|
|
|
|
|
*add_entry = NULL, |
3218
|
8
|
|
|
|
|
|
*remove_entry = NULL; |
3219
|
|
|
|
|
|
|
int diff; |
3220
|
|
|
|
|
|
|
|
3221
|
8
|
|
|
|
|
|
error = 0; |
3222
|
|
|
|
|
|
|
|
3223
|
8
|
100
|
|
|
|
|
if (old_entry && new_entry) |
|
|
50
|
|
|
|
|
|
3224
|
6
|
|
|
|
|
|
diff = git_index_entry_cmp(old_entry, new_entry); |
3225
|
2
|
50
|
|
|
|
|
else if (!old_entry && new_entry) |
|
|
50
|
|
|
|
|
|
3226
|
0
|
|
|
|
|
|
diff = 1; |
3227
|
2
|
50
|
|
|
|
|
else if (old_entry && !new_entry) |
|
|
0
|
|
|
|
|
|
3228
|
0
|
|
|
|
|
|
diff = -1; |
3229
|
|
|
|
|
|
|
else |
3230
|
|
|
|
|
|
|
break; |
3231
|
|
|
|
|
|
|
|
3232
|
6
|
50
|
|
|
|
|
if (diff < 0) { |
3233
|
0
|
|
|
|
|
|
remove_entry = (git_index_entry *)old_entry; |
3234
|
6
|
50
|
|
|
|
|
} else if (diff > 0) { |
3235
|
0
|
|
|
|
|
|
dup_entry = (git_index_entry *)new_entry; |
3236
|
|
|
|
|
|
|
} else { |
3237
|
|
|
|
|
|
|
/* Path and stage are equal, if the OID is equal, keep it to |
3238
|
|
|
|
|
|
|
* keep the stat cache data. |
3239
|
|
|
|
|
|
|
*/ |
3240
|
6
|
100
|
|
|
|
|
if (git_oid_equal(&old_entry->id, &new_entry->id) && |
|
|
50
|
|
|
|
|
|
3241
|
4
|
|
|
|
|
|
old_entry->mode == new_entry->mode) { |
3242
|
4
|
|
|
|
|
|
add_entry = (git_index_entry *)old_entry; |
3243
|
|
|
|
|
|
|
} else { |
3244
|
2
|
|
|
|
|
|
dup_entry = (git_index_entry *)new_entry; |
3245
|
2
|
|
|
|
|
|
remove_entry = (git_index_entry *)old_entry; |
3246
|
|
|
|
|
|
|
} |
3247
|
|
|
|
|
|
|
} |
3248
|
|
|
|
|
|
|
|
3249
|
6
|
100
|
|
|
|
|
if (dup_entry) { |
3250
|
2
|
50
|
|
|
|
|
if ((error = index_entry_dup_nocache(&add_entry, index, dup_entry)) < 0) |
3251
|
0
|
|
|
|
|
|
goto done; |
3252
|
|
|
|
|
|
|
|
3253
|
2
|
|
|
|
|
|
index_entry_adjust_namemask(add_entry, |
3254
|
2
|
|
|
|
|
|
((struct entry_internal *)add_entry)->pathlen); |
3255
|
|
|
|
|
|
|
} |
3256
|
|
|
|
|
|
|
|
3257
|
|
|
|
|
|
|
/* invalidate this path in the tree cache if this is new (to |
3258
|
|
|
|
|
|
|
* invalidate the parent trees) |
3259
|
|
|
|
|
|
|
*/ |
3260
|
6
|
100
|
|
|
|
|
if (dup_entry && !remove_entry && index->tree) |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
3261
|
0
|
|
|
|
|
|
git_tree_cache_invalidate_path(index->tree, dup_entry->path); |
3262
|
|
|
|
|
|
|
|
3263
|
6
|
50
|
|
|
|
|
if (add_entry) { |
3264
|
6
|
50
|
|
|
|
|
if ((error = git_vector_insert(&new_entries, add_entry)) == 0) |
3265
|
6
|
|
|
|
|
|
error = index_map_set(new_entries_map, add_entry, |
3266
|
6
|
|
|
|
|
|
index->ignore_case); |
3267
|
|
|
|
|
|
|
} |
3268
|
|
|
|
|
|
|
|
3269
|
6
|
100
|
|
|
|
|
if (remove_entry && error >= 0) |
|
|
50
|
|
|
|
|
|
3270
|
2
|
|
|
|
|
|
error = git_vector_insert(&remove_entries, remove_entry); |
3271
|
|
|
|
|
|
|
|
3272
|
6
|
50
|
|
|
|
|
if (error < 0) { |
3273
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "failed to insert entry"); |
3274
|
0
|
|
|
|
|
|
goto done; |
3275
|
|
|
|
|
|
|
} |
3276
|
|
|
|
|
|
|
|
3277
|
6
|
50
|
|
|
|
|
if (diff <= 0) { |
3278
|
6
|
100
|
|
|
|
|
if ((error = git_iterator_advance(&old_entry, index_iterator)) < 0 && |
|
|
50
|
|
|
|
|
|
3279
|
|
|
|
|
|
|
error != GIT_ITEROVER) |
3280
|
0
|
|
|
|
|
|
goto done; |
3281
|
|
|
|
|
|
|
} |
3282
|
|
|
|
|
|
|
|
3283
|
6
|
50
|
|
|
|
|
if (diff >= 0) { |
3284
|
6
|
100
|
|
|
|
|
if ((error = git_iterator_advance(&new_entry, new_iterator)) < 0 && |
|
|
50
|
|
|
|
|
|
3285
|
|
|
|
|
|
|
error != GIT_ITEROVER) |
3286
|
0
|
|
|
|
|
|
goto done; |
3287
|
|
|
|
|
|
|
} |
3288
|
6
|
|
|
|
|
|
} |
3289
|
|
|
|
|
|
|
|
3290
|
2
|
50
|
|
|
|
|
if ((error = git_index_name_clear(index)) < 0 || |
|
|
50
|
|
|
|
|
|
3291
|
|
|
|
|
|
|
(error = git_index_reuc_clear(index)) < 0) |
3292
|
|
|
|
|
|
|
goto done; |
3293
|
|
|
|
|
|
|
|
3294
|
2
|
|
|
|
|
|
git_vector_swap(&new_entries, &index->entries); |
3295
|
2
|
|
|
|
|
|
new_entries_map = git__swap(index->entries_map, new_entries_map); |
3296
|
|
|
|
|
|
|
|
3297
|
4
|
100
|
|
|
|
|
git_vector_foreach(&remove_entries, i, entry) { |
3298
|
2
|
50
|
|
|
|
|
if (index->tree) |
3299
|
2
|
|
|
|
|
|
git_tree_cache_invalidate_path(index->tree, entry->path); |
3300
|
|
|
|
|
|
|
|
3301
|
2
|
|
|
|
|
|
index_entry_free(entry); |
3302
|
|
|
|
|
|
|
} |
3303
|
|
|
|
|
|
|
|
3304
|
2
|
|
|
|
|
|
clear_uptodate(index); |
3305
|
|
|
|
|
|
|
|
3306
|
2
|
|
|
|
|
|
index->dirty = 1; |
3307
|
2
|
|
|
|
|
|
error = 0; |
3308
|
|
|
|
|
|
|
|
3309
|
|
|
|
|
|
|
done: |
3310
|
2
|
|
|
|
|
|
git_idxmap_free(new_entries_map); |
3311
|
2
|
|
|
|
|
|
git_vector_free(&new_entries); |
3312
|
2
|
|
|
|
|
|
git_vector_free(&remove_entries); |
3313
|
2
|
|
|
|
|
|
git_iterator_free(index_iterator); |
3314
|
2
|
|
|
|
|
|
return error; |
3315
|
|
|
|
|
|
|
} |
3316
|
|
|
|
|
|
|
|
3317
|
2
|
|
|
|
|
|
int git_index_read_index( |
3318
|
|
|
|
|
|
|
git_index *index, |
3319
|
|
|
|
|
|
|
const git_index *new_index) |
3320
|
|
|
|
|
|
|
{ |
3321
|
2
|
|
|
|
|
|
git_iterator *new_iterator = NULL; |
3322
|
2
|
|
|
|
|
|
git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT; |
3323
|
|
|
|
|
|
|
int error; |
3324
|
|
|
|
|
|
|
|
3325
|
2
|
|
|
|
|
|
opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | |
3326
|
|
|
|
|
|
|
GIT_ITERATOR_INCLUDE_CONFLICTS; |
3327
|
|
|
|
|
|
|
|
3328
|
2
|
50
|
|
|
|
|
if ((error = git_iterator_for_index(&new_iterator, |
3329
|
2
|
|
|
|
|
|
git_index_owner(new_index), (git_index *)new_index, &opts)) < 0 || |
3330
|
2
|
|
|
|
|
|
(error = git_index_read_iterator(index, new_iterator, |
3331
|
|
|
|
|
|
|
new_index->entries.length)) < 0) |
3332
|
|
|
|
|
|
|
goto done; |
3333
|
|
|
|
|
|
|
|
3334
|
|
|
|
|
|
|
done: |
3335
|
2
|
|
|
|
|
|
git_iterator_free(new_iterator); |
3336
|
2
|
|
|
|
|
|
return error; |
3337
|
|
|
|
|
|
|
} |
3338
|
|
|
|
|
|
|
|
3339
|
54
|
|
|
|
|
|
git_repository *git_index_owner(const git_index *index) |
3340
|
|
|
|
|
|
|
{ |
3341
|
54
|
|
|
|
|
|
return INDEX_OWNER(index); |
3342
|
|
|
|
|
|
|
} |
3343
|
|
|
|
|
|
|
|
3344
|
|
|
|
|
|
|
enum { |
3345
|
|
|
|
|
|
|
INDEX_ACTION_NONE = 0, |
3346
|
|
|
|
|
|
|
INDEX_ACTION_UPDATE = 1, |
3347
|
|
|
|
|
|
|
INDEX_ACTION_REMOVE = 2, |
3348
|
|
|
|
|
|
|
INDEX_ACTION_ADDALL = 3, |
3349
|
|
|
|
|
|
|
}; |
3350
|
|
|
|
|
|
|
|
3351
|
4
|
|
|
|
|
|
int git_index_add_all( |
3352
|
|
|
|
|
|
|
git_index *index, |
3353
|
|
|
|
|
|
|
const git_strarray *paths, |
3354
|
|
|
|
|
|
|
unsigned int flags, |
3355
|
|
|
|
|
|
|
git_index_matched_path_cb cb, |
3356
|
|
|
|
|
|
|
void *payload) |
3357
|
|
|
|
|
|
|
{ |
3358
|
|
|
|
|
|
|
int error; |
3359
|
|
|
|
|
|
|
git_repository *repo; |
3360
|
4
|
|
|
|
|
|
git_iterator *wditer = NULL; |
3361
|
|
|
|
|
|
|
git_pathspec ps; |
3362
|
4
|
|
|
|
|
|
bool no_fnmatch = (flags & GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH) != 0; |
3363
|
|
|
|
|
|
|
|
3364
|
4
|
50
|
|
|
|
|
assert(index); |
3365
|
|
|
|
|
|
|
|
3366
|
4
|
|
|
|
|
|
repo = INDEX_OWNER(index); |
3367
|
4
|
50
|
|
|
|
|
if ((error = git_repository__ensure_not_bare(repo, "index add all")) < 0) |
3368
|
0
|
|
|
|
|
|
return error; |
3369
|
|
|
|
|
|
|
|
3370
|
4
|
50
|
|
|
|
|
if ((error = git_pathspec__init(&ps, paths)) < 0) |
3371
|
0
|
|
|
|
|
|
return error; |
3372
|
|
|
|
|
|
|
|
3373
|
|
|
|
|
|
|
/* optionally check that pathspec doesn't mention any ignored files */ |
3374
|
4
|
50
|
|
|
|
|
if ((flags & GIT_INDEX_ADD_CHECK_PATHSPEC) != 0 && |
|
|
0
|
|
|
|
|
|
3375
|
0
|
0
|
|
|
|
|
(flags & GIT_INDEX_ADD_FORCE) == 0 && |
3376
|
0
|
|
|
|
|
|
(error = git_ignore__check_pathspec_for_exact_ignores( |
3377
|
|
|
|
|
|
|
repo, &ps.pathspec, no_fnmatch)) < 0) |
3378
|
0
|
|
|
|
|
|
goto cleanup; |
3379
|
|
|
|
|
|
|
|
3380
|
4
|
|
|
|
|
|
error = index_apply_to_wd_diff(index, INDEX_ACTION_ADDALL, paths, flags, cb, payload); |
3381
|
|
|
|
|
|
|
|
3382
|
4
|
50
|
|
|
|
|
if (error) |
3383
|
0
|
|
|
|
|
|
git_error_set_after_callback(error); |
3384
|
|
|
|
|
|
|
|
3385
|
|
|
|
|
|
|
cleanup: |
3386
|
4
|
|
|
|
|
|
git_iterator_free(wditer); |
3387
|
4
|
|
|
|
|
|
git_pathspec__clear(&ps); |
3388
|
|
|
|
|
|
|
|
3389
|
4
|
|
|
|
|
|
return error; |
3390
|
|
|
|
|
|
|
} |
3391
|
|
|
|
|
|
|
|
3392
|
|
|
|
|
|
|
struct foreach_diff_data { |
3393
|
|
|
|
|
|
|
git_index *index; |
3394
|
|
|
|
|
|
|
const git_pathspec *pathspec; |
3395
|
|
|
|
|
|
|
unsigned int flags; |
3396
|
|
|
|
|
|
|
git_index_matched_path_cb cb; |
3397
|
|
|
|
|
|
|
void *payload; |
3398
|
|
|
|
|
|
|
}; |
3399
|
|
|
|
|
|
|
|
3400
|
12
|
|
|
|
|
|
static int apply_each_file(const git_diff_delta *delta, float progress, void *payload) |
3401
|
|
|
|
|
|
|
{ |
3402
|
12
|
|
|
|
|
|
struct foreach_diff_data *data = payload; |
3403
|
|
|
|
|
|
|
const char *match, *path; |
3404
|
12
|
|
|
|
|
|
int error = 0; |
3405
|
|
|
|
|
|
|
|
3406
|
|
|
|
|
|
|
GIT_UNUSED(progress); |
3407
|
|
|
|
|
|
|
|
3408
|
12
|
|
|
|
|
|
path = delta->old_file.path; |
3409
|
|
|
|
|
|
|
|
3410
|
|
|
|
|
|
|
/* We only want those which match the pathspecs */ |
3411
|
12
|
100
|
|
|
|
|
if (!git_pathspec__match( |
3412
|
24
|
|
|
|
|
|
&data->pathspec->pathspec, path, false, (bool)data->index->ignore_case, |
3413
|
|
|
|
|
|
|
&match, NULL)) |
3414
|
6
|
|
|
|
|
|
return 0; |
3415
|
|
|
|
|
|
|
|
3416
|
6
|
50
|
|
|
|
|
if (data->cb) |
3417
|
6
|
|
|
|
|
|
error = data->cb(path, match, data->payload); |
3418
|
|
|
|
|
|
|
|
3419
|
6
|
100
|
|
|
|
|
if (error > 0) /* skip this entry */ |
3420
|
1
|
|
|
|
|
|
return 0; |
3421
|
5
|
100
|
|
|
|
|
if (error < 0) /* actual error */ |
3422
|
1
|
|
|
|
|
|
return error; |
3423
|
|
|
|
|
|
|
|
3424
|
|
|
|
|
|
|
/* If the workdir item does not exist, remove it from the index. */ |
3425
|
4
|
50
|
|
|
|
|
if ((delta->new_file.flags & GIT_DIFF_FLAG_EXISTS) == 0) |
3426
|
0
|
|
|
|
|
|
error = git_index_remove_bypath(data->index, path); |
3427
|
|
|
|
|
|
|
else |
3428
|
4
|
|
|
|
|
|
error = git_index_add_bypath(data->index, delta->new_file.path); |
3429
|
|
|
|
|
|
|
|
3430
|
12
|
|
|
|
|
|
return error; |
3431
|
|
|
|
|
|
|
} |
3432
|
|
|
|
|
|
|
|
3433
|
8
|
|
|
|
|
|
static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths, |
3434
|
|
|
|
|
|
|
unsigned int flags, |
3435
|
|
|
|
|
|
|
git_index_matched_path_cb cb, void *payload) |
3436
|
|
|
|
|
|
|
{ |
3437
|
|
|
|
|
|
|
int error; |
3438
|
|
|
|
|
|
|
git_diff *diff; |
3439
|
|
|
|
|
|
|
git_pathspec ps; |
3440
|
|
|
|
|
|
|
git_repository *repo; |
3441
|
8
|
|
|
|
|
|
git_diff_options opts = GIT_DIFF_OPTIONS_INIT; |
3442
|
8
|
|
|
|
|
|
struct foreach_diff_data data = { |
3443
|
|
|
|
|
|
|
index, |
3444
|
|
|
|
|
|
|
NULL, |
3445
|
|
|
|
|
|
|
flags, |
3446
|
|
|
|
|
|
|
cb, |
3447
|
|
|
|
|
|
|
payload, |
3448
|
|
|
|
|
|
|
}; |
3449
|
|
|
|
|
|
|
|
3450
|
8
|
50
|
|
|
|
|
assert(index); |
3451
|
8
|
100
|
|
|
|
|
assert(action == INDEX_ACTION_UPDATE || action == INDEX_ACTION_ADDALL); |
|
|
50
|
|
|
|
|
|
3452
|
|
|
|
|
|
|
|
3453
|
8
|
|
|
|
|
|
repo = INDEX_OWNER(index); |
3454
|
|
|
|
|
|
|
|
3455
|
8
|
50
|
|
|
|
|
if (!repo) { |
3456
|
0
|
|
|
|
|
|
return create_index_error(-1, |
3457
|
|
|
|
|
|
|
"cannot run update; the index is not backed up by a repository."); |
3458
|
|
|
|
|
|
|
} |
3459
|
|
|
|
|
|
|
|
3460
|
|
|
|
|
|
|
/* |
3461
|
|
|
|
|
|
|
* We do the matching ourselves intead of passing the list to |
3462
|
|
|
|
|
|
|
* diff because we want to tell the callback which one |
3463
|
|
|
|
|
|
|
* matched, which we do not know if we ask diff to filter for us. |
3464
|
|
|
|
|
|
|
*/ |
3465
|
8
|
50
|
|
|
|
|
if ((error = git_pathspec__init(&ps, paths)) < 0) |
3466
|
0
|
|
|
|
|
|
return error; |
3467
|
|
|
|
|
|
|
|
3468
|
8
|
|
|
|
|
|
opts.flags = GIT_DIFF_INCLUDE_TYPECHANGE; |
3469
|
8
|
100
|
|
|
|
|
if (action == INDEX_ACTION_ADDALL) { |
3470
|
4
|
|
|
|
|
|
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | |
3471
|
|
|
|
|
|
|
GIT_DIFF_RECURSE_UNTRACKED_DIRS; |
3472
|
|
|
|
|
|
|
|
3473
|
4
|
100
|
|
|
|
|
if (flags == GIT_INDEX_ADD_FORCE) |
3474
|
1
|
|
|
|
|
|
opts.flags |= GIT_DIFF_INCLUDE_IGNORED; |
3475
|
|
|
|
|
|
|
} |
3476
|
|
|
|
|
|
|
|
3477
|
8
|
50
|
|
|
|
|
if ((error = git_diff_index_to_workdir(&diff, repo, index, &opts)) < 0) |
3478
|
0
|
|
|
|
|
|
goto cleanup; |
3479
|
|
|
|
|
|
|
|
3480
|
8
|
|
|
|
|
|
data.pathspec = &ps; |
3481
|
8
|
|
|
|
|
|
error = git_diff_foreach(diff, apply_each_file, NULL, NULL, NULL, &data); |
3482
|
8
|
|
|
|
|
|
git_diff_free(diff); |
3483
|
|
|
|
|
|
|
|
3484
|
8
|
100
|
|
|
|
|
if (error) /* make sure error is set if callback stopped iteration */ |
3485
|
1
|
|
|
|
|
|
git_error_set_after_callback(error); |
3486
|
|
|
|
|
|
|
|
3487
|
|
|
|
|
|
|
cleanup: |
3488
|
8
|
|
|
|
|
|
git_pathspec__clear(&ps); |
3489
|
8
|
|
|
|
|
|
return error; |
3490
|
|
|
|
|
|
|
} |
3491
|
|
|
|
|
|
|
|
3492
|
1
|
|
|
|
|
|
static int index_apply_to_all( |
3493
|
|
|
|
|
|
|
git_index *index, |
3494
|
|
|
|
|
|
|
int action, |
3495
|
|
|
|
|
|
|
const git_strarray *paths, |
3496
|
|
|
|
|
|
|
git_index_matched_path_cb cb, |
3497
|
|
|
|
|
|
|
void *payload) |
3498
|
|
|
|
|
|
|
{ |
3499
|
1
|
|
|
|
|
|
int error = 0; |
3500
|
|
|
|
|
|
|
size_t i; |
3501
|
|
|
|
|
|
|
git_pathspec ps; |
3502
|
|
|
|
|
|
|
const char *match; |
3503
|
1
|
|
|
|
|
|
git_buf path = GIT_BUF_INIT; |
3504
|
|
|
|
|
|
|
|
3505
|
1
|
50
|
|
|
|
|
assert(index); |
3506
|
|
|
|
|
|
|
|
3507
|
1
|
50
|
|
|
|
|
if ((error = git_pathspec__init(&ps, paths)) < 0) |
3508
|
0
|
|
|
|
|
|
return error; |
3509
|
|
|
|
|
|
|
|
3510
|
1
|
|
|
|
|
|
git_vector_sort(&index->entries); |
3511
|
|
|
|
|
|
|
|
3512
|
2
|
50
|
|
|
|
|
for (i = 0; !error && i < index->entries.length; ++i) { |
|
|
100
|
|
|
|
|
|
3513
|
1
|
|
|
|
|
|
git_index_entry *entry = git_vector_get(&index->entries, i); |
3514
|
|
|
|
|
|
|
|
3515
|
|
|
|
|
|
|
/* check if path actually matches */ |
3516
|
1
|
50
|
|
|
|
|
if (!git_pathspec__match( |
3517
|
1
|
|
|
|
|
|
&ps.pathspec, entry->path, false, (bool)index->ignore_case, |
3518
|
|
|
|
|
|
|
&match, NULL)) |
3519
|
0
|
|
|
|
|
|
continue; |
3520
|
|
|
|
|
|
|
|
3521
|
|
|
|
|
|
|
/* issue notification callback if requested */ |
3522
|
1
|
50
|
|
|
|
|
if (cb && (error = cb(entry->path, match, payload)) != 0) { |
|
|
50
|
|
|
|
|
|
3523
|
0
|
0
|
|
|
|
|
if (error > 0) { /* return > 0 means skip this one */ |
3524
|
0
|
|
|
|
|
|
error = 0; |
3525
|
0
|
|
|
|
|
|
continue; |
3526
|
|
|
|
|
|
|
} |
3527
|
0
|
0
|
|
|
|
|
if (error < 0) /* return < 0 means abort */ |
3528
|
0
|
|
|
|
|
|
break; |
3529
|
|
|
|
|
|
|
} |
3530
|
|
|
|
|
|
|
|
3531
|
|
|
|
|
|
|
/* index manipulation may alter entry, so don't depend on it */ |
3532
|
1
|
50
|
|
|
|
|
if ((error = git_buf_sets(&path, entry->path)) < 0) |
3533
|
0
|
|
|
|
|
|
break; |
3534
|
|
|
|
|
|
|
|
3535
|
1
|
|
|
|
|
|
switch (action) { |
3536
|
|
|
|
|
|
|
case INDEX_ACTION_NONE: |
3537
|
0
|
|
|
|
|
|
break; |
3538
|
|
|
|
|
|
|
case INDEX_ACTION_UPDATE: |
3539
|
0
|
|
|
|
|
|
error = git_index_add_bypath(index, path.ptr); |
3540
|
|
|
|
|
|
|
|
3541
|
0
|
0
|
|
|
|
|
if (error == GIT_ENOTFOUND) { |
3542
|
0
|
|
|
|
|
|
git_error_clear(); |
3543
|
|
|
|
|
|
|
|
3544
|
0
|
|
|
|
|
|
error = git_index_remove_bypath(index, path.ptr); |
3545
|
|
|
|
|
|
|
|
3546
|
0
|
0
|
|
|
|
|
if (!error) /* back up foreach if we removed this */ |
3547
|
0
|
|
|
|
|
|
i--; |
3548
|
|
|
|
|
|
|
} |
3549
|
0
|
|
|
|
|
|
break; |
3550
|
|
|
|
|
|
|
case INDEX_ACTION_REMOVE: |
3551
|
1
|
50
|
|
|
|
|
if (!(error = git_index_remove_bypath(index, path.ptr))) |
3552
|
1
|
|
|
|
|
|
i--; /* back up foreach if we removed this */ |
3553
|
1
|
|
|
|
|
|
break; |
3554
|
|
|
|
|
|
|
default: |
3555
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "unknown index action %d", action); |
3556
|
0
|
|
|
|
|
|
error = -1; |
3557
|
0
|
|
|
|
|
|
break; |
3558
|
|
|
|
|
|
|
} |
3559
|
|
|
|
|
|
|
} |
3560
|
|
|
|
|
|
|
|
3561
|
1
|
|
|
|
|
|
git_buf_dispose(&path); |
3562
|
1
|
|
|
|
|
|
git_pathspec__clear(&ps); |
3563
|
|
|
|
|
|
|
|
3564
|
1
|
|
|
|
|
|
return error; |
3565
|
|
|
|
|
|
|
} |
3566
|
|
|
|
|
|
|
|
3567
|
1
|
|
|
|
|
|
int git_index_remove_all( |
3568
|
|
|
|
|
|
|
git_index *index, |
3569
|
|
|
|
|
|
|
const git_strarray *pathspec, |
3570
|
|
|
|
|
|
|
git_index_matched_path_cb cb, |
3571
|
|
|
|
|
|
|
void *payload) |
3572
|
|
|
|
|
|
|
{ |
3573
|
1
|
|
|
|
|
|
int error = index_apply_to_all( |
3574
|
|
|
|
|
|
|
index, INDEX_ACTION_REMOVE, pathspec, cb, payload); |
3575
|
|
|
|
|
|
|
|
3576
|
1
|
50
|
|
|
|
|
if (error) /* make sure error is set if callback stopped iteration */ |
3577
|
0
|
|
|
|
|
|
git_error_set_after_callback(error); |
3578
|
|
|
|
|
|
|
|
3579
|
1
|
|
|
|
|
|
return error; |
3580
|
|
|
|
|
|
|
} |
3581
|
|
|
|
|
|
|
|
3582
|
4
|
|
|
|
|
|
int git_index_update_all( |
3583
|
|
|
|
|
|
|
git_index *index, |
3584
|
|
|
|
|
|
|
const git_strarray *pathspec, |
3585
|
|
|
|
|
|
|
git_index_matched_path_cb cb, |
3586
|
|
|
|
|
|
|
void *payload) |
3587
|
|
|
|
|
|
|
{ |
3588
|
4
|
|
|
|
|
|
int error = index_apply_to_wd_diff(index, INDEX_ACTION_UPDATE, pathspec, 0, cb, payload); |
3589
|
4
|
100
|
|
|
|
|
if (error) /* make sure error is set if callback stopped iteration */ |
3590
|
1
|
|
|
|
|
|
git_error_set_after_callback(error); |
3591
|
|
|
|
|
|
|
|
3592
|
4
|
|
|
|
|
|
return error; |
3593
|
|
|
|
|
|
|
} |
3594
|
|
|
|
|
|
|
|
3595
|
431
|
|
|
|
|
|
int git_index_snapshot_new(git_vector *snap, git_index *index) |
3596
|
|
|
|
|
|
|
{ |
3597
|
|
|
|
|
|
|
int error; |
3598
|
|
|
|
|
|
|
|
3599
|
431
|
|
|
|
|
|
GIT_REFCOUNT_INC(index); |
3600
|
|
|
|
|
|
|
|
3601
|
431
|
|
|
|
|
|
git_atomic_inc(&index->readers); |
3602
|
431
|
|
|
|
|
|
git_vector_sort(&index->entries); |
3603
|
|
|
|
|
|
|
|
3604
|
431
|
|
|
|
|
|
error = git_vector_dup(snap, &index->entries, index->entries._cmp); |
3605
|
|
|
|
|
|
|
|
3606
|
431
|
50
|
|
|
|
|
if (error < 0) |
3607
|
0
|
|
|
|
|
|
git_index_snapshot_release(snap, index); |
3608
|
|
|
|
|
|
|
|
3609
|
431
|
|
|
|
|
|
return error; |
3610
|
|
|
|
|
|
|
} |
3611
|
|
|
|
|
|
|
|
3612
|
431
|
|
|
|
|
|
void git_index_snapshot_release(git_vector *snap, git_index *index) |
3613
|
|
|
|
|
|
|
{ |
3614
|
431
|
|
|
|
|
|
git_vector_free(snap); |
3615
|
|
|
|
|
|
|
|
3616
|
431
|
|
|
|
|
|
git_atomic_dec(&index->readers); |
3617
|
|
|
|
|
|
|
|
3618
|
431
|
|
|
|
|
|
git_index_free(index); |
3619
|
431
|
|
|
|
|
|
} |
3620
|
|
|
|
|
|
|
|
3621
|
206
|
|
|
|
|
|
int git_index_snapshot_find( |
3622
|
|
|
|
|
|
|
size_t *out, git_vector *entries, git_vector_cmp entry_srch, |
3623
|
|
|
|
|
|
|
const char *path, size_t path_len, int stage) |
3624
|
|
|
|
|
|
|
{ |
3625
|
206
|
|
|
|
|
|
return index_find_in_entries(out, entries, entry_srch, path, path_len, stage); |
3626
|
|
|
|
|
|
|
} |
3627
|
|
|
|
|
|
|
|
3628
|
98
|
|
|
|
|
|
int git_indexwriter_init( |
3629
|
|
|
|
|
|
|
git_indexwriter *writer, |
3630
|
|
|
|
|
|
|
git_index *index) |
3631
|
|
|
|
|
|
|
{ |
3632
|
|
|
|
|
|
|
int error; |
3633
|
|
|
|
|
|
|
|
3634
|
98
|
|
|
|
|
|
GIT_REFCOUNT_INC(index); |
3635
|
|
|
|
|
|
|
|
3636
|
98
|
|
|
|
|
|
writer->index = index; |
3637
|
|
|
|
|
|
|
|
3638
|
98
|
50
|
|
|
|
|
if (!index->index_file_path) |
3639
|
0
|
|
|
|
|
|
return create_index_error(-1, |
3640
|
|
|
|
|
|
|
"failed to write index: The index is in-memory only"); |
3641
|
|
|
|
|
|
|
|
3642
|
98
|
50
|
|
|
|
|
if ((error = git_filebuf_open( |
3643
|
98
|
|
|
|
|
|
&writer->file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) { |
3644
|
|
|
|
|
|
|
|
3645
|
0
|
0
|
|
|
|
|
if (error == GIT_ELOCKED) |
3646
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INDEX, "the index is locked; this might be due to a concurrent or crashed process"); |
3647
|
|
|
|
|
|
|
|
3648
|
0
|
|
|
|
|
|
return error; |
3649
|
|
|
|
|
|
|
} |
3650
|
|
|
|
|
|
|
|
3651
|
98
|
|
|
|
|
|
writer->should_write = 1; |
3652
|
|
|
|
|
|
|
|
3653
|
98
|
|
|
|
|
|
return 0; |
3654
|
|
|
|
|
|
|
} |
3655
|
|
|
|
|
|
|
|
3656
|
15
|
|
|
|
|
|
int git_indexwriter_init_for_operation( |
3657
|
|
|
|
|
|
|
git_indexwriter *writer, |
3658
|
|
|
|
|
|
|
git_repository *repo, |
3659
|
|
|
|
|
|
|
unsigned int *checkout_strategy) |
3660
|
|
|
|
|
|
|
{ |
3661
|
|
|
|
|
|
|
git_index *index; |
3662
|
|
|
|
|
|
|
int error; |
3663
|
|
|
|
|
|
|
|
3664
|
15
|
50
|
|
|
|
|
if ((error = git_repository_index__weakptr(&index, repo)) < 0 || |
|
|
50
|
|
|
|
|
|
3665
|
15
|
|
|
|
|
|
(error = git_indexwriter_init(writer, index)) < 0) |
3666
|
0
|
|
|
|
|
|
return error; |
3667
|
|
|
|
|
|
|
|
3668
|
15
|
|
|
|
|
|
writer->should_write = (*checkout_strategy & GIT_CHECKOUT_DONT_WRITE_INDEX) == 0; |
3669
|
15
|
|
|
|
|
|
*checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX; |
3670
|
|
|
|
|
|
|
|
3671
|
15
|
|
|
|
|
|
return 0; |
3672
|
|
|
|
|
|
|
} |
3673
|
|
|
|
|
|
|
|
3674
|
97
|
|
|
|
|
|
int git_indexwriter_commit(git_indexwriter *writer) |
3675
|
|
|
|
|
|
|
{ |
3676
|
|
|
|
|
|
|
int error; |
3677
|
97
|
|
|
|
|
|
git_oid checksum = {{ 0 }}; |
3678
|
|
|
|
|
|
|
|
3679
|
97
|
50
|
|
|
|
|
if (!writer->should_write) |
3680
|
0
|
|
|
|
|
|
return 0; |
3681
|
|
|
|
|
|
|
|
3682
|
97
|
|
|
|
|
|
git_vector_sort(&writer->index->entries); |
3683
|
97
|
|
|
|
|
|
git_vector_sort(&writer->index->reuc); |
3684
|
|
|
|
|
|
|
|
3685
|
97
|
50
|
|
|
|
|
if ((error = write_index(&checksum, writer->index, &writer->file)) < 0) { |
3686
|
0
|
|
|
|
|
|
git_indexwriter_cleanup(writer); |
3687
|
0
|
|
|
|
|
|
return error; |
3688
|
|
|
|
|
|
|
} |
3689
|
|
|
|
|
|
|
|
3690
|
97
|
50
|
|
|
|
|
if ((error = git_filebuf_commit(&writer->file)) < 0) |
3691
|
0
|
|
|
|
|
|
return error; |
3692
|
|
|
|
|
|
|
|
3693
|
97
|
50
|
|
|
|
|
if ((error = git_futils_filestamp_check( |
3694
|
97
|
|
|
|
|
|
&writer->index->stamp, writer->index->index_file_path)) < 0) { |
3695
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "could not read index timestamp"); |
3696
|
0
|
|
|
|
|
|
return -1; |
3697
|
|
|
|
|
|
|
} |
3698
|
|
|
|
|
|
|
|
3699
|
97
|
|
|
|
|
|
writer->index->dirty = 0; |
3700
|
97
|
|
|
|
|
|
writer->index->on_disk = 1; |
3701
|
97
|
|
|
|
|
|
git_oid_cpy(&writer->index->checksum, &checksum); |
3702
|
|
|
|
|
|
|
|
3703
|
97
|
|
|
|
|
|
git_index_free(writer->index); |
3704
|
97
|
|
|
|
|
|
writer->index = NULL; |
3705
|
|
|
|
|
|
|
|
3706
|
97
|
|
|
|
|
|
return 0; |
3707
|
|
|
|
|
|
|
} |
3708
|
|
|
|
|
|
|
|
3709
|
98
|
|
|
|
|
|
void git_indexwriter_cleanup(git_indexwriter *writer) |
3710
|
|
|
|
|
|
|
{ |
3711
|
98
|
|
|
|
|
|
git_filebuf_cleanup(&writer->file); |
3712
|
|
|
|
|
|
|
|
3713
|
98
|
|
|
|
|
|
git_index_free(writer->index); |
3714
|
98
|
|
|
|
|
|
writer->index = NULL; |
3715
|
98
|
|
|
|
|
|
} |
3716
|
|
|
|
|
|
|
|
3717
|
|
|
|
|
|
|
/* Deprecated functions */ |
3718
|
|
|
|
|
|
|
|
3719
|
9
|
|
|
|
|
|
int git_index_add_frombuffer( |
3720
|
|
|
|
|
|
|
git_index *index, const git_index_entry *source_entry, |
3721
|
|
|
|
|
|
|
const void *buffer, size_t len) |
3722
|
|
|
|
|
|
|
{ |
3723
|
9
|
|
|
|
|
|
return git_index_add_from_buffer(index, source_entry, buffer, len); |
3724
|
|
|
|
|
|
|
} |