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