Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/refs.h
Line
Count
Source
1
#ifndef REFS_H
2
#define REFS_H
3
4
#include "object-name.h"
5
#include "commit.h"
6
#include "repository.h"
7
#include "repo-settings.h"
8
9
struct fsck_options;
10
struct object_id;
11
struct ref_store;
12
struct strbuf;
13
struct string_list;
14
struct string_list_item;
15
struct worktree;
16
17
enum ref_storage_format ref_storage_format_by_name(const char *name);
18
const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);
19
20
/* Parse a reference storage URI in the format "<format>[://<payload>]". */
21
enum ref_storage_format ref_storage_format_by_uri(const char *uri,
22
              char **payload);
23
24
enum ref_transaction_error {
25
  /* Default error code */
26
  REF_TRANSACTION_ERROR_GENERIC = -1,
27
  /* Ref name conflict like A vs A/B */
28
  REF_TRANSACTION_ERROR_NAME_CONFLICT = -2,
29
  /* Ref to be created already exists */
30
  REF_TRANSACTION_ERROR_CREATE_EXISTS = -3,
31
  /* ref expected but doesn't exist */
32
  REF_TRANSACTION_ERROR_NONEXISTENT_REF = -4,
33
  /* Provided old_oid or old_target of reference doesn't match actual */
34
  REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE = -5,
35
  /* Provided new_oid or new_target is invalid */
36
  REF_TRANSACTION_ERROR_INVALID_NEW_VALUE = -6,
37
  /* Expected ref to be symref, but is a regular ref */
38
  REF_TRANSACTION_ERROR_EXPECTED_SYMREF = -7,
39
  /* Cannot create ref due to case-insensitive filesystem */
40
  REF_TRANSACTION_ERROR_CASE_CONFLICT = -8,
41
};
42
43
/*
44
 * Resolve a reference, recursively following symbolic references.
45
 *
46
 * Return the name of the non-symbolic reference that ultimately pointed
47
 * at the resolved object name.  The return value, if not NULL, is a
48
 * pointer into either a static buffer or the input ref.
49
 *
50
 * If oid is non-NULL, store the referred-to object's name in it.
51
 *
52
 * If the reference cannot be resolved to an object, the behavior
53
 * depends on the RESOLVE_REF_READING flag:
54
 *
55
 * - If RESOLVE_REF_READING is set, return NULL.
56
 *
57
 * - If RESOLVE_REF_READING is not set, clear oid and return the name of
58
 *   the last reference name in the chain, which will either be a non-symbolic
59
 *   reference or an undefined reference.  If this is a prelude to
60
 *   "writing" to the ref, the return value is the name of the ref
61
 *   that will actually be created or changed.
62
 *
63
 * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one
64
 * level of symbolic reference.  The value stored in oid for a symbolic
65
 * reference will always be null_oid in this case, and the return
66
 * value is the reference that the symref refers to directly.
67
 *
68
 * If flags is non-NULL, set the value that it points to the
69
 * combination of REF_ISPACKED (if the reference was found among the
70
 * packed references), REF_ISSYMREF (if the initial reference was a
71
 * symbolic reference), REF_BAD_NAME (if the reference name is ill
72
 * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN
73
 * (if the ref is malformed or has a bad name). See refs.h for more detail
74
 * on each flag.
75
 *
76
 * If ref is not a properly-formatted, normalized reference, return
77
 * NULL.  If more than MAXDEPTH recursive symbolic lookups are needed,
78
 * give up and return NULL.
79
 *
80
 * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their
81
 * name is invalid according to git-check-ref-format(1).  If the name
82
 * is bad then the value stored in oid will be null_oid and the two
83
 * flags REF_ISBROKEN and REF_BAD_NAME will be set.
84
 *
85
 * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/
86
 * directory and do not consist of all caps and underscores cannot be
87
 * resolved. The function returns NULL for such ref names.
88
 * Caps and underscores refers to root refs and pseudorefs, such as HEAD,
89
 * FETCH_HEAD and friends, that all live outside of the refs/ directory.
90
 */
91
0
#define RESOLVE_REF_READING 0x01
92
0
#define RESOLVE_REF_NO_RECURSE 0x02
93
0
#define RESOLVE_REF_ALLOW_BAD_NAME 0x04
94
95
const char *refs_resolve_ref_unsafe(struct ref_store *refs,
96
            const char *refname,
97
            int resolve_flags,
98
            struct object_id *oid,
99
            int *flags);
100
101
char *refs_resolve_refdup(struct ref_store *refs,
102
        const char *refname, int resolve_flags,
103
        struct object_id *oid, int *flags);
104
105
int refs_read_ref_full(struct ref_store *refs, const char *refname,
106
           int resolve_flags, struct object_id *oid, int *flags);
107
108
int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid);
109
110
0
#define NOT_A_SYMREF -2
111
112
/*
113
 * Read the symbolic ref named "refname" and write its immediate referent into
114
 * the provided buffer. Referent is left empty if "refname" is not a symbolic
115
 * ref. It does not resolve the symbolic reference recursively in case the
116
 * target is also a symbolic ref.
117
 *
118
 * Returns 0 on success, -2 if the "refname" is not a symbolic ref,
119
 * -1 otherwise.
120
 */
121
int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
122
         struct strbuf *referent);
123
124
/*
125
 * Return 0 if a reference named refname could be created without
126
 * conflicting with the name of an existing reference. Otherwise,
127
 * return a negative value and write an explanation to err. If extras
128
 * is non-NULL, it is a list of additional refnames with which refname
129
 * is not allowed to conflict. If skip is non-NULL, ignore potential
130
 * conflicts with refs in skip (e.g., because they are scheduled for
131
 * deletion in the same operation). Behavior is undefined if the same
132
 * name is listed in both extras and skip.
133
 *
134
 * Two reference names conflict if one of them exactly matches the
135
 * leading components of the other; e.g., "foo/bar" conflicts with
136
 * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
137
 * "foo/barbados".
138
 *
139
 * If `initial_transaction` is truish, then all collision checks with
140
 * preexisting refs are skipped.
141
 *
142
 * extras and skip must be sorted.
143
 */
144
enum ref_transaction_error refs_verify_refname_available(struct ref_store *refs,
145
             const char *refname,
146
             const struct string_list *extras,
147
             const struct string_list *skip,
148
             unsigned int initial_transaction,
149
             struct strbuf *err);
150
151
int refs_ref_exists(struct ref_store *refs, const char *refname);
152
153
enum log_refs_config {
154
  LOG_REFS_UNSET = -1,
155
  LOG_REFS_NONE = 0,
156
  LOG_REFS_NORMAL,
157
  LOG_REFS_ALWAYS
158
};
159
160
enum log_refs_config refs_parse_log_all_ref_updates_config(const char *value);
161
162
int should_autocreate_reflog(enum log_refs_config log_all_ref_updates,
163
           const char *refname);
164
165
int is_branch(const char *refname);
166
167
0
#define REF_STORE_CREATE_ON_DISK_IS_WORKTREE (1 << 0)
168
169
int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err);
170
171
/*
172
 * Release all memory and resources associated with the ref store.
173
 */
174
void ref_store_release(struct ref_store *ref_store);
175
176
/*
177
 * Remove the ref store from disk. This deletes all associated data.
178
 */
179
int ref_store_remove_on_disk(struct ref_store *refs, struct strbuf *err);
180
181
/**
182
 * Resolve refname in the nested "gitlink" repository in the specified
183
 * submodule (which must be non-NULL). If the resolution is
184
 * successful, return 0 and set oid to the name of the object;
185
 * otherwise, return a non-zero value.
186
 */
187
int repo_resolve_gitlink_ref(struct repository *r,
188
           const char *submodule, const char *refname,
189
           struct object_id *oid);
