1 #ifndef REFS_REFS_INTERNAL_H
2 #define REFS_REFS_INTERNAL_H
6 #include "string-list.h"
9 struct ref_transaction
;
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.
18 * The following flags can appear in `ref_update::flags`. Their
19 * numerical values must not conflict with those of REF_NO_DEREF and
20 * REF_FORCE_CREATE_REFLOG, which are also stored in
21 * `ref_update::flags`.
25 * The reference should be updated to new_oid.
27 #define REF_HAVE_NEW (1 << 2)
30 * The current reference's value should be checked to make sure that
31 * it agrees with old_oid.
33 #define REF_HAVE_OLD (1 << 3)
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.
40 #define REF_LOG_ONLY (1 << 7)
43 * Return the length of time to retry acquiring a loose reference lock
44 * before giving up, in milliseconds:
46 long get_files_ref_lock_timeout_ms(void);
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().
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").
62 int refname_is_safe(const char *refname
);
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.
69 int ref_resolves_to_object(const char *refname
,
70 struct repository
*repo
,
71 const struct object_id
*oid
,
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
83 * If (flags & REF_HAVE_NEW), set the reference to this value
84 * (or delete it, if `new_oid` is `null_oid`).
86 struct object_id new_oid
;
89 * If (flags & REF_HAVE_OLD), check that the reference
90 * previously had this value (or didn't previously exist, if
91 * `old_oid` is `null_oid`).
93 struct object_id old_oid
;
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`.
100 const char *new_target
;
103 * If set, check that the reference previously pointed to this
104 * value. Cannot be set together with `old_oid`.
106 const char *old_target
;
109 * One or more of REF_NO_DEREF, REF_FORCE_CREATE_REFLOG,
110 * REF_HAVE_NEW, REF_HAVE_OLD, or backend-specific flags.
117 char *committer_info
;
120 * The index overrides the default sort algorithm. This is needed
121 * when migrating reflogs and we want to ensure we carry over the
127 * Used in batched reference updates to mark if a given update
130 enum ref_transaction_error rejection_err
;
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.
142 struct ref_update
*parent_update
;
144 const char refname
[FLEX_ARRAY
];
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
);
152 * Mark a given update as rejected with a given reason.
154 int ref_transaction_maybe_set_rejected(struct ref_transaction
*transaction
,
156 enum ref_transaction_error err
);
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
161 * that refname is well-formed. new_oid and old_oid are only
162 * dereferenced if the REF_HAVE_NEW and REF_HAVE_OLD bits,
163 * respectively, are set in flags.
165 struct ref_update
*ref_transaction_add_update(
166 struct ref_transaction
*transaction
,
167 const char *refname
, unsigned int flags
,
168 const struct object_id
*new_oid
,
169 const struct object_id
*old_oid
,
170 const char *new_target
, const char *old_target
,
171 const char *committer_info
,
175 * Transaction states.
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).
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
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.
193 enum ref_transaction_state
{
194 REF_TRANSACTION_OPEN
= 0,
195 REF_TRANSACTION_PREPARED
= 1,
196 REF_TRANSACTION_CLOSED
= 2
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
205 struct ref_transaction_rejections
{
206 size_t *update_indices
;
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.
216 struct ref_transaction
{
217 struct ref_store
*ref_store
;
218 struct ref_update
**updates
;
219 struct string_list refnames
;
222 enum ref_transaction_state state
;
223 struct ref_transaction_rejections
*rejections
;
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.
236 * extras and skip must be sorted lists of reference names. Either one
237 * can be NULL, signifying the empty list.
239 const char *find_descendant_ref(const char *dirname
,
240 const struct string_list
*extras
,
241 const struct string_list
*skip
);
243 /* We allow "recursive" symbolic refs. Only within reason, though */
244 #define SYMREF_MAXDEPTH 5
247 * Data structure for holding a reference iterator. See refs.h for
248 * more details and usage instructions.
250 struct ref_iterator
{
251 struct ref_iterator_vtable
*vtable
;
252 struct reference ref
;
256 * An iterator over nothing (its first ref_iterator_advance() call
257 * returns ITER_DONE).
259 struct ref_iterator
*empty_ref_iterator_begin(void);
262 * Return true iff ref_iterator is an empty_ref_iterator.
264 int is_empty_ref_iterator(struct ref_iterator
*ref_iterator
);
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.
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.
278 typedef enum iterator_selection
ref_iterator_select_fn(
279 struct ref_iterator
*iter0
, struct ref_iterator
*iter1
,
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.
287 enum iterator_selection
ref_iterator_select(struct ref_iterator
*iter_worktree
,
288 struct ref_iterator
*iter_common
,
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
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
);
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.
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.
312 struct ref_iterator
*overlay_ref_iterator_begin(
313 struct ref_iterator
*front
, struct ref_iterator
*back
);
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.
323 * As an convenience to callers, if prefix is the empty string and
324 * trim is zero, this function returns iter0 directly, without
327 struct ref_iterator
*prefix_ref_iterator_begin(struct ref_iterator
*iter0
,
331 /* Internal implementation of reference iteration: */
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
339 void base_ref_iterator_init(struct ref_iterator
*iter
,
340 struct ref_iterator_vtable
*vtable
);
342 /* Virtual function declarations for ref_iterators: */
345 * backend-specific implementation of ref_iterator_advance. For symrefs, the
346 * function should set REF_ISSYMREF, and it should also dereference the symref
347 * to provide the OID referent. It should respect do_for_each_ref_flags
348 * that were passed to refs_ref_iterator_begin().
350 typedef int ref_iterator_advance_fn(struct ref_iterator
*ref_iterator
);
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.
357 typedef int ref_iterator_seek_fn(struct ref_iterator
*ref_iterator
,
358 const char *refname
, unsigned int flags
);
361 * Implementations of this function should free any resources specific
362 * to the derived class.
364 typedef void ref_iterator_release_fn(struct ref_iterator
*ref_iterator
);
366 struct ref_iterator_vtable
{
367 ref_iterator_advance_fn
*advance
;
368 ref_iterator_seek_fn
*seek
;
369 ref_iterator_release_fn
*release
;
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)
381 #define REF_STORE_ALL_CAPS (REF_STORE_READ | \
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.
391 typedef struct ref_store
*ref_store_init_fn(struct repository
*repo
,
395 * Release all memory and resources associated with the ref store.
397 typedef void ref_store_release_fn(struct ref_store
*refs
);
399 typedef int ref_store_create_on_disk_fn(struct ref_store
*refs
,
404 * Remove the reference store from disk.
406 typedef int ref_store_remove_on_disk_fn(struct ref_store
*refs
,
409 typedef int ref_transaction_prepare_fn(struct ref_store
*refs
,
410 struct ref_transaction
*transaction
,
413 typedef int ref_transaction_finish_fn(struct ref_store
*refs
,
414 struct ref_transaction
*transaction
,
417 typedef int ref_transaction_abort_fn(struct ref_store
*refs
,
418 struct ref_transaction
*transaction
,
421 typedef int ref_transaction_commit_fn(struct ref_store
*refs
,
422 struct ref_transaction
*transaction
,
425 typedef int optimize_fn(struct ref_store
*ref_store
,
426 struct refs_optimize_opts
*opts
);
428 typedef int optimize_required_fn(struct ref_store
*ref_store
,
429 struct refs_optimize_opts
*opts
,
432 typedef int rename_ref_fn(struct ref_store
*ref_store
,
433 const char *oldref
, const char *newref
,
435 typedef int copy_ref_fn(struct ref_store
*ref_store
,
436 const char *oldref
, const char *newref
,
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
443 * over all references in `ref_store`. The output is ordered by
446 typedef struct ref_iterator
*ref_iterator_begin_fn(
447 struct ref_store
*ref_store
,
448 const char *prefix
, const char **exclude_patterns
,
451 /* reflog functions */
454 * Iterate over the references in the specified ref_store that have a
455 * reflog. The refs are iterated over in arbitrary order.
457 typedef struct ref_iterator
*reflog_iterator_begin_fn(
458 struct ref_store
*ref_store
);
460 typedef int for_each_reflog_ent_fn(struct ref_store
*ref_store
,
462 each_reflog_ent_fn fn
,
464 typedef int for_each_reflog_ent_reverse_fn(struct ref_store
*ref_store
,
466 each_reflog_ent_fn fn
,
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
,
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
,
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
);
481 * Read a reference from the specified reference store, non-recursively.
482 * Set type to describe the reference, and:
484 * - If refname is the name of a normal reference, fill in oid
485 * (leaving referent unchanged).
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
490 * type (leaving oid unchanged). The caller is responsible for
491 * validating that referent is a valid reference name.
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.
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)
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
510 * Backend-specific flags might be set in type as well, regardless of
513 * It is OK for refname to point into referent. If so:
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.
519 * - in all other cases, referent will be untouched, and therefore
520 * refname will still be valid and unchanged.
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
);
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.
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.
538 typedef int read_symbolic_ref_fn(struct ref_store
*ref_store
, const char *refname
,
539 struct strbuf
*referent
);
541 typedef int fsck_fn(struct ref_store
*ref_store
,
542 struct fsck_options
*o
,
543 struct worktree
*wt
);
545 struct ref_storage_be
{
547 ref_store_init_fn
*init
;
548 ref_store_release_fn
*release
;
549 ref_store_create_on_disk_fn
*create_on_disk
;
550 ref_store_remove_on_disk_fn
*remove_on_disk
;
552 ref_transaction_prepare_fn
*transaction_prepare
;
553 ref_transaction_finish_fn
*transaction_finish
;
554 ref_transaction_abort_fn
*transaction_abort
;
556 optimize_fn
*optimize
;
557 optimize_required_fn
*optimize_required
;
558 rename_ref_fn
*rename_ref
;
559 copy_ref_fn
*copy_ref
;
561 ref_iterator_begin_fn
*iterator_begin
;
562 read_raw_ref_fn
*read_raw_ref
;
565 * Please refer to `refs_read_symbolic_ref()` for the expected
568 read_symbolic_ref_fn
*read_symbolic_ref
;
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
;
581 extern struct ref_storage_be refs_be_files
;
582 extern struct ref_storage_be refs_be_reftable
;
583 extern struct ref_storage_be refs_be_packed
;
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
588 * hash map; see repo_get_submodule_ref_store() for more info.
591 /* The backend describing this ref_store's storage scheme: */
592 const struct ref_storage_be
*be
;
594 struct repository
*repo
;
597 * The gitdir that this ref_store applies to. Note that this is not
598 * necessarily repo->gitdir if the repo has multiple worktrees.
604 * Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for
607 int parse_loose_ref_contents(const struct git_hash_algo
*algop
,
608 const char *buf
, struct object_id
*oid
,
609 struct strbuf
*referent
, unsigned int *type
,
610 const char **trailing
, int *failure_errno
);
613 * Fill in the generic part of refs and add it to our collection of
616 void base_ref_store_init(struct ref_store
*refs
, struct repository
*repo
,
617 const char *path
, const struct ref_storage_be
*be
);
620 * Support GIT_TRACE_REFS by optionally wrapping the given ref_store instance.
622 struct ref_store
*maybe_debug_wrap_ref_store(const char *gitdir
, struct ref_store
*store
);
625 * Return the refname under which update was originally requested.
627 const char *ref_update_original_update_refname(struct ref_update
*update
);
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.
634 int ref_update_has_null_new_value(struct ref_update
*update
);
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
642 enum ref_transaction_error
ref_update_check_old_target(const char *referent
,
643 struct ref_update
*update
,
647 * Check if the ref must exist, this means that the old_oid or
648 * old_target is non NULL. Log-only updates never require the old state to
651 int ref_update_expects_existing_old_ref(struct ref_update
*update
);
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.
658 * If using batched updates, then individual updates are marked rejected,
659 * reference backends are then in charge of not committing those updates.
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
,
669 #endif /* REFS_REFS_INTERNAL_H */