interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / refs.h

1464 lines · 53142 bytes Raw Blame History
1#ifndef REFS_H
2#define REFS_H
3
4#include "object-name.h"
5#include "commit.h"
6#include "repository.h"
7#include "repo-settings.h"
8
9struct fsck_options;
10struct object_id;
11struct ref_store;
12struct strbuf;
13struct string_list;
14struct string_list_item;
15struct worktree;
16
17enum ref_storage_format ref_storage_format_by_name(const char *name);
18const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);
19
20/* Parse a reference storage URI in the format "<format>[://<payload>]". */
21enum ref_storage_format ref_storage_format_by_uri(const char *uri,
22 char **payload);
23
24enum ref_transaction_error {
25 /* Default error code */
26 REF_TRANSACTION_ERROR_GENERIC = -1,
27 /* Ref name conflict like A vs A/B */
28 REF_TRANSACTION_ERROR_NAME_CONFLICT = -2,
29 /* Ref to be created already exists */
30 REF_TRANSACTION_ERROR_CREATE_EXISTS = -3,
31 /* ref expected but doesn't exist */
32 REF_TRANSACTION_ERROR_NONEXISTENT_REF = -4,
33 /* Provided old_oid or old_target of reference doesn't match actual */
34 REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE = -5,
35 /* Provided new_oid or new_target is invalid */
36 REF_TRANSACTION_ERROR_INVALID_NEW_VALUE = -6,
37 /* Expected ref to be symref, but is a regular ref */
38 REF_TRANSACTION_ERROR_EXPECTED_SYMREF = -7,
39 /* Cannot create ref due to case-insensitive filesystem */
40 REF_TRANSACTION_ERROR_CASE_CONFLICT = -8,
41};
42
43/*
44 * Resolve a reference, recursively following symbolic references.
45 *
46 * Return the name of the non-symbolic reference that ultimately pointed
47 * at the resolved object name. The return value, if not NULL, is a
48 * pointer into either a static buffer or the input ref.
49 *
50 * If oid is non-NULL, store the referred-to object's name in it.
51 *
52 * If the reference cannot be resolved to an object, the behavior
53 * depends on the RESOLVE_REF_READING flag:
54 *
55 * - If RESOLVE_REF_READING is set, return NULL.
56 *
57 * - If RESOLVE_REF_READING is not set, clear oid and return the name of
58 * the last reference name in the chain, which will either be a non-symbolic
59 * reference or an undefined reference. If this is a prelude to
60 * "writing" to the ref, the return value is the name of the ref
61 * that will actually be created or changed.
62 *
63 * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one
64 * level of symbolic reference. The value stored in oid for a symbolic
65 * reference will always be null_oid in this case, and the return
66 * value is the reference that the symref refers to directly.
67 *
68 * If flags is non-NULL, set the value that it points to the
69 * combination of REF_ISPACKED (if the reference was found among the
70 * packed references), REF_ISSYMREF (if the initial reference was a
71 * symbolic reference), REF_BAD_NAME (if the reference name is ill
72 * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN
73 * (if the ref is malformed or has a bad name). See refs.h for more detail
74 * on each flag.
75 *
76 * If ref is not a properly-formatted, normalized reference, return
77 * NULL. If more than MAXDEPTH recursive symbolic lookups are needed,
78 * give up and return NULL.
79 *
80 * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their
81 * name is invalid according to git-check-ref-format(1). If the name
82 * is bad then the value stored in oid will be null_oid and the two
83 * flags REF_ISBROKEN and REF_BAD_NAME will be set.
84 *
85 * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/
86 * directory and do not consist of all caps and underscores cannot be
87 * resolved. The function returns NULL for such ref names.
88 * Caps and underscores refers to the pseudorefs, such as HEAD,
89 * FETCH_HEAD and friends, that all live outside of the refs/ directory.
90 */
91#define RESOLVE_REF_READING 0x01
92#define RESOLVE_REF_NO_RECURSE 0x02
93#define RESOLVE_REF_ALLOW_BAD_NAME 0x04
94
95const char *refs_resolve_ref_unsafe(struct ref_store *refs,
96 const char *refname,
97 int resolve_flags,
98 struct object_id *oid,
99 int *flags);
100
101char *refs_resolve_refdup(struct ref_store *refs,
102 const char *refname, int resolve_flags,
103 struct object_id *oid, int *flags);
104
105int refs_read_ref_full(struct ref_store *refs, const char *refname,
106 int resolve_flags, struct object_id *oid, int *flags);
107
108int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
109
110#define NOT_A_SYMREF -2
111
112/*
113 * Read the symbolic ref named "refname" and write its immediate referent into
114 * the provided buffer. Referent is left empty if "refname" is not a symbolic
115 * ref. It does not resolve the symbolic reference recursively in case the
116 * target is also a symbolic ref.
117 *
118 * Returns 0 on success, -2 if the "refname" is not a symbolic ref,
119 * -1 otherwise.
120 */
121int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
122 struct strbuf *referent);
123
124/*
125 * Return 0 if a reference named refname could be created without
126 * conflicting with the name of an existing reference. Otherwise,
127 * return a negative value and write an explanation to err. If extras
128 * is non-NULL, it is a list of additional refnames with which refname
129 * is not allowed to conflict. If skip is non-NULL, ignore potential
130 * conflicts with refs in skip (e.g., because they are scheduled for
131 * deletion in the same operation). Behavior is undefined if the same
132 * name is listed in both extras and skip.
133 *
134 * Two reference names conflict if one of them exactly matches the
135 * leading components of the other; e.g., "foo/bar" conflicts with
136 * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
137 * "foo/barbados".
138 *
139 * If `initial_transaction` is truish, then all collision checks with
140 * preexisting refs are skipped.
141 *
142 * extras and skip must be sorted.
143 */
144enum ref_transaction_error refs_verify_refname_available(struct ref_store *refs,
145 const char *refname,
146 const struct string_list *extras,
147 const struct string_list *skip,
148 unsigned int initial_transaction,
149 struct strbuf *err);
150
151int refs_ref_exists(struct ref_store *refs, const char *refname);
152
153enum log_refs_config {
154 LOG_REFS_UNSET = -1,
155 LOG_REFS_NONE = 0,
156 LOG_REFS_NORMAL,
157 LOG_REFS_ALWAYS
158};
159
160enum log_refs_config refs_parse_log_all_ref_updates_config(const char *value);
161
162int should_autocreate_reflog(enum log_refs_config log_all_ref_updates,
163 const char *refname);
164
165int is_branch(const char *refname);
166
167#define REF_STORE_CREATE_ON_DISK_IS_WORKTREE (1 << 0)
168
169int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err);
170
171/*
172 * Release all memory and resources associated with the ref store.
173 */
174void ref_store_release(struct ref_store *ref_store);
175
176/*
177 * Remove the ref store from disk. This deletes all associated data.
178 */
179int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err);
180
181/*
182 * Return the peeled value of the oid currently being iterated via
183 * for_each_ref(), etc. This is equivalent to calling:
184 *
185 * peel_object(r, oid, &peeled);
186 *
187 * with the "oid" value given to the refs_for_each_cb callback, except
188 * that some ref storage may be able to answer the query without
189 * actually loading the object in memory.
190 */
191int peel_iterated_oid(struct repository *r,
192 const struct object_id *base, struct object_id *peeled);
193
194/**
195 * Resolve refname in the nested "gitlink" repository in the specified
196 * submodule (which must be non-NULL). If the resolution is
197 * successful, return 0 and set oid to the name of the object;
198 * otherwise, return a non-zero value.
199 */
200int repo_resolve_gitlink_ref(struct repository *r,
201 const char *submodule, const char *refname,
202 struct object_id *oid);
203
204/*
205 * Return true iff abbrev_name is a possible abbreviation for
206 * full_name according to the rules defined by ref_rev_parse_rules in
207 * refs.c.
208 */
209int refname_match(const char *abbrev_name, const char *full_name);
210
211/*
212 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
213 * the results to 'prefixes'
214 */
215struct strvec;
216void expand_ref_prefix(struct strvec *prefixes, const char *prefix);
217
218int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
219int repo_dwim_ref(struct repository *r, const char *str, int len,
220 struct object_id *oid, char **ref, int nonfatal_dangling_mark);
221int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
222
223/*
224 * Retrieves the default branch name for newly-initialized repositories.
225 *
226 * The return value is an allocated string.
227 */
228char *repo_default_branch_name(struct repository *r, int quiet);
229
230/*
231 * Copy "name" to "sb", expanding any special @-marks as handled by
232 * repo_interpret_branch_name(). The result is a non-qualified branch name
233 * (so "foo" or "origin/master" instead of "refs/heads/foo" or
234 * "refs/remotes/origin/master").
235 *
236 * Note that the resulting name may not be a syntactically valid refname.
237 *
238 * If "allowed" is non-zero, restrict the set of allowed expansions. See
239 * repo_interpret_branch_name() for details.
240 */
241void copy_branchname(struct repository *repo,
242 struct strbuf *sb, const char *name,
243 enum interpret_branch_kind allowed);
244
245/*
246 * Like copy_branchname() above, but confirm that the result is
247 * syntactically valid to be used as a local branch name in refs/heads/.
248 *
249 * The return value is "0" if the result is valid, and "-1" otherwise.
250 */
251int check_branch_ref(struct repository *repo, struct strbuf *sb, const char *name);
252
253/*
254 * Similar for a tag name in refs/tags/.
255 *
256 * The return value is "0" if the result is valid, and "-1" otherwise.
257 */
258int check_tag_ref(struct strbuf *sb, const char *name);
259
260/*
261 * A ref_transaction represents a collection of reference updates that
262 * should succeed or fail together.
263 *
264 * Calling sequence
265 * ----------------
266 *
267 * - Allocate and initialize a `struct ref_transaction` by calling
268 * `ref_transaction_begin()`.
269 *
270 * - Specify the intended ref updates by calling one or more of the
271 * following functions:
272 * - `ref_transaction_update()`
273 * - `ref_transaction_create()`
274 * - `ref_transaction_delete()`
275 * - `ref_transaction_verify()`
276 *
277 * - Then either:
278 *
279 * - Optionally call `ref_transaction_prepare()` to prepare the
280 * transaction. This locks all references, checks preconditions,
281 * etc. but doesn't finalize anything. If this step fails, the
282 * transaction has been closed and can only be freed. If this step
283 * succeeds, then `ref_transaction_commit()` is almost certain to
284 * succeed. However, you can still call `ref_transaction_abort()`
285 * if you decide not to commit the transaction after all.
286 *
287 * - Call `ref_transaction_commit()` to execute the transaction,
288 * make the changes permanent, and release all locks. If you
289 * haven't already called `ref_transaction_prepare()`, then
290 * `ref_transaction_commit()` calls it for you.
291 *
292 * Or
293 *
294 * - Call `ref_transaction_begin()` with REF_TRANSACTION_FLAG_INITIAL if the
295 * ref database is known to be empty and have no other writers (e.g. during
296 * clone). This is likely to be much faster than without the flag.
297 *
298 * - Then finally, call `ref_transaction_free()` to free the
299 * `ref_transaction` data structure.
300 *
301 * At any time before calling `ref_transaction_commit()`, you can call
302 * `ref_transaction_abort()` to abort the transaction, rollback any
303 * locks, and free any associated resources (including the
304 * `ref_transaction` data structure).
305 *
306 * Putting it all together, a complete reference update looks like
307 *
308 * struct ref_transaction *transaction;
309 * struct strbuf err = STRBUF_INIT;
310 * int ret = 0;
311 *
312 * transaction = ref_store_transaction_begin(refs, 0, &err);
313 * if (!transaction ||
314 * ref_transaction_update(...) ||
315 * ref_transaction_create(...) ||
316 * ...etc... ||
317 * ref_transaction_commit(transaction, &err)) {
318 * error("%s", err.buf);
319 * ret = -1;
320 * }
321 * ref_transaction_free(transaction);
322 * strbuf_release(&err);
323 * return ret;
324 *
325 * Error handling
326 * --------------
327 *
328 * On error, transaction functions append a message about what
329 * went wrong to the 'err' argument. The message mentions what
330 * ref was being updated (if any) when the error occurred so it
331 * can be passed to 'die' or 'error' as-is.
332 *
333 * The message is appended to err without first clearing err.
334 * err will not be '\n' terminated.
335 *
336 * Caveats
337 * -------
338 *
339 * Note that no locks are taken, and no refs are read, until
340 * `ref_transaction_prepare()` or `ref_transaction_commit()` is
341 * called. So, for example, `ref_transaction_verify()` won't report a
342 * verification failure until the commit is attempted.
343 */
344struct ref_transaction;
345
346/*
347 * Bit values set in the flags argument passed to refs_for_each_cb() and
348 * stored in ref_iterator::flags. Other bits are for internal use
349 * only:
350 */
351enum reference_status {
352 /* Reference is a symbolic reference. */
353 REF_ISSYMREF = (1 << 0),
354
355 /* Reference is a packed reference. */
356 REF_ISPACKED = (1 << 1),
357
358 /*
359 * Reference cannot be resolved to an object name: dangling symbolic
360 * reference (directly or indirectly), corrupt reference file,
361 * reference exists but name is bad, or symbolic reference refers to
362 * ill-formatted reference name.
363 */
364 REF_ISBROKEN = (1 << 2),
365
366 /*
367 * Reference name is not well formed.
368 *
369 * See git-check-ref-format(1) for the definition of well formed ref names.
370 */
371 REF_BAD_NAME = (1 << 3),
372};
373
374/* A reference passed to `for_each_ref()`-style callbacks. */
375struct reference {
376 /* The fully-qualified name of the reference. */
377 const char *name;
378
379 /* The target of a symbolic ref. `NULL` for direct references. */
380 const char *target;
381
382 /*
383 * The object ID of a reference. Either the direct object ID or the
384 * resolved object ID in the case of a symbolic ref. May be the zero
385 * object ID in case the symbolic ref cannot be resolved.
386 */
387 const struct object_id *oid;
388
389 /*
390 * An optional peeled object ID. This field _may_ be set for tags in
391 * case the peeled value is present in the backend. Please refer to
392 * `reference_get_peeled_oid()`.
393 */
394 const struct object_id *peeled_oid;
395
396 /* A bitfield of `enum reference_status` flags. */
397 unsigned flags;
398};
399
400/*
401 * Peel the tag to a non-tag commit. If present, this uses the peeled object ID
402 * exposed by the reference backend. Otherwise, the object is peeled via the
403 * object database, which is less efficient.
404 *
405 * Return `0` if the reference could be peeled, a negative error code
406 * otherwise.
407 */
408int reference_get_peeled_oid(struct repository *repo,
409 const struct reference *ref,
410 struct object_id *peeled_oid);
411
412/*
413 * The signature for the callback function for the for_each_*()
414 * functions below. The memory pointed to by the `struct reference`
415 * argument is only guaranteed to be valid for the duration of a
416 * single callback invocation.
417 */
418typedef int refs_for_each_cb(const struct reference *ref, void *cb_data);
419
420/*
421 * These flags are passed to refs_ref_iterator_begin() (and do_for_each_ref(),
422 * which feeds it).
423 */
424enum refs_for_each_flag {
425 /*
426 * Include broken references in a do_for_each_ref*() iteration, which
427 * would normally be omitted. This includes both refs that point to
428 * missing objects (a true repository corruption), ones with illegal
429 * names (which we prefer not to expose to callers), as well as
430 * dangling symbolic refs (i.e., those that point to a non-existent
431 * ref; this is not a corruption, but as they have no valid oid, we
432 * omit them from normal iteration results).
433 */
434 REFS_FOR_EACH_INCLUDE_BROKEN = (1 << 0),
435
436 /*
437 * Only include per-worktree refs in a do_for_each_ref*() iteration.
438 * Normally this will be used with a files ref_store, since that's
439 * where all reference backends will presumably store their
440 * per-worktree refs.
441 */
442 REFS_FOR_EACH_PER_WORKTREE_ONLY = (1 << 1),
443
444 /*
445 * Omit dangling symrefs from output; this only has an effect with
446 * INCLUDE_BROKEN, since they are otherwise not included at all.
447 */
448 REFS_FOR_EACH_OMIT_DANGLING_SYMREFS = (1 << 2),
449
450 /*
451 * Include root refs i.e. HEAD and pseudorefs along with the regular
452 * refs.
453 */
454 REFS_FOR_EACH_INCLUDE_ROOT_REFS = (1 << 3),
455};
456
457/*
458 * The following functions invoke the specified callback function for
459 * each reference indicated. If the function ever returns a nonzero
460 * value, stop the iteration and return that value. Please note that
461 * it is not safe to modify references while an iteration is in
462 * progress, unless the same callback function invocation that
463 * modifies the reference also returns a nonzero value to immediately
464 * stop the iteration. Returned references are sorted.
465 */
466int refs_head_ref(struct ref_store *refs,
467 refs_for_each_cb fn, void *cb_data);
468int refs_head_ref_namespaced(struct ref_store *refs,
469 refs_for_each_cb fn, void *cb_data);
470
471
472struct refs_for_each_ref_options {
473 /* Only iterate over references that have this given prefix. */
474 const char *prefix;
475
476 /*
477 * A globbing pattern that can be used to only yield refs that match.
478 * If given, refs will be matched against the pattern with
479 * `wildmatch()`.
480 *
481 * If the pattern doesn't contain any globbing characters then it is
482 * treated as if it was ending with "/" and "*".
483 */
484 const char *pattern;
485
486 /*
487 * If set, only yield refs part of the configured namespace. Exclude
488 * patterns will be rewritten to apply to the namespace, and the prefix
489 * will be considered relative to the namespace.
490 */
491 const char *namespace;
492
493 /*
494 * Exclude any references that match any of these patterns on a
495 * best-effort basis. The caller needs to be prepared for the exclude
496 * patterns to be ignored.
497 *
498 * The array must be terminated with a NULL sentinel value.
499 */
500 const char **exclude_patterns;
501
502 /*
503 * The number of bytes to trim from the refname. Note that the trimmed
504 * bytes must not cause the reference to become empty. As such, this
505 * field should typically only be set when one uses a `prefix` ending
506 * in a slash.
507 */
508 size_t trim_prefix;
509
510 /* Flags that change which refs will be included. */
511 enum refs_for_each_flag flags;
512};
513
514int refs_for_each_ref(struct ref_store *refs,
515 refs_for_each_cb fn, void *cb_data);
516int refs_for_each_ref_ext(struct ref_store *refs,
517 refs_for_each_cb cb, void *cb_data,
518 const struct refs_for_each_ref_options *opts);
519int refs_for_each_tag_ref(struct ref_store *refs,
520 refs_for_each_cb fn, void *cb_data);
521int refs_for_each_branch_ref(struct ref_store *refs,
522 refs_for_each_cb fn, void *cb_data);
523int refs_for_each_remote_ref(struct ref_store *refs,
524 refs_for_each_cb fn, void *cb_data);
525int refs_for_each_replace_ref(struct ref_store *refs,
526 refs_for_each_cb fn, void *cb_data);
527
528/**
529 * Iterate all refs in "prefixes" by partitioning prefixes into disjoint sets
530 * and iterating the longest-common prefix of each set.
531 */
532int refs_for_each_ref_in_prefixes(struct ref_store *refs,
533 const char **prefixes,
534 const struct refs_for_each_ref_options *opts,
535 refs_for_each_cb cb, void *cb_data);
536
537/*
538 * Normalizes partial refs to their fully qualified form.
539 * Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'.
540 * <prefix> will default to 'refs/' if NULL.
541 *
542 * item.string will be set to the result.
543 * item.util will be set to NULL if <pattern> contains glob characters, or
544 * non-NULL if it doesn't.
545 */
546void normalize_glob_ref(struct string_list_item *item, const char *prefix,
547 const char *pattern);
548
549static inline const char *has_glob_specials(const char *pattern)
550{
551 return strpbrk(pattern, "?*[");
552}
553
554void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
555 const char *indent, int dry_run,
556 const struct string_list *refnames);
557
558/*
559 * Flags for controlling behaviour of refs_optimize()
560 * REFS_OPTIMIZE_PRUNE: Prune loose refs after packing
561 * REFS_OPTIMIZE_AUTO: Pack refs on a best effort basis. The heuristics and end
562 * result are decided by the ref backend. Backends may ignore
563 * this flag and fall back to a normal repack.
564 */
565#define REFS_OPTIMIZE_PRUNE (1 << 0)
566#define REFS_OPTIMIZE_AUTO (1 << 1)
567
568struct refs_optimize_opts {
569 unsigned int flags;
570 struct ref_exclusions *exclusions;
571 struct string_list *includes;
572};
573
574/*
575 * Optimize the ref store. The exact behavior is up to the backend.
576 * For the files backend, this is equivalent to packing refs.
577 */
578int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts);
579
580/*
581 * Check if refs backend can be optimized by calling 'refs_optimize'.
582 */
583int refs_optimize_required(struct ref_store *ref_store,
584 struct refs_optimize_opts *opts,
585 bool *required);
586
587/*
588 * Setup reflog before using. Fill in err and return -1 on failure.
589 */
590int refs_create_reflog(struct ref_store *refs, const char *refname,
591 struct strbuf *err);
592
593/**
594 * Reads log for the value of ref during at_time (in which case "cnt" should be
595 * negative) or the reflog "cnt" entries from the top (in which case "at_time"
596 * should be 0).
597 *
598 * If we found the reflog entry in question, returns 0 (and details of the
599 * entry can be found in the out-parameters).
600 *
601 * If we ran out of reflog entries, the out-parameters are filled with the
602 * details of the oldest entry we did find, and the function returns 1. Note
603 * that there is one important special case here! If the reflog was empty
604 * and the caller asked for the 0-th cnt, we will return "1" but leave the
605 * "oid" field untouched.
606 **/
607int read_ref_at(struct ref_store *refs,
608 const char *refname, unsigned int flags,
609 timestamp_t at_time, int cnt,
610 struct object_id *oid, char **msg,
611 timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
612
613/** Check if a particular reflog exists */
614int refs_reflog_exists(struct ref_store *refs, const char *refname);
615
616/*
617 * Delete the specified reference. If old_oid is non-NULL, then
618 * verify that the current value of the reference is old_oid before
619 * deleting it. If old_oid is NULL, delete the reference if it
620 * exists, regardless of its old value. It is an error for old_oid to
621 * be null_oid. msg and flags are passed through to
622 * ref_transaction_delete().
623 */
624int refs_delete_ref(struct ref_store *refs, const char *msg,
625 const char *refname,
626 const struct object_id *old_oid,
627 unsigned int flags);
628
629/*
630 * Delete the specified references. If there are any problems, emit
631 * errors but attempt to keep going (i.e., the deletes are not done in
632 * an all-or-nothing transaction). msg and flags are passed through to
633 * ref_transaction_delete().
634 */
635int refs_delete_refs(struct ref_store *refs, const char *msg,
636 struct string_list *refnames, unsigned int flags);
637
638/** Delete a reflog */
639int refs_delete_reflog(struct ref_store *refs, const char *refname);
640
641/*
642 * Callback to process a reflog entry found by the iteration functions (see
643 * below).
644 *
645 * The committer parameter is a single string, in the form
646 * "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" (without double quotes).
647 *
648 * The timestamp parameter gives the time when entry was created as the number
649 * of seconds since the UNIX epoch.
650 *
651 * The tz parameter gives the timezone offset for the user who created
652 * the reflog entry, and its value gives a positive or negative offset
653 * from UTC. Its absolute value is formed by multiplying the hour
654 * part by 100 and adding the minute part. For example, 1 hour ahead
655 * of UTC, CET == "+0100", is represented as positive one hundred (not
656 * positive sixty).
657 *
658 * The msg parameter is a single complete line; a reflog message given
659 * to refs_delete_ref, refs_update_ref, etc. is returned to the
660 * callback normalized---each run of whitespaces are squashed into a
661 * single whitespace, trailing whitespace, if exists, is trimmed, and
662 * then a single LF is added at the end.
663 *
664 * The cb_data is a caller-supplied pointer given to the iterator
665 * functions.
666 */
667typedef int each_reflog_ent_fn(const char *refname,
668 struct object_id *old_oid,
669 struct object_id *new_oid,
670 const char *committer,
671 timestamp_t timestamp,
672 int tz, const char *msg,
673 void *cb_data);
674
675/* Iterate over reflog entries in the log for `refname`. */
676
677/* oldest entry first */
678int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
679 each_reflog_ent_fn fn, void *cb_data);
680
681/* youngest entry first */
682int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
683 const char *refname,
684 each_reflog_ent_fn fn,
685 void *cb_data);
686
687/*
688 * The signature for the callback function for the refs_for_each_reflog()
689 * functions below. The memory pointed to by the refname argument is only
690 * guaranteed to be valid for the duration of a single callback invocation.
691 */
692typedef int each_reflog_fn(const char *refname, void *cb_data);
693
694/*
695 * Calls the specified function for each reflog file until it returns nonzero,
696 * and returns the value. Reflog file order is unspecified.
697 */
698int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data);
699
700#define REFNAME_ALLOW_ONELEVEL 1
701#define REFNAME_REFSPEC_PATTERN 2
702
703/*
704 * Return 0 iff refname has the correct format for a refname according
705 * to the rules described in Documentation/git-check-ref-format.adoc.
706 * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level
707 * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then
708 * allow a single "*" wildcard character in the refspec. No leading or
709 * repeated slashes are accepted.
710 */
711int check_refname_format(const char *refname, int flags);
712
713struct fsck_ref_report;
714
715/*
716 * Perform generic checks for a specific direct ref. This function is
717 * expected to be called by the ref backends for every symbolic ref.
718 */
719int refs_fsck_ref(struct ref_store *refs, struct fsck_options *o,
720 struct fsck_ref_report *report,
721 const char *refname, const struct object_id *oid);
722
723/*
724 * Perform generic checks for a specific symref target. This function is
725 * expected to be called by the ref backends for every symbolic ref.
726 */
727int refs_fsck_symref(struct ref_store *refs, struct fsck_options *o,
728 struct fsck_ref_report *report,
729 const char *refname, const char *target);
730
731/*
732 * Check the reference database for consistency. Return 0 if refs and
733 * reflogs are consistent, and non-zero otherwise. The errors will be
734 * written to stderr.
735 */
736int refs_fsck(struct ref_store *refs, struct fsck_options *o,
737 struct worktree *wt);
738
739/*
740 * Apply the rules from check_refname_format, but mutate the result until it
741 * is acceptable, and place the result in "out".
742 */
743void sanitize_refname_component(const char *refname, struct strbuf *out);
744
745const char *prettify_refname(const char *refname);
746
747char *refs_shorten_unambiguous_ref(struct ref_store *refs,
748 const char *refname, int strict);
749
750/** rename ref, return 0 on success **/
751int refs_rename_ref(struct ref_store *refs, const char *oldref,
752 const char *newref, const char *logmsg);
753
754/** copy ref, return 0 on success **/
755int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
756 const char *newref, const char *logmsg);
757
758int refs_update_symref(struct ref_store *refs, const char *refname,
759 const char *target, const char *logmsg);
760
761int refs_update_symref_extended(struct ref_store *refs, const char *refname,
762 const char *target, const char *logmsg,
763 struct strbuf *referent, int create_only);
764
765enum action_on_err {
766 UPDATE_REFS_MSG_ON_ERR,
767 UPDATE_REFS_DIE_ON_ERR,
768 UPDATE_REFS_QUIET_ON_ERR
769};
770
771enum ref_transaction_flag {
772 /*
773 * The ref transaction is part of the initial creation of the ref store
774 * and can thus assume that the ref store is completely empty. This
775 * allows the backend to perform the transaction more efficiently by
776 * skipping certain checks.
777 *
778 * It is a bug to set this flag when there might be other processes
779 * accessing the repository or if there are existing references that
780 * might conflict with the ones being created. All old_oid values must
781 * either be absent or null_oid.
782 */
783 REF_TRANSACTION_FLAG_INITIAL = (1 << 0),
784
785 /*
786 * The transaction mechanism by default fails all updates if any conflict
787 * is detected. This flag allows transactions to partially apply updates
788 * while rejecting updates which do not match the expected state.
789 */
790 REF_TRANSACTION_ALLOW_FAILURE = (1 << 1),
791};
792
793/*
794 * Begin a reference transaction. The reference transaction must
795 * be freed by calling ref_transaction_free().
796 */
797struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
798 unsigned int flags,
799 struct strbuf *err);
800
801/*
802 * Reference transaction updates
803 *
804 * The following four functions add a reference check or update to a
805 * ref_transaction. They have some common similar parameters:
806 *
807 * transaction -- a pointer to an open ref_transaction, obtained
808 * from ref_transaction_begin().
809 *
810 * refname -- the name of the reference to be affected.
811 *
812 * new_oid -- the object ID that should be set to be the new value
813 * of the reference. Some functions allow this parameter to be
814 * NULL, meaning that the reference is not changed, or
815 * null_oid, meaning that the reference should be deleted. A
816 * copy of this value is made in the transaction.
817 *
818 * old_oid -- the object ID that the reference must have before
819 * the update. Some functions allow this parameter to be NULL,
820 * meaning that the old value of the reference is not checked,
821 * or null_oid, meaning that the reference must not exist
822 * before the update. A copy of this value is made in the
823 * transaction.
824 *
825 * new_target -- the target reference that the reference will be
826 * updated to point to. If the reference is a regular reference,
827 * it will be converted to a symbolic reference. Cannot be set
828 * together with `new_oid`. A copy of this value is made in the
829 * transaction.
830 *
831 * old_target -- the reference that the reference must be pointing to.
832 * Canont be set together with `old_oid`. A copy of this value is
833 * made in the transaction.
834 *
835 * flags -- flags affecting the update, passed to
836 * update_ref_lock(). Possible flags: REF_NO_DEREF,
837 * REF_FORCE_CREATE_REFLOG. See those constants for more
838 * information.
839 *
840 * msg -- a message describing the change (for the reflog).
841 *
842 * err -- a strbuf for receiving a description of any error that
843 * might have occurred.
844 *
845 * The functions make internal copies of refname and msg, so the
846 * caller retains ownership of these parameters.
847 *
848 * The functions return 0 on success and non-zero on failure. A
849 * failure means that the transaction as a whole has failed and needs
850 * to be rolled back.
851 */
852
853/*
854 * The following flags can be passed to ref_transaction_update() etc.
855 * Internally, they are stored in `ref_update::flags`, along with some
856 * internal flags.
857 */
858
859/*
860 * Act on the ref directly; i.e., without dereferencing symbolic refs.
861 * If this flag is not specified, then symbolic references are
862 * dereferenced and the update is applied to the referent.
863 */
864#define REF_NO_DEREF (1 << 0)
865
866/*
867 * Force the creation of a reflog for this reference, even if it
868 * didn't previously have a reflog.
869 */
870#define REF_FORCE_CREATE_REFLOG (1 << 1)
871
872/*
873 * Blindly write an object_id. This is useful for testing data corruption
874 * scenarios.
875 */
876#define REF_SKIP_OID_VERIFICATION (1 << 10)
877
878/*
879 * Skip verifying refname. This is useful for testing data corruption scenarios.
880 */
881#define REF_SKIP_REFNAME_VERIFICATION (1 << 11)
882
883/*
884 * Skip creation of a reflog entry, even if it would have otherwise been
885 * created.
886 */
887#define REF_SKIP_CREATE_REFLOG (1 << 12)
888
889/*
890 * When writing a REF_LOG_ONLY record, use the old and new object IDs provided
891 * in the update instead of resolving the old object ID. The caller must also
892 * set both REF_HAVE_OLD and REF_HAVE_NEW.
893 */
894#define REF_LOG_USE_PROVIDED_OIDS (1 << 13)
895
896/*
897 * Bitmask of all of the flags that are allowed to be passed in to
898 * ref_transaction_update() and friends:
899 */
900#define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
901 (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION | \
902 REF_SKIP_REFNAME_VERIFICATION | REF_SKIP_CREATE_REFLOG | REF_LOG_USE_PROVIDED_OIDS)
903
904/*
905 * Add a reference update to transaction. `new_oid` is the value that
906 * the reference should have after the update, or `null_oid` if it
907 * should be deleted. If `new_oid` is NULL, then the reference is not
908 * changed at all. `old_oid` is the value that the reference must have
909 * before the update, or `null_oid` if it must not have existed
910 * beforehand. The old value is checked after the lock is taken to
911 * prevent races. If the old value doesn't agree with old_oid, the
912 * whole transaction fails. If old_oid is NULL, then the previous
913 * value is not checked. If `old_target` is not NULL, treat the reference
914 * as a symbolic ref and validate that its target before the update is
915 * `old_target`. If the `new_target` is not NULL, then the reference
916 * will be updated to a symbolic ref which targets `new_target`.
917 * Together, these allow us to update between regular refs and symrefs.
918 *
919 * See the above comment "Reference transaction updates" for more
920 * information.
921 */
922enum ref_transaction_error ref_transaction_update(struct ref_transaction *transaction,
923 const char *refname,
924 const struct object_id *new_oid,
925 const struct object_id *old_oid,
926 const char *new_target,
927 const char *old_target,
928 unsigned int flags, const char *msg,
929 struct strbuf *err);
930
931/*
932 * Similar to `ref_transaction_update`, but this function is only for adding
933 * a reflog update. Supports providing custom committer information. The index
934 * field can be utiltized to order updates as desired. When set to zero, the
935 * updates default to being ordered by refname.
936 */
937int ref_transaction_update_reflog(struct ref_transaction *transaction,
938 const char *refname,
939 const struct object_id *new_oid,
940 const struct object_id *old_oid,
941 const char *committer_info,
942 const char *msg,
943 uint64_t index,
944 struct strbuf *err);
945
946/*
947 * Add a reference creation to transaction. new_oid is the value that
948 * the reference should have after the update; it must not be
949 * null_oid. It is verified that the reference does not exist
950 * already.
951 *
952 * See the above comment "Reference transaction updates" for more
953 * information.
954 */
955int ref_transaction_create(struct ref_transaction *transaction,
956 const char *refname,
957 const struct object_id *new_oid,
958 const char *new_target,
959 unsigned int flags, const char *msg,
960 struct strbuf *err);
961
962/*
963 * Add a reference deletion to transaction. If old_oid is non-NULL,
964 * then it holds the value that the reference should have had before
965 * the update (which must not be null_oid).
966 *
967 * See the above comment "Reference transaction updates" for more
968 * information.
969 */
970int ref_transaction_delete(struct ref_transaction *transaction,
971 const char *refname,
972 const struct object_id *old_oid,
973 const char *old_target,
974 unsigned int flags,
975 const char *msg,
976 struct strbuf *err);
977
978/*
979 * Verify, within a transaction, that refname has the value old_oid,
980 * or, if old_oid is null_oid, then verify that the reference
981 * doesn't exist. old_oid must be non-NULL.
982 *
983 * See the above comment "Reference transaction updates" for more
984 * information.
985 */
986int ref_transaction_verify(struct ref_transaction *transaction,
987 const char *refname,
988 const struct object_id *old_oid,
989 const char *old_target,
990 unsigned int flags,
991 struct strbuf *err);
992
993/*
994 * Perform the preparatory stages of committing `transaction`. Acquire
995 * any needed locks, check preconditions, etc.; basically, do as much
996 * as possible to ensure that the transaction will be able to go
997 * through, stopping just short of making any irrevocable or
998 * user-visible changes. The updates that this function prepares can
999 * be finished up by calling `ref_transaction_commit()` or rolled back
1000 * by calling `ref_transaction_abort()`.
1001 *
1002 * On success, return 0 and leave the transaction in "prepared" state.
1003 * On failure, abort the transaction, write an error message to `err`,
1004 * and return one of the `TRANSACTION_*` constants.
1005 *
1006 * Callers who don't need such fine-grained control over committing
1007 * reference transactions should just call `ref_transaction_commit()`.
1008 */
1009int ref_transaction_prepare(struct ref_transaction *transaction,
1010 struct strbuf *err);
1011
1012/*
1013 * Commit all of the changes that have been queued in transaction, as
1014 * atomically as possible. On success, return 0 and leave the
1015 * transaction in "closed" state. On failure, roll back the
1016 * transaction, write an error message to `err`, and return one of the
1017 * `TRANSACTION_*` constants
1018 */
1019int ref_transaction_commit(struct ref_transaction *transaction,
1020 struct strbuf *err);
1021
1022/*
1023 * Abort `transaction`, which has been begun and possibly prepared,
1024 * but not yet committed.
1025 */
1026int ref_transaction_abort(struct ref_transaction *transaction,
1027 struct strbuf *err);
1028
1029/*
1030 * Execute the given callback function for each of the reference updates which
1031 * have been queued in the given transaction. `old_oid` and `new_oid` may be
1032 * `NULL` pointers depending on whether the update has these object IDs set or
1033 * not.
1034 */
1035typedef void ref_transaction_for_each_queued_update_fn(const char *refname,
1036 const struct object_id *old_oid,
1037 const struct object_id *new_oid,
1038 void *cb_data);
1039void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
1040 ref_transaction_for_each_queued_update_fn cb,
1041 void *cb_data);
1042
1043/*
1044 * Execute the given callback function for each of the reference updates which
1045 * have been rejected in the given transaction.
1046 */
1047typedef void ref_transaction_for_each_rejected_update_fn(const char *refname,
1048 const struct object_id *old_oid,
1049 const struct object_id *new_oid,
1050 const char *old_target,
1051 const char *new_target,
1052 enum ref_transaction_error err,
1053 const char *details,
1054 void *cb_data);
1055void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction,
1056 ref_transaction_for_each_rejected_update_fn cb,
1057 void *cb_data);
1058
1059/*
1060 * Translate errors to human readable error messages.
1061 */
1062const char *ref_transaction_error_msg(enum ref_transaction_error err);
1063
1064/*
1065 * Free `*transaction` and all associated data.
1066 */
1067void ref_transaction_free(struct ref_transaction *transaction);
1068
1069/**
1070 * Lock, update, and unlock a single reference. This function
1071 * basically does a transaction containing a single call to
1072 * ref_transaction_update(). The parameters to this function have the
1073 * same meaning as the corresponding parameters to
1074 * ref_transaction_update(). Handle errors as requested by the `onerr`
1075 * argument.
1076 */
1077int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname,
1078 const struct object_id *new_oid, const struct object_id *old_oid,
1079 unsigned int flags, enum action_on_err onerr);
1080
1081int parse_hide_refs_config(const char *var, const char *value, const char *,
1082 struct strvec *);
1083
1084/*
1085 * Check whether a ref is hidden. If no namespace is set, both the first and
1086 * the second parameter point to the full ref name. If a namespace is set and
1087 * the ref is inside that namespace, the first parameter is a pointer to the
1088 * name of the ref with the namespace prefix removed. If a namespace is set and
1089 * the ref is outside that namespace, the first parameter is NULL. The second
1090 * parameter always points to the full ref name.
1091 */
1092int ref_is_hidden(const char *, const char *, const struct strvec *);
1093
1094/*
1095 * Returns an array of patterns to use as excluded_patterns, if none of the
1096 * hidden references use the token '!' or '^'.
1097 */
1098const char **hidden_refs_to_excludes(const struct strvec *hide_refs);
1099
1100/*
1101 * Prefix all exclude patterns with the namespace, if any. This is required
1102 * because exclude patterns apply to the stripped reference name, not the full
1103 * reference name with the namespace.
1104 */
1105const char **get_namespaced_exclude_patterns(const char **exclude_patterns,
1106 const char *namespace,
1107 struct strvec *out);
1108
1109/* Is this a per-worktree ref living in the refs/ namespace? */
1110int is_per_worktree_ref(const char *refname);
1111
1112/* Describes how a refname relates to worktrees */
1113enum ref_worktree_type {
1114 REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or
1115 refs/bisect/SOMETHING */
1116 REF_WORKTREE_MAIN, /* explicitly in main worktree, eg.
1117 main-worktree/HEAD */
1118 REF_WORKTREE_OTHER, /* explicitly in named worktree, eg.
1119 worktrees/bla/HEAD */
1120 REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */
1121};
1122
1123/*
1124 * Parse a `maybe_worktree_ref` as a ref that possibly refers to a worktree ref
1125 * (ie. either REFNAME, main-worktree/REFNAME or worktree/WORKTREE/REFNAME). It
1126 * returns what kind of ref was found, and in case of REF_WORKTREE_OTHER, the
1127 * worktree name is returned in `worktree_name` (pointing into
1128 * `maybe_worktree_ref`) and `worktree_name_length`. The bare refname (the
1129 * refname stripped of prefixes) is returned in `bare_refname`. The
1130 * `worktree_name`, `worktree_name_length` and `bare_refname` arguments may be
1131 * NULL.
1132 */
1133enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
1134 const char **worktree_name,
1135 int *worktree_name_length,
1136 const char **bare_refname);
1137
1138enum expire_reflog_flags {
1139 EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
1140 EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
1141 EXPIRE_REFLOGS_REWRITE = 1 << 2,
1142};
1143
1144/*
1145 * The following interface is used for reflog expiration. The caller
1146 * calls refs_reflog_expire(), supplying it with three callback functions,
1147 * of the following types. The callback functions define the
1148 * expiration policy that is desired.
1149 *
1150 * reflog_expiry_prepare_fn -- Called once after the reference is
1151 * locked. Called with the OID of the locked reference.
1152 *
1153 * reflog_expiry_should_prune_fn -- Called once for each entry in the
1154 * existing reflog. It should return true iff that entry should be
1155 * pruned.
1156 *
1157 * reflog_expiry_cleanup_fn -- Called once before the reference is
1158 * unlocked again.
1159 */
1160typedef void reflog_expiry_prepare_fn(const char *refname,
1161 const struct object_id *oid,
1162 void *cb_data);
1163typedef int reflog_expiry_should_prune_fn(struct object_id *ooid,
1164 struct object_id *noid,
1165 const char *email,
1166 timestamp_t timestamp, int tz,
1167 const char *message, void *cb_data);
1168typedef void reflog_expiry_cleanup_fn(void *cb_data);
1169
1170/*
1171 * Expire reflog entries for the specified reference.
1172 * flags is a combination of the constants in
1173 * enum expire_reflog_flags. The three function pointers are described
1174 * above. On success, return zero.
1175 */
1176int refs_reflog_expire(struct ref_store *refs,
1177 const char *refname,
1178 unsigned int flags,
1179 reflog_expiry_prepare_fn prepare_fn,
1180 reflog_expiry_should_prune_fn should_prune_fn,
1181 reflog_expiry_cleanup_fn cleanup_fn,
1182 void *policy_cb_data);
1183
1184struct ref_store *get_main_ref_store(struct repository *r);
1185
1186/**
1187 * Submodules
1188 * ----------
1189 *
1190 * If you want to iterate the refs of a submodule you first need to add the
1191 * submodules object database. You can do this by a code-snippet like
1192 * this:
1193 *
1194 * const char *path = "path/to/submodule"
1195 * if (add_submodule_odb(path))
1196 * die("Error submodule '%s' not populated.", path);
1197 *
1198 * `add_submodule_odb()` will return zero on success. If you
1199 * do not do this you will get an error for each ref that it does not point
1200 * to a valid object.
1201 *
1202 * Note: As a side-effect of this you cannot safely assume that all
1203 * objects you lookup are available in superproject. All submodule objects
1204 * will be available the same way as the superprojects objects.
1205 *
1206 * Example:
1207 * --------
1208 *
1209 * ----
1210 * static int handle_remote_ref(const char *refname,
1211 * const unsigned char *sha1, int flags, void *cb_data)
1212 * {
1213 * struct strbuf *output = cb_data;
1214 * strbuf_addf(output, "%s\n", refname);
1215 * return 0;
1216 * }
1217 *
1218 */
1219
1220/*
1221 * Return the ref_store instance for the specified submodule. For the
1222 * main repository, use submodule==NULL; such a call cannot fail. For
1223 * a submodule, the submodule must exist and be a nonbare repository,
1224 * otherwise return NULL. If the requested reference store has not yet
1225 * been initialized, initialize it first.
1226 *
1227 * For backwards compatibility, submodule=="" is treated the same as
1228 * submodule==NULL.
1229 */
1230struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
1231 const char *submodule);
1232struct ref_store *get_worktree_ref_store(const struct worktree *wt);
1233
1234/*
1235 * Some of the names specified by refs have special meaning to Git.
1236 * Organize these namespaces in a common 'ref_namespace' array for
1237 * reference from multiple places in the codebase.
1238 */
1239
1240struct ref_namespace_info {
1241 const char *ref;
1242 enum decoration_type decoration;
1243
1244 /*
1245 * If 'exact' is true, then we must match the 'ref' exactly.
1246 * Otherwise, use a prefix match.
1247 *
1248 * 'ref_updated' is for internal use. It represents whether the
1249 * 'ref' value was replaced from its original literal version.
1250 */
1251 unsigned exact:1,
1252 ref_updated:1;
1253};
1254
1255enum ref_namespace {
1256 NAMESPACE_HEAD,
1257 NAMESPACE_BRANCHES,
1258 NAMESPACE_TAGS,
1259 NAMESPACE_REMOTE_REFS,
1260 NAMESPACE_STASH,
1261 NAMESPACE_REPLACE,
1262 NAMESPACE_NOTES,
1263 NAMESPACE_PREFETCH,
1264 NAMESPACE_REWRITTEN,
1265
1266 /* Must be last */
1267 NAMESPACE__COUNT
1268};
1269
1270/* See refs.c for the contents of this array. */
1271extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT];
1272
1273/*
1274 * Some ref namespaces can be modified by config values or environment
1275 * variables. Modify a namespace as specified by its ref_namespace key.
1276 */
1277void update_ref_namespace(enum ref_namespace namespace, char *ref);
1278
1279/*
1280 * Check whether the provided name names a root reference. This function only
1281 * performs a syntactic check.
1282 *
1283 * A root ref is a reference that lives in the root of the reference hierarchy.
1284 * These references must conform to special syntax:
1285 *
1286 * - Their name must be all-uppercase or underscores ("_").
1287 *
1288 * - Their name must end with "_HEAD". As a special rule, "HEAD" is a root
1289 * ref, as well.
1290 *
1291 * - Their name may not contain a slash.
1292 *
1293 * There is a special set of irregular root refs that exist due to historic
1294 * reasons, only. This list shall not be expanded in the future:
1295 *
1296 * - AUTO_MERGE
1297 *
1298 * - BISECT_EXPECTED_REV
1299 *
1300 * - NOTES_MERGE_PARTIAL
1301 *
1302 * - NOTES_MERGE_REF
1303 *
1304 * - MERGE_AUTOSTASH
1305 */
1306int is_root_ref(const char *refname);
1307
1308/*
1309 * Pseudorefs are refs that have different semantics compared to
1310 * "normal" refs. These refs can thus not be stored in the ref backend,
1311 * but must always be accessed via the filesystem. The following refs
1312 * are pseudorefs:
1313 *
1314 * - FETCH_HEAD may contain multiple object IDs, and each one of them
1315 * carries additional metadata like where it came from.
1316 *
1317 * - MERGE_HEAD may contain multiple object IDs when merging multiple
1318 * heads.
1319 *
1320 * Reading, writing or deleting references must consistently go either
1321 * through the filesystem (pseudorefs) or through the reference
1322 * backend (normal ones).
1323 */
1324int is_pseudo_ref(const char *refname);
1325
1326/*
1327 * The following flags can be passed to `repo_migrate_ref_storage_format()`:
1328 *
1329 * - REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN: perform a dry-run migration
1330 * without touching the main repository. The result will be written into a
1331 * temporary ref storage directory.
1332 *
1333 * - REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG: skip migration of reflogs.
1334 */
1335#define REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN (1 << 0)
1336#define REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG (1 << 1)
1337
1338/*
1339 * Migrate the ref storage format used by the repository to the
1340 * specified one.
1341 */
1342int repo_migrate_ref_storage_format(struct repository *repo,
1343 enum ref_storage_format format,
1344 unsigned int flags,
1345 struct strbuf *err);
1346
1347/*
1348 * Reference iterators
1349 *
1350 * A reference iterator encapsulates the state of an in-progress
1351 * iteration over references. Create an instance of `struct
1352 * ref_iterator` via one of the functions in this module.
1353 *
1354 * A freshly-created ref_iterator doesn't yet point at a reference. To
1355 * advance the iterator, call ref_iterator_advance(). If successful,
1356 * this sets the iterator's refname, oid, and flags fields to describe
1357 * the next reference and returns ITER_OK. The data pointed at by
1358 * refname and oid belong to the iterator; if you want to retain them
1359 * after calling ref_iterator_advance() again or calling
1360 * ref_iterator_free(), you must make a copy. When the iteration has
1361 * been exhausted, ref_iterator_advance() releases any resources
1362 * associated with the iteration, frees the ref_iterator object, and
1363 * returns ITER_DONE. If you want to abort the iteration early, call
1364 * ref_iterator_free(), which also frees the ref_iterator object and
1365 * any associated resources. If there was an internal error advancing
1366 * to the next entry, ref_iterator_advance() aborts the iteration,
1367 * frees the ref_iterator, and returns ITER_ERROR.
1368 *
1369 * Putting it all together, a typical iteration looks like this:
1370 *
1371 * int ok;
1372 * struct ref_iterator *iter = ...;
1373 *
1374 * while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1375 * if (want_to_stop_iteration()) {
1376 * ok = ITER_DONE;
1377 * break;
1378 * }
1379 *
1380 * // Access information about the current reference:
1381 * if (!(iter->flags & REF_ISSYMREF))
1382 * printf("%s is %s\n", iter->refname, oid_to_hex(iter->oid));
1383 * }
1384 *
1385 * if (ok != ITER_DONE)
1386 * handle_error();
1387 * ref_iterator_free(iter);
1388 */
1389struct ref_iterator;
1390
1391/*
1392 * Return an iterator that goes over each reference in `refs` for
1393 * which the refname begins with prefix. If trim is non-zero, then
1394 * trim that many characters off the beginning of each refname.
1395 * The output is ordered by refname.
1396 */
1397struct ref_iterator *refs_ref_iterator_begin(
1398 struct ref_store *refs,
1399 const char *prefix, const char **exclude_patterns,
1400 int trim, enum refs_for_each_flag flags);
1401
1402/*
1403 * Advance the iterator to the first or next item and return ITER_OK.
1404 * If the iteration is exhausted, free the resources associated with
1405 * the ref_iterator and return ITER_DONE. On errors, free the iterator
1406 * resources and return ITER_ERROR. It is a bug to use ref_iterator or
1407 * call this function again after it has returned ITER_DONE or
1408 * ITER_ERROR.
1409 */
1410int ref_iterator_advance(struct ref_iterator *ref_iterator);
1411
1412enum ref_iterator_seek_flag {
1413 /*
1414 * When the REF_ITERATOR_SEEK_SET_PREFIX flag is set, the iterator's prefix is
1415 * updated to match the provided string, affecting all subsequent iterations. If
1416 * not, the iterator seeks to the specified reference and clears any previously
1417 * set prefix.
1418 */
1419 REF_ITERATOR_SEEK_SET_PREFIX = (1 << 0),
1420};
1421
1422/*
1423 * Seek the iterator to the first reference matching the given seek string.
1424 * The seek string is matched as a literal string, without regard for path
1425 * separators. If seek is NULL or the empty string, seek the iterator to the
1426 * first reference again.
1427 *
1428 * This function is expected to behave as if a new ref iterator has been
1429 * created, but allows reuse of existing iterators for optimization.
1430 *
1431 * Returns 0 on success, a negative error code otherwise.
1432 */
1433int ref_iterator_seek(struct ref_iterator *ref_iterator, const char *refname,
1434 unsigned int flags);
1435
1436/* Free the reference iterator and any associated resources. */
1437void ref_iterator_free(struct ref_iterator *ref_iterator);
1438
1439/*
1440 * The common backend for the for_each_*ref* functions. Call fn for
1441 * each reference in iter. If the iterator itself ever returns
1442 * ITER_ERROR, return -1. If fn ever returns a non-zero value, stop
1443 * the iteration and return that value. Otherwise, return 0. In any
1444 * case, free the iterator when done. This function is basically an
1445 * adapter between the callback style of reference iteration and the
1446 * iterator style.
1447 */
1448int do_for_each_ref_iterator(struct ref_iterator *iter,
1449 refs_for_each_cb fn, void *cb_data);
1450
1451/*
1452 * Git only recognizes a directory as a repository if it contains:
1453 * - HEAD file
1454 * - refs/ folder
1455 * While it is necessary within the files backend, newer backends may not
1456 * follow the same structure. To go around this, we create stubs as necessary.
1457 *
1458 * If provided with a 'refs_heads_content', we create the 'refs/heads/head' file
1459 * with the provided message.
1460 */
1461void refs_create_refdir_stubs(struct repository *repo, const char *refdir,
1462 const char *refs_heads_content);
1463
1464#endif /* REFS_H */