190
191
/*
192
 * Return true iff abbrev_name is a possible abbreviation for
193
 * full_name according to the rules defined by ref_rev_parse_rules in
194
 * refs.c.
195
 */
196
int refname_match(const char *abbrev_name, const char *full_name);
197
198
/*
199
 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
200
 * the results to 'prefixes'
201
 */
202
struct strvec;
203
void expand_ref_prefix(struct strvec *prefixes, const char *prefix);
204
205
int expand_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
206
int repo_dwim_ref(struct repository *r, const char *str, int len,
207
      struct object_id *oid, char **ref, int nonfatal_dangling_mark);
208
int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
209
210
/*
211
 * Retrieves the default branch name for newly-initialized repositories.
212
 *
213
 * The return value is an allocated string.
214
 */
215
char *repo_default_branch_name(struct repository *r, int quiet);
216
217
/*
218
 * Copy "name" to "sb", expanding any special @-marks as handled by
219
 * repo_interpret_branch_name(). The result is a non-qualified branch name
220
 * (so "foo" or "origin/master" instead of "refs/heads/foo" or
221
 * "refs/remotes/origin/master").
222
 *
223
 * Note that the resulting name may not be a syntactically valid refname.
224
 *
225
 * If "allowed" is non-zero, restrict the set of allowed expansions. See
226
 * repo_interpret_branch_name() for details.
227
 */
228
void copy_branchname(struct repository *repo,
229
         struct strbuf *sb, const char *name,
230
         enum interpret_branch_kind allowed);
231
232
/*
233
 * Like copy_branchname() above, but confirm that the result is
234
 * syntactically valid to be used as a local branch name in refs/heads/.
235
 *
236
 * The return value is "0" if the result is valid, and "-1" otherwise.
237
 */
238
int check_branch_ref(struct repository *repo, struct strbuf *sb, const char *name);
239
240
/*
241
 * Similar for a tag name in refs/tags/.
242
 *
243
 * The return value is "0" if the result is valid, and "-1" otherwise.
244
 */
245
int check_tag_ref(struct strbuf *sb, const char *name);
246
247
/*
248
 * A ref_transaction represents a collection of reference updates that
249
 * should succeed or fail together.
250
 *
251
 * Calling sequence
252
 * ----------------
253
 *
254
 * - Allocate and initialize a `struct ref_transaction` by calling
255
 *   `ref_transaction_begin()`.
256
 *
257
 * - Specify the intended ref updates by calling one or more of the
258
 *   following functions:
259
 *   - `ref_transaction_update()`
260
 *   - `ref_transaction_create()`
261
 *   - `ref_transaction_delete()`
262
 *   - `ref_transaction_verify()`
263
 *
264
 * - Then either:
265
 *
266
 *   - Optionally call `ref_transaction_prepare()` to prepare the
267
 *     transaction. This locks all references, checks preconditions,
268
 *     etc. but doesn't finalize anything. If this step fails, the
269
 *     transaction has been closed and can only be freed. If this step
270
 *     succeeds, then `ref_transaction_commit()` is almost certain to
271
 *     succeed. However, you can still call `ref_transaction_abort()`
272
 *     if you decide not to commit the transaction after all.
273
 *
274
 *   - Call `ref_transaction_commit()` to execute the transaction,
275
 *     make the changes permanent, and release all locks. If you
276
 *     haven't already called `ref_transaction_prepare()`, then
277
 *     `ref_transaction_commit()` calls it for you.
278
 *
279
 *   Or
280
 *
281
 *   - Call `ref_transaction_begin()` with REF_TRANSACTION_FLAG_INITIAL if the
282
 *     ref database is known to be empty and have no other writers (e.g. during
283
 *     clone). This is likely to be much faster than without the flag.
284
 *
285
 * - Then finally, call `ref_transaction_free()` to free the
286
 *   `ref_transaction` data structure.
287
 *
288
 * At any time before calling `ref_transaction_commit()`, you can call
289
 * `ref_transaction_abort()` to abort the transaction, rollback any
290
 * locks, and free any associated resources (including the
291
 * `ref_transaction` data structure).
292
 *
293
 * Putting it all together, a complete reference update looks like
294
 *
295
 *         struct ref_transaction *transaction;
296
 *         struct strbuf err = STRBUF_INIT;
297
 *         int ret = 0;
298
 *
299
 *         transaction = ref_store_transaction_begin(refs, 0, &err);
300
 *         if (!transaction ||
301
 *             ref_transaction_update(...) ||
302
 *             ref_transaction_create(...) ||
303
 *             ...etc... ||
304
 *             ref_transaction_commit(transaction, &err)) {
305
 *                 error("%s", err.buf);
306
 *                 ret = -1;
307
 *         }
308
 *         ref_transaction_free(transaction);
309
 *         strbuf_release(&err);
310
 *         return ret;
311
 *
312
 * Error handling
313
 * --------------
314
 *
315
 * On error, transaction functions append a message about what
316
 * went wrong to the 'err' argument.  The message mentions what
317
 * ref was being updated (if any) when the error occurred so it
318
 * can be passed to 'die' or 'error' as-is.
319
 *
320
 * The message is appended to err without first clearing err.
321
 * err will not be '\n' terminated.
322
 *
323
 * Caveats
324
 * -------
325
 *
326
 * Note that no locks are taken, and no refs are read, until
327
 * `ref_transaction_prepare()` or `ref_transaction_commit()` is
328
 * called. So, for example, `ref_transaction_verify()` won't report a
329
 * verification failure until the commit is attempted.
330
 */
331
struct ref_transaction;
332
333
/*
334
 * Bit values set in the flags argument passed to refs_for_each_cb() and
335
 * stored in ref_iterator::flags. Other bits are for internal use
336
 * only:
337
 */
338
enum reference_status {
339
  /* Reference is a symbolic reference. */
340
  REF_ISSYMREF = (1 << 0),
341
342
  /* Reference is a packed reference. */
343
  REF_ISPACKED = (1 << 1),
344
345
  /*
346
   * Reference cannot be resolved to an object name: dangling symbolic
347
   * reference (directly or indirectly), corrupt reference file,
348
   * reference exists but name is bad, or symbolic reference refers to
349
   * ill-formatted reference name.
350
   */
351
  REF_ISBROKEN = (1 << 2),
352
353
  /*
354
   * Reference name is not well formed.
355
   *
356
   * See git-check-ref-format(1) for the definition of well formed ref names.
357
   */
358
  REF_BAD_NAME = (1 << 3),
359
};
360
361
/* A reference passed to `for_each_ref()`-style callbacks. */
362
struct reference {
363
  /* The fully-qualified name of the reference. */
364
  const char *name;
365
366
  /* The target of a symbolic ref. `NULL` for direct references. */
367
  const char *target;
368
369
  /*
370
   * The object ID of a reference. Either the direct object ID or the
371
   * resolved object ID in the case of a symbolic ref. May be the zero
372
   * object ID in case the symbolic ref cannot be resolved.
373
   */
374
  const struct object_id *oid;
375
376
  /*
377
   * An optional peeled object ID. This field _may_ be set for tags in
378
   * case the peeled value is present in the backend. Please refer to
379
   * `reference_get_peeled_oid()`.
380
   */
381
  const struct object_id *peeled_oid;
382
383
  /* A bitfield of `enum reference_status` flags. */
384
  unsigned flags;
385
};
386
387
/*
388
 * Peel the tag to a non-tag commit. If present, this uses the peeled object ID
389
 * exposed by the reference backend. Otherwise, the object is peeled via the
390
 * object database, which is less efficient.
391
 *
392
 * Return `0` if the reference could be peeled, a negative error code
393
 * otherwise.
394
 */
395
int reference_get_peeled_oid(struct repository *repo,
396
           const struct reference *ref,
397
           struct object_id *peeled_oid);
