]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - refs/refs-internal.h
The third batch
[thirdparty/git.git] / refs / refs-internal.h
... / ...
CommitLineData
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 struct reference ref;
253};
254
255/*
256 * An iterator over nothing (its first ref_iterator_advance() call
257 * returns ITER_DONE).
258 */
259struct ref_iterator *empty_ref_iterator_begin(void);
260
261/*
262 * Return true iff ref_iterator is an empty_ref_iterator.
263 */
264int 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 */
278typedef enum iterator_selection ref_iterator_select_fn(
279 struct ref_iterator *iter0, struct ref_iterator *iter1,
280 void *cb_data);
281
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 */
287enum iterator_selection ref_iterator_select(struct ref_iterator *iter_worktree,
288 struct ref_iterator *iter_common,
289 void *cb_data);
290
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
295 * over.
296 */
297struct 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 */
312struct 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 */
327struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
328 const char *prefix,
329 int trim);
330
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 */
339void base_ref_iterator_init(struct ref_iterator *iter,
340 struct ref_iterator_vtable *vtable);
341
342/* Virtual function declarations for ref_iterators: */
343
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
347 * to provide the OID referent. It should respect do_for_each_ref_flags
348 * that were passed to refs_ref_iterator_begin().
349 */
350typedef int ref_iterator_advance_fn(struct ref_iterator *ref_iterator);
351
352/*
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.
356 */
357typedef int ref_iterator_seek_fn(struct ref_iterator *ref_iterator,
358 const char *refname, unsigned int flags);
359
360/*
361 * Implementations of this function should free any resources specific
362 * to the derived class.
363 */
364typedef void ref_iterator_release_fn(struct ref_iterator *ref_iterator);
365
366struct ref_iterator_vtable {
367 ref_iterator_advance_fn *advance;
368 ref_iterator_seek_fn *seek;
369 ref_iterator_release_fn *release;
370};
371
372struct ref_store;
373
374/* refs backends */
375
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 | \
382 REF_STORE_WRITE | \
383 REF_STORE_ODB | \
384 REF_STORE_MAIN)
385
386/*
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.
390 */
391typedef struct ref_store *ref_store_init_fn(struct repository *repo,
392 const char *gitdir,
393 unsigned int flags);
394/*
395 * Release all memory and resources associated with the ref store.
396 */
397typedef void ref_store_release_fn(struct ref_store *refs);
398
399typedef int ref_store_create_on_disk_fn(struct ref_store *refs,
400 int flags,
401 struct strbuf *err);
402
403/*
404 * Remove the reference store from disk.
405 */
406typedef int ref_store_remove_on_disk_fn(struct ref_store *refs,
407 struct strbuf *err);
408
409typedef int ref_transaction_prepare_fn(struct ref_store *refs,
410 struct ref_transaction *transaction,
411 struct strbuf *err);
412
413typedef int ref_transaction_finish_fn(struct ref_store *refs,
414 struct ref_transaction *transaction,
415 struct strbuf *err);
416
417typedef int ref_transaction_abort_fn(struct ref_store *refs,
418 struct ref_transaction *transaction,
419 struct strbuf *err);
420
421typedef int ref_transaction_commit_fn(struct ref_store *refs,
422 struct ref_transaction *transaction,
423 struct strbuf *err);
424
425typedef int optimize_fn(struct ref_store *ref_store,
426 struct refs_optimize_opts *opts);
427
428typedef int optimize_required_fn(struct ref_store *ref_store,
429 struct refs_optimize_opts *opts,
430 bool *required);
431
432typedef int rename_ref_fn(struct ref_store *ref_store,
433 const char *oldref, const char *newref,
434 const char *logmsg);
435typedef int copy_ref_fn(struct ref_store *ref_store,
436 const char *oldref, const char *newref,
437 const char *logmsg);
438
439/*
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
444 * refname.
445 */
446typedef struct ref_iterator *ref_iterator_begin_fn(
447 struct ref_store *ref_store,
448 const char *prefix, const char **exclude_patterns,
449 unsigned int flags);
450
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 */
457typedef struct ref_iterator *reflog_iterator_begin_fn(
458 struct ref_store *ref_store);
459
460typedef 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);
464typedef 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);
468typedef int reflog_exists_fn(struct ref_store *ref_store, const char *refname);
469typedef int create_reflog_fn(struct ref_store *ref_store, const char *refname,
470 struct strbuf *err);
471typedef int delete_reflog_fn(struct ref_store *ref_store, const char *refname);
472typedef int reflog_expire_fn(struct ref_store *ref_store,
473 const char *refname,
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
480/*
481 * Read a reference from the specified reference store, non-recursively.
482 * Set type to describe the reference, and:
483 *
484 * - If refname is the name of a normal reference, fill in oid
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
490 * type (leaving oid unchanged). The caller is responsible for
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 *
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
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 */
522typedef 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);
525
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 */
538typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refname,
539 struct strbuf *referent);
540
541typedef int fsck_fn(struct ref_store *ref_store,
542 struct fsck_options *o,
543 struct worktree *wt);
544
545struct ref_storage_be {
546 const char *name;
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;
551
552 ref_transaction_prepare_fn *transaction_prepare;
553 ref_transaction_finish_fn *transaction_finish;
554 ref_transaction_abort_fn *transaction_abort;
555
556 optimize_fn *optimize;
557 optimize_required_fn *optimize_required;
558 rename_ref_fn *rename_ref;
559 copy_ref_fn *copy_ref;
560
561 ref_iterator_begin_fn *iterator_begin;
562 read_raw_ref_fn *read_raw_ref;
563
564 /*
565 * Please refer to `refs_read_symbolic_ref()` for the expected
566 * behaviour.
567 */
568 read_symbolic_ref_fn *read_symbolic_ref;
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;
577
578 fsck_fn *fsck;
579};
580
581extern struct ref_storage_be refs_be_files;
582extern struct ref_storage_be refs_be_reftable;
583extern struct ref_storage_be refs_be_packed;
584
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
588 * hash map; see repo_get_submodule_ref_store() for more info.
589 */
590struct ref_store {
591 /* The backend describing this ref_store's storage scheme: */
592 const struct ref_storage_be *be;
593
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 */
600 char *gitdir;
601};
602
603/*
604 * Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for
605 * invalid contents.
606 */
607int 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);
611
612/*
613 * Fill in the generic part of refs and add it to our collection of
614 * reference stores.
615 */
616void base_ref_store_init(struct ref_store *refs, struct repository *repo,
617 const char *path, const struct ref_storage_be *be);
618
619/*
620 * Support GIT_TRACE_REFS by optionally wrapping the given ref_store instance.
621 */
622struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store);
623
624/*
625 * Return the refname under which update was originally requested.
626 */
627const char *ref_update_original_update_refname(struct ref_update *update);
628
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 */
634int 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 */
642enum ref_transaction_error ref_update_check_old_target(const char *referent,
643 struct ref_update *update,
644 struct strbuf *err);
645
646/*
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
649 * match.
650 */
651int ref_update_expects_existing_old_ref(struct ref_update *update);
652
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 */
661enum 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
669#endif /* REFS_REFS_INTERNAL_H */