Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 1 | #ifndef REFS_REFS_INTERNAL_H |
| 2 | #define REFS_REFS_INTERNAL_H |
| 3 | |
Ramsay Jones | 4eb4416 | 2018-09-19 01:12:47 +0100 | [diff] [blame] | 4 | #include "refs.h" |
Junio C Hamano | 13f925f | 2018-07-09 14:36:12 -0700 | [diff] [blame] | 5 | #include "iterator.h" |
Beat Bolli | 91c2f20 | 2018-07-09 21:25:33 +0200 | [diff] [blame] | 6 | |
Ramsay Jones | 4eb4416 | 2018-09-19 01:12:47 +0100 | [diff] [blame] | 7 | struct ref_transaction; |
| 8 | |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 9 | /* |
| 10 | * Data structures and functions for the internal use of the refs |
| 11 | * module. Code outside of the refs module should use only the public |
| 12 | * functions defined in "refs.h", and should *not* include this file. |
| 13 | */ |
| 14 | |
| 15 | /* |
Michael Haggerty | 5ac95fe | 2017-11-05 09:42:05 +0100 | [diff] [blame] | 16 | * The following flags can appear in `ref_update::flags`. Their |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 17 | * numerical values must not conflict with those of REF_NO_DEREF and |
Michael Haggerty | 5ac95fe | 2017-11-05 09:42:05 +0100 | [diff] [blame] | 18 | * REF_FORCE_CREATE_REFLOG, which are also stored in |
| 19 | * `ref_update::flags`. |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
Michael Haggerty | 78fb457 | 2017-11-05 09:42:09 +0100 | [diff] [blame] | 23 | * The reference should be updated to new_oid. |
David Turner | d99aa88 | 2016-02-24 17:58:50 -0500 | [diff] [blame] | 24 | */ |
Michael Haggerty | 5ac95fe | 2017-11-05 09:42:05 +0100 | [diff] [blame] | 25 | #define REF_HAVE_NEW (1 << 2) |
David Turner | d99aa88 | 2016-02-24 17:58:50 -0500 | [diff] [blame] | 26 | |
| 27 | /* |
Michael Haggerty | 5ac95fe | 2017-11-05 09:42:05 +0100 | [diff] [blame] | 28 | * The current reference's value should be checked to make sure that |
Michael Haggerty | 78fb457 | 2017-11-05 09:42:09 +0100 | [diff] [blame] | 29 | * it agrees with old_oid. |
Michael Haggerty | 92b1551 | 2016-04-25 15:56:07 +0200 | [diff] [blame] | 30 | */ |
Michael Haggerty | 5ac95fe | 2017-11-05 09:42:05 +0100 | [diff] [blame] | 31 | #define REF_HAVE_OLD (1 << 3) |
Michael Haggerty | 4463977 | 2017-01-06 17:22:43 +0100 | [diff] [blame] | 32 | |
| 33 | /* |
Han-Wen Nienhuys | 63c0567 | 2020-08-28 15:25:33 +0000 | [diff] [blame] | 34 | * Used as a flag in ref_update::flags when we want to log a ref |
| 35 | * update but not actually perform it. This is used when a symbolic |
| 36 | * ref update is split up. |
| 37 | */ |
| 38 | #define REF_LOG_ONLY (1 << 7) |
| 39 | |
| 40 | /* |
Michael Haggerty | 4ff0f01 | 2017-08-21 13:51:34 +0200 | [diff] [blame] | 41 | * Return the length of time to retry acquiring a loose reference lock |
| 42 | * before giving up, in milliseconds: |
| 43 | */ |
| 44 | long get_files_ref_lock_timeout_ms(void); |
| 45 | |
| 46 | /* |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 47 | * Return true iff refname is minimally safe. "Safe" here means that |
| 48 | * deleting a loose reference by this name will not do any damage, for |
| 49 | * example by causing a file that is not a reference to be deleted. |
| 50 | * This function does not check that the reference name is legal; for |
| 51 | * that, use check_refname_format(). |
| 52 | * |
Michael Haggerty | 15ee2c7 | 2017-01-06 17:22:22 +0100 | [diff] [blame] | 53 | * A refname that starts with "refs/" is considered safe iff it |
| 54 | * doesn't contain any "." or ".." components or consecutive '/' |
| 55 | * characters, end with '/', or (on Windows) contain any '\' |
| 56 | * characters. Names that do not start with "refs/" are considered |
| 57 | * safe iff they consist entirely of upper case characters and '_' |
| 58 | * (like "HEAD" and "MERGE_HEAD" but not "config" or "FOO/BAR"). |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 59 | */ |
| 60 | int refname_is_safe(const char *refname); |
| 61 | |
Michael Haggerty | 67be7c5 | 2017-06-23 09:01:37 +0200 | [diff] [blame] | 62 | /* |
| 63 | * Helper function: return true if refname, which has the specified |
| 64 | * oid and flags, can be resolved to an object in the database. If the |
| 65 | * referred-to object does not exist, emit a warning and return false. |
| 66 | */ |
| 67 | int ref_resolves_to_object(const char *refname, |
Jonathan Tan | 9bc45a2 | 2021-10-08 14:08:15 -0700 | [diff] [blame] | 68 | struct repository *repo, |
Michael Haggerty | 67be7c5 | 2017-06-23 09:01:37 +0200 | [diff] [blame] | 69 | const struct object_id *oid, |
| 70 | unsigned int flags); |
| 71 | |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 72 | enum peel_status { |
| 73 | /* object was peeled successfully: */ |
| 74 | PEEL_PEELED = 0, |
| 75 | |
| 76 | /* |
| 77 | * object cannot be peeled because the named object (or an |
| 78 | * object referred to by a tag in the peel chain), does not |
| 79 | * exist. |
| 80 | */ |
| 81 | PEEL_INVALID = -1, |
| 82 | |
| 83 | /* object cannot be peeled because it is not a tag: */ |
| 84 | PEEL_NON_TAG = -2, |
| 85 | |
| 86 | /* ref_entry contains no peeled value because it is a symref: */ |
| 87 | PEEL_IS_SYMREF = -3, |
| 88 | |
| 89 | /* |
| 90 | * ref_entry cannot be peeled because it is broken (i.e., the |
| 91 | * symbolic reference cannot even be resolved to an object |
| 92 | * name): |
| 93 | */ |
| 94 | PEEL_BROKEN = -4 |
| 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * Peel the named object; i.e., if the object is a tag, resolve the |
| 99 | * tag recursively until a non-tag is found. If successful, store the |
brian m. carlson | ac2ed0d | 2017-10-15 22:07:10 +0000 | [diff] [blame] | 100 | * result to oid and return PEEL_PEELED. If the object is not a tag |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 101 | * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively, |
Michael Haggerty | 78fb457 | 2017-11-05 09:42:09 +0100 | [diff] [blame] | 102 | * and leave oid unchanged. |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 103 | */ |
brian m. carlson | ac2ed0d | 2017-10-15 22:07:10 +0000 | [diff] [blame] | 104 | enum peel_status peel_object(const struct object_id *name, struct object_id *oid); |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 105 | |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 106 | /** |
Michael Haggerty | 78fb457 | 2017-11-05 09:42:09 +0100 | [diff] [blame] | 107 | * Information needed for a single ref update. Set new_oid to the new |
| 108 | * value or to null_oid to delete the ref. To check the old value |
| 109 | * while the ref is locked, set (flags & REF_HAVE_OLD) and set old_oid |
| 110 | * to the old value, or to null_oid to ensure the ref does not exist |
| 111 | * before update. |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 112 | */ |
| 113 | struct ref_update { |
| 114 | /* |
Michael Haggerty | 5ac95fe | 2017-11-05 09:42:05 +0100 | [diff] [blame] | 115 | * If (flags & REF_HAVE_NEW), set the reference to this value |
| 116 | * (or delete it, if `new_oid` is `null_oid`). |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 117 | */ |
brian m. carlson | 9849129 | 2017-05-06 22:10:23 +0000 | [diff] [blame] | 118 | struct object_id new_oid; |
Michael Haggerty | 6e30b2f | 2016-04-25 17:48:32 +0200 | [diff] [blame] | 119 | |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 120 | /* |
| 121 | * If (flags & REF_HAVE_OLD), check that the reference |
Michael Haggerty | 5ac95fe | 2017-11-05 09:42:05 +0100 | [diff] [blame] | 122 | * previously had this value (or didn't previously exist, if |
| 123 | * `old_oid` is `null_oid`). |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 124 | */ |
brian m. carlson | 9849129 | 2017-05-06 22:10:23 +0000 | [diff] [blame] | 125 | struct object_id old_oid; |
Michael Haggerty | 6e30b2f | 2016-04-25 17:48:32 +0200 | [diff] [blame] | 126 | |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 127 | /* |
Karthik Nayak | 1bc4cc3 | 2024-05-07 14:58:52 +0200 | [diff] [blame] | 128 | * If set, point the reference to this value. This can also be |
| 129 | * used to convert regular references to become symbolic refs. |
| 130 | * Cannot be set together with `new_oid`. |
| 131 | */ |
| 132 | const char *new_target; |
| 133 | |
| 134 | /* |
| 135 | * If set, check that the reference previously pointed to this |
| 136 | * value. Cannot be set together with `old_oid`. |
| 137 | */ |
| 138 | const char *old_target; |
| 139 | |
| 140 | /* |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 141 | * One or more of REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, |
Michael Haggerty | 5ac95fe | 2017-11-05 09:42:05 +0100 | [diff] [blame] | 142 | * REF_HAVE_NEW, REF_HAVE_OLD, or backend-specific flags. |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 143 | */ |
| 144 | unsigned int flags; |
Michael Haggerty | 6e30b2f | 2016-04-25 17:48:32 +0200 | [diff] [blame] | 145 | |
David Turner | 7d61826 | 2016-09-04 18:08:43 +0200 | [diff] [blame] | 146 | void *backend_data; |
Michael Haggerty | 92b1551 | 2016-04-25 15:56:07 +0200 | [diff] [blame] | 147 | unsigned int type; |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 148 | char *msg; |
Michael Haggerty | 6e30b2f | 2016-04-25 17:48:32 +0200 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * If this ref_update was split off of a symref update via |
| 152 | * split_symref_update(), then this member points at that |
| 153 | * update. This is used for two purposes: |
| 154 | * 1. When reporting errors, we report the refname under which |
| 155 | * the update was originally requested. |
| 156 | * 2. When we read the old value of this reference, we |
| 157 | * propagate it back to its parent update for recording in |
| 158 | * the latter's reflog. |
| 159 | */ |
| 160 | struct ref_update *parent_update; |
| 161 | |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 162 | const char refname[FLEX_ARRAY]; |
| 163 | }; |
| 164 | |
Han-Wen Nienhuys | 8b72fea | 2021-10-16 11:39:09 +0200 | [diff] [blame] | 165 | int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, |
| 166 | struct object_id *oid, struct strbuf *referent, |
| 167 | unsigned int *type, int *failure_errno); |
Michael Haggerty | 470be51 | 2017-03-20 17:33:07 +0100 | [diff] [blame] | 168 | |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 169 | /* |
Michael Haggerty | 2ced105 | 2017-05-22 16:17:45 +0200 | [diff] [blame] | 170 | * Write an error to `err` and return a nonzero value iff the same |
| 171 | * refname appears multiple times in `refnames`. `refnames` must be |
| 172 | * sorted on entry to this function. |
| 173 | */ |
| 174 | int ref_update_reject_duplicates(struct string_list *refnames, |
| 175 | struct strbuf *err); |
| 176 | |
| 177 | /* |
Michael Haggerty | 7156451 | 2016-04-25 11:39:54 +0200 | [diff] [blame] | 178 | * Add a ref_update with the specified properties to transaction, and |
| 179 | * return a pointer to the new object. This function does not verify |
Michael Haggerty | 78fb457 | 2017-11-05 09:42:09 +0100 | [diff] [blame] | 180 | * that refname is well-formed. new_oid and old_oid are only |
Michael Haggerty | 7156451 | 2016-04-25 11:39:54 +0200 | [diff] [blame] | 181 | * dereferenced if the REF_HAVE_NEW and REF_HAVE_OLD bits, |
| 182 | * respectively, are set in flags. |
| 183 | */ |
| 184 | struct ref_update *ref_transaction_add_update( |
| 185 | struct ref_transaction *transaction, |
| 186 | const char *refname, unsigned int flags, |
brian m. carlson | 89f3bbd | 2017-10-15 22:06:53 +0000 | [diff] [blame] | 187 | const struct object_id *new_oid, |
| 188 | const struct object_id *old_oid, |
Karthik Nayak | 1bc4cc3 | 2024-05-07 14:58:52 +0200 | [diff] [blame] | 189 | const char *new_target, const char *old_target, |
Michael Haggerty | 7156451 | 2016-04-25 11:39:54 +0200 | [diff] [blame] | 190 | const char *msg); |
| 191 | |
| 192 | /* |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 193 | * Transaction states. |
Michael Haggerty | 30173b8 | 2017-05-22 16:17:44 +0200 | [diff] [blame] | 194 | * |
| 195 | * OPEN: The transaction is initialized and new updates can still be |
| 196 | * added to it. An OPEN transaction can be prepared, |
| 197 | * committed, freed, or aborted (freeing and aborting an open |
| 198 | * transaction are equivalent). |
| 199 | * |
| 200 | * PREPARED: ref_transaction_prepare(), which locks all of the |
| 201 | * references involved in the update and checks that the |
| 202 | * update has no errors, has been called successfully for the |
| 203 | * transaction. A PREPARED transaction can be committed or |
| 204 | * aborted. |
| 205 | * |
| 206 | * CLOSED: The transaction is no longer active. A transaction becomes |
| 207 | * CLOSED if there is a failure while building the transaction |
| 208 | * or if a transaction is committed or aborted. A CLOSED |
| 209 | * transaction can only be freed. |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 210 | */ |
| 211 | enum ref_transaction_state { |
Michael Haggerty | 30173b8 | 2017-05-22 16:17:44 +0200 | [diff] [blame] | 212 | REF_TRANSACTION_OPEN = 0, |
| 213 | REF_TRANSACTION_PREPARED = 1, |
| 214 | REF_TRANSACTION_CLOSED = 2 |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | /* |
| 218 | * Data structure for holding a reference transaction, which can |
| 219 | * consist of checks and updates to multiple references, carried out |
| 220 | * as atomically as possible. This structure is opaque to callers. |
| 221 | */ |
| 222 | struct ref_transaction { |
Nguyễn Thái Ngọc Duy | c0fe4e8 | 2017-03-26 09:42:35 +0700 | [diff] [blame] | 223 | struct ref_store *ref_store; |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 224 | struct ref_update **updates; |
| 225 | size_t alloc; |
| 226 | size_t nr; |
| 227 | enum ref_transaction_state state; |
Michael Haggerty | 3bf4f56 | 2017-09-08 15:51:44 +0200 | [diff] [blame] | 228 | void *backend_data; |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 229 | }; |
| 230 | |
David Turner | 0845122 | 2015-11-10 12:42:40 +0100 | [diff] [blame] | 231 | /* |
| 232 | * Check for entries in extras that are within the specified |
| 233 | * directory, where dirname is a reference directory name including |
| 234 | * the trailing slash (e.g., "refs/heads/foo/"). Ignore any |
| 235 | * conflicting references that are found in skip. If there is a |
| 236 | * conflicting reference, return its name. |
| 237 | * |
| 238 | * extras and skip must be sorted lists of reference names. Either one |
| 239 | * can be NULL, signifying the empty list. |
| 240 | */ |
| 241 | const char *find_descendant_ref(const char *dirname, |
| 242 | const struct string_list *extras, |
| 243 | const struct string_list *skip); |
| 244 | |
David Turner | 2d0663b | 2016-04-07 15:03:10 -0400 | [diff] [blame] | 245 | /* We allow "recursive" symbolic refs. Only within reason, though */ |
| 246 | #define SYMREF_MAXDEPTH 5 |
David Turner | 9377059 | 2016-04-07 15:02:49 -0400 | [diff] [blame] | 247 | |
David Turner | 9377059 | 2016-04-07 15:02:49 -0400 | [diff] [blame] | 248 | /* |
Jeff King | 9aab952 | 2021-09-24 14:39:44 -0400 | [diff] [blame] | 249 | * These flags are passed to refs_ref_iterator_begin() (and do_for_each_ref(), |
| 250 | * which feeds it). |
Jeff King | bf708ad | 2021-09-24 14:37:58 -0400 | [diff] [blame] | 251 | */ |
Jeff King | 9aab952 | 2021-09-24 14:39:44 -0400 | [diff] [blame] | 252 | enum do_for_each_ref_flags { |
| 253 | /* |
| 254 | * Include broken references in a do_for_each_ref*() iteration, which |
| 255 | * would normally be omitted. This includes both refs that point to |
| 256 | * missing objects (a true repository corruption), ones with illegal |
| 257 | * names (which we prefer not to expose to callers), as well as |
| 258 | * dangling symbolic refs (i.e., those that point to a non-existent |
| 259 | * ref; this is not a corruption, but as they have no valid oid, we |
| 260 | * omit them from normal iteration results). |
| 261 | */ |
| 262 | DO_FOR_EACH_INCLUDE_BROKEN = (1 << 0), |
| 263 | |
| 264 | /* |
| 265 | * Only include per-worktree refs in a do_for_each_ref*() iteration. |
| 266 | * Normally this will be used with a files ref_store, since that's |
| 267 | * where all reference backends will presumably store their |
| 268 | * per-worktree refs. |
| 269 | */ |
| 270 | DO_FOR_EACH_PER_WORKTREE_ONLY = (1 << 1), |
Jeff King | 8dccb22 | 2021-09-24 14:41:32 -0400 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * Omit dangling symrefs from output; this only has an effect with |
| 274 | * INCLUDE_BROKEN, since they are otherwise not included at all. |
| 275 | */ |
| 276 | DO_FOR_EACH_OMIT_DANGLING_SYMREFS = (1 << 2), |
Karthik Nayak | d0f00c1 | 2024-02-23 11:01:10 +0100 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * Include root refs i.e. HEAD and pseudorefs along with the regular |
| 280 | * refs. |
| 281 | */ |
| 282 | DO_FOR_EACH_INCLUDE_ROOT_REFS = (1 << 3), |
Jeff King | 9aab952 | 2021-09-24 14:39:44 -0400 | [diff] [blame] | 283 | }; |
David Turner | 9377059 | 2016-04-07 15:02:49 -0400 | [diff] [blame] | 284 | |
| 285 | /* |
Michael Haggerty | 3bc581b | 2016-06-18 06:15:15 +0200 | [diff] [blame] | 286 | * Reference iterators |
| 287 | * |
| 288 | * A reference iterator encapsulates the state of an in-progress |
| 289 | * iteration over references. Create an instance of `struct |
| 290 | * ref_iterator` via one of the functions in this module. |
| 291 | * |
| 292 | * A freshly-created ref_iterator doesn't yet point at a reference. To |
| 293 | * advance the iterator, call ref_iterator_advance(). If successful, |
| 294 | * this sets the iterator's refname, oid, and flags fields to describe |
| 295 | * the next reference and returns ITER_OK. The data pointed at by |
| 296 | * refname and oid belong to the iterator; if you want to retain them |
| 297 | * after calling ref_iterator_advance() again or calling |
| 298 | * ref_iterator_abort(), you must make a copy. When the iteration has |
| 299 | * been exhausted, ref_iterator_advance() releases any resources |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 300 | * associated with the iteration, frees the ref_iterator object, and |
Michael Haggerty | 3bc581b | 2016-06-18 06:15:15 +0200 | [diff] [blame] | 301 | * returns ITER_DONE. If you want to abort the iteration early, call |
| 302 | * ref_iterator_abort(), which also frees the ref_iterator object and |
| 303 | * any associated resources. If there was an internal error advancing |
| 304 | * to the next entry, ref_iterator_advance() aborts the iteration, |
| 305 | * frees the ref_iterator, and returns ITER_ERROR. |
| 306 | * |
| 307 | * The reference currently being looked at can be peeled by calling |
| 308 | * ref_iterator_peel(). This function is often faster than peel_ref(), |
| 309 | * so it should be preferred when iterating over references. |
| 310 | * |
| 311 | * Putting it all together, a typical iteration looks like this: |
| 312 | * |
| 313 | * int ok; |
| 314 | * struct ref_iterator *iter = ...; |
| 315 | * |
| 316 | * while ((ok = ref_iterator_advance(iter)) == ITER_OK) { |
| 317 | * if (want_to_stop_iteration()) { |
| 318 | * ok = ref_iterator_abort(iter); |
| 319 | * break; |
| 320 | * } |
| 321 | * |
| 322 | * // Access information about the current reference: |
| 323 | * if (!(iter->flags & REF_ISSYMREF)) |
Tao Qingyun | 7b6057c | 2018-09-15 10:15:46 +0800 | [diff] [blame] | 324 | * printf("%s is %s\n", iter->refname, oid_to_hex(iter->oid)); |
Michael Haggerty | 3bc581b | 2016-06-18 06:15:15 +0200 | [diff] [blame] | 325 | * |
| 326 | * // If you need to peel the reference: |
| 327 | * ref_iterator_peel(iter, &oid); |
| 328 | * } |
| 329 | * |
| 330 | * if (ok != ITER_DONE) |
| 331 | * handle_error(); |
| 332 | */ |
| 333 | struct ref_iterator { |
| 334 | struct ref_iterator_vtable *vtable; |
| 335 | const char *refname; |
| 336 | const struct object_id *oid; |
| 337 | unsigned int flags; |
| 338 | }; |
| 339 | |
| 340 | /* |
| 341 | * Advance the iterator to the first or next item and return ITER_OK. |
| 342 | * If the iteration is exhausted, free the resources associated with |
| 343 | * the ref_iterator and return ITER_DONE. On errors, free the iterator |
| 344 | * resources and return ITER_ERROR. It is a bug to use ref_iterator or |
| 345 | * call this function again after it has returned ITER_DONE or |
| 346 | * ITER_ERROR. |
| 347 | */ |
| 348 | int ref_iterator_advance(struct ref_iterator *ref_iterator); |
| 349 | |
| 350 | /* |
| 351 | * If possible, peel the reference currently being viewed by the |
| 352 | * iterator. Return 0 on success. |
| 353 | */ |
| 354 | int ref_iterator_peel(struct ref_iterator *ref_iterator, |
| 355 | struct object_id *peeled); |
| 356 | |
| 357 | /* |
| 358 | * End the iteration before it has been exhausted, freeing the |
| 359 | * reference iterator and any associated resources and returning |
| 360 | * ITER_DONE. If the abort itself failed, return ITER_ERROR. |
| 361 | */ |
| 362 | int ref_iterator_abort(struct ref_iterator *ref_iterator); |
| 363 | |
| 364 | /* |
| 365 | * An iterator over nothing (its first ref_iterator_advance() call |
| 366 | * returns ITER_DONE). |
| 367 | */ |
| 368 | struct ref_iterator *empty_ref_iterator_begin(void); |
| 369 | |
| 370 | /* |
| 371 | * Return true iff ref_iterator is an empty_ref_iterator. |
| 372 | */ |
| 373 | int is_empty_ref_iterator(struct ref_iterator *ref_iterator); |
| 374 | |
| 375 | /* |
Michael Haggerty | e121b9c | 2017-03-20 17:33:08 +0100 | [diff] [blame] | 376 | * Return an iterator that goes over each reference in `refs` for |
| 377 | * which the refname begins with prefix. If trim is non-zero, then |
Han-Wen Nienhuys | 84ee4ca | 2020-05-20 17:36:09 +0000 | [diff] [blame] | 378 | * trim that many characters off the beginning of each refname. |
Jeff King | 9aab952 | 2021-09-24 14:39:44 -0400 | [diff] [blame] | 379 | * The output is ordered by refname. |
Michael Haggerty | e121b9c | 2017-03-20 17:33:08 +0100 | [diff] [blame] | 380 | */ |
| 381 | struct ref_iterator *refs_ref_iterator_begin( |
| 382 | struct ref_store *refs, |
Taylor Blau | b269ac5 | 2023-07-10 17:12:22 -0400 | [diff] [blame] | 383 | const char *prefix, const char **exclude_patterns, |
| 384 | int trim, enum do_for_each_ref_flags flags); |
Michael Haggerty | e121b9c | 2017-03-20 17:33:08 +0100 | [diff] [blame] | 385 | |
| 386 | /* |
Michael Haggerty | 3bc581b | 2016-06-18 06:15:15 +0200 | [diff] [blame] | 387 | * A callback function used to instruct merge_ref_iterator how to |
| 388 | * interleave the entries from iter0 and iter1. The function should |
| 389 | * return one of the constants defined in enum iterator_selection. It |
| 390 | * must not advance either of the iterators itself. |
| 391 | * |
| 392 | * The function must be prepared to handle the case that iter0 and/or |
| 393 | * iter1 is NULL, which indicates that the corresponding sub-iterator |
| 394 | * has been exhausted. Its return value must be consistent with the |
| 395 | * current states of the iterators; e.g., it must not return |
| 396 | * ITER_SKIP_1 if iter1 has already been exhausted. |
| 397 | */ |
| 398 | typedef enum iterator_selection ref_iterator_select_fn( |
| 399 | struct ref_iterator *iter0, struct ref_iterator *iter1, |
| 400 | void *cb_data); |
| 401 | |
| 402 | /* |
Patrick Steinhardt | 6f22780 | 2024-02-21 13:37:31 +0100 | [diff] [blame] | 403 | * An implementation of ref_iterator_select_fn that merges worktree and common |
| 404 | * refs. Per-worktree refs from the common iterator are ignored, worktree refs |
| 405 | * override common refs. Refs are selected lexicographically. |
| 406 | */ |
| 407 | enum iterator_selection ref_iterator_select(struct ref_iterator *iter_worktree, |
| 408 | struct ref_iterator *iter_common, |
| 409 | void *cb_data); |
| 410 | |
| 411 | /* |
Michael Haggerty | 3bc581b | 2016-06-18 06:15:15 +0200 | [diff] [blame] | 412 | * Iterate over the entries from iter0 and iter1, with the values |
| 413 | * interleaved as directed by the select function. The iterator takes |
| 414 | * ownership of iter0 and iter1 and frees them when the iteration is |
Patrick Steinhardt | 5e01d83 | 2024-02-21 13:37:35 +0100 | [diff] [blame] | 415 | * over. |
Michael Haggerty | 3bc581b | 2016-06-18 06:15:15 +0200 | [diff] [blame] | 416 | */ |
| 417 | struct ref_iterator *merge_ref_iterator_begin( |
| 418 | struct ref_iterator *iter0, struct ref_iterator *iter1, |
| 419 | ref_iterator_select_fn *select, void *cb_data); |
| 420 | |
| 421 | /* |
| 422 | * An iterator consisting of the union of the entries from front and |
| 423 | * back. If there are entries common to the two sub-iterators, use the |
| 424 | * one from front. Each iterator must iterate over its entries in |
| 425 | * strcmp() order by refname for this to work. |
| 426 | * |
| 427 | * The new iterator takes ownership of its arguments and frees them |
| 428 | * when the iteration is over. As a convenience to callers, if front |
| 429 | * or back is an empty_ref_iterator, then abort that one immediately |
| 430 | * and return the other iterator directly, without wrapping it. |
| 431 | */ |
| 432 | struct ref_iterator *overlay_ref_iterator_begin( |
| 433 | struct ref_iterator *front, struct ref_iterator *back); |
| 434 | |
| 435 | /* |
| 436 | * Wrap iter0, only letting through the references whose names start |
| 437 | * with prefix. If trim is set, set iter->refname to the name of the |
| 438 | * reference with that many characters trimmed off the front; |
| 439 | * otherwise set it to the full refname. The new iterator takes over |
| 440 | * ownership of iter0 and frees it when iteration is over. It makes |
| 441 | * its own copy of prefix. |
| 442 | * |
| 443 | * As an convenience to callers, if prefix is the empty string and |
| 444 | * trim is zero, this function returns iter0 directly, without |
| 445 | * wrapping it. |
| 446 | */ |
| 447 | struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0, |
| 448 | const char *prefix, |
| 449 | int trim); |
| 450 | |
Michael Haggerty | 3bc581b | 2016-06-18 06:15:15 +0200 | [diff] [blame] | 451 | /* Internal implementation of reference iteration: */ |
| 452 | |
| 453 | /* |
| 454 | * Base class constructor for ref_iterators. Initialize the |
| 455 | * ref_iterator part of iter, setting its vtable pointer as specified. |
| 456 | * This is meant to be called only by the initializers of derived |
| 457 | * classes. |
| 458 | */ |
| 459 | void base_ref_iterator_init(struct ref_iterator *iter, |
Patrick Steinhardt | 5e01d83 | 2024-02-21 13:37:35 +0100 | [diff] [blame] | 460 | struct ref_iterator_vtable *vtable); |
Michael Haggerty | 3bc581b | 2016-06-18 06:15:15 +0200 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * Base class destructor for ref_iterators. Destroy the ref_iterator |
| 464 | * part of iter and shallow-free the object. This is meant to be |
| 465 | * called only by the destructors of derived classes. |
| 466 | */ |
| 467 | void base_ref_iterator_free(struct ref_iterator *iter); |
| 468 | |
| 469 | /* Virtual function declarations for ref_iterators: */ |
| 470 | |
Han-Wen Nienhuys | 84ee4ca | 2020-05-20 17:36:09 +0000 | [diff] [blame] | 471 | /* |
| 472 | * backend-specific implementation of ref_iterator_advance. For symrefs, the |
| 473 | * function should set REF_ISSYMREF, and it should also dereference the symref |
Jeff King | 9aab952 | 2021-09-24 14:39:44 -0400 | [diff] [blame] | 474 | * to provide the OID referent. It should respect do_for_each_ref_flags |
| 475 | * that were passed to refs_ref_iterator_begin(). |
Han-Wen Nienhuys | 84ee4ca | 2020-05-20 17:36:09 +0000 | [diff] [blame] | 476 | */ |
Michael Haggerty | 3bc581b | 2016-06-18 06:15:15 +0200 | [diff] [blame] | 477 | typedef int ref_iterator_advance_fn(struct ref_iterator *ref_iterator); |
| 478 | |
Han-Wen Nienhuys | 617480d | 2021-05-19 15:31:28 +0000 | [diff] [blame] | 479 | /* |
| 480 | * Peels the current ref, returning 0 for success or -1 for failure. |
| 481 | */ |
Michael Haggerty | 3bc581b | 2016-06-18 06:15:15 +0200 | [diff] [blame] | 482 | typedef int ref_iterator_peel_fn(struct ref_iterator *ref_iterator, |
| 483 | struct object_id *peeled); |
| 484 | |
| 485 | /* |
| 486 | * Implementations of this function should free any resources specific |
| 487 | * to the derived class, then call base_ref_iterator_free() to clean |
| 488 | * up and free the ref_iterator object. |
| 489 | */ |
| 490 | typedef int ref_iterator_abort_fn(struct ref_iterator *ref_iterator); |
| 491 | |
| 492 | struct ref_iterator_vtable { |
| 493 | ref_iterator_advance_fn *advance; |
| 494 | ref_iterator_peel_fn *peel; |
| 495 | ref_iterator_abort_fn *abort; |
| 496 | }; |
| 497 | |
| 498 | /* |
Michael Haggerty | 4c4de89 | 2016-06-18 06:15:16 +0200 | [diff] [blame] | 499 | * current_ref_iter is a performance hack: when iterating over |
| 500 | * references using the for_each_ref*() functions, current_ref_iter is |
| 501 | * set to the reference iterator before calling the callback function. |
| 502 | * If the callback function calls peel_ref(), then peel_ref() first |
| 503 | * checks whether the reference to be peeled is the one referred to by |
| 504 | * the iterator (it usually is) and if so, asks the iterator for the |
| 505 | * peeled version of the reference if it is available. This avoids a |
| 506 | * refname lookup in a common case. current_ref_iter is set to NULL |
| 507 | * when the iteration is over. |
David Turner | 9377059 | 2016-04-07 15:02:49 -0400 | [diff] [blame] | 508 | */ |
Michael Haggerty | 4c4de89 | 2016-06-18 06:15:16 +0200 | [diff] [blame] | 509 | extern struct ref_iterator *current_ref_iter; |
| 510 | |
| 511 | /* |
| 512 | * The common backend for the for_each_*ref* functions. Call fn for |
| 513 | * each reference in iter. If the iterator itself ever returns |
| 514 | * ITER_ERROR, return -1. If fn ever returns a non-zero value, stop |
| 515 | * the iteration and return that value. Otherwise, return 0. In any |
| 516 | * case, free the iterator when done. This function is basically an |
| 517 | * adapter between the callback style of reference iteration and the |
| 518 | * iterator style. |
| 519 | */ |
Stefan Beller | 4a6067c | 2018-08-20 18:24:16 +0000 | [diff] [blame] | 520 | int do_for_each_repo_ref_iterator(struct repository *r, |
| 521 | struct ref_iterator *iter, |
| 522 | each_repo_ref_fn fn, void *cb_data); |
David Turner | 2d0663b | 2016-04-07 15:03:10 -0400 | [diff] [blame] | 523 | |
Michael Haggerty | 1a76900 | 2016-09-04 18:08:37 +0200 | [diff] [blame] | 524 | struct ref_store; |
| 525 | |
David Turner | 0c09ec0 | 2016-09-04 18:08:44 +0200 | [diff] [blame] | 526 | /* refs backends */ |
| 527 | |
Nguyễn Thái Ngọc Duy | 9e7ec63 | 2017-03-26 09:42:32 +0700 | [diff] [blame] | 528 | /* ref_store_init flags */ |
| 529 | #define REF_STORE_READ (1 << 0) |
| 530 | #define REF_STORE_WRITE (1 << 1) /* can perform update operations */ |
| 531 | #define REF_STORE_ODB (1 << 2) /* has access to object database */ |
| 532 | #define REF_STORE_MAIN (1 << 3) |
Nguyễn Thái Ngọc Duy | 0d8a814 | 2017-04-24 17:01:21 +0700 | [diff] [blame] | 533 | #define REF_STORE_ALL_CAPS (REF_STORE_READ | \ |
| 534 | REF_STORE_WRITE | \ |
| 535 | REF_STORE_ODB | \ |
| 536 | REF_STORE_MAIN) |
Nguyễn Thái Ngọc Duy | 9e7ec63 | 2017-03-26 09:42:32 +0700 | [diff] [blame] | 537 | |
Michael Haggerty | e1e33b7 | 2016-09-04 18:08:25 +0200 | [diff] [blame] | 538 | /* |
Nguyễn Thái Ngọc Duy | 5d0bc90 | 2017-03-26 09:42:31 +0700 | [diff] [blame] | 539 | * Initialize the ref_store for the specified gitdir. These functions |
| 540 | * should call base_ref_store_init() to initialize the shared part of |
| 541 | * the ref_store and to record the ref_store for later lookup. |
Michael Haggerty | e1e33b7 | 2016-09-04 18:08:25 +0200 | [diff] [blame] | 542 | */ |
Jonathan Tan | 34224e1 | 2021-10-08 14:08:14 -0700 | [diff] [blame] | 543 | typedef struct ref_store *ref_store_init_fn(struct repository *repo, |
| 544 | const char *gitdir, |
Nguyễn Thái Ngọc Duy | 9e7ec63 | 2017-03-26 09:42:32 +0700 | [diff] [blame] | 545 | unsigned int flags); |
Michael Haggerty | e1e33b7 | 2016-09-04 18:08:25 +0200 | [diff] [blame] | 546 | |
Patrick Steinhardt | 2e573d6 | 2024-01-08 11:05:26 +0100 | [diff] [blame] | 547 | typedef int ref_init_db_fn(struct ref_store *refs, |
| 548 | int flags, |
| 549 | struct strbuf *err); |
David Turner | 6fb5acf | 2016-09-04 18:08:41 +0200 | [diff] [blame] | 550 | |
Michael Haggerty | 30173b8 | 2017-05-22 16:17:44 +0200 | [diff] [blame] | 551 | typedef int ref_transaction_prepare_fn(struct ref_store *refs, |
| 552 | struct ref_transaction *transaction, |
| 553 | struct strbuf *err); |
| 554 | |
| 555 | typedef int ref_transaction_finish_fn(struct ref_store *refs, |
| 556 | struct ref_transaction *transaction, |
| 557 | struct strbuf *err); |
| 558 | |
| 559 | typedef int ref_transaction_abort_fn(struct ref_store *refs, |
| 560 | struct ref_transaction *transaction, |
| 561 | struct strbuf *err); |
| 562 | |
Michael Haggerty | e1e33b7 | 2016-09-04 18:08:25 +0200 | [diff] [blame] | 563 | typedef int ref_transaction_commit_fn(struct ref_store *refs, |
| 564 | struct ref_transaction *transaction, |
| 565 | struct strbuf *err); |
| 566 | |
John Cai | 826ae79 | 2023-05-12 21:34:41 +0000 | [diff] [blame] | 567 | typedef int pack_refs_fn(struct ref_store *ref_store, |
| 568 | struct pack_refs_opts *opts); |
David Turner | 9b6b40d | 2016-09-04 18:08:42 +0200 | [diff] [blame] | 569 | typedef int rename_ref_fn(struct ref_store *ref_store, |
| 570 | const char *oldref, const char *newref, |
| 571 | const char *logmsg); |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 572 | typedef int copy_ref_fn(struct ref_store *ref_store, |
| 573 | const char *oldref, const char *newref, |
| 574 | const char *logmsg); |
Michael Haggerty | 8231527 | 2016-09-04 18:08:27 +0200 | [diff] [blame] | 575 | |
Michael Haggerty | cf59644 | 2016-05-06 17:25:31 +0200 | [diff] [blame] | 576 | /* |
Michael Haggerty | e186057 | 2017-05-22 16:17:33 +0200 | [diff] [blame] | 577 | * Iterate over the references in `ref_store` whose names start with |
| 578 | * `prefix`. `prefix` is matched as a literal string, without regard |
| 579 | * for path separators. If prefix is NULL or the empty string, iterate |
Michael Haggerty | 8738a8a | 2017-09-13 19:15:55 +0200 | [diff] [blame] | 580 | * over all references in `ref_store`. The output is ordered by |
| 581 | * refname. |
Michael Haggerty | 1a76900 | 2016-09-04 18:08:37 +0200 | [diff] [blame] | 582 | */ |
| 583 | typedef struct ref_iterator *ref_iterator_begin_fn( |
| 584 | struct ref_store *ref_store, |
Taylor Blau | b269ac5 | 2023-07-10 17:12:22 -0400 | [diff] [blame] | 585 | const char *prefix, const char **exclude_patterns, |
| 586 | unsigned int flags); |
Michael Haggerty | 1a76900 | 2016-09-04 18:08:37 +0200 | [diff] [blame] | 587 | |
David Turner | e3688bd | 2016-09-04 18:08:38 +0200 | [diff] [blame] | 588 | /* reflog functions */ |
| 589 | |
| 590 | /* |
| 591 | * Iterate over the references in the specified ref_store that have a |
| 592 | * reflog. The refs are iterated over in arbitrary order. |
| 593 | */ |
| 594 | typedef struct ref_iterator *reflog_iterator_begin_fn( |
| 595 | struct ref_store *ref_store); |
| 596 | |
| 597 | typedef int for_each_reflog_ent_fn(struct ref_store *ref_store, |
| 598 | const char *refname, |
| 599 | each_reflog_ent_fn fn, |
| 600 | void *cb_data); |
| 601 | typedef int for_each_reflog_ent_reverse_fn(struct ref_store *ref_store, |
| 602 | const char *refname, |
| 603 | each_reflog_ent_fn fn, |
| 604 | void *cb_data); |
| 605 | typedef int reflog_exists_fn(struct ref_store *ref_store, const char *refname); |
| 606 | typedef int create_reflog_fn(struct ref_store *ref_store, const char *refname, |
Han-Wen Nienhuys | 7b08912 | 2021-11-22 14:19:08 +0000 | [diff] [blame] | 607 | struct strbuf *err); |
David Turner | e3688bd | 2016-09-04 18:08:38 +0200 | [diff] [blame] | 608 | typedef int delete_reflog_fn(struct ref_store *ref_store, const char *refname); |
| 609 | typedef int reflog_expire_fn(struct ref_store *ref_store, |
Ævar Arnfjörð Bjarmason | cc40b5c | 2021-08-23 13:36:11 +0200 | [diff] [blame] | 610 | const char *refname, |
David Turner | e3688bd | 2016-09-04 18:08:38 +0200 | [diff] [blame] | 611 | unsigned int flags, |
| 612 | reflog_expiry_prepare_fn prepare_fn, |
| 613 | reflog_expiry_should_prune_fn should_prune_fn, |
| 614 | reflog_expiry_cleanup_fn cleanup_fn, |
| 615 | void *policy_cb_data); |
| 616 | |
Michael Haggerty | 1a76900 | 2016-09-04 18:08:37 +0200 | [diff] [blame] | 617 | /* |
Michael Haggerty | 34c7ad8 | 2016-09-04 18:08:20 +0200 | [diff] [blame] | 618 | * Read a reference from the specified reference store, non-recursively. |
| 619 | * Set type to describe the reference, and: |
Michael Haggerty | cf59644 | 2016-05-06 17:25:31 +0200 | [diff] [blame] | 620 | * |
brian m. carlson | 99afe91 | 2017-10-15 22:07:11 +0000 | [diff] [blame] | 621 | * - If refname is the name of a normal reference, fill in oid |
Michael Haggerty | cf59644 | 2016-05-06 17:25:31 +0200 | [diff] [blame] | 622 | * (leaving referent unchanged). |
| 623 | * |
| 624 | * - If refname is the name of a symbolic reference, write the full |
| 625 | * name of the reference to which it refers (e.g. |
| 626 | * "refs/heads/master") to referent and set the REF_ISSYMREF bit in |
brian m. carlson | 99afe91 | 2017-10-15 22:07:11 +0000 | [diff] [blame] | 627 | * type (leaving oid unchanged). The caller is responsible for |
Michael Haggerty | cf59644 | 2016-05-06 17:25:31 +0200 | [diff] [blame] | 628 | * validating that referent is a valid reference name. |
| 629 | * |
| 630 | * WARNING: refname might be used as part of a filename, so it is |
| 631 | * important from a security standpoint that it be safe in the sense |
| 632 | * of refname_is_safe(). Moreover, for symrefs this function sets |
| 633 | * referent to whatever the repository says, which might not be a |
| 634 | * properly-formatted or even safe reference name. NEITHER INPUT NOR |
| 635 | * OUTPUT REFERENCE NAMES ARE VALIDATED WITHIN THIS FUNCTION. |
| 636 | * |
Han-Wen Nienhuys | 5b12e16 | 2021-08-23 13:52:40 +0200 | [diff] [blame] | 637 | * Return 0 on success, or -1 on failure. If the ref exists but is neither a |
| 638 | * symbolic ref nor an object ID, it is broken. In this case set REF_ISBROKEN in |
| 639 | * type, and return -1 (failure_errno should not be ENOENT) |
| 640 | * |
| 641 | * failure_errno provides errno codes that are interpreted beyond error |
| 642 | * reporting. The following error codes have special meaning: |
| 643 | * * ENOENT: the ref doesn't exist |
| 644 | * * EISDIR: ref name is a directory |
| 645 | * * ENOTDIR: ref prefix is not a directory |
Michael Haggerty | cf59644 | 2016-05-06 17:25:31 +0200 | [diff] [blame] | 646 | * |
| 647 | * Backend-specific flags might be set in type as well, regardless of |
| 648 | * outcome. |
| 649 | * |
| 650 | * It is OK for refname to point into referent. If so: |
| 651 | * |
| 652 | * - if the function succeeds with REF_ISSYMREF, referent will be |
| 653 | * overwritten and the memory formerly pointed to by it might be |
| 654 | * changed or even freed. |
| 655 | * |
| 656 | * - in all other cases, referent will be untouched, and therefore |
| 657 | * refname will still be valid and unchanged. |
| 658 | */ |
Han-Wen Nienhuys | 5b12e16 | 2021-08-23 13:52:40 +0200 | [diff] [blame] | 659 | typedef int read_raw_ref_fn(struct ref_store *ref_store, const char *refname, |
| 660 | struct object_id *oid, struct strbuf *referent, |
| 661 | unsigned int *type, int *failure_errno); |
Ronnie Sahlberg | 127b42a | 2016-09-04 18:08:16 +0200 | [diff] [blame] | 662 | |
Patrick Steinhardt | cd475b3 | 2022-03-01 10:33:46 +0100 | [diff] [blame] | 663 | /* |
| 664 | * Read a symbolic reference from the specified reference store. This function |
| 665 | * is optional: if not implemented by a backend, then `read_raw_ref_fn` is used |
| 666 | * to read the symbolcic reference instead. It is intended to be implemented |
| 667 | * only in case the backend can optimize the reading of symbolic references. |
| 668 | * |
| 669 | * Return 0 on success, or -1 on failure. `referent` will be set to the target |
| 670 | * of the symbolic reference on success. This function explicitly does not |
| 671 | * distinguish between error cases and the reference not being a symbolic |
| 672 | * reference to allow backends to optimize this operation in case symbolic and |
| 673 | * non-symbolic references are treated differently. |
| 674 | */ |
| 675 | typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refname, |
| 676 | struct strbuf *referent); |
| 677 | |
Ronnie Sahlberg | 3dce444 | 2016-09-04 18:08:10 +0200 | [diff] [blame] | 678 | struct ref_storage_be { |
Ronnie Sahlberg | 3dce444 | 2016-09-04 18:08:10 +0200 | [diff] [blame] | 679 | const char *name; |
Michael Haggerty | 00eebe3 | 2016-09-04 18:08:11 +0200 | [diff] [blame] | 680 | ref_store_init_fn *init; |
David Turner | 6fb5acf | 2016-09-04 18:08:41 +0200 | [diff] [blame] | 681 | ref_init_db_fn *init_db; |
Michael Haggerty | 30173b8 | 2017-05-22 16:17:44 +0200 | [diff] [blame] | 682 | |
| 683 | ref_transaction_prepare_fn *transaction_prepare; |
| 684 | ref_transaction_finish_fn *transaction_finish; |
| 685 | ref_transaction_abort_fn *transaction_abort; |
David Turner | fc68146 | 2016-09-04 18:08:39 +0200 | [diff] [blame] | 686 | ref_transaction_commit_fn *initial_transaction_commit; |
Michael Haggerty | e1e33b7 | 2016-09-04 18:08:25 +0200 | [diff] [blame] | 687 | |
Michael Haggerty | 8231527 | 2016-09-04 18:08:27 +0200 | [diff] [blame] | 688 | pack_refs_fn *pack_refs; |
David Turner | 9b6b40d | 2016-09-04 18:08:42 +0200 | [diff] [blame] | 689 | rename_ref_fn *rename_ref; |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 690 | copy_ref_fn *copy_ref; |
Michael Haggerty | 8231527 | 2016-09-04 18:08:27 +0200 | [diff] [blame] | 691 | |
Michael Haggerty | 1a76900 | 2016-09-04 18:08:37 +0200 | [diff] [blame] | 692 | ref_iterator_begin_fn *iterator_begin; |
Michael Haggerty | e1e33b7 | 2016-09-04 18:08:25 +0200 | [diff] [blame] | 693 | read_raw_ref_fn *read_raw_ref; |
Patrick Steinhardt | cd475b3 | 2022-03-01 10:33:46 +0100 | [diff] [blame] | 694 | read_symbolic_ref_fn *read_symbolic_ref; |
David Turner | e3688bd | 2016-09-04 18:08:38 +0200 | [diff] [blame] | 695 | |
| 696 | reflog_iterator_begin_fn *reflog_iterator_begin; |
| 697 | for_each_reflog_ent_fn *for_each_reflog_ent; |
| 698 | for_each_reflog_ent_reverse_fn *for_each_reflog_ent_reverse; |
| 699 | reflog_exists_fn *reflog_exists; |
| 700 | create_reflog_fn *create_reflog; |
| 701 | delete_reflog_fn *delete_reflog; |
| 702 | reflog_expire_fn *reflog_expire; |
Ronnie Sahlberg | 3dce444 | 2016-09-04 18:08:10 +0200 | [diff] [blame] | 703 | }; |
| 704 | |
| 705 | extern struct ref_storage_be refs_be_files; |
Patrick Steinhardt | 57db2a0 | 2024-02-07 08:20:31 +0100 | [diff] [blame] | 706 | extern struct ref_storage_be refs_be_reftable; |
Michael Haggerty | e0cc8ac | 2017-06-23 09:01:38 +0200 | [diff] [blame] | 707 | extern struct ref_storage_be refs_be_packed; |
Ronnie Sahlberg | 3dce444 | 2016-09-04 18:08:10 +0200 | [diff] [blame] | 708 | |
Michael Haggerty | 00eebe3 | 2016-09-04 18:08:11 +0200 | [diff] [blame] | 709 | /* |
| 710 | * A representation of the reference store for the main repository or |
| 711 | * a submodule. The ref_store instances for submodules are kept in a |
Han-Wen Nienhuys | 4877c6c | 2020-08-19 14:27:56 +0000 | [diff] [blame] | 712 | * hash map; see get_submodule_ref_store() for more info. |
Michael Haggerty | 00eebe3 | 2016-09-04 18:08:11 +0200 | [diff] [blame] | 713 | */ |
| 714 | struct ref_store { |
| 715 | /* The backend describing this ref_store's storage scheme: */ |
| 716 | const struct ref_storage_be *be; |
Han-Wen Nienhuys | 5085aef | 2020-08-19 14:27:57 +0000 | [diff] [blame] | 717 | |
Jonathan Tan | 34224e1 | 2021-10-08 14:08:14 -0700 | [diff] [blame] | 718 | struct repository *repo; |
| 719 | |
| 720 | /* |
| 721 | * The gitdir that this ref_store applies to. Note that this is not |
| 722 | * necessarily repo->gitdir if the repo has multiple worktrees. |
| 723 | */ |
Han-Wen Nienhuys | 5085aef | 2020-08-19 14:27:57 +0000 | [diff] [blame] | 724 | char *gitdir; |
Michael Haggerty | 00eebe3 | 2016-09-04 18:08:11 +0200 | [diff] [blame] | 725 | }; |
| 726 | |
| 727 | /* |
Han-Wen Nienhuys | df3458e | 2021-10-16 11:39:10 +0200 | [diff] [blame] | 728 | * Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for |
| 729 | * invalid contents. |
Han-Wen Nienhuys | e39620f | 2020-08-19 14:27:55 +0000 | [diff] [blame] | 730 | */ |
| 731 | int parse_loose_ref_contents(const char *buf, struct object_id *oid, |
Han-Wen Nienhuys | df3458e | 2021-10-16 11:39:10 +0200 | [diff] [blame] | 732 | struct strbuf *referent, unsigned int *type, |
| 733 | int *failure_errno); |
Han-Wen Nienhuys | e39620f | 2020-08-19 14:27:55 +0000 | [diff] [blame] | 734 | |
| 735 | /* |
Michael Haggerty | fbfd0a2 | 2017-02-10 12:16:17 +0100 | [diff] [blame] | 736 | * Fill in the generic part of refs and add it to our collection of |
| 737 | * reference stores. |
Michael Haggerty | 00eebe3 | 2016-09-04 18:08:11 +0200 | [diff] [blame] | 738 | */ |
Han-Wen Nienhuys | f9f7fd3 | 2021-12-22 18:11:54 +0000 | [diff] [blame] | 739 | void base_ref_store_init(struct ref_store *refs, struct repository *repo, |
| 740 | const char *path, const struct ref_storage_be *be); |
Michael Haggerty | 00eebe3 | 2016-09-04 18:08:11 +0200 | [diff] [blame] | 741 | |
Han-Wen Nienhuys | 4441f42 | 2020-09-09 10:15:08 +0000 | [diff] [blame] | 742 | /* |
| 743 | * Support GIT_TRACE_REFS by optionally wrapping the given ref_store instance. |
| 744 | */ |
| 745 | struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store); |
| 746 | |
Karthik Nayak | e9965ba | 2024-05-07 14:58:55 +0200 | [diff] [blame] | 747 | /* |
| 748 | * Return the refname under which update was originally requested. |
| 749 | */ |
| 750 | const char *ref_update_original_update_refname(struct ref_update *update); |
| 751 | |
Karthik Nayak | 644daf7 | 2024-05-07 14:58:56 +0200 | [diff] [blame] | 752 | /* |
| 753 | * Helper function to check if the new value is null, this |
| 754 | * takes into consideration that the update could be a regular |
| 755 | * ref or a symbolic ref. |
| 756 | */ |
| 757 | int ref_update_has_null_new_value(struct ref_update *update); |
| 758 | |
| 759 | /* |
| 760 | * Check whether the old_target values stored in update are consistent |
| 761 | * with the referent, which is the symbolic reference's current value. |
| 762 | * If everything is OK, return 0; otherwise, write an error message to |
| 763 | * err and return -1. |
| 764 | */ |
| 765 | int ref_update_check_old_target(const char *referent, struct ref_update *update, |
| 766 | struct strbuf *err); |
| 767 | |
Michael Haggerty | 4cb7700 | 2015-11-10 12:42:36 +0100 | [diff] [blame] | 768 | #endif /* REFS_REFS_INTERNAL_H */ |