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