398
399
/*
400
 * The signature for the callback function for the for_each_*()
401
 * functions below.  The memory pointed to by the `struct reference`
402
 * argument is only guaranteed to be valid for the duration of a
403
 * single callback invocation.
404
 */
405
typedef int refs_for_each_cb(const struct reference *ref, void *cb_data);
406
407
/*
408
 * These flags are passed to refs_ref_iterator_begin() (and do_for_each_ref(),
409
 * which feeds it).
410
 */
411
enum refs_for_each_flag {
412
  /*
413
   * Include broken references in a do_for_each_ref*() iteration, which
414
   * would normally be omitted. This includes both refs that point to
415
   * missing objects (a true repository corruption), ones with illegal
416
   * names (which we prefer not to expose to callers), as well as
417
   * dangling symbolic refs (i.e., those that point to a non-existent
418
   * ref; this is not a corruption, but as they have no valid oid, we
419
   * omit them from normal iteration results).
420
   */
421
  REFS_FOR_EACH_INCLUDE_BROKEN = (1 << 0),
422
423
  /*
424
   * Only include per-worktree refs in a do_for_each_ref*() iteration.
425
   * Normally this will be used with a files ref_store, since that's
426
   * where all reference backends will presumably store their
427
   * per-worktree refs.
428
   */
429
  REFS_FOR_EACH_PER_WORKTREE_ONLY = (1 << 1),
430
431
  /*
432
   * Omit dangling symrefs from output; this only has an effect with
433
   * INCLUDE_BROKEN, since they are otherwise not included at all.
434
   */
435
  REFS_FOR_EACH_OMIT_DANGLING_SYMREFS = (1 << 2),
436
437
  /*
438
   * Include root refs, such as HEAD and ORIG_HEAD, along with the
439
   * regular refs.
440
   */
441
  REFS_FOR_EACH_INCLUDE_ROOT_REFS = (1 << 3),
442
};
443
444
/*
445
 * The following functions invoke the specified callback function for
446
 * each reference indicated.  If the function ever returns a nonzero
447
 * value, stop the iteration and return that value.  Please note that
448
 * it is not safe to modify references while an iteration is in
449
 * progress, unless the same callback function invocation that
450
 * modifies the reference also returns a nonzero value to immediately
451
 * stop the iteration. Returned references are sorted.
452
 */
453
int refs_head_ref(struct ref_store *refs,
454
      refs_for_each_cb fn, void *cb_data);
455
int refs_head_ref_namespaced(struct ref_store *refs,
456
           refs_for_each_cb fn, void *cb_data);
457
458
459
struct refs_for_each_ref_options {
460
  /* Only iterate over references that have this given prefix. */
461
  const char *prefix;
462
463
  /*
464
   * A globbing pattern that can be used to only yield refs that match.
465
   * If given, refs will be matched against the pattern with
466
   * `wildmatch()`.
467
   *
468
   * If the pattern doesn't contain any globbing characters then it is
469
   * treated as if it was ending with "/" and "*".
470
   */
471
  const char *pattern;
472
473
  /*
474
   * If set, only yield refs part of the configured namespace. Exclude
475
   * patterns will be rewritten to apply to the namespace, and the prefix
476
   * will be considered relative to the namespace.
477
   */
478
  const char *namespace;
479
480
  /*
481
   * Exclude any references that match any of these patterns on a
482
   * best-effort basis. The caller needs to be prepared for the exclude
483
   * patterns to be ignored.
484
   *
485
   * The array must be terminated with a NULL sentinel value.
486
   */
487
  const char **exclude_patterns;
488
489
  /*
490
   * The number of bytes to trim from the refname. Note that the trimmed
491
   * bytes must not cause the reference to become empty. As such, this
492
   * field should typically only be set when one uses a `prefix` ending
493
   * in a slash.
494
   */
495
  size_t trim_prefix;
496
497
  /* Flags that change which refs will be included. */
498
  enum refs_for_each_flag flags;
499
};
500
501
int refs_for_each_ref(struct ref_store *refs,
502
          refs_for_each_cb fn, void *cb_data);
503
int refs_for_each_ref_ext(struct ref_store *refs,
504
        refs_for_each_cb cb, void *cb_data,
505
        const struct refs_for_each_ref_options *opts);
506
int refs_for_each_tag_ref(struct ref_store *refs,
507
        refs_for_each_cb fn, void *cb_data);
508
int refs_for_each_branch_ref(struct ref_store *refs,
509
           refs_for_each_cb fn, void *cb_data);
510
int refs_for_each_remote_ref(struct ref_store *refs,
511
           refs_for_each_cb fn, void *cb_data);
512
int refs_for_each_replace_ref(struct ref_store *refs,
513
            refs_for_each_cb fn, void *cb_data);
514
515
/**
516
 * Iterate all refs in "prefixes" by partitioning prefixes into disjoint sets
517
 * and iterating the longest-common prefix of each set.
518
 */
519
int refs_for_each_ref_in_prefixes(struct ref_store *refs,
520
          const char **prefixes,
521
          const struct refs_for_each_ref_options *opts,
522
          refs_for_each_cb cb, void *cb_data);
523
524
/*
525
 * Normalizes partial refs to their fully qualified form.
526
 * Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'.
527
 * <prefix> will default to 'refs/' if NULL.
528
 *
529
 * item.string will be set to the result.
530
 * item.util will be set to NULL if <pattern> contains glob characters, or
531
 * non-NULL if it doesn't.
532
 */
533
void normalize_glob_ref(struct string_list_item *item, const char *prefix,
534
      const char *pattern);
535
536
static inline const char *has_glob_specials(const char *pattern)
537
0
{
538
0
  return strpbrk(pattern, "?*[");
539
0
}
Unexecuted instantiation: remote.c:has_glob_specials
Unexecuted instantiation: repo-settings.c:has_glob_specials
Unexecuted instantiation: repository.c:has_glob_specials
Unexecuted instantiation: revision.c:has_glob_specials
Unexecuted instantiation: setup.c:has_glob_specials
Unexecuted instantiation: shallow.c:has_glob_specials
Unexecuted instantiation: submodule.c:has_glob_specials
Unexecuted instantiation: symlinks.c:has_glob_specials
Unexecuted instantiation: trace.c:has_glob_specials
Unexecuted instantiation: transport.c:has_glob_specials
Unexecuted instantiation: worktree.c:has_glob_specials
Unexecuted instantiation: wt-status.c:has_glob_specials
Unexecuted instantiation: bisect.c:has_glob_specials
Unexecuted instantiation: branch.c:has_glob_specials
Unexecuted instantiation: bundle-uri.c:has_glob_specials
Unexecuted instantiation: bundle.c:has_glob_specials
Unexecuted instantiation: combine-diff.c:has_glob_specials
Unexecuted instantiation: commit-graph.c:has_glob_specials
Unexecuted instantiation: commit.c:has_glob_specials
Unexecuted instantiation: config.c:has_glob_specials
Unexecuted instantiation: connect.c:has_glob_specials
Unexecuted instantiation: diff-lib.c:has_glob_specials
Unexecuted instantiation: diff.c:has_glob_specials
Unexecuted instantiation: dir.c:has_glob_specials
Unexecuted instantiation: environment.c:has_glob_specials
Unexecuted instantiation: fetch-pack.c:has_glob_specials
Unexecuted instantiation: fsck.c:has_glob_specials
Unexecuted instantiation: hook.c:has_glob_specials
Unexecuted instantiation: line-log.c:has_glob_specials
Unexecuted instantiation: log-tree.c:has_glob_specials
Unexecuted instantiation: mailmap.c:has_glob_specials
Unexecuted instantiation: merge-ort.c:has_glob_specials
Unexecuted instantiation: midx.c:has_glob_specials
Unexecuted instantiation: notes-cache.c:has_glob_specials
Unexecuted instantiation: notes.c:has_glob_specials
Unexecuted instantiation: object-file.c:has_glob_specials
Unexecuted instantiation: object-name.c:has_glob_specials
Unexecuted instantiation: odb.c:has_glob_specials
Unexecuted instantiation: source-packed.c:has_glob_specials
Unexecuted instantiation: pack-bitmap.c:has_glob_specials
Unexecuted instantiation: parse-options-cb.c:has_glob_specials
Unexecuted instantiation: path.c:has_glob_specials
Unexecuted instantiation: pathspec.c:has_glob_specials
Unexecuted instantiation: pseudo-merge.c:has_glob_specials
Unexecuted instantiation: read-cache.c:has_glob_specials
Unexecuted instantiation: reflog-walk.c:has_glob_specials
Unexecuted instantiation: refs.c:has_glob_specials
Unexecuted instantiation: debug.c:has_glob_specials
Unexecuted instantiation: files-backend.c:has_glob_specials
Unexecuted instantiation: reftable-backend.c:has_glob_specials
Unexecuted instantiation: iterator.c:has_glob_specials
Unexecuted instantiation: packed-backend.c:has_glob_specials
Unexecuted instantiation: ref-cache.c:has_glob_specials
Unexecuted instantiation: refspec.c:has_glob_specials
Unexecuted instantiation: replace-object.c:has_glob_specials
Unexecuted instantiation: sequencer.c:has_glob_specials
Unexecuted instantiation: transport-helper.c:has_glob_specials
Unexecuted instantiation: unpack-trees.c:has_glob_specials
Unexecuted instantiation: apply.c:has_glob_specials
Unexecuted instantiation: attr.c:has_glob_specials
Unexecuted instantiation: default.c:has_glob_specials
Unexecuted instantiation: skipping.c:has_glob_specials
Unexecuted instantiation: notes-utils.c:has_glob_specials
Unexecuted instantiation: pack-bitmap-write.c:has_glob_specials
Unexecuted instantiation: reset.c:has_glob_specials
540
541
void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
542
        const char *indent, int dry_run,
