]> git.proxmox.com Git - libgit2.git/blob - include/git2/refs.h
Merge pull request #277 from schu/sign-compare
[libgit2.git] / include / git2 / refs.h
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25 #ifndef INCLUDE_git_refs_h__
26 #define INCLUDE_git_refs_h__
27
28 #include "common.h"
29 #include "types.h"
30 #include "oid.h"
31
32 /**
33 * @file git2/refs.h
34 * @brief Git reference management routines
35 * @defgroup git_reference Git reference management routines
36 * @ingroup Git
37 * @{
38 */
39 GIT_BEGIN_DECL
40
41 /**
42 * Lookup a reference by its name in a repository.
43 *
44 * The generated reference is owned by the repository and
45 * should not be freed by the user.
46 *
47 * @param reference_out pointer to the looked-up reference
48 * @param repo the repository to look up the reference
49 * @param name the long name for the reference (e.g. HEAD, ref/heads/master, refs/tags/v0.1.0, ...)
50 * @return 0 on success; error code otherwise
51 */
52 GIT_EXTERN(int) git_reference_lookup(git_reference **reference_out, git_repository *repo, const char *name);
53
54 /**
55 * Create a new symbolic reference.
56 *
57 * The reference will be created in the repository and written
58 * to the disk.
59 *
60 * This reference is owned by the repository and shall not
61 * be free'd by the user.
62 *
63 * If `force` is true and there already exists a reference
64 * with the same name, it will be overwritten.
65 *
66 * @param ref_out Pointer to the newly created reference
67 * @param repo Repository where that reference will live
68 * @param name The name of the reference
69 * @param target The target of the reference
70 * @param force Overwrite existing references
71 * @return 0 on success; error code otherwise
72 */
73 GIT_EXTERN(int) git_reference_create_symbolic(git_reference **ref_out, git_repository *repo, const char *name, const char *target, int force);
74
75 /**
76 * Create a new object id reference.
77 *
78 * The reference will be created in the repository and written
79 * to the disk.
80 *
81 * This reference is owned by the repository and shall not
82 * be free'd by the user.
83 *
84 * If `force` is true and there already exists a reference
85 * with the same name, it will be overwritten.
86 *
87 * @param ref_out Pointer to the newly created reference
88 * @param repo Repository where that reference will live
89 * @param name The name of the reference
90 * @param id The object id pointed to by the reference.
91 * @param force Overwrite existing references
92 * @return 0 on success; error code otherwise
93 */
94 GIT_EXTERN(int) git_reference_create_oid(git_reference **ref_out, git_repository *repo, const char *name, const git_oid *id, int force);
95
96 /**
97 * Get the OID pointed to by a reference.
98 *
99 * Only available if the reference is direct (i.e. not symbolic)
100 *
101 * @param ref The reference
102 * @return a pointer to the oid if available, NULL otherwise
103 */
104 GIT_EXTERN(const git_oid *) git_reference_oid(git_reference *ref);
105
106 /**
107 * Get full name to the reference pointed by this reference
108 *
109 * Only available if the reference is symbolic
110 *
111 * @param ref The reference
112 * @return a pointer to the name if available, NULL otherwise
113 */
114 GIT_EXTERN(const char *) git_reference_target(git_reference *ref);
115
116 /**
117 * Get the type of a reference
118 *
119 * Either direct (GIT_REF_OID) or symbolic (GIT_REF_SYMBOLIC)
120 *
121 * @param ref The reference
122 * @return the type
123 */
124 GIT_EXTERN(git_rtype) git_reference_type(git_reference *ref);
125
126 /**
127 * Get the full name of a reference
128 *
129 * @param ref The reference
130 * @return the full name for the ref
131 */
132 GIT_EXTERN(const char *) git_reference_name(git_reference *ref);
133
134 /**
135 * Resolve a symbolic reference
136 *
137 * Thie method iteratively peels a symbolic reference
138 * until it resolves to a direct reference to an OID.
139 *
140 * If a direct reference is passed as an argument,
141 * that reference is returned immediately
142 *
143 * @param resolved_ref Pointer to the peeled reference
144 * @param ref The reference
145 * @return 0 on success; error code otherwise
146 */
147 GIT_EXTERN(int) git_reference_resolve(git_reference **resolved_ref, git_reference *ref);
148
149 /**
150 * Get the repository where a reference resides
151 *
152 * @param ref The reference
153 * @return a pointer to the repo
154 */
155 GIT_EXTERN(git_repository *) git_reference_owner(git_reference *ref);
156
157 /**
158 * Set the symbolic target of a reference.
159 *
160 * The reference must be a symbolic reference, otherwise
161 * this method will fail.
162 *
163 * The reference will be automatically updated in
164 * memory and on disk.
165 *
166 * @param ref The reference
167 * @param target The new target for the reference
168 * @return 0 on success; error code otherwise
169 */
170 GIT_EXTERN(int) git_reference_set_target(git_reference *ref, const char *target);
171
172 /**
173 * Set the OID target of a reference.
174 *
175 * The reference must be a direct reference, otherwise
176 * this method will fail.
177 *
178 * The reference will be automatically updated in
179 * memory and on disk.
180 *
181 * @param ref The reference
182 * @param id The new target OID for the reference
183 * @return 0 on success; error code otherwise
184 */
185 GIT_EXTERN(int) git_reference_set_oid(git_reference *ref, const git_oid *id);
186
187 /**
188 * Rename an existing reference
189 *
190 * This method works for both direct and symbolic references.
191 * The new name will be checked for validity and may be
192 * modified into a normalized form.
193 *
194 * The refernece will be immediately renamed in-memory
195 * and on disk.
196 *
197 */
198 GIT_EXTERN(int) git_reference_rename(git_reference *ref, const char *new_name);
199
200 /**
201 * Rename an existing reference, overwriting an existing one with the
202 * same name, if it exists.
203 *
204 * This method works for both direct and symbolic references.
205 * The new name will be checked for validity and may be
206 * modified into a normalized form.
207 *
208 * The refernece will be immediately renamed in-memory
209 * and on disk.
210 *
211 */
212 GIT_EXTERN(int) git_reference_rename_f(git_reference *ref, const char *new_name);
213
214 /**
215 * Delete an existing reference
216 *
217 * This method works for both direct and symbolic references.
218 *
219 * The reference will be immediately removed on disk and from
220 * memory. The given reference pointer will no longer be valid.
221 *
222 */
223 GIT_EXTERN(int) git_reference_delete(git_reference *ref);
224
225 /**
226 * Pack all the loose references in the repository
227 *
228 * This method will load into the cache all the loose
229 * references on the repository and update the
230 * `packed-refs` file with them.
231 *
232 * Once the `packed-refs` file has been written properly,
233 * the loose references will be removed from disk.
234 *
235 * WARNING: calling this method may invalidate any existing
236 * references previously loaded on the cache.
237 *
238 * @param repo Repository where the loose refs will be packed
239 * @return 0 on success; error code otherwise
240 */
241 GIT_EXTERN(int) git_reference_packall(git_repository *repo);
242
243 /**
244 * Fill a list with all the references that can be found
245 * in a repository.
246 *
247 * The listed references may be filtered by type, or using
248 * a bitwise OR of several types. Use the magic value
249 * `GIT_REF_LISTALL` to obtain all references, including
250 * packed ones.
251 *
252 * The string array will be filled with the names of all
253 * references; these values are owned by the user and
254 * should be free'd manually when no longer needed, using
255 * `git_strarray_free`.
256 *
257 * @param array Pointer to a git_strarray structure where
258 * the reference names will be stored
259 * @param repo Repository where to find the refs
260 * @param list_flags Filtering flags for the reference
261 * listing.
262 * @return 0 on success; error code otherwise
263 */
264 GIT_EXTERN(int) git_reference_listall(git_strarray *array, git_repository *repo, unsigned int list_flags);
265
266
267 /**
268 * Perform an operation on each reference in the repository
269 *
270 * The processed references may be filtered by type, or using
271 * a bitwise OR of several types. Use the magic value
272 * `GIT_REF_LISTALL` to obtain all references, including
273 * packed ones.
274 *
275 * The `callback` function will be called for each of the references
276 * in the repository, and will receive the name of the reference and
277 * the `payload` value passed to this method.
278 *
279 * @param repo Repository where to find the refs
280 * @param list_flags Filtering flags for the reference
281 * listing.
282 * @param callback Function which will be called for every listed ref
283 * @param payload Additional data to pass to the callback
284 * @return 0 on success; error code otherwise
285 */
286 GIT_EXTERN(int) git_reference_foreach(git_repository *repo, unsigned int list_flags, int (*callback)(const char *, void *), void *payload);
287
288 /** @} */
289 GIT_END_DECL
290 #endif