]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 | 6 | #include "cache.h" |
36bf1958 | 7 | #include "alloc.h" |
b2141fc1 | 8 | #include "config.h" |
cffbfad5 EN |
9 | #include "diff.h" |
10 | #include "diffcore.h" | |
41771fa4 | 11 | #include "hex.h" |
f6ecc62d | 12 | #include "tempfile.h" |
697cc8ef | 13 | #include "lockfile.h" |
bad68ec9 | 14 | #include "cache-tree.h" |
f35a6d3b | 15 | #include "refs.h" |
d616813d | 16 | #include "dir.h" |
cbd53a21 | 17 | #include "object-store.h" |
041aee31 JH |
18 | #include "tree.h" |
19 | #include "commit.h" | |
39425819 | 20 | #include "blob.h" |
f394e093 | 21 | #include "gettext.h" |
cfc5789a | 22 | #include "resolve-undo.h" |
1956ecd0 | 23 | #include "run-command.h" |
6c9cd161 JH |
24 | #include "strbuf.h" |
25 | #include "varint.h" | |
5fc2fc8f | 26 | #include "split-index.h" |
a42643aa | 27 | #include "utf8.h" |
883e248b | 28 | #include "fsmonitor.h" |
abb4bb83 | 29 | #include "thread-utils.h" |
ae9af122 | 30 | #include "progress.h" |
6e773527 | 31 | #include "sparse-index.h" |
410334ed | 32 | #include "csum-file.h" |
b2896d27 | 33 | #include "promisor-remote.h" |
dbb1c613 | 34 | #include "hook.h" |
d5ebb50d | 35 | #include "wrapper.h" |
bad68ec9 | 36 | |
b60e188c TG |
37 | /* Mask for the name length in ce_flags in the on-disk index */ |
38 | ||
39 | #define CE_NAMEMASK (0x0fff) | |
40 | ||
bad68ec9 JH |
41 | /* Index extensions. |
42 | * | |
43 | * The first letter should be 'A'..'Z' for extensions that are not | |
44 | * necessary for a correct operation (i.e. optimization data). | |
45 | * When new extensions are added that _needs_ to be understood in | |
46 | * order to correctly interpret the index file, pick character that | |
47 | * is outside the range, to cause the reader to abort. | |
48 | */ | |
49 | ||
50 | #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) | |
51 | #define CACHE_EXT_TREE 0x54524545 /* "TREE" */ | |
b659b49b | 52 | #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */ |
5fc2fc8f | 53 | #define CACHE_EXT_LINK 0x6c696e6b /* "link" */ |
83c094ad | 54 | #define CACHE_EXT_UNTRACKED 0x554E5452 /* "UNTR" */ |
883e248b | 55 | #define CACHE_EXT_FSMONITOR 0x46534D4E /* "FSMN" */ |
3b1d9e04 | 56 | #define CACHE_EXT_ENDOFINDEXENTRIES 0x454F4945 /* "EOIE" */ |
3255089a | 57 | #define CACHE_EXT_INDEXENTRYOFFSETTABLE 0x49454F54 /* "IEOT" */ |
cd42415f | 58 | #define CACHE_EXT_SPARSE_DIRECTORIES 0x73646972 /* "sdir" */ |
5fc2fc8f NTND |
59 | |
60 | /* changes that can be kept in $GIT_DIR/index (basically all extensions) */ | |
e0cf0d7d | 61 | #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \ |
c18b80a0 | 62 | CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \ |
883e248b | 63 | SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED | FSMONITOR_CHANGED) |
e83c5163 | 64 | |
8e72d675 JM |
65 | |
66 | /* | |
67 | * This is an estimate of the pathname length in the index. We use | |
68 | * this for V4 index files to guess the un-deltafied size of the index | |
69 | * in memory because of pathname deltafication. This is not required | |
70 | * for V2/V3 index formats because their pathnames are not compressed. | |
71 | * If the initial amount of memory set aside is not sufficient, the | |
72 | * mem pool will allocate extra memory. | |
73 | */ | |
74 | #define CACHE_ENTRY_PATH_LENGTH 80 | |
75 | ||
20ec2d03 VD |
76 | enum index_search_mode { |
77 | NO_EXPAND_SPARSE = 0, | |
78 | EXPAND_SPARSE = 1 | |
79 | }; | |
80 | ||
8e72d675 JM |
81 | static inline struct cache_entry *mem_pool__ce_alloc(struct mem_pool *mem_pool, size_t len) |
82 | { | |
83 | struct cache_entry *ce; | |
84 | ce = mem_pool_alloc(mem_pool, cache_entry_size(len)); | |
85 | ce->mem_pool_allocated = 1; | |
86 | return ce; | |
87 | } | |
88 | ||
89 | static inline struct cache_entry *mem_pool__ce_calloc(struct mem_pool *mem_pool, size_t len) | |
90 | { | |
91 | struct cache_entry * ce; | |
92 | ce = mem_pool_calloc(mem_pool, 1, cache_entry_size(len)); | |
93 | ce->mem_pool_allocated = 1; | |
94 | return ce; | |
95 | } | |
96 | ||
97 | static struct mem_pool *find_mem_pool(struct index_state *istate) | |
98 | { | |
99 | struct mem_pool **pool_ptr; | |
100 | ||
101 | if (istate->split_index && istate->split_index->base) | |
102 | pool_ptr = &istate->split_index->base->ce_mem_pool; | |
103 | else | |
104 | pool_ptr = &istate->ce_mem_pool; | |
105 | ||
44c7e1a7 EN |
106 | if (!*pool_ptr) { |
107 | *pool_ptr = xmalloc(sizeof(**pool_ptr)); | |
108 | mem_pool_init(*pool_ptr, 0); | |
109 | } | |
8e72d675 JM |
110 | |
111 | return *pool_ptr; | |
112 | } | |
113 | ||
626f35c8 | 114 | static const char *alternate_index_output; |
8fd2cb40 | 115 | |
9cb76b8c JH |
116 | static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce) |
117 | { | |
4300f844 | 118 | if (S_ISSPARSEDIR(ce->ce_mode)) |
9fadb373 | 119 | istate->sparse_index = INDEX_COLLAPSED; |
4300f844 | 120 | |
9cb76b8c | 121 | istate->cache[nr] = ce; |
96872bc2 | 122 | add_name_hash(istate, ce); |
9cb76b8c JH |
123 | } |
124 | ||
cf558704 LT |
125 | static void replace_index_entry(struct index_state *istate, int nr, struct cache_entry *ce) |
126 | { | |
127 | struct cache_entry *old = istate->cache[nr]; | |
128 | ||
078a58e8 | 129 | replace_index_entry_in_base(istate, old, ce); |
2092678c | 130 | remove_name_hash(istate, old); |
a849735b | 131 | discard_cache_entry(old); |
0e267b7a | 132 | ce->ce_flags &= ~CE_HASHED; |
a22c6371 | 133 | set_index_entry(istate, nr, ce); |
078a58e8 | 134 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
883e248b | 135 | mark_fsmonitor_invalid(istate, ce); |
e636a7b4 | 136 | istate->cache_changed |= CE_ENTRY_CHANGED; |
cf558704 LT |
137 | } |
138 | ||
81dc2307 PB |
139 | void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name) |
140 | { | |
b7f9130a | 141 | struct cache_entry *old_entry = istate->cache[nr], *new_entry, *refreshed; |
81dc2307 PB |
142 | int namelen = strlen(new_name); |
143 | ||
a849735b | 144 | new_entry = make_empty_cache_entry(istate, namelen); |
285c2e25 BW |
145 | copy_cache_entry(new_entry, old_entry); |
146 | new_entry->ce_flags &= ~CE_HASHED; | |
147 | new_entry->ce_namelen = namelen; | |
148 | new_entry->index = 0; | |
149 | memcpy(new_entry->name, new_name, namelen + 1); | |
81dc2307 | 150 | |
285c2e25 BW |
151 | cache_tree_invalidate_path(istate, old_entry->name); |
152 | untracked_cache_remove_from_index(istate, old_entry->name); | |
81dc2307 | 153 | remove_index_entry_at(istate, nr); |
b7f9130a VD |
154 | |
155 | /* | |
156 | * Refresh the new index entry. Using 'refresh_cache_entry' ensures | |
157 | * we only update stat info if the entry is otherwise up-to-date (i.e., | |
158 | * the contents/mode haven't changed). This ensures that we reflect the | |
159 | * 'ctime' of the rename in the index without (incorrectly) updating | |
160 | * the cached stat info to reflect unstaged changes on disk. | |
161 | */ | |
162 | refreshed = refresh_cache_entry(istate, new_entry, CE_MATCH_REFRESH); | |
163 | if (refreshed && refreshed != new_entry) { | |
164 | add_index_entry(istate, refreshed, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); | |
165 | discard_cache_entry(new_entry); | |
166 | } else | |
167 | add_index_entry(istate, new_entry, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); | |
81dc2307 PB |
168 | } |
169 | ||
c21d39d7 MH |
170 | void fill_stat_data(struct stat_data *sd, struct stat *st) |
171 | { | |
172 | sd->sd_ctime.sec = (unsigned int)st->st_ctime; | |
173 | sd->sd_mtime.sec = (unsigned int)st->st_mtime; | |
174 | sd->sd_ctime.nsec = ST_CTIME_NSEC(*st); | |
175 | sd->sd_mtime.nsec = ST_MTIME_NSEC(*st); | |
176 | sd->sd_dev = st->st_dev; | |
177 | sd->sd_ino = st->st_ino; | |
178 | sd->sd_uid = st->st_uid; | |
179 | sd->sd_gid = st->st_gid; | |
180 | sd->sd_size = st->st_size; | |
181 | } | |
182 | ||
183 | int match_stat_data(const struct stat_data *sd, struct stat *st) | |
184 | { | |
185 | int changed = 0; | |
186 | ||
187 | if (sd->sd_mtime.sec != (unsigned int)st->st_mtime) | |
188 | changed |= MTIME_CHANGED; | |
189 | if (trust_ctime && check_stat && | |
190 | sd->sd_ctime.sec != (unsigned int)st->st_ctime) | |
191 | changed |= CTIME_CHANGED; | |
192 | ||
193 | #ifdef USE_NSEC | |
194 | if (check_stat && sd->sd_mtime.nsec != ST_MTIME_NSEC(*st)) | |
195 | changed |= MTIME_CHANGED; | |
196 | if (trust_ctime && check_stat && | |
197 | sd->sd_ctime.nsec != ST_CTIME_NSEC(*st)) | |
198 | changed |= CTIME_CHANGED; | |
199 | #endif | |
200 | ||
201 | if (check_stat) { | |
202 | if (sd->sd_uid != (unsigned int) st->st_uid || | |
203 | sd->sd_gid != (unsigned int) st->st_gid) | |
204 | changed |= OWNER_CHANGED; | |
205 | if (sd->sd_ino != (unsigned int) st->st_ino) | |
206 | changed |= INODE_CHANGED; | |
207 | } | |
208 | ||
209 | #ifdef USE_STDEV | |
210 | /* | |
211 | * st_dev breaks on network filesystems where different | |
212 | * clients will have different views of what "device" | |
213 | * the filesystem is on | |
214 | */ | |
215 | if (check_stat && sd->sd_dev != (unsigned int) st->st_dev) | |
216 | changed |= INODE_CHANGED; | |
217 | #endif | |
218 | ||
219 | if (sd->sd_size != (unsigned int) st->st_size) | |
220 | changed |= DATA_CHANGED; | |
221 | ||
222 | return changed; | |
223 | } | |
224 | ||
415e96c8 JH |
225 | /* |
226 | * This only updates the "non-critical" parts of the directory | |
227 | * cache, ie the parts that aren't tracked by GIT, and only used | |
228 | * to validate the cache. | |
229 | */ | |
d4c0a3ac | 230 | void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st) |
415e96c8 | 231 | { |
c21d39d7 | 232 | fill_stat_data(&ce->ce_stat_data, st); |
5f73076c JH |
233 | |
234 | if (assume_unchanged) | |
7a51ed66 | 235 | ce->ce_flags |= CE_VALID; |
eadb5831 | 236 | |
883e248b | 237 | if (S_ISREG(st->st_mode)) { |
eadb5831 | 238 | ce_mark_uptodate(ce); |
b5a81697 | 239 | mark_fsmonitor_valid(istate, ce); |
883e248b | 240 | } |
415e96c8 JH |
241 | } |
242 | ||
58bf2a4c NTND |
243 | static int ce_compare_data(struct index_state *istate, |
244 | const struct cache_entry *ce, | |
245 | struct stat *st) | |
29e4d363 JH |
246 | { |
247 | int match = -1; | |
1b8ac5ea | 248 | int fd = git_open_cloexec(ce->name, O_RDONLY); |
29e4d363 JH |
249 | |
250 | if (fd >= 0) { | |
bebfecb9 | 251 | struct object_id oid; |
58bf2a4c | 252 | if (!index_fd(istate, &oid, fd, st, OBJ_BLOB, ce->name, 0)) |
6a29d7b7 | 253 | match = !oideq(&oid, &ce->oid); |
7f8508e8 | 254 | /* index_fd() closed the file descriptor already */ |
29e4d363 JH |
255 | } |
256 | return match; | |
257 | } | |
258 | ||
21a6b9fa | 259 | static int ce_compare_link(const struct cache_entry *ce, size_t expected_size) |
29e4d363 JH |
260 | { |
261 | int match = -1; | |
29e4d363 JH |
262 | void *buffer; |
263 | unsigned long size; | |
21666f1a | 264 | enum object_type type; |
a60272b3 | 265 | struct strbuf sb = STRBUF_INIT; |
29e4d363 | 266 | |
a60272b3 | 267 | if (strbuf_readlink(&sb, ce->name, expected_size)) |
29e4d363 | 268 | return -1; |
a60272b3 | 269 | |
b4f5aca4 | 270 | buffer = read_object_file(&ce->oid, &type, &size); |
a60272b3 LT |
271 | if (buffer) { |
272 | if (size == sb.len) | |
273 | match = memcmp(buffer, sb.buf, size); | |
274 | free(buffer); | |
29e4d363 | 275 | } |
a60272b3 | 276 | strbuf_release(&sb); |
29e4d363 JH |
277 | return match; |
278 | } | |
279 | ||
21a6b9fa | 280 | static int ce_compare_gitlink(const struct cache_entry *ce) |
f35a6d3b | 281 | { |
1053fe82 | 282 | struct object_id oid; |
f35a6d3b LT |
283 | |
284 | /* | |
285 | * We don't actually require that the .git directory | |
302b9282 | 286 | * under GITLINK directory be a valid git directory. It |
f35a6d3b LT |
287 | * might even be missing (in case nobody populated that |
288 | * sub-project). | |
289 | * | |
290 | * If so, we consider it always to match. | |
291 | */ | |
a98e6101 | 292 | if (resolve_gitlink_ref(ce->name, "HEAD", &oid) < 0) |
f35a6d3b | 293 | return 0; |
6a29d7b7 | 294 | return !oideq(&oid, &ce->oid); |
f35a6d3b LT |
295 | } |
296 | ||
58bf2a4c NTND |
297 | static int ce_modified_check_fs(struct index_state *istate, |
298 | const struct cache_entry *ce, | |
299 | struct stat *st) | |
29e4d363 JH |
300 | { |
301 | switch (st->st_mode & S_IFMT) { | |
302 | case S_IFREG: | |
58bf2a4c | 303 | if (ce_compare_data(istate, ce, st)) |
29e4d363 JH |
304 | return DATA_CHANGED; |
305 | break; | |
306 | case S_IFLNK: | |
dc49cd76 | 307 | if (ce_compare_link(ce, xsize_t(st->st_size))) |
29e4d363 JH |
308 | return DATA_CHANGED; |
309 | break; | |
a8ee75bc | 310 | case S_IFDIR: |
7a51ed66 | 311 | if (S_ISGITLINK(ce->ce_mode)) |
c70115b4 | 312 | return ce_compare_gitlink(ce) ? DATA_CHANGED : 0; |
1cf01a34 | 313 | /* else fallthrough */ |
29e4d363 JH |
314 | default: |
315 | return TYPE_CHANGED; | |
316 | } | |
317 | return 0; | |
318 | } | |
319 | ||
21a6b9fa | 320 | static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st) |
734aab75 LT |
321 | { |
322 | unsigned int changed = 0; | |
323 | ||
7a51ed66 LT |
324 | if (ce->ce_flags & CE_REMOVE) |
325 | return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; | |
326 | ||
327 | switch (ce->ce_mode & S_IFMT) { | |
8ae0a8c5 KS |
328 | case S_IFREG: |
329 | changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0; | |
3e09cdfd JH |
330 | /* We consider only the owner x bit to be relevant for |
331 | * "mode changes" | |
332 | */ | |
333 | if (trust_executable_bit && | |
7a51ed66 | 334 | (0100 & (ce->ce_mode ^ st->st_mode))) |
ffbe1add | 335 | changed |= MODE_CHANGED; |
8ae0a8c5 KS |
336 | break; |
337 | case S_IFLNK: | |
78a8d641 JS |
338 | if (!S_ISLNK(st->st_mode) && |
339 | (has_symlinks || !S_ISREG(st->st_mode))) | |
340 | changed |= TYPE_CHANGED; | |
8ae0a8c5 | 341 | break; |
302b9282 | 342 | case S_IFGITLINK: |
c70115b4 | 343 | /* We ignore most of the st_xxx fields for gitlinks */ |
f35a6d3b LT |
344 | if (!S_ISDIR(st->st_mode)) |
345 | changed |= TYPE_CHANGED; | |
346 | else if (ce_compare_gitlink(ce)) | |
347 | changed |= DATA_CHANGED; | |
a8ee75bc | 348 | return changed; |
8ae0a8c5 | 349 | default: |
391408e5 | 350 | BUG("unsupported ce_mode: %o", ce->ce_mode); |
8ae0a8c5 | 351 | } |
2cb45e95 | 352 | |
c21d39d7 | 353 | changed |= match_stat_data(&ce->ce_stat_data, st); |
b0391890 | 354 | |
f49c2c22 | 355 | /* Racily smudged entry? */ |
c21d39d7 | 356 | if (!ce->ce_stat_data.sd_size) { |
99d1a986 | 357 | if (!is_empty_blob_sha1(ce->oid.hash)) |
f49c2c22 LT |
358 | changed |= DATA_CHANGED; |
359 | } | |
360 | ||
407c8eb0 JH |
361 | return changed; |
362 | } | |
363 | ||
2bb4cda1 NTND |
364 | static int is_racy_stat(const struct index_state *istate, |
365 | const struct stat_data *sd) | |
6d91da6d | 366 | { |
2bb4cda1 | 367 | return (istate->timestamp.sec && |
fba2f38a KB |
368 | #ifdef USE_NSEC |
369 | /* nanosecond timestamped files can also be racy! */ | |
2bb4cda1 NTND |
370 | (istate->timestamp.sec < sd->sd_mtime.sec || |
371 | (istate->timestamp.sec == sd->sd_mtime.sec && | |
372 | istate->timestamp.nsec <= sd->sd_mtime.nsec)) | |
fba2f38a | 373 | #else |
2bb4cda1 | 374 | istate->timestamp.sec <= sd->sd_mtime.sec |
fba2f38a | 375 | #endif |
2bb4cda1 NTND |
376 | ); |
377 | } | |
378 | ||
5581a019 | 379 | int is_racy_timestamp(const struct index_state *istate, |
2bb4cda1 NTND |
380 | const struct cache_entry *ce) |
381 | { | |
382 | return (!S_ISGITLINK(ce->ce_mode) && | |
383 | is_racy_stat(istate, &ce->ce_stat_data)); | |
6d91da6d JH |
384 | } |
385 | ||
ed4efab1 NTND |
386 | int match_stat_data_racy(const struct index_state *istate, |
387 | const struct stat_data *sd, struct stat *st) | |
388 | { | |
389 | if (is_racy_stat(istate, sd)) | |
390 | return MTIME_CHANGED; | |
391 | return match_stat_data(sd, st); | |
6d91da6d JH |
392 | } |
393 | ||
883e248b | 394 | int ie_match_stat(struct index_state *istate, |
21a6b9fa | 395 | const struct cache_entry *ce, struct stat *st, |
4bd5b7da | 396 | unsigned int options) |
407c8eb0 | 397 | { |
5f73076c | 398 | unsigned int changed; |
4bd5b7da | 399 | int ignore_valid = options & CE_MATCH_IGNORE_VALID; |
56cac48c | 400 | int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; |
4bd5b7da | 401 | int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; |
883e248b | 402 | int ignore_fsmonitor = options & CE_MATCH_IGNORE_FSMONITOR; |
5f73076c | 403 | |
883e248b BP |
404 | if (!ignore_fsmonitor) |
405 | refresh_fsmonitor(istate); | |
5f73076c JH |
406 | /* |
407 | * If it's marked as always valid in the index, it's | |
408 | * valid whatever the checked-out copy says. | |
56cac48c NTND |
409 | * |
410 | * skip-worktree has the same effect with higher precedence | |
5f73076c | 411 | */ |
56cac48c NTND |
412 | if (!ignore_skip_worktree && ce_skip_worktree(ce)) |
413 | return 0; | |
7a51ed66 | 414 | if (!ignore_valid && (ce->ce_flags & CE_VALID)) |
5f73076c | 415 | return 0; |
883e248b BP |
416 | if (!ignore_fsmonitor && (ce->ce_flags & CE_FSMONITOR_VALID)) |
417 | return 0; | |
5f73076c | 418 | |
331fcb59 JH |
419 | /* |
420 | * Intent-to-add entries have not been added, so the index entry | |
421 | * by definition never matches what is in the work tree until it | |
422 | * actually gets added. | |
423 | */ | |
895ff3b2 | 424 | if (ce_intent_to_add(ce)) |
331fcb59 JH |
425 | return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; |
426 | ||
5f73076c | 427 | changed = ce_match_stat_basic(ce, st); |
407c8eb0 | 428 | |
29e4d363 JH |
429 | /* |
430 | * Within 1 second of this sequence: | |
431 | * echo xyzzy >file && git-update-index --add file | |
432 | * running this command: | |
433 | * echo frotz >file | |
434 | * would give a falsely clean cache entry. The mtime and | |
435 | * length match the cache, and other stat fields do not change. | |
436 | * | |
437 | * We could detect this at update-index time (the cache entry | |
438 | * being registered/updated records the same time as "now") | |
439 | * and delay the return from git-update-index, but that would | |
440 | * effectively mean we can make at most one commit per second, | |
441 | * which is not acceptable. Instead, we check cache entries | |
442 | * whose mtime are the same as the index file timestamp more | |
5f73076c | 443 | * carefully than others. |
29e4d363 | 444 | */ |
6d91da6d | 445 | if (!changed && is_racy_timestamp(istate, ce)) { |
42f77406 JH |
446 | if (assume_racy_is_modified) |
447 | changed |= DATA_CHANGED; | |
448 | else | |
58bf2a4c | 449 | changed |= ce_modified_check_fs(istate, ce, st); |
42f77406 | 450 | } |
b0391890 | 451 | |
29e4d363 | 452 | return changed; |
b0391890 JH |
453 | } |
454 | ||
883e248b | 455 | int ie_modified(struct index_state *istate, |
21a6b9fa RS |
456 | const struct cache_entry *ce, |
457 | struct stat *st, unsigned int options) | |
b0391890 | 458 | { |
29e4d363 | 459 | int changed, changed_fs; |
4bd5b7da JH |
460 | |
461 | changed = ie_match_stat(istate, ce, st, options); | |
b0391890 JH |
462 | if (!changed) |
463 | return 0; | |
b0391890 JH |
464 | /* |
465 | * If the mode or type has changed, there's no point in trying | |
466 | * to refresh the entry - it's not going to match | |
467 | */ | |
468 | if (changed & (MODE_CHANGED | TYPE_CHANGED)) | |
469 | return changed; | |
470 | ||
c70115b4 JH |
471 | /* |
472 | * Immediately after read-tree or update-index --cacheinfo, | |
473 | * the length field is zero, as we have never even read the | |
474 | * lstat(2) information once, and we cannot trust DATA_CHANGED | |
475 | * returned by ie_match_stat() which in turn was returned by | |
476 | * ce_match_stat_basic() to signal that the filesize of the | |
477 | * blob changed. We have to actually go to the filesystem to | |
478 | * see if the contents match, and if so, should answer "unchanged". | |
479 | * | |
480 | * The logic does not apply to gitlinks, as ce_match_stat_basic() | |
481 | * already has checked the actual HEAD from the filesystem in the | |
482 | * subproject. If ie_match_stat() already said it is different, | |
483 | * then we know it is. | |
b0391890 | 484 | */ |
c70115b4 | 485 | if ((changed & DATA_CHANGED) && |
c21d39d7 | 486 | (S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0)) |
b0391890 JH |
487 | return changed; |
488 | ||
58bf2a4c | 489 | changed_fs = ce_modified_check_fs(istate, ce, st); |
29e4d363 JH |
490 | if (changed_fs) |
491 | return changed | changed_fs; | |
b0391890 JH |
492 | return 0; |
493 | } | |
494 | ||
1b4a38d7 RS |
495 | int base_name_compare(const char *name1, size_t len1, int mode1, |
496 | const char *name2, size_t len2, int mode2) | |
958ba6c9 LT |
497 | { |
498 | unsigned char c1, c2; | |
1b4a38d7 | 499 | size_t len = len1 < len2 ? len1 : len2; |
958ba6c9 LT |
500 | int cmp; |
501 | ||
502 | cmp = memcmp(name1, name2, len); | |
503 | if (cmp) | |
504 | return cmp; | |
505 | c1 = name1[len]; | |
506 | c2 = name2[len]; | |
1833a925 | 507 | if (!c1 && S_ISDIR(mode1)) |
958ba6c9 | 508 | c1 = '/'; |
1833a925 | 509 | if (!c2 && S_ISDIR(mode2)) |
958ba6c9 LT |
510 | c2 = '/'; |
511 | return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; | |
512 | } | |
513 | ||
0ab9e1e8 LT |
514 | /* |
515 | * df_name_compare() is identical to base_name_compare(), except it | |
516 | * compares conflicting directory/file entries as equal. Note that | |
517 | * while a directory name compares as equal to a regular file, they | |
518 | * then individually compare _differently_ to a filename that has | |
519 | * a dot after the basename (because '\0' < '.' < '/'). | |
520 | * | |
521 | * This is used by routines that want to traverse the git namespace | |
522 | * but then handle conflicting entries together when possible. | |
523 | */ | |
1b4a38d7 RS |
524 | int df_name_compare(const char *name1, size_t len1, int mode1, |
525 | const char *name2, size_t len2, int mode2) | |
0ab9e1e8 | 526 | { |
0ab9e1e8 | 527 | unsigned char c1, c2; |
1b4a38d7 RS |
528 | size_t len = len1 < len2 ? len1 : len2; |
529 | int cmp; | |
0ab9e1e8 LT |
530 | |
531 | cmp = memcmp(name1, name2, len); | |
532 | if (cmp) | |
533 | return cmp; | |
534 | /* Directories and files compare equal (same length, same name) */ | |
535 | if (len1 == len2) | |
536 | return 0; | |
537 | c1 = name1[len]; | |
538 | if (!c1 && S_ISDIR(mode1)) | |
539 | c1 = '/'; | |
540 | c2 = name2[len]; | |
541 | if (!c2 && S_ISDIR(mode2)) | |
542 | c2 = '/'; | |
543 | if (c1 == '/' && !c2) | |
544 | return 0; | |
545 | if (c2 == '/' && !c1) | |
546 | return 0; | |
547 | return c1 - c2; | |
548 | } | |
549 | ||
ccdd4a0f | 550 | int name_compare(const char *name1, size_t len1, const char *name2, size_t len2) |
eb38c22f | 551 | { |
ccdd4a0f JM |
552 | size_t min_len = (len1 < len2) ? len1 : len2; |
553 | int cmp = memcmp(name1, name2, min_len); | |
eb38c22f LT |
554 | if (cmp) |
555 | return cmp; | |
556 | if (len1 < len2) | |
557 | return -1; | |
558 | if (len1 > len2) | |
559 | return 1; | |
ccdd4a0f JM |
560 | return 0; |
561 | } | |
562 | ||
563 | int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2) | |
564 | { | |
565 | int cmp; | |
566 | ||
567 | cmp = name_compare(name1, len1, name2, len2); | |
568 | if (cmp) | |
569 | return cmp; | |
5f73076c | 570 | |
b60e188c | 571 | if (stage1 < stage2) |
95fd5bf8 | 572 | return -1; |
b60e188c | 573 | if (stage1 > stage2) |
95fd5bf8 | 574 | return 1; |
eb38c22f LT |
575 | return 0; |
576 | } | |
577 | ||
20ec2d03 VD |
578 | static int index_name_stage_pos(struct index_state *istate, |
579 | const char *name, int namelen, | |
580 | int stage, | |
581 | enum index_search_mode search_mode) | |
eb38c22f LT |
582 | { |
583 | int first, last; | |
584 | ||
585 | first = 0; | |
4aab5b46 | 586 | last = istate->cache_nr; |
eb38c22f | 587 | while (last > first) { |
568a05c5 | 588 | int next = first + ((last - first) >> 1); |
4aab5b46 | 589 | struct cache_entry *ce = istate->cache[next]; |
b60e188c | 590 | int cmp = cache_name_stage_compare(name, namelen, stage, ce->name, ce_namelen(ce), ce_stage(ce)); |
eb38c22f | 591 | if (!cmp) |
76e7f4ec | 592 | return next; |
eb38c22f LT |
593 | if (cmp < 0) { |
594 | last = next; | |
595 | continue; | |
596 | } | |
597 | first = next+1; | |
598 | } | |
95e0321c | 599 | |
20ec2d03 | 600 | if (search_mode == EXPAND_SPARSE && istate->sparse_index && |
95e0321c DS |
601 | first > 0) { |
602 | /* Note: first <= istate->cache_nr */ | |
603 | struct cache_entry *ce = istate->cache[first - 1]; | |
604 | ||
605 | /* | |
606 | * If we are in a sparse-index _and_ the entry before the | |
607 | * insertion position is a sparse-directory entry that is | |
608 | * an ancestor of 'name', then we need to expand the index | |
609 | * and search again. This will only trigger once, because | |
610 | * thereafter the index is fully expanded. | |
611 | */ | |
612 | if (S_ISSPARSEDIR(ce->ce_mode) && | |
613 | ce_namelen(ce) < namelen && | |
614 | !strncmp(name, ce->name, ce_namelen(ce))) { | |
615 | ensure_full_index(istate); | |
20ec2d03 | 616 | return index_name_stage_pos(istate, name, namelen, stage, search_mode); |
95e0321c DS |
617 | } |
618 | } | |
619 | ||
76e7f4ec | 620 | return -first-1; |
eb38c22f LT |
621 | } |
622 | ||
847a9e5d | 623 | int index_name_pos(struct index_state *istate, const char *name, int namelen) |
b60e188c | 624 | { |
20ec2d03 VD |
625 | return index_name_stage_pos(istate, name, namelen, 0, EXPAND_SPARSE); |
626 | } | |
627 | ||
9553aa0f VD |
628 | int index_name_pos_sparse(struct index_state *istate, const char *name, int namelen) |
629 | { | |
630 | return index_name_stage_pos(istate, name, namelen, 0, NO_EXPAND_SPARSE); | |
631 | } | |
632 | ||
20ec2d03 VD |
633 | int index_entry_exists(struct index_state *istate, const char *name, int namelen) |
634 | { | |
635 | return index_name_stage_pos(istate, name, namelen, 0, NO_EXPAND_SPARSE) >= 0; | |
b60e188c TG |
636 | } |
637 | ||
4aab5b46 | 638 | int remove_index_entry_at(struct index_state *istate, int pos) |
7b937ca3 | 639 | { |
cf558704 LT |
640 | struct cache_entry *ce = istate->cache[pos]; |
641 | ||
cfc5789a | 642 | record_resolve_undo(istate, ce); |
2092678c | 643 | remove_name_hash(istate, ce); |
045113a5 | 644 | save_or_free_index_entry(istate, ce); |
e636a7b4 | 645 | istate->cache_changed |= CE_ENTRY_REMOVED; |
4aab5b46 JH |
646 | istate->cache_nr--; |
647 | if (pos >= istate->cache_nr) | |
7b937ca3 | 648 | return 0; |
f331ab9d RS |
649 | MOVE_ARRAY(istate->cache + pos, istate->cache + pos + 1, |
650 | istate->cache_nr - pos); | |
7b937ca3 LT |
651 | return 1; |
652 | } | |
653 | ||
36419c8e | 654 | /* |
98e023de | 655 | * Remove all cache entries marked for removal, that is where |
36419c8e KB |
656 | * CE_REMOVE is set in ce_flags. This is much more effective than |
657 | * calling remove_index_entry_at() for each entry to be removed. | |
658 | */ | |
6fdc2057 | 659 | void remove_marked_cache_entries(struct index_state *istate, int invalidate) |
36419c8e KB |
660 | { |
661 | struct cache_entry **ce_array = istate->cache; | |
662 | unsigned int i, j; | |
663 | ||
664 | for (i = j = 0; i < istate->cache_nr; i++) { | |
5699d17e | 665 | if (ce_array[i]->ce_flags & CE_REMOVE) { |
6fdc2057 TG |
666 | if (invalidate) { |
667 | cache_tree_invalidate_path(istate, | |
668 | ce_array[i]->name); | |
669 | untracked_cache_remove_from_index(istate, | |
670 | ce_array[i]->name); | |
671 | } | |
2092678c | 672 | remove_name_hash(istate, ce_array[i]); |
045113a5 | 673 | save_or_free_index_entry(istate, ce_array[i]); |
5699d17e | 674 | } |
36419c8e KB |
675 | else |
676 | ce_array[j++] = ce_array[i]; | |
677 | } | |
ad837d9e NTND |
678 | if (j == istate->cache_nr) |
679 | return; | |
e636a7b4 | 680 | istate->cache_changed |= CE_ENTRY_REMOVED; |
36419c8e KB |
681 | istate->cache_nr = j; |
682 | } | |
683 | ||
4aab5b46 | 684 | int remove_file_from_index(struct index_state *istate, const char *path) |
197ee8c9 | 685 | { |
4aab5b46 | 686 | int pos = index_name_pos(istate, path, strlen(path)); |
c4e3cca1 JH |
687 | if (pos < 0) |
688 | pos = -pos-1; | |
a5400efe | 689 | cache_tree_invalidate_path(istate, path); |
e931371a | 690 | untracked_cache_remove_from_index(istate, path); |
4aab5b46 JH |
691 | while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) |
692 | remove_index_entry_at(istate, pos); | |
197ee8c9 LT |
693 | return 0; |
694 | } | |
695 | ||
20314271 JS |
696 | static int compare_name(struct cache_entry *ce, const char *path, int namelen) |
697 | { | |
698 | return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen); | |
699 | } | |
700 | ||
701 | static int index_name_pos_also_unmerged(struct index_state *istate, | |
702 | const char *path, int namelen) | |
703 | { | |
704 | int pos = index_name_pos(istate, path, namelen); | |
705 | struct cache_entry *ce; | |
706 | ||
707 | if (pos >= 0) | |
708 | return pos; | |
709 | ||
710 | /* maybe unmerged? */ | |
711 | pos = -1 - pos; | |
712 | if (pos >= istate->cache_nr || | |
713 | compare_name((ce = istate->cache[pos]), path, namelen)) | |
714 | return -1; | |
715 | ||
716 | /* order of preference: stage 2, 1, 3 */ | |
717 | if (ce_stage(ce) == 1 && pos + 1 < istate->cache_nr && | |
718 | ce_stage((ce = istate->cache[pos + 1])) == 2 && | |
719 | !compare_name(ce, path, namelen)) | |
720 | pos++; | |
721 | return pos; | |
722 | } | |
723 | ||
1102952b LT |
724 | static int different_name(struct cache_entry *ce, struct cache_entry *alias) |
725 | { | |
726 | int len = ce_namelen(ce); | |
727 | return ce_namelen(alias) != len || memcmp(ce->name, alias->name, len); | |
728 | } | |
729 | ||
730 | /* | |
731 | * If we add a filename that aliases in the cache, we will use the | |
732 | * name that we already have - but we don't want to update the same | |
733 | * alias twice, because that implies that there were actually two | |
734 | * different files with aliasing names! | |
735 | * | |
736 | * So we use the CE_ADDED flag to verify that the alias was an old | |
737 | * one before we accept it as | |
738 | */ | |
045113a5 NTND |
739 | static struct cache_entry *create_alias_ce(struct index_state *istate, |
740 | struct cache_entry *ce, | |
741 | struct cache_entry *alias) | |
1102952b LT |
742 | { |
743 | int len; | |
285c2e25 | 744 | struct cache_entry *new_entry; |
1102952b LT |
745 | |
746 | if (alias->ce_flags & CE_ADDED) | |
9d0a9e90 NTND |
747 | die(_("will not add file alias '%s' ('%s' already exists in index)"), |
748 | ce->name, alias->name); | |
1102952b LT |
749 | |
750 | /* Ok, create the new entry using the name of the existing alias */ | |
751 | len = ce_namelen(alias); | |
a849735b | 752 | new_entry = make_empty_cache_entry(istate, len); |
285c2e25 BW |
753 | memcpy(new_entry->name, alias->name, len); |
754 | copy_cache_entry(new_entry, ce); | |
045113a5 | 755 | save_or_free_index_entry(istate, ce); |
285c2e25 | 756 | return new_entry; |
1102952b LT |
757 | } |
758 | ||
b4b313f9 | 759 | void set_object_name_for_intent_to_add_entry(struct cache_entry *ce) |
39425819 | 760 | { |
a09c985e | 761 | struct object_id oid; |
c80d226a | 762 | if (write_object_file("", 0, OBJ_BLOB, &oid)) |
9d0a9e90 | 763 | die(_("cannot create an empty blob in the object database")); |
a09c985e | 764 | oidcpy(&ce->oid, &oid); |
39425819 JH |
765 | } |
766 | ||
610d55af | 767 | int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags) |
11be42a4 | 768 | { |
a849735b | 769 | int namelen, was_same; |
d177cab0 | 770 | mode_t st_mode = st->st_mode; |
9472935d | 771 | struct cache_entry *ce, *alias = NULL; |
56cac48c | 772 | unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; |
38ed1d89 JH |
773 | int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); |
774 | int pretend = flags & ADD_CACHE_PRETEND; | |
39425819 JH |
775 | int intent_only = flags & ADD_CACHE_INTENT; |
776 | int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| | |
777 | (intent_only ? ADD_CACHE_NEW_ONLY : 0)); | |
e578d031 | 778 | unsigned hash_flags = pretend ? 0 : HASH_WRITE_OBJECT; |
f937bc2f | 779 | struct object_id oid; |
9472935d | 780 | |
9e5da3d0 JK |
781 | if (flags & ADD_CACHE_RENORMALIZE) |
782 | hash_flags |= HASH_RENORMALIZE; | |
11be42a4 | 783 | |
d177cab0 | 784 | if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) |
9d0a9e90 | 785 | return error(_("%s: can only add regular files, symbolic links or git-directories"), path); |
11be42a4 JS |
786 | |
787 | namelen = strlen(path); | |
d177cab0 | 788 | if (S_ISDIR(st_mode)) { |
f937bc2f KM |
789 | if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) |
790 | return error(_("'%s' does not have a commit checked out"), path); | |
09595258 LT |
791 | while (namelen && path[namelen-1] == '/') |
792 | namelen--; | |
793 | } | |
a849735b | 794 | ce = make_empty_cache_entry(istate, namelen); |
11be42a4 | 795 | memcpy(ce->name, path, namelen); |
b60e188c | 796 | ce->ce_namelen = namelen; |
39425819 | 797 | if (!intent_only) |
d4c0a3ac | 798 | fill_stat_cache_info(istate, ce, st); |
388b2acd JH |
799 | else |
800 | ce->ce_flags |= CE_INTENT_TO_ADD; | |
11be42a4 | 801 | |
610d55af TG |
802 | |
803 | if (trust_executable_bit && has_symlinks) { | |
d177cab0 | 804 | ce->ce_mode = create_ce_mode(st_mode); |
610d55af | 805 | } else { |
78a8d641 JS |
806 | /* If there is an existing entry, pick the mode bits and type |
807 | * from it, otherwise assume unexecutable regular file. | |
11be42a4 | 808 | */ |
185c975f | 809 | struct cache_entry *ent; |
20314271 | 810 | int pos = index_name_pos_also_unmerged(istate, path, namelen); |
185c975f | 811 | |
4aab5b46 | 812 | ent = (0 <= pos) ? istate->cache[pos] : NULL; |
d177cab0 | 813 | ce->ce_mode = ce_mode_from_stat(ent, st_mode); |
11be42a4 JS |
814 | } |
815 | ||
dc1ae704 JJ |
816 | /* When core.ignorecase=true, determine if a directory of the same name but differing |
817 | * case already exists within the Git repository. If it does, ensure the directory | |
818 | * case of the file being added to the repository matches (is folded into) the existing | |
819 | * entry's directory case. | |
820 | */ | |
821 | if (ignore_case) { | |
41284eb0 | 822 | adjust_dirname_case(istate, ce->name); |
dc1ae704 | 823 | } |
e2c2a375 | 824 | if (!(flags & ADD_CACHE_RENORMALIZE)) { |
9472935d TB |
825 | alias = index_file_exists(istate, ce->name, |
826 | ce_namelen(ce), ignore_case); | |
827 | if (alias && | |
828 | !ce_stage(alias) && | |
829 | !ie_match_stat(istate, alias, st, ce_option)) { | |
830 | /* Nothing changed, really */ | |
831 | if (!S_ISGITLINK(alias->ce_mode)) | |
832 | ce_mark_uptodate(alias); | |
833 | alias->ce_flags |= CE_ADDED; | |
dc1ae704 | 834 | |
a849735b | 835 | discard_cache_entry(ce); |
9472935d TB |
836 | return 0; |
837 | } | |
0781b8a9 | 838 | } |
39425819 | 839 | if (!intent_only) { |
1c418243 | 840 | if (index_path(istate, &ce->oid, path, st, hash_flags)) { |
a849735b | 841 | discard_cache_entry(ce); |
9d0a9e90 | 842 | return error(_("unable to index file '%s'"), path); |
2d9426b0 | 843 | } |
39425819 | 844 | } else |
b4b313f9 | 845 | set_object_name_for_intent_to_add_entry(ce); |
39425819 | 846 | |
1102952b | 847 | if (ignore_case && alias && different_name(ce, alias)) |
045113a5 | 848 | ce = create_alias_ce(istate, ce, alias); |
1102952b | 849 | ce->ce_flags |= CE_ADDED; |
38ed1d89 | 850 | |
3bf0dd1f | 851 | /* It was suspected to be racily clean, but it turns out to be Ok */ |
38ed1d89 JH |
852 | was_same = (alias && |
853 | !ce_stage(alias) && | |
4a7e27e9 | 854 | oideq(&alias->oid, &ce->oid) && |
38ed1d89 JH |
855 | ce->ce_mode == alias->ce_mode); |
856 | ||
857 | if (pretend) | |
a849735b | 858 | discard_cache_entry(ce); |
067178ed | 859 | else if (add_index_entry(istate, ce, add_option)) { |
a849735b | 860 | discard_cache_entry(ce); |
9d0a9e90 | 861 | return error(_("unable to add '%s' to index"), path); |
067178ed | 862 | } |
38ed1d89 | 863 | if (verbose && !was_same) |
11be42a4 | 864 | printf("add '%s'\n", path); |
11be42a4 JS |
865 | return 0; |
866 | } | |
867 | ||
610d55af | 868 | int add_file_to_index(struct index_state *istate, const char *path, int flags) |
d177cab0 LT |
869 | { |
870 | struct stat st; | |
871 | if (lstat(path, &st)) | |
9d0a9e90 | 872 | die_errno(_("unable to stat '%s'"), path); |
610d55af | 873 | return add_to_index(istate, path, &st, flags); |
d177cab0 LT |
874 | } |
875 | ||
a849735b JM |
876 | struct cache_entry *make_empty_cache_entry(struct index_state *istate, size_t len) |
877 | { | |
8e72d675 | 878 | return mem_pool__ce_calloc(find_mem_pool(istate), len); |
a849735b JM |
879 | } |
880 | ||
96168827 MT |
881 | struct cache_entry *make_empty_transient_cache_entry(size_t len, |
882 | struct mem_pool *ce_mem_pool) | |
a849735b | 883 | { |
96168827 MT |
884 | if (ce_mem_pool) |
885 | return mem_pool__ce_calloc(ce_mem_pool, len); | |
a849735b JM |
886 | return xcalloc(1, cache_entry_size(len)); |
887 | } | |
888 | ||
2a1ae649 RS |
889 | enum verify_path_result { |
890 | PATH_OK, | |
891 | PATH_INVALID, | |
892 | PATH_DIR_WITH_SEP, | |
893 | }; | |
894 | ||
895 | static enum verify_path_result verify_path_internal(const char *, unsigned); | |
896 | ||
897 | int verify_path(const char *path, unsigned mode) | |
898 | { | |
c8ad9d04 | 899 | return verify_path_internal(path, mode) == PATH_OK; |
2a1ae649 RS |
900 | } |
901 | ||
a849735b JM |
902 | struct cache_entry *make_cache_entry(struct index_state *istate, |
903 | unsigned int mode, | |
825ed4d9 JM |
904 | const struct object_id *oid, |
905 | const char *path, | |
906 | int stage, | |
907 | unsigned int refresh_options) | |
6640f881 | 908 | { |
bc1c2caa | 909 | struct cache_entry *ce, *ret; |
a849735b | 910 | int len; |
6640f881 | 911 | |
c8ad9d04 | 912 | if (verify_path_internal(path, mode) == PATH_INVALID) { |
9d0a9e90 | 913 | error(_("invalid path '%s'"), path); |
6640f881 | 914 | return NULL; |
7e7abea9 | 915 | } |
6640f881 CR |
916 | |
917 | len = strlen(path); | |
a849735b | 918 | ce = make_empty_cache_entry(istate, len); |
6640f881 | 919 | |
825ed4d9 | 920 | oidcpy(&ce->oid, oid); |
6640f881 | 921 | memcpy(ce->name, path, len); |
b60e188c TG |
922 | ce->ce_flags = create_ce_flags(stage); |
923 | ce->ce_namelen = len; | |
6640f881 CR |
924 | ce->ce_mode = create_ce_mode(mode); |
925 | ||
d7b665c3 | 926 | ret = refresh_cache_entry(istate, ce, refresh_options); |
915e44c6 | 927 | if (ret != ce) |
a849735b | 928 | discard_cache_entry(ce); |
915e44c6 | 929 | return ret; |
6640f881 CR |
930 | } |
931 | ||
96168827 MT |
932 | struct cache_entry *make_transient_cache_entry(unsigned int mode, |
933 | const struct object_id *oid, | |
934 | const char *path, | |
935 | int stage, | |
936 | struct mem_pool *ce_mem_pool) | |
a849735b JM |
937 | { |
938 | struct cache_entry *ce; | |
939 | int len; | |
940 | ||
941 | if (!verify_path(path, mode)) { | |
9d0a9e90 | 942 | error(_("invalid path '%s'"), path); |
a849735b JM |
943 | return NULL; |
944 | } | |
945 | ||
946 | len = strlen(path); | |
96168827 | 947 | ce = make_empty_transient_cache_entry(len, ce_mem_pool); |
a849735b JM |
948 | |
949 | oidcpy(&ce->oid, oid); | |
950 | memcpy(ce->name, path, len); | |
951 | ce->ce_flags = create_ce_flags(stage); | |
952 | ce->ce_namelen = len; | |
953 | ce->ce_mode = create_ce_mode(mode); | |
954 | ||
955 | return ce; | |
956 | } | |
957 | ||
d9d70966 TG |
958 | /* |
959 | * Chmod an index entry with either +x or -x. | |
960 | * | |
961 | * Returns -1 if the chmod for the particular cache entry failed (if it's | |
962 | * not a regular file), -2 if an invalid flip argument is passed in, 0 | |
963 | * otherwise. | |
964 | */ | |
965 | int chmod_index_entry(struct index_state *istate, struct cache_entry *ce, | |
966 | char flip) | |
967 | { | |
968 | if (!S_ISREG(ce->ce_mode)) | |
969 | return -1; | |
970 | switch (flip) { | |
971 | case '+': | |
972 | ce->ce_mode |= 0111; | |
973 | break; | |
974 | case '-': | |
975 | ce->ce_mode &= ~0111; | |
976 | break; | |
977 | default: | |
978 | return -2; | |
979 | } | |
980 | cache_tree_invalidate_path(istate, ce->name); | |
981 | ce->ce_flags |= CE_UPDATE_IN_BASE; | |
883e248b | 982 | mark_fsmonitor_invalid(istate, ce); |
d9d70966 TG |
983 | istate->cache_changed |= CE_ENTRY_CHANGED; |
984 | ||
985 | return 0; | |
986 | } | |
987 | ||
9c5e6c80 | 988 | int ce_same_name(const struct cache_entry *a, const struct cache_entry *b) |
7b937ca3 LT |
989 | { |
990 | int len = ce_namelen(a); | |
991 | return ce_namelen(b) == len && !memcmp(a->name, b->name, len); | |
992 | } | |
993 | ||
8dcf39c4 LT |
994 | /* |
995 | * We fundamentally don't like some paths: we don't want | |
996 | * dot or dot-dot anywhere, and for obvious reasons don't | |
997 | * want to recurse into ".git" either. | |
998 | * | |
999 | * Also, we don't want double slashes or slashes at the | |
1000 | * end that can make pathnames ambiguous. | |
1001 | */ | |
10ecfa76 | 1002 | static int verify_dotfile(const char *rest, unsigned mode) |
8dcf39c4 LT |
1003 | { |
1004 | /* | |
1005 | * The first character was '.', but that | |
1006 | * has already been discarded, we now test | |
1007 | * the rest. | |
1008 | */ | |
e0f530ff | 1009 | |
8dcf39c4 | 1010 | /* "." is not allowed */ |
e0f530ff | 1011 | if (*rest == '\0' || is_dir_sep(*rest)) |
8dcf39c4 LT |
1012 | return 0; |
1013 | ||
e0f530ff | 1014 | switch (*rest) { |
8dcf39c4 | 1015 | /* |
641084b6 JK |
1016 | * ".git" followed by NUL or slash is bad. Note that we match |
1017 | * case-insensitively here, even if ignore_case is not set. | |
1018 | * This outlaws ".GIT" everywhere out of an abundance of caution, | |
1019 | * since there's really no good reason to allow it. | |
10ecfa76 JK |
1020 | * |
1021 | * Once we've seen ".git", we can also find ".gitmodules", etc (also | |
1022 | * case-insensitively). | |
8dcf39c4 LT |
1023 | */ |
1024 | case 'g': | |
cc2fc7c2 JK |
1025 | case 'G': |
1026 | if (rest[1] != 'i' && rest[1] != 'I') | |
8dcf39c4 | 1027 | break; |
cc2fc7c2 | 1028 | if (rest[2] != 't' && rest[2] != 'T') |
8dcf39c4 | 1029 | break; |
e19e5e66 JK |
1030 | if (rest[3] == '\0' || is_dir_sep(rest[3])) |
1031 | return 0; | |
10ecfa76 JK |
1032 | if (S_ISLNK(mode)) { |
1033 | rest += 3; | |
1034 | if (skip_iprefix(rest, "modules", &rest) && | |
1035 | (*rest == '\0' || is_dir_sep(*rest))) | |
1036 | return 0; | |
1037 | } | |
e19e5e66 | 1038 | break; |
8dcf39c4 | 1039 | case '.': |
e0f530ff | 1040 | if (rest[1] == '\0' || is_dir_sep(rest[1])) |
8dcf39c4 LT |
1041 | return 0; |
1042 | } | |
1043 | return 1; | |
1044 | } | |
1045 | ||
2a1ae649 RS |
1046 | static enum verify_path_result verify_path_internal(const char *path, |
1047 | unsigned mode) | |
8dcf39c4 | 1048 | { |
49e268e2 | 1049 | char c = 0; |
8dcf39c4 | 1050 | |
56948cb6 | 1051 | if (has_dos_drive_prefix(path)) |
2a1ae649 | 1052 | return PATH_INVALID; |
56948cb6 | 1053 | |
d2c84dad | 1054 | if (!is_valid_path(path)) |
2a1ae649 | 1055 | return PATH_INVALID; |
d2c84dad | 1056 | |
8dcf39c4 LT |
1057 | goto inside; |
1058 | for (;;) { | |
1059 | if (!c) | |
2a1ae649 | 1060 | return PATH_OK; |
56948cb6 | 1061 | if (is_dir_sep(c)) { |
8dcf39c4 | 1062 | inside: |
10ecfa76 | 1063 | if (protect_hfs) { |
49e268e2 | 1064 | |
10ecfa76 | 1065 | if (is_hfs_dotgit(path)) |
2a1ae649 | 1066 | return PATH_INVALID; |
10ecfa76 JK |
1067 | if (S_ISLNK(mode)) { |
1068 | if (is_hfs_dotgitmodules(path)) | |
2a1ae649 | 1069 | return PATH_INVALID; |
10ecfa76 JK |
1070 | } |
1071 | } | |
1072 | if (protect_ntfs) { | |
bccc37fd | 1073 | #if defined GIT_WINDOWS_NATIVE || defined __CYGWIN__ |
49e268e2 | 1074 | if (c == '\\') |
2a1ae649 | 1075 | return PATH_INVALID; |
49e268e2 | 1076 | #endif |
10ecfa76 | 1077 | if (is_ntfs_dotgit(path)) |
2a1ae649 | 1078 | return PATH_INVALID; |
10ecfa76 JK |
1079 | if (S_ISLNK(mode)) { |
1080 | if (is_ntfs_dotgitmodules(path)) | |
2a1ae649 | 1081 | return PATH_INVALID; |
10ecfa76 JK |
1082 | } |
1083 | } | |
1084 | ||
8dcf39c4 | 1085 | c = *path++; |
10ecfa76 | 1086 | if ((c == '.' && !verify_dotfile(path, mode)) || |
6e773527 | 1087 | is_dir_sep(c)) |
2a1ae649 | 1088 | return PATH_INVALID; |
6e773527 DS |
1089 | /* |
1090 | * allow terminating directory separators for | |
1091 | * sparse directory entries. | |
1092 | */ | |
1093 | if (c == '\0') | |
2a1ae649 RS |
1094 | return S_ISDIR(mode) ? PATH_DIR_WITH_SEP : |
1095 | PATH_INVALID; | |
288a74bc JS |
1096 | } else if (c == '\\' && protect_ntfs) { |
1097 | if (is_ntfs_dotgit(path)) | |
2a1ae649 | 1098 | return PATH_INVALID; |
288a74bc JS |
1099 | if (S_ISLNK(mode)) { |
1100 | if (is_ntfs_dotgitmodules(path)) | |
2a1ae649 | 1101 | return PATH_INVALID; |
288a74bc | 1102 | } |
8dcf39c4 | 1103 | } |
288a74bc | 1104 | |
8dcf39c4 LT |
1105 | c = *path++; |
1106 | } | |
1107 | } | |
1108 | ||
12676608 LT |
1109 | /* |
1110 | * Do we have another file that has the beginning components being a | |
1111 | * proper superset of the name we're trying to add? | |
0f1e4f04 | 1112 | */ |
4aab5b46 JH |
1113 | static int has_file_name(struct index_state *istate, |
1114 | const struct cache_entry *ce, int pos, int ok_to_replace) | |
0f1e4f04 | 1115 | { |
12676608 LT |
1116 | int retval = 0; |
1117 | int len = ce_namelen(ce); | |
b155725d | 1118 | int stage = ce_stage(ce); |
12676608 | 1119 | const char *name = ce->name; |
0f1e4f04 | 1120 | |
4aab5b46 JH |
1121 | while (pos < istate->cache_nr) { |
1122 | struct cache_entry *p = istate->cache[pos++]; | |
0f1e4f04 | 1123 | |
12676608 | 1124 | if (len >= ce_namelen(p)) |
0f1e4f04 | 1125 | break; |
12676608 LT |
1126 | if (memcmp(name, p->name, len)) |
1127 | break; | |
b155725d JH |
1128 | if (ce_stage(p) != stage) |
1129 | continue; | |
12676608 LT |
1130 | if (p->name[len] != '/') |
1131 | continue; | |
7a51ed66 | 1132 | if (p->ce_flags & CE_REMOVE) |
21cd8d00 | 1133 | continue; |
12676608 LT |
1134 | retval = -1; |
1135 | if (!ok_to_replace) | |
1136 | break; | |
4aab5b46 | 1137 | remove_index_entry_at(istate, --pos); |
0f1e4f04 | 1138 | } |
12676608 LT |
1139 | return retval; |
1140 | } | |
0f1e4f04 | 1141 | |
a6db3fbb JH |
1142 | |
1143 | /* | |
1144 | * Like strcmp(), but also return the offset of the first change. | |
1145 | * If strings are equal, return the length. | |
1146 | */ | |
1147 | int strcmp_offset(const char *s1, const char *s2, size_t *first_change) | |
1148 | { | |
1149 | size_t k; | |
1150 | ||
1151 | if (!first_change) | |
1152 | return strcmp(s1, s2); | |
1153 | ||
1154 | for (k = 0; s1[k] == s2[k]; k++) | |
1155 | if (s1[k] == '\0') | |
1156 | break; | |
1157 | ||
1158 | *first_change = k; | |
1159 | return (unsigned char)s1[k] - (unsigned char)s2[k]; | |
1160 | } | |
1161 | ||
12676608 LT |
1162 | /* |
1163 | * Do we have another file with a pathname that is a proper | |
1164 | * subset of the name we're trying to add? | |
06b6d81b JH |
1165 | * |
1166 | * That is, is there another file in the index with a path | |
1167 | * that matches a sub-directory in the given entry? | |
12676608 | 1168 | */ |
4aab5b46 JH |
1169 | static int has_dir_name(struct index_state *istate, |
1170 | const struct cache_entry *ce, int pos, int ok_to_replace) | |
12676608 LT |
1171 | { |
1172 | int retval = 0; | |
b155725d | 1173 | int stage = ce_stage(ce); |
12676608 LT |
1174 | const char *name = ce->name; |
1175 | const char *slash = name + ce_namelen(ce); | |
06b6d81b JH |
1176 | size_t len_eq_last; |
1177 | int cmp_last = 0; | |
1178 | ||
1179 | /* | |
1180 | * We are frequently called during an iteration on a sorted | |
1181 | * list of pathnames and while building a new index. Therefore, | |
1182 | * there is a high probability that this entry will eventually | |
1183 | * be appended to the index, rather than inserted in the middle. | |
1184 | * If we can confirm that, we can avoid binary searches on the | |
1185 | * components of the pathname. | |
1186 | * | |
1187 | * Compare the entry's full path with the last path in the index. | |
1188 | */ | |
1189 | if (istate->cache_nr > 0) { | |
1190 | cmp_last = strcmp_offset(name, | |
1191 | istate->cache[istate->cache_nr - 1]->name, | |
1192 | &len_eq_last); | |
1193 | if (cmp_last > 0) { | |
1194 | if (len_eq_last == 0) { | |
1195 | /* | |
1196 | * The entry sorts AFTER the last one in the | |
1197 | * index and their paths have no common prefix, | |
1198 | * so there cannot be a F/D conflict. | |
1199 | */ | |
1200 | return retval; | |
1201 | } else { | |
1202 | /* | |
1203 | * The entry sorts AFTER the last one in the | |
1204 | * index, but has a common prefix. Fall through | |
1205 | * to the loop below to disect the entry's path | |
1206 | * and see where the difference is. | |
1207 | */ | |
1208 | } | |
1209 | } else if (cmp_last == 0) { | |
1210 | /* | |
1211 | * The entry exactly matches the last one in the | |
1212 | * index, but because of multiple stage and CE_REMOVE | |
1213 | * items, we fall through and let the regular search | |
1214 | * code handle it. | |
1215 | */ | |
1216 | } | |
1217 | } | |
0f1e4f04 | 1218 | |
12676608 | 1219 | for (;;) { |
b986df5c | 1220 | size_t len; |
0f1e4f04 | 1221 | |
12676608 LT |
1222 | for (;;) { |
1223 | if (*--slash == '/') | |
1224 | break; | |
1225 | if (slash <= ce->name) | |
1226 | return retval; | |
1227 | } | |
1228 | len = slash - name; | |
0f1e4f04 | 1229 | |
b986df5c JH |
1230 | if (cmp_last > 0) { |
1231 | /* | |
1232 | * (len + 1) is a directory boundary (including | |
1233 | * the trailing slash). And since the loop is | |
1234 | * decrementing "slash", the first iteration is | |
1235 | * the longest directory prefix; subsequent | |
1236 | * iterations consider parent directories. | |
1237 | */ | |
1238 | ||
1239 | if (len + 1 <= len_eq_last) { | |
1240 | /* | |
1241 | * The directory prefix (including the trailing | |
1242 | * slash) also appears as a prefix in the last | |
1243 | * entry, so the remainder cannot collide (because | |
1244 | * strcmp said the whole path was greater). | |
1245 | * | |
1246 | * EQ: last: xxx/A | |
1247 | * this: xxx/B | |
1248 | * | |
1249 | * LT: last: xxx/file_A | |
1250 | * this: xxx/file_B | |
1251 | */ | |
1252 | return retval; | |
1253 | } | |
1254 | ||
1255 | if (len > len_eq_last) { | |
1256 | /* | |
1257 | * This part of the directory prefix (excluding | |
1258 | * the trailing slash) is longer than the known | |
1259 | * equal portions, so this sub-directory cannot | |
1260 | * collide with a file. | |
1261 | * | |
1262 | * GT: last: xxxA | |
1263 | * this: xxxB/file | |
1264 | */ | |
1265 | return retval; | |
1266 | } | |
1267 | ||
b986df5c JH |
1268 | /* |
1269 | * This is a possible collision. Fall through and | |
1270 | * let the regular search code handle it. | |
1271 | * | |
1272 | * last: xxx | |
1273 | * this: xxx/file | |
1274 | */ | |
1275 | } | |
1276 | ||
20ec2d03 | 1277 | pos = index_name_stage_pos(istate, name, len, stage, EXPAND_SPARSE); |
12676608 | 1278 | if (pos >= 0) { |
21cd8d00 JH |
1279 | /* |
1280 | * Found one, but not so fast. This could | |
1281 | * be a marker that says "I was here, but | |
1282 | * I am being removed". Such an entry is | |
1283 | * not a part of the resulting tree, and | |
1284 | * it is Ok to have a directory at the same | |
1285 | * path. | |
1286 | */ | |
077c48df | 1287 | if (!(istate->cache[pos]->ce_flags & CE_REMOVE)) { |
21cd8d00 JH |
1288 | retval = -1; |
1289 | if (!ok_to_replace) | |
1290 | break; | |
4aab5b46 | 1291 | remove_index_entry_at(istate, pos); |
21cd8d00 JH |
1292 | continue; |
1293 | } | |
12676608 | 1294 | } |
21cd8d00 JH |
1295 | else |
1296 | pos = -pos-1; | |
12676608 LT |
1297 | |
1298 | /* | |
1299 | * Trivial optimization: if we find an entry that | |
1300 | * already matches the sub-directory, then we know | |
b155725d | 1301 | * we're ok, and we can exit. |
12676608 | 1302 | */ |
4aab5b46 JH |
1303 | while (pos < istate->cache_nr) { |
1304 | struct cache_entry *p = istate->cache[pos]; | |
b155725d JH |
1305 | if ((ce_namelen(p) <= len) || |
1306 | (p->name[len] != '/') || | |
1307 | memcmp(p->name, name, len)) | |
1308 | break; /* not our subdirectory */ | |
077c48df JH |
1309 | if (ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) |
1310 | /* | |
1311 | * p is at the same stage as our entry, and | |
b155725d JH |
1312 | * is a subdirectory of what we are looking |
1313 | * at, so we cannot have conflicts at our | |
1314 | * level or anything shorter. | |
1315 | */ | |
1316 | return retval; | |
1317 | pos++; | |
192268c1 | 1318 | } |
0f1e4f04 | 1319 | } |
12676608 LT |
1320 | return retval; |
1321 | } | |
1322 | ||
1323 | /* We may be in a situation where we already have path/file and path | |
1324 | * is being added, or we already have path and path/file is being | |
1325 | * added. Either one would result in a nonsense tree that has path | |
1326 | * twice when git-write-tree tries to write it out. Prevent it. | |
a6080a0a | 1327 | * |
12676608 LT |
1328 | * If ok-to-replace is specified, we remove the conflicting entries |
1329 | * from the cache so the caller should recompute the insert position. | |
1330 | * When this happens, we return non-zero. | |
1331 | */ | |
4aab5b46 JH |
1332 | static int check_file_directory_conflict(struct index_state *istate, |
1333 | const struct cache_entry *ce, | |
1334 | int pos, int ok_to_replace) | |
12676608 | 1335 | { |
21cd8d00 JH |
1336 | int retval; |
1337 | ||
1338 | /* | |
1339 | * When ce is an "I am going away" entry, we allow it to be added | |
1340 | */ | |
7a51ed66 | 1341 | if (ce->ce_flags & CE_REMOVE) |
21cd8d00 JH |
1342 | return 0; |
1343 | ||
12676608 LT |
1344 | /* |
1345 | * We check if the path is a sub-path of a subsequent pathname | |
1346 | * first, since removing those will not change the position | |
21cd8d00 | 1347 | * in the array. |
12676608 | 1348 | */ |
4aab5b46 | 1349 | retval = has_file_name(istate, ce, pos, ok_to_replace); |
21cd8d00 | 1350 | |
12676608 LT |
1351 | /* |
1352 | * Then check if the path might have a clashing sub-directory | |
1353 | * before it. | |
1354 | */ | |
4aab5b46 | 1355 | return retval + has_dir_name(istate, ce, pos, ok_to_replace); |
0f1e4f04 JH |
1356 | } |
1357 | ||
af3785dc | 1358 | static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option) |
197ee8c9 LT |
1359 | { |
1360 | int pos; | |
192268c1 JH |
1361 | int ok_to_add = option & ADD_CACHE_OK_TO_ADD; |
1362 | int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; | |
b155725d | 1363 | int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; |
39425819 | 1364 | int new_only = option & ADD_CACHE_NEW_ONLY; |
5f73076c | 1365 | |
e5494631 JH |
1366 | /* |
1367 | * If this entry's path sorts after the last entry in the index, | |
1368 | * we can avoid searching for it. | |
1369 | */ | |
1370 | if (istate->cache_nr > 0 && | |
1371 | strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0) | |
c097b95a | 1372 | pos = index_pos_to_insert_pos(istate->cache_nr); |
e5494631 | 1373 | else |
20ec2d03 | 1374 | pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE); |
197ee8c9 | 1375 | |
c35e9f5e VD |
1376 | /* |
1377 | * Cache tree path should be invalidated only after index_name_stage_pos, | |
1378 | * in case it expands a sparse index. | |
1379 | */ | |
1380 | if (!(option & ADD_CACHE_KEEP_CACHE_TREE)) | |
1381 | cache_tree_invalidate_path(istate, ce->name); | |
1382 | ||
3e09cdfd | 1383 | /* existing match? Just replace it. */ |
76e7f4ec | 1384 | if (pos >= 0) { |
39425819 JH |
1385 | if (!new_only) |
1386 | replace_index_entry(istate, pos, ce); | |
197ee8c9 LT |
1387 | return 0; |
1388 | } | |
76e7f4ec | 1389 | pos = -pos-1; |
197ee8c9 | 1390 | |
ffcc9ba7 NTND |
1391 | if (!(option & ADD_CACHE_KEEP_CACHE_TREE)) |
1392 | untracked_cache_add_to_index(istate, ce->name); | |
e931371a | 1393 | |
7b937ca3 LT |
1394 | /* |
1395 | * Inserting a merged entry ("stage 0") into the index | |
1396 | * will always replace all non-merged entries.. | |
1397 | */ | |
4aab5b46 JH |
1398 | if (pos < istate->cache_nr && ce_stage(ce) == 0) { |
1399 | while (ce_same_name(istate->cache[pos], ce)) { | |
7b937ca3 | 1400 | ok_to_add = 1; |
4aab5b46 | 1401 | if (!remove_index_entry_at(istate, pos)) |
7b937ca3 LT |
1402 | break; |
1403 | } | |
1404 | } | |
1405 | ||
121481ab LT |
1406 | if (!ok_to_add) |
1407 | return -1; | |
c8ad9d04 | 1408 | if (verify_path_internal(ce->name, ce->ce_mode) == PATH_INVALID) |
9d0a9e90 | 1409 | return error(_("invalid path '%s'"), ce->name); |
121481ab | 1410 | |
3e09cdfd | 1411 | if (!skip_df_check && |
4aab5b46 | 1412 | check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { |
192268c1 | 1413 | if (!ok_to_replace) |
9d0a9e90 | 1414 | return error(_("'%s' appears as both a file and as a directory"), |
4aab5b46 | 1415 | ce->name); |
20ec2d03 | 1416 | pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE); |
192268c1 JH |
1417 | pos = -pos-1; |
1418 | } | |
af3785dc JH |
1419 | return pos + 1; |
1420 | } | |
1421 | ||
1422 | int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option) | |
1423 | { | |
1424 | int pos; | |
1425 | ||
1426 | if (option & ADD_CACHE_JUST_APPEND) | |
1427 | pos = istate->cache_nr; | |
1428 | else { | |
1429 | int ret; | |
1430 | ret = add_index_entry_with_check(istate, ce, option); | |
1431 | if (ret <= 0) | |
1432 | return ret; | |
1433 | pos = ret - 1; | |
1434 | } | |
0f1e4f04 | 1435 | |
197ee8c9 | 1436 | /* Make sure the array is big enough .. */ |
999f5660 | 1437 | ALLOC_GROW(istate->cache, istate->cache_nr + 1, istate->cache_alloc); |
197ee8c9 LT |
1438 | |
1439 | /* Add it in.. */ | |
4aab5b46 | 1440 | istate->cache_nr++; |
af3785dc | 1441 | if (istate->cache_nr > pos + 1) |
f919ffeb SG |
1442 | MOVE_ARRAY(istate->cache + pos + 1, istate->cache + pos, |
1443 | istate->cache_nr - pos - 1); | |
cf558704 | 1444 | set_index_entry(istate, pos, ce); |
e636a7b4 | 1445 | istate->cache_changed |= CE_ENTRY_ADDED; |
197ee8c9 LT |
1446 | return 0; |
1447 | } | |
1448 | ||
405e5b2f LT |
1449 | /* |
1450 | * "refresh" does not calculate a new sha1 file or bring the | |
1451 | * cache up-to-date for mode/content changes. But what it | |
1452 | * _does_ do is to "re-match" the stat information of a file | |
1453 | * with the cache, so that you can refresh the cache for a | |
1454 | * file that hasn't been changed but where the stat entry is | |
1455 | * out of date. | |
1456 | * | |
1457 | * For example, you'd want to do this after doing a "git-read-tree", | |
1458 | * to link up the stat cache details with the proper files. | |
1459 | */ | |
4aab5b46 | 1460 | static struct cache_entry *refresh_cache_ent(struct index_state *istate, |
4bd5b7da | 1461 | struct cache_entry *ce, |
d05e6970 | 1462 | unsigned int options, int *err, |
a98e0f2d | 1463 | int *changed_ret, |
15268d12 JH |
1464 | int *t2_did_lstat, |
1465 | int *t2_did_scan) | |
405e5b2f LT |
1466 | { |
1467 | struct stat st; | |
1468 | struct cache_entry *updated; | |
a849735b | 1469 | int changed; |
25762726 | 1470 | int refresh = options & CE_MATCH_REFRESH; |
4bd5b7da | 1471 | int ignore_valid = options & CE_MATCH_IGNORE_VALID; |
56cac48c | 1472 | int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; |
2e2e7ec1 | 1473 | int ignore_missing = options & CE_MATCH_IGNORE_MISSING; |
883e248b | 1474 | int ignore_fsmonitor = options & CE_MATCH_IGNORE_FSMONITOR; |
405e5b2f | 1475 | |
25762726 | 1476 | if (!refresh || ce_uptodate(ce)) |
eadb5831 JH |
1477 | return ce; |
1478 | ||
883e248b BP |
1479 | if (!ignore_fsmonitor) |
1480 | refresh_fsmonitor(istate); | |
aa9349d4 | 1481 | /* |
56cac48c NTND |
1482 | * CE_VALID or CE_SKIP_WORKTREE means the user promised us |
1483 | * that the change to the work tree does not matter and told | |
1484 | * us not to worry. | |
aa9349d4 | 1485 | */ |
56cac48c NTND |
1486 | if (!ignore_skip_worktree && ce_skip_worktree(ce)) { |
1487 | ce_mark_uptodate(ce); | |
1488 | return ce; | |
1489 | } | |
aa9349d4 MSO |
1490 | if (!ignore_valid && (ce->ce_flags & CE_VALID)) { |
1491 | ce_mark_uptodate(ce); | |
1492 | return ce; | |
1493 | } | |
883e248b BP |
1494 | if (!ignore_fsmonitor && (ce->ce_flags & CE_FSMONITOR_VALID)) { |
1495 | ce_mark_uptodate(ce); | |
1496 | return ce; | |
1497 | } | |
aa9349d4 | 1498 | |
ccad42d4 RS |
1499 | if (has_symlink_leading_path(ce->name, ce_namelen(ce))) { |
1500 | if (ignore_missing) | |
1501 | return ce; | |
1502 | if (err) | |
1503 | *err = ENOENT; | |
1504 | return NULL; | |
1505 | } | |
1506 | ||
a98e0f2d JH |
1507 | if (t2_did_lstat) |
1508 | *t2_did_lstat = 1; | |
8fd2cb40 | 1509 | if (lstat(ce->name, &st) < 0) { |
2e2e7ec1 BK |
1510 | if (ignore_missing && errno == ENOENT) |
1511 | return ce; | |
ec0cc704 JH |
1512 | if (err) |
1513 | *err = errno; | |
8fd2cb40 JS |
1514 | return NULL; |
1515 | } | |
405e5b2f | 1516 | |
4bd5b7da | 1517 | changed = ie_match_stat(istate, ce, &st, options); |
d05e6970 JK |
1518 | if (changed_ret) |
1519 | *changed_ret = changed; | |
405e5b2f | 1520 | if (!changed) { |
4bd5b7da JH |
1521 | /* |
1522 | * The path is unchanged. If we were told to ignore | |
1523 | * valid bit, then we did the actual stat check and | |
1524 | * found that the entry is unmodified. If the entry | |
1525 | * is not marked VALID, this is the place to mark it | |
1526 | * valid again, under "assume unchanged" mode. | |
1527 | */ | |
1528 | if (ignore_valid && assume_unchanged && | |
7a51ed66 | 1529 | !(ce->ce_flags & CE_VALID)) |
405e5b2f | 1530 | ; /* mark this one VALID again */ |
eadb5831 JH |
1531 | else { |
1532 | /* | |
1533 | * We do not mark the index itself "modified" | |
1534 | * because CE_UPTODATE flag is in-core only; | |
1535 | * we are not going to write this change out. | |
1536 | */ | |
883e248b | 1537 | if (!S_ISGITLINK(ce->ce_mode)) { |
125fd984 | 1538 | ce_mark_uptodate(ce); |
b5a81697 | 1539 | mark_fsmonitor_valid(istate, ce); |
883e248b | 1540 | } |
8fd2cb40 | 1541 | return ce; |
eadb5831 | 1542 | } |
405e5b2f LT |
1543 | } |
1544 | ||
15268d12 JH |
1545 | if (t2_did_scan) |
1546 | *t2_did_scan = 1; | |
4bd5b7da | 1547 | if (ie_modified(istate, ce, &st, options)) { |
ec0cc704 JH |
1548 | if (err) |
1549 | *err = EINVAL; | |
8fd2cb40 JS |
1550 | return NULL; |
1551 | } | |
405e5b2f | 1552 | |
a849735b | 1553 | updated = make_empty_cache_entry(istate, ce_namelen(ce)); |
0e267b7a BP |
1554 | copy_cache_entry(updated, ce); |
1555 | memcpy(updated->name, ce->name, ce->ce_namelen + 1); | |
d4c0a3ac | 1556 | fill_stat_cache_info(istate, updated, &st); |
4bd5b7da JH |
1557 | /* |
1558 | * If ignore_valid is not set, we should leave CE_VALID bit | |
1559 | * alone. Otherwise, paths marked with --no-assume-unchanged | |
1560 | * (i.e. things to be edited) will reacquire CE_VALID bit | |
1561 | * automatically, which is not really what we want. | |
405e5b2f | 1562 | */ |
4bd5b7da | 1563 | if (!ignore_valid && assume_unchanged && |
7a51ed66 LT |
1564 | !(ce->ce_flags & CE_VALID)) |
1565 | updated->ce_flags &= ~CE_VALID; | |
405e5b2f | 1566 | |
e636a7b4 | 1567 | /* istate->cache_changed is updated in the caller */ |
405e5b2f LT |
1568 | return updated; |
1569 | } | |
1570 | ||
3deffc52 | 1571 | static void show_file(const char * fmt, const char * name, int in_porcelain, |
046613c5 | 1572 | int * first, const char *header_msg) |
3deffc52 MM |
1573 | { |
1574 | if (in_porcelain && *first && header_msg) { | |
1575 | printf("%s\n", header_msg); | |
cd2b8ae9 | 1576 | *first = 0; |
3deffc52 MM |
1577 | } |
1578 | printf(fmt, name); | |
1579 | } | |
1580 | ||
22184497 TG |
1581 | int repo_refresh_and_write_index(struct repository *repo, |
1582 | unsigned int refresh_flags, | |
1583 | unsigned int write_flags, | |
1584 | int gentle, | |
1585 | const struct pathspec *pathspec, | |
1586 | char *seen, const char *header_msg) | |
1587 | { | |
1588 | struct lock_file lock_file = LOCK_INIT; | |
1589 | int fd, ret = 0; | |
1590 | ||
1591 | fd = repo_hold_locked_index(repo, &lock_file, 0); | |
1592 | if (!gentle && fd < 0) | |
1593 | return -1; | |
1594 | if (refresh_index(repo->index, refresh_flags, pathspec, seen, header_msg)) | |
1595 | ret = 1; | |
1596 | if (0 <= fd && write_locked_index(repo->index, &lock_file, COMMIT_LOCK | write_flags)) | |
1597 | ret = -1; | |
1598 | return ret; | |
1599 | } | |
1600 | ||
1601 | ||
9b2d6149 NTND |
1602 | int refresh_index(struct index_state *istate, unsigned int flags, |
1603 | const struct pathspec *pathspec, | |
046613c5 | 1604 | char *seen, const char *header_msg) |
405e5b2f LT |
1605 | { |
1606 | int i; | |
1607 | int has_errors = 0; | |
1608 | int really = (flags & REFRESH_REALLY) != 0; | |
1609 | int allow_unmerged = (flags & REFRESH_UNMERGED) != 0; | |
1610 | int quiet = (flags & REFRESH_QUIET) != 0; | |
1611 | int not_new = (flags & REFRESH_IGNORE_MISSING) != 0; | |
5fdeacb0 | 1612 | int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0; |
b243012c | 1613 | int ignore_skip_worktree = (flags & REFRESH_IGNORE_SKIP_WORKTREE) != 0; |
3deffc52 MM |
1614 | int first = 1; |
1615 | int in_porcelain = (flags & REFRESH_IN_PORCELAIN); | |
25762726 BK |
1616 | unsigned int options = (CE_MATCH_REFRESH | |
1617 | (really ? CE_MATCH_IGNORE_VALID : 0) | | |
2e2e7ec1 | 1618 | (not_new ? CE_MATCH_IGNORE_MISSING : 0)); |
4bd4e730 | 1619 | const char *modified_fmt; |
73b7eae6 JK |
1620 | const char *deleted_fmt; |
1621 | const char *typechange_fmt; | |
1622 | const char *added_fmt; | |
4bd4e730 | 1623 | const char *unmerged_fmt; |
ae9af122 | 1624 | struct progress *progress = NULL; |
a98e0f2d | 1625 | int t2_sum_lstat = 0; |
15268d12 | 1626 | int t2_sum_scan = 0; |
ae9af122 NTND |
1627 | |
1628 | if (flags & REFRESH_PROGRESS && isatty(2)) | |
1629 | progress = start_delayed_progress(_("Refresh index"), | |
1630 | istate->cache_nr); | |
405e5b2f | 1631 | |
c46c406a | 1632 | trace_performance_enter(); |
a71806a7 NTND |
1633 | modified_fmt = in_porcelain ? "M\t%s\n" : "%s: needs update\n"; |
1634 | deleted_fmt = in_porcelain ? "D\t%s\n" : "%s: needs update\n"; | |
1635 | typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n"; | |
1636 | added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n"; | |
1637 | unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n"; | |
99ce720c BP |
1638 | /* |
1639 | * Use the multi-threaded preload_index() to refresh most of the | |
1640 | * cache entries quickly then in the single threaded loop below, | |
1641 | * we only have to do the special cases that are left. | |
1642 | */ | |
1643 | preload_index(istate, pathspec, 0); | |
a98e0f2d | 1644 | trace2_region_enter("index", "refresh", NULL); |
d76723ee | 1645 | |
4aab5b46 | 1646 | for (i = 0; i < istate->cache_nr; i++) { |
285c2e25 | 1647 | struct cache_entry *ce, *new_entry; |
ec0cc704 | 1648 | int cache_errno = 0; |
73b7eae6 | 1649 | int changed = 0; |
3d1f148c | 1650 | int filtered = 0; |
a98e0f2d | 1651 | int t2_did_lstat = 0; |
15268d12 | 1652 | int t2_did_scan = 0; |
ec0cc704 | 1653 | |
4aab5b46 | 1654 | ce = istate->cache[i]; |
5fdeacb0 JS |
1655 | if (ignore_submodules && S_ISGITLINK(ce->ce_mode)) |
1656 | continue; | |
b243012c MT |
1657 | if (ignore_skip_worktree && ce_skip_worktree(ce)) |
1658 | continue; | |
5fdeacb0 | 1659 | |
d76723ee DS |
1660 | /* |
1661 | * If this entry is a sparse directory, then there isn't | |
1662 | * any stat() information to update. Ignore the entry. | |
1663 | */ | |
1664 | if (S_ISSPARSEDIR(ce->ce_mode)) | |
1665 | continue; | |
1666 | ||
d7b665c3 | 1667 | if (pathspec && !ce_path_match(istate, ce, pathspec, seen)) |
3d1f148c JH |
1668 | filtered = 1; |
1669 | ||
405e5b2f | 1670 | if (ce_stage(ce)) { |
4aab5b46 JH |
1671 | while ((i < istate->cache_nr) && |
1672 | ! strcmp(istate->cache[i]->name, ce->name)) | |
405e5b2f LT |
1673 | i++; |
1674 | i--; | |
1675 | if (allow_unmerged) | |
1676 | continue; | |
3d1f148c JH |
1677 | if (!filtered) |
1678 | show_file(unmerged_fmt, ce->name, in_porcelain, | |
1679 | &first, header_msg); | |
405e5b2f LT |
1680 | has_errors = 1; |
1681 | continue; | |
1682 | } | |
1683 | ||
3d1f148c | 1684 | if (filtered) |
d616813d AJ |
1685 | continue; |
1686 | ||
a98e0f2d JH |
1687 | new_entry = refresh_cache_ent(istate, ce, options, |
1688 | &cache_errno, &changed, | |
15268d12 | 1689 | &t2_did_lstat, &t2_did_scan); |
a98e0f2d | 1690 | t2_sum_lstat += t2_did_lstat; |
15268d12 | 1691 | t2_sum_scan += t2_did_scan; |
285c2e25 | 1692 | if (new_entry == ce) |
405e5b2f | 1693 | continue; |
b7b793d1 | 1694 | display_progress(progress, i); |
285c2e25 | 1695 | if (!new_entry) { |
73b7eae6 JK |
1696 | const char *fmt; |
1697 | ||
8fd2cb40 | 1698 | if (really && cache_errno == EINVAL) { |
405e5b2f LT |
1699 | /* If we are doing --really-refresh that |
1700 | * means the index is not valid anymore. | |
1701 | */ | |
7a51ed66 | 1702 | ce->ce_flags &= ~CE_VALID; |
078a58e8 | 1703 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
883e248b | 1704 | mark_fsmonitor_invalid(istate, ce); |
e636a7b4 | 1705 | istate->cache_changed |= CE_ENTRY_CHANGED; |
405e5b2f LT |
1706 | } |
1707 | if (quiet) | |
1708 | continue; | |
73b7eae6 JK |
1709 | |
1710 | if (cache_errno == ENOENT) | |
1711 | fmt = deleted_fmt; | |
895ff3b2 | 1712 | else if (ce_intent_to_add(ce)) |
73b7eae6 JK |
1713 | fmt = added_fmt; /* must be before other checks */ |
1714 | else if (changed & TYPE_CHANGED) | |
1715 | fmt = typechange_fmt; | |
1716 | else | |
1717 | fmt = modified_fmt; | |
1718 | show_file(fmt, | |
1719 | ce->name, in_porcelain, &first, header_msg); | |
405e5b2f LT |
1720 | has_errors = 1; |
1721 | continue; | |
1722 | } | |
cf558704 | 1723 | |
285c2e25 | 1724 | replace_index_entry(istate, i, new_entry); |
405e5b2f | 1725 | } |
a98e0f2d | 1726 | trace2_data_intmax("index", NULL, "refresh/sum_lstat", t2_sum_lstat); |
15268d12 | 1727 | trace2_data_intmax("index", NULL, "refresh/sum_scan", t2_sum_scan); |
a98e0f2d | 1728 | trace2_region_leave("index", "refresh", NULL); |
b7b793d1 ÆAB |
1729 | display_progress(progress, istate->cache_nr); |
1730 | stop_progress(&progress); | |
c46c406a | 1731 | trace_performance_leave("refresh index"); |
405e5b2f LT |
1732 | return has_errors; |
1733 | } | |
1734 | ||
768d7965 JM |
1735 | struct cache_entry *refresh_cache_entry(struct index_state *istate, |
1736 | struct cache_entry *ce, | |
1737 | unsigned int options) | |
ec0cc704 | 1738 | { |
15268d12 | 1739 | return refresh_cache_ent(istate, ce, options, NULL, NULL, NULL, NULL); |
ec0cc704 JH |
1740 | } |
1741 | ||
db3b313c JH |
1742 | |
1743 | /***************************************************************** | |
1744 | * Index File I/O | |
1745 | *****************************************************************/ | |
1746 | ||
9d227781 JH |
1747 | #define INDEX_FORMAT_DEFAULT 3 |
1748 | ||
7211b9e7 | 1749 | static unsigned int get_index_format_default(struct repository *r) |
136347d7 TG |
1750 | { |
1751 | char *envversion = getenv("GIT_INDEX_VERSION"); | |
3c09d684 TG |
1752 | char *endp; |
1753 | unsigned int version = INDEX_FORMAT_DEFAULT; | |
1754 | ||
136347d7 | 1755 | if (!envversion) { |
7211b9e7 DS |
1756 | prepare_repo_settings(r); |
1757 | ||
1758 | if (r->settings.index_version >= 0) | |
1759 | version = r->settings.index_version; | |
3c09d684 TG |
1760 | if (version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) { |
1761 | warning(_("index.version set, but the value is invalid.\n" | |
136347d7 | 1762 | "Using version %i"), INDEX_FORMAT_DEFAULT); |
3c09d684 | 1763 | return INDEX_FORMAT_DEFAULT; |
136347d7 TG |
1764 | } |
1765 | return version; | |
1766 | } | |
3c09d684 TG |
1767 | |
1768 | version = strtoul(envversion, &endp, 10); | |
1769 | if (*endp || | |
1770 | version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) { | |
1771 | warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n" | |
1772 | "Using version %i"), INDEX_FORMAT_DEFAULT); | |
1773 | version = INDEX_FORMAT_DEFAULT; | |
1774 | } | |
1775 | return version; | |
136347d7 TG |
1776 | } |
1777 | ||
db3b313c JH |
1778 | /* |
1779 | * dev/ino/uid/gid/size are also just tracked to the low 32 bits | |
1780 | * Again - this is just a (very strong in practice) heuristic that | |
1781 | * the inode hasn't changed. | |
1782 | * | |
1783 | * We save the fields in big-endian order to allow using the | |
1784 | * index file over NFS transparently. | |
1785 | */ | |
1786 | struct ondisk_cache_entry { | |
1787 | struct cache_time ctime; | |
1788 | struct cache_time mtime; | |
7800c1eb TG |
1789 | uint32_t dev; |
1790 | uint32_t ino; | |
1791 | uint32_t mode; | |
1792 | uint32_t uid; | |
1793 | uint32_t gid; | |
1794 | uint32_t size; | |
575fa8a3 | 1795 | /* |
1796 | * unsigned char hash[hashsz]; | |
1797 | * uint16_t flags; | |
1798 | * if (flags & CE_EXTENDED) | |
1799 | * uint16_t flags2; | |
1800 | */ | |
1801 | unsigned char data[GIT_MAX_RAWSZ + 2 * sizeof(uint16_t)]; | |
1802 | char name[FLEX_ARRAY]; | |
db3b313c JH |
1803 | }; |
1804 | ||
6c9cd161 | 1805 | /* These are only used for v3 or lower */ |
ce012deb | 1806 | #define align_padding_size(size, len) ((size + (len) + 8) & ~7) - (size + len) |
575fa8a3 | 1807 | #define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,data) + (len) + 8) & ~7) |
db3b313c | 1808 | #define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len) |
575fa8a3 | 1809 | #define ondisk_data_size(flags, len) (the_hash_algo->rawsz + \ |
1810 | ((flags & CE_EXTENDED) ? 2 : 1) * sizeof(uint16_t) + len) | |
1811 | #define ondisk_data_size_max(len) (ondisk_data_size(CE_EXTENDED, len)) | |
1812 | #define ondisk_ce_size(ce) (ondisk_cache_entry_size(ondisk_data_size((ce)->ce_flags, ce_namelen(ce)))) | |
db3b313c | 1813 | |
a33fc72f JH |
1814 | /* Allow fsck to force verification of the index checksum. */ |
1815 | int verify_index_checksum; | |
1816 | ||
00ec50e5 BP |
1817 | /* Allow fsck to force verification of the cache entry order. */ |
1818 | int verify_ce_order; | |
1819 | ||
371ed0de | 1820 | static int verify_hdr(const struct cache_header *hdr, unsigned long size) |
e83c5163 | 1821 | { |
aab61359 | 1822 | git_hash_ctx c; |
1823 | unsigned char hash[GIT_MAX_RAWSZ]; | |
0136bac9 | 1824 | int hdr_version; |
ee1f0c24 DS |
1825 | unsigned char *start, *end; |
1826 | struct object_id oid; | |
e83c5163 | 1827 | |
ccc4feb5 | 1828 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
9d0a9e90 | 1829 | return error(_("bad signature 0x%08x"), hdr->hdr_signature); |
0136bac9 | 1830 | hdr_version = ntohl(hdr->hdr_version); |
b82a7b5b | 1831 | if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version) |
9d0a9e90 | 1832 | return error(_("bad index version %d"), hdr_version); |
a33fc72f JH |
1833 | |
1834 | if (!verify_index_checksum) | |
1835 | return 0; | |
1836 | ||
ee1f0c24 DS |
1837 | end = (unsigned char *)hdr + size; |
1838 | start = end - the_hash_algo->rawsz; | |
1839 | oidread(&oid, start); | |
1840 | if (oideq(&oid, null_oid())) | |
1841 | return 0; | |
1842 | ||
aab61359 | 1843 | the_hash_algo->init_fn(&c); |
1844 | the_hash_algo->update_fn(&c, hdr, size - the_hash_algo->rawsz); | |
1845 | the_hash_algo->final_fn(hash, &c); | |
ee1f0c24 | 1846 | if (!hasheq(hash, start)) |
9d0a9e90 | 1847 | return error(_("bad index file sha1 signature")); |
e83c5163 LT |
1848 | return 0; |
1849 | } | |
1850 | ||
4aab5b46 | 1851 | static int read_index_extension(struct index_state *istate, |
371ed0de | 1852 | const char *ext, const char *data, unsigned long sz) |
bad68ec9 JH |
1853 | { |
1854 | switch (CACHE_EXT(ext)) { | |
1855 | case CACHE_EXT_TREE: | |
4aab5b46 | 1856 | istate->cache_tree = cache_tree_read(data, sz); |
bad68ec9 | 1857 | break; |
cfc5789a JH |
1858 | case CACHE_EXT_RESOLVE_UNDO: |
1859 | istate->resolve_undo = resolve_undo_read(data, sz); | |
1860 | break; | |
5fc2fc8f NTND |
1861 | case CACHE_EXT_LINK: |
1862 | if (read_link_extension(istate, data, sz)) | |
1863 | return -1; | |
1864 | break; | |
f9e6c649 NTND |
1865 | case CACHE_EXT_UNTRACKED: |
1866 | istate->untracked = read_untracked_extension(data, sz); | |
1867 | break; | |
883e248b BP |
1868 | case CACHE_EXT_FSMONITOR: |
1869 | read_fsmonitor_extension(istate, data, sz); | |
1870 | break; | |
3b1d9e04 | 1871 | case CACHE_EXT_ENDOFINDEXENTRIES: |
3255089a | 1872 | case CACHE_EXT_INDEXENTRYOFFSETTABLE: |
3b1d9e04 BP |
1873 | /* already handled in do_read_index() */ |
1874 | break; | |
cd42415f DS |
1875 | case CACHE_EXT_SPARSE_DIRECTORIES: |
1876 | /* no content, only an indicator */ | |
9fadb373 | 1877 | istate->sparse_index = INDEX_COLLAPSED; |
cd42415f | 1878 | break; |
bad68ec9 JH |
1879 | default: |
1880 | if (*ext < 'A' || 'Z' < *ext) | |
9d0a9e90 | 1881 | return error(_("index uses %.4s extension, which we do not understand"), |
bad68ec9 | 1882 | ext); |
9d0a9e90 | 1883 | fprintf_ln(stderr, _("ignoring %.4s extension"), ext); |
bad68ec9 JH |
1884 | break; |
1885 | } | |
1886 | return 0; | |
1887 | } | |
1888 | ||
4a6ed30f VD |
1889 | /* |
1890 | * Parses the contents of the cache entry contained within the 'ondisk' buffer | |
1891 | * into a new incore 'cache_entry'. | |
1892 | * | |
1893 | * Note that 'char *ondisk' may not be aligned to a 4-byte address interval in | |
1894 | * index v4, so we cannot cast it to 'struct ondisk_cache_entry *' and access | |
1895 | * its members. Instead, we use the byte offsets of members within the struct to | |
1896 | * identify where 'get_be16()', 'get_be32()', and 'oidread()' (which can all | |
1897 | * read from an unaligned memory buffer) should read from the 'ondisk' buffer | |
1898 | * into the corresponding incore 'cache_entry' members. | |
1899 | */ | |
77ff1127 BP |
1900 | static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool, |
1901 | unsigned int version, | |
4a6ed30f | 1902 | const char *ondisk, |
6c9cd161 | 1903 | unsigned long *ent_size, |
252d079c | 1904 | const struct cache_entry *previous_ce) |
7a51ed66 | 1905 | { |
debed2a6 | 1906 | struct cache_entry *ce; |
7fec10b7 | 1907 | size_t len; |
06aaaa0b | 1908 | const char *name; |
575fa8a3 | 1909 | const unsigned hashsz = the_hash_algo->rawsz; |
4a6ed30f | 1910 | const char *flagsp = ondisk + offsetof(struct ondisk_cache_entry, data) + hashsz; |
debed2a6 | 1911 | unsigned int flags; |
f5c4a9af | 1912 | size_t copy_len = 0; |
252d079c NTND |
1913 | /* |
1914 | * Adjacent cache entries tend to share the leading paths, so it makes | |
1915 | * sense to only store the differences in later entries. In the v4 | |
1916 | * on-disk format of the index, each on-disk cache entry stores the | |
1917 | * number of bytes to be stripped from the end of the previous name, | |
1918 | * and the bytes to append to the result, to come up with its name. | |
1919 | */ | |
77ff1127 | 1920 | int expand_name_field = version == 4; |
7fec10b7 | 1921 | |
7a51ed66 | 1922 | /* On-disk flags are just 16 bits */ |
575fa8a3 | 1923 | flags = get_be16(flagsp); |
debed2a6 | 1924 | len = flags & CE_NAMEMASK; |
7fec10b7 | 1925 | |
debed2a6 | 1926 | if (flags & CE_EXTENDED) { |
06aaaa0b | 1927 | int extended_flags; |
4a6ed30f | 1928 | extended_flags = get_be16(flagsp + sizeof(uint16_t)) << 16; |
06aaaa0b NTND |
1929 | /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */ |
1930 | if (extended_flags & ~CE_EXTENDED_FLAGS) | |
9d0a9e90 | 1931 | die(_("unknown index entry format 0x%08x"), extended_flags); |
debed2a6 | 1932 | flags |= extended_flags; |
4a6ed30f | 1933 | name = (const char *)(flagsp + 2 * sizeof(uint16_t)); |
06aaaa0b NTND |
1934 | } |
1935 | else | |
4a6ed30f | 1936 | name = (const char *)(flagsp + sizeof(uint16_t)); |
06aaaa0b | 1937 | |
252d079c NTND |
1938 | if (expand_name_field) { |
1939 | const unsigned char *cp = (const unsigned char *)name; | |
1940 | size_t strip_len, previous_len; | |
6c9cd161 | 1941 | |
15beaaa3 | 1942 | /* If we're at the beginning of a block, ignore the previous name */ |
252d079c | 1943 | strip_len = decode_varint(&cp); |
77ff1127 BP |
1944 | if (previous_ce) { |
1945 | previous_len = previous_ce->ce_namelen; | |
1946 | if (previous_len < strip_len) | |
252d079c | 1947 | die(_("malformed name field in the index, near path '%s'"), |
77ff1127 BP |
1948 | previous_ce->name); |
1949 | copy_len = previous_len - strip_len; | |
252d079c | 1950 | } |
252d079c NTND |
1951 | name = (const char *)cp; |
1952 | } | |
6c9cd161 | 1953 | |
252d079c NTND |
1954 | if (len == CE_NAMEMASK) { |
1955 | len = strlen(name); | |
1956 | if (expand_name_field) | |
1957 | len += copy_len; | |
1958 | } | |
6c9cd161 | 1959 | |
77ff1127 | 1960 | ce = mem_pool__ce_alloc(ce_mem_pool, len); |
252d079c | 1961 | |
4a6ed30f VD |
1962 | /* |
1963 | * NEEDSWORK: using 'offsetof()' is cumbersome and should be replaced | |
1964 | * with something more akin to 'load_bitmap_entries_v1()'s use of | |
1965 | * 'read_be16'/'read_be32'. For consistency with the corresponding | |
1966 | * ondisk entry write function ('copy_cache_entry_to_ondisk()'), this | |
1967 | * should be done at the same time as removing references to | |
1968 | * 'ondisk_cache_entry' there. | |
1969 | */ | |
1970 | ce->ce_stat_data.sd_ctime.sec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, ctime) | |
1971 | + offsetof(struct cache_time, sec)); | |
1972 | ce->ce_stat_data.sd_mtime.sec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, mtime) | |
1973 | + offsetof(struct cache_time, sec)); | |
1974 | ce->ce_stat_data.sd_ctime.nsec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, ctime) | |
1975 | + offsetof(struct cache_time, nsec)); | |
1976 | ce->ce_stat_data.sd_mtime.nsec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, mtime) | |
1977 | + offsetof(struct cache_time, nsec)); | |
1978 | ce->ce_stat_data.sd_dev = get_be32(ondisk + offsetof(struct ondisk_cache_entry, dev)); | |
1979 | ce->ce_stat_data.sd_ino = get_be32(ondisk + offsetof(struct ondisk_cache_entry, ino)); | |
1980 | ce->ce_mode = get_be32(ondisk + offsetof(struct ondisk_cache_entry, mode)); | |
1981 | ce->ce_stat_data.sd_uid = get_be32(ondisk + offsetof(struct ondisk_cache_entry, uid)); | |
1982 | ce->ce_stat_data.sd_gid = get_be32(ondisk + offsetof(struct ondisk_cache_entry, gid)); | |
1983 | ce->ce_stat_data.sd_size = get_be32(ondisk + offsetof(struct ondisk_cache_entry, size)); | |
252d079c NTND |
1984 | ce->ce_flags = flags & ~CE_NAMEMASK; |
1985 | ce->ce_namelen = len; | |
1986 | ce->index = 0; | |
4a6ed30f | 1987 | oidread(&ce->oid, (const unsigned char *)ondisk + offsetof(struct ondisk_cache_entry, data)); |
6c9cd161 | 1988 | |
252d079c NTND |
1989 | if (expand_name_field) { |
1990 | if (copy_len) | |
1991 | memcpy(ce->name, previous_ce->name, copy_len); | |
1992 | memcpy(ce->name + copy_len, name, len + 1 - copy_len); | |
1993 | *ent_size = (name - ((char *)ondisk)) + len + 1 - copy_len; | |
1994 | } else { | |
1995 | memcpy(ce->name, name, len + 1); | |
1996 | *ent_size = ondisk_ce_size(ce); | |
6c9cd161 | 1997 | } |
debed2a6 | 1998 | return ce; |
cf558704 LT |
1999 | } |
2000 | ||
03f15a79 | 2001 | static void check_ce_order(struct index_state *istate) |
15999d0b | 2002 | { |
03f15a79 TG |
2003 | unsigned int i; |
2004 | ||
00ec50e5 BP |
2005 | if (!verify_ce_order) |
2006 | return; | |
2007 | ||
03f15a79 TG |
2008 | for (i = 1; i < istate->cache_nr; i++) { |
2009 | struct cache_entry *ce = istate->cache[i - 1]; | |
2010 | struct cache_entry *next_ce = istate->cache[i]; | |
2011 | int name_compare = strcmp(ce->name, next_ce->name); | |
2012 | ||
2013 | if (0 < name_compare) | |
9d0a9e90 | 2014 | die(_("unordered stage entries in index")); |
03f15a79 TG |
2015 | if (!name_compare) { |
2016 | if (!ce_stage(ce)) | |
9d0a9e90 | 2017 | die(_("multiple stage entries for merged file '%s'"), |
03f15a79 TG |
2018 | ce->name); |
2019 | if (ce_stage(ce) > ce_stage(next_ce)) | |
9d0a9e90 | 2020 | die(_("unordered stage entries for '%s'"), |
03f15a79 TG |
2021 | ce->name); |
2022 | } | |
15999d0b JSP |
2023 | } |
2024 | } | |
2025 | ||
435ec090 CC |
2026 | static void tweak_untracked_cache(struct index_state *istate) |
2027 | { | |
ad0fb659 DS |
2028 | struct repository *r = the_repository; |
2029 | ||
2030 | prepare_repo_settings(r); | |
2031 | ||
f1bee828 ÆAB |
2032 | switch (r->settings.core_untracked_cache) { |
2033 | case UNTRACKED_CACHE_REMOVE: | |
435ec090 | 2034 | remove_untracked_cache(istate); |
f1bee828 ÆAB |
2035 | break; |
2036 | case UNTRACKED_CACHE_WRITE: | |
ad0fb659 | 2037 | add_untracked_cache(istate); |
f1bee828 ÆAB |
2038 | break; |
2039 | case UNTRACKED_CACHE_KEEP: | |
3050b6df ÆAB |
2040 | /* |
2041 | * Either an explicit "core.untrackedCache=keep", the | |
2042 | * default if "core.untrackedCache" isn't configured, | |
2043 | * or a fallback on an unknown "core.untrackedCache" | |
2044 | * value. | |
2045 | */ | |
f1bee828 | 2046 | break; |
f1bee828 | 2047 | } |
435ec090 CC |
2048 | } |
2049 | ||
43925312 CC |
2050 | static void tweak_split_index(struct index_state *istate) |
2051 | { | |
2052 | switch (git_config_get_split_index()) { | |
2053 | case -1: /* unset: do nothing */ | |
2054 | break; | |
2055 | case 0: /* false */ | |
2056 | remove_split_index(istate); | |
2057 | break; | |
2058 | case 1: /* true */ | |
2059 | add_split_index(istate); | |
2060 | break; | |
2061 | default: /* unknown value: do nothing */ | |
2062 | break; | |
2063 | } | |
2064 | } | |
2065 | ||
435ec090 CC |
2066 | static void post_read_index_from(struct index_state *istate) |
2067 | { | |
2068 | check_ce_order(istate); | |
2069 | tweak_untracked_cache(istate); | |
43925312 | 2070 | tweak_split_index(istate); |
883e248b | 2071 | tweak_fsmonitor(istate); |
435ec090 CC |
2072 | } |
2073 | ||
8e72d675 JM |
2074 | static size_t estimate_cache_size_from_compressed(unsigned int entries) |
2075 | { | |
2076 | return entries * (sizeof(struct cache_entry) + CACHE_ENTRY_PATH_LENGTH); | |
2077 | } | |
2078 | ||
2079 | static size_t estimate_cache_size(size_t ondisk_size, unsigned int entries) | |
2080 | { | |
2081 | long per_entry = sizeof(struct cache_entry) - sizeof(struct ondisk_cache_entry); | |
2082 | ||
2083 | /* | |
2084 | * Account for potential alignment differences. | |
2085 | */ | |
c097b95a | 2086 | per_entry += align_padding_size(per_entry, 0); |
8e72d675 JM |
2087 | return ondisk_size + entries * per_entry; |
2088 | } | |
2089 | ||
3255089a BP |
2090 | struct index_entry_offset |
2091 | { | |
2092 | /* starting byte offset into index file, count of index entries in this block */ | |
2093 | int offset, nr; | |
2094 | }; | |
2095 | ||
2096 | struct index_entry_offset_table | |
2097 | { | |
2098 | int nr; | |
2099 | struct index_entry_offset entries[FLEX_ARRAY]; | |
2100 | }; | |
2101 | ||
3255089a BP |
2102 | static struct index_entry_offset_table *read_ieot_extension(const char *mmap, size_t mmap_size, size_t offset); |
2103 | static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot); | |
3255089a | 2104 | |
3b1d9e04 BP |
2105 | static size_t read_eoie_extension(const char *mmap, size_t mmap_size); |
2106 | static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset); | |
2107 | ||
abb4bb83 BP |
2108 | struct load_index_extensions |
2109 | { | |
abb4bb83 | 2110 | pthread_t pthread; |
abb4bb83 BP |
2111 | struct index_state *istate; |
2112 | const char *mmap; | |
2113 | size_t mmap_size; | |
2114 | unsigned long src_offset; | |
2115 | }; | |
2116 | ||
2117 | static void *load_index_extensions(void *_data) | |
2118 | { | |
2119 | struct load_index_extensions *p = _data; | |
2120 | unsigned long src_offset = p->src_offset; | |
2121 | ||
2122 | while (src_offset <= p->mmap_size - the_hash_algo->rawsz - 8) { | |
2123 | /* After an array of active_nr index entries, | |
2124 | * there can be arbitrary number of extended | |
2125 | * sections, each of which is prefixed with | |
2126 | * extension name (4-byte) and section length | |
2127 | * in 4-byte network byte order. | |
2128 | */ | |
2129 | uint32_t extsize = get_be32(p->mmap + src_offset + 4); | |
2130 | if (read_index_extension(p->istate, | |
2131 | p->mmap + src_offset, | |
2132 | p->mmap + src_offset + 8, | |
2133 | extsize) < 0) { | |
2134 | munmap((void *)p->mmap, p->mmap_size); | |
2135 | die(_("index file corrupt")); | |
2136 | } | |
2137 | src_offset += 8; | |
2138 | src_offset += extsize; | |
2139 | } | |
2140 | ||
2141 | return NULL; | |
2142 | } | |
2143 | ||
77ff1127 BP |
2144 | /* |
2145 | * A helper function that will load the specified range of cache entries | |
2146 | * from the memory mapped file and add them to the given index. | |
2147 | */ | |
2148 | static unsigned long load_cache_entry_block(struct index_state *istate, | |
2149 | struct mem_pool *ce_mem_pool, int offset, int nr, const char *mmap, | |
2150 | unsigned long start_offset, const struct cache_entry *previous_ce) | |
2151 | { | |
2152 | int i; | |
2153 | unsigned long src_offset = start_offset; | |
2154 | ||
2155 | for (i = offset; i < offset + nr; i++) { | |
77ff1127 BP |
2156 | struct cache_entry *ce; |
2157 | unsigned long consumed; | |
2158 | ||
4a6ed30f VD |
2159 | ce = create_from_disk(ce_mem_pool, istate->version, |
2160 | mmap + src_offset, | |
2161 | &consumed, previous_ce); | |
77ff1127 BP |
2162 | set_index_entry(istate, i, ce); |
2163 | ||
2164 | src_offset += consumed; | |
2165 | previous_ce = ce; | |
2166 | } | |
2167 | return src_offset - start_offset; | |
2168 | } | |
2169 | ||
2170 | static unsigned long load_all_cache_entries(struct index_state *istate, | |
2171 | const char *mmap, size_t mmap_size, unsigned long src_offset) | |
2172 | { | |
2173 | unsigned long consumed; | |
2174 | ||
44c7e1a7 | 2175 | istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool)); |
77ff1127 | 2176 | if (istate->version == 4) { |
44c7e1a7 | 2177 | mem_pool_init(istate->ce_mem_pool, |
77ff1127 BP |
2178 | estimate_cache_size_from_compressed(istate->cache_nr)); |
2179 | } else { | |
44c7e1a7 | 2180 | mem_pool_init(istate->ce_mem_pool, |
77ff1127 BP |
2181 | estimate_cache_size(mmap_size, istate->cache_nr)); |
2182 | } | |
2183 | ||
2184 | consumed = load_cache_entry_block(istate, istate->ce_mem_pool, | |
2185 | 0, istate->cache_nr, mmap, src_offset, NULL); | |
2186 | return consumed; | |
2187 | } | |
2188 | ||
3255089a BP |
2189 | /* |
2190 | * Mostly randomly chosen maximum thread counts: we | |
2191 | * cap the parallelism to online_cpus() threads, and we want | |
2192 | * to have at least 10000 cache entries per thread for it to | |
2193 | * be worth starting a thread. | |
2194 | */ | |
2195 | ||
2196 | #define THREAD_COST (10000) | |
2197 | ||
77ff1127 BP |
2198 | struct load_cache_entries_thread_data |
2199 | { | |
2200 | pthread_t pthread; | |
2201 | struct index_state *istate; | |
2202 | struct mem_pool *ce_mem_pool; | |
2203 | int offset; | |
2204 | const char *mmap; | |
2205 | struct index_entry_offset_table *ieot; | |
2206 | int ieot_start; /* starting index into the ieot array */ | |
2207 | int ieot_blocks; /* count of ieot entries to process */ | |
2208 | unsigned long consumed; /* return # of bytes in index file processed */ | |
2209 | }; | |
2210 | ||
2211 | /* | |
2212 | * A thread proc to run the load_cache_entries() computation | |
2213 | * across multiple background threads. | |
2214 | */ | |
2215 | static void *load_cache_entries_thread(void *_data) | |
2216 | { | |
2217 | struct load_cache_entries_thread_data *p = _data; | |
2218 | int i; | |
2219 | ||
2220 | /* iterate across all ieot blocks assigned to this thread */ | |
2221 | for (i = p->ieot_start; i < p->ieot_start + p->ieot_blocks; i++) { | |
2222 | p->consumed += load_cache_entry_block(p->istate, p->ce_mem_pool, | |
2223 | p->offset, p->ieot->entries[i].nr, p->mmap, p->ieot->entries[i].offset, NULL); | |
2224 | p->offset += p->ieot->entries[i].nr; | |
2225 | } | |
2226 | return NULL; | |
2227 | } | |
2228 | ||
2229 | static unsigned long load_cache_entries_threaded(struct index_state *istate, const char *mmap, size_t mmap_size, | |
7bd9631b | 2230 | int nr_threads, struct index_entry_offset_table *ieot) |
77ff1127 BP |
2231 | { |
2232 | int i, offset, ieot_blocks, ieot_start, err; | |
2233 | struct load_cache_entries_thread_data *data; | |
2234 | unsigned long consumed = 0; | |
2235 | ||
2236 | /* a little sanity checking */ | |
2237 | if (istate->name_hash_initialized) | |
2238 | BUG("the name hash isn't thread safe"); | |
2239 | ||
44c7e1a7 EN |
2240 | istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool)); |
2241 | mem_pool_init(istate->ce_mem_pool, 0); | |
77ff1127 BP |
2242 | |
2243 | /* ensure we have no more threads than we have blocks to process */ | |
2244 | if (nr_threads > ieot->nr) | |
2245 | nr_threads = ieot->nr; | |
ca56dadb | 2246 | CALLOC_ARRAY(data, nr_threads); |
77ff1127 BP |
2247 | |
2248 | offset = ieot_start = 0; | |
2249 | ieot_blocks = DIV_ROUND_UP(ieot->nr, nr_threads); | |
2250 | for (i = 0; i < nr_threads; i++) { | |
2251 | struct load_cache_entries_thread_data *p = &data[i]; | |
2252 | int nr, j; | |
2253 | ||
2254 | if (ieot_start + ieot_blocks > ieot->nr) | |
2255 | ieot_blocks = ieot->nr - ieot_start; | |
2256 | ||
2257 | p->istate = istate; | |
2258 | p->offset = offset; | |
2259 | p->mmap = mmap; | |
2260 | p->ieot = ieot; | |
2261 | p->ieot_start = ieot_start; | |
2262 | p->ieot_blocks = ieot_blocks; | |
2263 | ||
2264 | /* create a mem_pool for each thread */ | |
2265 | nr = 0; | |
2266 | for (j = p->ieot_start; j < p->ieot_start + p->ieot_blocks; j++) | |
2267 | nr += p->ieot->entries[j].nr; | |
bcd2c5ee | 2268 | p->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool)); |
77ff1127 | 2269 | if (istate->version == 4) { |
44c7e1a7 | 2270 | mem_pool_init(p->ce_mem_pool, |
77ff1127 BP |
2271 | estimate_cache_size_from_compressed(nr)); |
2272 | } else { | |
44c7e1a7 | 2273 | mem_pool_init(p->ce_mem_pool, |
77ff1127 BP |
2274 | estimate_cache_size(mmap_size, nr)); |
2275 | } | |
2276 | ||
2277 | err = pthread_create(&p->pthread, NULL, load_cache_entries_thread, p); | |
2278 | if (err) | |
2279 | die(_("unable to create load_cache_entries thread: %s"), strerror(err)); | |
2280 | ||
2281 | /* increment by the number of cache entries in the ieot block being processed */ | |
2282 | for (j = 0; j < ieot_blocks; j++) | |
2283 | offset += ieot->entries[ieot_start + j].nr; | |
2284 | ieot_start += ieot_blocks; | |
2285 | } | |
2286 | ||
2287 | for (i = 0; i < nr_threads; i++) { | |
2288 | struct load_cache_entries_thread_data *p = &data[i]; | |
2289 | ||
2290 | err = pthread_join(p->pthread, NULL); | |
2291 | if (err) | |
2292 | die(_("unable to join load_cache_entries thread: %s"), strerror(err)); | |
2293 | mem_pool_combine(istate->ce_mem_pool, p->ce_mem_pool); | |
2294 | consumed += p->consumed; | |
2295 | } | |
2296 | ||
2297 | free(data); | |
2298 | ||
2299 | return consumed; | |
2300 | } | |
77ff1127 | 2301 | |
491df5f6 VD |
2302 | static void set_new_index_sparsity(struct index_state *istate) |
2303 | { | |
2304 | /* | |
2305 | * If the index's repo exists, mark it sparse according to | |
2306 | * repo settings. | |
2307 | */ | |
5bdf6d4a ÆAB |
2308 | prepare_repo_settings(istate->repo); |
2309 | if (!istate->repo->settings.command_requires_full_index && | |
2310 | is_sparse_index_allowed(istate, 0)) | |
2311 | istate->sparse_index = 1; | |
491df5f6 VD |
2312 | } |
2313 | ||
8fd2cb40 | 2314 | /* remember to discard_cache() before reading a different cache! */ |
3e52f70b | 2315 | int do_read_index(struct index_state *istate, const char *path, int must_exist) |
e83c5163 | 2316 | { |
77ff1127 | 2317 | int fd; |
e83c5163 | 2318 | struct stat st; |
debed2a6 | 2319 | unsigned long src_offset; |
371ed0de BP |
2320 | const struct cache_header *hdr; |
2321 | const char *mmap; | |
7a51ed66 | 2322 | size_t mmap_size; |
abb4bb83 BP |
2323 | struct load_index_extensions p; |
2324 | size_t extension_offset = 0; | |
77ff1127 BP |
2325 | int nr_threads, cpus; |
2326 | struct index_entry_offset_table *ieot = NULL; | |
e83c5163 | 2327 | |
913e0e99 | 2328 | if (istate->initialized) |
4aab5b46 | 2329 | return istate->cache_nr; |
5d1a5c02 | 2330 | |
fba2f38a KB |
2331 | istate->timestamp.sec = 0; |
2332 | istate->timestamp.nsec = 0; | |
8fd2cb40 | 2333 | fd = open(path, O_RDONLY); |
5d1a5c02 | 2334 | if (fd < 0) { |
491df5f6 VD |
2335 | if (!must_exist && errno == ENOENT) { |
2336 | set_new_index_sparsity(istate); | |
5d1a5c02 | 2337 | return 0; |
491df5f6 | 2338 | } |
9d0a9e90 | 2339 | die_errno(_("%s: index file open failed"), path); |
5d1a5c02 | 2340 | } |
e83c5163 | 2341 | |
3511a377 | 2342 | if (fstat(fd, &st)) |
9d0a9e90 | 2343 | die_errno(_("%s: cannot stat the open index"), path); |
3511a377 | 2344 | |
7a51ed66 | 2345 | mmap_size = xsize_t(st.st_size); |
aab61359 | 2346 | if (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz) |
9d0a9e90 | 2347 | die(_("%s: index file smaller than expected"), path); |
3511a377 | 2348 | |
02638d1e | 2349 | mmap = xmmap_gently(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0); |
7a51ed66 | 2350 | if (mmap == MAP_FAILED) |
dc059294 EW |
2351 | die_errno(_("%s: unable to map index file%s"), path, |
2352 | mmap_os_err()); | |
57d84f8d | 2353 | close(fd); |
e83c5163 | 2354 | |
371ed0de | 2355 | hdr = (const struct cache_header *)mmap; |
7a51ed66 | 2356 | if (verify_hdr(hdr, mmap_size) < 0) |
e83c5163 LT |
2357 | goto unmap; |
2358 | ||
92e2cab9 | 2359 | oidread(&istate->oid, (const unsigned char *)hdr + mmap_size - the_hash_algo->rawsz); |
9d227781 | 2360 | istate->version = ntohl(hdr->hdr_version); |
4aab5b46 JH |
2361 | istate->cache_nr = ntohl(hdr->hdr_entries); |
2362 | istate->cache_alloc = alloc_nr(istate->cache_nr); | |
ca56dadb | 2363 | CALLOC_ARRAY(istate->cache, istate->cache_alloc); |
913e0e99 | 2364 | istate->initialized = 1; |
7a51ed66 | 2365 | |
abb4bb83 BP |
2366 | p.istate = istate; |
2367 | p.mmap = mmap; | |
2368 | p.mmap_size = mmap_size; | |
6c9cd161 | 2369 | |
7a51ed66 | 2370 | src_offset = sizeof(*hdr); |
4aab5b46 | 2371 | |
2a9dedef JN |
2372 | if (git_config_get_index_threads(&nr_threads)) |
2373 | nr_threads = 1; | |
7a51ed66 | 2374 | |
77ff1127 BP |
2375 | /* TODO: does creating more threads than cores help? */ |
2376 | if (!nr_threads) { | |
2377 | nr_threads = istate->cache_nr / THREAD_COST; | |
2378 | cpus = online_cpus(); | |
2379 | if (nr_threads > cpus) | |
2380 | nr_threads = cpus; | |
e83c5163 | 2381 | } |
abb4bb83 | 2382 | |
88168b9b NTND |
2383 | if (!HAVE_THREADS) |
2384 | nr_threads = 1; | |
2385 | ||
abb4bb83 BP |
2386 | if (nr_threads > 1) { |
2387 | extension_offset = read_eoie_extension(mmap, mmap_size); | |
2388 | if (extension_offset) { | |
2389 | int err; | |
2390 | ||
2391 | p.src_offset = extension_offset; | |
2392 | err = pthread_create(&p.pthread, NULL, load_index_extensions, &p); | |
2393 | if (err) | |
2394 | die(_("unable to create load_index_extensions thread: %s"), strerror(err)); | |
2395 | ||
2396 | nr_threads--; | |
2397 | } | |
2398 | } | |
abb4bb83 | 2399 | |
77ff1127 BP |
2400 | /* |
2401 | * Locate and read the index entry offset table so that we can use it | |
2402 | * to multi-thread the reading of the cache entries. | |
2403 | */ | |
2404 | if (extension_offset && nr_threads > 1) | |
2405 | ieot = read_ieot_extension(mmap, mmap_size, extension_offset); | |
2406 | ||
2407 | if (ieot) { | |
7bd9631b | 2408 | src_offset += load_cache_entries_threaded(istate, mmap, mmap_size, nr_threads, ieot); |
77ff1127 | 2409 | free(ieot); |
8e72d675 | 2410 | } else { |
77ff1127 | 2411 | src_offset += load_all_cache_entries(istate, mmap, mmap_size, src_offset); |
8e72d675 | 2412 | } |
6c9cd161 | 2413 | |
fba2f38a | 2414 | istate->timestamp.sec = st.st_mtime; |
c06ff490 | 2415 | istate->timestamp.nsec = ST_MTIME_NSEC(st); |
fba2f38a | 2416 | |
abb4bb83 | 2417 | /* if we created a thread, join it otherwise load the extensions on the primary thread */ |
abb4bb83 BP |
2418 | if (extension_offset) { |
2419 | int ret = pthread_join(p.pthread, NULL); | |
2420 | if (ret) | |
2421 | die(_("unable to join load_index_extensions thread: %s"), strerror(ret)); | |
88168b9b | 2422 | } else { |
abb4bb83 BP |
2423 | p.src_offset = src_offset; |
2424 | load_index_extensions(&p); | |
bad68ec9 | 2425 | } |
371ed0de | 2426 | munmap((void *)mmap, mmap_size); |
42fee7a3 JH |
2427 | |
2428 | /* | |
2429 | * TODO trace2: replace "the_repository" with the actual repo instance | |
2430 | * that is associated with the given "istate". | |
2431 | */ | |
2432 | trace2_data_intmax("index", the_repository, "read/version", | |
2433 | istate->version); | |
2434 | trace2_data_intmax("index", the_repository, "read/cache_nr", | |
2435 | istate->cache_nr); | |
2436 | ||
7ca4fc88 VD |
2437 | /* |
2438 | * If the command explicitly requires a full index, force it | |
2439 | * to be full. Otherwise, correct the sparsity based on repository | |
2440 | * settings and other properties of the index (if necessary). | |
2441 | */ | |
4300f844 DS |
2442 | prepare_repo_settings(istate->repo); |
2443 | if (istate->repo->settings.command_requires_full_index) | |
2444 | ensure_full_index(istate); | |
7ca4fc88 VD |
2445 | else |
2446 | ensure_correct_sparsity(istate); | |
4300f844 | 2447 | |
4aab5b46 | 2448 | return istate->cache_nr; |
e83c5163 LT |
2449 | |
2450 | unmap: | |
371ed0de | 2451 | munmap((void *)mmap, mmap_size); |
9d0a9e90 | 2452 | die(_("index file corrupt")); |
e83c5163 LT |
2453 | } |
2454 | ||
0d59ffb4 CC |
2455 | /* |
2456 | * Signal that the shared index is used by updating its mtime. | |
2457 | * | |
2458 | * This way, shared index can be removed if they have not been used | |
2459 | * for some time. | |
2460 | */ | |
a125a223 | 2461 | static void freshen_shared_index(const char *shared_index, int warn) |
0d59ffb4 | 2462 | { |
0d59ffb4 | 2463 | if (!check_and_freshen_file(shared_index, 1) && warn) |
9d0a9e90 | 2464 | warning(_("could not freshen shared index '%s'"), shared_index); |
0d59ffb4 CC |
2465 | } |
2466 | ||
a125a223 TG |
2467 | int read_index_from(struct index_state *istate, const char *path, |
2468 | const char *gitdir) | |
5fc2fc8f NTND |
2469 | { |
2470 | struct split_index *split_index; | |
2471 | int ret; | |
2182abd9 | 2472 | char *base_oid_hex; |
a125a223 | 2473 | char *base_path; |
5fc2fc8f NTND |
2474 | |
2475 | /* istate->initialized covers both .git/index and .git/sharedindex.xxx */ | |
2476 | if (istate->initialized) | |
2477 | return istate->cache_nr; | |
2478 | ||
42fee7a3 JH |
2479 | /* |
2480 | * TODO trace2: replace "the_repository" with the actual repo instance | |
2481 | * that is associated with the given "istate". | |
2482 | */ | |
2483 | trace2_region_enter_printf("index", "do_read_index", the_repository, | |
2484 | "%s", path); | |
c46c406a | 2485 | trace_performance_enter(); |
5fc2fc8f | 2486 | ret = do_read_index(istate, path, 0); |
c46c406a | 2487 | trace_performance_leave("read cache %s", path); |
42fee7a3 JH |
2488 | trace2_region_leave_printf("index", "do_read_index", the_repository, |
2489 | "%s", path); | |
435ec090 | 2490 | |
5fc2fc8f | 2491 | split_index = istate->split_index; |
2182abd9 | 2492 | if (!split_index || is_null_oid(&split_index->base_oid)) { |
435ec090 | 2493 | post_read_index_from(istate); |
5fc2fc8f | 2494 | return ret; |
03f15a79 | 2495 | } |
5fc2fc8f | 2496 | |
c46c406a | 2497 | trace_performance_enter(); |
5fc2fc8f | 2498 | if (split_index->base) |
2f6b1eb7 | 2499 | release_index(split_index->base); |
5fc2fc8f | 2500 | else |
2f6b1eb7 | 2501 | ALLOC_ARRAY(split_index->base, 1); |
6269f8ea | 2502 | index_state_init(split_index->base, istate->repo); |
de6ae5f9 | 2503 | |
2182abd9 | 2504 | base_oid_hex = oid_to_hex(&split_index->base_oid); |
2505 | base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex); | |
42fee7a3 JH |
2506 | trace2_region_enter_printf("index", "shared/do_read_index", |
2507 | the_repository, "%s", base_path); | |
998330ac | 2508 | ret = do_read_index(split_index->base, base_path, 0); |
42fee7a3 JH |
2509 | trace2_region_leave_printf("index", "shared/do_read_index", |
2510 | the_repository, "%s", base_path); | |
998330ac SG |
2511 | if (!ret) { |
2512 | char *path_copy = xstrdup(path); | |
652891de JS |
2513 | char *base_path2 = xstrfmt("%s/sharedindex.%s", |
2514 | dirname(path_copy), base_oid_hex); | |
998330ac SG |
2515 | free(path_copy); |
2516 | trace2_region_enter_printf("index", "shared/do_read_index", | |
2517 | the_repository, "%s", base_path2); | |
2518 | ret = do_read_index(split_index->base, base_path2, 1); | |
2519 | trace2_region_leave_printf("index", "shared/do_read_index", | |
2520 | the_repository, "%s", base_path2); | |
652891de | 2521 | free(base_path2); |
998330ac | 2522 | } |
9001dc2a | 2523 | if (!oideq(&split_index->base_oid, &split_index->base->oid)) |
9d0a9e90 | 2524 | die(_("broken index, expect %s in %s, got %s"), |
2182abd9 | 2525 | base_oid_hex, base_path, |
75691ea3 | 2526 | oid_to_hex(&split_index->base->oid)); |
de6ae5f9 | 2527 | |
a125a223 | 2528 | freshen_shared_index(base_path, 0); |
5fc2fc8f | 2529 | merge_base_index(istate); |
435ec090 | 2530 | post_read_index_from(istate); |
c46c406a | 2531 | trace_performance_leave("read cache %s", base_path); |
b42ad7d5 | 2532 | free(base_path); |
5fc2fc8f NTND |
2533 | return ret; |
2534 | } | |
2535 | ||
fa7b3c2f JH |
2536 | int is_index_unborn(struct index_state *istate) |
2537 | { | |
debed2a6 | 2538 | return (!istate->cache_nr && !istate->timestamp.sec); |
fa7b3c2f JH |
2539 | } |
2540 | ||
6269f8ea | 2541 | void index_state_init(struct index_state *istate, struct repository *r) |
2f6b1eb7 | 2542 | { |
6269f8ea | 2543 | struct index_state blank = INDEX_STATE_INIT(r); |
2f6b1eb7 ÆAB |
2544 | memcpy(istate, &blank, sizeof(*istate)); |
2545 | } | |
2546 | ||
2547 | void release_index(struct index_state *istate) | |
6d297f81 | 2548 | { |
8e72d675 JM |
2549 | /* |
2550 | * Cache entries in istate->cache[] should have been allocated | |
2551 | * from the memory pool associated with this index, or from an | |
2552 | * associated split_index. There is no need to free individual | |
8616a2d0 JM |
2553 | * cache entries. validate_cache_entries can detect when this |
2554 | * assertion does not hold. | |
8e72d675 | 2555 | */ |
8616a2d0 | 2556 | validate_cache_entries(istate); |
debed2a6 | 2557 | |
cfc5789a | 2558 | resolve_undo_clear_index(istate); |
2092678c | 2559 | free_name_hash(istate); |
4aab5b46 | 2560 | cache_tree_free(&(istate->cache_tree)); |
2f6b1eb7 ÆAB |
2561 | free(istate->fsmonitor_last_update); |
2562 | free(istate->cache); | |
5fc2fc8f | 2563 | discard_split_index(istate); |
f9e6c649 | 2564 | free_untracked_cache(istate->untracked); |
8e72d675 | 2565 | |
b5fcb1c0 ÆAB |
2566 | if (istate->sparse_checkout_patterns) { |
2567 | clear_pattern_list(istate->sparse_checkout_patterns); | |
2568 | FREE_AND_NULL(istate->sparse_checkout_patterns); | |
2569 | } | |
2570 | ||
8e72d675 | 2571 | if (istate->ce_mem_pool) { |
8616a2d0 | 2572 | mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries()); |
44c7e1a7 | 2573 | FREE_AND_NULL(istate->ce_mem_pool); |
8e72d675 | 2574 | } |
6d297f81 JS |
2575 | } |
2576 | ||
2f6b1eb7 ÆAB |
2577 | void discard_index(struct index_state *istate) |
2578 | { | |
2579 | release_index(istate); | |
6269f8ea | 2580 | index_state_init(istate, istate->repo); |
2f6b1eb7 ÆAB |
2581 | } |
2582 | ||
8616a2d0 JM |
2583 | /* |
2584 | * Validate the cache entries of this index. | |
2585 | * All cache entries associated with this index | |
2586 | * should have been allocated by the memory pool | |
2587 | * associated with this index, or by a referenced | |
2588 | * split index. | |
2589 | */ | |
2590 | void validate_cache_entries(const struct index_state *istate) | |
2591 | { | |
2592 | int i; | |
2593 | ||
2594 | if (!should_validate_cache_entries() ||!istate || !istate->initialized) | |
2595 | return; | |
2596 | ||
2597 | for (i = 0; i < istate->cache_nr; i++) { | |
2598 | if (!istate) { | |
391408e5 | 2599 | BUG("cache entry is not allocated from expected memory pool"); |
8616a2d0 JM |
2600 | } else if (!istate->ce_mem_pool || |
2601 | !mem_pool_contains(istate->ce_mem_pool, istate->cache[i])) { | |
2602 | if (!istate->split_index || | |
2603 | !istate->split_index->base || | |
2604 | !istate->split_index->base->ce_mem_pool || | |
2605 | !mem_pool_contains(istate->split_index->base->ce_mem_pool, istate->cache[i])) { | |
391408e5 | 2606 | BUG("cache entry is not allocated from expected memory pool"); |
8616a2d0 JM |
2607 | } |
2608 | } | |
2609 | } | |
2610 | ||
2611 | if (istate->split_index) | |
2612 | validate_cache_entries(istate->split_index->base); | |
2613 | } | |
2614 | ||
d1f128b0 | 2615 | int unmerged_index(const struct index_state *istate) |
94a5728c DB |
2616 | { |
2617 | int i; | |
2618 | for (i = 0; i < istate->cache_nr; i++) { | |
2619 | if (ce_stage(istate->cache[i])) | |
2620 | return 1; | |
2621 | } | |
2622 | return 0; | |
2623 | } | |
2624 | ||
150fe065 NTND |
2625 | int repo_index_has_changes(struct repository *repo, |
2626 | struct tree *tree, | |
2627 | struct strbuf *sb) | |
cffbfad5 | 2628 | { |
150fe065 | 2629 | struct index_state *istate = repo->index; |
e1f8694f | 2630 | struct object_id cmp; |
cffbfad5 EN |
2631 | int i; |
2632 | ||
e1f8694f EN |
2633 | if (tree) |
2634 | cmp = tree->object.oid; | |
2635 | if (tree || !get_oid_tree("HEAD", &cmp)) { | |
cffbfad5 EN |
2636 | struct diff_options opt; |
2637 | ||
150fe065 | 2638 | repo_diff_setup(repo, &opt); |
cffbfad5 EN |
2639 | opt.flags.exit_with_status = 1; |
2640 | if (!sb) | |
2641 | opt.flags.quick = 1; | |
8d833e93 | 2642 | diff_setup_done(&opt); |
e1f8694f | 2643 | do_diff_cache(&cmp, &opt); |
cffbfad5 EN |
2644 | diffcore_std(&opt); |
2645 | for (i = 0; sb && i < diff_queued_diff.nr; i++) { | |
2646 | if (i) | |
2647 | strbuf_addch(sb, ' '); | |
2648 | strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path); | |
2649 | } | |
2650 | diff_flush(&opt); | |
2651 | return opt.flags.has_changes != 0; | |
2652 | } else { | |
0c18c059 DS |
2653 | /* TODO: audit for interaction with sparse-index. */ |
2654 | ensure_full_index(istate); | |
1b9fbefb | 2655 | for (i = 0; sb && i < istate->cache_nr; i++) { |
cffbfad5 EN |
2656 | if (i) |
2657 | strbuf_addch(sb, ' '); | |
1b9fbefb | 2658 | strbuf_addstr(sb, istate->cache[i]->name); |
cffbfad5 | 2659 | } |
1b9fbefb | 2660 | return !!istate->cache_nr; |
cffbfad5 EN |
2661 | } |
2662 | } | |
2663 | ||
410334ed DS |
2664 | static int write_index_ext_header(struct hashfile *f, |
2665 | git_hash_ctx *eoie_f, | |
2666 | unsigned int ext, | |
2667 | unsigned int sz) | |
bad68ec9 | 2668 | { |
410334ed DS |
2669 | hashwrite_be32(f, ext); |
2670 | hashwrite_be32(f, sz); | |
2671 | ||
2672 | if (eoie_f) { | |
2673 | ext = htonl(ext); | |
2674 | sz = htonl(sz); | |
2675 | the_hash_algo->update_fn(eoie_f, &ext, sizeof(ext)); | |
2676 | the_hash_algo->update_fn(eoie_f, &sz, sizeof(sz)); | |
3b1d9e04 | 2677 | } |
410334ed | 2678 | return 0; |
bad68ec9 JH |
2679 | } |
2680 | ||
58bf2a4c NTND |
2681 | static void ce_smudge_racily_clean_entry(struct index_state *istate, |
2682 | struct cache_entry *ce) | |
407c8eb0 JH |
2683 | { |
2684 | /* | |
2685 | * The only thing we care about in this function is to smudge the | |
2686 | * falsely clean entry due to touch-update-touch race, so we leave | |
2687 | * everything else as they are. We are called for entries whose | |
c21d39d7 | 2688 | * ce_stat_data.sd_mtime match the index file mtime. |
c70115b4 JH |
2689 | * |
2690 | * Note that this actually does not do much for gitlinks, for | |
2691 | * which ce_match_stat_basic() always goes to the actual | |
2692 | * contents. The caller checks with is_racy_timestamp() which | |
2693 | * always says "no" for gitlinks, so we are not called for them ;-) | |
407c8eb0 JH |
2694 | */ |
2695 | struct stat st; | |
2696 | ||
2697 | if (lstat(ce->name, &st) < 0) | |
2698 | return; | |
2699 | if (ce_match_stat_basic(ce, &st)) | |
2700 | return; | |
58bf2a4c | 2701 | if (ce_modified_check_fs(istate, ce, &st)) { |
4b3511b0 JH |
2702 | /* This is "racily clean"; smudge it. Note that this |
2703 | * is a tricky code. At first glance, it may appear | |
2704 | * that it can break with this sequence: | |
2705 | * | |
2706 | * $ echo xyzzy >frotz | |
2707 | * $ git-update-index --add frotz | |
2708 | * $ : >frotz | |
2709 | * $ sleep 3 | |
2710 | * $ echo filfre >nitfol | |
2711 | * $ git-update-index --add nitfol | |
2712 | * | |
b7e58b17 | 2713 | * but it does not. When the second update-index runs, |
4b3511b0 JH |
2714 | * it notices that the entry "frotz" has the same timestamp |
2715 | * as index, and if we were to smudge it by resetting its | |
2716 | * size to zero here, then the object name recorded | |
2717 | * in index is the 6-byte file but the cached stat information | |
2718 | * becomes zero --- which would then match what we would | |
a6080a0a | 2719 | * obtain from the filesystem next time we stat("frotz"). |
4b3511b0 JH |
2720 | * |
2721 | * However, the second update-index, before calling | |
2722 | * this function, notices that the cached size is 6 | |
2723 | * bytes and what is on the filesystem is an empty | |
2724 | * file, and never calls us, so the cached size information | |
2725 | * for "frotz" stays 6 which does not match the filesystem. | |
2726 | */ | |
c21d39d7 | 2727 | ce->ce_stat_data.sd_size = 0; |
407c8eb0 JH |
2728 | } |
2729 | } | |
2730 | ||
f136f7bf | 2731 | /* Copy miscellaneous fields but not the name */ |
ce012deb | 2732 | static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk, |
f136f7bf | 2733 | struct cache_entry *ce) |
7a51ed66 | 2734 | { |
b60e188c | 2735 | short flags; |
575fa8a3 | 2736 | const unsigned hashsz = the_hash_algo->rawsz; |
2737 | uint16_t *flagsp = (uint16_t *)(ondisk->data + hashsz); | |
b60e188c | 2738 | |
c21d39d7 MH |
2739 | ondisk->ctime.sec = htonl(ce->ce_stat_data.sd_ctime.sec); |
2740 | ondisk->mtime.sec = htonl(ce->ce_stat_data.sd_mtime.sec); | |
2741 | ondisk->ctime.nsec = htonl(ce->ce_stat_data.sd_ctime.nsec); | |
2742 | ondisk->mtime.nsec = htonl(ce->ce_stat_data.sd_mtime.nsec); | |
2743 | ondisk->dev = htonl(ce->ce_stat_data.sd_dev); | |
2744 | ondisk->ino = htonl(ce->ce_stat_data.sd_ino); | |
7a51ed66 | 2745 | ondisk->mode = htonl(ce->ce_mode); |
c21d39d7 MH |
2746 | ondisk->uid = htonl(ce->ce_stat_data.sd_uid); |
2747 | ondisk->gid = htonl(ce->ce_stat_data.sd_gid); | |
2748 | ondisk->size = htonl(ce->ce_stat_data.sd_size); | |
575fa8a3 | 2749 | hashcpy(ondisk->data, ce->oid.hash); |
b60e188c | 2750 | |
ce51bf09 | 2751 | flags = ce->ce_flags & ~CE_NAMEMASK; |
b60e188c | 2752 | flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce)); |
575fa8a3 | 2753 | flagsp[0] = htons(flags); |
06aaaa0b | 2754 | if (ce->ce_flags & CE_EXTENDED) { |
575fa8a3 | 2755 | flagsp[1] = htons((ce->ce_flags & CE_EXTENDED_FLAGS) >> 16); |
f136f7bf JH |
2756 | } |
2757 | } | |
2758 | ||
410334ed | 2759 | static int ce_write_entry(struct hashfile *f, struct cache_entry *ce, |
ce012deb | 2760 | struct strbuf *previous_name, struct ondisk_cache_entry *ondisk) |
f136f7bf | 2761 | { |
9d227781 | 2762 | int size; |
00a4b035 RJ |
2763 | unsigned int saved_namelen; |
2764 | int stripped_name = 0; | |
ce012deb | 2765 | static unsigned char padding[8] = { 0x00 }; |
f136f7bf | 2766 | |
b3c96fb1 NTND |
2767 | if (ce->ce_flags & CE_STRIP_NAME) { |
2768 | saved_namelen = ce_namelen(ce); | |
2769 | ce->ce_namelen = 0; | |
00a4b035 | 2770 | stripped_name = 1; |
b3c96fb1 NTND |
2771 | } |
2772 | ||
575fa8a3 | 2773 | size = offsetof(struct ondisk_cache_entry,data) + ondisk_data_size(ce->ce_flags, 0); |
ce012deb | 2774 | |
9d227781 | 2775 | if (!previous_name) { |
ce012deb KW |
2776 | int len = ce_namelen(ce); |
2777 | copy_cache_entry_to_ondisk(ondisk, ce); | |
410334ed DS |
2778 | hashwrite(f, ondisk, size); |
2779 | hashwrite(f, ce->name, len); | |
2780 | hashwrite(f, padding, align_padding_size(size, len)); | |
9d227781 JH |
2781 | } else { |
2782 | int common, to_remove, prefix_size; | |
2783 | unsigned char to_remove_vi[16]; | |
2784 | for (common = 0; | |
2785 | (ce->name[common] && | |
2786 | common < previous_name->len && | |
2787 | ce->name[common] == previous_name->buf[common]); | |
2788 | common++) | |
2789 | ; /* still matching */ | |
2790 | to_remove = previous_name->len - common; | |
2791 | prefix_size = encode_varint(to_remove, to_remove_vi); | |
2792 | ||
ce012deb | 2793 | copy_cache_entry_to_ondisk(ondisk, ce); |
410334ed DS |
2794 | hashwrite(f, ondisk, size); |
2795 | hashwrite(f, to_remove_vi, prefix_size); | |
2796 | hashwrite(f, ce->name + common, ce_namelen(ce) - common); | |
2797 | hashwrite(f, padding, 1); | |
9d227781 JH |
2798 | |
2799 | strbuf_splice(previous_name, common, to_remove, | |
2800 | ce->name + common, ce_namelen(ce) - common); | |
06aaaa0b | 2801 | } |
00a4b035 | 2802 | if (stripped_name) { |
b3c96fb1 NTND |
2803 | ce->ce_namelen = saved_namelen; |
2804 | ce->ce_flags &= ~CE_STRIP_NAME; | |
2805 | } | |
7a51ed66 | 2806 | |
410334ed | 2807 | return 0; |
7a51ed66 LT |
2808 | } |
2809 | ||
426ddeea YM |
2810 | /* |
2811 | * This function verifies if index_state has the correct sha1 of the | |
2812 | * index file. Don't die if we have any other failure, just return 0. | |
2813 | */ | |
2814 | static int verify_index_from(const struct index_state *istate, const char *path) | |
2815 | { | |
2816 | int fd; | |
2817 | ssize_t n; | |
2818 | struct stat st; | |
aab61359 | 2819 | unsigned char hash[GIT_MAX_RAWSZ]; |
426ddeea YM |
2820 | |
2821 | if (!istate->initialized) | |
2822 | return 0; | |
2823 | ||
2824 | fd = open(path, O_RDONLY); | |
2825 | if (fd < 0) | |
2826 | return 0; | |
2827 | ||
2828 | if (fstat(fd, &st)) | |
2829 | goto out; | |
2830 | ||
aab61359 | 2831 | if (st.st_size < sizeof(struct cache_header) + the_hash_algo->rawsz) |
426ddeea YM |
2832 | goto out; |
2833 | ||
aab61359 | 2834 | n = pread_in_full(fd, hash, the_hash_algo->rawsz, st.st_size - the_hash_algo->rawsz); |
2835 | if (n != the_hash_algo->rawsz) | |
426ddeea YM |
2836 | goto out; |
2837 | ||
67947c34 | 2838 | if (!hasheq(istate->oid.hash, hash)) |
426ddeea YM |
2839 | goto out; |
2840 | ||
2841 | close(fd); | |
2842 | return 1; | |
2843 | ||
2844 | out: | |
2845 | close(fd); | |
2846 | return 0; | |
2847 | } | |
2848 | ||
1b0d968b | 2849 | static int repo_verify_index(struct repository *repo) |
426ddeea | 2850 | { |
1b0d968b | 2851 | return verify_index_from(repo->index, repo->index_file); |
426ddeea YM |
2852 | } |
2853 | ||
2ede073f | 2854 | int has_racy_timestamp(struct index_state *istate) |
483fbe2b JH |
2855 | { |
2856 | int entries = istate->cache_nr; | |
2857 | int i; | |
2858 | ||
2859 | for (i = 0; i < entries; i++) { | |
2860 | struct cache_entry *ce = istate->cache[i]; | |
2861 | if (is_racy_timestamp(istate, ce)) | |
2862 | return 1; | |
2863 | } | |
2864 | return 0; | |
2865 | } | |
2866 | ||
1b0d968b NTND |
2867 | void repo_update_index_if_able(struct repository *repo, |
2868 | struct lock_file *lockfile) | |
ccdc4ec3 | 2869 | { |
1b0d968b NTND |
2870 | if ((repo->index->cache_changed || |
2871 | has_racy_timestamp(repo->index)) && | |
2872 | repo_verify_index(repo)) | |
2873 | write_locked_index(repo->index, lockfile, COMMIT_LOCK); | |
b74c90fb MÅ |
2874 | else |
2875 | rollback_lock_file(lockfile); | |
ccdc4ec3 JH |
2876 | } |
2877 | ||
d8465500 JN |
2878 | static int record_eoie(void) |
2879 | { | |
2880 | int val; | |
2881 | ||
2882 | if (!git_config_get_bool("index.recordendofindexentries", &val)) | |
2883 | return val; | |
2a9dedef JN |
2884 | |
2885 | /* | |
2886 | * As a convenience, the end of index entries extension | |
2887 | * used for threading is written by default if the user | |
2888 | * explicitly requested threaded index reads. | |
2889 | */ | |
2890 | return !git_config_get_index_threads(&val) && val != 1; | |
d8465500 JN |
2891 | } |
2892 | ||
42916054 JN |
2893 | static int record_ieot(void) |
2894 | { | |
2895 | int val; | |
2896 | ||
2897 | if (!git_config_get_bool("index.recordoffsettable", &val)) | |
2898 | return val; | |
2a9dedef JN |
2899 | |
2900 | /* | |
2901 | * As a convenience, the offset table used for threading is | |
2902 | * written by default if the user explicitly requested | |
2903 | * threaded index reads. | |
2904 | */ | |
2905 | return !git_config_get_index_threads(&val) && val != 1; | |
42916054 JN |
2906 | } |
2907 | ||
812d6b00 MÅ |
2908 | /* |
2909 | * On success, `tempfile` is closed. If it is the temporary file | |
2910 | * of a `struct lock_file`, we will therefore effectively perform | |
2911 | * a 'close_lock_file_gently()`. Since that is an implementation | |
2912 | * detail of lockfiles, callers of `do_write_index()` should not | |
2913 | * rely on it. | |
2914 | */ | |
9f41c7a6 | 2915 | static int do_write_index(struct index_state *istate, struct tempfile *tempfile, |
ba95e96d | 2916 | int strip_extensions, unsigned flags) |
197ee8c9 | 2917 | { |
ca54d9ba | 2918 | uint64_t start = getnanotime(); |
410334ed DS |
2919 | struct hashfile *f; |
2920 | git_hash_ctx *eoie_c = NULL; | |
197ee8c9 | 2921 | struct cache_header hdr; |
b50386c7 | 2922 | int i, err = 0, removed, extended, hdr_version; |
4aab5b46 JH |
2923 | struct cache_entry **cache = istate->cache; |
2924 | int entries = istate->cache_nr; | |
e1afca4f | 2925 | struct stat st; |
575fa8a3 | 2926 | struct ondisk_cache_entry ondisk; |
9d227781 | 2927 | struct strbuf previous_name_buf = STRBUF_INIT, *previous_name; |
4bddd983 | 2928 | int drop_cache_tree = istate->drop_cache_tree; |
3b1d9e04 | 2929 | off_t offset; |
ba95e96d | 2930 | int csum_fsync_flag; |
77ff1127 | 2931 | int ieot_entries = 1; |
3255089a BP |
2932 | struct index_entry_offset_table *ieot = NULL; |
2933 | int nr, nr_threads; | |
6269f8ea | 2934 | struct repository *r = istate->repo; |
025a0709 | 2935 | |
410334ed DS |
2936 | f = hashfd(tempfile->fd, tempfile->filename.buf); |
2937 | ||
17194b19 DS |
2938 | prepare_repo_settings(r); |
2939 | f->skip_hash = r->settings.index_skip_hash; | |
ee1f0c24 | 2940 | |
06aaaa0b | 2941 | for (i = removed = extended = 0; i < entries; i++) { |
7a51ed66 | 2942 | if (cache[i]->ce_flags & CE_REMOVE) |
025a0709 | 2943 | removed++; |
197ee8c9 | 2944 | |
06aaaa0b NTND |
2945 | /* reduce extended entries if possible */ |
2946 | cache[i]->ce_flags &= ~CE_EXTENDED; | |
2947 | if (cache[i]->ce_flags & CE_EXTENDED_FLAGS) { | |
2948 | extended++; | |
2949 | cache[i]->ce_flags |= CE_EXTENDED; | |
2950 | } | |
2951 | } | |
2952 | ||
e8ffd034 | 2953 | if (!istate->version) |
7211b9e7 | 2954 | istate->version = get_index_format_default(the_repository); |
9d227781 JH |
2955 | |
2956 | /* demote version 3 to version 2 when the latter suffices */ | |
2957 | if (istate->version == 3 || istate->version == 2) | |
2958 | istate->version = extended ? 3 : 2; | |
2959 | ||
2960 | hdr_version = istate->version; | |
2961 | ||
ccc4feb5 | 2962 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
9d227781 | 2963 | hdr.hdr_version = htonl(hdr_version); |
025a0709 | 2964 | hdr.hdr_entries = htonl(entries - removed); |
197ee8c9 | 2965 | |
410334ed | 2966 | hashwrite(f, &hdr, sizeof(hdr)); |
197ee8c9 | 2967 | |
2a9dedef | 2968 | if (!HAVE_THREADS || git_config_get_index_threads(&nr_threads)) |
88168b9b | 2969 | nr_threads = 1; |
62e5ee81 | 2970 | |
42916054 | 2971 | if (nr_threads != 1 && record_ieot()) { |
3255089a BP |
2972 | int ieot_blocks, cpus; |
2973 | ||
2974 | /* | |
2975 | * ensure default number of ieot blocks maps evenly to the | |
2976 | * default number of threads that will process them leaving | |
2977 | * room for the thread to load the index extensions. | |
2978 | */ | |
2979 | if (!nr_threads) { | |
2980 | ieot_blocks = istate->cache_nr / THREAD_COST; | |
2981 | cpus = online_cpus(); | |
2982 | if (ieot_blocks > cpus - 1) | |
2983 | ieot_blocks = cpus - 1; | |
2984 | } else { | |
2985 | ieot_blocks = nr_threads; | |
77ff1127 BP |
2986 | if (ieot_blocks > istate->cache_nr) |
2987 | ieot_blocks = istate->cache_nr; | |
3255089a BP |
2988 | } |
2989 | ||
2990 | /* | |
2991 | * no reason to write out the IEOT extension if we don't | |
2992 | * have enough blocks to utilize multi-threading | |
2993 | */ | |
2994 | if (ieot_blocks > 1) { | |
2995 | ieot = xcalloc(1, sizeof(struct index_entry_offset_table) | |
2996 | + (ieot_blocks * sizeof(struct index_entry_offset))); | |
77ff1127 | 2997 | ieot_entries = DIV_ROUND_UP(entries, ieot_blocks); |
3255089a BP |
2998 | } |
2999 | } | |
3255089a | 3000 | |
410334ed DS |
3001 | offset = hashfile_total(f); |
3002 | ||
3255089a | 3003 | nr = 0; |
9d227781 | 3004 | previous_name = (hdr_version == 4) ? &previous_name_buf : NULL; |
ce012deb | 3005 | |
197ee8c9 LT |
3006 | for (i = 0; i < entries; i++) { |
3007 | struct cache_entry *ce = cache[i]; | |
7a51ed66 | 3008 | if (ce->ce_flags & CE_REMOVE) |
aa16021e | 3009 | continue; |
e06c43c7 | 3010 | if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce)) |
58bf2a4c | 3011 | ce_smudge_racily_clean_entry(istate, ce); |
99d1a986 | 3012 | if (is_null_oid(&ce->oid)) { |
83bd7437 JK |
3013 | static const char msg[] = "cache entry has null sha1: %s"; |
3014 | static int allow = -1; | |
3015 | ||
3016 | if (allow < 0) | |
3017 | allow = git_env_bool("GIT_ALLOW_NULL_SHA1", 0); | |
3018 | if (allow) | |
3019 | warning(msg, ce->name); | |
3020 | else | |
b50386c7 | 3021 | err = error(msg, ce->name); |
a96d3cc3 JK |
3022 | |
3023 | drop_cache_tree = 1; | |
83bd7437 | 3024 | } |
77ff1127 | 3025 | if (ieot && i && (i % ieot_entries == 0)) { |
3255089a BP |
3026 | ieot->entries[ieot->nr].nr = nr; |
3027 | ieot->entries[ieot->nr].offset = offset; | |
3028 | ieot->nr++; | |
3029 | /* | |
3030 | * If we have a V4 index, set the first byte to an invalid | |
3031 | * character to ensure there is nothing common with the previous | |
3032 | * entry | |
3033 | */ | |
3034 | if (previous_name) | |
3035 | previous_name->buf[0] = 0; | |
3036 | nr = 0; | |
410334ed DS |
3037 | |
3038 | offset = hashfile_total(f); | |
3255089a | 3039 | } |
410334ed | 3040 | if (ce_write_entry(f, ce, previous_name, (struct ondisk_cache_entry *)&ondisk) < 0) |
b50386c7 KW |
3041 | err = -1; |
3042 | ||
3043 | if (err) | |
3044 | break; | |
3255089a BP |
3045 | nr++; |
3046 | } | |
3047 | if (ieot && nr) { | |
3048 | ieot->entries[ieot->nr].nr = nr; | |
3049 | ieot->entries[ieot->nr].offset = offset; | |
3050 | ieot->nr++; | |
197ee8c9 | 3051 | } |
9d227781 | 3052 | strbuf_release(&previous_name_buf); |
1af1c2b6 | 3053 | |
3255089a BP |
3054 | if (err) { |
3055 | free(ieot); | |
b50386c7 | 3056 | return err; |
3255089a | 3057 | } |
b50386c7 | 3058 | |
410334ed DS |
3059 | offset = hashfile_total(f); |
3060 | ||
3061 | /* | |
3062 | * The extension headers must be hashed on their own for the | |
3063 | * EOIE extension. Create a hashfile here to compute that hash. | |
3064 | */ | |
3065 | if (offset && record_eoie()) { | |
3066 | CALLOC_ARRAY(eoie_c, 1); | |
3067 | the_hash_algo->init_fn(eoie_c); | |
3255089a | 3068 | } |
3b1d9e04 | 3069 | |
3255089a BP |
3070 | /* |
3071 | * Lets write out CACHE_EXT_INDEXENTRYOFFSETTABLE first so that we | |
3072 | * can minimize the number of extensions we have to scan through to | |
3073 | * find it during load. Write it out regardless of the | |
3074 | * strip_extensions parameter as we need it when loading the shared | |
3075 | * index. | |
3076 | */ | |
3255089a BP |
3077 | if (ieot) { |
3078 | struct strbuf sb = STRBUF_INIT; | |
3079 | ||
3080 | write_ieot_extension(&sb, ieot); | |
410334ed DS |
3081 | err = write_index_ext_header(f, eoie_c, CACHE_EXT_INDEXENTRYOFFSETTABLE, sb.len) < 0; |
3082 | hashwrite(f, sb.buf, sb.len); | |
3255089a BP |
3083 | strbuf_release(&sb); |
3084 | free(ieot); | |
3085 | if (err) | |
3086 | return -1; | |
3087 | } | |
3255089a | 3088 | |
6e37c8ed NTND |
3089 | if (!strip_extensions && istate->split_index && |
3090 | !is_null_oid(&istate->split_index->base_oid)) { | |
5fc2fc8f NTND |
3091 | struct strbuf sb = STRBUF_INIT; |
3092 | ||
451b66c5 JS |
3093 | if (istate->sparse_index) |
3094 | die(_("cannot write split index for a sparse index")); | |
3095 | ||
5fc2fc8f | 3096 | err = write_link_extension(&sb, istate) < 0 || |
410334ed DS |
3097 | write_index_ext_header(f, eoie_c, CACHE_EXT_LINK, |
3098 | sb.len) < 0; | |
3099 | hashwrite(f, sb.buf, sb.len); | |
5fc2fc8f NTND |
3100 | strbuf_release(&sb); |
3101 | if (err) | |
3102 | return -1; | |
3103 | } | |
a96d3cc3 | 3104 | if (!strip_extensions && !drop_cache_tree && istate->cache_tree) { |
f285a2d7 | 3105 | struct strbuf sb = STRBUF_INIT; |
1dffb8fa | 3106 | |
1dffb8fa | 3107 | cache_tree_write(&sb, istate->cache_tree); |
410334ed DS |
3108 | err = write_index_ext_header(f, eoie_c, CACHE_EXT_TREE, sb.len) < 0; |
3109 | hashwrite(f, sb.buf, sb.len); | |
1dffb8fa PH |
3110 | strbuf_release(&sb); |
3111 | if (err) | |
bad68ec9 | 3112 | return -1; |
bad68ec9 | 3113 | } |
c18b80a0 | 3114 | if (!strip_extensions && istate->resolve_undo) { |
cfc5789a JH |
3115 | struct strbuf sb = STRBUF_INIT; |
3116 | ||
3117 | resolve_undo_write(&sb, istate->resolve_undo); | |
410334ed DS |
3118 | err = write_index_ext_header(f, eoie_c, CACHE_EXT_RESOLVE_UNDO, |
3119 | sb.len) < 0; | |
3120 | hashwrite(f, sb.buf, sb.len); | |
cfc5789a JH |
3121 | strbuf_release(&sb); |
3122 | if (err) | |
3123 | return -1; | |
3124 | } | |
83c094ad NTND |
3125 | if (!strip_extensions && istate->untracked) { |
3126 | struct strbuf sb = STRBUF_INIT; | |
3127 | ||
3128 | write_untracked_extension(&sb, istate->untracked); | |
410334ed DS |
3129 | err = write_index_ext_header(f, eoie_c, CACHE_EXT_UNTRACKED, |
3130 | sb.len) < 0; | |
3131 | hashwrite(f, sb.buf, sb.len); | |
83c094ad NTND |
3132 | strbuf_release(&sb); |
3133 | if (err) | |
3134 | return -1; | |
3135 | } | |
883e248b BP |
3136 | if (!strip_extensions && istate->fsmonitor_last_update) { |
3137 | struct strbuf sb = STRBUF_INIT; | |
3138 | ||
3139 | write_fsmonitor_extension(&sb, istate); | |
410334ed DS |
3140 | err = write_index_ext_header(f, eoie_c, CACHE_EXT_FSMONITOR, sb.len) < 0; |
3141 | hashwrite(f, sb.buf, sb.len); | |
3b1d9e04 BP |
3142 | strbuf_release(&sb); |
3143 | if (err) | |
3144 | return -1; | |
3145 | } | |
cd42415f | 3146 | if (istate->sparse_index) { |
410334ed | 3147 | if (write_index_ext_header(f, eoie_c, CACHE_EXT_SPARSE_DIRECTORIES, 0) < 0) |
cd42415f DS |
3148 | return -1; |
3149 | } | |
3b1d9e04 BP |
3150 | |
3151 | /* | |
3152 | * CACHE_EXT_ENDOFINDEXENTRIES must be written as the last entry before the SHA1 | |
3153 | * so that it can be found and processed before all the index entries are | |
3154 | * read. Write it out regardless of the strip_extensions parameter as we need it | |
3155 | * when loading the shared index. | |
3156 | */ | |
410334ed | 3157 | if (eoie_c) { |
3b1d9e04 BP |
3158 | struct strbuf sb = STRBUF_INIT; |
3159 | ||
410334ed DS |
3160 | write_eoie_extension(&sb, eoie_c, offset); |
3161 | err = write_index_ext_header(f, NULL, CACHE_EXT_ENDOFINDEXENTRIES, sb.len) < 0; | |
3162 | hashwrite(f, sb.buf, sb.len); | |
883e248b BP |
3163 | strbuf_release(&sb); |
3164 | if (err) | |
3165 | return -1; | |
3166 | } | |
e1afca4f | 3167 | |
ba95e96d NS |
3168 | csum_fsync_flag = 0; |
3169 | if (!alternate_index_output && (flags & COMMIT_LOCK)) | |
3170 | csum_fsync_flag = CSUM_FSYNC; | |
3171 | ||
3172 | finalize_hashfile(f, istate->oid.hash, FSYNC_COMPONENT_INDEX, | |
3173 | CSUM_HASH_IN_STREAM | csum_fsync_flag); | |
3174 | ||
49bd0fc2 | 3175 | if (close_tempfile_gently(tempfile)) { |
6a8c89d0 | 3176 | error(_("could not close '%s'"), get_tempfile_path(tempfile)); |
49bd0fc2 JK |
3177 | return -1; |
3178 | } | |
6a8c89d0 | 3179 | if (stat(get_tempfile_path(tempfile), &st)) |
e1afca4f | 3180 | return -1; |
5bcf109c KB |
3181 | istate->timestamp.sec = (unsigned int)st.st_mtime; |
3182 | istate->timestamp.nsec = ST_MTIME_NSEC(st); | |
ca54d9ba | 3183 | trace_performance_since(start, "write index, changed mask = %x", istate->cache_changed); |
42fee7a3 JH |
3184 | |
3185 | /* | |
3186 | * TODO trace2: replace "the_repository" with the actual repo instance | |
3187 | * that is associated with the given "istate". | |
3188 | */ | |
3189 | trace2_data_intmax("index", the_repository, "write/version", | |
3190 | istate->version); | |
3191 | trace2_data_intmax("index", the_repository, "write/cache_nr", | |
3192 | istate->cache_nr); | |
3193 | ||
e1afca4f | 3194 | return 0; |
197ee8c9 | 3195 | } |
e46bbcf6 | 3196 | |
626f35c8 NTND |
3197 | void set_alternate_index_output(const char *name) |
3198 | { | |
3199 | alternate_index_output = name; | |
3200 | } | |
3201 | ||
3202 | static int commit_locked_index(struct lock_file *lk) | |
3203 | { | |
751baced MH |
3204 | if (alternate_index_output) |
3205 | return commit_lock_file_to(lk, alternate_index_output); | |
3206 | else | |
626f35c8 | 3207 | return commit_lock_file(lk); |
626f35c8 NTND |
3208 | } |
3209 | ||
03b86647 NTND |
3210 | static int do_write_locked_index(struct index_state *istate, struct lock_file *lock, |
3211 | unsigned flags) | |
3212 | { | |
42fee7a3 | 3213 | int ret; |
9fadb373 | 3214 | int was_full = istate->sparse_index == INDEX_EXPANDED; |
6e773527 | 3215 | |
ce7a9f01 | 3216 | ret = convert_to_sparse(istate, 0); |
6e773527 DS |
3217 | |
3218 | if (ret) { | |
3219 | warning(_("failed to convert to a sparse-index")); | |
3220 | return ret; | |
3221 | } | |
42fee7a3 JH |
3222 | |
3223 | /* | |
3224 | * TODO trace2: replace "the_repository" with the actual repo instance | |
3225 | * that is associated with the given "istate". | |
3226 | */ | |
3227 | trace2_region_enter_printf("index", "do_write_index", the_repository, | |
6a8c89d0 | 3228 | "%s", get_lock_file_path(lock)); |
ba95e96d | 3229 | ret = do_write_index(istate, lock->tempfile, 0, flags); |
42fee7a3 | 3230 | trace2_region_leave_printf("index", "do_write_index", the_repository, |
6a8c89d0 | 3231 | "%s", get_lock_file_path(lock)); |
42fee7a3 | 3232 | |
6e773527 DS |
3233 | if (was_full) |
3234 | ensure_full_index(istate); | |
3235 | ||
03b86647 NTND |
3236 | if (ret) |
3237 | return ret; | |
03b86647 | 3238 | if (flags & COMMIT_LOCK) |
1956ecd0 BP |
3239 | ret = commit_locked_index(lock); |
3240 | else | |
3241 | ret = close_lock_file_gently(lock); | |
3242 | ||
dbb1c613 | 3243 | run_hooks_l("post-index-change", |
1956ecd0 BP |
3244 | istate->updated_workdir ? "1" : "0", |
3245 | istate->updated_skipworktree ? "1" : "0", NULL); | |
3246 | istate->updated_workdir = 0; | |
3247 | istate->updated_skipworktree = 0; | |
3248 | ||
3249 | return ret; | |
03b86647 NTND |
3250 | } |
3251 | ||
5fc2fc8f NTND |
3252 | static int write_split_index(struct index_state *istate, |
3253 | struct lock_file *lock, | |
3254 | unsigned flags) | |
3255 | { | |
3256 | int ret; | |
3257 | prepare_to_write_split_index(istate); | |
3258 | ret = do_write_locked_index(istate, lock, flags); | |
3259 | finish_writing_split_index(istate); | |
3260 | return ret; | |
3261 | } | |
3262 | ||
b9683722 CC |
3263 | static const char *shared_index_expire = "2.weeks.ago"; |
3264 | ||
3265 | static unsigned long get_shared_index_expire_date(void) | |
3266 | { | |
3267 | static unsigned long shared_index_expire_date; | |
3268 | static int shared_index_expire_date_prepared; | |
3269 | ||
3270 | if (!shared_index_expire_date_prepared) { | |
3271 | git_config_get_expiry("splitindex.sharedindexexpire", | |
3272 | &shared_index_expire); | |
3273 | shared_index_expire_date = approxidate(shared_index_expire); | |
3274 | shared_index_expire_date_prepared = 1; | |
3275 | } | |
3276 | ||
3277 | return shared_index_expire_date; | |
3278 | } | |
3279 | ||
3280 | static int should_delete_shared_index(const char *shared_index_path) | |
3281 | { | |
3282 | struct stat st; | |
3283 | unsigned long expiration; | |
3284 | ||
3285 | /* Check timestamp */ | |
3286 | expiration = get_shared_index_expire_date(); | |
3287 | if (!expiration) | |
3288 | return 0; | |
3289 | if (stat(shared_index_path, &st)) | |
78bde923 | 3290 | return error_errno(_("could not stat '%s'"), shared_index_path); |
b9683722 CC |
3291 | if (st.st_mtime > expiration) |
3292 | return 0; | |
3293 | ||
3294 | return 1; | |
3295 | } | |
3296 | ||
3297 | static int clean_shared_index_files(const char *current_hex) | |
3298 | { | |
3299 | struct dirent *de; | |
3300 | DIR *dir = opendir(get_git_dir()); | |
3301 | ||
3302 | if (!dir) | |
3303 | return error_errno(_("unable to open git dir: %s"), get_git_dir()); | |
3304 | ||
3305 | while ((de = readdir(dir)) != NULL) { | |
3306 | const char *sha1_hex; | |
3307 | const char *shared_index_path; | |
3308 | if (!skip_prefix(de->d_name, "sharedindex.", &sha1_hex)) | |
3309 | continue; | |
3310 | if (!strcmp(sha1_hex, current_hex)) | |
3311 | continue; | |
3312 | shared_index_path = git_path("%s", de->d_name); | |
3313 | if (should_delete_shared_index(shared_index_path) > 0 && | |
3314 | unlink(shared_index_path)) | |
3315 | warning_errno(_("unable to unlink: %s"), shared_index_path); | |
3316 | } | |
3317 | closedir(dir); | |
3318 | ||
3319 | return 0; | |
3320 | } | |
3321 | ||
a0a96756 | 3322 | static int write_shared_index(struct index_state *istate, |
ba95e96d | 3323 | struct tempfile **temp, unsigned flags) |
c18b80a0 NTND |
3324 | { |
3325 | struct split_index *si = istate->split_index; | |
6e773527 | 3326 | int ret, was_full = !istate->sparse_index; |
c18b80a0 | 3327 | |
c18b80a0 | 3328 | move_cache_to_base_index(istate); |
ce7a9f01 | 3329 | convert_to_sparse(istate, 0); |
42fee7a3 JH |
3330 | |
3331 | trace2_region_enter_printf("index", "shared/do_write_index", | |
6a8c89d0 | 3332 | the_repository, "%s", get_tempfile_path(*temp)); |
ba95e96d | 3333 | ret = do_write_index(si->base, *temp, 1, flags); |
c173542c | 3334 | trace2_region_leave_printf("index", "shared/do_write_index", |
6a8c89d0 | 3335 | the_repository, "%s", get_tempfile_path(*temp)); |
42fee7a3 | 3336 | |
6e773527 DS |
3337 | if (was_full) |
3338 | ensure_full_index(istate); | |
3339 | ||
59f9d2dd | 3340 | if (ret) |
c18b80a0 | 3341 | return ret; |
7db2d08c | 3342 | ret = adjust_shared_perm(get_tempfile_path(*temp)); |
df801f3f | 3343 | if (ret) { |
9d0a9e90 | 3344 | error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp)); |
df801f3f CC |
3345 | return ret; |
3346 | } | |
7db2d08c | 3347 | ret = rename_tempfile(temp, |
75691ea3 | 3348 | git_path("sharedindex.%s", oid_to_hex(&si->base->oid))); |
b9683722 | 3349 | if (!ret) { |
75691ea3 | 3350 | oidcpy(&si->base_oid, &si->base->oid); |
3351 | clean_shared_index_files(oid_to_hex(&si->base->oid)); | |
b9683722 CC |
3352 | } |
3353 | ||
c18b80a0 NTND |
3354 | return ret; |
3355 | } | |
3356 | ||
e6a1dd77 CC |
3357 | static const int default_max_percent_split_change = 20; |
3358 | ||
3359 | static int too_many_not_shared_entries(struct index_state *istate) | |
3360 | { | |
3361 | int i, not_shared = 0; | |
3362 | int max_split = git_config_get_max_percent_split_change(); | |
3363 | ||
3364 | switch (max_split) { | |
3365 | case -1: | |
3366 | /* not or badly configured: use the default value */ | |
3367 | max_split = default_max_percent_split_change; | |
3368 | break; | |
3369 | case 0: | |
3370 | return 1; /* 0% means always write a new shared index */ | |
3371 | case 100: | |
3372 | return 0; /* 100% means never write a new shared index */ | |
3373 | default: | |
3374 | break; /* just use the configured value */ | |
3375 | } | |
3376 | ||
3377 | /* Count not shared entries */ | |
3378 | for (i = 0; i < istate->cache_nr; i++) { | |
3379 | struct cache_entry *ce = istate->cache[i]; | |
3380 | if (!ce->index) | |
3381 | not_shared++; | |
3382 | } | |
3383 | ||
3384 | return (int64_t)istate->cache_nr * max_split < (int64_t)not_shared * 100; | |
3385 | } | |
3386 | ||
03b86647 NTND |
3387 | int write_locked_index(struct index_state *istate, struct lock_file *lock, |
3388 | unsigned flags) | |
3389 | { | |
e8ffd034 | 3390 | int new_shared_index, ret, test_split_index_env; |
5fc2fc8f NTND |
3391 | struct split_index *si = istate->split_index; |
3392 | ||
4592e608 | 3393 | if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0)) |
c207e9e1 | 3394 | cache_tree_verify(the_repository, istate); |
4592e608 | 3395 | |
61000814 MÅ |
3396 | if ((flags & SKIP_IF_UNCHANGED) && !istate->cache_changed) { |
3397 | if (flags & COMMIT_LOCK) | |
3398 | rollback_lock_file(lock); | |
3399 | return 0; | |
3400 | } | |
3401 | ||
3bd28eb2 AV |
3402 | if (istate->fsmonitor_last_update) |
3403 | fill_fsmonitor_bitmap(istate); | |
3404 | ||
e8ffd034 SG |
3405 | test_split_index_env = git_env_bool("GIT_TEST_SPLIT_INDEX", 0); |
3406 | ||
3407 | if ((!si && !test_split_index_env) || | |
3408 | alternate_index_output || | |
5165dd59 | 3409 | (istate->cache_changed & ~EXTMASK)) { |
5fc2fc8f | 3410 | if (si) |
2182abd9 | 3411 | oidclr(&si->base_oid); |
df60cf57 MÅ |
3412 | ret = do_write_locked_index(istate, lock, flags); |
3413 | goto out; | |
5fc2fc8f NTND |
3414 | } |
3415 | ||
e8ffd034 SG |
3416 | if (test_split_index_env) { |
3417 | if (!si) { | |
3418 | si = init_split_index(istate); | |
d6e3c181 | 3419 | istate->cache_changed |= SPLIT_INDEX_ORDERED; |
e8ffd034 SG |
3420 | } else { |
3421 | int v = si->base_oid.hash[0]; | |
3422 | if ((v & 15) < 6) | |
3423 | istate->cache_changed |= SPLIT_INDEX_ORDERED; | |
3424 | } | |
d6e3c181 | 3425 | } |
e6a1dd77 CC |
3426 | if (too_many_not_shared_entries(istate)) |
3427 | istate->cache_changed |= SPLIT_INDEX_ORDERED; | |
0d59ffb4 CC |
3428 | |
3429 | new_shared_index = istate->cache_changed & SPLIT_INDEX_ORDERED; | |
3430 | ||
3431 | if (new_shared_index) { | |
59f9d2dd NTND |
3432 | struct tempfile *temp; |
3433 | int saved_errno; | |
3434 | ||
c9d6c788 ÆAB |
3435 | /* Same initial permissions as the main .git/index file */ |
3436 | temp = mks_tempfile_sm(git_path("sharedindex_XXXXXX"), 0, 0666); | |
59f9d2dd | 3437 | if (!temp) { |
2182abd9 | 3438 | oidclr(&si->base_oid); |
59f9d2dd | 3439 | ret = do_write_locked_index(istate, lock, flags); |
ef5b3a6c NTND |
3440 | goto out; |
3441 | } | |
ba95e96d | 3442 | ret = write_shared_index(istate, &temp, flags); |
59f9d2dd NTND |
3443 | |
3444 | saved_errno = errno; | |
3445 | if (is_tempfile_active(temp)) | |
3446 | delete_tempfile(&temp); | |
3447 | errno = saved_errno; | |
3448 | ||
c18b80a0 | 3449 | if (ret) |
df60cf57 | 3450 | goto out; |
c18b80a0 NTND |
3451 | } |
3452 | ||
0d59ffb4 CC |
3453 | ret = write_split_index(istate, lock, flags); |
3454 | ||
3455 | /* Freshen the shared index only if the split-index was written */ | |
6e37c8ed | 3456 | if (!ret && !new_shared_index && !is_null_oid(&si->base_oid)) { |
a125a223 | 3457 | const char *shared_index = git_path("sharedindex.%s", |
2182abd9 | 3458 | oid_to_hex(&si->base_oid)); |
a125a223 TG |
3459 | freshen_shared_index(shared_index, 1); |
3460 | } | |
0d59ffb4 | 3461 | |
df60cf57 MÅ |
3462 | out: |
3463 | if (flags & COMMIT_LOCK) | |
3464 | rollback_lock_file(lock); | |
0d59ffb4 | 3465 | return ret; |
03b86647 NTND |
3466 | } |
3467 | ||
e46bbcf6 MV |
3468 | /* |
3469 | * Read the index file that is potentially unmerged into given | |
ad376204 EN |
3470 | * index_state, dropping any unmerged entries to stage #0 (potentially |
3471 | * resulting in a path appearing as both a file and a directory in the | |
3472 | * index; the caller is responsible to clear out the extra entries | |
3473 | * before writing the index to a tree). Returns true if the index is | |
3474 | * unmerged. Callers who want to refuse to work from an unmerged | |
3475 | * state can call this and check its return value, instead of calling | |
3476 | * read_cache(). | |
e46bbcf6 | 3477 | */ |
e1ff0a32 | 3478 | int repo_read_index_unmerged(struct repository *repo) |
e46bbcf6 | 3479 | { |
e1ff0a32 | 3480 | struct index_state *istate; |
e46bbcf6 | 3481 | int i; |
d1a43f2a | 3482 | int unmerged = 0; |
e46bbcf6 | 3483 | |
e1ff0a32 NTND |
3484 | repo_read_index(repo); |
3485 | istate = repo->index; | |
e46bbcf6 MV |
3486 | for (i = 0; i < istate->cache_nr; i++) { |
3487 | struct cache_entry *ce = istate->cache[i]; | |
d1a43f2a | 3488 | struct cache_entry *new_ce; |
a849735b | 3489 | int len; |
d1a43f2a JH |
3490 | |
3491 | if (!ce_stage(ce)) | |
e46bbcf6 | 3492 | continue; |
d1a43f2a | 3493 | unmerged = 1; |
68c4f6a5 | 3494 | len = ce_namelen(ce); |
a849735b | 3495 | new_ce = make_empty_cache_entry(istate, len); |
d1a43f2a | 3496 | memcpy(new_ce->name, ce->name, len); |
b60e188c TG |
3497 | new_ce->ce_flags = create_ce_flags(0) | CE_CONFLICTED; |
3498 | new_ce->ce_namelen = len; | |
d1a43f2a | 3499 | new_ce->ce_mode = ce->ce_mode; |
ad376204 | 3500 | if (add_index_entry(istate, new_ce, ADD_CACHE_SKIP_DFCHECK)) |
9d0a9e90 | 3501 | return error(_("%s: cannot drop to stage #0"), |
5699d17e | 3502 | new_ce->name); |
e46bbcf6 | 3503 | } |
d1a43f2a | 3504 | return unmerged; |
e46bbcf6 | 3505 | } |
041aee31 | 3506 | |
98fa4738 JK |
3507 | /* |
3508 | * Returns 1 if the path is an "other" path with respect to | |
3509 | * the index; that is, the path is not mentioned in the index at all, | |
3510 | * either as a file, a directory with some files in the index, | |
3511 | * or as an unmerged entry. | |
3512 | * | |
3513 | * We helpfully remove a trailing "/" from directories so that | |
3514 | * the output of read_directory can be used as-is. | |
3515 | */ | |
847a9e5d DS |
3516 | int index_name_is_other(struct index_state *istate, const char *name, |
3517 | int namelen) | |
98fa4738 JK |
3518 | { |
3519 | int pos; | |
3520 | if (namelen && name[namelen - 1] == '/') | |
3521 | namelen--; | |
3522 | pos = index_name_pos(istate, name, namelen); | |
3523 | if (0 <= pos) | |
3524 | return 0; /* exact match */ | |
3525 | pos = -pos - 1; | |
3526 | if (pos < istate->cache_nr) { | |
3527 | struct cache_entry *ce = istate->cache[pos]; | |
3528 | if (ce_namelen(ce) == namelen && | |
3529 | !memcmp(ce->name, name, namelen)) | |
3530 | return 0; /* Yup, this one exists unmerged */ | |
3531 | } | |
3532 | return 1; | |
3533 | } | |
29fb37b2 | 3534 | |
847a9e5d | 3535 | void *read_blob_data_from_index(struct index_state *istate, |
87542508 | 3536 | const char *path, unsigned long *size) |
29fb37b2 LF |
3537 | { |
3538 | int pos, len; | |
3539 | unsigned long sz; | |
3540 | enum object_type type; | |
3541 | void *data; | |
3542 | ||
3543 | len = strlen(path); | |
3544 | pos = index_name_pos(istate, path, len); | |
3545 | if (pos < 0) { | |
3546 | /* | |
3547 | * We might be in the middle of a merge, in which | |
3548 | * case we would read stage #2 (ours). | |
3549 | */ | |
3550 | int i; | |
3551 | for (i = -pos - 1; | |
3552 | (pos < 0 && i < istate->cache_nr && | |
3553 | !strcmp(istate->cache[i]->name, path)); | |
3554 | i++) | |
3555 | if (ce_stage(istate->cache[i]) == 2) | |
3556 | pos = i; | |
3557 | } | |
3558 | if (pos < 0) | |
3559 | return NULL; | |
b4f5aca4 | 3560 | data = read_object_file(&istate->cache[pos]->oid, &type, &sz); |
29fb37b2 LF |
3561 | if (!data || type != OBJ_BLOB) { |
3562 | free(data); | |
3563 | return NULL; | |
3564 | } | |
ff366825 LF |
3565 | if (size) |
3566 | *size = sz; | |
29fb37b2 LF |
3567 | return data; |
3568 | } | |
38612532 MH |
3569 | |
3570 | void stat_validity_clear(struct stat_validity *sv) | |
3571 | { | |
6a83d902 | 3572 | FREE_AND_NULL(sv->sd); |
38612532 MH |
3573 | } |
3574 | ||
3575 | int stat_validity_check(struct stat_validity *sv, const char *path) | |
3576 | { | |
3577 | struct stat st; | |
3578 | ||
3579 | if (stat(path, &st) < 0) | |
3580 | return sv->sd == NULL; | |
3581 | if (!sv->sd) | |
3582 | return 0; | |
3583 | return S_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st); | |
3584 | } | |
3585 | ||
3586 | void stat_validity_update(struct stat_validity *sv, int fd) | |
3587 | { | |
3588 | struct stat st; | |
3589 | ||
3590 | if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode)) | |
3591 | stat_validity_clear(sv); | |
3592 | else { | |
3593 | if (!sv->sd) | |
ca56dadb | 3594 | CALLOC_ARRAY(sv->sd, 1); |
38612532 MH |
3595 | fill_stat_data(sv->sd, &st); |
3596 | } | |
3597 | } | |
edf3b905 DT |
3598 | |
3599 | void move_index_extensions(struct index_state *dst, struct index_state *src) | |
3600 | { | |
3601 | dst->untracked = src->untracked; | |
3602 | src->untracked = NULL; | |
836ef2b6 NTND |
3603 | dst->cache_tree = src->cache_tree; |
3604 | src->cache_tree = NULL; | |
edf3b905 | 3605 | } |
a849735b | 3606 | |
8e72d675 JM |
3607 | struct cache_entry *dup_cache_entry(const struct cache_entry *ce, |
3608 | struct index_state *istate) | |
3609 | { | |
3610 | unsigned int size = ce_size(ce); | |
3611 | int mem_pool_allocated; | |
3612 | struct cache_entry *new_entry = make_empty_cache_entry(istate, ce_namelen(ce)); | |
3613 | mem_pool_allocated = new_entry->mem_pool_allocated; | |
3614 | ||
3615 | memcpy(new_entry, ce, size); | |
3616 | new_entry->mem_pool_allocated = mem_pool_allocated; | |
3617 | return new_entry; | |
3618 | } | |
3619 | ||
a849735b JM |
3620 | void discard_cache_entry(struct cache_entry *ce) |
3621 | { | |
8616a2d0 JM |
3622 | if (ce && should_validate_cache_entries()) |
3623 | memset(ce, 0xCD, cache_entry_size(ce->ce_namelen)); | |
3624 | ||
8e72d675 JM |
3625 | if (ce && ce->mem_pool_allocated) |
3626 | return; | |
3627 | ||
a849735b JM |
3628 | free(ce); |
3629 | } | |
8616a2d0 JM |
3630 | |
3631 | int should_validate_cache_entries(void) | |
3632 | { | |
3633 | static int validate_index_cache_entries = -1; | |
3634 | ||
3635 | if (validate_index_cache_entries < 0) { | |
3636 | if (getenv("GIT_TEST_VALIDATE_INDEX_CACHE_ENTRIES")) | |
3637 | validate_index_cache_entries = 1; | |
3638 | else | |
3639 | validate_index_cache_entries = 0; | |
3640 | } | |
3641 | ||
3642 | return validate_index_cache_entries; | |
3643 | } | |
3b1d9e04 BP |
3644 | |
3645 | #define EOIE_SIZE (4 + GIT_SHA1_RAWSZ) /* <4-byte offset> + <20-byte hash> */ | |
3646 | #define EOIE_SIZE_WITH_HEADER (4 + 4 + EOIE_SIZE) /* <4-byte signature> + <4-byte length> + EOIE_SIZE */ | |
3647 | ||
3648 | static size_t read_eoie_extension(const char *mmap, size_t mmap_size) | |
3649 | { | |
3650 | /* | |
3651 | * The end of index entries (EOIE) extension is guaranteed to be last | |
3652 | * so that it can be found by scanning backwards from the EOF. | |
3653 | * | |
3654 | * "EOIE" | |
3655 | * <4-byte length> | |
3656 | * <4-byte offset> | |
3657 | * <20-byte hash> | |
3658 | */ | |
3659 | const char *index, *eoie; | |
3660 | uint32_t extsize; | |
3661 | size_t offset, src_offset; | |
3662 | unsigned char hash[GIT_MAX_RAWSZ]; | |
3663 | git_hash_ctx c; | |
3664 | ||
3665 | /* ensure we have an index big enough to contain an EOIE extension */ | |
3666 | if (mmap_size < sizeof(struct cache_header) + EOIE_SIZE_WITH_HEADER + the_hash_algo->rawsz) | |
3667 | return 0; | |
3668 | ||
3669 | /* validate the extension signature */ | |
3670 | index = eoie = mmap + mmap_size - EOIE_SIZE_WITH_HEADER - the_hash_algo->rawsz; | |
3671 | if (CACHE_EXT(index) != CACHE_EXT_ENDOFINDEXENTRIES) | |
3672 | return 0; | |
3673 | index += sizeof(uint32_t); | |
3674 | ||
3675 | /* validate the extension size */ | |
3676 | extsize = get_be32(index); | |
3677 | if (extsize != EOIE_SIZE) | |
3678 | return 0; | |
3679 | index += sizeof(uint32_t); | |
3680 | ||
3681 | /* | |
3682 | * Validate the offset we're going to look for the first extension | |
3683 | * signature is after the index header and before the eoie extension. | |
3684 | */ | |
3685 | offset = get_be32(index); | |
3686 | if (mmap + offset < mmap + sizeof(struct cache_header)) | |
3687 | return 0; | |
3688 | if (mmap + offset >= eoie) | |
3689 | return 0; | |
3690 | index += sizeof(uint32_t); | |
3691 | ||
3692 | /* | |
3693 | * The hash is computed over extension types and their sizes (but not | |
3694 | * their contents). E.g. if we have "TREE" extension that is N-bytes | |
3695 | * long, "REUC" extension that is M-bytes long, followed by "EOIE", | |
3696 | * then the hash would be: | |
3697 | * | |
3698 | * SHA-1("TREE" + <binary representation of N> + | |
3699 | * "REUC" + <binary representation of M>) | |
3700 | */ | |
3701 | src_offset = offset; | |
3702 | the_hash_algo->init_fn(&c); | |
3703 | while (src_offset < mmap_size - the_hash_algo->rawsz - EOIE_SIZE_WITH_HEADER) { | |
3704 | /* After an array of active_nr index entries, | |
3705 | * there can be arbitrary number of extended | |
3706 | * sections, each of which is prefixed with | |
3707 | * extension name (4-byte) and section length | |
3708 | * in 4-byte network byte order. | |
3709 | */ | |
3710 | uint32_t extsize; | |
3711 | memcpy(&extsize, mmap + src_offset + 4, 4); | |
3712 | extsize = ntohl(extsize); | |
3713 | ||
3714 | /* verify the extension size isn't so large it will wrap around */ | |
3715 | if (src_offset + 8 + extsize < src_offset) | |
3716 | return 0; | |
3717 | ||
3718 | the_hash_algo->update_fn(&c, mmap + src_offset, 8); | |
3719 | ||
3720 | src_offset += 8; | |
3721 | src_offset += extsize; | |
3722 | } | |
3723 | the_hash_algo->final_fn(hash, &c); | |
3724 | if (!hasheq(hash, (const unsigned char *)index)) | |
3725 | return 0; | |
3726 | ||
3727 | /* Validate that the extension offsets returned us back to the eoie extension. */ | |
3728 | if (src_offset != mmap_size - the_hash_algo->rawsz - EOIE_SIZE_WITH_HEADER) | |
3729 | return 0; | |
3730 | ||
3731 | return offset; | |
3732 | } | |
3733 | ||
3734 | static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset) | |
3735 | { | |
3736 | uint32_t buffer; | |
3737 | unsigned char hash[GIT_MAX_RAWSZ]; | |
3738 | ||
3739 | /* offset */ | |
3740 | put_be32(&buffer, offset); | |
3741 | strbuf_add(sb, &buffer, sizeof(uint32_t)); | |
3742 | ||
3743 | /* hash */ | |
3744 | the_hash_algo->final_fn(hash, eoie_context); | |
3745 | strbuf_add(sb, hash, the_hash_algo->rawsz); | |
3746 | } | |
3255089a | 3747 | |
3255089a BP |
3748 | #define IEOT_VERSION (1) |
3749 | ||
3750 | static struct index_entry_offset_table *read_ieot_extension(const char *mmap, size_t mmap_size, size_t offset) | |
3751 | { | |
ec36c42a NTND |
3752 | const char *index = NULL; |
3753 | uint32_t extsize, ext_version; | |
3754 | struct index_entry_offset_table *ieot; | |
3755 | int i, nr; | |
3756 | ||
3757 | /* find the IEOT extension */ | |
3758 | if (!offset) | |
3759 | return NULL; | |
3760 | while (offset <= mmap_size - the_hash_algo->rawsz - 8) { | |
3761 | extsize = get_be32(mmap + offset + 4); | |
3762 | if (CACHE_EXT((mmap + offset)) == CACHE_EXT_INDEXENTRYOFFSETTABLE) { | |
3763 | index = mmap + offset + 4 + 4; | |
3764 | break; | |
3765 | } | |
3766 | offset += 8; | |
3767 | offset += extsize; | |
3768 | } | |
3769 | if (!index) | |
3770 | return NULL; | |
3771 | ||
3772 | /* validate the version is IEOT_VERSION */ | |
3773 | ext_version = get_be32(index); | |
3774 | if (ext_version != IEOT_VERSION) { | |
3775 | error("invalid IEOT version %d", ext_version); | |
3776 | return NULL; | |
3777 | } | |
3778 | index += sizeof(uint32_t); | |
3779 | ||
3780 | /* extension size - version bytes / bytes per entry */ | |
3781 | nr = (extsize - sizeof(uint32_t)) / (sizeof(uint32_t) + sizeof(uint32_t)); | |
3782 | if (!nr) { | |
3783 | error("invalid number of IEOT entries %d", nr); | |
3784 | return NULL; | |
3785 | } | |
3786 | ieot = xmalloc(sizeof(struct index_entry_offset_table) | |
3787 | + (nr * sizeof(struct index_entry_offset))); | |
3788 | ieot->nr = nr; | |
3789 | for (i = 0; i < nr; i++) { | |
3790 | ieot->entries[i].offset = get_be32(index); | |
3791 | index += sizeof(uint32_t); | |
3792 | ieot->entries[i].nr = get_be32(index); | |
3793 | index += sizeof(uint32_t); | |
3794 | } | |
3795 | ||
3796 | return ieot; | |
3255089a BP |
3797 | } |
3798 | ||
3799 | static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot) | |
3800 | { | |
ec36c42a NTND |
3801 | uint32_t buffer; |
3802 | int i; | |
3255089a | 3803 | |
ec36c42a NTND |
3804 | /* version */ |
3805 | put_be32(&buffer, IEOT_VERSION); | |
3806 | strbuf_add(sb, &buffer, sizeof(uint32_t)); | |
3255089a | 3807 | |
ec36c42a NTND |
3808 | /* ieot */ |
3809 | for (i = 0; i < ieot->nr; i++) { | |
3255089a | 3810 | |
ec36c42a NTND |
3811 | /* offset */ |
3812 | put_be32(&buffer, ieot->entries[i].offset); | |
3813 | strbuf_add(sb, &buffer, sizeof(uint32_t)); | |
3255089a | 3814 | |
ec36c42a NTND |
3815 | /* count */ |
3816 | put_be32(&buffer, ieot->entries[i].nr); | |
3817 | strbuf_add(sb, &buffer, sizeof(uint32_t)); | |
3818 | } | |
3255089a | 3819 | } |
b2896d27 JT |
3820 | |
3821 | void prefetch_cache_entries(const struct index_state *istate, | |
3822 | must_prefetch_predicate must_prefetch) | |
3823 | { | |
3824 | int i; | |
3825 | struct oid_array to_fetch = OID_ARRAY_INIT; | |
3826 | ||
3827 | for (i = 0; i < istate->cache_nr; i++) { | |
3828 | struct cache_entry *ce = istate->cache[i]; | |
3829 | ||
3830 | if (S_ISGITLINK(ce->ce_mode) || !must_prefetch(ce)) | |
3831 | continue; | |
3832 | if (!oid_object_info_extended(the_repository, &ce->oid, | |
3833 | NULL, | |
3834 | OBJECT_INFO_FOR_PREFETCH)) | |
3835 | continue; | |
3836 | oid_array_append(&to_fetch, &ce->oid); | |
3837 | } | |
3838 | promisor_remote_get_direct(the_repository, | |
3839 | to_fetch.oid, to_fetch.nr); | |
3840 | oid_array_clear(&to_fetch); | |
3841 | } |