543
        const struct string_list *refnames);
544
545
/*
546
 * Flags for controlling behaviour of refs_optimize()
547
 * REFS_OPTIMIZE_PRUNE: Prune loose refs after packing
548
 * REFS_OPTIMIZE_AUTO: Pack refs on a best effort basis. The heuristics and end
549
 *                     result are decided by the ref backend. Backends may ignore
550
 *                     this flag and fall back to a normal repack.
551
 */
552
0
#define REFS_OPTIMIZE_PRUNE (1 << 0)
553
0
#define REFS_OPTIMIZE_AUTO  (1 << 1)
554
555
struct refs_optimize_opts {
556
  unsigned int flags;
557
  struct ref_exclusions *exclusions;
558
  struct string_list *includes;
559
};
560
561
/*
562
 * Optimize the ref store. The exact behavior is up to the backend.
563
 * For the files backend, this is equivalent to packing refs.
564
 */
565
int refs_optimize(struct ref_store *refs, struct refs_optimize_opts *opts);
566
567
/*
568
 * Check if refs backend can be optimized by calling 'refs_optimize'.
569
 */
570
int refs_optimize_required(struct ref_store *ref_store,
571
         struct refs_optimize_opts *opts,
572
         bool *required);
573
574
/*
575
 * Setup reflog before using. Fill in err and return -1 on failure.
576
 */
577
int refs_create_reflog(struct ref_store *refs, const char *refname,
578
           struct strbuf *err);
579
580
/**
581
 * Reads log for the value of ref during at_time (in which case "cnt" should be
582
 * negative) or the reflog "cnt" entries from the top (in which case "at_time"
583
 * should be 0).
584
 *
585
 * If we found the reflog entry in question, returns 0 (and details of the
586
 * entry can be found in the out-parameters).
587
 *
588
 * If we ran out of reflog entries, the out-parameters are filled with the
589
 * details of the oldest entry we did find, and the function returns 1. Note
590
 * that there is one important special case here! If the reflog was empty
591
 * and the caller asked for the 0-th cnt, we will return "1" but leave the
592
 * "oid" field untouched.
593
 **/
594
int read_ref_at(struct ref_store *refs,
595
    const char *refname, unsigned int flags,
596
    timestamp_t at_time, int cnt,
597
    struct object_id *oid, char **msg,
598
    timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
599
600
/** Check if a particular reflog exists */
601
int refs_reflog_exists(struct ref_store *refs, const char *refname);
602
603
/*
604
 * Delete the specified reference. If old_oid is non-NULL, then
605
 * verify that the current value of the reference is old_oid before
606
 * deleting it. If old_oid is NULL, delete the reference if it
607
 * exists, regardless of its old value. It is an error for old_oid to
608
 * be null_oid. msg and flags are passed through to
609
 * ref_transaction_delete().
610
 */
611
int refs_delete_ref(struct ref_store *refs, const char *msg,
612
        const char *refname,
613
        const struct object_id *old_oid,
614
        unsigned int flags);
615
616
/*
617
 * Delete the specified references. If there are any problems, emit
618
 * errors but attempt to keep going (i.e., the deletes are not done in
619
 * an all-or-nothing transaction). msg and flags are passed through to
620
 * ref_transaction_delete().
621
 */
622
int refs_delete_refs(struct ref_store *refs, const char *msg,
623
         struct string_list *refnames, unsigned int flags);
624
625
/** Delete a reflog */
626
int refs_delete_reflog(struct ref_store *refs, const char *refname);
627
628
/*
629
 * Callback to process a reflog entry found by the iteration functions (see
630
 * below).
631
 *
632
 * The committer parameter is a single string, in the form
633
 * "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" (without double quotes).
634
 *
635
 * The timestamp parameter gives the time when entry was created as the number
636
 * of seconds since the UNIX epoch.
637
 *
638
 * The tz parameter gives the timezone offset for the user who created
639
 * the reflog entry, and its value gives a positive or negative offset
640
 * from UTC.  Its absolute value is formed by multiplying the hour
641
 * part by 100 and adding the minute part.  For example, 1 hour ahead
642
 * of UTC, CET == "+0100", is represented as positive one hundred (not
643
 * positive sixty).
644
 *
645
 * The msg parameter is a single complete line; a reflog message given
646
 * to refs_delete_ref, refs_update_ref, etc. is returned to the
647
 * callback normalized---each run of whitespaces are squashed into a
648
 * single whitespace, trailing whitespace, if exists, is trimmed, and
649
 * then a single LF is added at the end.
650
 *
651
 * The cb_data is a caller-supplied pointer given to the iterator
652
 * functions.
653
 */
654
typedef int each_reflog_ent_fn(const char *refname,
655
             struct object_id *old_oid,
656
             struct object_id *new_oid,
657
             const char *committer,
658
             timestamp_t timestamp,
659
             int tz, const char *msg,
660
             void *cb_data);
661
662
/* Iterate over reflog entries in the log for `refname`. */
663
664
/* oldest entry first */
665
int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
666
           each_reflog_ent_fn fn, void *cb_data);
667
668
/* youngest entry first */
669
int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
670
             const char *refname,
671
             each_reflog_ent_fn fn,
672
             void *cb_data);
673
674
/*
675
 * The signature for the callback function for the refs_for_each_reflog()
676
 * functions below. The memory pointed to by the refname argument is only
677
 * guaranteed to be valid for the duration of a single callback invocation.
678
 */
679
typedef int each_reflog_fn(const char *refname, void *cb_data);
680
681
/*
682
 * Calls the specified function for each reflog file until it returns nonzero,
683
 * and returns the value. Reflog file order is unspecified.
684
 */
