2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #define NO_THE_INDEX_COMPATIBILITY_MACROS
9 #include "cache-tree.h"
15 #include "resolve-undo.h"
18 #include "split-index.h"
22 static struct cache_entry
*refresh_cache_entry(struct cache_entry
*ce
,
23 unsigned int options
);
25 /* Mask for the name length in ce_flags in the on-disk index */
27 #define CE_NAMEMASK (0x0fff)
31 * The first letter should be 'A'..'Z' for extensions that are not
32 * necessary for a correct operation (i.e. optimization data).
33 * When new extensions are added that _needs_ to be understood in
34 * order to correctly interpret the index file, pick character that
35 * is outside the range, to cause the reader to abort.
38 #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
39 #define CACHE_EXT_TREE 0x54524545 /* "TREE" */
40 #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
41 #define CACHE_EXT_LINK 0x6c696e6b /* "link" */
43 /* changes that can be kept in $GIT_DIR/index (basically all extensions) */
44 #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
45 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \
48 struct index_state the_index
;
49 static const char *alternate_index_output
;
51 static void set_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
53 istate
->cache
[nr
] = ce
;
54 add_name_hash(istate
, ce
);
57 static void replace_index_entry(struct index_state
*istate
, int nr
, struct cache_entry
*ce
)
59 struct cache_entry
*old
= istate
->cache
[nr
];
61 replace_index_entry_in_base(istate
, old
, ce
);
62 remove_name_hash(istate
, old
);
64 set_index_entry(istate
, nr
, ce
);
65 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
66 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
69 void rename_index_entry_at(struct index_state
*istate
, int nr
, const char *new_name
)
71 struct cache_entry
*old
= istate
->cache
[nr
], *new;
72 int namelen
= strlen(new_name
);
74 new = xmalloc(cache_entry_size(namelen
));
75 copy_cache_entry(new, old
);
76 new->ce_flags
&= ~CE_HASHED
;
77 new->ce_namelen
= namelen
;
79 memcpy(new->name
, new_name
, namelen
+ 1);
81 cache_tree_invalidate_path(istate
, old
->name
);
82 remove_index_entry_at(istate
, nr
);
83 add_index_entry(istate
, new, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
);
86 void fill_stat_data(struct stat_data
*sd
, struct stat
*st
)
88 sd
->sd_ctime
.sec
= (unsigned int)st
->st_ctime
;
89 sd
->sd_mtime
.sec
= (unsigned int)st
->st_mtime
;
90 sd
->sd_ctime
.nsec
= ST_CTIME_NSEC(*st
);
91 sd
->sd_mtime
.nsec
= ST_MTIME_NSEC(*st
);
92 sd
->sd_dev
= st
->st_dev
;
93 sd
->sd_ino
= st
->st_ino
;
94 sd
->sd_uid
= st
->st_uid
;
95 sd
->sd_gid
= st
->st_gid
;
96 sd
->sd_size
= st
->st_size
;
99 int match_stat_data(const struct stat_data
*sd
, struct stat
*st
)
103 if (sd
->sd_mtime
.sec
!= (unsigned int)st
->st_mtime
)
104 changed
|= MTIME_CHANGED
;
105 if (trust_ctime
&& check_stat
&&
106 sd
->sd_ctime
.sec
!= (unsigned int)st
->st_ctime
)
107 changed
|= CTIME_CHANGED
;
110 if (check_stat
&& sd
->sd_mtime
.nsec
!= ST_MTIME_NSEC(*st
))
111 changed
|= MTIME_CHANGED
;
112 if (trust_ctime
&& check_stat
&&
113 sd
->sd_ctime
.nsec
!= ST_CTIME_NSEC(*st
))
114 changed
|= CTIME_CHANGED
;
118 if (sd
->sd_uid
!= (unsigned int) st
->st_uid
||
119 sd
->sd_gid
!= (unsigned int) st
->st_gid
)
120 changed
|= OWNER_CHANGED
;
121 if (sd
->sd_ino
!= (unsigned int) st
->st_ino
)
122 changed
|= INODE_CHANGED
;
127 * st_dev breaks on network filesystems where different
128 * clients will have different views of what "device"
129 * the filesystem is on
131 if (check_stat
&& sd
->sd_dev
!= (unsigned int) st
->st_dev
)
132 changed
|= INODE_CHANGED
;
135 if (sd
->sd_size
!= (unsigned int) st
->st_size
)
136 changed
|= DATA_CHANGED
;
142 * This only updates the "non-critical" parts of the directory
143 * cache, ie the parts that aren't tracked by GIT, and only used
144 * to validate the cache.
146 void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
148 fill_stat_data(&ce
->ce_stat_data
, st
);
150 if (assume_unchanged
)
151 ce
->ce_flags
|= CE_VALID
;
153 if (S_ISREG(st
->st_mode
))
154 ce_mark_uptodate(ce
);
157 static int ce_compare_data(const struct cache_entry
*ce
, struct stat
*st
)
160 int fd
= open(ce
->name
, O_RDONLY
);
163 unsigned char sha1
[20];
164 if (!index_fd(sha1
, fd
, st
, OBJ_BLOB
, ce
->name
, 0))
165 match
= hashcmp(sha1
, ce
->sha1
);
166 /* index_fd() closed the file descriptor already */
171 static int ce_compare_link(const struct cache_entry
*ce
, size_t expected_size
)
176 enum object_type type
;
177 struct strbuf sb
= STRBUF_INIT
;
179 if (strbuf_readlink(&sb
, ce
->name
, expected_size
))
182 buffer
= read_sha1_file(ce
->sha1
, &type
, &size
);
185 match
= memcmp(buffer
, sb
.buf
, size
);
192 static int ce_compare_gitlink(const struct cache_entry
*ce
)
194 unsigned char sha1
[20];
197 * We don't actually require that the .git directory
198 * under GITLINK directory be a valid git directory. It
199 * might even be missing (in case nobody populated that
202 * If so, we consider it always to match.
204 if (resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) < 0)
206 return hashcmp(sha1
, ce
->sha1
);
209 static int ce_modified_check_fs(const struct cache_entry
*ce
, struct stat
*st
)
211 switch (st
->st_mode
& S_IFMT
) {
213 if (ce_compare_data(ce
, st
))
217 if (ce_compare_link(ce
, xsize_t(st
->st_size
)))
221 if (S_ISGITLINK(ce
->ce_mode
))
222 return ce_compare_gitlink(ce
) ? DATA_CHANGED
: 0;
229 static int ce_match_stat_basic(const struct cache_entry
*ce
, struct stat
*st
)
231 unsigned int changed
= 0;
233 if (ce
->ce_flags
& CE_REMOVE
)
234 return MODE_CHANGED
| DATA_CHANGED
| TYPE_CHANGED
;
236 switch (ce
->ce_mode
& S_IFMT
) {
238 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
239 /* We consider only the owner x bit to be relevant for
242 if (trust_executable_bit
&&
243 (0100 & (ce
->ce_mode
^ st
->st_mode
)))
244 changed
|= MODE_CHANGED
;
247 if (!S_ISLNK(st
->st_mode
) &&
248 (has_symlinks
|| !S_ISREG(st
->st_mode
)))
249 changed
|= TYPE_CHANGED
;
252 /* We ignore most of the st_xxx fields for gitlinks */
253 if (!S_ISDIR(st
->st_mode
))
254 changed
|= TYPE_CHANGED
;
255 else if (ce_compare_gitlink(ce
))
256 changed
|= DATA_CHANGED
;
259 die("internal error: ce_mode is %o", ce
->ce_mode
);
262 changed
|= match_stat_data(&ce
->ce_stat_data
, st
);
264 /* Racily smudged entry? */
265 if (!ce
->ce_stat_data
.sd_size
) {
266 if (!is_empty_blob_sha1(ce
->sha1
))
267 changed
|= DATA_CHANGED
;
273 static int is_racy_timestamp(const struct index_state
*istate
,
274 const struct cache_entry
*ce
)
276 return (!S_ISGITLINK(ce
->ce_mode
) &&
277 istate
->timestamp
.sec
&&
279 /* nanosecond timestamped files can also be racy! */
280 (istate
->timestamp
.sec
< ce
->ce_stat_data
.sd_mtime
.sec
||
281 (istate
->timestamp
.sec
== ce
->ce_stat_data
.sd_mtime
.sec
&&
282 istate
->timestamp
.nsec
<= ce
->ce_stat_data
.sd_mtime
.nsec
))
284 istate
->timestamp
.sec
<= ce
->ce_stat_data
.sd_mtime
.sec
289 int ie_match_stat(const struct index_state
*istate
,
290 const struct cache_entry
*ce
, struct stat
*st
,
291 unsigned int options
)
293 unsigned int changed
;
294 int ignore_valid
= options
& CE_MATCH_IGNORE_VALID
;
295 int ignore_skip_worktree
= options
& CE_MATCH_IGNORE_SKIP_WORKTREE
;
296 int assume_racy_is_modified
= options
& CE_MATCH_RACY_IS_DIRTY
;
299 * If it's marked as always valid in the index, it's
300 * valid whatever the checked-out copy says.
302 * skip-worktree has the same effect with higher precedence
304 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
))
306 if (!ignore_valid
&& (ce
->ce_flags
& CE_VALID
))
310 * Intent-to-add entries have not been added, so the index entry
311 * by definition never matches what is in the work tree until it
312 * actually gets added.
314 if (ce
->ce_flags
& CE_INTENT_TO_ADD
)
315 return DATA_CHANGED
| TYPE_CHANGED
| MODE_CHANGED
;
317 changed
= ce_match_stat_basic(ce
, st
);
320 * Within 1 second of this sequence:
321 * echo xyzzy >file && git-update-index --add file
322 * running this command:
324 * would give a falsely clean cache entry. The mtime and
325 * length match the cache, and other stat fields do not change.
327 * We could detect this at update-index time (the cache entry
328 * being registered/updated records the same time as "now")
329 * and delay the return from git-update-index, but that would
330 * effectively mean we can make at most one commit per second,
331 * which is not acceptable. Instead, we check cache entries
332 * whose mtime are the same as the index file timestamp more
333 * carefully than others.
335 if (!changed
&& is_racy_timestamp(istate
, ce
)) {
336 if (assume_racy_is_modified
)
337 changed
|= DATA_CHANGED
;
339 changed
|= ce_modified_check_fs(ce
, st
);
345 int ie_modified(const struct index_state
*istate
,
346 const struct cache_entry
*ce
,
347 struct stat
*st
, unsigned int options
)
349 int changed
, changed_fs
;
351 changed
= ie_match_stat(istate
, ce
, st
, options
);
355 * If the mode or type has changed, there's no point in trying
356 * to refresh the entry - it's not going to match
358 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
362 * Immediately after read-tree or update-index --cacheinfo,
363 * the length field is zero, as we have never even read the
364 * lstat(2) information once, and we cannot trust DATA_CHANGED
365 * returned by ie_match_stat() which in turn was returned by
366 * ce_match_stat_basic() to signal that the filesize of the
367 * blob changed. We have to actually go to the filesystem to
368 * see if the contents match, and if so, should answer "unchanged".
370 * The logic does not apply to gitlinks, as ce_match_stat_basic()
371 * already has checked the actual HEAD from the filesystem in the
372 * subproject. If ie_match_stat() already said it is different,
373 * then we know it is.
375 if ((changed
& DATA_CHANGED
) &&
376 (S_ISGITLINK(ce
->ce_mode
) || ce
->ce_stat_data
.sd_size
!= 0))
379 changed_fs
= ce_modified_check_fs(ce
, st
);
381 return changed
| changed_fs
;
385 int base_name_compare(const char *name1
, int len1
, int mode1
,
386 const char *name2
, int len2
, int mode2
)
388 unsigned char c1
, c2
;
389 int len
= len1
< len2
? len1
: len2
;
392 cmp
= memcmp(name1
, name2
, len
);
397 if (!c1
&& S_ISDIR(mode1
))
399 if (!c2
&& S_ISDIR(mode2
))
401 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
405 * df_name_compare() is identical to base_name_compare(), except it
406 * compares conflicting directory/file entries as equal. Note that
407 * while a directory name compares as equal to a regular file, they
408 * then individually compare _differently_ to a filename that has
409 * a dot after the basename (because '\0' < '.' < '/').
411 * This is used by routines that want to traverse the git namespace
412 * but then handle conflicting entries together when possible.
414 int df_name_compare(const char *name1
, int len1
, int mode1
,
415 const char *name2
, int len2
, int mode2
)
417 int len
= len1
< len2
? len1
: len2
, cmp
;
418 unsigned char c1
, c2
;
420 cmp
= memcmp(name1
, name2
, len
);
423 /* Directories and files compare equal (same length, same name) */
427 if (!c1
&& S_ISDIR(mode1
))
430 if (!c2
&& S_ISDIR(mode2
))
432 if (c1
== '/' && !c2
)
434 if (c2
== '/' && !c1
)
439 int name_compare(const char *name1
, size_t len1
, const char *name2
, size_t len2
)
441 size_t min_len
= (len1
< len2
) ? len1
: len2
;
442 int cmp
= memcmp(name1
, name2
, min_len
);
452 int cache_name_stage_compare(const char *name1
, int len1
, int stage1
, const char *name2
, int len2
, int stage2
)
456 cmp
= name_compare(name1
, len1
, name2
, len2
);
467 static int index_name_stage_pos(const struct index_state
*istate
, const char *name
, int namelen
, int stage
)
472 last
= istate
->cache_nr
;
473 while (last
> first
) {
474 int next
= (last
+ first
) >> 1;
475 struct cache_entry
*ce
= istate
->cache
[next
];
476 int cmp
= cache_name_stage_compare(name
, namelen
, stage
, ce
->name
, ce_namelen(ce
), ce_stage(ce
));
488 int index_name_pos(const struct index_state
*istate
, const char *name
, int namelen
)
490 return index_name_stage_pos(istate
, name
, namelen
, 0);
493 /* Remove entry, return true if there are more entries to go.. */
494 int remove_index_entry_at(struct index_state
*istate
, int pos
)
496 struct cache_entry
*ce
= istate
->cache
[pos
];
498 record_resolve_undo(istate
, ce
);
499 remove_name_hash(istate
, ce
);
500 save_or_free_index_entry(istate
, ce
);
501 istate
->cache_changed
|= CE_ENTRY_REMOVED
;
503 if (pos
>= istate
->cache_nr
)
505 memmove(istate
->cache
+ pos
,
506 istate
->cache
+ pos
+ 1,
507 (istate
->cache_nr
- pos
) * sizeof(struct cache_entry
*));
512 * Remove all cache entries marked for removal, that is where
513 * CE_REMOVE is set in ce_flags. This is much more effective than
514 * calling remove_index_entry_at() for each entry to be removed.
516 void remove_marked_cache_entries(struct index_state
*istate
)
518 struct cache_entry
**ce_array
= istate
->cache
;
521 for (i
= j
= 0; i
< istate
->cache_nr
; i
++) {
522 if (ce_array
[i
]->ce_flags
& CE_REMOVE
) {
523 remove_name_hash(istate
, ce_array
[i
]);
524 save_or_free_index_entry(istate
, ce_array
[i
]);
527 ce_array
[j
++] = ce_array
[i
];
529 if (j
== istate
->cache_nr
)
531 istate
->cache_changed
|= CE_ENTRY_REMOVED
;
532 istate
->cache_nr
= j
;
535 int remove_file_from_index(struct index_state
*istate
, const char *path
)
537 int pos
= index_name_pos(istate
, path
, strlen(path
));
540 cache_tree_invalidate_path(istate
, path
);
541 while (pos
< istate
->cache_nr
&& !strcmp(istate
->cache
[pos
]->name
, path
))
542 remove_index_entry_at(istate
, pos
);
546 static int compare_name(struct cache_entry
*ce
, const char *path
, int namelen
)
548 return namelen
!= ce_namelen(ce
) || memcmp(path
, ce
->name
, namelen
);
551 static int index_name_pos_also_unmerged(struct index_state
*istate
,
552 const char *path
, int namelen
)
554 int pos
= index_name_pos(istate
, path
, namelen
);
555 struct cache_entry
*ce
;
560 /* maybe unmerged? */
562 if (pos
>= istate
->cache_nr
||
563 compare_name((ce
= istate
->cache
[pos
]), path
, namelen
))
566 /* order of preference: stage 2, 1, 3 */
567 if (ce_stage(ce
) == 1 && pos
+ 1 < istate
->cache_nr
&&
568 ce_stage((ce
= istate
->cache
[pos
+ 1])) == 2 &&
569 !compare_name(ce
, path
, namelen
))
574 static int different_name(struct cache_entry
*ce
, struct cache_entry
*alias
)
576 int len
= ce_namelen(ce
);
577 return ce_namelen(alias
) != len
|| memcmp(ce
->name
, alias
->name
, len
);
581 * If we add a filename that aliases in the cache, we will use the
582 * name that we already have - but we don't want to update the same
583 * alias twice, because that implies that there were actually two
584 * different files with aliasing names!
586 * So we use the CE_ADDED flag to verify that the alias was an old
587 * one before we accept it as
589 static struct cache_entry
*create_alias_ce(struct index_state
*istate
,
590 struct cache_entry
*ce
,
591 struct cache_entry
*alias
)
594 struct cache_entry
*new;
596 if (alias
->ce_flags
& CE_ADDED
)
597 die("Will not add file alias '%s' ('%s' already exists in index)", ce
->name
, alias
->name
);
599 /* Ok, create the new entry using the name of the existing alias */
600 len
= ce_namelen(alias
);
601 new = xcalloc(1, cache_entry_size(len
));
602 memcpy(new->name
, alias
->name
, len
);
603 copy_cache_entry(new, ce
);
604 save_or_free_index_entry(istate
, ce
);
608 void set_object_name_for_intent_to_add_entry(struct cache_entry
*ce
)
610 unsigned char sha1
[20];
611 if (write_sha1_file("", 0, blob_type
, sha1
))
612 die("cannot create an empty blob in the object database");
613 hashcpy(ce
->sha1
, sha1
);
616 int add_to_index(struct index_state
*istate
, const char *path
, struct stat
*st
, int flags
)
618 int size
, namelen
, was_same
;
619 mode_t st_mode
= st
->st_mode
;
620 struct cache_entry
*ce
, *alias
;
621 unsigned ce_option
= CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
|CE_MATCH_RACY_IS_DIRTY
;
622 int verbose
= flags
& (ADD_CACHE_VERBOSE
| ADD_CACHE_PRETEND
);
623 int pretend
= flags
& ADD_CACHE_PRETEND
;
624 int intent_only
= flags
& ADD_CACHE_INTENT
;
625 int add_option
= (ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
|
626 (intent_only
? ADD_CACHE_NEW_ONLY
: 0));
628 if (!S_ISREG(st_mode
) && !S_ISLNK(st_mode
) && !S_ISDIR(st_mode
))
629 return error("%s: can only add regular files, symbolic links or git-directories", path
);
631 namelen
= strlen(path
);
632 if (S_ISDIR(st_mode
)) {
633 while (namelen
&& path
[namelen
-1] == '/')
636 size
= cache_entry_size(namelen
);
637 ce
= xcalloc(1, size
);
638 memcpy(ce
->name
, path
, namelen
);
639 ce
->ce_namelen
= namelen
;
641 fill_stat_cache_info(ce
, st
);
643 ce
->ce_flags
|= CE_INTENT_TO_ADD
;
645 if (trust_executable_bit
&& has_symlinks
)
646 ce
->ce_mode
= create_ce_mode(st_mode
);
648 /* If there is an existing entry, pick the mode bits and type
649 * from it, otherwise assume unexecutable regular file.
651 struct cache_entry
*ent
;
652 int pos
= index_name_pos_also_unmerged(istate
, path
, namelen
);
654 ent
= (0 <= pos
) ? istate
->cache
[pos
] : NULL
;
655 ce
->ce_mode
= ce_mode_from_stat(ent
, st_mode
);
658 /* When core.ignorecase=true, determine if a directory of the same name but differing
659 * case already exists within the Git repository. If it does, ensure the directory
660 * case of the file being added to the repository matches (is folded into) the existing
661 * entry's directory case.
664 const char *startPtr
= ce
->name
;
665 const char *ptr
= startPtr
;
667 while (*ptr
&& *ptr
!= '/')
670 struct cache_entry
*foundce
;
672 foundce
= index_dir_exists(istate
, ce
->name
, ptr
- ce
->name
- 1);
674 memcpy((void *)startPtr
, foundce
->name
+ (startPtr
- ce
->name
), ptr
- startPtr
);
681 alias
= index_file_exists(istate
, ce
->name
, ce_namelen(ce
), ignore_case
);
682 if (alias
&& !ce_stage(alias
) && !ie_match_stat(istate
, alias
, st
, ce_option
)) {
683 /* Nothing changed, really */
684 if (!S_ISGITLINK(alias
->ce_mode
))
685 ce_mark_uptodate(alias
);
686 alias
->ce_flags
|= CE_ADDED
;
692 if (index_path(ce
->sha1
, path
, st
, HASH_WRITE_OBJECT
)) {
694 return error("unable to index file %s", path
);
697 set_object_name_for_intent_to_add_entry(ce
);
699 if (ignore_case
&& alias
&& different_name(ce
, alias
))
700 ce
= create_alias_ce(istate
, ce
, alias
);
701 ce
->ce_flags
|= CE_ADDED
;
703 /* It was suspected to be racily clean, but it turns out to be Ok */
706 !hashcmp(alias
->sha1
, ce
->sha1
) &&
707 ce
->ce_mode
== alias
->ce_mode
);
711 else if (add_index_entry(istate
, ce
, add_option
))
712 return error("unable to add %s to index",path
);
713 if (verbose
&& !was_same
)
714 printf("add '%s'\n", path
);
718 int add_file_to_index(struct index_state
*istate
, const char *path
, int flags
)
721 if (lstat(path
, &st
))
722 die_errno("unable to stat '%s'", path
);
723 return add_to_index(istate
, path
, &st
, flags
);
726 struct cache_entry
*make_cache_entry(unsigned int mode
,
727 const unsigned char *sha1
, const char *path
, int stage
,
728 unsigned int refresh_options
)
731 struct cache_entry
*ce
, *ret
;
733 if (!verify_path(path
)) {
734 error("Invalid path '%s'", path
);
739 size
= cache_entry_size(len
);
740 ce
= xcalloc(1, size
);
742 hashcpy(ce
->sha1
, sha1
);
743 memcpy(ce
->name
, path
, len
);
744 ce
->ce_flags
= create_ce_flags(stage
);
745 ce
->ce_namelen
= len
;
746 ce
->ce_mode
= create_ce_mode(mode
);
748 ret
= refresh_cache_entry(ce
, refresh_options
);
757 int ce_same_name(const struct cache_entry
*a
, const struct cache_entry
*b
)
759 int len
= ce_namelen(a
);
760 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
764 * We fundamentally don't like some paths: we don't want
765 * dot or dot-dot anywhere, and for obvious reasons don't
766 * want to recurse into ".git" either.
768 * Also, we don't want double slashes or slashes at the
769 * end that can make pathnames ambiguous.
771 static int verify_dotfile(const char *rest
)
774 * The first character was '.', but that
775 * has already been discarded, we now test
779 /* "." is not allowed */
780 if (*rest
== '\0' || is_dir_sep(*rest
))
785 * ".git" followed by NUL or slash is bad. This
786 * shares the path end test with the ".." case.
790 if (rest
[1] != 'i' && rest
[1] != 'I')
792 if (rest
[2] != 't' && rest
[2] != 'T')
797 if (rest
[1] == '\0' || is_dir_sep(rest
[1]))
803 int verify_path(const char *path
)
807 if (has_dos_drive_prefix(path
))
816 if (protect_hfs
&& is_hfs_dotgit(path
))
818 if (protect_ntfs
&& is_ntfs_dotgit(path
))
821 if ((c
== '.' && !verify_dotfile(path
)) ||
822 is_dir_sep(c
) || c
== '\0')
830 * Do we have another file that has the beginning components being a
831 * proper superset of the name we're trying to add?
833 static int has_file_name(struct index_state
*istate
,
834 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
837 int len
= ce_namelen(ce
);
838 int stage
= ce_stage(ce
);
839 const char *name
= ce
->name
;
841 while (pos
< istate
->cache_nr
) {
842 struct cache_entry
*p
= istate
->cache
[pos
++];
844 if (len
>= ce_namelen(p
))
846 if (memcmp(name
, p
->name
, len
))
848 if (ce_stage(p
) != stage
)
850 if (p
->name
[len
] != '/')
852 if (p
->ce_flags
& CE_REMOVE
)
857 remove_index_entry_at(istate
, --pos
);
863 * Do we have another file with a pathname that is a proper
864 * subset of the name we're trying to add?
866 static int has_dir_name(struct index_state
*istate
,
867 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
870 int stage
= ce_stage(ce
);
871 const char *name
= ce
->name
;
872 const char *slash
= name
+ ce_namelen(ce
);
880 if (slash
<= ce
->name
)
885 pos
= index_name_stage_pos(istate
, name
, len
, stage
);
888 * Found one, but not so fast. This could
889 * be a marker that says "I was here, but
890 * I am being removed". Such an entry is
891 * not a part of the resulting tree, and
892 * it is Ok to have a directory at the same
895 if (!(istate
->cache
[pos
]->ce_flags
& CE_REMOVE
)) {
899 remove_index_entry_at(istate
, pos
);
907 * Trivial optimization: if we find an entry that
908 * already matches the sub-directory, then we know
909 * we're ok, and we can exit.
911 while (pos
< istate
->cache_nr
) {
912 struct cache_entry
*p
= istate
->cache
[pos
];
913 if ((ce_namelen(p
) <= len
) ||
914 (p
->name
[len
] != '/') ||
915 memcmp(p
->name
, name
, len
))
916 break; /* not our subdirectory */
917 if (ce_stage(p
) == stage
&& !(p
->ce_flags
& CE_REMOVE
))
919 * p is at the same stage as our entry, and
920 * is a subdirectory of what we are looking
921 * at, so we cannot have conflicts at our
922 * level or anything shorter.
931 /* We may be in a situation where we already have path/file and path
932 * is being added, or we already have path and path/file is being
933 * added. Either one would result in a nonsense tree that has path
934 * twice when git-write-tree tries to write it out. Prevent it.
936 * If ok-to-replace is specified, we remove the conflicting entries
937 * from the cache so the caller should recompute the insert position.
938 * When this happens, we return non-zero.
940 static int check_file_directory_conflict(struct index_state
*istate
,
941 const struct cache_entry
*ce
,
942 int pos
, int ok_to_replace
)
947 * When ce is an "I am going away" entry, we allow it to be added
949 if (ce
->ce_flags
& CE_REMOVE
)
953 * We check if the path is a sub-path of a subsequent pathname
954 * first, since removing those will not change the position
957 retval
= has_file_name(istate
, ce
, pos
, ok_to_replace
);
960 * Then check if the path might have a clashing sub-directory
963 return retval
+ has_dir_name(istate
, ce
, pos
, ok_to_replace
);
966 static int add_index_entry_with_check(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
969 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
970 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
971 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
972 int new_only
= option
& ADD_CACHE_NEW_ONLY
;
974 if (!(option
& ADD_CACHE_KEEP_CACHE_TREE
))
975 cache_tree_invalidate_path(istate
, ce
->name
);
976 pos
= index_name_stage_pos(istate
, ce
->name
, ce_namelen(ce
), ce_stage(ce
));
978 /* existing match? Just replace it. */
981 replace_index_entry(istate
, pos
, ce
);
987 * Inserting a merged entry ("stage 0") into the index
988 * will always replace all non-merged entries..
990 if (pos
< istate
->cache_nr
&& ce_stage(ce
) == 0) {
991 while (ce_same_name(istate
->cache
[pos
], ce
)) {
993 if (!remove_index_entry_at(istate
, pos
))
1000 if (!verify_path(ce
->name
))
1001 return error("Invalid path '%s'", ce
->name
);
1003 if (!skip_df_check
&&
1004 check_file_directory_conflict(istate
, ce
, pos
, ok_to_replace
)) {
1006 return error("'%s' appears as both a file and as a directory",
1008 pos
= index_name_stage_pos(istate
, ce
->name
, ce_namelen(ce
), ce_stage(ce
));
1014 int add_index_entry(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
1018 if (option
& ADD_CACHE_JUST_APPEND
)
1019 pos
= istate
->cache_nr
;
1022 ret
= add_index_entry_with_check(istate
, ce
, option
);
1028 /* Make sure the array is big enough .. */
1029 ALLOC_GROW(istate
->cache
, istate
->cache_nr
+ 1, istate
->cache_alloc
);
1033 if (istate
->cache_nr
> pos
+ 1)
1034 memmove(istate
->cache
+ pos
+ 1,
1035 istate
->cache
+ pos
,
1036 (istate
->cache_nr
- pos
- 1) * sizeof(ce
));
1037 set_index_entry(istate
, pos
, ce
);
1038 istate
->cache_changed
|= CE_ENTRY_ADDED
;
1043 * "refresh" does not calculate a new sha1 file or bring the
1044 * cache up-to-date for mode/content changes. But what it
1045 * _does_ do is to "re-match" the stat information of a file
1046 * with the cache, so that you can refresh the cache for a
1047 * file that hasn't been changed but where the stat entry is
1050 * For example, you'd want to do this after doing a "git-read-tree",
1051 * to link up the stat cache details with the proper files.
1053 static struct cache_entry
*refresh_cache_ent(struct index_state
*istate
,
1054 struct cache_entry
*ce
,
1055 unsigned int options
, int *err
,
1059 struct cache_entry
*updated
;
1061 int refresh
= options
& CE_MATCH_REFRESH
;
1062 int ignore_valid
= options
& CE_MATCH_IGNORE_VALID
;
1063 int ignore_skip_worktree
= options
& CE_MATCH_IGNORE_SKIP_WORKTREE
;
1064 int ignore_missing
= options
& CE_MATCH_IGNORE_MISSING
;
1066 if (!refresh
|| ce_uptodate(ce
))
1070 * CE_VALID or CE_SKIP_WORKTREE means the user promised us
1071 * that the change to the work tree does not matter and told
1074 if (!ignore_skip_worktree
&& ce_skip_worktree(ce
)) {
1075 ce_mark_uptodate(ce
);
1078 if (!ignore_valid
&& (ce
->ce_flags
& CE_VALID
)) {
1079 ce_mark_uptodate(ce
);
1083 if (has_symlink_leading_path(ce
->name
, ce_namelen(ce
))) {
1091 if (lstat(ce
->name
, &st
) < 0) {
1092 if (ignore_missing
&& errno
== ENOENT
)
1099 changed
= ie_match_stat(istate
, ce
, &st
, options
);
1101 *changed_ret
= changed
;
1104 * The path is unchanged. If we were told to ignore
1105 * valid bit, then we did the actual stat check and
1106 * found that the entry is unmodified. If the entry
1107 * is not marked VALID, this is the place to mark it
1108 * valid again, under "assume unchanged" mode.
1110 if (ignore_valid
&& assume_unchanged
&&
1111 !(ce
->ce_flags
& CE_VALID
))
1112 ; /* mark this one VALID again */
1115 * We do not mark the index itself "modified"
1116 * because CE_UPTODATE flag is in-core only;
1117 * we are not going to write this change out.
1119 if (!S_ISGITLINK(ce
->ce_mode
))
1120 ce_mark_uptodate(ce
);
1125 if (ie_modified(istate
, ce
, &st
, options
)) {
1132 updated
= xmalloc(size
);
1133 memcpy(updated
, ce
, size
);
1134 fill_stat_cache_info(updated
, &st
);
1136 * If ignore_valid is not set, we should leave CE_VALID bit
1137 * alone. Otherwise, paths marked with --no-assume-unchanged
1138 * (i.e. things to be edited) will reacquire CE_VALID bit
1139 * automatically, which is not really what we want.
1141 if (!ignore_valid
&& assume_unchanged
&&
1142 !(ce
->ce_flags
& CE_VALID
))
1143 updated
->ce_flags
&= ~CE_VALID
;
1145 /* istate->cache_changed is updated in the caller */
1149 static void show_file(const char * fmt
, const char * name
, int in_porcelain
,
1150 int * first
, const char *header_msg
)
1152 if (in_porcelain
&& *first
&& header_msg
) {
1153 printf("%s\n", header_msg
);
1159 int refresh_index(struct index_state
*istate
, unsigned int flags
,
1160 const struct pathspec
*pathspec
,
1161 char *seen
, const char *header_msg
)
1165 int really
= (flags
& REFRESH_REALLY
) != 0;
1166 int allow_unmerged
= (flags
& REFRESH_UNMERGED
) != 0;
1167 int quiet
= (flags
& REFRESH_QUIET
) != 0;
1168 int not_new
= (flags
& REFRESH_IGNORE_MISSING
) != 0;
1169 int ignore_submodules
= (flags
& REFRESH_IGNORE_SUBMODULES
) != 0;
1171 int in_porcelain
= (flags
& REFRESH_IN_PORCELAIN
);
1172 unsigned int options
= (CE_MATCH_REFRESH
|
1173 (really
? CE_MATCH_IGNORE_VALID
: 0) |
1174 (not_new
? CE_MATCH_IGNORE_MISSING
: 0));
1175 const char *modified_fmt
;
1176 const char *deleted_fmt
;
1177 const char *typechange_fmt
;
1178 const char *added_fmt
;
1179 const char *unmerged_fmt
;
1181 modified_fmt
= (in_porcelain
? "M\t%s\n" : "%s: needs update\n");
1182 deleted_fmt
= (in_porcelain
? "D\t%s\n" : "%s: needs update\n");
1183 typechange_fmt
= (in_porcelain
? "T\t%s\n" : "%s needs update\n");
1184 added_fmt
= (in_porcelain
? "A\t%s\n" : "%s needs update\n");
1185 unmerged_fmt
= (in_porcelain
? "U\t%s\n" : "%s: needs merge\n");
1186 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1187 struct cache_entry
*ce
, *new;
1188 int cache_errno
= 0;
1192 ce
= istate
->cache
[i
];
1193 if (ignore_submodules
&& S_ISGITLINK(ce
->ce_mode
))
1196 if (pathspec
&& !ce_path_match(ce
, pathspec
, seen
))
1200 while ((i
< istate
->cache_nr
) &&
1201 ! strcmp(istate
->cache
[i
]->name
, ce
->name
))
1207 show_file(unmerged_fmt
, ce
->name
, in_porcelain
,
1208 &first
, header_msg
);
1216 new = refresh_cache_ent(istate
, ce
, options
, &cache_errno
, &changed
);
1222 if (really
&& cache_errno
== EINVAL
) {
1223 /* If we are doing --really-refresh that
1224 * means the index is not valid anymore.
1226 ce
->ce_flags
&= ~CE_VALID
;
1227 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
1228 istate
->cache_changed
|= CE_ENTRY_CHANGED
;
1233 if (cache_errno
== ENOENT
)
1235 else if (ce
->ce_flags
& CE_INTENT_TO_ADD
)
1236 fmt
= added_fmt
; /* must be before other checks */
1237 else if (changed
& TYPE_CHANGED
)
1238 fmt
= typechange_fmt
;
1242 ce
->name
, in_porcelain
, &first
, header_msg
);
1247 replace_index_entry(istate
, i
, new);
1252 static struct cache_entry
*refresh_cache_entry(struct cache_entry
*ce
,
1253 unsigned int options
)
1255 return refresh_cache_ent(&the_index
, ce
, options
, NULL
, NULL
);
1259 /*****************************************************************
1261 *****************************************************************/
1263 #define INDEX_FORMAT_DEFAULT 3
1265 static unsigned int get_index_format_default(void)
1267 char *envversion
= getenv("GIT_INDEX_VERSION");
1270 unsigned int version
= INDEX_FORMAT_DEFAULT
;
1273 if (!git_config_get_int("index.version", &value
))
1275 if (version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< version
) {
1276 warning(_("index.version set, but the value is invalid.\n"
1277 "Using version %i"), INDEX_FORMAT_DEFAULT
);
1278 return INDEX_FORMAT_DEFAULT
;
1283 version
= strtoul(envversion
, &endp
, 10);
1285 version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< version
) {
1286 warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1287 "Using version %i"), INDEX_FORMAT_DEFAULT
);
1288 version
= INDEX_FORMAT_DEFAULT
;
1294 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
1295 * Again - this is just a (very strong in practice) heuristic that
1296 * the inode hasn't changed.
1298 * We save the fields in big-endian order to allow using the
1299 * index file over NFS transparently.
1301 struct ondisk_cache_entry
{
1302 struct cache_time ctime
;
1303 struct cache_time mtime
;
1310 unsigned char sha1
[20];
1312 char name
[FLEX_ARRAY
]; /* more */
1316 * This struct is used when CE_EXTENDED bit is 1
1317 * The struct must match ondisk_cache_entry exactly from
1320 struct ondisk_cache_entry_extended
{
1321 struct cache_time ctime
;
1322 struct cache_time mtime
;
1329 unsigned char sha1
[20];
1332 char name
[FLEX_ARRAY
]; /* more */
1335 /* These are only used for v3 or lower */
1336 #define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)
1337 #define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)
1338 #define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)
1339 #define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \
1340 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \
1341 ondisk_cache_entry_size(ce_namelen(ce)))
1343 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
1346 unsigned char sha1
[20];
1349 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
1350 return error("bad signature");
1351 hdr_version
= ntohl(hdr
->hdr_version
);
1352 if (hdr_version
< INDEX_FORMAT_LB
|| INDEX_FORMAT_UB
< hdr_version
)
1353 return error("bad index version %d", hdr_version
);
1355 git_SHA1_Update(&c
, hdr
, size
- 20);
1356 git_SHA1_Final(sha1
, &c
);
1357 if (hashcmp(sha1
, (unsigned char *)hdr
+ size
- 20))
1358 return error("bad index file sha1 signature");
1362 static int read_index_extension(struct index_state
*istate
,
1363 const char *ext
, void *data
, unsigned long sz
)
1365 switch (CACHE_EXT(ext
)) {
1366 case CACHE_EXT_TREE
:
1367 istate
->cache_tree
= cache_tree_read(data
, sz
);
1369 case CACHE_EXT_RESOLVE_UNDO
:
1370 istate
->resolve_undo
= resolve_undo_read(data
, sz
);
1372 case CACHE_EXT_LINK
:
1373 if (read_link_extension(istate
, data
, sz
))
1377 if (*ext
< 'A' || 'Z' < *ext
)
1378 return error("index uses %.4s extension, which we do not understand",
1380 fprintf(stderr
, "ignoring %.4s extension\n", ext
);
1386 int hold_locked_index(struct lock_file
*lk
, int die_on_error
)
1388 return hold_lock_file_for_update(lk
, get_index_file(),
1394 int read_index(struct index_state
*istate
)
1396 return read_index_from(istate
, get_index_file());
1399 static struct cache_entry
*cache_entry_from_ondisk(struct ondisk_cache_entry
*ondisk
,
1404 struct cache_entry
*ce
= xmalloc(cache_entry_size(len
));
1406 ce
->ce_stat_data
.sd_ctime
.sec
= get_be32(&ondisk
->ctime
.sec
);
1407 ce
->ce_stat_data
.sd_mtime
.sec
= get_be32(&ondisk
->mtime
.sec
);
1408 ce
->ce_stat_data
.sd_ctime
.nsec
= get_be32(&ondisk
->ctime
.nsec
);
1409 ce
->ce_stat_data
.sd_mtime
.nsec
= get_be32(&ondisk
->mtime
.nsec
);
1410 ce
->ce_stat_data
.sd_dev
= get_be32(&ondisk
->dev
);
1411 ce
->ce_stat_data
.sd_ino
= get_be32(&ondisk
->ino
);
1412 ce
->ce_mode
= get_be32(&ondisk
->mode
);
1413 ce
->ce_stat_data
.sd_uid
= get_be32(&ondisk
->uid
);
1414 ce
->ce_stat_data
.sd_gid
= get_be32(&ondisk
->gid
);
1415 ce
->ce_stat_data
.sd_size
= get_be32(&ondisk
->size
);
1416 ce
->ce_flags
= flags
& ~CE_NAMEMASK
;
1417 ce
->ce_namelen
= len
;
1419 hashcpy(ce
->sha1
, ondisk
->sha1
);
1420 memcpy(ce
->name
, name
, len
);
1421 ce
->name
[len
] = '\0';
1426 * Adjacent cache entries tend to share the leading paths, so it makes
1427 * sense to only store the differences in later entries. In the v4
1428 * on-disk format of the index, each on-disk cache entry stores the
1429 * number of bytes to be stripped from the end of the previous name,
1430 * and the bytes to append to the result, to come up with its name.
1432 static unsigned long expand_name_field(struct strbuf
*name
, const char *cp_
)
1434 const unsigned char *ep
, *cp
= (const unsigned char *)cp_
;
1435 size_t len
= decode_varint(&cp
);
1437 if (name
->len
< len
)
1438 die("malformed name field in the index");
1439 strbuf_remove(name
, name
->len
- len
, len
);
1440 for (ep
= cp
; *ep
; ep
++)
1441 ; /* find the end */
1442 strbuf_add(name
, cp
, ep
- cp
);
1443 return (const char *)ep
+ 1 - cp_
;
1446 static struct cache_entry
*create_from_disk(struct ondisk_cache_entry
*ondisk
,
1447 unsigned long *ent_size
,
1448 struct strbuf
*previous_name
)
1450 struct cache_entry
*ce
;
1455 /* On-disk flags are just 16 bits */
1456 flags
= get_be16(&ondisk
->flags
);
1457 len
= flags
& CE_NAMEMASK
;
1459 if (flags
& CE_EXTENDED
) {
1460 struct ondisk_cache_entry_extended
*ondisk2
;
1462 ondisk2
= (struct ondisk_cache_entry_extended
*)ondisk
;
1463 extended_flags
= get_be16(&ondisk2
->flags2
) << 16;
1464 /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1465 if (extended_flags
& ~CE_EXTENDED_FLAGS
)
1466 die("Unknown index entry format %08x", extended_flags
);
1467 flags
|= extended_flags
;
1468 name
= ondisk2
->name
;
1471 name
= ondisk
->name
;
1473 if (!previous_name
) {
1474 /* v3 and earlier */
1475 if (len
== CE_NAMEMASK
)
1477 ce
= cache_entry_from_ondisk(ondisk
, flags
, name
, len
);
1479 *ent_size
= ondisk_ce_size(ce
);
1481 unsigned long consumed
;
1482 consumed
= expand_name_field(previous_name
, name
);
1483 ce
= cache_entry_from_ondisk(ondisk
, flags
,
1485 previous_name
->len
);
1487 *ent_size
= (name
- ((char *)ondisk
)) + consumed
;
1492 static void check_ce_order(struct cache_entry
*ce
, struct cache_entry
*next_ce
)
1494 int name_compare
= strcmp(ce
->name
, next_ce
->name
);
1495 if (0 < name_compare
)
1496 die("unordered stage entries in index");
1497 if (!name_compare
) {
1499 die("multiple stage entries for merged file '%s'",
1501 if (ce_stage(ce
) > ce_stage(next_ce
))
1502 die("unordered stage entries for '%s'",
1507 /* remember to discard_cache() before reading a different cache! */
1508 int do_read_index(struct index_state
*istate
, const char *path
, int must_exist
)
1512 unsigned long src_offset
;
1513 struct cache_header
*hdr
;
1516 struct strbuf previous_name_buf
= STRBUF_INIT
, *previous_name
;
1518 if (istate
->initialized
)
1519 return istate
->cache_nr
;
1521 istate
->timestamp
.sec
= 0;
1522 istate
->timestamp
.nsec
= 0;
1523 fd
= open(path
, O_RDONLY
);
1525 if (!must_exist
&& errno
== ENOENT
)
1527 die_errno("%s: index file open failed", path
);
1531 die_errno("cannot stat the open index");
1533 mmap_size
= xsize_t(st
.st_size
);
1534 if (mmap_size
< sizeof(struct cache_header
) + 20)
1535 die("index file smaller than expected");
1537 mmap
= xmmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
1538 if (mmap
== MAP_FAILED
)
1539 die_errno("unable to map index file");
1543 if (verify_hdr(hdr
, mmap_size
) < 0)
1546 hashcpy(istate
->sha1
, (const unsigned char *)hdr
+ mmap_size
- 20);
1547 istate
->version
= ntohl(hdr
->hdr_version
);
1548 istate
->cache_nr
= ntohl(hdr
->hdr_entries
);
1549 istate
->cache_alloc
= alloc_nr(istate
->cache_nr
);
1550 istate
->cache
= xcalloc(istate
->cache_alloc
, sizeof(*istate
->cache
));
1551 istate
->initialized
= 1;
1553 if (istate
->version
== 4)
1554 previous_name
= &previous_name_buf
;
1556 previous_name
= NULL
;
1558 src_offset
= sizeof(*hdr
);
1559 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1560 struct ondisk_cache_entry
*disk_ce
;
1561 struct cache_entry
*ce
;
1562 unsigned long consumed
;
1564 disk_ce
= (struct ondisk_cache_entry
*)((char *)mmap
+ src_offset
);
1565 ce
= create_from_disk(disk_ce
, &consumed
, previous_name
);
1566 set_index_entry(istate
, i
, ce
);
1569 check_ce_order(istate
->cache
[i
- 1], ce
);
1571 src_offset
+= consumed
;
1573 strbuf_release(&previous_name_buf
);
1574 istate
->timestamp
.sec
= st
.st_mtime
;
1575 istate
->timestamp
.nsec
= ST_MTIME_NSEC(st
);
1577 while (src_offset
<= mmap_size
- 20 - 8) {
1578 /* After an array of active_nr index entries,
1579 * there can be arbitrary number of extended
1580 * sections, each of which is prefixed with
1581 * extension name (4-byte) and section length
1582 * in 4-byte network byte order.
1585 memcpy(&extsize
, (char *)mmap
+ src_offset
+ 4, 4);
1586 extsize
= ntohl(extsize
);
1587 if (read_index_extension(istate
,
1588 (const char *) mmap
+ src_offset
,
1589 (char *) mmap
+ src_offset
+ 8,
1593 src_offset
+= extsize
;
1595 munmap(mmap
, mmap_size
);
1596 return istate
->cache_nr
;
1599 munmap(mmap
, mmap_size
);
1600 die("index file corrupt");
1603 int read_index_from(struct index_state
*istate
, const char *path
)
1605 struct split_index
*split_index
;
1608 /* istate->initialized covers both .git/index and .git/sharedindex.xxx */
1609 if (istate
->initialized
)
1610 return istate
->cache_nr
;
1612 ret
= do_read_index(istate
, path
, 0);
1613 split_index
= istate
->split_index
;
1617 if (is_null_sha1(split_index
->base_sha1
))
1620 if (split_index
->base
)
1621 discard_index(split_index
->base
);
1623 split_index
->base
= xcalloc(1, sizeof(*split_index
->base
));
1624 ret
= do_read_index(split_index
->base
,
1625 git_path("sharedindex.%s",
1626 sha1_to_hex(split_index
->base_sha1
)), 1);
1627 if (hashcmp(split_index
->base_sha1
, split_index
->base
->sha1
))
1628 die("broken index, expect %s in %s, got %s",
1629 sha1_to_hex(split_index
->base_sha1
),
1630 git_path("sharedindex.%s",
1631 sha1_to_hex(split_index
->base_sha1
)),
1632 sha1_to_hex(split_index
->base
->sha1
));
1633 merge_base_index(istate
);
1637 int is_index_unborn(struct index_state
*istate
)
1639 return (!istate
->cache_nr
&& !istate
->timestamp
.sec
);
1642 int discard_index(struct index_state
*istate
)
1646 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1647 if (istate
->cache
[i
]->index
&&
1648 istate
->split_index
&&
1649 istate
->split_index
->base
&&
1650 istate
->cache
[i
]->index
<= istate
->split_index
->base
->cache_nr
&&
1651 istate
->cache
[i
] == istate
->split_index
->base
->cache
[istate
->cache
[i
]->index
- 1])
1653 free(istate
->cache
[i
]);
1655 resolve_undo_clear_index(istate
);
1656 istate
->cache_nr
= 0;
1657 istate
->cache_changed
= 0;
1658 istate
->timestamp
.sec
= 0;
1659 istate
->timestamp
.nsec
= 0;
1660 free_name_hash(istate
);
1661 cache_tree_free(&(istate
->cache_tree
));
1662 istate
->initialized
= 0;
1663 free(istate
->cache
);
1664 istate
->cache
= NULL
;
1665 istate
->cache_alloc
= 0;
1666 discard_split_index(istate
);
1670 int unmerged_index(const struct index_state
*istate
)
1673 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1674 if (ce_stage(istate
->cache
[i
]))
1680 #define WRITE_BUFFER_SIZE 8192
1681 static unsigned char write_buffer
[WRITE_BUFFER_SIZE
];
1682 static unsigned long write_buffer_len
;
1684 static int ce_write_flush(git_SHA_CTX
*context
, int fd
)
1686 unsigned int buffered
= write_buffer_len
;
1688 git_SHA1_Update(context
, write_buffer
, buffered
);
1689 if (write_in_full(fd
, write_buffer
, buffered
) != buffered
)
1691 write_buffer_len
= 0;
1696 static int ce_write(git_SHA_CTX
*context
, int fd
, void *data
, unsigned int len
)
1699 unsigned int buffered
= write_buffer_len
;
1700 unsigned int partial
= WRITE_BUFFER_SIZE
- buffered
;
1703 memcpy(write_buffer
+ buffered
, data
, partial
);
1704 buffered
+= partial
;
1705 if (buffered
== WRITE_BUFFER_SIZE
) {
1706 write_buffer_len
= buffered
;
1707 if (ce_write_flush(context
, fd
))
1711 write_buffer_len
= buffered
;
1713 data
= (char *) data
+ partial
;
1718 static int write_index_ext_header(git_SHA_CTX
*context
, int fd
,
1719 unsigned int ext
, unsigned int sz
)
1723 return ((ce_write(context
, fd
, &ext
, 4) < 0) ||
1724 (ce_write(context
, fd
, &sz
, 4) < 0)) ? -1 : 0;
1727 static int ce_flush(git_SHA_CTX
*context
, int fd
, unsigned char *sha1
)
1729 unsigned int left
= write_buffer_len
;
1732 write_buffer_len
= 0;
1733 git_SHA1_Update(context
, write_buffer
, left
);
1736 /* Flush first if not enough space for SHA1 signature */
1737 if (left
+ 20 > WRITE_BUFFER_SIZE
) {
1738 if (write_in_full(fd
, write_buffer
, left
) != left
)
1743 /* Append the SHA1 signature at the end */
1744 git_SHA1_Final(write_buffer
+ left
, context
);
1745 hashcpy(sha1
, write_buffer
+ left
);
1747 return (write_in_full(fd
, write_buffer
, left
) != left
) ? -1 : 0;
1750 static void ce_smudge_racily_clean_entry(struct cache_entry
*ce
)
1753 * The only thing we care about in this function is to smudge the
1754 * falsely clean entry due to touch-update-touch race, so we leave
1755 * everything else as they are. We are called for entries whose
1756 * ce_stat_data.sd_mtime match the index file mtime.
1758 * Note that this actually does not do much for gitlinks, for
1759 * which ce_match_stat_basic() always goes to the actual
1760 * contents. The caller checks with is_racy_timestamp() which
1761 * always says "no" for gitlinks, so we are not called for them ;-)
1765 if (lstat(ce
->name
, &st
) < 0)
1767 if (ce_match_stat_basic(ce
, &st
))
1769 if (ce_modified_check_fs(ce
, &st
)) {
1770 /* This is "racily clean"; smudge it. Note that this
1771 * is a tricky code. At first glance, it may appear
1772 * that it can break with this sequence:
1774 * $ echo xyzzy >frotz
1775 * $ git-update-index --add frotz
1778 * $ echo filfre >nitfol
1779 * $ git-update-index --add nitfol
1781 * but it does not. When the second update-index runs,
1782 * it notices that the entry "frotz" has the same timestamp
1783 * as index, and if we were to smudge it by resetting its
1784 * size to zero here, then the object name recorded
1785 * in index is the 6-byte file but the cached stat information
1786 * becomes zero --- which would then match what we would
1787 * obtain from the filesystem next time we stat("frotz").
1789 * However, the second update-index, before calling
1790 * this function, notices that the cached size is 6
1791 * bytes and what is on the filesystem is an empty
1792 * file, and never calls us, so the cached size information
1793 * for "frotz" stays 6 which does not match the filesystem.
1795 ce
->ce_stat_data
.sd_size
= 0;
1799 /* Copy miscellaneous fields but not the name */
1800 static char *copy_cache_entry_to_ondisk(struct ondisk_cache_entry
*ondisk
,
1801 struct cache_entry
*ce
)
1805 ondisk
->ctime
.sec
= htonl(ce
->ce_stat_data
.sd_ctime
.sec
);
1806 ondisk
->mtime
.sec
= htonl(ce
->ce_stat_data
.sd_mtime
.sec
);
1807 ondisk
->ctime
.nsec
= htonl(ce
->ce_stat_data
.sd_ctime
.nsec
);
1808 ondisk
->mtime
.nsec
= htonl(ce
->ce_stat_data
.sd_mtime
.nsec
);
1809 ondisk
->dev
= htonl(ce
->ce_stat_data
.sd_dev
);
1810 ondisk
->ino
= htonl(ce
->ce_stat_data
.sd_ino
);
1811 ondisk
->mode
= htonl(ce
->ce_mode
);
1812 ondisk
->uid
= htonl(ce
->ce_stat_data
.sd_uid
);
1813 ondisk
->gid
= htonl(ce
->ce_stat_data
.sd_gid
);
1814 ondisk
->size
= htonl(ce
->ce_stat_data
.sd_size
);
1815 hashcpy(ondisk
->sha1
, ce
->sha1
);
1817 flags
= ce
->ce_flags
& ~CE_NAMEMASK
;
1818 flags
|= (ce_namelen(ce
) >= CE_NAMEMASK
? CE_NAMEMASK
: ce_namelen(ce
));
1819 ondisk
->flags
= htons(flags
);
1820 if (ce
->ce_flags
& CE_EXTENDED
) {
1821 struct ondisk_cache_entry_extended
*ondisk2
;
1822 ondisk2
= (struct ondisk_cache_entry_extended
*)ondisk
;
1823 ondisk2
->flags2
= htons((ce
->ce_flags
& CE_EXTENDED_FLAGS
) >> 16);
1824 return ondisk2
->name
;
1827 return ondisk
->name
;
1831 static int ce_write_entry(git_SHA_CTX
*c
, int fd
, struct cache_entry
*ce
,
1832 struct strbuf
*previous_name
)
1835 struct ondisk_cache_entry
*ondisk
;
1836 int saved_namelen
= saved_namelen
; /* compiler workaround */
1840 if (ce
->ce_flags
& CE_STRIP_NAME
) {
1841 saved_namelen
= ce_namelen(ce
);
1845 if (!previous_name
) {
1846 size
= ondisk_ce_size(ce
);
1847 ondisk
= xcalloc(1, size
);
1848 name
= copy_cache_entry_to_ondisk(ondisk
, ce
);
1849 memcpy(name
, ce
->name
, ce_namelen(ce
));
1851 int common
, to_remove
, prefix_size
;
1852 unsigned char to_remove_vi
[16];
1854 (ce
->name
[common
] &&
1855 common
< previous_name
->len
&&
1856 ce
->name
[common
] == previous_name
->buf
[common
]);
1858 ; /* still matching */
1859 to_remove
= previous_name
->len
- common
;
1860 prefix_size
= encode_varint(to_remove
, to_remove_vi
);
1862 if (ce
->ce_flags
& CE_EXTENDED
)
1863 size
= offsetof(struct ondisk_cache_entry_extended
, name
);
1865 size
= offsetof(struct ondisk_cache_entry
, name
);
1866 size
+= prefix_size
+ (ce_namelen(ce
) - common
+ 1);
1868 ondisk
= xcalloc(1, size
);
1869 name
= copy_cache_entry_to_ondisk(ondisk
, ce
);
1870 memcpy(name
, to_remove_vi
, prefix_size
);
1871 memcpy(name
+ prefix_size
, ce
->name
+ common
, ce_namelen(ce
) - common
);
1873 strbuf_splice(previous_name
, common
, to_remove
,
1874 ce
->name
+ common
, ce_namelen(ce
) - common
);
1876 if (ce
->ce_flags
& CE_STRIP_NAME
) {
1877 ce
->ce_namelen
= saved_namelen
;
1878 ce
->ce_flags
&= ~CE_STRIP_NAME
;
1881 result
= ce_write(c
, fd
, ondisk
, size
);
1887 * This function verifies if index_state has the correct sha1 of the
1888 * index file. Don't die if we have any other failure, just return 0.
1890 static int verify_index_from(const struct index_state
*istate
, const char *path
)
1895 unsigned char sha1
[20];
1897 if (!istate
->initialized
)
1900 fd
= open(path
, O_RDONLY
);
1907 if (st
.st_size
< sizeof(struct cache_header
) + 20)
1910 n
= pread_in_full(fd
, sha1
, 20, st
.st_size
- 20);
1914 if (hashcmp(istate
->sha1
, sha1
))
1925 static int verify_index(const struct index_state
*istate
)
1927 return verify_index_from(istate
, get_index_file());
1930 static int has_racy_timestamp(struct index_state
*istate
)
1932 int entries
= istate
->cache_nr
;
1935 for (i
= 0; i
< entries
; i
++) {
1936 struct cache_entry
*ce
= istate
->cache
[i
];
1937 if (is_racy_timestamp(istate
, ce
))
1944 * Opportunistically update the index but do not complain if we can't
1946 void update_index_if_able(struct index_state
*istate
, struct lock_file
*lockfile
)
1948 if ((istate
->cache_changed
|| has_racy_timestamp(istate
)) &&
1949 verify_index(istate
) &&
1950 write_locked_index(istate
, lockfile
, COMMIT_LOCK
))
1951 rollback_lock_file(lockfile
);
1954 static int do_write_index(struct index_state
*istate
, int newfd
,
1955 int strip_extensions
)
1958 struct cache_header hdr
;
1959 int i
, err
, removed
, extended
, hdr_version
;
1960 struct cache_entry
**cache
= istate
->cache
;
1961 int entries
= istate
->cache_nr
;
1963 struct strbuf previous_name_buf
= STRBUF_INIT
, *previous_name
;
1965 for (i
= removed
= extended
= 0; i
< entries
; i
++) {
1966 if (cache
[i
]->ce_flags
& CE_REMOVE
)
1969 /* reduce extended entries if possible */
1970 cache
[i
]->ce_flags
&= ~CE_EXTENDED
;
1971 if (cache
[i
]->ce_flags
& CE_EXTENDED_FLAGS
) {
1973 cache
[i
]->ce_flags
|= CE_EXTENDED
;
1977 if (!istate
->version
) {
1978 istate
->version
= get_index_format_default();
1979 if (getenv("GIT_TEST_SPLIT_INDEX"))
1980 init_split_index(istate
);
1983 /* demote version 3 to version 2 when the latter suffices */
1984 if (istate
->version
== 3 || istate
->version
== 2)
1985 istate
->version
= extended
? 3 : 2;
1987 hdr_version
= istate
->version
;
1989 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
1990 hdr
.hdr_version
= htonl(hdr_version
);
1991 hdr
.hdr_entries
= htonl(entries
- removed
);
1994 if (ce_write(&c
, newfd
, &hdr
, sizeof(hdr
)) < 0)
1997 previous_name
= (hdr_version
== 4) ? &previous_name_buf
: NULL
;
1998 for (i
= 0; i
< entries
; i
++) {
1999 struct cache_entry
*ce
= cache
[i
];
2000 if (ce
->ce_flags
& CE_REMOVE
)
2002 if (!ce_uptodate(ce
) && is_racy_timestamp(istate
, ce
))
2003 ce_smudge_racily_clean_entry(ce
);
2004 if (is_null_sha1(ce
->sha1
)) {
2005 static const char msg
[] = "cache entry has null sha1: %s";
2006 static int allow
= -1;
2009 allow
= git_env_bool("GIT_ALLOW_NULL_SHA1", 0);
2011 warning(msg
, ce
->name
);
2013 return error(msg
, ce
->name
);
2015 if (ce_write_entry(&c
, newfd
, ce
, previous_name
) < 0)
2018 strbuf_release(&previous_name_buf
);
2020 /* Write extension data here */
2021 if (!strip_extensions
&& istate
->split_index
) {
2022 struct strbuf sb
= STRBUF_INIT
;
2024 err
= write_link_extension(&sb
, istate
) < 0 ||
2025 write_index_ext_header(&c
, newfd
, CACHE_EXT_LINK
,
2027 ce_write(&c
, newfd
, sb
.buf
, sb
.len
) < 0;
2028 strbuf_release(&sb
);
2032 if (!strip_extensions
&& istate
->cache_tree
) {
2033 struct strbuf sb
= STRBUF_INIT
;
2035 cache_tree_write(&sb
, istate
->cache_tree
);
2036 err
= write_index_ext_header(&c
, newfd
, CACHE_EXT_TREE
, sb
.len
) < 0
2037 || ce_write(&c
, newfd
, sb
.buf
, sb
.len
) < 0;
2038 strbuf_release(&sb
);
2042 if (!strip_extensions
&& istate
->resolve_undo
) {
2043 struct strbuf sb
= STRBUF_INIT
;
2045 resolve_undo_write(&sb
, istate
->resolve_undo
);
2046 err
= write_index_ext_header(&c
, newfd
, CACHE_EXT_RESOLVE_UNDO
,
2048 || ce_write(&c
, newfd
, sb
.buf
, sb
.len
) < 0;
2049 strbuf_release(&sb
);
2054 if (ce_flush(&c
, newfd
, istate
->sha1
) || fstat(newfd
, &st
))
2056 istate
->timestamp
.sec
= (unsigned int)st
.st_mtime
;
2057 istate
->timestamp
.nsec
= ST_MTIME_NSEC(st
);
2061 void set_alternate_index_output(const char *name
)
2063 alternate_index_output
= name
;
2066 static int commit_locked_index(struct lock_file
*lk
)
2068 if (alternate_index_output
)
2069 return commit_lock_file_to(lk
, alternate_index_output
);
2071 return commit_lock_file(lk
);
2074 static int do_write_locked_index(struct index_state
*istate
, struct lock_file
*lock
,
2077 int ret
= do_write_index(istate
, lock
->fd
, 0);
2080 assert((flags
& (COMMIT_LOCK
| CLOSE_LOCK
)) !=
2081 (COMMIT_LOCK
| CLOSE_LOCK
));
2082 if (flags
& COMMIT_LOCK
)
2083 return commit_locked_index(lock
);
2084 else if (flags
& CLOSE_LOCK
)
2085 return close_lock_file(lock
);
2090 static int write_split_index(struct index_state
*istate
,
2091 struct lock_file
*lock
,
2095 prepare_to_write_split_index(istate
);
2096 ret
= do_write_locked_index(istate
, lock
, flags
);
2097 finish_writing_split_index(istate
);
2101 static char *temporary_sharedindex
;
2103 static void remove_temporary_sharedindex(void)
2105 if (temporary_sharedindex
) {
2106 unlink_or_warn(temporary_sharedindex
);
2107 free(temporary_sharedindex
);
2108 temporary_sharedindex
= NULL
;
2112 static void remove_temporary_sharedindex_on_signal(int signo
)
2114 remove_temporary_sharedindex();
2115 sigchain_pop(signo
);
2119 static int write_shared_index(struct index_state
*istate
,
2120 struct lock_file
*lock
, unsigned flags
)
2122 struct split_index
*si
= istate
->split_index
;
2123 static int installed_handler
;
2126 temporary_sharedindex
= git_pathdup("sharedindex_XXXXXX");
2127 fd
= mkstemp(temporary_sharedindex
);
2129 free(temporary_sharedindex
);
2130 temporary_sharedindex
= NULL
;
2131 hashclr(si
->base_sha1
);
2132 return do_write_locked_index(istate
, lock
, flags
);
2134 if (!installed_handler
) {
2135 atexit(remove_temporary_sharedindex
);
2136 sigchain_push_common(remove_temporary_sharedindex_on_signal
);
2138 move_cache_to_base_index(istate
);
2139 ret
= do_write_index(si
->base
, fd
, 1);
2142 remove_temporary_sharedindex();
2145 ret
= rename(temporary_sharedindex
,
2146 git_path("sharedindex.%s", sha1_to_hex(si
->base
->sha1
)));
2147 free(temporary_sharedindex
);
2148 temporary_sharedindex
= NULL
;
2150 hashcpy(si
->base_sha1
, si
->base
->sha1
);
2154 int write_locked_index(struct index_state
*istate
, struct lock_file
*lock
,
2157 struct split_index
*si
= istate
->split_index
;
2159 if (!si
|| alternate_index_output
||
2160 (istate
->cache_changed
& ~EXTMASK
)) {
2162 hashclr(si
->base_sha1
);
2163 return do_write_locked_index(istate
, lock
, flags
);
2166 if (getenv("GIT_TEST_SPLIT_INDEX")) {
2167 int v
= si
->base_sha1
[0];
2169 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
2171 if (istate
->cache_changed
& SPLIT_INDEX_ORDERED
) {
2172 int ret
= write_shared_index(istate
, lock
, flags
);
2177 return write_split_index(istate
, lock
, flags
);
2181 * Read the index file that is potentially unmerged into given
2182 * index_state, dropping any unmerged entries. Returns true if
2183 * the index is unmerged. Callers who want to refuse to work
2184 * from an unmerged state can call this and check its return value,
2185 * instead of calling read_cache().
2187 int read_index_unmerged(struct index_state
*istate
)
2193 for (i
= 0; i
< istate
->cache_nr
; i
++) {
2194 struct cache_entry
*ce
= istate
->cache
[i
];
2195 struct cache_entry
*new_ce
;
2201 len
= ce_namelen(ce
);
2202 size
= cache_entry_size(len
);
2203 new_ce
= xcalloc(1, size
);
2204 memcpy(new_ce
->name
, ce
->name
, len
);
2205 new_ce
->ce_flags
= create_ce_flags(0) | CE_CONFLICTED
;
2206 new_ce
->ce_namelen
= len
;
2207 new_ce
->ce_mode
= ce
->ce_mode
;
2208 if (add_index_entry(istate
, new_ce
, 0))
2209 return error("%s: cannot drop to stage #0",
2216 * Returns 1 if the path is an "other" path with respect to
2217 * the index; that is, the path is not mentioned in the index at all,
2218 * either as a file, a directory with some files in the index,
2219 * or as an unmerged entry.
2221 * We helpfully remove a trailing "/" from directories so that
2222 * the output of read_directory can be used as-is.
2224 int index_name_is_other(const struct index_state
*istate
, const char *name
,
2228 if (namelen
&& name
[namelen
- 1] == '/')
2230 pos
= index_name_pos(istate
, name
, namelen
);
2232 return 0; /* exact match */
2234 if (pos
< istate
->cache_nr
) {
2235 struct cache_entry
*ce
= istate
->cache
[pos
];
2236 if (ce_namelen(ce
) == namelen
&&
2237 !memcmp(ce
->name
, name
, namelen
))
2238 return 0; /* Yup, this one exists unmerged */
2243 void *read_blob_data_from_index(struct index_state
*istate
, const char *path
, unsigned long *size
)
2247 enum object_type type
;
2251 pos
= index_name_pos(istate
, path
, len
);
2254 * We might be in the middle of a merge, in which
2255 * case we would read stage #2 (ours).
2259 (pos
< 0 && i
< istate
->cache_nr
&&
2260 !strcmp(istate
->cache
[i
]->name
, path
));
2262 if (ce_stage(istate
->cache
[i
]) == 2)
2267 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
2268 if (!data
|| type
!= OBJ_BLOB
) {
2277 void stat_validity_clear(struct stat_validity
*sv
)
2283 int stat_validity_check(struct stat_validity
*sv
, const char *path
)
2287 if (stat(path
, &st
) < 0)
2288 return sv
->sd
== NULL
;
2291 return S_ISREG(st
.st_mode
) && !match_stat_data(sv
->sd
, &st
);
2294 void stat_validity_update(struct stat_validity
*sv
, int fd
)
2298 if (fstat(fd
, &st
) < 0 || !S_ISREG(st
.st_mode
))
2299 stat_validity_clear(sv
);
2302 sv
->sd
= xcalloc(1, sizeof(struct stat_data
));
2303 fill_stat_data(sv
->sd
, &st
);