blob: 123cfa4424453fbda6503dbbb66d28f092815154 [file] [log] [blame]
Daniel Barkalow95fc7512005-06-06 16:31:29 -04001#ifndef REFS_H
2#define REFS_H
3
Derrick Stoleeb9342b32022-08-05 17:58:36 +00004#include "commit.h"
Jonathan Tanec06b052020-09-01 15:28:08 -07005
Nguyễn Thái Ngọc Duy504c4d42017-03-18 09:03:11 +07006struct object_id;
Nguyễn Thái Ngọc Duy077be782017-03-26 09:42:29 +07007struct ref_store;
Elijah Newrenef3ca952018-08-15 10:54:05 -07008struct repository;
Nguyễn Thái Ngọc Duy504c4d42017-03-18 09:03:11 +07009struct strbuf;
10struct string_list;
Elijah Newrenef3ca952018-08-15 10:54:05 -070011struct string_list_item;
Nguyễn Thái Ngọc Duy17eff962017-04-24 17:01:22 +070012struct worktree;
Nguyễn Thái Ngọc Duy504c4d42017-03-18 09:03:11 +070013
Ronnie Sahlbergb416af52014-04-16 15:26:44 -070014/*
Michael Haggertyfb58c8d2015-06-22 16:03:05 +020015 * Resolve a reference, recursively following symbolic refererences.
16 *
René Scharfe54fad662017-09-23 11:41:45 +020017 * Return the name of the non-symbolic reference that ultimately pointed
18 * at the resolved object name. The return value, if not NULL, is a
19 * pointer into either a static buffer or the input ref.
20 *
brian m. carlson49e61472017-10-15 22:07:09 +000021 * If oid is non-NULL, store the referred-to object's name in it.
Michael Haggertyfb58c8d2015-06-22 16:03:05 +020022 *
23 * If the reference cannot be resolved to an object, the behavior
24 * depends on the RESOLVE_REF_READING flag:
25 *
26 * - If RESOLVE_REF_READING is set, return NULL.
27 *
brian m. carlson49e61472017-10-15 22:07:09 +000028 * - If RESOLVE_REF_READING is not set, clear oid and return the name of
Michael Haggertyfb58c8d2015-06-22 16:03:05 +020029 * the last reference name in the chain, which will either be a non-symbolic
30 * reference or an undefined reference. If this is a prelude to
31 * "writing" to the ref, the return value is the name of the ref
32 * that will actually be created or changed.
33 *
34 * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one
brian m. carlson49e61472017-10-15 22:07:09 +000035 * level of symbolic reference. The value stored in oid for a symbolic
36 * reference will always be null_oid in this case, and the return
Michael Haggertyfb58c8d2015-06-22 16:03:05 +020037 * value is the reference that the symref refers to directly.
38 *
39 * If flags is non-NULL, set the value that it points to the
40 * combination of REF_ISPACKED (if the reference was found among the
41 * packed references), REF_ISSYMREF (if the initial reference was a
42 * symbolic reference), REF_BAD_NAME (if the reference name is ill
43 * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN
44 * (if the ref is malformed or has a bad name). See refs.h for more detail
45 * on each flag.
46 *
47 * If ref is not a properly-formatted, normalized reference, return
48 * NULL. If more than MAXDEPTH recursive symbolic lookups are needed,
49 * give up and return NULL.
50 *
51 * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their
52 * name is invalid according to git-check-ref-format(1). If the name
brian m. carlson49e61472017-10-15 22:07:09 +000053 * is bad then the value stored in oid will be null_oid and the two
Michael Haggertyfb58c8d2015-06-22 16:03:05 +020054 * flags REF_ISBROKEN and REF_BAD_NAME will be set.
55 *
56 * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/
57 * directory and do not consist of all caps and underscores cannot be
58 * resolved. The function returns NULL for such ref names.
59 * Caps and underscores refers to the special refs, such as HEAD,
60 * FETCH_HEAD and friends, that all live outside of the refs/ directory.
61 */
62#define RESOLVE_REF_READING 0x01
63#define RESOLVE_REF_NO_RECURSE 0x02
64#define RESOLVE_REF_ALLOW_BAD_NAME 0x04
65
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +070066const char *refs_resolve_ref_unsafe(struct ref_store *refs,
67 const char *refname,
68 int resolve_flags,
brian m. carlson49e61472017-10-15 22:07:09 +000069 struct object_id *oid,
Ævar Arnfjörð Bjarmasonce14de02022-01-26 15:37:01 +010070 int *flags);
Ævar Arnfjörð Bjarmason25a33b32021-10-16 11:39:26 +020071
Michael Haggerty1354c9b2016-03-31 06:19:22 +020072const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
brian m. carlson49e61472017-10-15 22:07:09 +000073 struct object_id *oid, int *flags);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +020074
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +070075char *refs_resolve_refdup(struct ref_store *refs,
76 const char *refname, int resolve_flags,
brian m. carlson0f2dc722017-10-15 22:06:55 +000077 struct object_id *oid, int *flags);
Michael Haggerty1354c9b2016-03-31 06:19:22 +020078char *resolve_refdup(const char *refname, int resolve_flags,
brian m. carlson0f2dc722017-10-15 22:06:55 +000079 struct object_id *oid, int *flags);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +020080
Michael Haggerty1354c9b2016-03-31 06:19:22 +020081int read_ref_full(const char *refname, int resolve_flags,
brian m. carlson34c290a2017-10-15 22:06:56 +000082 struct object_id *oid, int *flags);
83int read_ref(const char *refname, struct object_id *oid);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +020084
Patrick Steinhardtcd475b32022-03-01 10:33:46 +010085int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
86 struct strbuf *referent);
87
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +070088/*
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.
97 *
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".
102 *
103 * extras and skip must be sorted.
104 */
105
106int refs_verify_refname_available(struct ref_store *refs,
107 const char *refname,
Michael Haggertyb05855b2017-04-16 08:41:26 +0200108 const struct string_list *extras,
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700109 const struct string_list *skip,
110 struct strbuf *err);
111
Han-Wen Nienhuys3f9f1ac2020-08-21 16:59:34 +0000112int refs_ref_exists(struct ref_store *refs, const char *refname);
113
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200114int ref_exists(const char *refname);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200115
Cornelius Weig341fb282017-01-27 11:09:47 +0100116int should_autocreate_reflog(const char *refname);
117
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200118int is_branch(const char *refname);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200119
Denton Liu55454422019-04-29 04:28:14 -0400120int refs_init_db(struct strbuf *err);
David Turner6fb5acf2016-09-04 18:08:41 +0200121
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200122/*
Jeff King36a31792021-01-20 14:44:43 -0500123 * Return the peeled value of the oid currently being iterated via
124 * for_each_ref(), etc. This is equivalent to calling:
125 *
126 * peel_object(oid, &peeled);
127 *
128 * with the "oid" value given to the each_ref_fn callback, except
129 * that some ref storage may be able to answer the query without
130 * actually loading the object in memory.
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200131 */
Jeff King36a31792021-01-20 14:44:43 -0500132int peel_iterated_oid(const struct object_id *base, struct object_id *peeled);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200133
134/**
Michael Haggertya8355bb2016-09-04 18:08:24 +0200135 * Resolve refname in the nested "gitlink" repository in the specified
136 * submodule (which must be non-NULL). If the resolution is
Michael Haggerty78fb4572017-11-05 09:42:09 +0100137 * successful, return 0 and set oid to the name of the object;
Michael Haggertya8355bb2016-09-04 18:08:24 +0200138 * otherwise, return a non-zero value.
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200139 */
Michael Haggertya8355bb2016-09-04 18:08:24 +0200140int resolve_gitlink_ref(const char *submodule, const char *refname,
brian m. carlsona98e6102017-10-15 22:07:07 +0000141 struct object_id *oid);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200142
143/*
144 * Return true iff abbrev_name is a possible abbreviation for
145 * full_name according to the rules defined by ref_rev_parse_rules in
146 * refs.c.
147 */
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200148int refname_match(const char *abbrev_name, const char *full_name);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200149
Brandon Williamsb4be7412018-03-15 10:31:24 -0700150/*
151 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
152 * the results to 'prefixes'
153 */
Jeff King873cd282020-07-28 16:23:25 -0400154struct strvec;
155void expand_ref_prefix(struct strvec *prefixes, const char *prefix);
Brandon Williamsb4be7412018-03-15 10:31:24 -0700156
Nguyễn Thái Ngọc Duy0b1dbf52019-04-06 18:34:27 +0700157int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
Jonathan Tanf24c30e2020-09-01 15:28:09 -0700158int repo_dwim_ref(struct repository *r, const char *str, int len,
159 struct object_id *oid, char **ref, int nonfatal_dangling_mark);
Nguyễn Thái Ngọc Duy56700902019-04-06 18:34:29 +0700160int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
brian m. carlson334dc522017-10-15 22:06:59 +0000161int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200162
163/*
Don Goodman-Wilson8747ebb2020-06-24 14:46:33 +0000164 * Retrieves the default branch name for newly-initialized repositories.
165 *
166 * The return value of `repo_default_branch_name()` is an allocated string. The
167 * return value of `git_default_branch_name()` is a singleton.
168 */
Johannes Schindelincc0f13c2020-12-11 11:36:56 +0000169const char *git_default_branch_name(int quiet);
170char *repo_default_branch_name(struct repository *r, int quiet);
Don Goodman-Wilson8747ebb2020-06-24 14:46:33 +0000171
172/*
Michael Haggerty30173b82017-05-22 16:17:44 +0200173 * A ref_transaction represents a collection of reference updates that
174 * should succeed or fail together.
Ronnie Sahlbergb416af52014-04-16 15:26:44 -0700175 *
176 * Calling sequence
177 * ----------------
Michael Haggerty30173b82017-05-22 16:17:44 +0200178 *
Ronnie Sahlbergb416af52014-04-16 15:26:44 -0700179 * - Allocate and initialize a `struct ref_transaction` by calling
180 * `ref_transaction_begin()`.
181 *
Michael Haggerty30173b82017-05-22 16:17:44 +0200182 * - Specify the intended ref updates by calling one or more of the
183 * following functions:
184 * - `ref_transaction_update()`
185 * - `ref_transaction_create()`
186 * - `ref_transaction_delete()`
187 * - `ref_transaction_verify()`
Ronnie Sahlbergb416af52014-04-16 15:26:44 -0700188 *
Michael Haggerty30173b82017-05-22 16:17:44 +0200189 * - Then either:
Ronnie Sahlbergb416af52014-04-16 15:26:44 -0700190 *
Michael Haggerty30173b82017-05-22 16:17:44 +0200191 * - Optionally call `ref_transaction_prepare()` to prepare the
192 * transaction. This locks all references, checks preconditions,
193 * etc. but doesn't finalize anything. If this step fails, the
194 * transaction has been closed and can only be freed. If this step
195 * succeeds, then `ref_transaction_commit()` is almost certain to
196 * succeed. However, you can still call `ref_transaction_abort()`
197 * if you decide not to commit the transaction after all.
David Turner49386862016-02-25 15:05:46 -0500198 *
Michael Haggerty30173b82017-05-22 16:17:44 +0200199 * - Call `ref_transaction_commit()` to execute the transaction,
200 * make the changes permanent, and release all locks. If you
201 * haven't already called `ref_transaction_prepare()`, then
202 * `ref_transaction_commit()` calls it for you.
203 *
204 * Or
205 *
206 * - Call `initial_ref_transaction_commit()` if the ref database is
207 * known to be empty and have no other writers (e.g. during
208 * clone). This is likely to be much faster than
209 * `ref_transaction_commit()`. `ref_transaction_prepare()` should
210 * *not* be called before `initial_ref_transaction_commit()`.
211 *
212 * - Then finally, call `ref_transaction_free()` to free the
213 * `ref_transaction` data structure.
214 *
215 * At any time before calling `ref_transaction_commit()`, you can call
216 * `ref_transaction_abort()` to abort the transaction, rollback any
217 * locks, and free any associated resources (including the
218 * `ref_transaction` data structure).
219 *
220 * Putting it all together, a complete reference update looks like
221 *
222 * struct ref_transaction *transaction;
223 * struct strbuf err = STRBUF_INIT;
224 * int ret = 0;
225 *
Junio C Hamanoc6da34a2022-04-13 15:51:33 -0700226 * transaction = ref_store_transaction_begin(refs, &err);
Michael Haggerty30173b82017-05-22 16:17:44 +0200227 * if (!transaction ||
228 * ref_transaction_update(...) ||
229 * ref_transaction_create(...) ||
230 * ...etc... ||
231 * ref_transaction_commit(transaction, &err)) {
232 * error("%s", err.buf);
233 * ret = -1;
234 * }
235 * ref_transaction_free(transaction);
236 * strbuf_release(&err);
237 * return ret;
Ronnie Sahlbergb416af52014-04-16 15:26:44 -0700238 *
239 * Error handling
240 * --------------
241 *
242 * On error, transaction functions append a message about what
243 * went wrong to the 'err' argument. The message mentions what
244 * ref was being updated (if any) when the error occurred so it
245 * can be passed to 'die' or 'error' as-is.
246 *
247 * The message is appended to err without first clearing err.
248 * err will not be '\n' terminated.
David Turner49386862016-02-25 15:05:46 -0500249 *
250 * Caveats
251 * -------
252 *
253 * Note that no locks are taken, and no refs are read, until
Michael Haggerty30173b82017-05-22 16:17:44 +0200254 * `ref_transaction_prepare()` or `ref_transaction_commit()` is
255 * called. So, for example, `ref_transaction_verify()` won't report a
256 * verification failure until the commit is attempted.
Ronnie Sahlbergb416af52014-04-16 15:26:44 -0700257 */
Michael Haggertycaa40462014-04-07 15:48:10 +0200258struct ref_transaction;
259
Michael Haggerty89df9c82013-04-14 14:54:16 +0200260/*
Michael Haggerty3bc581b2016-06-18 06:15:15 +0200261 * Bit values set in the flags argument passed to each_ref_fn() and
262 * stored in ref_iterator::flags. Other bits are for internal use
263 * only:
Michael Haggerty89df9c82013-04-14 14:54:16 +0200264 */
265
266/* Reference is a symbolic reference. */
Junio C Hamano98ac34b2011-10-19 13:45:50 -0700267#define REF_ISSYMREF 0x01
Michael Haggerty89df9c82013-04-14 14:54:16 +0200268
269/* Reference is a packed reference. */
Junio C Hamano98ac34b2011-10-19 13:45:50 -0700270#define REF_ISPACKED 0x02
Michael Haggerty89df9c82013-04-14 14:54:16 +0200271
272/*
273 * Reference cannot be resolved to an object name: dangling symbolic
Ronnie Sahlbergd0f810f2014-09-03 11:45:43 -0700274 * reference (directly or indirectly), corrupt reference file,
275 * reference exists but name is bad, or symbolic reference refers to
276 * ill-formatted reference name.
Michael Haggerty89df9c82013-04-14 14:54:16 +0200277 */
Junio C Hamano98ac34b2011-10-19 13:45:50 -0700278#define REF_ISBROKEN 0x04
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800279
Linus Torvalds8a65ff72005-07-02 20:23:36 -0700280/*
Ronnie Sahlbergd0f810f2014-09-03 11:45:43 -0700281 * Reference name is not well formed.
282 *
283 * See git-check-ref-format(1) for the definition of well formed ref names.
284 */
285#define REF_BAD_NAME 0x08
286
287/*
Michael Haggerty4f78c242013-05-25 11:08:24 +0200288 * The signature for the callback function for the for_each_*()
Michael Haggerty78fb4572017-11-05 09:42:09 +0100289 * functions below. The memory pointed to by the refname and oid
Michael Haggerty4f78c242013-05-25 11:08:24 +0200290 * arguments is only guaranteed to be valid for the duration of a
291 * single callback invocation.
Linus Torvalds8a65ff72005-07-02 20:23:36 -0700292 */
Michael Haggerty4f78c242013-05-25 11:08:24 +0200293typedef int each_ref_fn(const char *refname,
Michael Haggerty2b2a5be2015-05-25 18:38:28 +0000294 const struct object_id *oid, int flags, void *cb_data);
295
Michael Haggerty4f78c242013-05-25 11:08:24 +0200296/*
Stefan Beller4a6067c2018-08-20 18:24:16 +0000297 * The same as each_ref_fn, but also with a repository argument that
298 * contains the repository associated with the callback.
299 */
300typedef int each_repo_ref_fn(struct repository *r,
301 const char *refname,
302 const struct object_id *oid,
303 int flags,
304 void *cb_data);
305
306/*
Michael Haggerty4f78c242013-05-25 11:08:24 +0200307 * The following functions invoke the specified callback function for
308 * each reference indicated. If the function ever returns a nonzero
309 * value, stop the iteration and return that value. Please note that
310 * it is not safe to modify references while an iteration is in
311 * progress, unless the same callback function invocation that
312 * modifies the reference also returns a nonzero value to immediately
Nguyễn Thái Ngọc Duyadac8112017-03-26 09:42:41 +0700313 * stop the iteration. Returned references are sorted.
Michael Haggerty4f78c242013-05-25 11:08:24 +0200314 */
Nguyễn Thái Ngọc Duy62f0b392017-08-23 19:36:55 +0700315int refs_head_ref(struct ref_store *refs,
316 each_ref_fn fn, void *cb_data);
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700317int refs_for_each_ref(struct ref_store *refs,
318 each_ref_fn fn, void *cb_data);
319int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
320 each_ref_fn fn, void *cb_data);
321int refs_for_each_tag_ref(struct ref_store *refs,
322 each_ref_fn fn, void *cb_data);
323int refs_for_each_branch_ref(struct ref_store *refs,
324 each_ref_fn fn, void *cb_data);
325int refs_for_each_remote_ref(struct ref_store *refs,
326 each_ref_fn fn, void *cb_data);
327
Heba Waly126c1cc2019-11-17 21:04:46 +0000328/* just iterates the head ref. */
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200329int head_ref(each_ref_fn fn, void *cb_data);
Heba Waly126c1cc2019-11-17 21:04:46 +0000330
331/* iterates all refs. */
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200332int for_each_ref(each_ref_fn fn, void *cb_data);
Heba Waly126c1cc2019-11-17 21:04:46 +0000333
334/**
335 * iterates all refs which have a defined prefix and strips that prefix from
336 * the passed variable refname.
337 */
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200338int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
Heba Waly126c1cc2019-11-17 21:04:46 +0000339
Nguyễn Thái Ngọc Duy073cf632017-08-23 19:36:56 +0700340int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
Jeff King67985e42021-09-24 14:48:48 -0400341 each_ref_fn fn, void *cb_data);
342int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data);
Heba Waly126c1cc2019-11-17 21:04:46 +0000343
344/**
Taylor Blau16b19852021-01-20 11:04:21 -0500345 * iterate all refs in "patterns" by partitioning patterns into disjoint sets
346 * and iterating the longest-common prefix of each set.
347 *
348 * callers should be prepared to ignore references that they did not ask for.
349 */
Jeff King91e2ab12022-12-13 06:11:10 -0500350int refs_for_each_fullref_in_prefixes(struct ref_store *refs,
351 const char *namespace, const char **patterns,
352 each_ref_fn fn, void *cb_data);
353
Taylor Blau16b19852021-01-20 11:04:21 -0500354/**
Heba Waly126c1cc2019-11-17 21:04:46 +0000355 * iterate refs from the respective area.
356 */
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200357int for_each_tag_ref(each_ref_fn fn, void *cb_data);
358int for_each_branch_ref(each_ref_fn fn, void *cb_data);
359int for_each_remote_ref(each_ref_fn fn, void *cb_data);
Stefan Beller212e0f72018-08-20 18:24:19 +0000360int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data);
Heba Waly126c1cc2019-11-17 21:04:46 +0000361
362/* iterates all refs that match the specified glob pattern. */
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200363int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data);
Heba Waly126c1cc2019-11-17 21:04:46 +0000364
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200365int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
366 const char *prefix, void *cb_data);
Linus Torvalds8a65ff72005-07-02 20:23:36 -0700367
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200368int head_ref_namespaced(each_ref_fn fn, void *cb_data);
369int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700370
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200371/* can be used to learn about broken ref and symref */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700372int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data);
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200373int for_each_rawref(each_ref_fn fn, void *cb_data);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200374
Rafael Ascensão65516f52017-11-21 21:33:41 +0000375/*
376 * Normalizes partial refs to their fully qualified form.
377 * Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'.
378 * <prefix> will default to 'refs/' if NULL.
379 *
380 * item.string will be set to the result.
381 * item.util will be set to NULL if <pattern> contains glob characters, or
382 * non-NULL if it doesn't.
383 */
384void normalize_glob_ref(struct string_list_item *item, const char *prefix,
385 const char *pattern);
386
Thomas Rast894a9d32010-03-12 18:04:26 +0100387static inline const char *has_glob_specials(const char *pattern)
388{
389 return strpbrk(pattern, "?*[");
390}
391
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200392void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname);
393void warn_dangling_symrefs(FILE *fp, const char *msg_fmt,
394 const struct string_list *refnames);
Junio C Hamanof8948e22009-02-08 23:27:10 -0800395
Daniel Barkalowe142a3c2008-04-27 13:39:24 -0400396/*
Michael Haggerty32d462c2013-04-22 21:52:32 +0200397 * Flags for controlling behaviour of pack_refs()
398 * PACK_REFS_PRUNE: Prune loose refs after packing
399 * PACK_REFS_ALL: Pack _all_ refs, not just tags and already packed refs
400 */
401#define PACK_REFS_PRUNE 0x0001
402#define PACK_REFS_ALL 0x0002
403
404/*
405 * Write a packed-refs file for the current repository.
406 * flags: Combination of the above PACK_REFS_* flags.
407 */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700408int refs_pack_refs(struct ref_store *refs, unsigned int flags);
Michael Haggerty32d462c2013-04-22 21:52:32 +0200409
Ronnie Sahlberg835e3c92014-06-20 07:42:51 -0700410/*
David Turnera4c653d2015-07-21 17:04:50 -0400411 * Setup reflog before using. Fill in err and return -1 on failure.
Ronnie Sahlbergbd3b02d2014-06-20 07:42:50 -0700412 */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700413int refs_create_reflog(struct ref_store *refs, const char *refname,
Han-Wen Nienhuys7b089122021-11-22 14:19:08 +0000414 struct strbuf *err);
415int safe_create_reflog(const char *refname, struct strbuf *err);
Erick Mattos859c3012010-05-21 21:28:36 -0300416
Shawn Pearced556fae2006-05-17 05:56:09 -0400417/** Reads log for the value of ref during at_time. **/
Nguyễn Thái Ngọc Duy7fdff472019-04-06 18:34:30 +0700418int read_ref_at(struct ref_store *refs,
419 const char *refname, unsigned int flags,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200420 timestamp_t at_time, int cnt,
brian m. carlson8eb36d92017-10-15 22:07:03 +0000421 struct object_id *oid, char **msg,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200422 timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
Shawn Pearced556fae2006-05-17 05:56:09 -0400423
Ronnie Sahlberg4da58832014-05-06 15:45:52 -0700424/** Check if a particular reflog exists */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700425int refs_reflog_exists(struct ref_store *refs, const char *refname);
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200426int reflog_exists(const char *refname);
Ronnie Sahlberg4da58832014-05-06 15:45:52 -0700427
Michael Haggertyfc1c2162015-06-22 16:02:52 +0200428/*
brian m. carlson2616a5e2017-10-15 22:06:50 +0000429 * Delete the specified reference. If old_oid is non-NULL, then
Michael Haggerty78fb4572017-11-05 09:42:09 +0100430 * verify that the current value of the reference is old_oid before
brian m. carlson2616a5e2017-10-15 22:06:50 +0000431 * deleting it. If old_oid is NULL, delete the reference if it
432 * exists, regardless of its old value. It is an error for old_oid to
433 * be null_oid. msg and flags are passed through to
Michael Haggerty64da4192017-05-22 16:17:38 +0200434 * ref_transaction_delete().
Michael Haggertyfc1c2162015-06-22 16:02:52 +0200435 */
Nguyễn Thái Ngọc Duyc0fe4e82017-03-26 09:42:35 +0700436int refs_delete_ref(struct ref_store *refs, const char *msg,
437 const char *refname,
brian m. carlson2616a5e2017-10-15 22:06:50 +0000438 const struct object_id *old_oid,
Nguyễn Thái Ngọc Duyc0fe4e82017-03-26 09:42:35 +0700439 unsigned int flags);
Kyle Meyer755b49a2017-02-20 20:10:32 -0500440int delete_ref(const char *msg, const char *refname,
brian m. carlson2616a5e2017-10-15 22:06:50 +0000441 const struct object_id *old_oid, unsigned int flags);
Michael Haggertyfc1c2162015-06-22 16:02:52 +0200442
Michael Haggerty98ffd5f2015-06-22 16:02:55 +0200443/*
444 * Delete the specified references. If there are any problems, emit
445 * errors but attempt to keep going (i.e., the deletes are not done in
Michael Haggerty64da4192017-05-22 16:17:38 +0200446 * an all-or-nothing transaction). msg and flags are passed through to
Michael Haggertyc5f04dd2016-06-18 06:15:10 +0200447 * ref_transaction_delete().
Michael Haggerty98ffd5f2015-06-22 16:02:55 +0200448 */
Michael Haggerty64da4192017-05-22 16:17:38 +0200449int refs_delete_refs(struct ref_store *refs, const char *msg,
450 struct string_list *refnames, unsigned int flags);
451int delete_refs(const char *msg, struct string_list *refnames,
452 unsigned int flags);
Michael Haggerty98ffd5f2015-06-22 16:02:55 +0200453
Ronnie Sahlberg4da58832014-05-06 15:45:52 -0700454/** Delete a reflog */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700455int refs_delete_reflog(struct ref_store *refs, const char *refname);
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200456int delete_reflog(const char *refname);
Ronnie Sahlberg4da58832014-05-06 15:45:52 -0700457
Han-Wen Nienhuysd1eb22d2020-05-20 17:36:07 +0000458/*
459 * Callback to process a reflog entry found by the iteration functions (see
Junio C Hamanoe6e94f32021-11-28 11:25:35 -0800460 * below).
461 *
462 * The committer parameter is a single string, in the form
463 * "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" (without double quotes).
464 *
465 * The timestamp parameter gives the time when entry was created as the number
466 * of seconds since the UNIX epoch.
467 *
468 * The tz parameter gives the timezone offset for the user who created
469 * the reflog entry, and its value gives a positive or negative offset
470 * from UTC. Its absolute value is formed by multiplying the hour
471 * part by 100 and adding the minute part. For example, 1 hour ahead
472 * of UTC, CET == "+0100", is represented as positive one hundred (not
473 * postiive sixty).
474 *
475 * The msg parameter is a single complete line; a reflog message given
476 * to refs_delete_ref, refs_update_ref, etc. is returned to the
477 * callback normalized---each run of whitespaces are squashed into a
478 * single whitespace, trailing whitespace, if exists, is trimmed, and
479 * then a single LF is added at the end.
480 *
481 * The cb_data is a caller-supplied pointer given to the iterator
482 * functions.
Han-Wen Nienhuysd1eb22d2020-05-20 17:36:07 +0000483 */
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200484typedef int each_reflog_ent_fn(
brian m. carlson9461d272017-02-21 23:47:32 +0000485 struct object_id *old_oid, struct object_id *new_oid,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200486 const char *committer, timestamp_t timestamp,
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200487 int tz, const char *msg, void *cb_data);
488
Han-Wen Nienhuysd1eb22d2020-05-20 17:36:07 +0000489/* Iterate over reflog entries in the log for `refname`. */
490
491/* oldest entry first */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700492int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
493 each_reflog_ent_fn fn, void *cb_data);
Han-Wen Nienhuysd1eb22d2020-05-20 17:36:07 +0000494
495/* youngest entry first */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700496int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
497 const char *refname,
498 each_reflog_ent_fn fn,
499 void *cb_data);
Han-Wen Nienhuysd1eb22d2020-05-20 17:36:07 +0000500
501/*
502 * Iterate over reflog entries in the log for `refname` in the main ref store.
503 */
504
505/* oldest entry first */
Michael Haggertydfefa932011-12-12 06:38:09 +0100506int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data);
Han-Wen Nienhuysd1eb22d2020-05-20 17:36:07 +0000507
508/* youngest entry first */
Junio C Hamano98f85ff2013-03-08 13:27:37 -0800509int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data);
Junio C Hamano2ff81662006-12-18 01:18:16 -0800510
Nicolas Pitreeb8381c2007-02-03 13:25:43 -0500511/*
512 * Calls the specified function for each reflog file until it returns nonzero,
Nguyễn Thái Ngọc Duyadac8112017-03-26 09:42:41 +0700513 * and returns the value. Reflog file order is unspecified.
Nicolas Pitreeb8381c2007-02-03 13:25:43 -0500514 */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700515int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data);
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200516int for_each_reflog(each_ref_fn fn, void *cb_data);
Nicolas Pitreeb8381c2007-02-03 13:25:43 -0500517
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200518#define REFNAME_ALLOW_ONELEVEL 1
519#define REFNAME_REFSPEC_PATTERN 2
520
521/*
Michael Haggertydfefa932011-12-12 06:38:09 +0100522 * Return 0 iff refname has the correct format for a refname according
523 * to the rules described in Documentation/git-check-ref-format.txt.
524 * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200525 * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then
Jacob Kellercd377f42015-07-22 14:05:33 -0700526 * allow a single "*" wildcard character in the refspec. No leading or
527 * repeated slashes are accepted.
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200528 */
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200529int check_refname_format(const char *refname, int flags);
Daniel Barkalow95fc7512005-06-06 16:31:29 -0400530
Nguyễn Thái Ngọc Duy1de16ae2019-03-08 16:28:34 +0700531/*
532 * Apply the rules from check_refname_format, but mutate the result until it
533 * is acceptable, and place the result in "out".
534 */
535void sanitize_refname_component(const char *refname, struct strbuf *out);
536
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200537const char *prettify_refname(const char *refname);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200538
Nguyễn Thái Ngọc Duy546edf32019-04-06 18:34:25 +0700539char *refs_shorten_unambiguous_ref(struct ref_store *refs,
540 const char *refname, int strict);
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200541char *shorten_unambiguous_ref(const char *refname, int strict);
Daniel Barkalowa9c37a72009-03-08 21:06:05 -0400542
Lars Hjemlic976d412006-11-28 15:47:40 +0100543/** rename ref, return 0 on success **/
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700544int refs_rename_ref(struct ref_store *refs, const char *oldref,
545 const char *newref, const char *logmsg);
Sahil Dua52d59cc2017-06-18 23:19:16 +0200546int rename_ref(const char *oldref, const char *newref,
547 const char *logmsg);
548
549/** copy ref, return 0 on success **/
550int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
551 const char *newref, const char *logmsg);
552int copy_existing_ref(const char *oldref, const char *newref,
553 const char *logmsg);
Lars Hjemlic976d412006-11-28 15:47:40 +0100554
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700555int refs_create_symref(struct ref_store *refs, const char *refname,
556 const char *target, const char *logmsg);
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200557int create_symref(const char *refname, const char *target, const char *logmsg);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700558
Michael Haggertyf4124112014-04-07 15:47:56 +0200559enum action_on_err {
560 UPDATE_REFS_MSG_ON_ERR,
561 UPDATE_REFS_DIE_ON_ERR,
562 UPDATE_REFS_QUIET_ON_ERR
563};
564
Michael Haggertycaa40462014-04-07 15:48:10 +0200565/*
566 * Begin a reference transaction. The reference transaction must
Ronnie Sahlberg33f9fc52014-06-20 07:42:43 -0700567 * be freed by calling ref_transaction_free().
Michael Haggertycaa40462014-04-07 15:48:10 +0200568 */
Nguyễn Thái Ngọc Duyc0fe4e82017-03-26 09:42:35 +0700569struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
570 struct strbuf *err);
Ronnie Sahlberg93a644e2014-05-19 10:42:34 -0700571struct ref_transaction *ref_transaction_begin(struct strbuf *err);
Michael Haggertycaa40462014-04-07 15:48:10 +0200572
573/*
Michael Haggertyd1dd7212015-02-17 18:00:23 +0100574 * Reference transaction updates
575 *
576 * The following four functions add a reference check or update to a
577 * ref_transaction. They have some common similar parameters:
578 *
579 * transaction -- a pointer to an open ref_transaction, obtained
580 * from ref_transaction_begin().
581 *
582 * refname -- the name of the reference to be affected.
583 *
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100584 * new_oid -- the object ID that should be set to be the new value
585 * of the reference. Some functions allow this parameter to be
Michael Haggertyfd2ce9c2017-05-22 16:17:32 +0200586 * NULL, meaning that the reference is not changed, or
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100587 * null_oid, meaning that the reference should be deleted. A
Michael Haggertyfd2ce9c2017-05-22 16:17:32 +0200588 * copy of this value is made in the transaction.
589 *
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100590 * old_oid -- the object ID that the reference must have before
Michael Haggertyfd2ce9c2017-05-22 16:17:32 +0200591 * the update. Some functions allow this parameter to be NULL,
592 * meaning that the old value of the reference is not checked,
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100593 * or null_oid, meaning that the reference must not exist
Michael Haggertyfd2ce9c2017-05-22 16:17:32 +0200594 * before the update. A copy of this value is made in the
595 * transaction.
596 *
Michael Haggertyd1dd7212015-02-17 18:00:23 +0100597 * flags -- flags affecting the update, passed to
Michael Haggerty91774af2017-11-05 09:42:06 +0100598 * update_ref_lock(). Possible flags: REF_NO_DEREF,
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100599 * REF_FORCE_CREATE_REFLOG. See those constants for more
600 * information.
Michael Haggertyd1dd7212015-02-17 18:00:23 +0100601 *
602 * msg -- a message describing the change (for the reflog).
603 *
604 * err -- a strbuf for receiving a description of any error that
Peter Colbergdc72b502016-06-10 15:05:26 -0400605 * might have occurred.
Michael Haggertyd1dd7212015-02-17 18:00:23 +0100606 *
607 * The functions make internal copies of refname and msg, so the
608 * caller retains ownership of these parameters.
609 *
610 * The functions return 0 on success and non-zero on failure. A
611 * failure means that the transaction as a whole has failed and needs
612 * to be rolled back.
Michael Haggertycaa40462014-04-07 15:48:10 +0200613 */
614
Michael Haggertycaa40462014-04-07 15:48:10 +0200615/*
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100616 * The following flags can be passed to ref_transaction_update() etc.
617 * Internally, they are stored in `ref_update::flags`, along with some
618 * internal flags.
619 */
620
621/*
622 * Act on the ref directly; i.e., without dereferencing symbolic refs.
623 * If this flag is not specified, then symbolic references are
624 * dereferenced and the update is applied to the referent.
625 */
Michael Haggerty91774af2017-11-05 09:42:06 +0100626#define REF_NO_DEREF (1 << 0)
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100627
628/*
629 * Force the creation of a reflog for this reference, even if it
630 * didn't previously have a reflog.
631 */
632#define REF_FORCE_CREATE_REFLOG (1 << 1)
633
634/*
Han-Wen Nienhuyse9706a12021-12-07 13:38:17 +0000635 * Blindly write an object_id. This is useful for testing data corruption
636 * scenarios.
637 */
638#define REF_SKIP_OID_VERIFICATION (1 << 10)
639
640/*
Han-Wen Nienhuys3c966c72021-12-07 13:38:18 +0000641 * Skip verifying refname. This is useful for testing data corruption scenarios.
642 */
643#define REF_SKIP_REFNAME_VERIFICATION (1 << 11)
644
645/*
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100646 * Bitmask of all of the flags that are allowed to be passed in to
647 * ref_transaction_update() and friends:
648 */
Han-Wen Nienhuys3c966c72021-12-07 13:38:18 +0000649#define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \
650 (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION | \
651 REF_SKIP_REFNAME_VERIFICATION)
Michael Haggerty5ac95fe2017-11-05 09:42:05 +0100652
653/*
654 * Add a reference update to transaction. `new_oid` is the value that
655 * the reference should have after the update, or `null_oid` if it
656 * should be deleted. If `new_oid` is NULL, then the reference is not
657 * changed at all. `old_oid` is the value that the reference must have
658 * before the update, or `null_oid` if it must not have existed
Michael Haggerty16180332015-02-17 18:00:21 +0100659 * beforehand. The old value is checked after the lock is taken to
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000660 * prevent races. If the old value doesn't agree with old_oid, the
661 * whole transaction fails. If old_oid is NULL, then the previous
Michael Haggerty16180332015-02-17 18:00:21 +0100662 * value is not checked.
663 *
Michael Haggertyd1dd7212015-02-17 18:00:23 +0100664 * See the above comment "Reference transaction updates" for more
665 * information.
Michael Haggertycaa40462014-04-07 15:48:10 +0200666 */
Ronnie Sahlberg8e348002014-06-20 07:43:00 -0700667int ref_transaction_update(struct ref_transaction *transaction,
668 const char *refname,
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000669 const struct object_id *new_oid,
670 const struct object_id *old_oid,
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100671 unsigned int flags, const char *msg,
Ronnie Sahlberg8e348002014-06-20 07:43:00 -0700672 struct strbuf *err);
Michael Haggertycaa40462014-04-07 15:48:10 +0200673
674/*
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000675 * Add a reference creation to transaction. new_oid is the value that
Michael Haggertyd1dd7212015-02-17 18:00:23 +0100676 * the reference should have after the update; it must not be
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000677 * null_oid. It is verified that the reference does not exist
Michael Haggertycaa40462014-04-07 15:48:10 +0200678 * already.
Michael Haggertyd1dd7212015-02-17 18:00:23 +0100679 *
680 * See the above comment "Reference transaction updates" for more
681 * information.
Michael Haggertycaa40462014-04-07 15:48:10 +0200682 */
Ronnie Sahlbergb416af52014-04-16 15:26:44 -0700683int ref_transaction_create(struct ref_transaction *transaction,
684 const char *refname,
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000685 const struct object_id *new_oid,
Michael Haggertyfec14ec2015-02-17 18:00:13 +0100686 unsigned int flags, const char *msg,
Ronnie Sahlbergb416af52014-04-16 15:26:44 -0700687 struct strbuf *err);
Michael Haggertycaa40462014-04-07 15:48:10 +0200688
689/*
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000690 * Add a reference deletion to transaction. If old_oid is non-NULL,
Michael Haggertyd1dd7212015-02-17 18:00:23 +0100691 * then it holds the value that the reference should have had before
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000692 * the update (which must not be null_oid).
Michael Haggertyd1dd7212015-02-17 18:00:23 +0100693 *
694 * See the above comment "Reference transaction updates" for more
695 * information.
Michael Haggertycaa40462014-04-07 15:48:10 +0200696 */
Ronnie Sahlberg8c8bdc02014-04-16 15:27:45 -0700697int ref_transaction_delete(struct ref_transaction *transaction,
698 const char *refname,
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000699 const struct object_id *old_oid,
Michael Haggertyfb5a6bb2015-02-17 18:00:16 +0100700 unsigned int flags, const char *msg,
Ronnie Sahlberg8c8bdc02014-04-16 15:27:45 -0700701 struct strbuf *err);
Michael Haggertycaa40462014-04-07 15:48:10 +0200702
703/*
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000704 * Verify, within a transaction, that refname has the value old_oid,
705 * or, if old_oid is null_oid, then verify that the reference
706 * doesn't exist. old_oid must be non-NULL.
Michael Haggertyd1dd7212015-02-17 18:00:23 +0100707 *
708 * See the above comment "Reference transaction updates" for more
709 * information.
Michael Haggerty16180332015-02-17 18:00:21 +0100710 */
711int ref_transaction_verify(struct ref_transaction *transaction,
712 const char *refname,
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000713 const struct object_id *old_oid,
Michael Haggerty16180332015-02-17 18:00:21 +0100714 unsigned int flags,
715 struct strbuf *err);
716
Ronnie Sahlberg28e6a972014-05-16 14:14:38 -0700717/* Naming conflict (for example, the ref names A and A/B conflict). */
718#define TRANSACTION_NAME_CONFLICT -1
719/* All other errors. */
720#define TRANSACTION_GENERIC_ERROR -2
Michael Haggerty30173b82017-05-22 16:17:44 +0200721
722/*
Ville Skyttä64127572017-06-25 13:20:41 +0300723 * Perform the preparatory stages of committing `transaction`. Acquire
Michael Haggerty30173b82017-05-22 16:17:44 +0200724 * any needed locks, check preconditions, etc.; basically, do as much
725 * as possible to ensure that the transaction will be able to go
726 * through, stopping just short of making any irrevocable or
727 * user-visible changes. The updates that this function prepares can
728 * be finished up by calling `ref_transaction_commit()` or rolled back
729 * by calling `ref_transaction_abort()`.
730 *
731 * On success, return 0 and leave the transaction in "prepared" state.
732 * On failure, abort the transaction, write an error message to `err`,
733 * and return one of the `TRANSACTION_*` constants.
734 *
Ville Skyttä64127572017-06-25 13:20:41 +0300735 * Callers who don't need such fine-grained control over committing
Michael Haggerty30173b82017-05-22 16:17:44 +0200736 * reference transactions should just call `ref_transaction_commit()`.
737 */
738int ref_transaction_prepare(struct ref_transaction *transaction,
739 struct strbuf *err);
740
741/*
742 * Commit all of the changes that have been queued in transaction, as
743 * atomically as possible. On success, return 0 and leave the
744 * transaction in "closed" state. On failure, roll back the
745 * transaction, write an error message to `err`, and return one of the
746 * `TRANSACTION_*` constants
747 */
Michael Haggertycaa40462014-04-07 15:48:10 +0200748int ref_transaction_commit(struct ref_transaction *transaction,
Ronnie Sahlbergdb7516a2014-04-30 12:22:42 -0700749 struct strbuf *err);
Michael Haggertycaa40462014-04-07 15:48:10 +0200750
Ronnie Sahlberg026bd1d2014-06-20 07:42:42 -0700751/*
Michael Haggerty30173b82017-05-22 16:17:44 +0200752 * Abort `transaction`, which has been begun and possibly prepared,
753 * but not yet committed.
754 */
755int ref_transaction_abort(struct ref_transaction *transaction,
756 struct strbuf *err);
757
758/*
Michael Haggerty58f233c2015-06-22 16:03:01 +0200759 * Like ref_transaction_commit(), but optimized for creating
760 * references when originally initializing a repository (e.g., by "git
761 * clone"). It writes the new references directly to packed-refs
762 * without locking the individual references.
763 *
764 * It is a bug to call this function when there might be other
765 * processes accessing the repository or if there are existing
766 * references that might conflict with the ones being created. All
Michael Haggerty78fb4572017-11-05 09:42:09 +0100767 * old_oid values must either be absent or null_oid.
Michael Haggerty58f233c2015-06-22 16:03:01 +0200768 */
769int initial_ref_transaction_commit(struct ref_transaction *transaction,
770 struct strbuf *err);
771
772/*
Patrick Steinhardt4f2ba2d2022-02-17 14:04:32 +0100773 * Execute the given callback function for each of the reference updates which
774 * have been queued in the given transaction. `old_oid` and `new_oid` may be
775 * `NULL` pointers depending on whether the update has these object IDs set or
776 * not.
777 */
778typedef void ref_transaction_for_each_queued_update_fn(const char *refname,
779 const struct object_id *old_oid,
780 const struct object_id *new_oid,
781 void *cb_data);
782void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
783 ref_transaction_for_each_queued_update_fn cb,
784 void *cb_data);
785
786/*
Michael Haggerty30173b82017-05-22 16:17:44 +0200787 * Free `*transaction` and all associated data.
Ronnie Sahlberg026bd1d2014-06-20 07:42:42 -0700788 */
789void ref_transaction_free(struct ref_transaction *transaction);
790
Michael Haggerty4b7b5202015-02-17 18:00:22 +0100791/**
792 * Lock, update, and unlock a single reference. This function
793 * basically does a transaction containing a single call to
794 * ref_transaction_update(). The parameters to this function have the
795 * same meaning as the corresponding parameters to
796 * ref_transaction_update(). Handle errors as requested by the `onerr`
797 * argument.
798 */
Nguyễn Thái Ngọc Duyc0fe4e82017-03-26 09:42:35 +0700799int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname,
brian m. carlsonae077772017-10-15 22:06:51 +0000800 const struct object_id *new_oid, const struct object_id *old_oid,
Nguyễn Thái Ngọc Duyc0fe4e82017-03-26 09:42:35 +0700801 unsigned int flags, enum action_on_err onerr);
Michael Haggerty4b7b5202015-02-17 18:00:22 +0100802int update_ref(const char *msg, const char *refname,
brian m. carlson8f6dc7e2016-09-05 20:08:08 +0000803 const struct object_id *new_oid, const struct object_id *old_oid,
804 unsigned int flags, enum action_on_err onerr);
Carlos Rica3d9f0372007-09-05 03:38:24 +0200805
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +0100806int parse_hide_refs_config(const char *var, const char *value, const char *,
807 struct string_list *);
Michael Haggertyfb58c8d2015-06-22 16:03:05 +0200808
Lukas Fleischer78a766a2015-11-03 08:58:16 +0100809/*
810 * Check whether a ref is hidden. If no namespace is set, both the first and
811 * the second parameter point to the full ref name. If a namespace is set and
812 * the ref is inside that namespace, the first parameter is a pointer to the
813 * name of the ref with the namespace prefix removed. If a namespace is set and
814 * the ref is outside that namespace, the first parameter is NULL. The second
815 * parameter always points to the full ref name.
816 */
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +0100817int ref_is_hidden(const char *, const char *, const struct string_list *);
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800818
Han-Wen Nienhuys71e54732022-09-19 16:34:50 +0000819/* Is this a per-worktree ref living in the refs/ namespace? */
820int is_per_worktree_ref(const char *refname);
821
822/* Describes how a refname relates to worktrees */
823enum ref_worktree_type {
824 REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or
825 refs/bisect/SOMETHING */
826 REF_WORKTREE_MAIN, /* explicitly in main worktree, eg.
827 main-worktree/HEAD */
828 REF_WORKTREE_OTHER, /* explicitly in named worktree, eg.
829 worktrees/bla/HEAD */
830 REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */
David Turner266b1822015-07-31 02:06:18 -0400831};
832
Han-Wen Nienhuys71e54732022-09-19 16:34:50 +0000833/*
834 * Parse a `maybe_worktree_ref` as a ref that possibly refers to a worktree ref
835 * (ie. either REFNAME, main-worktree/REFNAME or worktree/WORKTREE/REFNAME). It
836 * returns what kind of ref was found, and in case of REF_WORKTREE_OTHER, the
837 * worktree name is returned in `worktree_name` (pointing into
838 * `maybe_worktree_ref`) and `worktree_name_length`. The bare refname (the
839 * refname stripped of prefixes) is returned in `bare_refname`. The
840 * `worktree_name`, `worktree_name_length` and `bare_refname` arguments may be
841 * NULL.
842 */
843enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
844 const char **worktree_name,
845 int *worktree_name_length,
846 const char **bare_refname);
David Turner266b1822015-07-31 02:06:18 -0400847
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100848enum expire_reflog_flags {
849 EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
850 EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
Ævar Arnfjörð Bjarmasonfcd2c3d2021-12-22 05:06:48 +0100851 EXPIRE_REFLOGS_REWRITE = 1 << 2,
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100852};
853
854/*
855 * The following interface is used for reflog expiration. The caller
856 * calls reflog_expire(), supplying it with three callback functions,
857 * of the following types. The callback functions define the
858 * expiration policy that is desired.
859 *
860 * reflog_expiry_prepare_fn -- Called once after the reference is
Ævar Arnfjörð Bjarmasonae35e162021-08-23 13:36:10 +0200861 * locked. Called with the OID of the locked reference.
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100862 *
863 * reflog_expiry_should_prune_fn -- Called once for each entry in the
864 * existing reflog. It should return true iff that entry should be
865 * pruned.
866 *
867 * reflog_expiry_cleanup_fn -- Called once before the reference is
868 * unlocked again.
869 */
870typedef void reflog_expiry_prepare_fn(const char *refname,
brian m. carlson43224782017-05-06 22:10:00 +0000871 const struct object_id *oid,
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100872 void *cb_data);
brian m. carlson43224782017-05-06 22:10:00 +0000873typedef int reflog_expiry_should_prune_fn(struct object_id *ooid,
874 struct object_id *noid,
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100875 const char *email,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200876 timestamp_t timestamp, int tz,
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100877 const char *message, void *cb_data);
878typedef void reflog_expiry_cleanup_fn(void *cb_data);
879
880/*
Ævar Arnfjörð Bjarmasoncc40b5c2021-08-23 13:36:11 +0200881 * Expire reflog entries for the specified reference.
882 * flags is a combination of the constants in
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100883 * enum expire_reflog_flags. The three function pointers are described
884 * above. On success, return zero.
885 */
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700886int refs_reflog_expire(struct ref_store *refs,
887 const char *refname,
Nguyễn Thái Ngọc Duy7d2df052017-03-26 09:42:34 +0700888 unsigned int flags,
889 reflog_expiry_prepare_fn prepare_fn,
890 reflog_expiry_should_prune_fn should_prune_fn,
891 reflog_expiry_cleanup_fn cleanup_fn,
892 void *policy_cb_data);
Ævar Arnfjörð Bjarmasoncc40b5c2021-08-23 13:36:11 +0200893int reflog_expire(const char *refname,
Michael Haggerty1354c9b2016-03-31 06:19:22 +0200894 unsigned int flags,
895 reflog_expiry_prepare_fn prepare_fn,
896 reflog_expiry_should_prune_fn should_prune_fn,
897 reflog_expiry_cleanup_fn cleanup_fn,
898 void *policy_cb_data);
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100899
Stefan Beller64a74162018-04-11 17:21:14 -0700900struct ref_store *get_main_ref_store(struct repository *r);
Heba Waly126c1cc2019-11-17 21:04:46 +0000901
902/**
903 * Submodules
904 * ----------
905 *
906 * If you want to iterate the refs of a submodule you first need to add the
907 * submodules object database. You can do this by a code-snippet like
908 * this:
909 *
910 * const char *path = "path/to/submodule"
911 * if (add_submodule_odb(path))
912 * die("Error submodule '%s' not populated.", path);
913 *
914 * `add_submodule_odb()` will return zero on success. If you
915 * do not do this you will get an error for each ref that it does not point
916 * to a valid object.
917 *
918 * Note: As a side-effect of this you cannot safely assume that all
919 * objects you lookup are available in superproject. All submodule objects
920 * will be available the same way as the superprojects objects.
921 *
922 * Example:
923 * --------
924 *
925 * ----
926 * static int handle_remote_ref(const char *refname,
927 * const unsigned char *sha1, int flags, void *cb_data)
928 * {
929 * struct strbuf *output = cb_data;
930 * strbuf_addf(output, "%s\n", refname);
931 * return 0;
932 * }
933 *
934 */
935
Nguyễn Thái Ngọc Duy18d00022017-03-26 09:42:33 +0700936/*
937 * Return the ref_store instance for the specified submodule. For the
938 * main repository, use submodule==NULL; such a call cannot fail. For
939 * a submodule, the submodule must exist and be a nonbare repository,
940 * otherwise return NULL. If the requested reference store has not yet
941 * been initialized, initialize it first.
942 *
943 * For backwards compatibility, submodule=="" is treated the same as
944 * submodule==NULL.
945 */
946struct ref_store *get_submodule_ref_store(const char *submodule);
Nguyễn Thái Ngọc Duy17eff962017-04-24 17:01:22 +0700947struct ref_store *get_worktree_ref_store(const struct worktree *wt);
Nguyễn Thái Ngọc Duy077be782017-03-26 09:42:29 +0700948
Derrick Stoleeb9342b32022-08-05 17:58:36 +0000949/*
950 * Some of the names specified by refs have special meaning to Git.
951 * Organize these namespaces in a comon 'ref_namespace' array for
952 * reference from multiple places in the codebase.
953 */
954
955struct ref_namespace_info {
956 char *ref;
957 enum decoration_type decoration;
958
959 /*
960 * If 'exact' is true, then we must match the 'ref' exactly.
961 * Otherwise, use a prefix match.
962 *
963 * 'ref_updated' is for internal use. It represents whether the
964 * 'ref' value was replaced from its original literal version.
965 */
966 unsigned exact:1,
967 ref_updated:1;
968};
969
970enum ref_namespace {
971 NAMESPACE_HEAD,
972 NAMESPACE_BRANCHES,
973 NAMESPACE_TAGS,
974 NAMESPACE_REMOTE_REFS,
975 NAMESPACE_STASH,
976 NAMESPACE_REPLACE,
977 NAMESPACE_NOTES,
978 NAMESPACE_PREFETCH,
979 NAMESPACE_REWRITTEN,
980
981 /* Must be last */
982 NAMESPACE__COUNT
983};
984
985/* See refs.c for the contents of this array. */
986extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT];
987
988/*
989 * Some ref namespaces can be modified by config values or environment
990 * variables. Modify a namespace as specified by its ref_namespace key.
991 */
992void update_ref_namespace(enum ref_namespace namespace, char *ref);
993
Daniel Barkalow95fc7512005-06-06 16:31:29 -0400994#endif /* REFS_H */