]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
4aab5b46 | 6 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
e83c5163 | 7 | #include "cache.h" |
697cc8ef | 8 | #include "lockfile.h" |
bad68ec9 | 9 | #include "cache-tree.h" |
f35a6d3b | 10 | #include "refs.h" |
d616813d | 11 | #include "dir.h" |
041aee31 JH |
12 | #include "tree.h" |
13 | #include "commit.h" | |
39425819 | 14 | #include "blob.h" |
cfc5789a | 15 | #include "resolve-undo.h" |
6c9cd161 JH |
16 | #include "strbuf.h" |
17 | #include "varint.h" | |
5fc2fc8f | 18 | #include "split-index.h" |
c18b80a0 | 19 | #include "sigchain.h" |
a42643aa | 20 | #include "utf8.h" |
bad68ec9 | 21 | |
25762726 BK |
22 | static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, |
23 | unsigned int options); | |
87b29e5a | 24 | |
b60e188c TG |
25 | /* Mask for the name length in ce_flags in the on-disk index */ |
26 | ||
27 | #define CE_NAMEMASK (0x0fff) | |
28 | ||
bad68ec9 JH |
29 | /* Index extensions. |
30 | * | |
31 | * The first letter should be 'A'..'Z' for extensions that are not | |
32 | * necessary for a correct operation (i.e. optimization data). | |
33 | * When new extensions are added that _needs_ to be understood in | |
34 | * order to correctly interpret the index file, pick character that | |
35 | * is outside the range, to cause the reader to abort. | |
36 | */ | |
37 | ||
38 | #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) | |
39 | #define CACHE_EXT_TREE 0x54524545 /* "TREE" */ | |
b659b49b | 40 | #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */ |
5fc2fc8f NTND |
41 | #define CACHE_EXT_LINK 0x6c696e6b /* "link" */ |
42 | ||
43 | /* changes that can be kept in $GIT_DIR/index (basically all extensions) */ | |
e0cf0d7d | 44 | #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \ |
c18b80a0 NTND |
45 | CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \ |
46 | SPLIT_INDEX_ORDERED) | |
e83c5163 | 47 | |
228e94f9 | 48 | struct index_state the_index; |
626f35c8 | 49 | static const char *alternate_index_output; |
8fd2cb40 | 50 | |
9cb76b8c JH |
51 | static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce) |
52 | { | |
53 | istate->cache[nr] = ce; | |
96872bc2 | 54 | add_name_hash(istate, ce); |
9cb76b8c JH |
55 | } |
56 | ||
cf558704 LT |
57 | static void replace_index_entry(struct index_state *istate, int nr, struct cache_entry *ce) |
58 | { | |
59 | struct cache_entry *old = istate->cache[nr]; | |
60 | ||
078a58e8 | 61 | replace_index_entry_in_base(istate, old, ce); |
2092678c | 62 | remove_name_hash(istate, old); |
5699d17e | 63 | free(old); |
a22c6371 | 64 | set_index_entry(istate, nr, ce); |
078a58e8 | 65 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
e636a7b4 | 66 | istate->cache_changed |= CE_ENTRY_CHANGED; |
cf558704 LT |
67 | } |
68 | ||
81dc2307 PB |
69 | void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name) |
70 | { | |
71 | struct cache_entry *old = istate->cache[nr], *new; | |
72 | int namelen = strlen(new_name); | |
73 | ||
74 | new = xmalloc(cache_entry_size(namelen)); | |
75 | copy_cache_entry(new, old); | |
419a597f | 76 | new->ce_flags &= ~CE_HASHED; |
b60e188c | 77 | new->ce_namelen = namelen; |
5fc2fc8f | 78 | new->index = 0; |
81dc2307 PB |
79 | memcpy(new->name, new_name, namelen + 1); |
80 | ||
a5400efe | 81 | cache_tree_invalidate_path(istate, old->name); |
81dc2307 PB |
82 | remove_index_entry_at(istate, nr); |
83 | add_index_entry(istate, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); | |
84 | } | |
85 | ||
c21d39d7 MH |
86 | void fill_stat_data(struct stat_data *sd, struct stat *st) |
87 | { | |
88 | sd->sd_ctime.sec = (unsigned int)st->st_ctime; | |
89 | sd->sd_mtime.sec = (unsigned int)st->st_mtime; | |
90 | sd->sd_ctime.nsec = ST_CTIME_NSEC(*st); | |
91 | sd->sd_mtime.nsec = ST_MTIME_NSEC(*st); | |
92 | sd->sd_dev = st->st_dev; | |
93 | sd->sd_ino = st->st_ino; | |
94 | sd->sd_uid = st->st_uid; | |
95 | sd->sd_gid = st->st_gid; | |
96 | sd->sd_size = st->st_size; | |
97 | } | |
98 | ||
99 | int match_stat_data(const struct stat_data *sd, struct stat *st) | |
100 | { | |
101 | int changed = 0; | |
102 | ||
103 | if (sd->sd_mtime.sec != (unsigned int)st->st_mtime) | |
104 | changed |= MTIME_CHANGED; | |
105 | if (trust_ctime && check_stat && | |
106 | sd->sd_ctime.sec != (unsigned int)st->st_ctime) | |
107 | changed |= CTIME_CHANGED; | |
108 | ||
109 | #ifdef USE_NSEC | |
110 | if (check_stat && sd->sd_mtime.nsec != ST_MTIME_NSEC(*st)) | |
111 | changed |= MTIME_CHANGED; | |
112 | if (trust_ctime && check_stat && | |
113 | sd->sd_ctime.nsec != ST_CTIME_NSEC(*st)) | |
114 | changed |= CTIME_CHANGED; | |
115 | #endif | |
116 | ||
117 | if (check_stat) { | |
118 | if (sd->sd_uid != (unsigned int) st->st_uid || | |
119 | sd->sd_gid != (unsigned int) st->st_gid) | |
120 | changed |= OWNER_CHANGED; | |
121 | if (sd->sd_ino != (unsigned int) st->st_ino) | |
122 | changed |= INODE_CHANGED; | |
123 | } | |
124 | ||
125 | #ifdef USE_STDEV | |
126 | /* | |
127 | * st_dev breaks on network filesystems where different | |
128 | * clients will have different views of what "device" | |
129 | * the filesystem is on | |
130 | */ | |
131 | if (check_stat && sd->sd_dev != (unsigned int) st->st_dev) | |
132 | changed |= INODE_CHANGED; | |
133 | #endif | |
134 | ||
135 | if (sd->sd_size != (unsigned int) st->st_size) | |
136 | changed |= DATA_CHANGED; | |
137 | ||
138 | return changed; | |
139 | } | |
140 | ||
415e96c8 JH |
141 | /* |
142 | * This only updates the "non-critical" parts of the directory | |
143 | * cache, ie the parts that aren't tracked by GIT, and only used | |
144 | * to validate the cache. | |
145 | */ | |
146 | void fill_stat_cache_info(struct cache_entry *ce, struct stat *st) | |
147 | { | |
c21d39d7 | 148 | fill_stat_data(&ce->ce_stat_data, st); |
5f73076c JH |
149 | |
150 | if (assume_unchanged) | |
7a51ed66 | 151 | ce->ce_flags |= CE_VALID; |
eadb5831 JH |
152 | |
153 | if (S_ISREG(st->st_mode)) | |
154 | ce_mark_uptodate(ce); | |
415e96c8 JH |
155 | } |
156 | ||
21a6b9fa | 157 | static int ce_compare_data(const struct cache_entry *ce, struct stat *st) |
29e4d363 JH |
158 | { |
159 | int match = -1; | |
160 | int fd = open(ce->name, O_RDONLY); | |
161 | ||
162 | if (fd >= 0) { | |
163 | unsigned char sha1[20]; | |
c4ce46fc | 164 | if (!index_fd(sha1, fd, st, OBJ_BLOB, ce->name, 0)) |
a89fccd2 | 165 | match = hashcmp(sha1, ce->sha1); |
7f8508e8 | 166 | /* index_fd() closed the file descriptor already */ |
29e4d363 JH |
167 | } |
168 | return match; | |
169 | } | |
170 | ||
21a6b9fa | 171 | static int ce_compare_link(const struct cache_entry *ce, size_t expected_size) |
29e4d363 JH |
172 | { |
173 | int match = -1; | |
29e4d363 JH |
174 | void *buffer; |
175 | unsigned long size; | |
21666f1a | 176 | enum object_type type; |
a60272b3 | 177 | struct strbuf sb = STRBUF_INIT; |
29e4d363 | 178 | |
a60272b3 | 179 | if (strbuf_readlink(&sb, ce->name, expected_size)) |
29e4d363 | 180 | return -1; |
a60272b3 | 181 | |
21666f1a | 182 | buffer = read_sha1_file(ce->sha1, &type, &size); |
a60272b3 LT |
183 | if (buffer) { |
184 | if (size == sb.len) | |
185 | match = memcmp(buffer, sb.buf, size); | |
186 | free(buffer); | |
29e4d363 | 187 | } |
a60272b3 | 188 | strbuf_release(&sb); |
29e4d363 JH |
189 | return match; |
190 | } | |
191 | ||
21a6b9fa | 192 | static int ce_compare_gitlink(const struct cache_entry *ce) |
f35a6d3b LT |
193 | { |
194 | unsigned char sha1[20]; | |
195 | ||
196 | /* | |
197 | * We don't actually require that the .git directory | |
302b9282 | 198 | * under GITLINK directory be a valid git directory. It |
f35a6d3b LT |
199 | * might even be missing (in case nobody populated that |
200 | * sub-project). | |
201 | * | |
202 | * If so, we consider it always to match. | |
203 | */ | |
204 | if (resolve_gitlink_ref(ce->name, "HEAD", sha1) < 0) | |
205 | return 0; | |
206 | return hashcmp(sha1, ce->sha1); | |
207 | } | |
208 | ||
21a6b9fa | 209 | static int ce_modified_check_fs(const struct cache_entry *ce, struct stat *st) |
29e4d363 JH |
210 | { |
211 | switch (st->st_mode & S_IFMT) { | |
212 | case S_IFREG: | |
213 | if (ce_compare_data(ce, st)) | |
214 | return DATA_CHANGED; | |
215 | break; | |
216 | case S_IFLNK: | |
dc49cd76 | 217 | if (ce_compare_link(ce, xsize_t(st->st_size))) |
29e4d363 JH |
218 | return DATA_CHANGED; |
219 | break; | |
a8ee75bc | 220 | case S_IFDIR: |
7a51ed66 | 221 | if (S_ISGITLINK(ce->ce_mode)) |
c70115b4 | 222 | return ce_compare_gitlink(ce) ? DATA_CHANGED : 0; |
29e4d363 JH |
223 | default: |
224 | return TYPE_CHANGED; | |
225 | } | |
226 | return 0; | |
227 | } | |
228 | ||
21a6b9fa | 229 | static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st) |
734aab75 LT |
230 | { |
231 | unsigned int changed = 0; | |
232 | ||
7a51ed66 LT |
233 | if (ce->ce_flags & CE_REMOVE) |
234 | return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; | |
235 | ||
236 | switch (ce->ce_mode & S_IFMT) { | |
8ae0a8c5 KS |
237 | case S_IFREG: |
238 | changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0; | |
3e09cdfd JH |
239 | /* We consider only the owner x bit to be relevant for |
240 | * "mode changes" | |
241 | */ | |
242 | if (trust_executable_bit && | |
7a51ed66 | 243 | (0100 & (ce->ce_mode ^ st->st_mode))) |
ffbe1add | 244 | changed |= MODE_CHANGED; |
8ae0a8c5 KS |
245 | break; |
246 | case S_IFLNK: | |
78a8d641 JS |
247 | if (!S_ISLNK(st->st_mode) && |
248 | (has_symlinks || !S_ISREG(st->st_mode))) | |
249 | changed |= TYPE_CHANGED; | |
8ae0a8c5 | 250 | break; |
302b9282 | 251 | case S_IFGITLINK: |
c70115b4 | 252 | /* We ignore most of the st_xxx fields for gitlinks */ |
f35a6d3b LT |
253 | if (!S_ISDIR(st->st_mode)) |
254 | changed |= TYPE_CHANGED; | |
255 | else if (ce_compare_gitlink(ce)) | |
256 | changed |= DATA_CHANGED; | |
a8ee75bc | 257 | return changed; |
8ae0a8c5 | 258 | default: |
7a51ed66 | 259 | die("internal error: ce_mode is %o", ce->ce_mode); |
8ae0a8c5 | 260 | } |
2cb45e95 | 261 | |
c21d39d7 | 262 | changed |= match_stat_data(&ce->ce_stat_data, st); |
b0391890 | 263 | |
f49c2c22 | 264 | /* Racily smudged entry? */ |
c21d39d7 | 265 | if (!ce->ce_stat_data.sd_size) { |
f49c2c22 LT |
266 | if (!is_empty_blob_sha1(ce->sha1)) |
267 | changed |= DATA_CHANGED; | |
268 | } | |
269 | ||
407c8eb0 JH |
270 | return changed; |
271 | } | |
272 | ||
21a6b9fa RS |
273 | static int is_racy_timestamp(const struct index_state *istate, |
274 | const struct cache_entry *ce) | |
6d91da6d | 275 | { |
050288d5 | 276 | return (!S_ISGITLINK(ce->ce_mode) && |
fba2f38a KB |
277 | istate->timestamp.sec && |
278 | #ifdef USE_NSEC | |
279 | /* nanosecond timestamped files can also be racy! */ | |
c21d39d7 MH |
280 | (istate->timestamp.sec < ce->ce_stat_data.sd_mtime.sec || |
281 | (istate->timestamp.sec == ce->ce_stat_data.sd_mtime.sec && | |
282 | istate->timestamp.nsec <= ce->ce_stat_data.sd_mtime.nsec)) | |
fba2f38a | 283 | #else |
c21d39d7 | 284 | istate->timestamp.sec <= ce->ce_stat_data.sd_mtime.sec |
fba2f38a KB |
285 | #endif |
286 | ); | |
6d91da6d JH |
287 | } |
288 | ||
d1f128b0 | 289 | int ie_match_stat(const struct index_state *istate, |
21a6b9fa | 290 | const struct cache_entry *ce, struct stat *st, |
4bd5b7da | 291 | unsigned int options) |
407c8eb0 | 292 | { |
5f73076c | 293 | unsigned int changed; |
4bd5b7da | 294 | int ignore_valid = options & CE_MATCH_IGNORE_VALID; |
56cac48c | 295 | int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; |
4bd5b7da | 296 | int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; |
5f73076c JH |
297 | |
298 | /* | |
299 | * If it's marked as always valid in the index, it's | |
300 | * valid whatever the checked-out copy says. | |
56cac48c NTND |
301 | * |
302 | * skip-worktree has the same effect with higher precedence | |
5f73076c | 303 | */ |
56cac48c NTND |
304 | if (!ignore_skip_worktree && ce_skip_worktree(ce)) |
305 | return 0; | |
7a51ed66 | 306 | if (!ignore_valid && (ce->ce_flags & CE_VALID)) |
5f73076c JH |
307 | return 0; |
308 | ||
331fcb59 JH |
309 | /* |
310 | * Intent-to-add entries have not been added, so the index entry | |
311 | * by definition never matches what is in the work tree until it | |
312 | * actually gets added. | |
313 | */ | |
314 | if (ce->ce_flags & CE_INTENT_TO_ADD) | |
315 | return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; | |
316 | ||
5f73076c | 317 | changed = ce_match_stat_basic(ce, st); |
407c8eb0 | 318 | |
29e4d363 JH |
319 | /* |
320 | * Within 1 second of this sequence: | |
321 | * echo xyzzy >file && git-update-index --add file | |
322 | * running this command: | |
323 | * echo frotz >file | |
324 | * would give a falsely clean cache entry. The mtime and | |
325 | * length match the cache, and other stat fields do not change. | |
326 | * | |
327 | * We could detect this at update-index time (the cache entry | |
328 | * being registered/updated records the same time as "now") | |
329 | * and delay the return from git-update-index, but that would | |
330 | * effectively mean we can make at most one commit per second, | |
331 | * which is not acceptable. Instead, we check cache entries | |
332 | * whose mtime are the same as the index file timestamp more | |
5f73076c | 333 | * carefully than others. |
29e4d363 | 334 | */ |
6d91da6d | 335 | if (!changed && is_racy_timestamp(istate, ce)) { |
42f77406 JH |
336 | if (assume_racy_is_modified) |
337 | changed |= DATA_CHANGED; | |
338 | else | |
339 | changed |= ce_modified_check_fs(ce, st); | |
340 | } | |
b0391890 | 341 | |
29e4d363 | 342 | return changed; |
b0391890 JH |
343 | } |
344 | ||
d1f128b0 | 345 | int ie_modified(const struct index_state *istate, |
21a6b9fa RS |
346 | const struct cache_entry *ce, |
347 | struct stat *st, unsigned int options) | |
b0391890 | 348 | { |
29e4d363 | 349 | int changed, changed_fs; |
4bd5b7da JH |
350 | |
351 | changed = ie_match_stat(istate, ce, st, options); | |
b0391890 JH |
352 | if (!changed) |
353 | return 0; | |
b0391890 JH |
354 | /* |
355 | * If the mode or type has changed, there's no point in trying | |
356 | * to refresh the entry - it's not going to match | |
357 | */ | |
358 | if (changed & (MODE_CHANGED | TYPE_CHANGED)) | |
359 | return changed; | |
360 | ||
c70115b4 JH |
361 | /* |
362 | * Immediately after read-tree or update-index --cacheinfo, | |
363 | * the length field is zero, as we have never even read the | |
364 | * lstat(2) information once, and we cannot trust DATA_CHANGED | |
365 | * returned by ie_match_stat() which in turn was returned by | |
366 | * ce_match_stat_basic() to signal that the filesize of the | |
367 | * blob changed. We have to actually go to the filesystem to | |
368 | * see if the contents match, and if so, should answer "unchanged". | |
369 | * | |
370 | * The logic does not apply to gitlinks, as ce_match_stat_basic() | |
371 | * already has checked the actual HEAD from the filesystem in the | |
372 | * subproject. If ie_match_stat() already said it is different, | |
373 | * then we know it is. | |
b0391890 | 374 | */ |
c70115b4 | 375 | if ((changed & DATA_CHANGED) && |
c21d39d7 | 376 | (S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0)) |
b0391890 JH |
377 | return changed; |
378 | ||
29e4d363 JH |
379 | changed_fs = ce_modified_check_fs(ce, st); |
380 | if (changed_fs) | |
381 | return changed | changed_fs; | |
b0391890 JH |
382 | return 0; |
383 | } | |
384 | ||
958ba6c9 LT |
385 | int base_name_compare(const char *name1, int len1, int mode1, |
386 | const char *name2, int len2, int mode2) | |
387 | { | |
388 | unsigned char c1, c2; | |
389 | int len = len1 < len2 ? len1 : len2; | |
390 | int cmp; | |
391 | ||
392 | cmp = memcmp(name1, name2, len); | |
393 | if (cmp) | |
394 | return cmp; | |
395 | c1 = name1[len]; | |
396 | c2 = name2[len]; | |
1833a925 | 397 | if (!c1 && S_ISDIR(mode1)) |
958ba6c9 | 398 | c1 = '/'; |
1833a925 | 399 | if (!c2 && S_ISDIR(mode2)) |
958ba6c9 LT |
400 | c2 = '/'; |
401 | return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; | |
402 | } | |
403 | ||
0ab9e1e8 LT |
404 | /* |
405 | * df_name_compare() is identical to base_name_compare(), except it | |
406 | * compares conflicting directory/file entries as equal. Note that | |
407 | * while a directory name compares as equal to a regular file, they | |
408 | * then individually compare _differently_ to a filename that has | |
409 | * a dot after the basename (because '\0' < '.' < '/'). | |
410 | * | |
411 | * This is used by routines that want to traverse the git namespace | |
412 | * but then handle conflicting entries together when possible. | |
413 | */ | |
414 | int df_name_compare(const char *name1, int len1, int mode1, | |
415 | const char *name2, int len2, int mode2) | |
416 | { | |
417 | int len = len1 < len2 ? len1 : len2, cmp; | |
418 | unsigned char c1, c2; | |
419 | ||
420 | cmp = memcmp(name1, name2, len); | |
421 | if (cmp) | |
422 | return cmp; | |
423 | /* Directories and files compare equal (same length, same name) */ | |
424 | if (len1 == len2) | |
425 | return 0; | |
426 | c1 = name1[len]; | |
427 | if (!c1 && S_ISDIR(mode1)) | |
428 | c1 = '/'; | |
429 | c2 = name2[len]; | |
430 | if (!c2 && S_ISDIR(mode2)) | |
431 | c2 = '/'; | |
432 | if (c1 == '/' && !c2) | |
433 | return 0; | |
434 | if (c2 == '/' && !c1) | |
435 | return 0; | |
436 | return c1 - c2; | |
437 | } | |
438 | ||
ccdd4a0f | 439 | int name_compare(const char *name1, size_t len1, const char *name2, size_t len2) |
eb38c22f | 440 | { |
ccdd4a0f JM |
441 | size_t min_len = (len1 < len2) ? len1 : len2; |
442 | int cmp = memcmp(name1, name2, min_len); | |
eb38c22f LT |
443 | if (cmp) |
444 | return cmp; | |
445 | if (len1 < len2) | |
446 | return -1; | |
447 | if (len1 > len2) | |
448 | return 1; | |
ccdd4a0f JM |
449 | return 0; |
450 | } | |
451 | ||
452 | int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2) | |
453 | { | |
454 | int cmp; | |
455 | ||
456 | cmp = name_compare(name1, len1, name2, len2); | |
457 | if (cmp) | |
458 | return cmp; | |
5f73076c | 459 | |
b60e188c | 460 | if (stage1 < stage2) |
95fd5bf8 | 461 | return -1; |
b60e188c | 462 | if (stage1 > stage2) |
95fd5bf8 | 463 | return 1; |
eb38c22f LT |
464 | return 0; |
465 | } | |
466 | ||
357e9c69 | 467 | static int index_name_stage_pos(const struct index_state *istate, const char *name, int namelen, int stage) |
eb38c22f LT |
468 | { |
469 | int first, last; | |
470 | ||
471 | first = 0; | |
4aab5b46 | 472 | last = istate->cache_nr; |
eb38c22f LT |
473 | while (last > first) { |
474 | int next = (last + first) >> 1; | |
4aab5b46 | 475 | struct cache_entry *ce = istate->cache[next]; |
b60e188c | 476 | int cmp = cache_name_stage_compare(name, namelen, stage, ce->name, ce_namelen(ce), ce_stage(ce)); |
eb38c22f | 477 | if (!cmp) |
76e7f4ec | 478 | return next; |
eb38c22f LT |
479 | if (cmp < 0) { |
480 | last = next; | |
481 | continue; | |
482 | } | |
483 | first = next+1; | |
484 | } | |
76e7f4ec | 485 | return -first-1; |
eb38c22f LT |
486 | } |
487 | ||
b60e188c TG |
488 | int index_name_pos(const struct index_state *istate, const char *name, int namelen) |
489 | { | |
490 | return index_name_stage_pos(istate, name, namelen, 0); | |
491 | } | |
492 | ||
7b937ca3 | 493 | /* Remove entry, return true if there are more entries to go.. */ |
4aab5b46 | 494 | int remove_index_entry_at(struct index_state *istate, int pos) |
7b937ca3 | 495 | { |
cf558704 LT |
496 | struct cache_entry *ce = istate->cache[pos]; |
497 | ||
cfc5789a | 498 | record_resolve_undo(istate, ce); |
2092678c | 499 | remove_name_hash(istate, ce); |
045113a5 | 500 | save_or_free_index_entry(istate, ce); |
e636a7b4 | 501 | istate->cache_changed |= CE_ENTRY_REMOVED; |
4aab5b46 JH |
502 | istate->cache_nr--; |
503 | if (pos >= istate->cache_nr) | |
7b937ca3 | 504 | return 0; |
4aab5b46 JH |
505 | memmove(istate->cache + pos, |
506 | istate->cache + pos + 1, | |
507 | (istate->cache_nr - pos) * sizeof(struct cache_entry *)); | |
7b937ca3 LT |
508 | return 1; |
509 | } | |
510 | ||
36419c8e | 511 | /* |
98e023de | 512 | * Remove all cache entries marked for removal, that is where |
36419c8e KB |
513 | * CE_REMOVE is set in ce_flags. This is much more effective than |
514 | * calling remove_index_entry_at() for each entry to be removed. | |
515 | */ | |
516 | void remove_marked_cache_entries(struct index_state *istate) | |
517 | { | |
518 | struct cache_entry **ce_array = istate->cache; | |
519 | unsigned int i, j; | |
520 | ||
521 | for (i = j = 0; i < istate->cache_nr; i++) { | |
5699d17e | 522 | if (ce_array[i]->ce_flags & CE_REMOVE) { |
2092678c | 523 | remove_name_hash(istate, ce_array[i]); |
045113a5 | 524 | save_or_free_index_entry(istate, ce_array[i]); |
5699d17e | 525 | } |
36419c8e KB |
526 | else |
527 | ce_array[j++] = ce_array[i]; | |
528 | } | |
ad837d9e NTND |
529 | if (j == istate->cache_nr) |
530 | return; | |
e636a7b4 | 531 | istate->cache_changed |= CE_ENTRY_REMOVED; |
36419c8e KB |
532 | istate->cache_nr = j; |
533 | } | |
534 | ||
4aab5b46 | 535 | int remove_file_from_index(struct index_state *istate, const char *path) |
197ee8c9 | 536 | { |
4aab5b46 | 537 | int pos = index_name_pos(istate, path, strlen(path)); |
c4e3cca1 JH |
538 | if (pos < 0) |
539 | pos = -pos-1; | |
a5400efe | 540 | cache_tree_invalidate_path(istate, path); |
4aab5b46 JH |
541 | while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) |
542 | remove_index_entry_at(istate, pos); | |
197ee8c9 LT |
543 | return 0; |
544 | } | |
545 | ||
20314271 JS |
546 | static int compare_name(struct cache_entry *ce, const char *path, int namelen) |
547 | { | |
548 | return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen); | |
549 | } | |
550 | ||
551 | static int index_name_pos_also_unmerged(struct index_state *istate, | |
552 | const char *path, int namelen) | |
553 | { | |
554 | int pos = index_name_pos(istate, path, namelen); | |
555 | struct cache_entry *ce; | |
556 | ||
557 | if (pos >= 0) | |
558 | return pos; | |
559 | ||
560 | /* maybe unmerged? */ | |
561 | pos = -1 - pos; | |
562 | if (pos >= istate->cache_nr || | |
563 | compare_name((ce = istate->cache[pos]), path, namelen)) | |
564 | return -1; | |
565 | ||
566 | /* order of preference: stage 2, 1, 3 */ | |
567 | if (ce_stage(ce) == 1 && pos + 1 < istate->cache_nr && | |
568 | ce_stage((ce = istate->cache[pos + 1])) == 2 && | |
569 | !compare_name(ce, path, namelen)) | |
570 | pos++; | |
571 | return pos; | |
572 | } | |
573 | ||
1102952b LT |
574 | static int different_name(struct cache_entry *ce, struct cache_entry *alias) |
575 | { | |
576 | int len = ce_namelen(ce); | |
577 | return ce_namelen(alias) != len || memcmp(ce->name, alias->name, len); | |
578 | } | |
579 | ||
580 | /* | |
581 | * If we add a filename that aliases in the cache, we will use the | |
582 | * name that we already have - but we don't want to update the same | |
583 | * alias twice, because that implies that there were actually two | |
584 | * different files with aliasing names! | |
585 | * | |
586 | * So we use the CE_ADDED flag to verify that the alias was an old | |
587 | * one before we accept it as | |
588 | */ | |
045113a5 NTND |
589 | static struct cache_entry *create_alias_ce(struct index_state *istate, |
590 | struct cache_entry *ce, | |
591 | struct cache_entry *alias) | |
1102952b LT |
592 | { |
593 | int len; | |
594 | struct cache_entry *new; | |
595 | ||
596 | if (alias->ce_flags & CE_ADDED) | |
597 | die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); | |
598 | ||
599 | /* Ok, create the new entry using the name of the existing alias */ | |
600 | len = ce_namelen(alias); | |
601 | new = xcalloc(1, cache_entry_size(len)); | |
602 | memcpy(new->name, alias->name, len); | |
603 | copy_cache_entry(new, ce); | |
045113a5 | 604 | save_or_free_index_entry(istate, ce); |
1102952b LT |
605 | return new; |
606 | } | |
607 | ||
b4b313f9 | 608 | void set_object_name_for_intent_to_add_entry(struct cache_entry *ce) |
39425819 JH |
609 | { |
610 | unsigned char sha1[20]; | |
611 | if (write_sha1_file("", 0, blob_type, sha1)) | |
612 | die("cannot create an empty blob in the object database"); | |
613 | hashcpy(ce->sha1, sha1); | |
614 | } | |
615 | ||
38ed1d89 | 616 | int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags) |
11be42a4 | 617 | { |
38ed1d89 | 618 | int size, namelen, was_same; |
d177cab0 | 619 | mode_t st_mode = st->st_mode; |
6835550d | 620 | struct cache_entry *ce, *alias; |
56cac48c | 621 | unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; |
38ed1d89 JH |
622 | int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); |
623 | int pretend = flags & ADD_CACHE_PRETEND; | |
39425819 JH |
624 | int intent_only = flags & ADD_CACHE_INTENT; |
625 | int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| | |
626 | (intent_only ? ADD_CACHE_NEW_ONLY : 0)); | |
11be42a4 | 627 | |
d177cab0 | 628 | if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) |
960b8ad1 | 629 | return error("%s: can only add regular files, symbolic links or git-directories", path); |
11be42a4 JS |
630 | |
631 | namelen = strlen(path); | |
d177cab0 | 632 | if (S_ISDIR(st_mode)) { |
09595258 LT |
633 | while (namelen && path[namelen-1] == '/') |
634 | namelen--; | |
635 | } | |
11be42a4 JS |
636 | size = cache_entry_size(namelen); |
637 | ce = xcalloc(1, size); | |
638 | memcpy(ce->name, path, namelen); | |
b60e188c | 639 | ce->ce_namelen = namelen; |
39425819 JH |
640 | if (!intent_only) |
641 | fill_stat_cache_info(ce, st); | |
388b2acd JH |
642 | else |
643 | ce->ce_flags |= CE_INTENT_TO_ADD; | |
11be42a4 | 644 | |
78a8d641 | 645 | if (trust_executable_bit && has_symlinks) |
d177cab0 | 646 | ce->ce_mode = create_ce_mode(st_mode); |
185c975f | 647 | else { |
78a8d641 JS |
648 | /* If there is an existing entry, pick the mode bits and type |
649 | * from it, otherwise assume unexecutable regular file. | |
11be42a4 | 650 | */ |
185c975f | 651 | struct cache_entry *ent; |
20314271 | 652 | int pos = index_name_pos_also_unmerged(istate, path, namelen); |
185c975f | 653 | |
4aab5b46 | 654 | ent = (0 <= pos) ? istate->cache[pos] : NULL; |
d177cab0 | 655 | ce->ce_mode = ce_mode_from_stat(ent, st_mode); |
11be42a4 JS |
656 | } |
657 | ||
dc1ae704 JJ |
658 | /* When core.ignorecase=true, determine if a directory of the same name but differing |
659 | * case already exists within the Git repository. If it does, ensure the directory | |
660 | * case of the file being added to the repository matches (is folded into) the existing | |
661 | * entry's directory case. | |
662 | */ | |
663 | if (ignore_case) { | |
664 | const char *startPtr = ce->name; | |
665 | const char *ptr = startPtr; | |
666 | while (*ptr) { | |
667 | while (*ptr && *ptr != '/') | |
668 | ++ptr; | |
669 | if (*ptr == '/') { | |
670 | struct cache_entry *foundce; | |
671 | ++ptr; | |
d28eec26 | 672 | foundce = index_dir_exists(istate, ce->name, ptr - ce->name - 1); |
dc1ae704 JJ |
673 | if (foundce) { |
674 | memcpy((void *)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr); | |
675 | startPtr = ptr; | |
676 | } | |
677 | } | |
678 | } | |
679 | } | |
680 | ||
ebbd7439 | 681 | alias = index_file_exists(istate, ce->name, ce_namelen(ce), ignore_case); |
d177cab0 | 682 | if (alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { |
0781b8a9 | 683 | /* Nothing changed, really */ |
125fd984 JH |
684 | if (!S_ISGITLINK(alias->ce_mode)) |
685 | ce_mark_uptodate(alias); | |
1102952b | 686 | alias->ce_flags |= CE_ADDED; |
2d9426b0 SB |
687 | |
688 | free(ce); | |
0781b8a9 JH |
689 | return 0; |
690 | } | |
39425819 | 691 | if (!intent_only) { |
2d9426b0 SB |
692 | if (index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) { |
693 | free(ce); | |
39425819 | 694 | return error("unable to index file %s", path); |
2d9426b0 | 695 | } |
39425819 | 696 | } else |
b4b313f9 | 697 | set_object_name_for_intent_to_add_entry(ce); |
39425819 | 698 | |
1102952b | 699 | if (ignore_case && alias && different_name(ce, alias)) |
045113a5 | 700 | ce = create_alias_ce(istate, ce, alias); |
1102952b | 701 | ce->ce_flags |= CE_ADDED; |
38ed1d89 | 702 | |
3bf0dd1f | 703 | /* It was suspected to be racily clean, but it turns out to be Ok */ |
38ed1d89 JH |
704 | was_same = (alias && |
705 | !ce_stage(alias) && | |
706 | !hashcmp(alias->sha1, ce->sha1) && | |
707 | ce->ce_mode == alias->ce_mode); | |
708 | ||
709 | if (pretend) | |
067178ed JH |
710 | free(ce); |
711 | else if (add_index_entry(istate, ce, add_option)) { | |
712 | free(ce); | |
713 | return error("unable to add %s to index", path); | |
714 | } | |
38ed1d89 | 715 | if (verbose && !was_same) |
11be42a4 | 716 | printf("add '%s'\n", path); |
11be42a4 JS |
717 | return 0; |
718 | } | |
719 | ||
38ed1d89 | 720 | int add_file_to_index(struct index_state *istate, const char *path, int flags) |
d177cab0 LT |
721 | { |
722 | struct stat st; | |
723 | if (lstat(path, &st)) | |
d824cbba | 724 | die_errno("unable to stat '%s'", path); |
38ed1d89 | 725 | return add_to_index(istate, path, &st, flags); |
d177cab0 LT |
726 | } |
727 | ||
6640f881 CR |
728 | struct cache_entry *make_cache_entry(unsigned int mode, |
729 | const unsigned char *sha1, const char *path, int stage, | |
25762726 | 730 | unsigned int refresh_options) |
6640f881 CR |
731 | { |
732 | int size, len; | |
bc1c2caa | 733 | struct cache_entry *ce, *ret; |
6640f881 | 734 | |
7e7abea9 DP |
735 | if (!verify_path(path)) { |
736 | error("Invalid path '%s'", path); | |
6640f881 | 737 | return NULL; |
7e7abea9 | 738 | } |
6640f881 CR |
739 | |
740 | len = strlen(path); | |
741 | size = cache_entry_size(len); | |
742 | ce = xcalloc(1, size); | |
743 | ||
744 | hashcpy(ce->sha1, sha1); | |
745 | memcpy(ce->name, path, len); | |
b60e188c TG |
746 | ce->ce_flags = create_ce_flags(stage); |
747 | ce->ce_namelen = len; | |
6640f881 CR |
748 | ce->ce_mode = create_ce_mode(mode); |
749 | ||
bc1c2caa | 750 | ret = refresh_cache_entry(ce, refresh_options); |
915e44c6 | 751 | if (ret != ce) |
bc1c2caa | 752 | free(ce); |
915e44c6 | 753 | return ret; |
6640f881 CR |
754 | } |
755 | ||
9c5e6c80 | 756 | int ce_same_name(const struct cache_entry *a, const struct cache_entry *b) |
7b937ca3 LT |
757 | { |
758 | int len = ce_namelen(a); | |
759 | return ce_namelen(b) == len && !memcmp(a->name, b->name, len); | |
760 | } | |
761 | ||
8dcf39c4 LT |
762 | /* |
763 | * We fundamentally don't like some paths: we don't want | |
764 | * dot or dot-dot anywhere, and for obvious reasons don't | |
765 | * want to recurse into ".git" either. | |
766 | * | |
767 | * Also, we don't want double slashes or slashes at the | |
768 | * end that can make pathnames ambiguous. | |
769 | */ | |
770 | static int verify_dotfile(const char *rest) | |
771 | { | |
772 | /* | |
773 | * The first character was '.', but that | |
774 | * has already been discarded, we now test | |
775 | * the rest. | |
776 | */ | |
e0f530ff | 777 | |
8dcf39c4 | 778 | /* "." is not allowed */ |
e0f530ff | 779 | if (*rest == '\0' || is_dir_sep(*rest)) |
8dcf39c4 LT |
780 | return 0; |
781 | ||
e0f530ff | 782 | switch (*rest) { |
8dcf39c4 LT |
783 | /* |
784 | * ".git" followed by NUL or slash is bad. This | |
785 | * shares the path end test with the ".." case. | |
786 | */ | |
787 | case 'g': | |
cc2fc7c2 JK |
788 | case 'G': |
789 | if (rest[1] != 'i' && rest[1] != 'I') | |
8dcf39c4 | 790 | break; |
cc2fc7c2 | 791 | if (rest[2] != 't' && rest[2] != 'T') |
8dcf39c4 LT |
792 | break; |
793 | rest += 2; | |
794 | /* fallthrough */ | |
795 | case '.': | |
e0f530ff | 796 | if (rest[1] == '\0' || is_dir_sep(rest[1])) |
8dcf39c4 LT |
797 | return 0; |
798 | } | |
799 | return 1; | |
800 | } | |
801 | ||
802 | int verify_path(const char *path) | |
803 | { | |
804 | char c; | |
805 | ||
56948cb6 EFL |
806 | if (has_dos_drive_prefix(path)) |
807 | return 0; | |
808 | ||
8dcf39c4 LT |
809 | goto inside; |
810 | for (;;) { | |
811 | if (!c) | |
812 | return 1; | |
56948cb6 | 813 | if (is_dir_sep(c)) { |
8dcf39c4 | 814 | inside: |
a42643aa JK |
815 | if (protect_hfs && is_hfs_dotgit(path)) |
816 | return 0; | |
2b4c6efc JS |
817 | if (protect_ntfs && is_ntfs_dotgit(path)) |
818 | return 0; | |
8dcf39c4 | 819 | c = *path++; |
3bdf09c7 JH |
820 | if ((c == '.' && !verify_dotfile(path)) || |
821 | is_dir_sep(c) || c == '\0') | |
822 | return 0; | |
8dcf39c4 LT |
823 | } |
824 | c = *path++; | |
825 | } | |
826 | } | |
827 | ||
12676608 LT |
828 | /* |
829 | * Do we have another file that has the beginning components being a | |
830 | * proper superset of the name we're trying to add? | |
0f1e4f04 | 831 | */ |
4aab5b46 JH |
832 | static int has_file_name(struct index_state *istate, |
833 | const struct cache_entry *ce, int pos, int ok_to_replace) | |
0f1e4f04 | 834 | { |
12676608 LT |
835 | int retval = 0; |
836 | int len = ce_namelen(ce); | |
b155725d | 837 | int stage = ce_stage(ce); |
12676608 | 838 | const char *name = ce->name; |
0f1e4f04 | 839 | |
4aab5b46 JH |
840 | while (pos < istate->cache_nr) { |
841 | struct cache_entry *p = istate->cache[pos++]; | |
0f1e4f04 | 842 | |
12676608 | 843 | if (len >= ce_namelen(p)) |
0f1e4f04 | 844 | break; |
12676608 LT |
845 | if (memcmp(name, p->name, len)) |
846 | break; | |
b155725d JH |
847 | if (ce_stage(p) != stage) |
848 | continue; | |
12676608 LT |
849 | if (p->name[len] != '/') |
850 | continue; | |
7a51ed66 | 851 | if (p->ce_flags & CE_REMOVE) |
21cd8d00 | 852 | continue; |
12676608 LT |
853 | retval = -1; |
854 | if (!ok_to_replace) | |
855 | break; | |
4aab5b46 | 856 | remove_index_entry_at(istate, --pos); |
0f1e4f04 | 857 | } |
12676608 LT |
858 | return retval; |
859 | } | |
0f1e4f04 | 860 | |
12676608 LT |
861 | /* |
862 | * Do we have another file with a pathname that is a proper | |
863 | * subset of the name we're trying to add? | |
864 | */ | |
4aab5b46 JH |
865 | static int has_dir_name(struct index_state *istate, |
866 | const struct cache_entry *ce, int pos, int ok_to_replace) | |
12676608 LT |
867 | { |
868 | int retval = 0; | |
b155725d | 869 | int stage = ce_stage(ce); |
12676608 LT |
870 | const char *name = ce->name; |
871 | const char *slash = name + ce_namelen(ce); | |
0f1e4f04 | 872 | |
12676608 LT |
873 | for (;;) { |
874 | int len; | |
0f1e4f04 | 875 | |
12676608 LT |
876 | for (;;) { |
877 | if (*--slash == '/') | |
878 | break; | |
879 | if (slash <= ce->name) | |
880 | return retval; | |
881 | } | |
882 | len = slash - name; | |
0f1e4f04 | 883 | |
b60e188c | 884 | pos = index_name_stage_pos(istate, name, len, stage); |
12676608 | 885 | if (pos >= 0) { |
21cd8d00 JH |
886 | /* |
887 | * Found one, but not so fast. This could | |
888 | * be a marker that says "I was here, but | |
889 | * I am being removed". Such an entry is | |
890 | * not a part of the resulting tree, and | |
891 | * it is Ok to have a directory at the same | |
892 | * path. | |
893 | */ | |
077c48df | 894 | if (!(istate->cache[pos]->ce_flags & CE_REMOVE)) { |
21cd8d00 JH |
895 | retval = -1; |
896 | if (!ok_to_replace) | |
897 | break; | |
4aab5b46 | 898 | remove_index_entry_at(istate, pos); |
21cd8d00 JH |
899 | continue; |
900 | } | |
12676608 | 901 | } |
21cd8d00 JH |
902 | else |
903 | pos = -pos-1; | |
12676608 LT |
904 | |
905 | /* | |
906 | * Trivial optimization: if we find an entry that | |
907 | * already matches the sub-directory, then we know | |
b155725d | 908 | * we're ok, and we can exit. |
12676608 | 909 | */ |
4aab5b46 JH |
910 | while (pos < istate->cache_nr) { |
911 | struct cache_entry *p = istate->cache[pos]; | |
b155725d JH |
912 | if ((ce_namelen(p) <= len) || |
913 | (p->name[len] != '/') || | |
914 | memcmp(p->name, name, len)) | |
915 | break; /* not our subdirectory */ | |
077c48df JH |
916 | if (ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) |
917 | /* | |
918 | * p is at the same stage as our entry, and | |
b155725d JH |
919 | * is a subdirectory of what we are looking |
920 | * at, so we cannot have conflicts at our | |
921 | * level or anything shorter. | |
922 | */ | |
923 | return retval; | |
924 | pos++; | |
192268c1 | 925 | } |
0f1e4f04 | 926 | } |
12676608 LT |
927 | return retval; |
928 | } | |
929 | ||
930 | /* We may be in a situation where we already have path/file and path | |
931 | * is being added, or we already have path and path/file is being | |
932 | * added. Either one would result in a nonsense tree that has path | |
933 | * twice when git-write-tree tries to write it out. Prevent it. | |
a6080a0a | 934 | * |
12676608 LT |
935 | * If ok-to-replace is specified, we remove the conflicting entries |
936 | * from the cache so the caller should recompute the insert position. | |
937 | * When this happens, we return non-zero. | |
938 | */ | |
4aab5b46 JH |
939 | static int check_file_directory_conflict(struct index_state *istate, |
940 | const struct cache_entry *ce, | |
941 | int pos, int ok_to_replace) | |
12676608 | 942 | { |
21cd8d00 JH |
943 | int retval; |
944 | ||
945 | /* | |
946 | * When ce is an "I am going away" entry, we allow it to be added | |
947 | */ | |
7a51ed66 | 948 | if (ce->ce_flags & CE_REMOVE) |
21cd8d00 JH |
949 | return 0; |
950 | ||
12676608 LT |
951 | /* |
952 | * We check if the path is a sub-path of a subsequent pathname | |
953 | * first, since removing those will not change the position | |
21cd8d00 | 954 | * in the array. |
12676608 | 955 | */ |
4aab5b46 | 956 | retval = has_file_name(istate, ce, pos, ok_to_replace); |
21cd8d00 | 957 | |
12676608 LT |
958 | /* |
959 | * Then check if the path might have a clashing sub-directory | |
960 | * before it. | |
961 | */ | |
4aab5b46 | 962 | return retval + has_dir_name(istate, ce, pos, ok_to_replace); |
0f1e4f04 JH |
963 | } |
964 | ||
af3785dc | 965 | static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option) |
197ee8c9 LT |
966 | { |
967 | int pos; | |
192268c1 JH |
968 | int ok_to_add = option & ADD_CACHE_OK_TO_ADD; |
969 | int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; | |
b155725d | 970 | int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; |
39425819 | 971 | int new_only = option & ADD_CACHE_NEW_ONLY; |
5f73076c | 972 | |
ce7c614b NTND |
973 | if (!(option & ADD_CACHE_KEEP_CACHE_TREE)) |
974 | cache_tree_invalidate_path(istate, ce->name); | |
b60e188c | 975 | pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce)); |
197ee8c9 | 976 | |
3e09cdfd | 977 | /* existing match? Just replace it. */ |
76e7f4ec | 978 | if (pos >= 0) { |
39425819 JH |
979 | if (!new_only) |
980 | replace_index_entry(istate, pos, ce); | |
197ee8c9 LT |
981 | return 0; |
982 | } | |
76e7f4ec | 983 | pos = -pos-1; |
197ee8c9 | 984 | |
7b937ca3 LT |
985 | /* |
986 | * Inserting a merged entry ("stage 0") into the index | |
987 | * will always replace all non-merged entries.. | |
988 | */ | |
4aab5b46 JH |
989 | if (pos < istate->cache_nr && ce_stage(ce) == 0) { |
990 | while (ce_same_name(istate->cache[pos], ce)) { | |
7b937ca3 | 991 | ok_to_add = 1; |
4aab5b46 | 992 | if (!remove_index_entry_at(istate, pos)) |
7b937ca3 LT |
993 | break; |
994 | } | |
995 | } | |
996 | ||
121481ab LT |
997 | if (!ok_to_add) |
998 | return -1; | |
8dcf39c4 | 999 | if (!verify_path(ce->name)) |
7e7abea9 | 1000 | return error("Invalid path '%s'", ce->name); |
121481ab | 1001 | |
3e09cdfd | 1002 | if (!skip_df_check && |
4aab5b46 | 1003 | check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { |
192268c1 | 1004 | if (!ok_to_replace) |
4aab5b46 JH |
1005 | return error("'%s' appears as both a file and as a directory", |
1006 | ce->name); | |
b60e188c | 1007 | pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce)); |
192268c1 JH |
1008 | pos = -pos-1; |
1009 | } | |
af3785dc JH |
1010 | return pos + 1; |
1011 | } | |
1012 | ||
1013 | int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option) | |
1014 | { | |
1015 | int pos; | |
1016 | ||
1017 | if (option & ADD_CACHE_JUST_APPEND) | |
1018 | pos = istate->cache_nr; | |
1019 | else { | |
1020 | int ret; | |
1021 | ret = add_index_entry_with_check(istate, ce, option); | |
1022 | if (ret <= 0) | |
1023 | return ret; | |
1024 | pos = ret - 1; | |
1025 | } | |
0f1e4f04 | 1026 | |
197ee8c9 | 1027 | /* Make sure the array is big enough .. */ |
999f5660 | 1028 | ALLOC_GROW(istate->cache, istate->cache_nr + 1, istate->cache_alloc); |
197ee8c9 LT |
1029 | |
1030 | /* Add it in.. */ | |
4aab5b46 | 1031 | istate->cache_nr++; |
af3785dc | 1032 | if (istate->cache_nr > pos + 1) |
4aab5b46 JH |
1033 | memmove(istate->cache + pos + 1, |
1034 | istate->cache + pos, | |
1035 | (istate->cache_nr - pos - 1) * sizeof(ce)); | |
cf558704 | 1036 | set_index_entry(istate, pos, ce); |
e636a7b4 | 1037 | istate->cache_changed |= CE_ENTRY_ADDED; |
197ee8c9 LT |
1038 | return 0; |
1039 | } | |
1040 | ||
405e5b2f LT |
1041 | /* |
1042 | * "refresh" does not calculate a new sha1 file or bring the | |
1043 | * cache up-to-date for mode/content changes. But what it | |
1044 | * _does_ do is to "re-match" the stat information of a file | |
1045 | * with the cache, so that you can refresh the cache for a | |
1046 | * file that hasn't been changed but where the stat entry is | |
1047 | * out of date. | |
1048 | * | |
1049 | * For example, you'd want to do this after doing a "git-read-tree", | |
1050 | * to link up the stat cache details with the proper files. | |
1051 | */ | |
4aab5b46 | 1052 | static struct cache_entry *refresh_cache_ent(struct index_state *istate, |
4bd5b7da | 1053 | struct cache_entry *ce, |
d05e6970 JK |
1054 | unsigned int options, int *err, |
1055 | int *changed_ret) | |
405e5b2f LT |
1056 | { |
1057 | struct stat st; | |
1058 | struct cache_entry *updated; | |
1059 | int changed, size; | |
25762726 | 1060 | int refresh = options & CE_MATCH_REFRESH; |
4bd5b7da | 1061 | int ignore_valid = options & CE_MATCH_IGNORE_VALID; |
56cac48c | 1062 | int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; |
2e2e7ec1 | 1063 | int ignore_missing = options & CE_MATCH_IGNORE_MISSING; |
405e5b2f | 1064 | |
25762726 | 1065 | if (!refresh || ce_uptodate(ce)) |
eadb5831 JH |
1066 | return ce; |
1067 | ||
aa9349d4 | 1068 | /* |
56cac48c NTND |
1069 | * CE_VALID or CE_SKIP_WORKTREE means the user promised us |
1070 | * that the change to the work tree does not matter and told | |
1071 | * us not to worry. | |
aa9349d4 | 1072 | */ |
56cac48c NTND |
1073 | if (!ignore_skip_worktree && ce_skip_worktree(ce)) { |
1074 | ce_mark_uptodate(ce); | |
1075 | return ce; | |
1076 | } | |
aa9349d4 MSO |
1077 | if (!ignore_valid && (ce->ce_flags & CE_VALID)) { |
1078 | ce_mark_uptodate(ce); | |
1079 | return ce; | |
1080 | } | |
1081 | ||
ccad42d4 RS |
1082 | if (has_symlink_leading_path(ce->name, ce_namelen(ce))) { |
1083 | if (ignore_missing) | |
1084 | return ce; | |
1085 | if (err) | |
1086 | *err = ENOENT; | |
1087 | return NULL; | |
1088 | } | |
1089 | ||
8fd2cb40 | 1090 | if (lstat(ce->name, &st) < 0) { |
2e2e7ec1 BK |
1091 | if (ignore_missing && errno == ENOENT) |
1092 | return ce; | |
ec0cc704 JH |
1093 | if (err) |
1094 | *err = errno; | |
8fd2cb40 JS |
1095 | return NULL; |
1096 | } | |
405e5b2f | 1097 | |
4bd5b7da | 1098 | changed = ie_match_stat(istate, ce, &st, options); |
d05e6970 JK |
1099 | if (changed_ret) |
1100 | *changed_ret = changed; | |
405e5b2f | 1101 | if (!changed) { |
4bd5b7da JH |
1102 | /* |
1103 | * The path is unchanged. If we were told to ignore | |
1104 | * valid bit, then we did the actual stat check and | |
1105 | * found that the entry is unmodified. If the entry | |
1106 | * is not marked VALID, this is the place to mark it | |
1107 | * valid again, under "assume unchanged" mode. | |
1108 | */ | |
1109 | if (ignore_valid && assume_unchanged && | |
7a51ed66 | 1110 | !(ce->ce_flags & CE_VALID)) |
405e5b2f | 1111 | ; /* mark this one VALID again */ |
eadb5831 JH |
1112 | else { |
1113 | /* | |
1114 | * We do not mark the index itself "modified" | |
1115 | * because CE_UPTODATE flag is in-core only; | |
1116 | * we are not going to write this change out. | |
1117 | */ | |
125fd984 JH |
1118 | if (!S_ISGITLINK(ce->ce_mode)) |
1119 | ce_mark_uptodate(ce); | |
8fd2cb40 | 1120 | return ce; |
eadb5831 | 1121 | } |
405e5b2f LT |
1122 | } |
1123 | ||
4bd5b7da | 1124 | if (ie_modified(istate, ce, &st, options)) { |
ec0cc704 JH |
1125 | if (err) |
1126 | *err = EINVAL; | |
8fd2cb40 JS |
1127 | return NULL; |
1128 | } | |
405e5b2f LT |
1129 | |
1130 | size = ce_size(ce); | |
1131 | updated = xmalloc(size); | |
1132 | memcpy(updated, ce, size); | |
1133 | fill_stat_cache_info(updated, &st); | |
4bd5b7da JH |
1134 | /* |
1135 | * If ignore_valid is not set, we should leave CE_VALID bit | |
1136 | * alone. Otherwise, paths marked with --no-assume-unchanged | |
1137 | * (i.e. things to be edited) will reacquire CE_VALID bit | |
1138 | * automatically, which is not really what we want. | |
405e5b2f | 1139 | */ |
4bd5b7da | 1140 | if (!ignore_valid && assume_unchanged && |
7a51ed66 LT |
1141 | !(ce->ce_flags & CE_VALID)) |
1142 | updated->ce_flags &= ~CE_VALID; | |
405e5b2f | 1143 | |
e636a7b4 | 1144 | /* istate->cache_changed is updated in the caller */ |
405e5b2f LT |
1145 | return updated; |
1146 | } | |
1147 | ||
3deffc52 | 1148 | static void show_file(const char * fmt, const char * name, int in_porcelain, |
046613c5 | 1149 | int * first, const char *header_msg) |
3deffc52 MM |
1150 | { |
1151 | if (in_porcelain && *first && header_msg) { | |
1152 | printf("%s\n", header_msg); | |
cd2b8ae9 | 1153 | *first = 0; |
3deffc52 MM |
1154 | } |
1155 | printf(fmt, name); | |
1156 | } | |
1157 | ||
9b2d6149 NTND |
1158 | int refresh_index(struct index_state *istate, unsigned int flags, |
1159 | const struct pathspec *pathspec, | |
046613c5 | 1160 | char *seen, const char *header_msg) |
405e5b2f LT |
1161 | { |
1162 | int i; | |
1163 | int has_errors = 0; | |
1164 | int really = (flags & REFRESH_REALLY) != 0; | |
1165 | int allow_unmerged = (flags & REFRESH_UNMERGED) != 0; | |
1166 | int quiet = (flags & REFRESH_QUIET) != 0; | |
1167 | int not_new = (flags & REFRESH_IGNORE_MISSING) != 0; | |
5fdeacb0 | 1168 | int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0; |
3deffc52 MM |
1169 | int first = 1; |
1170 | int in_porcelain = (flags & REFRESH_IN_PORCELAIN); | |
25762726 BK |
1171 | unsigned int options = (CE_MATCH_REFRESH | |
1172 | (really ? CE_MATCH_IGNORE_VALID : 0) | | |
2e2e7ec1 | 1173 | (not_new ? CE_MATCH_IGNORE_MISSING : 0)); |
4bd4e730 | 1174 | const char *modified_fmt; |
73b7eae6 JK |
1175 | const char *deleted_fmt; |
1176 | const char *typechange_fmt; | |
1177 | const char *added_fmt; | |
4bd4e730 | 1178 | const char *unmerged_fmt; |
405e5b2f | 1179 | |
4bd4e730 | 1180 | modified_fmt = (in_porcelain ? "M\t%s\n" : "%s: needs update\n"); |
73b7eae6 JK |
1181 | deleted_fmt = (in_porcelain ? "D\t%s\n" : "%s: needs update\n"); |
1182 | typechange_fmt = (in_porcelain ? "T\t%s\n" : "%s needs update\n"); | |
1183 | added_fmt = (in_porcelain ? "A\t%s\n" : "%s needs update\n"); | |
4bd4e730 | 1184 | unmerged_fmt = (in_porcelain ? "U\t%s\n" : "%s: needs merge\n"); |
4aab5b46 | 1185 | for (i = 0; i < istate->cache_nr; i++) { |
405e5b2f | 1186 | struct cache_entry *ce, *new; |
ec0cc704 | 1187 | int cache_errno = 0; |
73b7eae6 | 1188 | int changed = 0; |
3d1f148c | 1189 | int filtered = 0; |
ec0cc704 | 1190 | |
4aab5b46 | 1191 | ce = istate->cache[i]; |
5fdeacb0 JS |
1192 | if (ignore_submodules && S_ISGITLINK(ce->ce_mode)) |
1193 | continue; | |
1194 | ||
429bb40a | 1195 | if (pathspec && !ce_path_match(ce, pathspec, seen)) |
3d1f148c JH |
1196 | filtered = 1; |
1197 | ||
405e5b2f | 1198 | if (ce_stage(ce)) { |
4aab5b46 JH |
1199 | while ((i < istate->cache_nr) && |
1200 | ! strcmp(istate->cache[i]->name, ce->name)) | |
405e5b2f LT |
1201 | i++; |
1202 | i--; | |
1203 | if (allow_unmerged) | |
1204 | continue; | |
3d1f148c JH |
1205 | if (!filtered) |
1206 | show_file(unmerged_fmt, ce->name, in_porcelain, | |
1207 | &first, header_msg); | |
405e5b2f LT |
1208 | has_errors = 1; |
1209 | continue; | |
1210 | } | |
1211 | ||
3d1f148c | 1212 | if (filtered) |
d616813d AJ |
1213 | continue; |
1214 | ||
73b7eae6 | 1215 | new = refresh_cache_ent(istate, ce, options, &cache_errno, &changed); |
8fd2cb40 | 1216 | if (new == ce) |
405e5b2f | 1217 | continue; |
8fd2cb40 | 1218 | if (!new) { |
73b7eae6 JK |
1219 | const char *fmt; |
1220 | ||
8fd2cb40 | 1221 | if (really && cache_errno == EINVAL) { |
405e5b2f LT |
1222 | /* If we are doing --really-refresh that |
1223 | * means the index is not valid anymore. | |
1224 | */ | |
7a51ed66 | 1225 | ce->ce_flags &= ~CE_VALID; |
078a58e8 | 1226 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
e636a7b4 | 1227 | istate->cache_changed |= CE_ENTRY_CHANGED; |
405e5b2f LT |
1228 | } |
1229 | if (quiet) | |
1230 | continue; | |
73b7eae6 JK |
1231 | |
1232 | if (cache_errno == ENOENT) | |
1233 | fmt = deleted_fmt; | |
1234 | else if (ce->ce_flags & CE_INTENT_TO_ADD) | |
1235 | fmt = added_fmt; /* must be before other checks */ | |
1236 | else if (changed & TYPE_CHANGED) | |
1237 | fmt = typechange_fmt; | |
1238 | else | |
1239 | fmt = modified_fmt; | |
1240 | show_file(fmt, | |
1241 | ce->name, in_porcelain, &first, header_msg); | |
405e5b2f LT |
1242 | has_errors = 1; |
1243 | continue; | |
1244 | } | |
cf558704 LT |
1245 | |
1246 | replace_index_entry(istate, i, new); | |
405e5b2f LT |
1247 | } |
1248 | return has_errors; | |
1249 | } | |
1250 | ||
25762726 BK |
1251 | static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, |
1252 | unsigned int options) | |
ec0cc704 | 1253 | { |
25762726 | 1254 | return refresh_cache_ent(&the_index, ce, options, NULL, NULL); |
ec0cc704 JH |
1255 | } |
1256 | ||
db3b313c JH |
1257 | |
1258 | /***************************************************************** | |
1259 | * Index File I/O | |
1260 | *****************************************************************/ | |
1261 | ||
9d227781 JH |
1262 | #define INDEX_FORMAT_DEFAULT 3 |
1263 | ||
136347d7 TG |
1264 | static unsigned int get_index_format_default(void) |
1265 | { | |
1266 | char *envversion = getenv("GIT_INDEX_VERSION"); | |
3c09d684 | 1267 | char *endp; |
b27a5720 | 1268 | int value; |
3c09d684 TG |
1269 | unsigned int version = INDEX_FORMAT_DEFAULT; |
1270 | ||
136347d7 | 1271 | if (!envversion) { |
b27a5720 TA |
1272 | if (!git_config_get_int("index.version", &value)) |
1273 | version = value; | |
3c09d684 TG |
1274 | if (version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) { |
1275 | warning(_("index.version set, but the value is invalid.\n" | |
136347d7 | 1276 | "Using version %i"), INDEX_FORMAT_DEFAULT); |
3c09d684 | 1277 | return INDEX_FORMAT_DEFAULT; |
136347d7 TG |
1278 | } |
1279 | return version; | |
1280 | } | |
3c09d684 TG |
1281 | |
1282 | version = strtoul(envversion, &endp, 10); | |
1283 | if (*endp || | |
1284 | version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) { | |
1285 | warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n" | |
1286 | "Using version %i"), INDEX_FORMAT_DEFAULT); | |
1287 | version = INDEX_FORMAT_DEFAULT; | |
1288 | } | |
1289 | return version; | |
136347d7 TG |
1290 | } |
1291 | ||
db3b313c JH |
1292 | /* |
1293 | * dev/ino/uid/gid/size are also just tracked to the low 32 bits | |
1294 | * Again - this is just a (very strong in practice) heuristic that | |
1295 | * the inode hasn't changed. | |
1296 | * | |
1297 | * We save the fields in big-endian order to allow using the | |
1298 | * index file over NFS transparently. | |
1299 | */ | |
1300 | struct ondisk_cache_entry { | |
1301 | struct cache_time ctime; | |
1302 | struct cache_time mtime; | |
7800c1eb TG |
1303 | uint32_t dev; |
1304 | uint32_t ino; | |
1305 | uint32_t mode; | |
1306 | uint32_t uid; | |
1307 | uint32_t gid; | |
1308 | uint32_t size; | |
db3b313c | 1309 | unsigned char sha1[20]; |
7800c1eb | 1310 | uint16_t flags; |
db3b313c JH |
1311 | char name[FLEX_ARRAY]; /* more */ |
1312 | }; | |
1313 | ||
1314 | /* | |
1315 | * This struct is used when CE_EXTENDED bit is 1 | |
1316 | * The struct must match ondisk_cache_entry exactly from | |
1317 | * ctime till flags | |
1318 | */ | |
1319 | struct ondisk_cache_entry_extended { | |
1320 | struct cache_time ctime; | |
1321 | struct cache_time mtime; | |
7800c1eb TG |
1322 | uint32_t dev; |
1323 | uint32_t ino; | |
1324 | uint32_t mode; | |
1325 | uint32_t uid; | |
1326 | uint32_t gid; | |
1327 | uint32_t size; | |
db3b313c | 1328 | unsigned char sha1[20]; |
7800c1eb TG |
1329 | uint16_t flags; |
1330 | uint16_t flags2; | |
db3b313c JH |
1331 | char name[FLEX_ARRAY]; /* more */ |
1332 | }; | |
1333 | ||
6c9cd161 | 1334 | /* These are only used for v3 or lower */ |
db3b313c JH |
1335 | #define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7) |
1336 | #define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len) | |
1337 | #define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len) | |
1338 | #define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \ | |
1339 | ondisk_cache_entry_extended_size(ce_namelen(ce)) : \ | |
1340 | ondisk_cache_entry_size(ce_namelen(ce))) | |
1341 | ||
bad68ec9 | 1342 | static int verify_hdr(struct cache_header *hdr, unsigned long size) |
e83c5163 | 1343 | { |
9126f009 | 1344 | git_SHA_CTX c; |
bad68ec9 | 1345 | unsigned char sha1[20]; |
0136bac9 | 1346 | int hdr_version; |
e83c5163 | 1347 | |
ccc4feb5 | 1348 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
e83c5163 | 1349 | return error("bad signature"); |
0136bac9 | 1350 | hdr_version = ntohl(hdr->hdr_version); |
b82a7b5b | 1351 | if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version) |
0136bac9 | 1352 | return error("bad index version %d", hdr_version); |
9126f009 NP |
1353 | git_SHA1_Init(&c); |
1354 | git_SHA1_Update(&c, hdr, size - 20); | |
1355 | git_SHA1_Final(sha1, &c); | |
a89fccd2 | 1356 | if (hashcmp(sha1, (unsigned char *)hdr + size - 20)) |
ca9be054 | 1357 | return error("bad index file sha1 signature"); |
e83c5163 LT |
1358 | return 0; |
1359 | } | |
1360 | ||
4aab5b46 JH |
1361 | static int read_index_extension(struct index_state *istate, |
1362 | const char *ext, void *data, unsigned long sz) | |
bad68ec9 JH |
1363 | { |
1364 | switch (CACHE_EXT(ext)) { | |
1365 | case CACHE_EXT_TREE: | |
4aab5b46 | 1366 | istate->cache_tree = cache_tree_read(data, sz); |
bad68ec9 | 1367 | break; |
cfc5789a JH |
1368 | case CACHE_EXT_RESOLVE_UNDO: |
1369 | istate->resolve_undo = resolve_undo_read(data, sz); | |
1370 | break; | |
5fc2fc8f NTND |
1371 | case CACHE_EXT_LINK: |
1372 | if (read_link_extension(istate, data, sz)) | |
1373 | return -1; | |
1374 | break; | |
bad68ec9 JH |
1375 | default: |
1376 | if (*ext < 'A' || 'Z' < *ext) | |
1377 | return error("index uses %.4s extension, which we do not understand", | |
1378 | ext); | |
1379 | fprintf(stderr, "ignoring %.4s extension\n", ext); | |
1380 | break; | |
1381 | } | |
1382 | return 0; | |
1383 | } | |
1384 | ||
216aab1e MH |
1385 | int hold_locked_index(struct lock_file *lk, int die_on_error) |
1386 | { | |
1387 | return hold_lock_file_for_update(lk, get_index_file(), | |
1388 | die_on_error | |
1389 | ? LOCK_DIE_ON_ERROR | |
1390 | : 0); | |
1391 | } | |
1392 | ||
4aab5b46 | 1393 | int read_index(struct index_state *istate) |
8fd2cb40 | 1394 | { |
4aab5b46 | 1395 | return read_index_from(istate, get_index_file()); |
8fd2cb40 JS |
1396 | } |
1397 | ||
3fc22b53 JH |
1398 | static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk, |
1399 | unsigned int flags, | |
1400 | const char *name, | |
1401 | size_t len) | |
1402 | { | |
1403 | struct cache_entry *ce = xmalloc(cache_entry_size(len)); | |
1404 | ||
c3d8da57 JK |
1405 | ce->ce_stat_data.sd_ctime.sec = get_be32(&ondisk->ctime.sec); |
1406 | ce->ce_stat_data.sd_mtime.sec = get_be32(&ondisk->mtime.sec); | |
1407 | ce->ce_stat_data.sd_ctime.nsec = get_be32(&ondisk->ctime.nsec); | |
1408 | ce->ce_stat_data.sd_mtime.nsec = get_be32(&ondisk->mtime.nsec); | |
1409 | ce->ce_stat_data.sd_dev = get_be32(&ondisk->dev); | |
1410 | ce->ce_stat_data.sd_ino = get_be32(&ondisk->ino); | |
1411 | ce->ce_mode = get_be32(&ondisk->mode); | |
1412 | ce->ce_stat_data.sd_uid = get_be32(&ondisk->uid); | |
1413 | ce->ce_stat_data.sd_gid = get_be32(&ondisk->gid); | |
1414 | ce->ce_stat_data.sd_size = get_be32(&ondisk->size); | |
b60e188c TG |
1415 | ce->ce_flags = flags & ~CE_NAMEMASK; |
1416 | ce->ce_namelen = len; | |
5fc2fc8f | 1417 | ce->index = 0; |
3fc22b53 JH |
1418 | hashcpy(ce->sha1, ondisk->sha1); |
1419 | memcpy(ce->name, name, len); | |
1420 | ce->name[len] = '\0'; | |
1421 | return ce; | |
1422 | } | |
1423 | ||
6c9cd161 JH |
1424 | /* |
1425 | * Adjacent cache entries tend to share the leading paths, so it makes | |
1426 | * sense to only store the differences in later entries. In the v4 | |
1427 | * on-disk format of the index, each on-disk cache entry stores the | |
1428 | * number of bytes to be stripped from the end of the previous name, | |
1429 | * and the bytes to append to the result, to come up with its name. | |
1430 | */ | |
1431 | static unsigned long expand_name_field(struct strbuf *name, const char *cp_) | |
1432 | { | |
1433 | const unsigned char *ep, *cp = (const unsigned char *)cp_; | |
1434 | size_t len = decode_varint(&cp); | |
1435 | ||
1436 | if (name->len < len) | |
1437 | die("malformed name field in the index"); | |
1438 | strbuf_remove(name, name->len - len, len); | |
1439 | for (ep = cp; *ep; ep++) | |
1440 | ; /* find the end */ | |
1441 | strbuf_add(name, cp, ep - cp); | |
1442 | return (const char *)ep + 1 - cp_; | |
1443 | } | |
1444 | ||
936f53d0 | 1445 | static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk, |
6c9cd161 JH |
1446 | unsigned long *ent_size, |
1447 | struct strbuf *previous_name) | |
7a51ed66 | 1448 | { |
debed2a6 | 1449 | struct cache_entry *ce; |
7fec10b7 | 1450 | size_t len; |
06aaaa0b | 1451 | const char *name; |
debed2a6 | 1452 | unsigned int flags; |
7fec10b7 | 1453 | |
7a51ed66 | 1454 | /* On-disk flags are just 16 bits */ |
c3d8da57 | 1455 | flags = get_be16(&ondisk->flags); |
debed2a6 | 1456 | len = flags & CE_NAMEMASK; |
7fec10b7 | 1457 | |
debed2a6 | 1458 | if (flags & CE_EXTENDED) { |
06aaaa0b NTND |
1459 | struct ondisk_cache_entry_extended *ondisk2; |
1460 | int extended_flags; | |
1461 | ondisk2 = (struct ondisk_cache_entry_extended *)ondisk; | |
c3d8da57 | 1462 | extended_flags = get_be16(&ondisk2->flags2) << 16; |
06aaaa0b NTND |
1463 | /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */ |
1464 | if (extended_flags & ~CE_EXTENDED_FLAGS) | |
1465 | die("Unknown index entry format %08x", extended_flags); | |
debed2a6 | 1466 | flags |= extended_flags; |
06aaaa0b NTND |
1467 | name = ondisk2->name; |
1468 | } | |
1469 | else | |
1470 | name = ondisk->name; | |
1471 | ||
6c9cd161 JH |
1472 | if (!previous_name) { |
1473 | /* v3 and earlier */ | |
1474 | if (len == CE_NAMEMASK) | |
1475 | len = strlen(name); | |
1476 | ce = cache_entry_from_ondisk(ondisk, flags, name, len); | |
1477 | ||
1478 | *ent_size = ondisk_ce_size(ce); | |
1479 | } else { | |
1480 | unsigned long consumed; | |
1481 | consumed = expand_name_field(previous_name, name); | |
1482 | ce = cache_entry_from_ondisk(ondisk, flags, | |
1483 | previous_name->buf, | |
1484 | previous_name->len); | |
1485 | ||
1486 | *ent_size = (name - ((char *)ondisk)) + consumed; | |
1487 | } | |
debed2a6 | 1488 | return ce; |
cf558704 LT |
1489 | } |
1490 | ||
03f15a79 | 1491 | static void check_ce_order(struct index_state *istate) |
15999d0b | 1492 | { |
03f15a79 TG |
1493 | unsigned int i; |
1494 | ||
1495 | for (i = 1; i < istate->cache_nr; i++) { | |
1496 | struct cache_entry *ce = istate->cache[i - 1]; | |
1497 | struct cache_entry *next_ce = istate->cache[i]; | |
1498 | int name_compare = strcmp(ce->name, next_ce->name); | |
1499 | ||
1500 | if (0 < name_compare) | |
1501 | die("unordered stage entries in index"); | |
1502 | if (!name_compare) { | |
1503 | if (!ce_stage(ce)) | |
1504 | die("multiple stage entries for merged file '%s'", | |
1505 | ce->name); | |
1506 | if (ce_stage(ce) > ce_stage(next_ce)) | |
1507 | die("unordered stage entries for '%s'", | |
1508 | ce->name); | |
1509 | } | |
15999d0b JSP |
1510 | } |
1511 | } | |
1512 | ||
8fd2cb40 | 1513 | /* remember to discard_cache() before reading a different cache! */ |
3e52f70b | 1514 | int do_read_index(struct index_state *istate, const char *path, int must_exist) |
e83c5163 LT |
1515 | { |
1516 | int fd, i; | |
1517 | struct stat st; | |
debed2a6 | 1518 | unsigned long src_offset; |
e83c5163 | 1519 | struct cache_header *hdr; |
7a51ed66 LT |
1520 | void *mmap; |
1521 | size_t mmap_size; | |
6c9cd161 | 1522 | struct strbuf previous_name_buf = STRBUF_INIT, *previous_name; |
e83c5163 | 1523 | |
913e0e99 | 1524 | if (istate->initialized) |
4aab5b46 | 1525 | return istate->cache_nr; |
5d1a5c02 | 1526 | |
fba2f38a KB |
1527 | istate->timestamp.sec = 0; |
1528 | istate->timestamp.nsec = 0; | |
8fd2cb40 | 1529 | fd = open(path, O_RDONLY); |
5d1a5c02 | 1530 | if (fd < 0) { |
5fc2fc8f | 1531 | if (!must_exist && errno == ENOENT) |
5d1a5c02 | 1532 | return 0; |
5fc2fc8f | 1533 | die_errno("%s: index file open failed", path); |
5d1a5c02 | 1534 | } |
e83c5163 | 1535 | |
3511a377 | 1536 | if (fstat(fd, &st)) |
d824cbba | 1537 | die_errno("cannot stat the open index"); |
3511a377 | 1538 | |
7a51ed66 LT |
1539 | mmap_size = xsize_t(st.st_size); |
1540 | if (mmap_size < sizeof(struct cache_header) + 20) | |
3511a377 LFC |
1541 | die("index file smaller than expected"); |
1542 | ||
a1293ef7 | 1543 | mmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0); |
7a51ed66 | 1544 | if (mmap == MAP_FAILED) |
0721c314 | 1545 | die_errno("unable to map index file"); |
57d84f8d | 1546 | close(fd); |
e83c5163 | 1547 | |
7a51ed66 LT |
1548 | hdr = mmap; |
1549 | if (verify_hdr(hdr, mmap_size) < 0) | |
e83c5163 LT |
1550 | goto unmap; |
1551 | ||
e93021b2 | 1552 | hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20); |
9d227781 | 1553 | istate->version = ntohl(hdr->hdr_version); |
4aab5b46 JH |
1554 | istate->cache_nr = ntohl(hdr->hdr_entries); |
1555 | istate->cache_alloc = alloc_nr(istate->cache_nr); | |
c4aa3167 | 1556 | istate->cache = xcalloc(istate->cache_alloc, sizeof(*istate->cache)); |
913e0e99 | 1557 | istate->initialized = 1; |
7a51ed66 | 1558 | |
9d227781 | 1559 | if (istate->version == 4) |
6c9cd161 JH |
1560 | previous_name = &previous_name_buf; |
1561 | else | |
1562 | previous_name = NULL; | |
1563 | ||
7a51ed66 | 1564 | src_offset = sizeof(*hdr); |
4aab5b46 | 1565 | for (i = 0; i < istate->cache_nr; i++) { |
7a51ed66 | 1566 | struct ondisk_cache_entry *disk_ce; |
4aab5b46 | 1567 | struct cache_entry *ce; |
936f53d0 | 1568 | unsigned long consumed; |
4aab5b46 | 1569 | |
7a51ed66 | 1570 | disk_ce = (struct ondisk_cache_entry *)((char *)mmap + src_offset); |
6c9cd161 | 1571 | ce = create_from_disk(disk_ce, &consumed, previous_name); |
cf558704 | 1572 | set_index_entry(istate, i, ce); |
7a51ed66 | 1573 | |
936f53d0 | 1574 | src_offset += consumed; |
e83c5163 | 1575 | } |
6c9cd161 | 1576 | strbuf_release(&previous_name_buf); |
fba2f38a | 1577 | istate->timestamp.sec = st.st_mtime; |
c06ff490 | 1578 | istate->timestamp.nsec = ST_MTIME_NSEC(st); |
fba2f38a | 1579 | |
7a51ed66 | 1580 | while (src_offset <= mmap_size - 20 - 8) { |
bad68ec9 JH |
1581 | /* After an array of active_nr index entries, |
1582 | * there can be arbitrary number of extended | |
1583 | * sections, each of which is prefixed with | |
1584 | * extension name (4-byte) and section length | |
1585 | * in 4-byte network byte order. | |
1586 | */ | |
07cc8eca | 1587 | uint32_t extsize; |
7a51ed66 | 1588 | memcpy(&extsize, (char *)mmap + src_offset + 4, 4); |
bad68ec9 | 1589 | extsize = ntohl(extsize); |
4aab5b46 | 1590 | if (read_index_extension(istate, |
7a51ed66 LT |
1591 | (const char *) mmap + src_offset, |
1592 | (char *) mmap + src_offset + 8, | |
1d7f171c | 1593 | extsize) < 0) |
bad68ec9 | 1594 | goto unmap; |
7a51ed66 LT |
1595 | src_offset += 8; |
1596 | src_offset += extsize; | |
bad68ec9 | 1597 | } |
7a51ed66 | 1598 | munmap(mmap, mmap_size); |
4aab5b46 | 1599 | return istate->cache_nr; |
e83c5163 LT |
1600 | |
1601 | unmap: | |
7a51ed66 | 1602 | munmap(mmap, mmap_size); |
5d1a5c02 | 1603 | die("index file corrupt"); |
e83c5163 LT |
1604 | } |
1605 | ||
5fc2fc8f NTND |
1606 | int read_index_from(struct index_state *istate, const char *path) |
1607 | { | |
1608 | struct split_index *split_index; | |
1609 | int ret; | |
1610 | ||
1611 | /* istate->initialized covers both .git/index and .git/sharedindex.xxx */ | |
1612 | if (istate->initialized) | |
1613 | return istate->cache_nr; | |
1614 | ||
1615 | ret = do_read_index(istate, path, 0); | |
1616 | split_index = istate->split_index; | |
03f15a79 TG |
1617 | if (!split_index || is_null_sha1(split_index->base_sha1)) { |
1618 | check_ce_order(istate); | |
5fc2fc8f | 1619 | return ret; |
03f15a79 | 1620 | } |
5fc2fc8f NTND |
1621 | |
1622 | if (split_index->base) | |
1623 | discard_index(split_index->base); | |
1624 | else | |
1625 | split_index->base = xcalloc(1, sizeof(*split_index->base)); | |
1626 | ret = do_read_index(split_index->base, | |
1627 | git_path("sharedindex.%s", | |
1628 | sha1_to_hex(split_index->base_sha1)), 1); | |
1629 | if (hashcmp(split_index->base_sha1, split_index->base->sha1)) | |
1630 | die("broken index, expect %s in %s, got %s", | |
1631 | sha1_to_hex(split_index->base_sha1), | |
1632 | git_path("sharedindex.%s", | |
1633 | sha1_to_hex(split_index->base_sha1)), | |
1634 | sha1_to_hex(split_index->base->sha1)); | |
1635 | merge_base_index(istate); | |
03f15a79 | 1636 | check_ce_order(istate); |
5fc2fc8f NTND |
1637 | return ret; |
1638 | } | |
1639 | ||
fa7b3c2f JH |
1640 | int is_index_unborn(struct index_state *istate) |
1641 | { | |
debed2a6 | 1642 | return (!istate->cache_nr && !istate->timestamp.sec); |
fa7b3c2f JH |
1643 | } |
1644 | ||
4aab5b46 | 1645 | int discard_index(struct index_state *istate) |
6d297f81 | 1646 | { |
debed2a6 RS |
1647 | int i; |
1648 | ||
5fc2fc8f NTND |
1649 | for (i = 0; i < istate->cache_nr; i++) { |
1650 | if (istate->cache[i]->index && | |
1651 | istate->split_index && | |
1652 | istate->split_index->base && | |
1653 | istate->cache[i]->index <= istate->split_index->base->cache_nr && | |
1654 | istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index - 1]) | |
1655 | continue; | |
debed2a6 | 1656 | free(istate->cache[i]); |
5fc2fc8f | 1657 | } |
cfc5789a | 1658 | resolve_undo_clear_index(istate); |
4aab5b46 JH |
1659 | istate->cache_nr = 0; |
1660 | istate->cache_changed = 0; | |
fba2f38a KB |
1661 | istate->timestamp.sec = 0; |
1662 | istate->timestamp.nsec = 0; | |
2092678c | 1663 | free_name_hash(istate); |
4aab5b46 | 1664 | cache_tree_free(&(istate->cache_tree)); |
913e0e99 | 1665 | istate->initialized = 0; |
a0fc4db0 RS |
1666 | free(istate->cache); |
1667 | istate->cache = NULL; | |
1668 | istate->cache_alloc = 0; | |
5fc2fc8f | 1669 | discard_split_index(istate); |
7a51ed66 | 1670 | return 0; |
6d297f81 JS |
1671 | } |
1672 | ||
d1f128b0 | 1673 | int unmerged_index(const struct index_state *istate) |
94a5728c DB |
1674 | { |
1675 | int i; | |
1676 | for (i = 0; i < istate->cache_nr; i++) { | |
1677 | if (ce_stage(istate->cache[i])) | |
1678 | return 1; | |
1679 | } | |
1680 | return 0; | |
1681 | } | |
1682 | ||
4990aadc | 1683 | #define WRITE_BUFFER_SIZE 8192 |
bf0f910d | 1684 | static unsigned char write_buffer[WRITE_BUFFER_SIZE]; |
4990aadc LT |
1685 | static unsigned long write_buffer_len; |
1686 | ||
9126f009 | 1687 | static int ce_write_flush(git_SHA_CTX *context, int fd) |
6015c28b JH |
1688 | { |
1689 | unsigned int buffered = write_buffer_len; | |
1690 | if (buffered) { | |
9126f009 | 1691 | git_SHA1_Update(context, write_buffer, buffered); |
93822c22 | 1692 | if (write_in_full(fd, write_buffer, buffered) != buffered) |
6015c28b JH |
1693 | return -1; |
1694 | write_buffer_len = 0; | |
1695 | } | |
1696 | return 0; | |
1697 | } | |
1698 | ||
9126f009 | 1699 | static int ce_write(git_SHA_CTX *context, int fd, void *data, unsigned int len) |
4990aadc LT |
1700 | { |
1701 | while (len) { | |
1702 | unsigned int buffered = write_buffer_len; | |
1703 | unsigned int partial = WRITE_BUFFER_SIZE - buffered; | |
1704 | if (partial > len) | |
1705 | partial = len; | |
1706 | memcpy(write_buffer + buffered, data, partial); | |
1707 | buffered += partial; | |
1708 | if (buffered == WRITE_BUFFER_SIZE) { | |
6015c28b JH |
1709 | write_buffer_len = buffered; |
1710 | if (ce_write_flush(context, fd)) | |
4990aadc LT |
1711 | return -1; |
1712 | buffered = 0; | |
1713 | } | |
1714 | write_buffer_len = buffered; | |
1715 | len -= partial; | |
1d7f171c | 1716 | data = (char *) data + partial; |
a6080a0a JH |
1717 | } |
1718 | return 0; | |
4990aadc LT |
1719 | } |
1720 | ||
9126f009 | 1721 | static int write_index_ext_header(git_SHA_CTX *context, int fd, |
ac58c7b1 | 1722 | unsigned int ext, unsigned int sz) |
bad68ec9 JH |
1723 | { |
1724 | ext = htonl(ext); | |
1725 | sz = htonl(sz); | |
968a1d65 DR |
1726 | return ((ce_write(context, fd, &ext, 4) < 0) || |
1727 | (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0; | |
bad68ec9 JH |
1728 | } |
1729 | ||
e93021b2 | 1730 | static int ce_flush(git_SHA_CTX *context, int fd, unsigned char *sha1) |
4990aadc LT |
1731 | { |
1732 | unsigned int left = write_buffer_len; | |
ca9be054 | 1733 | |
4990aadc LT |
1734 | if (left) { |
1735 | write_buffer_len = 0; | |
9126f009 | 1736 | git_SHA1_Update(context, write_buffer, left); |
4990aadc | 1737 | } |
ca9be054 | 1738 | |
2c865d9a QH |
1739 | /* Flush first if not enough space for SHA1 signature */ |
1740 | if (left + 20 > WRITE_BUFFER_SIZE) { | |
93822c22 | 1741 | if (write_in_full(fd, write_buffer, left) != left) |
2c865d9a QH |
1742 | return -1; |
1743 | left = 0; | |
1744 | } | |
1745 | ||
ca9be054 | 1746 | /* Append the SHA1 signature at the end */ |
9126f009 | 1747 | git_SHA1_Final(write_buffer + left, context); |
e93021b2 | 1748 | hashcpy(sha1, write_buffer + left); |
ca9be054 | 1749 | left += 20; |
93822c22 | 1750 | return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0; |
4990aadc LT |
1751 | } |
1752 | ||
407c8eb0 JH |
1753 | static void ce_smudge_racily_clean_entry(struct cache_entry *ce) |
1754 | { | |
1755 | /* | |
1756 | * The only thing we care about in this function is to smudge the | |
1757 | * falsely clean entry due to touch-update-touch race, so we leave | |
1758 | * everything else as they are. We are called for entries whose | |
c21d39d7 | 1759 | * ce_stat_data.sd_mtime match the index file mtime. |
c70115b4 JH |
1760 | * |
1761 | * Note that this actually does not do much for gitlinks, for | |
1762 | * which ce_match_stat_basic() always goes to the actual | |
1763 | * contents. The caller checks with is_racy_timestamp() which | |
1764 | * always says "no" for gitlinks, so we are not called for them ;-) | |
407c8eb0 JH |
1765 | */ |
1766 | struct stat st; | |
1767 | ||
1768 | if (lstat(ce->name, &st) < 0) | |
1769 | return; | |
1770 | if (ce_match_stat_basic(ce, &st)) | |
1771 | return; | |
1772 | if (ce_modified_check_fs(ce, &st)) { | |
4b3511b0 JH |
1773 | /* This is "racily clean"; smudge it. Note that this |
1774 | * is a tricky code. At first glance, it may appear | |
1775 | * that it can break with this sequence: | |
1776 | * | |
1777 | * $ echo xyzzy >frotz | |
1778 | * $ git-update-index --add frotz | |
1779 | * $ : >frotz | |
1780 | * $ sleep 3 | |
1781 | * $ echo filfre >nitfol | |
1782 | * $ git-update-index --add nitfol | |
1783 | * | |
b7e58b17 | 1784 | * but it does not. When the second update-index runs, |
4b3511b0 JH |
1785 | * it notices that the entry "frotz" has the same timestamp |
1786 | * as index, and if we were to smudge it by resetting its | |
1787 | * size to zero here, then the object name recorded | |
1788 | * in index is the 6-byte file but the cached stat information | |
1789 | * becomes zero --- which would then match what we would | |
a6080a0a | 1790 | * obtain from the filesystem next time we stat("frotz"). |
4b3511b0 JH |
1791 | * |
1792 | * However, the second update-index, before calling | |
1793 | * this function, notices that the cached size is 6 | |
1794 | * bytes and what is on the filesystem is an empty | |
1795 | * file, and never calls us, so the cached size information | |
1796 | * for "frotz" stays 6 which does not match the filesystem. | |
1797 | */ | |
c21d39d7 | 1798 | ce->ce_stat_data.sd_size = 0; |
407c8eb0 JH |
1799 | } |
1800 | } | |
1801 | ||
f136f7bf JH |
1802 | /* Copy miscellaneous fields but not the name */ |
1803 | static char *copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk, | |
1804 | struct cache_entry *ce) | |
7a51ed66 | 1805 | { |
b60e188c TG |
1806 | short flags; |
1807 | ||
c21d39d7 MH |
1808 | ondisk->ctime.sec = htonl(ce->ce_stat_data.sd_ctime.sec); |
1809 | ondisk->mtime.sec = htonl(ce->ce_stat_data.sd_mtime.sec); | |
1810 | ondisk->ctime.nsec = htonl(ce->ce_stat_data.sd_ctime.nsec); | |
1811 | ondisk->mtime.nsec = htonl(ce->ce_stat_data.sd_mtime.nsec); | |
1812 | ondisk->dev = htonl(ce->ce_stat_data.sd_dev); | |
1813 | ondisk->ino = htonl(ce->ce_stat_data.sd_ino); | |
7a51ed66 | 1814 | ondisk->mode = htonl(ce->ce_mode); |
c21d39d7 MH |
1815 | ondisk->uid = htonl(ce->ce_stat_data.sd_uid); |
1816 | ondisk->gid = htonl(ce->ce_stat_data.sd_gid); | |
1817 | ondisk->size = htonl(ce->ce_stat_data.sd_size); | |
7a51ed66 | 1818 | hashcpy(ondisk->sha1, ce->sha1); |
b60e188c | 1819 | |
ce51bf09 | 1820 | flags = ce->ce_flags & ~CE_NAMEMASK; |
b60e188c TG |
1821 | flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce)); |
1822 | ondisk->flags = htons(flags); | |
06aaaa0b NTND |
1823 | if (ce->ce_flags & CE_EXTENDED) { |
1824 | struct ondisk_cache_entry_extended *ondisk2; | |
1825 | ondisk2 = (struct ondisk_cache_entry_extended *)ondisk; | |
1826 | ondisk2->flags2 = htons((ce->ce_flags & CE_EXTENDED_FLAGS) >> 16); | |
f136f7bf | 1827 | return ondisk2->name; |
06aaaa0b | 1828 | } |
f136f7bf JH |
1829 | else { |
1830 | return ondisk->name; | |
1831 | } | |
1832 | } | |
1833 | ||
9d227781 JH |
1834 | static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce, |
1835 | struct strbuf *previous_name) | |
f136f7bf | 1836 | { |
9d227781 JH |
1837 | int size; |
1838 | struct ondisk_cache_entry *ondisk; | |
b3c96fb1 | 1839 | int saved_namelen = saved_namelen; /* compiler workaround */ |
f136f7bf JH |
1840 | char *name; |
1841 | int result; | |
1842 | ||
b3c96fb1 NTND |
1843 | if (ce->ce_flags & CE_STRIP_NAME) { |
1844 | saved_namelen = ce_namelen(ce); | |
1845 | ce->ce_namelen = 0; | |
1846 | } | |
1847 | ||
9d227781 JH |
1848 | if (!previous_name) { |
1849 | size = ondisk_ce_size(ce); | |
1850 | ondisk = xcalloc(1, size); | |
1851 | name = copy_cache_entry_to_ondisk(ondisk, ce); | |
1852 | memcpy(name, ce->name, ce_namelen(ce)); | |
1853 | } else { | |
1854 | int common, to_remove, prefix_size; | |
1855 | unsigned char to_remove_vi[16]; | |
1856 | for (common = 0; | |
1857 | (ce->name[common] && | |
1858 | common < previous_name->len && | |
1859 | ce->name[common] == previous_name->buf[common]); | |
1860 | common++) | |
1861 | ; /* still matching */ | |
1862 | to_remove = previous_name->len - common; | |
1863 | prefix_size = encode_varint(to_remove, to_remove_vi); | |
1864 | ||
1865 | if (ce->ce_flags & CE_EXTENDED) | |
1866 | size = offsetof(struct ondisk_cache_entry_extended, name); | |
1867 | else | |
1868 | size = offsetof(struct ondisk_cache_entry, name); | |
1869 | size += prefix_size + (ce_namelen(ce) - common + 1); | |
1870 | ||
1871 | ondisk = xcalloc(1, size); | |
1872 | name = copy_cache_entry_to_ondisk(ondisk, ce); | |
1873 | memcpy(name, to_remove_vi, prefix_size); | |
1874 | memcpy(name + prefix_size, ce->name + common, ce_namelen(ce) - common); | |
1875 | ||
1876 | strbuf_splice(previous_name, common, to_remove, | |
1877 | ce->name + common, ce_namelen(ce) - common); | |
06aaaa0b | 1878 | } |
b3c96fb1 NTND |
1879 | if (ce->ce_flags & CE_STRIP_NAME) { |
1880 | ce->ce_namelen = saved_namelen; | |
1881 | ce->ce_flags &= ~CE_STRIP_NAME; | |
1882 | } | |
7a51ed66 | 1883 | |
59efba64 JN |
1884 | result = ce_write(c, fd, ondisk, size); |
1885 | free(ondisk); | |
1886 | return result; | |
7a51ed66 LT |
1887 | } |
1888 | ||
426ddeea YM |
1889 | /* |
1890 | * This function verifies if index_state has the correct sha1 of the | |
1891 | * index file. Don't die if we have any other failure, just return 0. | |
1892 | */ | |
1893 | static int verify_index_from(const struct index_state *istate, const char *path) | |
1894 | { | |
1895 | int fd; | |
1896 | ssize_t n; | |
1897 | struct stat st; | |
1898 | unsigned char sha1[20]; | |
1899 | ||
1900 | if (!istate->initialized) | |
1901 | return 0; | |
1902 | ||
1903 | fd = open(path, O_RDONLY); | |
1904 | if (fd < 0) | |
1905 | return 0; | |
1906 | ||
1907 | if (fstat(fd, &st)) | |
1908 | goto out; | |
1909 | ||
1910 | if (st.st_size < sizeof(struct cache_header) + 20) | |
1911 | goto out; | |
1912 | ||
1913 | n = pread_in_full(fd, sha1, 20, st.st_size - 20); | |
1914 | if (n != 20) | |
1915 | goto out; | |
1916 | ||
1917 | if (hashcmp(istate->sha1, sha1)) | |
1918 | goto out; | |
1919 | ||
1920 | close(fd); | |
1921 | return 1; | |
1922 | ||
1923 | out: | |
1924 | close(fd); | |
1925 | return 0; | |
1926 | } | |
1927 | ||
1928 | static int verify_index(const struct index_state *istate) | |
1929 | { | |
1930 | return verify_index_from(istate, get_index_file()); | |
1931 | } | |
1932 | ||
483fbe2b JH |
1933 | static int has_racy_timestamp(struct index_state *istate) |
1934 | { | |
1935 | int entries = istate->cache_nr; | |
1936 | int i; | |
1937 | ||
1938 | for (i = 0; i < entries; i++) { | |
1939 | struct cache_entry *ce = istate->cache[i]; | |
1940 | if (is_racy_timestamp(istate, ce)) | |
1941 | return 1; | |
1942 | } | |
1943 | return 0; | |
1944 | } | |
1945 | ||
ccdc4ec3 | 1946 | /* |
98e023de | 1947 | * Opportunistically update the index but do not complain if we can't |
ccdc4ec3 JH |
1948 | */ |
1949 | void update_index_if_able(struct index_state *istate, struct lock_file *lockfile) | |
1950 | { | |
483fbe2b | 1951 | if ((istate->cache_changed || has_racy_timestamp(istate)) && |
788cef81 | 1952 | verify_index(istate) && |
03b86647 | 1953 | write_locked_index(istate, lockfile, COMMIT_LOCK)) |
ccdc4ec3 JH |
1954 | rollback_lock_file(lockfile); |
1955 | } | |
1956 | ||
c18b80a0 NTND |
1957 | static int do_write_index(struct index_state *istate, int newfd, |
1958 | int strip_extensions) | |
197ee8c9 | 1959 | { |
9126f009 | 1960 | git_SHA_CTX c; |
197ee8c9 | 1961 | struct cache_header hdr; |
9d227781 | 1962 | int i, err, removed, extended, hdr_version; |
4aab5b46 JH |
1963 | struct cache_entry **cache = istate->cache; |
1964 | int entries = istate->cache_nr; | |
e1afca4f | 1965 | struct stat st; |
9d227781 | 1966 | struct strbuf previous_name_buf = STRBUF_INIT, *previous_name; |
025a0709 | 1967 | |
06aaaa0b | 1968 | for (i = removed = extended = 0; i < entries; i++) { |
7a51ed66 | 1969 | if (cache[i]->ce_flags & CE_REMOVE) |
025a0709 | 1970 | removed++; |
197ee8c9 | 1971 | |
06aaaa0b NTND |
1972 | /* reduce extended entries if possible */ |
1973 | cache[i]->ce_flags &= ~CE_EXTENDED; | |
1974 | if (cache[i]->ce_flags & CE_EXTENDED_FLAGS) { | |
1975 | extended++; | |
1976 | cache[i]->ce_flags |= CE_EXTENDED; | |
1977 | } | |
1978 | } | |
1979 | ||
d6e3c181 | 1980 | if (!istate->version) { |
136347d7 | 1981 | istate->version = get_index_format_default(); |
d6e3c181 NTND |
1982 | if (getenv("GIT_TEST_SPLIT_INDEX")) |
1983 | init_split_index(istate); | |
1984 | } | |
9d227781 JH |
1985 | |
1986 | /* demote version 3 to version 2 when the latter suffices */ | |
1987 | if (istate->version == 3 || istate->version == 2) | |
1988 | istate->version = extended ? 3 : 2; | |
1989 | ||
1990 | hdr_version = istate->version; | |
1991 | ||
ccc4feb5 | 1992 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
9d227781 | 1993 | hdr.hdr_version = htonl(hdr_version); |
025a0709 | 1994 | hdr.hdr_entries = htonl(entries - removed); |
197ee8c9 | 1995 | |
9126f009 | 1996 | git_SHA1_Init(&c); |
ca9be054 | 1997 | if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0) |
197ee8c9 LT |
1998 | return -1; |
1999 | ||
9d227781 | 2000 | previous_name = (hdr_version == 4) ? &previous_name_buf : NULL; |
197ee8c9 LT |
2001 | for (i = 0; i < entries; i++) { |
2002 | struct cache_entry *ce = cache[i]; | |
7a51ed66 | 2003 | if (ce->ce_flags & CE_REMOVE) |
aa16021e | 2004 | continue; |
e06c43c7 | 2005 | if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce)) |
407c8eb0 | 2006 | ce_smudge_racily_clean_entry(ce); |
83bd7437 JK |
2007 | if (is_null_sha1(ce->sha1)) { |
2008 | static const char msg[] = "cache entry has null sha1: %s"; | |
2009 | static int allow = -1; | |
2010 | ||
2011 | if (allow < 0) | |
2012 | allow = git_env_bool("GIT_ALLOW_NULL_SHA1", 0); | |
2013 | if (allow) | |
2014 | warning(msg, ce->name); | |
2015 | else | |
2016 | return error(msg, ce->name); | |
2017 | } | |
9d227781 | 2018 | if (ce_write_entry(&c, newfd, ce, previous_name) < 0) |
197ee8c9 LT |
2019 | return -1; |
2020 | } | |
9d227781 | 2021 | strbuf_release(&previous_name_buf); |
1af1c2b6 | 2022 | |
bad68ec9 | 2023 | /* Write extension data here */ |
c18b80a0 | 2024 | if (!strip_extensions && istate->split_index) { |
5fc2fc8f NTND |
2025 | struct strbuf sb = STRBUF_INIT; |
2026 | ||
2027 | err = write_link_extension(&sb, istate) < 0 || | |
2028 | write_index_ext_header(&c, newfd, CACHE_EXT_LINK, | |
2029 | sb.len) < 0 || | |
2030 | ce_write(&c, newfd, sb.buf, sb.len) < 0; | |
2031 | strbuf_release(&sb); | |
2032 | if (err) | |
2033 | return -1; | |
2034 | } | |
c18b80a0 | 2035 | if (!strip_extensions && istate->cache_tree) { |
f285a2d7 | 2036 | struct strbuf sb = STRBUF_INIT; |
1dffb8fa | 2037 | |
1dffb8fa PH |
2038 | cache_tree_write(&sb, istate->cache_tree); |
2039 | err = write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) < 0 | |
2040 | || ce_write(&c, newfd, sb.buf, sb.len) < 0; | |
2041 | strbuf_release(&sb); | |
2042 | if (err) | |
bad68ec9 | 2043 | return -1; |
bad68ec9 | 2044 | } |
c18b80a0 | 2045 | if (!strip_extensions && istate->resolve_undo) { |
cfc5789a JH |
2046 | struct strbuf sb = STRBUF_INIT; |
2047 | ||
2048 | resolve_undo_write(&sb, istate->resolve_undo); | |
2049 | err = write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO, | |
2050 | sb.len) < 0 | |
2051 | || ce_write(&c, newfd, sb.buf, sb.len) < 0; | |
2052 | strbuf_release(&sb); | |
2053 | if (err) | |
2054 | return -1; | |
2055 | } | |
e1afca4f | 2056 | |
e93021b2 | 2057 | if (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st)) |
e1afca4f | 2058 | return -1; |
5bcf109c KB |
2059 | istate->timestamp.sec = (unsigned int)st.st_mtime; |
2060 | istate->timestamp.nsec = ST_MTIME_NSEC(st); | |
e1afca4f | 2061 | return 0; |
197ee8c9 | 2062 | } |
e46bbcf6 | 2063 | |
626f35c8 NTND |
2064 | void set_alternate_index_output(const char *name) |
2065 | { | |
2066 | alternate_index_output = name; | |
2067 | } | |
2068 | ||
2069 | static int commit_locked_index(struct lock_file *lk) | |
2070 | { | |
751baced MH |
2071 | if (alternate_index_output) |
2072 | return commit_lock_file_to(lk, alternate_index_output); | |
2073 | else | |
626f35c8 | 2074 | return commit_lock_file(lk); |
626f35c8 NTND |
2075 | } |
2076 | ||
03b86647 NTND |
2077 | static int do_write_locked_index(struct index_state *istate, struct lock_file *lock, |
2078 | unsigned flags) | |
2079 | { | |
c18b80a0 | 2080 | int ret = do_write_index(istate, lock->fd, 0); |
03b86647 NTND |
2081 | if (ret) |
2082 | return ret; | |
2083 | assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) != | |
2084 | (COMMIT_LOCK | CLOSE_LOCK)); | |
2085 | if (flags & COMMIT_LOCK) | |
2086 | return commit_locked_index(lock); | |
2087 | else if (flags & CLOSE_LOCK) | |
2088 | return close_lock_file(lock); | |
2089 | else | |
2090 | return ret; | |
2091 | } | |
2092 | ||
5fc2fc8f NTND |
2093 | static int write_split_index(struct index_state *istate, |
2094 | struct lock_file *lock, | |
2095 | unsigned flags) | |
2096 | { | |
2097 | int ret; | |
2098 | prepare_to_write_split_index(istate); | |
2099 | ret = do_write_locked_index(istate, lock, flags); | |
2100 | finish_writing_split_index(istate); | |
2101 | return ret; | |
2102 | } | |
2103 | ||
c18b80a0 NTND |
2104 | static char *temporary_sharedindex; |
2105 | ||
2106 | static void remove_temporary_sharedindex(void) | |
2107 | { | |
2108 | if (temporary_sharedindex) { | |
2109 | unlink_or_warn(temporary_sharedindex); | |
2110 | free(temporary_sharedindex); | |
2111 | temporary_sharedindex = NULL; | |
2112 | } | |
2113 | } | |
2114 | ||
2115 | static void remove_temporary_sharedindex_on_signal(int signo) | |
2116 | { | |
2117 | remove_temporary_sharedindex(); | |
2118 | sigchain_pop(signo); | |
2119 | raise(signo); | |
2120 | } | |
2121 | ||
a0a96756 NTND |
2122 | static int write_shared_index(struct index_state *istate, |
2123 | struct lock_file *lock, unsigned flags) | |
c18b80a0 NTND |
2124 | { |
2125 | struct split_index *si = istate->split_index; | |
2126 | static int installed_handler; | |
2127 | int fd, ret; | |
2128 | ||
2129 | temporary_sharedindex = git_pathdup("sharedindex_XXXXXX"); | |
a0a96756 NTND |
2130 | fd = mkstemp(temporary_sharedindex); |
2131 | if (fd < 0) { | |
2132 | free(temporary_sharedindex); | |
2133 | temporary_sharedindex = NULL; | |
2134 | hashclr(si->base_sha1); | |
2135 | return do_write_locked_index(istate, lock, flags); | |
2136 | } | |
c18b80a0 NTND |
2137 | if (!installed_handler) { |
2138 | atexit(remove_temporary_sharedindex); | |
2139 | sigchain_push_common(remove_temporary_sharedindex_on_signal); | |
2140 | } | |
2141 | move_cache_to_base_index(istate); | |
2142 | ret = do_write_index(si->base, fd, 1); | |
2143 | close(fd); | |
2144 | if (ret) { | |
2145 | remove_temporary_sharedindex(); | |
2146 | return ret; | |
2147 | } | |
2148 | ret = rename(temporary_sharedindex, | |
2149 | git_path("sharedindex.%s", sha1_to_hex(si->base->sha1))); | |
2150 | free(temporary_sharedindex); | |
2151 | temporary_sharedindex = NULL; | |
2152 | if (!ret) | |
2153 | hashcpy(si->base_sha1, si->base->sha1); | |
2154 | return ret; | |
2155 | } | |
2156 | ||
03b86647 NTND |
2157 | int write_locked_index(struct index_state *istate, struct lock_file *lock, |
2158 | unsigned flags) | |
2159 | { | |
5fc2fc8f NTND |
2160 | struct split_index *si = istate->split_index; |
2161 | ||
5165dd59 NTND |
2162 | if (!si || alternate_index_output || |
2163 | (istate->cache_changed & ~EXTMASK)) { | |
5fc2fc8f NTND |
2164 | if (si) |
2165 | hashclr(si->base_sha1); | |
2166 | return do_write_locked_index(istate, lock, flags); | |
2167 | } | |
2168 | ||
d6e3c181 NTND |
2169 | if (getenv("GIT_TEST_SPLIT_INDEX")) { |
2170 | int v = si->base_sha1[0]; | |
2171 | if ((v & 15) < 6) | |
2172 | istate->cache_changed |= SPLIT_INDEX_ORDERED; | |
2173 | } | |
c18b80a0 | 2174 | if (istate->cache_changed & SPLIT_INDEX_ORDERED) { |
a0a96756 | 2175 | int ret = write_shared_index(istate, lock, flags); |
c18b80a0 NTND |
2176 | if (ret) |
2177 | return ret; | |
2178 | } | |
2179 | ||
5fc2fc8f | 2180 | return write_split_index(istate, lock, flags); |
03b86647 NTND |
2181 | } |
2182 | ||
e46bbcf6 MV |
2183 | /* |
2184 | * Read the index file that is potentially unmerged into given | |
63e8dc5b | 2185 | * index_state, dropping any unmerged entries. Returns true if |
e46bbcf6 MV |
2186 | * the index is unmerged. Callers who want to refuse to work |
2187 | * from an unmerged state can call this and check its return value, | |
2188 | * instead of calling read_cache(). | |
2189 | */ | |
2190 | int read_index_unmerged(struct index_state *istate) | |
2191 | { | |
2192 | int i; | |
d1a43f2a | 2193 | int unmerged = 0; |
e46bbcf6 MV |
2194 | |
2195 | read_index(istate); | |
e46bbcf6 MV |
2196 | for (i = 0; i < istate->cache_nr; i++) { |
2197 | struct cache_entry *ce = istate->cache[i]; | |
d1a43f2a JH |
2198 | struct cache_entry *new_ce; |
2199 | int size, len; | |
2200 | ||
2201 | if (!ce_stage(ce)) | |
e46bbcf6 | 2202 | continue; |
d1a43f2a | 2203 | unmerged = 1; |
68c4f6a5 | 2204 | len = ce_namelen(ce); |
d1a43f2a JH |
2205 | size = cache_entry_size(len); |
2206 | new_ce = xcalloc(1, size); | |
d1a43f2a | 2207 | memcpy(new_ce->name, ce->name, len); |
b60e188c TG |
2208 | new_ce->ce_flags = create_ce_flags(0) | CE_CONFLICTED; |
2209 | new_ce->ce_namelen = len; | |
d1a43f2a JH |
2210 | new_ce->ce_mode = ce->ce_mode; |
2211 | if (add_index_entry(istate, new_ce, 0)) | |
2212 | return error("%s: cannot drop to stage #0", | |
5699d17e | 2213 | new_ce->name); |
e46bbcf6 | 2214 | } |
d1a43f2a | 2215 | return unmerged; |
e46bbcf6 | 2216 | } |
041aee31 | 2217 | |
98fa4738 JK |
2218 | /* |
2219 | * Returns 1 if the path is an "other" path with respect to | |
2220 | * the index; that is, the path is not mentioned in the index at all, | |
2221 | * either as a file, a directory with some files in the index, | |
2222 | * or as an unmerged entry. | |
2223 | * | |
2224 | * We helpfully remove a trailing "/" from directories so that | |
2225 | * the output of read_directory can be used as-is. | |
2226 | */ | |
2227 | int index_name_is_other(const struct index_state *istate, const char *name, | |
2228 | int namelen) | |
2229 | { | |
2230 | int pos; | |
2231 | if (namelen && name[namelen - 1] == '/') | |
2232 | namelen--; | |
2233 | pos = index_name_pos(istate, name, namelen); | |
2234 | if (0 <= pos) | |
2235 | return 0; /* exact match */ | |
2236 | pos = -pos - 1; | |
2237 | if (pos < istate->cache_nr) { | |
2238 | struct cache_entry *ce = istate->cache[pos]; | |
2239 | if (ce_namelen(ce) == namelen && | |
2240 | !memcmp(ce->name, name, namelen)) | |
2241 | return 0; /* Yup, this one exists unmerged */ | |
2242 | } | |
2243 | return 1; | |
2244 | } | |
29fb37b2 | 2245 | |
ff366825 | 2246 | void *read_blob_data_from_index(struct index_state *istate, const char *path, unsigned long *size) |
29fb37b2 LF |
2247 | { |
2248 | int pos, len; | |
2249 | unsigned long sz; | |
2250 | enum object_type type; | |
2251 | void *data; | |
2252 | ||
2253 | len = strlen(path); | |
2254 | pos = index_name_pos(istate, path, len); | |
2255 | if (pos < 0) { | |
2256 | /* | |
2257 | * We might be in the middle of a merge, in which | |
2258 | * case we would read stage #2 (ours). | |
2259 | */ | |
2260 | int i; | |
2261 | for (i = -pos - 1; | |
2262 | (pos < 0 && i < istate->cache_nr && | |
2263 | !strcmp(istate->cache[i]->name, path)); | |
2264 | i++) | |
2265 | if (ce_stage(istate->cache[i]) == 2) | |
2266 | pos = i; | |
2267 | } | |
2268 | if (pos < 0) | |
2269 | return NULL; | |
2270 | data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz); | |
2271 | if (!data || type != OBJ_BLOB) { | |
2272 | free(data); | |
2273 | return NULL; | |
2274 | } | |
ff366825 LF |
2275 | if (size) |
2276 | *size = sz; | |
29fb37b2 LF |
2277 | return data; |
2278 | } | |
38612532 MH |
2279 | |
2280 | void stat_validity_clear(struct stat_validity *sv) | |
2281 | { | |
2282 | free(sv->sd); | |
2283 | sv->sd = NULL; | |
2284 | } | |
2285 | ||
2286 | int stat_validity_check(struct stat_validity *sv, const char *path) | |
2287 | { | |
2288 | struct stat st; | |
2289 | ||
2290 | if (stat(path, &st) < 0) | |
2291 | return sv->sd == NULL; | |
2292 | if (!sv->sd) | |
2293 | return 0; | |
2294 | return S_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st); | |
2295 | } | |
2296 | ||
2297 | void stat_validity_update(struct stat_validity *sv, int fd) | |
2298 | { | |
2299 | struct stat st; | |
2300 | ||
2301 | if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode)) | |
2302 | stat_validity_clear(sv); | |
2303 | else { | |
2304 | if (!sv->sd) | |
2305 | sv->sd = xcalloc(1, sizeof(struct stat_data)); | |
2306 | fill_stat_data(sv->sd, &st); | |
2307 | } | |
2308 | } |