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