]>
| Commit | Line | Data |
|---|---|---|
| 4cb77009 MH |
1 | #ifndef REFS_REFS_INTERNAL_H |
| 2 | #define REFS_REFS_INTERNAL_H | |
| 3 | ||
| 4eb4416d | 4 | #include "refs.h" |
| 13f925f3 | 5 | #include "iterator.h" |
| c3baddf0 | 6 | #include "string-list.h" |
| 91c2f204 | 7 | |
| ab6f79d8 | 8 | struct fsck_options; |
| 4eb4416d RJ |
9 | struct ref_transaction; |
| 10 | ||
| 4cb77009 MH |
11 | /* |
| 12 | * Data structures and functions for the internal use of the refs | |
| 13 | * module. Code outside of the refs module should use only the public | |
| 14 | * functions defined in "refs.h", and should *not* include this file. | |
| 15 | */ | |
| 16 | ||
| 17 | /* | |
| 5ac95fee | 18 | * The following flags can appear in `ref_update::flags`. Their |
| 91774afc | 19 | * numerical values must not conflict with those of REF_NO_DEREF and |
| 5ac95fee MH |
20 | * REF_FORCE_CREATE_REFLOG, which are also stored in |
| 21 | * `ref_update::flags`. | |
| 4cb77009 | 22 | */ |
| 4cb77009 MH |
23 | |
| 24 | /* | |
| 78fb4579 | 25 | * The reference should be updated to new_oid. |
| 4cb77009 | 26 | */ |
| 5ac95fee | 27 | #define REF_HAVE_NEW (1 << 2) |
| 4cb77009 MH |
28 | |
| 29 | /* | |
| 5ac95fee | 30 | * The current reference's value should be checked to make sure that |
| 78fb4579 | 31 | * it agrees with old_oid. |
| 4cb77009 | 32 | */ |
| 5ac95fee | 33 | #define REF_HAVE_OLD (1 << 3) |
| 44639777 | 34 | |
| 63c05673 HWN |
35 | /* |
| 36 | * Used as a flag in ref_update::flags when we want to log a ref | |
| 37 | * update but not actually perform it. This is used when a symbolic | |
| 38 | * ref update is split up. | |
| 39 | */ | |
| 40 | #define REF_LOG_ONLY (1 << 7) | |
| 41 | ||
| 4ff0f01c MH |
42 | /* |
| 43 | * Return the length of time to retry acquiring a loose reference lock | |
| 44 | * before giving up, in milliseconds: | |
| 45 | */ | |
| 46 | long get_files_ref_lock_timeout_ms(void); | |
| 47 | ||
| 4cb77009 MH |
48 | /* |
| 49 | * Return true iff refname is minimally safe. "Safe" here means that | |
| 50 | * deleting a loose reference by this name will not do any damage, for | |
| 51 | * example by causing a file that is not a reference to be deleted. | |
| 52 | * This function does not check that the reference name is legal; for | |
| 53 | * that, use check_refname_format(). | |
| 54 | * | |
| 15ee2c72 MH |
55 | * A refname that starts with "refs/" is considered safe iff it |
| 56 | * doesn't contain any "." or ".." components or consecutive '/' | |
| 57 | * characters, end with '/', or (on Windows) contain any '\' | |
| 58 | * characters. Names that do not start with "refs/" are considered | |
| 59 | * safe iff they consist entirely of upper case characters and '_' | |
| 60 | * (like "HEAD" and "MERGE_HEAD" but not "config" or "FOO/BAR"). | |
| 4cb77009 MH |
61 | */ |
| 62 | int refname_is_safe(const char *refname); | |
| 63 | ||
| 67be7c5a MH |
64 | /* |
| 65 | * Helper function: return true if refname, which has the specified | |
| 66 | * oid and flags, can be resolved to an object in the database. If the | |
| 67 | * referred-to object does not exist, emit a warning and return false. | |
| 68 | */ | |
| 69 | int ref_resolves_to_object(const char *refname, | |
| 9bc45a28 | 70 | struct repository *repo, |
| 67be7c5a MH |
71 | const struct object_id *oid, |
| 72 | unsigned int flags); | |
| 73 | ||
| 4cb77009 | 74 | /** |
| 78fb4579 MH |
75 | * Information needed for a single ref update. Set new_oid to the new |
| 76 | * value or to null_oid to delete the ref. To check the old value | |
| 77 | * while the ref is locked, set (flags & REF_HAVE_OLD) and set old_oid | |
| 78 | * to the old value, or to null_oid to ensure the ref does not exist | |
| 79 | * before update. | |
| 4cb77009 MH |
80 | */ |
| 81 | struct ref_update { | |
| 82 | /* | |
| 5ac95fee MH |
83 | * If (flags & REF_HAVE_NEW), set the reference to this value |
| 84 | * (or delete it, if `new_oid` is `null_oid`). | |
| 4cb77009 | 85 | */ |
| 98491298 | 86 | struct object_id new_oid; |
| 6e30b2f6 | 87 | |
| 4cb77009 MH |
88 | /* |
| 89 | * If (flags & REF_HAVE_OLD), check that the reference | |
| 5ac95fee MH |
90 | * previously had this value (or didn't previously exist, if |
| 91 | * `old_oid` is `null_oid`). | |
| 4cb77009 | 92 | */ |
| 98491298 | 93 | struct object_id old_oid; |
| 6e30b2f6 | 94 | |
| 1bc4cc3f KN |
95 | /* |
| 96 | * If set, point the reference to this value. This can also be | |
| 97 | * used to convert regular references to become symbolic refs. | |
| 98 | * Cannot be set together with `new_oid`. | |
| 99 | */ | |
| 100 | const char *new_target; | |
| 101 | ||
| 102 | /* | |
| 103 | * If set, check that the reference previously pointed to this | |
| 104 | * value. Cannot be set together with `old_oid`. | |
| 105 | */ | |
| 106 | const char *old_target; | |
| 107 | ||
| 4cb77009 | 108 | /* |
| 91774afc | 109 | * One or more of REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, |
| 5ac95fee | 110 | * REF_HAVE_NEW, REF_HAVE_OLD, or backend-specific flags. |
| 4cb77009 MH |
111 | */ |
| 112 | unsigned int flags; | |
| 6e30b2f6 | 113 | |
| 7d618264 | 114 | void *backend_data; |
| 92b1551b | 115 | unsigned int type; |
| 4cb77009 | 116 | char *msg; |
| 1a83e26d | 117 | char *committer_info; |
| 6e30b2f6 | 118 | |
| a3582e2e KN |
119 | /* |
| 120 | * The index overrides the default sort algorithm. This is needed | |
| 121 | * when migrating reflogs and we want to ensure we carry over the | |
| 122 | * same order. | |
| 123 | */ | |
| e7c1b9f1 | 124 | uint64_t index; |
| 6e30b2f6 | 125 | |
| 23fc8e4f KN |
126 | /* |
| 127 | * Used in batched reference updates to mark if a given update | |
| 128 | * was rejected. | |
| 129 | */ | |
| 130 | enum ref_transaction_error rejection_err; | |
| 131 | ||
| 6e30b2f6 MH |
132 | /* |
| 133 | * If this ref_update was split off of a symref update via | |
| 134 | * split_symref_update(), then this member points at that | |
| 135 | * update. This is used for two purposes: | |
| 136 | * 1. When reporting errors, we report the refname under which | |
| 137 | * the update was originally requested. | |
| 138 | * 2. When we read the old value of this reference, we | |
| 139 | * propagate it back to its parent update for recording in | |
| 140 | * the latter's reflog. | |
| 141 | */ | |
| 142 | struct ref_update *parent_update; | |
| 143 | ||
| 4cb77009 MH |
144 | const char refname[FLEX_ARRAY]; |
| 145 | }; | |
| 146 | ||
| 8b72fea7 HWN |
147 | int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, |
| 148 | struct object_id *oid, struct strbuf *referent, | |
| 149 | unsigned int *type, int *failure_errno); | |
| 470be518 | 150 | |
| 23fc8e4f KN |
151 | /* |
| 152 | * Mark a given update as rejected with a given reason. | |
| 153 | */ | |
| 154 | int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction, | |
| 155 | size_t update_idx, | |
| 156 | enum ref_transaction_error err); | |
| 157 | ||
| 71564516 MH |
158 | /* |
| 159 | * Add a ref_update with the specified properties to transaction, and | |
| 160 | * return a pointer to the new object. This function does not verify | |
| 78fb4579 | 161 | * that refname is well-formed. new_oid and old_oid are only |
| 71564516 MH |
162 | * dereferenced if the REF_HAVE_NEW and REF_HAVE_OLD bits, |
| 163 | * respectively, are set in flags. | |
| 164 | */ | |
| 165 | struct ref_update *ref_transaction_add_update( | |
| 166 | struct ref_transaction *transaction, | |
| 167 | const char *refname, unsigned int flags, | |
| 89f3bbdd | 168 | const struct object_id *new_oid, |
| 169 | const struct object_id *old_oid, | |
| 1bc4cc3f | 170 | const char *new_target, const char *old_target, |
| 4483be36 | 171 | const char *committer_info, |
| 71564516 MH |
172 | const char *msg); |
| 173 | ||
| 4cb77009 MH |
174 | /* |
| 175 | * Transaction states. | |
| 30173b88 MH |
176 | * |
| 177 | * OPEN: The transaction is initialized and new updates can still be | |
| 178 | * added to it. An OPEN transaction can be prepared, | |
| 179 | * committed, freed, or aborted (freeing and aborting an open | |
| 180 | * transaction are equivalent). | |
| 181 | * | |
| 182 | * PREPARED: ref_transaction_prepare(), which locks all of the | |
| 183 | * references involved in the update and checks that the | |
| 184 | * update has no errors, has been called successfully for the | |
| 185 | * transaction. A PREPARED transaction can be committed or | |
| 186 | * aborted. | |
| 187 | * | |
| 188 | * CLOSED: The transaction is no longer active. A transaction becomes | |
| 189 | * CLOSED if there is a failure while building the transaction | |
| 190 | * or if a transaction is committed or aborted. A CLOSED | |
| 191 | * transaction can only be freed. | |
| 4cb77009 MH |
192 | */ |
| 193 | enum ref_transaction_state { | |
| 30173b88 MH |
194 | REF_TRANSACTION_OPEN = 0, |
| 195 | REF_TRANSACTION_PREPARED = 1, | |
| 196 | REF_TRANSACTION_CLOSED = 2 | |
| 4cb77009 MH |
197 | }; |
| 198 | ||
| 23fc8e4f KN |
199 | /* |
| 200 | * Data structure to hold indices of updates which were rejected, for batched | |
| 201 | * reference updates. While the updates themselves hold the rejection error, | |
| 202 | * this structure allows a transaction to iterate only over the rejected | |
| 203 | * updates. | |
| 204 | */ | |
| 205 | struct ref_transaction_rejections { | |
| 206 | size_t *update_indices; | |
| 207 | size_t alloc; | |
| 208 | size_t nr; | |
| 209 | }; | |
| 210 | ||
| 4cb77009 MH |
211 | /* |
| 212 | * Data structure for holding a reference transaction, which can | |
| 213 | * consist of checks and updates to multiple references, carried out | |
| 214 | * as atomically as possible. This structure is opaque to callers. | |
| 215 | */ | |
| 216 | struct ref_transaction { | |
| c0fe4e8b | 217 | struct ref_store *ref_store; |
| 4cb77009 | 218 | struct ref_update **updates; |
| c3baddf0 | 219 | struct string_list refnames; |
| 4cb77009 MH |
220 | size_t alloc; |
| 221 | size_t nr; | |
| 222 | enum ref_transaction_state state; | |
| 23fc8e4f | 223 | struct ref_transaction_rejections *rejections; |
| 3bf4f561 | 224 | void *backend_data; |
| a0efef14 | 225 | unsigned int flags; |
| e7c1b9f1 | 226 | uint64_t max_index; |
| 4cb77009 MH |
227 | }; |
| 228 | ||
| 0845122c DT |
229 | /* |
| 230 | * Check for entries in extras that are within the specified | |
| 231 | * directory, where dirname is a reference directory name including | |
| 232 | * the trailing slash (e.g., "refs/heads/foo/"). Ignore any | |
| 233 | * conflicting references that are found in skip. If there is a | |
| 234 | * conflicting reference, return its name. | |
| 235 | * | |
| 236 | * extras and skip must be sorted lists of reference names. Either one | |
| 237 | * can be NULL, signifying the empty list. | |
| 238 | */ | |
| 239 | const char *find_descendant_ref(const char *dirname, | |
| 240 | const struct string_list *extras, | |
| 241 | const struct string_list *skip); | |
| 242 | ||
| 2d0663b2 DT |
243 | /* We allow "recursive" symbolic refs. Only within reason, though */ |
| 244 | #define SYMREF_MAXDEPTH 5 | |
| 93770590 | 245 | |
| bf708add | 246 | /* |
| 6bde5d43 KN |
247 | * Data structure for holding a reference iterator. See refs.h for |
| 248 | * more details and usage instructions. | |
| 3bc581b9 MH |
249 | */ |
| 250 | struct ref_iterator { | |
| 251 | struct ref_iterator_vtable *vtable; | |
| 89baa52d | 252 | struct reference ref; |
| 3bc581b9 MH |
253 | }; |
| 254 | ||
| 3bc581b9 MH |
255 | /* |
| 256 | * An iterator over nothing (its first ref_iterator_advance() call | |
| 257 | * returns ITER_DONE). | |
| 258 | */ | |
| 259 | struct ref_iterator *empty_ref_iterator_begin(void); | |
| 260 | ||
| 261 | /* | |
| 262 | * Return true iff ref_iterator is an empty_ref_iterator. | |
| 263 | */ | |
| 264 | int is_empty_ref_iterator(struct ref_iterator *ref_iterator); | |
| 265 | ||
| 266 | /* | |
| 267 | * A callback function used to instruct merge_ref_iterator how to | |
| 268 | * interleave the entries from iter0 and iter1. The function should | |
| 269 | * return one of the constants defined in enum iterator_selection. It | |
| 270 | * must not advance either of the iterators itself. | |
| 271 | * | |
| 272 | * The function must be prepared to handle the case that iter0 and/or | |
| 273 | * iter1 is NULL, which indicates that the corresponding sub-iterator | |
| 274 | * has been exhausted. Its return value must be consistent with the | |
| 275 | * current states of the iterators; e.g., it must not return | |
| 276 | * ITER_SKIP_1 if iter1 has already been exhausted. | |
| 277 | */ | |
| 278 | typedef enum iterator_selection ref_iterator_select_fn( | |
| 279 | struct ref_iterator *iter0, struct ref_iterator *iter1, | |
| 280 | void *cb_data); | |
| 281 | ||
| 6f227800 PS |
282 | /* |
| 283 | * An implementation of ref_iterator_select_fn that merges worktree and common | |
| 284 | * refs. Per-worktree refs from the common iterator are ignored, worktree refs | |
| 285 | * override common refs. Refs are selected lexicographically. | |
| 286 | */ | |
| 287 | enum iterator_selection ref_iterator_select(struct ref_iterator *iter_worktree, | |
| 288 | struct ref_iterator *iter_common, | |
| 289 | void *cb_data); | |
| 290 | ||
| 3bc581b9 MH |
291 | /* |
| 292 | * Iterate over the entries from iter0 and iter1, with the values | |
| 293 | * interleaved as directed by the select function. The iterator takes | |
| 294 | * ownership of iter0 and iter1 and frees them when the iteration is | |
| 5e01d838 | 295 | * over. |
| 3bc581b9 MH |
296 | */ |
| 297 | struct ref_iterator *merge_ref_iterator_begin( | |
| 298 | struct ref_iterator *iter0, struct ref_iterator *iter1, | |
| 299 | ref_iterator_select_fn *select, void *cb_data); | |
| 300 | ||
| 301 | /* | |
| 302 | * An iterator consisting of the union of the entries from front and | |
| 303 | * back. If there are entries common to the two sub-iterators, use the | |
| 304 | * one from front. Each iterator must iterate over its entries in | |
| 305 | * strcmp() order by refname for this to work. | |
| 306 | * | |
| 307 | * The new iterator takes ownership of its arguments and frees them | |
| 308 | * when the iteration is over. As a convenience to callers, if front | |
| 309 | * or back is an empty_ref_iterator, then abort that one immediately | |
| 310 | * and return the other iterator directly, without wrapping it. | |
| 311 | */ | |
| 312 | struct ref_iterator *overlay_ref_iterator_begin( | |
| 313 | struct ref_iterator *front, struct ref_iterator *back); | |
| 314 | ||
| 315 | /* | |
| 316 | * Wrap iter0, only letting through the references whose names start | |
| 317 | * with prefix. If trim is set, set iter->refname to the name of the | |
| 318 | * reference with that many characters trimmed off the front; | |
| 319 | * otherwise set it to the full refname. The new iterator takes over | |
| 320 | * ownership of iter0 and frees it when iteration is over. It makes | |
| 321 | * its own copy of prefix. | |
| 322 | * | |
| 323 | * As an convenience to callers, if prefix is the empty string and | |
| 324 | * trim is zero, this function returns iter0 directly, without | |
| 325 | * wrapping it. | |
| 326 | */ | |
| 327 | struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0, | |
| 328 | const char *prefix, | |
| 329 | int trim); | |
| 330 | ||
| 3bc581b9 MH |
331 | /* Internal implementation of reference iteration: */ |
| 332 | ||
| 333 | /* | |
| 334 | * Base class constructor for ref_iterators. Initialize the | |
| 335 | * ref_iterator part of iter, setting its vtable pointer as specified. | |
| 336 | * This is meant to be called only by the initializers of derived | |
| 337 | * classes. | |
| 338 | */ | |
| 339 | void base_ref_iterator_init(struct ref_iterator *iter, | |
| 5e01d838 | 340 | struct ref_iterator_vtable *vtable); |
| 3bc581b9 | 341 | |
| 3bc581b9 MH |
342 | /* Virtual function declarations for ref_iterators: */ |
| 343 | ||
| 84ee4ca1 HWN |
344 | /* |
| 345 | * backend-specific implementation of ref_iterator_advance. For symrefs, the | |
| 346 | * function should set REF_ISSYMREF, and it should also dereference the symref | |
| 9aab952e JK |
347 | * to provide the OID referent. It should respect do_for_each_ref_flags |
| 348 | * that were passed to refs_ref_iterator_begin(). | |
| 84ee4ca1 | 349 | */ |
| 3bc581b9 MH |
350 | typedef int ref_iterator_advance_fn(struct ref_iterator *ref_iterator); |
| 351 | ||
| 82c39c60 | 352 | /* |
| 2b4648b9 KN |
353 | * Seek the iterator to the first matching reference. If the |
| 354 | * REF_ITERATOR_SEEK_SET_PREFIX flag is set, it would behave the same as if a | |
| 355 | * new iterator was created with the provided refname as prefix. | |
| 82c39c60 PS |
356 | */ |
| 357 | typedef int ref_iterator_seek_fn(struct ref_iterator *ref_iterator, | |
| 2b4648b9 | 358 | const char *refname, unsigned int flags); |
| 82c39c60 | 359 | |
| 3bc581b9 MH |
360 | /* |
| 361 | * Implementations of this function should free any resources specific | |
| cec2b6f5 | 362 | * to the derived class. |
| 3bc581b9 | 363 | */ |
| cec2b6f5 | 364 | typedef void ref_iterator_release_fn(struct ref_iterator *ref_iterator); |
| 3bc581b9 MH |
365 | |
| 366 | struct ref_iterator_vtable { | |
| 367 | ref_iterator_advance_fn *advance; | |
| 82c39c60 | 368 | ref_iterator_seek_fn *seek; |
| cec2b6f5 | 369 | ref_iterator_release_fn *release; |
| 3bc581b9 MH |
370 | }; |
| 371 | ||
| 1a769003 MH |
372 | struct ref_store; |
| 373 | ||
| 0c09ec07 DT |
374 | /* refs backends */ |
| 375 | ||
| 9e7ec634 NTND |
376 | /* ref_store_init flags */ |
| 377 | #define REF_STORE_READ (1 << 0) | |
| 378 | #define REF_STORE_WRITE (1 << 1) /* can perform update operations */ | |
| 379 | #define REF_STORE_ODB (1 << 2) /* has access to object database */ | |
| 380 | #define REF_STORE_MAIN (1 << 3) | |
| 0d8a814d NTND |
381 | #define REF_STORE_ALL_CAPS (REF_STORE_READ | \ |
| 382 | REF_STORE_WRITE | \ | |
| 383 | REF_STORE_ODB | \ | |
| 384 | REF_STORE_MAIN) | |
| 9e7ec634 | 385 | |
| e1e33b72 | 386 | /* |
| 5d0bc90e NTND |
387 | * Initialize the ref_store for the specified gitdir. These functions |
| 388 | * should call base_ref_store_init() to initialize the shared part of | |
| 389 | * the ref_store and to record the ref_store for later lookup. | |
| e1e33b72 | 390 | */ |
| 34224e14 JT |
391 | typedef struct ref_store *ref_store_init_fn(struct repository *repo, |
| 392 | const char *gitdir, | |
| 9e7ec634 | 393 | unsigned int flags); |
| 71c871b4 PS |
394 | /* |
| 395 | * Release all memory and resources associated with the ref store. | |
| 396 | */ | |
| 397 | typedef void ref_store_release_fn(struct ref_store *refs); | |
| e1e33b72 | 398 | |
| ed93ea16 PS |
399 | typedef int ref_store_create_on_disk_fn(struct ref_store *refs, |
| 400 | int flags, | |
| 401 | struct strbuf *err); | |
| 6fb5acfd | 402 | |
| 64a6dd8f PS |
403 | /* |
| 404 | * Remove the reference store from disk. | |
| 405 | */ | |
| 406 | typedef int ref_store_remove_on_disk_fn(struct ref_store *refs, | |
| 407 | struct strbuf *err); | |
| 408 | ||
| 30173b88 MH |
409 | typedef int ref_transaction_prepare_fn(struct ref_store *refs, |
| 410 | struct ref_transaction *transaction, | |
| 411 | struct strbuf *err); | |
| 412 | ||
| 413 | typedef int ref_transaction_finish_fn(struct ref_store *refs, | |
| 414 | struct ref_transaction *transaction, | |
| 415 | struct strbuf *err); | |
| 416 | ||
| 417 | typedef int ref_transaction_abort_fn(struct ref_store *refs, | |
| 418 | struct ref_transaction *transaction, | |
| 419 | struct strbuf *err); | |
| 420 | ||
| e1e33b72 MH |
421 | typedef int ref_transaction_commit_fn(struct ref_store *refs, |
| 422 | struct ref_transaction *transaction, | |
| 423 | struct strbuf *err); | |
| 424 | ||
| 8dfe077f | 425 | typedef int optimize_fn(struct ref_store *ref_store, |
| 2cd99d98 | 426 | struct refs_optimize_opts *opts); |
| f6c5ca38 KN |
427 | |
| 428 | typedef int optimize_required_fn(struct ref_store *ref_store, | |
| 429 | struct refs_optimize_opts *opts, | |
| 430 | bool *required); | |
| 431 | ||
| 9b6b40d9 DT |
432 | typedef int rename_ref_fn(struct ref_store *ref_store, |
| 433 | const char *oldref, const char *newref, | |
| 434 | const char *logmsg); | |
| 52d59cc6 SD |
435 | typedef int copy_ref_fn(struct ref_store *ref_store, |
| 436 | const char *oldref, const char *newref, | |
| 437 | const char *logmsg); | |
| 8231527e | 438 | |
| 1a769003 | 439 | /* |
| e1860571 MH |
440 | * Iterate over the references in `ref_store` whose names start with |
| 441 | * `prefix`. `prefix` is matched as a literal string, without regard | |
| 442 | * for path separators. If prefix is NULL or the empty string, iterate | |
| 8738a8a4 MH |
443 | * over all references in `ref_store`. The output is ordered by |
| 444 | * refname. | |
| 1a769003 MH |
445 | */ |
| 446 | typedef struct ref_iterator *ref_iterator_begin_fn( | |
| 447 | struct ref_store *ref_store, | |
| b269ac53 TB |
448 | const char *prefix, const char **exclude_patterns, |
| 449 | unsigned int flags); | |
| 1a769003 | 450 | |
| e3688bd6 DT |
451 | /* reflog functions */ |
| 452 | ||
| 453 | /* | |
| 454 | * Iterate over the references in the specified ref_store that have a | |
| 455 | * reflog. The refs are iterated over in arbitrary order. | |
| 456 | */ | |
| 457 | typedef struct ref_iterator *reflog_iterator_begin_fn( | |
| 458 | struct ref_store *ref_store); | |
| 459 | ||
| 460 | typedef int for_each_reflog_ent_fn(struct ref_store *ref_store, | |
| 461 | const char *refname, | |
| 462 | each_reflog_ent_fn fn, | |
| 463 | void *cb_data); | |
| 464 | typedef int for_each_reflog_ent_reverse_fn(struct ref_store *ref_store, | |
| 465 | const char *refname, | |
| 466 | each_reflog_ent_fn fn, | |
| 467 | void *cb_data); | |
| 468 | typedef int reflog_exists_fn(struct ref_store *ref_store, const char *refname); | |
| 469 | typedef int create_reflog_fn(struct ref_store *ref_store, const char *refname, | |
| 7b089120 | 470 | struct strbuf *err); |
| e3688bd6 DT |
471 | typedef int delete_reflog_fn(struct ref_store *ref_store, const char *refname); |
| 472 | typedef int reflog_expire_fn(struct ref_store *ref_store, | |
| cc40b5ce | 473 | const char *refname, |
| e3688bd6 DT |
474 | unsigned int flags, |
| 475 | reflog_expiry_prepare_fn prepare_fn, | |
| 476 | reflog_expiry_should_prune_fn should_prune_fn, | |
| 477 | reflog_expiry_cleanup_fn cleanup_fn, | |
| 478 | void *policy_cb_data); | |
| 479 | ||
| cf596442 | 480 | /* |
| 34c7ad8f MH |
481 | * Read a reference from the specified reference store, non-recursively. |
| 482 | * Set type to describe the reference, and: | |
| cf596442 | 483 | * |
| 99afe91a | 484 | * - If refname is the name of a normal reference, fill in oid |
| cf596442 MH |
485 | * (leaving referent unchanged). |
| 486 | * | |
| 487 | * - If refname is the name of a symbolic reference, write the full | |
| 488 | * name of the reference to which it refers (e.g. | |
| 489 | * "refs/heads/master") to referent and set the REF_ISSYMREF bit in | |
| 99afe91a | 490 | * type (leaving oid unchanged). The caller is responsible for |
| cf596442 MH |
491 | * validating that referent is a valid reference name. |
| 492 | * | |
| 493 | * WARNING: refname might be used as part of a filename, so it is | |
| 494 | * important from a security standpoint that it be safe in the sense | |
| 495 | * of refname_is_safe(). Moreover, for symrefs this function sets | |
| 496 | * referent to whatever the repository says, which might not be a | |
| 497 | * properly-formatted or even safe reference name. NEITHER INPUT NOR | |
| 498 | * OUTPUT REFERENCE NAMES ARE VALIDATED WITHIN THIS FUNCTION. | |
| 499 | * | |
| 5b12e16b HWN |
500 | * Return 0 on success, or -1 on failure. If the ref exists but is neither a |
| 501 | * symbolic ref nor an object ID, it is broken. In this case set REF_ISBROKEN in | |
| 502 | * type, and return -1 (failure_errno should not be ENOENT) | |
| 503 | * | |
| 504 | * failure_errno provides errno codes that are interpreted beyond error | |
| 505 | * reporting. The following error codes have special meaning: | |
| 506 | * * ENOENT: the ref doesn't exist | |
| 507 | * * EISDIR: ref name is a directory | |
| 508 | * * ENOTDIR: ref prefix is not a directory | |
| cf596442 MH |
509 | * |
| 510 | * Backend-specific flags might be set in type as well, regardless of | |
| 511 | * outcome. | |
| 512 | * | |
| 513 | * It is OK for refname to point into referent. If so: | |
| 514 | * | |
| 515 | * - if the function succeeds with REF_ISSYMREF, referent will be | |
| 516 | * overwritten and the memory formerly pointed to by it might be | |
| 517 | * changed or even freed. | |
| 518 | * | |
| 519 | * - in all other cases, referent will be untouched, and therefore | |
| 520 | * refname will still be valid and unchanged. | |
| 521 | */ | |
| 5b12e16b HWN |
522 | typedef int read_raw_ref_fn(struct ref_store *ref_store, const char *refname, |
| 523 | struct object_id *oid, struct strbuf *referent, | |
| 524 | unsigned int *type, int *failure_errno); | |
| 127b42a1 | 525 | |
| cd475b3b PS |
526 | /* |
| 527 | * Read a symbolic reference from the specified reference store. This function | |
| 528 | * is optional: if not implemented by a backend, then `read_raw_ref_fn` is used | |
| 529 | * to read the symbolcic reference instead. It is intended to be implemented | |
| 530 | * only in case the backend can optimize the reading of symbolic references. | |
| 531 | * | |
| 532 | * Return 0 on success, or -1 on failure. `referent` will be set to the target | |
| 533 | * of the symbolic reference on success. This function explicitly does not | |
| 534 | * distinguish between error cases and the reference not being a symbolic | |
| 535 | * reference to allow backends to optimize this operation in case symbolic and | |
| 536 | * non-symbolic references are treated differently. | |
| 537 | */ | |
| 538 | typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refname, | |
| 539 | struct strbuf *referent); | |
| 540 | ||
| ab6f79d8 | 541 | typedef int fsck_fn(struct ref_store *ref_store, |
| 7c78d819 | 542 | struct fsck_options *o, |
| 543 | struct worktree *wt); | |
| ab6f79d8 | 544 | |
| 3dce444f | 545 | struct ref_storage_be { |
| 3dce444f | 546 | const char *name; |
| 00eebe35 | 547 | ref_store_init_fn *init; |
| 71c871b4 | 548 | ref_store_release_fn *release; |
| ed93ea16 | 549 | ref_store_create_on_disk_fn *create_on_disk; |
| 64a6dd8f | 550 | ref_store_remove_on_disk_fn *remove_on_disk; |
| 30173b88 MH |
551 | |
| 552 | ref_transaction_prepare_fn *transaction_prepare; | |
| 553 | ref_transaction_finish_fn *transaction_finish; | |
| 554 | ref_transaction_abort_fn *transaction_abort; | |
| e1e33b72 | 555 | |
| 8dfe077f | 556 | optimize_fn *optimize; |
| f6c5ca38 | 557 | optimize_required_fn *optimize_required; |
| 9b6b40d9 | 558 | rename_ref_fn *rename_ref; |
| 52d59cc6 | 559 | copy_ref_fn *copy_ref; |
| 8231527e | 560 | |
| 1a769003 | 561 | ref_iterator_begin_fn *iterator_begin; |
| e1e33b72 | 562 | read_raw_ref_fn *read_raw_ref; |
| 8102d10f BF |
563 | |
| 564 | /* | |
| 565 | * Please refer to `refs_read_symbolic_ref()` for the expected | |
| 566 | * behaviour. | |
| 567 | */ | |
| cd475b3b | 568 | read_symbolic_ref_fn *read_symbolic_ref; |
| e3688bd6 DT |
569 | |
| 570 | reflog_iterator_begin_fn *reflog_iterator_begin; | |
| 571 | for_each_reflog_ent_fn *for_each_reflog_ent; | |
| 572 | for_each_reflog_ent_reverse_fn *for_each_reflog_ent_reverse; | |
| 573 | reflog_exists_fn *reflog_exists; | |
| 574 | create_reflog_fn *create_reflog; | |
| 575 | delete_reflog_fn *delete_reflog; | |
| 576 | reflog_expire_fn *reflog_expire; | |
| ab6f79d8 | 577 | |
| 578 | fsck_fn *fsck; | |
| 3dce444f RS |
579 | }; |
| 580 | ||
| 581 | extern struct ref_storage_be refs_be_files; | |
| 57db2a09 | 582 | extern struct ref_storage_be refs_be_reftable; |
| e0cc8ac8 | 583 | extern struct ref_storage_be refs_be_packed; |
| 3dce444f | 584 | |
| 00eebe35 MH |
585 | /* |
| 586 | * A representation of the reference store for the main repository or | |
| 587 | * a submodule. The ref_store instances for submodules are kept in a | |
| 965f8991 | 588 | * hash map; see repo_get_submodule_ref_store() for more info. |
| 00eebe35 MH |
589 | */ |
| 590 | struct ref_store { | |
| 591 | /* The backend describing this ref_store's storage scheme: */ | |
| 592 | const struct ref_storage_be *be; | |
| 5085aef4 | 593 | |
| 34224e14 JT |
594 | struct repository *repo; |
| 595 | ||
| 596 | /* | |
| 597 | * The gitdir that this ref_store applies to. Note that this is not | |
| 598 | * necessarily repo->gitdir if the repo has multiple worktrees. | |
| 599 | */ | |
| 5085aef4 | 600 | char *gitdir; |
| 00eebe35 MH |
601 | }; |
| 602 | ||
| e39620f0 | 603 | /* |
| df3458e9 HWN |
604 | * Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for |
| 605 | * invalid contents. | |
| e39620f0 | 606 | */ |
| 080b068f PS |
607 | int parse_loose_ref_contents(const struct git_hash_algo *algop, |
| 608 | const char *buf, struct object_id *oid, | |
| df3458e9 | 609 | struct strbuf *referent, unsigned int *type, |
| 1c0e2a00 | 610 | const char **trailing, int *failure_errno); |
| e39620f0 | 611 | |
| 00eebe35 | 612 | /* |
| fbfd0a29 MH |
613 | * Fill in the generic part of refs and add it to our collection of |
| 614 | * reference stores. | |
| 00eebe35 | 615 | */ |
| f9f7fd3b HWN |
616 | void base_ref_store_init(struct ref_store *refs, struct repository *repo, |
| 617 | const char *path, const struct ref_storage_be *be); | |
| 00eebe35 | 618 | |
| 4441f427 HWN |
619 | /* |
| 620 | * Support GIT_TRACE_REFS by optionally wrapping the given ref_store instance. | |
| 621 | */ | |
| 622 | struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store); | |
| 623 | ||
| e9965ba4 KN |
624 | /* |
| 625 | * Return the refname under which update was originally requested. | |
| 626 | */ | |
| 627 | const char *ref_update_original_update_refname(struct ref_update *update); | |
| 628 | ||
| 644daf77 KN |
629 | /* |
| 630 | * Helper function to check if the new value is null, this | |
| 631 | * takes into consideration that the update could be a regular | |
| 632 | * ref or a symbolic ref. | |
| 633 | */ | |
| 634 | int ref_update_has_null_new_value(struct ref_update *update); | |
| 635 | ||
| 636 | /* | |
| 637 | * Check whether the old_target values stored in update are consistent | |
| 638 | * with the referent, which is the symbolic reference's current value. | |
| 639 | * If everything is OK, return 0; otherwise, write an error message to | |
| 640 | * err and return -1. | |
| 641 | */ | |
| 76e760b9 KN |
642 | enum ref_transaction_error ref_update_check_old_target(const char *referent, |
| 643 | struct ref_update *update, | |
| 644 | struct strbuf *err); | |
| 644daf77 | 645 | |
| aba381c0 KN |
646 | /* |
| 647 | * Check if the ref must exist, this means that the old_oid or | |
| 046c6732 PS |
648 | * old_target is non NULL. Log-only updates never require the old state to |
| 649 | * match. | |
| aba381c0 KN |
650 | */ |
| 651 | int ref_update_expects_existing_old_ref(struct ref_update *update); | |
| 652 | ||
| 31726bb9 KN |
653 | /* |
| 654 | * Same as `refs_verify_refname_available()`, but checking for a list of | |
| 655 | * refnames instead of only a single item. This is more efficient in the case | |
| 656 | * where one needs to check multiple refnames. | |
| 657 | * | |
| 658 | * If using batched updates, then individual updates are marked rejected, | |
| 659 | * reference backends are then in charge of not committing those updates. | |
| 660 | */ | |
| 661 | enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs, | |
| 662 | const struct string_list *refnames, | |
| 663 | const struct string_list *extras, | |
| 664 | const struct string_list *skip, | |
| 665 | struct ref_transaction *transaction, | |
| 666 | unsigned int initial_transaction, | |
| 667 | struct strbuf *err); | |
| 668 | ||
| 4cb77009 | 669 | #endif /* REFS_REFS_INTERNAL_H */ |