reftable/reader: introduce refcounting
[git/gitster.git] / refs.h
blobb3e39bc257046d9672d2fdafb50e2e05c76f4bee
1 #ifndef REFS_H
2 #define REFS_H
4 #include "commit.h"
5 #include "repository.h"
7 struct object_id;
8 struct ref_store;
9 struct strbuf;
10 struct string_list;
11 struct string_list_item;
12 struct worktree;
14 enum ref_storage_format ref_storage_format_by_name(const char *name);
15 const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);
18 * Resolve a reference, recursively following symbolic refererences.
20 * Return the name of the non-symbolic reference that ultimately pointed
21 * at the resolved object name. The return value, if not NULL, is a
22 * pointer into either a static buffer or the input ref.
24 * If oid is non-NULL, store the referred-to object's name in it.
26 * If the reference cannot be resolved to an object, the behavior
27 * depends on the RESOLVE_REF_READING flag:
29 * - If RESOLVE_REF_READING is set, return NULL.
31 * - If RESOLVE_REF_READING is not set, clear oid and return the name of
32 * the last reference name in the chain, which will either be a non-symbolic
33 * reference or an undefined reference. If this is a prelude to
34 * "writing" to the ref, the return value is the name of the ref
35 * that will actually be created or changed.
37 * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one
38 * level of symbolic reference. The value stored in oid for a symbolic
39 * reference will always be null_oid in this case, and the return
40 * value is the reference that the symref refers to directly.
42 * If flags is non-NULL, set the value that it points to the
43 * combination of REF_ISPACKED (if the reference was found among the
44 * packed references), REF_ISSYMREF (if the initial reference was a
45 * symbolic reference), REF_BAD_NAME (if the reference name is ill
46 * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN
47 * (if the ref is malformed or has a bad name). See refs.h for more detail
48 * on each flag.
50 * If ref is not a properly-formatted, normalized reference, return
51 * NULL. If more than MAXDEPTH recursive symbolic lookups are needed,
52 * give up and return NULL.
54 * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their
55 * name is invalid according to git-check-ref-format(1). If the name
56 * is bad then the value stored in oid will be null_oid and the two
57 * flags REF_ISBROKEN and REF_BAD_NAME will be set.
59 * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/
60 * directory and do not consist of all caps and underscores cannot be
61 * resolved. The function returns NULL for such ref names.
62 * Caps and underscores refers to the pseudorefs, such as HEAD,
63 * FETCH_HEAD and friends, that all live outside of the refs/ directory.
65 #define RESOLVE_REF_READING 0x01
66 #define RESOLVE_REF_NO_RECURSE 0x02
67 #define RESOLVE_REF_ALLOW_BAD_NAME 0x04
69 const char *refs_resolve_ref_unsafe(struct ref_store *refs,
70 const char *refname,
71 int resolve_flags,
72 struct object_id *oid,
73 int *flags);
75 char *refs_resolve_refdup(struct ref_store *refs,
76 const char *refname, int resolve_flags,
77 struct object_id *oid, int *flags);
79 int refs_read_ref_full(struct ref_store *refs, const char *refname,
80 int resolve_flags, struct object_id *oid, int *flags);
82 int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
84 int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
85 struct strbuf *referent);
88 * Return 0 if a reference named refname could be created without
89 * conflicting with the name of an existing reference. Otherwise,
90 * return a negative value and write an explanation to err. If extras
91 * is non-NULL, it is a list of additional refnames with which refname
92 * is not allowed to conflict. If skip is non-NULL, ignore potential
93 * conflicts with refs in skip (e.g., because they are scheduled for
94 * deletion in the same operation). Behavior is undefined if the same
95 * name is listed in both extras and skip.
97 * Two reference names conflict if one of them exactly matches the
98 * leading components of the other; e.g., "foo/bar" conflicts with
99 * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
100 * "foo/barbados".
102 * extras and skip must be sorted.
105 int refs_verify_refname_available(struct ref_store *refs,
106 const char *refname,
107 const struct string_list *extras,
108 const struct string_list *skip,
109 struct strbuf *err);
111 int refs_ref_exists(struct ref_store *refs, const char *refname);
113 int should_autocreate_reflog(const char *refname);
115 int is_branch(const char *refname);
117 #define REF_STORE_CREATE_ON_DISK_IS_WORKTREE (1 << 0)
119 int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err);
122 * Release all memory and resources associated with the ref store.
124 void ref_store_release(struct ref_store *ref_store);
127 * Remove the ref store from disk. This deletes all associated data.
129 int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err);
132 * Return the peeled value of the oid currently being iterated via
133 * for_each_ref(), etc. This is equivalent to calling:
135 * peel_object(r, oid, &peeled);
137 * with the "oid" value given to the each_ref_fn callback, except
138 * that some ref storage may be able to answer the query without
139 * actually loading the object in memory.
141 int peel_iterated_oid(struct repository *r,
142 const struct object_id *base, struct object_id *peeled);
145 * Resolve refname in the nested "gitlink" repository in the specified
146 * submodule (which must be non-NULL). If the resolution is
147 * successful, return 0 and set oid to the name of the object;
148 * otherwise, return a non-zero value.
150 int repo_resolve_gitlink_ref(struct repository *r,
151 const char *submodule, const char *refname,
152 struct object_id *oid);
155 * Return true iff abbrev_name is a possible abbreviation for
156 * full_name according to the rules defined by ref_rev_parse_rules in
157 * refs.c.
159 int refname_match(const char *abbrev_name, const char *full_name);
162 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
163 * the results to 'prefixes'
165 struct strvec;
166 void expand_ref_prefix(struct strvec *prefixes, const char *prefix);
168 int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
169 int repo_dwim_ref(struct repository *r, const char *str, int len,
170 struct object_id *oid, char **ref, int nonfatal_dangling_mark);
171 int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
174 * Retrieves the default branch name for newly-initialized repositories.
176 * The return value is an allocated string.
178 char *repo_default_branch_name(struct repository *r, int quiet);
181 * A ref_transaction represents a collection of reference updates that
182 * should succeed or fail together.
184 * Calling sequence
185 * ----------------
187 * - Allocate and initialize a `struct ref_transaction` by calling
188 * `ref_transaction_begin()`.
190 * - Specify the intended ref updates by calling one or more of the
191 * following functions:
192 * - `ref_transaction_update()`
193 * - `ref_transaction_create()`
194 * - `ref_transaction_delete()`
195 * - `ref_transaction_verify()`
197 * - Then either:
199 * - Optionally call `ref_transaction_prepare()` to prepare the
200 * transaction. This locks all references, checks preconditions,
201 * etc. but doesn't finalize anything. If this step fails, the
202 * transaction has been closed and can only be freed. If this step
203 * succeeds, then `ref_transaction_commit()` is almost certain to
204 * succeed. However, you can still call `ref_transaction_abort()`
205 * if you decide not to commit the transaction after all.
207 * - Call `ref_transaction_commit()` to execute the transaction,
208 * make the changes permanent, and release all locks. If you
209 * haven't already called `ref_transaction_prepare()`, then
210 * `ref_transaction_commit()` calls it for you.
212 * Or
214 * - Call `initial_ref_transaction_commit()` if the ref database is
215 * known to be empty and have no other writers (e.g. during
216 * clone). This is likely to be much faster than
217 * `ref_transaction_commit()`. `ref_transaction_prepare()` should
218 * *not* be called before `initial_ref_transaction_commit()`.
220 * - Then finally, call `ref_transaction_free()` to free the
221 * `ref_transaction` data structure.
223 * At any time before calling `ref_transaction_commit()`, you can call
224 * `ref_transaction_abort()` to abort the transaction, rollback any
225 * locks, and free any associated resources (including the
226 * `ref_transaction` data structure).
228 * Putting it all together, a complete reference update looks like
230 * struct ref_transaction *transaction;
231 * struct strbuf err = STRBUF_INIT;
232 * int ret = 0;
234 * transaction = ref_store_transaction_begin(refs, &err);
235 * if (!transaction ||
236 * ref_transaction_update(...) ||
237 * ref_transaction_create(...) ||
238 * ...etc... ||
239 * ref_transaction_commit(transaction, &err)) {
240 * error("%s", err.buf);
241 * ret = -1;
243 * ref_transaction_free(transaction);
244 * strbuf_release(&err);
245 * return ret;
247 * Error handling
248 * --------------
250 * On error, transaction functions append a message about what
251 * went wrong to the 'err' argument. The message mentions what
252 * ref was being updated (if any) when the error occurred so it
253 * can be passed to 'die' or 'error' as-is.
255 * The message is appended to err without first clearing err.
256 * err will not be '\n' terminated.
258 * Caveats
259 * -------
261 * Note that no locks are taken, and no refs are read, until
262 * `ref_transaction_prepare()` or `ref_transaction_commit()` is
263 * called. So, for example, `ref_transaction_verify()` won't report a
264 * verification failure until the commit is attempted.
266 struct ref_transaction;
269 * Bit values set in the flags argument passed to each_ref_fn() and
270 * stored in ref_iterator::flags. Other bits are for internal use
271 * only:
274 /* Reference is a symbolic reference. */
275 #define REF_ISSYMREF 0x01
277 /* Reference is a packed reference. */
278 #define REF_ISPACKED 0x02
281 * Reference cannot be resolved to an object name: dangling symbolic
282 * reference (directly or indirectly), corrupt reference file,
283 * reference exists but name is bad, or symbolic reference refers to
284 * ill-formatted reference name.
286 #define REF_ISBROKEN 0x04
289 * Reference name is not well formed.
291 * See git-check-ref-format(1) for the definition of well formed ref names.
293 #define REF_BAD_NAME 0x08
296 * The signature for the callback function for the for_each_*()
297 * functions below. The memory pointed to by the refname and oid
298 * arguments is only guaranteed to be valid for the duration of a
299 * single callback invocation.
301 typedef int each_ref_fn(const char *refname,
302 const struct object_id *oid, int flags, void *cb_data);
305 * The following functions invoke the specified callback function for
306 * each reference indicated. If the function ever returns a nonzero
307 * value, stop the iteration and return that value. Please note that
308 * it is not safe to modify references while an iteration is in
309 * progress, unless the same callback function invocation that
310 * modifies the reference also returns a nonzero value to immediately
311 * stop the iteration. Returned references are sorted.
313 int refs_head_ref(struct ref_store *refs,
314 each_ref_fn fn, void *cb_data);
315 int refs_for_each_ref(struct ref_store *refs,
316 each_ref_fn fn, void *cb_data);
317 int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
318 each_ref_fn fn, void *cb_data);
319 int refs_for_each_tag_ref(struct ref_store *refs,
320 each_ref_fn fn, void *cb_data);
321 int refs_for_each_branch_ref(struct ref_store *refs,
322 each_ref_fn fn, void *cb_data);
323 int refs_for_each_remote_ref(struct ref_store *refs,
324 each_ref_fn fn, void *cb_data);
325 int refs_for_each_replace_ref(struct ref_store *refs,
326 each_ref_fn fn, void *cb_data);
329 * references matching any pattern in "exclude_patterns" are omitted from the
330 * result set on a best-effort basis.
332 int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
333 const char **exclude_patterns,
334 each_ref_fn fn, void *cb_data);
337 * iterate all refs in "patterns" by partitioning patterns into disjoint sets
338 * and iterating the longest-common prefix of each set.
340 * references matching any pattern in "exclude_patterns" are omitted from the
341 * result set on a best-effort basis.
343 * callers should be prepared to ignore references that they did not ask for.
345 int refs_for_each_fullref_in_prefixes(struct ref_store *refs,
346 const char *namespace,
347 const char **patterns,
348 const char **exclude_patterns,
349 each_ref_fn fn, void *cb_data);
351 /* iterates all refs that match the specified glob pattern. */
352 int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
353 const char *pattern, void *cb_data);
355 int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn,
356 const char *pattern, const char *prefix, void *cb_data);
358 int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data);
361 * references matching any pattern in "exclude_patterns" are omitted from the
362 * result set on a best-effort basis.
364 int refs_for_each_namespaced_ref(struct ref_store *refs,
365 const char **exclude_patterns,
366 each_ref_fn fn, void *cb_data);
368 /* can be used to learn about broken ref and symref */
369 int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data);
372 * Iterates over all refs including root refs, i.e. pseudorefs and HEAD.
374 int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn,
375 void *cb_data);
378 * Normalizes partial refs to their fully qualified form.
379 * Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'.
380 * <prefix> will default to 'refs/' if NULL.
382 * item.string will be set to the result.
383 * item.util will be set to NULL if <pattern> contains glob characters, or
384 * non-NULL if it doesn't.
386 void normalize_glob_ref(struct string_list_item *item, const char *prefix,
387 const char *pattern);
389 static inline const char *has_glob_specials(const char *pattern)
391 return strpbrk(pattern, "?*[");
394 void refs_warn_dangling_symref(struct ref_store *refs, FILE *fp,
395 const char *msg_fmt, const char *refname);
396 void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
397 const char *msg_fmt, const struct string_list *refnames);
400 * Flags for controlling behaviour of pack_refs()
401 * PACK_REFS_PRUNE: Prune loose refs after packing
402 * PACK_REFS_AUTO: Pack refs on a best effort basis. The heuristics and end
403 * result are decided by the ref backend. Backends may ignore
404 * this flag and fall back to a normal repack.
406 #define PACK_REFS_PRUNE (1 << 0)
407 #define PACK_REFS_AUTO (1 << 1)
409 struct pack_refs_opts {
410 unsigned int flags;
411 struct ref_exclusions *exclusions;
412 struct string_list *includes;
416 * Write a packed-refs file for the current repository.
417 * flags: Combination of the above PACK_REFS_* flags.
419 int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts);
422 * Setup reflog before using. Fill in err and return -1 on failure.
424 int refs_create_reflog(struct ref_store *refs, const char *refname,
425 struct strbuf *err);
428 * Reads log for the value of ref during at_time (in which case "cnt" should be
429 * negative) or the reflog "cnt" entries from the top (in which case "at_time"
430 * should be 0).
432 * If we found the reflog entry in question, returns 0 (and details of the
433 * entry can be found in the out-parameters).
435 * If we ran out of reflog entries, the out-parameters are filled with the
436 * details of the oldest entry we did find, and the function returns 1. Note
437 * that there is one important special case here! If the reflog was empty
438 * and the caller asked for the 0-th cnt, we will return "1" but leave the
439 * "oid" field untouched.
441 int read_ref_at(struct ref_store *refs,
442 const char *refname, unsigned int flags,
443 timestamp_t at_time, int cnt,
444 struct object_id *oid, char **msg,
445 timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
447 /** Check if a particular reflog exists */
448 int refs_reflog_exists(struct ref_store *refs, const char *refname);
451 * Delete the specified reference. If old_oid is non-NULL, then
452 * verify that the current value of the reference is old_oid before
453 * deleting it. If old_oid is NULL, delete the reference if it
454 * exists, regardless of its old value. It is an error for old_oid to
455 * be null_oid. msg and flags are passed through to
456 * ref_transaction_delete().
458 int refs_delete_ref(struct ref_store *refs, const char *msg,
459 const char *refname,
460 const struct object_id *old_oid,
461 unsigned int flags);
464 * Delete the specified references. If there are any problems, emit
465 * errors but attempt to keep going (i.e., the deletes are not done in
466 * an all-or-nothing transaction). msg and flags are passed through to
467 * ref_transaction_delete().
469 int refs_delete_refs(struct ref_store *refs, const char *msg,
470 struct string_list *refnames, unsigned int flags);
472 /** Delete a reflog */
473 int refs_delete_reflog(struct ref_store *refs, const char *refname);
476 * Callback to process a reflog entry found by the iteration functions (see
477 * below).
479 * The committer parameter is a single string, in the form
480 * "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" (without double quotes).
482 * The timestamp parameter gives the time when entry was created as the number
483 * of seconds since the UNIX epoch.
485 * The tz parameter gives the timezone offset for the user who created
486 * the reflog entry, and its value gives a positive or negative offset
487 * from UTC. Its absolute value is formed by multiplying the hour
488 * part by 100 and adding the minute part. For example, 1 hour ahead
489 * of UTC, CET == "+0100", is represented as positive one hundred (not
490 * postiive sixty).
492 * The msg parameter is a single complete line; a reflog message given
493 * to refs_delete_ref, refs_update_ref, etc. is returned to the
494 * callback normalized---each run of whitespaces are squashed into a
495 * single whitespace, trailing whitespace, if exists, is trimmed, and
496 * then a single LF is added at the end.
498 * The cb_data is a caller-supplied pointer given to the iterator
499 * functions.
501 typedef int each_reflog_ent_fn(
502 struct object_id *old_oid, struct object_id *new_oid,
503 const char *committer, timestamp_t timestamp,
504 int tz, const char *msg, void *cb_data);
506 /* Iterate over reflog entries in the log for `refname`. */
508 /* oldest entry first */
509 int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
510 each_reflog_ent_fn fn, void *cb_data);
512 /* youngest entry first */
513 int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
514 const char *refname,
515 each_reflog_ent_fn fn,
516 void *cb_data);
519 * The signature for the callback function for the refs_for_each_reflog()
520 * functions below. The memory pointed to by the refname argument is only
521 * guaranteed to be valid for the duration of a single callback invocation.
523 typedef int each_reflog_fn(const char *refname, void *cb_data);
526 * Calls the specified function for each reflog file until it returns nonzero,
527 * and returns the value. Reflog file order is unspecified.
529 int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data);
531 #define REFNAME_ALLOW_ONELEVEL 1
532 #define REFNAME_REFSPEC_PATTERN 2
535 * Return 0 iff refname has the correct format for a refname according
536 * to the rules described in Documentation/git-check-ref-format.txt.
537 * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level
538 * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then
539 * allow a single "*" wildcard character in the refspec. No leading or
540 * repeated slashes are accepted.
542 int check_refname_format(const char *refname, int flags);
545 * Apply the rules from check_refname_format, but mutate the result until it
546 * is acceptable, and place the result in "out".
548 void sanitize_refname_component(const char *refname, struct strbuf *out);
550 const char *prettify_refname(const char *refname);
552 char *refs_shorten_unambiguous_ref(struct ref_store *refs,
553 const char *refname, int strict);
555 /** rename ref, return 0 on success **/
556 int refs_rename_ref(struct ref_store *refs, const char *oldref,
557 const char *newref, const char *logmsg);
559 /** copy ref, return 0 on success **/
560 int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
561 const char *newref, const char *logmsg);
563 int refs_update_symref(struct ref_store *refs, const char *refname,
564 const char *target, const char *logmsg);
566 enum action_on_err {
567 UPDATE_REFS_MSG_ON_ERR,
568 UPDATE_REFS_DIE_ON_ERR,
569 UPDATE_REFS_QUIET_ON_ERR
573 * Begin a reference transaction. The reference transaction must
574 * be freed by calling ref_transaction_free().
576 struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
577 struct strbuf *err);
580 * Reference transaction updates
582 * The following four functions add a reference check or update to a
583 * ref_transaction. They have some common similar parameters:
585 * transaction -- a pointer to an open ref_transaction, obtained
586 * from ref_transaction_begin().
588 * refname -- the name of the reference to be affected.
590 * new_oid -- the object ID that should be set to be the new value
591 * of the reference. Some functions allow this parameter to be
592 * NULL, meaning that the reference is not changed, or
593 * null_oid, meaning that the reference should be deleted. A
594 * copy of this value is made in the transaction.
596 * old_oid -- the object ID that the reference must have before
597 * the update. Some functions allow this parameter to be NULL,
598 * meaning that the old value of the reference is not checked,
599 * or null_oid, meaning that the reference must not exist
600 * before the update. A copy of this value is made in the
601 * transaction.
603 * new_target -- the target reference that the reference will be
604 * updated to point to. If the reference is a regular reference,
605 * it will be converted to a symbolic reference. Cannot be set
606 * together with `new_oid`. A copy of this value is made in the
607 * transaction.
609 * old_target -- the reference that the reference must be pointing to.
610 * Canont be set together with `old_oid`. A copy of this value is
611 * made in the transaction.
613 * flags -- flags affecting the update, passed to
614 * update_ref_lock(). Possible flags: REF_NO_DEREF,
615 * REF_FORCE_CREATE_REFLOG. See those constants for more
616 * information.
618 * msg -- a message describing the change (for the reflog).
620 * err -- a strbuf for receiving a description of any error that
621 * might have occurred.
623 * The functions make internal copies of refname and msg, so the
624 * caller retains ownership of these parameters.
626 * The functions return 0 on success and non-zero on failure. A
627 * failure means that the transaction as a whole has failed and needs
628 * to be rolled back.
632 * The following flags can be passed to ref_transaction_update() etc.
633 * Internally, they are stored in `ref_update::flags`, along with some
634 * internal flags.
638 * Act on the ref directly; i.e., without dereferencing symbolic refs.
639 * If this flag is not specified, then symbolic references are
640 * dereferenced and the update is applied to the referent.
642 #define REF_NO_DEREF (1 << 0)
645 * Force the creation of a reflog for this reference, even if it
646 * didn't previously have a reflog.
648 #define REF_FORCE_CREATE_REFLOG (1 << 1)
651 * Blindly write an object_id. This is useful for testing data corruption
652 * scenarios.
654 #define REF_SKIP_OID_VERIFICATION (1 << 10)
657 * Skip verifying refname. This is useful for testing data corruption scenarios.
659 #define REF_SKIP_REFNAME_VERIFICATION (1 << 11)
662 * Skip creation of a reflog entry, even if it would have otherwise been
663 * created.
665 #define REF_SKIP_CREATE_REFLOG (1 << 12)
668 * Bitmask of all of the flags that are allowed to be passed in to
669 * ref_transaction_update() and friends:
671 #define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
672 (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION | \
673 REF_SKIP_REFNAME_VERIFICATION | REF_SKIP_CREATE_REFLOG)
676 * Add a reference update to transaction. `new_oid` is the value that
677 * the reference should have after the update, or `null_oid` if it
678 * should be deleted. If `new_oid` is NULL, then the reference is not
679 * changed at all. `old_oid` is the value that the reference must have
680 * before the update, or `null_oid` if it must not have existed
681 * beforehand. The old value is checked after the lock is taken to
682 * prevent races. If the old value doesn't agree with old_oid, the
683 * whole transaction fails. If old_oid is NULL, then the previous
684 * value is not checked. If `old_target` is not NULL, treat the reference
685 * as a symbolic ref and validate that its target before the update is
686 * `old_target`. If the `new_target` is not NULL, then the reference
687 * will be updated to a symbolic ref which targets `new_target`.
688 * Together, these allow us to update between regular refs and symrefs.
690 * See the above comment "Reference transaction updates" for more
691 * information.
693 int ref_transaction_update(struct ref_transaction *transaction,
694 const char *refname,
695 const struct object_id *new_oid,
696 const struct object_id *old_oid,
697 const char *new_target,
698 const char *old_target,
699 unsigned int flags, const char *msg,
700 struct strbuf *err);
703 * Add a reference creation to transaction. new_oid is the value that
704 * the reference should have after the update; it must not be
705 * null_oid. It is verified that the reference does not exist
706 * already.
708 * See the above comment "Reference transaction updates" for more
709 * information.
711 int ref_transaction_create(struct ref_transaction *transaction,
712 const char *refname,
713 const struct object_id *new_oid,
714 const char *new_target,
715 unsigned int flags, const char *msg,
716 struct strbuf *err);
719 * Add a reference deletion to transaction. If old_oid is non-NULL,
720 * then it holds the value that the reference should have had before
721 * the update (which must not be null_oid).
723 * See the above comment "Reference transaction updates" for more
724 * information.
726 int ref_transaction_delete(struct ref_transaction *transaction,
727 const char *refname,
728 const struct object_id *old_oid,
729 const char *old_target,
730 unsigned int flags,
731 const char *msg,
732 struct strbuf *err);
735 * Verify, within a transaction, that refname has the value old_oid,
736 * or, if old_oid is null_oid, then verify that the reference
737 * doesn't exist. old_oid must be non-NULL.
739 * See the above comment "Reference transaction updates" for more
740 * information.
742 int ref_transaction_verify(struct ref_transaction *transaction,
743 const char *refname,
744 const struct object_id *old_oid,
745 const char *old_target,
746 unsigned int flags,
747 struct strbuf *err);
749 /* Naming conflict (for example, the ref names A and A/B conflict). */
750 #define TRANSACTION_NAME_CONFLICT -1
751 /* All other errors. */
752 #define TRANSACTION_GENERIC_ERROR -2
755 * Perform the preparatory stages of committing `transaction`. Acquire
756 * any needed locks, check preconditions, etc.; basically, do as much
757 * as possible to ensure that the transaction will be able to go
758 * through, stopping just short of making any irrevocable or
759 * user-visible changes. The updates that this function prepares can
760 * be finished up by calling `ref_transaction_commit()` or rolled back
761 * by calling `ref_transaction_abort()`.
763 * On success, return 0 and leave the transaction in "prepared" state.
764 * On failure, abort the transaction, write an error message to `err`,
765 * and return one of the `TRANSACTION_*` constants.
767 * Callers who don't need such fine-grained control over committing
768 * reference transactions should just call `ref_transaction_commit()`.
770 int ref_transaction_prepare(struct ref_transaction *transaction,
771 struct strbuf *err);
774 * Commit all of the changes that have been queued in transaction, as
775 * atomically as possible. On success, return 0 and leave the
776 * transaction in "closed" state. On failure, roll back the
777 * transaction, write an error message to `err`, and return one of the
778 * `TRANSACTION_*` constants
780 int ref_transaction_commit(struct ref_transaction *transaction,
781 struct strbuf *err);
784 * Abort `transaction`, which has been begun and possibly prepared,
785 * but not yet committed.
787 int ref_transaction_abort(struct ref_transaction *transaction,
788 struct strbuf *err);
791 * Like ref_transaction_commit(), but optimized for creating
792 * references when originally initializing a repository (e.g., by "git
793 * clone"). It writes the new references directly to packed-refs
794 * without locking the individual references.
796 * It is a bug to call this function when there might be other
797 * processes accessing the repository or if there are existing
798 * references that might conflict with the ones being created. All
799 * old_oid values must either be absent or null_oid.
801 int initial_ref_transaction_commit(struct ref_transaction *transaction,
802 struct strbuf *err);
805 * Execute the given callback function for each of the reference updates which
806 * have been queued in the given transaction. `old_oid` and `new_oid` may be
807 * `NULL` pointers depending on whether the update has these object IDs set or
808 * not.
810 typedef void ref_transaction_for_each_queued_update_fn(const char *refname,
811 const struct object_id *old_oid,
812 const struct object_id *new_oid,
813 void *cb_data);
814 void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
815 ref_transaction_for_each_queued_update_fn cb,
816 void *cb_data);
819 * Free `*transaction` and all associated data.
821 void ref_transaction_free(struct ref_transaction *transaction);
824 * Lock, update, and unlock a single reference. This function
825 * basically does a transaction containing a single call to
826 * ref_transaction_update(). The parameters to this function have the
827 * same meaning as the corresponding parameters to
828 * ref_transaction_update(). Handle errors as requested by the `onerr`
829 * argument.
831 int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname,
832 const struct object_id *new_oid, const struct object_id *old_oid,
833 unsigned int flags, enum action_on_err onerr);
835 int parse_hide_refs_config(const char *var, const char *value, const char *,
836 struct strvec *);
839 * Check whether a ref is hidden. If no namespace is set, both the first and
840 * the second parameter point to the full ref name. If a namespace is set and
841 * the ref is inside that namespace, the first parameter is a pointer to the
842 * name of the ref with the namespace prefix removed. If a namespace is set and
843 * the ref is outside that namespace, the first parameter is NULL. The second
844 * parameter always points to the full ref name.
846 int ref_is_hidden(const char *, const char *, const struct strvec *);
849 * Returns an array of patterns to use as excluded_patterns, if none of the
850 * hidden references use the token '!' or '^'.
852 const char **hidden_refs_to_excludes(const struct strvec *hide_refs);
854 /* Is this a per-worktree ref living in the refs/ namespace? */
855 int is_per_worktree_ref(const char *refname);
857 /* Describes how a refname relates to worktrees */
858 enum ref_worktree_type {
859 REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or
860 refs/bisect/SOMETHING */
861 REF_WORKTREE_MAIN, /* explicitly in main worktree, eg.
862 main-worktree/HEAD */
863 REF_WORKTREE_OTHER, /* explicitly in named worktree, eg.
864 worktrees/bla/HEAD */
865 REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */
869 * Parse a `maybe_worktree_ref` as a ref that possibly refers to a worktree ref
870 * (ie. either REFNAME, main-worktree/REFNAME or worktree/WORKTREE/REFNAME). It
871 * returns what kind of ref was found, and in case of REF_WORKTREE_OTHER, the
872 * worktree name is returned in `worktree_name` (pointing into
873 * `maybe_worktree_ref`) and `worktree_name_length`. The bare refname (the
874 * refname stripped of prefixes) is returned in `bare_refname`. The
875 * `worktree_name`, `worktree_name_length` and `bare_refname` arguments may be
876 * NULL.
878 enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
879 const char **worktree_name,
880 int *worktree_name_length,
881 const char **bare_refname);
883 enum expire_reflog_flags {
884 EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
885 EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
886 EXPIRE_REFLOGS_REWRITE = 1 << 2,
890 * The following interface is used for reflog expiration. The caller
891 * calls refs_reflog_expire(), supplying it with three callback functions,
892 * of the following types. The callback functions define the
893 * expiration policy that is desired.
895 * reflog_expiry_prepare_fn -- Called once after the reference is
896 * locked. Called with the OID of the locked reference.
898 * reflog_expiry_should_prune_fn -- Called once for each entry in the
899 * existing reflog. It should return true iff that entry should be
900 * pruned.
902 * reflog_expiry_cleanup_fn -- Called once before the reference is
903 * unlocked again.
905 typedef void reflog_expiry_prepare_fn(const char *refname,
906 const struct object_id *oid,
907 void *cb_data);
908 typedef int reflog_expiry_should_prune_fn(struct object_id *ooid,
909 struct object_id *noid,
910 const char *email,
911 timestamp_t timestamp, int tz,
912 const char *message, void *cb_data);
913 typedef void reflog_expiry_cleanup_fn(void *cb_data);
916 * Expire reflog entries for the specified reference.
917 * flags is a combination of the constants in
918 * enum expire_reflog_flags. The three function pointers are described
919 * above. On success, return zero.
921 int refs_reflog_expire(struct ref_store *refs,
922 const char *refname,
923 unsigned int flags,
924 reflog_expiry_prepare_fn prepare_fn,
925 reflog_expiry_should_prune_fn should_prune_fn,
926 reflog_expiry_cleanup_fn cleanup_fn,
927 void *policy_cb_data);
929 struct ref_store *get_main_ref_store(struct repository *r);
932 * Submodules
933 * ----------
935 * If you want to iterate the refs of a submodule you first need to add the
936 * submodules object database. You can do this by a code-snippet like
937 * this:
939 * const char *path = "path/to/submodule"
940 * if (add_submodule_odb(path))
941 * die("Error submodule '%s' not populated.", path);
943 * `add_submodule_odb()` will return zero on success. If you
944 * do not do this you will get an error for each ref that it does not point
945 * to a valid object.
947 * Note: As a side-effect of this you cannot safely assume that all
948 * objects you lookup are available in superproject. All submodule objects
949 * will be available the same way as the superprojects objects.
951 * Example:
952 * --------
954 * ----
955 * static int handle_remote_ref(const char *refname,
956 * const unsigned char *sha1, int flags, void *cb_data)
958 * struct strbuf *output = cb_data;
959 * strbuf_addf(output, "%s\n", refname);
960 * return 0;
966 * Return the ref_store instance for the specified submodule. For the
967 * main repository, use submodule==NULL; such a call cannot fail. For
968 * a submodule, the submodule must exist and be a nonbare repository,
969 * otherwise return NULL. If the requested reference store has not yet
970 * been initialized, initialize it first.
972 * For backwards compatibility, submodule=="" is treated the same as
973 * submodule==NULL.
975 struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
976 const char *submodule);
977 struct ref_store *get_worktree_ref_store(const struct worktree *wt);
980 * Some of the names specified by refs have special meaning to Git.
981 * Organize these namespaces in a comon 'ref_namespace' array for
982 * reference from multiple places in the codebase.
985 struct ref_namespace_info {
986 const char *ref;
987 enum decoration_type decoration;
990 * If 'exact' is true, then we must match the 'ref' exactly.
991 * Otherwise, use a prefix match.
993 * 'ref_updated' is for internal use. It represents whether the
994 * 'ref' value was replaced from its original literal version.
996 unsigned exact:1,
997 ref_updated:1;
1000 enum ref_namespace {
1001 NAMESPACE_HEAD,
1002 NAMESPACE_BRANCHES,
1003 NAMESPACE_TAGS,
1004 NAMESPACE_REMOTE_REFS,
1005 NAMESPACE_STASH,
1006 NAMESPACE_REPLACE,
1007 NAMESPACE_NOTES,
1008 NAMESPACE_PREFETCH,
1009 NAMESPACE_REWRITTEN,
1011 /* Must be last */
1012 NAMESPACE__COUNT
1015 /* See refs.c for the contents of this array. */
1016 extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT];
1019 * Some ref namespaces can be modified by config values or environment
1020 * variables. Modify a namespace as specified by its ref_namespace key.
1022 void update_ref_namespace(enum ref_namespace namespace, char *ref);
1025 * Check whether the provided name names a root reference. This function only
1026 * performs a syntactic check.
1028 * A root ref is a reference that lives in the root of the reference hierarchy.
1029 * These references must conform to special syntax:
1031 * - Their name must be all-uppercase or underscores ("_").
1033 * - Their name must end with "_HEAD". As a special rule, "HEAD" is a root
1034 * ref, as well.
1036 * - Their name may not contain a slash.
1038 * There is a special set of irregular root refs that exist due to historic
1039 * reasons, only. This list shall not be expanded in the future:
1041 * - AUTO_MERGE
1043 * - BISECT_EXPECTED_REV
1045 * - NOTES_MERGE_PARTIAL
1047 * - NOTES_MERGE_REF
1049 * - MERGE_AUTOSTASH
1051 int is_root_ref(const char *refname);
1054 * Pseudorefs are refs that have different semantics compared to
1055 * "normal" refs. These refs can thus not be stored in the ref backend,
1056 * but must always be accessed via the filesystem. The following refs
1057 * are pseudorefs:
1059 * - FETCH_HEAD may contain multiple object IDs, and each one of them
1060 * carries additional metadata like where it came from.
1062 * - MERGE_HEAD may contain multiple object IDs when merging multiple
1063 * heads.
1065 * Reading, writing or deleting references must consistently go either
1066 * through the filesystem (pseudorefs) or through the reference
1067 * backend (normal ones).
1069 int is_pseudo_ref(const char *refname);
1072 * The following flags can be passed to `repo_migrate_ref_storage_format()`:
1074 * - REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN: perform a dry-run migration
1075 * without touching the main repository. The result will be written into a
1076 * temporary ref storage directory.
1078 #define REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN (1 << 0)
1081 * Migrate the ref storage format used by the repository to the
1082 * specified one.
1084 int repo_migrate_ref_storage_format(struct repository *repo,
1085 enum ref_storage_format format,
1086 unsigned int flags,
1087 struct strbuf *err);
1090 * The following functions have been removed in Git v2.46 in favor of functions
1091 * that receive a `ref_store` as parameter. The intent of this section is
1092 * merely to help patch authors of in-flight series to have a reference what
1093 * they should be migrating to. The section will be removed in Git v2.47.
1095 #if 0
1096 static char *resolve_refdup(const char *refname, int resolve_flags,
1097 struct object_id *oid, int *flags)
1099 return refs_resolve_refdup(get_main_ref_store(the_repository),
1100 refname, resolve_flags,
1101 oid, flags);
1104 static int read_ref_full(const char *refname, int resolve_flags,
1105 struct object_id *oid, int *flags)
1107 return refs_read_ref_full(get_main_ref_store(the_repository), refname,
1108 resolve_flags, oid, flags);
1111 static int read_ref(const char *refname, struct object_id *oid)
1113 return refs_read_ref(get_main_ref_store(the_repository), refname, oid);
1116 static int ref_exists(const char *refname)
1118 return refs_ref_exists(get_main_ref_store(the_repository), refname);
1121 static int for_each_tag_ref(each_ref_fn fn, void *cb_data)
1123 return refs_for_each_tag_ref(get_main_ref_store(the_repository), fn, cb_data);
1126 static int for_each_branch_ref(each_ref_fn fn, void *cb_data)
1128 return refs_for_each_branch_ref(get_main_ref_store(the_repository), fn, cb_data);
1131 static int for_each_remote_ref(each_ref_fn fn, void *cb_data)
1133 return refs_for_each_remote_ref(get_main_ref_store(the_repository), fn, cb_data);
1136 static int head_ref_namespaced(each_ref_fn fn, void *cb_data)
1138 return refs_head_ref_namespaced(get_main_ref_store(the_repository),
1139 fn, cb_data);
1142 static int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
1143 const char *prefix, void *cb_data)
1145 return refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
1146 fn, pattern, prefix, cb_data);
1149 static int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
1151 return refs_for_each_glob_ref(get_main_ref_store(the_repository),
1152 fn, pattern, cb_data);
1155 static int delete_ref(const char *msg, const char *refname,
1156 const struct object_id *old_oid, unsigned int flags)
1158 return refs_delete_ref(get_main_ref_store(the_repository), msg, refname,
1159 old_oid, flags);
1162 static struct ref_transaction *ref_transaction_begin(struct strbuf *err)
1164 return ref_store_transaction_begin(get_main_ref_store(the_repository), err);
1167 static int update_ref(const char *msg, const char *refname,
1168 const struct object_id *new_oid,
1169 const struct object_id *old_oid,
1170 unsigned int flags, enum action_on_err onerr)
1172 return refs_update_ref(get_main_ref_store(the_repository), msg, refname, new_oid,
1173 old_oid, flags, onerr);
1176 static char *shorten_unambiguous_ref(const char *refname, int strict)
1178 return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
1179 refname, strict);
1182 static int head_ref(each_ref_fn fn, void *cb_data)
1184 return refs_head_ref(get_main_ref_store(the_repository), fn, cb_data);
1187 static int for_each_ref(each_ref_fn fn, void *cb_data)
1189 return refs_for_each_ref(get_main_ref_store(the_repository), fn, cb_data);
1192 static int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1194 return refs_for_each_ref_in(get_main_ref_store(the_repository), prefix, fn, cb_data);
1197 static int for_each_fullref_in(const char *prefix,
1198 const char **exclude_patterns,
1199 each_ref_fn fn, void *cb_data)
1201 return refs_for_each_fullref_in(get_main_ref_store(the_repository),
1202 prefix, exclude_patterns, fn, cb_data);
1205 static int for_each_namespaced_ref(const char **exclude_patterns,
1206 each_ref_fn fn, void *cb_data)
1208 return refs_for_each_namespaced_ref(get_main_ref_store(the_repository),
1209 exclude_patterns, fn, cb_data);
1212 static int for_each_rawref(each_ref_fn fn, void *cb_data)
1214 return refs_for_each_rawref(get_main_ref_store(the_repository), fn, cb_data);
1217 static const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1218 struct object_id *oid, int *flags)
1220 return refs_resolve_ref_unsafe(get_main_ref_store(the_repository), refname,
1221 resolve_flags, oid, flags);
1224 static int create_symref(const char *ref_target, const char *refs_heads_master,
1225 const char *logmsg)
1227 return refs_create_symref(get_main_ref_store(the_repository), ref_target,
1228 refs_heads_master, logmsg);
1231 static int for_each_reflog(each_reflog_fn fn, void *cb_data)
1233 return refs_for_each_reflog(get_main_ref_store(the_repository), fn, cb_data);
1236 static int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,
1237 void *cb_data)
1239 return refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository),
1240 refname, fn, cb_data);
1243 static int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn,
1244 void *cb_data)
1246 return refs_for_each_reflog_ent(get_main_ref_store(the_repository), refname,
1247 fn, cb_data);
1250 static int reflog_exists(const char *refname)
1252 return refs_reflog_exists(get_main_ref_store(the_repository), refname);
1255 static int safe_create_reflog(const char *refname, struct strbuf *err)
1257 return refs_create_reflog(get_main_ref_store(the_repository), refname,
1258 err);
1261 static int delete_reflog(const char *refname)
1263 return refs_delete_reflog(get_main_ref_store(the_repository), refname);
1266 static int reflog_expire(const char *refname,
1267 unsigned int flags,
1268 reflog_expiry_prepare_fn prepare_fn,
1269 reflog_expiry_should_prune_fn should_prune_fn,
1270 reflog_expiry_cleanup_fn cleanup_fn,
1271 void *policy_cb_data)
1273 return refs_reflog_expire(get_main_ref_store(the_repository),
1274 refname, flags,
1275 prepare_fn, should_prune_fn,
1276 cleanup_fn, policy_cb_data);
1279 static int delete_refs(const char *msg, struct string_list *refnames,
1280 unsigned int flags)
1282 return refs_delete_refs(get_main_ref_store(the_repository), msg, refnames, flags);
1285 static int rename_ref(const char *oldref, const char *newref, const char *logmsg)
1287 return refs_rename_ref(get_main_ref_store(the_repository), oldref, newref, logmsg);
1290 static int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg)
1292 return refs_copy_existing_ref(get_main_ref_store(the_repository), oldref, newref, logmsg);
1294 #endif
1296 #endif /* REFS_H */