blob: a6db25a16daf89caf4762c62810ab76416dd4c92 [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 */
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00006#include "git-compat-util.h"
Elijah Newren6cee5eb2023-05-16 06:33:47 +00007#include "bulk-checkin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +00009#include "date.h"
Elijah Newrencffbfad2018-06-30 18:24:55 -070010#include "diff.h"
11#include "diffcore.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000012#include "hex.h"
Michael Haggertyf6ecc622015-08-10 11:47:45 +020013#include "tempfile.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020014#include "lockfile.h"
Junio C Hamanobad68ec2006-04-24 21:18:58 -070015#include "cache-tree.h"
Linus Torvaldsf35a6d32007-04-09 21:20:29 -070016#include "refs.h"
Alexandre Julliardd6168132007-08-11 23:59:01 +020017#include "dir.h"
Elijah Newren87bed172023-04-11 00:41:53 -070018#include "object-file.h"
Elijah Newrena034e912023-05-16 06:34:06 +000019#include "object-store-ll.h"
Elijah Newren6f2d7432023-04-11 03:00:42 +000020#include "oid-array.h"
Junio C Hamano041aee32008-07-21 01:24:17 -070021#include "tree.h"
22#include "commit.h"
Elijah Newren32a8f512023-03-21 06:26:03 +000023#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000024#include "gettext.h"
Elijah Newren5bc07222023-04-11 00:41:47 -070025#include "mem-pool.h"
Elijah Newrenf5653852023-05-16 06:33:50 +000026#include "name-hash.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070027#include "object-name.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000028#include "path.h"
Elijah Newrenfbffdfb2023-05-16 06:33:52 +000029#include "preload-index.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000030#include "read-cache.h"
Junio C Hamanocfc57892009-12-25 00:30:51 -080031#include "resolve-undo.h"
Elijah Newren6cee5eb2023-05-16 06:33:47 +000032#include "revision.h"
Junio C Hamano6c9cd162012-04-03 15:53:15 -070033#include "strbuf.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000034#include "trace2.h"
Junio C Hamano6c9cd162012-04-03 15:53:15 -070035#include "varint.h"
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070036#include "split-index.h"
Elijah Newrencb2a5132023-04-22 20:17:09 +000037#include "symlinks.h"
Jeff Kinga42643a2014-12-15 18:15:20 -050038#include "utf8.h"
Ben Peart883e2482017-09-22 12:35:40 -040039#include "fsmonitor.h"
Ben Peartabb4bb82018-10-10 11:59:36 -040040#include "thread-utils.h"
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +020041#include "progress.h"
Derrick Stolee6e773522021-03-30 13:10:55 +000042#include "sparse-index.h"
Derrick Stolee410334e2021-05-18 18:32:47 +000043#include "csum-file.h"
Jonathan Tanb2896d22021-07-23 11:52:22 -070044#include "promisor-remote.h"
Emily Shafferdbb1c612021-12-22 04:59:41 +010045#include "hook.h"
Junio C Hamanobad68ec2006-04-24 21:18:58 -070046
Thomas Gummererb60e1882012-07-11 11:22:37 +020047/* Mask for the name length in ce_flags in the on-disk index */
48
49#define CE_NAMEMASK (0x0fff)
50
Junio C Hamanobad68ec2006-04-24 21:18:58 -070051/* Index extensions.
52 *
53 * The first letter should be 'A'..'Z' for extensions that are not
54 * necessary for a correct operation (i.e. optimization data).
55 * When new extensions are added that _needs_ to be understood in
56 * order to correctly interpret the index file, pick character that
57 * is outside the range, to cause the reader to abort.
58 */
59
60#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
61#define CACHE_EXT_TREE 0x54524545 /* "TREE" */
Shawn O. Pearceb659b492010-02-02 07:33:28 -080062#define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070063#define CACHE_EXT_LINK 0x6c696e6b /* "link" */
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +070064#define CACHE_EXT_UNTRACKED 0x554E5452 /* "UNTR" */
Ben Peart883e2482017-09-22 12:35:40 -040065#define CACHE_EXT_FSMONITOR 0x46534D4E /* "FSMN" */
Ben Peart3b1d9e02018-10-10 11:59:34 -040066#define CACHE_EXT_ENDOFINDEXENTRIES 0x454F4945 /* "EOIE" */
Ben Peart32550892018-10-10 11:59:37 -040067#define CACHE_EXT_INDEXENTRYOFFSETTABLE 0x49454F54 /* "IEOT" */
Derrick Stoleecd424152021-03-30 13:10:54 +000068#define CACHE_EXT_SPARSE_DIRECTORIES 0x73646972 /* "sdir" */
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070069
70/* changes that can be kept in $GIT_DIR/index (basically all extensions) */
Nguyễn Thái Ngọc Duye0cf0d72014-06-13 19:19:37 +070071#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +070072 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \
Ben Peart883e2482017-09-22 12:35:40 -040073 SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED | FSMONITOR_CHANGED)
Linus Torvaldse83c5162005-04-07 15:13:13 -070074
Jameson Miller8e72d672018-07-02 19:49:37 +000075
76/*
77 * This is an estimate of the pathname length in the index. We use
78 * this for V4 index files to guess the un-deltafied size of the index
79 * in memory because of pathname deltafication. This is not required
80 * for V2/V3 index formats because their pathnames are not compressed.
81 * If the initial amount of memory set aside is not sufficient, the
82 * mem pool will allocate extra memory.
83 */
84#define CACHE_ENTRY_PATH_LENGTH 80
85
Victoria Dye20ec2d02021-11-29 15:52:41 +000086enum index_search_mode {
87 NO_EXPAND_SPARSE = 0,
88 EXPAND_SPARSE = 1
89};
90
Jameson Miller8e72d672018-07-02 19:49:37 +000091static inline struct cache_entry *mem_pool__ce_alloc(struct mem_pool *mem_pool, size_t len)
92{
93 struct cache_entry *ce;
94 ce = mem_pool_alloc(mem_pool, cache_entry_size(len));
95 ce->mem_pool_allocated = 1;
96 return ce;
97}
98
99static inline struct cache_entry *mem_pool__ce_calloc(struct mem_pool *mem_pool, size_t len)
100{
101 struct cache_entry * ce;
102 ce = mem_pool_calloc(mem_pool, 1, cache_entry_size(len));
103 ce->mem_pool_allocated = 1;
104 return ce;
105}
106
107static struct mem_pool *find_mem_pool(struct index_state *istate)
108{
109 struct mem_pool **pool_ptr;
110
111 if (istate->split_index && istate->split_index->base)
112 pool_ptr = &istate->split_index->base->ce_mem_pool;
113 else
114 pool_ptr = &istate->ce_mem_pool;
115
Elijah Newren44c7e1a2020-08-15 17:37:56 +0000116 if (!*pool_ptr) {
117 *pool_ptr = xmalloc(sizeof(**pool_ptr));
118 mem_pool_init(*pool_ptr, 0);
119 }
Jameson Miller8e72d672018-07-02 19:49:37 +0000120
121 return *pool_ptr;
122}
123
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +0700124static const char *alternate_index_output;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -0700125
Junio C Hamano9cb76b82008-01-22 23:01:13 -0800126static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
127{
Derrick Stolee4300f842021-03-30 13:10:48 +0000128 if (S_ISSPARSEDIR(ce->ce_mode))
Derrick Stolee9fadb372022-05-23 13:48:40 +0000129 istate->sparse_index = INDEX_COLLAPSED;
Derrick Stolee4300f842021-03-30 13:10:48 +0000130
Junio C Hamano9cb76b82008-01-22 23:01:13 -0800131 istate->cache[nr] = ce;
Linus Torvalds96872bc2008-03-21 13:16:24 -0700132 add_name_hash(istate, ce);
Junio C Hamano9cb76b82008-01-22 23:01:13 -0800133}
134
Linus Torvaldscf558702008-01-22 18:41:14 -0800135static void replace_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
136{
137 struct cache_entry *old = istate->cache[nr];
138
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700139 replace_index_entry_in_base(istate, old, ce);
Karsten Blees20926782013-02-28 00:57:48 +0100140 remove_name_hash(istate, old);
Jameson Millera8497352018-07-02 19:49:31 +0000141 discard_cache_entry(old);
Ben Peart0e267b72018-03-15 11:25:20 -0400142 ce->ce_flags &= ~CE_HASHED;
Linus Torvaldsa22c6372008-02-22 20:37:40 -0800143 set_index_entry(istate, nr, ce);
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700144 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -0400145 mark_fsmonitor_invalid(istate, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700146 istate->cache_changed |= CE_ENTRY_CHANGED;
Linus Torvaldscf558702008-01-22 18:41:14 -0800147}
148
Petr Baudis81dc2302008-07-21 02:25:56 +0200149void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name)
150{
Victoria Dyeb7f91302022-03-29 01:07:07 +0000151 struct cache_entry *old_entry = istate->cache[nr], *new_entry, *refreshed;
Petr Baudis81dc2302008-07-21 02:25:56 +0200152 int namelen = strlen(new_name);
153
Jameson Millera8497352018-07-02 19:49:31 +0000154 new_entry = make_empty_cache_entry(istate, namelen);
Brandon Williams285c2e22018-02-14 10:59:45 -0800155 copy_cache_entry(new_entry, old_entry);
156 new_entry->ce_flags &= ~CE_HASHED;
157 new_entry->ce_namelen = namelen;
158 new_entry->index = 0;
159 memcpy(new_entry->name, new_name, namelen + 1);
Petr Baudis81dc2302008-07-21 02:25:56 +0200160
Brandon Williams285c2e22018-02-14 10:59:45 -0800161 cache_tree_invalidate_path(istate, old_entry->name);
162 untracked_cache_remove_from_index(istate, old_entry->name);
Petr Baudis81dc2302008-07-21 02:25:56 +0200163 remove_index_entry_at(istate, nr);
Victoria Dyeb7f91302022-03-29 01:07:07 +0000164
165 /*
166 * Refresh the new index entry. Using 'refresh_cache_entry' ensures
167 * we only update stat info if the entry is otherwise up-to-date (i.e.,
168 * the contents/mode haven't changed). This ensures that we reflect the
169 * 'ctime' of the rename in the index without (incorrectly) updating
170 * the cached stat info to reflect unstaged changes on disk.
171 */
172 refreshed = refresh_cache_entry(istate, new_entry, CE_MATCH_REFRESH);
173 if (refreshed && refreshed != new_entry) {
174 add_index_entry(istate, refreshed, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
175 discard_cache_entry(new_entry);
176 } else
177 add_index_entry(istate, new_entry, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
Petr Baudis81dc2302008-07-21 02:25:56 +0200178}
179
Junio C Hamano415e96c2005-05-15 14:23:12 -0700180/*
181 * This only updates the "non-critical" parts of the directory
182 * cache, ie the parts that aren't tracked by GIT, and only used
183 * to validate the cache.
184 */
Johannes Schindelind4c0a3a2019-05-24 05:23:47 -0700185void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st)
Junio C Hamano415e96c2005-05-15 14:23:12 -0700186{
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200187 fill_stat_data(&ce->ce_stat_data, st);
Junio C Hamano5f730762006-02-08 21:15:24 -0800188
189 if (assume_unchanged)
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800190 ce->ce_flags |= CE_VALID;
Junio C Hamanoeadb5832008-01-18 23:45:24 -0800191
Ben Peart883e2482017-09-22 12:35:40 -0400192 if (S_ISREG(st->st_mode)) {
Junio C Hamanoeadb5832008-01-18 23:45:24 -0800193 ce_mark_uptodate(ce);
Johannes Schindelinb5a81692019-05-24 05:23:48 -0700194 mark_fsmonitor_valid(istate, ce);
Ben Peart883e2482017-09-22 12:35:40 -0400195 }
Junio C Hamano415e96c2005-05-15 14:23:12 -0700196}
197
Junio C Hamanoc33fa872023-09-14 14:46:46 -0700198static unsigned int st_mode_from_ce(const struct cache_entry *ce)
199{
200 extern int trust_executable_bit, has_symlinks;
201
202 switch (ce->ce_mode & S_IFMT) {
203 case S_IFLNK:
204 return has_symlinks ? S_IFLNK : (S_IFREG | 0644);
205 case S_IFREG:
206 return (ce->ce_mode & (trust_executable_bit ? 0755 : 0644)) | S_IFREG;
207 case S_IFGITLINK:
208 return S_IFDIR | 0755;
209 case S_IFDIR:
210 return ce->ce_mode;
211 default:
212 BUG("unsupported ce_mode: %o", ce->ce_mode);
213 }
214}
215
216int fake_lstat(const struct cache_entry *ce, struct stat *st)
217{
218 fake_lstat_data(&ce->ce_stat_data, st);
219 st->st_mode = st_mode_from_ce(ce);
220
221 /* always succeed as lstat() replacement */
222 return 0;
223}
224
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200225static int ce_compare_data(struct index_state *istate,
226 const struct cache_entry *ce,
227 struct stat *st)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800228{
229 int match = -1;
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -0700230 int fd = git_open_cloexec(ce->name, O_RDONLY);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800231
232 if (fd >= 0) {
Patryk Obarabebfecb2017-08-20 22:09:27 +0200233 struct object_id oid;
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200234 if (!index_fd(istate, &oid, fd, st, OBJ_BLOB, ce->name, 0))
Jeff King6a29d7b2018-08-28 17:22:59 -0400235 match = !oideq(&oid, &ce->oid);
Linus Torvalds7f8508e2006-07-31 09:55:15 -0700236 /* index_fd() closed the file descriptor already */
Junio C Hamano29e4d362005-12-20 00:02:15 -0800237 }
238 return match;
239}
240
René Scharfe21a6b9f2013-06-02 17:46:52 +0200241static int ce_compare_link(const struct cache_entry *ce, size_t expected_size)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800242{
243 int match = -1;
Junio C Hamano29e4d362005-12-20 00:02:15 -0800244 void *buffer;
245 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500246 enum object_type type;
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800247 struct strbuf sb = STRBUF_INIT;
Junio C Hamano29e4d362005-12-20 00:02:15 -0800248
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800249 if (strbuf_readlink(&sb, ce->name, expected_size))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800250 return -1;
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800251
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200252 buffer = repo_read_object_file(the_repository, &ce->oid, &type, &size);
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800253 if (buffer) {
254 if (size == sb.len)
255 match = memcmp(buffer, sb.buf, size);
256 free(buffer);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800257 }
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800258 strbuf_release(&sb);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800259 return match;
260}
261
René Scharfe21a6b9f2013-06-02 17:46:52 +0200262static int ce_compare_gitlink(const struct cache_entry *ce)
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700263{
brian m. carlson1053fe82017-10-15 22:07:06 +0000264 struct object_id oid;
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700265
266 /*
267 * We don't actually require that the .git directory
Martin Waitz302b9282007-05-21 22:08:28 +0200268 * under GITLINK directory be a valid git directory. It
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700269 * might even be missing (in case nobody populated that
270 * sub-project).
271 *
272 * If so, we consider it always to match.
273 */
brian m. carlsona98e6102017-10-15 22:07:07 +0000274 if (resolve_gitlink_ref(ce->name, "HEAD", &oid) < 0)
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700275 return 0;
Jeff King6a29d7b2018-08-28 17:22:59 -0400276 return !oideq(&oid, &ce->oid);
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700277}
278
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200279static int ce_modified_check_fs(struct index_state *istate,
280 const struct cache_entry *ce,
281 struct stat *st)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800282{
283 switch (st->st_mode & S_IFMT) {
284 case S_IFREG:
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200285 if (ce_compare_data(istate, ce, st))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800286 return DATA_CHANGED;
287 break;
288 case S_IFLNK:
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500289 if (ce_compare_link(ce, xsize_t(st->st_size)))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800290 return DATA_CHANGED;
291 break;
Linus Torvaldsa8ee75b2007-04-13 09:24:13 -0700292 case S_IFDIR:
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800293 if (S_ISGITLINK(ce->ce_mode))
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700294 return ce_compare_gitlink(ce) ? DATA_CHANGED : 0;
Jeff King1cf01a32017-09-21 02:25:41 -0400295 /* else fallthrough */
Junio C Hamano29e4d362005-12-20 00:02:15 -0800296 default:
297 return TYPE_CHANGED;
298 }
299 return 0;
300}
301
René Scharfe21a6b9f2013-06-02 17:46:52 +0200302static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st)
Linus Torvalds734aab72005-04-09 09:48:20 -0700303{
304 unsigned int changed = 0;
305
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800306 if (ce->ce_flags & CE_REMOVE)
307 return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED;
308
309 switch (ce->ce_mode & S_IFMT) {
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200310 case S_IFREG:
311 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
Junio C Hamano3e09cdf2005-10-11 18:45:33 -0700312 /* We consider only the owner x bit to be relevant for
313 * "mode changes"
314 */
315 if (trust_executable_bit &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800316 (0100 & (ce->ce_mode ^ st->st_mode)))
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200317 changed |= MODE_CHANGED;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200318 break;
319 case S_IFLNK:
Johannes Sixt78a8d642007-03-02 22:11:30 +0100320 if (!S_ISLNK(st->st_mode) &&
321 (has_symlinks || !S_ISREG(st->st_mode)))
322 changed |= TYPE_CHANGED;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200323 break;
Martin Waitz302b9282007-05-21 22:08:28 +0200324 case S_IFGITLINK:
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700325 /* We ignore most of the st_xxx fields for gitlinks */
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700326 if (!S_ISDIR(st->st_mode))
327 changed |= TYPE_CHANGED;
328 else if (ce_compare_gitlink(ce))
329 changed |= DATA_CHANGED;
Linus Torvaldsa8ee75b2007-04-13 09:24:13 -0700330 return changed;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200331 default:
Nguyễn Thái Ngọc Duy391408e2018-11-10 06:16:04 +0100332 BUG("unsupported ce_mode: %o", ce->ce_mode);
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200333 }
Linus Torvaldsccc4feb2005-04-15 10:44:27 -0700334
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200335 changed |= match_stat_data(&ce->ce_stat_data, st);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800336
Linus Torvaldsf49c2c22008-06-10 10:44:43 -0700337 /* Racily smudged entry? */
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200338 if (!ce->ce_stat_data.sd_size) {
brian m. carlson99d1a982016-09-05 20:07:52 +0000339 if (!is_empty_blob_sha1(ce->oid.hash))
Linus Torvaldsf49c2c22008-06-10 10:44:43 -0700340 changed |= DATA_CHANGED;
341 }
342
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800343 return changed;
344}
345
Nguyễn Thái Ngọc Duy2bb4cda2015-03-08 17:12:36 +0700346static int is_racy_stat(const struct index_state *istate,
347 const struct stat_data *sd)
348{
349 return (istate->timestamp.sec &&
350#ifdef USE_NSEC
351 /* nanosecond timestamped files can also be racy! */
352 (istate->timestamp.sec < sd->sd_mtime.sec ||
353 (istate->timestamp.sec == sd->sd_mtime.sec &&
354 istate->timestamp.nsec <= sd->sd_mtime.nsec))
355#else
356 istate->timestamp.sec <= sd->sd_mtime.sec
357#endif
358 );
359}
360
SZEDER Gábor5581a012018-10-11 11:43:09 +0200361int is_racy_timestamp(const struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200362 const struct cache_entry *ce)
Junio C Hamano6d91da62008-01-21 00:44:50 -0800363{
Junio C Hamano050288d2008-05-03 17:24:28 -0700364 return (!S_ISGITLINK(ce->ce_mode) &&
Nguyễn Thái Ngọc Duy2bb4cda2015-03-08 17:12:36 +0700365 is_racy_stat(istate, &ce->ce_stat_data));
Junio C Hamano6d91da62008-01-21 00:44:50 -0800366}
367
Nguyễn Thái Ngọc Duyed4efab2015-03-08 17:12:37 +0700368int match_stat_data_racy(const struct index_state *istate,
369 const struct stat_data *sd, struct stat *st)
370{
371 if (is_racy_stat(istate, sd))
372 return MTIME_CHANGED;
373 return match_stat_data(sd, st);
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800374}
375
Ben Peart883e2482017-09-22 12:35:40 -0400376int ie_match_stat(struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200377 const struct cache_entry *ce, struct stat *st,
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800378 unsigned int options)
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800379{
Junio C Hamano5f730762006-02-08 21:15:24 -0800380 unsigned int changed;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800381 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700382 int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800383 int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY;
Ben Peart883e2482017-09-22 12:35:40 -0400384 int ignore_fsmonitor = options & CE_MATCH_IGNORE_FSMONITOR;
Junio C Hamano5f730762006-02-08 21:15:24 -0800385
Ben Peart883e2482017-09-22 12:35:40 -0400386 if (!ignore_fsmonitor)
387 refresh_fsmonitor(istate);
Junio C Hamano5f730762006-02-08 21:15:24 -0800388 /*
389 * If it's marked as always valid in the index, it's
390 * valid whatever the checked-out copy says.
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700391 *
392 * skip-worktree has the same effect with higher precedence
Junio C Hamano5f730762006-02-08 21:15:24 -0800393 */
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700394 if (!ignore_skip_worktree && ce_skip_worktree(ce))
395 return 0;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800396 if (!ignore_valid && (ce->ce_flags & CE_VALID))
Junio C Hamano5f730762006-02-08 21:15:24 -0800397 return 0;
Ben Peart883e2482017-09-22 12:35:40 -0400398 if (!ignore_fsmonitor && (ce->ce_flags & CE_FSMONITOR_VALID))
399 return 0;
Junio C Hamano5f730762006-02-08 21:15:24 -0800400
Junio C Hamano331fcb52008-11-28 19:56:34 -0800401 /*
402 * Intent-to-add entries have not been added, so the index entry
403 * by definition never matches what is in the work tree until it
404 * actually gets added.
405 */
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +0700406 if (ce_intent_to_add(ce))
Junio C Hamano331fcb52008-11-28 19:56:34 -0800407 return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED;
408
Junio C Hamano5f730762006-02-08 21:15:24 -0800409 changed = ce_match_stat_basic(ce, st);
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800410
Junio C Hamano29e4d362005-12-20 00:02:15 -0800411 /*
412 * Within 1 second of this sequence:
413 * echo xyzzy >file && git-update-index --add file
414 * running this command:
415 * echo frotz >file
416 * would give a falsely clean cache entry. The mtime and
417 * length match the cache, and other stat fields do not change.
418 *
419 * We could detect this at update-index time (the cache entry
420 * being registered/updated records the same time as "now")
421 * and delay the return from git-update-index, but that would
422 * effectively mean we can make at most one commit per second,
423 * which is not acceptable. Instead, we check cache entries
424 * whose mtime are the same as the index file timestamp more
Junio C Hamano5f730762006-02-08 21:15:24 -0800425 * carefully than others.
Junio C Hamano29e4d362005-12-20 00:02:15 -0800426 */
Junio C Hamano6d91da62008-01-21 00:44:50 -0800427 if (!changed && is_racy_timestamp(istate, ce)) {
Junio C Hamano42f77402006-08-15 21:38:07 -0700428 if (assume_racy_is_modified)
429 changed |= DATA_CHANGED;
430 else
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200431 changed |= ce_modified_check_fs(istate, ce, st);
Junio C Hamano42f77402006-08-15 21:38:07 -0700432 }
Junio C Hamano29e4d362005-12-20 00:02:15 -0800433
Linus Torvalds734aab72005-04-09 09:48:20 -0700434 return changed;
435}
436
Ben Peart883e2482017-09-22 12:35:40 -0400437int ie_modified(struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200438 const struct cache_entry *ce,
439 struct stat *st, unsigned int options)
Junio C Hamanob0391892005-09-19 15:11:15 -0700440{
Junio C Hamano29e4d362005-12-20 00:02:15 -0800441 int changed, changed_fs;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800442
443 changed = ie_match_stat(istate, ce, st, options);
Junio C Hamanob0391892005-09-19 15:11:15 -0700444 if (!changed)
445 return 0;
Junio C Hamanob0391892005-09-19 15:11:15 -0700446 /*
447 * If the mode or type has changed, there's no point in trying
448 * to refresh the entry - it's not going to match
449 */
450 if (changed & (MODE_CHANGED | TYPE_CHANGED))
451 return changed;
452
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700453 /*
454 * Immediately after read-tree or update-index --cacheinfo,
455 * the length field is zero, as we have never even read the
456 * lstat(2) information once, and we cannot trust DATA_CHANGED
457 * returned by ie_match_stat() which in turn was returned by
458 * ce_match_stat_basic() to signal that the filesize of the
459 * blob changed. We have to actually go to the filesystem to
460 * see if the contents match, and if so, should answer "unchanged".
461 *
462 * The logic does not apply to gitlinks, as ce_match_stat_basic()
463 * already has checked the actual HEAD from the filesystem in the
464 * subproject. If ie_match_stat() already said it is different,
465 * then we know it is.
Junio C Hamanob0391892005-09-19 15:11:15 -0700466 */
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700467 if ((changed & DATA_CHANGED) &&
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200468 (S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0))
Junio C Hamanob0391892005-09-19 15:11:15 -0700469 return changed;
470
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200471 changed_fs = ce_modified_check_fs(istate, ce, st);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800472 if (changed_fs)
473 return changed | changed_fs;
Junio C Hamanob0391892005-09-19 15:11:15 -0700474 return 0;
475}
476
Elijah Newrenaabc5612023-04-22 20:17:21 +0000477static int cache_name_stage_compare(const char *name1, int len1, int stage1,
478 const char *name2, int len2, int stage2)
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -0700479{
480 int cmp;
481
482 cmp = name_compare(name1, len1, name2, len2);
483 if (cmp)
484 return cmp;
Junio C Hamano5f730762006-02-08 21:15:24 -0800485
Thomas Gummererb60e1882012-07-11 11:22:37 +0200486 if (stage1 < stage2)
Linus Torvalds95fd5bf2005-04-15 22:51:44 -0700487 return -1;
Thomas Gummererb60e1882012-07-11 11:22:37 +0200488 if (stage1 > stage2)
Linus Torvalds95fd5bf2005-04-15 22:51:44 -0700489 return 1;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700490 return 0;
491}
492
Elijah Newrenaabc5612023-04-22 20:17:21 +0000493int cmp_cache_name_compare(const void *a_, const void *b_)
494{
495 const struct cache_entry *ce1, *ce2;
496
497 ce1 = *((const struct cache_entry **)a_);
498 ce2 = *((const struct cache_entry **)b_);
499 return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
500 ce2->name, ce2->ce_namelen, ce_stage(ce2));
501}
502
Victoria Dye20ec2d02021-11-29 15:52:41 +0000503static int index_name_stage_pos(struct index_state *istate,
504 const char *name, int namelen,
505 int stage,
506 enum index_search_mode search_mode)
Linus Torvaldseb38c222005-04-09 09:26:55 -0700507{
508 int first, last;
509
510 first = 0;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700511 last = istate->cache_nr;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700512 while (last > first) {
René Scharfe568a05c2019-06-13 19:51:56 +0200513 int next = first + ((last - first) >> 1);
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700514 struct cache_entry *ce = istate->cache[next];
Thomas Gummererb60e1882012-07-11 11:22:37 +0200515 int cmp = cache_name_stage_compare(name, namelen, stage, ce->name, ce_namelen(ce), ce_stage(ce));
Linus Torvaldseb38c222005-04-09 09:26:55 -0700516 if (!cmp)
Linus Torvalds76e7f4e2005-04-10 22:06:50 -0700517 return next;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700518 if (cmp < 0) {
519 last = next;
520 continue;
521 }
522 first = next+1;
523 }
Derrick Stolee95e03212021-04-01 01:49:40 +0000524
Victoria Dye20ec2d02021-11-29 15:52:41 +0000525 if (search_mode == EXPAND_SPARSE && istate->sparse_index &&
Derrick Stolee95e03212021-04-01 01:49:40 +0000526 first > 0) {
527 /* Note: first <= istate->cache_nr */
528 struct cache_entry *ce = istate->cache[first - 1];
529
530 /*
531 * If we are in a sparse-index _and_ the entry before the
532 * insertion position is a sparse-directory entry that is
533 * an ancestor of 'name', then we need to expand the index
534 * and search again. This will only trigger once, because
535 * thereafter the index is fully expanded.
536 */
537 if (S_ISSPARSEDIR(ce->ce_mode) &&
538 ce_namelen(ce) < namelen &&
539 !strncmp(name, ce->name, ce_namelen(ce))) {
540 ensure_full_index(istate);
Victoria Dye20ec2d02021-11-29 15:52:41 +0000541 return index_name_stage_pos(istate, name, namelen, stage, search_mode);
Derrick Stolee95e03212021-04-01 01:49:40 +0000542 }
543 }
544
Linus Torvalds76e7f4e2005-04-10 22:06:50 -0700545 return -first-1;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700546}
547
Derrick Stolee847a9e52021-04-01 01:49:39 +0000548int index_name_pos(struct index_state *istate, const char *name, int namelen)
Thomas Gummererb60e1882012-07-11 11:22:37 +0200549{
Victoria Dye20ec2d02021-11-29 15:52:41 +0000550 return index_name_stage_pos(istate, name, namelen, 0, EXPAND_SPARSE);
551}
552
Victoria Dye9553aa02022-08-08 19:07:51 +0000553int index_name_pos_sparse(struct index_state *istate, const char *name, int namelen)
554{
555 return index_name_stage_pos(istate, name, namelen, 0, NO_EXPAND_SPARSE);
556}
557
Victoria Dye20ec2d02021-11-29 15:52:41 +0000558int index_entry_exists(struct index_state *istate, const char *name, int namelen)
559{
560 return index_name_stage_pos(istate, name, namelen, 0, NO_EXPAND_SPARSE) >= 0;
Thomas Gummererb60e1882012-07-11 11:22:37 +0200561}
562
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700563int remove_index_entry_at(struct index_state *istate, int pos)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700564{
Linus Torvaldscf558702008-01-22 18:41:14 -0800565 struct cache_entry *ce = istate->cache[pos];
566
Junio C Hamanocfc57892009-12-25 00:30:51 -0800567 record_resolve_undo(istate, ce);
Karsten Blees20926782013-02-28 00:57:48 +0100568 remove_name_hash(istate, ce);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700569 save_or_free_index_entry(istate, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700570 istate->cache_changed |= CE_ENTRY_REMOVED;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700571 istate->cache_nr--;
572 if (pos >= istate->cache_nr)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700573 return 0;
René Scharfef331ab92017-07-15 22:00:45 +0200574 MOVE_ARRAY(istate->cache + pos, istate->cache + pos + 1,
575 istate->cache_nr - pos);
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700576 return 1;
577}
578
Kjetil Barvik36419c82009-02-18 23:18:03 +0100579/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200580 * Remove all cache entries marked for removal, that is where
Kjetil Barvik36419c82009-02-18 23:18:03 +0100581 * CE_REMOVE is set in ce_flags. This is much more effective than
582 * calling remove_index_entry_at() for each entry to be removed.
583 */
Thomas Gummerer6fdc2052018-12-20 13:48:16 +0000584void remove_marked_cache_entries(struct index_state *istate, int invalidate)
Kjetil Barvik36419c82009-02-18 23:18:03 +0100585{
586 struct cache_entry **ce_array = istate->cache;
587 unsigned int i, j;
588
589 for (i = j = 0; i < istate->cache_nr; i++) {
Karsten Blees5699d172013-11-14 20:24:37 +0100590 if (ce_array[i]->ce_flags & CE_REMOVE) {
Thomas Gummerer6fdc2052018-12-20 13:48:16 +0000591 if (invalidate) {
592 cache_tree_invalidate_path(istate,
593 ce_array[i]->name);
594 untracked_cache_remove_from_index(istate,
595 ce_array[i]->name);
596 }
Karsten Blees20926782013-02-28 00:57:48 +0100597 remove_name_hash(istate, ce_array[i]);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700598 save_or_free_index_entry(istate, ce_array[i]);
Karsten Blees5699d172013-11-14 20:24:37 +0100599 }
Kjetil Barvik36419c82009-02-18 23:18:03 +0100600 else
601 ce_array[j++] = ce_array[i];
602 }
Nguyễn Thái Ngọc Duyad837d92014-06-13 19:19:26 +0700603 if (j == istate->cache_nr)
604 return;
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700605 istate->cache_changed |= CE_ENTRY_REMOVED;
Kjetil Barvik36419c82009-02-18 23:18:03 +0100606 istate->cache_nr = j;
607}
608
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700609int remove_file_from_index(struct index_state *istate, const char *path)
Linus Torvalds197ee8c2005-04-09 12:09:27 -0700610{
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700611 int pos = index_name_pos(istate, path, strlen(path));
Junio C Hamanoc4e3cca2005-04-17 09:53:35 -0700612 if (pos < 0)
613 pos = -pos-1;
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700614 cache_tree_invalidate_path(istate, path);
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +0700615 untracked_cache_remove_from_index(istate, path);
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700616 while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path))
617 remove_index_entry_at(istate, pos);
Linus Torvalds197ee8c2005-04-09 12:09:27 -0700618 return 0;
619}
620
Johannes Schindelin20314272007-06-29 18:32:46 +0100621static int compare_name(struct cache_entry *ce, const char *path, int namelen)
622{
623 return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen);
624}
625
626static int index_name_pos_also_unmerged(struct index_state *istate,
627 const char *path, int namelen)
628{
629 int pos = index_name_pos(istate, path, namelen);
630 struct cache_entry *ce;
631
632 if (pos >= 0)
633 return pos;
634
635 /* maybe unmerged? */
636 pos = -1 - pos;
637 if (pos >= istate->cache_nr ||
638 compare_name((ce = istate->cache[pos]), path, namelen))
639 return -1;
640
641 /* order of preference: stage 2, 1, 3 */
642 if (ce_stage(ce) == 1 && pos + 1 < istate->cache_nr &&
643 ce_stage((ce = istate->cache[pos + 1])) == 2 &&
644 !compare_name(ce, path, namelen))
645 pos++;
646 return pos;
647}
648
Linus Torvalds11029522008-03-22 14:22:44 -0700649static int different_name(struct cache_entry *ce, struct cache_entry *alias)
650{
651 int len = ce_namelen(ce);
652 return ce_namelen(alias) != len || memcmp(ce->name, alias->name, len);
653}
654
655/*
656 * If we add a filename that aliases in the cache, we will use the
657 * name that we already have - but we don't want to update the same
658 * alias twice, because that implies that there were actually two
659 * different files with aliasing names!
660 *
661 * So we use the CE_ADDED flag to verify that the alias was an old
662 * one before we accept it as
663 */
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700664static struct cache_entry *create_alias_ce(struct index_state *istate,
665 struct cache_entry *ce,
666 struct cache_entry *alias)
Linus Torvalds11029522008-03-22 14:22:44 -0700667{
668 int len;
Brandon Williams285c2e22018-02-14 10:59:45 -0800669 struct cache_entry *new_entry;
Linus Torvalds11029522008-03-22 14:22:44 -0700670
671 if (alias->ce_flags & CE_ADDED)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100672 die(_("will not add file alias '%s' ('%s' already exists in index)"),
673 ce->name, alias->name);
Linus Torvalds11029522008-03-22 14:22:44 -0700674
675 /* Ok, create the new entry using the name of the existing alias */
676 len = ce_namelen(alias);
Jameson Millera8497352018-07-02 19:49:31 +0000677 new_entry = make_empty_cache_entry(istate, len);
Brandon Williams285c2e22018-02-14 10:59:45 -0800678 memcpy(new_entry->name, alias->name, len);
679 copy_cache_entry(new_entry, ce);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700680 save_or_free_index_entry(istate, ce);
Brandon Williams285c2e22018-02-14 10:59:45 -0800681 return new_entry;
Linus Torvalds11029522008-03-22 14:22:44 -0700682}
683
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700684void set_object_name_for_intent_to_add_entry(struct cache_entry *ce)
Junio C Hamano39425812008-08-21 01:44:53 -0700685{
Patryk Obaraa09c9852018-01-28 01:13:19 +0100686 struct object_id oid;
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100687 if (write_object_file("", 0, OBJ_BLOB, &oid))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100688 die(_("cannot create an empty blob in the object database"));
Patryk Obaraa09c9852018-01-28 01:13:19 +0100689 oidcpy(&ce->oid, &oid);
Junio C Hamano39425812008-08-21 01:44:53 -0700690}
691
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100692int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200693{
Jameson Millera8497352018-07-02 19:49:31 +0000694 int namelen, was_same;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700695 mode_t st_mode = st->st_mode;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100696 struct cache_entry *ce, *alias = NULL;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700697 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 -0700698 int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND);
699 int pretend = flags & ADD_CACHE_PRETEND;
Junio C Hamano39425812008-08-21 01:44:53 -0700700 int intent_only = flags & ADD_CACHE_INTENT;
701 int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|
702 (intent_only ? ADD_CACHE_NEW_ONLY : 0));
René Scharfee578d032021-10-12 21:15:50 +0200703 unsigned hash_flags = pretend ? 0 : HASH_WRITE_OBJECT;
Kyle Meyerf937bc22019-04-09 19:07:37 -0400704 struct object_id oid;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100705
Jeff King9e5da3d2019-01-17 11:27:11 -0500706 if (flags & ADD_CACHE_RENORMALIZE)
707 hash_flags |= HASH_RENORMALIZE;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200708
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700709 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 +0100710 return error(_("%s: can only add regular files, symbolic links or git-directories"), path);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200711
712 namelen = strlen(path);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700713 if (S_ISDIR(st_mode)) {
Kyle Meyerf937bc22019-04-09 19:07:37 -0400714 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
715 return error(_("'%s' does not have a commit checked out"), path);
Linus Torvalds09595252007-04-11 14:49:44 -0700716 while (namelen && path[namelen-1] == '/')
717 namelen--;
718 }
Jameson Millera8497352018-07-02 19:49:31 +0000719 ce = make_empty_cache_entry(istate, namelen);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200720 memcpy(ce->name, path, namelen);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200721 ce->ce_namelen = namelen;
Junio C Hamano39425812008-08-21 01:44:53 -0700722 if (!intent_only)
Johannes Schindelind4c0a3a2019-05-24 05:23:47 -0700723 fill_stat_cache_info(istate, ce, st);
Junio C Hamano388b2ac2008-11-28 19:55:25 -0800724 else
725 ce->ce_flags |= CE_INTENT_TO_ADD;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200726
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100727
728 if (trust_executable_bit && has_symlinks) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700729 ce->ce_mode = create_ce_mode(st_mode);
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100730 } else {
Johannes Sixt78a8d642007-03-02 22:11:30 +0100731 /* If there is an existing entry, pick the mode bits and type
732 * from it, otherwise assume unexecutable regular file.
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200733 */
Junio C Hamano185c9752007-02-16 22:43:48 -0800734 struct cache_entry *ent;
Johannes Schindelin20314272007-06-29 18:32:46 +0100735 int pos = index_name_pos_also_unmerged(istate, path, namelen);
Junio C Hamano185c9752007-02-16 22:43:48 -0800736
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700737 ent = (0 <= pos) ? istate->cache[pos] : NULL;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700738 ce->ce_mode = ce_mode_from_stat(ent, st_mode);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200739 }
740
Joshua Jensendc1ae702010-10-03 09:56:45 +0000741 /* When core.ignorecase=true, determine if a directory of the same name but differing
742 * case already exists within the Git repository. If it does, ensure the directory
743 * case of the file being added to the repository matches (is folded into) the existing
744 * entry's directory case.
745 */
746 if (ignore_case) {
David Turner41284eb2015-10-21 13:54:11 -0400747 adjust_dirname_case(istate, ce->name);
Joshua Jensendc1ae702010-10-03 09:56:45 +0000748 }
Jeff Kinge2c2a372019-02-06 21:00:22 -0500749 if (!(flags & ADD_CACHE_RENORMALIZE)) {
Torsten Bögershausen94729352017-11-16 17:38:28 +0100750 alias = index_file_exists(istate, ce->name,
751 ce_namelen(ce), ignore_case);
752 if (alias &&
753 !ce_stage(alias) &&
754 !ie_match_stat(istate, alias, st, ce_option)) {
755 /* Nothing changed, really */
756 if (!S_ISGITLINK(alias->ce_mode))
757 ce_mark_uptodate(alias);
758 alias->ce_flags |= CE_ADDED;
Joshua Jensendc1ae702010-10-03 09:56:45 +0000759
Jameson Millera8497352018-07-02 19:49:31 +0000760 discard_cache_entry(ce);
Torsten Bögershausen94729352017-11-16 17:38:28 +0100761 return 0;
762 }
Junio C Hamano0781b8a2007-07-30 17:12:58 -0700763 }
Junio C Hamano39425812008-08-21 01:44:53 -0700764 if (!intent_only) {
Junio C Hamano1c418242019-02-05 14:26:13 -0800765 if (index_path(istate, &ce->oid, path, st, hash_flags)) {
Jameson Millera8497352018-07-02 19:49:31 +0000766 discard_cache_entry(ce);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100767 return error(_("unable to index file '%s'"), path);
Stefan Beller2d9426b2015-03-20 17:28:00 -0700768 }
Junio C Hamano39425812008-08-21 01:44:53 -0700769 } else
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700770 set_object_name_for_intent_to_add_entry(ce);
Junio C Hamano39425812008-08-21 01:44:53 -0700771
Linus Torvalds11029522008-03-22 14:22:44 -0700772 if (ignore_case && alias && different_name(ce, alias))
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700773 ce = create_alias_ce(istate, ce, alias);
Linus Torvalds11029522008-03-22 14:22:44 -0700774 ce->ce_flags |= CE_ADDED;
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700775
Junio C Hamano3bf0dd12008-07-16 18:48:58 -0700776 /* It was suspected to be racily clean, but it turns out to be Ok */
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700777 was_same = (alias &&
778 !ce_stage(alias) &&
Jeff King4a7e27e2018-08-28 17:22:40 -0400779 oideq(&alias->oid, &ce->oid) &&
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700780 ce->ce_mode == alias->ce_mode);
781
782 if (pretend)
Jameson Millera8497352018-07-02 19:49:31 +0000783 discard_cache_entry(ce);
Junio C Hamano067178e2015-03-23 10:58:00 -0700784 else if (add_index_entry(istate, ce, add_option)) {
Jameson Millera8497352018-07-02 19:49:31 +0000785 discard_cache_entry(ce);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100786 return error(_("unable to add '%s' to index"), path);
Junio C Hamano067178e2015-03-23 10:58:00 -0700787 }
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700788 if (verbose && !was_same)
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200789 printf("add '%s'\n", path);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200790 return 0;
791}
792
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100793int add_file_to_index(struct index_state *istate, const char *path, int flags)
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700794{
795 struct stat st;
796 if (lstat(path, &st))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100797 die_errno(_("unable to stat '%s'"), path);
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100798 return add_to_index(istate, path, &st, flags);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700799}
800
Jameson Millera8497352018-07-02 19:49:31 +0000801struct cache_entry *make_empty_cache_entry(struct index_state *istate, size_t len)
Carlos Rica6640f882007-09-11 05:17:28 +0200802{
Jameson Miller8e72d672018-07-02 19:49:37 +0000803 return mem_pool__ce_calloc(find_mem_pool(istate), len);
Jameson Millera8497352018-07-02 19:49:31 +0000804}
805
Matheus Tavares96168822021-05-04 13:27:28 -0300806struct cache_entry *make_empty_transient_cache_entry(size_t len,
807 struct mem_pool *ce_mem_pool)
Jameson Millera8497352018-07-02 19:49:31 +0000808{
Matheus Tavares96168822021-05-04 13:27:28 -0300809 if (ce_mem_pool)
810 return mem_pool__ce_calloc(ce_mem_pool, len);
Jameson Millera8497352018-07-02 19:49:31 +0000811 return xcalloc(1, cache_entry_size(len));
812}
813
René Scharfe2a1ae642021-10-07 22:31:44 +0200814enum verify_path_result {
815 PATH_OK,
816 PATH_INVALID,
817 PATH_DIR_WITH_SEP,
818};
819
820static enum verify_path_result verify_path_internal(const char *, unsigned);
821
822int verify_path(const char *path, unsigned mode)
823{
René Scharfec8ad9d02021-10-07 22:31:58 +0200824 return verify_path_internal(path, mode) == PATH_OK;
René Scharfe2a1ae642021-10-07 22:31:44 +0200825}
826
Jameson Millera8497352018-07-02 19:49:31 +0000827struct cache_entry *make_cache_entry(struct index_state *istate,
828 unsigned int mode,
Jameson Miller825ed4d2018-07-02 19:49:30 +0000829 const struct object_id *oid,
830 const char *path,
831 int stage,
832 unsigned int refresh_options)
Carlos Rica6640f882007-09-11 05:17:28 +0200833{
Stefan Bellerbc1c2ca2015-02-17 10:06:14 -0800834 struct cache_entry *ce, *ret;
Jameson Millera8497352018-07-02 19:49:31 +0000835 int len;
Carlos Rica6640f882007-09-11 05:17:28 +0200836
René Scharfec8ad9d02021-10-07 22:31:58 +0200837 if (verify_path_internal(path, mode) == PATH_INVALID) {
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100838 error(_("invalid path '%s'"), path);
Carlos Rica6640f882007-09-11 05:17:28 +0200839 return NULL;
Dmitry Potapov7e7abea2008-10-11 20:39:37 +0400840 }
Carlos Rica6640f882007-09-11 05:17:28 +0200841
842 len = strlen(path);
Jameson Millera8497352018-07-02 19:49:31 +0000843 ce = make_empty_cache_entry(istate, len);
Carlos Rica6640f882007-09-11 05:17:28 +0200844
Jameson Miller825ed4d2018-07-02 19:49:30 +0000845 oidcpy(&ce->oid, oid);
Carlos Rica6640f882007-09-11 05:17:28 +0200846 memcpy(ce->name, path, len);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200847 ce->ce_flags = create_ce_flags(stage);
848 ce->ce_namelen = len;
Carlos Rica6640f882007-09-11 05:17:28 +0200849 ce->ce_mode = create_ce_mode(mode);
850
Nguyễn Thái Ngọc Duyd7b665c2018-09-21 17:57:25 +0200851 ret = refresh_cache_entry(istate, ce, refresh_options);
Stefan Beller915e44c2015-03-23 10:57:11 -0700852 if (ret != ce)
Jameson Millera8497352018-07-02 19:49:31 +0000853 discard_cache_entry(ce);
Stefan Beller915e44c2015-03-23 10:57:11 -0700854 return ret;
Carlos Rica6640f882007-09-11 05:17:28 +0200855}
856
Matheus Tavares96168822021-05-04 13:27:28 -0300857struct cache_entry *make_transient_cache_entry(unsigned int mode,
858 const struct object_id *oid,
859 const char *path,
860 int stage,
861 struct mem_pool *ce_mem_pool)
Jameson Millera8497352018-07-02 19:49:31 +0000862{
863 struct cache_entry *ce;
864 int len;
865
866 if (!verify_path(path, mode)) {
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100867 error(_("invalid path '%s'"), path);
Jameson Millera8497352018-07-02 19:49:31 +0000868 return NULL;
869 }
870
871 len = strlen(path);
Matheus Tavares96168822021-05-04 13:27:28 -0300872 ce = make_empty_transient_cache_entry(len, ce_mem_pool);
Jameson Millera8497352018-07-02 19:49:31 +0000873
874 oidcpy(&ce->oid, oid);
875 memcpy(ce->name, path, len);
876 ce->ce_flags = create_ce_flags(stage);
877 ce->ce_namelen = len;
878 ce->ce_mode = create_ce_mode(mode);
879
880 return ce;
881}
882
Thomas Gummererd9d70962016-09-14 22:07:46 +0100883/*
884 * Chmod an index entry with either +x or -x.
885 *
886 * Returns -1 if the chmod for the particular cache entry failed (if it's
887 * not a regular file), -2 if an invalid flip argument is passed in, 0
888 * otherwise.
889 */
890int chmod_index_entry(struct index_state *istate, struct cache_entry *ce,
891 char flip)
892{
893 if (!S_ISREG(ce->ce_mode))
894 return -1;
895 switch (flip) {
896 case '+':
897 ce->ce_mode |= 0111;
898 break;
899 case '-':
900 ce->ce_mode &= ~0111;
901 break;
902 default:
903 return -2;
904 }
905 cache_tree_invalidate_path(istate, ce->name);
906 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -0400907 mark_fsmonitor_invalid(istate, ce);
Thomas Gummererd9d70962016-09-14 22:07:46 +0100908 istate->cache_changed |= CE_ENTRY_CHANGED;
909
910 return 0;
911}
912
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700913int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700914{
915 int len = ce_namelen(a);
916 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
917}
918
Linus Torvalds12676602005-06-18 20:21:34 -0700919/*
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700920 * We fundamentally don't like some paths: we don't want
921 * dot or dot-dot anywhere, and for obvious reasons don't
922 * want to recurse into ".git" either.
923 *
924 * Also, we don't want double slashes or slashes at the
925 * end that can make pathnames ambiguous.
926 */
Jeff King10ecfa72018-05-04 20:03:35 -0400927static int verify_dotfile(const char *rest, unsigned mode)
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700928{
929 /*
930 * The first character was '.', but that
931 * has already been discarded, we now test
932 * the rest.
933 */
Theo Niessinke0f530f2011-06-08 14:04:41 +0200934
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700935 /* "." is not allowed */
Theo Niessinke0f530f2011-06-08 14:04:41 +0200936 if (*rest == '\0' || is_dir_sep(*rest))
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700937 return 0;
938
Theo Niessinke0f530f2011-06-08 14:04:41 +0200939 switch (*rest) {
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700940 /*
Jeff King641084b2018-05-15 09:56:50 -0400941 * ".git" followed by NUL or slash is bad. Note that we match
942 * case-insensitively here, even if ignore_case is not set.
943 * This outlaws ".GIT" everywhere out of an abundance of caution,
944 * since there's really no good reason to allow it.
Jeff King10ecfa72018-05-04 20:03:35 -0400945 *
946 * Once we've seen ".git", we can also find ".gitmodules", etc (also
947 * case-insensitively).
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700948 */
949 case 'g':
Jeff Kingcc2fc7c2014-11-24 13:39:12 -0500950 case 'G':
951 if (rest[1] != 'i' && rest[1] != 'I')
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700952 break;
Jeff Kingcc2fc7c2014-11-24 13:39:12 -0500953 if (rest[2] != 't' && rest[2] != 'T')
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700954 break;
Jeff Kinge19e5e62018-05-13 13:00:23 -0400955 if (rest[3] == '\0' || is_dir_sep(rest[3]))
956 return 0;
Jeff King10ecfa72018-05-04 20:03:35 -0400957 if (S_ISLNK(mode)) {
958 rest += 3;
959 if (skip_iprefix(rest, "modules", &rest) &&
960 (*rest == '\0' || is_dir_sep(*rest)))
961 return 0;
962 }
Jeff Kinge19e5e62018-05-13 13:00:23 -0400963 break;
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700964 case '.':
Theo Niessinke0f530f2011-06-08 14:04:41 +0200965 if (rest[1] == '\0' || is_dir_sep(rest[1]))
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700966 return 0;
967 }
968 return 1;
969}
970
René Scharfe2a1ae642021-10-07 22:31:44 +0200971static enum verify_path_result verify_path_internal(const char *path,
972 unsigned mode)
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700973{
Johannes Schindelin via GitGitGadget49e268e2020-01-09 13:30:34 +0000974 char c = 0;
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700975
Erik Faye-Lund56948cb2011-05-27 18:00:40 +0200976 if (has_dos_drive_prefix(path))
René Scharfe2a1ae642021-10-07 22:31:44 +0200977 return PATH_INVALID;
Erik Faye-Lund56948cb2011-05-27 18:00:40 +0200978
Johannes Schindelind2c84da2019-09-05 13:27:53 +0200979 if (!is_valid_path(path))
René Scharfe2a1ae642021-10-07 22:31:44 +0200980 return PATH_INVALID;
Johannes Schindelind2c84da2019-09-05 13:27:53 +0200981
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700982 goto inside;
983 for (;;) {
984 if (!c)
René Scharfe2a1ae642021-10-07 22:31:44 +0200985 return PATH_OK;
Erik Faye-Lund56948cb2011-05-27 18:00:40 +0200986 if (is_dir_sep(c)) {
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700987inside:
Jeff King10ecfa72018-05-04 20:03:35 -0400988 if (protect_hfs) {
Johannes Schindelin via GitGitGadget49e268e2020-01-09 13:30:34 +0000989
Jeff King10ecfa72018-05-04 20:03:35 -0400990 if (is_hfs_dotgit(path))
René Scharfe2a1ae642021-10-07 22:31:44 +0200991 return PATH_INVALID;
Jeff King10ecfa72018-05-04 20:03:35 -0400992 if (S_ISLNK(mode)) {
993 if (is_hfs_dotgitmodules(path))
René Scharfe2a1ae642021-10-07 22:31:44 +0200994 return PATH_INVALID;
Jeff King10ecfa72018-05-04 20:03:35 -0400995 }
996 }
997 if (protect_ntfs) {
Adam Dinwoodiebccc37f2021-04-29 21:11:44 +0100998#if defined GIT_WINDOWS_NATIVE || defined __CYGWIN__
Johannes Schindelin via GitGitGadget49e268e2020-01-09 13:30:34 +0000999 if (c == '\\')
René Scharfe2a1ae642021-10-07 22:31:44 +02001000 return PATH_INVALID;
Johannes Schindelin via GitGitGadget49e268e2020-01-09 13:30:34 +00001001#endif
Jeff King10ecfa72018-05-04 20:03:35 -04001002 if (is_ntfs_dotgit(path))
René Scharfe2a1ae642021-10-07 22:31:44 +02001003 return PATH_INVALID;
Jeff King10ecfa72018-05-04 20:03:35 -04001004 if (S_ISLNK(mode)) {
1005 if (is_ntfs_dotgitmodules(path))
René Scharfe2a1ae642021-10-07 22:31:44 +02001006 return PATH_INVALID;
Jeff King10ecfa72018-05-04 20:03:35 -04001007 }
1008 }
1009
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001010 c = *path++;
Jeff King10ecfa72018-05-04 20:03:35 -04001011 if ((c == '.' && !verify_dotfile(path, mode)) ||
Derrick Stolee6e773522021-03-30 13:10:55 +00001012 is_dir_sep(c))
René Scharfe2a1ae642021-10-07 22:31:44 +02001013 return PATH_INVALID;
Derrick Stolee6e773522021-03-30 13:10:55 +00001014 /*
1015 * allow terminating directory separators for
1016 * sparse directory entries.
1017 */
1018 if (c == '\0')
René Scharfe2a1ae642021-10-07 22:31:44 +02001019 return S_ISDIR(mode) ? PATH_DIR_WITH_SEP :
1020 PATH_INVALID;
Johannes Schindelin288a74b2019-09-23 08:58:11 +02001021 } else if (c == '\\' && protect_ntfs) {
1022 if (is_ntfs_dotgit(path))
René Scharfe2a1ae642021-10-07 22:31:44 +02001023 return PATH_INVALID;
Johannes Schindelin288a74b2019-09-23 08:58:11 +02001024 if (S_ISLNK(mode)) {
1025 if (is_ntfs_dotgitmodules(path))
René Scharfe2a1ae642021-10-07 22:31:44 +02001026 return PATH_INVALID;
Johannes Schindelin288a74b2019-09-23 08:58:11 +02001027 }
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001028 }
Johannes Schindelin288a74b2019-09-23 08:58:11 +02001029
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001030 c = *path++;
1031 }
1032}
1033
1034/*
Linus Torvalds12676602005-06-18 20:21:34 -07001035 * Do we have another file that has the beginning components being a
1036 * proper superset of the name we're trying to add?
1037 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001038static int has_file_name(struct index_state *istate,
1039 const struct cache_entry *ce, int pos, int ok_to_replace)
Linus Torvalds12676602005-06-18 20:21:34 -07001040{
1041 int retval = 0;
1042 int len = ce_namelen(ce);
Junio C Hamanob1557252005-06-25 02:25:29 -07001043 int stage = ce_stage(ce);
Linus Torvalds12676602005-06-18 20:21:34 -07001044 const char *name = ce->name;
1045
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001046 while (pos < istate->cache_nr) {
1047 struct cache_entry *p = istate->cache[pos++];
Linus Torvalds12676602005-06-18 20:21:34 -07001048
1049 if (len >= ce_namelen(p))
1050 break;
1051 if (memcmp(name, p->name, len))
1052 break;
Junio C Hamanob1557252005-06-25 02:25:29 -07001053 if (ce_stage(p) != stage)
1054 continue;
Linus Torvalds12676602005-06-18 20:21:34 -07001055 if (p->name[len] != '/')
1056 continue;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001057 if (p->ce_flags & CE_REMOVE)
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001058 continue;
Linus Torvalds12676602005-06-18 20:21:34 -07001059 retval = -1;
1060 if (!ok_to_replace)
1061 break;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001062 remove_index_entry_at(istate, --pos);
Linus Torvalds12676602005-06-18 20:21:34 -07001063 }
1064 return retval;
1065}
1066
Jeff Hostetlera6db3fb2017-04-14 19:12:28 +00001067
1068/*
1069 * Like strcmp(), but also return the offset of the first change.
1070 * If strings are equal, return the length.
1071 */
1072int strcmp_offset(const char *s1, const char *s2, size_t *first_change)
1073{
1074 size_t k;
1075
1076 if (!first_change)
1077 return strcmp(s1, s2);
1078
1079 for (k = 0; s1[k] == s2[k]; k++)
1080 if (s1[k] == '\0')
1081 break;
1082
1083 *first_change = k;
1084 return (unsigned char)s1[k] - (unsigned char)s2[k];
1085}
1086
Linus Torvalds12676602005-06-18 20:21:34 -07001087/*
1088 * Do we have another file with a pathname that is a proper
1089 * subset of the name we're trying to add?
Jeff Hostetler06b6d812017-04-19 17:06:17 +00001090 *
1091 * That is, is there another file in the index with a path
1092 * that matches a sub-directory in the given entry?
Linus Torvalds12676602005-06-18 20:21:34 -07001093 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001094static int has_dir_name(struct index_state *istate,
1095 const struct cache_entry *ce, int pos, int ok_to_replace)
Linus Torvalds12676602005-06-18 20:21:34 -07001096{
1097 int retval = 0;
Junio C Hamanob1557252005-06-25 02:25:29 -07001098 int stage = ce_stage(ce);
Linus Torvalds12676602005-06-18 20:21:34 -07001099 const char *name = ce->name;
1100 const char *slash = name + ce_namelen(ce);
Jeff Hostetler06b6d812017-04-19 17:06:17 +00001101 size_t len_eq_last;
1102 int cmp_last = 0;
1103
1104 /*
1105 * We are frequently called during an iteration on a sorted
1106 * list of pathnames and while building a new index. Therefore,
1107 * there is a high probability that this entry will eventually
1108 * be appended to the index, rather than inserted in the middle.
1109 * If we can confirm that, we can avoid binary searches on the
1110 * components of the pathname.
1111 *
1112 * Compare the entry's full path with the last path in the index.
1113 */
1114 if (istate->cache_nr > 0) {
1115 cmp_last = strcmp_offset(name,
1116 istate->cache[istate->cache_nr - 1]->name,
1117 &len_eq_last);
1118 if (cmp_last > 0) {
Filip Hejsekc30a5742024-01-28 04:30:25 +01001119 if (name[len_eq_last] != '/') {
Jeff Hostetler06b6d812017-04-19 17:06:17 +00001120 /*
1121 * The entry sorts AFTER the last one in the
Filip Hejsekc30a5742024-01-28 04:30:25 +01001122 * index.
1123 *
1124 * If there were a conflict with "file", then our
1125 * name would start with "file/" and the last index
1126 * entry would start with "file" but not "file/".
1127 *
1128 * The next character after common prefix is
1129 * not '/', so there can be no conflict.
Jeff Hostetler06b6d812017-04-19 17:06:17 +00001130 */
1131 return retval;
1132 } else {
1133 /*
1134 * The entry sorts AFTER the last one in the
Filip Hejsekc30a5742024-01-28 04:30:25 +01001135 * index, and the next character after common
1136 * prefix is '/'.
1137 *
1138 * Either the last index entry is a file in
1139 * conflict with this entry, or it has a name
1140 * which sorts between this entry and the
1141 * potential conflicting file.
1142 *
1143 * In both cases, we fall through to the loop
1144 * below and let the regular search code handle it.
Jeff Hostetler06b6d812017-04-19 17:06:17 +00001145 */
1146 }
1147 } else if (cmp_last == 0) {
1148 /*
1149 * The entry exactly matches the last one in the
1150 * index, but because of multiple stage and CE_REMOVE
1151 * items, we fall through and let the regular search
1152 * code handle it.
1153 */
1154 }
1155 }
Linus Torvalds12676602005-06-18 20:21:34 -07001156
1157 for (;;) {
Jeff Hostetlerb986df52017-04-19 17:06:18 +00001158 size_t len;
Linus Torvalds12676602005-06-18 20:21:34 -07001159
1160 for (;;) {
1161 if (*--slash == '/')
1162 break;
1163 if (slash <= ce->name)
1164 return retval;
1165 }
1166 len = slash - name;
1167
Victoria Dye20ec2d02021-11-29 15:52:41 +00001168 pos = index_name_stage_pos(istate, name, len, stage, EXPAND_SPARSE);
Linus Torvalds12676602005-06-18 20:21:34 -07001169 if (pos >= 0) {
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001170 /*
1171 * Found one, but not so fast. This could
1172 * be a marker that says "I was here, but
1173 * I am being removed". Such an entry is
1174 * not a part of the resulting tree, and
1175 * it is Ok to have a directory at the same
1176 * path.
1177 */
Junio C Hamano077c48d2008-01-22 21:24:21 -08001178 if (!(istate->cache[pos]->ce_flags & CE_REMOVE)) {
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001179 retval = -1;
1180 if (!ok_to_replace)
1181 break;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001182 remove_index_entry_at(istate, pos);
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001183 continue;
1184 }
Linus Torvalds12676602005-06-18 20:21:34 -07001185 }
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001186 else
1187 pos = -pos-1;
Linus Torvalds12676602005-06-18 20:21:34 -07001188
1189 /*
1190 * Trivial optimization: if we find an entry that
1191 * already matches the sub-directory, then we know
Junio C Hamanob1557252005-06-25 02:25:29 -07001192 * we're ok, and we can exit.
Linus Torvalds12676602005-06-18 20:21:34 -07001193 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001194 while (pos < istate->cache_nr) {
1195 struct cache_entry *p = istate->cache[pos];
Junio C Hamanob1557252005-06-25 02:25:29 -07001196 if ((ce_namelen(p) <= len) ||
1197 (p->name[len] != '/') ||
1198 memcmp(p->name, name, len))
1199 break; /* not our subdirectory */
Junio C Hamano077c48d2008-01-22 21:24:21 -08001200 if (ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE))
1201 /*
1202 * p is at the same stage as our entry, and
Junio C Hamanob1557252005-06-25 02:25:29 -07001203 * is a subdirectory of what we are looking
1204 * at, so we cannot have conflicts at our
1205 * level or anything shorter.
1206 */
1207 return retval;
1208 pos++;
Linus Torvalds12676602005-06-18 20:21:34 -07001209 }
1210 }
1211 return retval;
1212}
1213
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001214/* We may be in a situation where we already have path/file and path
1215 * is being added, or we already have path and path/file is being
1216 * added. Either one would result in a nonsense tree that has path
1217 * twice when git-write-tree tries to write it out. Prevent it.
Junio C Hamanoa6080a02007-06-07 00:04:01 -07001218 *
Junio C Hamano192268c2005-05-07 21:55:21 -07001219 * If ok-to-replace is specified, we remove the conflicting entries
1220 * from the cache so the caller should recompute the insert position.
1221 * When this happens, we return non-zero.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001222 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001223static int check_file_directory_conflict(struct index_state *istate,
1224 const struct cache_entry *ce,
1225 int pos, int ok_to_replace)
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001226{
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001227 int retval;
1228
1229 /*
1230 * When ce is an "I am going away" entry, we allow it to be added
1231 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001232 if (ce->ce_flags & CE_REMOVE)
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001233 return 0;
1234
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001235 /*
Linus Torvalds12676602005-06-18 20:21:34 -07001236 * We check if the path is a sub-path of a subsequent pathname
1237 * first, since removing those will not change the position
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001238 * in the array.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001239 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001240 retval = has_file_name(istate, ce, pos, ok_to_replace);
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001241
Linus Torvalds12676602005-06-18 20:21:34 -07001242 /*
1243 * Then check if the path might have a clashing sub-directory
1244 * before it.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001245 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001246 return retval + has_dir_name(istate, ce, pos, ok_to_replace);
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001247}
1248
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001249static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001250{
1251 int pos;
Junio C Hamano192268c2005-05-07 21:55:21 -07001252 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
1253 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
Junio C Hamanob1557252005-06-25 02:25:29 -07001254 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
Junio C Hamano39425812008-08-21 01:44:53 -07001255 int new_only = option & ADD_CACHE_NEW_ONLY;
Junio C Hamano5f730762006-02-08 21:15:24 -08001256
Jeff Hostetlere5494632017-04-19 17:06:16 +00001257 /*
1258 * If this entry's path sorts after the last entry in the index,
1259 * we can avoid searching for it.
1260 */
1261 if (istate->cache_nr > 0 &&
1262 strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0)
Johannes Schindelinc097b952019-10-04 08:09:26 -07001263 pos = index_pos_to_insert_pos(istate->cache_nr);
Jeff Hostetlere5494632017-04-19 17:06:16 +00001264 else
Victoria Dye20ec2d02021-11-29 15:52:41 +00001265 pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001266
Victoria Dyec35e9f52022-01-11 18:05:05 +00001267 /*
1268 * Cache tree path should be invalidated only after index_name_stage_pos,
1269 * in case it expands a sparse index.
1270 */
1271 if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1272 cache_tree_invalidate_path(istate, ce->name);
1273
Junio C Hamano3e09cdf2005-10-11 18:45:33 -07001274 /* existing match? Just replace it. */
Linus Torvalds76e7f4e2005-04-10 22:06:50 -07001275 if (pos >= 0) {
Junio C Hamano39425812008-08-21 01:44:53 -07001276 if (!new_only)
1277 replace_index_entry(istate, pos, ce);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001278 return 0;
1279 }
Linus Torvalds76e7f4e2005-04-10 22:06:50 -07001280 pos = -pos-1;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001281
Nguyễn Thái Ngọc Duyffcc9ba2015-06-07 17:40:52 +07001282 if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1283 untracked_cache_add_to_index(istate, ce->name);
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07001284
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001285 /*
1286 * Inserting a merged entry ("stage 0") into the index
1287 * will always replace all non-merged entries..
1288 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001289 if (pos < istate->cache_nr && ce_stage(ce) == 0) {
1290 while (ce_same_name(istate->cache[pos], ce)) {
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001291 ok_to_add = 1;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001292 if (!remove_index_entry_at(istate, pos))
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001293 break;
1294 }
1295 }
1296
Linus Torvalds121481a2005-04-10 11:32:54 -07001297 if (!ok_to_add)
1298 return -1;
René Scharfec8ad9d02021-10-07 22:31:58 +02001299 if (verify_path_internal(ce->name, ce->ce_mode) == PATH_INVALID)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001300 return error(_("invalid path '%s'"), ce->name);
Linus Torvalds121481a2005-04-10 11:32:54 -07001301
Junio C Hamano3e09cdf2005-10-11 18:45:33 -07001302 if (!skip_df_check &&
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001303 check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {
Junio C Hamano192268c2005-05-07 21:55:21 -07001304 if (!ok_to_replace)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001305 return error(_("'%s' appears as both a file and as a directory"),
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001306 ce->name);
Victoria Dye20ec2d02021-11-29 15:52:41 +00001307 pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE);
Junio C Hamano192268c2005-05-07 21:55:21 -07001308 pos = -pos-1;
1309 }
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001310 return pos + 1;
1311}
1312
1313int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
1314{
1315 int pos;
1316
1317 if (option & ADD_CACHE_JUST_APPEND)
1318 pos = istate->cache_nr;
1319 else {
1320 int ret;
1321 ret = add_index_entry_with_check(istate, ce, option);
1322 if (ret <= 0)
1323 return ret;
1324 pos = ret - 1;
1325 }
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001326
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001327 /* Make sure the array is big enough .. */
Dmitry S. Dolzhenko999f5662014-03-04 02:32:01 +04001328 ALLOC_GROW(istate->cache, istate->cache_nr + 1, istate->cache_alloc);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001329
1330 /* Add it in.. */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001331 istate->cache_nr++;
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001332 if (istate->cache_nr > pos + 1)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +01001333 MOVE_ARRAY(istate->cache + pos + 1, istate->cache + pos,
1334 istate->cache_nr - pos - 1);
Linus Torvaldscf558702008-01-22 18:41:14 -08001335 set_index_entry(istate, pos, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001336 istate->cache_changed |= CE_ENTRY_ADDED;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001337 return 0;
1338}
1339
Linus Torvalds405e5b22006-05-19 09:56:35 -07001340/*
1341 * "refresh" does not calculate a new sha1 file or bring the
1342 * cache up-to-date for mode/content changes. But what it
1343 * _does_ do is to "re-match" the stat information of a file
1344 * with the cache, so that you can refresh the cache for a
1345 * file that hasn't been changed but where the stat entry is
1346 * out of date.
1347 *
1348 * For example, you'd want to do this after doing a "git-read-tree",
1349 * to link up the stat cache details with the proper files.
1350 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001351static struct cache_entry *refresh_cache_ent(struct index_state *istate,
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001352 struct cache_entry *ce,
Jeff Kingd05e6972011-11-18 06:11:08 -05001353 unsigned int options, int *err,
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001354 int *changed_ret,
Jeff Hostetler15268d12021-02-03 15:34:46 +00001355 int *t2_did_lstat,
1356 int *t2_did_scan)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001357{
1358 struct stat st;
1359 struct cache_entry *updated;
Jameson Millera8497352018-07-02 19:49:31 +00001360 int changed;
Brad King25762722014-01-27 09:45:08 -05001361 int refresh = options & CE_MATCH_REFRESH;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001362 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001363 int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
Brad King2e2e7ec2014-01-27 09:45:07 -05001364 int ignore_missing = options & CE_MATCH_IGNORE_MISSING;
Ben Peart883e2482017-09-22 12:35:40 -04001365 int ignore_fsmonitor = options & CE_MATCH_IGNORE_FSMONITOR;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001366
Brad King25762722014-01-27 09:45:08 -05001367 if (!refresh || ce_uptodate(ce))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001368 return ce;
1369
Ben Peart883e2482017-09-22 12:35:40 -04001370 if (!ignore_fsmonitor)
1371 refresh_fsmonitor(istate);
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001372 /*
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001373 * CE_VALID or CE_SKIP_WORKTREE means the user promised us
1374 * that the change to the work tree does not matter and told
1375 * us not to worry.
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001376 */
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001377 if (!ignore_skip_worktree && ce_skip_worktree(ce)) {
1378 ce_mark_uptodate(ce);
1379 return ce;
1380 }
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001381 if (!ignore_valid && (ce->ce_flags & CE_VALID)) {
1382 ce_mark_uptodate(ce);
1383 return ce;
1384 }
Ben Peart883e2482017-09-22 12:35:40 -04001385 if (!ignore_fsmonitor && (ce->ce_flags & CE_FSMONITOR_VALID)) {
1386 ce_mark_uptodate(ce);
1387 return ce;
1388 }
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001389
René Scharfeccad42d2014-08-09 19:43:29 +02001390 if (has_symlink_leading_path(ce->name, ce_namelen(ce))) {
1391 if (ignore_missing)
1392 return ce;
1393 if (err)
1394 *err = ENOENT;
1395 return NULL;
1396 }
1397
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001398 if (t2_did_lstat)
1399 *t2_did_lstat = 1;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001400 if (lstat(ce->name, &st) < 0) {
Brad King2e2e7ec2014-01-27 09:45:07 -05001401 if (ignore_missing && errno == ENOENT)
1402 return ce;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001403 if (err)
1404 *err = errno;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001405 return NULL;
1406 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001407
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001408 changed = ie_match_stat(istate, ce, &st, options);
Jeff Kingd05e6972011-11-18 06:11:08 -05001409 if (changed_ret)
1410 *changed_ret = changed;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001411 if (!changed) {
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001412 /*
1413 * The path is unchanged. If we were told to ignore
1414 * valid bit, then we did the actual stat check and
1415 * found that the entry is unmodified. If the entry
1416 * is not marked VALID, this is the place to mark it
1417 * valid again, under "assume unchanged" mode.
1418 */
1419 if (ignore_valid && assume_unchanged &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001420 !(ce->ce_flags & CE_VALID))
Linus Torvalds405e5b22006-05-19 09:56:35 -07001421 ; /* mark this one VALID again */
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001422 else {
1423 /*
1424 * We do not mark the index itself "modified"
1425 * because CE_UPTODATE flag is in-core only;
1426 * we are not going to write this change out.
1427 */
Ben Peart883e2482017-09-22 12:35:40 -04001428 if (!S_ISGITLINK(ce->ce_mode)) {
Junio C Hamano125fd982010-01-24 00:10:20 -08001429 ce_mark_uptodate(ce);
Johannes Schindelinb5a81692019-05-24 05:23:48 -07001430 mark_fsmonitor_valid(istate, ce);
Ben Peart883e2482017-09-22 12:35:40 -04001431 }
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001432 return ce;
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001433 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001434 }
1435
Jeff Hostetler15268d12021-02-03 15:34:46 +00001436 if (t2_did_scan)
1437 *t2_did_scan = 1;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001438 if (ie_modified(istate, ce, &st, options)) {
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001439 if (err)
1440 *err = EINVAL;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001441 return NULL;
1442 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001443
Jameson Millera8497352018-07-02 19:49:31 +00001444 updated = make_empty_cache_entry(istate, ce_namelen(ce));
Ben Peart0e267b72018-03-15 11:25:20 -04001445 copy_cache_entry(updated, ce);
1446 memcpy(updated->name, ce->name, ce->ce_namelen + 1);
Johannes Schindelind4c0a3a2019-05-24 05:23:47 -07001447 fill_stat_cache_info(istate, updated, &st);
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001448 /*
1449 * If ignore_valid is not set, we should leave CE_VALID bit
1450 * alone. Otherwise, paths marked with --no-assume-unchanged
1451 * (i.e. things to be edited) will reacquire CE_VALID bit
1452 * automatically, which is not really what we want.
Linus Torvalds405e5b22006-05-19 09:56:35 -07001453 */
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001454 if (!ignore_valid && assume_unchanged &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001455 !(ce->ce_flags & CE_VALID))
1456 updated->ce_flags &= ~CE_VALID;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001457
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001458 /* istate->cache_changed is updated in the caller */
Linus Torvalds405e5b22006-05-19 09:56:35 -07001459 return updated;
1460}
1461
Matthieu Moy3deffc52009-08-21 10:57:59 +02001462static void show_file(const char * fmt, const char * name, int in_porcelain,
Jonathan Nieder046613c2011-02-22 22:43:23 +00001463 int * first, const char *header_msg)
Matthieu Moy3deffc52009-08-21 10:57:59 +02001464{
1465 if (in_porcelain && *first && header_msg) {
1466 printf("%s\n", header_msg);
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -07001467 *first = 0;
Matthieu Moy3deffc52009-08-21 10:57:59 +02001468 }
1469 printf(fmt, name);
1470}
1471
Thomas Gummerer22184492019-09-11 19:20:25 +01001472int repo_refresh_and_write_index(struct repository *repo,
1473 unsigned int refresh_flags,
1474 unsigned int write_flags,
1475 int gentle,
1476 const struct pathspec *pathspec,
1477 char *seen, const char *header_msg)
1478{
1479 struct lock_file lock_file = LOCK_INIT;
1480 int fd, ret = 0;
1481
1482 fd = repo_hold_locked_index(repo, &lock_file, 0);
1483 if (!gentle && fd < 0)
1484 return -1;
1485 if (refresh_index(repo->index, refresh_flags, pathspec, seen, header_msg))
1486 ret = 1;
1487 if (0 <= fd && write_locked_index(repo->index, &lock_file, COMMIT_LOCK | write_flags))
1488 ret = -1;
1489 return ret;
1490}
1491
1492
Nguyễn Thái Ngọc Duy9b2d6142013-07-14 15:35:54 +07001493int refresh_index(struct index_state *istate, unsigned int flags,
1494 const struct pathspec *pathspec,
Jonathan Nieder046613c2011-02-22 22:43:23 +00001495 char *seen, const char *header_msg)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001496{
1497 int i;
1498 int has_errors = 0;
1499 int really = (flags & REFRESH_REALLY) != 0;
1500 int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
1501 int quiet = (flags & REFRESH_QUIET) != 0;
1502 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
Johannes Schindelin5fdeacb2008-05-14 18:03:45 +01001503 int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0;
Matheus Tavaresb2430122021-04-08 17:41:26 -03001504 int ignore_skip_worktree = (flags & REFRESH_IGNORE_SKIP_WORKTREE) != 0;
Matthieu Moy3deffc52009-08-21 10:57:59 +02001505 int first = 1;
1506 int in_porcelain = (flags & REFRESH_IN_PORCELAIN);
Brad King25762722014-01-27 09:45:08 -05001507 unsigned int options = (CE_MATCH_REFRESH |
1508 (really ? CE_MATCH_IGNORE_VALID : 0) |
Brad King2e2e7ec2014-01-27 09:45:07 -05001509 (not_new ? CE_MATCH_IGNORE_MISSING : 0));
Jeff King4bd4e732011-11-18 06:11:28 -05001510 const char *modified_fmt;
Jeff King73b7eae2011-11-18 06:13:08 -05001511 const char *deleted_fmt;
1512 const char *typechange_fmt;
1513 const char *added_fmt;
Jeff King4bd4e732011-11-18 06:11:28 -05001514 const char *unmerged_fmt;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001515 struct progress *progress = NULL;
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001516 int t2_sum_lstat = 0;
Jeff Hostetler15268d12021-02-03 15:34:46 +00001517 int t2_sum_scan = 0;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001518
1519 if (flags & REFRESH_PROGRESS && isatty(2))
1520 progress = start_delayed_progress(_("Refresh index"),
1521 istate->cache_nr);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001522
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02001523 trace_performance_enter();
Nguyễn Thái Ngọc Duya71806a2018-11-10 06:16:06 +01001524 modified_fmt = in_porcelain ? "M\t%s\n" : "%s: needs update\n";
1525 deleted_fmt = in_porcelain ? "D\t%s\n" : "%s: needs update\n";
1526 typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
1527 added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
1528 unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
Ben Peart99ce7202018-10-29 16:41:59 -04001529 /*
1530 * Use the multi-threaded preload_index() to refresh most of the
1531 * cache entries quickly then in the single threaded loop below,
1532 * we only have to do the special cases that are left.
1533 */
1534 preload_index(istate, pathspec, 0);
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001535 trace2_region_enter("index", "refresh", NULL);
Derrick Stoleed76723e2021-07-14 13:12:37 +00001536
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001537 for (i = 0; i < istate->cache_nr; i++) {
Brandon Williams285c2e22018-02-14 10:59:45 -08001538 struct cache_entry *ce, *new_entry;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001539 int cache_errno = 0;
Jeff King73b7eae2011-11-18 06:13:08 -05001540 int changed = 0;
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001541 int filtered = 0;
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001542 int t2_did_lstat = 0;
Jeff Hostetler15268d12021-02-03 15:34:46 +00001543 int t2_did_scan = 0;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001544
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001545 ce = istate->cache[i];
Johannes Schindelin5fdeacb2008-05-14 18:03:45 +01001546 if (ignore_submodules && S_ISGITLINK(ce->ce_mode))
1547 continue;
Matheus Tavaresb2430122021-04-08 17:41:26 -03001548 if (ignore_skip_worktree && ce_skip_worktree(ce))
1549 continue;
Johannes Schindelin5fdeacb2008-05-14 18:03:45 +01001550
Derrick Stoleed76723e2021-07-14 13:12:37 +00001551 /*
1552 * If this entry is a sparse directory, then there isn't
1553 * any stat() information to update. Ignore the entry.
1554 */
1555 if (S_ISSPARSEDIR(ce->ce_mode))
1556 continue;
1557
Nguyễn Thái Ngọc Duyd7b665c2018-09-21 17:57:25 +02001558 if (pathspec && !ce_path_match(istate, ce, pathspec, seen))
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001559 filtered = 1;
1560
Linus Torvalds405e5b22006-05-19 09:56:35 -07001561 if (ce_stage(ce)) {
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001562 while ((i < istate->cache_nr) &&
1563 ! strcmp(istate->cache[i]->name, ce->name))
Linus Torvalds405e5b22006-05-19 09:56:35 -07001564 i++;
1565 i--;
1566 if (allow_unmerged)
1567 continue;
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001568 if (!filtered)
1569 show_file(unmerged_fmt, ce->name, in_porcelain,
1570 &first, header_msg);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001571 has_errors = 1;
1572 continue;
1573 }
1574
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001575 if (filtered)
Alexandre Julliardd6168132007-08-11 23:59:01 +02001576 continue;
1577
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001578 new_entry = refresh_cache_ent(istate, ce, options,
1579 &cache_errno, &changed,
Jeff Hostetler15268d12021-02-03 15:34:46 +00001580 &t2_did_lstat, &t2_did_scan);
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001581 t2_sum_lstat += t2_did_lstat;
Jeff Hostetler15268d12021-02-03 15:34:46 +00001582 t2_sum_scan += t2_did_scan;
Brandon Williams285c2e22018-02-14 10:59:45 -08001583 if (new_entry == ce)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001584 continue;
Ævar Arnfjörð Bjarmasonb7b793d2021-06-07 16:43:22 +02001585 display_progress(progress, i);
Brandon Williams285c2e22018-02-14 10:59:45 -08001586 if (!new_entry) {
Jeff King73b7eae2011-11-18 06:13:08 -05001587 const char *fmt;
1588
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001589 if (really && cache_errno == EINVAL) {
Linus Torvalds405e5b22006-05-19 09:56:35 -07001590 /* If we are doing --really-refresh that
1591 * means the index is not valid anymore.
1592 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001593 ce->ce_flags &= ~CE_VALID;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +07001594 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -04001595 mark_fsmonitor_invalid(istate, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001596 istate->cache_changed |= CE_ENTRY_CHANGED;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001597 }
1598 if (quiet)
1599 continue;
Jeff King73b7eae2011-11-18 06:13:08 -05001600
1601 if (cache_errno == ENOENT)
1602 fmt = deleted_fmt;
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +07001603 else if (ce_intent_to_add(ce))
Jeff King73b7eae2011-11-18 06:13:08 -05001604 fmt = added_fmt; /* must be before other checks */
1605 else if (changed & TYPE_CHANGED)
1606 fmt = typechange_fmt;
1607 else
1608 fmt = modified_fmt;
1609 show_file(fmt,
1610 ce->name, in_porcelain, &first, header_msg);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001611 has_errors = 1;
1612 continue;
1613 }
Linus Torvaldscf558702008-01-22 18:41:14 -08001614
Brandon Williams285c2e22018-02-14 10:59:45 -08001615 replace_index_entry(istate, i, new_entry);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001616 }
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001617 trace2_data_intmax("index", NULL, "refresh/sum_lstat", t2_sum_lstat);
Jeff Hostetler15268d12021-02-03 15:34:46 +00001618 trace2_data_intmax("index", NULL, "refresh/sum_scan", t2_sum_scan);
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001619 trace2_region_leave("index", "refresh", NULL);
Ævar Arnfjörð Bjarmasonb7b793d2021-06-07 16:43:22 +02001620 display_progress(progress, istate->cache_nr);
1621 stop_progress(&progress);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02001622 trace_performance_leave("refresh index");
Linus Torvalds405e5b22006-05-19 09:56:35 -07001623 return has_errors;
1624}
1625
Jameson Miller768d7962018-07-02 19:49:29 +00001626struct cache_entry *refresh_cache_entry(struct index_state *istate,
1627 struct cache_entry *ce,
1628 unsigned int options)
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001629{
Jeff Hostetler15268d12021-02-03 15:34:46 +00001630 return refresh_cache_ent(istate, ce, options, NULL, NULL, NULL, NULL);
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001631}
1632
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001633
1634/*****************************************************************
1635 * Index File I/O
1636 *****************************************************************/
1637
Junio C Hamano9d227782012-04-04 09:12:43 -07001638#define INDEX_FORMAT_DEFAULT 3
1639
Derrick Stolee7211b9e2019-08-13 11:37:43 -07001640static unsigned int get_index_format_default(struct repository *r)
Thomas Gummerer136347d2014-02-23 21:49:57 +01001641{
1642 char *envversion = getenv("GIT_INDEX_VERSION");
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001643 char *endp;
1644 unsigned int version = INDEX_FORMAT_DEFAULT;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001645
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001646 if (!envversion) {
Derrick Stolee7211b9e2019-08-13 11:37:43 -07001647 prepare_repo_settings(r);
1648
1649 if (r->settings.index_version >= 0)
1650 version = r->settings.index_version;
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001651 if (version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1652 warning(_("index.version set, but the value is invalid.\n"
Thomas Gummerer136347d2014-02-23 21:49:57 +01001653 "Using version %i"), INDEX_FORMAT_DEFAULT);
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001654 return INDEX_FORMAT_DEFAULT;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001655 }
1656 return version;
1657 }
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001658
1659 version = strtoul(envversion, &endp, 10);
1660 if (*endp ||
1661 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1662 warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1663 "Using version %i"), INDEX_FORMAT_DEFAULT);
1664 version = INDEX_FORMAT_DEFAULT;
1665 }
1666 return version;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001667}
1668
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001669/*
1670 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
1671 * Again - this is just a (very strong in practice) heuristic that
1672 * the inode hasn't changed.
1673 *
1674 * We save the fields in big-endian order to allow using the
1675 * index file over NFS transparently.
1676 */
1677struct ondisk_cache_entry {
1678 struct cache_time ctime;
1679 struct cache_time mtime;
Thomas Gummerer7800c1e2013-08-18 21:41:51 +02001680 uint32_t dev;
1681 uint32_t ino;
1682 uint32_t mode;
1683 uint32_t uid;
1684 uint32_t gid;
1685 uint32_t size;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001686 /*
1687 * unsigned char hash[hashsz];
1688 * uint16_t flags;
1689 * if (flags & CE_EXTENDED)
1690 * uint16_t flags2;
1691 */
1692 unsigned char data[GIT_MAX_RAWSZ + 2 * sizeof(uint16_t)];
1693 char name[FLEX_ARRAY];
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001694};
1695
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001696/* These are only used for v3 or lower */
Kevin Willfordce012de2017-08-21 15:24:32 -06001697#define align_padding_size(size, len) ((size + (len) + 8) & ~7) - (size + len)
brian m. carlson575fa8a2019-02-19 00:05:24 +00001698#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,data) + (len) + 8) & ~7)
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001699#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)
brian m. carlson575fa8a2019-02-19 00:05:24 +00001700#define ondisk_data_size(flags, len) (the_hash_algo->rawsz + \
1701 ((flags & CE_EXTENDED) ? 2 : 1) * sizeof(uint16_t) + len)
1702#define ondisk_data_size_max(len) (ondisk_data_size(CE_EXTENDED, len))
1703#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 -07001704
Jeff Hostetlera33fc722017-04-14 20:32:21 +00001705/* Allow fsck to force verification of the index checksum. */
1706int verify_index_checksum;
1707
Ben Peart00ec50e2017-10-18 10:27:25 -04001708/* Allow fsck to force verification of the cache entry order. */
1709int verify_ce_order;
1710
Ben Peart371ed0d2018-10-10 11:59:33 -04001711static int verify_hdr(const struct cache_header *hdr, unsigned long size)
Linus Torvaldse83c5162005-04-07 15:13:13 -07001712{
brian m. carlsonaab61352018-02-01 02:18:45 +00001713 git_hash_ctx c;
1714 unsigned char hash[GIT_MAX_RAWSZ];
Junio C Hamano0136bac2012-04-03 15:53:12 -07001715 int hdr_version;
Derrick Stoleeee1f0c22023-01-06 16:31:54 +00001716 unsigned char *start, *end;
1717 struct object_id oid;
Linus Torvaldse83c5162005-04-07 15:13:13 -07001718
Linus Torvaldsccc4feb2005-04-15 10:44:27 -07001719 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001720 return error(_("bad signature 0x%08x"), hdr->hdr_signature);
Junio C Hamano0136bac2012-04-03 15:53:12 -07001721 hdr_version = ntohl(hdr->hdr_version);
Nguyễn Thái Ngọc Duyb82a7b52013-02-22 19:09:24 +07001722 if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001723 return error(_("bad index version %d"), hdr_version);
Jeff Hostetlera33fc722017-04-14 20:32:21 +00001724
1725 if (!verify_index_checksum)
1726 return 0;
1727
Derrick Stoleeee1f0c22023-01-06 16:31:54 +00001728 end = (unsigned char *)hdr + size;
1729 start = end - the_hash_algo->rawsz;
1730 oidread(&oid, start);
1731 if (oideq(&oid, null_oid()))
1732 return 0;
1733
brian m. carlsonaab61352018-02-01 02:18:45 +00001734 the_hash_algo->init_fn(&c);
1735 the_hash_algo->update_fn(&c, hdr, size - the_hash_algo->rawsz);
1736 the_hash_algo->final_fn(hash, &c);
Derrick Stoleeee1f0c22023-01-06 16:31:54 +00001737 if (!hasheq(hash, start))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001738 return error(_("bad index file sha1 signature"));
Linus Torvaldse83c5162005-04-07 15:13:13 -07001739 return 0;
1740}
1741
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001742static int read_index_extension(struct index_state *istate,
Ben Peart371ed0d2018-10-10 11:59:33 -04001743 const char *ext, const char *data, unsigned long sz)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001744{
1745 switch (CACHE_EXT(ext)) {
1746 case CACHE_EXT_TREE:
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001747 istate->cache_tree = cache_tree_read(data, sz);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001748 break;
Junio C Hamanocfc57892009-12-25 00:30:51 -08001749 case CACHE_EXT_RESOLVE_UNDO:
1750 istate->resolve_undo = resolve_undo_read(data, sz);
1751 break;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001752 case CACHE_EXT_LINK:
1753 if (read_link_extension(istate, data, sz))
1754 return -1;
1755 break;
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07001756 case CACHE_EXT_UNTRACKED:
1757 istate->untracked = read_untracked_extension(data, sz);
1758 break;
Ben Peart883e2482017-09-22 12:35:40 -04001759 case CACHE_EXT_FSMONITOR:
1760 read_fsmonitor_extension(istate, data, sz);
1761 break;
Ben Peart3b1d9e02018-10-10 11:59:34 -04001762 case CACHE_EXT_ENDOFINDEXENTRIES:
Ben Peart32550892018-10-10 11:59:37 -04001763 case CACHE_EXT_INDEXENTRYOFFSETTABLE:
Ben Peart3b1d9e02018-10-10 11:59:34 -04001764 /* already handled in do_read_index() */
1765 break;
Derrick Stoleecd424152021-03-30 13:10:54 +00001766 case CACHE_EXT_SPARSE_DIRECTORIES:
1767 /* no content, only an indicator */
Derrick Stolee9fadb372022-05-23 13:48:40 +00001768 istate->sparse_index = INDEX_COLLAPSED;
Derrick Stoleecd424152021-03-30 13:10:54 +00001769 break;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001770 default:
1771 if (*ext < 'A' || 'Z' < *ext)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001772 return error(_("index uses %.4s extension, which we do not understand"),
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001773 ext);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001774 fprintf_ln(stderr, _("ignoring %.4s extension"), ext);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001775 break;
1776 }
1777 return 0;
1778}
1779
Victoria Dye4a6ed302022-09-28 17:19:00 +00001780/*
1781 * Parses the contents of the cache entry contained within the 'ondisk' buffer
1782 * into a new incore 'cache_entry'.
1783 *
1784 * Note that 'char *ondisk' may not be aligned to a 4-byte address interval in
1785 * index v4, so we cannot cast it to 'struct ondisk_cache_entry *' and access
1786 * its members. Instead, we use the byte offsets of members within the struct to
1787 * identify where 'get_be16()', 'get_be32()', and 'oidread()' (which can all
1788 * read from an unaligned memory buffer) should read from the 'ondisk' buffer
1789 * into the corresponding incore 'cache_entry' members.
1790 */
Ben Peart77ff1122018-10-10 11:59:38 -04001791static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool,
1792 unsigned int version,
Victoria Dye4a6ed302022-09-28 17:19:00 +00001793 const char *ondisk,
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001794 unsigned long *ent_size,
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001795 const struct cache_entry *previous_ce)
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001796{
René Scharfedebed2a2011-10-24 23:59:14 +02001797 struct cache_entry *ce;
Junio C Hamano7fec10b2008-01-18 23:42:00 -08001798 size_t len;
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07001799 const char *name;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001800 const unsigned hashsz = the_hash_algo->rawsz;
Victoria Dye4a6ed302022-09-28 17:19:00 +00001801 const char *flagsp = ondisk + offsetof(struct ondisk_cache_entry, data) + hashsz;
René Scharfedebed2a2011-10-24 23:59:14 +02001802 unsigned int flags;
Nguyễn Thái Ngọc Duyf5c4a9a2018-11-03 09:48:49 +01001803 size_t copy_len = 0;
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001804 /*
1805 * Adjacent cache entries tend to share the leading paths, so it makes
1806 * sense to only store the differences in later entries. In the v4
1807 * on-disk format of the index, each on-disk cache entry stores the
1808 * number of bytes to be stripped from the end of the previous name,
1809 * and the bytes to append to the result, to come up with its name.
1810 */
Ben Peart77ff1122018-10-10 11:59:38 -04001811 int expand_name_field = version == 4;
René Scharfedebed2a2011-10-24 23:59:14 +02001812
1813 /* On-disk flags are just 16 bits */
brian m. carlson575fa8a2019-02-19 00:05:24 +00001814 flags = get_be16(flagsp);
René Scharfedebed2a2011-10-24 23:59:14 +02001815 len = flags & CE_NAMEMASK;
1816
1817 if (flags & CE_EXTENDED) {
René Scharfedebed2a2011-10-24 23:59:14 +02001818 int extended_flags;
Victoria Dye4a6ed302022-09-28 17:19:00 +00001819 extended_flags = get_be16(flagsp + sizeof(uint16_t)) << 16;
René Scharfedebed2a2011-10-24 23:59:14 +02001820 /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1821 if (extended_flags & ~CE_EXTENDED_FLAGS)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001822 die(_("unknown index entry format 0x%08x"), extended_flags);
René Scharfedebed2a2011-10-24 23:59:14 +02001823 flags |= extended_flags;
Victoria Dye4a6ed302022-09-28 17:19:00 +00001824 name = (const char *)(flagsp + 2 * sizeof(uint16_t));
René Scharfedebed2a2011-10-24 23:59:14 +02001825 }
1826 else
Victoria Dye4a6ed302022-09-28 17:19:00 +00001827 name = (const char *)(flagsp + sizeof(uint16_t));
René Scharfedebed2a2011-10-24 23:59:14 +02001828
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001829 if (expand_name_field) {
1830 const unsigned char *cp = (const unsigned char *)name;
1831 size_t strip_len, previous_len;
René Scharfedebed2a2011-10-24 23:59:14 +02001832
Elijah Newren15beaaa2019-11-05 17:07:23 +00001833 /* 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 -04001834 strip_len = decode_varint(&cp);
Ben Peart77ff1122018-10-10 11:59:38 -04001835 if (previous_ce) {
1836 previous_len = previous_ce->ce_namelen;
1837 if (previous_len < strip_len)
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001838 die(_("malformed name field in the index, near path '%s'"),
Ben Peart77ff1122018-10-10 11:59:38 -04001839 previous_ce->name);
1840 copy_len = previous_len - strip_len;
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001841 }
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001842 name = (const char *)cp;
1843 }
1844
1845 if (len == CE_NAMEMASK) {
1846 len = strlen(name);
1847 if (expand_name_field)
1848 len += copy_len;
1849 }
1850
Ben Peart77ff1122018-10-10 11:59:38 -04001851 ce = mem_pool__ce_alloc(ce_mem_pool, len);
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001852
Victoria Dye4a6ed302022-09-28 17:19:00 +00001853 /*
1854 * NEEDSWORK: using 'offsetof()' is cumbersome and should be replaced
1855 * with something more akin to 'load_bitmap_entries_v1()'s use of
1856 * 'read_be16'/'read_be32'. For consistency with the corresponding
1857 * ondisk entry write function ('copy_cache_entry_to_ondisk()'), this
1858 * should be done at the same time as removing references to
1859 * 'ondisk_cache_entry' there.
1860 */
1861 ce->ce_stat_data.sd_ctime.sec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, ctime)
1862 + offsetof(struct cache_time, sec));
1863 ce->ce_stat_data.sd_mtime.sec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, mtime)
1864 + offsetof(struct cache_time, sec));
1865 ce->ce_stat_data.sd_ctime.nsec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, ctime)
1866 + offsetof(struct cache_time, nsec));
1867 ce->ce_stat_data.sd_mtime.nsec = get_be32(ondisk + offsetof(struct ondisk_cache_entry, mtime)
1868 + offsetof(struct cache_time, nsec));
1869 ce->ce_stat_data.sd_dev = get_be32(ondisk + offsetof(struct ondisk_cache_entry, dev));
1870 ce->ce_stat_data.sd_ino = get_be32(ondisk + offsetof(struct ondisk_cache_entry, ino));
1871 ce->ce_mode = get_be32(ondisk + offsetof(struct ondisk_cache_entry, mode));
1872 ce->ce_stat_data.sd_uid = get_be32(ondisk + offsetof(struct ondisk_cache_entry, uid));
1873 ce->ce_stat_data.sd_gid = get_be32(ondisk + offsetof(struct ondisk_cache_entry, gid));
1874 ce->ce_stat_data.sd_size = get_be32(ondisk + offsetof(struct ondisk_cache_entry, size));
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001875 ce->ce_flags = flags & ~CE_NAMEMASK;
1876 ce->ce_namelen = len;
1877 ce->index = 0;
Victoria Dye4a6ed302022-09-28 17:19:00 +00001878 oidread(&ce->oid, (const unsigned char *)ondisk + offsetof(struct ondisk_cache_entry, data));
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001879
1880 if (expand_name_field) {
1881 if (copy_len)
1882 memcpy(ce->name, previous_ce->name, copy_len);
1883 memcpy(ce->name + copy_len, name, len + 1 - copy_len);
1884 *ent_size = (name - ((char *)ondisk)) + len + 1 - copy_len;
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001885 } else {
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001886 memcpy(ce->name, name, len + 1);
1887 *ent_size = ondisk_ce_size(ce);
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001888 }
René Scharfedebed2a2011-10-24 23:59:14 +02001889 return ce;
Linus Torvaldscf558702008-01-22 18:41:14 -08001890}
1891
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001892static void check_ce_order(struct index_state *istate)
Jaime Soriano Pastor15999d02014-08-29 10:54:41 +02001893{
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001894 unsigned int i;
1895
Ben Peart00ec50e2017-10-18 10:27:25 -04001896 if (!verify_ce_order)
1897 return;
1898
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001899 for (i = 1; i < istate->cache_nr; i++) {
1900 struct cache_entry *ce = istate->cache[i - 1];
1901 struct cache_entry *next_ce = istate->cache[i];
1902 int name_compare = strcmp(ce->name, next_ce->name);
1903
1904 if (0 < name_compare)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001905 die(_("unordered stage entries in index"));
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001906 if (!name_compare) {
1907 if (!ce_stage(ce))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001908 die(_("multiple stage entries for merged file '%s'"),
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001909 ce->name);
1910 if (ce_stage(ce) > ce_stage(next_ce))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001911 die(_("unordered stage entries for '%s'"),
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001912 ce->name);
1913 }
Jaime Soriano Pastor15999d02014-08-29 10:54:41 +02001914 }
1915}
1916
Christian Couder435ec092016-01-27 07:58:05 +01001917static void tweak_untracked_cache(struct index_state *istate)
1918{
Derrick Stoleead0fb652019-08-13 11:37:46 -07001919 struct repository *r = the_repository;
1920
1921 prepare_repo_settings(r);
1922
Ævar Arnfjörð Bjarmasonf1bee822021-09-21 15:13:01 +02001923 switch (r->settings.core_untracked_cache) {
1924 case UNTRACKED_CACHE_REMOVE:
Christian Couder435ec092016-01-27 07:58:05 +01001925 remove_untracked_cache(istate);
Ævar Arnfjörð Bjarmasonf1bee822021-09-21 15:13:01 +02001926 break;
1927 case UNTRACKED_CACHE_WRITE:
Derrick Stoleead0fb652019-08-13 11:37:46 -07001928 add_untracked_cache(istate);
Ævar Arnfjörð Bjarmasonf1bee822021-09-21 15:13:01 +02001929 break;
1930 case UNTRACKED_CACHE_KEEP:
Ævar Arnfjörð Bjarmason3050b6d2021-09-21 15:13:02 +02001931 /*
1932 * Either an explicit "core.untrackedCache=keep", the
1933 * default if "core.untrackedCache" isn't configured,
1934 * or a fallback on an unknown "core.untrackedCache"
1935 * value.
1936 */
Ævar Arnfjörð Bjarmasonf1bee822021-09-21 15:13:01 +02001937 break;
Ævar Arnfjörð Bjarmasonf1bee822021-09-21 15:13:01 +02001938 }
Christian Couder435ec092016-01-27 07:58:05 +01001939}
1940
Christian Couder43925312017-02-27 19:00:02 +01001941static void tweak_split_index(struct index_state *istate)
1942{
1943 switch (git_config_get_split_index()) {
1944 case -1: /* unset: do nothing */
1945 break;
1946 case 0: /* false */
1947 remove_split_index(istate);
1948 break;
1949 case 1: /* true */
1950 add_split_index(istate);
1951 break;
1952 default: /* unknown value: do nothing */
1953 break;
1954 }
1955}
1956
Christian Couder435ec092016-01-27 07:58:05 +01001957static void post_read_index_from(struct index_state *istate)
1958{
1959 check_ce_order(istate);
1960 tweak_untracked_cache(istate);
Christian Couder43925312017-02-27 19:00:02 +01001961 tweak_split_index(istate);
Ben Peart883e2482017-09-22 12:35:40 -04001962 tweak_fsmonitor(istate);
Christian Couder435ec092016-01-27 07:58:05 +01001963}
1964
Jameson Miller8e72d672018-07-02 19:49:37 +00001965static size_t estimate_cache_size_from_compressed(unsigned int entries)
1966{
1967 return entries * (sizeof(struct cache_entry) + CACHE_ENTRY_PATH_LENGTH);
1968}
1969
1970static size_t estimate_cache_size(size_t ondisk_size, unsigned int entries)
1971{
1972 long per_entry = sizeof(struct cache_entry) - sizeof(struct ondisk_cache_entry);
1973
1974 /*
1975 * Account for potential alignment differences.
1976 */
Johannes Schindelinc097b952019-10-04 08:09:26 -07001977 per_entry += align_padding_size(per_entry, 0);
Jameson Miller8e72d672018-07-02 19:49:37 +00001978 return ondisk_size + entries * per_entry;
1979}
1980
Ben Peart32550892018-10-10 11:59:37 -04001981struct index_entry_offset
1982{
1983 /* starting byte offset into index file, count of index entries in this block */
1984 int offset, nr;
1985};
1986
1987struct index_entry_offset_table
1988{
1989 int nr;
1990 struct index_entry_offset entries[FLEX_ARRAY];
1991};
1992
Ben Peart32550892018-10-10 11:59:37 -04001993static struct index_entry_offset_table *read_ieot_extension(const char *mmap, size_t mmap_size, size_t offset);
1994static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot);
Ben Peart32550892018-10-10 11:59:37 -04001995
Ben Peart3b1d9e02018-10-10 11:59:34 -04001996static size_t read_eoie_extension(const char *mmap, size_t mmap_size);
1997static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset);
1998
Ben Peartabb4bb82018-10-10 11:59:36 -04001999struct load_index_extensions
2000{
Ben Peartabb4bb82018-10-10 11:59:36 -04002001 pthread_t pthread;
Ben Peartabb4bb82018-10-10 11:59:36 -04002002 struct index_state *istate;
2003 const char *mmap;
2004 size_t mmap_size;
2005 unsigned long src_offset;
2006};
2007
2008static void *load_index_extensions(void *_data)
2009{
2010 struct load_index_extensions *p = _data;
2011 unsigned long src_offset = p->src_offset;
2012
2013 while (src_offset <= p->mmap_size - the_hash_algo->rawsz - 8) {
2014 /* After an array of active_nr index entries,
2015 * there can be arbitrary number of extended
2016 * sections, each of which is prefixed with
2017 * extension name (4-byte) and section length
2018 * in 4-byte network byte order.
2019 */
2020 uint32_t extsize = get_be32(p->mmap + src_offset + 4);
2021 if (read_index_extension(p->istate,
2022 p->mmap + src_offset,
2023 p->mmap + src_offset + 8,
2024 extsize) < 0) {
2025 munmap((void *)p->mmap, p->mmap_size);
2026 die(_("index file corrupt"));
2027 }
2028 src_offset += 8;
2029 src_offset += extsize;
2030 }
2031
2032 return NULL;
2033}
2034
Ben Peart32550892018-10-10 11:59:37 -04002035/*
Ben Peart77ff1122018-10-10 11:59:38 -04002036 * A helper function that will load the specified range of cache entries
2037 * from the memory mapped file and add them to the given index.
2038 */
2039static unsigned long load_cache_entry_block(struct index_state *istate,
2040 struct mem_pool *ce_mem_pool, int offset, int nr, const char *mmap,
2041 unsigned long start_offset, const struct cache_entry *previous_ce)
2042{
2043 int i;
2044 unsigned long src_offset = start_offset;
2045
2046 for (i = offset; i < offset + nr; i++) {
Ben Peart77ff1122018-10-10 11:59:38 -04002047 struct cache_entry *ce;
2048 unsigned long consumed;
2049
Victoria Dye4a6ed302022-09-28 17:19:00 +00002050 ce = create_from_disk(ce_mem_pool, istate->version,
2051 mmap + src_offset,
2052 &consumed, previous_ce);
Ben Peart77ff1122018-10-10 11:59:38 -04002053 set_index_entry(istate, i, ce);
2054
2055 src_offset += consumed;
2056 previous_ce = ce;
2057 }
2058 return src_offset - start_offset;
2059}
2060
2061static unsigned long load_all_cache_entries(struct index_state *istate,
2062 const char *mmap, size_t mmap_size, unsigned long src_offset)
2063{
2064 unsigned long consumed;
2065
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002066 istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
Ben Peart77ff1122018-10-10 11:59:38 -04002067 if (istate->version == 4) {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002068 mem_pool_init(istate->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002069 estimate_cache_size_from_compressed(istate->cache_nr));
2070 } else {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002071 mem_pool_init(istate->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002072 estimate_cache_size(mmap_size, istate->cache_nr));
2073 }
2074
2075 consumed = load_cache_entry_block(istate, istate->ce_mem_pool,
2076 0, istate->cache_nr, mmap, src_offset, NULL);
2077 return consumed;
2078}
2079
Ben Peart77ff1122018-10-10 11:59:38 -04002080/*
Ben Peart32550892018-10-10 11:59:37 -04002081 * Mostly randomly chosen maximum thread counts: we
2082 * cap the parallelism to online_cpus() threads, and we want
2083 * to have at least 10000 cache entries per thread for it to
2084 * be worth starting a thread.
2085 */
2086
2087#define THREAD_COST (10000)
2088
Ben Peart77ff1122018-10-10 11:59:38 -04002089struct load_cache_entries_thread_data
2090{
2091 pthread_t pthread;
2092 struct index_state *istate;
2093 struct mem_pool *ce_mem_pool;
2094 int offset;
2095 const char *mmap;
2096 struct index_entry_offset_table *ieot;
2097 int ieot_start; /* starting index into the ieot array */
2098 int ieot_blocks; /* count of ieot entries to process */
2099 unsigned long consumed; /* return # of bytes in index file processed */
2100};
2101
2102/*
2103 * A thread proc to run the load_cache_entries() computation
2104 * across multiple background threads.
2105 */
2106static void *load_cache_entries_thread(void *_data)
2107{
2108 struct load_cache_entries_thread_data *p = _data;
2109 int i;
2110
2111 /* iterate across all ieot blocks assigned to this thread */
2112 for (i = p->ieot_start; i < p->ieot_start + p->ieot_blocks; i++) {
2113 p->consumed += load_cache_entry_block(p->istate, p->ce_mem_pool,
2114 p->offset, p->ieot->entries[i].nr, p->mmap, p->ieot->entries[i].offset, NULL);
2115 p->offset += p->ieot->entries[i].nr;
2116 }
2117 return NULL;
2118}
2119
2120static unsigned long load_cache_entries_threaded(struct index_state *istate, const char *mmap, size_t mmap_size,
Jeff King7bd96312019-05-09 17:29:44 -04002121 int nr_threads, struct index_entry_offset_table *ieot)
Ben Peart77ff1122018-10-10 11:59:38 -04002122{
2123 int i, offset, ieot_blocks, ieot_start, err;
2124 struct load_cache_entries_thread_data *data;
2125 unsigned long consumed = 0;
2126
2127 /* a little sanity checking */
2128 if (istate->name_hash_initialized)
2129 BUG("the name hash isn't thread safe");
2130
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002131 istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
2132 mem_pool_init(istate->ce_mem_pool, 0);
Ben Peart77ff1122018-10-10 11:59:38 -04002133
2134 /* ensure we have no more threads than we have blocks to process */
2135 if (nr_threads > ieot->nr)
2136 nr_threads = ieot->nr;
René Scharfeca56dad2021-03-13 17:17:22 +01002137 CALLOC_ARRAY(data, nr_threads);
Ben Peart77ff1122018-10-10 11:59:38 -04002138
2139 offset = ieot_start = 0;
2140 ieot_blocks = DIV_ROUND_UP(ieot->nr, nr_threads);
2141 for (i = 0; i < nr_threads; i++) {
2142 struct load_cache_entries_thread_data *p = &data[i];
2143 int nr, j;
2144
2145 if (ieot_start + ieot_blocks > ieot->nr)
2146 ieot_blocks = ieot->nr - ieot_start;
2147
2148 p->istate = istate;
2149 p->offset = offset;
2150 p->mmap = mmap;
2151 p->ieot = ieot;
2152 p->ieot_start = ieot_start;
2153 p->ieot_blocks = ieot_blocks;
2154
2155 /* create a mem_pool for each thread */
2156 nr = 0;
2157 for (j = p->ieot_start; j < p->ieot_start + p->ieot_blocks; j++)
2158 nr += p->ieot->entries[j].nr;
René Scharfebcd2c5e2020-09-04 19:33:55 +02002159 p->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
Ben Peart77ff1122018-10-10 11:59:38 -04002160 if (istate->version == 4) {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002161 mem_pool_init(p->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002162 estimate_cache_size_from_compressed(nr));
2163 } else {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002164 mem_pool_init(p->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002165 estimate_cache_size(mmap_size, nr));
2166 }
2167
2168 err = pthread_create(&p->pthread, NULL, load_cache_entries_thread, p);
2169 if (err)
2170 die(_("unable to create load_cache_entries thread: %s"), strerror(err));
2171
2172 /* increment by the number of cache entries in the ieot block being processed */
2173 for (j = 0; j < ieot_blocks; j++)
2174 offset += ieot->entries[ieot_start + j].nr;
2175 ieot_start += ieot_blocks;
2176 }
2177
2178 for (i = 0; i < nr_threads; i++) {
2179 struct load_cache_entries_thread_data *p = &data[i];
2180
2181 err = pthread_join(p->pthread, NULL);
2182 if (err)
2183 die(_("unable to join load_cache_entries thread: %s"), strerror(err));
2184 mem_pool_combine(istate->ce_mem_pool, p->ce_mem_pool);
2185 consumed += p->consumed;
2186 }
2187
2188 free(data);
2189
2190 return consumed;
2191}
Ben Peart77ff1122018-10-10 11:59:38 -04002192
Victoria Dye491df5f2022-05-10 23:32:30 +00002193static void set_new_index_sparsity(struct index_state *istate)
2194{
2195 /*
2196 * If the index's repo exists, mark it sparse according to
2197 * repo settings.
2198 */
Ævar Arnfjörð Bjarmason5bdf6d42023-01-12 13:55:26 +01002199 prepare_repo_settings(istate->repo);
2200 if (!istate->repo->settings.command_requires_full_index &&
2201 is_sparse_index_allowed(istate, 0))
2202 istate->sparse_index = 1;
Victoria Dye491df5f2022-05-10 23:32:30 +00002203}
2204
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07002205/* remember to discard_cache() before reading a different cache! */
Nguyễn Thái Ngọc Duy3e52f702014-06-13 19:19:51 +07002206int do_read_index(struct index_state *istate, const char *path, int must_exist)
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07002207{
Ben Peart77ff1122018-10-10 11:59:38 -04002208 int fd;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002209 struct stat st;
René Scharfedebed2a2011-10-24 23:59:14 +02002210 unsigned long src_offset;
Ben Peart371ed0d2018-10-10 11:59:33 -04002211 const struct cache_header *hdr;
2212 const char *mmap;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002213 size_t mmap_size;
Ben Peartabb4bb82018-10-10 11:59:36 -04002214 struct load_index_extensions p;
2215 size_t extension_offset = 0;
Ben Peart77ff1122018-10-10 11:59:38 -04002216 int nr_threads, cpus;
2217 struct index_entry_offset_table *ieot = NULL;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002218
Junio C Hamano913e0e92008-08-23 12:57:30 -07002219 if (istate->initialized)
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002220 return istate->cache_nr;
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002221
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002222 istate->timestamp.sec = 0;
2223 istate->timestamp.nsec = 0;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07002224 fd = open(path, O_RDONLY);
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002225 if (fd < 0) {
Victoria Dye491df5f2022-05-10 23:32:30 +00002226 if (!must_exist && errno == ENOENT) {
2227 set_new_index_sparsity(istate);
Johannes Schindelin866b43e2023-06-29 13:23:08 +00002228 istate->initialized = 1;
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002229 return 0;
Victoria Dye491df5f2022-05-10 23:32:30 +00002230 }
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002231 die_errno(_("%s: index file open failed"), path);
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002232 }
Linus Torvaldse83c5162005-04-07 15:13:13 -07002233
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03002234 if (fstat(fd, &st))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002235 die_errno(_("%s: cannot stat the open index"), path);
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03002236
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002237 mmap_size = xsize_t(st.st_size);
brian m. carlsonaab61352018-02-01 02:18:45 +00002238 if (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002239 die(_("%s: index file smaller than expected"), path);
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03002240
Varun Naik02638d12019-07-13 20:01:53 -07002241 mmap = xmmap_gently(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002242 if (mmap == MAP_FAILED)
Eric Wongdc059292021-06-30 00:01:32 +00002243 die_errno(_("%s: unable to map index file%s"), path,
2244 mmap_os_err());
Nguyễn Thái Ngọc Duy57d84f82012-08-06 18:27:09 +07002245 close(fd);
Linus Torvaldse83c5162005-04-07 15:13:13 -07002246
Ben Peart371ed0d2018-10-10 11:59:33 -04002247 hdr = (const struct cache_header *)mmap;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002248 if (verify_hdr(hdr, mmap_size) < 0)
Linus Torvaldse83c5162005-04-07 15:13:13 -07002249 goto unmap;
2250
brian m. carlson92e2cab2021-04-26 01:02:50 +00002251 oidread(&istate->oid, (const unsigned char *)hdr + mmap_size - the_hash_algo->rawsz);
Junio C Hamano9d227782012-04-04 09:12:43 -07002252 istate->version = ntohl(hdr->hdr_version);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002253 istate->cache_nr = ntohl(hdr->hdr_entries);
2254 istate->cache_alloc = alloc_nr(istate->cache_nr);
René Scharfeca56dad2021-03-13 17:17:22 +01002255 CALLOC_ARRAY(istate->cache, istate->cache_alloc);
Junio C Hamano913e0e92008-08-23 12:57:30 -07002256 istate->initialized = 1;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002257
Ben Peartabb4bb82018-10-10 11:59:36 -04002258 p.istate = istate;
2259 p.mmap = mmap;
2260 p.mmap_size = mmap_size;
Junio C Hamano6c9cd162012-04-03 15:53:15 -07002261
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002262 src_offset = sizeof(*hdr);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002263
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002264 if (git_config_get_index_threads(&nr_threads))
2265 nr_threads = 1;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002266
Ben Peart77ff1122018-10-10 11:59:38 -04002267 /* TODO: does creating more threads than cores help? */
2268 if (!nr_threads) {
2269 nr_threads = istate->cache_nr / THREAD_COST;
2270 cpus = online_cpus();
2271 if (nr_threads > cpus)
2272 nr_threads = cpus;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002273 }
Ben Peartabb4bb82018-10-10 11:59:36 -04002274
Nguyễn Thái Ngọc Duy88168b92018-11-03 09:48:48 +01002275 if (!HAVE_THREADS)
2276 nr_threads = 1;
2277
Ben Peartabb4bb82018-10-10 11:59:36 -04002278 if (nr_threads > 1) {
2279 extension_offset = read_eoie_extension(mmap, mmap_size);
2280 if (extension_offset) {
2281 int err;
2282
2283 p.src_offset = extension_offset;
2284 err = pthread_create(&p.pthread, NULL, load_index_extensions, &p);
2285 if (err)
2286 die(_("unable to create load_index_extensions thread: %s"), strerror(err));
2287
2288 nr_threads--;
2289 }
2290 }
Ben Peart77ff1122018-10-10 11:59:38 -04002291
2292 /*
2293 * Locate and read the index entry offset table so that we can use it
2294 * to multi-thread the reading of the cache entries.
2295 */
2296 if (extension_offset && nr_threads > 1)
2297 ieot = read_ieot_extension(mmap, mmap_size, extension_offset);
2298
2299 if (ieot) {
Jeff King7bd96312019-05-09 17:29:44 -04002300 src_offset += load_cache_entries_threaded(istate, mmap, mmap_size, nr_threads, ieot);
Ben Peart77ff1122018-10-10 11:59:38 -04002301 free(ieot);
2302 } else {
2303 src_offset += load_all_cache_entries(istate, mmap, mmap_size, src_offset);
2304 }
Ben Peartabb4bb82018-10-10 11:59:36 -04002305
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002306 istate->timestamp.sec = st.st_mtime;
Kjetil Barvikc06ff492009-03-04 18:47:40 +01002307 istate->timestamp.nsec = ST_MTIME_NSEC(st);
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002308
Ben Peartabb4bb82018-10-10 11:59:36 -04002309 /* if we created a thread, join it otherwise load the extensions on the primary thread */
Ben Peartabb4bb82018-10-10 11:59:36 -04002310 if (extension_offset) {
2311 int ret = pthread_join(p.pthread, NULL);
2312 if (ret)
2313 die(_("unable to join load_index_extensions thread: %s"), strerror(ret));
Nguyễn Thái Ngọc Duy88168b92018-11-03 09:48:48 +01002314 } else {
Ben Peartabb4bb82018-10-10 11:59:36 -04002315 p.src_offset = src_offset;
2316 load_index_extensions(&p);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002317 }
Ben Peart371ed0d2018-10-10 11:59:33 -04002318 munmap((void *)mmap, mmap_size);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002319
2320 /*
2321 * TODO trace2: replace "the_repository" with the actual repo instance
2322 * that is associated with the given "istate".
2323 */
2324 trace2_data_intmax("index", the_repository, "read/version",
2325 istate->version);
2326 trace2_data_intmax("index", the_repository, "read/cache_nr",
2327 istate->cache_nr);
2328
Victoria Dye7ca4fc82021-11-23 00:20:33 +00002329 /*
2330 * If the command explicitly requires a full index, force it
2331 * to be full. Otherwise, correct the sparsity based on repository
2332 * settings and other properties of the index (if necessary).
2333 */
Derrick Stolee4300f842021-03-30 13:10:48 +00002334 prepare_repo_settings(istate->repo);
2335 if (istate->repo->settings.command_requires_full_index)
2336 ensure_full_index(istate);
Victoria Dye7ca4fc82021-11-23 00:20:33 +00002337 else
2338 ensure_correct_sparsity(istate);
Derrick Stolee4300f842021-03-30 13:10:48 +00002339
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002340 return istate->cache_nr;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002341
2342unmap:
Ben Peart371ed0d2018-10-10 11:59:33 -04002343 munmap((void *)mmap, mmap_size);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002344 die(_("index file corrupt"));
Linus Torvaldse83c5162005-04-07 15:13:13 -07002345}
2346
Christian Couder0d59ffb2017-02-27 19:00:12 +01002347/*
2348 * Signal that the shared index is used by updating its mtime.
2349 *
2350 * This way, shared index can be removed if they have not been used
2351 * for some time.
2352 */
Thomas Gummerera125a222018-01-07 22:30:13 +00002353static void freshen_shared_index(const char *shared_index, int warn)
Christian Couder0d59ffb2017-02-27 19:00:12 +01002354{
Christian Couder0d59ffb2017-02-27 19:00:12 +01002355 if (!check_and_freshen_file(shared_index, 1) && warn)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002356 warning(_("could not freshen shared index '%s'"), shared_index);
Christian Couder0d59ffb2017-02-27 19:00:12 +01002357}
2358
Thomas Gummerera125a222018-01-07 22:30:13 +00002359int read_index_from(struct index_state *istate, const char *path,
2360 const char *gitdir)
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002361{
2362 struct split_index *split_index;
2363 int ret;
brian m. carlson2182abd2018-05-02 00:25:43 +00002364 char *base_oid_hex;
Thomas Gummerera125a222018-01-07 22:30:13 +00002365 char *base_path;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002366
2367 /* istate->initialized covers both .git/index and .git/sharedindex.xxx */
2368 if (istate->initialized)
2369 return istate->cache_nr;
2370
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002371 /*
2372 * TODO trace2: replace "the_repository" with the actual repo instance
2373 * that is associated with the given "istate".
2374 */
2375 trace2_region_enter_printf("index", "do_read_index", the_repository,
2376 "%s", path);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002377 trace_performance_enter();
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002378 ret = do_read_index(istate, path, 0);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002379 trace_performance_leave("read cache %s", path);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002380 trace2_region_leave_printf("index", "do_read_index", the_repository,
2381 "%s", path);
Christian Couder435ec092016-01-27 07:58:05 +01002382
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002383 split_index = istate->split_index;
brian m. carlson2182abd2018-05-02 00:25:43 +00002384 if (!split_index || is_null_oid(&split_index->base_oid)) {
Christian Couder435ec092016-01-27 07:58:05 +01002385 post_read_index_from(istate);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002386 return ret;
Thomas Gummerer03f15a72015-03-20 22:43:14 +01002387 }
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002388
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002389 trace_performance_enter();
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002390 if (split_index->base)
Ævar Arnfjörð Bjarmason2f6b1eb2023-01-12 13:55:27 +01002391 release_index(split_index->base);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002392 else
Ævar Arnfjörð Bjarmason2f6b1eb2023-01-12 13:55:27 +01002393 ALLOC_ARRAY(split_index->base, 1);
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +01002394 index_state_init(split_index->base, istate->repo);
Christian Couderde6ae5f2017-03-06 10:42:00 +01002395
brian m. carlson2182abd2018-05-02 00:25:43 +00002396 base_oid_hex = oid_to_hex(&split_index->base_oid);
2397 base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
Johannes Schindelin7667f4f2023-06-29 13:23:09 +00002398 if (file_exists(base_path)) {
2399 trace2_region_enter_printf("index", "shared/do_read_index",
2400 the_repository, "%s", base_path);
2401
2402 ret = do_read_index(split_index->base, base_path, 0);
2403 trace2_region_leave_printf("index", "shared/do_read_index",
2404 the_repository, "%s", base_path);
2405 } else {
SZEDER Gábor998330a2021-08-26 23:00:02 +02002406 char *path_copy = xstrdup(path);
Johannes Schindelin652891d2022-06-15 23:35:42 +00002407 char *base_path2 = xstrfmt("%s/sharedindex.%s",
2408 dirname(path_copy), base_oid_hex);
SZEDER Gábor998330a2021-08-26 23:00:02 +02002409 free(path_copy);
2410 trace2_region_enter_printf("index", "shared/do_read_index",
2411 the_repository, "%s", base_path2);
2412 ret = do_read_index(split_index->base, base_path2, 1);
2413 trace2_region_leave_printf("index", "shared/do_read_index",
2414 the_repository, "%s", base_path2);
Johannes Schindelin652891d2022-06-15 23:35:42 +00002415 free(base_path2);
SZEDER Gábor998330a2021-08-26 23:00:02 +02002416 }
Jeff King9001dc22018-08-28 17:22:48 -04002417 if (!oideq(&split_index->base_oid, &split_index->base->oid))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002418 die(_("broken index, expect %s in %s, got %s"),
brian m. carlson2182abd2018-05-02 00:25:43 +00002419 base_oid_hex, base_path,
brian m. carlson75691ea2018-05-02 00:25:44 +00002420 oid_to_hex(&split_index->base->oid));
Christian Couderde6ae5f2017-03-06 10:42:00 +01002421
Thomas Gummerera125a222018-01-07 22:30:13 +00002422 freshen_shared_index(base_path, 0);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002423 merge_base_index(istate);
Christian Couder435ec092016-01-27 07:58:05 +01002424 post_read_index_from(istate);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002425 trace_performance_leave("read cache %s", base_path);
Carlo Marcelo Arenas Belónb42ad7d2018-10-20 00:33:34 -07002426 free(base_path);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002427 return ret;
2428}
2429
Junio C Hamanofa7b3c22008-11-12 11:52:35 -08002430int is_index_unborn(struct index_state *istate)
2431{
René Scharfedebed2a2011-10-24 23:59:14 +02002432 return (!istate->cache_nr && !istate->timestamp.sec);
Junio C Hamanofa7b3c22008-11-12 11:52:35 -08002433}
2434
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +01002435void index_state_init(struct index_state *istate, struct repository *r)
Ævar Arnfjörð Bjarmason2f6b1eb2023-01-12 13:55:27 +01002436{
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +01002437 struct index_state blank = INDEX_STATE_INIT(r);
Ævar Arnfjörð Bjarmason2f6b1eb2023-01-12 13:55:27 +01002438 memcpy(istate, &blank, sizeof(*istate));
2439}
2440
2441void release_index(struct index_state *istate)
Johannes Schindelin6d297f82006-07-08 18:42:41 +02002442{
Jameson Miller8e72d672018-07-02 19:49:37 +00002443 /*
2444 * Cache entries in istate->cache[] should have been allocated
2445 * from the memory pool associated with this index, or from an
2446 * associated split_index. There is no need to free individual
Jameson Miller8616a2d2018-07-02 19:49:39 +00002447 * cache entries. validate_cache_entries can detect when this
2448 * assertion does not hold.
Jameson Miller8e72d672018-07-02 19:49:37 +00002449 */
Jameson Miller8616a2d2018-07-02 19:49:39 +00002450 validate_cache_entries(istate);
René Scharfedebed2a2011-10-24 23:59:14 +02002451
Junio C Hamanocfc57892009-12-25 00:30:51 -08002452 resolve_undo_clear_index(istate);
Karsten Blees20926782013-02-28 00:57:48 +01002453 free_name_hash(istate);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002454 cache_tree_free(&(istate->cache_tree));
Ævar Arnfjörð Bjarmason2f6b1eb2023-01-12 13:55:27 +01002455 free(istate->fsmonitor_last_update);
2456 free(istate->cache);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002457 discard_split_index(istate);
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002458 free_untracked_cache(istate->untracked);
Jameson Miller8e72d672018-07-02 19:49:37 +00002459
Ævar Arnfjörð Bjarmasonb5fcb1c2022-11-08 19:17:40 +01002460 if (istate->sparse_checkout_patterns) {
2461 clear_pattern_list(istate->sparse_checkout_patterns);
2462 FREE_AND_NULL(istate->sparse_checkout_patterns);
2463 }
2464
Jameson Miller8e72d672018-07-02 19:49:37 +00002465 if (istate->ce_mem_pool) {
Jameson Miller8616a2d2018-07-02 19:49:39 +00002466 mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries());
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002467 FREE_AND_NULL(istate->ce_mem_pool);
Jameson Miller8e72d672018-07-02 19:49:37 +00002468 }
Johannes Schindelin6d297f82006-07-08 18:42:41 +02002469}
2470
Ævar Arnfjörð Bjarmason2f6b1eb2023-01-12 13:55:27 +01002471void discard_index(struct index_state *istate)
2472{
2473 release_index(istate);
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +01002474 index_state_init(istate, istate->repo);
Ævar Arnfjörð Bjarmason2f6b1eb2023-01-12 13:55:27 +01002475}
2476
Jameson Miller8616a2d2018-07-02 19:49:39 +00002477/*
2478 * Validate the cache entries of this index.
2479 * All cache entries associated with this index
2480 * should have been allocated by the memory pool
2481 * associated with this index, or by a referenced
2482 * split index.
2483 */
2484void validate_cache_entries(const struct index_state *istate)
2485{
2486 int i;
2487
2488 if (!should_validate_cache_entries() ||!istate || !istate->initialized)
2489 return;
2490
2491 for (i = 0; i < istate->cache_nr; i++) {
2492 if (!istate) {
Nguyễn Thái Ngọc Duy391408e2018-11-10 06:16:04 +01002493 BUG("cache entry is not allocated from expected memory pool");
Jameson Miller8616a2d2018-07-02 19:49:39 +00002494 } else if (!istate->ce_mem_pool ||
2495 !mem_pool_contains(istate->ce_mem_pool, istate->cache[i])) {
2496 if (!istate->split_index ||
2497 !istate->split_index->base ||
2498 !istate->split_index->base->ce_mem_pool ||
2499 !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 +01002500 BUG("cache entry is not allocated from expected memory pool");
Jameson Miller8616a2d2018-07-02 19:49:39 +00002501 }
2502 }
2503 }
2504
2505 if (istate->split_index)
2506 validate_cache_entries(istate->split_index->base);
2507}
2508
Linus Torvaldsd1f128b2008-03-06 12:46:09 -08002509int unmerged_index(const struct index_state *istate)
Daniel Barkalow94a57282008-02-07 11:40:13 -05002510{
2511 int i;
2512 for (i = 0; i < istate->cache_nr; i++) {
2513 if (ce_stage(istate->cache[i]))
2514 return 1;
2515 }
2516 return 0;
2517}
2518
Nguyễn Thái Ngọc Duy150fe062019-01-12 09:13:31 +07002519int repo_index_has_changes(struct repository *repo,
2520 struct tree *tree,
2521 struct strbuf *sb)
Elijah Newrencffbfad2018-06-30 18:24:55 -07002522{
Nguyễn Thái Ngọc Duy150fe062019-01-12 09:13:31 +07002523 struct index_state *istate = repo->index;
Elijah Newrene1f86942018-06-30 18:25:00 -07002524 struct object_id cmp;
Elijah Newrencffbfad2018-06-30 18:24:55 -07002525 int i;
2526
Elijah Newrene1f86942018-06-30 18:25:00 -07002527 if (tree)
2528 cmp = tree->object.oid;
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02002529 if (tree || !repo_get_oid_tree(repo, "HEAD", &cmp)) {
Elijah Newrencffbfad2018-06-30 18:24:55 -07002530 struct diff_options opt;
2531
Nguyễn Thái Ngọc Duy150fe062019-01-12 09:13:31 +07002532 repo_diff_setup(repo, &opt);
Elijah Newrencffbfad2018-06-30 18:24:55 -07002533 opt.flags.exit_with_status = 1;
2534 if (!sb)
2535 opt.flags.quick = 1;
Andrzej Hunt8d833e92021-07-25 15:08:25 +02002536 diff_setup_done(&opt);
Elijah Newrene1f86942018-06-30 18:25:00 -07002537 do_diff_cache(&cmp, &opt);
Elijah Newrencffbfad2018-06-30 18:24:55 -07002538 diffcore_std(&opt);
2539 for (i = 0; sb && i < diff_queued_diff.nr; i++) {
2540 if (i)
2541 strbuf_addch(sb, ' ');
2542 strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
2543 }
2544 diff_flush(&opt);
2545 return opt.flags.has_changes != 0;
2546 } else {
Derrick Stolee0c18c052021-04-01 01:49:58 +00002547 /* TODO: audit for interaction with sparse-index. */
2548 ensure_full_index(istate);
Elijah Newren1b9fbef2018-06-30 18:24:56 -07002549 for (i = 0; sb && i < istate->cache_nr; i++) {
Elijah Newrencffbfad2018-06-30 18:24:55 -07002550 if (i)
2551 strbuf_addch(sb, ' ');
Elijah Newren1b9fbef2018-06-30 18:24:56 -07002552 strbuf_addstr(sb, istate->cache[i]->name);
Elijah Newrencffbfad2018-06-30 18:24:55 -07002553 }
Elijah Newren1b9fbef2018-06-30 18:24:56 -07002554 return !!istate->cache_nr;
Elijah Newrencffbfad2018-06-30 18:24:55 -07002555 }
2556}
2557
Derrick Stolee410334e2021-05-18 18:32:47 +00002558static int write_index_ext_header(struct hashfile *f,
2559 git_hash_ctx *eoie_f,
2560 unsigned int ext,
2561 unsigned int sz)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002562{
Derrick Stolee410334e2021-05-18 18:32:47 +00002563 hashwrite_be32(f, ext);
2564 hashwrite_be32(f, sz);
2565
2566 if (eoie_f) {
2567 ext = htonl(ext);
2568 sz = htonl(sz);
2569 the_hash_algo->update_fn(eoie_f, &ext, sizeof(ext));
2570 the_hash_algo->update_fn(eoie_f, &sz, sizeof(sz));
Ben Peart3b1d9e02018-10-10 11:59:34 -04002571 }
Derrick Stolee410334e2021-05-18 18:32:47 +00002572 return 0;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002573}
2574
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002575static void ce_smudge_racily_clean_entry(struct index_state *istate,
2576 struct cache_entry *ce)
Junio C Hamano407c8eb2005-12-20 12:12:18 -08002577{
2578 /*
2579 * The only thing we care about in this function is to smudge the
2580 * falsely clean entry due to touch-update-touch race, so we leave
2581 * everything else as they are. We are called for entries whose
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002582 * ce_stat_data.sd_mtime match the index file mtime.
Junio C Hamanoc70115b2008-07-29 01:13:44 -07002583 *
2584 * Note that this actually does not do much for gitlinks, for
2585 * which ce_match_stat_basic() always goes to the actual
2586 * contents. The caller checks with is_racy_timestamp() which
2587 * always says "no" for gitlinks, so we are not called for them ;-)
Junio C Hamano407c8eb2005-12-20 12:12:18 -08002588 */
2589 struct stat st;
2590
2591 if (lstat(ce->name, &st) < 0)
2592 return;
2593 if (ce_match_stat_basic(ce, &st))
2594 return;
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002595 if (ce_modified_check_fs(istate, ce, &st)) {
Junio C Hamano4b3511b2005-12-20 14:18:47 -08002596 /* This is "racily clean"; smudge it. Note that this
2597 * is a tricky code. At first glance, it may appear
2598 * that it can break with this sequence:
2599 *
2600 * $ echo xyzzy >frotz
2601 * $ git-update-index --add frotz
2602 * $ : >frotz
2603 * $ sleep 3
2604 * $ echo filfre >nitfol
2605 * $ git-update-index --add nitfol
2606 *
Junio C Hamanob7e58b12006-08-05 04:16:02 -07002607 * but it does not. When the second update-index runs,
Junio C Hamano4b3511b2005-12-20 14:18:47 -08002608 * it notices that the entry "frotz" has the same timestamp
2609 * as index, and if we were to smudge it by resetting its
2610 * size to zero here, then the object name recorded
2611 * in index is the 6-byte file but the cached stat information
2612 * becomes zero --- which would then match what we would
Junio C Hamanoa6080a02007-06-07 00:04:01 -07002613 * obtain from the filesystem next time we stat("frotz").
Junio C Hamano4b3511b2005-12-20 14:18:47 -08002614 *
2615 * However, the second update-index, before calling
2616 * this function, notices that the cached size is 6
2617 * bytes and what is on the filesystem is an empty
2618 * file, and never calls us, so the cached size information
2619 * for "frotz" stays 6 which does not match the filesystem.
2620 */
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002621 ce->ce_stat_data.sd_size = 0;
Junio C Hamano407c8eb2005-12-20 12:12:18 -08002622 }
2623}
2624
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002625/* Copy miscellaneous fields but not the name */
Kevin Willfordce012de2017-08-21 15:24:32 -06002626static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002627 struct cache_entry *ce)
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002628{
Thomas Gummererb60e1882012-07-11 11:22:37 +02002629 short flags;
brian m. carlson575fa8a2019-02-19 00:05:24 +00002630 const unsigned hashsz = the_hash_algo->rawsz;
2631 uint16_t *flagsp = (uint16_t *)(ondisk->data + hashsz);
Thomas Gummererb60e1882012-07-11 11:22:37 +02002632
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002633 ondisk->ctime.sec = htonl(ce->ce_stat_data.sd_ctime.sec);
2634 ondisk->mtime.sec = htonl(ce->ce_stat_data.sd_mtime.sec);
2635 ondisk->ctime.nsec = htonl(ce->ce_stat_data.sd_ctime.nsec);
2636 ondisk->mtime.nsec = htonl(ce->ce_stat_data.sd_mtime.nsec);
2637 ondisk->dev = htonl(ce->ce_stat_data.sd_dev);
2638 ondisk->ino = htonl(ce->ce_stat_data.sd_ino);
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002639 ondisk->mode = htonl(ce->ce_mode);
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002640 ondisk->uid = htonl(ce->ce_stat_data.sd_uid);
2641 ondisk->gid = htonl(ce->ce_stat_data.sd_gid);
2642 ondisk->size = htonl(ce->ce_stat_data.sd_size);
brian m. carlson575fa8a2019-02-19 00:05:24 +00002643 hashcpy(ondisk->data, ce->oid.hash);
Thomas Gummererb60e1882012-07-11 11:22:37 +02002644
Nguyễn Thái Ngọc Duyce51bf02014-06-13 19:19:25 +07002645 flags = ce->ce_flags & ~CE_NAMEMASK;
Thomas Gummererb60e1882012-07-11 11:22:37 +02002646 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce));
brian m. carlson575fa8a2019-02-19 00:05:24 +00002647 flagsp[0] = htons(flags);
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002648 if (ce->ce_flags & CE_EXTENDED) {
brian m. carlson575fa8a2019-02-19 00:05:24 +00002649 flagsp[1] = htons((ce->ce_flags & CE_EXTENDED_FLAGS) >> 16);
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002650 }
2651}
2652
Derrick Stolee410334e2021-05-18 18:32:47 +00002653static int ce_write_entry(struct hashfile *f, struct cache_entry *ce,
Kevin Willfordce012de2017-08-21 15:24:32 -06002654 struct strbuf *previous_name, struct ondisk_cache_entry *ondisk)
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002655{
Junio C Hamano9d227782012-04-04 09:12:43 -07002656 int size;
Ramsay Jones00a4b032018-03-19 17:56:11 +00002657 unsigned int saved_namelen;
2658 int stripped_name = 0;
Kevin Willfordce012de2017-08-21 15:24:32 -06002659 static unsigned char padding[8] = { 0x00 };
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002660
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07002661 if (ce->ce_flags & CE_STRIP_NAME) {
2662 saved_namelen = ce_namelen(ce);
2663 ce->ce_namelen = 0;
Ramsay Jones00a4b032018-03-19 17:56:11 +00002664 stripped_name = 1;
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07002665 }
2666
brian m. carlson575fa8a2019-02-19 00:05:24 +00002667 size = offsetof(struct ondisk_cache_entry,data) + ondisk_data_size(ce->ce_flags, 0);
Kevin Willfordce012de2017-08-21 15:24:32 -06002668
Junio C Hamano9d227782012-04-04 09:12:43 -07002669 if (!previous_name) {
Kevin Willfordce012de2017-08-21 15:24:32 -06002670 int len = ce_namelen(ce);
2671 copy_cache_entry_to_ondisk(ondisk, ce);
Derrick Stolee410334e2021-05-18 18:32:47 +00002672 hashwrite(f, ondisk, size);
2673 hashwrite(f, ce->name, len);
2674 hashwrite(f, padding, align_padding_size(size, len));
Junio C Hamano9d227782012-04-04 09:12:43 -07002675 } else {
2676 int common, to_remove, prefix_size;
2677 unsigned char to_remove_vi[16];
2678 for (common = 0;
2679 (ce->name[common] &&
2680 common < previous_name->len &&
2681 ce->name[common] == previous_name->buf[common]);
2682 common++)
2683 ; /* still matching */
2684 to_remove = previous_name->len - common;
2685 prefix_size = encode_varint(to_remove, to_remove_vi);
2686
Kevin Willfordce012de2017-08-21 15:24:32 -06002687 copy_cache_entry_to_ondisk(ondisk, ce);
Derrick Stolee410334e2021-05-18 18:32:47 +00002688 hashwrite(f, ondisk, size);
2689 hashwrite(f, to_remove_vi, prefix_size);
2690 hashwrite(f, ce->name + common, ce_namelen(ce) - common);
2691 hashwrite(f, padding, 1);
Junio C Hamano9d227782012-04-04 09:12:43 -07002692
2693 strbuf_splice(previous_name, common, to_remove,
2694 ce->name + common, ce_namelen(ce) - common);
2695 }
Ramsay Jones00a4b032018-03-19 17:56:11 +00002696 if (stripped_name) {
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07002697 ce->ce_namelen = saved_namelen;
2698 ce->ce_flags &= ~CE_STRIP_NAME;
2699 }
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002700
Derrick Stolee410334e2021-05-18 18:32:47 +00002701 return 0;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002702}
2703
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002704/*
2705 * This function verifies if index_state has the correct sha1 of the
2706 * index file. Don't die if we have any other failure, just return 0.
2707 */
2708static int verify_index_from(const struct index_state *istate, const char *path)
2709{
2710 int fd;
2711 ssize_t n;
2712 struct stat st;
brian m. carlsonaab61352018-02-01 02:18:45 +00002713 unsigned char hash[GIT_MAX_RAWSZ];
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002714
2715 if (!istate->initialized)
2716 return 0;
2717
2718 fd = open(path, O_RDONLY);
2719 if (fd < 0)
2720 return 0;
2721
2722 if (fstat(fd, &st))
2723 goto out;
2724
brian m. carlsonaab61352018-02-01 02:18:45 +00002725 if (st.st_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002726 goto out;
2727
brian m. carlsonaab61352018-02-01 02:18:45 +00002728 n = pread_in_full(fd, hash, the_hash_algo->rawsz, st.st_size - the_hash_algo->rawsz);
2729 if (n != the_hash_algo->rawsz)
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002730 goto out;
2731
Jeff King67947c32018-08-28 17:22:52 -04002732 if (!hasheq(istate->oid.hash, hash))
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002733 goto out;
2734
2735 close(fd);
2736 return 1;
2737
2738out:
2739 close(fd);
2740 return 0;
2741}
2742
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002743static int repo_verify_index(struct repository *repo)
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002744{
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002745 return verify_index_from(repo->index, repo->index_file);
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002746}
2747
Marc Strapetz2ede0732022-01-07 11:17:31 +00002748int has_racy_timestamp(struct index_state *istate)
Junio C Hamano483fbe22011-03-21 10:18:19 -07002749{
2750 int entries = istate->cache_nr;
2751 int i;
2752
2753 for (i = 0; i < entries; i++) {
2754 struct cache_entry *ce = istate->cache[i];
2755 if (is_racy_timestamp(istate, ce))
2756 return 1;
2757 }
2758 return 0;
2759}
2760
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002761void repo_update_index_if_able(struct repository *repo,
2762 struct lock_file *lockfile)
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07002763{
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002764 if ((repo->index->cache_changed ||
2765 has_racy_timestamp(repo->index)) &&
2766 repo_verify_index(repo))
2767 write_locked_index(repo->index, lockfile, COMMIT_LOCK);
Martin Ågrenb74c90f2017-10-06 22:12:14 +02002768 else
2769 rollback_lock_file(lockfile);
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07002770}
2771
Jonathan Niederd8465502018-11-19 22:11:47 -08002772static int record_eoie(void)
2773{
2774 int val;
2775
2776 if (!git_config_get_bool("index.recordendofindexentries", &val))
2777 return val;
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002778
2779 /*
2780 * As a convenience, the end of index entries extension
2781 * used for threading is written by default if the user
2782 * explicitly requested threaded index reads.
2783 */
2784 return !git_config_get_index_threads(&val) && val != 1;
Jonathan Niederd8465502018-11-19 22:11:47 -08002785}
2786
Jonathan Nieder42916052018-11-19 22:12:22 -08002787static int record_ieot(void)
2788{
2789 int val;
2790
2791 if (!git_config_get_bool("index.recordoffsettable", &val))
2792 return val;
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002793
2794 /*
2795 * As a convenience, the offset table used for threading is
2796 * written by default if the user explicitly requested
2797 * threaded index reads.
2798 */
2799 return !git_config_get_index_threads(&val) && val != 1;
Jonathan Nieder42916052018-11-19 22:12:22 -08002800}
2801
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00002802enum write_extensions {
2803 WRITE_NO_EXTENSION = 0,
2804 WRITE_SPLIT_INDEX_EXTENSION = 1<<0,
2805 WRITE_CACHE_TREE_EXTENSION = 1<<1,
2806 WRITE_RESOLVE_UNDO_EXTENSION = 1<<2,
2807 WRITE_UNTRACKED_CACHE_EXTENSION = 1<<3,
2808 WRITE_FSMONITOR_EXTENSION = 1<<4,
2809};
2810#define WRITE_ALL_EXTENSIONS ((enum write_extensions)-1)
2811
Martin Ågren812d6b02017-10-06 22:12:12 +02002812/*
2813 * On success, `tempfile` is closed. If it is the temporary file
2814 * of a `struct lock_file`, we will therefore effectively perform
2815 * a 'close_lock_file_gently()`. Since that is an implementation
2816 * detail of lockfiles, callers of `do_write_index()` should not
2817 * rely on it.
2818 */
Jeff Hostetler9f41c7a2017-04-26 22:05:23 +02002819static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00002820 enum write_extensions write_extensions, unsigned flags)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002821{
Nguyễn Thái Ngọc Duyca54d9b2018-01-27 19:27:56 +07002822 uint64_t start = getnanotime();
Derrick Stolee410334e2021-05-18 18:32:47 +00002823 struct hashfile *f;
2824 git_hash_ctx *eoie_c = NULL;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002825 struct cache_header hdr;
Kevin Willfordb50386c2017-08-21 15:24:31 -06002826 int i, err = 0, removed, extended, hdr_version;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002827 struct cache_entry **cache = istate->cache;
2828 int entries = istate->cache_nr;
Kjetil Barvike1afca42009-02-23 19:02:57 +01002829 struct stat st;
brian m. carlson575fa8a2019-02-19 00:05:24 +00002830 struct ondisk_cache_entry ondisk;
Junio C Hamano9d227782012-04-04 09:12:43 -07002831 struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
Thomas Gummerer4bddd982018-01-07 22:30:14 +00002832 int drop_cache_tree = istate->drop_cache_tree;
Ben Peart3b1d9e02018-10-10 11:59:34 -04002833 off_t offset;
Neeraj Singhba95e962022-03-10 22:43:23 +00002834 int csum_fsync_flag;
Ben Peart77ff1122018-10-10 11:59:38 -04002835 int ieot_entries = 1;
Ben Peart32550892018-10-10 11:59:37 -04002836 struct index_entry_offset_table *ieot = NULL;
2837 int nr, nr_threads;
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +01002838 struct repository *r = istate->repo;
Junio C Hamano025a0702005-06-10 01:32:37 -07002839
Derrick Stolee410334e2021-05-18 18:32:47 +00002840 f = hashfd(tempfile->fd, tempfile->filename.buf);
2841
Derrick Stolee17194b12023-01-06 16:31:56 +00002842 prepare_repo_settings(r);
2843 f->skip_hash = r->settings.index_skip_hash;
Derrick Stoleeee1f0c22023-01-06 16:31:54 +00002844
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002845 for (i = removed = extended = 0; i < entries; i++) {
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002846 if (cache[i]->ce_flags & CE_REMOVE)
Junio C Hamano025a0702005-06-10 01:32:37 -07002847 removed++;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002848
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002849 /* reduce extended entries if possible */
2850 cache[i]->ce_flags &= ~CE_EXTENDED;
2851 if (cache[i]->ce_flags & CE_EXTENDED_FLAGS) {
2852 extended++;
2853 cache[i]->ce_flags |= CE_EXTENDED;
2854 }
2855 }
2856
SZEDER Gábore8ffd032021-08-26 23:00:04 +02002857 if (!istate->version)
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02002858 istate->version = get_index_format_default(r);
Junio C Hamano9d227782012-04-04 09:12:43 -07002859
2860 /* demote version 3 to version 2 when the latter suffices */
2861 if (istate->version == 3 || istate->version == 2)
2862 istate->version = extended ? 3 : 2;
2863
2864 hdr_version = istate->version;
2865
Linus Torvaldsccc4feb2005-04-15 10:44:27 -07002866 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
Junio C Hamano9d227782012-04-04 09:12:43 -07002867 hdr.hdr_version = htonl(hdr_version);
Junio C Hamano025a0702005-06-10 01:32:37 -07002868 hdr.hdr_entries = htonl(entries - removed);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002869
Derrick Stolee410334e2021-05-18 18:32:47 +00002870 hashwrite(f, &hdr, sizeof(hdr));
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002871
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002872 if (!HAVE_THREADS || git_config_get_index_threads(&nr_threads))
Nguyễn Thái Ngọc Duy88168b92018-11-03 09:48:48 +01002873 nr_threads = 1;
Nguyễn Thái Ngọc Duy62e5ee82018-11-03 09:48:47 +01002874
Jonathan Nieder42916052018-11-19 22:12:22 -08002875 if (nr_threads != 1 && record_ieot()) {
Ben Peart32550892018-10-10 11:59:37 -04002876 int ieot_blocks, cpus;
2877
2878 /*
2879 * ensure default number of ieot blocks maps evenly to the
2880 * default number of threads that will process them leaving
2881 * room for the thread to load the index extensions.
2882 */
2883 if (!nr_threads) {
2884 ieot_blocks = istate->cache_nr / THREAD_COST;
2885 cpus = online_cpus();
2886 if (ieot_blocks > cpus - 1)
2887 ieot_blocks = cpus - 1;
2888 } else {
2889 ieot_blocks = nr_threads;
Ben Peart77ff1122018-10-10 11:59:38 -04002890 if (ieot_blocks > istate->cache_nr)
2891 ieot_blocks = istate->cache_nr;
Ben Peart32550892018-10-10 11:59:37 -04002892 }
2893
2894 /*
2895 * no reason to write out the IEOT extension if we don't
2896 * have enough blocks to utilize multi-threading
2897 */
2898 if (ieot_blocks > 1) {
2899 ieot = xcalloc(1, sizeof(struct index_entry_offset_table)
2900 + (ieot_blocks * sizeof(struct index_entry_offset)));
Ben Peart77ff1122018-10-10 11:59:38 -04002901 ieot_entries = DIV_ROUND_UP(entries, ieot_blocks);
Ben Peart32550892018-10-10 11:59:37 -04002902 }
2903 }
Ben Peart32550892018-10-10 11:59:37 -04002904
Derrick Stolee410334e2021-05-18 18:32:47 +00002905 offset = hashfile_total(f);
2906
Ben Peart32550892018-10-10 11:59:37 -04002907 nr = 0;
Junio C Hamano9d227782012-04-04 09:12:43 -07002908 previous_name = (hdr_version == 4) ? &previous_name_buf : NULL;
Kevin Willfordce012de2017-08-21 15:24:32 -06002909
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002910 for (i = 0; i < entries; i++) {
2911 struct cache_entry *ce = cache[i];
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002912 if (ce->ce_flags & CE_REMOVE)
Linus Torvaldsaa160212005-06-09 15:34:04 -07002913 continue;
Junio C Hamanoe06c43c2008-03-30 09:25:52 -07002914 if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce))
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002915 ce_smudge_racily_clean_entry(istate, ce);
brian m. carlson99d1a982016-09-05 20:07:52 +00002916 if (is_null_oid(&ce->oid)) {
Jeff King83bd7432013-08-27 16:41:12 -04002917 static const char msg[] = "cache entry has null sha1: %s";
2918 static int allow = -1;
2919
2920 if (allow < 0)
2921 allow = git_env_bool("GIT_ALLOW_NULL_SHA1", 0);
2922 if (allow)
2923 warning(msg, ce->name);
2924 else
Kevin Willfordb50386c2017-08-21 15:24:31 -06002925 err = error(msg, ce->name);
Jeff Kinga96d3cc2017-04-21 14:46:17 -04002926
2927 drop_cache_tree = 1;
Jeff King83bd7432013-08-27 16:41:12 -04002928 }
Ben Peart77ff1122018-10-10 11:59:38 -04002929 if (ieot && i && (i % ieot_entries == 0)) {
Ben Peart32550892018-10-10 11:59:37 -04002930 ieot->entries[ieot->nr].nr = nr;
2931 ieot->entries[ieot->nr].offset = offset;
2932 ieot->nr++;
2933 /*
2934 * If we have a V4 index, set the first byte to an invalid
2935 * character to ensure there is nothing common with the previous
2936 * entry
2937 */
2938 if (previous_name)
2939 previous_name->buf[0] = 0;
2940 nr = 0;
Derrick Stolee410334e2021-05-18 18:32:47 +00002941
2942 offset = hashfile_total(f);
Ben Peart32550892018-10-10 11:59:37 -04002943 }
Derrick Stolee410334e2021-05-18 18:32:47 +00002944 if (ce_write_entry(f, ce, previous_name, (struct ondisk_cache_entry *)&ondisk) < 0)
Kevin Willfordb50386c2017-08-21 15:24:31 -06002945 err = -1;
2946
2947 if (err)
2948 break;
Ben Peart32550892018-10-10 11:59:37 -04002949 nr++;
2950 }
2951 if (ieot && nr) {
2952 ieot->entries[ieot->nr].nr = nr;
2953 ieot->entries[ieot->nr].offset = offset;
2954 ieot->nr++;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002955 }
Junio C Hamano9d227782012-04-04 09:12:43 -07002956 strbuf_release(&previous_name_buf);
Junio C Hamano1af1c2b2006-04-23 16:52:08 -07002957
Ben Peart32550892018-10-10 11:59:37 -04002958 if (err) {
2959 free(ieot);
Kevin Willfordb50386c2017-08-21 15:24:31 -06002960 return err;
Ben Peart32550892018-10-10 11:59:37 -04002961 }
Kevin Willfordb50386c2017-08-21 15:24:31 -06002962
Derrick Stolee410334e2021-05-18 18:32:47 +00002963 offset = hashfile_total(f);
2964
2965 /*
2966 * The extension headers must be hashed on their own for the
2967 * EOIE extension. Create a hashfile here to compute that hash.
2968 */
2969 if (offset && record_eoie()) {
2970 CALLOC_ARRAY(eoie_c, 1);
2971 the_hash_algo->init_fn(eoie_c);
Ben Peart32550892018-10-10 11:59:37 -04002972 }
Ben Peart3b1d9e02018-10-10 11:59:34 -04002973
Ben Peart32550892018-10-10 11:59:37 -04002974 /*
2975 * Lets write out CACHE_EXT_INDEXENTRYOFFSETTABLE first so that we
2976 * can minimize the number of extensions we have to scan through to
2977 * find it during load. Write it out regardless of the
2978 * strip_extensions parameter as we need it when loading the shared
2979 * index.
2980 */
Ben Peart32550892018-10-10 11:59:37 -04002981 if (ieot) {
2982 struct strbuf sb = STRBUF_INIT;
2983
2984 write_ieot_extension(&sb, ieot);
Derrick Stolee410334e2021-05-18 18:32:47 +00002985 err = write_index_ext_header(f, eoie_c, CACHE_EXT_INDEXENTRYOFFSETTABLE, sb.len) < 0;
2986 hashwrite(f, sb.buf, sb.len);
Ben Peart32550892018-10-10 11:59:37 -04002987 strbuf_release(&sb);
2988 free(ieot);
2989 if (err)
2990 return -1;
2991 }
Ben Peart32550892018-10-10 11:59:37 -04002992
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00002993 if (write_extensions & WRITE_SPLIT_INDEX_EXTENSION &&
2994 istate->split_index) {
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002995 struct strbuf sb = STRBUF_INIT;
2996
Johannes Schindelin451b66c2022-01-19 17:29:39 +00002997 if (istate->sparse_index)
2998 die(_("cannot write split index for a sparse index"));
2999
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003000 err = write_link_extension(&sb, istate) < 0 ||
Derrick Stolee410334e2021-05-18 18:32:47 +00003001 write_index_ext_header(f, eoie_c, CACHE_EXT_LINK,
3002 sb.len) < 0;
3003 hashwrite(f, sb.buf, sb.len);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003004 strbuf_release(&sb);
3005 if (err)
3006 return -1;
3007 }
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00003008 if (write_extensions & WRITE_CACHE_TREE_EXTENSION &&
3009 !drop_cache_tree && istate->cache_tree) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05003010 struct strbuf sb = STRBUF_INIT;
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02003011
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02003012 cache_tree_write(&sb, istate->cache_tree);
Derrick Stolee410334e2021-05-18 18:32:47 +00003013 err = write_index_ext_header(f, eoie_c, CACHE_EXT_TREE, sb.len) < 0;
3014 hashwrite(f, sb.buf, sb.len);
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02003015 strbuf_release(&sb);
3016 if (err)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07003017 return -1;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07003018 }
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00003019 if (write_extensions & WRITE_RESOLVE_UNDO_EXTENSION &&
3020 istate->resolve_undo) {
Junio C Hamanocfc57892009-12-25 00:30:51 -08003021 struct strbuf sb = STRBUF_INIT;
3022
3023 resolve_undo_write(&sb, istate->resolve_undo);
Derrick Stolee410334e2021-05-18 18:32:47 +00003024 err = write_index_ext_header(f, eoie_c, CACHE_EXT_RESOLVE_UNDO,
3025 sb.len) < 0;
3026 hashwrite(f, sb.buf, sb.len);
Junio C Hamanocfc57892009-12-25 00:30:51 -08003027 strbuf_release(&sb);
3028 if (err)
3029 return -1;
3030 }
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00003031 if (write_extensions & WRITE_UNTRACKED_CACHE_EXTENSION &&
3032 istate->untracked) {
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07003033 struct strbuf sb = STRBUF_INIT;
3034
3035 write_untracked_extension(&sb, istate->untracked);
Derrick Stolee410334e2021-05-18 18:32:47 +00003036 err = write_index_ext_header(f, eoie_c, CACHE_EXT_UNTRACKED,
3037 sb.len) < 0;
3038 hashwrite(f, sb.buf, sb.len);
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07003039 strbuf_release(&sb);
3040 if (err)
3041 return -1;
3042 }
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00003043 if (write_extensions & WRITE_FSMONITOR_EXTENSION &&
3044 istate->fsmonitor_last_update) {
Ben Peart883e2482017-09-22 12:35:40 -04003045 struct strbuf sb = STRBUF_INIT;
3046
3047 write_fsmonitor_extension(&sb, istate);
Derrick Stolee410334e2021-05-18 18:32:47 +00003048 err = write_index_ext_header(f, eoie_c, CACHE_EXT_FSMONITOR, sb.len) < 0;
3049 hashwrite(f, sb.buf, sb.len);
Ben Peart3b1d9e02018-10-10 11:59:34 -04003050 strbuf_release(&sb);
3051 if (err)
3052 return -1;
3053 }
Derrick Stoleecd424152021-03-30 13:10:54 +00003054 if (istate->sparse_index) {
Derrick Stolee410334e2021-05-18 18:32:47 +00003055 if (write_index_ext_header(f, eoie_c, CACHE_EXT_SPARSE_DIRECTORIES, 0) < 0)
Derrick Stoleecd424152021-03-30 13:10:54 +00003056 return -1;
3057 }
Ben Peart3b1d9e02018-10-10 11:59:34 -04003058
3059 /*
3060 * CACHE_EXT_ENDOFINDEXENTRIES must be written as the last entry before the SHA1
3061 * so that it can be found and processed before all the index entries are
3062 * read. Write it out regardless of the strip_extensions parameter as we need it
3063 * when loading the shared index.
3064 */
Derrick Stolee410334e2021-05-18 18:32:47 +00003065 if (eoie_c) {
Ben Peart3b1d9e02018-10-10 11:59:34 -04003066 struct strbuf sb = STRBUF_INIT;
3067
Derrick Stolee410334e2021-05-18 18:32:47 +00003068 write_eoie_extension(&sb, eoie_c, offset);
3069 err = write_index_ext_header(f, NULL, CACHE_EXT_ENDOFINDEXENTRIES, sb.len) < 0;
3070 hashwrite(f, sb.buf, sb.len);
Ben Peart883e2482017-09-22 12:35:40 -04003071 strbuf_release(&sb);
3072 if (err)
3073 return -1;
3074 }
Kjetil Barvike1afca42009-02-23 19:02:57 +01003075
Neeraj Singhba95e962022-03-10 22:43:23 +00003076 csum_fsync_flag = 0;
3077 if (!alternate_index_output && (flags & COMMIT_LOCK))
3078 csum_fsync_flag = CSUM_FSYNC;
3079
3080 finalize_hashfile(f, istate->oid.hash, FSYNC_COMPONENT_INDEX,
3081 CSUM_HASH_IN_STREAM | csum_fsync_flag);
3082
Jeff King49bd0fc2017-09-05 08:14:30 -04003083 if (close_tempfile_gently(tempfile)) {
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003084 error(_("could not close '%s'"), get_tempfile_path(tempfile));
Jeff King49bd0fc2017-09-05 08:14:30 -04003085 return -1;
3086 }
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003087 if (stat(get_tempfile_path(tempfile), &st))
Kjetil Barvike1afca42009-02-23 19:02:57 +01003088 return -1;
Kjetil Barvik5bcf1092009-03-15 12:38:55 +01003089 istate->timestamp.sec = (unsigned int)st.st_mtime;
3090 istate->timestamp.nsec = ST_MTIME_NSEC(st);
Nguyễn Thái Ngọc Duyca54d9b2018-01-27 19:27:56 +07003091 trace_performance_since(start, "write index, changed mask = %x", istate->cache_changed);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003092
3093 /*
3094 * TODO trace2: replace "the_repository" with the actual repo instance
3095 * that is associated with the given "istate".
3096 */
3097 trace2_data_intmax("index", the_repository, "write/version",
3098 istate->version);
3099 trace2_data_intmax("index", the_repository, "write/cache_nr",
3100 istate->cache_nr);
3101
Kjetil Barvike1afca42009-02-23 19:02:57 +01003102 return 0;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07003103}
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003104
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07003105void set_alternate_index_output(const char *name)
3106{
3107 alternate_index_output = name;
3108}
3109
3110static int commit_locked_index(struct lock_file *lk)
3111{
Michael Haggerty751bace2014-10-01 12:28:36 +02003112 if (alternate_index_output)
3113 return commit_lock_file_to(lk, alternate_index_output);
3114 else
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07003115 return commit_lock_file(lk);
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07003116}
3117
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00003118static int do_write_locked_index(struct index_state *istate,
3119 struct lock_file *lock,
3120 unsigned flags,
3121 enum write_extensions write_extensions)
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003122{
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003123 int ret;
Derrick Stolee9fadb372022-05-23 13:48:40 +00003124 int was_full = istate->sparse_index == INDEX_EXPANDED;
Derrick Stolee6e773522021-03-30 13:10:55 +00003125
Derrick Stoleece7a9f02021-09-08 01:42:32 +00003126 ret = convert_to_sparse(istate, 0);
Derrick Stolee6e773522021-03-30 13:10:55 +00003127
3128 if (ret) {
3129 warning(_("failed to convert to a sparse-index"));
3130 return ret;
3131 }
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003132
3133 /*
3134 * TODO trace2: replace "the_repository" with the actual repo instance
3135 * that is associated with the given "istate".
3136 */
3137 trace2_region_enter_printf("index", "do_write_index", the_repository,
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003138 "%s", get_lock_file_path(lock));
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00003139 ret = do_write_index(istate, lock->tempfile, write_extensions, flags);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003140 trace2_region_leave_printf("index", "do_write_index", the_repository,
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003141 "%s", get_lock_file_path(lock));
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003142
Derrick Stolee6e773522021-03-30 13:10:55 +00003143 if (was_full)
3144 ensure_full_index(istate);
3145
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003146 if (ret)
3147 return ret;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003148 if (flags & COMMIT_LOCK)
Ben Peart1956ecd2019-02-15 12:59:21 -05003149 ret = commit_locked_index(lock);
3150 else
3151 ret = close_lock_file_gently(lock);
3152
Emily Shafferdbb1c612021-12-22 04:59:41 +01003153 run_hooks_l("post-index-change",
Ben Peart1956ecd2019-02-15 12:59:21 -05003154 istate->updated_workdir ? "1" : "0",
3155 istate->updated_skipworktree ? "1" : "0", NULL);
3156 istate->updated_workdir = 0;
3157 istate->updated_skipworktree = 0;
3158
3159 return ret;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003160}
3161
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003162static int write_split_index(struct index_state *istate,
3163 struct lock_file *lock,
3164 unsigned flags)
3165{
3166 int ret;
3167 prepare_to_write_split_index(istate);
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00003168 ret = do_write_locked_index(istate, lock, flags, WRITE_ALL_EXTENSIONS);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003169 finish_writing_split_index(istate);
3170 return ret;
3171}
3172
Christian Couderb9683722017-03-06 10:41:58 +01003173static const char *shared_index_expire = "2.weeks.ago";
3174
3175static unsigned long get_shared_index_expire_date(void)
3176{
3177 static unsigned long shared_index_expire_date;
3178 static int shared_index_expire_date_prepared;
3179
3180 if (!shared_index_expire_date_prepared) {
3181 git_config_get_expiry("splitindex.sharedindexexpire",
3182 &shared_index_expire);
3183 shared_index_expire_date = approxidate(shared_index_expire);
3184 shared_index_expire_date_prepared = 1;
3185 }
3186
3187 return shared_index_expire_date;
3188}
3189
3190static int should_delete_shared_index(const char *shared_index_path)
3191{
3192 struct stat st;
3193 unsigned long expiration;
3194
3195 /* Check timestamp */
3196 expiration = get_shared_index_expire_date();
3197 if (!expiration)
3198 return 0;
3199 if (stat(shared_index_path, &st))
Peter Krefting78bde922017-04-30 23:32:48 +02003200 return error_errno(_("could not stat '%s'"), shared_index_path);
Christian Couderb9683722017-03-06 10:41:58 +01003201 if (st.st_mtime > expiration)
3202 return 0;
3203
3204 return 1;
3205}
3206
3207static int clean_shared_index_files(const char *current_hex)
3208{
3209 struct dirent *de;
3210 DIR *dir = opendir(get_git_dir());
3211
3212 if (!dir)
3213 return error_errno(_("unable to open git dir: %s"), get_git_dir());
3214
3215 while ((de = readdir(dir)) != NULL) {
3216 const char *sha1_hex;
3217 const char *shared_index_path;
3218 if (!skip_prefix(de->d_name, "sharedindex.", &sha1_hex))
3219 continue;
3220 if (!strcmp(sha1_hex, current_hex))
3221 continue;
3222 shared_index_path = git_path("%s", de->d_name);
3223 if (should_delete_shared_index(shared_index_path) > 0 &&
3224 unlink(shared_index_path))
3225 warning_errno(_("unable to unlink: %s"), shared_index_path);
3226 }
3227 closedir(dir);
3228
3229 return 0;
3230}
3231
Nguyễn Thái Ngọc Duya0a96752014-06-13 19:19:45 +07003232static int write_shared_index(struct index_state *istate,
Neeraj Singhba95e962022-03-10 22:43:23 +00003233 struct tempfile **temp, unsigned flags)
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003234{
3235 struct split_index *si = istate->split_index;
Derrick Stolee6e773522021-03-30 13:10:55 +00003236 int ret, was_full = !istate->sparse_index;
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003237
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003238 move_cache_to_base_index(istate);
Derrick Stoleece7a9f02021-09-08 01:42:32 +00003239 convert_to_sparse(istate, 0);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003240
3241 trace2_region_enter_printf("index", "shared/do_write_index",
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003242 the_repository, "%s", get_tempfile_path(*temp));
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00003243 ret = do_write_index(si->base, *temp, WRITE_NO_EXTENSION, flags);
Ævar Arnfjörð Bjarmasonc1735422019-05-10 15:37:38 +02003244 trace2_region_leave_printf("index", "shared/do_write_index",
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003245 the_repository, "%s", get_tempfile_path(*temp));
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003246
Derrick Stolee6e773522021-03-30 13:10:55 +00003247 if (was_full)
3248 ensure_full_index(istate);
3249
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003250 if (ret)
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003251 return ret;
Nguyễn Thái Ngọc Duy7db2d082018-01-14 17:18:18 +07003252 ret = adjust_shared_perm(get_tempfile_path(*temp));
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003253 if (ret) {
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01003254 error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp));
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003255 return ret;
3256 }
Nguyễn Thái Ngọc Duy7db2d082018-01-14 17:18:18 +07003257 ret = rename_tempfile(temp,
brian m. carlson75691ea2018-05-02 00:25:44 +00003258 git_path("sharedindex.%s", oid_to_hex(&si->base->oid)));
Christian Couderb9683722017-03-06 10:41:58 +01003259 if (!ret) {
brian m. carlson75691ea2018-05-02 00:25:44 +00003260 oidcpy(&si->base_oid, &si->base->oid);
3261 clean_shared_index_files(oid_to_hex(&si->base->oid));
Christian Couderb9683722017-03-06 10:41:58 +01003262 }
3263
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003264 return ret;
3265}
3266
Christian Coudere6a1dd72017-02-27 19:00:08 +01003267static const int default_max_percent_split_change = 20;
3268
3269static int too_many_not_shared_entries(struct index_state *istate)
3270{
3271 int i, not_shared = 0;
3272 int max_split = git_config_get_max_percent_split_change();
3273
3274 switch (max_split) {
3275 case -1:
3276 /* not or badly configured: use the default value */
3277 max_split = default_max_percent_split_change;
3278 break;
3279 case 0:
3280 return 1; /* 0% means always write a new shared index */
3281 case 100:
3282 return 0; /* 100% means never write a new shared index */
3283 default:
3284 break; /* just use the configured value */
3285 }
3286
3287 /* Count not shared entries */
3288 for (i = 0; i < istate->cache_nr; i++) {
3289 struct cache_entry *ce = istate->cache[i];
3290 if (!ce->index)
3291 not_shared++;
3292 }
3293
3294 return (int64_t)istate->cache_nr * max_split < (int64_t)not_shared * 100;
3295}
3296
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003297int write_locked_index(struct index_state *istate, struct lock_file *lock,
3298 unsigned flags)
3299{
SZEDER Gábore8ffd032021-08-26 23:00:04 +02003300 int new_shared_index, ret, test_split_index_env;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003301 struct split_index *si = istate->split_index;
3302
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +02003303 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +01003304 cache_tree_verify(the_repository, istate);
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +02003305
Martin Ågren61000812018-03-01 21:40:20 +01003306 if ((flags & SKIP_IF_UNCHANGED) && !istate->cache_changed) {
3307 if (flags & COMMIT_LOCK)
3308 rollback_lock_file(lock);
3309 return 0;
3310 }
3311
Alex Vandiver3bd28eb2017-11-09 11:58:10 -08003312 if (istate->fsmonitor_last_update)
3313 fill_fsmonitor_bitmap(istate);
3314
SZEDER Gábore8ffd032021-08-26 23:00:04 +02003315 test_split_index_env = git_env_bool("GIT_TEST_SPLIT_INDEX", 0);
3316
3317 if ((!si && !test_split_index_env) ||
3318 alternate_index_output ||
Nguyễn Thái Ngọc Duy5165dd52014-06-13 19:19:47 +07003319 (istate->cache_changed & ~EXTMASK)) {
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00003320 ret = do_write_locked_index(istate, lock, flags,
3321 ~WRITE_SPLIT_INDEX_EXTENSION);
Martin Ågrendf60cf52017-10-06 22:12:13 +02003322 goto out;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003323 }
3324
SZEDER Gábore8ffd032021-08-26 23:00:04 +02003325 if (test_split_index_env) {
3326 if (!si) {
3327 si = init_split_index(istate);
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07003328 istate->cache_changed |= SPLIT_INDEX_ORDERED;
SZEDER Gábore8ffd032021-08-26 23:00:04 +02003329 } else {
3330 int v = si->base_oid.hash[0];
3331 if ((v & 15) < 6)
3332 istate->cache_changed |= SPLIT_INDEX_ORDERED;
3333 }
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07003334 }
Christian Coudere6a1dd72017-02-27 19:00:08 +01003335 if (too_many_not_shared_entries(istate))
3336 istate->cache_changed |= SPLIT_INDEX_ORDERED;
Christian Couder0d59ffb2017-02-27 19:00:12 +01003337
3338 new_shared_index = istate->cache_changed & SPLIT_INDEX_ORDERED;
3339
3340 if (new_shared_index) {
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003341 struct tempfile *temp;
3342 int saved_errno;
3343
Ævar Arnfjörð Bjarmasonc9d6c782018-11-18 19:04:29 +00003344 /* Same initial permissions as the main .git/index file */
3345 temp = mks_tempfile_sm(git_path("sharedindex_XXXXXX"), 0, 0666);
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003346 if (!temp) {
Johannes Schindelin3b7a4472023-03-26 22:45:41 +00003347 ret = do_write_locked_index(istate, lock, flags,
3348 ~WRITE_SPLIT_INDEX_EXTENSION);
Nguyễn Thái Ngọc Duyef5b3a62018-01-24 16:38:29 +07003349 goto out;
3350 }
Neeraj Singhba95e962022-03-10 22:43:23 +00003351 ret = write_shared_index(istate, &temp, flags);
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003352
3353 saved_errno = errno;
3354 if (is_tempfile_active(temp))
3355 delete_tempfile(&temp);
3356 errno = saved_errno;
3357
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003358 if (ret)
Martin Ågrendf60cf52017-10-06 22:12:13 +02003359 goto out;
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003360 }
3361
Christian Couder0d59ffb2017-02-27 19:00:12 +01003362 ret = write_split_index(istate, lock, flags);
3363
3364 /* Freshen the shared index only if the split-index was written */
Nguyễn Thái Ngọc Duy6e37c8e2019-02-13 16:51:29 +07003365 if (!ret && !new_shared_index && !is_null_oid(&si->base_oid)) {
Thomas Gummerera125a222018-01-07 22:30:13 +00003366 const char *shared_index = git_path("sharedindex.%s",
brian m. carlson2182abd2018-05-02 00:25:43 +00003367 oid_to_hex(&si->base_oid));
Thomas Gummerera125a222018-01-07 22:30:13 +00003368 freshen_shared_index(shared_index, 1);
3369 }
Christian Couder0d59ffb2017-02-27 19:00:12 +01003370
Martin Ågrendf60cf52017-10-06 22:12:13 +02003371out:
3372 if (flags & COMMIT_LOCK)
3373 rollback_lock_file(lock);
Christian Couder0d59ffb2017-02-27 19:00:12 +01003374 return ret;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003375}
3376
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003377/*
3378 * Read the index file that is potentially unmerged into given
Elijah Newrenad376202018-07-31 10:12:05 -07003379 * index_state, dropping any unmerged entries to stage #0 (potentially
3380 * resulting in a path appearing as both a file and a directory in the
3381 * index; the caller is responsible to clear out the extra entries
3382 * before writing the index to a tree). Returns true if the index is
3383 * unmerged. Callers who want to refuse to work from an unmerged
3384 * state can call this and check its return value, instead of calling
3385 * read_cache().
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003386 */
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07003387int repo_read_index_unmerged(struct repository *repo)
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003388{
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07003389 struct index_state *istate;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003390 int i;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003391 int unmerged = 0;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003392
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07003393 repo_read_index(repo);
3394 istate = repo->index;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003395 for (i = 0; i < istate->cache_nr; i++) {
3396 struct cache_entry *ce = istate->cache[i];
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003397 struct cache_entry *new_ce;
Jameson Millera8497352018-07-02 19:49:31 +00003398 int len;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003399
3400 if (!ce_stage(ce))
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003401 continue;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003402 unmerged = 1;
Thomas Gummerer68c4f6a2012-07-06 18:07:30 +02003403 len = ce_namelen(ce);
Jameson Millera8497352018-07-02 19:49:31 +00003404 new_ce = make_empty_cache_entry(istate, len);
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003405 memcpy(new_ce->name, ce->name, len);
Thomas Gummererb60e1882012-07-11 11:22:37 +02003406 new_ce->ce_flags = create_ce_flags(0) | CE_CONFLICTED;
3407 new_ce->ce_namelen = len;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003408 new_ce->ce_mode = ce->ce_mode;
Elijah Newrenad376202018-07-31 10:12:05 -07003409 if (add_index_entry(istate, new_ce, ADD_CACHE_SKIP_DFCHECK))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01003410 return error(_("%s: cannot drop to stage #0"),
Karsten Blees5699d172013-11-14 20:24:37 +01003411 new_ce->name);
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003412 }
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003413 return unmerged;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003414}
Junio C Hamano041aee32008-07-21 01:24:17 -07003415
Jeff King98fa4732008-10-16 11:07:26 -04003416/*
3417 * Returns 1 if the path is an "other" path with respect to
3418 * the index; that is, the path is not mentioned in the index at all,
3419 * either as a file, a directory with some files in the index,
3420 * or as an unmerged entry.
3421 *
3422 * We helpfully remove a trailing "/" from directories so that
3423 * the output of read_directory can be used as-is.
3424 */
Derrick Stolee847a9e52021-04-01 01:49:39 +00003425int index_name_is_other(struct index_state *istate, const char *name,
3426 int namelen)
Jeff King98fa4732008-10-16 11:07:26 -04003427{
3428 int pos;
3429 if (namelen && name[namelen - 1] == '/')
3430 namelen--;
3431 pos = index_name_pos(istate, name, namelen);
3432 if (0 <= pos)
3433 return 0; /* exact match */
3434 pos = -pos - 1;
3435 if (pos < istate->cache_nr) {
3436 struct cache_entry *ce = istate->cache[pos];
3437 if (ce_namelen(ce) == namelen &&
3438 !memcmp(ce->name, name, namelen))
3439 return 0; /* Yup, this one exists unmerged */
3440 }
3441 return 1;
3442}
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003443
Derrick Stolee847a9e52021-04-01 01:49:39 +00003444void *read_blob_data_from_index(struct index_state *istate,
Brandon Williams87542502017-01-10 12:06:10 -08003445 const char *path, unsigned long *size)
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003446{
3447 int pos, len;
3448 unsigned long sz;
3449 enum object_type type;
3450 void *data;
3451
3452 len = strlen(path);
3453 pos = index_name_pos(istate, path, len);
3454 if (pos < 0) {
3455 /*
3456 * We might be in the middle of a merge, in which
3457 * case we would read stage #2 (ours).
3458 */
3459 int i;
3460 for (i = -pos - 1;
3461 (pos < 0 && i < istate->cache_nr &&
3462 !strcmp(istate->cache[i]->name, path));
3463 i++)
3464 if (ce_stage(istate->cache[i]) == 2)
3465 pos = i;
3466 }
3467 if (pos < 0)
3468 return NULL;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02003469 data = repo_read_object_file(the_repository, &istate->cache[pos]->oid,
3470 &type, &sz);
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003471 if (!data || type != OBJ_BLOB) {
3472 free(data);
3473 return NULL;
3474 }
Lukas Fleischerff366822013-04-13 15:28:31 +02003475 if (size)
3476 *size = sz;
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003477 return data;
3478}
Michael Haggerty38612532013-06-20 10:37:51 +02003479
David Turneredf3b902017-05-08 11:41:42 +02003480void move_index_extensions(struct index_state *dst, struct index_state *src)
3481{
3482 dst->untracked = src->untracked;
3483 src->untracked = NULL;
Nguyễn Thái Ngọc Duy836ef2b2018-08-18 16:41:26 +02003484 dst->cache_tree = src->cache_tree;
3485 src->cache_tree = NULL;
David Turneredf3b902017-05-08 11:41:42 +02003486}
Jameson Millera8497352018-07-02 19:49:31 +00003487
Jameson Miller8e72d672018-07-02 19:49:37 +00003488struct cache_entry *dup_cache_entry(const struct cache_entry *ce,
3489 struct index_state *istate)
3490{
3491 unsigned int size = ce_size(ce);
3492 int mem_pool_allocated;
3493 struct cache_entry *new_entry = make_empty_cache_entry(istate, ce_namelen(ce));
3494 mem_pool_allocated = new_entry->mem_pool_allocated;
3495
3496 memcpy(new_entry, ce, size);
3497 new_entry->mem_pool_allocated = mem_pool_allocated;
3498 return new_entry;
3499}
3500
Jameson Millera8497352018-07-02 19:49:31 +00003501void discard_cache_entry(struct cache_entry *ce)
3502{
Jameson Miller8616a2d2018-07-02 19:49:39 +00003503 if (ce && should_validate_cache_entries())
3504 memset(ce, 0xCD, cache_entry_size(ce->ce_namelen));
3505
Jameson Miller8e72d672018-07-02 19:49:37 +00003506 if (ce && ce->mem_pool_allocated)
3507 return;
3508
Jameson Millera8497352018-07-02 19:49:31 +00003509 free(ce);
3510}
Jameson Miller8616a2d2018-07-02 19:49:39 +00003511
3512int should_validate_cache_entries(void)
3513{
3514 static int validate_index_cache_entries = -1;
3515
3516 if (validate_index_cache_entries < 0) {
3517 if (getenv("GIT_TEST_VALIDATE_INDEX_CACHE_ENTRIES"))
3518 validate_index_cache_entries = 1;
3519 else
3520 validate_index_cache_entries = 0;
3521 }
3522
3523 return validate_index_cache_entries;
3524}
Ben Peart3b1d9e02018-10-10 11:59:34 -04003525
3526#define EOIE_SIZE (4 + GIT_SHA1_RAWSZ) /* <4-byte offset> + <20-byte hash> */
3527#define EOIE_SIZE_WITH_HEADER (4 + 4 + EOIE_SIZE) /* <4-byte signature> + <4-byte length> + EOIE_SIZE */
3528
3529static size_t read_eoie_extension(const char *mmap, size_t mmap_size)
3530{
3531 /*
3532 * The end of index entries (EOIE) extension is guaranteed to be last
3533 * so that it can be found by scanning backwards from the EOF.
3534 *
3535 * "EOIE"
3536 * <4-byte length>
3537 * <4-byte offset>
3538 * <20-byte hash>
3539 */
3540 const char *index, *eoie;
3541 uint32_t extsize;
3542 size_t offset, src_offset;
3543 unsigned char hash[GIT_MAX_RAWSZ];
3544 git_hash_ctx c;
3545
3546 /* ensure we have an index big enough to contain an EOIE extension */
3547 if (mmap_size < sizeof(struct cache_header) + EOIE_SIZE_WITH_HEADER + the_hash_algo->rawsz)
3548 return 0;
3549
3550 /* validate the extension signature */
3551 index = eoie = mmap + mmap_size - EOIE_SIZE_WITH_HEADER - the_hash_algo->rawsz;
3552 if (CACHE_EXT(index) != CACHE_EXT_ENDOFINDEXENTRIES)
3553 return 0;
3554 index += sizeof(uint32_t);
3555
3556 /* validate the extension size */
3557 extsize = get_be32(index);
3558 if (extsize != EOIE_SIZE)
3559 return 0;
3560 index += sizeof(uint32_t);
3561
3562 /*
3563 * Validate the offset we're going to look for the first extension
3564 * signature is after the index header and before the eoie extension.
3565 */
3566 offset = get_be32(index);
3567 if (mmap + offset < mmap + sizeof(struct cache_header))
3568 return 0;
3569 if (mmap + offset >= eoie)
3570 return 0;
3571 index += sizeof(uint32_t);
3572
3573 /*
3574 * The hash is computed over extension types and their sizes (but not
3575 * their contents). E.g. if we have "TREE" extension that is N-bytes
3576 * long, "REUC" extension that is M-bytes long, followed by "EOIE",
3577 * then the hash would be:
3578 *
3579 * SHA-1("TREE" + <binary representation of N> +
3580 * "REUC" + <binary representation of M>)
3581 */
3582 src_offset = offset;
3583 the_hash_algo->init_fn(&c);
3584 while (src_offset < mmap_size - the_hash_algo->rawsz - EOIE_SIZE_WITH_HEADER) {
3585 /* After an array of active_nr index entries,
3586 * there can be arbitrary number of extended
3587 * sections, each of which is prefixed with
3588 * extension name (4-byte) and section length
3589 * in 4-byte network byte order.
3590 */
3591 uint32_t extsize;
3592 memcpy(&extsize, mmap + src_offset + 4, 4);
3593 extsize = ntohl(extsize);
3594
3595 /* verify the extension size isn't so large it will wrap around */
3596 if (src_offset + 8 + extsize < src_offset)
3597 return 0;
3598
3599 the_hash_algo->update_fn(&c, mmap + src_offset, 8);
3600
3601 src_offset += 8;
3602 src_offset += extsize;
3603 }
3604 the_hash_algo->final_fn(hash, &c);
3605 if (!hasheq(hash, (const unsigned char *)index))
3606 return 0;
3607
3608 /* Validate that the extension offsets returned us back to the eoie extension. */
3609 if (src_offset != mmap_size - the_hash_algo->rawsz - EOIE_SIZE_WITH_HEADER)
3610 return 0;
3611
3612 return offset;
3613}
3614
3615static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset)
3616{
3617 uint32_t buffer;
3618 unsigned char hash[GIT_MAX_RAWSZ];
3619
3620 /* offset */
3621 put_be32(&buffer, offset);
3622 strbuf_add(sb, &buffer, sizeof(uint32_t));
3623
3624 /* hash */
3625 the_hash_algo->final_fn(hash, eoie_context);
3626 strbuf_add(sb, hash, the_hash_algo->rawsz);
3627}
Ben Peart32550892018-10-10 11:59:37 -04003628
Ben Peart32550892018-10-10 11:59:37 -04003629#define IEOT_VERSION (1)
3630
3631static struct index_entry_offset_table *read_ieot_extension(const char *mmap, size_t mmap_size, size_t offset)
3632{
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003633 const char *index = NULL;
3634 uint32_t extsize, ext_version;
3635 struct index_entry_offset_table *ieot;
3636 int i, nr;
Ben Peart32550892018-10-10 11:59:37 -04003637
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003638 /* find the IEOT extension */
3639 if (!offset)
3640 return NULL;
3641 while (offset <= mmap_size - the_hash_algo->rawsz - 8) {
3642 extsize = get_be32(mmap + offset + 4);
3643 if (CACHE_EXT((mmap + offset)) == CACHE_EXT_INDEXENTRYOFFSETTABLE) {
3644 index = mmap + offset + 4 + 4;
3645 break;
3646 }
3647 offset += 8;
3648 offset += extsize;
3649 }
3650 if (!index)
3651 return NULL;
Ben Peart32550892018-10-10 11:59:37 -04003652
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003653 /* validate the version is IEOT_VERSION */
3654 ext_version = get_be32(index);
3655 if (ext_version != IEOT_VERSION) {
3656 error("invalid IEOT version %d", ext_version);
3657 return NULL;
3658 }
3659 index += sizeof(uint32_t);
Ben Peart32550892018-10-10 11:59:37 -04003660
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003661 /* extension size - version bytes / bytes per entry */
3662 nr = (extsize - sizeof(uint32_t)) / (sizeof(uint32_t) + sizeof(uint32_t));
3663 if (!nr) {
3664 error("invalid number of IEOT entries %d", nr);
3665 return NULL;
3666 }
3667 ieot = xmalloc(sizeof(struct index_entry_offset_table)
3668 + (nr * sizeof(struct index_entry_offset)));
3669 ieot->nr = nr;
3670 for (i = 0; i < nr; i++) {
3671 ieot->entries[i].offset = get_be32(index);
3672 index += sizeof(uint32_t);
3673 ieot->entries[i].nr = get_be32(index);
3674 index += sizeof(uint32_t);
3675 }
Ben Peart32550892018-10-10 11:59:37 -04003676
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003677 return ieot;
Ben Peart32550892018-10-10 11:59:37 -04003678}
3679
3680static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot)
3681{
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003682 uint32_t buffer;
3683 int i;
Ben Peart32550892018-10-10 11:59:37 -04003684
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003685 /* version */
3686 put_be32(&buffer, IEOT_VERSION);
3687 strbuf_add(sb, &buffer, sizeof(uint32_t));
Ben Peart32550892018-10-10 11:59:37 -04003688
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003689 /* ieot */
3690 for (i = 0; i < ieot->nr; i++) {
Ben Peart32550892018-10-10 11:59:37 -04003691
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003692 /* offset */
3693 put_be32(&buffer, ieot->entries[i].offset);
3694 strbuf_add(sb, &buffer, sizeof(uint32_t));
Ben Peart32550892018-10-10 11:59:37 -04003695
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003696 /* count */
3697 put_be32(&buffer, ieot->entries[i].nr);
3698 strbuf_add(sb, &buffer, sizeof(uint32_t));
3699 }
Ben Peart32550892018-10-10 11:59:37 -04003700}
Jonathan Tanb2896d22021-07-23 11:52:22 -07003701
3702void prefetch_cache_entries(const struct index_state *istate,
3703 must_prefetch_predicate must_prefetch)
3704{
3705 int i;
3706 struct oid_array to_fetch = OID_ARRAY_INIT;
3707
3708 for (i = 0; i < istate->cache_nr; i++) {
3709 struct cache_entry *ce = istate->cache[i];
3710
3711 if (S_ISGITLINK(ce->ce_mode) || !must_prefetch(ce))
3712 continue;
3713 if (!oid_object_info_extended(the_repository, &ce->oid,
3714 NULL,
3715 OBJECT_INFO_FOR_PREFETCH))
3716 continue;
3717 oid_array_append(&to_fetch, &ce->oid);
3718 }
3719 promisor_remote_get_direct(the_repository,
3720 to_fetch.oid, to_fetch.nr);
3721 oid_array_clear(&to_fetch);
3722}
Elijah Newren1a40e7b2023-05-16 06:33:45 +00003723
3724static int read_one_entry_opt(struct index_state *istate,
3725 const struct object_id *oid,
3726 struct strbuf *base,
3727 const char *pathname,
3728 unsigned mode, int opt)
3729{
3730 int len;
3731 struct cache_entry *ce;
3732
3733 if (S_ISDIR(mode))
3734 return READ_TREE_RECURSIVE;
3735
3736 len = strlen(pathname);
3737 ce = make_empty_cache_entry(istate, base->len + len);
3738
3739 ce->ce_mode = create_ce_mode(mode);
3740 ce->ce_flags = create_ce_flags(1);
3741 ce->ce_namelen = base->len + len;
3742 memcpy(ce->name, base->buf, base->len);
3743 memcpy(ce->name + base->len, pathname, len+1);
3744 oidcpy(&ce->oid, oid);
3745 return add_index_entry(istate, ce, opt);
3746}
3747
3748static int read_one_entry(const struct object_id *oid, struct strbuf *base,
3749 const char *pathname, unsigned mode,
3750 void *context)
3751{
3752 struct index_state *istate = context;
3753 return read_one_entry_opt(istate, oid, base, pathname,
3754 mode,
3755 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
3756}
3757
3758/*
3759 * This is used when the caller knows there is no existing entries at
3760 * the stage that will conflict with the entry being added.
3761 */
3762static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
3763 const char *pathname, unsigned mode,
3764 void *context)
3765{
3766 struct index_state *istate = context;
3767 return read_one_entry_opt(istate, oid, base, pathname,
3768 mode, ADD_CACHE_JUST_APPEND);
3769}
3770
3771/*
3772 * Read the tree specified with --with-tree option
3773 * (typically, HEAD) into stage #1 and then
3774 * squash them down to stage #0. This is used for
3775 * --error-unmatch to list and check the path patterns
3776 * that were given from the command line. We are not
3777 * going to write this index out.
3778 */
3779void overlay_tree_on_index(struct index_state *istate,
3780 const char *tree_name, const char *prefix)
3781{
3782 struct tree *tree;
3783 struct object_id oid;
3784 struct pathspec pathspec;
3785 struct cache_entry *last_stage0 = NULL;
3786 int i;
3787 read_tree_fn_t fn = NULL;
3788 int err;
3789
3790 if (repo_get_oid(the_repository, tree_name, &oid))
3791 die("tree-ish %s not found.", tree_name);
3792 tree = parse_tree_indirect(&oid);
3793 if (!tree)
3794 die("bad tree-ish %s", tree_name);
3795
3796 /* Hoist the unmerged entries up to stage #3 to make room */
3797 /* TODO: audit for interaction with sparse-index. */
3798 ensure_full_index(istate);
3799 for (i = 0; i < istate->cache_nr; i++) {
3800 struct cache_entry *ce = istate->cache[i];
3801 if (!ce_stage(ce))
3802 continue;
3803 ce->ce_flags |= CE_STAGEMASK;
3804 }
3805
3806 if (prefix) {
3807 static const char *(matchbuf[1]);
3808 matchbuf[0] = NULL;
3809 parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
3810 PATHSPEC_PREFER_CWD, prefix, matchbuf);
3811 } else
3812 memset(&pathspec, 0, sizeof(pathspec));
3813
3814 /*
3815 * See if we have cache entry at the stage. If so,
3816 * do it the original slow way, otherwise, append and then
3817 * sort at the end.
3818 */
3819 for (i = 0; !fn && i < istate->cache_nr; i++) {
3820 const struct cache_entry *ce = istate->cache[i];
3821 if (ce_stage(ce) == 1)
3822 fn = read_one_entry;
3823 }
3824
3825 if (!fn)
3826 fn = read_one_entry_quick;
3827 err = read_tree(the_repository, tree, &pathspec, fn, istate);
3828 clear_pathspec(&pathspec);
3829 if (err)
3830 die("unable to read tree entries %s", tree_name);
3831
3832 /*
3833 * Sort the cache entry -- we need to nuke the cache tree, though.
3834 */
3835 if (fn == read_one_entry_quick) {
3836 cache_tree_free(&istate->cache_tree);
3837 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
3838 }
3839
3840 for (i = 0; i < istate->cache_nr; i++) {
3841 struct cache_entry *ce = istate->cache[i];
3842 switch (ce_stage(ce)) {
3843 case 0:
3844 last_stage0 = ce;
3845 /* fallthru */
3846 default:
3847 continue;
3848 case 1:
3849 /*
3850 * If there is stage #0 entry for this, we do not
3851 * need to show it. We use CE_UPDATE bit to mark
3852 * such an entry.
3853 */
3854 if (last_stage0 &&
3855 !strcmp(last_stage0->name, ce->name))
3856 ce->ce_flags |= CE_UPDATE;
3857 }
3858 }
3859}
Elijah Newren6cee5eb2023-05-16 06:33:47 +00003860
3861struct update_callback_data {
3862 struct index_state *index;
3863 int include_sparse;
3864 int flags;
3865 int add_errors;
3866};
3867
3868static int fix_unmerged_status(struct diff_filepair *p,
3869 struct update_callback_data *data)
3870{
3871 if (p->status != DIFF_STATUS_UNMERGED)
3872 return p->status;
3873 if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
3874 /*
3875 * This is not an explicit add request, and the
3876 * path is missing from the working tree (deleted)
3877 */
3878 return DIFF_STATUS_DELETED;
3879 else
3880 /*
3881 * Either an explicit add request, or path exists
3882 * in the working tree. An attempt to explicitly
3883 * add a path that does not exist in the working tree
3884 * will be caught as an error by the caller immediately.
3885 */
3886 return DIFF_STATUS_MODIFIED;
3887}
3888
3889static void update_callback(struct diff_queue_struct *q,
3890 struct diff_options *opt UNUSED, void *cbdata)
3891{
3892 int i;
3893 struct update_callback_data *data = cbdata;
3894
3895 for (i = 0; i < q->nr; i++) {
3896 struct diff_filepair *p = q->queue[i];
3897 const char *path = p->one->path;
3898
3899 if (!data->include_sparse &&
3900 !path_in_sparse_checkout(path, data->index))
3901 continue;
3902
3903 switch (fix_unmerged_status(p, data)) {
3904 default:
3905 die(_("unexpected diff status %c"), p->status);
3906 case DIFF_STATUS_MODIFIED:
3907 case DIFF_STATUS_TYPE_CHANGED:
3908 if (add_file_to_index(data->index, path, data->flags)) {
3909 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
3910 die(_("updating files failed"));
3911 data->add_errors++;
3912 }
3913 break;
3914 case DIFF_STATUS_DELETED:
3915 if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
3916 break;
3917 if (!(data->flags & ADD_CACHE_PRETEND))
3918 remove_file_from_index(data->index, path);
3919 if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
3920 printf(_("remove '%s'\n"), path);
3921 break;
3922 }
3923 }
3924}
3925
3926int add_files_to_cache(struct repository *repo, const char *prefix,
Junio C Hamano86829f32024-04-03 23:44:48 +05303927 const struct pathspec *pathspec, char *ps_matched,
3928 int include_sparse, int flags)
Elijah Newren6cee5eb2023-05-16 06:33:47 +00003929{
3930 struct update_callback_data data;
3931 struct rev_info rev;
3932
3933 memset(&data, 0, sizeof(data));
3934 data.index = repo->index;
3935 data.include_sparse = include_sparse;
3936 data.flags = flags;
3937
3938 repo_init_revisions(repo, &rev, prefix);
3939 setup_revisions(0, NULL, &rev, NULL);
Junio C Hamano86829f32024-04-03 23:44:48 +05303940 if (pathspec) {
Elijah Newren6cee5eb2023-05-16 06:33:47 +00003941 copy_pathspec(&rev.prune_data, pathspec);
Junio C Hamano86829f32024-04-03 23:44:48 +05303942 rev.ps_matched = ps_matched;
3943 }
Elijah Newren6cee5eb2023-05-16 06:33:47 +00003944 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
3945 rev.diffopt.format_callback = update_callback;
3946 rev.diffopt.format_callback_data = &data;
3947 rev.diffopt.flags.override_submodule_config = 1;
3948 rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
3949
3950 /*
3951 * Use an ODB transaction to optimize adding multiple objects.
3952 * This function is invoked from commands other than 'add', which
3953 * may not have their own transaction active.
3954 */
3955 begin_odb_transaction();
3956 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
3957 end_odb_transaction();
3958
3959 release_revisions(&rev);
3960 return !!data.add_errors;
3961}