685
int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data);
686
687
0
#define REFNAME_ALLOW_ONELEVEL 1
688
0
#define REFNAME_REFSPEC_PATTERN 2
689
690
/*
691
 * Return 0 iff refname has the correct format for a refname according
692
 * to the rules described in Documentation/git-check-ref-format.adoc.
693
 * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level
694
 * reference names.  If REFNAME_REFSPEC_PATTERN is set in flags, then
695
 * allow a single "*" wildcard character in the refspec. No leading or
696
 * repeated slashes are accepted.
697
 */
698
int check_refname_format(const char *refname, int flags);
699
700
struct fsck_ref_report;
701
702
/*
703
 * Perform generic checks for a specific direct ref. This function is
704
 * expected to be called by the ref backends for every symbolic ref.
705
 */
706
int refs_fsck_ref(struct ref_store *refs, struct fsck_options *o,
707
      struct fsck_ref_report *report,
708
      const char *refname, const struct object_id *oid);
709
710
/*
711
 * Perform generic checks for a specific symref target. This function is
712
 * expected to be called by the ref backends for every symbolic ref.
713
 */
714
int refs_fsck_symref(struct ref_store *refs, struct fsck_options *o,
715
         struct fsck_ref_report *report,
716
         const char *refname, const char *target);
717
718
/*
719
 * Check the reference database for consistency. Return 0 if refs and
720
 * reflogs are consistent, and non-zero otherwise. The errors will be
721
 * written to stderr.
722
 */
723
int refs_fsck(struct ref_store *refs, struct fsck_options *o,
724
        struct worktree *wt);
725
726
/*
727
 * Apply the rules from check_refname_format, but mutate the result until it
728
 * is acceptable, and place the result in "out".
729
 */
730
void sanitize_refname_component(const char *refname, struct strbuf *out);
731
732
const char *prettify_refname(const char *refname);
733
734
char *refs_shorten_unambiguous_ref(struct ref_store *refs,
735
           const char *refname, int strict);
736
737
/** rename ref, return 0 on success **/
738
int refs_rename_ref(struct ref_store *refs, const char *oldref,
739
        const char *newref, const char *logmsg);
740
741
/** copy ref, return 0 on success **/
742
int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
743
        const char *newref, const char *logmsg);
744
745
int refs_update_symref(struct ref_store *refs, const char *refname,
746
           const char *target, const char *logmsg);
747
748
int refs_update_symref_extended(struct ref_store *refs, const char *refname,
749
           const char *target, const char *logmsg,
750
           struct strbuf *referent, int create_only);
751
752
enum action_on_err {
753
  UPDATE_REFS_MSG_ON_ERR,
754
  UPDATE_REFS_DIE_ON_ERR,
755
  UPDATE_REFS_QUIET_ON_ERR
756
};
757
758
enum ref_transaction_flag {
759
  /*
760
   * The ref transaction is part of the initial creation of the ref store
761
   * and can thus assume that the ref store is completely empty. This
762
   * allows the backend to perform the transaction more efficiently by
763
   * skipping certain checks.
764
   *
765
   * It is a bug to set this flag when there might be other processes
766
   * accessing the repository or if there are existing references that
767
   * might conflict with the ones being created. All old_oid values must
768
   * either be absent or null_oid.
769
   */
770
  REF_TRANSACTION_FLAG_INITIAL = (1 << 0),
771
772
  /*
773
   * The transaction mechanism by default fails all updates if any conflict
774
   * is detected. This flag allows transactions to partially apply updates
775
   * while rejecting updates which do not match the expected state.
776
   */
777
  REF_TRANSACTION_ALLOW_FAILURE = (1 << 1),
778
};
779
780
/*
781
 * Begin a reference transaction.  The reference transaction must
782
 * be freed by calling ref_transaction_free().
783
 */
784
struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
785
                unsigned int flags,
786
                struct strbuf *err);
787
788
/*
789
 * Reference transaction updates
790
 *
791
 * The following four functions add a reference check or update to a
792
 * ref_transaction.  They have some common similar parameters:
793
 *
794
 *     transaction -- a pointer to an open ref_transaction, obtained
795
 *         from ref_transaction_begin().
796
 *
797
 *     refname -- the name of the reference to be affected.
798
 *
799
 *     new_oid -- the object ID that should be set to be the new value
800
 *         of the reference. Some functions allow this parameter to be
801
 *         NULL, meaning that the reference is not changed, or
802
 *         null_oid, meaning that the reference should be deleted. A
803
 *         copy of this value is made in the transaction.
804
 *
805
 *     old_oid -- the object ID that the reference must have before
806
 *         the update. Some functions allow this parameter to be NULL,
807
 *         meaning that the old value of the reference is not checked,
808
 *         or null_oid, meaning that the reference must not exist
809
 *         before the update. A copy of this value is made in the
810
 *         transaction.
811
 *
812
 *     new_target -- the target reference that the reference will be
813
 *         updated to point to. If the reference is a regular reference,
814
 *         it will be converted to a symbolic reference. Cannot be set
815
 *         together with `new_oid`. A copy of this value is made in the
816
 *         transaction.
817
 *
818
 *     old_target -- the reference that the reference must be pointing to.
819
 *         Canont be set together with `old_oid`. A copy of this value is
820
 *         made in the transaction.
821
 *
822
 *     flags -- flags affecting the update, passed to
823
 *         update_ref_lock(). Possible flags: REF_NO_DEREF,
824
 *         REF_FORCE_CREATE_REFLOG. See those constants for more
825
 *         information.
826
 *
827
 *     msg -- a message describing the change (for the reflog).
828
 *
829
 *     err -- a strbuf for receiving a description of any error that
830
 *         might have occurred.
831
 *
832
 * The functions make internal copies of refname and msg, so the
833
 * caller retains ownership of these parameters.
834
 *
835
 * The functions return 0 on success and non-zero on failure. A
836
 * failure means that the transaction as a whole has failed and needs
837
 * to be rolled back.
838
 */
839
840
/*
841
 * The following flags can be passed to ref_transaction_update() etc.
842
 * Internally, they are stored in `ref_update::flags`, along with some
843
 * internal flags.
844
 */
845
846
/*
847
 * Act on the ref directly; i.e., without dereferencing symbolic refs.
848
 * If this flag is not specified, then symbolic references are
849
 * dereferenced and the update is applied to the referent.
850
 */
851
0
#define REF_NO_DEREF (1 << 0)
852
853
/*
854
 * Force the creation of a reflog for this reference, even if it
855
 * didn't previously have a reflog.
856
 */
857
0
#define REF_FORCE_CREATE_REFLOG (1 << 1)
858
859
/*
860
 * Blindly write an object_id. This is useful for testing data corruption
861
 * scenarios.
862
 */
863
0
#define REF_SKIP_OID_VERIFICATION (1 << 10)
864
865
/*
866
 * Skip verifying refname. This is useful for testing data corruption scenarios.
867
 */
868
0
#define REF_SKIP_REFNAME_VERIFICATION (1 << 11)
869
870
/*
871
 * Skip creation of a reflog entry, even if it would have otherwise been
872
 * created.
873
 */
874
0
#define REF_SKIP_CREATE_REFLOG (1 << 12)
875
876
/*
877
 * When writing a REF_LOG_ONLY record, use the old and new object IDs provided
878
 * in the update instead of resolving the old object ID. The caller must also
879
 * set both REF_HAVE_OLD and REF_HAVE_NEW.
880
 */
881
0
#define REF_LOG_USE_PROVIDED_OIDS (1 << 13)
882
883
/*
884
 * Bitmask of all of the flags that are allowed to be passed in to
885
 * ref_transaction_update() and friends:
886
 */
