Merge branch 'ds/line-log-asan-fix'
[git/gitster.git] / refs.h
blob108dfc93b3428db491916ad8a55daea649d83ffd
1 #ifndef REFS_H
2 #define REFS_H
4 #include "commit.h"
5 #include "repository.h"
6 #include "repo-settings.h"
8 struct fsck_options;
9 struct object_id;
10 struct ref_store;
11 struct strbuf;
12 struct string_list;
13 struct string_list_item;
14 struct worktree;
16 enum ref_storage_format ref_storage_format_by_name(const char *name);
17 const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);
20 * Resolve a reference, recursively following symbolic references.
22 * Return the name of the non-symbolic reference that ultimately pointed
23 * at the resolved object name. The return value, if not NULL, is a
24 * pointer into either a static buffer or the input ref.
26 * If oid is non-NULL, store the referred-to object's name in it.
28 * If the reference cannot be resolved to an object, the behavior
29 * depends on the RESOLVE_REF_READING flag:
31 * - If RESOLVE_REF_READING is set, return NULL.
33 * - If RESOLVE_REF_READING is not set, clear oid and return the name of
34 * the last reference name in the chain, which will either be a non-symbolic
35 * reference or an undefined reference. If this is a prelude to
36 * "writing" to the ref, the return value is the name of the ref
37 * that will actually be created or changed.
39 * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one
40 * level of symbolic reference. The value stored in oid for a symbolic
41 * reference will always be null_oid in this case, and the return
42 * value is the reference that the symref refers to directly.
44 * If flags is non-NULL, set the value that it points to the
45 * combination of REF_ISPACKED (if the reference was found among the
46 * packed references), REF_ISSYMREF (if the initial reference was a
47 * symbolic reference), REF_BAD_NAME (if the reference name is ill
48 * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN
49 * (if the ref is malformed or has a bad name). See refs.h for more detail
50 * on each flag.
52 * If ref is not a properly-formatted, normalized reference, return
53 * NULL. If more than MAXDEPTH recursive symbolic lookups are needed,
54 * give up and return NULL.
56 * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their
57 * name is invalid according to git-check-ref-format(1). If the name
58 * is bad then the value stored in oid will be null_oid and the two
59 * flags REF_ISBROKEN and REF_BAD_NAME will be set.
61 * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/
62 * directory and do not consist of all caps and underscores cannot be
63 * resolved. The function returns NULL for such ref names.
64 * Caps and underscores refers to the pseudorefs, such as HEAD,
65 * FETCH_HEAD and friends, that all live outside of the refs/ directory.
67 #define RESOLVE_REF_READING 0x01
68 #define RESOLVE_REF_NO_RECURSE 0x02
69 #define RESOLVE_REF_ALLOW_BAD_NAME 0x04
71 const char *refs_resolve_ref_unsafe(struct ref_store *refs,
72 const char *refname,
73 int resolve_flags,
74 struct object_id *oid,
75 int *flags);
77 char *refs_resolve_refdup(struct ref_store *refs,
78 const char *refname, int resolve_flags,
79 struct object_id *oid, int *flags);
81 int refs_read_ref_full(struct ref_store *refs, const char *refname,
82 int resolve_flags, struct object_id *oid, int *flags);
84 int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
86 int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
87 struct strbuf *referent);
90 * Return 0 if a reference named refname could be created without
91 * conflicting with the name of an existing reference. Otherwise,
92 * return a negative value and write an explanation to err. If extras
93 * is non-NULL, it is a list of additional refnames with which refname
94 * is not allowed to conflict. If skip is non-NULL, ignore potential
95 * conflicts with refs in skip (e.g., because they are scheduled for
96 * deletion in the same operation). Behavior is undefined if the same
97 * name is listed in both extras and skip.
99 * Two reference names conflict if one of them exactly matches the
100 * leading components of the other; e.g., "foo/bar" conflicts with
101 * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
102 * "foo/barbados".
104 * extras and skip must be sorted.
107 int refs_verify_refname_available(struct ref_store *refs,
108 const char *refname,
109 const struct string_list *extras,
110 const struct string_list *skip,
111 struct strbuf *err);
113 int refs_ref_exists(struct ref_store *refs, const char *refname);
115 int should_autocreate_reflog(enum log_refs_config log_all_ref_updates,
116 const char *refname);
118 int is_branch(const char *refname);
120 #define REF_STORE_CREATE_ON_DISK_IS_WORKTREE (1 << 0)
122 int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err);
125 * Release all memory and resources associated with the ref store.
127 void ref_store_release(struct ref_store *ref_store);
130 * Remove the ref store from disk. This deletes all associated data.
132 int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err);
135 * Return the peeled value of the oid currently being iterated via
136 * for_each_ref(), etc. This is equivalent to calling:
138 * peel_object(r, oid, &peeled);
140 * with the "oid" value given to the each_ref_fn callback, except
141 * that some ref storage may be able to answer the query without
142 * actually loading the object in memory.
144 int peel_iterated_oid(struct repository *r,
145 const struct object_id *base, struct object_id *peeled);
148 * Resolve refname in the nested "gitlink" repository in the specified
149 * submodule (which must be non-NULL). If the resolution is
150 * successful, return 0 and set oid to the name of the object;
151 * otherwise, return a non-zero value.
153 int repo_resolve_gitlink_ref(struct repository *r,
154 const char *submodule, const char *refname,
155 struct object_id *oid);
158 * Return true iff abbrev_name is a possible abbreviation for
159 * full_name according to the rules defined by ref_rev_parse_rules in
160 * refs.c.
162 int refname_match(const char *abbrev_name, const char *full_name);
165 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
166 * the results to 'prefixes'
168 struct strvec;
169 void expand_ref_prefix(struct strvec *prefixes, const char *prefix);
171 int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
172 int repo_dwim_ref(struct repository *r, const char *str, int len,
173 struct object_id *oid, char **ref, int nonfatal_dangling_mark);
174 int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
177 * Retrieves the default branch name for newly-initialized repositories.
179 * The return value is an allocated string.
181 char *repo_default_branch_name(struct repository *r, int quiet);
184 * A ref_transaction represents a collection of reference updates that
185 * should succeed or fail together.
187 * Calling sequence
188 * ----------------
190 * - Allocate and initialize a `struct ref_transaction` by calling
191 * `ref_transaction_begin()`.
193 * - Specify the intended ref updates by calling one or more of the
194 * following functions:
195 * - `ref_transaction_update()`
196 * - `ref_transaction_create()`
197 * - `ref_transaction_delete()`
198 * - `ref_transaction_verify()`
200 * - Then either:
202 * - Optionally call `ref_transaction_prepare()` to prepare the
203 * transaction. This locks all references, checks preconditions,
204 * etc. but doesn't finalize anything. If this step fails, the
205 * transaction has been closed and can only be freed. If this step
206 * succeeds, then `ref_transaction_commit()` is almost certain to
207 * succeed. However, you can still call `ref_transaction_abort()`
208 * if you decide not to commit the transaction after all.
210 * - Call `ref_transaction_commit()` to execute the transaction,
211 * make the changes permanent, and release all locks. If you
212 * haven't already called `ref_transaction_prepare()`, then
213 * `ref_transaction_commit()` calls it for you.
215 * Or
217 * - Call `initial_ref_transaction_commit()` if the ref database is
218 * known to be empty and have no other writers (e.g. during
219 * clone). This is likely to be much faster than
220 * `ref_transaction_commit()`. `ref_transaction_prepare()` should
221 * *not* be called before `initial_ref_transaction_commit()`.
223 * - Then finally, call `ref_transaction_free()` to free the
224 * `ref_transaction` data structure.
226 * At any time before calling `ref_transaction_commit()`, you can call
227 * `ref_transaction_abort()` to abort the transaction, rollback any
228 * locks, and free any associated resources (including the
229 * `ref_transaction` data structure).
231 * Putting it all together, a complete reference update looks like
233 * struct ref_transaction *transaction;
234 * struct strbuf err = STRBUF_INIT;
235 * int ret = 0;
237 * transaction = ref_store_transaction_begin(refs, &err);
238 * if (!transaction ||
239 * ref_transaction_update(...) ||
240 * ref_transaction_create(...) ||
241 * ...etc... ||
242 * ref_transaction_commit(transaction, &err)) {
243 * error("%s", err.buf);
244 * ret = -1;
246 * ref_transaction_free(transaction);
247 * strbuf_release(&err);
248 * return ret;
250 * Error handling
251 * --------------
253 * On error, transaction functions append a message about what
254 * went wrong to the 'err' argument. The message mentions what
255 * ref was being updated (if any) when the error occurred so it
256 * can be passed to 'die' or 'error' as-is.
258 * The message is appended to err without first clearing err.
259 * err will not be '\n' terminated.
261 * Caveats
262 * -------
264 * Note that no locks are taken, and no refs are read, until
265 * `ref_transaction_prepare()` or `ref_transaction_commit()` is
266 * called. So, for example, `ref_transaction_verify()` won't report a
267 * verification failure until the commit is attempted.
269 struct ref_transaction;
272 * Bit values set in the flags argument passed to each_ref_fn() and
273 * stored in ref_iterator::flags. Other bits are for internal use
274 * only:
277 /* Reference is a symbolic reference. */
278 #define REF_ISSYMREF 0x01
280 /* Reference is a packed reference. */
281 #define REF_ISPACKED 0x02
284 * Reference cannot be resolved to an object name: dangling symbolic
285 * reference (directly or indirectly), corrupt reference file,
286 * reference exists but name is bad, or symbolic reference refers to
287 * ill-formatted reference name.
289 #define REF_ISBROKEN 0x04
292 * Reference name is not well formed.
294 * See git-check-ref-format(1) for the definition of well formed ref names.
296 #define REF_BAD_NAME 0x08
299 * The signature for the callback function for the for_each_*()
300 * functions below. The memory pointed to by the refname and oid
301 * arguments is only guaranteed to be valid for the duration of a
302 * single callback invocation.
304 typedef int each_ref_fn(const char *refname, const char *referent,
305 const struct object_id *oid, int flags, void *cb_data);
308 * The following functions invoke the specified callback function for
309 * each reference indicated. If the function ever returns a nonzero
310 * value, stop the iteration and return that value. Please note that
311 * it is not safe to modify references while an iteration is in
312 * progress, unless the same callback function invocation that
313 * modifies the reference also returns a nonzero value to immediately
314 * stop the iteration. Returned references are sorted.
316 int refs_head_ref(struct ref_store *refs,
317 each_ref_fn fn, void *cb_data);
318 int refs_for_each_ref(struct ref_store *refs,
319 each_ref_fn fn, void *cb_data);
320 int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
321 each_ref_fn fn, void *cb_data);
322 int refs_for_each_tag_ref(struct ref_store *refs,
323 each_ref_fn fn, void *cb_data);
324 int refs_for_each_branch_ref(struct ref_store *refs,
325 each_ref_fn fn, void *cb_data);
326 int refs_for_each_remote_ref(struct ref_store *refs,
327 each_ref_fn fn, void *cb_data);
328 int refs_for_each_replace_ref(struct ref_store *refs,
329 each_ref_fn fn, void *cb_data);
332 * references matching any pattern in "exclude_patterns" are omitted from the
333 * result set on a best-effort basis.
335 int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
336 const char **exclude_patterns,
337 each_ref_fn fn, void *cb_data);
340 * iterate all refs in "patterns" by partitioning patterns into disjoint sets
341 * and iterating the longest-common prefix of each set.
343 * references matching any pattern in "exclude_patterns" are omitted from the
344 * result set on a best-effort basis.
346 * callers should be prepared to ignore references that they did not ask for.
348 int refs_for_each_fullref_in_prefixes(struct ref_store *refs,
349 const char *namespace,
350 const char **patterns,
351 const char **exclude_patterns,
352 each_ref_fn fn, void *cb_data);
354 /* iterates all refs that match the specified glob pattern. */
355 int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
356 const char *pattern, void *cb_data);
358 int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn,
359 const char *pattern, const char *prefix, void *cb_data);
361 int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data);
364 * references matching any pattern in "exclude_patterns" are omitted from the
365 * result set on a best-effort basis.
367 int refs_for_each_namespaced_ref(struct ref_store *refs,
368 const char **exclude_patterns,
369 each_ref_fn fn, void *cb_data);
371 /* can be used to learn about broken ref and symref */
372 int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data);
375 * Iterates over all refs including root refs, i.e. pseudorefs and HEAD.
377 int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn,
378 void *cb_data);
381 * Normalizes partial refs to their fully qualified form.
382 * Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'.
383 * <prefix> will default to 'refs/' if NULL.
385 * item.string will be set to the result.
386 * item.util will be set to NULL if <pattern> contains glob characters, or
387 * non-NULL if it doesn't.
389 void normalize_glob_ref(struct string_list_item *item, const char *prefix,
390 const char *pattern);
392 static inline const char *has_glob_specials(const char *pattern)
394 return strpbrk(pattern, "?*[");
397 void refs_warn_dangling_symref(struct ref_store *refs, FILE *fp,
398 const char *msg_fmt, const char *refname);
399 void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
400 const char *msg_fmt, const struct string_list *refnames);
403 * Flags for controlling behaviour of pack_refs()
404 * PACK_REFS_PRUNE: Prune loose refs after packing
405 * PACK_REFS_AUTO: Pack refs on a best effort basis. The heuristics and end
406 * result are decided by the ref backend. Backends may ignore
407 * this flag and fall back to a normal repack.
409 #define PACK_REFS_PRUNE (1 << 0)
410 #define PACK_REFS_AUTO (1 << 1)
412 struct pack_refs_opts {
413 unsigned int flags;
414 struct ref_exclusions *exclusions;
415 struct string_list *includes;
419 * Write a packed-refs file for the current repository.
420 * flags: Combination of the above PACK_REFS_* flags.
422 int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts);
425 * Setup reflog before using. Fill in err and return -1 on failure.
427 int refs_create_reflog(struct ref_store *refs, const char *refname,
428 struct strbuf *err);
431 * Reads log for the value of ref during at_time (in which case "cnt" should be
432 * negative) or the reflog "cnt" entries from the top (in which case "at_time"
433 * should be 0).
435 * If we found the reflog entry in question, returns 0 (and details of the
436 * entry can be found in the out-parameters).
438 * If we ran out of reflog entries, the out-parameters are filled with the
439 * details of the oldest entry we did find, and the function returns 1. Note
440 * that there is one important special case here! If the reflog was empty
441 * and the caller asked for the 0-th cnt, we will return "1" but leave the
442 * "oid" field untouched.
444 int read_ref_at(struct ref_store *refs,
445 const char *refname, unsigned int flags,
446 timestamp_t at_time, int cnt,
447 struct object_id *oid, char **msg,
448 timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
450 /** Check if a particular reflog exists */
451 int refs_reflog_exists(struct ref_store *refs, const char *refname);
454 * Delete the specified reference. If old_oid is non-NULL, then
455 * verify that the current value of the reference is old_oid before
456 * deleting it. If old_oid is NULL, delete the reference if it
457 * exists, regardless of its old value. It is an error for old_oid to
458 * be null_oid. msg and flags are passed through to
459 * ref_transaction_delete().
461 int refs_delete_ref(struct ref_store *refs, const char *msg,
462 const char *refname,
463 const struct object_id *old_oid,
464 unsigned int flags);
467 * Delete the specified references. If there are any problems, emit
468 * errors but attempt to keep going (i.e., the deletes are not done in
469 * an all-or-nothing transaction). msg and flags are passed through to
470 * ref_transaction_delete().
472 int refs_delete_refs(struct ref_store *refs, const char *msg,
473 struct string_list *refnames, unsigned int flags);
475 /** Delete a reflog */
476 int refs_delete_reflog(struct ref_store *refs, const char *refname);
479 * Callback to process a reflog entry found by the iteration functions (see
480 * below).
482 * The committer parameter is a single string, in the form
483 * "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" (without double quotes).
485 * The timestamp parameter gives the time when entry was created as the number
486 * of seconds since the UNIX epoch.
488 * The tz parameter gives the timezone offset for the user who created
489 * the reflog entry, and its value gives a positive or negative offset
490 * from UTC. Its absolute value is formed by multiplying the hour
491 * part by 100 and adding the minute part. For example, 1 hour ahead
492 * of UTC, CET == "+0100", is represented as positive one hundred (not
493 * positive sixty).
495 * The msg parameter is a single complete line; a reflog message given
496 * to refs_delete_ref, refs_update_ref, etc. is returned to the
497 * callback normalized---each run of whitespaces are squashed into a
498 * single whitespace, trailing whitespace, if exists, is trimmed, and
499 * then a single LF is added at the end.
501 * The cb_data is a caller-supplied pointer given to the iterator
502 * functions.
504 typedef int each_reflog_ent_fn(
505 struct object_id *old_oid, struct object_id *new_oid,
506 const char *committer, timestamp_t timestamp,
507 int tz, const char *msg, void *cb_data);
509 /* Iterate over reflog entries in the log for `refname`. */
511 /* oldest entry first */
512 int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
513 each_reflog_ent_fn fn, void *cb_data);
515 /* youngest entry first */
516 int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
517 const char *refname,
518 each_reflog_ent_fn fn,
519 void *cb_data);
522 * The signature for the callback function for the refs_for_each_reflog()
523 * functions below. The memory pointed to by the refname argument is only
524 * guaranteed to be valid for the duration of a single callback invocation.
526 typedef int each_reflog_fn(const char *refname, void *cb_data);
529 * Calls the specified function for each reflog file until it returns nonzero,
530 * and returns the value. Reflog file order is unspecified.
532 int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data);
534 #define REFNAME_ALLOW_ONELEVEL 1
535 #define REFNAME_REFSPEC_PATTERN 2
538 * Return 0 iff refname has the correct format for a refname according
539 * to the rules described in Documentation/git-check-ref-format.txt.
540 * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level
541 * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then
542 * allow a single "*" wildcard character in the refspec. No leading or
543 * repeated slashes are accepted.
545 int check_refname_format(const char *refname, int flags);
548 * Check the reference database for consistency. Return 0 if refs and
549 * reflogs are consistent, and non-zero otherwise. The errors will be
550 * written to stderr.
552 int refs_fsck(struct ref_store *refs, struct fsck_options *o);
555 * Apply the rules from check_refname_format, but mutate the result until it
556 * is acceptable, and place the result in "out".
558 void sanitize_refname_component(const char *refname, struct strbuf *out);
560 const char *prettify_refname(const char *refname);
562 char *refs_shorten_unambiguous_ref(struct ref_store *refs,
563 const char *refname, int strict);
565 /** rename ref, return 0 on success **/
566 int refs_rename_ref(struct ref_store *refs, const char *oldref,
567 const char *newref, const char *logmsg);
569 /** copy ref, return 0 on success **/
570 int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
571 const char *newref, const char *logmsg);
573 int refs_update_symref(struct ref_store *refs, const char *refname,
574 const char *target, const char *logmsg);
576 enum action_on_err {
577 UPDATE_REFS_MSG_ON_ERR,
578 UPDATE_REFS_DIE_ON_ERR,
579 UPDATE_REFS_QUIET_ON_ERR
583 * Begin a reference transaction. The reference transaction must
584 * be freed by calling ref_transaction_free().
586 struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
587 struct strbuf *err);
590 * Reference transaction updates
592 * The following four functions add a reference check or update to a
593 * ref_transaction. They have some common similar parameters:
595 * transaction -- a pointer to an open ref_transaction, obtained
596 * from ref_transaction_begin().
598 * refname -- the name of the reference to be affected.
600 * new_oid -- the object ID that should be set to be the new value
601 * of the reference. Some functions allow this parameter to be
602 * NULL, meaning that the reference is not changed, or
603 * null_oid, meaning that the reference should be deleted. A
604 * copy of this value is made in the transaction.
606 * old_oid -- the object ID that the reference must have before
607 * the update. Some functions allow this parameter to be NULL,
608 * meaning that the old value of the reference is not checked,
609 * or null_oid, meaning that the reference must not exist
610 * before the update. A copy of this value is made in the
611 * transaction.
613 * new_target -- the target reference that the reference will be
614 * updated to point to. If the reference is a regular reference,
615 * it will be converted to a symbolic reference. Cannot be set
616 * together with `new_oid`. A copy of this value is made in the
617 * transaction.
619 * old_target -- the reference that the reference must be pointing to.
620 * Canont be set together with `old_oid`. A copy of this value is
621 * made in the transaction.
623 * flags -- flags affecting the update, passed to
624 * update_ref_lock(). Possible flags: REF_NO_DEREF,
625 * REF_FORCE_CREATE_REFLOG. See those constants for more
626 * information.
628 * msg -- a message describing the change (for the reflog).
630 * err -- a strbuf for receiving a description of any error that
631 * might have occurred.
633 * The functions make internal copies of refname and msg, so the
634 * caller retains ownership of these parameters.
636 * The functions return 0 on success and non-zero on failure. A
637 * failure means that the transaction as a whole has failed and needs
638 * to be rolled back.
642 * The following flags can be passed to ref_transaction_update() etc.
643 * Internally, they are stored in `ref_update::flags`, along with some
644 * internal flags.
648 * Act on the ref directly; i.e., without dereferencing symbolic refs.
649 * If this flag is not specified, then symbolic references are
650 * dereferenced and the update is applied to the referent.
652 #define REF_NO_DEREF (1 << 0)
655 * Force the creation of a reflog for this reference, even if it
656 * didn't previously have a reflog.
658 #define REF_FORCE_CREATE_REFLOG (1 << 1)
661 * Blindly write an object_id. This is useful for testing data corruption
662 * scenarios.
664 #define REF_SKIP_OID_VERIFICATION (1 << 10)
667 * Skip verifying refname. This is useful for testing data corruption scenarios.
669 #define REF_SKIP_REFNAME_VERIFICATION (1 << 11)
672 * Skip creation of a reflog entry, even if it would have otherwise been
673 * created.
675 #define REF_SKIP_CREATE_REFLOG (1 << 12)
678 * Bitmask of all of the flags that are allowed to be passed in to
679 * ref_transaction_update() and friends:
681 #define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
682 (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION | \
683 REF_SKIP_REFNAME_VERIFICATION | REF_SKIP_CREATE_REFLOG)
686 * Add a reference update to transaction. `new_oid` is the value that
687 * the reference should have after the update, or `null_oid` if it
688 * should be deleted. If `new_oid` is NULL, then the reference is not
689 * changed at all. `old_oid` is the value that the reference must have
690 * before the update, or `null_oid` if it must not have existed
691 * beforehand. The old value is checked after the lock is taken to
692 * prevent races. If the old value doesn't agree with old_oid, the
693 * whole transaction fails. If old_oid is NULL, then the previous
694 * value is not checked. If `old_target` is not NULL, treat the reference
695 * as a symbolic ref and validate that its target before the update is
696 * `old_target`. If the `new_target` is not NULL, then the reference
697 * will be updated to a symbolic ref which targets `new_target`.
698 * Together, these allow us to update between regular refs and symrefs.
700 * See the above comment "Reference transaction updates" for more
701 * information.
703 int ref_transaction_update(struct ref_transaction *transaction,
704 const char *refname,
705 const struct object_id *new_oid,
706 const struct object_id *old_oid,
707 const char *new_target,
708 const char *old_target,
709 unsigned int flags, const char *msg,
710 struct strbuf *err);
713 * Add a reference creation to transaction. new_oid is the value that
714 * the reference should have after the update; it must not be
715 * null_oid. It is verified that the reference does not exist
716 * already.
718 * See the above comment "Reference transaction updates" for more
719 * information.
721 int ref_transaction_create(struct ref_transaction *transaction,
722 const char *refname,
723 const struct object_id *new_oid,
724 const char *new_target,
725 unsigned int flags, const char *msg,
726 struct strbuf *err);
729 * Add a reference deletion to transaction. If old_oid is non-NULL,
730 * then it holds the value that the reference should have had before
731 * the update (which must not be null_oid).
733 * See the above comment "Reference transaction updates" for more
734 * information.
736 int ref_transaction_delete(struct ref_transaction *transaction,
737 const char *refname,
738 const struct object_id *old_oid,
739 const char *old_target,
740 unsigned int flags,
741 const char *msg,
742 struct strbuf *err);
745 * Verify, within a transaction, that refname has the value old_oid,
746 * or, if old_oid is null_oid, then verify that the reference
747 * doesn't exist. old_oid must be non-NULL.
749 * See the above comment "Reference transaction updates" for more
750 * information.
752 int ref_transaction_verify(struct ref_transaction *transaction,
753 const char *refname,
754 const struct object_id *old_oid,
755 const char *old_target,
756 unsigned int flags,
757 struct strbuf *err);
759 /* Naming conflict (for example, the ref names A and A/B conflict). */
760 #define TRANSACTION_NAME_CONFLICT -1
761 /* All other errors. */
762 #define TRANSACTION_GENERIC_ERROR -2
765 * Perform the preparatory stages of committing `transaction`. Acquire
766 * any needed locks, check preconditions, etc.; basically, do as much
767 * as possible to ensure that the transaction will be able to go
768 * through, stopping just short of making any irrevocable or
769 * user-visible changes. The updates that this function prepares can
770 * be finished up by calling `ref_transaction_commit()` or rolled back
771 * by calling `ref_transaction_abort()`.
773 * On success, return 0 and leave the transaction in "prepared" state.
774 * On failure, abort the transaction, write an error message to `err`,
775 * and return one of the `TRANSACTION_*` constants.
777 * Callers who don't need such fine-grained control over committing
778 * reference transactions should just call `ref_transaction_commit()`.
780 int ref_transaction_prepare(struct ref_transaction *transaction,
781 struct strbuf *err);
784 * Commit all of the changes that have been queued in transaction, as
785 * atomically as possible. On success, return 0 and leave the
786 * transaction in "closed" state. On failure, roll back the
787 * transaction, write an error message to `err`, and return one of the
788 * `TRANSACTION_*` constants
790 int ref_transaction_commit(struct ref_transaction *transaction,
791 struct strbuf *err);
794 * Abort `transaction`, which has been begun and possibly prepared,
795 * but not yet committed.
797 int ref_transaction_abort(struct ref_transaction *transaction,
798 struct strbuf *err);
801 * Like ref_transaction_commit(), but optimized for creating
802 * references when originally initializing a repository (e.g., by "git
803 * clone"). It writes the new references directly to packed-refs
804 * without locking the individual references.
806 * It is a bug to call this function when there might be other
807 * processes accessing the repository or if there are existing
808 * references that might conflict with the ones being created. All
809 * old_oid values must either be absent or null_oid.
811 int initial_ref_transaction_commit(struct ref_transaction *transaction,
812 struct strbuf *err);
815 * Execute the given callback function for each of the reference updates which
816 * have been queued in the given transaction. `old_oid` and `new_oid` may be
817 * `NULL` pointers depending on whether the update has these object IDs set or
818 * not.
820 typedef void ref_transaction_for_each_queued_update_fn(const char *refname,
821 const struct object_id *old_oid,
822 const struct object_id *new_oid,
823 void *cb_data);
824 void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
825 ref_transaction_for_each_queued_update_fn cb,
826 void *cb_data);
829 * Free `*transaction` and all associated data.
831 void ref_transaction_free(struct ref_transaction *transaction);
834 * Lock, update, and unlock a single reference. This function
835 * basically does a transaction containing a single call to
836 * ref_transaction_update(). The parameters to this function have the
837 * same meaning as the corresponding parameters to
838 * ref_transaction_update(). Handle errors as requested by the `onerr`
839 * argument.
841 int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname,
842 const struct object_id *new_oid, const struct object_id *old_oid,
843 unsigned int flags, enum action_on_err onerr);
845 int parse_hide_refs_config(const char *var, const char *value, const char *,
846 struct strvec *);
849 * Check whether a ref is hidden. If no namespace is set, both the first and
850 * the second parameter point to the full ref name. If a namespace is set and
851 * the ref is inside that namespace, the first parameter is a pointer to the
852 * name of the ref with the namespace prefix removed. If a namespace is set and
853 * the ref is outside that namespace, the first parameter is NULL. The second
854 * parameter always points to the full ref name.
856 int ref_is_hidden(const char *, const char *, const struct strvec *);
859 * Returns an array of patterns to use as excluded_patterns, if none of the
860 * hidden references use the token '!' or '^'.
862 const char **hidden_refs_to_excludes(const struct strvec *hide_refs);
865 * Prefix all exclude patterns with the namespace, if any. This is required
866 * because exclude patterns apply to the stripped reference name, not the full
867 * reference name with the namespace.
869 const char **get_namespaced_exclude_patterns(const char **exclude_patterns,
870 const char *namespace,
871 struct strvec *out);
873 /* Is this a per-worktree ref living in the refs/ namespace? */
874 int is_per_worktree_ref(const char *refname);
876 /* Describes how a refname relates to worktrees */
877 enum ref_worktree_type {
878 REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or
879 refs/bisect/SOMETHING */
880 REF_WORKTREE_MAIN, /* explicitly in main worktree, eg.
881 main-worktree/HEAD */
882 REF_WORKTREE_OTHER, /* explicitly in named worktree, eg.
883 worktrees/bla/HEAD */
884 REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */
888 * Parse a `maybe_worktree_ref` as a ref that possibly refers to a worktree ref
889 * (ie. either REFNAME, main-worktree/REFNAME or worktree/WORKTREE/REFNAME). It
890 * returns what kind of ref was found, and in case of REF_WORKTREE_OTHER, the
891 * worktree name is returned in `worktree_name` (pointing into
892 * `maybe_worktree_ref`) and `worktree_name_length`. The bare refname (the
893 * refname stripped of prefixes) is returned in `bare_refname`. The
894 * `worktree_name`, `worktree_name_length` and `bare_refname` arguments may be
895 * NULL.
897 enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
898 const char **worktree_name,
899 int *worktree_name_length,
900 const char **bare_refname);
902 enum expire_reflog_flags {
903 EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
904 EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
905 EXPIRE_REFLOGS_REWRITE = 1 << 2,
909 * The following interface is used for reflog expiration. The caller
910 * calls refs_reflog_expire(), supplying it with three callback functions,
911 * of the following types. The callback functions define the
912 * expiration policy that is desired.
914 * reflog_expiry_prepare_fn -- Called once after the reference is
915 * locked. Called with the OID of the locked reference.
917 * reflog_expiry_should_prune_fn -- Called once for each entry in the
918 * existing reflog. It should return true iff that entry should be
919 * pruned.
921 * reflog_expiry_cleanup_fn -- Called once before the reference is
922 * unlocked again.
924 typedef void reflog_expiry_prepare_fn(const char *refname,
925 const struct object_id *oid,
926 void *cb_data);
927 typedef int reflog_expiry_should_prune_fn(struct object_id *ooid,
928 struct object_id *noid,
929 const char *email,
930 timestamp_t timestamp, int tz,
931 const char *message, void *cb_data);
932 typedef void reflog_expiry_cleanup_fn(void *cb_data);
935 * Expire reflog entries for the specified reference.
936 * flags is a combination of the constants in
937 * enum expire_reflog_flags. The three function pointers are described
938 * above. On success, return zero.
940 int refs_reflog_expire(struct ref_store *refs,
941 const char *refname,
942 unsigned int flags,
943 reflog_expiry_prepare_fn prepare_fn,
944 reflog_expiry_should_prune_fn should_prune_fn,
945 reflog_expiry_cleanup_fn cleanup_fn,
946 void *policy_cb_data);
948 struct ref_store *get_main_ref_store(struct repository *r);
951 * Submodules
952 * ----------
954 * If you want to iterate the refs of a submodule you first need to add the
955 * submodules object database. You can do this by a code-snippet like
956 * this:
958 * const char *path = "path/to/submodule"
959 * if (add_submodule_odb(path))
960 * die("Error submodule '%s' not populated.", path);
962 * `add_submodule_odb()` will return zero on success. If you
963 * do not do this you will get an error for each ref that it does not point
964 * to a valid object.
966 * Note: As a side-effect of this you cannot safely assume that all
967 * objects you lookup are available in superproject. All submodule objects
968 * will be available the same way as the superprojects objects.
970 * Example:
971 * --------
973 * ----
974 * static int handle_remote_ref(const char *refname,
975 * const unsigned char *sha1, int flags, void *cb_data)
977 * struct strbuf *output = cb_data;
978 * strbuf_addf(output, "%s\n", refname);
979 * return 0;
985 * Return the ref_store instance for the specified submodule. For the
986 * main repository, use submodule==NULL; such a call cannot fail. For
987 * a submodule, the submodule must exist and be a nonbare repository,
988 * otherwise return NULL. If the requested reference store has not yet
989 * been initialized, initialize it first.
991 * For backwards compatibility, submodule=="" is treated the same as
992 * submodule==NULL.
994 struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
995 const char *submodule);
996 struct ref_store *get_worktree_ref_store(const struct worktree *wt);
999 * Some of the names specified by refs have special meaning to Git.
1000 * Organize these namespaces in a common 'ref_namespace' array for
1001 * reference from multiple places in the codebase.
1004 struct ref_namespace_info {
1005 const char *ref;
1006 enum decoration_type decoration;
1009 * If 'exact' is true, then we must match the 'ref' exactly.
1010 * Otherwise, use a prefix match.
1012 * 'ref_updated' is for internal use. It represents whether the
1013 * 'ref' value was replaced from its original literal version.
1015 unsigned exact:1,
1016 ref_updated:1;
1019 enum ref_namespace {
1020 NAMESPACE_HEAD,
1021 NAMESPACE_BRANCHES,
1022 NAMESPACE_TAGS,
1023 NAMESPACE_REMOTE_REFS,
1024 NAMESPACE_STASH,
1025 NAMESPACE_REPLACE,
1026 NAMESPACE_NOTES,
1027 NAMESPACE_PREFETCH,
1028 NAMESPACE_REWRITTEN,
1030 /* Must be last */
1031 NAMESPACE__COUNT
1034 /* See refs.c for the contents of this array. */
1035 extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT];
1038 * Some ref namespaces can be modified by config values or environment
1039 * variables. Modify a namespace as specified by its ref_namespace key.
1041 void update_ref_namespace(enum ref_namespace namespace, char *ref);
1044 * Check whether the provided name names a root reference. This function only
1045 * performs a syntactic check.
1047 * A root ref is a reference that lives in the root of the reference hierarchy.
1048 * These references must conform to special syntax:
1050 * - Their name must be all-uppercase or underscores ("_").
1052 * - Their name must end with "_HEAD". As a special rule, "HEAD" is a root
1053 * ref, as well.
1055 * - Their name may not contain a slash.
1057 * There is a special set of irregular root refs that exist due to historic
1058 * reasons, only. This list shall not be expanded in the future:
1060 * - AUTO_MERGE
1062 * - BISECT_EXPECTED_REV
1064 * - NOTES_MERGE_PARTIAL
1066 * - NOTES_MERGE_REF
1068 * - MERGE_AUTOSTASH
1070 int is_root_ref(const char *refname);
1073 * Pseudorefs are refs that have different semantics compared to
1074 * "normal" refs. These refs can thus not be stored in the ref backend,
1075 * but must always be accessed via the filesystem. The following refs
1076 * are pseudorefs:
1078 * - FETCH_HEAD may contain multiple object IDs, and each one of them
1079 * carries additional metadata like where it came from.
1081 * - MERGE_HEAD may contain multiple object IDs when merging multiple
1082 * heads.
1084 * Reading, writing or deleting references must consistently go either
1085 * through the filesystem (pseudorefs) or through the reference
1086 * backend (normal ones).
1088 int is_pseudo_ref(const char *refname);
1091 * The following flags can be passed to `repo_migrate_ref_storage_format()`:
1093 * - REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN: perform a dry-run migration
1094 * without touching the main repository. The result will be written into a
1095 * temporary ref storage directory.
1097 #define REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN (1 << 0)
1100 * Migrate the ref storage format used by the repository to the
1101 * specified one.
1103 int repo_migrate_ref_storage_format(struct repository *repo,
1104 enum ref_storage_format format,
1105 unsigned int flags,
1106 struct strbuf *err);
1108 #endif /* REFS_H */