blob: 1b3c2eb408be33a0ed5351cf226fade78bad2345 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07007#include "config.h"
Elijah Newrencffbfad2018-06-30 18:24:55 -07008#include "diff.h"
9#include "diffcore.h"
Michael Haggertyf6ecc622015-08-10 11:47:45 +020010#include "tempfile.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020011#include "lockfile.h"
Junio C Hamanobad68ec2006-04-24 21:18:58 -070012#include "cache-tree.h"
Linus Torvaldsf35a6d32007-04-09 21:20:29 -070013#include "refs.h"
Alexandre Julliardd6168132007-08-11 23:59:01 +020014#include "dir.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070015#include "object-store.h"
Junio C Hamano041aee32008-07-21 01:24:17 -070016#include "tree.h"
17#include "commit.h"
Junio C Hamano39425812008-08-21 01:44:53 -070018#include "blob.h"
Junio C Hamanocfc57892009-12-25 00:30:51 -080019#include "resolve-undo.h"
Ben Peart1956ecd2019-02-15 12:59:21 -050020#include "run-command.h"
Junio C Hamano6c9cd162012-04-03 15:53:15 -070021#include "strbuf.h"
22#include "varint.h"
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070023#include "split-index.h"
Jeff Kinga42643a2014-12-15 18:15:20 -050024#include "utf8.h"
Ben Peart883e2482017-09-22 12:35:40 -040025#include "fsmonitor.h"
Ben Peartabb4bb82018-10-10 11:59:36 -040026#include "thread-utils.h"
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +020027#include "progress.h"
Derrick Stolee6e773522021-03-30 13:10:55 +000028#include "sparse-index.h"
Junio C Hamanobad68ec2006-04-24 21:18:58 -070029
Thomas Gummererb60e1882012-07-11 11:22:37 +020030/* Mask for the name length in ce_flags in the on-disk index */
31
32#define CE_NAMEMASK (0x0fff)
33
Junio C Hamanobad68ec2006-04-24 21:18:58 -070034/* Index extensions.
35 *
36 * The first letter should be 'A'..'Z' for extensions that are not
37 * necessary for a correct operation (i.e. optimization data).
38 * When new extensions are added that _needs_ to be understood in
39 * order to correctly interpret the index file, pick character that
40 * is outside the range, to cause the reader to abort.
41 */
42
43#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
44#define CACHE_EXT_TREE 0x54524545 /* "TREE" */
Shawn O. Pearceb659b492010-02-02 07:33:28 -080045#define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070046#define CACHE_EXT_LINK 0x6c696e6b /* "link" */
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +070047#define CACHE_EXT_UNTRACKED 0x554E5452 /* "UNTR" */
Ben Peart883e2482017-09-22 12:35:40 -040048#define CACHE_EXT_FSMONITOR 0x46534D4E /* "FSMN" */
Ben Peart3b1d9e02018-10-10 11:59:34 -040049#define CACHE_EXT_ENDOFINDEXENTRIES 0x454F4945 /* "EOIE" */
Ben Peart32550892018-10-10 11:59:37 -040050#define CACHE_EXT_INDEXENTRYOFFSETTABLE 0x49454F54 /* "IEOT" */
Derrick Stoleecd424152021-03-30 13:10:54 +000051#define CACHE_EXT_SPARSE_DIRECTORIES 0x73646972 /* "sdir" */
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070052
53/* changes that can be kept in $GIT_DIR/index (basically all extensions) */
Nguyễn Thái Ngọc Duye0cf0d72014-06-13 19:19:37 +070054#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +070055 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \
Ben Peart883e2482017-09-22 12:35:40 -040056 SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED | FSMONITOR_CHANGED)
Linus Torvaldse83c5162005-04-07 15:13:13 -070057
Jameson Miller8e72d672018-07-02 19:49:37 +000058
59/*
60 * This is an estimate of the pathname length in the index. We use
61 * this for V4 index files to guess the un-deltafied size of the index
62 * in memory because of pathname deltafication. This is not required
63 * for V2/V3 index formats because their pathnames are not compressed.
64 * If the initial amount of memory set aside is not sufficient, the
65 * mem pool will allocate extra memory.
66 */
67#define CACHE_ENTRY_PATH_LENGTH 80
68
69static inline struct cache_entry *mem_pool__ce_alloc(struct mem_pool *mem_pool, size_t len)
70{
71 struct cache_entry *ce;
72 ce = mem_pool_alloc(mem_pool, cache_entry_size(len));
73 ce->mem_pool_allocated = 1;
74 return ce;
75}
76
77static inline struct cache_entry *mem_pool__ce_calloc(struct mem_pool *mem_pool, size_t len)
78{
79 struct cache_entry * ce;
80 ce = mem_pool_calloc(mem_pool, 1, cache_entry_size(len));
81 ce->mem_pool_allocated = 1;
82 return ce;
83}
84
85static struct mem_pool *find_mem_pool(struct index_state *istate)
86{
87 struct mem_pool **pool_ptr;
88
89 if (istate->split_index && istate->split_index->base)
90 pool_ptr = &istate->split_index->base->ce_mem_pool;
91 else
92 pool_ptr = &istate->ce_mem_pool;
93
Elijah Newren44c7e1a2020-08-15 17:37:56 +000094 if (!*pool_ptr) {
95 *pool_ptr = xmalloc(sizeof(**pool_ptr));
96 mem_pool_init(*pool_ptr, 0);
97 }
Jameson Miller8e72d672018-07-02 19:49:37 +000098
99 return *pool_ptr;
100}
101
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +0700102static const char *alternate_index_output;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -0700103
Junio C Hamano9cb76b82008-01-22 23:01:13 -0800104static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
105{
Derrick Stolee4300f842021-03-30 13:10:48 +0000106 if (S_ISSPARSEDIR(ce->ce_mode))
107 istate->sparse_index = 1;
108
Junio C Hamano9cb76b82008-01-22 23:01:13 -0800109 istate->cache[nr] = ce;
Linus Torvalds96872bc2008-03-21 13:16:24 -0700110 add_name_hash(istate, ce);
Junio C Hamano9cb76b82008-01-22 23:01:13 -0800111}
112
Linus Torvaldscf558702008-01-22 18:41:14 -0800113static void replace_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
114{
115 struct cache_entry *old = istate->cache[nr];
116
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700117 replace_index_entry_in_base(istate, old, ce);
Karsten Blees20926782013-02-28 00:57:48 +0100118 remove_name_hash(istate, old);
Jameson Millera8497352018-07-02 19:49:31 +0000119 discard_cache_entry(old);
Ben Peart0e267b72018-03-15 11:25:20 -0400120 ce->ce_flags &= ~CE_HASHED;
Linus Torvaldsa22c6372008-02-22 20:37:40 -0800121 set_index_entry(istate, nr, ce);
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700122 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -0400123 mark_fsmonitor_invalid(istate, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700124 istate->cache_changed |= CE_ENTRY_CHANGED;
Linus Torvaldscf558702008-01-22 18:41:14 -0800125}
126
Petr Baudis81dc2302008-07-21 02:25:56 +0200127void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name)
128{
Brandon Williams285c2e22018-02-14 10:59:45 -0800129 struct cache_entry *old_entry = istate->cache[nr], *new_entry;
Petr Baudis81dc2302008-07-21 02:25:56 +0200130 int namelen = strlen(new_name);
131
Jameson Millera8497352018-07-02 19:49:31 +0000132 new_entry = make_empty_cache_entry(istate, namelen);
Brandon Williams285c2e22018-02-14 10:59:45 -0800133 copy_cache_entry(new_entry, old_entry);
134 new_entry->ce_flags &= ~CE_HASHED;
135 new_entry->ce_namelen = namelen;
136 new_entry->index = 0;
137 memcpy(new_entry->name, new_name, namelen + 1);
Petr Baudis81dc2302008-07-21 02:25:56 +0200138
Brandon Williams285c2e22018-02-14 10:59:45 -0800139 cache_tree_invalidate_path(istate, old_entry->name);
140 untracked_cache_remove_from_index(istate, old_entry->name);
Petr Baudis81dc2302008-07-21 02:25:56 +0200141 remove_index_entry_at(istate, nr);
Brandon Williams285c2e22018-02-14 10:59:45 -0800142 add_index_entry(istate, new_entry, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
Petr Baudis81dc2302008-07-21 02:25:56 +0200143}
144
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200145void fill_stat_data(struct stat_data *sd, struct stat *st)
146{
147 sd->sd_ctime.sec = (unsigned int)st->st_ctime;
148 sd->sd_mtime.sec = (unsigned int)st->st_mtime;
149 sd->sd_ctime.nsec = ST_CTIME_NSEC(*st);
150 sd->sd_mtime.nsec = ST_MTIME_NSEC(*st);
151 sd->sd_dev = st->st_dev;
152 sd->sd_ino = st->st_ino;
153 sd->sd_uid = st->st_uid;
154 sd->sd_gid = st->st_gid;
155 sd->sd_size = st->st_size;
156}
157
158int match_stat_data(const struct stat_data *sd, struct stat *st)
159{
160 int changed = 0;
161
162 if (sd->sd_mtime.sec != (unsigned int)st->st_mtime)
163 changed |= MTIME_CHANGED;
164 if (trust_ctime && check_stat &&
165 sd->sd_ctime.sec != (unsigned int)st->st_ctime)
166 changed |= CTIME_CHANGED;
167
168#ifdef USE_NSEC
169 if (check_stat && sd->sd_mtime.nsec != ST_MTIME_NSEC(*st))
170 changed |= MTIME_CHANGED;
171 if (trust_ctime && check_stat &&
172 sd->sd_ctime.nsec != ST_CTIME_NSEC(*st))
173 changed |= CTIME_CHANGED;
174#endif
175
176 if (check_stat) {
177 if (sd->sd_uid != (unsigned int) st->st_uid ||
178 sd->sd_gid != (unsigned int) st->st_gid)
179 changed |= OWNER_CHANGED;
180 if (sd->sd_ino != (unsigned int) st->st_ino)
181 changed |= INODE_CHANGED;
182 }
183
184#ifdef USE_STDEV
185 /*
186 * st_dev breaks on network filesystems where different
187 * clients will have different views of what "device"
188 * the filesystem is on
189 */
190 if (check_stat && sd->sd_dev != (unsigned int) st->st_dev)
191 changed |= INODE_CHANGED;
192#endif
193
194 if (sd->sd_size != (unsigned int) st->st_size)
195 changed |= DATA_CHANGED;
196
197 return changed;
198}
199
Junio C Hamano415e96c2005-05-15 14:23:12 -0700200/*
201 * This only updates the "non-critical" parts of the directory
202 * cache, ie the parts that aren't tracked by GIT, and only used
203 * to validate the cache.
204 */
Johannes Schindelind4c0a3a2019-05-24 05:23:47 -0700205void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st)
Junio C Hamano415e96c2005-05-15 14:23:12 -0700206{
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200207 fill_stat_data(&ce->ce_stat_data, st);
Junio C Hamano5f730762006-02-08 21:15:24 -0800208
209 if (assume_unchanged)
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800210 ce->ce_flags |= CE_VALID;
Junio C Hamanoeadb5832008-01-18 23:45:24 -0800211
Ben Peart883e2482017-09-22 12:35:40 -0400212 if (S_ISREG(st->st_mode)) {
Junio C Hamanoeadb5832008-01-18 23:45:24 -0800213 ce_mark_uptodate(ce);
Johannes Schindelinb5a81692019-05-24 05:23:48 -0700214 mark_fsmonitor_valid(istate, ce);
Ben Peart883e2482017-09-22 12:35:40 -0400215 }
Junio C Hamano415e96c2005-05-15 14:23:12 -0700216}
217
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200218static int ce_compare_data(struct index_state *istate,
219 const struct cache_entry *ce,
220 struct stat *st)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800221{
222 int match = -1;
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -0700223 int fd = git_open_cloexec(ce->name, O_RDONLY);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800224
225 if (fd >= 0) {
Patryk Obarabebfecb2017-08-20 22:09:27 +0200226 struct object_id oid;
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200227 if (!index_fd(istate, &oid, fd, st, OBJ_BLOB, ce->name, 0))
Jeff King6a29d7b2018-08-28 17:22:59 -0400228 match = !oideq(&oid, &ce->oid);
Linus Torvalds7f8508e2006-07-31 09:55:15 -0700229 /* index_fd() closed the file descriptor already */
Junio C Hamano29e4d362005-12-20 00:02:15 -0800230 }
231 return match;
232}
233
René Scharfe21a6b9f2013-06-02 17:46:52 +0200234static int ce_compare_link(const struct cache_entry *ce, size_t expected_size)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800235{
236 int match = -1;
Junio C Hamano29e4d362005-12-20 00:02:15 -0800237 void *buffer;
238 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500239 enum object_type type;
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800240 struct strbuf sb = STRBUF_INIT;
Junio C Hamano29e4d362005-12-20 00:02:15 -0800241
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800242 if (strbuf_readlink(&sb, ce->name, expected_size))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800243 return -1;
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800244
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000245 buffer = read_object_file(&ce->oid, &type, &size);
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800246 if (buffer) {
247 if (size == sb.len)
248 match = memcmp(buffer, sb.buf, size);
249 free(buffer);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800250 }
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800251 strbuf_release(&sb);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800252 return match;
253}
254
René Scharfe21a6b9f2013-06-02 17:46:52 +0200255static int ce_compare_gitlink(const struct cache_entry *ce)
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700256{
brian m. carlson1053fe82017-10-15 22:07:06 +0000257 struct object_id oid;
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700258
259 /*
260 * We don't actually require that the .git directory
Martin Waitz302b9282007-05-21 22:08:28 +0200261 * under GITLINK directory be a valid git directory. It
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700262 * might even be missing (in case nobody populated that
263 * sub-project).
264 *
265 * If so, we consider it always to match.
266 */
brian m. carlsona98e6102017-10-15 22:07:07 +0000267 if (resolve_gitlink_ref(ce->name, "HEAD", &oid) < 0)
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700268 return 0;
Jeff King6a29d7b2018-08-28 17:22:59 -0400269 return !oideq(&oid, &ce->oid);
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700270}
271
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200272static int ce_modified_check_fs(struct index_state *istate,
273 const struct cache_entry *ce,
274 struct stat *st)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800275{
276 switch (st->st_mode & S_IFMT) {
277 case S_IFREG:
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200278 if (ce_compare_data(istate, ce, st))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800279 return DATA_CHANGED;
280 break;
281 case S_IFLNK:
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500282 if (ce_compare_link(ce, xsize_t(st->st_size)))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800283 return DATA_CHANGED;
284 break;
Linus Torvaldsa8ee75b2007-04-13 09:24:13 -0700285 case S_IFDIR:
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800286 if (S_ISGITLINK(ce->ce_mode))
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700287 return ce_compare_gitlink(ce) ? DATA_CHANGED : 0;
Jeff King1cf01a32017-09-21 02:25:41 -0400288 /* else fallthrough */
Junio C Hamano29e4d362005-12-20 00:02:15 -0800289 default:
290 return TYPE_CHANGED;
291 }
292 return 0;
293}
294
René Scharfe21a6b9f2013-06-02 17:46:52 +0200295static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st)
Linus Torvalds734aab72005-04-09 09:48:20 -0700296{
297 unsigned int changed = 0;
298
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800299 if (ce->ce_flags & CE_REMOVE)
300 return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED;
301
302 switch (ce->ce_mode & S_IFMT) {
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200303 case S_IFREG:
304 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
Junio C Hamano3e09cdf2005-10-11 18:45:33 -0700305 /* We consider only the owner x bit to be relevant for
306 * "mode changes"
307 */
308 if (trust_executable_bit &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800309 (0100 & (ce->ce_mode ^ st->st_mode)))
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200310 changed |= MODE_CHANGED;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200311 break;
312 case S_IFLNK:
Johannes Sixt78a8d642007-03-02 22:11:30 +0100313 if (!S_ISLNK(st->st_mode) &&
314 (has_symlinks || !S_ISREG(st->st_mode)))
315 changed |= TYPE_CHANGED;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200316 break;
Martin Waitz302b9282007-05-21 22:08:28 +0200317 case S_IFGITLINK:
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700318 /* We ignore most of the st_xxx fields for gitlinks */
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700319 if (!S_ISDIR(st->st_mode))
320 changed |= TYPE_CHANGED;
321 else if (ce_compare_gitlink(ce))
322 changed |= DATA_CHANGED;
Linus Torvaldsa8ee75b2007-04-13 09:24:13 -0700323 return changed;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200324 default:
Nguyễn Thái Ngọc Duy391408e2018-11-10 06:16:04 +0100325 BUG("unsupported ce_mode: %o", ce->ce_mode);
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200326 }
Linus Torvaldsccc4feb2005-04-15 10:44:27 -0700327
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200328 changed |= match_stat_data(&ce->ce_stat_data, st);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800329
Linus Torvaldsf49c2c22008-06-10 10:44:43 -0700330 /* Racily smudged entry? */
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200331 if (!ce->ce_stat_data.sd_size) {
brian m. carlson99d1a982016-09-05 20:07:52 +0000332 if (!is_empty_blob_sha1(ce->oid.hash))
Linus Torvaldsf49c2c22008-06-10 10:44:43 -0700333 changed |= DATA_CHANGED;
334 }
335
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800336 return changed;
337}
338
Nguyễn Thái Ngọc Duy2bb4cda2015-03-08 17:12:36 +0700339static int is_racy_stat(const struct index_state *istate,
340 const struct stat_data *sd)
341{
342 return (istate->timestamp.sec &&
343#ifdef USE_NSEC
344 /* nanosecond timestamped files can also be racy! */
345 (istate->timestamp.sec < sd->sd_mtime.sec ||
346 (istate->timestamp.sec == sd->sd_mtime.sec &&
347 istate->timestamp.nsec <= sd->sd_mtime.nsec))
348#else
349 istate->timestamp.sec <= sd->sd_mtime.sec
350#endif
351 );
352}
353
SZEDER Gábor5581a012018-10-11 11:43:09 +0200354int is_racy_timestamp(const struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200355 const struct cache_entry *ce)
Junio C Hamano6d91da62008-01-21 00:44:50 -0800356{
Junio C Hamano050288d2008-05-03 17:24:28 -0700357 return (!S_ISGITLINK(ce->ce_mode) &&
Nguyễn Thái Ngọc Duy2bb4cda2015-03-08 17:12:36 +0700358 is_racy_stat(istate, &ce->ce_stat_data));
Junio C Hamano6d91da62008-01-21 00:44:50 -0800359}
360
Nguyễn Thái Ngọc Duyed4efab2015-03-08 17:12:37 +0700361int match_stat_data_racy(const struct index_state *istate,
362 const struct stat_data *sd, struct stat *st)
363{
364 if (is_racy_stat(istate, sd))
365 return MTIME_CHANGED;
366 return match_stat_data(sd, st);
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800367}
368
Ben Peart883e2482017-09-22 12:35:40 -0400369int ie_match_stat(struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200370 const struct cache_entry *ce, struct stat *st,
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800371 unsigned int options)
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800372{
Junio C Hamano5f730762006-02-08 21:15:24 -0800373 unsigned int changed;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800374 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700375 int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800376 int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY;
Ben Peart883e2482017-09-22 12:35:40 -0400377 int ignore_fsmonitor = options & CE_MATCH_IGNORE_FSMONITOR;
Junio C Hamano5f730762006-02-08 21:15:24 -0800378
Ben Peart883e2482017-09-22 12:35:40 -0400379 if (!ignore_fsmonitor)
380 refresh_fsmonitor(istate);
Junio C Hamano5f730762006-02-08 21:15:24 -0800381 /*
382 * If it's marked as always valid in the index, it's
383 * valid whatever the checked-out copy says.
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700384 *
385 * skip-worktree has the same effect with higher precedence
Junio C Hamano5f730762006-02-08 21:15:24 -0800386 */
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700387 if (!ignore_skip_worktree && ce_skip_worktree(ce))
388 return 0;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800389 if (!ignore_valid && (ce->ce_flags & CE_VALID))
Junio C Hamano5f730762006-02-08 21:15:24 -0800390 return 0;
Ben Peart883e2482017-09-22 12:35:40 -0400391 if (!ignore_fsmonitor && (ce->ce_flags & CE_FSMONITOR_VALID))
392 return 0;
Junio C Hamano5f730762006-02-08 21:15:24 -0800393
Junio C Hamano331fcb52008-11-28 19:56:34 -0800394 /*
395 * Intent-to-add entries have not been added, so the index entry
396 * by definition never matches what is in the work tree until it
397 * actually gets added.
398 */
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +0700399 if (ce_intent_to_add(ce))
Junio C Hamano331fcb52008-11-28 19:56:34 -0800400 return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED;
401
Junio C Hamano5f730762006-02-08 21:15:24 -0800402 changed = ce_match_stat_basic(ce, st);
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800403
Junio C Hamano29e4d362005-12-20 00:02:15 -0800404 /*
405 * Within 1 second of this sequence:
406 * echo xyzzy >file && git-update-index --add file
407 * running this command:
408 * echo frotz >file
409 * would give a falsely clean cache entry. The mtime and
410 * length match the cache, and other stat fields do not change.
411 *
412 * We could detect this at update-index time (the cache entry
413 * being registered/updated records the same time as "now")
414 * and delay the return from git-update-index, but that would
415 * effectively mean we can make at most one commit per second,
416 * which is not acceptable. Instead, we check cache entries
417 * whose mtime are the same as the index file timestamp more
Junio C Hamano5f730762006-02-08 21:15:24 -0800418 * carefully than others.
Junio C Hamano29e4d362005-12-20 00:02:15 -0800419 */
Junio C Hamano6d91da62008-01-21 00:44:50 -0800420 if (!changed && is_racy_timestamp(istate, ce)) {
Junio C Hamano42f77402006-08-15 21:38:07 -0700421 if (assume_racy_is_modified)
422 changed |= DATA_CHANGED;
423 else
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200424 changed |= ce_modified_check_fs(istate, ce, st);
Junio C Hamano42f77402006-08-15 21:38:07 -0700425 }
Junio C Hamano29e4d362005-12-20 00:02:15 -0800426
Linus Torvalds734aab72005-04-09 09:48:20 -0700427 return changed;
428}
429
Ben Peart883e2482017-09-22 12:35:40 -0400430int ie_modified(struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200431 const struct cache_entry *ce,
432 struct stat *st, unsigned int options)
Junio C Hamanob0391892005-09-19 15:11:15 -0700433{
Junio C Hamano29e4d362005-12-20 00:02:15 -0800434 int changed, changed_fs;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800435
436 changed = ie_match_stat(istate, ce, st, options);
Junio C Hamanob0391892005-09-19 15:11:15 -0700437 if (!changed)
438 return 0;
Junio C Hamanob0391892005-09-19 15:11:15 -0700439 /*
440 * If the mode or type has changed, there's no point in trying
441 * to refresh the entry - it's not going to match
442 */
443 if (changed & (MODE_CHANGED | TYPE_CHANGED))
444 return changed;
445
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700446 /*
447 * Immediately after read-tree or update-index --cacheinfo,
448 * the length field is zero, as we have never even read the
449 * lstat(2) information once, and we cannot trust DATA_CHANGED
450 * returned by ie_match_stat() which in turn was returned by
451 * ce_match_stat_basic() to signal that the filesize of the
452 * blob changed. We have to actually go to the filesystem to
453 * see if the contents match, and if so, should answer "unchanged".
454 *
455 * The logic does not apply to gitlinks, as ce_match_stat_basic()
456 * already has checked the actual HEAD from the filesystem in the
457 * subproject. If ie_match_stat() already said it is different,
458 * then we know it is.
Junio C Hamanob0391892005-09-19 15:11:15 -0700459 */
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700460 if ((changed & DATA_CHANGED) &&
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200461 (S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0))
Junio C Hamanob0391892005-09-19 15:11:15 -0700462 return changed;
463
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200464 changed_fs = ce_modified_check_fs(istate, ce, st);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800465 if (changed_fs)
466 return changed | changed_fs;
Junio C Hamanob0391892005-09-19 15:11:15 -0700467 return 0;
468}
469
Linus Torvalds958ba6c2005-05-20 09:09:18 -0700470int base_name_compare(const char *name1, int len1, int mode1,
471 const char *name2, int len2, int mode2)
472{
473 unsigned char c1, c2;
474 int len = len1 < len2 ? len1 : len2;
475 int cmp;
476
477 cmp = memcmp(name1, name2, len);
478 if (cmp)
479 return cmp;
480 c1 = name1[len];
481 c2 = name2[len];
Linus Torvalds1833a922007-04-11 14:39:12 -0700482 if (!c1 && S_ISDIR(mode1))
Linus Torvalds958ba6c2005-05-20 09:09:18 -0700483 c1 = '/';
Linus Torvalds1833a922007-04-11 14:39:12 -0700484 if (!c2 && S_ISDIR(mode2))
Linus Torvalds958ba6c2005-05-20 09:09:18 -0700485 c2 = '/';
486 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
487}
488
Linus Torvalds0ab9e1e2008-03-05 18:25:10 -0800489/*
490 * df_name_compare() is identical to base_name_compare(), except it
491 * compares conflicting directory/file entries as equal. Note that
492 * while a directory name compares as equal to a regular file, they
493 * then individually compare _differently_ to a filename that has
494 * a dot after the basename (because '\0' < '.' < '/').
495 *
496 * This is used by routines that want to traverse the git namespace
497 * but then handle conflicting entries together when possible.
498 */
499int df_name_compare(const char *name1, int len1, int mode1,
500 const char *name2, int len2, int mode2)
501{
502 int len = len1 < len2 ? len1 : len2, cmp;
503 unsigned char c1, c2;
504
505 cmp = memcmp(name1, name2, len);
506 if (cmp)
507 return cmp;
508 /* Directories and files compare equal (same length, same name) */
509 if (len1 == len2)
510 return 0;
511 c1 = name1[len];
512 if (!c1 && S_ISDIR(mode1))
513 c1 = '/';
514 c2 = name2[len];
515 if (!c2 && S_ISDIR(mode2))
516 c2 = '/';
517 if (c1 == '/' && !c2)
518 return 0;
519 if (c2 == '/' && !c1)
520 return 0;
521 return c1 - c2;
522}
523
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -0700524int name_compare(const char *name1, size_t len1, const char *name2, size_t len2)
Linus Torvaldseb38c222005-04-09 09:26:55 -0700525{
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -0700526 size_t min_len = (len1 < len2) ? len1 : len2;
527 int cmp = memcmp(name1, name2, min_len);
Linus Torvaldseb38c222005-04-09 09:26:55 -0700528 if (cmp)
529 return cmp;
530 if (len1 < len2)
531 return -1;
532 if (len1 > len2)
533 return 1;
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -0700534 return 0;
535}
536
537int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2)
538{
539 int cmp;
540
541 cmp = name_compare(name1, len1, name2, len2);
542 if (cmp)
543 return cmp;
Junio C Hamano5f730762006-02-08 21:15:24 -0800544
Thomas Gummererb60e1882012-07-11 11:22:37 +0200545 if (stage1 < stage2)
Linus Torvalds95fd5bf2005-04-15 22:51:44 -0700546 return -1;
Thomas Gummererb60e1882012-07-11 11:22:37 +0200547 if (stage1 > stage2)
Linus Torvalds95fd5bf2005-04-15 22:51:44 -0700548 return 1;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700549 return 0;
550}
551
Derrick Stolee847a9e52021-04-01 01:49:39 +0000552static int index_name_stage_pos(struct index_state *istate, const char *name, int namelen, int stage)
Linus Torvaldseb38c222005-04-09 09:26:55 -0700553{
554 int first, last;
555
556 first = 0;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700557 last = istate->cache_nr;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700558 while (last > first) {
René Scharfe568a05c2019-06-13 19:51:56 +0200559 int next = first + ((last - first) >> 1);
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700560 struct cache_entry *ce = istate->cache[next];
Thomas Gummererb60e1882012-07-11 11:22:37 +0200561 int cmp = cache_name_stage_compare(name, namelen, stage, ce->name, ce_namelen(ce), ce_stage(ce));
Linus Torvaldseb38c222005-04-09 09:26:55 -0700562 if (!cmp)
Linus Torvalds76e7f4e2005-04-10 22:06:50 -0700563 return next;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700564 if (cmp < 0) {
565 last = next;
566 continue;
567 }
568 first = next+1;
569 }
Derrick Stolee95e03212021-04-01 01:49:40 +0000570
571 if (istate->sparse_index &&
572 first > 0) {
573 /* Note: first <= istate->cache_nr */
574 struct cache_entry *ce = istate->cache[first - 1];
575
576 /*
577 * If we are in a sparse-index _and_ the entry before the
578 * insertion position is a sparse-directory entry that is
579 * an ancestor of 'name', then we need to expand the index
580 * and search again. This will only trigger once, because
581 * thereafter the index is fully expanded.
582 */
583 if (S_ISSPARSEDIR(ce->ce_mode) &&
584 ce_namelen(ce) < namelen &&
585 !strncmp(name, ce->name, ce_namelen(ce))) {
586 ensure_full_index(istate);
587 return index_name_stage_pos(istate, name, namelen, stage);
588 }
589 }
590
Linus Torvalds76e7f4e2005-04-10 22:06:50 -0700591 return -first-1;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700592}
593
Derrick Stolee847a9e52021-04-01 01:49:39 +0000594int index_name_pos(struct index_state *istate, const char *name, int namelen)
Thomas Gummererb60e1882012-07-11 11:22:37 +0200595{
596 return index_name_stage_pos(istate, name, namelen, 0);
597}
598
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700599int remove_index_entry_at(struct index_state *istate, int pos)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700600{
Linus Torvaldscf558702008-01-22 18:41:14 -0800601 struct cache_entry *ce = istate->cache[pos];
602
Junio C Hamanocfc57892009-12-25 00:30:51 -0800603 record_resolve_undo(istate, ce);
Karsten Blees20926782013-02-28 00:57:48 +0100604 remove_name_hash(istate, ce);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700605 save_or_free_index_entry(istate, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700606 istate->cache_changed |= CE_ENTRY_REMOVED;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700607 istate->cache_nr--;
608 if (pos >= istate->cache_nr)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700609 return 0;
René Scharfef331ab92017-07-15 22:00:45 +0200610 MOVE_ARRAY(istate->cache + pos, istate->cache + pos + 1,
611 istate->cache_nr - pos);
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700612 return 1;
613}
614
Kjetil Barvik36419c82009-02-18 23:18:03 +0100615/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200616 * Remove all cache entries marked for removal, that is where
Kjetil Barvik36419c82009-02-18 23:18:03 +0100617 * CE_REMOVE is set in ce_flags. This is much more effective than
618 * calling remove_index_entry_at() for each entry to be removed.
619 */
Thomas Gummerer6fdc2052018-12-20 13:48:16 +0000620void remove_marked_cache_entries(struct index_state *istate, int invalidate)
Kjetil Barvik36419c82009-02-18 23:18:03 +0100621{
622 struct cache_entry **ce_array = istate->cache;
623 unsigned int i, j;
624
625 for (i = j = 0; i < istate->cache_nr; i++) {
Karsten Blees5699d172013-11-14 20:24:37 +0100626 if (ce_array[i]->ce_flags & CE_REMOVE) {
Thomas Gummerer6fdc2052018-12-20 13:48:16 +0000627 if (invalidate) {
628 cache_tree_invalidate_path(istate,
629 ce_array[i]->name);
630 untracked_cache_remove_from_index(istate,
631 ce_array[i]->name);
632 }
Karsten Blees20926782013-02-28 00:57:48 +0100633 remove_name_hash(istate, ce_array[i]);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700634 save_or_free_index_entry(istate, ce_array[i]);
Karsten Blees5699d172013-11-14 20:24:37 +0100635 }
Kjetil Barvik36419c82009-02-18 23:18:03 +0100636 else
637 ce_array[j++] = ce_array[i];
638 }
Nguyễn Thái Ngọc Duyad837d92014-06-13 19:19:26 +0700639 if (j == istate->cache_nr)
640 return;
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700641 istate->cache_changed |= CE_ENTRY_REMOVED;
Kjetil Barvik36419c82009-02-18 23:18:03 +0100642 istate->cache_nr = j;
643}
644
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700645int remove_file_from_index(struct index_state *istate, const char *path)
Linus Torvalds197ee8c2005-04-09 12:09:27 -0700646{
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700647 int pos = index_name_pos(istate, path, strlen(path));
Junio C Hamanoc4e3cca2005-04-17 09:53:35 -0700648 if (pos < 0)
649 pos = -pos-1;
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700650 cache_tree_invalidate_path(istate, path);
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +0700651 untracked_cache_remove_from_index(istate, path);
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700652 while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path))
653 remove_index_entry_at(istate, pos);
Linus Torvalds197ee8c2005-04-09 12:09:27 -0700654 return 0;
655}
656
Johannes Schindelin20314272007-06-29 18:32:46 +0100657static int compare_name(struct cache_entry *ce, const char *path, int namelen)
658{
659 return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen);
660}
661
662static int index_name_pos_also_unmerged(struct index_state *istate,
663 const char *path, int namelen)
664{
665 int pos = index_name_pos(istate, path, namelen);
666 struct cache_entry *ce;
667
668 if (pos >= 0)
669 return pos;
670
671 /* maybe unmerged? */
672 pos = -1 - pos;
673 if (pos >= istate->cache_nr ||
674 compare_name((ce = istate->cache[pos]), path, namelen))
675 return -1;
676
677 /* order of preference: stage 2, 1, 3 */
678 if (ce_stage(ce) == 1 && pos + 1 < istate->cache_nr &&
679 ce_stage((ce = istate->cache[pos + 1])) == 2 &&
680 !compare_name(ce, path, namelen))
681 pos++;
682 return pos;
683}
684
Linus Torvalds11029522008-03-22 14:22:44 -0700685static int different_name(struct cache_entry *ce, struct cache_entry *alias)
686{
687 int len = ce_namelen(ce);
688 return ce_namelen(alias) != len || memcmp(ce->name, alias->name, len);
689}
690
691/*
692 * If we add a filename that aliases in the cache, we will use the
693 * name that we already have - but we don't want to update the same
694 * alias twice, because that implies that there were actually two
695 * different files with aliasing names!
696 *
697 * So we use the CE_ADDED flag to verify that the alias was an old
698 * one before we accept it as
699 */
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700700static struct cache_entry *create_alias_ce(struct index_state *istate,
701 struct cache_entry *ce,
702 struct cache_entry *alias)
Linus Torvalds11029522008-03-22 14:22:44 -0700703{
704 int len;
Brandon Williams285c2e22018-02-14 10:59:45 -0800705 struct cache_entry *new_entry;
Linus Torvalds11029522008-03-22 14:22:44 -0700706
707 if (alias->ce_flags & CE_ADDED)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100708 die(_("will not add file alias '%s' ('%s' already exists in index)"),
709 ce->name, alias->name);
Linus Torvalds11029522008-03-22 14:22:44 -0700710
711 /* Ok, create the new entry using the name of the existing alias */
712 len = ce_namelen(alias);
Jameson Millera8497352018-07-02 19:49:31 +0000713 new_entry = make_empty_cache_entry(istate, len);
Brandon Williams285c2e22018-02-14 10:59:45 -0800714 memcpy(new_entry->name, alias->name, len);
715 copy_cache_entry(new_entry, ce);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700716 save_or_free_index_entry(istate, ce);
Brandon Williams285c2e22018-02-14 10:59:45 -0800717 return new_entry;
Linus Torvalds11029522008-03-22 14:22:44 -0700718}
719
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700720void set_object_name_for_intent_to_add_entry(struct cache_entry *ce)
Junio C Hamano39425812008-08-21 01:44:53 -0700721{
Patryk Obaraa09c9852018-01-28 01:13:19 +0100722 struct object_id oid;
723 if (write_object_file("", 0, blob_type, &oid))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100724 die(_("cannot create an empty blob in the object database"));
Patryk Obaraa09c9852018-01-28 01:13:19 +0100725 oidcpy(&ce->oid, &oid);
Junio C Hamano39425812008-08-21 01:44:53 -0700726}
727
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100728int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200729{
Jameson Millera8497352018-07-02 19:49:31 +0000730 int namelen, was_same;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700731 mode_t st_mode = st->st_mode;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100732 struct cache_entry *ce, *alias = NULL;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700733 unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY;
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700734 int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND);
735 int pretend = flags & ADD_CACHE_PRETEND;
Junio C Hamano39425812008-08-21 01:44:53 -0700736 int intent_only = flags & ADD_CACHE_INTENT;
737 int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|
738 (intent_only ? ADD_CACHE_NEW_ONLY : 0));
Jeff King9e5da3d2019-01-17 11:27:11 -0500739 int hash_flags = HASH_WRITE_OBJECT;
Kyle Meyerf937bc22019-04-09 19:07:37 -0400740 struct object_id oid;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100741
Jeff King9e5da3d2019-01-17 11:27:11 -0500742 if (flags & ADD_CACHE_RENORMALIZE)
743 hash_flags |= HASH_RENORMALIZE;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200744
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700745 if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100746 return error(_("%s: can only add regular files, symbolic links or git-directories"), path);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200747
748 namelen = strlen(path);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700749 if (S_ISDIR(st_mode)) {
Kyle Meyerf937bc22019-04-09 19:07:37 -0400750 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
751 return error(_("'%s' does not have a commit checked out"), path);
Linus Torvalds09595252007-04-11 14:49:44 -0700752 while (namelen && path[namelen-1] == '/')
753 namelen--;
754 }
Jameson Millera8497352018-07-02 19:49:31 +0000755 ce = make_empty_cache_entry(istate, namelen);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200756 memcpy(ce->name, path, namelen);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200757 ce->ce_namelen = namelen;
Junio C Hamano39425812008-08-21 01:44:53 -0700758 if (!intent_only)
Johannes Schindelind4c0a3a2019-05-24 05:23:47 -0700759 fill_stat_cache_info(istate, ce, st);
Junio C Hamano388b2ac2008-11-28 19:55:25 -0800760 else
761 ce->ce_flags |= CE_INTENT_TO_ADD;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200762
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100763
764 if (trust_executable_bit && has_symlinks) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700765 ce->ce_mode = create_ce_mode(st_mode);
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100766 } else {
Johannes Sixt78a8d642007-03-02 22:11:30 +0100767 /* If there is an existing entry, pick the mode bits and type
768 * from it, otherwise assume unexecutable regular file.
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200769 */
Junio C Hamano185c9752007-02-16 22:43:48 -0800770 struct cache_entry *ent;
Johannes Schindelin20314272007-06-29 18:32:46 +0100771 int pos = index_name_pos_also_unmerged(istate, path, namelen);
Junio C Hamano185c9752007-02-16 22:43:48 -0800772
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700773 ent = (0 <= pos) ? istate->cache[pos] : NULL;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700774 ce->ce_mode = ce_mode_from_stat(ent, st_mode);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200775 }
776
Joshua Jensendc1ae702010-10-03 09:56:45 +0000777 /* When core.ignorecase=true, determine if a directory of the same name but differing
778 * case already exists within the Git repository. If it does, ensure the directory
779 * case of the file being added to the repository matches (is folded into) the existing
780 * entry's directory case.
781 */
782 if (ignore_case) {
David Turner41284eb2015-10-21 13:54:11 -0400783 adjust_dirname_case(istate, ce->name);
Joshua Jensendc1ae702010-10-03 09:56:45 +0000784 }
Jeff Kinge2c2a372019-02-06 21:00:22 -0500785 if (!(flags & ADD_CACHE_RENORMALIZE)) {
Torsten Bögershausen94729352017-11-16 17:38:28 +0100786 alias = index_file_exists(istate, ce->name,
787 ce_namelen(ce), ignore_case);
788 if (alias &&
789 !ce_stage(alias) &&
790 !ie_match_stat(istate, alias, st, ce_option)) {
791 /* Nothing changed, really */
792 if (!S_ISGITLINK(alias->ce_mode))
793 ce_mark_uptodate(alias);
794 alias->ce_flags |= CE_ADDED;
Joshua Jensendc1ae702010-10-03 09:56:45 +0000795
Jameson Millera8497352018-07-02 19:49:31 +0000796 discard_cache_entry(ce);
Torsten Bögershausen94729352017-11-16 17:38:28 +0100797 return 0;
798 }
Junio C Hamano0781b8a2007-07-30 17:12:58 -0700799 }
Junio C Hamano39425812008-08-21 01:44:53 -0700800 if (!intent_only) {
Junio C Hamano1c418242019-02-05 14:26:13 -0800801 if (index_path(istate, &ce->oid, path, st, hash_flags)) {
Jameson Millera8497352018-07-02 19:49:31 +0000802 discard_cache_entry(ce);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100803 return error(_("unable to index file '%s'"), path);
Stefan Beller2d9426b2015-03-20 17:28:00 -0700804 }
Junio C Hamano39425812008-08-21 01:44:53 -0700805 } else
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700806 set_object_name_for_intent_to_add_entry(ce);
Junio C Hamano39425812008-08-21 01:44:53 -0700807
Linus Torvalds11029522008-03-22 14:22:44 -0700808 if (ignore_case && alias && different_name(ce, alias))
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700809 ce = create_alias_ce(istate, ce, alias);
Linus Torvalds11029522008-03-22 14:22:44 -0700810 ce->ce_flags |= CE_ADDED;
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700811
Junio C Hamano3bf0dd12008-07-16 18:48:58 -0700812 /* It was suspected to be racily clean, but it turns out to be Ok */
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700813 was_same = (alias &&
814 !ce_stage(alias) &&
Jeff King4a7e27e2018-08-28 17:22:40 -0400815 oideq(&alias->oid, &ce->oid) &&
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700816 ce->ce_mode == alias->ce_mode);
817
818 if (pretend)
Jameson Millera8497352018-07-02 19:49:31 +0000819 discard_cache_entry(ce);
Junio C Hamano067178e2015-03-23 10:58:00 -0700820 else if (add_index_entry(istate, ce, add_option)) {
Jameson Millera8497352018-07-02 19:49:31 +0000821 discard_cache_entry(ce);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100822 return error(_("unable to add '%s' to index"), path);
Junio C Hamano067178e2015-03-23 10:58:00 -0700823 }
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700824 if (verbose && !was_same)
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200825 printf("add '%s'\n", path);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200826 return 0;
827}
828
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100829int add_file_to_index(struct index_state *istate, const char *path, int flags)
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700830{
831 struct stat st;
832 if (lstat(path, &st))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100833 die_errno(_("unable to stat '%s'"), path);
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100834 return add_to_index(istate, path, &st, flags);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700835}
836
Jameson Millera8497352018-07-02 19:49:31 +0000837struct cache_entry *make_empty_cache_entry(struct index_state *istate, size_t len)
Carlos Rica6640f882007-09-11 05:17:28 +0200838{
Jameson Miller8e72d672018-07-02 19:49:37 +0000839 return mem_pool__ce_calloc(find_mem_pool(istate), len);
Jameson Millera8497352018-07-02 19:49:31 +0000840}
841
Matheus Tavares96168822021-05-04 13:27:28 -0300842struct cache_entry *make_empty_transient_cache_entry(size_t len,
843 struct mem_pool *ce_mem_pool)
Jameson Millera8497352018-07-02 19:49:31 +0000844{
Matheus Tavares96168822021-05-04 13:27:28 -0300845 if (ce_mem_pool)
846 return mem_pool__ce_calloc(ce_mem_pool, len);
Jameson Millera8497352018-07-02 19:49:31 +0000847 return xcalloc(1, cache_entry_size(len));
848}
849
850struct cache_entry *make_cache_entry(struct index_state *istate,
851 unsigned int mode,
Jameson Miller825ed4d2018-07-02 19:49:30 +0000852 const struct object_id *oid,
853 const char *path,
854 int stage,
855 unsigned int refresh_options)
Carlos Rica6640f882007-09-11 05:17:28 +0200856{
Stefan Bellerbc1c2ca2015-02-17 10:06:14 -0800857 struct cache_entry *ce, *ret;
Jameson Millera8497352018-07-02 19:49:31 +0000858 int len;
Carlos Rica6640f882007-09-11 05:17:28 +0200859
Jeff King10ecfa72018-05-04 20:03:35 -0400860 if (!verify_path(path, mode)) {
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100861 error(_("invalid path '%s'"), path);
Carlos Rica6640f882007-09-11 05:17:28 +0200862 return NULL;
Dmitry Potapov7e7abea2008-10-11 20:39:37 +0400863 }
Carlos Rica6640f882007-09-11 05:17:28 +0200864
865 len = strlen(path);
Jameson Millera8497352018-07-02 19:49:31 +0000866 ce = make_empty_cache_entry(istate, len);
Carlos Rica6640f882007-09-11 05:17:28 +0200867
Jameson Miller825ed4d2018-07-02 19:49:30 +0000868 oidcpy(&ce->oid, oid);
Carlos Rica6640f882007-09-11 05:17:28 +0200869 memcpy(ce->name, path, len);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200870 ce->ce_flags = create_ce_flags(stage);
871 ce->ce_namelen = len;
Carlos Rica6640f882007-09-11 05:17:28 +0200872 ce->ce_mode = create_ce_mode(mode);
873
Nguyễn Thái Ngọc Duyd7b665c2018-09-21 17:57:25 +0200874 ret = refresh_cache_entry(istate, ce, refresh_options);
Stefan Beller915e44c2015-03-23 10:57:11 -0700875 if (ret != ce)
Jameson Millera8497352018-07-02 19:49:31 +0000876 discard_cache_entry(ce);
Stefan Beller915e44c2015-03-23 10:57:11 -0700877 return ret;
Carlos Rica6640f882007-09-11 05:17:28 +0200878}
879
Matheus Tavares96168822021-05-04 13:27:28 -0300880struct cache_entry *make_transient_cache_entry(unsigned int mode,
881 const struct object_id *oid,
882 const char *path,
883 int stage,
884 struct mem_pool *ce_mem_pool)
Jameson Millera8497352018-07-02 19:49:31 +0000885{
886 struct cache_entry *ce;
887 int len;
888
889 if (!verify_path(path, mode)) {
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100890 error(_("invalid path '%s'"), path);
Jameson Millera8497352018-07-02 19:49:31 +0000891 return NULL;
892 }
893
894 len = strlen(path);
Matheus Tavares96168822021-05-04 13:27:28 -0300895 ce = make_empty_transient_cache_entry(len, ce_mem_pool);
Jameson Millera8497352018-07-02 19:49:31 +0000896
897 oidcpy(&ce->oid, oid);
898 memcpy(ce->name, path, len);
899 ce->ce_flags = create_ce_flags(stage);
900 ce->ce_namelen = len;
901 ce->ce_mode = create_ce_mode(mode);
902
903 return ce;
904}
905
Thomas Gummererd9d70962016-09-14 22:07:46 +0100906/*
907 * Chmod an index entry with either +x or -x.
908 *
909 * Returns -1 if the chmod for the particular cache entry failed (if it's
910 * not a regular file), -2 if an invalid flip argument is passed in, 0
911 * otherwise.
912 */
913int chmod_index_entry(struct index_state *istate, struct cache_entry *ce,
914 char flip)
915{
916 if (!S_ISREG(ce->ce_mode))
917 return -1;
918 switch (flip) {
919 case '+':
920 ce->ce_mode |= 0111;
921 break;
922 case '-':
923 ce->ce_mode &= ~0111;
924 break;
925 default:
926 return -2;
927 }
928 cache_tree_invalidate_path(istate, ce->name);
929 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -0400930 mark_fsmonitor_invalid(istate, ce);
Thomas Gummererd9d70962016-09-14 22:07:46 +0100931 istate->cache_changed |= CE_ENTRY_CHANGED;
932
933 return 0;
934}
935
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700936int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700937{
938 int len = ce_namelen(a);
939 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
940}
941
Linus Torvalds12676602005-06-18 20:21:34 -0700942/*
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700943 * We fundamentally don't like some paths: we don't want
944 * dot or dot-dot anywhere, and for obvious reasons don't
945 * want to recurse into ".git" either.
946 *
947 * Also, we don't want double slashes or slashes at the
948 * end that can make pathnames ambiguous.
949 */
Jeff King10ecfa72018-05-04 20:03:35 -0400950static int verify_dotfile(const char *rest, unsigned mode)
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700951{
952 /*
953 * The first character was '.', but that
954 * has already been discarded, we now test
955 * the rest.
956 */
Theo Niessinke0f530f2011-06-08 14:04:41 +0200957
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700958 /* "." is not allowed */
Theo Niessinke0f530f2011-06-08 14:04:41 +0200959 if (*rest == '\0' || is_dir_sep(*rest))
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700960 return 0;
961
Theo Niessinke0f530f2011-06-08 14:04:41 +0200962 switch (*rest) {
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700963 /*
Jeff King641084b2018-05-15 09:56:50 -0400964 * ".git" followed by NUL or slash is bad. Note that we match
965 * case-insensitively here, even if ignore_case is not set.
966 * This outlaws ".GIT" everywhere out of an abundance of caution,
967 * since there's really no good reason to allow it.
Jeff King10ecfa72018-05-04 20:03:35 -0400968 *
969 * Once we've seen ".git", we can also find ".gitmodules", etc (also
970 * case-insensitively).
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700971 */
972 case 'g':
Jeff Kingcc2fc7c2014-11-24 13:39:12 -0500973 case 'G':
974 if (rest[1] != 'i' && rest[1] != 'I')
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700975 break;
Jeff Kingcc2fc7c2014-11-24 13:39:12 -0500976 if (rest[2] != 't' && rest[2] != 'T')
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700977 break;
Jeff Kinge19e5e62018-05-13 13:00:23 -0400978 if (rest[3] == '\0' || is_dir_sep(rest[3]))
979 return 0;
Jeff King10ecfa72018-05-04 20:03:35 -0400980 if (S_ISLNK(mode)) {
981 rest += 3;
982 if (skip_iprefix(rest, "modules", &rest) &&
983 (*rest == '\0' || is_dir_sep(*rest)))
984 return 0;
985 }
Jeff Kinge19e5e62018-05-13 13:00:23 -0400986 break;
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700987 case '.':
Theo Niessinke0f530f2011-06-08 14:04:41 +0200988 if (rest[1] == '\0' || is_dir_sep(rest[1]))
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700989 return 0;
990 }
991 return 1;
992}
993
Jeff King10ecfa72018-05-04 20:03:35 -0400994int verify_path(const char *path, unsigned mode)
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700995{
Johannes Schindelin via GitGitGadget49e268e2020-01-09 13:30:34 +0000996 char c = 0;
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700997
Erik Faye-Lund56948cb2011-05-27 18:00:40 +0200998 if (has_dos_drive_prefix(path))
999 return 0;
1000
Johannes Schindelind2c84da2019-09-05 13:27:53 +02001001 if (!is_valid_path(path))
1002 return 0;
1003
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001004 goto inside;
1005 for (;;) {
1006 if (!c)
1007 return 1;
Erik Faye-Lund56948cb2011-05-27 18:00:40 +02001008 if (is_dir_sep(c)) {
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001009inside:
Jeff King10ecfa72018-05-04 20:03:35 -04001010 if (protect_hfs) {
Johannes Schindelin via GitGitGadget49e268e2020-01-09 13:30:34 +00001011
Jeff King10ecfa72018-05-04 20:03:35 -04001012 if (is_hfs_dotgit(path))
1013 return 0;
1014 if (S_ISLNK(mode)) {
1015 if (is_hfs_dotgitmodules(path))
1016 return 0;
1017 }
1018 }
1019 if (protect_ntfs) {
Adam Dinwoodiebccc37f2021-04-29 21:11:44 +01001020#if defined GIT_WINDOWS_NATIVE || defined __CYGWIN__
Johannes Schindelin via GitGitGadget49e268e2020-01-09 13:30:34 +00001021 if (c == '\\')
1022 return 0;
1023#endif
Jeff King10ecfa72018-05-04 20:03:35 -04001024 if (is_ntfs_dotgit(path))
1025 return 0;
1026 if (S_ISLNK(mode)) {
1027 if (is_ntfs_dotgitmodules(path))
1028 return 0;
1029 }
1030 }
1031
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001032 c = *path++;
Jeff King10ecfa72018-05-04 20:03:35 -04001033 if ((c == '.' && !verify_dotfile(path, mode)) ||
Derrick Stolee6e773522021-03-30 13:10:55 +00001034 is_dir_sep(c))
Junio C Hamano3bdf09c2011-06-06 20:49:06 -07001035 return 0;
Derrick Stolee6e773522021-03-30 13:10:55 +00001036 /*
1037 * allow terminating directory separators for
1038 * sparse directory entries.
1039 */
1040 if (c == '\0')
1041 return S_ISDIR(mode);
Johannes Schindelin288a74b2019-09-23 08:58:11 +02001042 } else if (c == '\\' && protect_ntfs) {
1043 if (is_ntfs_dotgit(path))
1044 return 0;
1045 if (S_ISLNK(mode)) {
1046 if (is_ntfs_dotgitmodules(path))
1047 return 0;
1048 }
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001049 }
Johannes Schindelin288a74b2019-09-23 08:58:11 +02001050
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001051 c = *path++;
1052 }
1053}
1054
1055/*
Linus Torvalds12676602005-06-18 20:21:34 -07001056 * Do we have another file that has the beginning components being a
1057 * proper superset of the name we're trying to add?
1058 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001059static int has_file_name(struct index_state *istate,
1060 const struct cache_entry *ce, int pos, int ok_to_replace)
Linus Torvalds12676602005-06-18 20:21:34 -07001061{
1062 int retval = 0;
1063 int len = ce_namelen(ce);
Junio C Hamanob1557252005-06-25 02:25:29 -07001064 int stage = ce_stage(ce);
Linus Torvalds12676602005-06-18 20:21:34 -07001065 const char *name = ce->name;
1066
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001067 while (pos < istate->cache_nr) {
1068 struct cache_entry *p = istate->cache[pos++];
Linus Torvalds12676602005-06-18 20:21:34 -07001069
1070 if (len >= ce_namelen(p))
1071 break;
1072 if (memcmp(name, p->name, len))
1073 break;
Junio C Hamanob1557252005-06-25 02:25:29 -07001074 if (ce_stage(p) != stage)
1075 continue;
Linus Torvalds12676602005-06-18 20:21:34 -07001076 if (p->name[len] != '/')
1077 continue;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001078 if (p->ce_flags & CE_REMOVE)
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001079 continue;
Linus Torvalds12676602005-06-18 20:21:34 -07001080 retval = -1;
1081 if (!ok_to_replace)
1082 break;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001083 remove_index_entry_at(istate, --pos);
Linus Torvalds12676602005-06-18 20:21:34 -07001084 }
1085 return retval;
1086}
1087
Jeff Hostetlera6db3fb2017-04-14 19:12:28 +00001088
1089/*
1090 * Like strcmp(), but also return the offset of the first change.
1091 * If strings are equal, return the length.
1092 */
1093int strcmp_offset(const char *s1, const char *s2, size_t *first_change)
1094{
1095 size_t k;
1096
1097 if (!first_change)
1098 return strcmp(s1, s2);
1099
1100 for (k = 0; s1[k] == s2[k]; k++)
1101 if (s1[k] == '\0')
1102 break;
1103
1104 *first_change = k;
1105 return (unsigned char)s1[k] - (unsigned char)s2[k];
1106}
1107
Linus Torvalds12676602005-06-18 20:21:34 -07001108/*
1109 * Do we have another file with a pathname that is a proper
1110 * subset of the name we're trying to add?
Jeff Hostetler06b6d812017-04-19 17:06:17 +00001111 *
1112 * That is, is there another file in the index with a path
1113 * that matches a sub-directory in the given entry?
Linus Torvalds12676602005-06-18 20:21:34 -07001114 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001115static int has_dir_name(struct index_state *istate,
1116 const struct cache_entry *ce, int pos, int ok_to_replace)
Linus Torvalds12676602005-06-18 20:21:34 -07001117{
1118 int retval = 0;
Junio C Hamanob1557252005-06-25 02:25:29 -07001119 int stage = ce_stage(ce);
Linus Torvalds12676602005-06-18 20:21:34 -07001120 const char *name = ce->name;
1121 const char *slash = name + ce_namelen(ce);
Jeff Hostetler06b6d812017-04-19 17:06:17 +00001122 size_t len_eq_last;
1123 int cmp_last = 0;
1124
1125 /*
1126 * We are frequently called during an iteration on a sorted
1127 * list of pathnames and while building a new index. Therefore,
1128 * there is a high probability that this entry will eventually
1129 * be appended to the index, rather than inserted in the middle.
1130 * If we can confirm that, we can avoid binary searches on the
1131 * components of the pathname.
1132 *
1133 * Compare the entry's full path with the last path in the index.
1134 */
1135 if (istate->cache_nr > 0) {
1136 cmp_last = strcmp_offset(name,
1137 istate->cache[istate->cache_nr - 1]->name,
1138 &len_eq_last);
1139 if (cmp_last > 0) {
1140 if (len_eq_last == 0) {
1141 /*
1142 * The entry sorts AFTER the last one in the
1143 * index and their paths have no common prefix,
1144 * so there cannot be a F/D conflict.
1145 */
1146 return retval;
1147 } else {
1148 /*
1149 * The entry sorts AFTER the last one in the
1150 * index, but has a common prefix. Fall through
1151 * to the loop below to disect the entry's path
1152 * and see where the difference is.
1153 */
1154 }
1155 } else if (cmp_last == 0) {
1156 /*
1157 * The entry exactly matches the last one in the
1158 * index, but because of multiple stage and CE_REMOVE
1159 * items, we fall through and let the regular search
1160 * code handle it.
1161 */
1162 }
1163 }
Linus Torvalds12676602005-06-18 20:21:34 -07001164
1165 for (;;) {
Jeff Hostetlerb986df52017-04-19 17:06:18 +00001166 size_t len;
Linus Torvalds12676602005-06-18 20:21:34 -07001167
1168 for (;;) {
1169 if (*--slash == '/')
1170 break;
1171 if (slash <= ce->name)
1172 return retval;
1173 }
1174 len = slash - name;
1175
Jeff Hostetlerb986df52017-04-19 17:06:18 +00001176 if (cmp_last > 0) {
1177 /*
1178 * (len + 1) is a directory boundary (including
1179 * the trailing slash). And since the loop is
1180 * decrementing "slash", the first iteration is
1181 * the longest directory prefix; subsequent
1182 * iterations consider parent directories.
1183 */
1184
1185 if (len + 1 <= len_eq_last) {
1186 /*
1187 * The directory prefix (including the trailing
1188 * slash) also appears as a prefix in the last
1189 * entry, so the remainder cannot collide (because
1190 * strcmp said the whole path was greater).
1191 *
1192 * EQ: last: xxx/A
1193 * this: xxx/B
1194 *
1195 * LT: last: xxx/file_A
1196 * this: xxx/file_B
1197 */
1198 return retval;
1199 }
1200
1201 if (len > len_eq_last) {
1202 /*
1203 * This part of the directory prefix (excluding
1204 * the trailing slash) is longer than the known
1205 * equal portions, so this sub-directory cannot
1206 * collide with a file.
1207 *
1208 * GT: last: xxxA
1209 * this: xxxB/file
1210 */
1211 return retval;
1212 }
1213
Jeff Hostetlerb986df52017-04-19 17:06:18 +00001214 /*
1215 * This is a possible collision. Fall through and
1216 * let the regular search code handle it.
1217 *
1218 * last: xxx
1219 * this: xxx/file
1220 */
1221 }
1222
Thomas Gummererb60e1882012-07-11 11:22:37 +02001223 pos = index_name_stage_pos(istate, name, len, stage);
Linus Torvalds12676602005-06-18 20:21:34 -07001224 if (pos >= 0) {
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001225 /*
1226 * Found one, but not so fast. This could
1227 * be a marker that says "I was here, but
1228 * I am being removed". Such an entry is
1229 * not a part of the resulting tree, and
1230 * it is Ok to have a directory at the same
1231 * path.
1232 */
Junio C Hamano077c48d2008-01-22 21:24:21 -08001233 if (!(istate->cache[pos]->ce_flags & CE_REMOVE)) {
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001234 retval = -1;
1235 if (!ok_to_replace)
1236 break;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001237 remove_index_entry_at(istate, pos);
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001238 continue;
1239 }
Linus Torvalds12676602005-06-18 20:21:34 -07001240 }
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001241 else
1242 pos = -pos-1;
Linus Torvalds12676602005-06-18 20:21:34 -07001243
1244 /*
1245 * Trivial optimization: if we find an entry that
1246 * already matches the sub-directory, then we know
Junio C Hamanob1557252005-06-25 02:25:29 -07001247 * we're ok, and we can exit.
Linus Torvalds12676602005-06-18 20:21:34 -07001248 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001249 while (pos < istate->cache_nr) {
1250 struct cache_entry *p = istate->cache[pos];
Junio C Hamanob1557252005-06-25 02:25:29 -07001251 if ((ce_namelen(p) <= len) ||
1252 (p->name[len] != '/') ||
1253 memcmp(p->name, name, len))
1254 break; /* not our subdirectory */
Junio C Hamano077c48d2008-01-22 21:24:21 -08001255 if (ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE))
1256 /*
1257 * p is at the same stage as our entry, and
Junio C Hamanob1557252005-06-25 02:25:29 -07001258 * is a subdirectory of what we are looking
1259 * at, so we cannot have conflicts at our
1260 * level or anything shorter.
1261 */
1262 return retval;
1263 pos++;
Linus Torvalds12676602005-06-18 20:21:34 -07001264 }
1265 }
1266 return retval;
1267}
1268
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001269/* We may be in a situation where we already have path/file and path
1270 * is being added, or we already have path and path/file is being
1271 * added. Either one would result in a nonsense tree that has path
1272 * twice when git-write-tree tries to write it out. Prevent it.
Junio C Hamanoa6080a02007-06-07 00:04:01 -07001273 *
Junio C Hamano192268c2005-05-07 21:55:21 -07001274 * If ok-to-replace is specified, we remove the conflicting entries
1275 * from the cache so the caller should recompute the insert position.
1276 * When this happens, we return non-zero.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001277 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001278static int check_file_directory_conflict(struct index_state *istate,
1279 const struct cache_entry *ce,
1280 int pos, int ok_to_replace)
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001281{
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001282 int retval;
1283
1284 /*
1285 * When ce is an "I am going away" entry, we allow it to be added
1286 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001287 if (ce->ce_flags & CE_REMOVE)
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001288 return 0;
1289
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001290 /*
Linus Torvalds12676602005-06-18 20:21:34 -07001291 * We check if the path is a sub-path of a subsequent pathname
1292 * first, since removing those will not change the position
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001293 * in the array.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001294 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001295 retval = has_file_name(istate, ce, pos, ok_to_replace);
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001296
Linus Torvalds12676602005-06-18 20:21:34 -07001297 /*
1298 * Then check if the path might have a clashing sub-directory
1299 * before it.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001300 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001301 return retval + has_dir_name(istate, ce, pos, ok_to_replace);
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001302}
1303
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001304static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001305{
1306 int pos;
Junio C Hamano192268c2005-05-07 21:55:21 -07001307 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
1308 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
Junio C Hamanob1557252005-06-25 02:25:29 -07001309 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
Junio C Hamano39425812008-08-21 01:44:53 -07001310 int new_only = option & ADD_CACHE_NEW_ONLY;
Junio C Hamano5f730762006-02-08 21:15:24 -08001311
Nguyễn Thái Ngọc Duyce7c6142014-06-13 19:19:42 +07001312 if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1313 cache_tree_invalidate_path(istate, ce->name);
Jeff Hostetlere5494632017-04-19 17:06:16 +00001314
1315 /*
1316 * If this entry's path sorts after the last entry in the index,
1317 * we can avoid searching for it.
1318 */
1319 if (istate->cache_nr > 0 &&
1320 strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0)
Johannes Schindelinc097b952019-10-04 08:09:26 -07001321 pos = index_pos_to_insert_pos(istate->cache_nr);
Jeff Hostetlere5494632017-04-19 17:06:16 +00001322 else
1323 pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001324
Junio C Hamano3e09cdf2005-10-11 18:45:33 -07001325 /* existing match? Just replace it. */
Linus Torvalds76e7f4e2005-04-10 22:06:50 -07001326 if (pos >= 0) {
Junio C Hamano39425812008-08-21 01:44:53 -07001327 if (!new_only)
1328 replace_index_entry(istate, pos, ce);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001329 return 0;
1330 }
Linus Torvalds76e7f4e2005-04-10 22:06:50 -07001331 pos = -pos-1;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001332
Nguyễn Thái Ngọc Duyffcc9ba2015-06-07 17:40:52 +07001333 if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1334 untracked_cache_add_to_index(istate, ce->name);
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07001335
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001336 /*
1337 * Inserting a merged entry ("stage 0") into the index
1338 * will always replace all non-merged entries..
1339 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001340 if (pos < istate->cache_nr && ce_stage(ce) == 0) {
1341 while (ce_same_name(istate->cache[pos], ce)) {
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001342 ok_to_add = 1;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001343 if (!remove_index_entry_at(istate, pos))
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001344 break;
1345 }
1346 }
1347
Linus Torvalds121481a2005-04-10 11:32:54 -07001348 if (!ok_to_add)
1349 return -1;
Jeff King10ecfa72018-05-04 20:03:35 -04001350 if (!verify_path(ce->name, ce->ce_mode))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001351 return error(_("invalid path '%s'"), ce->name);
Linus Torvalds121481a2005-04-10 11:32:54 -07001352
Junio C Hamano3e09cdf2005-10-11 18:45:33 -07001353 if (!skip_df_check &&
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001354 check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {
Junio C Hamano192268c2005-05-07 21:55:21 -07001355 if (!ok_to_replace)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001356 return error(_("'%s' appears as both a file and as a directory"),
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001357 ce->name);
Thomas Gummererb60e1882012-07-11 11:22:37 +02001358 pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
Junio C Hamano192268c2005-05-07 21:55:21 -07001359 pos = -pos-1;
1360 }
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001361 return pos + 1;
1362}
1363
1364int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
1365{
1366 int pos;
1367
1368 if (option & ADD_CACHE_JUST_APPEND)
1369 pos = istate->cache_nr;
1370 else {
1371 int ret;
1372 ret = add_index_entry_with_check(istate, ce, option);
1373 if (ret <= 0)
1374 return ret;
1375 pos = ret - 1;
1376 }
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001377
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001378 /* Make sure the array is big enough .. */
Dmitry S. Dolzhenko999f5662014-03-04 02:32:01 +04001379 ALLOC_GROW(istate->cache, istate->cache_nr + 1, istate->cache_alloc);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001380
1381 /* Add it in.. */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001382 istate->cache_nr++;
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001383 if (istate->cache_nr > pos + 1)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +01001384 MOVE_ARRAY(istate->cache + pos + 1, istate->cache + pos,
1385 istate->cache_nr - pos - 1);
Linus Torvaldscf558702008-01-22 18:41:14 -08001386 set_index_entry(istate, pos, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001387 istate->cache_changed |= CE_ENTRY_ADDED;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001388 return 0;
1389}
1390
Linus Torvalds405e5b22006-05-19 09:56:35 -07001391/*
1392 * "refresh" does not calculate a new sha1 file or bring the
1393 * cache up-to-date for mode/content changes. But what it
1394 * _does_ do is to "re-match" the stat information of a file
1395 * with the cache, so that you can refresh the cache for a
1396 * file that hasn't been changed but where the stat entry is
1397 * out of date.
1398 *
1399 * For example, you'd want to do this after doing a "git-read-tree",
1400 * to link up the stat cache details with the proper files.
1401 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001402static struct cache_entry *refresh_cache_ent(struct index_state *istate,
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001403 struct cache_entry *ce,
Jeff Kingd05e6972011-11-18 06:11:08 -05001404 unsigned int options, int *err,
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001405 int *changed_ret,
Jeff Hostetler15268d12021-02-03 15:34:46 +00001406 int *t2_did_lstat,
1407 int *t2_did_scan)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001408{
1409 struct stat st;
1410 struct cache_entry *updated;
Jameson Millera8497352018-07-02 19:49:31 +00001411 int changed;
Brad King25762722014-01-27 09:45:08 -05001412 int refresh = options & CE_MATCH_REFRESH;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001413 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001414 int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
Brad King2e2e7ec2014-01-27 09:45:07 -05001415 int ignore_missing = options & CE_MATCH_IGNORE_MISSING;
Ben Peart883e2482017-09-22 12:35:40 -04001416 int ignore_fsmonitor = options & CE_MATCH_IGNORE_FSMONITOR;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001417
Brad King25762722014-01-27 09:45:08 -05001418 if (!refresh || ce_uptodate(ce))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001419 return ce;
1420
Ben Peart883e2482017-09-22 12:35:40 -04001421 if (!ignore_fsmonitor)
1422 refresh_fsmonitor(istate);
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001423 /*
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001424 * CE_VALID or CE_SKIP_WORKTREE means the user promised us
1425 * that the change to the work tree does not matter and told
1426 * us not to worry.
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001427 */
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001428 if (!ignore_skip_worktree && ce_skip_worktree(ce)) {
1429 ce_mark_uptodate(ce);
1430 return ce;
1431 }
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001432 if (!ignore_valid && (ce->ce_flags & CE_VALID)) {
1433 ce_mark_uptodate(ce);
1434 return ce;
1435 }
Ben Peart883e2482017-09-22 12:35:40 -04001436 if (!ignore_fsmonitor && (ce->ce_flags & CE_FSMONITOR_VALID)) {
1437 ce_mark_uptodate(ce);
1438 return ce;
1439 }
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001440
René Scharfeccad42d2014-08-09 19:43:29 +02001441 if (has_symlink_leading_path(ce->name, ce_namelen(ce))) {
1442 if (ignore_missing)
1443 return ce;
1444 if (err)
1445 *err = ENOENT;
1446 return NULL;
1447 }
1448
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001449 if (t2_did_lstat)
1450 *t2_did_lstat = 1;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001451 if (lstat(ce->name, &st) < 0) {
Brad King2e2e7ec2014-01-27 09:45:07 -05001452 if (ignore_missing && errno == ENOENT)
1453 return ce;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001454 if (err)
1455 *err = errno;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001456 return NULL;
1457 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001458
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001459 changed = ie_match_stat(istate, ce, &st, options);
Jeff Kingd05e6972011-11-18 06:11:08 -05001460 if (changed_ret)
1461 *changed_ret = changed;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001462 if (!changed) {
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001463 /*
1464 * The path is unchanged. If we were told to ignore
1465 * valid bit, then we did the actual stat check and
1466 * found that the entry is unmodified. If the entry
1467 * is not marked VALID, this is the place to mark it
1468 * valid again, under "assume unchanged" mode.
1469 */
1470 if (ignore_valid && assume_unchanged &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001471 !(ce->ce_flags & CE_VALID))
Linus Torvalds405e5b22006-05-19 09:56:35 -07001472 ; /* mark this one VALID again */
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001473 else {
1474 /*
1475 * We do not mark the index itself "modified"
1476 * because CE_UPTODATE flag is in-core only;
1477 * we are not going to write this change out.
1478 */
Ben Peart883e2482017-09-22 12:35:40 -04001479 if (!S_ISGITLINK(ce->ce_mode)) {
Junio C Hamano125fd982010-01-24 00:10:20 -08001480 ce_mark_uptodate(ce);
Johannes Schindelinb5a81692019-05-24 05:23:48 -07001481 mark_fsmonitor_valid(istate, ce);
Ben Peart883e2482017-09-22 12:35:40 -04001482 }
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001483 return ce;
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001484 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001485 }
1486
Jeff Hostetler15268d12021-02-03 15:34:46 +00001487 if (t2_did_scan)
1488 *t2_did_scan = 1;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001489 if (ie_modified(istate, ce, &st, options)) {
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001490 if (err)
1491 *err = EINVAL;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001492 return NULL;
1493 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001494
Jameson Millera8497352018-07-02 19:49:31 +00001495 updated = make_empty_cache_entry(istate, ce_namelen(ce));
Ben Peart0e267b72018-03-15 11:25:20 -04001496 copy_cache_entry(updated, ce);
1497 memcpy(updated->name, ce->name, ce->ce_namelen + 1);
Johannes Schindelind4c0a3a2019-05-24 05:23:47 -07001498 fill_stat_cache_info(istate, updated, &st);
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001499 /*
1500 * If ignore_valid is not set, we should leave CE_VALID bit
1501 * alone. Otherwise, paths marked with --no-assume-unchanged
1502 * (i.e. things to be edited) will reacquire CE_VALID bit
1503 * automatically, which is not really what we want.
Linus Torvalds405e5b22006-05-19 09:56:35 -07001504 */
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001505 if (!ignore_valid && assume_unchanged &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001506 !(ce->ce_flags & CE_VALID))
1507 updated->ce_flags &= ~CE_VALID;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001508
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001509 /* istate->cache_changed is updated in the caller */
Linus Torvalds405e5b22006-05-19 09:56:35 -07001510 return updated;
1511}
1512
Matthieu Moy3deffc52009-08-21 10:57:59 +02001513static void show_file(const char * fmt, const char * name, int in_porcelain,
Jonathan Nieder046613c2011-02-22 22:43:23 +00001514 int * first, const char *header_msg)
Matthieu Moy3deffc52009-08-21 10:57:59 +02001515{
1516 if (in_porcelain && *first && header_msg) {
1517 printf("%s\n", header_msg);
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -07001518 *first = 0;
Matthieu Moy3deffc52009-08-21 10:57:59 +02001519 }
1520 printf(fmt, name);
1521}
1522
Thomas Gummerer22184492019-09-11 19:20:25 +01001523int repo_refresh_and_write_index(struct repository *repo,
1524 unsigned int refresh_flags,
1525 unsigned int write_flags,
1526 int gentle,
1527 const struct pathspec *pathspec,
1528 char *seen, const char *header_msg)
1529{
1530 struct lock_file lock_file = LOCK_INIT;
1531 int fd, ret = 0;
1532
1533 fd = repo_hold_locked_index(repo, &lock_file, 0);
1534 if (!gentle && fd < 0)
1535 return -1;
1536 if (refresh_index(repo->index, refresh_flags, pathspec, seen, header_msg))
1537 ret = 1;
1538 if (0 <= fd && write_locked_index(repo->index, &lock_file, COMMIT_LOCK | write_flags))
1539 ret = -1;
1540 return ret;
1541}
1542
1543
Nguyễn Thái Ngọc Duy9b2d6142013-07-14 15:35:54 +07001544int refresh_index(struct index_state *istate, unsigned int flags,
1545 const struct pathspec *pathspec,
Jonathan Nieder046613c2011-02-22 22:43:23 +00001546 char *seen, const char *header_msg)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001547{
1548 int i;
1549 int has_errors = 0;
1550 int really = (flags & REFRESH_REALLY) != 0;
1551 int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
1552 int quiet = (flags & REFRESH_QUIET) != 0;
1553 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
Johannes Schindelin5fdeacb2008-05-14 18:03:45 +01001554 int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0;
Matheus Tavaresb2430122021-04-08 17:41:26 -03001555 int ignore_skip_worktree = (flags & REFRESH_IGNORE_SKIP_WORKTREE) != 0;
Matthieu Moy3deffc52009-08-21 10:57:59 +02001556 int first = 1;
1557 int in_porcelain = (flags & REFRESH_IN_PORCELAIN);
Brad King25762722014-01-27 09:45:08 -05001558 unsigned int options = (CE_MATCH_REFRESH |
1559 (really ? CE_MATCH_IGNORE_VALID : 0) |
Brad King2e2e7ec2014-01-27 09:45:07 -05001560 (not_new ? CE_MATCH_IGNORE_MISSING : 0));
Jeff King4bd4e732011-11-18 06:11:28 -05001561 const char *modified_fmt;
Jeff King73b7eae2011-11-18 06:13:08 -05001562 const char *deleted_fmt;
1563 const char *typechange_fmt;
1564 const char *added_fmt;
Jeff King4bd4e732011-11-18 06:11:28 -05001565 const char *unmerged_fmt;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001566 struct progress *progress = NULL;
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001567 int t2_sum_lstat = 0;
Jeff Hostetler15268d12021-02-03 15:34:46 +00001568 int t2_sum_scan = 0;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001569
1570 if (flags & REFRESH_PROGRESS && isatty(2))
1571 progress = start_delayed_progress(_("Refresh index"),
1572 istate->cache_nr);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001573
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02001574 trace_performance_enter();
Nguyễn Thái Ngọc Duya71806a2018-11-10 06:16:06 +01001575 modified_fmt = in_porcelain ? "M\t%s\n" : "%s: needs update\n";
1576 deleted_fmt = in_porcelain ? "D\t%s\n" : "%s: needs update\n";
1577 typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
1578 added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
1579 unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
Ben Peart99ce7202018-10-29 16:41:59 -04001580 /*
1581 * Use the multi-threaded preload_index() to refresh most of the
1582 * cache entries quickly then in the single threaded loop below,
1583 * we only have to do the special cases that are left.
1584 */
1585 preload_index(istate, pathspec, 0);
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001586 trace2_region_enter("index", "refresh", NULL);
Derrick Stolee0c18c052021-04-01 01:49:58 +00001587 /* TODO: audit for interaction with sparse-index. */
1588 ensure_full_index(istate);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001589 for (i = 0; i < istate->cache_nr; i++) {
Brandon Williams285c2e22018-02-14 10:59:45 -08001590 struct cache_entry *ce, *new_entry;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001591 int cache_errno = 0;
Jeff King73b7eae2011-11-18 06:13:08 -05001592 int changed = 0;
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001593 int filtered = 0;
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001594 int t2_did_lstat = 0;
Jeff Hostetler15268d12021-02-03 15:34:46 +00001595 int t2_did_scan = 0;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001596
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001597 ce = istate->cache[i];
Johannes Schindelin5fdeacb2008-05-14 18:03:45 +01001598 if (ignore_submodules && S_ISGITLINK(ce->ce_mode))
1599 continue;
Matheus Tavaresb2430122021-04-08 17:41:26 -03001600 if (ignore_skip_worktree && ce_skip_worktree(ce))
1601 continue;
Johannes Schindelin5fdeacb2008-05-14 18:03:45 +01001602
Nguyễn Thái Ngọc Duyd7b665c2018-09-21 17:57:25 +02001603 if (pathspec && !ce_path_match(istate, ce, pathspec, seen))
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001604 filtered = 1;
1605
Linus Torvalds405e5b22006-05-19 09:56:35 -07001606 if (ce_stage(ce)) {
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001607 while ((i < istate->cache_nr) &&
1608 ! strcmp(istate->cache[i]->name, ce->name))
Linus Torvalds405e5b22006-05-19 09:56:35 -07001609 i++;
1610 i--;
1611 if (allow_unmerged)
1612 continue;
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001613 if (!filtered)
1614 show_file(unmerged_fmt, ce->name, in_porcelain,
1615 &first, header_msg);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001616 has_errors = 1;
1617 continue;
1618 }
1619
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001620 if (filtered)
Alexandre Julliardd6168132007-08-11 23:59:01 +02001621 continue;
1622
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001623 new_entry = refresh_cache_ent(istate, ce, options,
1624 &cache_errno, &changed,
Jeff Hostetler15268d12021-02-03 15:34:46 +00001625 &t2_did_lstat, &t2_did_scan);
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001626 t2_sum_lstat += t2_did_lstat;
Jeff Hostetler15268d12021-02-03 15:34:46 +00001627 t2_sum_scan += t2_did_scan;
Brandon Williams285c2e22018-02-14 10:59:45 -08001628 if (new_entry == ce)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001629 continue;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001630 if (progress)
1631 display_progress(progress, i);
Brandon Williams285c2e22018-02-14 10:59:45 -08001632 if (!new_entry) {
Jeff King73b7eae2011-11-18 06:13:08 -05001633 const char *fmt;
1634
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001635 if (really && cache_errno == EINVAL) {
Linus Torvalds405e5b22006-05-19 09:56:35 -07001636 /* If we are doing --really-refresh that
1637 * means the index is not valid anymore.
1638 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001639 ce->ce_flags &= ~CE_VALID;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +07001640 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -04001641 mark_fsmonitor_invalid(istate, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001642 istate->cache_changed |= CE_ENTRY_CHANGED;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001643 }
1644 if (quiet)
1645 continue;
Jeff King73b7eae2011-11-18 06:13:08 -05001646
1647 if (cache_errno == ENOENT)
1648 fmt = deleted_fmt;
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +07001649 else if (ce_intent_to_add(ce))
Jeff King73b7eae2011-11-18 06:13:08 -05001650 fmt = added_fmt; /* must be before other checks */
1651 else if (changed & TYPE_CHANGED)
1652 fmt = typechange_fmt;
1653 else
1654 fmt = modified_fmt;
1655 show_file(fmt,
1656 ce->name, in_porcelain, &first, header_msg);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001657 has_errors = 1;
1658 continue;
1659 }
Linus Torvaldscf558702008-01-22 18:41:14 -08001660
Brandon Williams285c2e22018-02-14 10:59:45 -08001661 replace_index_entry(istate, i, new_entry);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001662 }
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001663 trace2_data_intmax("index", NULL, "refresh/sum_lstat", t2_sum_lstat);
Jeff Hostetler15268d12021-02-03 15:34:46 +00001664 trace2_data_intmax("index", NULL, "refresh/sum_scan", t2_sum_scan);
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001665 trace2_region_leave("index", "refresh", NULL);
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001666 if (progress) {
1667 display_progress(progress, istate->cache_nr);
1668 stop_progress(&progress);
1669 }
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02001670 trace_performance_leave("refresh index");
Linus Torvalds405e5b22006-05-19 09:56:35 -07001671 return has_errors;
1672}
1673
Jameson Miller768d7962018-07-02 19:49:29 +00001674struct cache_entry *refresh_cache_entry(struct index_state *istate,
1675 struct cache_entry *ce,
1676 unsigned int options)
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001677{
Jeff Hostetler15268d12021-02-03 15:34:46 +00001678 return refresh_cache_ent(istate, ce, options, NULL, NULL, NULL, NULL);
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001679}
1680
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001681
1682/*****************************************************************
1683 * Index File I/O
1684 *****************************************************************/
1685
Junio C Hamano9d227782012-04-04 09:12:43 -07001686#define INDEX_FORMAT_DEFAULT 3
1687
Derrick Stolee7211b9e2019-08-13 11:37:43 -07001688static unsigned int get_index_format_default(struct repository *r)
Thomas Gummerer136347d2014-02-23 21:49:57 +01001689{
1690 char *envversion = getenv("GIT_INDEX_VERSION");
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001691 char *endp;
1692 unsigned int version = INDEX_FORMAT_DEFAULT;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001693
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001694 if (!envversion) {
Derrick Stolee7211b9e2019-08-13 11:37:43 -07001695 prepare_repo_settings(r);
1696
1697 if (r->settings.index_version >= 0)
1698 version = r->settings.index_version;
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001699 if (version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1700 warning(_("index.version set, but the value is invalid.\n"
Thomas Gummerer136347d2014-02-23 21:49:57 +01001701 "Using version %i"), INDEX_FORMAT_DEFAULT);
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001702 return INDEX_FORMAT_DEFAULT;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001703 }
1704 return version;
1705 }
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001706
1707 version = strtoul(envversion, &endp, 10);
1708 if (*endp ||
1709 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1710 warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1711 "Using version %i"), INDEX_FORMAT_DEFAULT);
1712 version = INDEX_FORMAT_DEFAULT;
1713 }
1714 return version;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001715}
1716
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001717/*
1718 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
1719 * Again - this is just a (very strong in practice) heuristic that
1720 * the inode hasn't changed.
1721 *
1722 * We save the fields in big-endian order to allow using the
1723 * index file over NFS transparently.
1724 */
1725struct ondisk_cache_entry {
1726 struct cache_time ctime;
1727 struct cache_time mtime;
Thomas Gummerer7800c1e2013-08-18 21:41:51 +02001728 uint32_t dev;
1729 uint32_t ino;
1730 uint32_t mode;
1731 uint32_t uid;
1732 uint32_t gid;
1733 uint32_t size;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001734 /*
1735 * unsigned char hash[hashsz];
1736 * uint16_t flags;
1737 * if (flags & CE_EXTENDED)
1738 * uint16_t flags2;
1739 */
1740 unsigned char data[GIT_MAX_RAWSZ + 2 * sizeof(uint16_t)];
1741 char name[FLEX_ARRAY];
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001742};
1743
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001744/* These are only used for v3 or lower */
Kevin Willfordce012de2017-08-21 15:24:32 -06001745#define align_padding_size(size, len) ((size + (len) + 8) & ~7) - (size + len)
brian m. carlson575fa8a2019-02-19 00:05:24 +00001746#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,data) + (len) + 8) & ~7)
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001747#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)
brian m. carlson575fa8a2019-02-19 00:05:24 +00001748#define ondisk_data_size(flags, len) (the_hash_algo->rawsz + \
1749 ((flags & CE_EXTENDED) ? 2 : 1) * sizeof(uint16_t) + len)
1750#define ondisk_data_size_max(len) (ondisk_data_size(CE_EXTENDED, len))
1751#define ondisk_ce_size(ce) (ondisk_cache_entry_size(ondisk_data_size((ce)->ce_flags, ce_namelen(ce))))
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001752
Jeff Hostetlera33fc722017-04-14 20:32:21 +00001753/* Allow fsck to force verification of the index checksum. */
1754int verify_index_checksum;
1755
Ben Peart00ec50e2017-10-18 10:27:25 -04001756/* Allow fsck to force verification of the cache entry order. */
1757int verify_ce_order;
1758
Ben Peart371ed0d2018-10-10 11:59:33 -04001759static int verify_hdr(const struct cache_header *hdr, unsigned long size)
Linus Torvaldse83c5162005-04-07 15:13:13 -07001760{
brian m. carlsonaab61352018-02-01 02:18:45 +00001761 git_hash_ctx c;
1762 unsigned char hash[GIT_MAX_RAWSZ];
Junio C Hamano0136bac2012-04-03 15:53:12 -07001763 int hdr_version;
Linus Torvaldse83c5162005-04-07 15:13:13 -07001764
Linus Torvaldsccc4feb2005-04-15 10:44:27 -07001765 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001766 return error(_("bad signature 0x%08x"), hdr->hdr_signature);
Junio C Hamano0136bac2012-04-03 15:53:12 -07001767 hdr_version = ntohl(hdr->hdr_version);
Nguyễn Thái Ngọc Duyb82a7b52013-02-22 19:09:24 +07001768 if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001769 return error(_("bad index version %d"), hdr_version);
Jeff Hostetlera33fc722017-04-14 20:32:21 +00001770
1771 if (!verify_index_checksum)
1772 return 0;
1773
brian m. carlsonaab61352018-02-01 02:18:45 +00001774 the_hash_algo->init_fn(&c);
1775 the_hash_algo->update_fn(&c, hdr, size - the_hash_algo->rawsz);
1776 the_hash_algo->final_fn(hash, &c);
Jeff King67947c32018-08-28 17:22:52 -04001777 if (!hasheq(hash, (unsigned char *)hdr + size - the_hash_algo->rawsz))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001778 return error(_("bad index file sha1 signature"));
Linus Torvaldse83c5162005-04-07 15:13:13 -07001779 return 0;
1780}
1781
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001782static int read_index_extension(struct index_state *istate,
Ben Peart371ed0d2018-10-10 11:59:33 -04001783 const char *ext, const char *data, unsigned long sz)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001784{
1785 switch (CACHE_EXT(ext)) {
1786 case CACHE_EXT_TREE:
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001787 istate->cache_tree = cache_tree_read(data, sz);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001788 break;
Junio C Hamanocfc57892009-12-25 00:30:51 -08001789 case CACHE_EXT_RESOLVE_UNDO:
1790 istate->resolve_undo = resolve_undo_read(data, sz);
1791 break;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001792 case CACHE_EXT_LINK:
1793 if (read_link_extension(istate, data, sz))
1794 return -1;
1795 break;
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07001796 case CACHE_EXT_UNTRACKED:
1797 istate->untracked = read_untracked_extension(data, sz);
1798 break;
Ben Peart883e2482017-09-22 12:35:40 -04001799 case CACHE_EXT_FSMONITOR:
1800 read_fsmonitor_extension(istate, data, sz);
1801 break;
Ben Peart3b1d9e02018-10-10 11:59:34 -04001802 case CACHE_EXT_ENDOFINDEXENTRIES:
Ben Peart32550892018-10-10 11:59:37 -04001803 case CACHE_EXT_INDEXENTRYOFFSETTABLE:
Ben Peart3b1d9e02018-10-10 11:59:34 -04001804 /* already handled in do_read_index() */
1805 break;
Derrick Stoleecd424152021-03-30 13:10:54 +00001806 case CACHE_EXT_SPARSE_DIRECTORIES:
1807 /* no content, only an indicator */
1808 istate->sparse_index = 1;
1809 break;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001810 default:
1811 if (*ext < 'A' || 'Z' < *ext)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001812 return error(_("index uses %.4s extension, which we do not understand"),
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001813 ext);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001814 fprintf_ln(stderr, _("ignoring %.4s extension"), ext);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001815 break;
1816 }
1817 return 0;
1818}
1819
Ben Peart77ff1122018-10-10 11:59:38 -04001820static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool,
1821 unsigned int version,
Jameson Millera8497352018-07-02 19:49:31 +00001822 struct ondisk_cache_entry *ondisk,
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001823 unsigned long *ent_size,
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001824 const struct cache_entry *previous_ce)
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001825{
René Scharfedebed2a2011-10-24 23:59:14 +02001826 struct cache_entry *ce;
Junio C Hamano7fec10b2008-01-18 23:42:00 -08001827 size_t len;
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07001828 const char *name;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001829 const unsigned hashsz = the_hash_algo->rawsz;
1830 const uint16_t *flagsp = (const uint16_t *)(ondisk->data + hashsz);
René Scharfedebed2a2011-10-24 23:59:14 +02001831 unsigned int flags;
Nguyễn Thái Ngọc Duyf5c4a9a2018-11-03 09:48:49 +01001832 size_t copy_len = 0;
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001833 /*
1834 * Adjacent cache entries tend to share the leading paths, so it makes
1835 * sense to only store the differences in later entries. In the v4
1836 * on-disk format of the index, each on-disk cache entry stores the
1837 * number of bytes to be stripped from the end of the previous name,
1838 * and the bytes to append to the result, to come up with its name.
1839 */
Ben Peart77ff1122018-10-10 11:59:38 -04001840 int expand_name_field = version == 4;
René Scharfedebed2a2011-10-24 23:59:14 +02001841
1842 /* On-disk flags are just 16 bits */
brian m. carlson575fa8a2019-02-19 00:05:24 +00001843 flags = get_be16(flagsp);
René Scharfedebed2a2011-10-24 23:59:14 +02001844 len = flags & CE_NAMEMASK;
1845
1846 if (flags & CE_EXTENDED) {
René Scharfedebed2a2011-10-24 23:59:14 +02001847 int extended_flags;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001848 extended_flags = get_be16(flagsp + 1) << 16;
René Scharfedebed2a2011-10-24 23:59:14 +02001849 /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1850 if (extended_flags & ~CE_EXTENDED_FLAGS)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001851 die(_("unknown index entry format 0x%08x"), extended_flags);
René Scharfedebed2a2011-10-24 23:59:14 +02001852 flags |= extended_flags;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001853 name = (const char *)(flagsp + 2);
René Scharfedebed2a2011-10-24 23:59:14 +02001854 }
1855 else
brian m. carlson575fa8a2019-02-19 00:05:24 +00001856 name = (const char *)(flagsp + 1);
René Scharfedebed2a2011-10-24 23:59:14 +02001857
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001858 if (expand_name_field) {
1859 const unsigned char *cp = (const unsigned char *)name;
1860 size_t strip_len, previous_len;
René Scharfedebed2a2011-10-24 23:59:14 +02001861
Elijah Newren15beaaa2019-11-05 17:07:23 +00001862 /* If we're at the beginning of a block, ignore the previous name */
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001863 strip_len = decode_varint(&cp);
Ben Peart77ff1122018-10-10 11:59:38 -04001864 if (previous_ce) {
1865 previous_len = previous_ce->ce_namelen;
1866 if (previous_len < strip_len)
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001867 die(_("malformed name field in the index, near path '%s'"),
Ben Peart77ff1122018-10-10 11:59:38 -04001868 previous_ce->name);
1869 copy_len = previous_len - strip_len;
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001870 }
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001871 name = (const char *)cp;
1872 }
1873
1874 if (len == CE_NAMEMASK) {
1875 len = strlen(name);
1876 if (expand_name_field)
1877 len += copy_len;
1878 }
1879
Ben Peart77ff1122018-10-10 11:59:38 -04001880 ce = mem_pool__ce_alloc(ce_mem_pool, len);
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001881
1882 ce->ce_stat_data.sd_ctime.sec = get_be32(&ondisk->ctime.sec);
1883 ce->ce_stat_data.sd_mtime.sec = get_be32(&ondisk->mtime.sec);
1884 ce->ce_stat_data.sd_ctime.nsec = get_be32(&ondisk->ctime.nsec);
1885 ce->ce_stat_data.sd_mtime.nsec = get_be32(&ondisk->mtime.nsec);
1886 ce->ce_stat_data.sd_dev = get_be32(&ondisk->dev);
1887 ce->ce_stat_data.sd_ino = get_be32(&ondisk->ino);
1888 ce->ce_mode = get_be32(&ondisk->mode);
1889 ce->ce_stat_data.sd_uid = get_be32(&ondisk->uid);
1890 ce->ce_stat_data.sd_gid = get_be32(&ondisk->gid);
1891 ce->ce_stat_data.sd_size = get_be32(&ondisk->size);
1892 ce->ce_flags = flags & ~CE_NAMEMASK;
1893 ce->ce_namelen = len;
1894 ce->index = 0;
brian m. carlson92e2cab2021-04-26 01:02:50 +00001895 oidread(&ce->oid, ondisk->data);
brian m. carlson575fa8a2019-02-19 00:05:24 +00001896 memcpy(ce->name, name, len);
1897 ce->name[len] = '\0';
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001898
1899 if (expand_name_field) {
1900 if (copy_len)
1901 memcpy(ce->name, previous_ce->name, copy_len);
1902 memcpy(ce->name + copy_len, name, len + 1 - copy_len);
1903 *ent_size = (name - ((char *)ondisk)) + len + 1 - copy_len;
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001904 } else {
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001905 memcpy(ce->name, name, len + 1);
1906 *ent_size = ondisk_ce_size(ce);
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001907 }
René Scharfedebed2a2011-10-24 23:59:14 +02001908 return ce;
Linus Torvaldscf558702008-01-22 18:41:14 -08001909}
1910
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001911static void check_ce_order(struct index_state *istate)
Jaime Soriano Pastor15999d02014-08-29 10:54:41 +02001912{
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001913 unsigned int i;
1914
Ben Peart00ec50e2017-10-18 10:27:25 -04001915 if (!verify_ce_order)
1916 return;
1917
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001918 for (i = 1; i < istate->cache_nr; i++) {
1919 struct cache_entry *ce = istate->cache[i - 1];
1920 struct cache_entry *next_ce = istate->cache[i];
1921 int name_compare = strcmp(ce->name, next_ce->name);
1922
1923 if (0 < name_compare)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001924 die(_("unordered stage entries in index"));
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001925 if (!name_compare) {
1926 if (!ce_stage(ce))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001927 die(_("multiple stage entries for merged file '%s'"),
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001928 ce->name);
1929 if (ce_stage(ce) > ce_stage(next_ce))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001930 die(_("unordered stage entries for '%s'"),
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001931 ce->name);
1932 }
Jaime Soriano Pastor15999d02014-08-29 10:54:41 +02001933 }
1934}
1935
Christian Couder435ec092016-01-27 07:58:05 +01001936static void tweak_untracked_cache(struct index_state *istate)
1937{
Derrick Stoleead0fb652019-08-13 11:37:46 -07001938 struct repository *r = the_repository;
1939
1940 prepare_repo_settings(r);
1941
1942 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE) {
Christian Couder435ec092016-01-27 07:58:05 +01001943 remove_untracked_cache(istate);
Derrick Stoleead0fb652019-08-13 11:37:46 -07001944 return;
Christian Couder435ec092016-01-27 07:58:05 +01001945 }
Derrick Stoleead0fb652019-08-13 11:37:46 -07001946
1947 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
1948 add_untracked_cache(istate);
Christian Couder435ec092016-01-27 07:58:05 +01001949}
1950
Christian Couder43925312017-02-27 19:00:02 +01001951static void tweak_split_index(struct index_state *istate)
1952{
1953 switch (git_config_get_split_index()) {
1954 case -1: /* unset: do nothing */
1955 break;
1956 case 0: /* false */
1957 remove_split_index(istate);
1958 break;
1959 case 1: /* true */
1960 add_split_index(istate);
1961 break;
1962 default: /* unknown value: do nothing */
1963 break;
1964 }
1965}
1966
Christian Couder435ec092016-01-27 07:58:05 +01001967static void post_read_index_from(struct index_state *istate)
1968{
1969 check_ce_order(istate);
1970 tweak_untracked_cache(istate);
Christian Couder43925312017-02-27 19:00:02 +01001971 tweak_split_index(istate);
Ben Peart883e2482017-09-22 12:35:40 -04001972 tweak_fsmonitor(istate);
Christian Couder435ec092016-01-27 07:58:05 +01001973}
1974
Jameson Miller8e72d672018-07-02 19:49:37 +00001975static size_t estimate_cache_size_from_compressed(unsigned int entries)
1976{
1977 return entries * (sizeof(struct cache_entry) + CACHE_ENTRY_PATH_LENGTH);
1978}
1979
1980static size_t estimate_cache_size(size_t ondisk_size, unsigned int entries)
1981{
1982 long per_entry = sizeof(struct cache_entry) - sizeof(struct ondisk_cache_entry);
1983
1984 /*
1985 * Account for potential alignment differences.
1986 */
Johannes Schindelinc097b952019-10-04 08:09:26 -07001987 per_entry += align_padding_size(per_entry, 0);
Jameson Miller8e72d672018-07-02 19:49:37 +00001988 return ondisk_size + entries * per_entry;
1989}
1990
Ben Peart32550892018-10-10 11:59:37 -04001991struct index_entry_offset
1992{
1993 /* starting byte offset into index file, count of index entries in this block */
1994 int offset, nr;
1995};
1996
1997struct index_entry_offset_table
1998{
1999 int nr;
2000 struct index_entry_offset entries[FLEX_ARRAY];
2001};
2002
Ben Peart32550892018-10-10 11:59:37 -04002003static struct index_entry_offset_table *read_ieot_extension(const char *mmap, size_t mmap_size, size_t offset);
2004static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot);
Ben Peart32550892018-10-10 11:59:37 -04002005
Ben Peart3b1d9e02018-10-10 11:59:34 -04002006static size_t read_eoie_extension(const char *mmap, size_t mmap_size);
2007static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset);
2008
Ben Peartabb4bb82018-10-10 11:59:36 -04002009struct load_index_extensions
2010{
Ben Peartabb4bb82018-10-10 11:59:36 -04002011 pthread_t pthread;
Ben Peartabb4bb82018-10-10 11:59:36 -04002012 struct index_state *istate;
2013 const char *mmap;
2014 size_t mmap_size;
2015 unsigned long src_offset;
2016};
2017
2018static void *load_index_extensions(void *_data)
2019{
2020 struct load_index_extensions *p = _data;
2021 unsigned long src_offset = p->src_offset;
2022
2023 while (src_offset <= p->mmap_size - the_hash_algo->rawsz - 8) {
2024 /* After an array of active_nr index entries,
2025 * there can be arbitrary number of extended
2026 * sections, each of which is prefixed with
2027 * extension name (4-byte) and section length
2028 * in 4-byte network byte order.
2029 */
2030 uint32_t extsize = get_be32(p->mmap + src_offset + 4);
2031 if (read_index_extension(p->istate,
2032 p->mmap + src_offset,
2033 p->mmap + src_offset + 8,
2034 extsize) < 0) {
2035 munmap((void *)p->mmap, p->mmap_size);
2036 die(_("index file corrupt"));
2037 }
2038 src_offset += 8;
2039 src_offset += extsize;
2040 }
2041
2042 return NULL;
2043}
2044
Ben Peart32550892018-10-10 11:59:37 -04002045/*
Ben Peart77ff1122018-10-10 11:59:38 -04002046 * A helper function that will load the specified range of cache entries
2047 * from the memory mapped file and add them to the given index.
2048 */
2049static unsigned long load_cache_entry_block(struct index_state *istate,
2050 struct mem_pool *ce_mem_pool, int offset, int nr, const char *mmap,
2051 unsigned long start_offset, const struct cache_entry *previous_ce)
2052{
2053 int i;
2054 unsigned long src_offset = start_offset;
2055
2056 for (i = offset; i < offset + nr; i++) {
2057 struct ondisk_cache_entry *disk_ce;
2058 struct cache_entry *ce;
2059 unsigned long consumed;
2060
2061 disk_ce = (struct ondisk_cache_entry *)(mmap + src_offset);
2062 ce = create_from_disk(ce_mem_pool, istate->version, disk_ce, &consumed, previous_ce);
2063 set_index_entry(istate, i, ce);
2064
2065 src_offset += consumed;
2066 previous_ce = ce;
2067 }
2068 return src_offset - start_offset;
2069}
2070
2071static unsigned long load_all_cache_entries(struct index_state *istate,
2072 const char *mmap, size_t mmap_size, unsigned long src_offset)
2073{
2074 unsigned long consumed;
2075
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002076 istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
Ben Peart77ff1122018-10-10 11:59:38 -04002077 if (istate->version == 4) {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002078 mem_pool_init(istate->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002079 estimate_cache_size_from_compressed(istate->cache_nr));
2080 } else {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002081 mem_pool_init(istate->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002082 estimate_cache_size(mmap_size, istate->cache_nr));
2083 }
2084
2085 consumed = load_cache_entry_block(istate, istate->ce_mem_pool,
2086 0, istate->cache_nr, mmap, src_offset, NULL);
2087 return consumed;
2088}
2089
Ben Peart77ff1122018-10-10 11:59:38 -04002090/*
Ben Peart32550892018-10-10 11:59:37 -04002091 * Mostly randomly chosen maximum thread counts: we
2092 * cap the parallelism to online_cpus() threads, and we want
2093 * to have at least 10000 cache entries per thread for it to
2094 * be worth starting a thread.
2095 */
2096
2097#define THREAD_COST (10000)
2098
Ben Peart77ff1122018-10-10 11:59:38 -04002099struct load_cache_entries_thread_data
2100{
2101 pthread_t pthread;
2102 struct index_state *istate;
2103 struct mem_pool *ce_mem_pool;
2104 int offset;
2105 const char *mmap;
2106 struct index_entry_offset_table *ieot;
2107 int ieot_start; /* starting index into the ieot array */
2108 int ieot_blocks; /* count of ieot entries to process */
2109 unsigned long consumed; /* return # of bytes in index file processed */
2110};
2111
2112/*
2113 * A thread proc to run the load_cache_entries() computation
2114 * across multiple background threads.
2115 */
2116static void *load_cache_entries_thread(void *_data)
2117{
2118 struct load_cache_entries_thread_data *p = _data;
2119 int i;
2120
2121 /* iterate across all ieot blocks assigned to this thread */
2122 for (i = p->ieot_start; i < p->ieot_start + p->ieot_blocks; i++) {
2123 p->consumed += load_cache_entry_block(p->istate, p->ce_mem_pool,
2124 p->offset, p->ieot->entries[i].nr, p->mmap, p->ieot->entries[i].offset, NULL);
2125 p->offset += p->ieot->entries[i].nr;
2126 }
2127 return NULL;
2128}
2129
2130static unsigned long load_cache_entries_threaded(struct index_state *istate, const char *mmap, size_t mmap_size,
Jeff King7bd96312019-05-09 17:29:44 -04002131 int nr_threads, struct index_entry_offset_table *ieot)
Ben Peart77ff1122018-10-10 11:59:38 -04002132{
2133 int i, offset, ieot_blocks, ieot_start, err;
2134 struct load_cache_entries_thread_data *data;
2135 unsigned long consumed = 0;
2136
2137 /* a little sanity checking */
2138 if (istate->name_hash_initialized)
2139 BUG("the name hash isn't thread safe");
2140
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002141 istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
2142 mem_pool_init(istate->ce_mem_pool, 0);
Ben Peart77ff1122018-10-10 11:59:38 -04002143
2144 /* ensure we have no more threads than we have blocks to process */
2145 if (nr_threads > ieot->nr)
2146 nr_threads = ieot->nr;
René Scharfeca56dad2021-03-13 17:17:22 +01002147 CALLOC_ARRAY(data, nr_threads);
Ben Peart77ff1122018-10-10 11:59:38 -04002148
2149 offset = ieot_start = 0;
2150 ieot_blocks = DIV_ROUND_UP(ieot->nr, nr_threads);
2151 for (i = 0; i < nr_threads; i++) {
2152 struct load_cache_entries_thread_data *p = &data[i];
2153 int nr, j;
2154
2155 if (ieot_start + ieot_blocks > ieot->nr)
2156 ieot_blocks = ieot->nr - ieot_start;
2157
2158 p->istate = istate;
2159 p->offset = offset;
2160 p->mmap = mmap;
2161 p->ieot = ieot;
2162 p->ieot_start = ieot_start;
2163 p->ieot_blocks = ieot_blocks;
2164
2165 /* create a mem_pool for each thread */
2166 nr = 0;
2167 for (j = p->ieot_start; j < p->ieot_start + p->ieot_blocks; j++)
2168 nr += p->ieot->entries[j].nr;
René Scharfebcd2c5e2020-09-04 19:33:55 +02002169 p->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
Ben Peart77ff1122018-10-10 11:59:38 -04002170 if (istate->version == 4) {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002171 mem_pool_init(p->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002172 estimate_cache_size_from_compressed(nr));
2173 } else {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002174 mem_pool_init(p->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002175 estimate_cache_size(mmap_size, nr));
2176 }
2177
2178 err = pthread_create(&p->pthread, NULL, load_cache_entries_thread, p);
2179 if (err)
2180 die(_("unable to create load_cache_entries thread: %s"), strerror(err));
2181
2182 /* increment by the number of cache entries in the ieot block being processed */
2183 for (j = 0; j < ieot_blocks; j++)
2184 offset += ieot->entries[ieot_start + j].nr;
2185 ieot_start += ieot_blocks;
2186 }
2187
2188 for (i = 0; i < nr_threads; i++) {
2189 struct load_cache_entries_thread_data *p = &data[i];
2190
2191 err = pthread_join(p->pthread, NULL);
2192 if (err)
2193 die(_("unable to join load_cache_entries thread: %s"), strerror(err));
2194 mem_pool_combine(istate->ce_mem_pool, p->ce_mem_pool);
2195 consumed += p->consumed;
2196 }
2197
2198 free(data);
2199
2200 return consumed;
2201}
Ben Peart77ff1122018-10-10 11:59:38 -04002202
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07002203/* remember to discard_cache() before reading a different cache! */
Nguyễn Thái Ngọc Duy3e52f702014-06-13 19:19:51 +07002204int do_read_index(struct index_state *istate, const char *path, int must_exist)
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07002205{
Ben Peart77ff1122018-10-10 11:59:38 -04002206 int fd;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002207 struct stat st;
René Scharfedebed2a2011-10-24 23:59:14 +02002208 unsigned long src_offset;
Ben Peart371ed0d2018-10-10 11:59:33 -04002209 const struct cache_header *hdr;
2210 const char *mmap;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002211 size_t mmap_size;
Ben Peartabb4bb82018-10-10 11:59:36 -04002212 struct load_index_extensions p;
2213 size_t extension_offset = 0;
Ben Peart77ff1122018-10-10 11:59:38 -04002214 int nr_threads, cpus;
2215 struct index_entry_offset_table *ieot = NULL;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002216
Junio C Hamano913e0e92008-08-23 12:57:30 -07002217 if (istate->initialized)
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002218 return istate->cache_nr;
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002219
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002220 istate->timestamp.sec = 0;
2221 istate->timestamp.nsec = 0;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07002222 fd = open(path, O_RDONLY);
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002223 if (fd < 0) {
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002224 if (!must_exist && errno == ENOENT)
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002225 return 0;
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002226 die_errno(_("%s: index file open failed"), path);
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002227 }
Linus Torvaldse83c5162005-04-07 15:13:13 -07002228
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03002229 if (fstat(fd, &st))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002230 die_errno(_("%s: cannot stat the open index"), path);
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03002231
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002232 mmap_size = xsize_t(st.st_size);
brian m. carlsonaab61352018-02-01 02:18:45 +00002233 if (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002234 die(_("%s: index file smaller than expected"), path);
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03002235
Varun Naik02638d12019-07-13 20:01:53 -07002236 mmap = xmmap_gently(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002237 if (mmap == MAP_FAILED)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002238 die_errno(_("%s: unable to map index file"), path);
Nguyễn Thái Ngọc Duy57d84f82012-08-06 18:27:09 +07002239 close(fd);
Linus Torvaldse83c5162005-04-07 15:13:13 -07002240
Ben Peart371ed0d2018-10-10 11:59:33 -04002241 hdr = (const struct cache_header *)mmap;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002242 if (verify_hdr(hdr, mmap_size) < 0)
Linus Torvaldse83c5162005-04-07 15:13:13 -07002243 goto unmap;
2244
brian m. carlson92e2cab2021-04-26 01:02:50 +00002245 oidread(&istate->oid, (const unsigned char *)hdr + mmap_size - the_hash_algo->rawsz);
Junio C Hamano9d227782012-04-04 09:12:43 -07002246 istate->version = ntohl(hdr->hdr_version);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002247 istate->cache_nr = ntohl(hdr->hdr_entries);
2248 istate->cache_alloc = alloc_nr(istate->cache_nr);
René Scharfeca56dad2021-03-13 17:17:22 +01002249 CALLOC_ARRAY(istate->cache, istate->cache_alloc);
Junio C Hamano913e0e92008-08-23 12:57:30 -07002250 istate->initialized = 1;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002251
Ben Peartabb4bb82018-10-10 11:59:36 -04002252 p.istate = istate;
2253 p.mmap = mmap;
2254 p.mmap_size = mmap_size;
Junio C Hamano6c9cd162012-04-03 15:53:15 -07002255
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002256 src_offset = sizeof(*hdr);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002257
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002258 if (git_config_get_index_threads(&nr_threads))
2259 nr_threads = 1;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002260
Ben Peart77ff1122018-10-10 11:59:38 -04002261 /* TODO: does creating more threads than cores help? */
2262 if (!nr_threads) {
2263 nr_threads = istate->cache_nr / THREAD_COST;
2264 cpus = online_cpus();
2265 if (nr_threads > cpus)
2266 nr_threads = cpus;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002267 }
Ben Peartabb4bb82018-10-10 11:59:36 -04002268
Nguyễn Thái Ngọc Duy88168b92018-11-03 09:48:48 +01002269 if (!HAVE_THREADS)
2270 nr_threads = 1;
2271
Ben Peartabb4bb82018-10-10 11:59:36 -04002272 if (nr_threads > 1) {
2273 extension_offset = read_eoie_extension(mmap, mmap_size);
2274 if (extension_offset) {
2275 int err;
2276
2277 p.src_offset = extension_offset;
2278 err = pthread_create(&p.pthread, NULL, load_index_extensions, &p);
2279 if (err)
2280 die(_("unable to create load_index_extensions thread: %s"), strerror(err));
2281
2282 nr_threads--;
2283 }
2284 }
Ben Peart77ff1122018-10-10 11:59:38 -04002285
2286 /*
2287 * Locate and read the index entry offset table so that we can use it
2288 * to multi-thread the reading of the cache entries.
2289 */
2290 if (extension_offset && nr_threads > 1)
2291 ieot = read_ieot_extension(mmap, mmap_size, extension_offset);
2292
2293 if (ieot) {
Jeff King7bd96312019-05-09 17:29:44 -04002294 src_offset += load_cache_entries_threaded(istate, mmap, mmap_size, nr_threads, ieot);
Ben Peart77ff1122018-10-10 11:59:38 -04002295 free(ieot);
2296 } else {
2297 src_offset += load_all_cache_entries(istate, mmap, mmap_size, src_offset);
2298 }
Ben Peartabb4bb82018-10-10 11:59:36 -04002299
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002300 istate->timestamp.sec = st.st_mtime;
Kjetil Barvikc06ff492009-03-04 18:47:40 +01002301 istate->timestamp.nsec = ST_MTIME_NSEC(st);
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002302
Ben Peartabb4bb82018-10-10 11:59:36 -04002303 /* if we created a thread, join it otherwise load the extensions on the primary thread */
Ben Peartabb4bb82018-10-10 11:59:36 -04002304 if (extension_offset) {
2305 int ret = pthread_join(p.pthread, NULL);
2306 if (ret)
2307 die(_("unable to join load_index_extensions thread: %s"), strerror(ret));
Nguyễn Thái Ngọc Duy88168b92018-11-03 09:48:48 +01002308 } else {
Ben Peartabb4bb82018-10-10 11:59:36 -04002309 p.src_offset = src_offset;
2310 load_index_extensions(&p);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002311 }
Ben Peart371ed0d2018-10-10 11:59:33 -04002312 munmap((void *)mmap, mmap_size);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002313
2314 /*
2315 * TODO trace2: replace "the_repository" with the actual repo instance
2316 * that is associated with the given "istate".
2317 */
2318 trace2_data_intmax("index", the_repository, "read/version",
2319 istate->version);
2320 trace2_data_intmax("index", the_repository, "read/cache_nr",
2321 istate->cache_nr);
2322
Derrick Stolee4300f842021-03-30 13:10:48 +00002323 if (!istate->repo)
2324 istate->repo = the_repository;
2325 prepare_repo_settings(istate->repo);
2326 if (istate->repo->settings.command_requires_full_index)
2327 ensure_full_index(istate);
2328
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002329 return istate->cache_nr;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002330
2331unmap:
Ben Peart371ed0d2018-10-10 11:59:33 -04002332 munmap((void *)mmap, mmap_size);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002333 die(_("index file corrupt"));
Linus Torvaldse83c5162005-04-07 15:13:13 -07002334}
2335
Christian Couder0d59ffb2017-02-27 19:00:12 +01002336/*
2337 * Signal that the shared index is used by updating its mtime.
2338 *
2339 * This way, shared index can be removed if they have not been used
2340 * for some time.
2341 */
Thomas Gummerera125a222018-01-07 22:30:13 +00002342static void freshen_shared_index(const char *shared_index, int warn)
Christian Couder0d59ffb2017-02-27 19:00:12 +01002343{
Christian Couder0d59ffb2017-02-27 19:00:12 +01002344 if (!check_and_freshen_file(shared_index, 1) && warn)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002345 warning(_("could not freshen shared index '%s'"), shared_index);
Christian Couder0d59ffb2017-02-27 19:00:12 +01002346}
2347
Thomas Gummerera125a222018-01-07 22:30:13 +00002348int read_index_from(struct index_state *istate, const char *path,
2349 const char *gitdir)
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002350{
2351 struct split_index *split_index;
2352 int ret;
brian m. carlson2182abd2018-05-02 00:25:43 +00002353 char *base_oid_hex;
Thomas Gummerera125a222018-01-07 22:30:13 +00002354 char *base_path;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002355
2356 /* istate->initialized covers both .git/index and .git/sharedindex.xxx */
2357 if (istate->initialized)
2358 return istate->cache_nr;
2359
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002360 /*
2361 * TODO trace2: replace "the_repository" with the actual repo instance
2362 * that is associated with the given "istate".
2363 */
2364 trace2_region_enter_printf("index", "do_read_index", the_repository,
2365 "%s", path);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002366 trace_performance_enter();
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002367 ret = do_read_index(istate, path, 0);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002368 trace_performance_leave("read cache %s", path);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002369 trace2_region_leave_printf("index", "do_read_index", the_repository,
2370 "%s", path);
Christian Couder435ec092016-01-27 07:58:05 +01002371
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002372 split_index = istate->split_index;
brian m. carlson2182abd2018-05-02 00:25:43 +00002373 if (!split_index || is_null_oid(&split_index->base_oid)) {
Christian Couder435ec092016-01-27 07:58:05 +01002374 post_read_index_from(istate);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002375 return ret;
Thomas Gummerer03f15a72015-03-20 22:43:14 +01002376 }
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002377
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002378 trace_performance_enter();
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002379 if (split_index->base)
2380 discard_index(split_index->base);
2381 else
René Scharfeca56dad2021-03-13 17:17:22 +01002382 CALLOC_ARRAY(split_index->base, 1);
Christian Couderde6ae5f2017-03-06 10:42:00 +01002383
brian m. carlson2182abd2018-05-02 00:25:43 +00002384 base_oid_hex = oid_to_hex(&split_index->base_oid);
2385 base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002386 trace2_region_enter_printf("index", "shared/do_read_index",
2387 the_repository, "%s", base_path);
Christian Couderde6ae5f2017-03-06 10:42:00 +01002388 ret = do_read_index(split_index->base, base_path, 1);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002389 trace2_region_leave_printf("index", "shared/do_read_index",
2390 the_repository, "%s", base_path);
Jeff King9001dc22018-08-28 17:22:48 -04002391 if (!oideq(&split_index->base_oid, &split_index->base->oid))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002392 die(_("broken index, expect %s in %s, got %s"),
brian m. carlson2182abd2018-05-02 00:25:43 +00002393 base_oid_hex, base_path,
brian m. carlson75691ea2018-05-02 00:25:44 +00002394 oid_to_hex(&split_index->base->oid));
Christian Couderde6ae5f2017-03-06 10:42:00 +01002395
Thomas Gummerera125a222018-01-07 22:30:13 +00002396 freshen_shared_index(base_path, 0);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002397 merge_base_index(istate);
Christian Couder435ec092016-01-27 07:58:05 +01002398 post_read_index_from(istate);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002399 trace_performance_leave("read cache %s", base_path);
Carlo Marcelo Arenas Belónb42ad7d2018-10-20 00:33:34 -07002400 free(base_path);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002401 return ret;
2402}
2403
Junio C Hamanofa7b3c22008-11-12 11:52:35 -08002404int is_index_unborn(struct index_state *istate)
2405{
René Scharfedebed2a2011-10-24 23:59:14 +02002406 return (!istate->cache_nr && !istate->timestamp.sec);
Junio C Hamanofa7b3c22008-11-12 11:52:35 -08002407}
2408
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002409int discard_index(struct index_state *istate)
Johannes Schindelin6d297f82006-07-08 18:42:41 +02002410{
Jameson Miller8e72d672018-07-02 19:49:37 +00002411 /*
2412 * Cache entries in istate->cache[] should have been allocated
2413 * from the memory pool associated with this index, or from an
2414 * associated split_index. There is no need to free individual
Jameson Miller8616a2d2018-07-02 19:49:39 +00002415 * cache entries. validate_cache_entries can detect when this
2416 * assertion does not hold.
Jameson Miller8e72d672018-07-02 19:49:37 +00002417 */
Jameson Miller8616a2d2018-07-02 19:49:39 +00002418 validate_cache_entries(istate);
René Scharfedebed2a2011-10-24 23:59:14 +02002419
Junio C Hamanocfc57892009-12-25 00:30:51 -08002420 resolve_undo_clear_index(istate);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002421 istate->cache_nr = 0;
2422 istate->cache_changed = 0;
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002423 istate->timestamp.sec = 0;
2424 istate->timestamp.nsec = 0;
Karsten Blees20926782013-02-28 00:57:48 +01002425 free_name_hash(istate);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002426 cache_tree_free(&(istate->cache_tree));
Junio C Hamano913e0e92008-08-23 12:57:30 -07002427 istate->initialized = 0;
Johannes Schindelin398a3b02019-05-07 13:10:21 +02002428 istate->fsmonitor_has_run_once = 0;
Johannes Schindelin4abc5782021-03-17 15:30:49 +00002429 FREE_AND_NULL(istate->fsmonitor_last_update);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002430 FREE_AND_NULL(istate->cache);
René Scharfea0fc4db2013-06-09 19:39:18 +02002431 istate->cache_alloc = 0;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002432 discard_split_index(istate);
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002433 free_untracked_cache(istate->untracked);
2434 istate->untracked = NULL;
Jameson Miller8e72d672018-07-02 19:49:37 +00002435
2436 if (istate->ce_mem_pool) {
Jameson Miller8616a2d2018-07-02 19:49:39 +00002437 mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries());
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002438 FREE_AND_NULL(istate->ce_mem_pool);
Jameson Miller8e72d672018-07-02 19:49:37 +00002439 }
2440
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002441 return 0;
Johannes Schindelin6d297f82006-07-08 18:42:41 +02002442}
2443
Jameson Miller8616a2d2018-07-02 19:49:39 +00002444/*
2445 * Validate the cache entries of this index.
2446 * All cache entries associated with this index
2447 * should have been allocated by the memory pool
2448 * associated with this index, or by a referenced
2449 * split index.
2450 */
2451void validate_cache_entries(const struct index_state *istate)
2452{
2453 int i;
2454
2455 if (!should_validate_cache_entries() ||!istate || !istate->initialized)
2456 return;
2457
2458 for (i = 0; i < istate->cache_nr; i++) {
2459 if (!istate) {
Nguyễn Thái Ngọc Duy391408e2018-11-10 06:16:04 +01002460 BUG("cache entry is not allocated from expected memory pool");
Jameson Miller8616a2d2018-07-02 19:49:39 +00002461 } else if (!istate->ce_mem_pool ||
2462 !mem_pool_contains(istate->ce_mem_pool, istate->cache[i])) {
2463 if (!istate->split_index ||
2464 !istate->split_index->base ||
2465 !istate->split_index->base->ce_mem_pool ||
2466 !mem_pool_contains(istate->split_index->base->ce_mem_pool, istate->cache[i])) {
Nguyễn Thái Ngọc Duy391408e2018-11-10 06:16:04 +01002467 BUG("cache entry is not allocated from expected memory pool");
Jameson Miller8616a2d2018-07-02 19:49:39 +00002468 }
2469 }
2470 }
2471
2472 if (istate->split_index)
2473 validate_cache_entries(istate->split_index->base);
2474}
2475
Linus Torvaldsd1f128b2008-03-06 12:46:09 -08002476int unmerged_index(const struct index_state *istate)
Daniel Barkalow94a57282008-02-07 11:40:13 -05002477{
2478 int i;
2479 for (i = 0; i < istate->cache_nr; i++) {
2480 if (ce_stage(istate->cache[i]))
2481 return 1;
2482 }
2483 return 0;
2484}
2485
Nguyễn Thái Ngọc Duy150fe062019-01-12 09:13:31 +07002486int repo_index_has_changes(struct repository *repo,
2487 struct tree *tree,
2488 struct strbuf *sb)
Elijah Newrencffbfad2018-06-30 18:24:55 -07002489{
Nguyễn Thái Ngọc Duy150fe062019-01-12 09:13:31 +07002490 struct index_state *istate = repo->index;
Elijah Newrene1f86942018-06-30 18:25:00 -07002491 struct object_id cmp;
Elijah Newrencffbfad2018-06-30 18:24:55 -07002492 int i;
2493
Elijah Newrene1f86942018-06-30 18:25:00 -07002494 if (tree)
2495 cmp = tree->object.oid;
2496 if (tree || !get_oid_tree("HEAD", &cmp)) {
Elijah Newrencffbfad2018-06-30 18:24:55 -07002497 struct diff_options opt;
2498
Nguyễn Thái Ngọc Duy150fe062019-01-12 09:13:31 +07002499 repo_diff_setup(repo, &opt);
Elijah Newrencffbfad2018-06-30 18:24:55 -07002500 opt.flags.exit_with_status = 1;
2501 if (!sb)
2502 opt.flags.quick = 1;
Elijah Newrene1f86942018-06-30 18:25:00 -07002503 do_diff_cache(&cmp, &opt);
Elijah Newrencffbfad2018-06-30 18:24:55 -07002504 diffcore_std(&opt);
2505 for (i = 0; sb && i < diff_queued_diff.nr; i++) {
2506 if (i)
2507 strbuf_addch(sb, ' ');
2508 strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
2509 }
2510 diff_flush(&opt);
2511 return opt.flags.has_changes != 0;
2512 } else {
Derrick Stolee0c18c052021-04-01 01:49:58 +00002513 /* TODO: audit for interaction with sparse-index. */
2514 ensure_full_index(istate);
Elijah Newren1b9fbef2018-06-30 18:24:56 -07002515 for (i = 0; sb && i < istate->cache_nr; i++) {
Elijah Newrencffbfad2018-06-30 18:24:55 -07002516 if (i)
2517 strbuf_addch(sb, ' ');
Elijah Newren1b9fbef2018-06-30 18:24:56 -07002518 strbuf_addstr(sb, istate->cache[i]->name);
Elijah Newrencffbfad2018-06-30 18:24:55 -07002519 }
Elijah Newren1b9fbef2018-06-30 18:24:56 -07002520 return !!istate->cache_nr;
Elijah Newrencffbfad2018-06-30 18:24:55 -07002521 }
2522}
2523
Neeraj Singhf2798942021-02-18 02:48:26 +00002524#define WRITE_BUFFER_SIZE (128 * 1024)
Brian Gerstbf0f9102005-05-18 08:14:09 -04002525static unsigned char write_buffer[WRITE_BUFFER_SIZE];
Linus Torvalds4990aad2005-04-20 12:16:57 -07002526static unsigned long write_buffer_len;
2527
brian m. carlsonaab61352018-02-01 02:18:45 +00002528static int ce_write_flush(git_hash_ctx *context, int fd)
Junio C Hamano6015c282006-08-08 14:47:32 -07002529{
2530 unsigned int buffered = write_buffer_len;
2531 if (buffered) {
brian m. carlsonaab61352018-02-01 02:18:45 +00002532 the_hash_algo->update_fn(context, write_buffer, buffered);
Jeff King06f46f22017-09-13 13:16:03 -04002533 if (write_in_full(fd, write_buffer, buffered) < 0)
Junio C Hamano6015c282006-08-08 14:47:32 -07002534 return -1;
2535 write_buffer_len = 0;
2536 }
2537 return 0;
2538}
2539
brian m. carlsonaab61352018-02-01 02:18:45 +00002540static int ce_write(git_hash_ctx *context, int fd, void *data, unsigned int len)
Linus Torvalds4990aad2005-04-20 12:16:57 -07002541{
2542 while (len) {
2543 unsigned int buffered = write_buffer_len;
2544 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
2545 if (partial > len)
2546 partial = len;
2547 memcpy(write_buffer + buffered, data, partial);
2548 buffered += partial;
2549 if (buffered == WRITE_BUFFER_SIZE) {
Junio C Hamano6015c282006-08-08 14:47:32 -07002550 write_buffer_len = buffered;
2551 if (ce_write_flush(context, fd))
Linus Torvalds4990aad2005-04-20 12:16:57 -07002552 return -1;
2553 buffered = 0;
2554 }
2555 write_buffer_len = buffered;
2556 len -= partial;
Florian Forster1d7f1712006-06-18 17:18:09 +02002557 data = (char *) data + partial;
Junio C Hamanoa6080a02007-06-07 00:04:01 -07002558 }
2559 return 0;
Linus Torvalds4990aad2005-04-20 12:16:57 -07002560}
2561
Ben Peart3b1d9e02018-10-10 11:59:34 -04002562static int write_index_ext_header(git_hash_ctx *context, git_hash_ctx *eoie_context,
2563 int fd, unsigned int ext, unsigned int sz)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002564{
2565 ext = htonl(ext);
2566 sz = htonl(sz);
Ben Peart3b1d9e02018-10-10 11:59:34 -04002567 if (eoie_context) {
2568 the_hash_algo->update_fn(eoie_context, &ext, 4);
2569 the_hash_algo->update_fn(eoie_context, &sz, 4);
2570 }
David Rientjes968a1d62006-08-14 13:38:14 -07002571 return ((ce_write(context, fd, &ext, 4) < 0) ||
2572 (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002573}
2574
brian m. carlsonaab61352018-02-01 02:18:45 +00002575static int ce_flush(git_hash_ctx *context, int fd, unsigned char *hash)
Linus Torvalds4990aad2005-04-20 12:16:57 -07002576{
2577 unsigned int left = write_buffer_len;
Linus Torvaldsca9be052005-04-20 12:36:41 -07002578
Linus Torvalds4990aad2005-04-20 12:16:57 -07002579 if (left) {
2580 write_buffer_len = 0;
brian m. carlsonaab61352018-02-01 02:18:45 +00002581 the_hash_algo->update_fn(context, write_buffer, left);
Linus Torvalds4990aad2005-04-20 12:16:57 -07002582 }
Linus Torvaldsca9be052005-04-20 12:36:41 -07002583
brian m. carlsonaab61352018-02-01 02:18:45 +00002584 /* Flush first if not enough space for hash signature */
2585 if (left + the_hash_algo->rawsz > WRITE_BUFFER_SIZE) {
Jeff King06f46f22017-09-13 13:16:03 -04002586 if (write_in_full(fd, write_buffer, left) < 0)
Qingning Huo2c865d92005-09-11 14:27:47 +01002587 return -1;
2588 left = 0;
2589 }
2590
brian m. carlsonaab61352018-02-01 02:18:45 +00002591 /* Append the hash signature at the end */
2592 the_hash_algo->final_fn(write_buffer + left, context);
2593 hashcpy(hash, write_buffer + left);
2594 left += the_hash_algo->rawsz;
Jeff King06f46f22017-09-13 13:16:03 -04002595 return (write_in_full(fd, write_buffer, left) < 0) ? -1 : 0;
Linus Torvalds4990aad2005-04-20 12:16:57 -07002596}
2597
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002598static void ce_smudge_racily_clean_entry(struct index_state *istate,
2599 struct cache_entry *ce)
Junio C Hamano407c8eb2005-12-20 12:12:18 -08002600{
2601 /*
2602 * The only thing we care about in this function is to smudge the
2603 * falsely clean entry due to touch-update-touch race, so we leave
2604 * everything else as they are. We are called for entries whose
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002605 * ce_stat_data.sd_mtime match the index file mtime.
Junio C Hamanoc70115b2008-07-29 01:13:44 -07002606 *
2607 * Note that this actually does not do much for gitlinks, for
2608 * which ce_match_stat_basic() always goes to the actual
2609 * contents. The caller checks with is_racy_timestamp() which
2610 * always says "no" for gitlinks, so we are not called for them ;-)
Junio C Hamano407c8eb2005-12-20 12:12:18 -08002611 */
2612 struct stat st;
2613
2614 if (lstat(ce->name, &st) < 0)
2615 return;
2616 if (ce_match_stat_basic(ce, &st))
2617 return;
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002618 if (ce_modified_check_fs(istate, ce, &st)) {
Junio C Hamano4b3511b2005-12-20 14:18:47 -08002619 /* This is "racily clean"; smudge it. Note that this
2620 * is a tricky code. At first glance, it may appear
2621 * that it can break with this sequence:
2622 *
2623 * $ echo xyzzy >frotz
2624 * $ git-update-index --add frotz
2625 * $ : >frotz
2626 * $ sleep 3
2627 * $ echo filfre >nitfol
2628 * $ git-update-index --add nitfol
2629 *
Junio C Hamanob7e58b12006-08-05 04:16:02 -07002630 * but it does not. When the second update-index runs,
Junio C Hamano4b3511b2005-12-20 14:18:47 -08002631 * it notices that the entry "frotz" has the same timestamp
2632 * as index, and if we were to smudge it by resetting its
2633 * size to zero here, then the object name recorded
2634 * in index is the 6-byte file but the cached stat information
2635 * becomes zero --- which would then match what we would
Junio C Hamanoa6080a02007-06-07 00:04:01 -07002636 * obtain from the filesystem next time we stat("frotz").
Junio C Hamano4b3511b2005-12-20 14:18:47 -08002637 *
2638 * However, the second update-index, before calling
2639 * this function, notices that the cached size is 6
2640 * bytes and what is on the filesystem is an empty
2641 * file, and never calls us, so the cached size information
2642 * for "frotz" stays 6 which does not match the filesystem.
2643 */
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002644 ce->ce_stat_data.sd_size = 0;
Junio C Hamano407c8eb2005-12-20 12:12:18 -08002645 }
2646}
2647
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002648/* Copy miscellaneous fields but not the name */
Kevin Willfordce012de2017-08-21 15:24:32 -06002649static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002650 struct cache_entry *ce)
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002651{
Thomas Gummererb60e1882012-07-11 11:22:37 +02002652 short flags;
brian m. carlson575fa8a2019-02-19 00:05:24 +00002653 const unsigned hashsz = the_hash_algo->rawsz;
2654 uint16_t *flagsp = (uint16_t *)(ondisk->data + hashsz);
Thomas Gummererb60e1882012-07-11 11:22:37 +02002655
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002656 ondisk->ctime.sec = htonl(ce->ce_stat_data.sd_ctime.sec);
2657 ondisk->mtime.sec = htonl(ce->ce_stat_data.sd_mtime.sec);
2658 ondisk->ctime.nsec = htonl(ce->ce_stat_data.sd_ctime.nsec);
2659 ondisk->mtime.nsec = htonl(ce->ce_stat_data.sd_mtime.nsec);
2660 ondisk->dev = htonl(ce->ce_stat_data.sd_dev);
2661 ondisk->ino = htonl(ce->ce_stat_data.sd_ino);
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002662 ondisk->mode = htonl(ce->ce_mode);
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002663 ondisk->uid = htonl(ce->ce_stat_data.sd_uid);
2664 ondisk->gid = htonl(ce->ce_stat_data.sd_gid);
2665 ondisk->size = htonl(ce->ce_stat_data.sd_size);
brian m. carlson575fa8a2019-02-19 00:05:24 +00002666 hashcpy(ondisk->data, ce->oid.hash);
Thomas Gummererb60e1882012-07-11 11:22:37 +02002667
Nguyễn Thái Ngọc Duyce51bf02014-06-13 19:19:25 +07002668 flags = ce->ce_flags & ~CE_NAMEMASK;
Thomas Gummererb60e1882012-07-11 11:22:37 +02002669 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce));
brian m. carlson575fa8a2019-02-19 00:05:24 +00002670 flagsp[0] = htons(flags);
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002671 if (ce->ce_flags & CE_EXTENDED) {
brian m. carlson575fa8a2019-02-19 00:05:24 +00002672 flagsp[1] = htons((ce->ce_flags & CE_EXTENDED_FLAGS) >> 16);
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002673 }
2674}
2675
brian m. carlsonaab61352018-02-01 02:18:45 +00002676static int ce_write_entry(git_hash_ctx *c, int fd, struct cache_entry *ce,
Kevin Willfordce012de2017-08-21 15:24:32 -06002677 struct strbuf *previous_name, struct ondisk_cache_entry *ondisk)
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002678{
Junio C Hamano9d227782012-04-04 09:12:43 -07002679 int size;
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002680 int result;
Ramsay Jones00a4b032018-03-19 17:56:11 +00002681 unsigned int saved_namelen;
2682 int stripped_name = 0;
Kevin Willfordce012de2017-08-21 15:24:32 -06002683 static unsigned char padding[8] = { 0x00 };
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002684
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07002685 if (ce->ce_flags & CE_STRIP_NAME) {
2686 saved_namelen = ce_namelen(ce);
2687 ce->ce_namelen = 0;
Ramsay Jones00a4b032018-03-19 17:56:11 +00002688 stripped_name = 1;
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07002689 }
2690
brian m. carlson575fa8a2019-02-19 00:05:24 +00002691 size = offsetof(struct ondisk_cache_entry,data) + ondisk_data_size(ce->ce_flags, 0);
Kevin Willfordce012de2017-08-21 15:24:32 -06002692
Junio C Hamano9d227782012-04-04 09:12:43 -07002693 if (!previous_name) {
Kevin Willfordce012de2017-08-21 15:24:32 -06002694 int len = ce_namelen(ce);
2695 copy_cache_entry_to_ondisk(ondisk, ce);
2696 result = ce_write(c, fd, ondisk, size);
2697 if (!result)
2698 result = ce_write(c, fd, ce->name, len);
2699 if (!result)
2700 result = ce_write(c, fd, padding, align_padding_size(size, len));
Junio C Hamano9d227782012-04-04 09:12:43 -07002701 } else {
2702 int common, to_remove, prefix_size;
2703 unsigned char to_remove_vi[16];
2704 for (common = 0;
2705 (ce->name[common] &&
2706 common < previous_name->len &&
2707 ce->name[common] == previous_name->buf[common]);
2708 common++)
2709 ; /* still matching */
2710 to_remove = previous_name->len - common;
2711 prefix_size = encode_varint(to_remove, to_remove_vi);
2712
Kevin Willfordce012de2017-08-21 15:24:32 -06002713 copy_cache_entry_to_ondisk(ondisk, ce);
2714 result = ce_write(c, fd, ondisk, size);
2715 if (!result)
2716 result = ce_write(c, fd, to_remove_vi, prefix_size);
2717 if (!result)
Thomas Gummerer0b90b882017-09-07 20:24:12 +01002718 result = ce_write(c, fd, ce->name + common, ce_namelen(ce) - common);
2719 if (!result)
2720 result = ce_write(c, fd, padding, 1);
Junio C Hamano9d227782012-04-04 09:12:43 -07002721
2722 strbuf_splice(previous_name, common, to_remove,
2723 ce->name + common, ce_namelen(ce) - common);
2724 }
Ramsay Jones00a4b032018-03-19 17:56:11 +00002725 if (stripped_name) {
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07002726 ce->ce_namelen = saved_namelen;
2727 ce->ce_flags &= ~CE_STRIP_NAME;
2728 }
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002729
Jonathan Nieder59efba62010-08-09 22:28:07 -05002730 return result;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002731}
2732
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002733/*
2734 * This function verifies if index_state has the correct sha1 of the
2735 * index file. Don't die if we have any other failure, just return 0.
2736 */
2737static int verify_index_from(const struct index_state *istate, const char *path)
2738{
2739 int fd;
2740 ssize_t n;
2741 struct stat st;
brian m. carlsonaab61352018-02-01 02:18:45 +00002742 unsigned char hash[GIT_MAX_RAWSZ];
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002743
2744 if (!istate->initialized)
2745 return 0;
2746
2747 fd = open(path, O_RDONLY);
2748 if (fd < 0)
2749 return 0;
2750
2751 if (fstat(fd, &st))
2752 goto out;
2753
brian m. carlsonaab61352018-02-01 02:18:45 +00002754 if (st.st_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002755 goto out;
2756
brian m. carlsonaab61352018-02-01 02:18:45 +00002757 n = pread_in_full(fd, hash, the_hash_algo->rawsz, st.st_size - the_hash_algo->rawsz);
2758 if (n != the_hash_algo->rawsz)
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002759 goto out;
2760
Jeff King67947c32018-08-28 17:22:52 -04002761 if (!hasheq(istate->oid.hash, hash))
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002762 goto out;
2763
2764 close(fd);
2765 return 1;
2766
2767out:
2768 close(fd);
2769 return 0;
2770}
2771
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002772static int repo_verify_index(struct repository *repo)
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002773{
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002774 return verify_index_from(repo->index, repo->index_file);
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002775}
2776
Junio C Hamano483fbe22011-03-21 10:18:19 -07002777static int has_racy_timestamp(struct index_state *istate)
2778{
2779 int entries = istate->cache_nr;
2780 int i;
2781
2782 for (i = 0; i < entries; i++) {
2783 struct cache_entry *ce = istate->cache[i];
2784 if (is_racy_timestamp(istate, ce))
2785 return 1;
2786 }
2787 return 0;
2788}
2789
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002790void repo_update_index_if_able(struct repository *repo,
2791 struct lock_file *lockfile)
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07002792{
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002793 if ((repo->index->cache_changed ||
2794 has_racy_timestamp(repo->index)) &&
2795 repo_verify_index(repo))
2796 write_locked_index(repo->index, lockfile, COMMIT_LOCK);
Martin Ågrenb74c90f2017-10-06 22:12:14 +02002797 else
2798 rollback_lock_file(lockfile);
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07002799}
2800
Jonathan Niederd8465502018-11-19 22:11:47 -08002801static int record_eoie(void)
2802{
2803 int val;
2804
2805 if (!git_config_get_bool("index.recordendofindexentries", &val))
2806 return val;
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002807
2808 /*
2809 * As a convenience, the end of index entries extension
2810 * used for threading is written by default if the user
2811 * explicitly requested threaded index reads.
2812 */
2813 return !git_config_get_index_threads(&val) && val != 1;
Jonathan Niederd8465502018-11-19 22:11:47 -08002814}
2815
Jonathan Nieder42916052018-11-19 22:12:22 -08002816static int record_ieot(void)
2817{
2818 int val;
2819
2820 if (!git_config_get_bool("index.recordoffsettable", &val))
2821 return val;
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002822
2823 /*
2824 * As a convenience, the offset table used for threading is
2825 * written by default if the user explicitly requested
2826 * threaded index reads.
2827 */
2828 return !git_config_get_index_threads(&val) && val != 1;
Jonathan Nieder42916052018-11-19 22:12:22 -08002829}
2830
Martin Ågren812d6b02017-10-06 22:12:12 +02002831/*
2832 * On success, `tempfile` is closed. If it is the temporary file
2833 * of a `struct lock_file`, we will therefore effectively perform
2834 * a 'close_lock_file_gently()`. Since that is an implementation
2835 * detail of lockfiles, callers of `do_write_index()` should not
2836 * rely on it.
2837 */
Jeff Hostetler9f41c7a2017-04-26 22:05:23 +02002838static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002839 int strip_extensions)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002840{
Nguyễn Thái Ngọc Duyca54d9b2018-01-27 19:27:56 +07002841 uint64_t start = getnanotime();
Jeff Hostetler9f41c7a2017-04-26 22:05:23 +02002842 int newfd = tempfile->fd;
Ben Peart3b1d9e02018-10-10 11:59:34 -04002843 git_hash_ctx c, eoie_c;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002844 struct cache_header hdr;
Kevin Willfordb50386c2017-08-21 15:24:31 -06002845 int i, err = 0, removed, extended, hdr_version;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002846 struct cache_entry **cache = istate->cache;
2847 int entries = istate->cache_nr;
Kjetil Barvike1afca42009-02-23 19:02:57 +01002848 struct stat st;
brian m. carlson575fa8a2019-02-19 00:05:24 +00002849 struct ondisk_cache_entry ondisk;
Junio C Hamano9d227782012-04-04 09:12:43 -07002850 struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
Thomas Gummerer4bddd982018-01-07 22:30:14 +00002851 int drop_cache_tree = istate->drop_cache_tree;
Ben Peart3b1d9e02018-10-10 11:59:34 -04002852 off_t offset;
Ben Peart77ff1122018-10-10 11:59:38 -04002853 int ieot_entries = 1;
Ben Peart32550892018-10-10 11:59:37 -04002854 struct index_entry_offset_table *ieot = NULL;
2855 int nr, nr_threads;
Junio C Hamano025a0702005-06-10 01:32:37 -07002856
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002857 for (i = removed = extended = 0; i < entries; i++) {
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002858 if (cache[i]->ce_flags & CE_REMOVE)
Junio C Hamano025a0702005-06-10 01:32:37 -07002859 removed++;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002860
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002861 /* reduce extended entries if possible */
2862 cache[i]->ce_flags &= ~CE_EXTENDED;
2863 if (cache[i]->ce_flags & CE_EXTENDED_FLAGS) {
2864 extended++;
2865 cache[i]->ce_flags |= CE_EXTENDED;
2866 }
2867 }
2868
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07002869 if (!istate->version) {
Derrick Stolee7211b9e2019-08-13 11:37:43 -07002870 istate->version = get_index_format_default(the_repository);
Nguyễn Thái Ngọc Duy4c2db932018-04-14 17:34:59 +02002871 if (git_env_bool("GIT_TEST_SPLIT_INDEX", 0))
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07002872 init_split_index(istate);
2873 }
Junio C Hamano9d227782012-04-04 09:12:43 -07002874
2875 /* demote version 3 to version 2 when the latter suffices */
2876 if (istate->version == 3 || istate->version == 2)
2877 istate->version = extended ? 3 : 2;
2878
2879 hdr_version = istate->version;
2880
Linus Torvaldsccc4feb2005-04-15 10:44:27 -07002881 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
Junio C Hamano9d227782012-04-04 09:12:43 -07002882 hdr.hdr_version = htonl(hdr_version);
Junio C Hamano025a0702005-06-10 01:32:37 -07002883 hdr.hdr_entries = htonl(entries - removed);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002884
brian m. carlsonaab61352018-02-01 02:18:45 +00002885 the_hash_algo->init_fn(&c);
Linus Torvaldsca9be052005-04-20 12:36:41 -07002886 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002887 return -1;
2888
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002889 if (!HAVE_THREADS || git_config_get_index_threads(&nr_threads))
Nguyễn Thái Ngọc Duy88168b92018-11-03 09:48:48 +01002890 nr_threads = 1;
Nguyễn Thái Ngọc Duy62e5ee82018-11-03 09:48:47 +01002891
Jonathan Nieder42916052018-11-19 22:12:22 -08002892 if (nr_threads != 1 && record_ieot()) {
Ben Peart32550892018-10-10 11:59:37 -04002893 int ieot_blocks, cpus;
2894
2895 /*
2896 * ensure default number of ieot blocks maps evenly to the
2897 * default number of threads that will process them leaving
2898 * room for the thread to load the index extensions.
2899 */
2900 if (!nr_threads) {
2901 ieot_blocks = istate->cache_nr / THREAD_COST;
2902 cpus = online_cpus();
2903 if (ieot_blocks > cpus - 1)
2904 ieot_blocks = cpus - 1;
2905 } else {
2906 ieot_blocks = nr_threads;
Ben Peart77ff1122018-10-10 11:59:38 -04002907 if (ieot_blocks > istate->cache_nr)
2908 ieot_blocks = istate->cache_nr;
Ben Peart32550892018-10-10 11:59:37 -04002909 }
2910
2911 /*
2912 * no reason to write out the IEOT extension if we don't
2913 * have enough blocks to utilize multi-threading
2914 */
2915 if (ieot_blocks > 1) {
2916 ieot = xcalloc(1, sizeof(struct index_entry_offset_table)
2917 + (ieot_blocks * sizeof(struct index_entry_offset)));
Ben Peart77ff1122018-10-10 11:59:38 -04002918 ieot_entries = DIV_ROUND_UP(entries, ieot_blocks);
Ben Peart32550892018-10-10 11:59:37 -04002919 }
2920 }
Ben Peart32550892018-10-10 11:59:37 -04002921
Ben Peart3b1d9e02018-10-10 11:59:34 -04002922 offset = lseek(newfd, 0, SEEK_CUR);
Ben Peart32550892018-10-10 11:59:37 -04002923 if (offset < 0) {
2924 free(ieot);
Ben Peart3b1d9e02018-10-10 11:59:34 -04002925 return -1;
Ben Peart32550892018-10-10 11:59:37 -04002926 }
Ben Peart3b1d9e02018-10-10 11:59:34 -04002927 offset += write_buffer_len;
Ben Peart32550892018-10-10 11:59:37 -04002928 nr = 0;
Junio C Hamano9d227782012-04-04 09:12:43 -07002929 previous_name = (hdr_version == 4) ? &previous_name_buf : NULL;
Kevin Willfordce012de2017-08-21 15:24:32 -06002930
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002931 for (i = 0; i < entries; i++) {
2932 struct cache_entry *ce = cache[i];
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002933 if (ce->ce_flags & CE_REMOVE)
Linus Torvaldsaa160212005-06-09 15:34:04 -07002934 continue;
Junio C Hamanoe06c43c2008-03-30 09:25:52 -07002935 if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce))
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002936 ce_smudge_racily_clean_entry(istate, ce);
brian m. carlson99d1a982016-09-05 20:07:52 +00002937 if (is_null_oid(&ce->oid)) {
Jeff King83bd7432013-08-27 16:41:12 -04002938 static const char msg[] = "cache entry has null sha1: %s";
2939 static int allow = -1;
2940
2941 if (allow < 0)
2942 allow = git_env_bool("GIT_ALLOW_NULL_SHA1", 0);
2943 if (allow)
2944 warning(msg, ce->name);
2945 else
Kevin Willfordb50386c2017-08-21 15:24:31 -06002946 err = error(msg, ce->name);
Jeff Kinga96d3cc2017-04-21 14:46:17 -04002947
2948 drop_cache_tree = 1;
Jeff King83bd7432013-08-27 16:41:12 -04002949 }
Ben Peart77ff1122018-10-10 11:59:38 -04002950 if (ieot && i && (i % ieot_entries == 0)) {
Ben Peart32550892018-10-10 11:59:37 -04002951 ieot->entries[ieot->nr].nr = nr;
2952 ieot->entries[ieot->nr].offset = offset;
2953 ieot->nr++;
2954 /*
2955 * If we have a V4 index, set the first byte to an invalid
2956 * character to ensure there is nothing common with the previous
2957 * entry
2958 */
2959 if (previous_name)
2960 previous_name->buf[0] = 0;
2961 nr = 0;
2962 offset = lseek(newfd, 0, SEEK_CUR);
2963 if (offset < 0) {
2964 free(ieot);
2965 return -1;
2966 }
2967 offset += write_buffer_len;
2968 }
Kevin Willfordce012de2017-08-21 15:24:32 -06002969 if (ce_write_entry(&c, newfd, ce, previous_name, (struct ondisk_cache_entry *)&ondisk) < 0)
Kevin Willfordb50386c2017-08-21 15:24:31 -06002970 err = -1;
2971
2972 if (err)
2973 break;
Ben Peart32550892018-10-10 11:59:37 -04002974 nr++;
2975 }
2976 if (ieot && nr) {
2977 ieot->entries[ieot->nr].nr = nr;
2978 ieot->entries[ieot->nr].offset = offset;
2979 ieot->nr++;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002980 }
Junio C Hamano9d227782012-04-04 09:12:43 -07002981 strbuf_release(&previous_name_buf);
Junio C Hamano1af1c2b2006-04-23 16:52:08 -07002982
Ben Peart32550892018-10-10 11:59:37 -04002983 if (err) {
2984 free(ieot);
Kevin Willfordb50386c2017-08-21 15:24:31 -06002985 return err;
Ben Peart32550892018-10-10 11:59:37 -04002986 }
Kevin Willfordb50386c2017-08-21 15:24:31 -06002987
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002988 /* Write extension data here */
Ben Peart3b1d9e02018-10-10 11:59:34 -04002989 offset = lseek(newfd, 0, SEEK_CUR);
Ben Peart32550892018-10-10 11:59:37 -04002990 if (offset < 0) {
2991 free(ieot);
Ben Peart3b1d9e02018-10-10 11:59:34 -04002992 return -1;
Ben Peart32550892018-10-10 11:59:37 -04002993 }
Ben Peart3b1d9e02018-10-10 11:59:34 -04002994 offset += write_buffer_len;
2995 the_hash_algo->init_fn(&eoie_c);
2996
Ben Peart32550892018-10-10 11:59:37 -04002997 /*
2998 * Lets write out CACHE_EXT_INDEXENTRYOFFSETTABLE first so that we
2999 * can minimize the number of extensions we have to scan through to
3000 * find it during load. Write it out regardless of the
3001 * strip_extensions parameter as we need it when loading the shared
3002 * index.
3003 */
Ben Peart32550892018-10-10 11:59:37 -04003004 if (ieot) {
3005 struct strbuf sb = STRBUF_INIT;
3006
3007 write_ieot_extension(&sb, ieot);
3008 err = write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_INDEXENTRYOFFSETTABLE, sb.len) < 0
3009 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
3010 strbuf_release(&sb);
3011 free(ieot);
3012 if (err)
3013 return -1;
3014 }
Ben Peart32550892018-10-10 11:59:37 -04003015
Nguyễn Thái Ngọc Duy6e37c8e2019-02-13 16:51:29 +07003016 if (!strip_extensions && istate->split_index &&
3017 !is_null_oid(&istate->split_index->base_oid)) {
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003018 struct strbuf sb = STRBUF_INIT;
3019
3020 err = write_link_extension(&sb, istate) < 0 ||
Ben Peart3b1d9e02018-10-10 11:59:34 -04003021 write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_LINK,
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003022 sb.len) < 0 ||
3023 ce_write(&c, newfd, sb.buf, sb.len) < 0;
3024 strbuf_release(&sb);
3025 if (err)
3026 return -1;
3027 }
Jeff Kinga96d3cc2017-04-21 14:46:17 -04003028 if (!strip_extensions && !drop_cache_tree && istate->cache_tree) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05003029 struct strbuf sb = STRBUF_INIT;
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02003030
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02003031 cache_tree_write(&sb, istate->cache_tree);
Ben Peart3b1d9e02018-10-10 11:59:34 -04003032 err = write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_TREE, sb.len) < 0
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02003033 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
3034 strbuf_release(&sb);
3035 if (err)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07003036 return -1;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07003037 }
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003038 if (!strip_extensions && istate->resolve_undo) {
Junio C Hamanocfc57892009-12-25 00:30:51 -08003039 struct strbuf sb = STRBUF_INIT;
3040
3041 resolve_undo_write(&sb, istate->resolve_undo);
Ben Peart3b1d9e02018-10-10 11:59:34 -04003042 err = write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_RESOLVE_UNDO,
Junio C Hamanocfc57892009-12-25 00:30:51 -08003043 sb.len) < 0
3044 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
3045 strbuf_release(&sb);
3046 if (err)
3047 return -1;
3048 }
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07003049 if (!strip_extensions && istate->untracked) {
3050 struct strbuf sb = STRBUF_INIT;
3051
3052 write_untracked_extension(&sb, istate->untracked);
Ben Peart3b1d9e02018-10-10 11:59:34 -04003053 err = write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_UNTRACKED,
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07003054 sb.len) < 0 ||
3055 ce_write(&c, newfd, sb.buf, sb.len) < 0;
3056 strbuf_release(&sb);
3057 if (err)
3058 return -1;
3059 }
Ben Peart883e2482017-09-22 12:35:40 -04003060 if (!strip_extensions && istate->fsmonitor_last_update) {
3061 struct strbuf sb = STRBUF_INIT;
3062
3063 write_fsmonitor_extension(&sb, istate);
Ben Peart3b1d9e02018-10-10 11:59:34 -04003064 err = write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_FSMONITOR, sb.len) < 0
3065 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
3066 strbuf_release(&sb);
3067 if (err)
3068 return -1;
3069 }
Derrick Stoleecd424152021-03-30 13:10:54 +00003070 if (istate->sparse_index) {
3071 if (write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_SPARSE_DIRECTORIES, 0) < 0)
3072 return -1;
3073 }
Ben Peart3b1d9e02018-10-10 11:59:34 -04003074
3075 /*
3076 * CACHE_EXT_ENDOFINDEXENTRIES must be written as the last entry before the SHA1
3077 * so that it can be found and processed before all the index entries are
3078 * read. Write it out regardless of the strip_extensions parameter as we need it
3079 * when loading the shared index.
3080 */
Jonathan Niederd8465502018-11-19 22:11:47 -08003081 if (offset && record_eoie()) {
Ben Peart3b1d9e02018-10-10 11:59:34 -04003082 struct strbuf sb = STRBUF_INIT;
3083
3084 write_eoie_extension(&sb, &eoie_c, offset);
3085 err = write_index_ext_header(&c, NULL, newfd, CACHE_EXT_ENDOFINDEXENTRIES, sb.len) < 0
Ben Peart883e2482017-09-22 12:35:40 -04003086 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
3087 strbuf_release(&sb);
3088 if (err)
3089 return -1;
3090 }
Kjetil Barvike1afca42009-02-23 19:02:57 +01003091
brian m. carlson75691ea2018-05-02 00:25:44 +00003092 if (ce_flush(&c, newfd, istate->oid.hash))
Jeff Hostetler9f41c7a2017-04-26 22:05:23 +02003093 return -1;
Jeff King49bd0fc2017-09-05 08:14:30 -04003094 if (close_tempfile_gently(tempfile)) {
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003095 error(_("could not close '%s'"), get_tempfile_path(tempfile));
Jeff King49bd0fc2017-09-05 08:14:30 -04003096 return -1;
3097 }
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003098 if (stat(get_tempfile_path(tempfile), &st))
Kjetil Barvike1afca42009-02-23 19:02:57 +01003099 return -1;
Kjetil Barvik5bcf1092009-03-15 12:38:55 +01003100 istate->timestamp.sec = (unsigned int)st.st_mtime;
3101 istate->timestamp.nsec = ST_MTIME_NSEC(st);
Nguyễn Thái Ngọc Duyca54d9b2018-01-27 19:27:56 +07003102 trace_performance_since(start, "write index, changed mask = %x", istate->cache_changed);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003103
3104 /*
3105 * TODO trace2: replace "the_repository" with the actual repo instance
3106 * that is associated with the given "istate".
3107 */
3108 trace2_data_intmax("index", the_repository, "write/version",
3109 istate->version);
3110 trace2_data_intmax("index", the_repository, "write/cache_nr",
3111 istate->cache_nr);
3112
Kjetil Barvike1afca42009-02-23 19:02:57 +01003113 return 0;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07003114}
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003115
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07003116void set_alternate_index_output(const char *name)
3117{
3118 alternate_index_output = name;
3119}
3120
3121static int commit_locked_index(struct lock_file *lk)
3122{
Michael Haggerty751bace2014-10-01 12:28:36 +02003123 if (alternate_index_output)
3124 return commit_lock_file_to(lk, alternate_index_output);
3125 else
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07003126 return commit_lock_file(lk);
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07003127}
3128
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003129static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,
3130 unsigned flags)
3131{
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003132 int ret;
Derrick Stolee6e773522021-03-30 13:10:55 +00003133 int was_full = !istate->sparse_index;
3134
3135 ret = convert_to_sparse(istate);
3136
3137 if (ret) {
3138 warning(_("failed to convert to a sparse-index"));
3139 return ret;
3140 }
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003141
3142 /*
3143 * TODO trace2: replace "the_repository" with the actual repo instance
3144 * that is associated with the given "istate".
3145 */
3146 trace2_region_enter_printf("index", "do_write_index", the_repository,
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003147 "%s", get_lock_file_path(lock));
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003148 ret = do_write_index(istate, lock->tempfile, 0);
3149 trace2_region_leave_printf("index", "do_write_index", the_repository,
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003150 "%s", get_lock_file_path(lock));
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003151
Derrick Stolee6e773522021-03-30 13:10:55 +00003152 if (was_full)
3153 ensure_full_index(istate);
3154
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003155 if (ret)
3156 return ret;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003157 if (flags & COMMIT_LOCK)
Ben Peart1956ecd2019-02-15 12:59:21 -05003158 ret = commit_locked_index(lock);
3159 else
3160 ret = close_lock_file_gently(lock);
3161
3162 run_hook_le(NULL, "post-index-change",
3163 istate->updated_workdir ? "1" : "0",
3164 istate->updated_skipworktree ? "1" : "0", NULL);
3165 istate->updated_workdir = 0;
3166 istate->updated_skipworktree = 0;
3167
3168 return ret;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003169}
3170
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003171static int write_split_index(struct index_state *istate,
3172 struct lock_file *lock,
3173 unsigned flags)
3174{
3175 int ret;
3176 prepare_to_write_split_index(istate);
3177 ret = do_write_locked_index(istate, lock, flags);
3178 finish_writing_split_index(istate);
3179 return ret;
3180}
3181
Christian Couderb9683722017-03-06 10:41:58 +01003182static const char *shared_index_expire = "2.weeks.ago";
3183
3184static unsigned long get_shared_index_expire_date(void)
3185{
3186 static unsigned long shared_index_expire_date;
3187 static int shared_index_expire_date_prepared;
3188
3189 if (!shared_index_expire_date_prepared) {
3190 git_config_get_expiry("splitindex.sharedindexexpire",
3191 &shared_index_expire);
3192 shared_index_expire_date = approxidate(shared_index_expire);
3193 shared_index_expire_date_prepared = 1;
3194 }
3195
3196 return shared_index_expire_date;
3197}
3198
3199static int should_delete_shared_index(const char *shared_index_path)
3200{
3201 struct stat st;
3202 unsigned long expiration;
3203
3204 /* Check timestamp */
3205 expiration = get_shared_index_expire_date();
3206 if (!expiration)
3207 return 0;
3208 if (stat(shared_index_path, &st))
Peter Krefting78bde922017-04-30 23:32:48 +02003209 return error_errno(_("could not stat '%s'"), shared_index_path);
Christian Couderb9683722017-03-06 10:41:58 +01003210 if (st.st_mtime > expiration)
3211 return 0;
3212
3213 return 1;
3214}
3215
3216static int clean_shared_index_files(const char *current_hex)
3217{
3218 struct dirent *de;
3219 DIR *dir = opendir(get_git_dir());
3220
3221 if (!dir)
3222 return error_errno(_("unable to open git dir: %s"), get_git_dir());
3223
3224 while ((de = readdir(dir)) != NULL) {
3225 const char *sha1_hex;
3226 const char *shared_index_path;
3227 if (!skip_prefix(de->d_name, "sharedindex.", &sha1_hex))
3228 continue;
3229 if (!strcmp(sha1_hex, current_hex))
3230 continue;
3231 shared_index_path = git_path("%s", de->d_name);
3232 if (should_delete_shared_index(shared_index_path) > 0 &&
3233 unlink(shared_index_path))
3234 warning_errno(_("unable to unlink: %s"), shared_index_path);
3235 }
3236 closedir(dir);
3237
3238 return 0;
3239}
3240
Nguyễn Thái Ngọc Duya0a96752014-06-13 19:19:45 +07003241static int write_shared_index(struct index_state *istate,
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003242 struct tempfile **temp)
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003243{
3244 struct split_index *si = istate->split_index;
Derrick Stolee6e773522021-03-30 13:10:55 +00003245 int ret, was_full = !istate->sparse_index;
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003246
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003247 move_cache_to_base_index(istate);
Derrick Stolee6e773522021-03-30 13:10:55 +00003248 convert_to_sparse(istate);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003249
3250 trace2_region_enter_printf("index", "shared/do_write_index",
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003251 the_repository, "%s", get_tempfile_path(*temp));
Nguyễn Thái Ngọc Duy7db2d082018-01-14 17:18:18 +07003252 ret = do_write_index(si->base, *temp, 1);
Ævar Arnfjörð Bjarmasonc1735422019-05-10 15:37:38 +02003253 trace2_region_leave_printf("index", "shared/do_write_index",
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003254 the_repository, "%s", get_tempfile_path(*temp));
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003255
Derrick Stolee6e773522021-03-30 13:10:55 +00003256 if (was_full)
3257 ensure_full_index(istate);
3258
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003259 if (ret)
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003260 return ret;
Nguyễn Thái Ngọc Duy7db2d082018-01-14 17:18:18 +07003261 ret = adjust_shared_perm(get_tempfile_path(*temp));
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003262 if (ret) {
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01003263 error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp));
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003264 return ret;
3265 }
Nguyễn Thái Ngọc Duy7db2d082018-01-14 17:18:18 +07003266 ret = rename_tempfile(temp,
brian m. carlson75691ea2018-05-02 00:25:44 +00003267 git_path("sharedindex.%s", oid_to_hex(&si->base->oid)));
Christian Couderb9683722017-03-06 10:41:58 +01003268 if (!ret) {
brian m. carlson75691ea2018-05-02 00:25:44 +00003269 oidcpy(&si->base_oid, &si->base->oid);
3270 clean_shared_index_files(oid_to_hex(&si->base->oid));
Christian Couderb9683722017-03-06 10:41:58 +01003271 }
3272
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003273 return ret;
3274}
3275
Christian Coudere6a1dd72017-02-27 19:00:08 +01003276static const int default_max_percent_split_change = 20;
3277
3278static int too_many_not_shared_entries(struct index_state *istate)
3279{
3280 int i, not_shared = 0;
3281 int max_split = git_config_get_max_percent_split_change();
3282
3283 switch (max_split) {
3284 case -1:
3285 /* not or badly configured: use the default value */
3286 max_split = default_max_percent_split_change;
3287 break;
3288 case 0:
3289 return 1; /* 0% means always write a new shared index */
3290 case 100:
3291 return 0; /* 100% means never write a new shared index */
3292 default:
3293 break; /* just use the configured value */
3294 }
3295
3296 /* Count not shared entries */
3297 for (i = 0; i < istate->cache_nr; i++) {
3298 struct cache_entry *ce = istate->cache[i];
3299 if (!ce->index)
3300 not_shared++;
3301 }
3302
3303 return (int64_t)istate->cache_nr * max_split < (int64_t)not_shared * 100;
3304}
3305
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003306int write_locked_index(struct index_state *istate, struct lock_file *lock,
3307 unsigned flags)
3308{
Christian Couder0d59ffb2017-02-27 19:00:12 +01003309 int new_shared_index, ret;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003310 struct split_index *si = istate->split_index;
3311
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +02003312 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +01003313 cache_tree_verify(the_repository, istate);
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +02003314
Martin Ågren61000812018-03-01 21:40:20 +01003315 if ((flags & SKIP_IF_UNCHANGED) && !istate->cache_changed) {
3316 if (flags & COMMIT_LOCK)
3317 rollback_lock_file(lock);
3318 return 0;
3319 }
3320
Alex Vandiver3bd28eb2017-11-09 11:58:10 -08003321 if (istate->fsmonitor_last_update)
3322 fill_fsmonitor_bitmap(istate);
3323
Nguyễn Thái Ngọc Duy5165dd52014-06-13 19:19:47 +07003324 if (!si || alternate_index_output ||
3325 (istate->cache_changed & ~EXTMASK)) {
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003326 if (si)
brian m. carlson2182abd2018-05-02 00:25:43 +00003327 oidclr(&si->base_oid);
Martin Ågrendf60cf52017-10-06 22:12:13 +02003328 ret = do_write_locked_index(istate, lock, flags);
3329 goto out;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003330 }
3331
Nguyễn Thái Ngọc Duy4c2db932018-04-14 17:34:59 +02003332 if (git_env_bool("GIT_TEST_SPLIT_INDEX", 0)) {
brian m. carlson2182abd2018-05-02 00:25:43 +00003333 int v = si->base_oid.hash[0];
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07003334 if ((v & 15) < 6)
3335 istate->cache_changed |= SPLIT_INDEX_ORDERED;
3336 }
Christian Coudere6a1dd72017-02-27 19:00:08 +01003337 if (too_many_not_shared_entries(istate))
3338 istate->cache_changed |= SPLIT_INDEX_ORDERED;
Christian Couder0d59ffb2017-02-27 19:00:12 +01003339
3340 new_shared_index = istate->cache_changed & SPLIT_INDEX_ORDERED;
3341
3342 if (new_shared_index) {
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003343 struct tempfile *temp;
3344 int saved_errno;
3345
Ævar Arnfjörð Bjarmasonc9d6c782018-11-18 19:04:29 +00003346 /* Same initial permissions as the main .git/index file */
3347 temp = mks_tempfile_sm(git_path("sharedindex_XXXXXX"), 0, 0666);
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003348 if (!temp) {
brian m. carlson2182abd2018-05-02 00:25:43 +00003349 oidclr(&si->base_oid);
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003350 ret = do_write_locked_index(istate, lock, flags);
Nguyễn Thái Ngọc Duyef5b3a62018-01-24 16:38:29 +07003351 goto out;
3352 }
3353 ret = write_shared_index(istate, &temp);
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003354
3355 saved_errno = errno;
3356 if (is_tempfile_active(temp))
3357 delete_tempfile(&temp);
3358 errno = saved_errno;
3359
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003360 if (ret)
Martin Ågrendf60cf52017-10-06 22:12:13 +02003361 goto out;
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003362 }
3363
Christian Couder0d59ffb2017-02-27 19:00:12 +01003364 ret = write_split_index(istate, lock, flags);
3365
3366 /* Freshen the shared index only if the split-index was written */
Nguyễn Thái Ngọc Duy6e37c8e2019-02-13 16:51:29 +07003367 if (!ret && !new_shared_index && !is_null_oid(&si->base_oid)) {
Thomas Gummerera125a222018-01-07 22:30:13 +00003368 const char *shared_index = git_path("sharedindex.%s",
brian m. carlson2182abd2018-05-02 00:25:43 +00003369 oid_to_hex(&si->base_oid));
Thomas Gummerera125a222018-01-07 22:30:13 +00003370 freshen_shared_index(shared_index, 1);
3371 }
Christian Couder0d59ffb2017-02-27 19:00:12 +01003372
Martin Ågrendf60cf52017-10-06 22:12:13 +02003373out:
3374 if (flags & COMMIT_LOCK)
3375 rollback_lock_file(lock);
Christian Couder0d59ffb2017-02-27 19:00:12 +01003376 return ret;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003377}
3378
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003379/*
3380 * Read the index file that is potentially unmerged into given
Elijah Newrenad376202018-07-31 10:12:05 -07003381 * index_state, dropping any unmerged entries to stage #0 (potentially
3382 * resulting in a path appearing as both a file and a directory in the
3383 * index; the caller is responsible to clear out the extra entries
3384 * before writing the index to a tree). Returns true if the index is
3385 * unmerged. Callers who want to refuse to work from an unmerged
3386 * state can call this and check its return value, instead of calling
3387 * read_cache().
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003388 */
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07003389int repo_read_index_unmerged(struct repository *repo)
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003390{
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07003391 struct index_state *istate;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003392 int i;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003393 int unmerged = 0;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003394
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07003395 repo_read_index(repo);
3396 istate = repo->index;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003397 for (i = 0; i < istate->cache_nr; i++) {
3398 struct cache_entry *ce = istate->cache[i];
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003399 struct cache_entry *new_ce;
Jameson Millera8497352018-07-02 19:49:31 +00003400 int len;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003401
3402 if (!ce_stage(ce))
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003403 continue;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003404 unmerged = 1;
Thomas Gummerer68c4f6a2012-07-06 18:07:30 +02003405 len = ce_namelen(ce);
Jameson Millera8497352018-07-02 19:49:31 +00003406 new_ce = make_empty_cache_entry(istate, len);
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003407 memcpy(new_ce->name, ce->name, len);
Thomas Gummererb60e1882012-07-11 11:22:37 +02003408 new_ce->ce_flags = create_ce_flags(0) | CE_CONFLICTED;
3409 new_ce->ce_namelen = len;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003410 new_ce->ce_mode = ce->ce_mode;
Elijah Newrenad376202018-07-31 10:12:05 -07003411 if (add_index_entry(istate, new_ce, ADD_CACHE_SKIP_DFCHECK))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01003412 return error(_("%s: cannot drop to stage #0"),
Karsten Blees5699d172013-11-14 20:24:37 +01003413 new_ce->name);
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003414 }
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003415 return unmerged;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003416}
Junio C Hamano041aee32008-07-21 01:24:17 -07003417
Jeff King98fa4732008-10-16 11:07:26 -04003418/*
3419 * Returns 1 if the path is an "other" path with respect to
3420 * the index; that is, the path is not mentioned in the index at all,
3421 * either as a file, a directory with some files in the index,
3422 * or as an unmerged entry.
3423 *
3424 * We helpfully remove a trailing "/" from directories so that
3425 * the output of read_directory can be used as-is.
3426 */
Derrick Stolee847a9e52021-04-01 01:49:39 +00003427int index_name_is_other(struct index_state *istate, const char *name,
3428 int namelen)
Jeff King98fa4732008-10-16 11:07:26 -04003429{
3430 int pos;
3431 if (namelen && name[namelen - 1] == '/')
3432 namelen--;
3433 pos = index_name_pos(istate, name, namelen);
3434 if (0 <= pos)
3435 return 0; /* exact match */
3436 pos = -pos - 1;
3437 if (pos < istate->cache_nr) {
3438 struct cache_entry *ce = istate->cache[pos];
3439 if (ce_namelen(ce) == namelen &&
3440 !memcmp(ce->name, name, namelen))
3441 return 0; /* Yup, this one exists unmerged */
3442 }
3443 return 1;
3444}
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003445
Derrick Stolee847a9e52021-04-01 01:49:39 +00003446void *read_blob_data_from_index(struct index_state *istate,
Brandon Williams87542502017-01-10 12:06:10 -08003447 const char *path, unsigned long *size)
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003448{
3449 int pos, len;
3450 unsigned long sz;
3451 enum object_type type;
3452 void *data;
3453
3454 len = strlen(path);
3455 pos = index_name_pos(istate, path, len);
3456 if (pos < 0) {
3457 /*
3458 * We might be in the middle of a merge, in which
3459 * case we would read stage #2 (ours).
3460 */
3461 int i;
3462 for (i = -pos - 1;
3463 (pos < 0 && i < istate->cache_nr &&
3464 !strcmp(istate->cache[i]->name, path));
3465 i++)
3466 if (ce_stage(istate->cache[i]) == 2)
3467 pos = i;
3468 }
3469 if (pos < 0)
3470 return NULL;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00003471 data = read_object_file(&istate->cache[pos]->oid, &type, &sz);
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003472 if (!data || type != OBJ_BLOB) {
3473 free(data);
3474 return NULL;
3475 }
Lukas Fleischerff366822013-04-13 15:28:31 +02003476 if (size)
3477 *size = sz;
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003478 return data;
3479}
Michael Haggerty38612532013-06-20 10:37:51 +02003480
3481void stat_validity_clear(struct stat_validity *sv)
3482{
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00003483 FREE_AND_NULL(sv->sd);
Michael Haggerty38612532013-06-20 10:37:51 +02003484}
3485
3486int stat_validity_check(struct stat_validity *sv, const char *path)
3487{
3488 struct stat st;
3489
3490 if (stat(path, &st) < 0)
3491 return sv->sd == NULL;
3492 if (!sv->sd)
3493 return 0;
3494 return S_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);
3495}
3496
3497void stat_validity_update(struct stat_validity *sv, int fd)
3498{
3499 struct stat st;
3500
3501 if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode))
3502 stat_validity_clear(sv);
3503 else {
3504 if (!sv->sd)
René Scharfeca56dad2021-03-13 17:17:22 +01003505 CALLOC_ARRAY(sv->sd, 1);
Michael Haggerty38612532013-06-20 10:37:51 +02003506 fill_stat_data(sv->sd, &st);
3507 }
3508}
David Turneredf3b902017-05-08 11:41:42 +02003509
3510void move_index_extensions(struct index_state *dst, struct index_state *src)
3511{
3512 dst->untracked = src->untracked;
3513 src->untracked = NULL;
Nguyễn Thái Ngọc Duy836ef2b2018-08-18 16:41:26 +02003514 dst->cache_tree = src->cache_tree;
3515 src->cache_tree = NULL;
David Turneredf3b902017-05-08 11:41:42 +02003516}
Jameson Millera8497352018-07-02 19:49:31 +00003517
Jameson Miller8e72d672018-07-02 19:49:37 +00003518struct cache_entry *dup_cache_entry(const struct cache_entry *ce,
3519 struct index_state *istate)
3520{
3521 unsigned int size = ce_size(ce);
3522 int mem_pool_allocated;
3523 struct cache_entry *new_entry = make_empty_cache_entry(istate, ce_namelen(ce));
3524 mem_pool_allocated = new_entry->mem_pool_allocated;
3525
3526 memcpy(new_entry, ce, size);
3527 new_entry->mem_pool_allocated = mem_pool_allocated;
3528 return new_entry;
3529}
3530
Jameson Millera8497352018-07-02 19:49:31 +00003531void discard_cache_entry(struct cache_entry *ce)
3532{
Jameson Miller8616a2d2018-07-02 19:49:39 +00003533 if (ce && should_validate_cache_entries())
3534 memset(ce, 0xCD, cache_entry_size(ce->ce_namelen));
3535
Jameson Miller8e72d672018-07-02 19:49:37 +00003536 if (ce && ce->mem_pool_allocated)
3537 return;
3538
Jameson Millera8497352018-07-02 19:49:31 +00003539 free(ce);
3540}
Jameson Miller8616a2d2018-07-02 19:49:39 +00003541
3542int should_validate_cache_entries(void)
3543{
3544 static int validate_index_cache_entries = -1;
3545
3546 if (validate_index_cache_entries < 0) {
3547 if (getenv("GIT_TEST_VALIDATE_INDEX_CACHE_ENTRIES"))
3548 validate_index_cache_entries = 1;
3549 else
3550 validate_index_cache_entries = 0;
3551 }
3552
3553 return validate_index_cache_entries;
3554}
Ben Peart3b1d9e02018-10-10 11:59:34 -04003555
3556#define EOIE_SIZE (4 + GIT_SHA1_RAWSZ) /* <4-byte offset> + <20-byte hash> */
3557#define EOIE_SIZE_WITH_HEADER (4 + 4 + EOIE_SIZE) /* <4-byte signature> + <4-byte length> + EOIE_SIZE */
3558
3559static size_t read_eoie_extension(const char *mmap, size_t mmap_size)
3560{
3561 /*
3562 * The end of index entries (EOIE) extension is guaranteed to be last
3563 * so that it can be found by scanning backwards from the EOF.
3564 *
3565 * "EOIE"
3566 * <4-byte length>
3567 * <4-byte offset>
3568 * <20-byte hash>
3569 */
3570 const char *index, *eoie;
3571 uint32_t extsize;
3572 size_t offset, src_offset;
3573 unsigned char hash[GIT_MAX_RAWSZ];
3574 git_hash_ctx c;
3575
3576 /* ensure we have an index big enough to contain an EOIE extension */
3577 if (mmap_size < sizeof(struct cache_header) + EOIE_SIZE_WITH_HEADER + the_hash_algo->rawsz)
3578 return 0;
3579
3580 /* validate the extension signature */
3581 index = eoie = mmap + mmap_size - EOIE_SIZE_WITH_HEADER - the_hash_algo->rawsz;
3582 if (CACHE_EXT(index) != CACHE_EXT_ENDOFINDEXENTRIES)
3583 return 0;
3584 index += sizeof(uint32_t);
3585
3586 /* validate the extension size */
3587 extsize = get_be32(index);
3588 if (extsize != EOIE_SIZE)
3589 return 0;
3590 index += sizeof(uint32_t);
3591
3592 /*
3593 * Validate the offset we're going to look for the first extension
3594 * signature is after the index header and before the eoie extension.
3595 */
3596 offset = get_be32(index);
3597 if (mmap + offset < mmap + sizeof(struct cache_header))
3598 return 0;
3599 if (mmap + offset >= eoie)
3600 return 0;
3601 index += sizeof(uint32_t);
3602
3603 /*
3604 * The hash is computed over extension types and their sizes (but not
3605 * their contents). E.g. if we have "TREE" extension that is N-bytes
3606 * long, "REUC" extension that is M-bytes long, followed by "EOIE",
3607 * then the hash would be:
3608 *
3609 * SHA-1("TREE" + <binary representation of N> +
3610 * "REUC" + <binary representation of M>)
3611 */
3612 src_offset = offset;
3613 the_hash_algo->init_fn(&c);
3614 while (src_offset < mmap_size - the_hash_algo->rawsz - EOIE_SIZE_WITH_HEADER) {
3615 /* After an array of active_nr index entries,
3616 * there can be arbitrary number of extended
3617 * sections, each of which is prefixed with
3618 * extension name (4-byte) and section length
3619 * in 4-byte network byte order.
3620 */
3621 uint32_t extsize;
3622 memcpy(&extsize, mmap + src_offset + 4, 4);
3623 extsize = ntohl(extsize);
3624
3625 /* verify the extension size isn't so large it will wrap around */
3626 if (src_offset + 8 + extsize < src_offset)
3627 return 0;
3628
3629 the_hash_algo->update_fn(&c, mmap + src_offset, 8);
3630
3631 src_offset += 8;
3632 src_offset += extsize;
3633 }
3634 the_hash_algo->final_fn(hash, &c);
3635 if (!hasheq(hash, (const unsigned char *)index))
3636 return 0;
3637
3638 /* Validate that the extension offsets returned us back to the eoie extension. */
3639 if (src_offset != mmap_size - the_hash_algo->rawsz - EOIE_SIZE_WITH_HEADER)
3640 return 0;
3641
3642 return offset;
3643}
3644
3645static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset)
3646{
3647 uint32_t buffer;
3648 unsigned char hash[GIT_MAX_RAWSZ];
3649
3650 /* offset */
3651 put_be32(&buffer, offset);
3652 strbuf_add(sb, &buffer, sizeof(uint32_t));
3653
3654 /* hash */
3655 the_hash_algo->final_fn(hash, eoie_context);
3656 strbuf_add(sb, hash, the_hash_algo->rawsz);
3657}
Ben Peart32550892018-10-10 11:59:37 -04003658
Ben Peart32550892018-10-10 11:59:37 -04003659#define IEOT_VERSION (1)
3660
3661static struct index_entry_offset_table *read_ieot_extension(const char *mmap, size_t mmap_size, size_t offset)
3662{
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003663 const char *index = NULL;
3664 uint32_t extsize, ext_version;
3665 struct index_entry_offset_table *ieot;
3666 int i, nr;
Ben Peart32550892018-10-10 11:59:37 -04003667
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003668 /* find the IEOT extension */
3669 if (!offset)
3670 return NULL;
3671 while (offset <= mmap_size - the_hash_algo->rawsz - 8) {
3672 extsize = get_be32(mmap + offset + 4);
3673 if (CACHE_EXT((mmap + offset)) == CACHE_EXT_INDEXENTRYOFFSETTABLE) {
3674 index = mmap + offset + 4 + 4;
3675 break;
3676 }
3677 offset += 8;
3678 offset += extsize;
3679 }
3680 if (!index)
3681 return NULL;
Ben Peart32550892018-10-10 11:59:37 -04003682
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003683 /* validate the version is IEOT_VERSION */
3684 ext_version = get_be32(index);
3685 if (ext_version != IEOT_VERSION) {
3686 error("invalid IEOT version %d", ext_version);
3687 return NULL;
3688 }
3689 index += sizeof(uint32_t);
Ben Peart32550892018-10-10 11:59:37 -04003690
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003691 /* extension size - version bytes / bytes per entry */
3692 nr = (extsize - sizeof(uint32_t)) / (sizeof(uint32_t) + sizeof(uint32_t));
3693 if (!nr) {
3694 error("invalid number of IEOT entries %d", nr);
3695 return NULL;
3696 }
3697 ieot = xmalloc(sizeof(struct index_entry_offset_table)
3698 + (nr * sizeof(struct index_entry_offset)));
3699 ieot->nr = nr;
3700 for (i = 0; i < nr; i++) {
3701 ieot->entries[i].offset = get_be32(index);
3702 index += sizeof(uint32_t);
3703 ieot->entries[i].nr = get_be32(index);
3704 index += sizeof(uint32_t);
3705 }
Ben Peart32550892018-10-10 11:59:37 -04003706
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003707 return ieot;
Ben Peart32550892018-10-10 11:59:37 -04003708}
3709
3710static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot)
3711{
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003712 uint32_t buffer;
3713 int i;
Ben Peart32550892018-10-10 11:59:37 -04003714
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003715 /* version */
3716 put_be32(&buffer, IEOT_VERSION);
3717 strbuf_add(sb, &buffer, sizeof(uint32_t));
Ben Peart32550892018-10-10 11:59:37 -04003718
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003719 /* ieot */
3720 for (i = 0; i < ieot->nr; i++) {
Ben Peart32550892018-10-10 11:59:37 -04003721
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003722 /* offset */
3723 put_be32(&buffer, ieot->entries[i].offset);
3724 strbuf_add(sb, &buffer, sizeof(uint32_t));
Ben Peart32550892018-10-10 11:59:37 -04003725
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003726 /* count */
3727 put_be32(&buffer, ieot->entries[i].nr);
3728 strbuf_add(sb, &buffer, sizeof(uint32_t));
3729 }
Ben Peart32550892018-10-10 11:59:37 -04003730}