887
#define REF_TRANSACTION_UPDATE_ALLOWED_FLAGS                                  \
888
0
  (REF_NO_DEREF | REF_FORCE_CREATE_REFLOG | REF_SKIP_OID_VERIFICATION | \
889
0
   REF_SKIP_REFNAME_VERIFICATION | REF_SKIP_CREATE_REFLOG | REF_LOG_USE_PROVIDED_OIDS)
890
891
/*
892
 * Add a reference update to transaction. `new_oid` is the value that
893
 * the reference should have after the update, or `null_oid` if it
894
 * should be deleted. If `new_oid` is NULL, then the reference is not
895
 * changed at all. `old_oid` is the value that the reference must have
896
 * before the update, or `null_oid` if it must not have existed
897
 * beforehand. The old value is checked after the lock is taken to
898
 * prevent races. If the old value doesn't agree with old_oid, the
899
 * whole transaction fails. If old_oid is NULL, then the previous
900
 * value is not checked. If `old_target` is not NULL, treat the reference
901
 * as a symbolic ref and validate that its target before the update is
902
 * `old_target`. If the `new_target` is not NULL, then the reference
903
 * will be updated to a symbolic ref which targets `new_target`.
904
 * Together, these allow us to update between regular refs and symrefs.
905
 *
906
 * See the above comment "Reference transaction updates" for more
907
 * information.
908
 */
909
enum ref_transaction_error ref_transaction_update(struct ref_transaction *transaction,
910
              const char *refname,
911
              const struct object_id *new_oid,
912
              const struct object_id *old_oid,
913
              const char *new_target,
914
              const char *old_target,
915
              unsigned int flags, const char *msg,
916
              struct strbuf *err);
917
918
/*
919
 * Similar to `ref_transaction_update`, but this function is only for adding
920
 * a reflog update. Supports providing custom committer information. The index
921
 * field can be utiltized to order updates as desired. When set to zero, the
922
 * updates default to being ordered by refname.
923
 */
924
int ref_transaction_update_reflog(struct ref_transaction *transaction,
925
          const char *refname,
926
          const struct object_id *new_oid,
927
          const struct object_id *old_oid,
928
          const char *committer_info,
929
          const char *msg,
930
          uint64_t index,
931
          struct strbuf *err);
932
933
/*
934
 * Add a reference creation to transaction. new_oid is the value that
935
 * the reference should have after the update; it must not be
936
 * null_oid. It is verified that the reference does not exist
937
 * already.
938
 *
939
 * See the above comment "Reference transaction updates" for more
940
 * information.
941
 */
942
int ref_transaction_create(struct ref_transaction *transaction,
943
         const char *refname,
944
         const struct object_id *new_oid,
945
         const char *new_target,
946
         unsigned int flags, const char *msg,
947
         struct strbuf *err);
948
949
/*
950
 * Add a reference deletion to transaction. If old_oid is non-NULL,
951
 * then it holds the value that the reference should have had before
952
 * the update (which must not be null_oid).
953
 *
954
 * See the above comment "Reference transaction updates" for more
955
 * information.
956
 */
957
int ref_transaction_delete(struct ref_transaction *transaction,
958
         const char *refname,
959
         const struct object_id *old_oid,
960
         const char *old_target,
961
         unsigned int flags,
962
         const char *msg,
963
         struct strbuf *err);
964
965
/*
966
 * Verify, within a transaction, that refname has the value old_oid,
967
 * or, if old_oid is null_oid, then verify that the reference
968
 * doesn't exist. old_oid must be non-NULL.
969
 *
970
 * See the above comment "Reference transaction updates" for more
971
 * information.
972
 */
973
int ref_transaction_verify(struct ref_transaction *transaction,
974
         const char *refname,
975
         const struct object_id *old_oid,
976
         const char *old_target,
977
         unsigned int flags,
978
         struct strbuf *err);
979
980
/*
981
 * Perform the preparatory stages of committing `transaction`. Acquire
982
 * any needed locks, check preconditions, etc.; basically, do as much
983
 * as possible to ensure that the transaction will be able to go
984
 * through, stopping just short of making any irrevocable or
985
 * user-visible changes. The updates that this function prepares can
986
 * be finished up by calling `ref_transaction_commit()` or rolled back
987
 * by calling `ref_transaction_abort()`.
988
 *
989
 * On success, return 0 and leave the transaction in "prepared" state.
990
 * On failure, abort the transaction, write an error message to `err`,
991
 * and return one of the `TRANSACTION_*` constants.
992
 *
993
 * Callers who don't need such fine-grained control over committing
994
 * reference transactions should just call `ref_transaction_commit()`.
995
 */
996
int ref_transaction_prepare(struct ref_transaction *transaction,
997
          struct strbuf *err);
998
999
/*
1000
 * Commit all of the changes that have been queued in transaction, as
1001
 * atomically as possible. On success, return 0 and leave the
1002
 * transaction in "closed" state. On failure, roll back the
1003
 * transaction, write an error message to `err`, and return one of the
1004
 * `TRANSACTION_*` constants
1005
 */
1006
int ref_transaction_commit(struct ref_transaction *transaction,
1007
         struct strbuf *err);
1008
1009
/*
1010
 * Abort `transaction`, which has been begun and possibly prepared,
1011
 * but not yet committed.
1012
 */
1013
int ref_transaction_abort(struct ref_transaction *transaction,
1014
        struct strbuf *err);
1015
1016
/*
1017
 * Execute the given callback function for each of the reference updates which
1018
 * have been queued in the given transaction. `old_oid` and `new_oid` may be
1019
 * `NULL` pointers depending on whether the update has these object IDs set or
1020
 * not.
1021
 */
1022
typedef void ref_transaction_for_each_queued_update_fn(const char *refname,
1023
                   const struct object_id *old_oid,
1024
                   const struct object_id *new_oid,
1025
                   void *cb_data);
1026
void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
1027
              ref_transaction_for_each_queued_update_fn cb,
1028
              void *cb_data);
1029
1030
/*
1031
 * Execute the given callback function for each of the reference updates which
1032
 * have been rejected in the given transaction.
1033
 */
1034
typedef void ref_transaction_for_each_rejected_update_fn(const char *refname,
1035
               const struct object_id *old_oid,
1036
               const struct object_id *new_oid,
1037
               const char *old_target,
1038
               const char *new_target,
1039
               enum ref_transaction_error err,
1040
               const char *details,
1041
               void *cb_data);
1042
void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction,
1043
                ref_transaction_for_each_rejected_update_fn cb,
1044
                void *cb_data);
1045
1046
/*
1047
 * Translate errors to human readable error messages.
1048
 */
1049
const char *ref_transaction_error_msg(enum ref_transaction_error err);
1050
1051
/*
1052
 * Free `*transaction` and all associated data.
1053
 */
1054
void ref_transaction_free(struct ref_transaction *transaction);
1055
1056
/**
1057
 * Lock, update, and unlock a single reference. This function
1058
 * basically does a transaction containing a single call to
1059
 * ref_transaction_update(). The parameters to this function have the
1060
 * same meaning as the corresponding parameters to
1061
 * ref_transaction_update(). Handle errors as requested by the `onerr`
1062
 * argument.
1063
 */
1064
int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname,
1065
        const struct object_id *new_oid, const struct object_id *old_oid,
1066
        unsigned int flags, enum action_on_err onerr);
1067
1068
int parse_hide_refs_config(const char *var, const char *value, const char *,
1069
         struct strvec *);
1070
1071
/*
1072
 * Check whether a ref is hidden. If no namespace is set, both the first and
1073
 * the second parameter point to the full ref name. If a namespace is set and
1074
 * the ref is inside that namespace, the first parameter is a pointer to the
1075
 * name of the ref with the namespace prefix removed. If a namespace is set and
1076
 * the ref is outside that namespace, the first parameter is NULL. The second
1077
 * parameter always points to the full ref name.
1078
 */
