Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 6 | #include "git-compat-util.h" |
Elijah Newren | 6cee5eb | 2023-05-16 06:33:47 +0000 | [diff] [blame] | 7 | #include "bulk-checkin.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 8 | #include "config.h" |
Elijah Newren | d4a4f92 | 2023-04-22 20:17:26 +0000 | [diff] [blame] | 9 | #include "date.h" |
Elijah Newren | cffbfad | 2018-06-30 18:24:55 -0700 | [diff] [blame] | 10 | #include "diff.h" |
| 11 | #include "diffcore.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 12 | #include "hex.h" |
Michael Haggerty | f6ecc62 | 2015-08-10 11:47:45 +0200 | [diff] [blame] | 13 | #include "tempfile.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 14 | #include "lockfile.h" |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 15 | #include "cache-tree.h" |
Linus Torvalds | f35a6d3 | 2007-04-09 21:20:29 -0700 | [diff] [blame] | 16 | #include "refs.h" |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 17 | #include "dir.h" |
Elijah Newren | 87bed17 | 2023-04-11 00:41:53 -0700 | [diff] [blame] | 18 | #include "object-file.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 19 | #include "object-store-ll.h" |
Elijah Newren | 6f2d743 | 2023-04-11 03:00:42 +0000 | [diff] [blame] | 20 | #include "oid-array.h" |
Junio C Hamano | 041aee3 | 2008-07-21 01:24:17 -0700 | [diff] [blame] | 21 | #include "tree.h" |
| 22 | #include "commit.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 23 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 24 | #include "gettext.h" |
Elijah Newren | 5bc0722 | 2023-04-11 00:41:47 -0700 | [diff] [blame] | 25 | #include "mem-pool.h" |
Elijah Newren | f565385 | 2023-05-16 06:33:50 +0000 | [diff] [blame] | 26 | #include "name-hash.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 27 | #include "object-name.h" |
Elijah Newren | c339932 | 2023-05-16 06:33:59 +0000 | [diff] [blame] | 28 | #include "path.h" |
Elijah Newren | fbffdfb | 2023-05-16 06:33:52 +0000 | [diff] [blame] | 29 | #include "preload-index.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 30 | #include "read-cache.h" |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 31 | #include "resolve-undo.h" |
Elijah Newren | 6cee5eb | 2023-05-16 06:33:47 +0000 | [diff] [blame] | 32 | #include "revision.h" |
Junio C Hamano | 6c9cd16 | 2012-04-03 15:53:15 -0700 | [diff] [blame] | 33 | #include "strbuf.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 34 | #include "trace2.h" |
Junio C Hamano | 6c9cd16 | 2012-04-03 15:53:15 -0700 | [diff] [blame] | 35 | #include "varint.h" |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 36 | #include "split-index.h" |
Elijah Newren | cb2a513 | 2023-04-22 20:17:09 +0000 | [diff] [blame] | 37 | #include "symlinks.h" |
Jeff King | a42643a | 2014-12-15 18:15:20 -0500 | [diff] [blame] | 38 | #include "utf8.h" |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 39 | #include "fsmonitor.h" |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 40 | #include "thread-utils.h" |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 41 | #include "progress.h" |
Derrick Stolee | 6e77352 | 2021-03-30 13:10:55 +0000 | [diff] [blame] | 42 | #include "sparse-index.h" |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 43 | #include "csum-file.h" |
Jonathan Tan | b2896d2 | 2021-07-23 11:52:22 -0700 | [diff] [blame] | 44 | #include "promisor-remote.h" |
Emily Shaffer | dbb1c61 | 2021-12-22 04:59:41 +0100 | [diff] [blame] | 45 | #include "hook.h" |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 46 | |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 47 | /* Mask for the name length in ce_flags in the on-disk index */ |
| 48 | |
| 49 | #define CE_NAMEMASK (0x0fff) |
| 50 | |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 51 | /* 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. Pearce | b659b49 | 2010-02-02 07:33:28 -0800 | [diff] [blame] | 62 | #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */ |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 63 | #define CACHE_EXT_LINK 0x6c696e6b /* "link" */ |
Nguyễn Thái Ngọc Duy | 83c094a | 2015-03-08 17:12:33 +0700 | [diff] [blame] | 64 | #define CACHE_EXT_UNTRACKED 0x554E5452 /* "UNTR" */ |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 65 | #define CACHE_EXT_FSMONITOR 0x46534D4E /* "FSMN" */ |
Ben Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 66 | #define CACHE_EXT_ENDOFINDEXENTRIES 0x454F4945 /* "EOIE" */ |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 67 | #define CACHE_EXT_INDEXENTRYOFFSETTABLE 0x49454F54 /* "IEOT" */ |
Derrick Stolee | cd42415 | 2021-03-30 13:10:54 +0000 | [diff] [blame] | 68 | #define CACHE_EXT_SPARSE_DIRECTORIES 0x73646972 /* "sdir" */ |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 69 | |
| 70 | /* changes that can be kept in $GIT_DIR/index (basically all extensions) */ |
Nguyễn Thái Ngọc Duy | e0cf0d7 | 2014-06-13 19:19:37 +0700 | [diff] [blame] | 71 | #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \ |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 72 | CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \ |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 73 | SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED | FSMONITOR_CHANGED) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 74 | |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 75 | |
| 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 Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 86 | enum index_search_mode { |
| 87 | NO_EXPAND_SPARSE = 0, |
| 88 | EXPAND_SPARSE = 1 |
| 89 | }; |
| 90 | |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 91 | static 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 | |
| 99 | static 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 | |
| 107 | static 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 Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 116 | if (!*pool_ptr) { |
| 117 | *pool_ptr = xmalloc(sizeof(**pool_ptr)); |
| 118 | mem_pool_init(*pool_ptr, 0); |
| 119 | } |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 120 | |
| 121 | return *pool_ptr; |
| 122 | } |
| 123 | |
Nguyễn Thái Ngọc Duy | 626f35c | 2014-06-13 19:19:24 +0700 | [diff] [blame] | 124 | static const char *alternate_index_output; |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 125 | |
Junio C Hamano | 9cb76b8 | 2008-01-22 23:01:13 -0800 | [diff] [blame] | 126 | static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce) |
| 127 | { |
Derrick Stolee | 4300f84 | 2021-03-30 13:10:48 +0000 | [diff] [blame] | 128 | if (S_ISSPARSEDIR(ce->ce_mode)) |
Derrick Stolee | 9fadb37 | 2022-05-23 13:48:40 +0000 | [diff] [blame] | 129 | istate->sparse_index = INDEX_COLLAPSED; |
Derrick Stolee | 4300f84 | 2021-03-30 13:10:48 +0000 | [diff] [blame] | 130 | |
Junio C Hamano | 9cb76b8 | 2008-01-22 23:01:13 -0800 | [diff] [blame] | 131 | istate->cache[nr] = ce; |
Linus Torvalds | 96872bc | 2008-03-21 13:16:24 -0700 | [diff] [blame] | 132 | add_name_hash(istate, ce); |
Junio C Hamano | 9cb76b8 | 2008-01-22 23:01:13 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Linus Torvalds | cf55870 | 2008-01-22 18:41:14 -0800 | [diff] [blame] | 135 | static 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 Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 139 | replace_index_entry_in_base(istate, old, ce); |
Karsten Blees | 2092678 | 2013-02-28 00:57:48 +0100 | [diff] [blame] | 140 | remove_name_hash(istate, old); |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 141 | discard_cache_entry(old); |
Ben Peart | 0e267b7 | 2018-03-15 11:25:20 -0400 | [diff] [blame] | 142 | ce->ce_flags &= ~CE_HASHED; |
Linus Torvalds | a22c637 | 2008-02-22 20:37:40 -0800 | [diff] [blame] | 143 | set_index_entry(istate, nr, ce); |
Nguyễn Thái Ngọc Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 144 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 145 | mark_fsmonitor_invalid(istate, ce); |
Nguyễn Thái Ngọc Duy | e636a7b | 2014-06-13 19:19:27 +0700 | [diff] [blame] | 146 | istate->cache_changed |= CE_ENTRY_CHANGED; |
Linus Torvalds | cf55870 | 2008-01-22 18:41:14 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Petr Baudis | 81dc230 | 2008-07-21 02:25:56 +0200 | [diff] [blame] | 149 | void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name) |
| 150 | { |
Victoria Dye | b7f9130 | 2022-03-29 01:07:07 +0000 | [diff] [blame] | 151 | struct cache_entry *old_entry = istate->cache[nr], *new_entry, *refreshed; |
Petr Baudis | 81dc230 | 2008-07-21 02:25:56 +0200 | [diff] [blame] | 152 | int namelen = strlen(new_name); |
| 153 | |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 154 | new_entry = make_empty_cache_entry(istate, namelen); |
Brandon Williams | 285c2e2 | 2018-02-14 10:59:45 -0800 | [diff] [blame] | 155 | 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 Baudis | 81dc230 | 2008-07-21 02:25:56 +0200 | [diff] [blame] | 160 | |
Brandon Williams | 285c2e2 | 2018-02-14 10:59:45 -0800 | [diff] [blame] | 161 | cache_tree_invalidate_path(istate, old_entry->name); |
| 162 | untracked_cache_remove_from_index(istate, old_entry->name); |
Petr Baudis | 81dc230 | 2008-07-21 02:25:56 +0200 | [diff] [blame] | 163 | remove_index_entry_at(istate, nr); |
Victoria Dye | b7f9130 | 2022-03-29 01:07:07 +0000 | [diff] [blame] | 164 | |
| 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 Baudis | 81dc230 | 2008-07-21 02:25:56 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Junio C Hamano | 415e96c | 2005-05-15 14:23:12 -0700 | [diff] [blame] | 180 | /* |
| 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 Schindelin | d4c0a3a | 2019-05-24 05:23:47 -0700 | [diff] [blame] | 185 | void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st) |
Junio C Hamano | 415e96c | 2005-05-15 14:23:12 -0700 | [diff] [blame] | 186 | { |
Michael Haggerty | c21d39d | 2013-06-20 10:37:50 +0200 | [diff] [blame] | 187 | fill_stat_data(&ce->ce_stat_data, st); |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 188 | |
| 189 | if (assume_unchanged) |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 190 | ce->ce_flags |= CE_VALID; |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 191 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 192 | if (S_ISREG(st->st_mode)) { |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 193 | ce_mark_uptodate(ce); |
Johannes Schindelin | b5a8169 | 2019-05-24 05:23:48 -0700 | [diff] [blame] | 194 | mark_fsmonitor_valid(istate, ce); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 195 | } |
Junio C Hamano | 415e96c | 2005-05-15 14:23:12 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Junio C Hamano | c33fa87 | 2023-09-14 14:46:46 -0700 | [diff] [blame] | 198 | static 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 | |
| 216 | int 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 Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 225 | static int ce_compare_data(struct index_state *istate, |
| 226 | const struct cache_entry *ce, |
| 227 | struct stat *st) |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 228 | { |
| 229 | int match = -1; |
Junio C Hamano | 1b8ac5e | 2016-10-28 06:23:07 -0700 | [diff] [blame] | 230 | int fd = git_open_cloexec(ce->name, O_RDONLY); |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 231 | |
| 232 | if (fd >= 0) { |
Patryk Obara | bebfecb | 2017-08-20 22:09:27 +0200 | [diff] [blame] | 233 | struct object_id oid; |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 234 | if (!index_fd(istate, &oid, fd, st, OBJ_BLOB, ce->name, 0)) |
Jeff King | 6a29d7b | 2018-08-28 17:22:59 -0400 | [diff] [blame] | 235 | match = !oideq(&oid, &ce->oid); |
Linus Torvalds | 7f8508e | 2006-07-31 09:55:15 -0700 | [diff] [blame] | 236 | /* index_fd() closed the file descriptor already */ |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 237 | } |
| 238 | return match; |
| 239 | } |
| 240 | |
René Scharfe | 21a6b9f | 2013-06-02 17:46:52 +0200 | [diff] [blame] | 241 | static int ce_compare_link(const struct cache_entry *ce, size_t expected_size) |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 242 | { |
| 243 | int match = -1; |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 244 | void *buffer; |
| 245 | unsigned long size; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 246 | enum object_type type; |
Linus Torvalds | a60272b | 2008-12-17 09:47:27 -0800 | [diff] [blame] | 247 | struct strbuf sb = STRBUF_INIT; |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 248 | |
Linus Torvalds | a60272b | 2008-12-17 09:47:27 -0800 | [diff] [blame] | 249 | if (strbuf_readlink(&sb, ce->name, expected_size)) |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 250 | return -1; |
Linus Torvalds | a60272b | 2008-12-17 09:47:27 -0800 | [diff] [blame] | 251 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 252 | buffer = repo_read_object_file(the_repository, &ce->oid, &type, &size); |
Linus Torvalds | a60272b | 2008-12-17 09:47:27 -0800 | [diff] [blame] | 253 | if (buffer) { |
| 254 | if (size == sb.len) |
| 255 | match = memcmp(buffer, sb.buf, size); |
| 256 | free(buffer); |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 257 | } |
Linus Torvalds | a60272b | 2008-12-17 09:47:27 -0800 | [diff] [blame] | 258 | strbuf_release(&sb); |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 259 | return match; |
| 260 | } |
| 261 | |
René Scharfe | 21a6b9f | 2013-06-02 17:46:52 +0200 | [diff] [blame] | 262 | static int ce_compare_gitlink(const struct cache_entry *ce) |
Linus Torvalds | f35a6d3 | 2007-04-09 21:20:29 -0700 | [diff] [blame] | 263 | { |
brian m. carlson | 1053fe8 | 2017-10-15 22:07:06 +0000 | [diff] [blame] | 264 | struct object_id oid; |
Linus Torvalds | f35a6d3 | 2007-04-09 21:20:29 -0700 | [diff] [blame] | 265 | |
| 266 | /* |
| 267 | * We don't actually require that the .git directory |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 268 | * under GITLINK directory be a valid git directory. It |
Linus Torvalds | f35a6d3 | 2007-04-09 21:20:29 -0700 | [diff] [blame] | 269 | * 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. carlson | a98e610 | 2017-10-15 22:07:07 +0000 | [diff] [blame] | 274 | if (resolve_gitlink_ref(ce->name, "HEAD", &oid) < 0) |
Linus Torvalds | f35a6d3 | 2007-04-09 21:20:29 -0700 | [diff] [blame] | 275 | return 0; |
Jeff King | 6a29d7b | 2018-08-28 17:22:59 -0400 | [diff] [blame] | 276 | return !oideq(&oid, &ce->oid); |
Linus Torvalds | f35a6d3 | 2007-04-09 21:20:29 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 279 | static int ce_modified_check_fs(struct index_state *istate, |
| 280 | const struct cache_entry *ce, |
| 281 | struct stat *st) |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 282 | { |
| 283 | switch (st->st_mode & S_IFMT) { |
| 284 | case S_IFREG: |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 285 | if (ce_compare_data(istate, ce, st)) |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 286 | return DATA_CHANGED; |
| 287 | break; |
| 288 | case S_IFLNK: |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 289 | if (ce_compare_link(ce, xsize_t(st->st_size))) |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 290 | return DATA_CHANGED; |
| 291 | break; |
Linus Torvalds | a8ee75b | 2007-04-13 09:24:13 -0700 | [diff] [blame] | 292 | case S_IFDIR: |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 293 | if (S_ISGITLINK(ce->ce_mode)) |
Junio C Hamano | c70115b | 2008-07-29 01:13:44 -0700 | [diff] [blame] | 294 | return ce_compare_gitlink(ce) ? DATA_CHANGED : 0; |
Jeff King | 1cf01a3 | 2017-09-21 02:25:41 -0400 | [diff] [blame] | 295 | /* else fallthrough */ |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 296 | default: |
| 297 | return TYPE_CHANGED; |
| 298 | } |
| 299 | return 0; |
| 300 | } |
| 301 | |
René Scharfe | 21a6b9f | 2013-06-02 17:46:52 +0200 | [diff] [blame] | 302 | static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st) |
Linus Torvalds | 734aab7 | 2005-04-09 09:48:20 -0700 | [diff] [blame] | 303 | { |
| 304 | unsigned int changed = 0; |
| 305 | |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 306 | if (ce->ce_flags & CE_REMOVE) |
| 307 | return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; |
| 308 | |
| 309 | switch (ce->ce_mode & S_IFMT) { |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 310 | case S_IFREG: |
| 311 | changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0; |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 312 | /* We consider only the owner x bit to be relevant for |
| 313 | * "mode changes" |
| 314 | */ |
| 315 | if (trust_executable_bit && |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 316 | (0100 & (ce->ce_mode ^ st->st_mode))) |
Kay Sievers | ffbe1ad | 2005-05-06 15:45:01 +0200 | [diff] [blame] | 317 | changed |= MODE_CHANGED; |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 318 | break; |
| 319 | case S_IFLNK: |
Johannes Sixt | 78a8d64 | 2007-03-02 22:11:30 +0100 | [diff] [blame] | 320 | if (!S_ISLNK(st->st_mode) && |
| 321 | (has_symlinks || !S_ISREG(st->st_mode))) |
| 322 | changed |= TYPE_CHANGED; |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 323 | break; |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 324 | case S_IFGITLINK: |
Junio C Hamano | c70115b | 2008-07-29 01:13:44 -0700 | [diff] [blame] | 325 | /* We ignore most of the st_xxx fields for gitlinks */ |
Linus Torvalds | f35a6d3 | 2007-04-09 21:20:29 -0700 | [diff] [blame] | 326 | if (!S_ISDIR(st->st_mode)) |
| 327 | changed |= TYPE_CHANGED; |
| 328 | else if (ce_compare_gitlink(ce)) |
| 329 | changed |= DATA_CHANGED; |
Linus Torvalds | a8ee75b | 2007-04-13 09:24:13 -0700 | [diff] [blame] | 330 | return changed; |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 331 | default: |
Nguyễn Thái Ngọc Duy | 391408e | 2018-11-10 06:16:04 +0100 | [diff] [blame] | 332 | BUG("unsupported ce_mode: %o", ce->ce_mode); |
Kay Sievers | 8ae0a8c | 2005-05-05 14:38:25 +0200 | [diff] [blame] | 333 | } |
Linus Torvalds | ccc4feb | 2005-04-15 10:44:27 -0700 | [diff] [blame] | 334 | |
Michael Haggerty | c21d39d | 2013-06-20 10:37:50 +0200 | [diff] [blame] | 335 | changed |= match_stat_data(&ce->ce_stat_data, st); |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 336 | |
Linus Torvalds | f49c2c2 | 2008-06-10 10:44:43 -0700 | [diff] [blame] | 337 | /* Racily smudged entry? */ |
Michael Haggerty | c21d39d | 2013-06-20 10:37:50 +0200 | [diff] [blame] | 338 | if (!ce->ce_stat_data.sd_size) { |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 339 | if (!is_empty_blob_sha1(ce->oid.hash)) |
Linus Torvalds | f49c2c2 | 2008-06-10 10:44:43 -0700 | [diff] [blame] | 340 | changed |= DATA_CHANGED; |
| 341 | } |
| 342 | |
Junio C Hamano | 407c8eb | 2005-12-20 12:12:18 -0800 | [diff] [blame] | 343 | return changed; |
| 344 | } |
| 345 | |
Nguyễn Thái Ngọc Duy | 2bb4cda | 2015-03-08 17:12:36 +0700 | [diff] [blame] | 346 | static 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ábor | 5581a01 | 2018-10-11 11:43:09 +0200 | [diff] [blame] | 361 | int is_racy_timestamp(const struct index_state *istate, |
René Scharfe | 21a6b9f | 2013-06-02 17:46:52 +0200 | [diff] [blame] | 362 | const struct cache_entry *ce) |
Junio C Hamano | 6d91da6 | 2008-01-21 00:44:50 -0800 | [diff] [blame] | 363 | { |
Junio C Hamano | 050288d | 2008-05-03 17:24:28 -0700 | [diff] [blame] | 364 | return (!S_ISGITLINK(ce->ce_mode) && |
Nguyễn Thái Ngọc Duy | 2bb4cda | 2015-03-08 17:12:36 +0700 | [diff] [blame] | 365 | is_racy_stat(istate, &ce->ce_stat_data)); |
Junio C Hamano | 6d91da6 | 2008-01-21 00:44:50 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Nguyễn Thái Ngọc Duy | ed4efab | 2015-03-08 17:12:37 +0700 | [diff] [blame] | 368 | int 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 Hamano | 407c8eb | 2005-12-20 12:12:18 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 376 | int ie_match_stat(struct index_state *istate, |
René Scharfe | 21a6b9f | 2013-06-02 17:46:52 +0200 | [diff] [blame] | 377 | const struct cache_entry *ce, struct stat *st, |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 378 | unsigned int options) |
Junio C Hamano | 407c8eb | 2005-12-20 12:12:18 -0800 | [diff] [blame] | 379 | { |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 380 | unsigned int changed; |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 381 | int ignore_valid = options & CE_MATCH_IGNORE_VALID; |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 382 | int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 383 | int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 384 | int ignore_fsmonitor = options & CE_MATCH_IGNORE_FSMONITOR; |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 385 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 386 | if (!ignore_fsmonitor) |
| 387 | refresh_fsmonitor(istate); |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 388 | /* |
| 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 Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 391 | * |
| 392 | * skip-worktree has the same effect with higher precedence |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 393 | */ |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 394 | if (!ignore_skip_worktree && ce_skip_worktree(ce)) |
| 395 | return 0; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 396 | if (!ignore_valid && (ce->ce_flags & CE_VALID)) |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 397 | return 0; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 398 | if (!ignore_fsmonitor && (ce->ce_flags & CE_FSMONITOR_VALID)) |
| 399 | return 0; |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 400 | |
Junio C Hamano | 331fcb5 | 2008-11-28 19:56:34 -0800 | [diff] [blame] | 401 | /* |
| 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 Duy | 895ff3b | 2015-08-22 08:08:05 +0700 | [diff] [blame] | 406 | if (ce_intent_to_add(ce)) |
Junio C Hamano | 331fcb5 | 2008-11-28 19:56:34 -0800 | [diff] [blame] | 407 | return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; |
| 408 | |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 409 | changed = ce_match_stat_basic(ce, st); |
Junio C Hamano | 407c8eb | 2005-12-20 12:12:18 -0800 | [diff] [blame] | 410 | |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 411 | /* |
| 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 Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 425 | * carefully than others. |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 426 | */ |
Junio C Hamano | 6d91da6 | 2008-01-21 00:44:50 -0800 | [diff] [blame] | 427 | if (!changed && is_racy_timestamp(istate, ce)) { |
Junio C Hamano | 42f7740 | 2006-08-15 21:38:07 -0700 | [diff] [blame] | 428 | if (assume_racy_is_modified) |
| 429 | changed |= DATA_CHANGED; |
| 430 | else |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 431 | changed |= ce_modified_check_fs(istate, ce, st); |
Junio C Hamano | 42f7740 | 2006-08-15 21:38:07 -0700 | [diff] [blame] | 432 | } |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 433 | |
Linus Torvalds | 734aab7 | 2005-04-09 09:48:20 -0700 | [diff] [blame] | 434 | return changed; |
| 435 | } |
| 436 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 437 | int ie_modified(struct index_state *istate, |
René Scharfe | 21a6b9f | 2013-06-02 17:46:52 +0200 | [diff] [blame] | 438 | const struct cache_entry *ce, |
| 439 | struct stat *st, unsigned int options) |
Junio C Hamano | b039189 | 2005-09-19 15:11:15 -0700 | [diff] [blame] | 440 | { |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 441 | int changed, changed_fs; |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 442 | |
| 443 | changed = ie_match_stat(istate, ce, st, options); |
Junio C Hamano | b039189 | 2005-09-19 15:11:15 -0700 | [diff] [blame] | 444 | if (!changed) |
| 445 | return 0; |
Junio C Hamano | b039189 | 2005-09-19 15:11:15 -0700 | [diff] [blame] | 446 | /* |
| 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 Hamano | c70115b | 2008-07-29 01:13:44 -0700 | [diff] [blame] | 453 | /* |
| 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 Hamano | b039189 | 2005-09-19 15:11:15 -0700 | [diff] [blame] | 466 | */ |
Junio C Hamano | c70115b | 2008-07-29 01:13:44 -0700 | [diff] [blame] | 467 | if ((changed & DATA_CHANGED) && |
Michael Haggerty | c21d39d | 2013-06-20 10:37:50 +0200 | [diff] [blame] | 468 | (S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0)) |
Junio C Hamano | b039189 | 2005-09-19 15:11:15 -0700 | [diff] [blame] | 469 | return changed; |
| 470 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 471 | changed_fs = ce_modified_check_fs(istate, ce, st); |
Junio C Hamano | 29e4d36 | 2005-12-20 00:02:15 -0800 | [diff] [blame] | 472 | if (changed_fs) |
| 473 | return changed | changed_fs; |
Junio C Hamano | b039189 | 2005-09-19 15:11:15 -0700 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
Elijah Newren | aabc561 | 2023-04-22 20:17:21 +0000 | [diff] [blame] | 477 | static int cache_name_stage_compare(const char *name1, int len1, int stage1, |
| 478 | const char *name2, int len2, int stage2) |
Jeremiah Mahler | ccdd4a0 | 2014-06-19 19:06:44 -0700 | [diff] [blame] | 479 | { |
| 480 | int cmp; |
| 481 | |
| 482 | cmp = name_compare(name1, len1, name2, len2); |
| 483 | if (cmp) |
| 484 | return cmp; |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 485 | |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 486 | if (stage1 < stage2) |
Linus Torvalds | 95fd5bf | 2005-04-15 22:51:44 -0700 | [diff] [blame] | 487 | return -1; |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 488 | if (stage1 > stage2) |
Linus Torvalds | 95fd5bf | 2005-04-15 22:51:44 -0700 | [diff] [blame] | 489 | return 1; |
Linus Torvalds | eb38c22 | 2005-04-09 09:26:55 -0700 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
Elijah Newren | aabc561 | 2023-04-22 20:17:21 +0000 | [diff] [blame] | 493 | int 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 Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 503 | static 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 Torvalds | eb38c22 | 2005-04-09 09:26:55 -0700 | [diff] [blame] | 507 | { |
| 508 | int first, last; |
| 509 | |
| 510 | first = 0; |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 511 | last = istate->cache_nr; |
Linus Torvalds | eb38c22 | 2005-04-09 09:26:55 -0700 | [diff] [blame] | 512 | while (last > first) { |
René Scharfe | 568a05c | 2019-06-13 19:51:56 +0200 | [diff] [blame] | 513 | int next = first + ((last - first) >> 1); |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 514 | struct cache_entry *ce = istate->cache[next]; |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 515 | int cmp = cache_name_stage_compare(name, namelen, stage, ce->name, ce_namelen(ce), ce_stage(ce)); |
Linus Torvalds | eb38c22 | 2005-04-09 09:26:55 -0700 | [diff] [blame] | 516 | if (!cmp) |
Linus Torvalds | 76e7f4e | 2005-04-10 22:06:50 -0700 | [diff] [blame] | 517 | return next; |
Linus Torvalds | eb38c22 | 2005-04-09 09:26:55 -0700 | [diff] [blame] | 518 | if (cmp < 0) { |
| 519 | last = next; |
| 520 | continue; |
| 521 | } |
| 522 | first = next+1; |
| 523 | } |
Derrick Stolee | 95e0321 | 2021-04-01 01:49:40 +0000 | [diff] [blame] | 524 | |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 525 | if (search_mode == EXPAND_SPARSE && istate->sparse_index && |
Derrick Stolee | 95e0321 | 2021-04-01 01:49:40 +0000 | [diff] [blame] | 526 | 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 Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 541 | return index_name_stage_pos(istate, name, namelen, stage, search_mode); |
Derrick Stolee | 95e0321 | 2021-04-01 01:49:40 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Linus Torvalds | 76e7f4e | 2005-04-10 22:06:50 -0700 | [diff] [blame] | 545 | return -first-1; |
Linus Torvalds | eb38c22 | 2005-04-09 09:26:55 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 548 | int index_name_pos(struct index_state *istate, const char *name, int namelen) |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 549 | { |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 550 | return index_name_stage_pos(istate, name, namelen, 0, EXPAND_SPARSE); |
| 551 | } |
| 552 | |
Victoria Dye | 9553aa0 | 2022-08-08 19:07:51 +0000 | [diff] [blame] | 553 | int 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 Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 558 | int 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 Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 561 | } |
| 562 | |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 563 | int remove_index_entry_at(struct index_state *istate, int pos) |
Linus Torvalds | 7b937ca | 2005-04-16 12:05:45 -0700 | [diff] [blame] | 564 | { |
Linus Torvalds | cf55870 | 2008-01-22 18:41:14 -0800 | [diff] [blame] | 565 | struct cache_entry *ce = istate->cache[pos]; |
| 566 | |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 567 | record_resolve_undo(istate, ce); |
Karsten Blees | 2092678 | 2013-02-28 00:57:48 +0100 | [diff] [blame] | 568 | remove_name_hash(istate, ce); |
Nguyễn Thái Ngọc Duy | 045113a | 2014-06-13 19:19:38 +0700 | [diff] [blame] | 569 | save_or_free_index_entry(istate, ce); |
Nguyễn Thái Ngọc Duy | e636a7b | 2014-06-13 19:19:27 +0700 | [diff] [blame] | 570 | istate->cache_changed |= CE_ENTRY_REMOVED; |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 571 | istate->cache_nr--; |
| 572 | if (pos >= istate->cache_nr) |
Linus Torvalds | 7b937ca | 2005-04-16 12:05:45 -0700 | [diff] [blame] | 573 | return 0; |
René Scharfe | f331ab9 | 2017-07-15 22:00:45 +0200 | [diff] [blame] | 574 | MOVE_ARRAY(istate->cache + pos, istate->cache + pos + 1, |
| 575 | istate->cache_nr - pos); |
Linus Torvalds | 7b937ca | 2005-04-16 12:05:45 -0700 | [diff] [blame] | 576 | return 1; |
| 577 | } |
| 578 | |
Kjetil Barvik | 36419c8 | 2009-02-18 23:18:03 +0100 | [diff] [blame] | 579 | /* |
Ondřej Bílka | 98e023d | 2013-07-29 10:18:21 +0200 | [diff] [blame] | 580 | * Remove all cache entries marked for removal, that is where |
Kjetil Barvik | 36419c8 | 2009-02-18 23:18:03 +0100 | [diff] [blame] | 581 | * 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 Gummerer | 6fdc205 | 2018-12-20 13:48:16 +0000 | [diff] [blame] | 584 | void remove_marked_cache_entries(struct index_state *istate, int invalidate) |
Kjetil Barvik | 36419c8 | 2009-02-18 23:18:03 +0100 | [diff] [blame] | 585 | { |
| 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 Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 590 | if (ce_array[i]->ce_flags & CE_REMOVE) { |
Thomas Gummerer | 6fdc205 | 2018-12-20 13:48:16 +0000 | [diff] [blame] | 591 | 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 Blees | 2092678 | 2013-02-28 00:57:48 +0100 | [diff] [blame] | 597 | remove_name_hash(istate, ce_array[i]); |
Nguyễn Thái Ngọc Duy | 045113a | 2014-06-13 19:19:38 +0700 | [diff] [blame] | 598 | save_or_free_index_entry(istate, ce_array[i]); |
Karsten Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 599 | } |
Kjetil Barvik | 36419c8 | 2009-02-18 23:18:03 +0100 | [diff] [blame] | 600 | else |
| 601 | ce_array[j++] = ce_array[i]; |
| 602 | } |
Nguyễn Thái Ngọc Duy | ad837d9 | 2014-06-13 19:19:26 +0700 | [diff] [blame] | 603 | if (j == istate->cache_nr) |
| 604 | return; |
Nguyễn Thái Ngọc Duy | e636a7b | 2014-06-13 19:19:27 +0700 | [diff] [blame] | 605 | istate->cache_changed |= CE_ENTRY_REMOVED; |
Kjetil Barvik | 36419c8 | 2009-02-18 23:18:03 +0100 | [diff] [blame] | 606 | istate->cache_nr = j; |
| 607 | } |
| 608 | |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 609 | int remove_file_from_index(struct index_state *istate, const char *path) |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 610 | { |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 611 | int pos = index_name_pos(istate, path, strlen(path)); |
Junio C Hamano | c4e3cca | 2005-04-17 09:53:35 -0700 | [diff] [blame] | 612 | if (pos < 0) |
| 613 | pos = -pos-1; |
Nguyễn Thái Ngọc Duy | a5400ef | 2014-06-13 19:19:31 +0700 | [diff] [blame] | 614 | cache_tree_invalidate_path(istate, path); |
Nguyễn Thái Ngọc Duy | e931371 | 2015-03-08 17:12:35 +0700 | [diff] [blame] | 615 | untracked_cache_remove_from_index(istate, path); |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 616 | while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) |
| 617 | remove_index_entry_at(istate, pos); |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 618 | return 0; |
| 619 | } |
| 620 | |
Johannes Schindelin | 2031427 | 2007-06-29 18:32:46 +0100 | [diff] [blame] | 621 | static 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 | |
| 626 | static 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 Torvalds | 1102952 | 2008-03-22 14:22:44 -0700 | [diff] [blame] | 649 | static 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 Duy | 045113a | 2014-06-13 19:19:38 +0700 | [diff] [blame] | 664 | static struct cache_entry *create_alias_ce(struct index_state *istate, |
| 665 | struct cache_entry *ce, |
| 666 | struct cache_entry *alias) |
Linus Torvalds | 1102952 | 2008-03-22 14:22:44 -0700 | [diff] [blame] | 667 | { |
| 668 | int len; |
Brandon Williams | 285c2e2 | 2018-02-14 10:59:45 -0800 | [diff] [blame] | 669 | struct cache_entry *new_entry; |
Linus Torvalds | 1102952 | 2008-03-22 14:22:44 -0700 | [diff] [blame] | 670 | |
| 671 | if (alias->ce_flags & CE_ADDED) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 672 | die(_("will not add file alias '%s' ('%s' already exists in index)"), |
| 673 | ce->name, alias->name); |
Linus Torvalds | 1102952 | 2008-03-22 14:22:44 -0700 | [diff] [blame] | 674 | |
| 675 | /* Ok, create the new entry using the name of the existing alias */ |
| 676 | len = ce_namelen(alias); |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 677 | new_entry = make_empty_cache_entry(istate, len); |
Brandon Williams | 285c2e2 | 2018-02-14 10:59:45 -0800 | [diff] [blame] | 678 | memcpy(new_entry->name, alias->name, len); |
| 679 | copy_cache_entry(new_entry, ce); |
Nguyễn Thái Ngọc Duy | 045113a | 2014-06-13 19:19:38 +0700 | [diff] [blame] | 680 | save_or_free_index_entry(istate, ce); |
Brandon Williams | 285c2e2 | 2018-02-14 10:59:45 -0800 | [diff] [blame] | 681 | return new_entry; |
Linus Torvalds | 1102952 | 2008-03-22 14:22:44 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 684 | void set_object_name_for_intent_to_add_entry(struct cache_entry *ce) |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 685 | { |
Patryk Obara | a09c985 | 2018-01-28 01:13:19 +0100 | [diff] [blame] | 686 | struct object_id oid; |
Ævar Arnfjörð Bjarmason | c80d226 | 2022-02-05 00:48:26 +0100 | [diff] [blame] | 687 | if (write_object_file("", 0, OBJ_BLOB, &oid)) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 688 | die(_("cannot create an empty blob in the object database")); |
Patryk Obara | a09c985 | 2018-01-28 01:13:19 +0100 | [diff] [blame] | 689 | oidcpy(&ce->oid, &oid); |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 692 | int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags) |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 693 | { |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 694 | int namelen, was_same; |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 695 | mode_t st_mode = st->st_mode; |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 696 | struct cache_entry *ce, *alias = NULL; |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 697 | unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; |
Junio C Hamano | 38ed1d8 | 2008-05-21 12:04:34 -0700 | [diff] [blame] | 698 | int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); |
| 699 | int pretend = flags & ADD_CACHE_PRETEND; |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 700 | 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é Scharfe | e578d03 | 2021-10-12 21:15:50 +0200 | [diff] [blame] | 703 | unsigned hash_flags = pretend ? 0 : HASH_WRITE_OBJECT; |
Kyle Meyer | f937bc2 | 2019-04-09 19:07:37 -0400 | [diff] [blame] | 704 | struct object_id oid; |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 705 | |
Jeff King | 9e5da3d | 2019-01-17 11:27:11 -0500 | [diff] [blame] | 706 | if (flags & ADD_CACHE_RENORMALIZE) |
| 707 | hash_flags |= HASH_RENORMALIZE; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 708 | |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 709 | if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 710 | return error(_("%s: can only add regular files, symbolic links or git-directories"), path); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 711 | |
| 712 | namelen = strlen(path); |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 713 | if (S_ISDIR(st_mode)) { |
Kyle Meyer | f937bc2 | 2019-04-09 19:07:37 -0400 | [diff] [blame] | 714 | if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) |
| 715 | return error(_("'%s' does not have a commit checked out"), path); |
Linus Torvalds | 0959525 | 2007-04-11 14:49:44 -0700 | [diff] [blame] | 716 | while (namelen && path[namelen-1] == '/') |
| 717 | namelen--; |
| 718 | } |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 719 | ce = make_empty_cache_entry(istate, namelen); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 720 | memcpy(ce->name, path, namelen); |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 721 | ce->ce_namelen = namelen; |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 722 | if (!intent_only) |
Johannes Schindelin | d4c0a3a | 2019-05-24 05:23:47 -0700 | [diff] [blame] | 723 | fill_stat_cache_info(istate, ce, st); |
Junio C Hamano | 388b2ac | 2008-11-28 19:55:25 -0800 | [diff] [blame] | 724 | else |
| 725 | ce->ce_flags |= CE_INTENT_TO_ADD; |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 726 | |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 727 | |
| 728 | if (trust_executable_bit && has_symlinks) { |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 729 | ce->ce_mode = create_ce_mode(st_mode); |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 730 | } else { |
Johannes Sixt | 78a8d64 | 2007-03-02 22:11:30 +0100 | [diff] [blame] | 731 | /* If there is an existing entry, pick the mode bits and type |
| 732 | * from it, otherwise assume unexecutable regular file. |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 733 | */ |
Junio C Hamano | 185c975 | 2007-02-16 22:43:48 -0800 | [diff] [blame] | 734 | struct cache_entry *ent; |
Johannes Schindelin | 2031427 | 2007-06-29 18:32:46 +0100 | [diff] [blame] | 735 | int pos = index_name_pos_also_unmerged(istate, path, namelen); |
Junio C Hamano | 185c975 | 2007-02-16 22:43:48 -0800 | [diff] [blame] | 736 | |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 737 | ent = (0 <= pos) ? istate->cache[pos] : NULL; |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 738 | ce->ce_mode = ce_mode_from_stat(ent, st_mode); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Joshua Jensen | dc1ae70 | 2010-10-03 09:56:45 +0000 | [diff] [blame] | 741 | /* 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 Turner | 41284eb | 2015-10-21 13:54:11 -0400 | [diff] [blame] | 747 | adjust_dirname_case(istate, ce->name); |
Joshua Jensen | dc1ae70 | 2010-10-03 09:56:45 +0000 | [diff] [blame] | 748 | } |
Jeff King | e2c2a37 | 2019-02-06 21:00:22 -0500 | [diff] [blame] | 749 | if (!(flags & ADD_CACHE_RENORMALIZE)) { |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 750 | 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 Jensen | dc1ae70 | 2010-10-03 09:56:45 +0000 | [diff] [blame] | 759 | |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 760 | discard_cache_entry(ce); |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 761 | return 0; |
| 762 | } |
Junio C Hamano | 0781b8a | 2007-07-30 17:12:58 -0700 | [diff] [blame] | 763 | } |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 764 | if (!intent_only) { |
Junio C Hamano | 1c41824 | 2019-02-05 14:26:13 -0800 | [diff] [blame] | 765 | if (index_path(istate, &ce->oid, path, st, hash_flags)) { |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 766 | discard_cache_entry(ce); |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 767 | return error(_("unable to index file '%s'"), path); |
Stefan Beller | 2d9426b | 2015-03-20 17:28:00 -0700 | [diff] [blame] | 768 | } |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 769 | } else |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 770 | set_object_name_for_intent_to_add_entry(ce); |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 771 | |
Linus Torvalds | 1102952 | 2008-03-22 14:22:44 -0700 | [diff] [blame] | 772 | if (ignore_case && alias && different_name(ce, alias)) |
Nguyễn Thái Ngọc Duy | 045113a | 2014-06-13 19:19:38 +0700 | [diff] [blame] | 773 | ce = create_alias_ce(istate, ce, alias); |
Linus Torvalds | 1102952 | 2008-03-22 14:22:44 -0700 | [diff] [blame] | 774 | ce->ce_flags |= CE_ADDED; |
Junio C Hamano | 38ed1d8 | 2008-05-21 12:04:34 -0700 | [diff] [blame] | 775 | |
Junio C Hamano | 3bf0dd1 | 2008-07-16 18:48:58 -0700 | [diff] [blame] | 776 | /* It was suspected to be racily clean, but it turns out to be Ok */ |
Junio C Hamano | 38ed1d8 | 2008-05-21 12:04:34 -0700 | [diff] [blame] | 777 | was_same = (alias && |
| 778 | !ce_stage(alias) && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 779 | oideq(&alias->oid, &ce->oid) && |
Junio C Hamano | 38ed1d8 | 2008-05-21 12:04:34 -0700 | [diff] [blame] | 780 | ce->ce_mode == alias->ce_mode); |
| 781 | |
| 782 | if (pretend) |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 783 | discard_cache_entry(ce); |
Junio C Hamano | 067178e | 2015-03-23 10:58:00 -0700 | [diff] [blame] | 784 | else if (add_index_entry(istate, ce, add_option)) { |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 785 | discard_cache_entry(ce); |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 786 | return error(_("unable to add '%s' to index"), path); |
Junio C Hamano | 067178e | 2015-03-23 10:58:00 -0700 | [diff] [blame] | 787 | } |
Junio C Hamano | 38ed1d8 | 2008-05-21 12:04:34 -0700 | [diff] [blame] | 788 | if (verbose && !was_same) |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 789 | printf("add '%s'\n", path); |
Johannes Schindelin | 11be42a | 2006-07-26 03:52:35 +0200 | [diff] [blame] | 790 | return 0; |
| 791 | } |
| 792 | |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 793 | int add_file_to_index(struct index_state *istate, const char *path, int flags) |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 794 | { |
| 795 | struct stat st; |
| 796 | if (lstat(path, &st)) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 797 | die_errno(_("unable to stat '%s'"), path); |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 798 | return add_to_index(istate, path, &st, flags); |
Linus Torvalds | d177cab | 2008-05-09 09:11:43 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 801 | struct cache_entry *make_empty_cache_entry(struct index_state *istate, size_t len) |
Carlos Rica | 6640f88 | 2007-09-11 05:17:28 +0200 | [diff] [blame] | 802 | { |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 803 | return mem_pool__ce_calloc(find_mem_pool(istate), len); |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Matheus Tavares | 9616882 | 2021-05-04 13:27:28 -0300 | [diff] [blame] | 806 | struct cache_entry *make_empty_transient_cache_entry(size_t len, |
| 807 | struct mem_pool *ce_mem_pool) |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 808 | { |
Matheus Tavares | 9616882 | 2021-05-04 13:27:28 -0300 | [diff] [blame] | 809 | if (ce_mem_pool) |
| 810 | return mem_pool__ce_calloc(ce_mem_pool, len); |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 811 | return xcalloc(1, cache_entry_size(len)); |
| 812 | } |
| 813 | |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 814 | enum verify_path_result { |
| 815 | PATH_OK, |
| 816 | PATH_INVALID, |
| 817 | PATH_DIR_WITH_SEP, |
| 818 | }; |
| 819 | |
| 820 | static enum verify_path_result verify_path_internal(const char *, unsigned); |
| 821 | |
| 822 | int verify_path(const char *path, unsigned mode) |
| 823 | { |
René Scharfe | c8ad9d0 | 2021-10-07 22:31:58 +0200 | [diff] [blame] | 824 | return verify_path_internal(path, mode) == PATH_OK; |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 825 | } |
| 826 | |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 827 | struct cache_entry *make_cache_entry(struct index_state *istate, |
| 828 | unsigned int mode, |
Jameson Miller | 825ed4d | 2018-07-02 19:49:30 +0000 | [diff] [blame] | 829 | const struct object_id *oid, |
| 830 | const char *path, |
| 831 | int stage, |
| 832 | unsigned int refresh_options) |
Carlos Rica | 6640f88 | 2007-09-11 05:17:28 +0200 | [diff] [blame] | 833 | { |
Stefan Beller | bc1c2ca | 2015-02-17 10:06:14 -0800 | [diff] [blame] | 834 | struct cache_entry *ce, *ret; |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 835 | int len; |
Carlos Rica | 6640f88 | 2007-09-11 05:17:28 +0200 | [diff] [blame] | 836 | |
René Scharfe | c8ad9d0 | 2021-10-07 22:31:58 +0200 | [diff] [blame] | 837 | if (verify_path_internal(path, mode) == PATH_INVALID) { |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 838 | error(_("invalid path '%s'"), path); |
Carlos Rica | 6640f88 | 2007-09-11 05:17:28 +0200 | [diff] [blame] | 839 | return NULL; |
Dmitry Potapov | 7e7abea | 2008-10-11 20:39:37 +0400 | [diff] [blame] | 840 | } |
Carlos Rica | 6640f88 | 2007-09-11 05:17:28 +0200 | [diff] [blame] | 841 | |
| 842 | len = strlen(path); |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 843 | ce = make_empty_cache_entry(istate, len); |
Carlos Rica | 6640f88 | 2007-09-11 05:17:28 +0200 | [diff] [blame] | 844 | |
Jameson Miller | 825ed4d | 2018-07-02 19:49:30 +0000 | [diff] [blame] | 845 | oidcpy(&ce->oid, oid); |
Carlos Rica | 6640f88 | 2007-09-11 05:17:28 +0200 | [diff] [blame] | 846 | memcpy(ce->name, path, len); |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 847 | ce->ce_flags = create_ce_flags(stage); |
| 848 | ce->ce_namelen = len; |
Carlos Rica | 6640f88 | 2007-09-11 05:17:28 +0200 | [diff] [blame] | 849 | ce->ce_mode = create_ce_mode(mode); |
| 850 | |
Nguyễn Thái Ngọc Duy | d7b665c | 2018-09-21 17:57:25 +0200 | [diff] [blame] | 851 | ret = refresh_cache_entry(istate, ce, refresh_options); |
Stefan Beller | 915e44c | 2015-03-23 10:57:11 -0700 | [diff] [blame] | 852 | if (ret != ce) |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 853 | discard_cache_entry(ce); |
Stefan Beller | 915e44c | 2015-03-23 10:57:11 -0700 | [diff] [blame] | 854 | return ret; |
Carlos Rica | 6640f88 | 2007-09-11 05:17:28 +0200 | [diff] [blame] | 855 | } |
| 856 | |
Matheus Tavares | 9616882 | 2021-05-04 13:27:28 -0300 | [diff] [blame] | 857 | struct 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 Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 862 | { |
| 863 | struct cache_entry *ce; |
| 864 | int len; |
| 865 | |
| 866 | if (!verify_path(path, mode)) { |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 867 | error(_("invalid path '%s'"), path); |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 868 | return NULL; |
| 869 | } |
| 870 | |
| 871 | len = strlen(path); |
Matheus Tavares | 9616882 | 2021-05-04 13:27:28 -0300 | [diff] [blame] | 872 | ce = make_empty_transient_cache_entry(len, ce_mem_pool); |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 873 | |
| 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 Gummerer | d9d7096 | 2016-09-14 22:07:46 +0100 | [diff] [blame] | 883 | /* |
| 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 | */ |
| 890 | int 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 Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 907 | mark_fsmonitor_invalid(istate, ce); |
Thomas Gummerer | d9d7096 | 2016-09-14 22:07:46 +0100 | [diff] [blame] | 908 | istate->cache_changed |= CE_ENTRY_CHANGED; |
| 909 | |
| 910 | return 0; |
| 911 | } |
| 912 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 913 | int ce_same_name(const struct cache_entry *a, const struct cache_entry *b) |
Linus Torvalds | 7b937ca | 2005-04-16 12:05:45 -0700 | [diff] [blame] | 914 | { |
| 915 | int len = ce_namelen(a); |
| 916 | return ce_namelen(b) == len && !memcmp(a->name, b->name, len); |
| 917 | } |
| 918 | |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 919 | /* |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 920 | * 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 King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 927 | static int verify_dotfile(const char *rest, unsigned mode) |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 928 | { |
| 929 | /* |
| 930 | * The first character was '.', but that |
| 931 | * has already been discarded, we now test |
| 932 | * the rest. |
| 933 | */ |
Theo Niessink | e0f530f | 2011-06-08 14:04:41 +0200 | [diff] [blame] | 934 | |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 935 | /* "." is not allowed */ |
Theo Niessink | e0f530f | 2011-06-08 14:04:41 +0200 | [diff] [blame] | 936 | if (*rest == '\0' || is_dir_sep(*rest)) |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 937 | return 0; |
| 938 | |
Theo Niessink | e0f530f | 2011-06-08 14:04:41 +0200 | [diff] [blame] | 939 | switch (*rest) { |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 940 | /* |
Jeff King | 641084b | 2018-05-15 09:56:50 -0400 | [diff] [blame] | 941 | * ".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 King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 945 | * |
| 946 | * Once we've seen ".git", we can also find ".gitmodules", etc (also |
| 947 | * case-insensitively). |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 948 | */ |
| 949 | case 'g': |
Jeff King | cc2fc7c | 2014-11-24 13:39:12 -0500 | [diff] [blame] | 950 | case 'G': |
| 951 | if (rest[1] != 'i' && rest[1] != 'I') |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 952 | break; |
Jeff King | cc2fc7c | 2014-11-24 13:39:12 -0500 | [diff] [blame] | 953 | if (rest[2] != 't' && rest[2] != 'T') |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 954 | break; |
Jeff King | e19e5e6 | 2018-05-13 13:00:23 -0400 | [diff] [blame] | 955 | if (rest[3] == '\0' || is_dir_sep(rest[3])) |
| 956 | return 0; |
Jeff King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 957 | 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 King | e19e5e6 | 2018-05-13 13:00:23 -0400 | [diff] [blame] | 963 | break; |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 964 | case '.': |
Theo Niessink | e0f530f | 2011-06-08 14:04:41 +0200 | [diff] [blame] | 965 | if (rest[1] == '\0' || is_dir_sep(rest[1])) |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 966 | return 0; |
| 967 | } |
| 968 | return 1; |
| 969 | } |
| 970 | |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 971 | static enum verify_path_result verify_path_internal(const char *path, |
| 972 | unsigned mode) |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 973 | { |
Johannes Schindelin via GitGitGadget | 49e268e | 2020-01-09 13:30:34 +0000 | [diff] [blame] | 974 | char c = 0; |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 975 | |
Erik Faye-Lund | 56948cb | 2011-05-27 18:00:40 +0200 | [diff] [blame] | 976 | if (has_dos_drive_prefix(path)) |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 977 | return PATH_INVALID; |
Erik Faye-Lund | 56948cb | 2011-05-27 18:00:40 +0200 | [diff] [blame] | 978 | |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 979 | if (!is_valid_path(path)) |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 980 | return PATH_INVALID; |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 981 | |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 982 | goto inside; |
| 983 | for (;;) { |
| 984 | if (!c) |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 985 | return PATH_OK; |
Erik Faye-Lund | 56948cb | 2011-05-27 18:00:40 +0200 | [diff] [blame] | 986 | if (is_dir_sep(c)) { |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 987 | inside: |
Jeff King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 988 | if (protect_hfs) { |
Johannes Schindelin via GitGitGadget | 49e268e | 2020-01-09 13:30:34 +0000 | [diff] [blame] | 989 | |
Jeff King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 990 | if (is_hfs_dotgit(path)) |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 991 | return PATH_INVALID; |
Jeff King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 992 | if (S_ISLNK(mode)) { |
| 993 | if (is_hfs_dotgitmodules(path)) |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 994 | return PATH_INVALID; |
Jeff King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | if (protect_ntfs) { |
Adam Dinwoodie | bccc37f | 2021-04-29 21:11:44 +0100 | [diff] [blame] | 998 | #if defined GIT_WINDOWS_NATIVE || defined __CYGWIN__ |
Johannes Schindelin via GitGitGadget | 49e268e | 2020-01-09 13:30:34 +0000 | [diff] [blame] | 999 | if (c == '\\') |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 1000 | return PATH_INVALID; |
Johannes Schindelin via GitGitGadget | 49e268e | 2020-01-09 13:30:34 +0000 | [diff] [blame] | 1001 | #endif |
Jeff King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 1002 | if (is_ntfs_dotgit(path)) |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 1003 | return PATH_INVALID; |
Jeff King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 1004 | if (S_ISLNK(mode)) { |
| 1005 | if (is_ntfs_dotgitmodules(path)) |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 1006 | return PATH_INVALID; |
Jeff King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 1010 | c = *path++; |
Jeff King | 10ecfa7 | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 1011 | if ((c == '.' && !verify_dotfile(path, mode)) || |
Derrick Stolee | 6e77352 | 2021-03-30 13:10:55 +0000 | [diff] [blame] | 1012 | is_dir_sep(c)) |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 1013 | return PATH_INVALID; |
Derrick Stolee | 6e77352 | 2021-03-30 13:10:55 +0000 | [diff] [blame] | 1014 | /* |
| 1015 | * allow terminating directory separators for |
| 1016 | * sparse directory entries. |
| 1017 | */ |
| 1018 | if (c == '\0') |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 1019 | return S_ISDIR(mode) ? PATH_DIR_WITH_SEP : |
| 1020 | PATH_INVALID; |
Johannes Schindelin | 288a74b | 2019-09-23 08:58:11 +0200 | [diff] [blame] | 1021 | } else if (c == '\\' && protect_ntfs) { |
| 1022 | if (is_ntfs_dotgit(path)) |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 1023 | return PATH_INVALID; |
Johannes Schindelin | 288a74b | 2019-09-23 08:58:11 +0200 | [diff] [blame] | 1024 | if (S_ISLNK(mode)) { |
| 1025 | if (is_ntfs_dotgitmodules(path)) |
René Scharfe | 2a1ae64 | 2021-10-07 22:31:44 +0200 | [diff] [blame] | 1026 | return PATH_INVALID; |
Johannes Schindelin | 288a74b | 2019-09-23 08:58:11 +0200 | [diff] [blame] | 1027 | } |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 1028 | } |
Johannes Schindelin | 288a74b | 2019-09-23 08:58:11 +0200 | [diff] [blame] | 1029 | |
Linus Torvalds | 8dcf39c | 2006-05-18 12:07:31 -0700 | [diff] [blame] | 1030 | c = *path++; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /* |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1035 | * 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 Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1038 | static int has_file_name(struct index_state *istate, |
| 1039 | const struct cache_entry *ce, int pos, int ok_to_replace) |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1040 | { |
| 1041 | int retval = 0; |
| 1042 | int len = ce_namelen(ce); |
Junio C Hamano | b155725 | 2005-06-25 02:25:29 -0700 | [diff] [blame] | 1043 | int stage = ce_stage(ce); |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1044 | const char *name = ce->name; |
| 1045 | |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1046 | while (pos < istate->cache_nr) { |
| 1047 | struct cache_entry *p = istate->cache[pos++]; |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1048 | |
| 1049 | if (len >= ce_namelen(p)) |
| 1050 | break; |
| 1051 | if (memcmp(name, p->name, len)) |
| 1052 | break; |
Junio C Hamano | b155725 | 2005-06-25 02:25:29 -0700 | [diff] [blame] | 1053 | if (ce_stage(p) != stage) |
| 1054 | continue; |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1055 | if (p->name[len] != '/') |
| 1056 | continue; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 1057 | if (p->ce_flags & CE_REMOVE) |
Junio C Hamano | 21cd8d0 | 2007-03-30 01:55:37 -0700 | [diff] [blame] | 1058 | continue; |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1059 | retval = -1; |
| 1060 | if (!ok_to_replace) |
| 1061 | break; |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1062 | remove_index_entry_at(istate, --pos); |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1063 | } |
| 1064 | return retval; |
| 1065 | } |
| 1066 | |
Jeff Hostetler | a6db3fb | 2017-04-14 19:12:28 +0000 | [diff] [blame] | 1067 | |
| 1068 | /* |
| 1069 | * Like strcmp(), but also return the offset of the first change. |
| 1070 | * If strings are equal, return the length. |
| 1071 | */ |
| 1072 | int 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 Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1087 | /* |
| 1088 | * Do we have another file with a pathname that is a proper |
| 1089 | * subset of the name we're trying to add? |
Jeff Hostetler | 06b6d81 | 2017-04-19 17:06:17 +0000 | [diff] [blame] | 1090 | * |
| 1091 | * That is, is there another file in the index with a path |
| 1092 | * that matches a sub-directory in the given entry? |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1093 | */ |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1094 | static int has_dir_name(struct index_state *istate, |
| 1095 | const struct cache_entry *ce, int pos, int ok_to_replace) |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1096 | { |
| 1097 | int retval = 0; |
Junio C Hamano | b155725 | 2005-06-25 02:25:29 -0700 | [diff] [blame] | 1098 | int stage = ce_stage(ce); |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1099 | const char *name = ce->name; |
| 1100 | const char *slash = name + ce_namelen(ce); |
Jeff Hostetler | 06b6d81 | 2017-04-19 17:06:17 +0000 | [diff] [blame] | 1101 | 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 Hejsek | c30a574 | 2024-01-28 04:30:25 +0100 | [diff] [blame] | 1119 | if (name[len_eq_last] != '/') { |
Jeff Hostetler | 06b6d81 | 2017-04-19 17:06:17 +0000 | [diff] [blame] | 1120 | /* |
| 1121 | * The entry sorts AFTER the last one in the |
Filip Hejsek | c30a574 | 2024-01-28 04:30:25 +0100 | [diff] [blame] | 1122 | * 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 Hostetler | 06b6d81 | 2017-04-19 17:06:17 +0000 | [diff] [blame] | 1130 | */ |
| 1131 | return retval; |
| 1132 | } else { |
| 1133 | /* |
| 1134 | * The entry sorts AFTER the last one in the |
Filip Hejsek | c30a574 | 2024-01-28 04:30:25 +0100 | [diff] [blame] | 1135 | * 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 Hostetler | 06b6d81 | 2017-04-19 17:06:17 +0000 | [diff] [blame] | 1145 | */ |
| 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 Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1156 | |
| 1157 | for (;;) { |
Jeff Hostetler | b986df5 | 2017-04-19 17:06:18 +0000 | [diff] [blame] | 1158 | size_t len; |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1159 | |
| 1160 | for (;;) { |
| 1161 | if (*--slash == '/') |
| 1162 | break; |
| 1163 | if (slash <= ce->name) |
| 1164 | return retval; |
| 1165 | } |
| 1166 | len = slash - name; |
| 1167 | |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 1168 | pos = index_name_stage_pos(istate, name, len, stage, EXPAND_SPARSE); |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1169 | if (pos >= 0) { |
Junio C Hamano | 21cd8d0 | 2007-03-30 01:55:37 -0700 | [diff] [blame] | 1170 | /* |
| 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 Hamano | 077c48d | 2008-01-22 21:24:21 -0800 | [diff] [blame] | 1178 | if (!(istate->cache[pos]->ce_flags & CE_REMOVE)) { |
Junio C Hamano | 21cd8d0 | 2007-03-30 01:55:37 -0700 | [diff] [blame] | 1179 | retval = -1; |
| 1180 | if (!ok_to_replace) |
| 1181 | break; |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1182 | remove_index_entry_at(istate, pos); |
Junio C Hamano | 21cd8d0 | 2007-03-30 01:55:37 -0700 | [diff] [blame] | 1183 | continue; |
| 1184 | } |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1185 | } |
Junio C Hamano | 21cd8d0 | 2007-03-30 01:55:37 -0700 | [diff] [blame] | 1186 | else |
| 1187 | pos = -pos-1; |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1188 | |
| 1189 | /* |
| 1190 | * Trivial optimization: if we find an entry that |
| 1191 | * already matches the sub-directory, then we know |
Junio C Hamano | b155725 | 2005-06-25 02:25:29 -0700 | [diff] [blame] | 1192 | * we're ok, and we can exit. |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1193 | */ |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1194 | while (pos < istate->cache_nr) { |
| 1195 | struct cache_entry *p = istate->cache[pos]; |
Junio C Hamano | b155725 | 2005-06-25 02:25:29 -0700 | [diff] [blame] | 1196 | if ((ce_namelen(p) <= len) || |
| 1197 | (p->name[len] != '/') || |
| 1198 | memcmp(p->name, name, len)) |
| 1199 | break; /* not our subdirectory */ |
Junio C Hamano | 077c48d | 2008-01-22 21:24:21 -0800 | [diff] [blame] | 1200 | if (ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) |
| 1201 | /* |
| 1202 | * p is at the same stage as our entry, and |
Junio C Hamano | b155725 | 2005-06-25 02:25:29 -0700 | [diff] [blame] | 1203 | * 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 Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | return retval; |
| 1212 | } |
| 1213 | |
Junio C Hamano | 0f1e4f0 | 2005-05-07 21:48:12 -0700 | [diff] [blame] | 1214 | /* 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 Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 1218 | * |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 1219 | * 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 Hamano | 0f1e4f0 | 2005-05-07 21:48:12 -0700 | [diff] [blame] | 1222 | */ |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1223 | static int check_file_directory_conflict(struct index_state *istate, |
| 1224 | const struct cache_entry *ce, |
| 1225 | int pos, int ok_to_replace) |
Junio C Hamano | 0f1e4f0 | 2005-05-07 21:48:12 -0700 | [diff] [blame] | 1226 | { |
Junio C Hamano | 21cd8d0 | 2007-03-30 01:55:37 -0700 | [diff] [blame] | 1227 | int retval; |
| 1228 | |
| 1229 | /* |
| 1230 | * When ce is an "I am going away" entry, we allow it to be added |
| 1231 | */ |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 1232 | if (ce->ce_flags & CE_REMOVE) |
Junio C Hamano | 21cd8d0 | 2007-03-30 01:55:37 -0700 | [diff] [blame] | 1233 | return 0; |
| 1234 | |
Junio C Hamano | 0f1e4f0 | 2005-05-07 21:48:12 -0700 | [diff] [blame] | 1235 | /* |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1236 | * 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 Hamano | 21cd8d0 | 2007-03-30 01:55:37 -0700 | [diff] [blame] | 1238 | * in the array. |
Junio C Hamano | 0f1e4f0 | 2005-05-07 21:48:12 -0700 | [diff] [blame] | 1239 | */ |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1240 | retval = has_file_name(istate, ce, pos, ok_to_replace); |
Junio C Hamano | 21cd8d0 | 2007-03-30 01:55:37 -0700 | [diff] [blame] | 1241 | |
Linus Torvalds | 1267660 | 2005-06-18 20:21:34 -0700 | [diff] [blame] | 1242 | /* |
| 1243 | * Then check if the path might have a clashing sub-directory |
| 1244 | * before it. |
Junio C Hamano | 0f1e4f0 | 2005-05-07 21:48:12 -0700 | [diff] [blame] | 1245 | */ |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1246 | return retval + has_dir_name(istate, ce, pos, ok_to_replace); |
Junio C Hamano | 0f1e4f0 | 2005-05-07 21:48:12 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 1249 | static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option) |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 1250 | { |
| 1251 | int pos; |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 1252 | int ok_to_add = option & ADD_CACHE_OK_TO_ADD; |
| 1253 | int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; |
Junio C Hamano | b155725 | 2005-06-25 02:25:29 -0700 | [diff] [blame] | 1254 | int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 1255 | int new_only = option & ADD_CACHE_NEW_ONLY; |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 1256 | |
Jeff Hostetler | e549463 | 2017-04-19 17:06:16 +0000 | [diff] [blame] | 1257 | /* |
| 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 Schindelin | c097b95 | 2019-10-04 08:09:26 -0700 | [diff] [blame] | 1263 | pos = index_pos_to_insert_pos(istate->cache_nr); |
Jeff Hostetler | e549463 | 2017-04-19 17:06:16 +0000 | [diff] [blame] | 1264 | else |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 1265 | pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE); |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 1266 | |
Victoria Dye | c35e9f5 | 2022-01-11 18:05:05 +0000 | [diff] [blame] | 1267 | /* |
| 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 Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 1274 | /* existing match? Just replace it. */ |
Linus Torvalds | 76e7f4e | 2005-04-10 22:06:50 -0700 | [diff] [blame] | 1275 | if (pos >= 0) { |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 1276 | if (!new_only) |
| 1277 | replace_index_entry(istate, pos, ce); |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 1278 | return 0; |
| 1279 | } |
Linus Torvalds | 76e7f4e | 2005-04-10 22:06:50 -0700 | [diff] [blame] | 1280 | pos = -pos-1; |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 1281 | |
Nguyễn Thái Ngọc Duy | ffcc9ba | 2015-06-07 17:40:52 +0700 | [diff] [blame] | 1282 | if (!(option & ADD_CACHE_KEEP_CACHE_TREE)) |
| 1283 | untracked_cache_add_to_index(istate, ce->name); |
Nguyễn Thái Ngọc Duy | e931371 | 2015-03-08 17:12:35 +0700 | [diff] [blame] | 1284 | |
Linus Torvalds | 7b937ca | 2005-04-16 12:05:45 -0700 | [diff] [blame] | 1285 | /* |
| 1286 | * Inserting a merged entry ("stage 0") into the index |
| 1287 | * will always replace all non-merged entries.. |
| 1288 | */ |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1289 | if (pos < istate->cache_nr && ce_stage(ce) == 0) { |
| 1290 | while (ce_same_name(istate->cache[pos], ce)) { |
Linus Torvalds | 7b937ca | 2005-04-16 12:05:45 -0700 | [diff] [blame] | 1291 | ok_to_add = 1; |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1292 | if (!remove_index_entry_at(istate, pos)) |
Linus Torvalds | 7b937ca | 2005-04-16 12:05:45 -0700 | [diff] [blame] | 1293 | break; |
| 1294 | } |
| 1295 | } |
| 1296 | |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 1297 | if (!ok_to_add) |
| 1298 | return -1; |
René Scharfe | c8ad9d0 | 2021-10-07 22:31:58 +0200 | [diff] [blame] | 1299 | if (verify_path_internal(ce->name, ce->ce_mode) == PATH_INVALID) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1300 | return error(_("invalid path '%s'"), ce->name); |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 1301 | |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 1302 | if (!skip_df_check && |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1303 | check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 1304 | if (!ok_to_replace) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1305 | return error(_("'%s' appears as both a file and as a directory"), |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1306 | ce->name); |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 1307 | pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce), EXPAND_SPARSE); |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 1308 | pos = -pos-1; |
| 1309 | } |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 1310 | return pos + 1; |
| 1311 | } |
| 1312 | |
| 1313 | int 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 Hamano | 0f1e4f0 | 2005-05-07 21:48:12 -0700 | [diff] [blame] | 1326 | |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 1327 | /* Make sure the array is big enough .. */ |
Dmitry S. Dolzhenko | 999f566 | 2014-03-04 02:32:01 +0400 | [diff] [blame] | 1328 | ALLOC_GROW(istate->cache, istate->cache_nr + 1, istate->cache_alloc); |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 1329 | |
| 1330 | /* Add it in.. */ |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1331 | istate->cache_nr++; |
Junio C Hamano | af3785d | 2007-08-09 13:42:50 -0700 | [diff] [blame] | 1332 | if (istate->cache_nr > pos + 1) |
SZEDER Gábor | f919ffe | 2018-01-22 18:50:09 +0100 | [diff] [blame] | 1333 | MOVE_ARRAY(istate->cache + pos + 1, istate->cache + pos, |
| 1334 | istate->cache_nr - pos - 1); |
Linus Torvalds | cf55870 | 2008-01-22 18:41:14 -0800 | [diff] [blame] | 1335 | set_index_entry(istate, pos, ce); |
Nguyễn Thái Ngọc Duy | e636a7b | 2014-06-13 19:19:27 +0700 | [diff] [blame] | 1336 | istate->cache_changed |= CE_ENTRY_ADDED; |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 1337 | return 0; |
| 1338 | } |
| 1339 | |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1340 | /* |
| 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 Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1351 | static struct cache_entry *refresh_cache_ent(struct index_state *istate, |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 1352 | struct cache_entry *ce, |
Jeff King | d05e697 | 2011-11-18 06:11:08 -0500 | [diff] [blame] | 1353 | unsigned int options, int *err, |
Jeff Hostetler | a98e0f2 | 2021-02-03 15:34:45 +0000 | [diff] [blame] | 1354 | int *changed_ret, |
Jeff Hostetler | 15268d1 | 2021-02-03 15:34:46 +0000 | [diff] [blame] | 1355 | int *t2_did_lstat, |
| 1356 | int *t2_did_scan) |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1357 | { |
| 1358 | struct stat st; |
| 1359 | struct cache_entry *updated; |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 1360 | int changed; |
Brad King | 2576272 | 2014-01-27 09:45:08 -0500 | [diff] [blame] | 1361 | int refresh = options & CE_MATCH_REFRESH; |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 1362 | int ignore_valid = options & CE_MATCH_IGNORE_VALID; |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 1363 | int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; |
Brad King | 2e2e7ec | 2014-01-27 09:45:07 -0500 | [diff] [blame] | 1364 | int ignore_missing = options & CE_MATCH_IGNORE_MISSING; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 1365 | int ignore_fsmonitor = options & CE_MATCH_IGNORE_FSMONITOR; |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1366 | |
Brad King | 2576272 | 2014-01-27 09:45:08 -0500 | [diff] [blame] | 1367 | if (!refresh || ce_uptodate(ce)) |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 1368 | return ce; |
| 1369 | |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 1370 | if (!ignore_fsmonitor) |
| 1371 | refresh_fsmonitor(istate); |
Marius Storm-Olsen | aa9349d | 2008-05-30 14:38:35 +0200 | [diff] [blame] | 1372 | /* |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 1373 | * 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-Olsen | aa9349d | 2008-05-30 14:38:35 +0200 | [diff] [blame] | 1376 | */ |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 1377 | if (!ignore_skip_worktree && ce_skip_worktree(ce)) { |
| 1378 | ce_mark_uptodate(ce); |
| 1379 | return ce; |
| 1380 | } |
Marius Storm-Olsen | aa9349d | 2008-05-30 14:38:35 +0200 | [diff] [blame] | 1381 | if (!ignore_valid && (ce->ce_flags & CE_VALID)) { |
| 1382 | ce_mark_uptodate(ce); |
| 1383 | return ce; |
| 1384 | } |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 1385 | if (!ignore_fsmonitor && (ce->ce_flags & CE_FSMONITOR_VALID)) { |
| 1386 | ce_mark_uptodate(ce); |
| 1387 | return ce; |
| 1388 | } |
Marius Storm-Olsen | aa9349d | 2008-05-30 14:38:35 +0200 | [diff] [blame] | 1389 | |
René Scharfe | ccad42d | 2014-08-09 19:43:29 +0200 | [diff] [blame] | 1390 | 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 Hostetler | a98e0f2 | 2021-02-03 15:34:45 +0000 | [diff] [blame] | 1398 | if (t2_did_lstat) |
| 1399 | *t2_did_lstat = 1; |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 1400 | if (lstat(ce->name, &st) < 0) { |
Brad King | 2e2e7ec | 2014-01-27 09:45:07 -0500 | [diff] [blame] | 1401 | if (ignore_missing && errno == ENOENT) |
| 1402 | return ce; |
Junio C Hamano | ec0cc70 | 2007-04-01 21:34:34 -0700 | [diff] [blame] | 1403 | if (err) |
| 1404 | *err = errno; |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 1405 | return NULL; |
| 1406 | } |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1407 | |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 1408 | changed = ie_match_stat(istate, ce, &st, options); |
Jeff King | d05e697 | 2011-11-18 06:11:08 -0500 | [diff] [blame] | 1409 | if (changed_ret) |
| 1410 | *changed_ret = changed; |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1411 | if (!changed) { |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 1412 | /* |
| 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 Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 1420 | !(ce->ce_flags & CE_VALID)) |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1421 | ; /* mark this one VALID again */ |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 1422 | 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 Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 1428 | if (!S_ISGITLINK(ce->ce_mode)) { |
Junio C Hamano | 125fd98 | 2010-01-24 00:10:20 -0800 | [diff] [blame] | 1429 | ce_mark_uptodate(ce); |
Johannes Schindelin | b5a8169 | 2019-05-24 05:23:48 -0700 | [diff] [blame] | 1430 | mark_fsmonitor_valid(istate, ce); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 1431 | } |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 1432 | return ce; |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 1433 | } |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
Jeff Hostetler | 15268d1 | 2021-02-03 15:34:46 +0000 | [diff] [blame] | 1436 | if (t2_did_scan) |
| 1437 | *t2_did_scan = 1; |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 1438 | if (ie_modified(istate, ce, &st, options)) { |
Junio C Hamano | ec0cc70 | 2007-04-01 21:34:34 -0700 | [diff] [blame] | 1439 | if (err) |
| 1440 | *err = EINVAL; |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 1441 | return NULL; |
| 1442 | } |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1443 | |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 1444 | updated = make_empty_cache_entry(istate, ce_namelen(ce)); |
Ben Peart | 0e267b7 | 2018-03-15 11:25:20 -0400 | [diff] [blame] | 1445 | copy_cache_entry(updated, ce); |
| 1446 | memcpy(updated->name, ce->name, ce->ce_namelen + 1); |
Johannes Schindelin | d4c0a3a | 2019-05-24 05:23:47 -0700 | [diff] [blame] | 1447 | fill_stat_cache_info(istate, updated, &st); |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 1448 | /* |
| 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 Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1453 | */ |
Junio C Hamano | 4bd5b7d | 2007-11-10 00:15:03 -0800 | [diff] [blame] | 1454 | if (!ignore_valid && assume_unchanged && |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 1455 | !(ce->ce_flags & CE_VALID)) |
| 1456 | updated->ce_flags &= ~CE_VALID; |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1457 | |
Nguyễn Thái Ngọc Duy | e636a7b | 2014-06-13 19:19:27 +0700 | [diff] [blame] | 1458 | /* istate->cache_changed is updated in the caller */ |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1459 | return updated; |
| 1460 | } |
| 1461 | |
Matthieu Moy | 3deffc5 | 2009-08-21 10:57:59 +0200 | [diff] [blame] | 1462 | static void show_file(const char * fmt, const char * name, int in_porcelain, |
Jonathan Nieder | 046613c | 2011-02-22 22:43:23 +0000 | [diff] [blame] | 1463 | int * first, const char *header_msg) |
Matthieu Moy | 3deffc5 | 2009-08-21 10:57:59 +0200 | [diff] [blame] | 1464 | { |
| 1465 | if (in_porcelain && *first && header_msg) { |
| 1466 | printf("%s\n", header_msg); |
Junio C Hamano | cd2b8ae | 2011-08-25 14:46:52 -0700 | [diff] [blame] | 1467 | *first = 0; |
Matthieu Moy | 3deffc5 | 2009-08-21 10:57:59 +0200 | [diff] [blame] | 1468 | } |
| 1469 | printf(fmt, name); |
| 1470 | } |
| 1471 | |
Thomas Gummerer | 2218449 | 2019-09-11 19:20:25 +0100 | [diff] [blame] | 1472 | int 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 Duy | 9b2d614 | 2013-07-14 15:35:54 +0700 | [diff] [blame] | 1493 | int refresh_index(struct index_state *istate, unsigned int flags, |
| 1494 | const struct pathspec *pathspec, |
Jonathan Nieder | 046613c | 2011-02-22 22:43:23 +0000 | [diff] [blame] | 1495 | char *seen, const char *header_msg) |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1496 | { |
| 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 Schindelin | 5fdeacb | 2008-05-14 18:03:45 +0100 | [diff] [blame] | 1503 | int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0; |
Matheus Tavares | b243012 | 2021-04-08 17:41:26 -0300 | [diff] [blame] | 1504 | int ignore_skip_worktree = (flags & REFRESH_IGNORE_SKIP_WORKTREE) != 0; |
Matthieu Moy | 3deffc5 | 2009-08-21 10:57:59 +0200 | [diff] [blame] | 1505 | int first = 1; |
| 1506 | int in_porcelain = (flags & REFRESH_IN_PORCELAIN); |
Brad King | 2576272 | 2014-01-27 09:45:08 -0500 | [diff] [blame] | 1507 | unsigned int options = (CE_MATCH_REFRESH | |
| 1508 | (really ? CE_MATCH_IGNORE_VALID : 0) | |
Brad King | 2e2e7ec | 2014-01-27 09:45:07 -0500 | [diff] [blame] | 1509 | (not_new ? CE_MATCH_IGNORE_MISSING : 0)); |
Jeff King | 4bd4e73 | 2011-11-18 06:11:28 -0500 | [diff] [blame] | 1510 | const char *modified_fmt; |
Jeff King | 73b7eae | 2011-11-18 06:13:08 -0500 | [diff] [blame] | 1511 | const char *deleted_fmt; |
| 1512 | const char *typechange_fmt; |
| 1513 | const char *added_fmt; |
Jeff King | 4bd4e73 | 2011-11-18 06:11:28 -0500 | [diff] [blame] | 1514 | const char *unmerged_fmt; |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 1515 | struct progress *progress = NULL; |
Jeff Hostetler | a98e0f2 | 2021-02-03 15:34:45 +0000 | [diff] [blame] | 1516 | int t2_sum_lstat = 0; |
Jeff Hostetler | 15268d1 | 2021-02-03 15:34:46 +0000 | [diff] [blame] | 1517 | int t2_sum_scan = 0; |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 1518 | |
| 1519 | if (flags & REFRESH_PROGRESS && isatty(2)) |
| 1520 | progress = start_delayed_progress(_("Refresh index"), |
| 1521 | istate->cache_nr); |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1522 | |
Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 16:41:22 +0200 | [diff] [blame] | 1523 | trace_performance_enter(); |
Nguyễn Thái Ngọc Duy | a71806a | 2018-11-10 06:16:06 +0100 | [diff] [blame] | 1524 | 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 Peart | 99ce720 | 2018-10-29 16:41:59 -0400 | [diff] [blame] | 1529 | /* |
| 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 Hostetler | a98e0f2 | 2021-02-03 15:34:45 +0000 | [diff] [blame] | 1535 | trace2_region_enter("index", "refresh", NULL); |
Derrick Stolee | d76723e | 2021-07-14 13:12:37 +0000 | [diff] [blame] | 1536 | |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1537 | for (i = 0; i < istate->cache_nr; i++) { |
Brandon Williams | 285c2e2 | 2018-02-14 10:59:45 -0800 | [diff] [blame] | 1538 | struct cache_entry *ce, *new_entry; |
Junio C Hamano | ec0cc70 | 2007-04-01 21:34:34 -0700 | [diff] [blame] | 1539 | int cache_errno = 0; |
Jeff King | 73b7eae | 2011-11-18 06:13:08 -0500 | [diff] [blame] | 1540 | int changed = 0; |
Junio C Hamano | 3d1f148 | 2012-02-17 10:11:05 -0800 | [diff] [blame] | 1541 | int filtered = 0; |
Jeff Hostetler | a98e0f2 | 2021-02-03 15:34:45 +0000 | [diff] [blame] | 1542 | int t2_did_lstat = 0; |
Jeff Hostetler | 15268d1 | 2021-02-03 15:34:46 +0000 | [diff] [blame] | 1543 | int t2_did_scan = 0; |
Junio C Hamano | ec0cc70 | 2007-04-01 21:34:34 -0700 | [diff] [blame] | 1544 | |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1545 | ce = istate->cache[i]; |
Johannes Schindelin | 5fdeacb | 2008-05-14 18:03:45 +0100 | [diff] [blame] | 1546 | if (ignore_submodules && S_ISGITLINK(ce->ce_mode)) |
| 1547 | continue; |
Matheus Tavares | b243012 | 2021-04-08 17:41:26 -0300 | [diff] [blame] | 1548 | if (ignore_skip_worktree && ce_skip_worktree(ce)) |
| 1549 | continue; |
Johannes Schindelin | 5fdeacb | 2008-05-14 18:03:45 +0100 | [diff] [blame] | 1550 | |
Derrick Stolee | d76723e | 2021-07-14 13:12:37 +0000 | [diff] [blame] | 1551 | /* |
| 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 Duy | d7b665c | 2018-09-21 17:57:25 +0200 | [diff] [blame] | 1558 | if (pathspec && !ce_path_match(istate, ce, pathspec, seen)) |
Junio C Hamano | 3d1f148 | 2012-02-17 10:11:05 -0800 | [diff] [blame] | 1559 | filtered = 1; |
| 1560 | |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1561 | if (ce_stage(ce)) { |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1562 | while ((i < istate->cache_nr) && |
| 1563 | ! strcmp(istate->cache[i]->name, ce->name)) |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1564 | i++; |
| 1565 | i--; |
| 1566 | if (allow_unmerged) |
| 1567 | continue; |
Junio C Hamano | 3d1f148 | 2012-02-17 10:11:05 -0800 | [diff] [blame] | 1568 | if (!filtered) |
| 1569 | show_file(unmerged_fmt, ce->name, in_porcelain, |
| 1570 | &first, header_msg); |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1571 | has_errors = 1; |
| 1572 | continue; |
| 1573 | } |
| 1574 | |
Junio C Hamano | 3d1f148 | 2012-02-17 10:11:05 -0800 | [diff] [blame] | 1575 | if (filtered) |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 1576 | continue; |
| 1577 | |
Jeff Hostetler | a98e0f2 | 2021-02-03 15:34:45 +0000 | [diff] [blame] | 1578 | new_entry = refresh_cache_ent(istate, ce, options, |
| 1579 | &cache_errno, &changed, |
Jeff Hostetler | 15268d1 | 2021-02-03 15:34:46 +0000 | [diff] [blame] | 1580 | &t2_did_lstat, &t2_did_scan); |
Jeff Hostetler | a98e0f2 | 2021-02-03 15:34:45 +0000 | [diff] [blame] | 1581 | t2_sum_lstat += t2_did_lstat; |
Jeff Hostetler | 15268d1 | 2021-02-03 15:34:46 +0000 | [diff] [blame] | 1582 | t2_sum_scan += t2_did_scan; |
Brandon Williams | 285c2e2 | 2018-02-14 10:59:45 -0800 | [diff] [blame] | 1583 | if (new_entry == ce) |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1584 | continue; |
Ævar Arnfjörð Bjarmason | b7b793d | 2021-06-07 16:43:22 +0200 | [diff] [blame] | 1585 | display_progress(progress, i); |
Brandon Williams | 285c2e2 | 2018-02-14 10:59:45 -0800 | [diff] [blame] | 1586 | if (!new_entry) { |
Jeff King | 73b7eae | 2011-11-18 06:13:08 -0500 | [diff] [blame] | 1587 | const char *fmt; |
| 1588 | |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 1589 | if (really && cache_errno == EINVAL) { |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1590 | /* If we are doing --really-refresh that |
| 1591 | * means the index is not valid anymore. |
| 1592 | */ |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 1593 | ce->ce_flags &= ~CE_VALID; |
Nguyễn Thái Ngọc Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 1594 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 1595 | mark_fsmonitor_invalid(istate, ce); |
Nguyễn Thái Ngọc Duy | e636a7b | 2014-06-13 19:19:27 +0700 | [diff] [blame] | 1596 | istate->cache_changed |= CE_ENTRY_CHANGED; |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1597 | } |
| 1598 | if (quiet) |
| 1599 | continue; |
Jeff King | 73b7eae | 2011-11-18 06:13:08 -0500 | [diff] [blame] | 1600 | |
| 1601 | if (cache_errno == ENOENT) |
| 1602 | fmt = deleted_fmt; |
Nguyễn Thái Ngọc Duy | 895ff3b | 2015-08-22 08:08:05 +0700 | [diff] [blame] | 1603 | else if (ce_intent_to_add(ce)) |
Jeff King | 73b7eae | 2011-11-18 06:13:08 -0500 | [diff] [blame] | 1604 | 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 Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1611 | has_errors = 1; |
| 1612 | continue; |
| 1613 | } |
Linus Torvalds | cf55870 | 2008-01-22 18:41:14 -0800 | [diff] [blame] | 1614 | |
Brandon Williams | 285c2e2 | 2018-02-14 10:59:45 -0800 | [diff] [blame] | 1615 | replace_index_entry(istate, i, new_entry); |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1616 | } |
Jeff Hostetler | a98e0f2 | 2021-02-03 15:34:45 +0000 | [diff] [blame] | 1617 | trace2_data_intmax("index", NULL, "refresh/sum_lstat", t2_sum_lstat); |
Jeff Hostetler | 15268d1 | 2021-02-03 15:34:46 +0000 | [diff] [blame] | 1618 | trace2_data_intmax("index", NULL, "refresh/sum_scan", t2_sum_scan); |
Jeff Hostetler | a98e0f2 | 2021-02-03 15:34:45 +0000 | [diff] [blame] | 1619 | trace2_region_leave("index", "refresh", NULL); |
Ævar Arnfjörð Bjarmason | b7b793d | 2021-06-07 16:43:22 +0200 | [diff] [blame] | 1620 | display_progress(progress, istate->cache_nr); |
| 1621 | stop_progress(&progress); |
Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 16:41:22 +0200 | [diff] [blame] | 1622 | trace_performance_leave("refresh index"); |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 1623 | return has_errors; |
| 1624 | } |
| 1625 | |
Jameson Miller | 768d796 | 2018-07-02 19:49:29 +0000 | [diff] [blame] | 1626 | struct cache_entry *refresh_cache_entry(struct index_state *istate, |
| 1627 | struct cache_entry *ce, |
| 1628 | unsigned int options) |
Junio C Hamano | ec0cc70 | 2007-04-01 21:34:34 -0700 | [diff] [blame] | 1629 | { |
Jeff Hostetler | 15268d1 | 2021-02-03 15:34:46 +0000 | [diff] [blame] | 1630 | return refresh_cache_ent(istate, ce, options, NULL, NULL, NULL, NULL); |
Junio C Hamano | ec0cc70 | 2007-04-01 21:34:34 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Junio C Hamano | db3b313 | 2012-04-03 15:53:09 -0700 | [diff] [blame] | 1633 | |
| 1634 | /***************************************************************** |
| 1635 | * Index File I/O |
| 1636 | *****************************************************************/ |
| 1637 | |
Junio C Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 1638 | #define INDEX_FORMAT_DEFAULT 3 |
| 1639 | |
Derrick Stolee | 7211b9e | 2019-08-13 11:37:43 -0700 | [diff] [blame] | 1640 | static unsigned int get_index_format_default(struct repository *r) |
Thomas Gummerer | 136347d | 2014-02-23 21:49:57 +0100 | [diff] [blame] | 1641 | { |
| 1642 | char *envversion = getenv("GIT_INDEX_VERSION"); |
Thomas Gummerer | 3c09d68 | 2014-02-23 21:49:59 +0100 | [diff] [blame] | 1643 | char *endp; |
| 1644 | unsigned int version = INDEX_FORMAT_DEFAULT; |
Thomas Gummerer | 136347d | 2014-02-23 21:49:57 +0100 | [diff] [blame] | 1645 | |
Thomas Gummerer | 3c09d68 | 2014-02-23 21:49:59 +0100 | [diff] [blame] | 1646 | if (!envversion) { |
Derrick Stolee | 7211b9e | 2019-08-13 11:37:43 -0700 | [diff] [blame] | 1647 | prepare_repo_settings(r); |
| 1648 | |
| 1649 | if (r->settings.index_version >= 0) |
| 1650 | version = r->settings.index_version; |
Thomas Gummerer | 3c09d68 | 2014-02-23 21:49:59 +0100 | [diff] [blame] | 1651 | if (version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) { |
| 1652 | warning(_("index.version set, but the value is invalid.\n" |
Thomas Gummerer | 136347d | 2014-02-23 21:49:57 +0100 | [diff] [blame] | 1653 | "Using version %i"), INDEX_FORMAT_DEFAULT); |
Thomas Gummerer | 3c09d68 | 2014-02-23 21:49:59 +0100 | [diff] [blame] | 1654 | return INDEX_FORMAT_DEFAULT; |
Thomas Gummerer | 136347d | 2014-02-23 21:49:57 +0100 | [diff] [blame] | 1655 | } |
| 1656 | return version; |
| 1657 | } |
Thomas Gummerer | 3c09d68 | 2014-02-23 21:49:59 +0100 | [diff] [blame] | 1658 | |
| 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 Gummerer | 136347d | 2014-02-23 21:49:57 +0100 | [diff] [blame] | 1667 | } |
| 1668 | |
Junio C Hamano | db3b313 | 2012-04-03 15:53:09 -0700 | [diff] [blame] | 1669 | /* |
| 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 | */ |
| 1677 | struct ondisk_cache_entry { |
| 1678 | struct cache_time ctime; |
| 1679 | struct cache_time mtime; |
Thomas Gummerer | 7800c1e | 2013-08-18 21:41:51 +0200 | [diff] [blame] | 1680 | 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. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 1686 | /* |
| 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 Hamano | db3b313 | 2012-04-03 15:53:09 -0700 | [diff] [blame] | 1694 | }; |
| 1695 | |
Junio C Hamano | 6c9cd16 | 2012-04-03 15:53:15 -0700 | [diff] [blame] | 1696 | /* These are only used for v3 or lower */ |
Kevin Willford | ce012de | 2017-08-21 15:24:32 -0600 | [diff] [blame] | 1697 | #define align_padding_size(size, len) ((size + (len) + 8) & ~7) - (size + len) |
brian m. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 1698 | #define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,data) + (len) + 8) & ~7) |
Junio C Hamano | db3b313 | 2012-04-03 15:53:09 -0700 | [diff] [blame] | 1699 | #define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len) |
brian m. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 1700 | #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 Hamano | db3b313 | 2012-04-03 15:53:09 -0700 | [diff] [blame] | 1704 | |
Jeff Hostetler | a33fc72 | 2017-04-14 20:32:21 +0000 | [diff] [blame] | 1705 | /* Allow fsck to force verification of the index checksum. */ |
| 1706 | int verify_index_checksum; |
| 1707 | |
Ben Peart | 00ec50e | 2017-10-18 10:27:25 -0400 | [diff] [blame] | 1708 | /* Allow fsck to force verification of the cache entry order. */ |
| 1709 | int verify_ce_order; |
| 1710 | |
Ben Peart | 371ed0d | 2018-10-10 11:59:33 -0400 | [diff] [blame] | 1711 | static int verify_hdr(const struct cache_header *hdr, unsigned long size) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 1712 | { |
brian m. carlson | aab6135 | 2018-02-01 02:18:45 +0000 | [diff] [blame] | 1713 | git_hash_ctx c; |
| 1714 | unsigned char hash[GIT_MAX_RAWSZ]; |
Junio C Hamano | 0136bac | 2012-04-03 15:53:12 -0700 | [diff] [blame] | 1715 | int hdr_version; |
Derrick Stolee | ee1f0c2 | 2023-01-06 16:31:54 +0000 | [diff] [blame] | 1716 | unsigned char *start, *end; |
| 1717 | struct object_id oid; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 1718 | |
Linus Torvalds | ccc4feb | 2005-04-15 10:44:27 -0700 | [diff] [blame] | 1719 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1720 | return error(_("bad signature 0x%08x"), hdr->hdr_signature); |
Junio C Hamano | 0136bac | 2012-04-03 15:53:12 -0700 | [diff] [blame] | 1721 | hdr_version = ntohl(hdr->hdr_version); |
Nguyễn Thái Ngọc Duy | b82a7b5 | 2013-02-22 19:09:24 +0700 | [diff] [blame] | 1722 | if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1723 | return error(_("bad index version %d"), hdr_version); |
Jeff Hostetler | a33fc72 | 2017-04-14 20:32:21 +0000 | [diff] [blame] | 1724 | |
| 1725 | if (!verify_index_checksum) |
| 1726 | return 0; |
| 1727 | |
Derrick Stolee | ee1f0c2 | 2023-01-06 16:31:54 +0000 | [diff] [blame] | 1728 | 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. carlson | aab6135 | 2018-02-01 02:18:45 +0000 | [diff] [blame] | 1734 | 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 Stolee | ee1f0c2 | 2023-01-06 16:31:54 +0000 | [diff] [blame] | 1737 | if (!hasheq(hash, start)) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1738 | return error(_("bad index file sha1 signature")); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 1739 | return 0; |
| 1740 | } |
| 1741 | |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1742 | static int read_index_extension(struct index_state *istate, |
Ben Peart | 371ed0d | 2018-10-10 11:59:33 -0400 | [diff] [blame] | 1743 | const char *ext, const char *data, unsigned long sz) |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 1744 | { |
| 1745 | switch (CACHE_EXT(ext)) { |
| 1746 | case CACHE_EXT_TREE: |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 1747 | istate->cache_tree = cache_tree_read(data, sz); |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 1748 | break; |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 1749 | case CACHE_EXT_RESOLVE_UNDO: |
| 1750 | istate->resolve_undo = resolve_undo_read(data, sz); |
| 1751 | break; |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 1752 | case CACHE_EXT_LINK: |
| 1753 | if (read_link_extension(istate, data, sz)) |
| 1754 | return -1; |
| 1755 | break; |
Nguyễn Thái Ngọc Duy | f9e6c64 | 2015-03-08 17:12:34 +0700 | [diff] [blame] | 1756 | case CACHE_EXT_UNTRACKED: |
| 1757 | istate->untracked = read_untracked_extension(data, sz); |
| 1758 | break; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 1759 | case CACHE_EXT_FSMONITOR: |
| 1760 | read_fsmonitor_extension(istate, data, sz); |
| 1761 | break; |
Ben Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 1762 | case CACHE_EXT_ENDOFINDEXENTRIES: |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 1763 | case CACHE_EXT_INDEXENTRYOFFSETTABLE: |
Ben Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 1764 | /* already handled in do_read_index() */ |
| 1765 | break; |
Derrick Stolee | cd42415 | 2021-03-30 13:10:54 +0000 | [diff] [blame] | 1766 | case CACHE_EXT_SPARSE_DIRECTORIES: |
| 1767 | /* no content, only an indicator */ |
Derrick Stolee | 9fadb37 | 2022-05-23 13:48:40 +0000 | [diff] [blame] | 1768 | istate->sparse_index = INDEX_COLLAPSED; |
Derrick Stolee | cd42415 | 2021-03-30 13:10:54 +0000 | [diff] [blame] | 1769 | break; |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 1770 | default: |
| 1771 | if (*ext < 'A' || 'Z' < *ext) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1772 | return error(_("index uses %.4s extension, which we do not understand"), |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 1773 | ext); |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1774 | fprintf_ln(stderr, _("ignoring %.4s extension"), ext); |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 1775 | break; |
| 1776 | } |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
Victoria Dye | 4a6ed30 | 2022-09-28 17:19:00 +0000 | [diff] [blame] | 1780 | /* |
| 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 Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 1791 | static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool, |
| 1792 | unsigned int version, |
Victoria Dye | 4a6ed30 | 2022-09-28 17:19:00 +0000 | [diff] [blame] | 1793 | const char *ondisk, |
Junio C Hamano | 6c9cd16 | 2012-04-03 15:53:15 -0700 | [diff] [blame] | 1794 | unsigned long *ent_size, |
Nguyễn Thái Ngọc Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1795 | const struct cache_entry *previous_ce) |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 1796 | { |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1797 | struct cache_entry *ce; |
Junio C Hamano | 7fec10b | 2008-01-18 23:42:00 -0800 | [diff] [blame] | 1798 | size_t len; |
Nguyễn Thái Ngọc Duy | 06aaaa0 | 2008-10-01 11:04:01 +0700 | [diff] [blame] | 1799 | const char *name; |
brian m. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 1800 | const unsigned hashsz = the_hash_algo->rawsz; |
Victoria Dye | 4a6ed30 | 2022-09-28 17:19:00 +0000 | [diff] [blame] | 1801 | const char *flagsp = ondisk + offsetof(struct ondisk_cache_entry, data) + hashsz; |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1802 | unsigned int flags; |
Nguyễn Thái Ngọc Duy | f5c4a9a | 2018-11-03 09:48:49 +0100 | [diff] [blame] | 1803 | size_t copy_len = 0; |
Nguyễn Thái Ngọc Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1804 | /* |
| 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 Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 1811 | int expand_name_field = version == 4; |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1812 | |
| 1813 | /* On-disk flags are just 16 bits */ |
brian m. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 1814 | flags = get_be16(flagsp); |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1815 | len = flags & CE_NAMEMASK; |
| 1816 | |
| 1817 | if (flags & CE_EXTENDED) { |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1818 | int extended_flags; |
Victoria Dye | 4a6ed30 | 2022-09-28 17:19:00 +0000 | [diff] [blame] | 1819 | extended_flags = get_be16(flagsp + sizeof(uint16_t)) << 16; |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1820 | /* 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 Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1822 | die(_("unknown index entry format 0x%08x"), extended_flags); |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1823 | flags |= extended_flags; |
Victoria Dye | 4a6ed30 | 2022-09-28 17:19:00 +0000 | [diff] [blame] | 1824 | name = (const char *)(flagsp + 2 * sizeof(uint16_t)); |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1825 | } |
| 1826 | else |
Victoria Dye | 4a6ed30 | 2022-09-28 17:19:00 +0000 | [diff] [blame] | 1827 | name = (const char *)(flagsp + sizeof(uint16_t)); |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1828 | |
Nguyễn Thái Ngọc Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1829 | if (expand_name_field) { |
| 1830 | const unsigned char *cp = (const unsigned char *)name; |
| 1831 | size_t strip_len, previous_len; |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1832 | |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 1833 | /* If we're at the beginning of a block, ignore the previous name */ |
Nguyễn Thái Ngọc Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1834 | strip_len = decode_varint(&cp); |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 1835 | if (previous_ce) { |
| 1836 | previous_len = previous_ce->ce_namelen; |
| 1837 | if (previous_len < strip_len) |
Nguyễn Thái Ngọc Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1838 | die(_("malformed name field in the index, near path '%s'"), |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 1839 | previous_ce->name); |
| 1840 | copy_len = previous_len - strip_len; |
Nguyễn Thái Ngọc Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1841 | } |
Nguyễn Thái Ngọc Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1842 | 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 Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 1851 | ce = mem_pool__ce_alloc(ce_mem_pool, len); |
Nguyễn Thái Ngọc Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1852 | |
Victoria Dye | 4a6ed30 | 2022-09-28 17:19:00 +0000 | [diff] [blame] | 1853 | /* |
| 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 Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1875 | ce->ce_flags = flags & ~CE_NAMEMASK; |
| 1876 | ce->ce_namelen = len; |
| 1877 | ce->index = 0; |
Victoria Dye | 4a6ed30 | 2022-09-28 17:19:00 +0000 | [diff] [blame] | 1878 | oidread(&ce->oid, (const unsigned char *)ondisk + offsetof(struct ondisk_cache_entry, data)); |
Nguyễn Thái Ngọc Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1879 | |
| 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 Hamano | 6c9cd16 | 2012-04-03 15:53:15 -0700 | [diff] [blame] | 1885 | } else { |
Nguyễn Thái Ngọc Duy | 252d079 | 2018-09-26 15:54:36 -0400 | [diff] [blame] | 1886 | memcpy(ce->name, name, len + 1); |
| 1887 | *ent_size = ondisk_ce_size(ce); |
Junio C Hamano | 6c9cd16 | 2012-04-03 15:53:15 -0700 | [diff] [blame] | 1888 | } |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 1889 | return ce; |
Linus Torvalds | cf55870 | 2008-01-22 18:41:14 -0800 | [diff] [blame] | 1890 | } |
| 1891 | |
Thomas Gummerer | 03f15a7 | 2015-03-20 22:43:14 +0100 | [diff] [blame] | 1892 | static void check_ce_order(struct index_state *istate) |
Jaime Soriano Pastor | 15999d0 | 2014-08-29 10:54:41 +0200 | [diff] [blame] | 1893 | { |
Thomas Gummerer | 03f15a7 | 2015-03-20 22:43:14 +0100 | [diff] [blame] | 1894 | unsigned int i; |
| 1895 | |
Ben Peart | 00ec50e | 2017-10-18 10:27:25 -0400 | [diff] [blame] | 1896 | if (!verify_ce_order) |
| 1897 | return; |
| 1898 | |
Thomas Gummerer | 03f15a7 | 2015-03-20 22:43:14 +0100 | [diff] [blame] | 1899 | 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 Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1905 | die(_("unordered stage entries in index")); |
Thomas Gummerer | 03f15a7 | 2015-03-20 22:43:14 +0100 | [diff] [blame] | 1906 | if (!name_compare) { |
| 1907 | if (!ce_stage(ce)) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1908 | die(_("multiple stage entries for merged file '%s'"), |
Thomas Gummerer | 03f15a7 | 2015-03-20 22:43:14 +0100 | [diff] [blame] | 1909 | ce->name); |
| 1910 | if (ce_stage(ce) > ce_stage(next_ce)) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 1911 | die(_("unordered stage entries for '%s'"), |
Thomas Gummerer | 03f15a7 | 2015-03-20 22:43:14 +0100 | [diff] [blame] | 1912 | ce->name); |
| 1913 | } |
Jaime Soriano Pastor | 15999d0 | 2014-08-29 10:54:41 +0200 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | |
Christian Couder | 435ec09 | 2016-01-27 07:58:05 +0100 | [diff] [blame] | 1917 | static void tweak_untracked_cache(struct index_state *istate) |
| 1918 | { |
Derrick Stolee | ad0fb65 | 2019-08-13 11:37:46 -0700 | [diff] [blame] | 1919 | struct repository *r = the_repository; |
| 1920 | |
| 1921 | prepare_repo_settings(r); |
| 1922 | |
Ævar Arnfjörð Bjarmason | f1bee82 | 2021-09-21 15:13:01 +0200 | [diff] [blame] | 1923 | switch (r->settings.core_untracked_cache) { |
| 1924 | case UNTRACKED_CACHE_REMOVE: |
Christian Couder | 435ec09 | 2016-01-27 07:58:05 +0100 | [diff] [blame] | 1925 | remove_untracked_cache(istate); |
Ævar Arnfjörð Bjarmason | f1bee82 | 2021-09-21 15:13:01 +0200 | [diff] [blame] | 1926 | break; |
| 1927 | case UNTRACKED_CACHE_WRITE: |
Derrick Stolee | ad0fb65 | 2019-08-13 11:37:46 -0700 | [diff] [blame] | 1928 | add_untracked_cache(istate); |
Ævar Arnfjörð Bjarmason | f1bee82 | 2021-09-21 15:13:01 +0200 | [diff] [blame] | 1929 | break; |
| 1930 | case UNTRACKED_CACHE_KEEP: |
Ævar Arnfjörð Bjarmason | 3050b6d | 2021-09-21 15:13:02 +0200 | [diff] [blame] | 1931 | /* |
| 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ð Bjarmason | f1bee82 | 2021-09-21 15:13:01 +0200 | [diff] [blame] | 1937 | break; |
Ævar Arnfjörð Bjarmason | f1bee82 | 2021-09-21 15:13:01 +0200 | [diff] [blame] | 1938 | } |
Christian Couder | 435ec09 | 2016-01-27 07:58:05 +0100 | [diff] [blame] | 1939 | } |
| 1940 | |
Christian Couder | 4392531 | 2017-02-27 19:00:02 +0100 | [diff] [blame] | 1941 | static 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 Couder | 435ec09 | 2016-01-27 07:58:05 +0100 | [diff] [blame] | 1957 | static void post_read_index_from(struct index_state *istate) |
| 1958 | { |
| 1959 | check_ce_order(istate); |
| 1960 | tweak_untracked_cache(istate); |
Christian Couder | 4392531 | 2017-02-27 19:00:02 +0100 | [diff] [blame] | 1961 | tweak_split_index(istate); |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 1962 | tweak_fsmonitor(istate); |
Christian Couder | 435ec09 | 2016-01-27 07:58:05 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 1965 | static size_t estimate_cache_size_from_compressed(unsigned int entries) |
| 1966 | { |
| 1967 | return entries * (sizeof(struct cache_entry) + CACHE_ENTRY_PATH_LENGTH); |
| 1968 | } |
| 1969 | |
| 1970 | static 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 Schindelin | c097b95 | 2019-10-04 08:09:26 -0700 | [diff] [blame] | 1977 | per_entry += align_padding_size(per_entry, 0); |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 1978 | return ondisk_size + entries * per_entry; |
| 1979 | } |
| 1980 | |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 1981 | struct index_entry_offset |
| 1982 | { |
| 1983 | /* starting byte offset into index file, count of index entries in this block */ |
| 1984 | int offset, nr; |
| 1985 | }; |
| 1986 | |
| 1987 | struct index_entry_offset_table |
| 1988 | { |
| 1989 | int nr; |
| 1990 | struct index_entry_offset entries[FLEX_ARRAY]; |
| 1991 | }; |
| 1992 | |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 1993 | static struct index_entry_offset_table *read_ieot_extension(const char *mmap, size_t mmap_size, size_t offset); |
| 1994 | static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot); |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 1995 | |
Ben Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 1996 | static size_t read_eoie_extension(const char *mmap, size_t mmap_size); |
| 1997 | static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset); |
| 1998 | |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 1999 | struct load_index_extensions |
| 2000 | { |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 2001 | pthread_t pthread; |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 2002 | struct index_state *istate; |
| 2003 | const char *mmap; |
| 2004 | size_t mmap_size; |
| 2005 | unsigned long src_offset; |
| 2006 | }; |
| 2007 | |
| 2008 | static 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 Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2035 | /* |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2036 | * 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 | */ |
| 2039 | static 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 Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2047 | struct cache_entry *ce; |
| 2048 | unsigned long consumed; |
| 2049 | |
Victoria Dye | 4a6ed30 | 2022-09-28 17:19:00 +0000 | [diff] [blame] | 2050 | ce = create_from_disk(ce_mem_pool, istate->version, |
| 2051 | mmap + src_offset, |
| 2052 | &consumed, previous_ce); |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2053 | set_index_entry(istate, i, ce); |
| 2054 | |
| 2055 | src_offset += consumed; |
| 2056 | previous_ce = ce; |
| 2057 | } |
| 2058 | return src_offset - start_offset; |
| 2059 | } |
| 2060 | |
| 2061 | static 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 Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 2066 | istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool)); |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2067 | if (istate->version == 4) { |
Elijah Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 2068 | mem_pool_init(istate->ce_mem_pool, |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2069 | estimate_cache_size_from_compressed(istate->cache_nr)); |
| 2070 | } else { |
Elijah Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 2071 | mem_pool_init(istate->ce_mem_pool, |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2072 | 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 Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2080 | /* |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2081 | * 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 Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2089 | struct 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 | */ |
| 2106 | static 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 | |
| 2120 | static unsigned long load_cache_entries_threaded(struct index_state *istate, const char *mmap, size_t mmap_size, |
Jeff King | 7bd9631 | 2019-05-09 17:29:44 -0400 | [diff] [blame] | 2121 | int nr_threads, struct index_entry_offset_table *ieot) |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2122 | { |
| 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 Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 2131 | istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool)); |
| 2132 | mem_pool_init(istate->ce_mem_pool, 0); |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2133 | |
| 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é Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2137 | CALLOC_ARRAY(data, nr_threads); |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2138 | |
| 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é Scharfe | bcd2c5e | 2020-09-04 19:33:55 +0200 | [diff] [blame] | 2159 | p->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool)); |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2160 | if (istate->version == 4) { |
Elijah Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 2161 | mem_pool_init(p->ce_mem_pool, |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2162 | estimate_cache_size_from_compressed(nr)); |
| 2163 | } else { |
Elijah Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 2164 | mem_pool_init(p->ce_mem_pool, |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2165 | 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 Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2192 | |
Victoria Dye | 491df5f | 2022-05-10 23:32:30 +0000 | [diff] [blame] | 2193 | static 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ð Bjarmason | 5bdf6d4 | 2023-01-12 13:55:26 +0100 | [diff] [blame] | 2199 | 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 Dye | 491df5f | 2022-05-10 23:32:30 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 2205 | /* remember to discard_cache() before reading a different cache! */ |
Nguyễn Thái Ngọc Duy | 3e52f70 | 2014-06-13 19:19:51 +0700 | [diff] [blame] | 2206 | int do_read_index(struct index_state *istate, const char *path, int must_exist) |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 2207 | { |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2208 | int fd; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 2209 | struct stat st; |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 2210 | unsigned long src_offset; |
Ben Peart | 371ed0d | 2018-10-10 11:59:33 -0400 | [diff] [blame] | 2211 | const struct cache_header *hdr; |
| 2212 | const char *mmap; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2213 | size_t mmap_size; |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 2214 | struct load_index_extensions p; |
| 2215 | size_t extension_offset = 0; |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2216 | int nr_threads, cpus; |
| 2217 | struct index_entry_offset_table *ieot = NULL; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 2218 | |
Junio C Hamano | 913e0e9 | 2008-08-23 12:57:30 -0700 | [diff] [blame] | 2219 | if (istate->initialized) |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 2220 | return istate->cache_nr; |
Linus Torvalds | 5d1a5c0 | 2005-10-01 13:24:27 -0700 | [diff] [blame] | 2221 | |
Kjetil Barvik | fba2f38 | 2009-02-19 21:08:29 +0100 | [diff] [blame] | 2222 | istate->timestamp.sec = 0; |
| 2223 | istate->timestamp.nsec = 0; |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 2224 | fd = open(path, O_RDONLY); |
Linus Torvalds | 5d1a5c0 | 2005-10-01 13:24:27 -0700 | [diff] [blame] | 2225 | if (fd < 0) { |
Victoria Dye | 491df5f | 2022-05-10 23:32:30 +0000 | [diff] [blame] | 2226 | if (!must_exist && errno == ENOENT) { |
| 2227 | set_new_index_sparsity(istate); |
Johannes Schindelin | 866b43e | 2023-06-29 13:23:08 +0000 | [diff] [blame] | 2228 | istate->initialized = 1; |
Linus Torvalds | 5d1a5c0 | 2005-10-01 13:24:27 -0700 | [diff] [blame] | 2229 | return 0; |
Victoria Dye | 491df5f | 2022-05-10 23:32:30 +0000 | [diff] [blame] | 2230 | } |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 2231 | die_errno(_("%s: index file open failed"), path); |
Linus Torvalds | 5d1a5c0 | 2005-10-01 13:24:27 -0700 | [diff] [blame] | 2232 | } |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 2233 | |
Luiz Fernando N. Capitulino | 3511a37 | 2007-04-25 11:18:17 -0300 | [diff] [blame] | 2234 | if (fstat(fd, &st)) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 2235 | die_errno(_("%s: cannot stat the open index"), path); |
Luiz Fernando N. Capitulino | 3511a37 | 2007-04-25 11:18:17 -0300 | [diff] [blame] | 2236 | |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2237 | mmap_size = xsize_t(st.st_size); |
brian m. carlson | aab6135 | 2018-02-01 02:18:45 +0000 | [diff] [blame] | 2238 | if (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 2239 | die(_("%s: index file smaller than expected"), path); |
Luiz Fernando N. Capitulino | 3511a37 | 2007-04-25 11:18:17 -0300 | [diff] [blame] | 2240 | |
Varun Naik | 02638d1 | 2019-07-13 20:01:53 -0700 | [diff] [blame] | 2241 | mmap = xmmap_gently(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0); |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2242 | if (mmap == MAP_FAILED) |
Eric Wong | dc05929 | 2021-06-30 00:01:32 +0000 | [diff] [blame] | 2243 | die_errno(_("%s: unable to map index file%s"), path, |
| 2244 | mmap_os_err()); |
Nguyễn Thái Ngọc Duy | 57d84f8 | 2012-08-06 18:27:09 +0700 | [diff] [blame] | 2245 | close(fd); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 2246 | |
Ben Peart | 371ed0d | 2018-10-10 11:59:33 -0400 | [diff] [blame] | 2247 | hdr = (const struct cache_header *)mmap; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2248 | if (verify_hdr(hdr, mmap_size) < 0) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 2249 | goto unmap; |
| 2250 | |
brian m. carlson | 92e2cab | 2021-04-26 01:02:50 +0000 | [diff] [blame] | 2251 | oidread(&istate->oid, (const unsigned char *)hdr + mmap_size - the_hash_algo->rawsz); |
Junio C Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 2252 | istate->version = ntohl(hdr->hdr_version); |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 2253 | istate->cache_nr = ntohl(hdr->hdr_entries); |
| 2254 | istate->cache_alloc = alloc_nr(istate->cache_nr); |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2255 | CALLOC_ARRAY(istate->cache, istate->cache_alloc); |
Junio C Hamano | 913e0e9 | 2008-08-23 12:57:30 -0700 | [diff] [blame] | 2256 | istate->initialized = 1; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2257 | |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 2258 | p.istate = istate; |
| 2259 | p.mmap = mmap; |
| 2260 | p.mmap_size = mmap_size; |
Junio C Hamano | 6c9cd16 | 2012-04-03 15:53:15 -0700 | [diff] [blame] | 2261 | |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2262 | src_offset = sizeof(*hdr); |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 2263 | |
Jonathan Nieder | 2a9dede | 2018-11-19 22:14:26 -0800 | [diff] [blame] | 2264 | if (git_config_get_index_threads(&nr_threads)) |
| 2265 | nr_threads = 1; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2266 | |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2267 | /* 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 Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 2273 | } |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 2274 | |
Nguyễn Thái Ngọc Duy | 88168b9 | 2018-11-03 09:48:48 +0100 | [diff] [blame] | 2275 | if (!HAVE_THREADS) |
| 2276 | nr_threads = 1; |
| 2277 | |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 2278 | 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 Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2291 | |
| 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 King | 7bd9631 | 2019-05-09 17:29:44 -0400 | [diff] [blame] | 2300 | src_offset += load_cache_entries_threaded(istate, mmap, mmap_size, nr_threads, ieot); |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2301 | free(ieot); |
| 2302 | } else { |
| 2303 | src_offset += load_all_cache_entries(istate, mmap, mmap_size, src_offset); |
| 2304 | } |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 2305 | |
Kjetil Barvik | fba2f38 | 2009-02-19 21:08:29 +0100 | [diff] [blame] | 2306 | istate->timestamp.sec = st.st_mtime; |
Kjetil Barvik | c06ff49 | 2009-03-04 18:47:40 +0100 | [diff] [blame] | 2307 | istate->timestamp.nsec = ST_MTIME_NSEC(st); |
Kjetil Barvik | fba2f38 | 2009-02-19 21:08:29 +0100 | [diff] [blame] | 2308 | |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 2309 | /* if we created a thread, join it otherwise load the extensions on the primary thread */ |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 2310 | 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 Duy | 88168b9 | 2018-11-03 09:48:48 +0100 | [diff] [blame] | 2314 | } else { |
Ben Peart | abb4bb8 | 2018-10-10 11:59:36 -0400 | [diff] [blame] | 2315 | p.src_offset = src_offset; |
| 2316 | load_index_extensions(&p); |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 2317 | } |
Ben Peart | 371ed0d | 2018-10-10 11:59:33 -0400 | [diff] [blame] | 2318 | munmap((void *)mmap, mmap_size); |
Jeff Hostetler | 42fee7a | 2019-02-22 14:25:07 -0800 | [diff] [blame] | 2319 | |
| 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 Dye | 7ca4fc8 | 2021-11-23 00:20:33 +0000 | [diff] [blame] | 2329 | /* |
| 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 Stolee | 4300f84 | 2021-03-30 13:10:48 +0000 | [diff] [blame] | 2334 | prepare_repo_settings(istate->repo); |
| 2335 | if (istate->repo->settings.command_requires_full_index) |
| 2336 | ensure_full_index(istate); |
Victoria Dye | 7ca4fc8 | 2021-11-23 00:20:33 +0000 | [diff] [blame] | 2337 | else |
| 2338 | ensure_correct_sparsity(istate); |
Derrick Stolee | 4300f84 | 2021-03-30 13:10:48 +0000 | [diff] [blame] | 2339 | |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 2340 | return istate->cache_nr; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 2341 | |
| 2342 | unmap: |
Ben Peart | 371ed0d | 2018-10-10 11:59:33 -0400 | [diff] [blame] | 2343 | munmap((void *)mmap, mmap_size); |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 2344 | die(_("index file corrupt")); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
Christian Couder | 0d59ffb | 2017-02-27 19:00:12 +0100 | [diff] [blame] | 2347 | /* |
| 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 Gummerer | a125a22 | 2018-01-07 22:30:13 +0000 | [diff] [blame] | 2353 | static void freshen_shared_index(const char *shared_index, int warn) |
Christian Couder | 0d59ffb | 2017-02-27 19:00:12 +0100 | [diff] [blame] | 2354 | { |
Christian Couder | 0d59ffb | 2017-02-27 19:00:12 +0100 | [diff] [blame] | 2355 | if (!check_and_freshen_file(shared_index, 1) && warn) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 2356 | warning(_("could not freshen shared index '%s'"), shared_index); |
Christian Couder | 0d59ffb | 2017-02-27 19:00:12 +0100 | [diff] [blame] | 2357 | } |
| 2358 | |
Thomas Gummerer | a125a22 | 2018-01-07 22:30:13 +0000 | [diff] [blame] | 2359 | int read_index_from(struct index_state *istate, const char *path, |
| 2360 | const char *gitdir) |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2361 | { |
| 2362 | struct split_index *split_index; |
| 2363 | int ret; |
brian m. carlson | 2182abd | 2018-05-02 00:25:43 +0000 | [diff] [blame] | 2364 | char *base_oid_hex; |
Thomas Gummerer | a125a22 | 2018-01-07 22:30:13 +0000 | [diff] [blame] | 2365 | char *base_path; |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2366 | |
| 2367 | /* istate->initialized covers both .git/index and .git/sharedindex.xxx */ |
| 2368 | if (istate->initialized) |
| 2369 | return istate->cache_nr; |
| 2370 | |
Jeff Hostetler | 42fee7a | 2019-02-22 14:25:07 -0800 | [diff] [blame] | 2371 | /* |
| 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 Duy | c46c406 | 2018-08-18 16:41:22 +0200 | [diff] [blame] | 2377 | trace_performance_enter(); |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2378 | ret = do_read_index(istate, path, 0); |
Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 16:41:22 +0200 | [diff] [blame] | 2379 | trace_performance_leave("read cache %s", path); |
Jeff Hostetler | 42fee7a | 2019-02-22 14:25:07 -0800 | [diff] [blame] | 2380 | trace2_region_leave_printf("index", "do_read_index", the_repository, |
| 2381 | "%s", path); |
Christian Couder | 435ec09 | 2016-01-27 07:58:05 +0100 | [diff] [blame] | 2382 | |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2383 | split_index = istate->split_index; |
brian m. carlson | 2182abd | 2018-05-02 00:25:43 +0000 | [diff] [blame] | 2384 | if (!split_index || is_null_oid(&split_index->base_oid)) { |
Christian Couder | 435ec09 | 2016-01-27 07:58:05 +0100 | [diff] [blame] | 2385 | post_read_index_from(istate); |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2386 | return ret; |
Thomas Gummerer | 03f15a7 | 2015-03-20 22:43:14 +0100 | [diff] [blame] | 2387 | } |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2388 | |
Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 16:41:22 +0200 | [diff] [blame] | 2389 | trace_performance_enter(); |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2390 | if (split_index->base) |
Ævar Arnfjörð Bjarmason | 2f6b1eb | 2023-01-12 13:55:27 +0100 | [diff] [blame] | 2391 | release_index(split_index->base); |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2392 | else |
Ævar Arnfjörð Bjarmason | 2f6b1eb | 2023-01-12 13:55:27 +0100 | [diff] [blame] | 2393 | ALLOC_ARRAY(split_index->base, 1); |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 2394 | index_state_init(split_index->base, istate->repo); |
Christian Couder | de6ae5f | 2017-03-06 10:42:00 +0100 | [diff] [blame] | 2395 | |
brian m. carlson | 2182abd | 2018-05-02 00:25:43 +0000 | [diff] [blame] | 2396 | base_oid_hex = oid_to_hex(&split_index->base_oid); |
| 2397 | base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex); |
Johannes Schindelin | 7667f4f | 2023-06-29 13:23:09 +0000 | [diff] [blame] | 2398 | 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ábor | 998330a | 2021-08-26 23:00:02 +0200 | [diff] [blame] | 2406 | char *path_copy = xstrdup(path); |
Johannes Schindelin | 652891d | 2022-06-15 23:35:42 +0000 | [diff] [blame] | 2407 | char *base_path2 = xstrfmt("%s/sharedindex.%s", |
| 2408 | dirname(path_copy), base_oid_hex); |
SZEDER Gábor | 998330a | 2021-08-26 23:00:02 +0200 | [diff] [blame] | 2409 | 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 Schindelin | 652891d | 2022-06-15 23:35:42 +0000 | [diff] [blame] | 2415 | free(base_path2); |
SZEDER Gábor | 998330a | 2021-08-26 23:00:02 +0200 | [diff] [blame] | 2416 | } |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 2417 | if (!oideq(&split_index->base_oid, &split_index->base->oid)) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 2418 | die(_("broken index, expect %s in %s, got %s"), |
brian m. carlson | 2182abd | 2018-05-02 00:25:43 +0000 | [diff] [blame] | 2419 | base_oid_hex, base_path, |
brian m. carlson | 75691ea | 2018-05-02 00:25:44 +0000 | [diff] [blame] | 2420 | oid_to_hex(&split_index->base->oid)); |
Christian Couder | de6ae5f | 2017-03-06 10:42:00 +0100 | [diff] [blame] | 2421 | |
Thomas Gummerer | a125a22 | 2018-01-07 22:30:13 +0000 | [diff] [blame] | 2422 | freshen_shared_index(base_path, 0); |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2423 | merge_base_index(istate); |
Christian Couder | 435ec09 | 2016-01-27 07:58:05 +0100 | [diff] [blame] | 2424 | post_read_index_from(istate); |
Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 16:41:22 +0200 | [diff] [blame] | 2425 | trace_performance_leave("read cache %s", base_path); |
Carlo Marcelo Arenas Belón | b42ad7d | 2018-10-20 00:33:34 -0700 | [diff] [blame] | 2426 | free(base_path); |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2427 | return ret; |
| 2428 | } |
| 2429 | |
Junio C Hamano | fa7b3c2 | 2008-11-12 11:52:35 -0800 | [diff] [blame] | 2430 | int is_index_unborn(struct index_state *istate) |
| 2431 | { |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 2432 | return (!istate->cache_nr && !istate->timestamp.sec); |
Junio C Hamano | fa7b3c2 | 2008-11-12 11:52:35 -0800 | [diff] [blame] | 2433 | } |
| 2434 | |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 2435 | void index_state_init(struct index_state *istate, struct repository *r) |
Ævar Arnfjörð Bjarmason | 2f6b1eb | 2023-01-12 13:55:27 +0100 | [diff] [blame] | 2436 | { |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 2437 | struct index_state blank = INDEX_STATE_INIT(r); |
Ævar Arnfjörð Bjarmason | 2f6b1eb | 2023-01-12 13:55:27 +0100 | [diff] [blame] | 2438 | memcpy(istate, &blank, sizeof(*istate)); |
| 2439 | } |
| 2440 | |
| 2441 | void release_index(struct index_state *istate) |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 2442 | { |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 2443 | /* |
| 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 Miller | 8616a2d | 2018-07-02 19:49:39 +0000 | [diff] [blame] | 2447 | * cache entries. validate_cache_entries can detect when this |
| 2448 | * assertion does not hold. |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 2449 | */ |
Jameson Miller | 8616a2d | 2018-07-02 19:49:39 +0000 | [diff] [blame] | 2450 | validate_cache_entries(istate); |
René Scharfe | debed2a | 2011-10-24 23:59:14 +0200 | [diff] [blame] | 2451 | |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 2452 | resolve_undo_clear_index(istate); |
Karsten Blees | 2092678 | 2013-02-28 00:57:48 +0100 | [diff] [blame] | 2453 | free_name_hash(istate); |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 2454 | cache_tree_free(&(istate->cache_tree)); |
Ævar Arnfjörð Bjarmason | 2f6b1eb | 2023-01-12 13:55:27 +0100 | [diff] [blame] | 2455 | free(istate->fsmonitor_last_update); |
| 2456 | free(istate->cache); |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2457 | discard_split_index(istate); |
Nguyễn Thái Ngọc Duy | f9e6c64 | 2015-03-08 17:12:34 +0700 | [diff] [blame] | 2458 | free_untracked_cache(istate->untracked); |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 2459 | |
Ævar Arnfjörð Bjarmason | b5fcb1c | 2022-11-08 19:17:40 +0100 | [diff] [blame] | 2460 | if (istate->sparse_checkout_patterns) { |
| 2461 | clear_pattern_list(istate->sparse_checkout_patterns); |
| 2462 | FREE_AND_NULL(istate->sparse_checkout_patterns); |
| 2463 | } |
| 2464 | |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 2465 | if (istate->ce_mem_pool) { |
Jameson Miller | 8616a2d | 2018-07-02 19:49:39 +0000 | [diff] [blame] | 2466 | mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries()); |
Elijah Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 2467 | FREE_AND_NULL(istate->ce_mem_pool); |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 2468 | } |
Johannes Schindelin | 6d297f8 | 2006-07-08 18:42:41 +0200 | [diff] [blame] | 2469 | } |
| 2470 | |
Ævar Arnfjörð Bjarmason | 2f6b1eb | 2023-01-12 13:55:27 +0100 | [diff] [blame] | 2471 | void discard_index(struct index_state *istate) |
| 2472 | { |
| 2473 | release_index(istate); |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 2474 | index_state_init(istate, istate->repo); |
Ævar Arnfjörð Bjarmason | 2f6b1eb | 2023-01-12 13:55:27 +0100 | [diff] [blame] | 2475 | } |
| 2476 | |
Jameson Miller | 8616a2d | 2018-07-02 19:49:39 +0000 | [diff] [blame] | 2477 | /* |
| 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 | */ |
| 2484 | void 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 Duy | 391408e | 2018-11-10 06:16:04 +0100 | [diff] [blame] | 2493 | BUG("cache entry is not allocated from expected memory pool"); |
Jameson Miller | 8616a2d | 2018-07-02 19:49:39 +0000 | [diff] [blame] | 2494 | } 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 Duy | 391408e | 2018-11-10 06:16:04 +0100 | [diff] [blame] | 2500 | BUG("cache entry is not allocated from expected memory pool"); |
Jameson Miller | 8616a2d | 2018-07-02 19:49:39 +0000 | [diff] [blame] | 2501 | } |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | if (istate->split_index) |
| 2506 | validate_cache_entries(istate->split_index->base); |
| 2507 | } |
| 2508 | |
Linus Torvalds | d1f128b | 2008-03-06 12:46:09 -0800 | [diff] [blame] | 2509 | int unmerged_index(const struct index_state *istate) |
Daniel Barkalow | 94a5728 | 2008-02-07 11:40:13 -0500 | [diff] [blame] | 2510 | { |
| 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 Duy | 150fe06 | 2019-01-12 09:13:31 +0700 | [diff] [blame] | 2519 | int repo_index_has_changes(struct repository *repo, |
| 2520 | struct tree *tree, |
| 2521 | struct strbuf *sb) |
Elijah Newren | cffbfad | 2018-06-30 18:24:55 -0700 | [diff] [blame] | 2522 | { |
Nguyễn Thái Ngọc Duy | 150fe06 | 2019-01-12 09:13:31 +0700 | [diff] [blame] | 2523 | struct index_state *istate = repo->index; |
Elijah Newren | e1f8694 | 2018-06-30 18:25:00 -0700 | [diff] [blame] | 2524 | struct object_id cmp; |
Elijah Newren | cffbfad | 2018-06-30 18:24:55 -0700 | [diff] [blame] | 2525 | int i; |
| 2526 | |
Elijah Newren | e1f8694 | 2018-06-30 18:25:00 -0700 | [diff] [blame] | 2527 | if (tree) |
| 2528 | cmp = tree->object.oid; |
Ævar Arnfjörð Bjarmason | 4a93b89 | 2023-03-28 15:58:58 +0200 | [diff] [blame] | 2529 | if (tree || !repo_get_oid_tree(repo, "HEAD", &cmp)) { |
Elijah Newren | cffbfad | 2018-06-30 18:24:55 -0700 | [diff] [blame] | 2530 | struct diff_options opt; |
| 2531 | |
Nguyễn Thái Ngọc Duy | 150fe06 | 2019-01-12 09:13:31 +0700 | [diff] [blame] | 2532 | repo_diff_setup(repo, &opt); |
Elijah Newren | cffbfad | 2018-06-30 18:24:55 -0700 | [diff] [blame] | 2533 | opt.flags.exit_with_status = 1; |
| 2534 | if (!sb) |
| 2535 | opt.flags.quick = 1; |
Andrzej Hunt | 8d833e9 | 2021-07-25 15:08:25 +0200 | [diff] [blame] | 2536 | diff_setup_done(&opt); |
Elijah Newren | e1f8694 | 2018-06-30 18:25:00 -0700 | [diff] [blame] | 2537 | do_diff_cache(&cmp, &opt); |
Elijah Newren | cffbfad | 2018-06-30 18:24:55 -0700 | [diff] [blame] | 2538 | 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 Stolee | 0c18c05 | 2021-04-01 01:49:58 +0000 | [diff] [blame] | 2547 | /* TODO: audit for interaction with sparse-index. */ |
| 2548 | ensure_full_index(istate); |
Elijah Newren | 1b9fbef | 2018-06-30 18:24:56 -0700 | [diff] [blame] | 2549 | for (i = 0; sb && i < istate->cache_nr; i++) { |
Elijah Newren | cffbfad | 2018-06-30 18:24:55 -0700 | [diff] [blame] | 2550 | if (i) |
| 2551 | strbuf_addch(sb, ' '); |
Elijah Newren | 1b9fbef | 2018-06-30 18:24:56 -0700 | [diff] [blame] | 2552 | strbuf_addstr(sb, istate->cache[i]->name); |
Elijah Newren | cffbfad | 2018-06-30 18:24:55 -0700 | [diff] [blame] | 2553 | } |
Elijah Newren | 1b9fbef | 2018-06-30 18:24:56 -0700 | [diff] [blame] | 2554 | return !!istate->cache_nr; |
Elijah Newren | cffbfad | 2018-06-30 18:24:55 -0700 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2558 | static int write_index_ext_header(struct hashfile *f, |
| 2559 | git_hash_ctx *eoie_f, |
| 2560 | unsigned int ext, |
| 2561 | unsigned int sz) |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 2562 | { |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2563 | 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 Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 2571 | } |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2572 | return 0; |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 2573 | } |
| 2574 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 2575 | static void ce_smudge_racily_clean_entry(struct index_state *istate, |
| 2576 | struct cache_entry *ce) |
Junio C Hamano | 407c8eb | 2005-12-20 12:12:18 -0800 | [diff] [blame] | 2577 | { |
| 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 Haggerty | c21d39d | 2013-06-20 10:37:50 +0200 | [diff] [blame] | 2582 | * ce_stat_data.sd_mtime match the index file mtime. |
Junio C Hamano | c70115b | 2008-07-29 01:13:44 -0700 | [diff] [blame] | 2583 | * |
| 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 Hamano | 407c8eb | 2005-12-20 12:12:18 -0800 | [diff] [blame] | 2588 | */ |
| 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 Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 2595 | if (ce_modified_check_fs(istate, ce, &st)) { |
Junio C Hamano | 4b3511b | 2005-12-20 14:18:47 -0800 | [diff] [blame] | 2596 | /* 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 Hamano | b7e58b1 | 2006-08-05 04:16:02 -0700 | [diff] [blame] | 2607 | * but it does not. When the second update-index runs, |
Junio C Hamano | 4b3511b | 2005-12-20 14:18:47 -0800 | [diff] [blame] | 2608 | * 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 Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 2613 | * obtain from the filesystem next time we stat("frotz"). |
Junio C Hamano | 4b3511b | 2005-12-20 14:18:47 -0800 | [diff] [blame] | 2614 | * |
| 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 Haggerty | c21d39d | 2013-06-20 10:37:50 +0200 | [diff] [blame] | 2621 | ce->ce_stat_data.sd_size = 0; |
Junio C Hamano | 407c8eb | 2005-12-20 12:12:18 -0800 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | |
Junio C Hamano | f136f7b | 2012-04-03 15:53:14 -0700 | [diff] [blame] | 2625 | /* Copy miscellaneous fields but not the name */ |
Kevin Willford | ce012de | 2017-08-21 15:24:32 -0600 | [diff] [blame] | 2626 | static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk, |
Junio C Hamano | f136f7b | 2012-04-03 15:53:14 -0700 | [diff] [blame] | 2627 | struct cache_entry *ce) |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2628 | { |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 2629 | short flags; |
brian m. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 2630 | const unsigned hashsz = the_hash_algo->rawsz; |
| 2631 | uint16_t *flagsp = (uint16_t *)(ondisk->data + hashsz); |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 2632 | |
Michael Haggerty | c21d39d | 2013-06-20 10:37:50 +0200 | [diff] [blame] | 2633 | 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 Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2639 | ondisk->mode = htonl(ce->ce_mode); |
Michael Haggerty | c21d39d | 2013-06-20 10:37:50 +0200 | [diff] [blame] | 2640 | 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. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 2643 | hashcpy(ondisk->data, ce->oid.hash); |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 2644 | |
Nguyễn Thái Ngọc Duy | ce51bf0 | 2014-06-13 19:19:25 +0700 | [diff] [blame] | 2645 | flags = ce->ce_flags & ~CE_NAMEMASK; |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 2646 | flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce)); |
brian m. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 2647 | flagsp[0] = htons(flags); |
Nguyễn Thái Ngọc Duy | 06aaaa0 | 2008-10-01 11:04:01 +0700 | [diff] [blame] | 2648 | if (ce->ce_flags & CE_EXTENDED) { |
brian m. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 2649 | flagsp[1] = htons((ce->ce_flags & CE_EXTENDED_FLAGS) >> 16); |
Junio C Hamano | f136f7b | 2012-04-03 15:53:14 -0700 | [diff] [blame] | 2650 | } |
| 2651 | } |
| 2652 | |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2653 | static int ce_write_entry(struct hashfile *f, struct cache_entry *ce, |
Kevin Willford | ce012de | 2017-08-21 15:24:32 -0600 | [diff] [blame] | 2654 | struct strbuf *previous_name, struct ondisk_cache_entry *ondisk) |
Junio C Hamano | f136f7b | 2012-04-03 15:53:14 -0700 | [diff] [blame] | 2655 | { |
Junio C Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 2656 | int size; |
Ramsay Jones | 00a4b03 | 2018-03-19 17:56:11 +0000 | [diff] [blame] | 2657 | unsigned int saved_namelen; |
| 2658 | int stripped_name = 0; |
Kevin Willford | ce012de | 2017-08-21 15:24:32 -0600 | [diff] [blame] | 2659 | static unsigned char padding[8] = { 0x00 }; |
Junio C Hamano | f136f7b | 2012-04-03 15:53:14 -0700 | [diff] [blame] | 2660 | |
Nguyễn Thái Ngọc Duy | b3c96fb | 2014-06-13 19:19:43 +0700 | [diff] [blame] | 2661 | if (ce->ce_flags & CE_STRIP_NAME) { |
| 2662 | saved_namelen = ce_namelen(ce); |
| 2663 | ce->ce_namelen = 0; |
Ramsay Jones | 00a4b03 | 2018-03-19 17:56:11 +0000 | [diff] [blame] | 2664 | stripped_name = 1; |
Nguyễn Thái Ngọc Duy | b3c96fb | 2014-06-13 19:19:43 +0700 | [diff] [blame] | 2665 | } |
| 2666 | |
brian m. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 2667 | size = offsetof(struct ondisk_cache_entry,data) + ondisk_data_size(ce->ce_flags, 0); |
Kevin Willford | ce012de | 2017-08-21 15:24:32 -0600 | [diff] [blame] | 2668 | |
Junio C Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 2669 | if (!previous_name) { |
Kevin Willford | ce012de | 2017-08-21 15:24:32 -0600 | [diff] [blame] | 2670 | int len = ce_namelen(ce); |
| 2671 | copy_cache_entry_to_ondisk(ondisk, ce); |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2672 | hashwrite(f, ondisk, size); |
| 2673 | hashwrite(f, ce->name, len); |
| 2674 | hashwrite(f, padding, align_padding_size(size, len)); |
Junio C Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 2675 | } 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 Willford | ce012de | 2017-08-21 15:24:32 -0600 | [diff] [blame] | 2687 | copy_cache_entry_to_ondisk(ondisk, ce); |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2688 | 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 Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 2692 | |
| 2693 | strbuf_splice(previous_name, common, to_remove, |
| 2694 | ce->name + common, ce_namelen(ce) - common); |
| 2695 | } |
Ramsay Jones | 00a4b03 | 2018-03-19 17:56:11 +0000 | [diff] [blame] | 2696 | if (stripped_name) { |
Nguyễn Thái Ngọc Duy | b3c96fb | 2014-06-13 19:19:43 +0700 | [diff] [blame] | 2697 | ce->ce_namelen = saved_namelen; |
| 2698 | ce->ce_flags &= ~CE_STRIP_NAME; |
| 2699 | } |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2700 | |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2701 | return 0; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2702 | } |
| 2703 | |
Yiannis Marangos | 426ddee | 2014-04-10 21:31:21 +0300 | [diff] [blame] | 2704 | /* |
| 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 | */ |
| 2708 | static 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. carlson | aab6135 | 2018-02-01 02:18:45 +0000 | [diff] [blame] | 2713 | unsigned char hash[GIT_MAX_RAWSZ]; |
Yiannis Marangos | 426ddee | 2014-04-10 21:31:21 +0300 | [diff] [blame] | 2714 | |
| 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. carlson | aab6135 | 2018-02-01 02:18:45 +0000 | [diff] [blame] | 2725 | if (st.st_size < sizeof(struct cache_header) + the_hash_algo->rawsz) |
Yiannis Marangos | 426ddee | 2014-04-10 21:31:21 +0300 | [diff] [blame] | 2726 | goto out; |
| 2727 | |
brian m. carlson | aab6135 | 2018-02-01 02:18:45 +0000 | [diff] [blame] | 2728 | 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 Marangos | 426ddee | 2014-04-10 21:31:21 +0300 | [diff] [blame] | 2730 | goto out; |
| 2731 | |
Jeff King | 67947c3 | 2018-08-28 17:22:52 -0400 | [diff] [blame] | 2732 | if (!hasheq(istate->oid.hash, hash)) |
Yiannis Marangos | 426ddee | 2014-04-10 21:31:21 +0300 | [diff] [blame] | 2733 | goto out; |
| 2734 | |
| 2735 | close(fd); |
| 2736 | return 1; |
| 2737 | |
| 2738 | out: |
| 2739 | close(fd); |
| 2740 | return 0; |
| 2741 | } |
| 2742 | |
Nguyễn Thái Ngọc Duy | 1b0d968 | 2019-01-12 09:13:27 +0700 | [diff] [blame] | 2743 | static int repo_verify_index(struct repository *repo) |
Yiannis Marangos | 426ddee | 2014-04-10 21:31:21 +0300 | [diff] [blame] | 2744 | { |
Nguyễn Thái Ngọc Duy | 1b0d968 | 2019-01-12 09:13:27 +0700 | [diff] [blame] | 2745 | return verify_index_from(repo->index, repo->index_file); |
Yiannis Marangos | 426ddee | 2014-04-10 21:31:21 +0300 | [diff] [blame] | 2746 | } |
| 2747 | |
Marc Strapetz | 2ede073 | 2022-01-07 11:17:31 +0000 | [diff] [blame] | 2748 | int has_racy_timestamp(struct index_state *istate) |
Junio C Hamano | 483fbe2 | 2011-03-21 10:18:19 -0700 | [diff] [blame] | 2749 | { |
| 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 Duy | 1b0d968 | 2019-01-12 09:13:27 +0700 | [diff] [blame] | 2761 | void repo_update_index_if_able(struct repository *repo, |
| 2762 | struct lock_file *lockfile) |
Junio C Hamano | ccdc4ec | 2011-03-21 10:16:10 -0700 | [diff] [blame] | 2763 | { |
Nguyễn Thái Ngọc Duy | 1b0d968 | 2019-01-12 09:13:27 +0700 | [diff] [blame] | 2764 | 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 Ågren | b74c90f | 2017-10-06 22:12:14 +0200 | [diff] [blame] | 2768 | else |
| 2769 | rollback_lock_file(lockfile); |
Junio C Hamano | ccdc4ec | 2011-03-21 10:16:10 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
Jonathan Nieder | d846550 | 2018-11-19 22:11:47 -0800 | [diff] [blame] | 2772 | static int record_eoie(void) |
| 2773 | { |
| 2774 | int val; |
| 2775 | |
| 2776 | if (!git_config_get_bool("index.recordendofindexentries", &val)) |
| 2777 | return val; |
Jonathan Nieder | 2a9dede | 2018-11-19 22:14:26 -0800 | [diff] [blame] | 2778 | |
| 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 Nieder | d846550 | 2018-11-19 22:11:47 -0800 | [diff] [blame] | 2785 | } |
| 2786 | |
Jonathan Nieder | 4291605 | 2018-11-19 22:12:22 -0800 | [diff] [blame] | 2787 | static int record_ieot(void) |
| 2788 | { |
| 2789 | int val; |
| 2790 | |
| 2791 | if (!git_config_get_bool("index.recordoffsettable", &val)) |
| 2792 | return val; |
Jonathan Nieder | 2a9dede | 2018-11-19 22:14:26 -0800 | [diff] [blame] | 2793 | |
| 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 Nieder | 4291605 | 2018-11-19 22:12:22 -0800 | [diff] [blame] | 2800 | } |
| 2801 | |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 2802 | enum 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 Ågren | 812d6b0 | 2017-10-06 22:12:12 +0200 | [diff] [blame] | 2812 | /* |
| 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 Hostetler | 9f41c7a | 2017-04-26 22:05:23 +0200 | [diff] [blame] | 2819 | static int do_write_index(struct index_state *istate, struct tempfile *tempfile, |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 2820 | enum write_extensions write_extensions, unsigned flags) |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 2821 | { |
Nguyễn Thái Ngọc Duy | ca54d9b | 2018-01-27 19:27:56 +0700 | [diff] [blame] | 2822 | uint64_t start = getnanotime(); |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2823 | struct hashfile *f; |
| 2824 | git_hash_ctx *eoie_c = NULL; |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 2825 | struct cache_header hdr; |
Kevin Willford | b50386c | 2017-08-21 15:24:31 -0600 | [diff] [blame] | 2826 | int i, err = 0, removed, extended, hdr_version; |
Junio C Hamano | 4aab5b4 | 2007-04-01 23:26:07 -0700 | [diff] [blame] | 2827 | struct cache_entry **cache = istate->cache; |
| 2828 | int entries = istate->cache_nr; |
Kjetil Barvik | e1afca4 | 2009-02-23 19:02:57 +0100 | [diff] [blame] | 2829 | struct stat st; |
brian m. carlson | 575fa8a | 2019-02-19 00:05:24 +0000 | [diff] [blame] | 2830 | struct ondisk_cache_entry ondisk; |
Junio C Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 2831 | struct strbuf previous_name_buf = STRBUF_INIT, *previous_name; |
Thomas Gummerer | 4bddd98 | 2018-01-07 22:30:14 +0000 | [diff] [blame] | 2832 | int drop_cache_tree = istate->drop_cache_tree; |
Ben Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 2833 | off_t offset; |
Neeraj Singh | ba95e96 | 2022-03-10 22:43:23 +0000 | [diff] [blame] | 2834 | int csum_fsync_flag; |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2835 | int ieot_entries = 1; |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2836 | struct index_entry_offset_table *ieot = NULL; |
| 2837 | int nr, nr_threads; |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 2838 | struct repository *r = istate->repo; |
Junio C Hamano | 025a070 | 2005-06-10 01:32:37 -0700 | [diff] [blame] | 2839 | |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2840 | f = hashfd(tempfile->fd, tempfile->filename.buf); |
| 2841 | |
Derrick Stolee | 17194b1 | 2023-01-06 16:31:56 +0000 | [diff] [blame] | 2842 | prepare_repo_settings(r); |
| 2843 | f->skip_hash = r->settings.index_skip_hash; |
Derrick Stolee | ee1f0c2 | 2023-01-06 16:31:54 +0000 | [diff] [blame] | 2844 | |
Nguyễn Thái Ngọc Duy | 06aaaa0 | 2008-10-01 11:04:01 +0700 | [diff] [blame] | 2845 | for (i = removed = extended = 0; i < entries; i++) { |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2846 | if (cache[i]->ce_flags & CE_REMOVE) |
Junio C Hamano | 025a070 | 2005-06-10 01:32:37 -0700 | [diff] [blame] | 2847 | removed++; |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 2848 | |
Nguyễn Thái Ngọc Duy | 06aaaa0 | 2008-10-01 11:04:01 +0700 | [diff] [blame] | 2849 | /* 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ábor | e8ffd03 | 2021-08-26 23:00:04 +0200 | [diff] [blame] | 2857 | if (!istate->version) |
Ævar Arnfjörð Bjarmason | 4a93b89 | 2023-03-28 15:58:58 +0200 | [diff] [blame] | 2858 | istate->version = get_index_format_default(r); |
Junio C Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 2859 | |
| 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 Torvalds | ccc4feb | 2005-04-15 10:44:27 -0700 | [diff] [blame] | 2866 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
Junio C Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 2867 | hdr.hdr_version = htonl(hdr_version); |
Junio C Hamano | 025a070 | 2005-06-10 01:32:37 -0700 | [diff] [blame] | 2868 | hdr.hdr_entries = htonl(entries - removed); |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 2869 | |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2870 | hashwrite(f, &hdr, sizeof(hdr)); |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 2871 | |
Jonathan Nieder | 2a9dede | 2018-11-19 22:14:26 -0800 | [diff] [blame] | 2872 | if (!HAVE_THREADS || git_config_get_index_threads(&nr_threads)) |
Nguyễn Thái Ngọc Duy | 88168b9 | 2018-11-03 09:48:48 +0100 | [diff] [blame] | 2873 | nr_threads = 1; |
Nguyễn Thái Ngọc Duy | 62e5ee8 | 2018-11-03 09:48:47 +0100 | [diff] [blame] | 2874 | |
Jonathan Nieder | 4291605 | 2018-11-19 22:12:22 -0800 | [diff] [blame] | 2875 | if (nr_threads != 1 && record_ieot()) { |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2876 | 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 Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2890 | if (ieot_blocks > istate->cache_nr) |
| 2891 | ieot_blocks = istate->cache_nr; |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2892 | } |
| 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 Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2901 | ieot_entries = DIV_ROUND_UP(entries, ieot_blocks); |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2902 | } |
| 2903 | } |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2904 | |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2905 | offset = hashfile_total(f); |
| 2906 | |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2907 | nr = 0; |
Junio C Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 2908 | previous_name = (hdr_version == 4) ? &previous_name_buf : NULL; |
Kevin Willford | ce012de | 2017-08-21 15:24:32 -0600 | [diff] [blame] | 2909 | |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 2910 | for (i = 0; i < entries; i++) { |
| 2911 | struct cache_entry *ce = cache[i]; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2912 | if (ce->ce_flags & CE_REMOVE) |
Linus Torvalds | aa16021 | 2005-06-09 15:34:04 -0700 | [diff] [blame] | 2913 | continue; |
Junio C Hamano | e06c43c | 2008-03-30 09:25:52 -0700 | [diff] [blame] | 2914 | if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce)) |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 2915 | ce_smudge_racily_clean_entry(istate, ce); |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 2916 | if (is_null_oid(&ce->oid)) { |
Jeff King | 83bd743 | 2013-08-27 16:41:12 -0400 | [diff] [blame] | 2917 | 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 Willford | b50386c | 2017-08-21 15:24:31 -0600 | [diff] [blame] | 2925 | err = error(msg, ce->name); |
Jeff King | a96d3cc | 2017-04-21 14:46:17 -0400 | [diff] [blame] | 2926 | |
| 2927 | drop_cache_tree = 1; |
Jeff King | 83bd743 | 2013-08-27 16:41:12 -0400 | [diff] [blame] | 2928 | } |
Ben Peart | 77ff112 | 2018-10-10 11:59:38 -0400 | [diff] [blame] | 2929 | if (ieot && i && (i % ieot_entries == 0)) { |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2930 | 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 Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2941 | |
| 2942 | offset = hashfile_total(f); |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2943 | } |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2944 | if (ce_write_entry(f, ce, previous_name, (struct ondisk_cache_entry *)&ondisk) < 0) |
Kevin Willford | b50386c | 2017-08-21 15:24:31 -0600 | [diff] [blame] | 2945 | err = -1; |
| 2946 | |
| 2947 | if (err) |
| 2948 | break; |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2949 | nr++; |
| 2950 | } |
| 2951 | if (ieot && nr) { |
| 2952 | ieot->entries[ieot->nr].nr = nr; |
| 2953 | ieot->entries[ieot->nr].offset = offset; |
| 2954 | ieot->nr++; |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 2955 | } |
Junio C Hamano | 9d22778 | 2012-04-04 09:12:43 -0700 | [diff] [blame] | 2956 | strbuf_release(&previous_name_buf); |
Junio C Hamano | 1af1c2b | 2006-04-23 16:52:08 -0700 | [diff] [blame] | 2957 | |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2958 | if (err) { |
| 2959 | free(ieot); |
Kevin Willford | b50386c | 2017-08-21 15:24:31 -0600 | [diff] [blame] | 2960 | return err; |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2961 | } |
Kevin Willford | b50386c | 2017-08-21 15:24:31 -0600 | [diff] [blame] | 2962 | |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2963 | 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 Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2972 | } |
Ben Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 2973 | |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2974 | /* |
| 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 Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2981 | if (ieot) { |
| 2982 | struct strbuf sb = STRBUF_INIT; |
| 2983 | |
| 2984 | write_ieot_extension(&sb, ieot); |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 2985 | err = write_index_ext_header(f, eoie_c, CACHE_EXT_INDEXENTRYOFFSETTABLE, sb.len) < 0; |
| 2986 | hashwrite(f, sb.buf, sb.len); |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2987 | strbuf_release(&sb); |
| 2988 | free(ieot); |
| 2989 | if (err) |
| 2990 | return -1; |
| 2991 | } |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 2992 | |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 2993 | if (write_extensions & WRITE_SPLIT_INDEX_EXTENSION && |
| 2994 | istate->split_index) { |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 2995 | struct strbuf sb = STRBUF_INIT; |
| 2996 | |
Johannes Schindelin | 451b66c | 2022-01-19 17:29:39 +0000 | [diff] [blame] | 2997 | if (istate->sparse_index) |
| 2998 | die(_("cannot write split index for a sparse index")); |
| 2999 | |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 3000 | err = write_link_extension(&sb, istate) < 0 || |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 3001 | 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 Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 3004 | strbuf_release(&sb); |
| 3005 | if (err) |
| 3006 | return -1; |
| 3007 | } |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 3008 | if (write_extensions & WRITE_CACHE_TREE_EXTENSION && |
| 3009 | !drop_cache_tree && istate->cache_tree) { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 3010 | struct strbuf sb = STRBUF_INIT; |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 3011 | |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 3012 | cache_tree_write(&sb, istate->cache_tree); |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 3013 | err = write_index_ext_header(f, eoie_c, CACHE_EXT_TREE, sb.len) < 0; |
| 3014 | hashwrite(f, sb.buf, sb.len); |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 3015 | strbuf_release(&sb); |
| 3016 | if (err) |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 3017 | return -1; |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 3018 | } |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 3019 | if (write_extensions & WRITE_RESOLVE_UNDO_EXTENSION && |
| 3020 | istate->resolve_undo) { |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 3021 | struct strbuf sb = STRBUF_INIT; |
| 3022 | |
| 3023 | resolve_undo_write(&sb, istate->resolve_undo); |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 3024 | 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 Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 3027 | strbuf_release(&sb); |
| 3028 | if (err) |
| 3029 | return -1; |
| 3030 | } |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 3031 | if (write_extensions & WRITE_UNTRACKED_CACHE_EXTENSION && |
| 3032 | istate->untracked) { |
Nguyễn Thái Ngọc Duy | 83c094a | 2015-03-08 17:12:33 +0700 | [diff] [blame] | 3033 | struct strbuf sb = STRBUF_INIT; |
| 3034 | |
| 3035 | write_untracked_extension(&sb, istate->untracked); |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 3036 | 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 Duy | 83c094a | 2015-03-08 17:12:33 +0700 | [diff] [blame] | 3039 | strbuf_release(&sb); |
| 3040 | if (err) |
| 3041 | return -1; |
| 3042 | } |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 3043 | if (write_extensions & WRITE_FSMONITOR_EXTENSION && |
| 3044 | istate->fsmonitor_last_update) { |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 3045 | struct strbuf sb = STRBUF_INIT; |
| 3046 | |
| 3047 | write_fsmonitor_extension(&sb, istate); |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 3048 | err = write_index_ext_header(f, eoie_c, CACHE_EXT_FSMONITOR, sb.len) < 0; |
| 3049 | hashwrite(f, sb.buf, sb.len); |
Ben Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 3050 | strbuf_release(&sb); |
| 3051 | if (err) |
| 3052 | return -1; |
| 3053 | } |
Derrick Stolee | cd42415 | 2021-03-30 13:10:54 +0000 | [diff] [blame] | 3054 | if (istate->sparse_index) { |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 3055 | if (write_index_ext_header(f, eoie_c, CACHE_EXT_SPARSE_DIRECTORIES, 0) < 0) |
Derrick Stolee | cd42415 | 2021-03-30 13:10:54 +0000 | [diff] [blame] | 3056 | return -1; |
| 3057 | } |
Ben Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 3058 | |
| 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 Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 3065 | if (eoie_c) { |
Ben Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 3066 | struct strbuf sb = STRBUF_INIT; |
| 3067 | |
Derrick Stolee | 410334e | 2021-05-18 18:32:47 +0000 | [diff] [blame] | 3068 | 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 Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 3071 | strbuf_release(&sb); |
| 3072 | if (err) |
| 3073 | return -1; |
| 3074 | } |
Kjetil Barvik | e1afca4 | 2009-02-23 19:02:57 +0100 | [diff] [blame] | 3075 | |
Neeraj Singh | ba95e96 | 2022-03-10 22:43:23 +0000 | [diff] [blame] | 3076 | 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 King | 49bd0fc | 2017-09-05 08:14:30 -0400 | [diff] [blame] | 3083 | if (close_tempfile_gently(tempfile)) { |
Martin Ågren | 6a8c89d | 2021-01-05 20:23:50 +0100 | [diff] [blame] | 3084 | error(_("could not close '%s'"), get_tempfile_path(tempfile)); |
Jeff King | 49bd0fc | 2017-09-05 08:14:30 -0400 | [diff] [blame] | 3085 | return -1; |
| 3086 | } |
Martin Ågren | 6a8c89d | 2021-01-05 20:23:50 +0100 | [diff] [blame] | 3087 | if (stat(get_tempfile_path(tempfile), &st)) |
Kjetil Barvik | e1afca4 | 2009-02-23 19:02:57 +0100 | [diff] [blame] | 3088 | return -1; |
Kjetil Barvik | 5bcf109 | 2009-03-15 12:38:55 +0100 | [diff] [blame] | 3089 | istate->timestamp.sec = (unsigned int)st.st_mtime; |
| 3090 | istate->timestamp.nsec = ST_MTIME_NSEC(st); |
Nguyễn Thái Ngọc Duy | ca54d9b | 2018-01-27 19:27:56 +0700 | [diff] [blame] | 3091 | trace_performance_since(start, "write index, changed mask = %x", istate->cache_changed); |
Jeff Hostetler | 42fee7a | 2019-02-22 14:25:07 -0800 | [diff] [blame] | 3092 | |
| 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 Barvik | e1afca4 | 2009-02-23 19:02:57 +0100 | [diff] [blame] | 3102 | return 0; |
Linus Torvalds | 197ee8c | 2005-04-09 12:09:27 -0700 | [diff] [blame] | 3103 | } |
Miklos Vajna | e46bbcf | 2008-06-27 18:21:58 +0200 | [diff] [blame] | 3104 | |
Nguyễn Thái Ngọc Duy | 626f35c | 2014-06-13 19:19:24 +0700 | [diff] [blame] | 3105 | void set_alternate_index_output(const char *name) |
| 3106 | { |
| 3107 | alternate_index_output = name; |
| 3108 | } |
| 3109 | |
| 3110 | static int commit_locked_index(struct lock_file *lk) |
| 3111 | { |
Michael Haggerty | 751bace | 2014-10-01 12:28:36 +0200 | [diff] [blame] | 3112 | if (alternate_index_output) |
| 3113 | return commit_lock_file_to(lk, alternate_index_output); |
| 3114 | else |
Nguyễn Thái Ngọc Duy | 626f35c | 2014-06-13 19:19:24 +0700 | [diff] [blame] | 3115 | return commit_lock_file(lk); |
Nguyễn Thái Ngọc Duy | 626f35c | 2014-06-13 19:19:24 +0700 | [diff] [blame] | 3116 | } |
| 3117 | |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 3118 | static 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 Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 3122 | { |
Jeff Hostetler | 42fee7a | 2019-02-22 14:25:07 -0800 | [diff] [blame] | 3123 | int ret; |
Derrick Stolee | 9fadb37 | 2022-05-23 13:48:40 +0000 | [diff] [blame] | 3124 | int was_full = istate->sparse_index == INDEX_EXPANDED; |
Derrick Stolee | 6e77352 | 2021-03-30 13:10:55 +0000 | [diff] [blame] | 3125 | |
Derrick Stolee | ce7a9f0 | 2021-09-08 01:42:32 +0000 | [diff] [blame] | 3126 | ret = convert_to_sparse(istate, 0); |
Derrick Stolee | 6e77352 | 2021-03-30 13:10:55 +0000 | [diff] [blame] | 3127 | |
| 3128 | if (ret) { |
| 3129 | warning(_("failed to convert to a sparse-index")); |
| 3130 | return ret; |
| 3131 | } |
Jeff Hostetler | 42fee7a | 2019-02-22 14:25:07 -0800 | [diff] [blame] | 3132 | |
| 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 Ågren | 6a8c89d | 2021-01-05 20:23:50 +0100 | [diff] [blame] | 3138 | "%s", get_lock_file_path(lock)); |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 3139 | ret = do_write_index(istate, lock->tempfile, write_extensions, flags); |
Jeff Hostetler | 42fee7a | 2019-02-22 14:25:07 -0800 | [diff] [blame] | 3140 | trace2_region_leave_printf("index", "do_write_index", the_repository, |
Martin Ågren | 6a8c89d | 2021-01-05 20:23:50 +0100 | [diff] [blame] | 3141 | "%s", get_lock_file_path(lock)); |
Jeff Hostetler | 42fee7a | 2019-02-22 14:25:07 -0800 | [diff] [blame] | 3142 | |
Derrick Stolee | 6e77352 | 2021-03-30 13:10:55 +0000 | [diff] [blame] | 3143 | if (was_full) |
| 3144 | ensure_full_index(istate); |
| 3145 | |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 3146 | if (ret) |
| 3147 | return ret; |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 3148 | if (flags & COMMIT_LOCK) |
Ben Peart | 1956ecd | 2019-02-15 12:59:21 -0500 | [diff] [blame] | 3149 | ret = commit_locked_index(lock); |
| 3150 | else |
| 3151 | ret = close_lock_file_gently(lock); |
| 3152 | |
Emily Shaffer | dbb1c61 | 2021-12-22 04:59:41 +0100 | [diff] [blame] | 3153 | run_hooks_l("post-index-change", |
Ben Peart | 1956ecd | 2019-02-15 12:59:21 -0500 | [diff] [blame] | 3154 | 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 Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 3160 | } |
| 3161 | |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 3162 | static 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 Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 3168 | ret = do_write_locked_index(istate, lock, flags, WRITE_ALL_EXTENSIONS); |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 3169 | finish_writing_split_index(istate); |
| 3170 | return ret; |
| 3171 | } |
| 3172 | |
Christian Couder | b968372 | 2017-03-06 10:41:58 +0100 | [diff] [blame] | 3173 | static const char *shared_index_expire = "2.weeks.ago"; |
| 3174 | |
| 3175 | static 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 | |
| 3190 | static 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 Krefting | 78bde92 | 2017-04-30 23:32:48 +0200 | [diff] [blame] | 3200 | return error_errno(_("could not stat '%s'"), shared_index_path); |
Christian Couder | b968372 | 2017-03-06 10:41:58 +0100 | [diff] [blame] | 3201 | if (st.st_mtime > expiration) |
| 3202 | return 0; |
| 3203 | |
| 3204 | return 1; |
| 3205 | } |
| 3206 | |
| 3207 | static 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 Duy | a0a9675 | 2014-06-13 19:19:45 +0700 | [diff] [blame] | 3232 | static int write_shared_index(struct index_state *istate, |
Neeraj Singh | ba95e96 | 2022-03-10 22:43:23 +0000 | [diff] [blame] | 3233 | struct tempfile **temp, unsigned flags) |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 3234 | { |
| 3235 | struct split_index *si = istate->split_index; |
Derrick Stolee | 6e77352 | 2021-03-30 13:10:55 +0000 | [diff] [blame] | 3236 | int ret, was_full = !istate->sparse_index; |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 3237 | |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 3238 | move_cache_to_base_index(istate); |
Derrick Stolee | ce7a9f0 | 2021-09-08 01:42:32 +0000 | [diff] [blame] | 3239 | convert_to_sparse(istate, 0); |
Jeff Hostetler | 42fee7a | 2019-02-22 14:25:07 -0800 | [diff] [blame] | 3240 | |
| 3241 | trace2_region_enter_printf("index", "shared/do_write_index", |
Martin Ågren | 6a8c89d | 2021-01-05 20:23:50 +0100 | [diff] [blame] | 3242 | the_repository, "%s", get_tempfile_path(*temp)); |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 3243 | ret = do_write_index(si->base, *temp, WRITE_NO_EXTENSION, flags); |
Ævar Arnfjörð Bjarmason | c173542 | 2019-05-10 15:37:38 +0200 | [diff] [blame] | 3244 | trace2_region_leave_printf("index", "shared/do_write_index", |
Martin Ågren | 6a8c89d | 2021-01-05 20:23:50 +0100 | [diff] [blame] | 3245 | the_repository, "%s", get_tempfile_path(*temp)); |
Jeff Hostetler | 42fee7a | 2019-02-22 14:25:07 -0800 | [diff] [blame] | 3246 | |
Derrick Stolee | 6e77352 | 2021-03-30 13:10:55 +0000 | [diff] [blame] | 3247 | if (was_full) |
| 3248 | ensure_full_index(istate); |
| 3249 | |
Nguyễn Thái Ngọc Duy | 59f9d2d | 2018-01-14 17:18:19 +0700 | [diff] [blame] | 3250 | if (ret) |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 3251 | return ret; |
Nguyễn Thái Ngọc Duy | 7db2d08 | 2018-01-14 17:18:18 +0700 | [diff] [blame] | 3252 | ret = adjust_shared_perm(get_tempfile_path(*temp)); |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 3253 | if (ret) { |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 3254 | error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp)); |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 3255 | return ret; |
| 3256 | } |
Nguyễn Thái Ngọc Duy | 7db2d08 | 2018-01-14 17:18:18 +0700 | [diff] [blame] | 3257 | ret = rename_tempfile(temp, |
brian m. carlson | 75691ea | 2018-05-02 00:25:44 +0000 | [diff] [blame] | 3258 | git_path("sharedindex.%s", oid_to_hex(&si->base->oid))); |
Christian Couder | b968372 | 2017-03-06 10:41:58 +0100 | [diff] [blame] | 3259 | if (!ret) { |
brian m. carlson | 75691ea | 2018-05-02 00:25:44 +0000 | [diff] [blame] | 3260 | oidcpy(&si->base_oid, &si->base->oid); |
| 3261 | clean_shared_index_files(oid_to_hex(&si->base->oid)); |
Christian Couder | b968372 | 2017-03-06 10:41:58 +0100 | [diff] [blame] | 3262 | } |
| 3263 | |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 3264 | return ret; |
| 3265 | } |
| 3266 | |
Christian Couder | e6a1dd7 | 2017-02-27 19:00:08 +0100 | [diff] [blame] | 3267 | static const int default_max_percent_split_change = 20; |
| 3268 | |
| 3269 | static 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 Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 3297 | int write_locked_index(struct index_state *istate, struct lock_file *lock, |
| 3298 | unsigned flags) |
| 3299 | { |
SZEDER Gábor | e8ffd03 | 2021-08-26 23:00:04 +0200 | [diff] [blame] | 3300 | int new_shared_index, ret, test_split_index_env; |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 3301 | struct split_index *si = istate->split_index; |
| 3302 | |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 3303 | if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0)) |
Nguyễn Thái Ngọc Duy | c207e9e | 2018-11-10 06:49:02 +0100 | [diff] [blame] | 3304 | cache_tree_verify(the_repository, istate); |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 3305 | |
Martin Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 3306 | if ((flags & SKIP_IF_UNCHANGED) && !istate->cache_changed) { |
| 3307 | if (flags & COMMIT_LOCK) |
| 3308 | rollback_lock_file(lock); |
| 3309 | return 0; |
| 3310 | } |
| 3311 | |
Alex Vandiver | 3bd28eb | 2017-11-09 11:58:10 -0800 | [diff] [blame] | 3312 | if (istate->fsmonitor_last_update) |
| 3313 | fill_fsmonitor_bitmap(istate); |
| 3314 | |
SZEDER Gábor | e8ffd03 | 2021-08-26 23:00:04 +0200 | [diff] [blame] | 3315 | 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 Duy | 5165dd5 | 2014-06-13 19:19:47 +0700 | [diff] [blame] | 3319 | (istate->cache_changed & ~EXTMASK)) { |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 3320 | ret = do_write_locked_index(istate, lock, flags, |
| 3321 | ~WRITE_SPLIT_INDEX_EXTENSION); |
Martin Ågren | df60cf5 | 2017-10-06 22:12:13 +0200 | [diff] [blame] | 3322 | goto out; |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 3323 | } |
| 3324 | |
SZEDER Gábor | e8ffd03 | 2021-08-26 23:00:04 +0200 | [diff] [blame] | 3325 | if (test_split_index_env) { |
| 3326 | if (!si) { |
| 3327 | si = init_split_index(istate); |
Nguyễn Thái Ngọc Duy | d6e3c18 | 2014-06-13 19:19:49 +0700 | [diff] [blame] | 3328 | istate->cache_changed |= SPLIT_INDEX_ORDERED; |
SZEDER Gábor | e8ffd03 | 2021-08-26 23:00:04 +0200 | [diff] [blame] | 3329 | } 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 Duy | d6e3c18 | 2014-06-13 19:19:49 +0700 | [diff] [blame] | 3334 | } |
Christian Couder | e6a1dd7 | 2017-02-27 19:00:08 +0100 | [diff] [blame] | 3335 | if (too_many_not_shared_entries(istate)) |
| 3336 | istate->cache_changed |= SPLIT_INDEX_ORDERED; |
Christian Couder | 0d59ffb | 2017-02-27 19:00:12 +0100 | [diff] [blame] | 3337 | |
| 3338 | new_shared_index = istate->cache_changed & SPLIT_INDEX_ORDERED; |
| 3339 | |
| 3340 | if (new_shared_index) { |
Nguyễn Thái Ngọc Duy | 59f9d2d | 2018-01-14 17:18:19 +0700 | [diff] [blame] | 3341 | struct tempfile *temp; |
| 3342 | int saved_errno; |
| 3343 | |
Ævar Arnfjörð Bjarmason | c9d6c78 | 2018-11-18 19:04:29 +0000 | [diff] [blame] | 3344 | /* 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 Duy | 59f9d2d | 2018-01-14 17:18:19 +0700 | [diff] [blame] | 3346 | if (!temp) { |
Johannes Schindelin | 3b7a447 | 2023-03-26 22:45:41 +0000 | [diff] [blame] | 3347 | ret = do_write_locked_index(istate, lock, flags, |
| 3348 | ~WRITE_SPLIT_INDEX_EXTENSION); |
Nguyễn Thái Ngọc Duy | ef5b3a6 | 2018-01-24 16:38:29 +0700 | [diff] [blame] | 3349 | goto out; |
| 3350 | } |
Neeraj Singh | ba95e96 | 2022-03-10 22:43:23 +0000 | [diff] [blame] | 3351 | ret = write_shared_index(istate, &temp, flags); |
Nguyễn Thái Ngọc Duy | 59f9d2d | 2018-01-14 17:18:19 +0700 | [diff] [blame] | 3352 | |
| 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 Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 3358 | if (ret) |
Martin Ågren | df60cf5 | 2017-10-06 22:12:13 +0200 | [diff] [blame] | 3359 | goto out; |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 3360 | } |
| 3361 | |
Christian Couder | 0d59ffb | 2017-02-27 19:00:12 +0100 | [diff] [blame] | 3362 | 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 Duy | 6e37c8e | 2019-02-13 16:51:29 +0700 | [diff] [blame] | 3365 | if (!ret && !new_shared_index && !is_null_oid(&si->base_oid)) { |
Thomas Gummerer | a125a22 | 2018-01-07 22:30:13 +0000 | [diff] [blame] | 3366 | const char *shared_index = git_path("sharedindex.%s", |
brian m. carlson | 2182abd | 2018-05-02 00:25:43 +0000 | [diff] [blame] | 3367 | oid_to_hex(&si->base_oid)); |
Thomas Gummerer | a125a22 | 2018-01-07 22:30:13 +0000 | [diff] [blame] | 3368 | freshen_shared_index(shared_index, 1); |
| 3369 | } |
Christian Couder | 0d59ffb | 2017-02-27 19:00:12 +0100 | [diff] [blame] | 3370 | |
Martin Ågren | df60cf5 | 2017-10-06 22:12:13 +0200 | [diff] [blame] | 3371 | out: |
| 3372 | if (flags & COMMIT_LOCK) |
| 3373 | rollback_lock_file(lock); |
Christian Couder | 0d59ffb | 2017-02-27 19:00:12 +0100 | [diff] [blame] | 3374 | return ret; |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 3375 | } |
| 3376 | |
Miklos Vajna | e46bbcf | 2008-06-27 18:21:58 +0200 | [diff] [blame] | 3377 | /* |
| 3378 | * Read the index file that is potentially unmerged into given |
Elijah Newren | ad37620 | 2018-07-31 10:12:05 -0700 | [diff] [blame] | 3379 | * 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 Vajna | e46bbcf | 2008-06-27 18:21:58 +0200 | [diff] [blame] | 3386 | */ |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 3387 | int repo_read_index_unmerged(struct repository *repo) |
Miklos Vajna | e46bbcf | 2008-06-27 18:21:58 +0200 | [diff] [blame] | 3388 | { |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 3389 | struct index_state *istate; |
Miklos Vajna | e46bbcf | 2008-06-27 18:21:58 +0200 | [diff] [blame] | 3390 | int i; |
Junio C Hamano | d1a43f2 | 2008-10-15 16:00:06 -0700 | [diff] [blame] | 3391 | int unmerged = 0; |
Miklos Vajna | e46bbcf | 2008-06-27 18:21:58 +0200 | [diff] [blame] | 3392 | |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 3393 | repo_read_index(repo); |
| 3394 | istate = repo->index; |
Miklos Vajna | e46bbcf | 2008-06-27 18:21:58 +0200 | [diff] [blame] | 3395 | for (i = 0; i < istate->cache_nr; i++) { |
| 3396 | struct cache_entry *ce = istate->cache[i]; |
Junio C Hamano | d1a43f2 | 2008-10-15 16:00:06 -0700 | [diff] [blame] | 3397 | struct cache_entry *new_ce; |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 3398 | int len; |
Junio C Hamano | d1a43f2 | 2008-10-15 16:00:06 -0700 | [diff] [blame] | 3399 | |
| 3400 | if (!ce_stage(ce)) |
Miklos Vajna | e46bbcf | 2008-06-27 18:21:58 +0200 | [diff] [blame] | 3401 | continue; |
Junio C Hamano | d1a43f2 | 2008-10-15 16:00:06 -0700 | [diff] [blame] | 3402 | unmerged = 1; |
Thomas Gummerer | 68c4f6a | 2012-07-06 18:07:30 +0200 | [diff] [blame] | 3403 | len = ce_namelen(ce); |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 3404 | new_ce = make_empty_cache_entry(istate, len); |
Junio C Hamano | d1a43f2 | 2008-10-15 16:00:06 -0700 | [diff] [blame] | 3405 | memcpy(new_ce->name, ce->name, len); |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 3406 | new_ce->ce_flags = create_ce_flags(0) | CE_CONFLICTED; |
| 3407 | new_ce->ce_namelen = len; |
Junio C Hamano | d1a43f2 | 2008-10-15 16:00:06 -0700 | [diff] [blame] | 3408 | new_ce->ce_mode = ce->ce_mode; |
Elijah Newren | ad37620 | 2018-07-31 10:12:05 -0700 | [diff] [blame] | 3409 | if (add_index_entry(istate, new_ce, ADD_CACHE_SKIP_DFCHECK)) |
Nguyễn Thái Ngọc Duy | 9d0a9e9 | 2018-11-10 06:16:05 +0100 | [diff] [blame] | 3410 | return error(_("%s: cannot drop to stage #0"), |
Karsten Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 3411 | new_ce->name); |
Miklos Vajna | e46bbcf | 2008-06-27 18:21:58 +0200 | [diff] [blame] | 3412 | } |
Junio C Hamano | d1a43f2 | 2008-10-15 16:00:06 -0700 | [diff] [blame] | 3413 | return unmerged; |
Miklos Vajna | e46bbcf | 2008-06-27 18:21:58 +0200 | [diff] [blame] | 3414 | } |
Junio C Hamano | 041aee3 | 2008-07-21 01:24:17 -0700 | [diff] [blame] | 3415 | |
Jeff King | 98fa473 | 2008-10-16 11:07:26 -0400 | [diff] [blame] | 3416 | /* |
| 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 Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 3425 | int index_name_is_other(struct index_state *istate, const char *name, |
| 3426 | int namelen) |
Jeff King | 98fa473 | 2008-10-16 11:07:26 -0400 | [diff] [blame] | 3427 | { |
| 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 Fleischer | 29fb37b | 2013-04-13 15:28:30 +0200 | [diff] [blame] | 3443 | |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 3444 | void *read_blob_data_from_index(struct index_state *istate, |
Brandon Williams | 8754250 | 2017-01-10 12:06:10 -0800 | [diff] [blame] | 3445 | const char *path, unsigned long *size) |
Lukas Fleischer | 29fb37b | 2013-04-13 15:28:30 +0200 | [diff] [blame] | 3446 | { |
| 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ð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 3469 | data = repo_read_object_file(the_repository, &istate->cache[pos]->oid, |
| 3470 | &type, &sz); |
Lukas Fleischer | 29fb37b | 2013-04-13 15:28:30 +0200 | [diff] [blame] | 3471 | if (!data || type != OBJ_BLOB) { |
| 3472 | free(data); |
| 3473 | return NULL; |
| 3474 | } |
Lukas Fleischer | ff36682 | 2013-04-13 15:28:31 +0200 | [diff] [blame] | 3475 | if (size) |
| 3476 | *size = sz; |
Lukas Fleischer | 29fb37b | 2013-04-13 15:28:30 +0200 | [diff] [blame] | 3477 | return data; |
| 3478 | } |
Michael Haggerty | 3861253 | 2013-06-20 10:37:51 +0200 | [diff] [blame] | 3479 | |
David Turner | edf3b90 | 2017-05-08 11:41:42 +0200 | [diff] [blame] | 3480 | void 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 Duy | 836ef2b | 2018-08-18 16:41:26 +0200 | [diff] [blame] | 3484 | dst->cache_tree = src->cache_tree; |
| 3485 | src->cache_tree = NULL; |
David Turner | edf3b90 | 2017-05-08 11:41:42 +0200 | [diff] [blame] | 3486 | } |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 3487 | |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 3488 | struct 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 Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 3501 | void discard_cache_entry(struct cache_entry *ce) |
| 3502 | { |
Jameson Miller | 8616a2d | 2018-07-02 19:49:39 +0000 | [diff] [blame] | 3503 | if (ce && should_validate_cache_entries()) |
| 3504 | memset(ce, 0xCD, cache_entry_size(ce->ce_namelen)); |
| 3505 | |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 3506 | if (ce && ce->mem_pool_allocated) |
| 3507 | return; |
| 3508 | |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 3509 | free(ce); |
| 3510 | } |
Jameson Miller | 8616a2d | 2018-07-02 19:49:39 +0000 | [diff] [blame] | 3511 | |
| 3512 | int 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 Peart | 3b1d9e0 | 2018-10-10 11:59:34 -0400 | [diff] [blame] | 3525 | |
| 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 | |
| 3529 | static 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 | |
| 3615 | static 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 Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3628 | |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3629 | #define IEOT_VERSION (1) |
| 3630 | |
| 3631 | static 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 Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 3633 | const char *index = NULL; |
| 3634 | uint32_t extsize, ext_version; |
| 3635 | struct index_entry_offset_table *ieot; |
| 3636 | int i, nr; |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3637 | |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 3638 | /* 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 Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3652 | |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 3653 | /* 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 Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3660 | |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 3661 | /* 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 Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3676 | |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 3677 | return ieot; |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot) |
| 3681 | { |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 3682 | uint32_t buffer; |
| 3683 | int i; |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3684 | |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 3685 | /* version */ |
| 3686 | put_be32(&buffer, IEOT_VERSION); |
| 3687 | strbuf_add(sb, &buffer, sizeof(uint32_t)); |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3688 | |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 3689 | /* ieot */ |
| 3690 | for (i = 0; i < ieot->nr; i++) { |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3691 | |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 3692 | /* offset */ |
| 3693 | put_be32(&buffer, ieot->entries[i].offset); |
| 3694 | strbuf_add(sb, &buffer, sizeof(uint32_t)); |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3695 | |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 3696 | /* count */ |
| 3697 | put_be32(&buffer, ieot->entries[i].nr); |
| 3698 | strbuf_add(sb, &buffer, sizeof(uint32_t)); |
| 3699 | } |
Ben Peart | 3255089 | 2018-10-10 11:59:37 -0400 | [diff] [blame] | 3700 | } |
Jonathan Tan | b2896d2 | 2021-07-23 11:52:22 -0700 | [diff] [blame] | 3701 | |
| 3702 | void 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 Newren | 1a40e7b | 2023-05-16 06:33:45 +0000 | [diff] [blame] | 3723 | |
| 3724 | static 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 | |
| 3748 | static 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 | */ |
| 3762 | static 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 | */ |
| 3779 | void 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 Newren | 6cee5eb | 2023-05-16 06:33:47 +0000 | [diff] [blame] | 3860 | |
| 3861 | struct update_callback_data { |
| 3862 | struct index_state *index; |
| 3863 | int include_sparse; |
| 3864 | int flags; |
| 3865 | int add_errors; |
| 3866 | }; |
| 3867 | |
| 3868 | static 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 | |
| 3889 | static 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 | |
| 3926 | int add_files_to_cache(struct repository *repo, const char *prefix, |
Junio C Hamano | 86829f3 | 2024-04-03 23:44:48 +0530 | [diff] [blame] | 3927 | const struct pathspec *pathspec, char *ps_matched, |
| 3928 | int include_sparse, int flags) |
Elijah Newren | 6cee5eb | 2023-05-16 06:33:47 +0000 | [diff] [blame] | 3929 | { |
| 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 Hamano | 86829f3 | 2024-04-03 23:44:48 +0530 | [diff] [blame] | 3940 | if (pathspec) { |
Elijah Newren | 6cee5eb | 2023-05-16 06:33:47 +0000 | [diff] [blame] | 3941 | copy_pathspec(&rev.prune_data, pathspec); |
Junio C Hamano | 86829f3 | 2024-04-03 23:44:48 +0530 | [diff] [blame] | 3942 | rev.ps_matched = ps_matched; |
| 3943 | } |
Elijah Newren | 6cee5eb | 2023-05-16 06:33:47 +0000 | [diff] [blame] | 3944 | 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 | } |