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