]>
Commit | Line | Data |
---|---|---|
1 | #ifndef REFS_H | |
2 | #define REFS_H | |
3 | ||
4 | #include "commit.h" | |
5 | #include "repository.h" | |
6 | #include "repo-settings.h" | |
7 | ||
8 | struct fsck_options; | |
9 | struct object_id; | |
10 | struct ref_store; | |
11 | struct strbuf; | |
12 | struct string_list; | |
13 | struct string_list_item; | |
14 | struct worktree; | |
15 | ||
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); | |
18 | ||
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 | ||
36 | /* | |
37 | * Resolve a reference, recursively following symbolic references. | |
38 | * | |
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 | * | |
43 | * If oid is non-NULL, store the referred-to object's name in it. | |
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 | * | |
50 | * - If RESOLVE_REF_READING is not set, clear oid and return the name of | |
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 | |
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 | |
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 | |
75 | * is bad then the value stored in oid will be null_oid and the two | |
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. | |
81 | * Caps and underscores refers to the pseudorefs, such as HEAD, | |
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 | ||
88 | const char *refs_resolve_ref_unsafe(struct ref_store *refs, | |
89 | const char *refname, | |
90 | int resolve_flags, | |
91 | struct object_id *oid, | |
92 | int *flags); | |
93 | ||
94 | char *refs_resolve_refdup(struct ref_store *refs, | |
95 | const char *refname, int resolve_flags, | |
96 | struct object_id *oid, int *flags); | |
97 | ||
98 | int refs_read_ref_full(struct ref_store *refs, const char *refname, | |
99 | int resolve_flags, struct object_id *oid, int *flags); | |
100 | ||
101 | int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid); | |
102 | ||
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 | */ | |
114 | int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname, | |
115 | struct strbuf *referent); | |
116 | ||
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 | * | |
132 | * If `initial_transaction` is truish, then all collision checks with | |
133 | * preexisting refs are skipped. | |
134 | * | |
135 | * extras and skip must be sorted. | |
136 | */ | |
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); | |
143 | ||
144 | int refs_ref_exists(struct ref_store *refs, const char *refname); | |
145 | ||
146 | int should_autocreate_reflog(enum log_refs_config log_all_ref_updates, | |
147 | const char *refname); | |
148 | ||
149 | int is_branch(const char *refname); | |
150 | ||
151 | #define REF_STORE_CREATE_ON_DISK_IS_WORKTREE (1 << 0) | |
152 | ||
153 | int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err); | |
154 | ||
155 | /* | |
156 | * Release all memory and resources associated with the ref store. | |
157 | */ | |
158 | void ref_store_release(struct ref_store *ref_store); | |
159 | ||
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 | ||
165 | /* | |
166 | * Return the peeled value of the oid currently being iterated via | |
167 | * for_each_ref(), etc. This is equivalent to calling: | |
168 | * | |
169 | * peel_object(r, oid, &peeled); | |
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. | |
174 | */ | |
175 | int peel_iterated_oid(struct repository *r, | |
176 | const struct object_id *base, struct object_id *peeled); | |
177 | ||
178 | /** | |
179 | * Resolve refname in the nested "gitlink" repository in the specified | |
180 | * submodule (which must be non-NULL). If the resolution is | |
181 | * successful, return 0 and set oid to the name of the object; | |
182 | * otherwise, return a non-zero value. | |
183 | */ | |
184 | int repo_resolve_gitlink_ref(struct repository *r, | |
185 | const char *submodule, const char *refname, | |
186 | struct object_id *oid); | |
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 | */ | |
193 | int refname_match(const char *abbrev_name, const char *full_name); | |
194 | ||
195 | /* | |
196 | * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add | |
197 | * the results to 'prefixes' | |
198 | */ | |
199 | struct strvec; | |
200 | void expand_ref_prefix(struct strvec *prefixes, const char *prefix); | |
201 | ||
202 | int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref); | |
203 | int repo_dwim_ref(struct repository *r, const char *str, int len, | |
204 | struct object_id *oid, char **ref, int nonfatal_dangling_mark); | |
205 | int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref); | |
206 | ||
207 | /* | |
208 | * Retrieves the default branch name for newly-initialized repositories. | |
209 | * | |
210 | * The return value is an allocated string. | |
211 | */ | |
212 | char *repo_default_branch_name(struct repository *r, int quiet); | |
213 | ||
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 | */ | |
225 | void copy_branchname(struct strbuf *sb, const char *name, | |
226 | unsigned allowed); | |
227 | ||
228 | /* | |
229 | * Like copy_branchname() above, but confirm that the result is | |
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 | */ | |
234 | int check_branch_ref(struct strbuf *sb, const char *name); | |
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 | */ | |
241 | int check_tag_ref(struct strbuf *sb, const char *name); | |
242 | ||
243 | /* | |
244 | * A ref_transaction represents a collection of reference updates that | |
245 | * should succeed or fail together. | |
246 | * | |
247 | * Calling sequence | |
248 | * ---------------- | |
249 | * | |
250 | * - Allocate and initialize a `struct ref_transaction` by calling | |
251 | * `ref_transaction_begin()`. | |
252 | * | |
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 | * | |
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. | |
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 | * | |
295 | * transaction = ref_store_transaction_begin(refs, 0, &err); | |
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; | |
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. | |
318 | * | |
319 | * Caveats | |
320 | * ------- | |
321 | * | |
322 | * Note that no locks are taken, and no refs are read, until | |
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. | |
326 | */ | |
327 | struct ref_transaction; | |
328 | ||
329 | /* | |
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: | |
333 | */ | |
334 | ||
335 | /* Reference is a symbolic reference. */ | |
336 | #define REF_ISSYMREF 0x01 | |
337 | ||
338 | /* Reference is a packed reference. */ | |
339 | #define REF_ISPACKED 0x02 | |
340 | ||
341 | /* | |
342 | * Reference cannot be resolved to an object name: dangling symbolic | |
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. | |
346 | */ | |
347 | #define REF_ISBROKEN 0x04 | |
348 | ||
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 | ||
356 | /* | |
357 | * The signature for the callback function for the for_each_*() | |
358 | * functions below. The memory pointed to by the refname and oid | |
359 | * arguments is only guaranteed to be valid for the duration of a | |
360 | * single callback invocation. | |
361 | */ | |
362 | typedef int each_ref_fn(const char *refname, const char *referent, | |
363 | const struct object_id *oid, int flags, void *cb_data); | |
364 | ||
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 | |
372 | * stop the iteration. Returned references are sorted. | |
373 | */ | |
374 | int refs_head_ref(struct ref_store *refs, | |
375 | each_ref_fn fn, void *cb_data); | |
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); | |
386 | int refs_for_each_replace_ref(struct ref_store *refs, | |
387 | each_ref_fn fn, void *cb_data); | |
388 | ||
389 | /* | |
390 | * references matching any pattern in "exclude_patterns" are omitted from the | |
391 | * result set on a best-effort basis. | |
392 | */ | |
393 | int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix, | |
394 | const char **exclude_patterns, | |
395 | each_ref_fn fn, void *cb_data); | |
396 | ||
397 | /** | |
398 | * iterate all refs in "patterns" by partitioning patterns into disjoint sets | |
399 | * and iterating the longest-common prefix of each set. | |
400 | * | |
401 | * references matching any pattern in "exclude_patterns" are omitted from the | |
402 | * result set on a best-effort basis. | |
403 | * | |
404 | * callers should be prepared to ignore references that they did not ask for. | |
405 | */ | |
406 | int refs_for_each_fullref_in_prefixes(struct ref_store *refs, | |
407 | const char *namespace, | |
408 | const char **patterns, | |
409 | const char **exclude_patterns, | |
410 | each_ref_fn fn, void *cb_data); | |
411 | ||
412 | /* iterates all refs that match the specified glob pattern. */ | |
413 | int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn, | |
414 | const char *pattern, void *cb_data); | |
415 | ||
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); | |
418 | ||
419 | int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data); | |
420 | ||
421 | /* | |
422 | * references matching any pattern in "exclude_patterns" are omitted from the | |
423 | * result set on a best-effort basis. | |
424 | */ | |
425 | int refs_for_each_namespaced_ref(struct ref_store *refs, | |
426 | const char **exclude_patterns, | |
427 | each_ref_fn fn, void *cb_data); | |
428 | ||
429 | /* can be used to learn about broken ref and symref */ | |
430 | int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data); | |
431 | int refs_for_each_rawref_in(struct ref_store *refs, const char *prefix, | |
432 | each_ref_fn fn, void *cb_data); | |
433 | ||
434 | /* | |
435 | * Iterates over all refs including root refs, i.e. pseudorefs and HEAD. | |
436 | */ | |
437 | int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn, | |
438 | void *cb_data); | |
439 | ||
440 | /* | |
441 | * Normalizes partial refs to their fully qualified form. | |
442 | * Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'. | |
443 | * <prefix> will default to 'refs/' if NULL. | |
444 | * | |
445 | * item.string will be set to the result. | |
446 | * item.util will be set to NULL if <pattern> contains glob characters, or | |
447 | * non-NULL if it doesn't. | |
448 | */ | |
449 | void normalize_glob_ref(struct string_list_item *item, const char *prefix, | |
450 | const char *pattern); | |
451 | ||
452 | static inline const char *has_glob_specials(const char *pattern) | |
453 | { | |
454 | return strpbrk(pattern, "?*["); | |
455 | } | |
456 | ||
457 | void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp, | |
458 | const char *indent, int dry_run, | |
459 | const struct string_list *refnames); | |
460 | ||
461 | /* | |
462 | * Flags for controlling behaviour of pack_refs() | |
463 | * PACK_REFS_PRUNE: Prune loose refs after packing | |
464 | * PACK_REFS_AUTO: Pack refs on a best effort basis. The heuristics and end | |
465 | * result are decided by the ref backend. Backends may ignore | |
466 | * this flag and fall back to a normal repack. | |
467 | */ | |
468 | #define PACK_REFS_PRUNE (1 << 0) | |
469 | #define PACK_REFS_AUTO (1 << 1) | |
470 | ||
471 | struct pack_refs_opts { | |
472 | unsigned int flags; | |
473 | struct ref_exclusions *exclusions; | |
474 | struct string_list *includes; | |
475 | }; | |
476 | ||
477 | /* | |
478 | * Write a packed-refs file for the current repository. | |
479 | * flags: Combination of the above PACK_REFS_* flags. | |
480 | */ | |
481 | int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts); | |
482 | ||
483 | /* | |
484 | * Setup reflog before using. Fill in err and return -1 on failure. | |
485 | */ | |
486 | int refs_create_reflog(struct ref_store *refs, const char *refname, | |
487 | struct strbuf *err); | |
488 | ||
489 | /** | |
490 | * Reads log for the value of ref during at_time (in which case "cnt" should be | |
491 | * negative) or the reflog "cnt" entries from the top (in which case "at_time" | |
492 | * should be 0). | |
493 | * | |
494 | * If we found the reflog entry in question, returns 0 (and details of the | |
495 | * entry can be found in the out-parameters). | |
496 | * | |
497 | * If we ran out of reflog entries, the out-parameters are filled with the | |
498 | * details of the oldest entry we did find, and the function returns 1. Note | |
499 | * that there is one important special case here! If the reflog was empty | |
500 | * and the caller asked for the 0-th cnt, we will return "1" but leave the | |
501 | * "oid" field untouched. | |
502 | **/ | |
503 | int read_ref_at(struct ref_store *refs, | |
504 | const char *refname, unsigned int flags, | |
505 | timestamp_t at_time, int cnt, | |
506 | struct object_id *oid, char **msg, | |
507 | timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt); | |
508 | ||
509 | /** Check if a particular reflog exists */ | |
510 | int refs_reflog_exists(struct ref_store *refs, const char *refname); | |
511 | ||
512 | /* | |
513 | * Delete the specified reference. If old_oid is non-NULL, then | |
514 | * verify that the current value of the reference is old_oid before | |
515 | * deleting it. If old_oid is NULL, delete the reference if it | |
516 | * exists, regardless of its old value. It is an error for old_oid to | |
517 | * be null_oid. msg and flags are passed through to | |
518 | * ref_transaction_delete(). | |
519 | */ | |
520 | int refs_delete_ref(struct ref_store *refs, const char *msg, | |
521 | const char *refname, | |
522 | const struct object_id *old_oid, | |
523 | unsigned int flags); | |
524 | ||
525 | /* | |
526 | * Delete the specified references. If there are any problems, emit | |
527 | * errors but attempt to keep going (i.e., the deletes are not done in | |
528 | * an all-or-nothing transaction). msg and flags are passed through to | |
529 | * ref_transaction_delete(). | |
530 | */ | |
531 | int refs_delete_refs(struct ref_store *refs, const char *msg, | |
532 | struct string_list *refnames, unsigned int flags); | |
533 | ||
534 | /** Delete a reflog */ | |
535 | int refs_delete_reflog(struct ref_store *refs, const char *refname); | |
536 | ||
537 | /* | |
538 | * Callback to process a reflog entry found by the iteration functions (see | |
539 | * below). | |
540 | * | |
541 | * The committer parameter is a single string, in the form | |
542 | * "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" (without double quotes). | |
543 | * | |
544 | * The timestamp parameter gives the time when entry was created as the number | |
545 | * of seconds since the UNIX epoch. | |
546 | * | |
547 | * The tz parameter gives the timezone offset for the user who created | |
548 | * the reflog entry, and its value gives a positive or negative offset | |
549 | * from UTC. Its absolute value is formed by multiplying the hour | |
550 | * part by 100 and adding the minute part. For example, 1 hour ahead | |
551 | * of UTC, CET == "+0100", is represented as positive one hundred (not | |
552 | * positive sixty). | |
553 | * | |
554 | * The msg parameter is a single complete line; a reflog message given | |
555 | * to refs_delete_ref, refs_update_ref, etc. is returned to the | |
556 | * callback normalized---each run of whitespaces are squashed into a | |
557 | * single whitespace, trailing whitespace, if exists, is trimmed, and | |
558 | * then a single LF is added at the end. | |
559 | * | |
560 | * The cb_data is a caller-supplied pointer given to the iterator | |
561 | * functions. | |
562 | */ | |
563 | typedef int each_reflog_ent_fn(const char *refname, | |
564 | struct object_id *old_oid, | |
565 | struct object_id *new_oid, | |
566 | const char *committer, | |
567 | timestamp_t timestamp, | |
568 | int tz, const char *msg, | |
569 | void *cb_data); | |
570 | ||
571 | /* Iterate over reflog entries in the log for `refname`. */ | |
572 | ||
573 | /* oldest entry first */ | |
574 | int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname, | |
575 | each_reflog_ent_fn fn, void *cb_data); | |
576 | ||
577 | /* youngest entry first */ | |
578 | int refs_for_each_reflog_ent_reverse(struct ref_store *refs, | |
579 | const char *refname, | |
580 | each_reflog_ent_fn fn, | |
581 | void *cb_data); | |
582 | ||
583 | /* | |
584 | * The signature for the callback function for the refs_for_each_reflog() | |
585 | * functions below. The memory pointed to by the refname argument is only | |
586 | * guaranteed to be valid for the duration of a single callback invocation. | |
587 | */ | |
588 | typedef int each_reflog_fn(const char *refname, void *cb_data); | |
589 | ||
590 | /* | |
591 | * Calls the specified function for each reflog file until it returns nonzero, | |
592 | * and returns the value. Reflog file order is unspecified. | |
593 | */ | |
594 | int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data); | |
595 | ||
596 | #define REFNAME_ALLOW_ONELEVEL 1 | |
597 | #define REFNAME_REFSPEC_PATTERN 2 | |
598 | ||
599 | /* | |
600 | * Return 0 iff refname has the correct format for a refname according | |
601 | * to the rules described in Documentation/git-check-ref-format.adoc. | |
602 | * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level | |
603 | * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then | |
604 | * allow a single "*" wildcard character in the refspec. No leading or | |
605 | * repeated slashes are accepted. | |
606 | */ | |
607 | int check_refname_format(const char *refname, int flags); | |
608 | ||
609 | /* | |
610 | * Check the reference database for consistency. Return 0 if refs and | |
611 | * reflogs are consistent, and non-zero otherwise. The errors will be | |
612 | * written to stderr. | |
613 | */ | |
614 | int refs_fsck(struct ref_store *refs, struct fsck_options *o, | |
615 | struct worktree *wt); | |
616 | ||
617 | /* | |
618 | * Apply the rules from check_refname_format, but mutate the result until it | |
619 | * is acceptable, and place the result in "out". | |
620 | */ | |
621 | void sanitize_refname_component(const char *refname, struct strbuf *out); | |
622 | ||
623 | const char *prettify_refname(const char *refname); | |
624 | ||
625 | char *refs_shorten_unambiguous_ref(struct ref_store *refs, | |
626 | const char *refname, int strict); | |
627 | ||
628 | /** rename ref, return 0 on success **/ | |
629 | int refs_rename_ref(struct ref_store *refs, const char *oldref, | |
630 | const char *newref, const char *logmsg); | |
631 | ||
632 | /** copy ref, return 0 on success **/ | |
633 | int refs_copy_existing_ref(struct ref_store *refs, const char *oldref, | |
634 | const char *newref, const char *logmsg); | |
635 | ||
636 | int refs_update_symref(struct ref_store *refs, const char *refname, | |
637 | const char *target, const char *logmsg); | |
638 | ||
639 | int refs_update_symref_extended(struct ref_store *refs, const char *refname, | |
640 | const char *target, const char *logmsg, | |
641 | struct strbuf *referent, int create_only); | |
642 | ||
643 | enum action_on_err { | |
644 | UPDATE_REFS_MSG_ON_ERR, | |
645 | UPDATE_REFS_DIE_ON_ERR, | |
646 | UPDATE_REFS_QUIET_ON_ERR | |
647 | }; | |
648 | ||
649 | enum ref_transaction_flag { | |
650 | /* | |
651 | * The ref transaction is part of the initial creation of the ref store | |
652 | * and can thus assume that the ref store is completely empty. This | |
653 | * allows the backend to perform the transaction more efficiently by | |
654 | * skipping certain checks. | |
655 | * | |
656 | * It is a bug to set this flag when there might be other processes | |
657 | * accessing the repository or if there are existing references that | |
658 | * might conflict with the ones being created. All old_oid values must | |
659 | * either be absent or null_oid. | |
660 | */ | |
661 | REF_TRANSACTION_FLAG_INITIAL = (1 << 0), | |
662 | ||
663 | /* | |
664 | * The transaction mechanism by default fails all updates if any conflict | |
665 | * is detected. This flag allows transactions to partially apply updates | |
666 | * while rejecting updates which do not match the expected state. | |
667 | */ | |
668 | REF_TRANSACTION_ALLOW_FAILURE = (1 << 1), | |
669 | }; | |
670 | ||
671 | /* | |
672 | * Begin a reference transaction. The reference transaction must | |
673 | * be freed by calling ref_transaction_free(). | |
674 | */ | |
675 | struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs, | |
676 | unsigned int flags, | |
677 | struct strbuf *err); | |
678 | ||
679 | /* | |
680 | * Reference transaction updates | |
681 | * | |
682 | * The following four functions add a reference check or update to a | |
683 | * ref_transaction. They have some common similar parameters: | |
684 | * | |
685 | * transaction -- a pointer to an open ref_transaction, obtained | |
686 | * from ref_transaction_begin(). | |
687 | * | |
688 | * refname -- the name of the reference to be affected. | |
689 | * | |
690 | * new_oid -- the object ID that should be set to be the new value | |
691 | * of the reference. Some functions allow this parameter to be | |
692 | * NULL, meaning that the reference is not changed, or | |
693 | * null_oid, meaning that the reference should be deleted. A | |
694 | * copy of this value is made in the transaction. | |
695 | * | |
696 | * old_oid -- the object ID that the reference must have before | |
697 | * the update. Some functions allow this parameter to be NULL, | |
698 | * meaning that the old value of the reference is not checked, | |
699 | * or null_oid, meaning that the reference must not exist | |
700 | * before the update. A copy of this value is made in the | |
701 | * transaction. | |
702 | * | |
703 | * new_target -- the target reference that the reference will be | |
704 | * updated to point to. If the reference is a regular reference, | |
705 | * it will be converted to a symbolic reference. Cannot be set | |
706 | * together with `new_oid`. A copy of this value is made in the | |
707 | * transaction. | |
708 | * | |
709 | * old_target -- the reference that the reference must be pointing to. | |
710 | * Canont be set together with `old_oid`. A copy of this value is | |
711 | * made in the transaction. | |
712 | * | |
713 | * flags -- flags affecting the update, passed to | |
714 | * update_ref_lock(). Possible flags: REF_NO_DEREF, | |
715 | * REF_FORCE_CREATE_REFLOG. See those constants for more | |
716 | * information. | |
717 | * | |
718 | * msg -- a message describing the change (for the reflog). | |
719 | * | |
720 | * err -- a strbuf for receiving a description of any error that | |
721 | * might have occurred. | |
722 | * | |
723 | * The functions make internal copies of refname and msg, so the | |
724 | * caller retains ownership of these parameters. | |
725 | * | |
726 | * The functions return 0 on success and non-zero on failure. A | |
727 | * failure means that the transaction as a whole has failed and needs | |
728 | * to be rolled back. | |
729 | */ | |
730 | ||
731 | /* | |
732 | * The following flags can be passed to ref_transaction_update() etc. | |
733 | * Internally, they are stored in `ref_update::flags`, along with some | |
734 | * internal flags. | |
735 | */ | |
736 | ||
737 | /* | |
738 | * Act on the ref directly; i.e., without dereferencing symbolic refs. | |
739 | * If this flag is not specified, then symbolic references are | |
740 | * dereferenced and the update is applied to the referent. | |
741 | */ | |
742 | #define REF_NO_DEREF (1 << 0) | |
743 | ||
744 | /* | |
745 | * Force the creation of a reflog for this reference, even if it | |
746 | * didn't previously have a reflog. | |
747 | */ | |
748 | #define REF_FORCE_CREATE_REFLOG (1 << 1) | |
749 | ||
750 | /* | |
751 | * Blindly write an object_id. This is useful for testing data corruption | |
752 | * scenarios. | |
753 | */ | |
754 | #define REF_SKIP_OID_VERIFICATION (1 << 10) | |
755 | ||
756 | /* | |
757 | * Skip verifying refname. This is useful for testing data corruption scenarios. | |
758 | */ | |
759 | #define REF_SKIP_REFNAME_VERIFICATION (1 << 11) | |
760 | ||
761 | /* | |
762 | * Skip creation of a reflog entry, even if it would have otherwise been | |
763 | * created. | |
764 | */ | |
765 | #define REF_SKIP_CREATE_REFLOG (1 << 12) | |
766 | ||
767 | /* | |
768 | * When writing a REF_LOG_ONLY record, use the old and new object IDs provided | |
769 | * in the update instead of resolving the old object ID. The caller must also | |
770 | * set both REF_HAVE_OLD and REF_HAVE_NEW. | |
771 | */ | |
772 | #define REF_LOG_USE_PROVIDED_OIDS (1 << 13) | |
773 | ||
774 | /* | |
775 | * Bitmask of all of the flags that are allowed to be passed in to | |
776 | * ref_transaction_update() and friends: | |
777 | */ | |
778 | #define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS \ | |
779 | (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION | \ | |
780 | REF_SKIP_REFNAME_VERIFICATION | REF_SKIP_CREATE_REFLOG | REF_LOG_USE_PROVIDED_OIDS) | |
781 | ||
782 | /* | |
783 | * Add a reference update to transaction. `new_oid` is the value that | |
784 | * the reference should have after the update, or `null_oid` if it | |
785 | * should be deleted. If `new_oid` is NULL, then the reference is not | |
786 | * changed at all. `old_oid` is the value that the reference must have | |
787 | * before the update, or `null_oid` if it must not have existed | |
788 | * beforehand. The old value is checked after the lock is taken to | |
789 | * prevent races. If the old value doesn't agree with old_oid, the | |
790 | * whole transaction fails. If old_oid is NULL, then the previous | |
791 | * value is not checked. If `old_target` is not NULL, treat the reference | |
792 | * as a symbolic ref and validate that its target before the update is | |
793 | * `old_target`. If the `new_target` is not NULL, then the reference | |
794 | * will be updated to a symbolic ref which targets `new_target`. | |
795 | * Together, these allow us to update between regular refs and symrefs. | |
796 | * | |
797 | * See the above comment "Reference transaction updates" for more | |
798 | * information. | |
799 | */ | |
800 | int ref_transaction_update(struct ref_transaction *transaction, | |
801 | const char *refname, | |
802 | const struct object_id *new_oid, | |
803 | const struct object_id *old_oid, | |
804 | const char *new_target, | |
805 | const char *old_target, | |
806 | unsigned int flags, const char *msg, | |
807 | struct strbuf *err); | |
808 | ||
809 | /* | |
810 | * Similar to `ref_transaction_update`, but this function is only for adding | |
811 | * a reflog update. Supports providing custom committer information. The index | |
812 | * field can be utiltized to order updates as desired. When set to zero, the | |
813 | * updates default to being ordered by refname. | |
814 | */ | |
815 | int ref_transaction_update_reflog(struct ref_transaction *transaction, | |
816 | const char *refname, | |
817 | const struct object_id *new_oid, | |
818 | const struct object_id *old_oid, | |
819 | const char *committer_info, | |
820 | const char *msg, | |
821 | uint64_t index, | |
822 | struct strbuf *err); | |
823 | ||
824 | /* | |
825 | * Add a reference creation to transaction. new_oid is the value that | |
826 | * the reference should have after the update; it must not be | |
827 | * null_oid. It is verified that the reference does not exist | |
828 | * already. | |
829 | * | |
830 | * See the above comment "Reference transaction updates" for more | |
831 | * information. | |
832 | */ | |
833 | int ref_transaction_create(struct ref_transaction *transaction, | |
834 | const char *refname, | |
835 | const struct object_id *new_oid, | |
836 | const char *new_target, | |
837 | unsigned int flags, const char *msg, | |
838 | struct strbuf *err); | |
839 | ||
840 | /* | |
841 | * Add a reference deletion to transaction. If old_oid is non-NULL, | |
842 | * then it holds the value that the reference should have had before | |
843 | * the update (which must not be null_oid). | |
844 | * | |
845 | * See the above comment "Reference transaction updates" for more | |
846 | * information. | |
847 | */ | |
848 | int ref_transaction_delete(struct ref_transaction *transaction, | |
849 | const char *refname, | |
850 | const struct object_id *old_oid, | |
851 | const char *old_target, | |
852 | unsigned int flags, | |
853 | const char *msg, | |
854 | struct strbuf *err); | |
855 | ||
856 | /* | |
857 | * Verify, within a transaction, that refname has the value old_oid, | |
858 | * or, if old_oid is null_oid, then verify that the reference | |
859 | * doesn't exist. old_oid must be non-NULL. | |
860 | * | |
861 | * See the above comment "Reference transaction updates" for more | |
862 | * information. | |
863 | */ | |
864 | int ref_transaction_verify(struct ref_transaction *transaction, | |
865 | const char *refname, | |
866 | const struct object_id *old_oid, | |
867 | const char *old_target, | |
868 | unsigned int flags, | |
869 | struct strbuf *err); | |
870 | ||
871 | /* | |
872 | * Perform the preparatory stages of committing `transaction`. Acquire | |
873 | * any needed locks, check preconditions, etc.; basically, do as much | |
874 | * as possible to ensure that the transaction will be able to go | |
875 | * through, stopping just short of making any irrevocable or | |
876 | * user-visible changes. The updates that this function prepares can | |
877 | * be finished up by calling `ref_transaction_commit()` or rolled back | |
878 | * by calling `ref_transaction_abort()`. | |
879 | * | |
880 | * On success, return 0 and leave the transaction in "prepared" state. | |
881 | * On failure, abort the transaction, write an error message to `err`, | |
882 | * and return one of the `TRANSACTION_*` constants. | |
883 | * | |
884 | * Callers who don't need such fine-grained control over committing | |
885 | * reference transactions should just call `ref_transaction_commit()`. | |
886 | */ | |
887 | int ref_transaction_prepare(struct ref_transaction *transaction, | |
888 | struct strbuf *err); | |
889 | ||
890 | /* | |
891 | * Commit all of the changes that have been queued in transaction, as | |
892 | * atomically as possible. On success, return 0 and leave the | |
893 | * transaction in "closed" state. On failure, roll back the | |
894 | * transaction, write an error message to `err`, and return one of the | |
895 | * `TRANSACTION_*` constants | |
896 | */ | |
897 | int ref_transaction_commit(struct ref_transaction *transaction, | |
898 | struct strbuf *err); | |
899 | ||
900 | /* | |
901 | * Abort `transaction`, which has been begun and possibly prepared, | |
902 | * but not yet committed. | |
903 | */ | |
904 | int ref_transaction_abort(struct ref_transaction *transaction, | |
905 | struct strbuf *err); | |
906 | ||
907 | /* | |
908 | * Execute the given callback function for each of the reference updates which | |
909 | * have been queued in the given transaction. `old_oid` and `new_oid` may be | |
910 | * `NULL` pointers depending on whether the update has these object IDs set or | |
911 | * not. | |
912 | */ | |
913 | typedef void ref_transaction_for_each_queued_update_fn(const char *refname, | |
914 | const struct object_id *old_oid, | |
915 | const struct object_id *new_oid, | |
916 | void *cb_data); | |
917 | void ref_transaction_for_each_queued_update(struct ref_transaction *transaction, | |
918 | ref_transaction_for_each_queued_update_fn cb, | |
919 | void *cb_data); | |
920 | ||
921 | /* | |
922 | * Execute the given callback function for each of the reference updates which | |
923 | * have been rejected in the given transaction. | |
924 | */ | |
925 | typedef void ref_transaction_for_each_rejected_update_fn(const char *refname, | |
926 | const struct object_id *old_oid, | |
927 | const struct object_id *new_oid, | |
928 | const char *old_target, | |
929 | const char *new_target, | |
930 | enum ref_transaction_error err, | |
931 | void *cb_data); | |
932 | void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction, | |
933 | ref_transaction_for_each_rejected_update_fn cb, | |
934 | void *cb_data); | |
935 | ||
936 | /* | |
937 | * Translate errors to human readable error messages. | |
938 | */ | |
939 | const char *ref_transaction_error_msg(enum ref_transaction_error err); | |
940 | ||
941 | /* | |
942 | * Free `*transaction` and all associated data. | |
943 | */ | |
944 | void ref_transaction_free(struct ref_transaction *transaction); | |
945 | ||
946 | /** | |
947 | * Lock, update, and unlock a single reference. This function | |
948 | * basically does a transaction containing a single call to | |
949 | * ref_transaction_update(). The parameters to this function have the | |
950 | * same meaning as the corresponding parameters to | |
951 | * ref_transaction_update(). Handle errors as requested by the `onerr` | |
952 | * argument. | |
953 | */ | |
954 | int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname, | |
955 | const struct object_id *new_oid, const struct object_id *old_oid, | |
956 | unsigned int flags, enum action_on_err onerr); | |
957 | ||
958 | int parse_hide_refs_config(const char *var, const char *value, const char *, | |
959 | struct strvec *); | |
960 | ||
961 | /* | |
962 | * Check whether a ref is hidden. If no namespace is set, both the first and | |
963 | * the second parameter point to the full ref name. If a namespace is set and | |
964 | * the ref is inside that namespace, the first parameter is a pointer to the | |
965 | * name of the ref with the namespace prefix removed. If a namespace is set and | |
966 | * the ref is outside that namespace, the first parameter is NULL. The second | |
967 | * parameter always points to the full ref name. | |
968 | */ | |
969 | int ref_is_hidden(const char *, const char *, const struct strvec *); | |
970 | ||
971 | /* | |
972 | * Returns an array of patterns to use as excluded_patterns, if none of the | |
973 | * hidden references use the token '!' or '^'. | |
974 | */ | |
975 | const char **hidden_refs_to_excludes(const struct strvec *hide_refs); | |
976 | ||
977 | /* | |
978 | * Prefix all exclude patterns with the namespace, if any. This is required | |
979 | * because exclude patterns apply to the stripped reference name, not the full | |
980 | * reference name with the namespace. | |
981 | */ | |
982 | const char **get_namespaced_exclude_patterns(const char **exclude_patterns, | |
983 | const char *namespace, | |
984 | struct strvec *out); | |
985 | ||
986 | /* Is this a per-worktree ref living in the refs/ namespace? */ | |
987 | int is_per_worktree_ref(const char *refname); | |
988 | ||
989 | /* Describes how a refname relates to worktrees */ | |
990 | enum ref_worktree_type { | |
991 | REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or | |
992 | refs/bisect/SOMETHING */ | |
993 | REF_WORKTREE_MAIN, /* explicitly in main worktree, eg. | |
994 | main-worktree/HEAD */ | |
995 | REF_WORKTREE_OTHER, /* explicitly in named worktree, eg. | |
996 | worktrees/bla/HEAD */ | |
997 | REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */ | |
998 | }; | |
999 | ||
1000 | /* | |
1001 | * Parse a `maybe_worktree_ref` as a ref that possibly refers to a worktree ref | |
1002 | * (ie. either REFNAME, main-worktree/REFNAME or worktree/WORKTREE/REFNAME). It | |
1003 | * returns what kind of ref was found, and in case of REF_WORKTREE_OTHER, the | |
1004 | * worktree name is returned in `worktree_name` (pointing into | |
1005 | * `maybe_worktree_ref`) and `worktree_name_length`. The bare refname (the | |
1006 | * refname stripped of prefixes) is returned in `bare_refname`. The | |
1007 | * `worktree_name`, `worktree_name_length` and `bare_refname` arguments may be | |
1008 | * NULL. | |
1009 | */ | |
1010 | enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref, | |
1011 | const char **worktree_name, | |
1012 | int *worktree_name_length, | |
1013 | const char **bare_refname); | |
1014 | ||
1015 | enum expire_reflog_flags { | |
1016 | EXPIRE_REFLOGS_DRY_RUN = 1 << 0, | |
1017 | EXPIRE_REFLOGS_UPDATE_REF = 1 << 1, | |
1018 | EXPIRE_REFLOGS_REWRITE = 1 << 2, | |
1019 | }; | |
1020 | ||
1021 | /* | |
1022 | * The following interface is used for reflog expiration. The caller | |
1023 | * calls refs_reflog_expire(), supplying it with three callback functions, | |
1024 | * of the following types. The callback functions define the | |
1025 | * expiration policy that is desired. | |
1026 | * | |
1027 | * reflog_expiry_prepare_fn -- Called once after the reference is | |
1028 | * locked. Called with the OID of the locked reference. | |
1029 | * | |
1030 | * reflog_expiry_should_prune_fn -- Called once for each entry in the | |
1031 | * existing reflog. It should return true iff that entry should be | |
1032 | * pruned. | |
1033 | * | |
1034 | * reflog_expiry_cleanup_fn -- Called once before the reference is | |
1035 | * unlocked again. | |
1036 | */ | |
1037 | typedef void reflog_expiry_prepare_fn(const char *refname, | |
1038 | const struct object_id *oid, | |
1039 | void *cb_data); | |
1040 | typedef int reflog_expiry_should_prune_fn(struct object_id *ooid, | |
1041 | struct object_id *noid, | |
1042 | const char *email, | |
1043 | timestamp_t timestamp, int tz, | |
1044 | const char *message, void *cb_data); | |
1045 | typedef void reflog_expiry_cleanup_fn(void *cb_data); | |
1046 | ||
1047 | /* | |
1048 | * Expire reflog entries for the specified reference. | |
1049 | * flags is a combination of the constants in | |
1050 | * enum expire_reflog_flags. The three function pointers are described | |
1051 | * above. On success, return zero. | |
1052 | */ | |
1053 | int refs_reflog_expire(struct ref_store *refs, | |
1054 | const char *refname, | |
1055 | unsigned int flags, | |
1056 | reflog_expiry_prepare_fn prepare_fn, | |
1057 | reflog_expiry_should_prune_fn should_prune_fn, | |
1058 | reflog_expiry_cleanup_fn cleanup_fn, | |
1059 | void *policy_cb_data); | |
1060 | ||
1061 | struct ref_store *get_main_ref_store(struct repository *r); | |
1062 | ||
1063 | /** | |
1064 | * Submodules | |
1065 | * ---------- | |
1066 | * | |
1067 | * If you want to iterate the refs of a submodule you first need to add the | |
1068 | * submodules object database. You can do this by a code-snippet like | |
1069 | * this: | |
1070 | * | |
1071 | * const char *path = "path/to/submodule" | |
1072 | * if (add_submodule_odb(path)) | |
1073 | * die("Error submodule '%s' not populated.", path); | |
1074 | * | |
1075 | * `add_submodule_odb()` will return zero on success. If you | |
1076 | * do not do this you will get an error for each ref that it does not point | |
1077 | * to a valid object. | |
1078 | * | |
1079 | * Note: As a side-effect of this you cannot safely assume that all | |
1080 | * objects you lookup are available in superproject. All submodule objects | |
1081 | * will be available the same way as the superprojects objects. | |
1082 | * | |
1083 | * Example: | |
1084 | * -------- | |
1085 | * | |
1086 | * ---- | |
1087 | * static int handle_remote_ref(const char *refname, | |
1088 | * const unsigned char *sha1, int flags, void *cb_data) | |
1089 | * { | |
1090 | * struct strbuf *output = cb_data; | |
1091 | * strbuf_addf(output, "%s\n", refname); | |
1092 | * return 0; | |
1093 | * } | |
1094 | * | |
1095 | */ | |
1096 | ||
1097 | /* | |
1098 | * Return the ref_store instance for the specified submodule. For the | |
1099 | * main repository, use submodule==NULL; such a call cannot fail. For | |
1100 | * a submodule, the submodule must exist and be a nonbare repository, | |
1101 | * otherwise return NULL. If the requested reference store has not yet | |
1102 | * been initialized, initialize it first. | |
1103 | * | |
1104 | * For backwards compatibility, submodule=="" is treated the same as | |
1105 | * submodule==NULL. | |
1106 | */ | |
1107 | struct ref_store *repo_get_submodule_ref_store(struct repository *repo, | |
1108 | const char *submodule); | |
1109 | struct ref_store *get_worktree_ref_store(const struct worktree *wt); | |
1110 | ||
1111 | /* | |
1112 | * Some of the names specified by refs have special meaning to Git. | |
1113 | * Organize these namespaces in a common 'ref_namespace' array for | |
1114 | * reference from multiple places in the codebase. | |
1115 | */ | |
1116 | ||
1117 | struct ref_namespace_info { | |
1118 | const char *ref; | |
1119 | enum decoration_type decoration; | |
1120 | ||
1121 | /* | |
1122 | * If 'exact' is true, then we must match the 'ref' exactly. | |
1123 | * Otherwise, use a prefix match. | |
1124 | * | |
1125 | * 'ref_updated' is for internal use. It represents whether the | |
1126 | * 'ref' value was replaced from its original literal version. | |
1127 | */ | |
1128 | unsigned exact:1, | |
1129 | ref_updated:1; | |
1130 | }; | |
1131 | ||
1132 | enum ref_namespace { | |
1133 | NAMESPACE_HEAD, | |
1134 | NAMESPACE_BRANCHES, | |
1135 | NAMESPACE_TAGS, | |
1136 | NAMESPACE_REMOTE_REFS, | |
1137 | NAMESPACE_STASH, | |
1138 | NAMESPACE_REPLACE, | |
1139 | NAMESPACE_NOTES, | |
1140 | NAMESPACE_PREFETCH, | |
1141 | NAMESPACE_REWRITTEN, | |
1142 | ||
1143 | /* Must be last */ | |
1144 | NAMESPACE__COUNT | |
1145 | }; | |
1146 | ||
1147 | /* See refs.c for the contents of this array. */ | |
1148 | extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT]; | |
1149 | ||
1150 | /* | |
1151 | * Some ref namespaces can be modified by config values or environment | |
1152 | * variables. Modify a namespace as specified by its ref_namespace key. | |
1153 | */ | |
1154 | void update_ref_namespace(enum ref_namespace namespace, char *ref); | |
1155 | ||
1156 | /* | |
1157 | * Check whether the provided name names a root reference. This function only | |
1158 | * performs a syntactic check. | |
1159 | * | |
1160 | * A root ref is a reference that lives in the root of the reference hierarchy. | |
1161 | * These references must conform to special syntax: | |
1162 | * | |
1163 | * - Their name must be all-uppercase or underscores ("_"). | |
1164 | * | |
1165 | * - Their name must end with "_HEAD". As a special rule, "HEAD" is a root | |
1166 | * ref, as well. | |
1167 | * | |
1168 | * - Their name may not contain a slash. | |
1169 | * | |
1170 | * There is a special set of irregular root refs that exist due to historic | |
1171 | * reasons, only. This list shall not be expanded in the future: | |
1172 | * | |
1173 | * - AUTO_MERGE | |
1174 | * | |
1175 | * - BISECT_EXPECTED_REV | |
1176 | * | |
1177 | * - NOTES_MERGE_PARTIAL | |
1178 | * | |
1179 | * - NOTES_MERGE_REF | |
1180 | * | |
1181 | * - MERGE_AUTOSTASH | |
1182 | */ | |
1183 | int is_root_ref(const char *refname); | |
1184 | ||
1185 | /* | |
1186 | * Pseudorefs are refs that have different semantics compared to | |
1187 | * "normal" refs. These refs can thus not be stored in the ref backend, | |
1188 | * but must always be accessed via the filesystem. The following refs | |
1189 | * are pseudorefs: | |
1190 | * | |
1191 | * - FETCH_HEAD may contain multiple object IDs, and each one of them | |
1192 | * carries additional metadata like where it came from. | |
1193 | * | |
1194 | * - MERGE_HEAD may contain multiple object IDs when merging multiple | |
1195 | * heads. | |
1196 | * | |
1197 | * Reading, writing or deleting references must consistently go either | |
1198 | * through the filesystem (pseudorefs) or through the reference | |
1199 | * backend (normal ones). | |
1200 | */ | |
1201 | int is_pseudo_ref(const char *refname); | |
1202 | ||
1203 | /* | |
1204 | * The following flags can be passed to `repo_migrate_ref_storage_format()`: | |
1205 | * | |
1206 | * - REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN: perform a dry-run migration | |
1207 | * without touching the main repository. The result will be written into a | |
1208 | * temporary ref storage directory. | |
1209 | * | |
1210 | * - REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG: skip migration of reflogs. | |
1211 | */ | |
1212 | #define REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN (1 << 0) | |
1213 | #define REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG (1 << 1) | |
1214 | ||
1215 | /* | |
1216 | * Migrate the ref storage format used by the repository to the | |
1217 | * specified one. | |
1218 | */ | |
1219 | int repo_migrate_ref_storage_format(struct repository *repo, | |
1220 | enum ref_storage_format format, | |
1221 | unsigned int flags, | |
1222 | struct strbuf *err); | |
1223 | ||
1224 | /* | |
1225 | * Reference iterators | |
1226 | * | |
1227 | * A reference iterator encapsulates the state of an in-progress | |
1228 | * iteration over references. Create an instance of `struct | |
1229 | * ref_iterator` via one of the functions in this module. | |
1230 | * | |
1231 | * A freshly-created ref_iterator doesn't yet point at a reference. To | |
1232 | * advance the iterator, call ref_iterator_advance(). If successful, | |
1233 | * this sets the iterator's refname, oid, and flags fields to describe | |
1234 | * the next reference and returns ITER_OK. The data pointed at by | |
1235 | * refname and oid belong to the iterator; if you want to retain them | |
1236 | * after calling ref_iterator_advance() again or calling | |
1237 | * ref_iterator_free(), you must make a copy. When the iteration has | |
1238 | * been exhausted, ref_iterator_advance() releases any resources | |
1239 | * associated with the iteration, frees the ref_iterator object, and | |
1240 | * returns ITER_DONE. If you want to abort the iteration early, call | |
1241 | * ref_iterator_free(), which also frees the ref_iterator object and | |
1242 | * any associated resources. If there was an internal error advancing | |
1243 | * to the next entry, ref_iterator_advance() aborts the iteration, | |
1244 | * frees the ref_iterator, and returns ITER_ERROR. | |
1245 | * | |
1246 | * The reference currently being looked at can be peeled by calling | |
1247 | * ref_iterator_peel(). This function is often faster than peel_ref(), | |
1248 | * so it should be preferred when iterating over references. | |
1249 | * | |
1250 | * Putting it all together, a typical iteration looks like this: | |
1251 | * | |
1252 | * int ok; | |
1253 | * struct ref_iterator *iter = ...; | |
1254 | * | |
1255 | * while ((ok = ref_iterator_advance(iter)) == ITER_OK) { | |
1256 | * if (want_to_stop_iteration()) { | |
1257 | * ok = ITER_DONE; | |
1258 | * break; | |
1259 | * } | |
1260 | * | |
1261 | * // Access information about the current reference: | |
1262 | * if (!(iter->flags & REF_ISSYMREF)) | |
1263 | * printf("%s is %s\n", iter->refname, oid_to_hex(iter->oid)); | |
1264 | * | |
1265 | * // If you need to peel the reference: | |
1266 | * ref_iterator_peel(iter, &oid); | |
1267 | * } | |
1268 | * | |
1269 | * if (ok != ITER_DONE) | |
1270 | * handle_error(); | |
1271 | * ref_iterator_free(iter); | |
1272 | */ | |
1273 | struct ref_iterator; | |
1274 | ||
1275 | /* | |
1276 | * These flags are passed to refs_ref_iterator_begin() (and do_for_each_ref(), | |
1277 | * which feeds it). | |
1278 | */ | |
1279 | enum do_for_each_ref_flags { | |
1280 | /* | |
1281 | * Include broken references in a do_for_each_ref*() iteration, which | |
1282 | * would normally be omitted. This includes both refs that point to | |
1283 | * missing objects (a true repository corruption), ones with illegal | |
1284 | * names (which we prefer not to expose to callers), as well as | |
1285 | * dangling symbolic refs (i.e., those that point to a non-existent | |
1286 | * ref; this is not a corruption, but as they have no valid oid, we | |
1287 | * omit them from normal iteration results). | |
1288 | */ | |
1289 | DO_FOR_EACH_INCLUDE_BROKEN = (1 << 0), | |
1290 | ||
1291 | /* | |
1292 | * Only include per-worktree refs in a do_for_each_ref*() iteration. | |
1293 | * Normally this will be used with a files ref_store, since that's | |
1294 | * where all reference backends will presumably store their | |
1295 | * per-worktree refs. | |
1296 | */ | |
1297 | DO_FOR_EACH_PER_WORKTREE_ONLY = (1 << 1), | |
1298 | ||
1299 | /* | |
1300 | * Omit dangling symrefs from output; this only has an effect with | |
1301 | * INCLUDE_BROKEN, since they are otherwise not included at all. | |
1302 | */ | |
1303 | DO_FOR_EACH_OMIT_DANGLING_SYMREFS = (1 << 2), | |
1304 | ||
1305 | /* | |
1306 | * Include root refs i.e. HEAD and pseudorefs along with the regular | |
1307 | * refs. | |
1308 | */ | |
1309 | DO_FOR_EACH_INCLUDE_ROOT_REFS = (1 << 3), | |
1310 | }; | |
1311 | ||
1312 | /* | |
1313 | * Return an iterator that goes over each reference in `refs` for | |
1314 | * which the refname begins with prefix. If trim is non-zero, then | |
1315 | * trim that many characters off the beginning of each refname. | |
1316 | * The output is ordered by refname. | |
1317 | */ | |
1318 | struct ref_iterator *refs_ref_iterator_begin( | |
1319 | struct ref_store *refs, | |
1320 | const char *prefix, const char **exclude_patterns, | |
1321 | int trim, enum do_for_each_ref_flags flags); | |
1322 | ||
1323 | /* | |
1324 | * Advance the iterator to the first or next item and return ITER_OK. | |
1325 | * If the iteration is exhausted, free the resources associated with | |
1326 | * the ref_iterator and return ITER_DONE. On errors, free the iterator | |
1327 | * resources and return ITER_ERROR. It is a bug to use ref_iterator or | |
1328 | * call this function again after it has returned ITER_DONE or | |
1329 | * ITER_ERROR. | |
1330 | */ | |
1331 | int ref_iterator_advance(struct ref_iterator *ref_iterator); | |
1332 | ||
1333 | enum ref_iterator_seek_flag { | |
1334 | /* | |
1335 | * When the REF_ITERATOR_SEEK_SET_PREFIX flag is set, the iterator's prefix is | |
1336 | * updated to match the provided string, affecting all subsequent iterations. If | |
1337 | * not, the iterator seeks to the specified reference and clears any previously | |
1338 | * set prefix. | |
1339 | */ | |
1340 | REF_ITERATOR_SEEK_SET_PREFIX = (1 << 0), | |
1341 | }; | |
1342 | ||
1343 | /* | |
1344 | * Seek the iterator to the first reference matching the given seek string. | |
1345 | * The seek string is matched as a literal string, without regard for path | |
1346 | * separators. If seek is NULL or the empty string, seek the iterator to the | |
1347 | * first reference again. | |
1348 | * | |
1349 | * This function is expected to behave as if a new ref iterator has been | |
1350 | * created, but allows reuse of existing iterators for optimization. | |
1351 | * | |
1352 | * Returns 0 on success, a negative error code otherwise. | |
1353 | */ | |
1354 | int ref_iterator_seek(struct ref_iterator *ref_iterator, const char *refname, | |
1355 | unsigned int flags); | |
1356 | ||
1357 | /* | |
1358 | * If possible, peel the reference currently being viewed by the | |
1359 | * iterator. Return 0 on success. | |
1360 | */ | |
1361 | int ref_iterator_peel(struct ref_iterator *ref_iterator, | |
1362 | struct object_id *peeled); | |
1363 | ||
1364 | /* Free the reference iterator and any associated resources. */ | |
1365 | void ref_iterator_free(struct ref_iterator *ref_iterator); | |
1366 | ||
1367 | /* | |
1368 | * The common backend for the for_each_*ref* functions. Call fn for | |
1369 | * each reference in iter. If the iterator itself ever returns | |
1370 | * ITER_ERROR, return -1. If fn ever returns a non-zero value, stop | |
1371 | * the iteration and return that value. Otherwise, return 0. In any | |
1372 | * case, free the iterator when done. This function is basically an | |
1373 | * adapter between the callback style of reference iteration and the | |
1374 | * iterator style. | |
1375 | */ | |
1376 | int do_for_each_ref_iterator(struct ref_iterator *iter, | |
1377 | each_ref_fn fn, void *cb_data); | |
1378 | ||
1379 | #endif /* REFS_H */ |