1079
int ref_is_hidden(const char *, const char *, const struct strvec *);
1080
1081
/*
1082
 * Returns an array of patterns to use as excluded_patterns, if none of the
1083
 * hidden references use the token '!' or '^'.
1084
 */
1085
const char **hidden_refs_to_excludes(const struct strvec *hide_refs);
1086
1087
/*
1088
 * Prefix all exclude patterns with the namespace, if any. This is required
1089
 * because exclude patterns apply to the stripped reference name, not the full
1090
 * reference name with the namespace.
1091
 */
1092
const char **get_namespaced_exclude_patterns(const char **exclude_patterns,
1093
               const char *namespace,
1094
               struct strvec *out);
1095
1096
/* Is this a per-worktree ref living in the refs/ namespace? */
1097
int is_per_worktree_ref(const char *refname);
1098
1099
/* Describes how a refname relates to worktrees */
1100
enum ref_worktree_type {
1101
  REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or
1102
         refs/bisect/SOMETHING */
1103
  REF_WORKTREE_MAIN, /* explicitly in main worktree, eg.
1104
            main-worktree/HEAD */
1105
  REF_WORKTREE_OTHER, /* explicitly in named worktree, eg.
1106
             worktrees/bla/HEAD */
1107
  REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */
1108
};
1109
1110
/*
1111
 * Parse a `maybe_worktree_ref` as a ref that possibly refers to a worktree ref
1112
 * (ie. either REFNAME, main-worktree/REFNAME or worktree/WORKTREE/REFNAME). It
1113
 * returns what kind of ref was found, and in case of REF_WORKTREE_OTHER, the
1114
 * worktree name is returned in `worktree_name` (pointing into
1115
 * `maybe_worktree_ref`) and `worktree_name_length`. The bare refname (the
1116
 * refname stripped of prefixes) is returned in `bare_refname`. The
1117
 * `worktree_name`, `worktree_name_length` and `bare_refname` arguments may be
1118
 * NULL.
1119
 */
1120
enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
1121
            const char **worktree_name,
1122
            int *worktree_name_length,
1123
            const char **bare_refname);
1124
1125
enum expire_reflog_flags {
1126
  EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
1127
  EXPIRE_REFLOGS_UPDATE_REF = 1 << 1,
1128
  EXPIRE_REFLOGS_REWRITE = 1 << 2,
1129
};
1130
1131
/*
1132
 * The following interface is used for reflog expiration. The caller
1133
 * calls refs_reflog_expire(), supplying it with three callback functions,
1134
 * of the following types. The callback functions define the
1135
 * expiration policy that is desired.
1136
 *
1137
 * reflog_expiry_prepare_fn -- Called once after the reference is
1138
 *     locked. Called with the OID of the locked reference.
1139
 *
1140
 * reflog_expiry_should_prune_fn -- Called once for each entry in the
1141
 *     existing reflog. It should return true iff that entry should be
1142
 *     pruned.
1143
 *
1144
 * reflog_expiry_cleanup_fn -- Called once before the reference is
1145
 *     unlocked again.
1146
 */
1147
typedef void reflog_expiry_prepare_fn(const char *refname,
1148
              const struct object_id *oid,
1149
              void *cb_data);
1150
typedef int reflog_expiry_should_prune_fn(struct object_id *ooid,
1151
            struct object_id *noid,
1152
            const char *email,
1153
            timestamp_t timestamp, int tz,
1154
            const char *message, void *cb_data);
1155
typedef void reflog_expiry_cleanup_fn(void *cb_data);
1156
1157
/*
1158
 * Expire reflog entries for the specified reference.
1159
 * flags is a combination of the constants in
1160
 * enum expire_reflog_flags. The three function pointers are described
1161
 * above. On success, return zero.
1162
 */
1163
int refs_reflog_expire(struct ref_store *refs,
1164
           const char *refname,
1165
           unsigned int flags,
1166
           reflog_expiry_prepare_fn prepare_fn,
1167
           reflog_expiry_should_prune_fn should_prune_fn,
1168
           reflog_expiry_cleanup_fn cleanup_fn,
1169
           void *policy_cb_data);
1170
1171
struct ref_store *get_main_ref_store(struct repository *r);
1172
1173
/**
1174
 * Submodules
1175
 * ----------
1176
 *
1177
 * If you want to iterate the refs of a submodule you first need to add the
1178
 * submodules object database. You can do this by a code-snippet like
1179
 * this:
1180
 *
1181
 *  const char *path = "path/to/submodule"
1182
 *  if (add_submodule_odb(path))
1183
 *    die("Error submodule '%s' not populated.", path);
1184
 *
1185
 * `add_submodule_odb()` will return zero on success. If you
1186
 * do not do this you will get an error for each ref that it does not point
1187
 * to a valid object.
1188
 *
1189
 * Note: As a side-effect of this you cannot safely assume that all
1190
 * objects you lookup are available in superproject. All submodule objects
1191
 * will be available the same way as the superprojects objects.
1192
 *
1193
 * Example:
1194
 * --------
1195
 *
1196
 * ----
1197
 * static int handle_remote_ref(const char *refname,
1198
 *    const unsigned char *sha1, int flags, void *cb_data)
1199
 * {
1200
 *  struct strbuf *output = cb_data;
1201
 *  strbuf_addf(output, "%s\n", refname);
1202
 *  return 0;
1203
 * }
1204
 *
1205
 */
1206
1207
/*
1208
 * Return the ref_store instance for the specified submodule. For the
1209
 * main repository, use submodule==NULL; such a call cannot fail. For
1210
 * a submodule, the submodule must exist and be a nonbare repository,
1211
 * otherwise return NULL. If the requested reference store has not yet
1212
 * been initialized, initialize it first.
1213
 *
1214
 * For backwards compatibility, submodule=="" is treated the same as
1215
 * submodule==NULL.
1216
 */
1217
struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
1218
                 const char *submodule);
1219
struct ref_store *get_worktree_ref_store(const struct worktree *wt);
1220
1221
/*
1222
 * Some of the names specified by refs have special meaning to Git.
1223
 * Organize these namespaces in a common 'ref_namespace' array for
1224
 * reference from multiple places in the codebase.
1225
 */
1226
1227
struct ref_namespace_info {
1228
  const char *ref;
1229
  enum decoration_type decoration;
1230
1231
  /*
1232
   * If 'exact' is true, then we must match the 'ref' exactly.
1233
   * Otherwise, use a prefix match.
1234
   *
1235
   * 'ref_updated' is for internal use. It represents whether the
1236
   * 'ref' value was replaced from its original literal version.
1237
   */
1238
  unsigned exact:1,
1239
     ref_updated:1;
1240
};
1241
1242
enum ref_namespace {
1243
  NAMESPACE_HEAD,
1244
  NAMESPACE_BRANCHES,
1245
  NAMESPACE_TAGS,
1246
  NAMESPACE_REMOTE_REFS,
1247
  NAMESPACE_STASH,
1248
  NAMESPACE_REPLACE,
1249
  NAMESPACE_NOTES,
1250
  NAMESPACE_PREFETCH,
1251
  NAMESPACE_REWRITTEN,
1252
1253
  /* Must be last */
1254
  NAMESPACE__COUNT
1255
};
1256
1257
/* See refs.c for the contents of this array. */
1258
extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT];
1259
1260
/*
1261
 * Some ref namespaces can be modified by config values or environment
1262
 * variables. Modify a namespace as specified by its ref_namespace key.
1263
 */
