refs: introduce an iterator interface
[git/git.git] / refs / refs-internal.h
1 #ifndef REFS_REFS_INTERNAL_H
2 #define REFS_REFS_INTERNAL_H
3
4 /*
5 * Data structures and functions for the internal use of the refs
6 * module. Code outside of the refs module should use only the public
7 * functions defined in "refs.h", and should *not* include this file.
8 */
9
10 /*
11 * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken
12 * refs (i.e., because the reference is about to be deleted anyway).
13 */
14 #define REF_DELETING 0x02
15
16 /*
17 * Used as a flag in ref_update::flags when a loose ref is being
18 * pruned. This flag must only be used when REF_NODEREF is set.
19 */
20 #define REF_ISPRUNING 0x04
21
22 /*
23 * Used as a flag in ref_update::flags when the reference should be
24 * updated to new_sha1.
25 */
26 #define REF_HAVE_NEW 0x08
27
28 /*
29 * Used as a flag in ref_update::flags when old_sha1 should be
30 * checked.
31 */
32 #define REF_HAVE_OLD 0x10
33
34 /*
35 * Used as a flag in ref_update::flags when the lockfile needs to be
36 * committed.
37 */
38 #define REF_NEEDS_COMMIT 0x20
39
40 /*
41 * 0x40 is REF_FORCE_CREATE_REFLOG, so skip it if you're adding a
42 * value to ref_update::flags
43 */
44
45 /*
46 * Used as a flag in ref_update::flags when we want to log a ref
47 * update but not actually perform it. This is used when a symbolic
48 * ref update is split up.
49 */
50 #define REF_LOG_ONLY 0x80
51
52 /*
53 * Internal flag, meaning that the containing ref_update was via an
54 * update to HEAD.
55 */
56 #define REF_UPDATE_VIA_HEAD 0x100
57
58 /*
59 * Return true iff refname is minimally safe. "Safe" here means that
60 * deleting a loose reference by this name will not do any damage, for
61 * example by causing a file that is not a reference to be deleted.
62 * This function does not check that the reference name is legal; for
63 * that, use check_refname_format().
64 *
65 * We consider a refname that starts with "refs/" to be safe as long
66 * as any ".." components that it might contain do not escape "refs/".
67 * Names that do not start with "refs/" are considered safe iff they
68 * consist entirely of upper case characters and '_' (like "HEAD" and
69 * "MERGE_HEAD" but not "config" or "FOO/BAR").
70 */
71 int refname_is_safe(const char *refname);
72
73 enum peel_status {
74 /* object was peeled successfully: */
75 PEEL_PEELED = 0,
76
77 /*
78 * object cannot be peeled because the named object (or an
79 * object referred to by a tag in the peel chain), does not
80 * exist.
81 */
82 PEEL_INVALID = -1,
83
84 /* object cannot be peeled because it is not a tag: */
85 PEEL_NON_TAG = -2,
86
87 /* ref_entry contains no peeled value because it is a symref: */
88 PEEL_IS_SYMREF = -3,
89
90 /*
91 * ref_entry cannot be peeled because it is broken (i.e., the
92 * symbolic reference cannot even be resolved to an object
93 * name):
94 */
95 PEEL_BROKEN = -4
96 };
97
98 /*
99 * Peel the named object; i.e., if the object is a tag, resolve the
100 * tag recursively until a non-tag is found. If successful, store the
101 * result to sha1 and return PEEL_PEELED. If the object is not a tag
102 * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively,
103 * and leave sha1 unchanged.
104 */
105 enum peel_status peel_object(const unsigned char *name, unsigned char *sha1);
106
107 /*
108 * Return 0 if a reference named refname could be created without
109 * conflicting with the name of an existing reference. Otherwise,
110 * return a negative value and write an explanation to err. If extras
111 * is non-NULL, it is a list of additional refnames with which refname
112 * is not allowed to conflict. If skip is non-NULL, ignore potential
113 * conflicts with refs in skip (e.g., because they are scheduled for
114 * deletion in the same operation). Behavior is undefined if the same
115 * name is listed in both extras and skip.
116 *
117 * Two reference names conflict if one of them exactly matches the
118 * leading components of the other; e.g., "foo/bar" conflicts with
119 * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
120 * "foo/barbados".
121 *
122 * extras and skip must be sorted.
123 */
124 int verify_refname_available(const char *newname,
125 const struct string_list *extras,
126 const struct string_list *skip,
127 struct strbuf *err);
128
129 /*
130 * Copy the reflog message msg to buf, which has been allocated sufficiently
131 * large, while cleaning up the whitespaces. Especially, convert LF to space,
132 * because reflog file is one line per entry.
133 */
134 int copy_reflog_msg(char *buf, const char *msg);
135
136 int should_autocreate_reflog(const char *refname);
137
138 /**
139 * Information needed for a single ref update. Set new_sha1 to the new
140 * value or to null_sha1 to delete the ref. To check the old value
141 * while the ref is locked, set (flags & REF_HAVE_OLD) and set
142 * old_sha1 to the old value, or to null_sha1 to ensure the ref does
143 * not exist before update.
144 */
145 struct ref_update {
146
147 /*
148 * If (flags & REF_HAVE_NEW), set the reference to this value:
149 */
150 unsigned char new_sha1[20];
151
152 /*
153 * If (flags & REF_HAVE_OLD), check that the reference
154 * previously had this value:
155 */
156 unsigned char old_sha1[20];
157
158 /*
159 * One or more of REF_HAVE_NEW, REF_HAVE_OLD, REF_NODEREF,
160 * REF_DELETING, REF_ISPRUNING, REF_LOG_ONLY, and
161 * REF_UPDATE_VIA_HEAD:
162 */
163 unsigned int flags;
164
165 struct ref_lock *lock;
166 unsigned int type;
167 char *msg;
168
169 /*
170 * If this ref_update was split off of a symref update via
171 * split_symref_update(), then this member points at that
172 * update. This is used for two purposes:
173 * 1. When reporting errors, we report the refname under which
174 * the update was originally requested.
175 * 2. When we read the old value of this reference, we
176 * propagate it back to its parent update for recording in
177 * the latter's reflog.
178 */
179 struct ref_update *parent_update;
180
181 const char refname[FLEX_ARRAY];
182 };
183
184 /*
185 * Add a ref_update with the specified properties to transaction, and
186 * return a pointer to the new object. This function does not verify
187 * that refname is well-formed. new_sha1 and old_sha1 are only
188 * dereferenced if the REF_HAVE_NEW and REF_HAVE_OLD bits,
189 * respectively, are set in flags.
190 */
191 struct ref_update *ref_transaction_add_update(
192 struct ref_transaction *transaction,
193 const char *refname, unsigned int flags,
194 const unsigned char *new_sha1,
195 const unsigned char *old_sha1,
196 const char *msg);
197
198 /*
199 * Transaction states.
200 * OPEN: The transaction is in a valid state and can accept new updates.
201 * An OPEN transaction can be committed.
202 * CLOSED: A closed transaction is no longer active and no other operations
203 * than free can be used on it in this state.
204 * A transaction can either become closed by successfully committing
205 * an active transaction or if there is a failure while building
206 * the transaction thus rendering it failed/inactive.
207 */
208 enum ref_transaction_state {
209 REF_TRANSACTION_OPEN = 0,
210 REF_TRANSACTION_CLOSED = 1
211 };
212
213 /*
214 * Data structure for holding a reference transaction, which can
215 * consist of checks and updates to multiple references, carried out
216 * as atomically as possible. This structure is opaque to callers.
217 */
218 struct ref_transaction {
219 struct ref_update **updates;
220 size_t alloc;
221 size_t nr;
222 enum ref_transaction_state state;
223 };
224
225 int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
226 const unsigned char *new_sha1, const char *msg,
227 int flags, struct strbuf *err);
228
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
243 int rename_ref_available(const char *oldname, const char *newname);
244
245 /* We allow "recursive" symbolic refs. Only within reason, though */
246 #define SYMREF_MAXDEPTH 5
247
248 /* Include broken references in a do_for_each_ref*() iteration: */
249 #define DO_FOR_EACH_INCLUDE_BROKEN 0x01
250
251 /*
252 * Reference iterators
253 *
254 * A reference iterator encapsulates the state of an in-progress
255 * iteration over references. Create an instance of `struct
256 * ref_iterator` via one of the functions in this module.
257 *
258 * A freshly-created ref_iterator doesn't yet point at a reference. To
259 * advance the iterator, call ref_iterator_advance(). If successful,
260 * this sets the iterator's refname, oid, and flags fields to describe
261 * the next reference and returns ITER_OK. The data pointed at by
262 * refname and oid belong to the iterator; if you want to retain them
263 * after calling ref_iterator_advance() again or calling
264 * ref_iterator_abort(), you must make a copy. When the iteration has
265 * been exhausted, ref_iterator_advance() releases any resources
266 * assocated with the iteration, frees the ref_iterator object, and
267 * returns ITER_DONE. If you want to abort the iteration early, call
268 * ref_iterator_abort(), which also frees the ref_iterator object and
269 * any associated resources. If there was an internal error advancing
270 * to the next entry, ref_iterator_advance() aborts the iteration,
271 * frees the ref_iterator, and returns ITER_ERROR.
272 *
273 * The reference currently being looked at can be peeled by calling
274 * ref_iterator_peel(). This function is often faster than peel_ref(),
275 * so it should be preferred when iterating over references.
276 *
277 * Putting it all together, a typical iteration looks like this:
278 *
279 * int ok;
280 * struct ref_iterator *iter = ...;
281 *
282 * while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
283 * if (want_to_stop_iteration()) {
284 * ok = ref_iterator_abort(iter);
285 * break;
286 * }
287 *
288 * // Access information about the current reference:
289 * if (!(iter->flags & REF_ISSYMREF))
290 * printf("%s is %s\n", iter->refname, oid_to_hex(&iter->oid));
291 *
292 * // If you need to peel the reference:
293 * ref_iterator_peel(iter, &oid);
294 * }
295 *
296 * if (ok != ITER_DONE)
297 * handle_error();
298 */
299 struct ref_iterator {
300 struct ref_iterator_vtable *vtable;
301 const char *refname;
302 const struct object_id *oid;
303 unsigned int flags;
304 };
305
306 /*
307 * Advance the iterator to the first or next item and return ITER_OK.
308 * If the iteration is exhausted, free the resources associated with
309 * the ref_iterator and return ITER_DONE. On errors, free the iterator
310 * resources and return ITER_ERROR. It is a bug to use ref_iterator or
311 * call this function again after it has returned ITER_DONE or
312 * ITER_ERROR.
313 */
314 int ref_iterator_advance(struct ref_iterator *ref_iterator);
315
316 /*
317 * If possible, peel the reference currently being viewed by the
318 * iterator. Return 0 on success.
319 */
320 int ref_iterator_peel(struct ref_iterator *ref_iterator,
321 struct object_id *peeled);
322
323 /*
324 * End the iteration before it has been exhausted, freeing the
325 * reference iterator and any associated resources and returning
326 * ITER_DONE. If the abort itself failed, return ITER_ERROR.
327 */
328 int ref_iterator_abort(struct ref_iterator *ref_iterator);
329
330 /*
331 * An iterator over nothing (its first ref_iterator_advance() call
332 * returns ITER_DONE).
333 */
334 struct ref_iterator *empty_ref_iterator_begin(void);
335
336 /*
337 * Return true iff ref_iterator is an empty_ref_iterator.
338 */
339 int is_empty_ref_iterator(struct ref_iterator *ref_iterator);
340
341 /*
342 * A callback function used to instruct merge_ref_iterator how to
343 * interleave the entries from iter0 and iter1. The function should
344 * return one of the constants defined in enum iterator_selection. It
345 * must not advance either of the iterators itself.
346 *
347 * The function must be prepared to handle the case that iter0 and/or
348 * iter1 is NULL, which indicates that the corresponding sub-iterator
349 * has been exhausted. Its return value must be consistent with the
350 * current states of the iterators; e.g., it must not return
351 * ITER_SKIP_1 if iter1 has already been exhausted.
352 */
353 typedef enum iterator_selection ref_iterator_select_fn(
354 struct ref_iterator *iter0, struct ref_iterator *iter1,
355 void *cb_data);
356
357 /*
358 * Iterate over the entries from iter0 and iter1, with the values
359 * interleaved as directed by the select function. The iterator takes
360 * ownership of iter0 and iter1 and frees them when the iteration is
361 * over.
362 */
363 struct ref_iterator *merge_ref_iterator_begin(
364 struct ref_iterator *iter0, struct ref_iterator *iter1,
365 ref_iterator_select_fn *select, void *cb_data);
366
367 /*
368 * An iterator consisting of the union of the entries from front and
369 * back. If there are entries common to the two sub-iterators, use the
370 * one from front. Each iterator must iterate over its entries in
371 * strcmp() order by refname for this to work.
372 *
373 * The new iterator takes ownership of its arguments and frees them
374 * when the iteration is over. As a convenience to callers, if front
375 * or back is an empty_ref_iterator, then abort that one immediately
376 * and return the other iterator directly, without wrapping it.
377 */
378 struct ref_iterator *overlay_ref_iterator_begin(
379 struct ref_iterator *front, struct ref_iterator *back);
380
381 /*
382 * Wrap iter0, only letting through the references whose names start
383 * with prefix. If trim is set, set iter->refname to the name of the
384 * reference with that many characters trimmed off the front;
385 * otherwise set it to the full refname. The new iterator takes over
386 * ownership of iter0 and frees it when iteration is over. It makes
387 * its own copy of prefix.
388 *
389 * As an convenience to callers, if prefix is the empty string and
390 * trim is zero, this function returns iter0 directly, without
391 * wrapping it.
392 */
393 struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
394 const char *prefix,
395 int trim);
396
397 /*
398 * Iterate over the packed and loose references in the specified
399 * submodule that are within find_containing_dir(prefix). If prefix is
400 * NULL or the empty string, iterate over all references in the
401 * submodule.
402 */
403 struct ref_iterator *files_ref_iterator_begin(const char *submodule,
404 const char *prefix,
405 unsigned int flags);
406
407 /* Internal implementation of reference iteration: */
408
409 /*
410 * Base class constructor for ref_iterators. Initialize the
411 * ref_iterator part of iter, setting its vtable pointer as specified.
412 * This is meant to be called only by the initializers of derived
413 * classes.
414 */
415 void base_ref_iterator_init(struct ref_iterator *iter,
416 struct ref_iterator_vtable *vtable);
417
418 /*
419 * Base class destructor for ref_iterators. Destroy the ref_iterator
420 * part of iter and shallow-free the object. This is meant to be
421 * called only by the destructors of derived classes.
422 */
423 void base_ref_iterator_free(struct ref_iterator *iter);
424
425 /* Virtual function declarations for ref_iterators: */
426
427 typedef int ref_iterator_advance_fn(struct ref_iterator *ref_iterator);
428
429 typedef int ref_iterator_peel_fn(struct ref_iterator *ref_iterator,
430 struct object_id *peeled);
431
432 /*
433 * Implementations of this function should free any resources specific
434 * to the derived class, then call base_ref_iterator_free() to clean
435 * up and free the ref_iterator object.
436 */
437 typedef int ref_iterator_abort_fn(struct ref_iterator *ref_iterator);
438
439 struct ref_iterator_vtable {
440 ref_iterator_advance_fn *advance;
441 ref_iterator_peel_fn *peel;
442 ref_iterator_abort_fn *abort;
443 };
444
445 /*
446 * Call fn for each reference in the specified submodule for which the
447 * refname begins with prefix. If trim is non-zero, then trim that
448 * many characters off the beginning of each refname before passing
449 * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
450 * include broken references in the iteration. If fn ever returns a
451 * non-zero value, stop the iteration and return that value;
452 * otherwise, return 0.
453 *
454 * This is the common backend for the for_each_*ref* functions.
455 */
456 int do_for_each_ref(const char *submodule, const char *prefix,
457 each_ref_fn fn, int trim, int flags, void *cb_data);
458
459 /*
460 * Read the specified reference from the filesystem or packed refs
461 * file, non-recursively. Set type to describe the reference, and:
462 *
463 * - If refname is the name of a normal reference, fill in sha1
464 * (leaving referent unchanged).
465 *
466 * - If refname is the name of a symbolic reference, write the full
467 * name of the reference to which it refers (e.g.
468 * "refs/heads/master") to referent and set the REF_ISSYMREF bit in
469 * type (leaving sha1 unchanged). The caller is responsible for
470 * validating that referent is a valid reference name.
471 *
472 * WARNING: refname might be used as part of a filename, so it is
473 * important from a security standpoint that it be safe in the sense
474 * of refname_is_safe(). Moreover, for symrefs this function sets
475 * referent to whatever the repository says, which might not be a
476 * properly-formatted or even safe reference name. NEITHER INPUT NOR
477 * OUTPUT REFERENCE NAMES ARE VALIDATED WITHIN THIS FUNCTION.
478 *
479 * Return 0 on success. If the ref doesn't exist, set errno to ENOENT
480 * and return -1. If the ref exists but is neither a symbolic ref nor
481 * a sha1, it is broken; set REF_ISBROKEN in type, set errno to
482 * EINVAL, and return -1. If there is another error reading the ref,
483 * set errno appropriately and return -1.
484 *
485 * Backend-specific flags might be set in type as well, regardless of
486 * outcome.
487 *
488 * It is OK for refname to point into referent. If so:
489 *
490 * - if the function succeeds with REF_ISSYMREF, referent will be
491 * overwritten and the memory formerly pointed to by it might be
492 * changed or even freed.
493 *
494 * - in all other cases, referent will be untouched, and therefore
495 * refname will still be valid and unchanged.
496 */
497 int read_raw_ref(const char *refname, unsigned char *sha1,
498 struct strbuf *referent, unsigned int *type);
499
500 #endif /* REFS_REFS_INTERNAL_H */