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