push: start warning upcoming default change for push.default
[git/gitweb.git] / read-cache.c
blob6c8f3958369b0a2afd0ad85aea5ab23a44678f70
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define NO_THE_INDEX_COMPATIBILITY_MACROS
7 #include "cache.h"
8 #include "cache-tree.h"
9 #include "refs.h"
10 #include "dir.h"
11 #include "tree.h"
12 #include "commit.h"
13 #include "blob.h"
14 #include "resolve-undo.h"
16 static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
18 /* Index extensions.
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.
27 #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
28 #define CACHE_EXT_TREE 0x54524545 /* "TREE" */
29 #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
31 struct index_state the_index;
33 static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
35 istate->cache[nr] = ce;
36 add_name_hash(istate, ce);
39 static void replace_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
41 struct cache_entry *old = istate->cache[nr];
43 remove_name_hash(old);
44 set_index_entry(istate, nr, ce);
45 istate->cache_changed = 1;
48 void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name)
50 struct cache_entry *old = istate->cache[nr], *new;
51 int namelen = strlen(new_name);
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);
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);
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.
69 void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
71 ce->ce_ctime.sec = (unsigned int)st->st_ctime;
72 ce->ce_mtime.sec = (unsigned int)st->st_mtime;
73 ce->ce_ctime.nsec = ST_CTIME_NSEC(*st);
74 ce->ce_mtime.nsec = ST_MTIME_NSEC(*st);
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;
81 if (assume_unchanged)
82 ce->ce_flags |= CE_VALID;
84 if (S_ISREG(st->st_mode))
85 ce_mark_uptodate(ce);
88 static int ce_compare_data(struct cache_entry *ce, struct stat *st)
90 int match = -1;
91 int fd = open(ce->name, O_RDONLY);
93 if (fd >= 0) {
94 unsigned char sha1[20];
95 if (!index_fd(sha1, fd, st, OBJ_BLOB, ce->name, 0))
96 match = hashcmp(sha1, ce->sha1);
97 /* index_fd() closed the file descriptor already */
99 return match;
102 static int ce_compare_link(struct cache_entry *ce, size_t expected_size)
104 int match = -1;
105 void *buffer;
106 unsigned long size;
107 enum object_type type;
108 struct strbuf sb = STRBUF_INIT;
110 if (strbuf_readlink(&sb, ce->name, expected_size))
111 return -1;
113 buffer = read_sha1_file(ce->sha1, &type, &size);
114 if (buffer) {
115 if (size == sb.len)
116 match = memcmp(buffer, sb.buf, size);
117 free(buffer);
119 strbuf_release(&sb);
120 return match;
123 static int ce_compare_gitlink(struct cache_entry *ce)
125 unsigned char sha1[20];
128 * We don't actually require that the .git directory
129 * under GITLINK directory be a valid git directory. It
130 * might even be missing (in case nobody populated that
131 * sub-project).
133 * If so, we consider it always to match.
135 if (resolve_gitlink_ref(ce->name, "HEAD", sha1) < 0)
136 return 0;
137 return hashcmp(sha1, ce->sha1);
140 static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
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:
148 if (ce_compare_link(ce, xsize_t(st->st_size)))
149 return DATA_CHANGED;
150 break;
151 case S_IFDIR:
152 if (S_ISGITLINK(ce->ce_mode))
153 return ce_compare_gitlink(ce) ? DATA_CHANGED : 0;
154 default:
155 return TYPE_CHANGED;
157 return 0;
160 static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
162 unsigned int changed = 0;
164 if (ce->ce_flags & CE_REMOVE)
165 return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED;
167 switch (ce->ce_mode & S_IFMT) {
168 case S_IFREG:
169 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
170 /* We consider only the owner x bit to be relevant for
171 * "mode changes"
173 if (trust_executable_bit &&
174 (0100 & (ce->ce_mode ^ st->st_mode)))
175 changed |= MODE_CHANGED;
176 break;
177 case S_IFLNK:
178 if (!S_ISLNK(st->st_mode) &&
179 (has_symlinks || !S_ISREG(st->st_mode)))
180 changed |= TYPE_CHANGED;
181 break;
182 case S_IFGITLINK:
183 /* We ignore most of the st_xxx fields for gitlinks */
184 if (!S_ISDIR(st->st_mode))
185 changed |= TYPE_CHANGED;
186 else if (ce_compare_gitlink(ce))
187 changed |= DATA_CHANGED;
188 return changed;
189 default:
190 die("internal error: ce_mode is %o", ce->ce_mode);
192 if (ce->ce_mtime.sec != (unsigned int)st->st_mtime)
193 changed |= MTIME_CHANGED;
194 if (trust_ctime && ce->ce_ctime.sec != (unsigned int)st->st_ctime)
195 changed |= CTIME_CHANGED;
197 #ifdef USE_NSEC
198 if (ce->ce_mtime.nsec != ST_MTIME_NSEC(*st))
199 changed |= MTIME_CHANGED;
200 if (trust_ctime && ce->ce_ctime.nsec != ST_CTIME_NSEC(*st))
201 changed |= CTIME_CHANGED;
202 #endif
204 if (ce->ce_uid != (unsigned int) st->st_uid ||
205 ce->ce_gid != (unsigned int) st->st_gid)
206 changed |= OWNER_CHANGED;
207 if (ce->ce_ino != (unsigned int) st->st_ino)
208 changed |= INODE_CHANGED;
210 #ifdef USE_STDEV
212 * st_dev breaks on network filesystems where different
213 * clients will have different views of what "device"
214 * the filesystem is on
216 if (ce->ce_dev != (unsigned int) st->st_dev)
217 changed |= INODE_CHANGED;
218 #endif
220 if (ce->ce_size != (unsigned int) st->st_size)
221 changed |= DATA_CHANGED;
223 /* Racily smudged entry? */
224 if (!ce->ce_size) {
225 if (!is_empty_blob_sha1(ce->sha1))
226 changed |= DATA_CHANGED;
229 return changed;
232 static int is_racy_timestamp(const struct index_state *istate, struct cache_entry *ce)
234 return (!S_ISGITLINK(ce->ce_mode) &&
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
247 int ie_match_stat(const struct index_state *istate,
248 struct cache_entry *ce, struct stat *st,
249 unsigned int options)
251 unsigned int changed;
252 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
253 int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
254 int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY;
257 * If it's marked as always valid in the index, it's
258 * valid whatever the checked-out copy says.
260 * skip-worktree has the same effect with higher precedence
262 if (!ignore_skip_worktree && ce_skip_worktree(ce))
263 return 0;
264 if (!ignore_valid && (ce->ce_flags & CE_VALID))
265 return 0;
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.
272 if (ce->ce_flags & CE_INTENT_TO_ADD)
273 return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED;
275 changed = ce_match_stat_basic(ce, st);
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.
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
291 * carefully than others.
293 if (!changed && is_racy_timestamp(istate, ce)) {
294 if (assume_racy_is_modified)
295 changed |= DATA_CHANGED;
296 else
297 changed |= ce_modified_check_fs(ce, st);
300 return changed;
303 int ie_modified(const struct index_state *istate,
304 struct cache_entry *ce, struct stat *st, unsigned int options)
306 int changed, changed_fs;
308 changed = ie_match_stat(istate, ce, st, options);
309 if (!changed)
310 return 0;
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
315 if (changed & (MODE_CHANGED | TYPE_CHANGED))
316 return changed;
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".
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.
332 if ((changed & DATA_CHANGED) &&
333 (S_ISGITLINK(ce->ce_mode) || ce->ce_size != 0))
334 return changed;
336 changed_fs = ce_modified_check_fs(ce, st);
337 if (changed_fs)
338 return changed | changed_fs;
339 return 0;
342 int base_name_compare(const char *name1, int len1, int mode1,
343 const char *name2, int len2, int mode2)
345 unsigned char c1, c2;
346 int len = len1 < len2 ? len1 : len2;
347 int cmp;
349 cmp = memcmp(name1, name2, len);
350 if (cmp)
351 return cmp;
352 c1 = name1[len];
353 c2 = name2[len];
354 if (!c1 && S_ISDIR(mode1))
355 c1 = '/';
356 if (!c2 && S_ISDIR(mode2))
357 c2 = '/';
358 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
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' < '.' < '/').
368 * This is used by routines that want to traverse the git namespace
369 * but then handle conflicting entries together when possible.
371 int df_name_compare(const char *name1, int len1, int mode1,
372 const char *name2, int len2, int mode2)
374 int len = len1 < len2 ? len1 : len2, cmp;
375 unsigned char c1, c2;
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;
396 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
398 int len1 = flags1 & CE_NAMEMASK;
399 int len2 = flags2 & CE_NAMEMASK;
400 int len = len1 < len2 ? len1 : len2;
401 int cmp;
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;
411 /* Compare stages */
412 flags1 &= CE_STAGEMASK;
413 flags2 &= CE_STAGEMASK;
415 if (flags1 < flags2)
416 return -1;
417 if (flags1 > flags2)
418 return 1;
419 return 0;
422 int index_name_pos(const struct index_state *istate, const char *name, int namelen)
424 int first, last;
426 first = 0;
427 last = istate->cache_nr;
428 while (last > first) {
429 int next = (last + first) >> 1;
430 struct cache_entry *ce = istate->cache[next];
431 int cmp = cache_name_compare(name, namelen, ce->name, ce->ce_flags);
432 if (!cmp)
433 return next;
434 if (cmp < 0) {
435 last = next;
436 continue;
438 first = next+1;
440 return -first-1;
443 /* Remove entry, return true if there are more entries to go.. */
444 int remove_index_entry_at(struct index_state *istate, int pos)
446 struct cache_entry *ce = istate->cache[pos];
448 record_resolve_undo(istate, ce);
449 remove_name_hash(ce);
450 istate->cache_changed = 1;
451 istate->cache_nr--;
452 if (pos >= istate->cache_nr)
453 return 0;
454 memmove(istate->cache + pos,
455 istate->cache + pos + 1,
456 (istate->cache_nr - pos) * sizeof(struct cache_entry *));
457 return 1;
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.
465 void remove_marked_cache_entries(struct index_state *istate)
467 struct cache_entry **ce_array = istate->cache;
468 unsigned int i, j;
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];
476 istate->cache_changed = 1;
477 istate->cache_nr = j;
480 int remove_file_from_index(struct index_state *istate, const char *path)
482 int pos = index_name_pos(istate, path, strlen(path));
483 if (pos < 0)
484 pos = -pos-1;
485 cache_tree_invalidate_path(istate->cache_tree, path);
486 while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path))
487 remove_index_entry_at(istate, pos);
488 return 0;
491 static int compare_name(struct cache_entry *ce, const char *path, int namelen)
493 return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen);
496 static int index_name_pos_also_unmerged(struct index_state *istate,
497 const char *path, int namelen)
499 int pos = index_name_pos(istate, path, namelen);
500 struct cache_entry *ce;
502 if (pos >= 0)
503 return pos;
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;
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;
519 static int different_name(struct cache_entry *ce, struct cache_entry *alias)
521 int len = ce_namelen(ce);
522 return ce_namelen(alias) != len || memcmp(ce->name, alias->name, len);
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!
531 * So we use the CE_ADDED flag to verify that the alias was an old
532 * one before we accept it as
534 static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_entry *alias)
536 int len;
537 struct cache_entry *new;
539 if (alias->ce_flags & CE_ADDED)
540 die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name);
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;
551 static void record_intent_to_add(struct cache_entry *ce)
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);
559 int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
561 int size, namelen, was_same;
562 mode_t st_mode = st->st_mode;
563 struct cache_entry *ce, *alias;
564 unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY;
565 int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND);
566 int pretend = flags & ADD_CACHE_PRETEND;
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));
571 if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode))
572 return error("%s: can only add regular files, symbolic links or git-directories", path);
574 namelen = strlen(path);
575 if (S_ISDIR(st_mode)) {
576 while (namelen && path[namelen-1] == '/')
577 namelen--;
579 size = cache_entry_size(namelen);
580 ce = xcalloc(1, size);
581 memcpy(ce->name, path, namelen);
582 ce->ce_flags = namelen;
583 if (!intent_only)
584 fill_stat_cache_info(ce, st);
585 else
586 ce->ce_flags |= CE_INTENT_TO_ADD;
588 if (trust_executable_bit && has_symlinks)
589 ce->ce_mode = create_ce_mode(st_mode);
590 else {
591 /* If there is an existing entry, pick the mode bits and type
592 * from it, otherwise assume unexecutable regular file.
594 struct cache_entry *ent;
595 int pos = index_name_pos_also_unmerged(istate, path, namelen);
597 ent = (0 <= pos) ? istate->cache[pos] : NULL;
598 ce->ce_mode = ce_mode_from_stat(ent, st_mode);
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.
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;
624 alias = index_name_exists(istate, ce->name, ce_namelen(ce), ignore_case);
625 if (alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) {
626 /* Nothing changed, really */
627 free(ce);
628 if (!S_ISGITLINK(alias->ce_mode))
629 ce_mark_uptodate(alias);
630 alias->ce_flags |= CE_ADDED;
631 return 0;
633 if (!intent_only) {
634 if (index_path(ce->sha1, path, st, HASH_WRITE_OBJECT))
635 return error("unable to index file %s", path);
636 } else
637 record_intent_to_add(ce);
639 if (ignore_case && alias && different_name(ce, alias))
640 ce = create_alias_ce(ce, alias);
641 ce->ce_flags |= CE_ADDED;
643 /* It was suspected to be racily clean, but it turns out to be Ok */
644 was_same = (alias &&
645 !ce_stage(alias) &&
646 !hashcmp(alias->sha1, ce->sha1) &&
647 ce->ce_mode == alias->ce_mode);
649 if (pretend)
651 else if (add_index_entry(istate, ce, add_option))
652 return error("unable to add %s to index",path);
653 if (verbose && !was_same)
654 printf("add '%s'\n", path);
655 return 0;
658 int add_file_to_index(struct index_state *istate, const char *path, int flags)
660 struct stat st;
661 if (lstat(path, &st))
662 die_errno("unable to stat '%s'", path);
663 return add_to_index(istate, path, &st, flags);
666 struct cache_entry *make_cache_entry(unsigned int mode,
667 const unsigned char *sha1, const char *path, int stage,
668 int refresh)
670 int size, len;
671 struct cache_entry *ce;
673 if (!verify_path(path)) {
674 error("Invalid path '%s'", path);
675 return NULL;
678 len = strlen(path);
679 size = cache_entry_size(len);
680 ce = xcalloc(1, size);
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);
687 if (refresh)
688 return refresh_cache_entry(ce, 0);
690 return ce;
693 int ce_same_name(struct cache_entry *a, struct cache_entry *b)
695 int len = ce_namelen(a);
696 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
699 int ce_path_match(const struct cache_entry *ce, const struct pathspec *pathspec)
701 return match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL);
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.
709 * Also, we don't want double slashes or slashes at the
710 * end that can make pathnames ambiguous.
712 static int verify_dotfile(const char *rest)
715 * The first character was '.', but that
716 * has already been discarded, we now test
717 * the rest.
720 /* "." is not allowed */
721 if (*rest == '\0' || is_dir_sep(*rest))
722 return 0;
724 switch (*rest) {
726 * ".git" followed by NUL or slash is bad. This
727 * shares the path end test with the ".." case.
729 case 'g':
730 if (rest[1] != 'i')
731 break;
732 if (rest[2] != 't')
733 break;
734 rest += 2;
735 /* fallthrough */
736 case '.':
737 if (rest[1] == '\0' || is_dir_sep(rest[1]))
738 return 0;
740 return 1;
743 int verify_path(const char *path)
745 char c;
747 if (has_dos_drive_prefix(path))
748 return 0;
750 goto inside;
751 for (;;) {
752 if (!c)
753 return 1;
754 if (is_dir_sep(c)) {
755 inside:
756 c = *path++;
757 if ((c == '.' && !verify_dotfile(path)) ||
758 is_dir_sep(c) || c == '\0')
759 return 0;
761 c = *path++;
766 * Do we have another file that has the beginning components being a
767 * proper superset of the name we're trying to add?
769 static int has_file_name(struct index_state *istate,
770 const struct cache_entry *ce, int pos, int ok_to_replace)
772 int retval = 0;
773 int len = ce_namelen(ce);
774 int stage = ce_stage(ce);
775 const char *name = ce->name;
777 while (pos < istate->cache_nr) {
778 struct cache_entry *p = istate->cache[pos++];
780 if (len >= ce_namelen(p))
781 break;
782 if (memcmp(name, p->name, len))
783 break;
784 if (ce_stage(p) != stage)
785 continue;
786 if (p->name[len] != '/')
787 continue;
788 if (p->ce_flags & CE_REMOVE)
789 continue;
790 retval = -1;
791 if (!ok_to_replace)
792 break;
793 remove_index_entry_at(istate, --pos);
795 return retval;
799 * Do we have another file with a pathname that is a proper
800 * subset of the name we're trying to add?
802 static int has_dir_name(struct index_state *istate,
803 const struct cache_entry *ce, int pos, int ok_to_replace)
805 int retval = 0;
806 int stage = ce_stage(ce);
807 const char *name = ce->name;
808 const char *slash = name + ce_namelen(ce);
810 for (;;) {
811 int len;
813 for (;;) {
814 if (*--slash == '/')
815 break;
816 if (slash <= ce->name)
817 return retval;
819 len = slash - name;
821 pos = index_name_pos(istate, name, create_ce_flags(len, stage));
822 if (pos >= 0) {
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.
831 if (!(istate->cache[pos]->ce_flags & CE_REMOVE)) {
832 retval = -1;
833 if (!ok_to_replace)
834 break;
835 remove_index_entry_at(istate, pos);
836 continue;
839 else
840 pos = -pos-1;
843 * Trivial optimization: if we find an entry that
844 * already matches the sub-directory, then we know
845 * we're ok, and we can exit.
847 while (pos < istate->cache_nr) {
848 struct cache_entry *p = istate->cache[pos];
849 if ((ce_namelen(p) <= len) ||
850 (p->name[len] != '/') ||
851 memcmp(p->name, name, len))
852 break; /* not our subdirectory */
853 if (ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE))
855 * p is at the same stage as our entry, and
856 * is a subdirectory of what we are looking
857 * at, so we cannot have conflicts at our
858 * level or anything shorter.
860 return retval;
861 pos++;
864 return retval;
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.
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.
876 static int check_file_directory_conflict(struct index_state *istate,
877 const struct cache_entry *ce,
878 int pos, int ok_to_replace)
880 int retval;
883 * When ce is an "I am going away" entry, we allow it to be added
885 if (ce->ce_flags & CE_REMOVE)
886 return 0;
889 * We check if the path is a sub-path of a subsequent pathname
890 * first, since removing those will not change the position
891 * in the array.
893 retval = has_file_name(istate, ce, pos, ok_to_replace);
896 * Then check if the path might have a clashing sub-directory
897 * before it.
899 return retval + has_dir_name(istate, ce, pos, ok_to_replace);
902 static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option)
904 int pos;
905 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
906 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
907 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
908 int new_only = option & ADD_CACHE_NEW_ONLY;
910 cache_tree_invalidate_path(istate->cache_tree, ce->name);
911 pos = index_name_pos(istate, ce->name, ce->ce_flags);
913 /* existing match? Just replace it. */
914 if (pos >= 0) {
915 if (!new_only)
916 replace_index_entry(istate, pos, ce);
917 return 0;
919 pos = -pos-1;
922 * Inserting a merged entry ("stage 0") into the index
923 * will always replace all non-merged entries..
925 if (pos < istate->cache_nr && ce_stage(ce) == 0) {
926 while (ce_same_name(istate->cache[pos], ce)) {
927 ok_to_add = 1;
928 if (!remove_index_entry_at(istate, pos))
929 break;
933 if (!ok_to_add)
934 return -1;
935 if (!verify_path(ce->name))
936 return error("Invalid path '%s'", ce->name);
938 if (!skip_df_check &&
939 check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {
940 if (!ok_to_replace)
941 return error("'%s' appears as both a file and as a directory",
942 ce->name);
943 pos = index_name_pos(istate, ce->name, ce->ce_flags);
944 pos = -pos-1;
946 return pos + 1;
949 int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
951 int pos;
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;
963 /* Make sure the array is big enough .. */
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 *));
970 /* Add it in.. */
971 istate->cache_nr++;
972 if (istate->cache_nr > pos + 1)
973 memmove(istate->cache + pos + 1,
974 istate->cache + pos,
975 (istate->cache_nr - pos - 1) * sizeof(ce));
976 set_index_entry(istate, pos, ce);
977 istate->cache_changed = 1;
978 return 0;
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.
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.
992 static struct cache_entry *refresh_cache_ent(struct index_state *istate,
993 struct cache_entry *ce,
994 unsigned int options, int *err,
995 int *changed_ret)
997 struct stat st;
998 struct cache_entry *updated;
999 int changed, size;
1000 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
1001 int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
1003 if (ce_uptodate(ce))
1004 return ce;
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.
1011 if (!ignore_skip_worktree && ce_skip_worktree(ce)) {
1012 ce_mark_uptodate(ce);
1013 return ce;
1015 if (!ignore_valid && (ce->ce_flags & CE_VALID)) {
1016 ce_mark_uptodate(ce);
1017 return ce;
1020 if (lstat(ce->name, &st) < 0) {
1021 if (err)
1022 *err = errno;
1023 return NULL;
1026 changed = ie_match_stat(istate, ce, &st, options);
1027 if (changed_ret)
1028 *changed_ret = changed;
1029 if (!changed) {
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.
1037 if (ignore_valid && assume_unchanged &&
1038 !(ce->ce_flags & CE_VALID))
1039 ; /* mark this one VALID again */
1040 else {
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.
1046 if (!S_ISGITLINK(ce->ce_mode))
1047 ce_mark_uptodate(ce);
1048 return ce;
1052 if (ie_modified(istate, ce, &st, options)) {
1053 if (err)
1054 *err = EINVAL;
1055 return NULL;
1058 size = ce_size(ce);
1059 updated = xmalloc(size);
1060 memcpy(updated, ce, size);
1061 fill_stat_cache_info(updated, &st);
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.
1068 if (!ignore_valid && assume_unchanged &&
1069 !(ce->ce_flags & CE_VALID))
1070 updated->ce_flags &= ~CE_VALID;
1072 return updated;
1075 static void show_file(const char * fmt, const char * name, int in_porcelain,
1076 int * first, const char *header_msg)
1078 if (in_porcelain && *first && header_msg) {
1079 printf("%s\n", header_msg);
1080 *first = 0;
1082 printf(fmt, name);
1085 int refresh_index(struct index_state *istate, unsigned int flags, const char **pathspec,
1086 char *seen, const char *header_msg)
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;
1094 int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0;
1095 int first = 1;
1096 int in_porcelain = (flags & REFRESH_IN_PORCELAIN);
1097 unsigned int options = really ? CE_MATCH_IGNORE_VALID : 0;
1098 const char *modified_fmt;
1099 const char *deleted_fmt;
1100 const char *typechange_fmt;
1101 const char *added_fmt;
1102 const char *unmerged_fmt;
1104 modified_fmt = (in_porcelain ? "M\t%s\n" : "%s: needs update\n");
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");
1108 unmerged_fmt = (in_porcelain ? "U\t%s\n" : "%s: needs merge\n");
1109 for (i = 0; i < istate->cache_nr; i++) {
1110 struct cache_entry *ce, *new;
1111 int cache_errno = 0;
1112 int changed = 0;
1113 int filtered = 0;
1115 ce = istate->cache[i];
1116 if (ignore_submodules && S_ISGITLINK(ce->ce_mode))
1117 continue;
1119 if (pathspec &&
1120 !match_pathspec(pathspec, ce->name, strlen(ce->name), 0, seen))
1121 filtered = 1;
1123 if (ce_stage(ce)) {
1124 while ((i < istate->cache_nr) &&
1125 ! strcmp(istate->cache[i]->name, ce->name))
1126 i++;
1127 i--;
1128 if (allow_unmerged)
1129 continue;
1130 if (!filtered)
1131 show_file(unmerged_fmt, ce->name, in_porcelain,
1132 &first, header_msg);
1133 has_errors = 1;
1134 continue;
1137 if (filtered)
1138 continue;
1140 new = refresh_cache_ent(istate, ce, options, &cache_errno, &changed);
1141 if (new == ce)
1142 continue;
1143 if (!new) {
1144 const char *fmt;
1146 if (not_new && cache_errno == ENOENT)
1147 continue;
1148 if (really && cache_errno == EINVAL) {
1149 /* If we are doing --really-refresh that
1150 * means the index is not valid anymore.
1152 ce->ce_flags &= ~CE_VALID;
1153 istate->cache_changed = 1;
1155 if (quiet)
1156 continue;
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);
1168 has_errors = 1;
1169 continue;
1172 replace_index_entry(istate, i, new);
1174 return has_errors;
1177 static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
1179 return refresh_cache_ent(&the_index, ce, really, NULL, NULL);
1182 static int verify_hdr(struct cache_header *hdr, unsigned long size)
1184 git_SHA_CTX c;
1185 unsigned char sha1[20];
1187 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
1188 return error("bad signature");
1189 if (hdr->hdr_version != htonl(2) && hdr->hdr_version != htonl(3))
1190 return error("bad index version");
1191 git_SHA1_Init(&c);
1192 git_SHA1_Update(&c, hdr, size - 20);
1193 git_SHA1_Final(sha1, &c);
1194 if (hashcmp(sha1, (unsigned char *)hdr + size - 20))
1195 return error("bad index file sha1 signature");
1196 return 0;
1199 static int read_index_extension(struct index_state *istate,
1200 const char *ext, void *data, unsigned long sz)
1202 switch (CACHE_EXT(ext)) {
1203 case CACHE_EXT_TREE:
1204 istate->cache_tree = cache_tree_read(data, sz);
1205 break;
1206 case CACHE_EXT_RESOLVE_UNDO:
1207 istate->resolve_undo = resolve_undo_read(data, sz);
1208 break;
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;
1216 return 0;
1219 int read_index(struct index_state *istate)
1221 return read_index_from(istate, get_index_file());
1224 static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk)
1226 struct cache_entry *ce;
1227 size_t len;
1228 const char *name;
1229 unsigned int flags;
1231 /* On-disk flags are just 16 bits */
1232 flags = ntohs(ondisk->flags);
1233 len = flags & CE_NAMEMASK;
1235 if (flags & CE_EXTENDED) {
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);
1243 flags |= extended_flags;
1244 name = ondisk2->name;
1246 else
1247 name = ondisk->name;
1249 if (len == CE_NAMEMASK)
1250 len = strlen(name);
1252 ce = xmalloc(cache_entry_size(len));
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;
1266 hashcpy(ce->sha1, ondisk->sha1);
1268 memcpy(ce->name, name, len);
1269 ce->name[len] = '\0';
1270 return ce;
1273 /* remember to discard_cache() before reading a different cache! */
1274 int read_index_from(struct index_state *istate, const char *path)
1276 int fd, i;
1277 struct stat st;
1278 unsigned long src_offset;
1279 struct cache_header *hdr;
1280 void *mmap;
1281 size_t mmap_size;
1283 errno = EBUSY;
1284 if (istate->initialized)
1285 return istate->cache_nr;
1287 errno = ENOENT;
1288 istate->timestamp.sec = 0;
1289 istate->timestamp.nsec = 0;
1290 fd = open(path, O_RDONLY);
1291 if (fd < 0) {
1292 if (errno == ENOENT)
1293 return 0;
1294 die_errno("index file open failed");
1297 if (fstat(fd, &st))
1298 die_errno("cannot stat the open index");
1300 errno = EINVAL;
1301 mmap_size = xsize_t(st.st_size);
1302 if (mmap_size < sizeof(struct cache_header) + 20)
1303 die("index file smaller than expected");
1305 mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
1306 close(fd);
1307 if (mmap == MAP_FAILED)
1308 die_errno("unable to map index file");
1310 hdr = mmap;
1311 if (verify_hdr(hdr, mmap_size) < 0)
1312 goto unmap;
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 *));
1317 istate->initialized = 1;
1319 src_offset = sizeof(*hdr);
1320 for (i = 0; i < istate->cache_nr; i++) {
1321 struct ondisk_cache_entry *disk_ce;
1322 struct cache_entry *ce;
1324 disk_ce = (struct ondisk_cache_entry *)((char *)mmap + src_offset);
1325 ce = create_from_disk(disk_ce);
1326 set_index_entry(istate, i, ce);
1328 src_offset += ondisk_ce_size(ce);
1330 istate->timestamp.sec = st.st_mtime;
1331 istate->timestamp.nsec = ST_MTIME_NSEC(st);
1333 while (src_offset <= mmap_size - 20 - 8) {
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.
1340 uint32_t extsize;
1341 memcpy(&extsize, (char *)mmap + src_offset + 4, 4);
1342 extsize = ntohl(extsize);
1343 if (read_index_extension(istate,
1344 (const char *) mmap + src_offset,
1345 (char *) mmap + src_offset + 8,
1346 extsize) < 0)
1347 goto unmap;
1348 src_offset += 8;
1349 src_offset += extsize;
1351 munmap(mmap, mmap_size);
1352 return istate->cache_nr;
1354 unmap:
1355 munmap(mmap, mmap_size);
1356 errno = EINVAL;
1357 die("index file corrupt");
1360 int is_index_unborn(struct index_state *istate)
1362 return (!istate->cache_nr && !istate->timestamp.sec);
1365 int discard_index(struct index_state *istate)
1367 int i;
1369 for (i = 0; i < istate->cache_nr; i++)
1370 free(istate->cache[i]);
1371 resolve_undo_clear_index(istate);
1372 istate->cache_nr = 0;
1373 istate->cache_changed = 0;
1374 istate->timestamp.sec = 0;
1375 istate->timestamp.nsec = 0;
1376 istate->name_hash_initialized = 0;
1377 free_hash(&istate->name_hash);
1378 cache_tree_free(&(istate->cache_tree));
1379 istate->initialized = 0;
1381 /* no need to throw away allocated active_cache */
1382 return 0;
1385 int unmerged_index(const struct index_state *istate)
1387 int i;
1388 for (i = 0; i < istate->cache_nr; i++) {
1389 if (ce_stage(istate->cache[i]))
1390 return 1;
1392 return 0;
1395 #define WRITE_BUFFER_SIZE 8192
1396 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
1397 static unsigned long write_buffer_len;
1399 static int ce_write_flush(git_SHA_CTX *context, int fd)
1401 unsigned int buffered = write_buffer_len;
1402 if (buffered) {
1403 git_SHA1_Update(context, write_buffer, buffered);
1404 if (write_in_full(fd, write_buffer, buffered) != buffered)
1405 return -1;
1406 write_buffer_len = 0;
1408 return 0;
1411 static int ce_write(git_SHA_CTX *context, int fd, void *data, unsigned int len)
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) {
1421 write_buffer_len = buffered;
1422 if (ce_write_flush(context, fd))
1423 return -1;
1424 buffered = 0;
1426 write_buffer_len = buffered;
1427 len -= partial;
1428 data = (char *) data + partial;
1430 return 0;
1433 static int write_index_ext_header(git_SHA_CTX *context, int fd,
1434 unsigned int ext, unsigned int sz)
1436 ext = htonl(ext);
1437 sz = htonl(sz);
1438 return ((ce_write(context, fd, &ext, 4) < 0) ||
1439 (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
1442 static int ce_flush(git_SHA_CTX *context, int fd)
1444 unsigned int left = write_buffer_len;
1446 if (left) {
1447 write_buffer_len = 0;
1448 git_SHA1_Update(context, write_buffer, left);
1451 /* Flush first if not enough space for SHA1 signature */
1452 if (left + 20 > WRITE_BUFFER_SIZE) {
1453 if (write_in_full(fd, write_buffer, left) != left)
1454 return -1;
1455 left = 0;
1458 /* Append the SHA1 signature at the end */
1459 git_SHA1_Final(write_buffer + left, context);
1460 left += 20;
1461 return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
1464 static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
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.
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 ;-)
1477 struct stat st;
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)) {
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:
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
1495 * but it does not. When the second update-index runs,
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
1501 * obtain from the filesystem next time we stat("frotz").
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.
1509 ce->ce_size = 0;
1513 static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce)
1515 int size = ondisk_ce_size(ce);
1516 struct ondisk_cache_entry *ondisk = xcalloc(1, size);
1517 char *name;
1518 int result;
1520 ondisk->ctime.sec = htonl(ce->ce_ctime.sec);
1521 ondisk->mtime.sec = htonl(ce->ce_mtime.sec);
1522 ondisk->ctime.nsec = htonl(ce->ce_ctime.nsec);
1523 ondisk->mtime.nsec = htonl(ce->ce_mtime.nsec);
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);
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;
1538 else
1539 name = ondisk->name;
1540 memcpy(name, ce->name, ce_namelen(ce));
1542 result = ce_write(c, fd, ondisk, size);
1543 free(ondisk);
1544 return result;
1547 static int has_racy_timestamp(struct index_state *istate)
1549 int entries = istate->cache_nr;
1550 int i;
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;
1557 return 0;
1561 * Opportunisticly update the index but do not complain if we can't
1563 void update_index_if_able(struct index_state *istate, struct lock_file *lockfile)
1565 if ((istate->cache_changed || has_racy_timestamp(istate)) &&
1566 !write_index(istate, lockfile->fd))
1567 commit_locked_index(lockfile);
1568 else
1569 rollback_lock_file(lockfile);
1572 int write_index(struct index_state *istate, int newfd)
1574 git_SHA_CTX c;
1575 struct cache_header hdr;
1576 int i, err, removed, extended;
1577 struct cache_entry **cache = istate->cache;
1578 int entries = istate->cache_nr;
1579 struct stat st;
1581 for (i = removed = extended = 0; i < entries; i++) {
1582 if (cache[i]->ce_flags & CE_REMOVE)
1583 removed++;
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;
1593 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
1594 /* for extended format, increase version so older git won't try to read it */
1595 hdr.hdr_version = htonl(extended ? 3 : 2);
1596 hdr.hdr_entries = htonl(entries - removed);
1598 git_SHA1_Init(&c);
1599 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
1600 return -1;
1602 for (i = 0; i < entries; i++) {
1603 struct cache_entry *ce = cache[i];
1604 if (ce->ce_flags & CE_REMOVE)
1605 continue;
1606 if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce))
1607 ce_smudge_racily_clean_entry(ce);
1608 if (ce_write_entry(&c, newfd, ce) < 0)
1609 return -1;
1612 /* Write extension data here */
1613 if (istate->cache_tree) {
1614 struct strbuf sb = STRBUF_INIT;
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)
1621 return -1;
1623 if (istate->resolve_undo) {
1624 struct strbuf sb = STRBUF_INIT;
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;
1635 if (ce_flush(&c, newfd) || fstat(newfd, &st))
1636 return -1;
1637 istate->timestamp.sec = (unsigned int)st.st_mtime;
1638 istate->timestamp.nsec = ST_MTIME_NSEC(st);
1639 return 0;
1643 * Read the index file that is potentially unmerged into given
1644 * index_state, dropping any unmerged entries. Returns true if
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().
1649 int read_index_unmerged(struct index_state *istate)
1651 int i;
1652 int unmerged = 0;
1654 read_index(istate);
1655 for (i = 0; i < istate->cache_nr; i++) {
1656 struct cache_entry *ce = istate->cache[i];
1657 struct cache_entry *new_ce;
1658 int size, len;
1660 if (!ce_stage(ce))
1661 continue;
1662 unmerged = 1;
1663 len = strlen(ce->name);
1664 size = cache_entry_size(len);
1665 new_ce = xcalloc(1, size);
1666 memcpy(new_ce->name, ce->name, len);
1667 new_ce->ce_flags = create_ce_flags(len, 0) | CE_CONFLICTED;
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);
1674 return unmerged;
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.
1683 * We helpfully remove a trailing "/" from directories so that
1684 * the output of read_directory can be used as-is.
1686 int index_name_is_other(const struct index_state *istate, const char *name,
1687 int namelen)
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 */
1702 return 1;