The tenth batch
[alt-git.git] / refs.h
blobf8b919a138868ed20e4fb344f5a9caf925c92755
1 #ifndef REFS_H
2 #define REFS_H
4 #include "commit.h"
5 #include "repository.h"
7 struct fsck_options;
8 struct object_id;
9 struct ref_store;
10 struct strbuf;
11 struct string_list;
12 struct string_list_item;
13 struct worktree;
15 enum ref_storage_format ref_storage_format_by_name(const char *name);
16 const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);
19 * Resolve a reference, recursively following symbolic refererences.
21 * Return the name of the non-symbolic reference that ultimately pointed
22 * at the resolved object name. The return value, if not NULL, is a
23 * pointer into either a static buffer or the input ref.
25 * If oid is non-NULL, store the referred-to object's name in it.
27 * If the reference cannot be resolved to an object, the behavior
28 * depends on the RESOLVE_REF_READING flag:
30 * - If RESOLVE_REF_READING is set, return NULL.
32 * - If RESOLVE_REF_READING is not set, clear oid and return the name of
33 * the last reference name in the chain, which will either be a non-symbolic
34 * reference or an undefined reference. If this is a prelude to
35 * "writing" to the ref, the return value is the name of the ref
36 * that will actually be created or changed.
38 * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one
39 * level of symbolic reference. The value stored in oid for a symbolic
40 * reference will always be null_oid in this case, and the return
41 * value is the reference that the symref refers to directly.
43 * If flags is non-NULL, set the value that it points to the
44 * combination of REF_ISPACKED (if the reference was found among the
45 * packed references), REF_ISSYMREF (if the initial reference was a
46 * symbolic reference), REF_BAD_NAME (if the reference name is ill
47 * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN
48 * (if the ref is malformed or has a bad name). See refs.h for more detail
49 * on each flag.
51 * If ref is not a properly-formatted, normalized reference, return
52 * NULL. If more than MAXDEPTH recursive symbolic lookups are needed,
53 * give up and return NULL.
55 * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their
56 * name is invalid according to git-check-ref-format(1). If the name
57 * is bad then the value stored in oid will be null_oid and the two
58 * flags REF_ISBROKEN and REF_BAD_NAME will be set.
60 * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/
61 * directory and do not consist of all caps and underscores cannot be
62 * resolved. The function returns NULL for such ref names.
63 * Caps and underscores refers to the pseudorefs, such as HEAD,
64 * FETCH_HEAD and friends, that all live outside of the refs/ directory.
66 #define RESOLVE_REF_READING 0x01
67 #define RESOLVE_REF_NO_RECURSE 0x02
68 #define RESOLVE_REF_ALLOW_BAD_NAME 0x04
70 const char *refs_resolve_ref_unsafe(struct ref_store *refs,
71 const char *refname,
72 int resolve_flags,
73 struct object_id *oid,
74 int *flags);
76 char *refs_resolve_refdup(struct ref_store *refs,
77 const char *refname, int resolve_flags,
78 struct object_id *oid, int *flags);
80 int refs_read_ref_full(struct ref_store *refs, const char *refname,
81 int resolve_flags, struct object_id *oid, int *flags);
83 int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
85 int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
86 struct strbuf *referent);
89 * Return 0 if a reference named refname could be created without
90 * conflicting with the name of an existing reference. Otherwise,
91 * return a negative value and write an explanation to err. If extras
92 * is non-NULL, it is a list of additional refnames with which refname
93 * is not allowed to conflict. If skip is non-NULL, ignore potential
94 * conflicts with refs in skip (e.g., because they are scheduled for
95 * deletion in the same operation). Behavior is undefined if the same
96 * name is listed in both extras and skip.
98 * Two reference names conflict if one of them exactly matches the
99 * leading components of the other; e.g., "foo/bar" conflicts with
100 * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
101 * "foo/barbados".
103 * extras and skip must be sorted.
106 int refs_verify_refname_available(struct ref_store *refs,
107 const char *refname,
108 const struct string_list *extras,
109 const struct string_list *skip,
110 struct strbuf *err);
112 int refs_ref_exists(struct ref_store *refs, const char *refname);
114 int should_autocreate_reflog(const char *refname);
116 int is_branch(const char *refname);
118 #define REF_STORE_CREATE_ON_DISK_IS_WORKTREE (1 << 0)
120 int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err);
123 * Release all memory and resources associated with the ref store.
125 void ref_store_release(struct ref_store *ref_store);
128 * Remove the ref store from disk. This deletes all associated data.
130 int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err);
133 * Return the peeled value of the oid currently being iterated via
134 * for_each_ref(), etc. This is equivalent to calling:
136 * peel_object(r, oid, &peeled);
138 * with the "oid" value given to the each_ref_fn callback, except
139 * that some ref storage may be able to answer the query without
140 * actually loading the object in memory.
142 int peel_iterated_oid(struct repository *r,
143 const struct object_id *base, struct object_id *peeled);
146 * Resolve refname in the nested "gitlink" repository in the specified
147 * submodule (which must be non-NULL). If the resolution is
148 * successful, return 0 and set oid to the name of the object;
149 * otherwise, return a non-zero value.
151 int repo_resolve_gitlink_ref(struct repository *r,
152 const char *submodule, const char *refname,
153 struct object_id *oid);
156 * Return true iff abbrev_name is a possible abbreviation for
157 * full_name according to the rules defined by ref_rev_parse_rules in
158 * refs.c.
160 int refname_match(const char *abbrev_name, const char *full_name);
163 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
164 * the results to 'prefixes'
166 struct strvec;
167 void expand_ref_prefix(struct strvec *prefixes, const char *prefix);
169 int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
170 int repo_dwim_ref(struct repository *r, const char *str, int len,
171 struct object_id *oid, char **ref, int nonfatal_dangling_mark);
172 int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
175 * Retrieves the default branch name for newly-initialized repositories.
177 * The return value is an allocated string.
179 char *repo_default_branch_name(struct repository *r, int quiet);
182 * A ref_transaction represents a collection of reference updates that
183 * should succeed or fail together.
185 * Calling sequence
186 * ----------------
188 * - Allocate and initialize a `struct ref_transaction` by calling
189 * `ref_transaction_begin()`.
191 * - Specify the intended ref updates by calling one or more of the
192 * following functions:
193 * - `ref_transaction_update()`
194 * - `ref_transaction_create()`
195 * - `ref_transaction_delete()`
196 * - `ref_transaction_verify()`
198 * - Then either:
200 * - Optionally call `ref_transaction_prepare()` to prepare the
201 * transaction. This locks all references, checks preconditions,
202 * etc. but doesn't finalize anything. If this step fails, the
203 * transaction has been closed and can only be freed. If this step
204 * succeeds, then `ref_transaction_commit()` is almost certain to
205 * succeed. However, you can still call `ref_transaction_abort()`
206 * if you decide not to commit the transaction after all.
208 * - Call `ref_transaction_commit()` to execute the transaction,
209 * make the changes permanent, and release all locks. If you
210 * haven't already called `ref_transaction_prepare()`, then
211 * `ref_transaction_commit()` calls it for you.
213 * Or
215 * - Call `initial_ref_transaction_commit()` if the ref database is
216 * known to be empty and have no other writers (e.g. during
217 * clone). This is likely to be much faster than
218 * `ref_transaction_commit()`. `ref_transaction_prepare()` should
219 * *not* be called before `initial_ref_transaction_commit()`.
221 * - Then finally, call `ref_transaction_free()` to free the
222 * `ref_transaction` data structure.
224 * At any time before calling `ref_transaction_commit()`, you can call
225 * `ref_transaction_abort()` to abort the transaction, rollback any
226 * locks, and free any associated resources (including the
227 * `ref_transaction` data structure).
229 * Putting it all together, a complete reference update looks like
231 * struct ref_transaction *transaction;
232 * struct strbuf err = STRBUF_INIT;
233 * int ret = 0;
235 * transaction = ref_store_transaction_begin(refs, &err);
236 * if (!transaction ||
237 * ref_transaction_update(...) ||
238 * ref_transaction_create(...) ||
239 * ...etc... ||
240 * ref_transaction_commit(transaction, &err)) {
241 * error("%s", err.buf);
242 * ret = -1;
244 * ref_transaction_free(transaction);
245 * strbuf_release(&err);
246 * return ret;
248 * Error handling
249 * --------------
251 * On error, transaction functions append a message about what
252 * went wrong to the 'err' argument. The message mentions what
253 * ref was being updated (if any) when the error occurred so it
254 * can be passed to 'die' or 'error' as-is.
256 * The message is appended to err without first clearing err.
257 * err will not be '\n' terminated.
259 * Caveats
260 * -------
262 * Note that no locks are taken, and no refs are read, until
263 * `ref_transaction_prepare()` or `ref_transaction_commit()` is
264 * called. So, for example, `ref_transaction_verify()` won't report a
265 * verification failure until the commit is attempted.
267 struct ref_transaction;
270 * Bit values set in the flags argument passed to each_ref_fn() and
271 * stored in ref_iterator::flags. Other bits are for internal use
272 * only:
275 /* Reference is a symbolic reference. */
276 #define REF_ISSYMREF 0x01
278 /* Reference is a packed reference. */
279 #define REF_ISPACKED 0x02
282 * Reference cannot be resolved to an object name: dangling symbolic
283 * reference (directly or indirectly), corrupt reference file,
284 * reference exists but name is bad, or symbolic reference refers to
285 * ill-formatted reference name.
287 #define REF_ISBROKEN 0x04
290 * Reference name is not well formed.
292 * See git-check-ref-format(1) for the definition of well formed ref names.
294 #define REF_BAD_NAME 0x08
297 * The signature for the callback function for the for_each_*()
298 * functions below. The memory pointed to by the refname and oid
299 * arguments is only guaranteed to be valid for the duration of a
300 * single callback invocation.
302 typedef int each_ref_fn(const char *refname, const char *referent,
303 const struct object_id *oid, int flags, void *cb_data);
306 * The following functions invoke the specified callback function for
307 * each reference indicated. If the function ever returns a nonzero
308 * value, stop the iteration and return that value. Please note that
309 * it is not safe to modify references while an iteration is in
310 * progress, unless the same callback function invocation that
311 * modifies the reference also returns a nonzero value to immediately
312 * stop the iteration. Returned references are sorted.
314 int refs_head_ref(struct ref_store *refs,
315 each_ref_fn fn, void *cb_data);
316 int refs_for_each_ref(struct ref_store *refs,
317 each_ref_fn fn, void *cb_data);
318 int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
319 each_ref_fn fn, void *cb_data);
320 int refs_for_each_tag_ref(struct ref_store *refs,
321 each_ref_fn fn, void *cb_data);
322 int refs_for_each_branch_ref(struct ref_store *refs,
323 each_ref_fn fn, void *cb_data);
324 int refs_for_each_remote_ref(struct ref_store *refs,
325 each_ref_fn fn, void *cb_data);
326 int refs_for_each_replace_ref(struct ref_store *refs,
327 each_ref_fn fn, void *cb_data);
330 * references matching any pattern in "exclude_patterns" are omitted from the
331 * result set on a best-effort basis.
333 int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
334 const char **exclude_patterns,
335 each_ref_fn fn, void *cb_data);
338 * iterate all refs in "patterns" by partitioning patterns into disjoint sets
339 * and iterating the longest-common prefix of each set.
341 * references matching any pattern in "exclude_patterns" are omitted from the
342 * result set on a best-effort basis.
344 * callers should be prepared to ignore references that they did not ask for.
346 int refs_for_each_fullref_in_prefixes(struct ref_store *refs,
347 const char *namespace,
348 const char **patterns,
349 const char **exclude_patterns,
350 each_ref_fn fn, void *cb_data);
352 /* iterates all refs that match the specified glob pattern. */
353 int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
354 const char *pattern, void *cb_data);
356 int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn,
357 const char *pattern, const char *prefix, void *cb_data);
359 int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data);
362 * references matching any pattern in "exclude_patterns" are omitted from the
363 * result set on a best-effort basis.
365 int refs_for_each_namespaced_ref(struct ref_store *refs,
366 const char **exclude_patterns,
367 each_ref_fn fn, void *cb_data);
369 /* can be used to learn about broken ref and symref */
370 int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data);
373 * Iterates over all refs including root refs, i.e. pseudorefs and HEAD.
375 int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn,
376 void *cb_data);
379 * Normalizes partial refs to their fully qualified form.
380 * Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'.
381 * <prefix> will default to 'refs/' if NULL.
383 * item.string will be set to the result.
384 * item.util will be set to NULL if <pattern> contains glob characters, or
385 * non-NULL if it doesn't.
387 void normalize_glob_ref(struct string_list_item *item, const char *prefix,
388 const char *pattern);
390 static inline const char *has_glob_specials(const char *pattern)
392 return strpbrk(pattern, "?*[");
395 void refs_warn_dangling_symref(struct ref_store *refs, FILE *fp,
396 const char *msg_fmt, const char *refname);
397 void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
398 const char *msg_fmt, const struct string_list *refnames);
401 * Flags for controlling behaviour of pack_refs()
402 * PACK_REFS_PRUNE: Prune loose refs after packing
403 * PACK_REFS_AUTO: Pack refs on a best effort basis. The heuristics and end
404 * result are decided by the ref backend. Backends may ignore
405 * this flag and fall back to a normal repack.
407 #define PACK_REFS_PRUNE (1 << 0)
408 #define PACK_REFS_AUTO (1 << 1)
410 struct pack_refs_opts {
411 unsigned int flags;
412 struct ref_exclusions *exclusions;
413 struct string_list *includes;
417 * Write a packed-refs file for the current repository.
418 * flags: Combination of the above PACK_REFS_* flags.
420 int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts);
423 * Setup reflog before using. Fill in err and return -1 on failure.
425 int refs_create_reflog(struct ref_store *refs, const char *refname,
426 struct strbuf *err);
429 * Reads log for the value of ref during at_time (in which case "cnt" should be
430 * negative) or the reflog "cnt" entries from the top (in which case "at_time"
431 * should be 0).
433 * If we found the reflog entry in question, returns 0 (and details of the
434 * entry can be found in the out-parameters).
436 * If we ran out of reflog entries, the out-parameters are filled with the
437 * details of the oldest entry we did find, and the function returns 1. Note
438 * that there is one important special case here! If the reflog was empty
439 * and the caller asked for the 0-th cnt, we will return "1" but leave the
440 * "oid" field untouched.
442 int read_ref_at(struct ref_store *refs,
443 const char *refname, unsigned int flags,
444 timestamp_t at_time, int cnt,
445 struct object_id *oid, char **msg,
446 timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
448 /** Check if a particular reflog exists */
449 int refs_reflog_exists(struct ref_store *refs, const char *refname);
452 * Delete the specified reference. If old_oid is non-NULL, then
453 * verify that the current value of the reference is old_oid before
454 * deleting it. If old_oid is NULL, delete the reference if it
455 * exists, regardless of its old value. It is an error for old_oid to
456 * be null_oid. msg and flags are passed through to
457 * ref_transaction_delete().
459 int refs_delete_ref(struct ref_store *refs, const char *msg,
460 const char *refname,
461 const struct object_id *old_oid,
462 unsigned int flags);
465 * Delete the specified references. If there are any problems, emit
466 * errors but attempt to keep going (i.e., the deletes are not done in
467 * an all-or-nothing transaction). msg and flags are passed through to
468 * ref_transaction_delete().
470 int refs_delete_refs(struct ref_store *refs, const char *msg,
471 struct string_list *refnames, unsigned int flags);
473 /** Delete a reflog */
474 int refs_delete_reflog(struct ref_store *refs, const char *refname);
477 * Callback to process a reflog entry found by the iteration functions (see
478 * below).
480 * The committer parameter is a single string, in the form
481 * "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" (without double quotes).
483 * The timestamp parameter gives the time when entry was created as the number
484 * of seconds since the UNIX epoch.
486 * The tz parameter gives the timezone offset for the user who created
487 * the reflog entry, and its value gives a positive or negative offset
488 * from UTC. Its absolute value is formed by multiplying the hour
489 * part by 100 and adding the minute part. For example, 1 hour ahead
490 * of UTC, CET == "+0100", is represented as positive one hundred (not
491 * postiive sixty).
493 * The msg parameter is a single complete line; a reflog message given
494 * to refs_delete_ref, refs_update_ref, etc. is returned to the
495 * callback normalized---each run of whitespaces are squashed into a
496 * single whitespace, trailing whitespace, if exists, is trimmed, and
497 * then a single LF is added at the end.
499 * The cb_data is a caller-supplied pointer given to the iterator
500 * functions.
502 typedef int each_reflog_ent_fn(
503 struct object_id *old_oid, struct object_id *new_oid,
504 const char *committer, timestamp_t timestamp,
505 int tz, const char *msg, void *cb_data);
507 /* Iterate over reflog entries in the log for `refname`. */
509 /* oldest entry first */
510 int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
511 each_reflog_ent_fn fn, void *cb_data);
513 /* youngest entry first */
514 int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
515 const char *refname,
516 each_reflog_ent_fn fn,
517 void *cb_data);
520 * The signature for the callback function for the refs_for_each_reflog()
521 * functions below. The memory pointed to by the refname argument is only
522 * guaranteed to be valid for the duration of a single callback invocation.
524 typedef int each_reflog_fn(const char *refname, void *cb_data);
527 * Calls the specified function for each reflog file until it returns nonzero,
528 * and returns the value. Reflog file order is unspecified.
530 int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data);
532 #define REFNAME_ALLOW_ONELEVEL 1
533 #define REFNAME_REFSPEC_PATTERN 2
536 * Return 0 iff refname has the correct format for a refname according
537 * to the rules described in Documentation/git-check-ref-format.txt.
538 * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level
539 * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then
540 * allow a single "*" wildcard character in the refspec. No leading or
541 * repeated slashes are accepted.
543 int check_refname_format(const char *refname, int flags);
546 * Check the reference database for consistency. Return 0 if refs and
547 * reflogs are consistent, and non-zero otherwise. The errors will be
548 * written to stderr.
550 int refs_fsck(struct ref_store *refs, struct fsck_options *o);
553 * Apply the rules from check_refname_format, but mutate the result until it
554 * is acceptable, and place the result in "out".
556 void sanitize_refname_component(const char *refname, struct strbuf *out);
558 const char *prettify_refname(const char *refname);
560 char *refs_shorten_unambiguous_ref(struct ref_store *refs,
561 const char *refname, int strict);
563 /** rename ref, return 0 on success **/
564 int refs_rename_ref(struct ref_store *refs, const char *oldref,
565 const char *newref, const char *logmsg);
567 /** copy ref, return 0 on success **/
568 int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
569 const char *newref, const char *logmsg);
571 int refs_update_symref(struct ref_store *refs, const char *refname,
572 const char *target, const char *logmsg);
574 enum action_on_err {
575 UPDATE_REFS_MSG_ON_ERR,
576 UPDATE_REFS_DIE_ON_ERR,
577 UPDATE_REFS_QUIET_ON_ERR
581 * Begin a reference transaction. The reference transaction must
582 * be freed by calling ref_transaction_free().
584 struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
585 struct strbuf *err);
588 * Reference transaction updates
590 * The following four functions add a reference check or update to a
591 * ref_transaction. They have some common similar parameters:
593 * transaction -- a pointer to an open ref_transaction, obtained
594 * from ref_transaction_begin().
596 * refname -- the name of the reference to be affected.
598 * new_oid -- the object ID that should be set to be the new value
599 * of the reference. Some functions allow this parameter to be
600 * NULL, meaning that the reference is not changed, or
601 * null_oid, meaning that the reference should be deleted. A
602 * copy of this value is made in the transaction.
604 * old_oid -- the object ID that the reference must have before
605 * the update. Some functions allow this parameter to be NULL,
606 * meaning that the old value of the reference is not checked,
607 * or null_oid, meaning that the reference must not exist
608 * before the update. A copy of this value is made in the
609 * transaction.
611 * new_target -- the target reference that the reference will be
612 * updated to point to. If the reference is a regular reference,
613 * it will be converted to a symbolic reference. Cannot be set
614 * together with `new_oid`. A copy of this value is made in the
615 * transaction.
617 * old_target -- the reference that the reference must be pointing to.
618 * Canont be set together with `old_oid`. A copy of this value is
619 * made in the transaction.
621 * flags -- flags affecting the update, passed to
622 * update_ref_lock(). Possible flags: REF_NO_DEREF,
623 * REF_FORCE_CREATE_REFLOG. See those constants for more
624 * information.
626 * msg -- a message describing the change (for the reflog).
628 * err -- a strbuf for receiving a description of any error that
629 * might have occurred.
631 * The functions make internal copies of refname and msg, so the
632 * caller retains ownership of these parameters.
634 * The functions return 0 on success and non-zero on failure. A
635 * failure means that the transaction as a whole has failed and needs
636 * to be rolled back.
640 * The following flags can be passed to ref_transaction_update() etc.
641 * Internally, they are stored in `ref_update::flags`, along with some
642 * internal flags.
646 * Act on the ref directly; i.e., without dereferencing symbolic refs.
647 * If this flag is not specified, then symbolic references are
648 * dereferenced and the update is applied to the referent.
650 #define REF_NO_DEREF (1 << 0)
653 * Force the creation of a reflog for this reference, even if it
654 * didn't previously have a reflog.
656 #define REF_FORCE_CREATE_REFLOG (1 << 1)
659 * Blindly write an object_id. This is useful for testing data corruption
660 * scenarios.
662 #define REF_SKIP_OID_VERIFICATION (1 << 10)
665 * Skip verifying refname. This is useful for testing data corruption scenarios.
667 #define REF_SKIP_REFNAME_VERIFICATION (1 << 11)
670 * Skip creation of a reflog entry, even if it would have otherwise been
671 * created.
673 #define REF_SKIP_CREATE_REFLOG (1 << 12)
676 * Bitmask of all of the flags that are allowed to be passed in to
677 * ref_transaction_update() and friends:
679 #define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
680 (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION | \
681 REF_SKIP_REFNAME_VERIFICATION | REF_SKIP_CREATE_REFLOG)
684 * Add a reference update to transaction. `new_oid` is the value that
685 * the reference should have after the update, or `null_oid` if it
686 * should be deleted. If `new_oid` is NULL, then the reference is not
687 * changed at all. `old_oid` is the value that the reference must have
688 * before the update, or `null_oid` if it must not have existed
689 * beforehand. The old value is checked after the lock is taken to
690 * prevent races. If the old value doesn't agree with old_oid, the
691 * whole transaction fails. If old_oid is NULL, then the previous
692 * value is not checked. If `old_target` is not NULL, treat the reference
693 * as a symbolic ref and validate that its target before the update is
694 * `old_target`. If the `new_target` is not NULL, then the reference
695 * will be updated to a symbolic ref which targets `new_target`.
696 * Together, these allow us to update between regular refs and symrefs.
698 * See the above comment "Reference transaction updates" for more
699 * information.
701 int ref_transaction_update(struct ref_transaction *transaction,
702 const char *refname,
703 const struct object_id *new_oid,
704 const struct object_id *old_oid,
705 const char *new_target,
706 const char *old_target,
707 unsigned int flags, const char *msg,
708 struct strbuf *err);
711 * Add a reference creation to transaction. new_oid is the value that
712 * the reference should have after the update; it must not be
713 * null_oid. It is verified that the reference does not exist
714 * already.
716 * See the above comment "Reference transaction updates" for more
717 * information.
719 int ref_transaction_create(struct ref_transaction *transaction,
720 const char *refname,
721 const struct object_id *new_oid,
722 const char *new_target,
723 unsigned int flags, const char *msg,
724 struct strbuf *err);
727 * Add a reference deletion to transaction. If old_oid is non-NULL,
728 * then it holds the value that the reference should have had before
729 * the update (which must not be null_oid).
731 * See the above comment "Reference transaction updates" for more
732 * information.
734 int ref_transaction_delete(struct ref_transaction *transaction,
735 const char *refname,
736 const struct object_id *old_oid,
737 const char *old_target,
738 unsigned int flags,
739 const char *msg,
740 struct strbuf *err);
743 * Verify, within a transaction, that refname has the value old_oid,
744 * or, if old_oid is null_oid, then verify that the reference
745 * doesn't exist. old_oid must be non-NULL.
747 * See the above comment "Reference transaction updates" for more
748 * information.
750 int ref_transaction_verify(struct ref_transaction *transaction,
751 const char *refname,
752 const struct object_id *old_oid,
753 const char *old_target,
754 unsigned int flags,
755 struct strbuf *err);
757 /* Naming conflict (for example, the ref names A and A/B conflict). */
758 #define TRANSACTION_NAME_CONFLICT -1
759 /* All other errors. */
760 #define TRANSACTION_GENERIC_ERROR -2
763 * Perform the preparatory stages of committing `transaction`. Acquire
764 * any needed locks, check preconditions, etc.; basically, do as much
765 * as possible to ensure that the transaction will be able to go
766 * through, stopping just short of making any irrevocable or
767 * user-visible changes. The updates that this function prepares can
768 * be finished up by calling `ref_transaction_commit()` or rolled back
769 * by calling `ref_transaction_abort()`.
771 * On success, return 0 and leave the transaction in "prepared" state.
772 * On failure, abort the transaction, write an error message to `err`,
773 * and return one of the `TRANSACTION_*` constants.
775 * Callers who don't need such fine-grained control over committing
776 * reference transactions should just call `ref_transaction_commit()`.
778 int ref_transaction_prepare(struct ref_transaction *transaction,
779 struct strbuf *err);
782 * Commit all of the changes that have been queued in transaction, as
783 * atomically as possible. On success, return 0 and leave the
784 * transaction in "closed" state. On failure, roll back the
785 * transaction, write an error message to `err`, and return one of the
786 * `TRANSACTION_*` constants
788 int ref_transaction_commit(struct ref_transaction *transaction,
789 struct strbuf *err);
792 * Abort `transaction`, which has been begun and possibly prepared,
793 * but not yet committed.
795 int ref_transaction_abort(struct ref_transaction *transaction,
796 struct strbuf *err);
799 * Like ref_transaction_commit(), but optimized for creating
800 * references when originally initializing a repository (e.g., by "git
801 * clone"). It writes the new references directly to packed-refs
802 * without locking the individual references.
804 * It is a bug to call this function when there might be other
805 * processes accessing the repository or if there are existing
806 * references that might conflict with the ones being created. All
807 * old_oid values must either be absent or null_oid.
809 int initial_ref_transaction_commit(struct ref_transaction *transaction,
810 struct strbuf *err);
813 * Execute the given callback function for each of the reference updates which
814 * have been queued in the given transaction. `old_oid` and `new_oid` may be
815 * `NULL` pointers depending on whether the update has these object IDs set or
816 * not.
818 typedef void ref_transaction_for_each_queued_update_fn(const char *refname,
819 const struct object_id *old_oid,
820 const struct object_id *new_oid,
821 void *cb_data);
822 void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
823 ref_transaction_for_each_queued_update_fn cb,
824 void *cb_data);
827 * Free `*transaction` and all associated data.
829 void ref_transaction_free(struct ref_transaction *transaction);
832 * Lock, update, and unlock a single reference. This function
833 * basically does a transaction containing a single call to
834 * ref_transaction_update(). The parameters to this function have the
835 * same meaning as the corresponding parameters to
836 * ref_transaction_update(). Handle errors as requested by the `onerr`
837 * argument.
839 int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname,
840 const struct object_id *new_oid, const struct object_id *old_oid,
841 unsigned int flags, enum action_on_err onerr);
843 int parse_hide_refs_config(const char *var, const char *value, const char *,
844 struct strvec *);
847 * Check whether a ref is hidden. If no namespace is set, both the first and
848 * the second parameter point to the full ref name. If a namespace is set and
849 * the ref is inside that namespace, the first parameter is a pointer to the
850 * name of the ref with the namespace prefix removed. If a namespace is set and
851 * the ref is outside that namespace, the first parameter is NULL. The second
852 * parameter always points to the full ref name.
854 int ref_is_hidden(const char *, const char *, const struct strvec *);
857 * Returns an array of patterns to use as excluded_patterns, if none of the
858 * hidden references use the token '!' or '^'.
860 const char **hidden_refs_to_excludes(const struct strvec *hide_refs);
862 /* Is this a per-worktree ref living in the refs/ namespace? */
863 int is_per_worktree_ref(const char *refname);
865 /* Describes how a refname relates to worktrees */
866 enum ref_worktree_type {
867 REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or
868 refs/bisect/SOMETHING */
869 REF_WORKTREE_MAIN, /* explicitly in main worktree, eg.
870 main-worktree/HEAD */
871 REF_WORKTREE_OTHER, /* explicitly in named worktree, eg.
872 worktrees/bla/HEAD */
873 REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */
877 * Parse a `maybe_worktree_ref` as a ref that possibly refers to a worktree ref
878 * (ie. either REFNAME, main-worktree/REFNAME or worktree/WORKTREE/REFNAME). It
879 * returns what kind of ref was found, and in case of REF_WORKTREE_OTHER, the
880 * worktree name is returned in `worktree_name` (pointing into
881 * `maybe_worktree_ref`) and `worktree_name_length`. The bare refname (the
882 * refname stripped of prefixes) is returned in `bare_refname`. The
883 * `worktree_name`, `worktree_name_length` and `bare_refname` arguments may be
884 * NULL.
886 enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
887 const char **worktree_name,
888 int *worktree_name_length,
889 const char **bare_refname);
891 enum expire_reflog_flags {
892 EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
893 EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
894 EXPIRE_REFLOGS_REWRITE = 1 << 2,
898 * The following interface is used for reflog expiration. The caller
899 * calls refs_reflog_expire(), supplying it with three callback functions,
900 * of the following types. The callback functions define the
901 * expiration policy that is desired.
903 * reflog_expiry_prepare_fn -- Called once after the reference is
904 * locked. Called with the OID of the locked reference.
906 * reflog_expiry_should_prune_fn -- Called once for each entry in the
907 * existing reflog. It should return true iff that entry should be
908 * pruned.
910 * reflog_expiry_cleanup_fn -- Called once before the reference is
911 * unlocked again.
913 typedef void reflog_expiry_prepare_fn(const char *refname,
914 const struct object_id *oid,
915 void *cb_data);
916 typedef int reflog_expiry_should_prune_fn(struct object_id *ooid,
917 struct object_id *noid,
918 const char *email,
919 timestamp_t timestamp, int tz,
920 const char *message, void *cb_data);
921 typedef void reflog_expiry_cleanup_fn(void *cb_data);
924 * Expire reflog entries for the specified reference.
925 * flags is a combination of the constants in
926 * enum expire_reflog_flags. The three function pointers are described
927 * above. On success, return zero.
929 int refs_reflog_expire(struct ref_store *refs,
930 const char *refname,
931 unsigned int flags,
932 reflog_expiry_prepare_fn prepare_fn,
933 reflog_expiry_should_prune_fn should_prune_fn,
934 reflog_expiry_cleanup_fn cleanup_fn,
935 void *policy_cb_data);
937 struct ref_store *get_main_ref_store(struct repository *r);
940 * Submodules
941 * ----------
943 * If you want to iterate the refs of a submodule you first need to add the
944 * submodules object database. You can do this by a code-snippet like
945 * this:
947 * const char *path = "path/to/submodule"
948 * if (add_submodule_odb(path))
949 * die("Error submodule '%s' not populated.", path);
951 * `add_submodule_odb()` will return zero on success. If you
952 * do not do this you will get an error for each ref that it does not point
953 * to a valid object.
955 * Note: As a side-effect of this you cannot safely assume that all
956 * objects you lookup are available in superproject. All submodule objects
957 * will be available the same way as the superprojects objects.
959 * Example:
960 * --------
962 * ----
963 * static int handle_remote_ref(const char *refname,
964 * const unsigned char *sha1, int flags, void *cb_data)
966 * struct strbuf *output = cb_data;
967 * strbuf_addf(output, "%s\n", refname);
968 * return 0;
974 * Return the ref_store instance for the specified submodule. For the
975 * main repository, use submodule==NULL; such a call cannot fail. For
976 * a submodule, the submodule must exist and be a nonbare repository,
977 * otherwise return NULL. If the requested reference store has not yet
978 * been initialized, initialize it first.
980 * For backwards compatibility, submodule=="" is treated the same as
981 * submodule==NULL.
983 struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
984 const char *submodule);
985 struct ref_store *get_worktree_ref_store(const struct worktree *wt);
988 * Some of the names specified by refs have special meaning to Git.
989 * Organize these namespaces in a comon 'ref_namespace' array for
990 * reference from multiple places in the codebase.
993 struct ref_namespace_info {
994 const char *ref;
995 enum decoration_type decoration;
998 * If 'exact' is true, then we must match the 'ref' exactly.
999 * Otherwise, use a prefix match.
1001 * 'ref_updated' is for internal use. It represents whether the
1002 * 'ref' value was replaced from its original literal version.
1004 unsigned exact:1,
1005 ref_updated:1;
1008 enum ref_namespace {
1009 NAMESPACE_HEAD,
1010 NAMESPACE_BRANCHES,
1011 NAMESPACE_TAGS,
1012 NAMESPACE_REMOTE_REFS,
1013 NAMESPACE_STASH,
1014 NAMESPACE_REPLACE,
1015 NAMESPACE_NOTES,
1016 NAMESPACE_PREFETCH,
1017 NAMESPACE_REWRITTEN,
1019 /* Must be last */
1020 NAMESPACE__COUNT
1023 /* See refs.c for the contents of this array. */
1024 extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT];
1027 * Some ref namespaces can be modified by config values or environment
1028 * variables. Modify a namespace as specified by its ref_namespace key.
1030 void update_ref_namespace(enum ref_namespace namespace, char *ref);
1033 * Check whether the provided name names a root reference. This function only
1034 * performs a syntactic check.
1036 * A root ref is a reference that lives in the root of the reference hierarchy.
1037 * These references must conform to special syntax:
1039 * - Their name must be all-uppercase or underscores ("_").
1041 * - Their name must end with "_HEAD". As a special rule, "HEAD" is a root
1042 * ref, as well.
1044 * - Their name may not contain a slash.
1046 * There is a special set of irregular root refs that exist due to historic
1047 * reasons, only. This list shall not be expanded in the future:
1049 * - AUTO_MERGE
1051 * - BISECT_EXPECTED_REV
1053 * - NOTES_MERGE_PARTIAL
1055 * - NOTES_MERGE_REF
1057 * - MERGE_AUTOSTASH
1059 int is_root_ref(const char *refname);
1062 * Pseudorefs are refs that have different semantics compared to
1063 * "normal" refs. These refs can thus not be stored in the ref backend,
1064 * but must always be accessed via the filesystem. The following refs
1065 * are pseudorefs:
1067 * - FETCH_HEAD may contain multiple object IDs, and each one of them
1068 * carries additional metadata like where it came from.
1070 * - MERGE_HEAD may contain multiple object IDs when merging multiple
1071 * heads.
1073 * Reading, writing or deleting references must consistently go either
1074 * through the filesystem (pseudorefs) or through the reference
1075 * backend (normal ones).
1077 int is_pseudo_ref(const char *refname);
1080 * The following flags can be passed to `repo_migrate_ref_storage_format()`:
1082 * - REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN: perform a dry-run migration
1083 * without touching the main repository. The result will be written into a
1084 * temporary ref storage directory.
1086 #define REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN (1 << 0)
1089 * Migrate the ref storage format used by the repository to the
1090 * specified one.
1092 int repo_migrate_ref_storage_format(struct repository *repo,
1093 enum ref_storage_format format,
1094 unsigned int flags,
1095 struct strbuf *err);
1097 #endif /* REFS_H */