]>
Commit | Line | Data |
---|---|---|
95fc7512 DB |
1 | #ifndef REFS_H |
2 | #define REFS_H | |
3 | ||
b9342b3f | 4 | #include "commit.h" |
36026a0f | 5 | #include "repository.h" |
9a20b889 | 6 | #include "repo-settings.h" |
ec06b055 | 7 | |
ab6f79d8 | 8 | struct fsck_options; |
504c4d42 | 9 | struct object_id; |
077be78d | 10 | struct ref_store; |
504c4d42 NTND |
11 | struct strbuf; |
12 | struct string_list; | |
ef3ca954 | 13 | struct string_list_item; |
17eff96b | 14 | struct worktree; |
504c4d42 | 15 | |
318efb96 PS |
16 | enum ref_storage_format ref_storage_format_by_name(const char *name); |
17 | const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format); | |
0fcc285c | 18 | |
76e760b9 KN |
19 | enum ref_transaction_error { |
20 | /* Default error code */ | |
21 | REF_TRANSACTION_ERROR_GENERIC = -1, | |
22 | /* Ref name conflict like A vs A/B */ | |
23 | REF_TRANSACTION_ERROR_NAME_CONFLICT = -2, | |
24 | /* Ref to be created already exists */ | |
25 | REF_TRANSACTION_ERROR_CREATE_EXISTS = -3, | |
26 | /* ref expected but doesn't exist */ | |
27 | REF_TRANSACTION_ERROR_NONEXISTENT_REF = -4, | |
28 | /* Provided old_oid or old_target of reference doesn't match actual */ | |
29 | REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE = -5, | |
30 | /* Provided new_oid or new_target is invalid */ | |
31 | REF_TRANSACTION_ERROR_INVALID_NEW_VALUE = -6, | |
32 | /* Expected ref to be symref, but is a regular ref */ | |
33 | REF_TRANSACTION_ERROR_EXPECTED_SYMREF = -7, | |
34 | }; | |
35 | ||
fb58c8d5 | 36 | /* |
619cbc01 | 37 | * Resolve a reference, recursively following symbolic references. |
fb58c8d5 | 38 | * |
54fad661 RS |
39 | * Return the name of the non-symbolic reference that ultimately pointed |
40 | * at the resolved object name. The return value, if not NULL, is a | |
41 | * pointer into either a static buffer or the input ref. | |
42 | * | |
49e61479 | 43 | * If oid is non-NULL, store the referred-to object's name in it. |
fb58c8d5 MH |
44 | * |
45 | * If the reference cannot be resolved to an object, the behavior | |
46 | * depends on the RESOLVE_REF_READING flag: | |
47 | * | |
48 | * - If RESOLVE_REF_READING is set, return NULL. | |
49 | * | |
49e61479 | 50 | * - If RESOLVE_REF_READING is not set, clear oid and return the name of |
fb58c8d5 MH |
51 | * the last reference name in the chain, which will either be a non-symbolic |
52 | * reference or an undefined reference. If this is a prelude to | |
53 | * "writing" to the ref, the return value is the name of the ref | |
54 | * that will actually be created or changed. | |
55 | * | |
56 | * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one | |
49e61479 | 57 | * level of symbolic reference. The value stored in oid for a symbolic |
58 | * reference will always be null_oid in this case, and the return | |
fb58c8d5 MH |
59 | * value is the reference that the symref refers to directly. |
60 | * | |
61 | * If flags is non-NULL, set the value that it points to the | |
62 | * combination of REF_ISPACKED (if the reference was found among the | |
63 | * packed references), REF_ISSYMREF (if the initial reference was a | |
64 | * symbolic reference), REF_BAD_NAME (if the reference name is ill | |
65 | * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN | |
66 | * (if the ref is malformed or has a bad name). See refs.h for more detail | |
67 | * on each flag. | |
68 | * | |
69 | * If ref is not a properly-formatted, normalized reference, return | |
70 | * NULL. If more than MAXDEPTH recursive symbolic lookups are needed, | |
71 | * give up and return NULL. | |
72 | * | |
73 | * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their | |
74 | * name is invalid according to git-check-ref-format(1). If the name | |
49e61479 | 75 | * is bad then the value stored in oid will be null_oid and the two |
fb58c8d5 MH |
76 | * flags REF_ISBROKEN and REF_BAD_NAME will be set. |
77 | * | |
78 | * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/ | |
79 | * directory and do not consist of all caps and underscores cannot be | |
80 | * resolved. The function returns NULL for such ref names. | |
7122f4f7 | 81 | * Caps and underscores refers to the pseudorefs, such as HEAD, |
fb58c8d5 MH |
82 | * FETCH_HEAD and friends, that all live outside of the refs/ directory. |
83 | */ | |
84 | #define RESOLVE_REF_READING 0x01 | |
85 | #define RESOLVE_REF_NO_RECURSE 0x02 | |
86 | #define RESOLVE_REF_ALLOW_BAD_NAME 0x04 | |
87 | ||
7d2df051 NTND |
88 | const char *refs_resolve_ref_unsafe(struct ref_store *refs, |
89 | const char *refname, | |
90 | int resolve_flags, | |
49e61479 | 91 | struct object_id *oid, |
ce14de03 | 92 | int *flags); |
25a33b33 | 93 | |
7d2df051 NTND |
94 | char *refs_resolve_refdup(struct ref_store *refs, |
95 | const char *refname, int resolve_flags, | |
0f2dc722 | 96 | struct object_id *oid, int *flags); |
fb58c8d5 | 97 | |
39a9ef8f PS |
98 | int refs_read_ref_full(struct ref_store *refs, const char *refname, |
99 | int resolve_flags, struct object_id *oid, int *flags); | |
39a9ef8f PS |
100 | |
101 | int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid); | |
fb58c8d5 | 102 | |
8102d10f BF |
103 | #define NOT_A_SYMREF -2 |
104 | ||
105 | /* | |
106 | * Read the symbolic ref named "refname" and write its immediate referent into | |
107 | * the provided buffer. Referent is left empty if "refname" is not a symbolic | |
108 | * ref. It does not resolve the symbolic reference recursively in case the | |
109 | * target is also a symbolic ref. | |
110 | * | |
111 | * Returns 0 on success, -2 if the "refname" is not a symbolic ref, | |
112 | * -1 otherwise. | |
113 | */ | |
cd475b3b PS |
114 | int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname, |
115 | struct strbuf *referent); | |
116 | ||
7d2df051 NTND |
117 | /* |
118 | * Return 0 if a reference named refname could be created without | |
119 | * conflicting with the name of an existing reference. Otherwise, | |
120 | * return a negative value and write an explanation to err. If extras | |
121 | * is non-NULL, it is a list of additional refnames with which refname | |
122 | * is not allowed to conflict. If skip is non-NULL, ignore potential | |
123 | * conflicts with refs in skip (e.g., because they are scheduled for | |
124 | * deletion in the same operation). Behavior is undefined if the same | |
125 | * name is listed in both extras and skip. | |
126 | * | |
127 | * Two reference names conflict if one of them exactly matches the | |
128 | * leading components of the other; e.g., "foo/bar" conflicts with | |
129 | * both "foo" and with "foo/bar/baz" but not with "foo/bar" or | |
130 | * "foo/barbados". | |
131 | * | |
e4929cdf PS |
132 | * If `initial_transaction` is truish, then all collision checks with |
133 | * preexisting refs are skipped. | |
134 | * | |
7d2df051 NTND |
135 | * extras and skip must be sorted. |
136 | */ | |
76e760b9 KN |
137 | enum ref_transaction_error refs_verify_refname_available(struct ref_store *refs, |
138 | const char *refname, | |
139 | const struct string_list *extras, | |
140 | const struct string_list *skip, | |
141 | unsigned int initial_transaction, | |
142 | struct strbuf *err); | |
2ff58dec | 143 | |
3f9f1acc HWN |
144 | int refs_ref_exists(struct ref_store *refs, const char *refname); |
145 | ||
9a20b889 PS |
146 | int should_autocreate_reflog(enum log_refs_config log_all_ref_updates, |
147 | const char *refname); | |
341fb286 | 148 | |
1354c9b2 | 149 | int is_branch(const char *refname); |
fb58c8d5 | 150 | |
ed93ea16 | 151 | #define REF_STORE_CREATE_ON_DISK_IS_WORKTREE (1 << 0) |
2eb1d0c4 | 152 | |
ed93ea16 | 153 | int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err); |
6fb5acfd | 154 | |
71c871b4 PS |
155 | /* |
156 | * Release all memory and resources associated with the ref store. | |
157 | */ | |
158 | void ref_store_release(struct ref_store *ref_store); | |
6fb5acfd | 159 | |
64a6dd8f PS |
160 | /* |
161 | * Remove the ref store from disk. This deletes all associated data. | |
162 | */ | |
163 | int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err); | |
164 | ||
fb58c8d5 | 165 | /* |
36a31792 JK |
166 | * Return the peeled value of the oid currently being iterated via |
167 | * for_each_ref(), etc. This is equivalent to calling: | |
168 | * | |
30aaff43 | 169 | * peel_object(r, oid, &peeled); |
36a31792 JK |
170 | * |
171 | * with the "oid" value given to the each_ref_fn callback, except | |
172 | * that some ref storage may be able to answer the query without | |
173 | * actually loading the object in memory. | |
fb58c8d5 | 174 | */ |
30aaff43 PS |
175 | int peel_iterated_oid(struct repository *r, |
176 | const struct object_id *base, struct object_id *peeled); | |
fb58c8d5 MH |
177 | |
178 | /** | |
a8355bb7 MH |
179 | * Resolve refname in the nested "gitlink" repository in the specified |
180 | * submodule (which must be non-NULL). If the resolution is | |
78fb4579 | 181 | * successful, return 0 and set oid to the name of the object; |
a8355bb7 | 182 | * otherwise, return a non-zero value. |
fb58c8d5 | 183 | */ |
e19488a6 PS |
184 | int repo_resolve_gitlink_ref(struct repository *r, |
185 | const char *submodule, const char *refname, | |
186 | struct object_id *oid); | |
fb58c8d5 MH |
187 | |
188 | /* | |
189 | * Return true iff abbrev_name is a possible abbreviation for | |
190 | * full_name according to the rules defined by ref_rev_parse_rules in | |
191 | * refs.c. | |
192 | */ | |
1354c9b2 | 193 | int refname_match(const char *abbrev_name, const char *full_name); |
fb58c8d5 | 194 | |
b4be7410 BW |
195 | /* |
196 | * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add | |
197 | * the results to 'prefixes' | |
198 | */ | |
873cd28a JK |
199 | struct strvec; |
200 | void expand_ref_prefix(struct strvec *prefixes, const char *prefix); | |
b4be7410 | 201 | |
0b1dbf53 | 202 | int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref); |
f24c30e0 JT |
203 | int repo_dwim_ref(struct repository *r, const char *str, int len, |
204 | struct object_id *oid, char **ref, int nonfatal_dangling_mark); | |
56700903 | 205 | int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref); |
fb58c8d5 | 206 | |
8747ebb7 DGW |
207 | /* |
208 | * Retrieves the default branch name for newly-initialized repositories. | |
209 | * | |
97abaab5 | 210 | * The return value is an allocated string. |
8747ebb7 | 211 | */ |
cc0f13c5 | 212 | char *repo_default_branch_name(struct repository *r, int quiet); |
8747ebb7 | 213 | |
5bcbde9e JH |
214 | /* |
215 | * Copy "name" to "sb", expanding any special @-marks as handled by | |
216 | * repo_interpret_branch_name(). The result is a non-qualified branch name | |
217 | * (so "foo" or "origin/master" instead of "refs/heads/foo" or | |
218 | * "refs/remotes/origin/master"). | |
219 | * | |
220 | * Note that the resulting name may not be a syntactically valid refname. | |
221 | * | |
222 | * If "allowed" is non-zero, restrict the set of allowed expansions. See | |
223 | * repo_interpret_branch_name() for details. | |
224 | */ | |
93e5e048 | 225 | void copy_branchname(struct strbuf *sb, const char *name, |
5bcbde9e JH |
226 | unsigned allowed); |
227 | ||
228 | /* | |
93e5e048 | 229 | * Like copy_branchname() above, but confirm that the result is |
5bcbde9e JH |
230 | * syntactically valid to be used as a local branch name in refs/heads/. |
231 | * | |
232 | * The return value is "0" if the result is valid, and "-1" otherwise. | |
233 | */ | |
93e5e048 | 234 | int check_branch_ref(struct strbuf *sb, const char *name); |
5bcbde9e JH |
235 | |
236 | /* | |
237 | * Similar for a tag name in refs/tags/. | |
238 | * | |
239 | * The return value is "0" if the result is valid, and "-1" otherwise. | |
240 | */ | |
93e5e048 | 241 | int check_tag_ref(struct strbuf *sb, const char *name); |
5bcbde9e | 242 | |
b416af5b | 243 | /* |
30173b88 MH |
244 | * A ref_transaction represents a collection of reference updates that |
245 | * should succeed or fail together. | |
b416af5b RS |
246 | * |
247 | * Calling sequence | |
248 | * ---------------- | |
30173b88 | 249 | * |
b416af5b RS |
250 | * - Allocate and initialize a `struct ref_transaction` by calling |
251 | * `ref_transaction_begin()`. | |
252 | * | |
30173b88 MH |
253 | * - Specify the intended ref updates by calling one or more of the |
254 | * following functions: | |
255 | * - `ref_transaction_update()` | |
256 | * - `ref_transaction_create()` | |
257 | * - `ref_transaction_delete()` | |
258 | * - `ref_transaction_verify()` | |
259 | * | |
260 | * - Then either: | |
261 | * | |
262 | * - Optionally call `ref_transaction_prepare()` to prepare the | |
263 | * transaction. This locks all references, checks preconditions, | |
264 | * etc. but doesn't finalize anything. If this step fails, the | |
265 | * transaction has been closed and can only be freed. If this step | |
266 | * succeeds, then `ref_transaction_commit()` is almost certain to | |
267 | * succeed. However, you can still call `ref_transaction_abort()` | |
268 | * if you decide not to commit the transaction after all. | |
269 | * | |
270 | * - Call `ref_transaction_commit()` to execute the transaction, | |
271 | * make the changes permanent, and release all locks. If you | |
272 | * haven't already called `ref_transaction_prepare()`, then | |
273 | * `ref_transaction_commit()` calls it for you. | |
274 | * | |
275 | * Or | |
276 | * | |
1c299d03 PS |
277 | * - Call `ref_transaction_begin()` with REF_TRANSACTION_FLAG_INITIAL if the |
278 | * ref database is known to be empty and have no other writers (e.g. during | |
279 | * clone). This is likely to be much faster than without the flag. | |
30173b88 MH |
280 | * |
281 | * - Then finally, call `ref_transaction_free()` to free the | |
282 | * `ref_transaction` data structure. | |
283 | * | |
284 | * At any time before calling `ref_transaction_commit()`, you can call | |
285 | * `ref_transaction_abort()` to abort the transaction, rollback any | |
286 | * locks, and free any associated resources (including the | |
287 | * `ref_transaction` data structure). | |
288 | * | |
289 | * Putting it all together, a complete reference update looks like | |
290 | * | |
291 | * struct ref_transaction *transaction; | |
292 | * struct strbuf err = STRBUF_INIT; | |
293 | * int ret = 0; | |
294 | * | |
a0efef14 | 295 | * transaction = ref_store_transaction_begin(refs, 0, &err); |
30173b88 MH |
296 | * if (!transaction || |
297 | * ref_transaction_update(...) || | |
298 | * ref_transaction_create(...) || | |
299 | * ...etc... || | |
300 | * ref_transaction_commit(transaction, &err)) { | |
301 | * error("%s", err.buf); | |
302 | * ret = -1; | |
303 | * } | |
304 | * ref_transaction_free(transaction); | |
305 | * strbuf_release(&err); | |
306 | * return ret; | |
b416af5b RS |
307 | * |
308 | * Error handling | |
309 | * -------------- | |
310 | * | |
311 | * On error, transaction functions append a message about what | |
312 | * went wrong to the 'err' argument. The message mentions what | |
313 | * ref was being updated (if any) when the error occurred so it | |
314 | * can be passed to 'die' or 'error' as-is. | |
315 | * | |
316 | * The message is appended to err without first clearing err. | |
317 | * err will not be '\n' terminated. | |
49386868 DT |
318 | * |
319 | * Caveats | |
320 | * ------- | |
321 | * | |
322 | * Note that no locks are taken, and no refs are read, until | |
30173b88 MH |
323 | * `ref_transaction_prepare()` or `ref_transaction_commit()` is |
324 | * called. So, for example, `ref_transaction_verify()` won't report a | |
325 | * verification failure until the commit is attempted. | |
b416af5b | 326 | */ |
caa4046c MH |
327 | struct ref_transaction; |
328 | ||
89df9c84 | 329 | /* |
3bc581b9 MH |
330 | * Bit values set in the flags argument passed to each_ref_fn() and |
331 | * stored in ref_iterator::flags. Other bits are for internal use | |
332 | * only: | |
89df9c84 MH |
333 | */ |
334 | ||
335 | /* Reference is a symbolic reference. */ | |
98ac34b2 | 336 | #define REF_ISSYMREF 0x01 |
89df9c84 MH |
337 | |
338 | /* Reference is a packed reference. */ | |
98ac34b2 | 339 | #define REF_ISPACKED 0x02 |
89df9c84 MH |
340 | |
341 | /* | |
342 | * Reference cannot be resolved to an object name: dangling symbolic | |
d0f810f0 RS |
343 | * reference (directly or indirectly), corrupt reference file, |
344 | * reference exists but name is bad, or symbolic reference refers to | |
345 | * ill-formatted reference name. | |
89df9c84 | 346 | */ |
98ac34b2 | 347 | #define REF_ISBROKEN 0x04 |
f4204ab9 | 348 | |
d0f810f0 RS |
349 | /* |
350 | * Reference name is not well formed. | |
351 | * | |
352 | * See git-check-ref-format(1) for the definition of well formed ref names. | |
353 | */ | |
354 | #define REF_BAD_NAME 0x08 | |
355 | ||
8a65ff76 | 356 | /* |
4f78c24c | 357 | * The signature for the callback function for the for_each_*() |
78fb4579 | 358 | * functions below. The memory pointed to by the refname and oid |
4f78c24c MH |
359 | * arguments is only guaranteed to be valid for the duration of a |
360 | * single callback invocation. | |
361 | */ | |
e8207717 | 362 | typedef int each_ref_fn(const char *refname, const char *referent, |
2b2a5be3 MH |
363 | const struct object_id *oid, int flags, void *cb_data); |
364 | ||
4f78c24c MH |
365 | /* |
366 | * The following functions invoke the specified callback function for | |
367 | * each reference indicated. If the function ever returns a nonzero | |
368 | * value, stop the iteration and return that value. Please note that | |
369 | * it is not safe to modify references while an iteration is in | |
370 | * progress, unless the same callback function invocation that | |
371 | * modifies the reference also returns a nonzero value to immediately | |
adac8115 | 372 | * stop the iteration. Returned references are sorted. |
8a65ff76 | 373 | */ |
62f0b399 NTND |
374 | int refs_head_ref(struct ref_store *refs, |
375 | each_ref_fn fn, void *cb_data); | |
7d2df051 NTND |
376 | int refs_for_each_ref(struct ref_store *refs, |
377 | each_ref_fn fn, void *cb_data); | |
378 | int refs_for_each_ref_in(struct ref_store *refs, const char *prefix, | |
379 | each_ref_fn fn, void *cb_data); | |
380 | int refs_for_each_tag_ref(struct ref_store *refs, | |
381 | each_ref_fn fn, void *cb_data); | |
382 | int refs_for_each_branch_ref(struct ref_store *refs, | |
383 | each_ref_fn fn, void *cb_data); | |
384 | int refs_for_each_remote_ref(struct ref_store *refs, | |
385 | each_ref_fn fn, void *cb_data); | |
8378c9d2 PS |
386 | int refs_for_each_replace_ref(struct ref_store *refs, |
387 | each_ref_fn fn, void *cb_data); | |
7d2df051 | 388 | |
59c35fac TB |
389 | /* |
390 | * references matching any pattern in "exclude_patterns" are omitted from the | |
391 | * result set on a best-effort basis. | |
392 | */ | |
073cf63c | 393 | int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix, |
b269ac53 | 394 | const char **exclude_patterns, |
67985e4e | 395 | each_ref_fn fn, void *cb_data); |
126c1cce | 396 | |
16b1985b TB |
397 | /** |
398 | * iterate all refs in "patterns" by partitioning patterns into disjoint sets | |
399 | * and iterating the longest-common prefix of each set. | |
400 | * | |
b269ac53 TB |
401 | * references matching any pattern in "exclude_patterns" are omitted from the |
402 | * result set on a best-effort basis. | |
403 | * | |
16b1985b TB |
404 | * callers should be prepared to ignore references that they did not ask for. |
405 | */ | |
91e2ab15 | 406 | int refs_for_each_fullref_in_prefixes(struct ref_store *refs, |
b269ac53 TB |
407 | const char *namespace, |
408 | const char **patterns, | |
409 | const char **exclude_patterns, | |
91e2ab15 JK |
410 | each_ref_fn fn, void *cb_data); |
411 | ||
126c1cce | 412 | /* iterates all refs that match the specified glob pattern. */ |
39a9ef8f PS |
413 | int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn, |
414 | const char *pattern, void *cb_data); | |
126c1cce | 415 | |
39a9ef8f PS |
416 | int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn, |
417 | const char *pattern, const char *prefix, void *cb_data); | |
1354c9b2 | 418 | |
39a9ef8f | 419 | int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data); |
39a9ef8f | 420 | |
e6bf24d3 TB |
421 | /* |
422 | * references matching any pattern in "exclude_patterns" are omitted from the | |
423 | * result set on a best-effort basis. | |
424 | */ | |
39a9ef8f PS |
425 | int refs_for_each_namespaced_ref(struct ref_store *refs, |
426 | const char **exclude_patterns, | |
427 | each_ref_fn fn, void *cb_data); | |
a1bea2c1 | 428 | |
fb58c8d5 | 429 | /* can be used to learn about broken ref and symref */ |
7d2df051 | 430 | int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data); |
fb58c8d5 | 431 | |
d0f00c1a KN |
432 | /* |
433 | * Iterates over all refs including root refs, i.e. pseudorefs and HEAD. | |
434 | */ | |
435 | int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn, | |
436 | void *cb_data); | |
437 | ||
65516f58 RA |
438 | /* |
439 | * Normalizes partial refs to their fully qualified form. | |
440 | * Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'. | |
441 | * <prefix> will default to 'refs/' if NULL. | |
442 | * | |
443 | * item.string will be set to the result. | |
444 | * item.util will be set to NULL if <pattern> contains glob characters, or | |
445 | * non-NULL if it doesn't. | |
446 | */ | |
447 | void normalize_glob_ref(struct string_list_item *item, const char *prefix, | |
448 | const char *pattern); | |
449 | ||
894a9d33 TR |
450 | static inline const char *has_glob_specials(const char *pattern) |
451 | { | |
452 | return strpbrk(pattern, "?*["); | |
453 | } | |
454 | ||
330a2ae6 PS |
455 | void refs_warn_dangling_symref(struct ref_store *refs, FILE *fp, |
456 | const char *msg_fmt, const char *refname); | |
457 | void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp, | |
458 | const char *msg_fmt, const struct string_list *refnames); | |
f8948e2f | 459 | |
32d462ce MH |
460 | /* |
461 | * Flags for controlling behaviour of pack_refs() | |
462 | * PACK_REFS_PRUNE: Prune loose refs after packing | |
f89356db PS |
463 | * PACK_REFS_AUTO: Pack refs on a best effort basis. The heuristics and end |
464 | * result are decided by the ref backend. Backends may ignore | |
465 | * this flag and fall back to a normal repack. | |
32d462ce | 466 | */ |
f89356db PS |
467 | #define PACK_REFS_PRUNE (1 << 0) |
468 | #define PACK_REFS_AUTO (1 << 1) | |
32d462ce | 469 | |
0f65c7a6 PS |
470 | struct pack_refs_opts { |
471 | unsigned int flags; | |
472 | struct ref_exclusions *exclusions; | |
473 | struct string_list *includes; | |
474 | }; | |
475 | ||
32d462ce MH |
476 | /* |
477 | * Write a packed-refs file for the current repository. | |
478 | * flags: Combination of the above PACK_REFS_* flags. | |
479 | */ | |
826ae79f | 480 | int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts); |
32d462ce | 481 | |
bd3b02da | 482 | /* |
a4c653df | 483 | * Setup reflog before using. Fill in err and return -1 on failure. |
bd3b02da | 484 | */ |
7d2df051 | 485 | int refs_create_reflog(struct ref_store *refs, const char *refname, |
7b089120 | 486 | struct strbuf *err); |
859c3017 | 487 | |
5edd1267 JK |
488 | /** |
489 | * Reads log for the value of ref during at_time (in which case "cnt" should be | |
490 | * negative) or the reflog "cnt" entries from the top (in which case "at_time" | |
491 | * should be 0). | |
492 | * | |
493 | * If we found the reflog entry in question, returns 0 (and details of the | |
494 | * entry can be found in the out-parameters). | |
495 | * | |
496 | * If we ran out of reflog entries, the out-parameters are filled with the | |
497 | * details of the oldest entry we did find, and the function returns 1. Note | |
498 | * that there is one important special case here! If the reflog was empty | |
499 | * and the caller asked for the 0-th cnt, we will return "1" but leave the | |
500 | * "oid" field untouched. | |
501 | **/ | |
7fdff474 NTND |
502 | int read_ref_at(struct ref_store *refs, |
503 | const char *refname, unsigned int flags, | |
dddbad72 | 504 | timestamp_t at_time, int cnt, |
8eb36d94 | 505 | struct object_id *oid, char **msg, |
dddbad72 | 506 | timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt); |
d556fae2 | 507 | |
4da58835 | 508 | /** Check if a particular reflog exists */ |
7d2df051 | 509 | int refs_reflog_exists(struct ref_store *refs, const char *refname); |
4da58835 | 510 | |
fc1c2168 | 511 | /* |
2616a5e5 | 512 | * Delete the specified reference. If old_oid is non-NULL, then |
78fb4579 | 513 | * verify that the current value of the reference is old_oid before |
2616a5e5 | 514 | * deleting it. If old_oid is NULL, delete the reference if it |
515 | * exists, regardless of its old value. It is an error for old_oid to | |
516 | * be null_oid. msg and flags are passed through to | |
64da4199 | 517 | * ref_transaction_delete(). |
fc1c2168 | 518 | */ |
c0fe4e8b NTND |
519 | int refs_delete_ref(struct ref_store *refs, const char *msg, |
520 | const char *refname, | |
2616a5e5 | 521 | const struct object_id *old_oid, |
c0fe4e8b | 522 | unsigned int flags); |
fc1c2168 | 523 | |
98ffd5ff MH |
524 | /* |
525 | * Delete the specified references. If there are any problems, emit | |
526 | * errors but attempt to keep going (i.e., the deletes are not done in | |
64da4199 | 527 | * an all-or-nothing transaction). msg and flags are passed through to |
c5f04ddd | 528 | * ref_transaction_delete(). |
98ffd5ff | 529 | */ |
64da4199 MH |
530 | int refs_delete_refs(struct ref_store *refs, const char *msg, |
531 | struct string_list *refnames, unsigned int flags); | |
98ffd5ff | 532 | |
4da58835 | 533 | /** Delete a reflog */ |
7d2df051 | 534 | int refs_delete_reflog(struct ref_store *refs, const char *refname); |
4da58835 | 535 | |
d1eb22da HWN |
536 | /* |
537 | * Callback to process a reflog entry found by the iteration functions (see | |
e6e94f34 JH |
538 | * below). |
539 | * | |
540 | * The committer parameter is a single string, in the form | |
541 | * "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" (without double quotes). | |
542 | * | |
543 | * The timestamp parameter gives the time when entry was created as the number | |
544 | * of seconds since the UNIX epoch. | |
545 | * | |
546 | * The tz parameter gives the timezone offset for the user who created | |
547 | * the reflog entry, and its value gives a positive or negative offset | |
548 | * from UTC. Its absolute value is formed by multiplying the hour | |
549 | * part by 100 and adding the minute part. For example, 1 hour ahead | |
550 | * of UTC, CET == "+0100", is represented as positive one hundred (not | |
619cbc01 | 551 | * positive sixty). |
e6e94f34 JH |
552 | * |
553 | * The msg parameter is a single complete line; a reflog message given | |
554 | * to refs_delete_ref, refs_update_ref, etc. is returned to the | |
555 | * callback normalized---each run of whitespaces are squashed into a | |
556 | * single whitespace, trailing whitespace, if exists, is trimmed, and | |
557 | * then a single LF is added at the end. | |
558 | * | |
559 | * The cb_data is a caller-supplied pointer given to the iterator | |
560 | * functions. | |
d1eb22da | 561 | */ |
1354c9b2 | 562 | typedef int each_reflog_ent_fn( |
9461d272 | 563 | struct object_id *old_oid, struct object_id *new_oid, |
dddbad72 | 564 | const char *committer, timestamp_t timestamp, |
1354c9b2 MH |
565 | int tz, const char *msg, void *cb_data); |
566 | ||
d1eb22da HWN |
567 | /* Iterate over reflog entries in the log for `refname`. */ |
568 | ||
569 | /* oldest entry first */ | |
7d2df051 NTND |
570 | int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname, |
571 | each_reflog_ent_fn fn, void *cb_data); | |
d1eb22da HWN |
572 | |
573 | /* youngest entry first */ | |
7d2df051 NTND |
574 | int refs_for_each_reflog_ent_reverse(struct ref_store *refs, |
575 | const char *refname, | |
576 | each_reflog_ent_fn fn, | |
577 | void *cb_data); | |
d1eb22da HWN |
578 | |
579 | /* | |
c8f815c2 | 580 | * The signature for the callback function for the refs_for_each_reflog() |
31f89839 PS |
581 | * functions below. The memory pointed to by the refname argument is only |
582 | * guaranteed to be valid for the duration of a single callback invocation. | |
583 | */ | |
584 | typedef int each_reflog_fn(const char *refname, void *cb_data); | |
585 | ||
eb8381c8 NP |
586 | /* |
587 | * Calls the specified function for each reflog file until it returns nonzero, | |
adac8115 | 588 | * and returns the value. Reflog file order is unspecified. |
eb8381c8 | 589 | */ |
31f89839 | 590 | int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data); |
eb8381c8 | 591 | |
8d9c5010 MH |
592 | #define REFNAME_ALLOW_ONELEVEL 1 |
593 | #define REFNAME_REFSPEC_PATTERN 2 | |
594 | ||
595 | /* | |
dfefa935 | 596 | * Return 0 iff refname has the correct format for a refname according |
72d38582 | 597 | * to the rules described in Documentation/git-check-ref-format.adoc. |
dfefa935 | 598 | * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level |
8d9c5010 | 599 | * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then |
cd377f45 JK |
600 | * allow a single "*" wildcard character in the refspec. No leading or |
601 | * repeated slashes are accepted. | |
8d9c5010 | 602 | */ |
1354c9b2 | 603 | int check_refname_format(const char *refname, int flags); |
95fc7512 | 604 | |
ab6f79d8 | 605 | /* |
606 | * Check the reference database for consistency. Return 0 if refs and | |
607 | * reflogs are consistent, and non-zero otherwise. The errors will be | |
608 | * written to stderr. | |
609 | */ | |
7c78d819 | 610 | int refs_fsck(struct ref_store *refs, struct fsck_options *o, |
611 | struct worktree *wt); | |
ab6f79d8 | 612 | |
1de16aec NTND |
613 | /* |
614 | * Apply the rules from check_refname_format, but mutate the result until it | |
615 | * is acceptable, and place the result in "out". | |
616 | */ | |
617 | void sanitize_refname_component(const char *refname, struct strbuf *out); | |
618 | ||
1354c9b2 | 619 | const char *prettify_refname(const char *refname); |
fb58c8d5 | 620 | |
546edf37 NTND |
621 | char *refs_shorten_unambiguous_ref(struct ref_store *refs, |
622 | const char *refname, int strict); | |
a9c37a72 | 623 | |
c976d415 | 624 | /** rename ref, return 0 on success **/ |
7d2df051 NTND |
625 | int refs_rename_ref(struct ref_store *refs, const char *oldref, |
626 | const char *newref, const char *logmsg); | |
52d59cc6 SD |
627 | |
628 | /** copy ref, return 0 on success **/ | |
629 | int refs_copy_existing_ref(struct ref_store *refs, const char *oldref, | |
630 | const char *newref, const char *logmsg); | |
c976d415 | 631 | |
f151dfe3 | 632 | int refs_update_symref(struct ref_store *refs, const char *refname, |
7d2df051 | 633 | const char *target, const char *logmsg); |
0ebde32c | 634 | |
d842cd13 BF |
635 | int refs_update_symref_extended(struct ref_store *refs, const char *refname, |
636 | const char *target, const char *logmsg, | |
9963746c | 637 | struct strbuf *referent, int create_only); |
d842cd13 | 638 | |
f4124112 MH |
639 | enum action_on_err { |
640 | UPDATE_REFS_MSG_ON_ERR, | |
641 | UPDATE_REFS_DIE_ON_ERR, | |
642 | UPDATE_REFS_QUIET_ON_ERR | |
643 | }; | |
644 | ||
1c299d03 PS |
645 | enum ref_transaction_flag { |
646 | /* | |
647 | * The ref transaction is part of the initial creation of the ref store | |
648 | * and can thus assume that the ref store is completely empty. This | |
649 | * allows the backend to perform the transaction more efficiently by | |
650 | * skipping certain checks. | |
651 | * | |
652 | * It is a bug to set this flag when there might be other processes | |
653 | * accessing the repository or if there are existing references that | |
654 | * might conflict with the ones being created. All old_oid values must | |
655 | * either be absent or null_oid. | |
656 | */ | |
657 | REF_TRANSACTION_FLAG_INITIAL = (1 << 0), | |
23fc8e4f KN |
658 | |
659 | /* | |
660 | * The transaction mechanism by default fails all updates if any conflict | |
661 | * is detected. This flag allows transactions to partially apply updates | |
662 | * while rejecting updates which do not match the expected state. | |
663 | */ | |
664 | REF_TRANSACTION_ALLOW_FAILURE = (1 << 1), | |
1c299d03 PS |
665 | }; |
666 | ||
caa4046c MH |
667 | /* |
668 | * Begin a reference transaction. The reference transaction must | |
33f9fc59 | 669 | * be freed by calling ref_transaction_free(). |
caa4046c | 670 | */ |
c0fe4e8b | 671 | struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs, |
a0efef14 | 672 | unsigned int flags, |
c0fe4e8b | 673 | struct strbuf *err); |
caa4046c | 674 | |
caa4046c | 675 | /* |
d1dd721f MH |
676 | * Reference transaction updates |
677 | * | |
678 | * The following four functions add a reference check or update to a | |
679 | * ref_transaction. They have some common similar parameters: | |
680 | * | |
681 | * transaction -- a pointer to an open ref_transaction, obtained | |
682 | * from ref_transaction_begin(). | |
683 | * | |
684 | * refname -- the name of the reference to be affected. | |
685 | * | |
5ac95fee MH |
686 | * new_oid -- the object ID that should be set to be the new value |
687 | * of the reference. Some functions allow this parameter to be | |
fd2ce9c0 | 688 | * NULL, meaning that the reference is not changed, or |
5ac95fee | 689 | * null_oid, meaning that the reference should be deleted. A |
fd2ce9c0 MH |
690 | * copy of this value is made in the transaction. |
691 | * | |
5ac95fee | 692 | * old_oid -- the object ID that the reference must have before |
fd2ce9c0 MH |
693 | * the update. Some functions allow this parameter to be NULL, |
694 | * meaning that the old value of the reference is not checked, | |
5ac95fee | 695 | * or null_oid, meaning that the reference must not exist |
fd2ce9c0 MH |
696 | * before the update. A copy of this value is made in the |
697 | * transaction. | |
698 | * | |
1bc4cc3f KN |
699 | * new_target -- the target reference that the reference will be |
700 | * updated to point to. If the reference is a regular reference, | |
701 | * it will be converted to a symbolic reference. Cannot be set | |
702 | * together with `new_oid`. A copy of this value is made in the | |
703 | * transaction. | |
704 | * | |
705 | * old_target -- the reference that the reference must be pointing to. | |
706 | * Canont be set together with `old_oid`. A copy of this value is | |
707 | * made in the transaction. | |
708 | * | |
d1dd721f | 709 | * flags -- flags affecting the update, passed to |
91774afc | 710 | * update_ref_lock(). Possible flags: REF_NO_DEREF, |
5ac95fee MH |
711 | * REF_FORCE_CREATE_REFLOG. See those constants for more |
712 | * information. | |
d1dd721f MH |
713 | * |
714 | * msg -- a message describing the change (for the reflog). | |
715 | * | |
716 | * err -- a strbuf for receiving a description of any error that | |
dc72b500 | 717 | * might have occurred. |
d1dd721f MH |
718 | * |
719 | * The functions make internal copies of refname and msg, so the | |
720 | * caller retains ownership of these parameters. | |
721 | * | |
722 | * The functions return 0 on success and non-zero on failure. A | |
723 | * failure means that the transaction as a whole has failed and needs | |
724 | * to be rolled back. | |
caa4046c MH |
725 | */ |
726 | ||
caa4046c | 727 | /* |
5ac95fee MH |
728 | * The following flags can be passed to ref_transaction_update() etc. |
729 | * Internally, they are stored in `ref_update::flags`, along with some | |
730 | * internal flags. | |
731 | */ | |
732 | ||
733 | /* | |
734 | * Act on the ref directly; i.e., without dereferencing symbolic refs. | |
735 | * If this flag is not specified, then symbolic references are | |
736 | * dereferenced and the update is applied to the referent. | |
737 | */ | |
91774afc | 738 | #define REF_NO_DEREF (1 << 0) |
5ac95fee MH |
739 | |
740 | /* | |
741 | * Force the creation of a reflog for this reference, even if it | |
742 | * didn't previously have a reflog. | |
743 | */ | |
744 | #define REF_FORCE_CREATE_REFLOG (1 << 1) | |
745 | ||
e9706a18 HWN |
746 | /* |
747 | * Blindly write an object_id. This is useful for testing data corruption | |
748 | * scenarios. | |
749 | */ | |
750 | #define REF_SKIP_OID_VERIFICATION (1 << 10) | |
751 | ||
3c966c7b HWN |
752 | /* |
753 | * Skip verifying refname. This is useful for testing data corruption scenarios. | |
754 | */ | |
755 | #define REF_SKIP_REFNAME_VERIFICATION (1 << 11) | |
756 | ||
fbd1a693 PS |
757 | /* |
758 | * Skip creation of a reflog entry, even if it would have otherwise been | |
759 | * created. | |
760 | */ | |
761 | #define REF_SKIP_CREATE_REFLOG (1 << 12) | |
762 | ||
5ac95fee MH |
763 | /* |
764 | * Bitmask of all of the flags that are allowed to be passed in to | |
765 | * ref_transaction_update() and friends: | |
766 | */ | |
3c966c7b HWN |
767 | #define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \ |
768 | (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION | \ | |
fbd1a693 | 769 | REF_SKIP_REFNAME_VERIFICATION | REF_SKIP_CREATE_REFLOG) |
5ac95fee MH |
770 | |
771 | /* | |
772 | * Add a reference update to transaction. `new_oid` is the value that | |
773 | * the reference should have after the update, or `null_oid` if it | |
774 | * should be deleted. If `new_oid` is NULL, then the reference is not | |
775 | * changed at all. `old_oid` is the value that the reference must have | |
776 | * before the update, or `null_oid` if it must not have existed | |
16180334 | 777 | * beforehand. The old value is checked after the lock is taken to |
89f3bbdd | 778 | * prevent races. If the old value doesn't agree with old_oid, the |
779 | * whole transaction fails. If old_oid is NULL, then the previous | |
1bc4cc3f KN |
780 | * value is not checked. If `old_target` is not NULL, treat the reference |
781 | * as a symbolic ref and validate that its target before the update is | |
782 | * `old_target`. If the `new_target` is not NULL, then the reference | |
783 | * will be updated to a symbolic ref which targets `new_target`. | |
784 | * Together, these allow us to update between regular refs and symrefs. | |
16180334 | 785 | * |
d1dd721f MH |
786 | * See the above comment "Reference transaction updates" for more |
787 | * information. | |
caa4046c | 788 | */ |
8e34800e RS |
789 | int ref_transaction_update(struct ref_transaction *transaction, |
790 | const char *refname, | |
89f3bbdd | 791 | const struct object_id *new_oid, |
792 | const struct object_id *old_oid, | |
1bc4cc3f KN |
793 | const char *new_target, |
794 | const char *old_target, | |
1d147bdf | 795 | unsigned int flags, const char *msg, |
8e34800e | 796 | struct strbuf *err); |
caa4046c MH |
797 | |
798 | /* | |
89f3bbdd | 799 | * Add a reference creation to transaction. new_oid is the value that |
d1dd721f | 800 | * the reference should have after the update; it must not be |
89f3bbdd | 801 | * null_oid. It is verified that the reference does not exist |
caa4046c | 802 | * already. |
d1dd721f MH |
803 | * |
804 | * See the above comment "Reference transaction updates" for more | |
805 | * information. | |
caa4046c | 806 | */ |
b416af5b RS |
807 | int ref_transaction_create(struct ref_transaction *transaction, |
808 | const char *refname, | |
89f3bbdd | 809 | const struct object_id *new_oid, |
ed327272 | 810 | const char *new_target, |
fec14ec3 | 811 | unsigned int flags, const char *msg, |
b416af5b | 812 | struct strbuf *err); |
caa4046c MH |
813 | |
814 | /* | |
89f3bbdd | 815 | * Add a reference deletion to transaction. If old_oid is non-NULL, |
d1dd721f | 816 | * then it holds the value that the reference should have had before |
89f3bbdd | 817 | * the update (which must not be null_oid). |
d1dd721f MH |
818 | * |
819 | * See the above comment "Reference transaction updates" for more | |
820 | * information. | |
caa4046c | 821 | */ |
8c8bdc0d RS |
822 | int ref_transaction_delete(struct ref_transaction *transaction, |
823 | const char *refname, | |
89f3bbdd | 824 | const struct object_id *old_oid, |
23437209 KN |
825 | const char *old_target, |
826 | unsigned int flags, | |
827 | const char *msg, | |
8c8bdc0d | 828 | struct strbuf *err); |
caa4046c | 829 | |
16180334 | 830 | /* |
89f3bbdd | 831 | * Verify, within a transaction, that refname has the value old_oid, |
832 | * or, if old_oid is null_oid, then verify that the reference | |
833 | * doesn't exist. old_oid must be non-NULL. | |
d1dd721f MH |
834 | * |
835 | * See the above comment "Reference transaction updates" for more | |
836 | * information. | |
16180334 MH |
837 | */ |
838 | int ref_transaction_verify(struct ref_transaction *transaction, | |
839 | const char *refname, | |
89f3bbdd | 840 | const struct object_id *old_oid, |
1451ac73 | 841 | const char *old_target, |
16180334 MH |
842 | unsigned int flags, |
843 | struct strbuf *err); | |
844 | ||
30173b88 | 845 | /* |
64127575 | 846 | * Perform the preparatory stages of committing `transaction`. Acquire |
30173b88 MH |
847 | * any needed locks, check preconditions, etc.; basically, do as much |
848 | * as possible to ensure that the transaction will be able to go | |
849 | * through, stopping just short of making any irrevocable or | |
850 | * user-visible changes. The updates that this function prepares can | |
851 | * be finished up by calling `ref_transaction_commit()` or rolled back | |
852 | * by calling `ref_transaction_abort()`. | |
853 | * | |
854 | * On success, return 0 and leave the transaction in "prepared" state. | |
855 | * On failure, abort the transaction, write an error message to `err`, | |
856 | * and return one of the `TRANSACTION_*` constants. | |
857 | * | |
64127575 | 858 | * Callers who don't need such fine-grained control over committing |
30173b88 MH |
859 | * reference transactions should just call `ref_transaction_commit()`. |
860 | */ | |
861 | int ref_transaction_prepare(struct ref_transaction *transaction, | |
862 | struct strbuf *err); | |
863 | ||
864 | /* | |
865 | * Commit all of the changes that have been queued in transaction, as | |
866 | * atomically as possible. On success, return 0 and leave the | |
867 | * transaction in "closed" state. On failure, roll back the | |
868 | * transaction, write an error message to `err`, and return one of the | |
869 | * `TRANSACTION_*` constants | |
870 | */ | |
caa4046c | 871 | int ref_transaction_commit(struct ref_transaction *transaction, |
db7516ab | 872 | struct strbuf *err); |
caa4046c | 873 | |
30173b88 MH |
874 | /* |
875 | * Abort `transaction`, which has been begun and possibly prepared, | |
876 | * but not yet committed. | |
877 | */ | |
878 | int ref_transaction_abort(struct ref_transaction *transaction, | |
879 | struct strbuf *err); | |
880 | ||
4f2ba2d0 PS |
881 | /* |
882 | * Execute the given callback function for each of the reference updates which | |
883 | * have been queued in the given transaction. `old_oid` and `new_oid` may be | |
884 | * `NULL` pointers depending on whether the update has these object IDs set or | |
885 | * not. | |
886 | */ | |
887 | typedef void ref_transaction_for_each_queued_update_fn(const char *refname, | |
888 | const struct object_id *old_oid, | |
889 | const struct object_id *new_oid, | |
890 | void *cb_data); | |
891 | void ref_transaction_for_each_queued_update(struct ref_transaction *transaction, | |
892 | ref_transaction_for_each_queued_update_fn cb, | |
893 | void *cb_data); | |
894 | ||
23fc8e4f KN |
895 | /* |
896 | * Execute the given callback function for each of the reference updates which | |
897 | * have been rejected in the given transaction. | |
898 | */ | |
899 | typedef void ref_transaction_for_each_rejected_update_fn(const char *refname, | |
900 | const struct object_id *old_oid, | |
901 | const struct object_id *new_oid, | |
902 | const char *old_target, | |
903 | const char *new_target, | |
904 | enum ref_transaction_error err, | |
905 | void *cb_data); | |
906 | void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction, | |
907 | ref_transaction_for_each_rejected_update_fn cb, | |
908 | void *cb_data); | |
909 | ||
026bd1d3 | 910 | /* |
30173b88 | 911 | * Free `*transaction` and all associated data. |
026bd1d3 RS |
912 | */ |
913 | void ref_transaction_free(struct ref_transaction *transaction); | |
914 | ||
4b7b520b MH |
915 | /** |
916 | * Lock, update, and unlock a single reference. This function | |
917 | * basically does a transaction containing a single call to | |
918 | * ref_transaction_update(). The parameters to this function have the | |
919 | * same meaning as the corresponding parameters to | |
920 | * ref_transaction_update(). Handle errors as requested by the `onerr` | |
921 | * argument. | |
922 | */ | |
c0fe4e8b | 923 | int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname, |
ae077771 | 924 | const struct object_id *new_oid, const struct object_id *old_oid, |
c0fe4e8b | 925 | unsigned int flags, enum action_on_err onerr); |
3d9f037c | 926 | |
9b67eb6f | 927 | int parse_hide_refs_config(const char *var, const char *value, const char *, |
c45841ff | 928 | struct strvec *); |
fb58c8d5 | 929 | |
78a766ab LF |
930 | /* |
931 | * Check whether a ref is hidden. If no namespace is set, both the first and | |
932 | * the second parameter point to the full ref name. If a namespace is set and | |
933 | * the ref is inside that namespace, the first parameter is a pointer to the | |
934 | * name of the ref with the namespace prefix removed. If a namespace is set and | |
935 | * the ref is outside that namespace, the first parameter is NULL. The second | |
936 | * parameter always points to the full ref name. | |
937 | */ | |
c45841ff | 938 | int ref_is_hidden(const char *, const char *, const struct strvec *); |
daebaa78 | 939 | |
15af64dc TB |
940 | /* |
941 | * Returns an array of patterns to use as excluded_patterns, if none of the | |
942 | * hidden references use the token '!' or '^'. | |
943 | */ | |
944 | const char **hidden_refs_to_excludes(const struct strvec *hide_refs); | |
daebaa78 | 945 | |
155dc844 PS |
946 | /* |
947 | * Prefix all exclude patterns with the namespace, if any. This is required | |
948 | * because exclude patterns apply to the stripped reference name, not the full | |
949 | * reference name with the namespace. | |
950 | */ | |
951 | const char **get_namespaced_exclude_patterns(const char **exclude_patterns, | |
952 | const char *namespace, | |
953 | struct strvec *out); | |
954 | ||
71e54734 HWN |
955 | /* Is this a per-worktree ref living in the refs/ namespace? */ |
956 | int is_per_worktree_ref(const char *refname); | |
957 | ||
958 | /* Describes how a refname relates to worktrees */ | |
959 | enum ref_worktree_type { | |
960 | REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or | |
961 | refs/bisect/SOMETHING */ | |
962 | REF_WORKTREE_MAIN, /* explicitly in main worktree, eg. | |
963 | main-worktree/HEAD */ | |
964 | REF_WORKTREE_OTHER, /* explicitly in named worktree, eg. | |
965 | worktrees/bla/HEAD */ | |
966 | REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */ | |
266b1827 DT |
967 | }; |
968 | ||
71e54734 HWN |
969 | /* |
970 | * Parse a `maybe_worktree_ref` as a ref that possibly refers to a worktree ref | |
971 | * (ie. either REFNAME, main-worktree/REFNAME or worktree/WORKTREE/REFNAME). It | |
972 | * returns what kind of ref was found, and in case of REF_WORKTREE_OTHER, the | |
973 | * worktree name is returned in `worktree_name` (pointing into | |
974 | * `maybe_worktree_ref`) and `worktree_name_length`. The bare refname (the | |
975 | * refname stripped of prefixes) is returned in `bare_refname`. The | |
976 | * `worktree_name`, `worktree_name_length` and `bare_refname` arguments may be | |
977 | * NULL. | |
978 | */ | |
979 | enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref, | |
980 | const char **worktree_name, | |
981 | int *worktree_name_length, | |
982 | const char **bare_refname); | |
266b1827 | 983 | |
fa5b1830 MH |
984 | enum expire_reflog_flags { |
985 | EXPIRE_REFLOGS_DRY_RUN = 1 << 0, | |
986 | EXPIRE_REFLOGS_UPDATE_REF = 1 << 1, | |
fcd2c3d9 | 987 | EXPIRE_REFLOGS_REWRITE = 1 << 2, |
fa5b1830 MH |
988 | }; |
989 | ||
990 | /* | |
991 | * The following interface is used for reflog expiration. The caller | |
c8f815c2 | 992 | * calls refs_reflog_expire(), supplying it with three callback functions, |
fa5b1830 MH |
993 | * of the following types. The callback functions define the |
994 | * expiration policy that is desired. | |
995 | * | |
996 | * reflog_expiry_prepare_fn -- Called once after the reference is | |
ae35e16c | 997 | * locked. Called with the OID of the locked reference. |
fa5b1830 MH |
998 | * |
999 | * reflog_expiry_should_prune_fn -- Called once for each entry in the | |
1000 | * existing reflog. It should return true iff that entry should be | |
1001 | * pruned. | |
1002 | * | |
1003 | * reflog_expiry_cleanup_fn -- Called once before the reference is | |
1004 | * unlocked again. | |
1005 | */ | |
1006 | typedef void reflog_expiry_prepare_fn(const char *refname, | |
4322478a | 1007 | const struct object_id *oid, |
fa5b1830 | 1008 | void *cb_data); |
4322478a | 1009 | typedef int reflog_expiry_should_prune_fn(struct object_id *ooid, |
1010 | struct object_id *noid, | |
fa5b1830 | 1011 | const char *email, |
dddbad72 | 1012 | timestamp_t timestamp, int tz, |
fa5b1830 MH |
1013 | const char *message, void *cb_data); |
1014 | typedef void reflog_expiry_cleanup_fn(void *cb_data); | |
1015 | ||
1016 | /* | |
cc40b5ce ÆAB |
1017 | * Expire reflog entries for the specified reference. |
1018 | * flags is a combination of the constants in | |
fa5b1830 MH |
1019 | * enum expire_reflog_flags. The three function pointers are described |
1020 | * above. On success, return zero. | |
1021 | */ | |
7d2df051 NTND |
1022 | int refs_reflog_expire(struct ref_store *refs, |
1023 | const char *refname, | |
7d2df051 NTND |
1024 | unsigned int flags, |
1025 | reflog_expiry_prepare_fn prepare_fn, | |
1026 | reflog_expiry_should_prune_fn should_prune_fn, | |
1027 | reflog_expiry_cleanup_fn cleanup_fn, | |
1028 | void *policy_cb_data); | |
fa5b1830 | 1029 | |
64a74161 | 1030 | struct ref_store *get_main_ref_store(struct repository *r); |
126c1cce HW |
1031 | |
1032 | /** | |
1033 | * Submodules | |
1034 | * ---------- | |
1035 | * | |
1036 | * If you want to iterate the refs of a submodule you first need to add the | |
1037 | * submodules object database. You can do this by a code-snippet like | |
1038 | * this: | |
1039 | * | |
1040 | * const char *path = "path/to/submodule" | |
1041 | * if (add_submodule_odb(path)) | |
1042 | * die("Error submodule '%s' not populated.", path); | |
1043 | * | |
1044 | * `add_submodule_odb()` will return zero on success. If you | |
1045 | * do not do this you will get an error for each ref that it does not point | |
1046 | * to a valid object. | |
1047 | * | |
1048 | * Note: As a side-effect of this you cannot safely assume that all | |
1049 | * objects you lookup are available in superproject. All submodule objects | |
1050 | * will be available the same way as the superprojects objects. | |
1051 | * | |
1052 | * Example: | |
1053 | * -------- | |
1054 | * | |
1055 | * ---- | |
1056 | * static int handle_remote_ref(const char *refname, | |
1057 | * const unsigned char *sha1, int flags, void *cb_data) | |
1058 | * { | |
1059 | * struct strbuf *output = cb_data; | |
1060 | * strbuf_addf(output, "%s\n", refname); | |
1061 | * return 0; | |
1062 | * } | |
1063 | * | |
1064 | */ | |
1065 | ||
18d0002d NTND |
1066 | /* |
1067 | * Return the ref_store instance for the specified submodule. For the | |
1068 | * main repository, use submodule==NULL; such a call cannot fail. For | |
1069 | * a submodule, the submodule must exist and be a nonbare repository, | |
1070 | * otherwise return NULL. If the requested reference store has not yet | |
1071 | * been initialized, initialize it first. | |
1072 | * | |
1073 | * For backwards compatibility, submodule=="" is treated the same as | |
1074 | * submodule==NULL. | |
1075 | */ | |
965f8991 PS |
1076 | struct ref_store *repo_get_submodule_ref_store(struct repository *repo, |
1077 | const char *submodule); | |
17eff96b | 1078 | struct ref_store *get_worktree_ref_store(const struct worktree *wt); |
077be78d | 1079 | |
b9342b3f DS |
1080 | /* |
1081 | * Some of the names specified by refs have special meaning to Git. | |
619cbc01 | 1082 | * Organize these namespaces in a common 'ref_namespace' array for |
b9342b3f DS |
1083 | * reference from multiple places in the codebase. |
1084 | */ | |
1085 | ||
1086 | struct ref_namespace_info { | |
b567004b | 1087 | const char *ref; |
b9342b3f DS |
1088 | enum decoration_type decoration; |
1089 | ||
1090 | /* | |
1091 | * If 'exact' is true, then we must match the 'ref' exactly. | |
1092 | * Otherwise, use a prefix match. | |
1093 | * | |
1094 | * 'ref_updated' is for internal use. It represents whether the | |
1095 | * 'ref' value was replaced from its original literal version. | |
1096 | */ | |
1097 | unsigned exact:1, | |
1098 | ref_updated:1; | |
1099 | }; | |
1100 | ||
1101 | enum ref_namespace { | |
1102 | NAMESPACE_HEAD, | |
1103 | NAMESPACE_BRANCHES, | |
1104 | NAMESPACE_TAGS, | |
1105 | NAMESPACE_REMOTE_REFS, | |
1106 | NAMESPACE_STASH, | |
1107 | NAMESPACE_REPLACE, | |
1108 | NAMESPACE_NOTES, | |
1109 | NAMESPACE_PREFETCH, | |
1110 | NAMESPACE_REWRITTEN, | |
1111 | ||
1112 | /* Must be last */ | |
1113 | NAMESPACE__COUNT | |
1114 | }; | |
1115 | ||
1116 | /* See refs.c for the contents of this array. */ | |
1117 | extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT]; | |
1118 | ||
1119 | /* | |
1120 | * Some ref namespaces can be modified by config values or environment | |
1121 | * variables. Modify a namespace as specified by its ref_namespace key. | |
1122 | */ | |
1123 | void update_ref_namespace(enum ref_namespace namespace, char *ref); | |
1124 | ||
f6936e62 | 1125 | /* |
afcd067d PS |
1126 | * Check whether the provided name names a root reference. This function only |
1127 | * performs a syntactic check. | |
f6936e62 PS |
1128 | * |
1129 | * A root ref is a reference that lives in the root of the reference hierarchy. | |
1130 | * These references must conform to special syntax: | |
1131 | * | |
1132 | * - Their name must be all-uppercase or underscores ("_"). | |
1133 | * | |
31951c22 PS |
1134 | * - Their name must end with "_HEAD". As a special rule, "HEAD" is a root |
1135 | * ref, as well. | |
f6936e62 PS |
1136 | * |
1137 | * - Their name may not contain a slash. | |
1138 | * | |
1139 | * There is a special set of irregular root refs that exist due to historic | |
1140 | * reasons, only. This list shall not be expanded in the future: | |
1141 | * | |
1142 | * - AUTO_MERGE | |
1143 | * | |
1144 | * - BISECT_EXPECTED_REV | |
1145 | * | |
1146 | * - NOTES_MERGE_PARTIAL | |
1147 | * | |
1148 | * - NOTES_MERGE_REF | |
1149 | * | |
1150 | * - MERGE_AUTOSTASH | |
1151 | */ | |
afcd067d | 1152 | int is_root_ref(const char *refname); |
f6936e62 | 1153 | |
f1701f27 PS |
1154 | /* |
1155 | * Pseudorefs are refs that have different semantics compared to | |
1156 | * "normal" refs. These refs can thus not be stored in the ref backend, | |
1157 | * but must always be accessed via the filesystem. The following refs | |
1158 | * are pseudorefs: | |
1159 | * | |
1160 | * - FETCH_HEAD may contain multiple object IDs, and each one of them | |
1161 | * carries additional metadata like where it came from. | |
1162 | * | |
1163 | * - MERGE_HEAD may contain multiple object IDs when merging multiple | |
1164 | * heads. | |
1165 | * | |
1166 | * Reading, writing or deleting references must consistently go either | |
1167 | * through the filesystem (pseudorefs) or through the reference | |
1168 | * backend (normal ones). | |
1169 | */ | |
1170 | int is_pseudo_ref(const char *refname); | |
1eba2240 | 1171 | |
6d6a3a99 PS |
1172 | /* |
1173 | * The following flags can be passed to `repo_migrate_ref_storage_format()`: | |
1174 | * | |
1175 | * - REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN: perform a dry-run migration | |
1176 | * without touching the main repository. The result will be written into a | |
1177 | * temporary ref storage directory. | |
89be7d27 KN |
1178 | * |
1179 | * - REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG: skip migration of reflogs. | |
6d6a3a99 | 1180 | */ |
89be7d27 KN |
1181 | #define REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN (1 << 0) |
1182 | #define REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG (1 << 1) | |
6d6a3a99 PS |
1183 | |
1184 | /* | |
1185 | * Migrate the ref storage format used by the repository to the | |
1186 | * specified one. | |
1187 | */ | |
1188 | int repo_migrate_ref_storage_format(struct repository *repo, | |
1189 | enum ref_storage_format format, | |
1190 | unsigned int flags, | |
1191 | struct strbuf *err); | |
1192 | ||
95fc7512 | 1193 | #endif /* REFS_H */ |