Git fork
1#ifndef REFS_REFS_INTERNAL_H
2#define REFS_REFS_INTERNAL_H
3
4#include "refs.h"
5#include "iterator.h"
6#include "string-list.h"
7
8struct fsck_options;
9struct ref_transaction;
10
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/*
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`.
22 */
23
24/*
25 * The reference should be updated to new_oid.
26 */
27#define REF_HAVE_NEW (1 << 2)
28
29/*
30 * The current reference's value should be checked to make sure that
31 * it agrees with old_oid.
32 */
33#define REF_HAVE_OLD (1 << 3)
34
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
42/*
43 * Return the length of time to retry acquiring a loose reference lock
44 * before giving up, in milliseconds:
45 */
46long get_files_ref_lock_timeout_ms(void);
47
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 *
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").
61 */
62int refname_is_safe(const char *refname);
63
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 */
69int ref_resolves_to_object(const char *refname,
70 struct repository *repo,
71 const struct object_id *oid,
72 unsigned int flags);
73
74/**
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.
80 */
81struct ref_update {
82 /*
83 * If (flags & REF_HAVE_NEW), set the reference to this value
84 * (or delete it, if `new_oid` is `null_oid`).
85 */
86 struct object_id new_oid;
87
88 /*
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`).
92 */
93 struct object_id old_oid;
94
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
108 /*
109 * One or more of REF_NO_DEREF, REF_FORCE_CREATE_REFLOG,
110 * REF_HAVE_NEW, REF_HAVE_OLD, or backend-specific flags.
111 */
112 unsigned int flags;
113
114 void *backend_data;
115 unsigned int type;
116 char *msg;
117 char *committer_info;
118
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 */
124 uint64_t index;
125
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
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
144 const char refname[FLEX_ARRAY];
145};
146
147int 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);
150
151/*
152 * Mark a given update as rejected with a given reason.
153 */
154int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,
155 size_t update_idx,
156 enum ref_transaction_error err);
157
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
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.
164 */
165struct 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,
172 const char *msg);
173
174/*
175 * Transaction states.
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.
192 */
193enum ref_transaction_state {
194 REF_TRANSACTION_OPEN = 0,
195 REF_TRANSACTION_PREPARED = 1,
196 REF_TRANSACTION_CLOSED = 2
197};
198
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 */
205struct ref_transaction_rejections {
206 size_t *update_indices;
207 size_t alloc;
208 size_t nr;
209};
210
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 */
216struct ref_transaction {
217 struct ref_store *ref_store;
218 struct ref_update **updates;
219 struct string_list refnames;
220 size_t alloc;
221 size_t nr;
222 enum ref_transaction_state state;
223 struct ref_transaction_rejections *rejections;
224 void *backend_data;
225 unsigned int flags;
226 uint64_t max_index;
227};
228
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 */
239const char *find_descendant_ref(const char *dirname,
240 const struct string_list *extras,
241 const struct string_list *skip);
242
243/* We allow "recursive" symbolic refs. Only within reason, though */
244#define SYMREF_MAXDEPTH 5
245
246/*
247 * Data structure for holding a reference iterator. See refs.h for
248 * more details and usage instructions.
249 */
250struct ref_iterator {
251 struct ref_iterator_vtable *vtable;
252 const char *refname;
253 const char *referent;
254 const struct object_id *oid;
255 unsigned int flags;
256};
257
258/*
259 * An iterator over nothing (its first ref_iterator_advance() call
260 * returns ITER_DONE).
261 */
262struct ref_iterator *empty_ref_iterator_begin(void);
263
264/*
265 * Return true iff ref_iterator is an empty_ref_iterator.
266 */
267int is_empty_ref_iterator(struct ref_iterator *ref_iterator);
268
269/*
270 * A callback function used to instruct merge_ref_iterator how to
271 * interleave the entries from iter0 and iter1. The function should
272 * return one of the constants defined in enum iterator_selection. It
273 * must not advance either of the iterators itself.
274 *
275 * The function must be prepared to handle the case that iter0 and/or
276 * iter1 is NULL, which indicates that the corresponding sub-iterator
277 * has been exhausted. Its return value must be consistent with the
278 * current states of the iterators; e.g., it must not return
279 * ITER_SKIP_1 if iter1 has already been exhausted.
280 */
281typedef enum iterator_selection ref_iterator_select_fn(
282 struct ref_iterator *iter0, struct ref_iterator *iter1,
283 void *cb_data);
284
285/*
286 * An implementation of ref_iterator_select_fn that merges worktree and common
287 * refs. Per-worktree refs from the common iterator are ignored, worktree refs
288 * override common refs. Refs are selected lexicographically.
289 */
290enum iterator_selection ref_iterator_select(struct ref_iterator *iter_worktree,
291 struct ref_iterator *iter_common,
292 void *cb_data);
293
294/*
295 * Iterate over the entries from iter0 and iter1, with the values
296 * interleaved as directed by the select function. The iterator takes
297 * ownership of iter0 and iter1 and frees them when the iteration is
298 * over.
299 */
300struct ref_iterator *merge_ref_iterator_begin(
301 struct ref_iterator *iter0, struct ref_iterator *iter1,
302 ref_iterator_select_fn *select, void *cb_data);
303
304/*
305 * An iterator consisting of the union of the entries from front and
306 * back. If there are entries common to the two sub-iterators, use the
307 * one from front. Each iterator must iterate over its entries in
308 * strcmp() order by refname for this to work.
309 *
310 * The new iterator takes ownership of its arguments and frees them
311 * when the iteration is over. As a convenience to callers, if front
312 * or back is an empty_ref_iterator, then abort that one immediately
313 * and return the other iterator directly, without wrapping it.
314 */
315struct ref_iterator *overlay_ref_iterator_begin(
316 struct ref_iterator *front, struct ref_iterator *back);
317
318/*
319 * Wrap iter0, only letting through the references whose names start
320 * with prefix. If trim is set, set iter->refname to the name of the
321 * reference with that many characters trimmed off the front;
322 * otherwise set it to the full refname. The new iterator takes over
323 * ownership of iter0 and frees it when iteration is over. It makes
324 * its own copy of prefix.
325 *
326 * As an convenience to callers, if prefix is the empty string and
327 * trim is zero, this function returns iter0 directly, without
328 * wrapping it.
329 */
330struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
331 const char *prefix,
332 int trim);
333
334/* Internal implementation of reference iteration: */
335
336/*
337 * Base class constructor for ref_iterators. Initialize the
338 * ref_iterator part of iter, setting its vtable pointer as specified.
339 * This is meant to be called only by the initializers of derived
340 * classes.
341 */
342void base_ref_iterator_init(struct ref_iterator *iter,
343 struct ref_iterator_vtable *vtable);
344
345/* Virtual function declarations for ref_iterators: */
346
347/*
348 * backend-specific implementation of ref_iterator_advance. For symrefs, the
349 * function should set REF_ISSYMREF, and it should also dereference the symref
350 * to provide the OID referent. It should respect do_for_each_ref_flags
351 * that were passed to refs_ref_iterator_begin().
352 */
353typedef int ref_iterator_advance_fn(struct ref_iterator *ref_iterator);
354
355/*
356 * Seek the iterator to the first matching reference. If the
357 * REF_ITERATOR_SEEK_SET_PREFIX flag is set, it would behave the same as if a
358 * new iterator was created with the provided refname as prefix.
359 */
360typedef int ref_iterator_seek_fn(struct ref_iterator *ref_iterator,
361 const char *refname, unsigned int flags);
362
363/*
364 * Peels the current ref, returning 0 for success or -1 for failure.
365 */
366typedef int ref_iterator_peel_fn(struct ref_iterator *ref_iterator,
367 struct object_id *peeled);
368
369/*
370 * Implementations of this function should free any resources specific
371 * to the derived class.
372 */
373typedef void ref_iterator_release_fn(struct ref_iterator *ref_iterator);
374
375struct ref_iterator_vtable {
376 ref_iterator_advance_fn *advance;
377 ref_iterator_seek_fn *seek;
378 ref_iterator_peel_fn *peel;
379 ref_iterator_release_fn *release;
380};
381
382/*
383 * current_ref_iter is a performance hack: when iterating over
384 * references using the for_each_ref*() functions, current_ref_iter is
385 * set to the reference iterator before calling the callback function.
386 * If the callback function calls peel_ref(), then peel_ref() first
387 * checks whether the reference to be peeled is the one referred to by
388 * the iterator (it usually is) and if so, asks the iterator for the
389 * peeled version of the reference if it is available. This avoids a
390 * refname lookup in a common case. current_ref_iter is set to NULL
391 * when the iteration is over.
392 */
393extern struct ref_iterator *current_ref_iter;
394
395struct ref_store;
396
397/* refs backends */
398
399/* ref_store_init flags */
400#define REF_STORE_READ (1 << 0)
401#define REF_STORE_WRITE (1 << 1) /* can perform update operations */
402#define REF_STORE_ODB (1 << 2) /* has access to object database */
403#define REF_STORE_MAIN (1 << 3)
404#define REF_STORE_ALL_CAPS (REF_STORE_READ | \
405 REF_STORE_WRITE | \
406 REF_STORE_ODB | \
407 REF_STORE_MAIN)
408
409/*
410 * Initialize the ref_store for the specified gitdir. These functions
411 * should call base_ref_store_init() to initialize the shared part of
412 * the ref_store and to record the ref_store for later lookup.
413 */
414typedef struct ref_store *ref_store_init_fn(struct repository *repo,
415 const char *gitdir,
416 unsigned int flags);
417/*
418 * Release all memory and resources associated with the ref store.
419 */
420typedef void ref_store_release_fn(struct ref_store *refs);
421
422typedef int ref_store_create_on_disk_fn(struct ref_store *refs,
423 int flags,
424 struct strbuf *err);
425
426/*
427 * Remove the reference store from disk.
428 */
429typedef int ref_store_remove_on_disk_fn(struct ref_store *refs,
430 struct strbuf *err);
431
432typedef int ref_transaction_prepare_fn(struct ref_store *refs,
433 struct ref_transaction *transaction,
434 struct strbuf *err);
435
436typedef int ref_transaction_finish_fn(struct ref_store *refs,
437 struct ref_transaction *transaction,
438 struct strbuf *err);
439
440typedef int ref_transaction_abort_fn(struct ref_store *refs,
441 struct ref_transaction *transaction,
442 struct strbuf *err);
443
444typedef int ref_transaction_commit_fn(struct ref_store *refs,
445 struct ref_transaction *transaction,
446 struct strbuf *err);
447
448typedef int pack_refs_fn(struct ref_store *ref_store,
449 struct pack_refs_opts *opts);
450typedef int optimize_fn(struct ref_store *ref_store,
451 struct pack_refs_opts *opts);
452typedef int rename_ref_fn(struct ref_store *ref_store,
453 const char *oldref, const char *newref,
454 const char *logmsg);
455typedef int copy_ref_fn(struct ref_store *ref_store,
456 const char *oldref, const char *newref,
457 const char *logmsg);
458
459/*
460 * Iterate over the references in `ref_store` whose names start with
461 * `prefix`. `prefix` is matched as a literal string, without regard
462 * for path separators. If prefix is NULL or the empty string, iterate
463 * over all references in `ref_store`. The output is ordered by
464 * refname.
465 */
466typedef struct ref_iterator *ref_iterator_begin_fn(
467 struct ref_store *ref_store,
468 const char *prefix, const char **exclude_patterns,
469 unsigned int flags);
470
471/* reflog functions */
472
473/*
474 * Iterate over the references in the specified ref_store that have a
475 * reflog. The refs are iterated over in arbitrary order.
476 */
477typedef struct ref_iterator *reflog_iterator_begin_fn(
478 struct ref_store *ref_store);
479
480typedef int for_each_reflog_ent_fn(struct ref_store *ref_store,
481 const char *refname,
482 each_reflog_ent_fn fn,
483 void *cb_data);
484typedef int for_each_reflog_ent_reverse_fn(struct ref_store *ref_store,
485 const char *refname,
486 each_reflog_ent_fn fn,
487 void *cb_data);
488typedef int reflog_exists_fn(struct ref_store *ref_store, const char *refname);
489typedef int create_reflog_fn(struct ref_store *ref_store, const char *refname,
490 struct strbuf *err);
491typedef int delete_reflog_fn(struct ref_store *ref_store, const char *refname);
492typedef int reflog_expire_fn(struct ref_store *ref_store,
493 const char *refname,
494 unsigned int flags,
495 reflog_expiry_prepare_fn prepare_fn,
496 reflog_expiry_should_prune_fn should_prune_fn,
497 reflog_expiry_cleanup_fn cleanup_fn,
498 void *policy_cb_data);
499
500/*
501 * Read a reference from the specified reference store, non-recursively.
502 * Set type to describe the reference, and:
503 *
504 * - If refname is the name of a normal reference, fill in oid
505 * (leaving referent unchanged).
506 *
507 * - If refname is the name of a symbolic reference, write the full
508 * name of the reference to which it refers (e.g.
509 * "refs/heads/master") to referent and set the REF_ISSYMREF bit in
510 * type (leaving oid unchanged). The caller is responsible for
511 * validating that referent is a valid reference name.
512 *
513 * WARNING: refname might be used as part of a filename, so it is
514 * important from a security standpoint that it be safe in the sense
515 * of refname_is_safe(). Moreover, for symrefs this function sets
516 * referent to whatever the repository says, which might not be a
517 * properly-formatted or even safe reference name. NEITHER INPUT NOR
518 * OUTPUT REFERENCE NAMES ARE VALIDATED WITHIN THIS FUNCTION.
519 *
520 * Return 0 on success, or -1 on failure. If the ref exists but is neither a
521 * symbolic ref nor an object ID, it is broken. In this case set REF_ISBROKEN in
522 * type, and return -1 (failure_errno should not be ENOENT)
523 *
524 * failure_errno provides errno codes that are interpreted beyond error
525 * reporting. The following error codes have special meaning:
526 * * ENOENT: the ref doesn't exist
527 * * EISDIR: ref name is a directory
528 * * ENOTDIR: ref prefix is not a directory
529 *
530 * Backend-specific flags might be set in type as well, regardless of
531 * outcome.
532 *
533 * It is OK for refname to point into referent. If so:
534 *
535 * - if the function succeeds with REF_ISSYMREF, referent will be
536 * overwritten and the memory formerly pointed to by it might be
537 * changed or even freed.
538 *
539 * - in all other cases, referent will be untouched, and therefore
540 * refname will still be valid and unchanged.
541 */
542typedef int read_raw_ref_fn(struct ref_store *ref_store, const char *refname,
543 struct object_id *oid, struct strbuf *referent,
544 unsigned int *type, int *failure_errno);
545
546/*
547 * Read a symbolic reference from the specified reference store. This function
548 * is optional: if not implemented by a backend, then `read_raw_ref_fn` is used
549 * to read the symbolcic reference instead. It is intended to be implemented
550 * only in case the backend can optimize the reading of symbolic references.
551 *
552 * Return 0 on success, or -1 on failure. `referent` will be set to the target
553 * of the symbolic reference on success. This function explicitly does not
554 * distinguish between error cases and the reference not being a symbolic
555 * reference to allow backends to optimize this operation in case symbolic and
556 * non-symbolic references are treated differently.
557 */
558typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refname,
559 struct strbuf *referent);
560
561typedef int fsck_fn(struct ref_store *ref_store,
562 struct fsck_options *o,
563 struct worktree *wt);
564
565struct ref_storage_be {
566 const char *name;
567 ref_store_init_fn *init;
568 ref_store_release_fn *release;
569 ref_store_create_on_disk_fn *create_on_disk;
570 ref_store_remove_on_disk_fn *remove_on_disk;
571
572 ref_transaction_prepare_fn *transaction_prepare;
573 ref_transaction_finish_fn *transaction_finish;
574 ref_transaction_abort_fn *transaction_abort;
575
576 pack_refs_fn *pack_refs;
577 optimize_fn *optimize;
578 rename_ref_fn *rename_ref;
579 copy_ref_fn *copy_ref;
580
581 ref_iterator_begin_fn *iterator_begin;
582 read_raw_ref_fn *read_raw_ref;
583
584 /*
585 * Please refer to `refs_read_symbolic_ref()` for the expected
586 * behaviour.
587 */
588 read_symbolic_ref_fn *read_symbolic_ref;
589
590 reflog_iterator_begin_fn *reflog_iterator_begin;
591 for_each_reflog_ent_fn *for_each_reflog_ent;
592 for_each_reflog_ent_reverse_fn *for_each_reflog_ent_reverse;
593 reflog_exists_fn *reflog_exists;
594 create_reflog_fn *create_reflog;
595 delete_reflog_fn *delete_reflog;
596 reflog_expire_fn *reflog_expire;
597
598 fsck_fn *fsck;
599};
600
601extern struct ref_storage_be refs_be_files;
602extern struct ref_storage_be refs_be_reftable;
603extern struct ref_storage_be refs_be_packed;
604
605/*
606 * A representation of the reference store for the main repository or
607 * a submodule. The ref_store instances for submodules are kept in a
608 * hash map; see repo_get_submodule_ref_store() for more info.
609 */
610struct ref_store {
611 /* The backend describing this ref_store's storage scheme: */
612 const struct ref_storage_be *be;
613
614 struct repository *repo;
615
616 /*
617 * The gitdir that this ref_store applies to. Note that this is not
618 * necessarily repo->gitdir if the repo has multiple worktrees.
619 */
620 char *gitdir;
621};
622
623/*
624 * Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for
625 * invalid contents.
626 */
627int parse_loose_ref_contents(const struct git_hash_algo *algop,
628 const char *buf, struct object_id *oid,
629 struct strbuf *referent, unsigned int *type,
630 const char **trailing, int *failure_errno);
631
632/*
633 * Fill in the generic part of refs and add it to our collection of
634 * reference stores.
635 */
636void base_ref_store_init(struct ref_store *refs, struct repository *repo,
637 const char *path, const struct ref_storage_be *be);
638
639/*
640 * Support GIT_TRACE_REFS by optionally wrapping the given ref_store instance.
641 */
642struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store);
643
644/*
645 * Return the refname under which update was originally requested.
646 */
647const char *ref_update_original_update_refname(struct ref_update *update);
648
649/*
650 * Helper function to check if the new value is null, this
651 * takes into consideration that the update could be a regular
652 * ref or a symbolic ref.
653 */
654int ref_update_has_null_new_value(struct ref_update *update);
655
656/*
657 * Check whether the old_target values stored in update are consistent
658 * with the referent, which is the symbolic reference's current value.
659 * If everything is OK, return 0; otherwise, write an error message to
660 * err and return -1.
661 */
662enum ref_transaction_error ref_update_check_old_target(const char *referent,
663 struct ref_update *update,
664 struct strbuf *err);
665
666/*
667 * Check if the ref must exist, this means that the old_oid or
668 * old_target is non NULL. Log-only updates never require the old state to
669 * match.
670 */
671int ref_update_expects_existing_old_ref(struct ref_update *update);
672
673/*
674 * Same as `refs_verify_refname_available()`, but checking for a list of
675 * refnames instead of only a single item. This is more efficient in the case
676 * where one needs to check multiple refnames.
677 *
678 * If using batched updates, then individual updates are marked rejected,
679 * reference backends are then in charge of not committing those updates.
680 */
681enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,
682 const struct string_list *refnames,
683 const struct string_list *extras,
684 const struct string_list *skip,
685 struct ref_transaction *transaction,
686 unsigned int initial_transaction,
687 struct strbuf *err);
688
689#endif /* REFS_REFS_INTERNAL_H */