1264
void update_ref_namespace(enum ref_namespace namespace, char *ref);
1265
1266
/*
1267
 * Check whether the provided name names a root reference. This function only
1268
 * performs a syntactic check.
1269
 *
1270
 * A root ref is a reference that lives in the root of the reference hierarchy.
1271
 * These references must conform to special syntax:
1272
 *
1273
 *   - Their name must be all-uppercase or underscores ("_").
1274
 *
1275
 *   - Their name must end with "_HEAD". As a special rule, "HEAD" is a root
1276
 *     ref, as well.
1277
 *
1278
 *   - Their name may not contain a slash.
1279
 *
1280
 * There is a special set of irregular root refs that exist due to historic
1281
 * reasons, only. This list shall not be expanded in the future:
1282
 *
1283
 *   - AUTO_MERGE
1284
 *
1285
 *   - BISECT_EXPECTED_REV
1286
 *
1287
 *   - NOTES_MERGE_PARTIAL
1288
 *
1289
 *   - NOTES_MERGE_REF
1290
 *
1291
 *   - MERGE_AUTOSTASH
1292
 */
1293
int is_root_ref(const char *refname);
1294
1295
/*
1296
 * Pseudorefs are names that can be used like refs, but have additional
1297
 * semantics. They cannot be stored in the ref backend, but must always
1298
 * be accessed via the filesystem. The following are pseudorefs:
1299
 *
1300
 * - FETCH_HEAD may contain multiple object IDs, and each one of them
1301
 *   carries additional metadata like where it came from.
1302
 *
1303
 * - MERGE_HEAD may contain multiple object IDs when merging multiple
1304
 *   heads.
1305
 *
1306
 * Reading, writing or deleting references must consistently go either
1307
 * through the filesystem (pseudorefs) or through the reference
1308
 * backend (normal ones).
1309
 */
1310
int is_pseudo_ref(const char *refname);
1311
1312
/*
1313
 * The following flags can be passed to `repo_migrate_ref_storage_format()`:
1314
 *
1315
 *   - REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN: perform a dry-run migration
1316
 *     without touching the main repository. The result will be written into a
1317
 *     temporary ref storage directory.
1318
 *
1319
 *   - REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG: skip migration of reflogs.
1320
 */
1321
0
#define REPO_MIGRATE_REF_STORAGE_FORMAT_DRYRUN      (1 << 0)
1322
0
#define REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG (1 << 1)
1323
1324
/*
1325
 * Migrate the ref storage format used by the repository to the
1326
 * specified one.
1327
 */
1328
int repo_migrate_ref_storage_format(struct repository *repo,
1329
            enum ref_storage_format format,
1330
            unsigned int flags,
1331
            struct strbuf *err);
1332
1333
/*
1334
 * Reference iterators
1335
 *
1336
 * A reference iterator encapsulates the state of an in-progress
1337
 * iteration over references. Create an instance of `struct
1338
 * ref_iterator` via one of the functions in this module.
1339
 *
1340
 * A freshly-created ref_iterator doesn't yet point at a reference. To
1341
 * advance the iterator, call ref_iterator_advance(). If successful,
1342
 * this sets the iterator's refname, oid, and flags fields to describe
1343
 * the next reference and returns ITER_OK. The data pointed at by
1344
 * refname and oid belong to the iterator; if you want to retain them
1345
 * after calling ref_iterator_advance() again or calling
1346
 * ref_iterator_free(), you must make a copy. When the iteration has
1347
 * been exhausted, ref_iterator_advance() releases any resources
1348
 * associated with the iteration, frees the ref_iterator object, and
1349
 * returns ITER_DONE. If you want to abort the iteration early, call
1350
 * ref_iterator_free(), which also frees the ref_iterator object and
1351
 * any associated resources. If there was an internal error advancing
1352
 * to the next entry, ref_iterator_advance() aborts the iteration,
1353
 * frees the ref_iterator, and returns ITER_ERROR.
1354
 *
1355
 * Putting it all together, a typical iteration looks like this:
1356
 *
1357
 *     int ok;
1358
 *     struct ref_iterator *iter = ...;
1359
 *
1360
 *     while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1361
 *             if (want_to_stop_iteration()) {
1362
 *                     ok = ITER_DONE;
1363
 *                     break;
1364
 *             }
1365
 *
1366
 *             // Access information about the current reference:
1367
 *             if (!(iter->flags & REF_ISSYMREF))
1368
 *                     printf("%s is %s\n", iter->refname, oid_to_hex(iter->oid));
1369
 *     }
1370
 *
1371
 *     if (ok != ITER_DONE)
1372
 *             handle_error();
1373
 *     ref_iterator_free(iter);
1374
 */
1375
struct ref_iterator;
1376
1377
/*
1378
 * Return an iterator that goes over each reference in `refs` for
1379
 * which the refname begins with prefix. If trim is non-zero, then
1380
 * trim that many characters off the beginning of each refname.
1381
 * The output is ordered by refname.
1382
 */
1383
struct ref_iterator *refs_ref_iterator_begin(
1384
  struct ref_store *refs,
1385
  const char *prefix, const char **exclude_patterns,
1386
  int trim, enum refs_for_each_flag flags);
1387
1388
/*
1389
 * Advance the iterator to the first or next item and return ITER_OK.
1390
 * If the iteration is exhausted, free the resources associated with
1391
 * the ref_iterator and return ITER_DONE. On errors, free the iterator
1392
 * resources and return ITER_ERROR. It is a bug to use ref_iterator or
1393
 * call this function again after it has returned ITER_DONE or
1394
 * ITER_ERROR.
1395
 */
1396
int ref_iterator_advance(struct ref_iterator *ref_iterator);
1397
1398
enum ref_iterator_seek_flag {
1399
  /*
1400
   * When the REF_ITERATOR_SEEK_SET_PREFIX flag is set, the iterator's prefix is
1401
   * updated to match the provided string, affecting all subsequent iterations. If
1402
   * not, the iterator seeks to the specified reference and clears any previously
1403
   * set prefix.
1404
   */
1405
  REF_ITERATOR_SEEK_SET_PREFIX = (1 << 0),
1406
};
1407
1408
/*
1409
 * Seek the iterator to the first reference matching the given seek string.
1410
 * The seek string is matched as a literal string, without regard for path
1411
 * separators. If seek is NULL or the empty string, seek the iterator to the
1412
 * first reference again.
1413
 *
1414
 * This function is expected to behave as if a new ref iterator has been
1415
 * created, but allows reuse of existing iterators for optimization.
1416
 *
1417
 * Returns 0 on success, a negative error code otherwise.
1418
 */
1419
int ref_iterator_seek(struct ref_iterator *ref_iterator, const char *refname,
1420
          unsigned int flags);
1421
1422
/* Free the reference iterator and any associated resources. */
1423
void ref_iterator_free(struct ref_iterator *ref_iterator);
1424
1425
/*
1426
 * The common backend for the for_each_*ref* functions. Call fn for
1427
 * each reference in iter. If the iterator itself ever returns
1428
 * ITER_ERROR, return -1. If fn ever returns a non-zero value, stop
1429
 * the iteration and return that value. Otherwise, return 0. In any
1430
 * case, free the iterator when done. This function is basically an
1431
 * adapter between the callback style of reference iteration and the
1432
 * iterator style.
1433
 */
1434
int do_for_each_ref_iterator(struct ref_iterator *iter,
1435
           refs_for_each_cb fn, void *cb_data);
1436
1437
/*
1438
 * Git only recognizes a directory as a repository if it contains:
1439
 * - HEAD file
1440
 * - refs/ folder
1441
 * While it is necessary within the files backend, newer backends may not
1442
 * follow the same structure. To go around this, we create stubs as necessary.
1443
 *
1444
 * If provided with a 'refs_heads_content', we create the 'refs/heads/head' file
1445
 * with the provided message.
1446
 */
1447
void refs_create_refdir_stubs(struct repository *repo, const char *refdir,
1448
            const char *refs_heads_content);
1449
1450
#endif /* REFS_H */