]> git.proxmox.com Git - libgit2.git/blame - include/git2/refs.h
Improve docs, examples, warnings
[libgit2.git] / include / git2 / refs.h
CommitLineData
2f8a8ab2 1/*
5e0de328 2 * Copyright (C) 2009-2012 the libgit2 contributors
2f8a8ab2 3 *
bb742ede
VM
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
2f8a8ab2
VM
6 */
7#ifndef INCLUDE_git_refs_h__
8#define INCLUDE_git_refs_h__
9
10#include "common.h"
11#include "types.h"
fc70832a 12#include "oid.h"
b46708aa 13#include "strarray.h"
2f8a8ab2
VM
14
15/**
16 * @file git2/refs.h
17 * @brief Git reference management routines
18 * @defgroup git_reference Git reference management routines
19 * @ingroup Git
20 * @{
21 */
22GIT_BEGIN_DECL
23
5de079b8 24/**
b90500f0 25 * Lookup a reference by name in a repository.
5de079b8 26 *
b90500f0
RB
27 * The returned reference must be freed by the user.
28 *
29 * See `git_reference_create_symbolic()` for documentation about valid
30 * reference names.
5de079b8
VM
31 *
32 * @param reference_out pointer to the looked-up reference
33 * @param repo the repository to look up the reference
b90500f0 34 * @param name the long name for the reference (e.g. HEAD, refs/heads/master, refs/tags/v0.1.0, ...)
e172cf08 35 * @return 0 or an error code
5de079b8
VM
36 */
37GIT_EXTERN(int) git_reference_lookup(git_reference **reference_out, git_repository *repo, const char *name);
38
f201d613
RB
39/**
40 * Lookup a reference by name and resolve immediately to OID.
41 *
b90500f0
RB
42 * This function provides a quick way to resolve a reference name straight
43 * through to the object id that it refers to. This avoids having to
44 * allocate or free any `git_reference` objects for simple situations.
45 *
f201d613
RB
46 * @param oid Pointer to oid to be filled in
47 * @param repo The repository in which to look up the reference
48 * @param name The long name for the reference
49 * @return 0 on success, -1 if name could not be resolved
50 */
26515e73
RB
51GIT_EXTERN(int) git_reference_name_to_oid(
52 git_oid *out, git_repository *repo, const char *name);
f201d613 53
2f8a8ab2 54/**
1d8cc731 55 * Create a new symbolic reference.
2f8a8ab2 56 *
b90500f0
RB
57 * A symbolic reference is a reference name that refers to another
58 * reference name. If the other name moves, the symbolic name will move,
59 * too. As a simple example, the "HEAD" reference might refer to
60 * "refs/heads/master" while on the "master" branch of a repository.
61 *
62 * The symbolic reference will be created in the repository and written to
63 * the disk. The generated reference object must be freed by the user.
64 *
65 * Valid reference names must follow one of two patterns:
2f8a8ab2 66 *
b90500f0
RB
67 * 1. Top-level names must contain only capital letters and underscores,
68 * and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD").
69 * 2. Names prefixed with "refs/" can be almost anything. You must avoid
70 * the characters '~', '^', ':', '\\', '?', '[', and '*', and the
71 * sequences ".." and "@{" which have special meaning to revparse.
32054c24 72 *
b90500f0
RB
73 * This function will return an error if a reference already exists with the
74 * given name unless `force` is true, in which case it will be overwritten.
fa204962
CMN
75 *
76 * @param ref_out Pointer to the newly created reference
77 * @param repo Repository where that reference will live
78 * @param name The name of the reference
79 * @param target The target of the reference
d5afc039 80 * @param force Overwrite existing references
e172cf08 81 * @return 0 or an error code
fa204962 82 */
d5afc039 83GIT_EXTERN(int) git_reference_create_symbolic(git_reference **ref_out, git_repository *repo, const char *name, const char *target, int force);
fa204962 84
1d8cc731 85/**
b90500f0 86 * Create a new direct reference.
1d8cc731 87 *
b90500f0
RB
88 * A direct reference (also called an object id reference) refers directly
89 * to a specific object id (a.k.a. OID or SHA) in the repository. The id
90 * permanently refers to the object (although the reference itself can be
91 * moved). For example, in libgit2 the direct ref "refs/tags/v0.17.0"
92 * refers to OID 5b9fac39d8a76b9139667c26a63e6b3f204b3977.
1d8cc731 93 *
b90500f0
RB
94 * The direct reference will be created in the repository and written to
95 * the disk. The generated reference object must be freed by the user.
32054c24 96 *
b90500f0
RB
97 * Valid reference names must follow one of two patterns:
98 *
99 * 1. Top-level names must contain only capital letters and underscores,
100 * and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD").
101 * 2. Names prefixed with "refs/" can be almost anything. You must avoid
102 * the characters '~', '^', ':', '\\', '?', '[', and '*', and the
103 * sequences ".." and "@{" which have special meaning to revparse.
104 *
105 * This function will return an error if a reference already exists with the
106 * given name unless `force` is true, in which case it will be overwritten.
fa204962
CMN
107 *
108 * @param ref_out Pointer to the newly created reference
109 * @param repo Repository where that reference will live
110 * @param name The name of the reference
111 * @param id The object id pointed to by the reference.
d5afc039 112 * @param force Overwrite existing references
e172cf08 113 * @return 0 or an error code
fa204962 114 */
d5afc039 115GIT_EXTERN(int) git_reference_create_oid(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id, int force);
fa204962 116
2f8a8ab2 117/**
b90500f0
RB
118 * Get the OID pointed to by a direct reference.
119 *
120 * Only available if the reference is direct (i.e. an object id reference,
121 * not a symbolic one).
932d1baf 122 *
b90500f0
RB
123 * To find the OID of a symbolic ref, call `git_reference_resolve()` and
124 * then this function (or maybe use `git_reference_name_to_oid()` to
125 * directly resolve a reference name all the way through to an OID).
2f8a8ab2
VM
126 *
127 * @param ref The reference
128 * @return a pointer to the oid if available, NULL otherwise
129 */
130GIT_EXTERN(const git_oid *) git_reference_oid(git_reference *ref);
131
132/**
b90500f0 133 * Get full name to the reference pointed to by a symbolic reference.
932d1baf 134 *
b90500f0 135 * Only available if the reference is symbolic.
2f8a8ab2
VM
136 *
137 * @param ref The reference
138 * @return a pointer to the name if available, NULL otherwise
139 */
140GIT_EXTERN(const char *) git_reference_target(git_reference *ref);
141
142/**
b90500f0 143 * Get the type of a reference.
2f8a8ab2
VM
144 *
145 * Either direct (GIT_REF_OID) or symbolic (GIT_REF_SYMBOLIC)
146 *
147 * @param ref The reference
148 * @return the type
149 */
2e2e9785 150GIT_EXTERN(git_ref_t) git_reference_type(git_reference *ref);
2f8a8ab2
VM
151
152/**
b90500f0
RB
153 * Get the full name of a reference.
154 *
155 * See `git_reference_create_symbolic()` for rules about valid names.
2f8a8ab2
VM
156 *
157 * @param ref The reference
158 * @return the full name for the ref
159 */
160GIT_EXTERN(const char *) git_reference_name(git_reference *ref);
161
162/**
b90500f0 163 * Resolve a symbolic reference to a direct reference.
2f8a8ab2 164 *
b90500f0
RB
165 * This method iteratively peels a symbolic reference until it resolves to
166 * a direct reference to an OID.
2f8a8ab2 167 *
b90500f0
RB
168 * The peeled reference is returned in the `resolved_ref` argument, and
169 * must be freed manually once it's no longer needed.
d4a0b124 170 *
b90500f0
RB
171 * If a direct reference is passed as an argument, a copy of that
172 * reference is returned. This copy must be manually freed too.
2f8a8ab2
VM
173 *
174 * @param resolved_ref Pointer to the peeled reference
175 * @param ref The reference
e172cf08 176 * @return 0 or an error code
2f8a8ab2
VM
177 */
178GIT_EXTERN(int) git_reference_resolve(git_reference **resolved_ref, git_reference *ref);
179
2f8a8ab2 180/**
b90500f0 181 * Get the repository where a reference resides.
2f8a8ab2
VM
182 *
183 * @param ref The reference
184 * @return a pointer to the repo
185 */
186GIT_EXTERN(git_repository *) git_reference_owner(git_reference *ref);
187
2f8a8ab2 188/**
32054c24 189 * Set the symbolic target of a reference.
2f8a8ab2 190 *
b90500f0 191 * The reference must be a symbolic reference, otherwise this will fail.
2f8a8ab2 192 *
b90500f0 193 * The reference will be automatically updated in memory and on disk.
2f8a8ab2
VM
194 *
195 * @param ref The reference
196 * @param target The new target for the reference
e172cf08 197 * @return 0 or an error code
2f8a8ab2 198 */
1d8cc731 199GIT_EXTERN(int) git_reference_set_target(git_reference *ref, const char *target);
2f8a8ab2
VM
200
201/**
202 * Set the OID target of a reference.
203 *
b90500f0 204 * The reference must be a direct reference, otherwise this will fail.
2f8a8ab2 205 *
b90500f0 206 * The reference will be automatically updated in memory and on disk.
2f8a8ab2
VM
207 *
208 * @param ref The reference
d144c569 209 * @param id The new target OID for the reference
e172cf08 210 * @return 0 or an error code
2f8a8ab2 211 */
1d8cc731 212GIT_EXTERN(int) git_reference_set_oid(git_reference *ref, const git_oid *id);
2f8a8ab2 213
32054c24 214/**
b90500f0 215 * Rename an existing reference.
32054c24
VM
216 *
217 * This method works for both direct and symbolic references.
218 * The new name will be checked for validity and may be
219 * modified into a normalized form.
220 *
d4a0b124 221 * The given git_reference will be updated in place.
a46ec457 222 *
b90500f0 223 * The reference will be immediately renamed in-memory and on disk.
32054c24 224 *
d4a0b124
VM
225 * If the `force` flag is not enabled, and there's already
226 * a reference with the given name, the renaming will fail.
227 *
a5cd086d
MS
228 * IMPORTANT:
229 * The user needs to write a proper reflog entry if the
230 * reflog is enabled for the repository. We only rename
231 * the reflog if it exists.
232 *
d4a0b124
VM
233 * @param ref The reference to rename
234 * @param new_name The new name for the reference
235 * @param force Overwrite an existing reference
e172cf08 236 * @return 0 or an error code
d4a0b124 237 *
32054c24 238 */
7376ad99 239GIT_EXTERN(int) git_reference_rename(git_reference *ref, const char *new_name, int force);
fa204962 240
32054c24 241/**
b90500f0 242 * Delete an existing reference.
32054c24
VM
243 *
244 * This method works for both direct and symbolic references.
245 *
b90500f0
RB
246 * The reference will be immediately removed on disk and from memory
247 * (i.e. freed). The given reference pointer will no longer be valid.
32054c24 248 *
d4a0b124 249 * @param ref The reference to remove
e172cf08 250 * @return 0 or an error code
32054c24
VM
251 */
252GIT_EXTERN(int) git_reference_delete(git_reference *ref);
253
87d3acf4 254/**
b90500f0 255 * Pack all the loose references in the repository.
87d3acf4
VM
256 *
257 * This method will load into the cache all the loose
932d1baf 258 * references on the repository and update the
87d3acf4
VM
259 * `packed-refs` file with them.
260 *
261 * Once the `packed-refs` file has been written properly,
262 * the loose references will be removed from disk.
263 *
87d3acf4 264 * @param repo Repository where the loose refs will be packed
e172cf08 265 * @return 0 or an error code
87d3acf4
VM
266 */
267GIT_EXTERN(int) git_reference_packall(git_repository *repo);
268
00571828 269/**
b90500f0 270 * Fill a list with all the references that can be found in a repository.
00571828 271 *
b90500f0
RB
272 * Using the `list_flags` parameter, the listed references may be filtered
273 * by type (`GIT_REF_OID` or `GIT_REF_SYMBOLIC`) or using a bitwise OR of
274 * `git_ref_t` values. To include packed refs, include `GIT_REF_PACKED`.
275 * For convenience, use the value `GIT_REF_LISTALL` to obtain all
276 * references, including packed ones.
00571828 277 *
b90500f0
RB
278 * The string array will be filled with the names of all references; these
279 * values are owned by the user and should be free'd manually when no
280 * longer needed, using `git_strarray_free()`.
00571828
VM
281 *
282 * @param array Pointer to a git_strarray structure where
283 * the reference names will be stored
284 * @param repo Repository where to find the refs
b90500f0 285 * @param list_flags Filtering flags for the reference listing
e172cf08 286 * @return 0 or an error code
00571828 287 */
4fbd1c00 288GIT_EXTERN(int) git_reference_list(git_strarray *array, git_repository *repo, unsigned int list_flags);
00571828 289
09e8de0f 290/**
b90500f0 291 * Perform a callback on each reference in the repository.
09e8de0f 292 *
b90500f0
RB
293 * Using the `list_flags` parameter, the references may be filtered by
294 * type (`GIT_REF_OID` or `GIT_REF_SYMBOLIC`) or using a bitwise OR of
295 * `git_ref_t` values. To include packed refs, include `GIT_REF_PACKED`.
296 * For convenience, use the value `GIT_REF_LISTALL` to obtain all
297 * references, including packed ones.
09e8de0f 298 *
b90500f0
RB
299 * The `callback` function will be called for each reference in the
300 * repository, receiving the name of the reference and the `payload` value
301 * passed to this method. Returning a non-zero value from the callback
302 * will terminate the iteration.
09e8de0f
VM
303 *
304 * @param repo Repository where to find the refs
b90500f0 305 * @param list_flags Filtering flags for the reference listing.
09e8de0f
VM
306 * @param callback Function which will be called for every listed ref
307 * @param payload Additional data to pass to the callback
5dca2010 308 * @return 0 on success, GIT_EUSER on non-zero callback, or error code
09e8de0f 309 */
43521d06 310GIT_EXTERN(int) git_reference_foreach(git_repository *repo, unsigned int list_flags, int (*callback)(const char *, void *), void *payload);
09e8de0f 311
a46ec457 312/**
b90500f0 313 * Check if a reference has been loaded from a packfile.
a46ec457 314 *
d4a0b124 315 * @param ref A git reference
a46ec457
MS
316 * @return 0 in case it's not packed; 1 otherwise
317 */
318GIT_EXTERN(int) git_reference_is_packed(git_reference *ref);
319
d4a0b124 320/**
b90500f0 321 * Reload a reference from disk.
d4a0b124 322 *
b90500f0
RB
323 * Reference pointers can become outdated if the Git repository is
324 * accessed simultaneously by other clients while the library is open.
d4a0b124 325 *
b90500f0
RB
326 * This method forces a reload of the reference from disk, to ensure that
327 * the provided information is still reliable.
d4a0b124 328 *
b90500f0
RB
329 * If the reload fails (e.g. the reference no longer exists on disk, or
330 * has become corrupted), an error code will be returned and the reference
331 * pointer will be invalidated and freed.
d4a0b124
VM
332 *
333 * @param ref The reference to reload
e172cf08 334 * @return 0 on success, or an error code
d4a0b124
VM
335 */
336GIT_EXTERN(int) git_reference_reload(git_reference *ref);
337
a46ec457 338/**
b90500f0 339 * Free the given reference.
a46ec457
MS
340 *
341 * @param ref git_reference
342 */
343GIT_EXTERN(void) git_reference_free(git_reference *ref);
344
f201d613
RB
345/**
346 * Compare two references.
347 *
348 * @param ref1 The first git_reference
349 * @param ref2 The second git_reference
e172cf08 350 * @return 0 if the same, else a stable but meaningless ordering.
f201d613
RB
351 */
352GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2);
353
527ed554 354/**
b90500f0
RB
355 * Perform a callback on each reference in the repository whose name
356 * matches the given pattern.
527ed554 357 *
b90500f0
RB
358 * This function acts like `git_reference_foreach()` with an additional
359 * pattern match being applied to the reference name before issuing the
360 * callback function. See that function for more information.
e0db9f11 361 *
b90500f0
RB
362 * The pattern is matched using fnmatch or "glob" style where a '*' matches
363 * any sequence of letters, a '?' matches any letter, and square brackets
364 * can be used to define character ranges (such as "[0-9]" for digits).
527ed554 365 *
b90500f0
RB
366 * @param repo Repository where to find the refs
367 * @param glob Pattern to match (fnmatch-style) against reference name.
368 * @param list_flags Filtering flags for the reference listing.
369 * @param callback Function which will be called for every listed ref
370 * @param payload Additional data to pass to the callback
371 * @return 0 on success, GIT_EUSER on non-zero callback, or error code
527ed554 372 */
373GIT_EXTERN(int) git_reference_foreach_glob(
b90500f0
RB
374 git_repository *repo,
375 const char *glob,
376 unsigned int list_flags,
377 int (*callback)(const char *reference_name, void *payload),
378 void *payload);
527ed554 379
75261421 380/**
381 * Check if a reflog exists for the specified reference.
382 *
383 * @param ref A git reference
384 *
385 * @return 0 when no reflog can be found, 1 when it exists;
386 * otherwise an error code.
387 */
388GIT_EXTERN(int) git_reference_has_log(git_reference *ref);
389
88bcd515 390/**
391 * Check if a reference is a local branch.
392 *
393 * @param ref A git reference
394 *
395 * @return 1 when the reference lives in the refs/heads
396 * namespace; 0 otherwise.
397 */
398GIT_EXTERN(int) git_reference_is_branch(git_reference *ref);
399
1c947daa
VM
400/**
401 * Check if a reference is a remote tracking branch
402 *
403 * @param ref A git reference
404 *
405 * @return 1 when the reference lives in the refs/remotes
406 * namespace; 0 otherwise.
407 */
408GIT_EXTERN(int) git_reference_is_remote(git_reference *ref);
409
b90500f0
RB
410
411typedef enum {
2e0c8816 412 GIT_REF_FORMAT_NORMAL = 0,
413
414 /**
415 * Control whether one-level refnames are accepted
416 * (i.e., refnames that do not contain multiple /-separated
77e06d7e 417 * components). Those are expected to be written only using
418 * uppercase letters and underscore (FETCH_HEAD, ...)
2e0c8816 419 */
420 GIT_REF_FORMAT_ALLOW_ONELEVEL = (1 << 0),
421
422 /**
423 * Interpret the provided name as a reference pattern for a
424 * refspec (as used with remote repositories). If this option
425 * is enabled, the name is allowed to contain a single * (<star>)
426 * in place of a one full pathname component
427 * (e.g., foo/<star>/bar but not foo/bar<star>).
428 */
429 GIT_REF_FORMAT_REFSPEC_PATTERN = (1 << 1),
b90500f0 430} git_reference_normalize_t;
2e0c8816 431
432/**
b90500f0
RB
433 * Normalize reference name and check validity.
434 *
435 * This will normalize the reference name by removing any leading slash
436 * '/' characters and collapsing runs of adjacent slashes between name
437 * components into a single slash.
438 *
439 * Once normalized, if the reference name is valid, it will be returned in
440 * the user allocated buffer.
441 *
442 * @param buffer_out User allocated buffer to store normalized name
443 * @param buffer_size Size of buffer_out
444 * @param name Reference name to be checked.
445 * @param flags Flags to constrain name validation rules - see the
446 * GIT_REF_FORMAT constants above.
447 * @return 0 on success or error code (GIT_EBUFS if buffer is too small, -1
448 * if reference is invalid)
2e0c8816 449 */
450GIT_EXTERN(int) git_reference_normalize_name(
451 char *buffer_out,
452 size_t buffer_size,
453 const char *name,
454 unsigned int flags);
455
31665948 456/**
b90500f0 457 * Recursively peel reference until object of the specified type is found.
31665948 458 *
459 * The retrieved `peeled` object is owned by the repository
460 * and should be closed with the `git_object_free` method.
461 *
462 * If you pass `GIT_OBJ_ANY` as the target type, then the object
463 * will be peeled until a non-tag object is met.
464 *
465 * @param peeled Pointer to the peeled git_object
466 * @param ref The reference to be processed
467 * @param target_type The type of the requested object
468 * @return 0 or an error code
469 */
470GIT_EXTERN(int) git_reference_peel(
471 git_object **out,
472 git_reference *ref,
473 git_otype type);
474
77e06d7e 475/**
476 * Ensure the reference name is well-formed.
477 *
b90500f0
RB
478 * Valid reference names must follow one of two patterns:
479 *
480 * 1. Top-level names must contain only capital letters and underscores,
481 * and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD").
482 * 2. Names prefixed with "refs/" can be almost anything. You must avoid
483 * the characters '~', '^', ':', '\\', '?', '[', and '*', and the
484 * sequences ".." and "@{" which have special meaning to revparse.
77e06d7e 485 *
b90500f0 486 * @param refname name to be checked.
77e06d7e 487 * @return 1 if the reference name is acceptable; 0 if it isn't
488 */
b90500f0 489GIT_EXTERN(int) git_reference_is_valid_name(const char *refname);
77e06d7e 490
2f8a8ab2
VM
491/** @} */
492GIT_END_DECL
493#endif