blob: 53a6c5d84200a9b5efc80d4dfcf649aa86d1747e [file] [log] [blame]
Michael Haggerty4cb77002015-11-10 12:42:36 +01001#ifndef REFS_REFS_INTERNAL_H
2#define REFS_REFS_INTERNAL_H
3
Ramsay Jones4eb44162018-09-19 01:12:47 +01004#include "refs.h"
Junio C Hamano13f925f2018-07-09 14:36:12 -07005#include "iterator.h"
Beat Bolli91c2f202018-07-09 21:25:33 +02006
Ramsay Jones4eb44162018-09-19 01:12:47 +01007struct ref_transaction;
8
Michael Haggerty4cb77002015-11-10 12:42:36 +01009/*
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 Haggerty5ac95fe2017-11-05 09:42:05 +010016 * The following flags can appear in `ref_update::flags`. Their
Michael Haggerty91774af2017-11-05 09:42:06 +010017 * numerical values must not conflict with those of REF_NO_DEREF and
Michael Haggerty5ac95fe2017-11-05 09:42:05 +010018 * REF_FORCE_CREATE_REFLOG, which are also stored in
19 * `ref_update::flags`.
Michael Haggerty4cb77002015-11-10 12:42:36 +010020 */
21
22/*
Michael Haggerty78fb4572017-11-05 09:42:09 +010023 * The reference should be updated to new_oid.
David Turnerd99aa882016-02-24 17:58:50 -050024 */
Michael Haggerty5ac95fe2017-11-05 09:42:05 +010025#define REF_HAVE_NEW (1 << 2)
David Turnerd99aa882016-02-24 17:58:50 -050026
27/*
Michael Haggerty5ac95fe2017-11-05 09:42:05 +010028 * The current reference's value should be checked to make sure that
Michael Haggerty78fb4572017-11-05 09:42:09 +010029 * it agrees with old_oid.
Michael Haggerty92b15512016-04-25 15:56:07 +020030 */
Michael Haggerty5ac95fe2017-11-05 09:42:05 +010031#define REF_HAVE_OLD (1 << 3)
Michael Haggerty44639772017-01-06 17:22:43 +010032
33/*
Han-Wen Nienhuys63c05672020-08-28 15:25:33 +000034 * 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 Haggerty4ff0f012017-08-21 13:51:34 +020041 * Return the length of time to retry acquiring a loose reference lock
42 * before giving up, in milliseconds:
43 */
44long get_files_ref_lock_timeout_ms(void);
45
46/*
Michael Haggerty4cb77002015-11-10 12:42:36 +010047 * 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 Haggerty15ee2c72017-01-06 17:22:22 +010053 * 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 Haggerty4cb77002015-11-10 12:42:36 +010059 */
60int refname_is_safe(const char *refname);
61
Michael Haggerty67be7c52017-06-23 09:01:37 +020062/*
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 */
67int ref_resolves_to_object(const char *refname,
Jonathan Tan9bc45a22021-10-08 14:08:15 -070068 struct repository *repo,
Michael Haggerty67be7c52017-06-23 09:01:37 +020069 const struct object_id *oid,
70 unsigned int flags);
71
Michael Haggerty4cb77002015-11-10 12:42:36 +010072enum 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. carlsonac2ed0d2017-10-15 22:07:10 +0000100 * result to oid and return PEEL_PEELED. If the object is not a tag
Michael Haggerty4cb77002015-11-10 12:42:36 +0100101 * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively,
Michael Haggerty78fb4572017-11-05 09:42:09 +0100102 * and leave oid unchanged.
Michael Haggerty4cb77002015-11-10 12:42:36 +0100103 */
brian m. carlsonac2ed0d2017-10-15 22:07:10 +0000104enum peel_status peel_object(const struct object_id *name, struct object_id *oid);
Michael Haggerty4cb77002015-11-10 12:42:36 +0100105
Michael Haggerty4cb77002015-11-10 12:42:36 +0100106/**
Michael Haggerty78fb4572017-11-05 09:42:09 +0100107 * 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 Haggerty4cb77002015-11-10 12:42:36 +0100112 */
113struct ref_update {
114 /*
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100115 * If (flags & REF_HAVE_NEW), set the reference to this value
116 * (or delete it, if `new_oid` is `null_oid`).
Michael Haggerty4cb77002015-11-10 12:42:36 +0100117 */
brian m. carlson98491292017-05-06 22:10:23 +0000118 struct object_id new_oid;
Michael Haggerty6e30b2f2016-04-25 17:48:32 +0200119
Michael Haggerty4cb77002015-11-10 12:42:36 +0100120 /*
121 * If (flags & REF_HAVE_OLD), check that the reference
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100122 * previously had this value (or didn't previously exist, if
123 * `old_oid` is `null_oid`).
Michael Haggerty4cb77002015-11-10 12:42:36 +0100124 */
brian m. carlson98491292017-05-06 22:10:23 +0000125 struct object_id old_oid;
Michael Haggerty6e30b2f2016-04-25 17:48:32 +0200126
Michael Haggerty4cb77002015-11-10 12:42:36 +0100127 /*
Karthik Nayak1bc4cc32024-05-07 14:58:52 +0200128 * 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 Haggerty91774af2017-11-05 09:42:06 +0100141 * One or more of REF_NO_DEREF, REF_FORCE_CREATE_REFLOG,
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100142 * REF_HAVE_NEW, REF_HAVE_OLD, or backend-specific flags.
Michael Haggerty4cb77002015-11-10 12:42:36 +0100143 */
144 unsigned int flags;
Michael Haggerty6e30b2f2016-04-25 17:48:32 +0200145
David Turner7d618262016-09-04 18:08:43 +0200146 void *backend_data;
Michael Haggerty92b15512016-04-25 15:56:07 +0200147 unsigned int type;
Michael Haggerty4cb77002015-11-10 12:42:36 +0100148 char *msg;
Michael Haggerty6e30b2f2016-04-25 17:48:32 +0200149
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 Haggerty4cb77002015-11-10 12:42:36 +0100162 const char refname[FLEX_ARRAY];
163};
164
Han-Wen Nienhuys8b72fea2021-10-16 11:39:09 +0200165int 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 Haggerty470be512017-03-20 17:33:07 +0100168
Michael Haggerty4cb77002015-11-10 12:42:36 +0100169/*
Michael Haggerty2ced1052017-05-22 16:17:45 +0200170 * 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 */
174int ref_update_reject_duplicates(struct string_list *refnames,
175 struct strbuf *err);
176
177/*
Michael Haggerty71564512016-04-25 11:39:54 +0200178 * 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 Haggerty78fb4572017-11-05 09:42:09 +0100180 * that refname is well-formed. new_oid and old_oid are only
Michael Haggerty71564512016-04-25 11:39:54 +0200181 * dereferenced if the REF_HAVE_NEW and REF_HAVE_OLD bits,
182 * respectively, are set in flags.
183 */
184struct ref_update *ref_transaction_add_update(
185 struct ref_transaction *transaction,
186 const char *refname, unsigned int flags,
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000187 const struct object_id *new_oid,
188 const struct object_id *old_oid,
Karthik Nayak1bc4cc32024-05-07 14:58:52 +0200189 const char *new_target, const char *old_target,
Michael Haggerty71564512016-04-25 11:39:54 +0200190 const char *msg);
191
192/*
Michael Haggerty4cb77002015-11-10 12:42:36 +0100193 * Transaction states.
Michael Haggerty30173b82017-05-22 16:17:44 +0200194 *
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 Haggerty4cb77002015-11-10 12:42:36 +0100210 */
211enum ref_transaction_state {
Michael Haggerty30173b82017-05-22 16:17:44 +0200212 REF_TRANSACTION_OPEN = 0,
213 REF_TRANSACTION_PREPARED = 1,
214 REF_TRANSACTION_CLOSED = 2
Michael Haggerty4cb77002015-11-10 12:42:36 +0100215};
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 */
222struct ref_transaction {
Nguyễn Thái Ngọc Duyc0fe4e82017-03-26 09:42:35 +0700223 struct ref_store *ref_store;
Michael Haggerty4cb77002015-11-10 12:42:36 +0100224 struct ref_update **updates;
225 size_t alloc;
226 size_t nr;
227 enum ref_transaction_state state;
Michael Haggerty3bf4f562017-09-08 15:51:44 +0200228 void *backend_data;
Michael Haggerty4cb77002015-11-10 12:42:36 +0100229};
230
David Turner08451222015-11-10 12:42:40 +0100231/*
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 */
241const char *find_descendant_ref(const char *dirname,
242 const struct string_list *extras,
243 const struct string_list *skip);
244
David Turner2d0663b2016-04-07 15:03:10 -0400245/* We allow "recursive" symbolic refs. Only within reason, though */
246#define SYMREF_MAXDEPTH 5
David Turner93770592016-04-07 15:02:49 -0400247
David Turner93770592016-04-07 15:02:49 -0400248/*
Jeff King9aab9522021-09-24 14:39:44 -0400249 * These flags are passed to refs_ref_iterator_begin() (and do_for_each_ref(),
250 * which feeds it).
Jeff Kingbf708ad2021-09-24 14:37:58 -0400251 */
Jeff King9aab9522021-09-24 14:39:44 -0400252enum 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 King8dccb222021-09-24 14:41:32 -0400271
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 Nayakd0f00c12024-02-23 11:01:10 +0100277
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 King9aab9522021-09-24 14:39:44 -0400283};
David Turner93770592016-04-07 15:02:49 -0400284
285/*
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200286 * 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 Newren15beaaa2019-11-05 17:07:23 +0000300 * associated with the iteration, frees the ref_iterator object, and
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200301 * 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 Qingyun7b6057c2018-09-15 10:15:46 +0800324 * printf("%s is %s\n", iter->refname, oid_to_hex(iter->oid));
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200325 *
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 */
333struct 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 */
348int 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 */
354int 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 */
362int 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 */
368struct ref_iterator *empty_ref_iterator_begin(void);
369
370/*
371 * Return true iff ref_iterator is an empty_ref_iterator.
372 */
373int is_empty_ref_iterator(struct ref_iterator *ref_iterator);
374
375/*
Michael Haggertye121b9c2017-03-20 17:33:08 +0100376 * 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 Nienhuys84ee4ca2020-05-20 17:36:09 +0000378 * trim that many characters off the beginning of each refname.
Jeff King9aab9522021-09-24 14:39:44 -0400379 * The output is ordered by refname.
Michael Haggertye121b9c2017-03-20 17:33:08 +0100380 */
381struct ref_iterator *refs_ref_iterator_begin(
382 struct ref_store *refs,
Taylor Blaub269ac52023-07-10 17:12:22 -0400383 const char *prefix, const char **exclude_patterns,
384 int trim, enum do_for_each_ref_flags flags);
Michael Haggertye121b9c2017-03-20 17:33:08 +0100385
386/*
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200387 * 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 */
398typedef enum iterator_selection ref_iterator_select_fn(
399 struct ref_iterator *iter0, struct ref_iterator *iter1,
400 void *cb_data);
401
402/*
Patrick Steinhardt6f227802024-02-21 13:37:31 +0100403 * 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 */
407enum iterator_selection ref_iterator_select(struct ref_iterator *iter_worktree,
408 struct ref_iterator *iter_common,
409 void *cb_data);
410
411/*
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200412 * 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 Steinhardt5e01d832024-02-21 13:37:35 +0100415 * over.
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200416 */
417struct 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 */
432struct 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 */
447struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
448 const char *prefix,
449 int trim);
450
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200451/* 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 */
459void base_ref_iterator_init(struct ref_iterator *iter,
Patrick Steinhardt5e01d832024-02-21 13:37:35 +0100460 struct ref_iterator_vtable *vtable);
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200461
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 */
467void base_ref_iterator_free(struct ref_iterator *iter);
468
469/* Virtual function declarations for ref_iterators: */
470
Han-Wen Nienhuys84ee4ca2020-05-20 17:36:09 +0000471/*
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 King9aab9522021-09-24 14:39:44 -0400474 * to provide the OID referent. It should respect do_for_each_ref_flags
475 * that were passed to refs_ref_iterator_begin().
Han-Wen Nienhuys84ee4ca2020-05-20 17:36:09 +0000476 */
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200477typedef int ref_iterator_advance_fn(struct ref_iterator *ref_iterator);
478
Han-Wen Nienhuys617480d2021-05-19 15:31:28 +0000479/*
480 * Peels the current ref, returning 0 for success or -1 for failure.
481 */
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200482typedef 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 */
490typedef int ref_iterator_abort_fn(struct ref_iterator *ref_iterator);
491
492struct 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 Haggerty4c4de892016-06-18 06:15:16 +0200499 * 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 Turner93770592016-04-07 15:02:49 -0400508 */
Michael Haggerty4c4de892016-06-18 06:15:16 +0200509extern 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 Beller4a6067c2018-08-20 18:24:16 +0000520int do_for_each_repo_ref_iterator(struct repository *r,
521 struct ref_iterator *iter,
522 each_repo_ref_fn fn, void *cb_data);
David Turner2d0663b2016-04-07 15:03:10 -0400523
Michael Haggerty1a769002016-09-04 18:08:37 +0200524struct ref_store;
525
David Turner0c09ec02016-09-04 18:08:44 +0200526/* refs backends */
527
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700528/* 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 Duy0d8a8142017-04-24 17:01:21 +0700533#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 Duy9e7ec632017-03-26 09:42:32 +0700537
Michael Haggertye1e33b72016-09-04 18:08:25 +0200538/*
Nguyễn Thái Ngọc Duy5d0bc902017-03-26 09:42:31 +0700539 * 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 Haggertye1e33b72016-09-04 18:08:25 +0200542 */
Jonathan Tan34224e12021-10-08 14:08:14 -0700543typedef struct ref_store *ref_store_init_fn(struct repository *repo,
544 const char *gitdir,
Nguyễn Thái Ngọc Duy9e7ec632017-03-26 09:42:32 +0700545 unsigned int flags);
Michael Haggertye1e33b72016-09-04 18:08:25 +0200546
Patrick Steinhardt2e573d62024-01-08 11:05:26 +0100547typedef int ref_init_db_fn(struct ref_store *refs,
548 int flags,
549 struct strbuf *err);
David Turner6fb5acf2016-09-04 18:08:41 +0200550
Michael Haggerty30173b82017-05-22 16:17:44 +0200551typedef int ref_transaction_prepare_fn(struct ref_store *refs,
552 struct ref_transaction *transaction,
553 struct strbuf *err);
554
555typedef int ref_transaction_finish_fn(struct ref_store *refs,
556 struct ref_transaction *transaction,
557 struct strbuf *err);
558
559typedef int ref_transaction_abort_fn(struct ref_store *refs,
560 struct ref_transaction *transaction,
561 struct strbuf *err);
562
Michael Haggertye1e33b72016-09-04 18:08:25 +0200563typedef int ref_transaction_commit_fn(struct ref_store *refs,
564 struct ref_transaction *transaction,
565 struct strbuf *err);
566
John Cai826ae792023-05-12 21:34:41 +0000567typedef int pack_refs_fn(struct ref_store *ref_store,
568 struct pack_refs_opts *opts);
David Turner9b6b40d2016-09-04 18:08:42 +0200569typedef int rename_ref_fn(struct ref_store *ref_store,
570 const char *oldref, const char *newref,
571 const char *logmsg);
Sahil Dua52d59cc2017-06-18 23:19:16 +0200572typedef int copy_ref_fn(struct ref_store *ref_store,
573 const char *oldref, const char *newref,
574 const char *logmsg);
Michael Haggerty82315272016-09-04 18:08:27 +0200575
Michael Haggertycf596442016-05-06 17:25:31 +0200576/*
Michael Haggertye1860572017-05-22 16:17:33 +0200577 * 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 Haggerty8738a8a2017-09-13 19:15:55 +0200580 * over all references in `ref_store`. The output is ordered by
581 * refname.
Michael Haggerty1a769002016-09-04 18:08:37 +0200582 */
583typedef struct ref_iterator *ref_iterator_begin_fn(
584 struct ref_store *ref_store,
Taylor Blaub269ac52023-07-10 17:12:22 -0400585 const char *prefix, const char **exclude_patterns,
586 unsigned int flags);
Michael Haggerty1a769002016-09-04 18:08:37 +0200587
David Turnere3688bd2016-09-04 18:08:38 +0200588/* 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 */
594typedef struct ref_iterator *reflog_iterator_begin_fn(
595 struct ref_store *ref_store);
596
597typedef 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);
601typedef 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);
605typedef int reflog_exists_fn(struct ref_store *ref_store, const char *refname);
606typedef int create_reflog_fn(struct ref_store *ref_store, const char *refname,
Han-Wen Nienhuys7b089122021-11-22 14:19:08 +0000607 struct strbuf *err);
David Turnere3688bd2016-09-04 18:08:38 +0200608typedef int delete_reflog_fn(struct ref_store *ref_store, const char *refname);
609typedef int reflog_expire_fn(struct ref_store *ref_store,
Ævar Arnfjörð Bjarmasoncc40b5c2021-08-23 13:36:11 +0200610 const char *refname,
David Turnere3688bd2016-09-04 18:08:38 +0200611 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 Haggerty1a769002016-09-04 18:08:37 +0200617/*
Michael Haggerty34c7ad82016-09-04 18:08:20 +0200618 * Read a reference from the specified reference store, non-recursively.
619 * Set type to describe the reference, and:
Michael Haggertycf596442016-05-06 17:25:31 +0200620 *
brian m. carlson99afe912017-10-15 22:07:11 +0000621 * - If refname is the name of a normal reference, fill in oid
Michael Haggertycf596442016-05-06 17:25:31 +0200622 * (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. carlson99afe912017-10-15 22:07:11 +0000627 * type (leaving oid unchanged). The caller is responsible for
Michael Haggertycf596442016-05-06 17:25:31 +0200628 * 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 Nienhuys5b12e162021-08-23 13:52:40 +0200637 * 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 Haggertycf596442016-05-06 17:25:31 +0200646 *
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 Nienhuys5b12e162021-08-23 13:52:40 +0200659typedef 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 Sahlberg127b42a2016-09-04 18:08:16 +0200662
Patrick Steinhardtcd475b32022-03-01 10:33:46 +0100663/*
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 */
675typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refname,
676 struct strbuf *referent);
677
Ronnie Sahlberg3dce4442016-09-04 18:08:10 +0200678struct ref_storage_be {
Ronnie Sahlberg3dce4442016-09-04 18:08:10 +0200679 const char *name;
Michael Haggerty00eebe32016-09-04 18:08:11 +0200680 ref_store_init_fn *init;
David Turner6fb5acf2016-09-04 18:08:41 +0200681 ref_init_db_fn *init_db;
Michael Haggerty30173b82017-05-22 16:17:44 +0200682
683 ref_transaction_prepare_fn *transaction_prepare;
684 ref_transaction_finish_fn *transaction_finish;
685 ref_transaction_abort_fn *transaction_abort;
David Turnerfc681462016-09-04 18:08:39 +0200686 ref_transaction_commit_fn *initial_transaction_commit;
Michael Haggertye1e33b72016-09-04 18:08:25 +0200687
Michael Haggerty82315272016-09-04 18:08:27 +0200688 pack_refs_fn *pack_refs;
David Turner9b6b40d2016-09-04 18:08:42 +0200689 rename_ref_fn *rename_ref;
Sahil Dua52d59cc2017-06-18 23:19:16 +0200690 copy_ref_fn *copy_ref;
Michael Haggerty82315272016-09-04 18:08:27 +0200691
Michael Haggerty1a769002016-09-04 18:08:37 +0200692 ref_iterator_begin_fn *iterator_begin;
Michael Haggertye1e33b72016-09-04 18:08:25 +0200693 read_raw_ref_fn *read_raw_ref;
Patrick Steinhardtcd475b32022-03-01 10:33:46 +0100694 read_symbolic_ref_fn *read_symbolic_ref;
David Turnere3688bd2016-09-04 18:08:38 +0200695
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 Sahlberg3dce4442016-09-04 18:08:10 +0200703};
704
705extern struct ref_storage_be refs_be_files;
Patrick Steinhardt57db2a02024-02-07 08:20:31 +0100706extern struct ref_storage_be refs_be_reftable;
Michael Haggertye0cc8ac2017-06-23 09:01:38 +0200707extern struct ref_storage_be refs_be_packed;
Ronnie Sahlberg3dce4442016-09-04 18:08:10 +0200708
Michael Haggerty00eebe32016-09-04 18:08:11 +0200709/*
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 Nienhuys4877c6c2020-08-19 14:27:56 +0000712 * hash map; see get_submodule_ref_store() for more info.
Michael Haggerty00eebe32016-09-04 18:08:11 +0200713 */
714struct ref_store {
715 /* The backend describing this ref_store's storage scheme: */
716 const struct ref_storage_be *be;
Han-Wen Nienhuys5085aef2020-08-19 14:27:57 +0000717
Jonathan Tan34224e12021-10-08 14:08:14 -0700718 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 Nienhuys5085aef2020-08-19 14:27:57 +0000724 char *gitdir;
Michael Haggerty00eebe32016-09-04 18:08:11 +0200725};
726
727/*
Han-Wen Nienhuysdf3458e2021-10-16 11:39:10 +0200728 * Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for
729 * invalid contents.
Han-Wen Nienhuyse39620f2020-08-19 14:27:55 +0000730 */
731int parse_loose_ref_contents(const char *buf, struct object_id *oid,
Han-Wen Nienhuysdf3458e2021-10-16 11:39:10 +0200732 struct strbuf *referent, unsigned int *type,
733 int *failure_errno);
Han-Wen Nienhuyse39620f2020-08-19 14:27:55 +0000734
735/*
Michael Haggertyfbfd0a22017-02-10 12:16:17 +0100736 * Fill in the generic part of refs and add it to our collection of
737 * reference stores.
Michael Haggerty00eebe32016-09-04 18:08:11 +0200738 */
Han-Wen Nienhuysf9f7fd32021-12-22 18:11:54 +0000739void base_ref_store_init(struct ref_store *refs, struct repository *repo,
740 const char *path, const struct ref_storage_be *be);
Michael Haggerty00eebe32016-09-04 18:08:11 +0200741
Han-Wen Nienhuys4441f422020-09-09 10:15:08 +0000742/*
743 * Support GIT_TRACE_REFS by optionally wrapping the given ref_store instance.
744 */
745struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store);
746
Karthik Nayake9965ba2024-05-07 14:58:55 +0200747/*
748 * Return the refname under which update was originally requested.
749 */
750const char *ref_update_original_update_refname(struct ref_update *update);
751
Karthik Nayak644daf72024-05-07 14:58:56 +0200752/*
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 */
757int 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 */
765int ref_update_check_old_target(const char *referent, struct ref_update *update,
766 struct strbuf *err);
767
Michael Haggerty4cb77002015-11-10 12:42:36 +0100768#endif /* REFS_REFS_INTERNAL_H */