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