]>
Commit | Line | Data |
---|---|---|
4cb77009 MH |
1 | #ifndef REFS_REFS_INTERNAL_H |
2 | #define REFS_REFS_INTERNAL_H | |
3 | ||
4eb4416d | 4 | #include "refs.h" |
13f925f3 | 5 | #include "iterator.h" |
c3baddf0 | 6 | #include "string-list.h" |
91c2f204 | 7 | |
ab6f79d8 | 8 | struct fsck_options; |
4eb4416d RJ |
9 | struct ref_transaction; |
10 | ||
4cb77009 MH |
11 | /* |
12 | * Data structures and functions for the internal use of the refs | |
13 | * module. Code outside of the refs module should use only the public | |
14 | * functions defined in "refs.h", and should *not* include this file. | |
15 | */ | |
16 | ||
17 | /* | |
5ac95fee | 18 | * The following flags can appear in `ref_update::flags`. Their |
91774afc | 19 | * numerical values must not conflict with those of REF_NO_DEREF and |
5ac95fee MH |
20 | * REF_FORCE_CREATE_REFLOG, which are also stored in |
21 | * `ref_update::flags`. | |
4cb77009 | 22 | */ |
4cb77009 MH |
23 | |
24 | /* | |
78fb4579 | 25 | * The reference should be updated to new_oid. |
4cb77009 | 26 | */ |
5ac95fee | 27 | #define REF_HAVE_NEW (1 << 2) |
4cb77009 MH |
28 | |
29 | /* | |
5ac95fee | 30 | * The current reference's value should be checked to make sure that |
78fb4579 | 31 | * it agrees with old_oid. |
4cb77009 | 32 | */ |
5ac95fee | 33 | #define REF_HAVE_OLD (1 << 3) |
44639777 | 34 | |
63c05673 HWN |
35 | /* |
36 | * Used as a flag in ref_update::flags when we want to log a ref | |
37 | * update but not actually perform it. This is used when a symbolic | |
38 | * ref update is split up. | |
39 | */ | |
40 | #define REF_LOG_ONLY (1 << 7) | |
41 | ||
4ff0f01c MH |
42 | /* |
43 | * Return the length of time to retry acquiring a loose reference lock | |
44 | * before giving up, in milliseconds: | |
45 | */ | |
46 | long get_files_ref_lock_timeout_ms(void); | |
47 | ||
4cb77009 MH |
48 | /* |
49 | * Return true iff refname is minimally safe. "Safe" here means that | |
50 | * deleting a loose reference by this name will not do any damage, for | |
51 | * example by causing a file that is not a reference to be deleted. | |
52 | * This function does not check that the reference name is legal; for | |
53 | * that, use check_refname_format(). | |
54 | * | |
15ee2c72 MH |
55 | * A refname that starts with "refs/" is considered safe iff it |
56 | * doesn't contain any "." or ".." components or consecutive '/' | |
57 | * characters, end with '/', or (on Windows) contain any '\' | |
58 | * characters. Names that do not start with "refs/" are considered | |
59 | * safe iff they consist entirely of upper case characters and '_' | |
60 | * (like "HEAD" and "MERGE_HEAD" but not "config" or "FOO/BAR"). | |
4cb77009 MH |
61 | */ |
62 | int refname_is_safe(const char *refname); | |
63 | ||
67be7c5a MH |
64 | /* |
65 | * Helper function: return true if refname, which has the specified | |
66 | * oid and flags, can be resolved to an object in the database. If the | |
67 | * referred-to object does not exist, emit a warning and return false. | |
68 | */ | |
69 | int ref_resolves_to_object(const char *refname, | |
9bc45a28 | 70 | struct repository *repo, |
67be7c5a MH |
71 | const struct object_id *oid, |
72 | unsigned int flags); | |
73 | ||
4cb77009 | 74 | /** |
78fb4579 MH |
75 | * Information needed for a single ref update. Set new_oid to the new |
76 | * value or to null_oid to delete the ref. To check the old value | |
77 | * while the ref is locked, set (flags & REF_HAVE_OLD) and set old_oid | |
78 | * to the old value, or to null_oid to ensure the ref does not exist | |
79 | * before update. | |
4cb77009 MH |
80 | */ |
81 | struct ref_update { | |
82 | /* | |
5ac95fee MH |
83 | * If (flags & REF_HAVE_NEW), set the reference to this value |
84 | * (or delete it, if `new_oid` is `null_oid`). | |
4cb77009 | 85 | */ |
98491298 | 86 | struct object_id new_oid; |
6e30b2f6 | 87 | |
4cb77009 MH |
88 | /* |
89 | * If (flags & REF_HAVE_OLD), check that the reference | |
5ac95fee MH |
90 | * previously had this value (or didn't previously exist, if |
91 | * `old_oid` is `null_oid`). | |
4cb77009 | 92 | */ |
98491298 | 93 | struct object_id old_oid; |
6e30b2f6 | 94 | |
1bc4cc3f KN |
95 | /* |
96 | * If set, point the reference to this value. This can also be | |
97 | * used to convert regular references to become symbolic refs. | |
98 | * Cannot be set together with `new_oid`. | |
99 | */ | |
100 | const char *new_target; | |
101 | ||
102 | /* | |
103 | * If set, check that the reference previously pointed to this | |
104 | * value. Cannot be set together with `old_oid`. | |
105 | */ | |
106 | const char *old_target; | |
107 | ||
4cb77009 | 108 | /* |
91774afc | 109 | * One or more of REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, |
5ac95fee | 110 | * REF_HAVE_NEW, REF_HAVE_OLD, or backend-specific flags. |
4cb77009 MH |
111 | */ |
112 | unsigned int flags; | |
6e30b2f6 | 113 | |
7d618264 | 114 | void *backend_data; |
92b1551b | 115 | unsigned int type; |
4cb77009 | 116 | char *msg; |
1a83e26d | 117 | char *committer_info; |
6e30b2f6 | 118 | |
a3582e2e KN |
119 | /* |
120 | * The index overrides the default sort algorithm. This is needed | |
121 | * when migrating reflogs and we want to ensure we carry over the | |
122 | * same order. | |
123 | */ | |
e7c1b9f1 | 124 | uint64_t index; |
6e30b2f6 | 125 | |
23fc8e4f KN |
126 | /* |
127 | * Used in batched reference updates to mark if a given update | |
128 | * was rejected. | |
129 | */ | |
130 | enum ref_transaction_error rejection_err; | |
131 | ||
6e30b2f6 MH |
132 | /* |
133 | * If this ref_update was split off of a symref update via | |
134 | * split_symref_update(), then this member points at that | |
135 | * update. This is used for two purposes: | |
136 | * 1. When reporting errors, we report the refname under which | |
137 | * the update was originally requested. | |
138 | * 2. When we read the old value of this reference, we | |
139 | * propagate it back to its parent update for recording in | |
140 | * the latter's reflog. | |
141 | */ | |
142 | struct ref_update *parent_update; | |
143 | ||
4cb77009 MH |
144 | const char refname[FLEX_ARRAY]; |
145 | }; | |
146 | ||
8b72fea7 HWN |
147 | int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, |
148 | struct object_id *oid, struct strbuf *referent, | |
149 | unsigned int *type, int *failure_errno); | |
470be518 | 150 | |
23fc8e4f KN |
151 | /* |
152 | * Mark a given update as rejected with a given reason. | |
153 | */ | |
154 | int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction, | |
155 | size_t update_idx, | |
156 | enum ref_transaction_error err); | |
157 | ||
71564516 MH |
158 | /* |
159 | * Add a ref_update with the specified properties to transaction, and | |
160 | * return a pointer to the new object. This function does not verify | |
78fb4579 | 161 | * that refname is well-formed. new_oid and old_oid are only |
71564516 MH |
162 | * dereferenced if the REF_HAVE_NEW and REF_HAVE_OLD bits, |
163 | * respectively, are set in flags. | |
164 | */ | |
165 | struct ref_update *ref_transaction_add_update( | |
166 | struct ref_transaction *transaction, | |
167 | const char *refname, unsigned int flags, | |
89f3bbdd | 168 | const struct object_id *new_oid, |
169 | const struct object_id *old_oid, | |
1bc4cc3f | 170 | const char *new_target, const char *old_target, |
4483be36 | 171 | const char *committer_info, |
71564516 MH |
172 | const char *msg); |
173 | ||
4cb77009 MH |
174 | /* |
175 | * Transaction states. | |
30173b88 MH |
176 | * |
177 | * OPEN: The transaction is initialized and new updates can still be | |
178 | * added to it. An OPEN transaction can be prepared, | |
179 | * committed, freed, or aborted (freeing and aborting an open | |
180 | * transaction are equivalent). | |
181 | * | |
182 | * PREPARED: ref_transaction_prepare(), which locks all of the | |
183 | * references involved in the update and checks that the | |
184 | * update has no errors, has been called successfully for the | |
185 | * transaction. A PREPARED transaction can be committed or | |
186 | * aborted. | |
187 | * | |
188 | * CLOSED: The transaction is no longer active. A transaction becomes | |
189 | * CLOSED if there is a failure while building the transaction | |
190 | * or if a transaction is committed or aborted. A CLOSED | |
191 | * transaction can only be freed. | |
4cb77009 MH |
192 | */ |
193 | enum ref_transaction_state { | |
30173b88 MH |
194 | REF_TRANSACTION_OPEN = 0, |
195 | REF_TRANSACTION_PREPARED = 1, | |
196 | REF_TRANSACTION_CLOSED = 2 | |
4cb77009 MH |
197 | }; |
198 | ||
23fc8e4f KN |
199 | /* |
200 | * Data structure to hold indices of updates which were rejected, for batched | |
201 | * reference updates. While the updates themselves hold the rejection error, | |
202 | * this structure allows a transaction to iterate only over the rejected | |
203 | * updates. | |
204 | */ | |
205 | struct ref_transaction_rejections { | |
206 | size_t *update_indices; | |
207 | size_t alloc; | |
208 | size_t nr; | |
209 | }; | |
210 | ||
4cb77009 MH |
211 | /* |
212 | * Data structure for holding a reference transaction, which can | |
213 | * consist of checks and updates to multiple references, carried out | |
214 | * as atomically as possible. This structure is opaque to callers. | |
215 | */ | |
216 | struct ref_transaction { | |
c0fe4e8b | 217 | struct ref_store *ref_store; |
4cb77009 | 218 | struct ref_update **updates; |
c3baddf0 | 219 | struct string_list refnames; |
4cb77009 MH |
220 | size_t alloc; |
221 | size_t nr; | |
222 | enum ref_transaction_state state; | |
23fc8e4f | 223 | struct ref_transaction_rejections *rejections; |
3bf4f561 | 224 | void *backend_data; |
a0efef14 | 225 | unsigned int flags; |
e7c1b9f1 | 226 | uint64_t max_index; |
4cb77009 MH |
227 | }; |
228 | ||
0845122c DT |
229 | /* |
230 | * Check for entries in extras that are within the specified | |
231 | * directory, where dirname is a reference directory name including | |
232 | * the trailing slash (e.g., "refs/heads/foo/"). Ignore any | |
233 | * conflicting references that are found in skip. If there is a | |
234 | * conflicting reference, return its name. | |
235 | * | |
236 | * extras and skip must be sorted lists of reference names. Either one | |
237 | * can be NULL, signifying the empty list. | |
238 | */ | |
239 | const char *find_descendant_ref(const char *dirname, | |
240 | const struct string_list *extras, | |
241 | const struct string_list *skip); | |
242 | ||
2d0663b2 DT |
243 | /* We allow "recursive" symbolic refs. Only within reason, though */ |
244 | #define SYMREF_MAXDEPTH 5 | |
93770590 | 245 | |
bf708add | 246 | /* |
9aab952e JK |
247 | * These flags are passed to refs_ref_iterator_begin() (and do_for_each_ref(), |
248 | * which feeds it). | |
bf708add | 249 | */ |
9aab952e JK |
250 | enum do_for_each_ref_flags { |
251 | /* | |
252 | * Include broken references in a do_for_each_ref*() iteration, which | |
253 | * would normally be omitted. This includes both refs that point to | |
254 | * missing objects (a true repository corruption), ones with illegal | |
255 | * names (which we prefer not to expose to callers), as well as | |
256 | * dangling symbolic refs (i.e., those that point to a non-existent | |
257 | * ref; this is not a corruption, but as they have no valid oid, we | |
258 | * omit them from normal iteration results). | |
259 | */ | |
260 | DO_FOR_EACH_INCLUDE_BROKEN = (1 << 0), | |
261 | ||
262 | /* | |
263 | * Only include per-worktree refs in a do_for_each_ref*() iteration. | |
264 | * Normally this will be used with a files ref_store, since that's | |
265 | * where all reference backends will presumably store their | |
266 | * per-worktree refs. | |
267 | */ | |
268 | DO_FOR_EACH_PER_WORKTREE_ONLY = (1 << 1), | |
8dccb224 JK |
269 | |
270 | /* | |
271 | * Omit dangling symrefs from output; this only has an effect with | |
272 | * INCLUDE_BROKEN, since they are otherwise not included at all. | |
273 | */ | |
274 | DO_FOR_EACH_OMIT_DANGLING_SYMREFS = (1 << 2), | |
d0f00c1a KN |
275 | |
276 | /* | |
277 | * Include root refs i.e. HEAD and pseudorefs along with the regular | |
278 | * refs. | |
279 | */ | |
280 | DO_FOR_EACH_INCLUDE_ROOT_REFS = (1 << 3), | |
9aab952e | 281 | }; |
93770590 | 282 | |
3bc581b9 MH |
283 | /* |
284 | * Reference iterators | |
285 | * | |
286 | * A reference iterator encapsulates the state of an in-progress | |
287 | * iteration over references. Create an instance of `struct | |
288 | * ref_iterator` via one of the functions in this module. | |
289 | * | |
290 | * A freshly-created ref_iterator doesn't yet point at a reference. To | |
291 | * advance the iterator, call ref_iterator_advance(). If successful, | |
292 | * this sets the iterator's refname, oid, and flags fields to describe | |
293 | * the next reference and returns ITER_OK. The data pointed at by | |
294 | * refname and oid belong to the iterator; if you want to retain them | |
295 | * after calling ref_iterator_advance() again or calling | |
cec2b6f5 | 296 | * ref_iterator_free(), you must make a copy. When the iteration has |
3bc581b9 | 297 | * been exhausted, ref_iterator_advance() releases any resources |
15beaaa3 | 298 | * associated with the iteration, frees the ref_iterator object, and |
3bc581b9 | 299 | * returns ITER_DONE. If you want to abort the iteration early, call |
cec2b6f5 | 300 | * ref_iterator_free(), which also frees the ref_iterator object and |
3bc581b9 MH |
301 | * any associated resources. If there was an internal error advancing |
302 | * to the next entry, ref_iterator_advance() aborts the iteration, | |
303 | * frees the ref_iterator, and returns ITER_ERROR. | |
304 | * | |
305 | * The reference currently being looked at can be peeled by calling | |
306 | * ref_iterator_peel(). This function is often faster than peel_ref(), | |
307 | * so it should be preferred when iterating over references. | |
308 | * | |
309 | * Putting it all together, a typical iteration looks like this: | |
310 | * | |
311 | * int ok; | |
312 | * struct ref_iterator *iter = ...; | |
313 | * | |
314 | * while ((ok = ref_iterator_advance(iter)) == ITER_OK) { | |
315 | * if (want_to_stop_iteration()) { | |
cec2b6f5 | 316 | * ok = ITER_DONE; |
3bc581b9 MH |
317 | * break; |
318 | * } | |
319 | * | |
320 | * // Access information about the current reference: | |
321 | * if (!(iter->flags & REF_ISSYMREF)) | |
7b6057c8 | 322 | * printf("%s is %s\n", iter->refname, oid_to_hex(iter->oid)); |
3bc581b9 MH |
323 | * |
324 | * // If you need to peel the reference: | |
325 | * ref_iterator_peel(iter, &oid); | |
326 | * } | |
327 | * | |
328 | * if (ok != ITER_DONE) | |
329 | * handle_error(); | |
cec2b6f5 | 330 | * ref_iterator_free(iter); |
3bc581b9 MH |
331 | */ |
332 | struct ref_iterator { | |
333 | struct ref_iterator_vtable *vtable; | |
334 | const char *refname; | |
cfd97152 | 335 | const char *referent; |
3bc581b9 MH |
336 | const struct object_id *oid; |
337 | unsigned int flags; | |
338 | }; | |
339 | ||
340 | /* | |
341 | * Advance the iterator to the first or next item and return ITER_OK. | |
342 | * If the iteration is exhausted, free the resources associated with | |
343 | * the ref_iterator and return ITER_DONE. On errors, free the iterator | |
344 | * resources and return ITER_ERROR. It is a bug to use ref_iterator or | |
345 | * call this function again after it has returned ITER_DONE or | |
346 | * ITER_ERROR. | |
347 | */ | |
348 | int ref_iterator_advance(struct ref_iterator *ref_iterator); | |
349 | ||
82c39c60 PS |
350 | /* |
351 | * Seek the iterator to the first reference with the given prefix. | |
352 | * The prefix is matched as a literal string, without regard for path | |
353 | * separators. If prefix is NULL or the empty string, seek the iterator to the | |
354 | * first reference again. | |
355 | * | |
356 | * This function is expected to behave as if a new ref iterator with the same | |
357 | * prefix had been created, but allows reuse of iterators and thus may allow | |
358 | * the backend to optimize. Parameters other than the prefix that have been | |
359 | * passed when creating the iterator will remain unchanged. | |
360 | * | |
361 | * Returns 0 on success, a negative error code otherwise. | |
362 | */ | |
363 | int ref_iterator_seek(struct ref_iterator *ref_iterator, | |
364 | const char *prefix); | |
365 | ||
3bc581b9 MH |
366 | /* |
367 | * If possible, peel the reference currently being viewed by the | |
368 | * iterator. Return 0 on success. | |
369 | */ | |
370 | int ref_iterator_peel(struct ref_iterator *ref_iterator, | |
371 | struct object_id *peeled); | |
372 | ||
cec2b6f5 PS |
373 | /* Free the reference iterator and any associated resources. */ |
374 | void ref_iterator_free(struct ref_iterator *ref_iterator); | |
3bc581b9 MH |
375 | |
376 | /* | |
377 | * An iterator over nothing (its first ref_iterator_advance() call | |
378 | * returns ITER_DONE). | |
379 | */ | |
380 | struct ref_iterator *empty_ref_iterator_begin(void); | |
381 | ||
382 | /* | |
383 | * Return true iff ref_iterator is an empty_ref_iterator. | |
384 | */ | |
385 | int is_empty_ref_iterator(struct ref_iterator *ref_iterator); | |
386 | ||
e121b9cb MH |
387 | /* |
388 | * Return an iterator that goes over each reference in `refs` for | |
389 | * which the refname begins with prefix. If trim is non-zero, then | |
84ee4ca1 | 390 | * trim that many characters off the beginning of each refname. |
9aab952e | 391 | * The output is ordered by refname. |
e121b9cb MH |
392 | */ |
393 | struct ref_iterator *refs_ref_iterator_begin( | |
394 | struct ref_store *refs, | |
b269ac53 TB |
395 | const char *prefix, const char **exclude_patterns, |
396 | int trim, enum do_for_each_ref_flags flags); | |
e121b9cb | 397 | |
3bc581b9 MH |
398 | /* |
399 | * A callback function used to instruct merge_ref_iterator how to | |
400 | * interleave the entries from iter0 and iter1. The function should | |
401 | * return one of the constants defined in enum iterator_selection. It | |
402 | * must not advance either of the iterators itself. | |
403 | * | |
404 | * The function must be prepared to handle the case that iter0 and/or | |
405 | * iter1 is NULL, which indicates that the corresponding sub-iterator | |
406 | * has been exhausted. Its return value must be consistent with the | |
407 | * current states of the iterators; e.g., it must not return | |
408 | * ITER_SKIP_1 if iter1 has already been exhausted. | |
409 | */ | |
410 | typedef enum iterator_selection ref_iterator_select_fn( | |
411 | struct ref_iterator *iter0, struct ref_iterator *iter1, | |
412 | void *cb_data); | |
413 | ||
6f227800 PS |
414 | /* |
415 | * An implementation of ref_iterator_select_fn that merges worktree and common | |
416 | * refs. Per-worktree refs from the common iterator are ignored, worktree refs | |
417 | * override common refs. Refs are selected lexicographically. | |
418 | */ | |
419 | enum iterator_selection ref_iterator_select(struct ref_iterator *iter_worktree, | |
420 | struct ref_iterator *iter_common, | |
421 | void *cb_data); | |
422 | ||
3bc581b9 MH |
423 | /* |
424 | * Iterate over the entries from iter0 and iter1, with the values | |
425 | * interleaved as directed by the select function. The iterator takes | |
426 | * ownership of iter0 and iter1 and frees them when the iteration is | |
5e01d838 | 427 | * over. |
3bc581b9 MH |
428 | */ |
429 | struct ref_iterator *merge_ref_iterator_begin( | |
430 | struct ref_iterator *iter0, struct ref_iterator *iter1, | |
431 | ref_iterator_select_fn *select, void *cb_data); | |
432 | ||
433 | /* | |
434 | * An iterator consisting of the union of the entries from front and | |
435 | * back. If there are entries common to the two sub-iterators, use the | |
436 | * one from front. Each iterator must iterate over its entries in | |
437 | * strcmp() order by refname for this to work. | |
438 | * | |
439 | * The new iterator takes ownership of its arguments and frees them | |
440 | * when the iteration is over. As a convenience to callers, if front | |
441 | * or back is an empty_ref_iterator, then abort that one immediately | |
442 | * and return the other iterator directly, without wrapping it. | |
443 | */ | |
444 | struct ref_iterator *overlay_ref_iterator_begin( | |
445 | struct ref_iterator *front, struct ref_iterator *back); | |
446 | ||
447 | /* | |
448 | * Wrap iter0, only letting through the references whose names start | |
449 | * with prefix. If trim is set, set iter->refname to the name of the | |
450 | * reference with that many characters trimmed off the front; | |
451 | * otherwise set it to the full refname. The new iterator takes over | |
452 | * ownership of iter0 and frees it when iteration is over. It makes | |
453 | * its own copy of prefix. | |
454 | * | |
455 | * As an convenience to callers, if prefix is the empty string and | |
456 | * trim is zero, this function returns iter0 directly, without | |
457 | * wrapping it. | |
458 | */ | |
459 | struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0, | |
460 | const char *prefix, | |
461 | int trim); | |
462 | ||
3bc581b9 MH |
463 | /* Internal implementation of reference iteration: */ |
464 | ||
465 | /* | |
466 | * Base class constructor for ref_iterators. Initialize the | |
467 | * ref_iterator part of iter, setting its vtable pointer as specified. | |
468 | * This is meant to be called only by the initializers of derived | |
469 | * classes. | |
470 | */ | |
471 | void base_ref_iterator_init(struct ref_iterator *iter, | |
5e01d838 | 472 | struct ref_iterator_vtable *vtable); |
3bc581b9 | 473 | |
3bc581b9 MH |
474 | /* Virtual function declarations for ref_iterators: */ |
475 | ||
84ee4ca1 HWN |
476 | /* |
477 | * backend-specific implementation of ref_iterator_advance. For symrefs, the | |
478 | * function should set REF_ISSYMREF, and it should also dereference the symref | |
9aab952e JK |
479 | * to provide the OID referent. It should respect do_for_each_ref_flags |
480 | * that were passed to refs_ref_iterator_begin(). | |
84ee4ca1 | 481 | */ |
3bc581b9 MH |
482 | typedef int ref_iterator_advance_fn(struct ref_iterator *ref_iterator); |
483 | ||
82c39c60 PS |
484 | /* |
485 | * Seek the iterator to the first reference matching the given prefix. Should | |
486 | * behave the same as if a new iterator was created with the same prefix. | |
487 | */ | |
488 | typedef int ref_iterator_seek_fn(struct ref_iterator *ref_iterator, | |
489 | const char *prefix); | |
490 | ||
617480d7 HWN |
491 | /* |
492 | * Peels the current ref, returning 0 for success or -1 for failure. | |
493 | */ | |
3bc581b9 MH |
494 | typedef int ref_iterator_peel_fn(struct ref_iterator *ref_iterator, |
495 | struct object_id *peeled); | |
496 | ||
497 | /* | |
498 | * Implementations of this function should free any resources specific | |
cec2b6f5 | 499 | * to the derived class. |
3bc581b9 | 500 | */ |
cec2b6f5 | 501 | typedef void ref_iterator_release_fn(struct ref_iterator *ref_iterator); |
3bc581b9 MH |
502 | |
503 | struct ref_iterator_vtable { | |
504 | ref_iterator_advance_fn *advance; | |
82c39c60 | 505 | ref_iterator_seek_fn *seek; |
3bc581b9 | 506 | ref_iterator_peel_fn *peel; |
cec2b6f5 | 507 | ref_iterator_release_fn *release; |
3bc581b9 MH |
508 | }; |
509 | ||
93770590 | 510 | /* |
4c4de895 MH |
511 | * current_ref_iter is a performance hack: when iterating over |
512 | * references using the for_each_ref*() functions, current_ref_iter is | |
513 | * set to the reference iterator before calling the callback function. | |
514 | * If the callback function calls peel_ref(), then peel_ref() first | |
515 | * checks whether the reference to be peeled is the one referred to by | |
516 | * the iterator (it usually is) and if so, asks the iterator for the | |
517 | * peeled version of the reference if it is available. This avoids a | |
518 | * refname lookup in a common case. current_ref_iter is set to NULL | |
519 | * when the iteration is over. | |
520 | */ | |
521 | extern struct ref_iterator *current_ref_iter; | |
522 | ||
523 | /* | |
524 | * The common backend for the for_each_*ref* functions. Call fn for | |
525 | * each reference in iter. If the iterator itself ever returns | |
526 | * ITER_ERROR, return -1. If fn ever returns a non-zero value, stop | |
527 | * the iteration and return that value. Otherwise, return 0. In any | |
528 | * case, free the iterator when done. This function is basically an | |
529 | * adapter between the callback style of reference iteration and the | |
530 | * iterator style. | |
531 | */ | |
8378c9d2 PS |
532 | int do_for_each_ref_iterator(struct ref_iterator *iter, |
533 | each_ref_fn fn, void *cb_data); | |
2d0663b2 | 534 | |
1a769003 MH |
535 | struct ref_store; |
536 | ||
0c09ec07 DT |
537 | /* refs backends */ |
538 | ||
9e7ec634 NTND |
539 | /* ref_store_init flags */ |
540 | #define REF_STORE_READ (1 << 0) | |
541 | #define REF_STORE_WRITE (1 << 1) /* can perform update operations */ | |
542 | #define REF_STORE_ODB (1 << 2) /* has access to object database */ | |
543 | #define REF_STORE_MAIN (1 << 3) | |
0d8a814d NTND |
544 | #define REF_STORE_ALL_CAPS (REF_STORE_READ | \ |
545 | REF_STORE_WRITE | \ | |
546 | REF_STORE_ODB | \ | |
547 | REF_STORE_MAIN) | |
9e7ec634 | 548 | |
e1e33b72 | 549 | /* |
5d0bc90e NTND |
550 | * Initialize the ref_store for the specified gitdir. These functions |
551 | * should call base_ref_store_init() to initialize the shared part of | |
552 | * the ref_store and to record the ref_store for later lookup. | |
e1e33b72 | 553 | */ |
34224e14 JT |
554 | typedef struct ref_store *ref_store_init_fn(struct repository *repo, |
555 | const char *gitdir, | |
9e7ec634 | 556 | unsigned int flags); |
71c871b4 PS |
557 | /* |
558 | * Release all memory and resources associated with the ref store. | |
559 | */ | |
560 | typedef void ref_store_release_fn(struct ref_store *refs); | |
e1e33b72 | 561 | |
ed93ea16 PS |
562 | typedef int ref_store_create_on_disk_fn(struct ref_store *refs, |
563 | int flags, | |
564 | struct strbuf *err); | |
6fb5acfd | 565 | |
64a6dd8f PS |
566 | /* |
567 | * Remove the reference store from disk. | |
568 | */ | |
569 | typedef int ref_store_remove_on_disk_fn(struct ref_store *refs, | |
570 | struct strbuf *err); | |
571 | ||
30173b88 MH |
572 | typedef int ref_transaction_prepare_fn(struct ref_store *refs, |
573 | struct ref_transaction *transaction, | |
574 | struct strbuf *err); | |
575 | ||
576 | typedef int ref_transaction_finish_fn(struct ref_store *refs, | |
577 | struct ref_transaction *transaction, | |
578 | struct strbuf *err); | |
579 | ||
580 | typedef int ref_transaction_abort_fn(struct ref_store *refs, | |
581 | struct ref_transaction *transaction, | |
582 | struct strbuf *err); | |
583 | ||
e1e33b72 MH |
584 | typedef int ref_transaction_commit_fn(struct ref_store *refs, |
585 | struct ref_transaction *transaction, | |
586 | struct strbuf *err); | |
587 | ||
826ae79f JC |
588 | typedef int pack_refs_fn(struct ref_store *ref_store, |
589 | struct pack_refs_opts *opts); | |
9b6b40d9 DT |
590 | typedef int rename_ref_fn(struct ref_store *ref_store, |
591 | const char *oldref, const char *newref, | |
592 | const char *logmsg); | |
52d59cc6 SD |
593 | typedef int copy_ref_fn(struct ref_store *ref_store, |
594 | const char *oldref, const char *newref, | |
595 | const char *logmsg); | |
8231527e | 596 | |
1a769003 | 597 | /* |
e1860571 MH |
598 | * Iterate over the references in `ref_store` whose names start with |
599 | * `prefix`. `prefix` is matched as a literal string, without regard | |
600 | * for path separators. If prefix is NULL or the empty string, iterate | |
8738a8a4 MH |
601 | * over all references in `ref_store`. The output is ordered by |
602 | * refname. | |
1a769003 MH |
603 | */ |
604 | typedef struct ref_iterator *ref_iterator_begin_fn( | |
605 | struct ref_store *ref_store, | |
b269ac53 TB |
606 | const char *prefix, const char **exclude_patterns, |
607 | unsigned int flags); | |
1a769003 | 608 | |
e3688bd6 DT |
609 | /* reflog functions */ |
610 | ||
611 | /* | |
612 | * Iterate over the references in the specified ref_store that have a | |
613 | * reflog. The refs are iterated over in arbitrary order. | |
614 | */ | |
615 | typedef struct ref_iterator *reflog_iterator_begin_fn( | |
616 | struct ref_store *ref_store); | |
617 | ||
618 | typedef int for_each_reflog_ent_fn(struct ref_store *ref_store, | |
619 | const char *refname, | |
620 | each_reflog_ent_fn fn, | |
621 | void *cb_data); | |
622 | typedef int for_each_reflog_ent_reverse_fn(struct ref_store *ref_store, | |
623 | const char *refname, | |
624 | each_reflog_ent_fn fn, | |
625 | void *cb_data); | |
626 | typedef int reflog_exists_fn(struct ref_store *ref_store, const char *refname); | |
627 | typedef int create_reflog_fn(struct ref_store *ref_store, const char *refname, | |
7b089120 | 628 | struct strbuf *err); |
e3688bd6 DT |
629 | typedef int delete_reflog_fn(struct ref_store *ref_store, const char *refname); |
630 | typedef int reflog_expire_fn(struct ref_store *ref_store, | |
cc40b5ce | 631 | const char *refname, |
e3688bd6 DT |
632 | unsigned int flags, |
633 | reflog_expiry_prepare_fn prepare_fn, | |
634 | reflog_expiry_should_prune_fn should_prune_fn, | |
635 | reflog_expiry_cleanup_fn cleanup_fn, | |
636 | void *policy_cb_data); | |
637 | ||
cf596442 | 638 | /* |
34c7ad8f MH |
639 | * Read a reference from the specified reference store, non-recursively. |
640 | * Set type to describe the reference, and: | |
cf596442 | 641 | * |
99afe91a | 642 | * - If refname is the name of a normal reference, fill in oid |
cf596442 MH |
643 | * (leaving referent unchanged). |
644 | * | |
645 | * - If refname is the name of a symbolic reference, write the full | |
646 | * name of the reference to which it refers (e.g. | |
647 | * "refs/heads/master") to referent and set the REF_ISSYMREF bit in | |
99afe91a | 648 | * type (leaving oid unchanged). The caller is responsible for |
cf596442 MH |
649 | * validating that referent is a valid reference name. |
650 | * | |
651 | * WARNING: refname might be used as part of a filename, so it is | |
652 | * important from a security standpoint that it be safe in the sense | |
653 | * of refname_is_safe(). Moreover, for symrefs this function sets | |
654 | * referent to whatever the repository says, which might not be a | |
655 | * properly-formatted or even safe reference name. NEITHER INPUT NOR | |
656 | * OUTPUT REFERENCE NAMES ARE VALIDATED WITHIN THIS FUNCTION. | |
657 | * | |
5b12e16b HWN |
658 | * Return 0 on success, or -1 on failure. If the ref exists but is neither a |
659 | * symbolic ref nor an object ID, it is broken. In this case set REF_ISBROKEN in | |
660 | * type, and return -1 (failure_errno should not be ENOENT) | |
661 | * | |
662 | * failure_errno provides errno codes that are interpreted beyond error | |
663 | * reporting. The following error codes have special meaning: | |
664 | * * ENOENT: the ref doesn't exist | |
665 | * * EISDIR: ref name is a directory | |
666 | * * ENOTDIR: ref prefix is not a directory | |
cf596442 MH |
667 | * |
668 | * Backend-specific flags might be set in type as well, regardless of | |
669 | * outcome. | |
670 | * | |
671 | * It is OK for refname to point into referent. If so: | |
672 | * | |
673 | * - if the function succeeds with REF_ISSYMREF, referent will be | |
674 | * overwritten and the memory formerly pointed to by it might be | |
675 | * changed or even freed. | |
676 | * | |
677 | * - in all other cases, referent will be untouched, and therefore | |
678 | * refname will still be valid and unchanged. | |
679 | */ | |
5b12e16b HWN |
680 | typedef int read_raw_ref_fn(struct ref_store *ref_store, const char *refname, |
681 | struct object_id *oid, struct strbuf *referent, | |
682 | unsigned int *type, int *failure_errno); | |
127b42a1 | 683 | |
cd475b3b PS |
684 | /* |
685 | * Read a symbolic reference from the specified reference store. This function | |
686 | * is optional: if not implemented by a backend, then `read_raw_ref_fn` is used | |
687 | * to read the symbolcic reference instead. It is intended to be implemented | |
688 | * only in case the backend can optimize the reading of symbolic references. | |
689 | * | |
690 | * Return 0 on success, or -1 on failure. `referent` will be set to the target | |
691 | * of the symbolic reference on success. This function explicitly does not | |
692 | * distinguish between error cases and the reference not being a symbolic | |
693 | * reference to allow backends to optimize this operation in case symbolic and | |
694 | * non-symbolic references are treated differently. | |
695 | */ | |
696 | typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refname, | |
697 | struct strbuf *referent); | |
698 | ||
ab6f79d8 | 699 | typedef int fsck_fn(struct ref_store *ref_store, |
7c78d819 | 700 | struct fsck_options *o, |
701 | struct worktree *wt); | |
ab6f79d8 | 702 | |
3dce444f | 703 | struct ref_storage_be { |
3dce444f | 704 | const char *name; |
00eebe35 | 705 | ref_store_init_fn *init; |
71c871b4 | 706 | ref_store_release_fn *release; |
ed93ea16 | 707 | ref_store_create_on_disk_fn *create_on_disk; |
64a6dd8f | 708 | ref_store_remove_on_disk_fn *remove_on_disk; |
30173b88 MH |
709 | |
710 | ref_transaction_prepare_fn *transaction_prepare; | |
711 | ref_transaction_finish_fn *transaction_finish; | |
712 | ref_transaction_abort_fn *transaction_abort; | |
e1e33b72 | 713 | |
8231527e | 714 | pack_refs_fn *pack_refs; |
9b6b40d9 | 715 | rename_ref_fn *rename_ref; |
52d59cc6 | 716 | copy_ref_fn *copy_ref; |
8231527e | 717 | |
1a769003 | 718 | ref_iterator_begin_fn *iterator_begin; |
e1e33b72 | 719 | read_raw_ref_fn *read_raw_ref; |
8102d10f BF |
720 | |
721 | /* | |
722 | * Please refer to `refs_read_symbolic_ref()` for the expected | |
723 | * behaviour. | |
724 | */ | |
cd475b3b | 725 | read_symbolic_ref_fn *read_symbolic_ref; |
e3688bd6 DT |
726 | |
727 | reflog_iterator_begin_fn *reflog_iterator_begin; | |
728 | for_each_reflog_ent_fn *for_each_reflog_ent; | |
729 | for_each_reflog_ent_reverse_fn *for_each_reflog_ent_reverse; | |
730 | reflog_exists_fn *reflog_exists; | |
731 | create_reflog_fn *create_reflog; | |
732 | delete_reflog_fn *delete_reflog; | |
733 | reflog_expire_fn *reflog_expire; | |
ab6f79d8 | 734 | |
735 | fsck_fn *fsck; | |
3dce444f RS |
736 | }; |
737 | ||
738 | extern struct ref_storage_be refs_be_files; | |
57db2a09 | 739 | extern struct ref_storage_be refs_be_reftable; |
e0cc8ac8 | 740 | extern struct ref_storage_be refs_be_packed; |
3dce444f | 741 | |
00eebe35 MH |
742 | /* |
743 | * A representation of the reference store for the main repository or | |
744 | * a submodule. The ref_store instances for submodules are kept in a | |
965f8991 | 745 | * hash map; see repo_get_submodule_ref_store() for more info. |
00eebe35 MH |
746 | */ |
747 | struct ref_store { | |
748 | /* The backend describing this ref_store's storage scheme: */ | |
749 | const struct ref_storage_be *be; | |
5085aef4 | 750 | |
34224e14 JT |
751 | struct repository *repo; |
752 | ||
753 | /* | |
754 | * The gitdir that this ref_store applies to. Note that this is not | |
755 | * necessarily repo->gitdir if the repo has multiple worktrees. | |
756 | */ | |
5085aef4 | 757 | char *gitdir; |
00eebe35 MH |
758 | }; |
759 | ||
e39620f0 | 760 | /* |
df3458e9 HWN |
761 | * Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for |
762 | * invalid contents. | |
e39620f0 | 763 | */ |
080b068f PS |
764 | int parse_loose_ref_contents(const struct git_hash_algo *algop, |
765 | const char *buf, struct object_id *oid, | |
df3458e9 | 766 | struct strbuf *referent, unsigned int *type, |
1c0e2a00 | 767 | const char **trailing, int *failure_errno); |
e39620f0 | 768 | |
00eebe35 | 769 | /* |
fbfd0a29 MH |
770 | * Fill in the generic part of refs and add it to our collection of |
771 | * reference stores. | |
00eebe35 | 772 | */ |
f9f7fd3b HWN |
773 | void base_ref_store_init(struct ref_store *refs, struct repository *repo, |
774 | const char *path, const struct ref_storage_be *be); | |
00eebe35 | 775 | |
4441f427 HWN |
776 | /* |
777 | * Support GIT_TRACE_REFS by optionally wrapping the given ref_store instance. | |
778 | */ | |
779 | struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store); | |
780 | ||
e9965ba4 KN |
781 | /* |
782 | * Return the refname under which update was originally requested. | |
783 | */ | |
784 | const char *ref_update_original_update_refname(struct ref_update *update); | |
785 | ||
644daf77 KN |
786 | /* |
787 | * Helper function to check if the new value is null, this | |
788 | * takes into consideration that the update could be a regular | |
789 | * ref or a symbolic ref. | |
790 | */ | |
791 | int ref_update_has_null_new_value(struct ref_update *update); | |
792 | ||
793 | /* | |
794 | * Check whether the old_target values stored in update are consistent | |
795 | * with the referent, which is the symbolic reference's current value. | |
796 | * If everything is OK, return 0; otherwise, write an error message to | |
797 | * err and return -1. | |
798 | */ | |
76e760b9 KN |
799 | enum ref_transaction_error ref_update_check_old_target(const char *referent, |
800 | struct ref_update *update, | |
801 | struct strbuf *err); | |
644daf77 | 802 | |
aba381c0 KN |
803 | /* |
804 | * Check if the ref must exist, this means that the old_oid or | |
805 | * old_target is non NULL. | |
806 | */ | |
807 | int ref_update_expects_existing_old_ref(struct ref_update *update); | |
808 | ||
31726bb9 KN |
809 | /* |
810 | * Same as `refs_verify_refname_available()`, but checking for a list of | |
811 | * refnames instead of only a single item. This is more efficient in the case | |
812 | * where one needs to check multiple refnames. | |
813 | * | |
814 | * If using batched updates, then individual updates are marked rejected, | |
815 | * reference backends are then in charge of not committing those updates. | |
816 | */ | |
817 | enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs, | |
818 | const struct string_list *refnames, | |
819 | const struct string_list *extras, | |
820 | const struct string_list *skip, | |
821 | struct ref_transaction *transaction, | |
822 | unsigned int initial_transaction, | |
823 | struct strbuf *err); | |
824 | ||
4cb77009 | 825 | #endif /* REFS_REFS_INTERNAL_H */ |