]>
Commit | Line | Data |
---|---|---|
f5918330 | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
f5918330 | 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. | |
f5918330 | 6 | */ |
3315782c VM |
7 | #ifndef INCLUDE_git_repository_h__ |
8 | #define INCLUDE_git_repository_h__ | |
9 | ||
10 | #include "common.h" | |
d12299fe VM |
11 | #include "types.h" |
12 | #include "oid.h" | |
7a3bd1e7 | 13 | #include "buffer.h" |
3315782c VM |
14 | |
15 | /** | |
f5918330 | 16 | * @file git2/repository.h |
d12299fe VM |
17 | * @brief Git repository management routines |
18 | * @defgroup git_repository Git repository management routines | |
3315782c VM |
19 | * @ingroup Git |
20 | * @{ | |
21 | */ | |
22 | GIT_BEGIN_DECL | |
23 | ||
24 | /** | |
6fd195d7 | 25 | * Open a git repository. |
3315782c | 26 | * |
9462c471 VM |
27 | * The 'path' argument must point to either a git repository |
28 | * folder, or an existing work dir. | |
3315782c | 29 | * |
9462c471 VM |
30 | * The method will automatically detect if 'path' is a normal |
31 | * or bare repository or fail is 'path' is neither. | |
6fd195d7 | 32 | * |
c9fc4a6f | 33 | * @param out pointer to the repo which will be opened |
6fd195d7 | 34 | * @param path the path to the repository |
e172cf08 | 35 | * @return 0 or an error code |
3315782c | 36 | */ |
c9fc4a6f | 37 | GIT_EXTERN(int) git_repository_open(git_repository **out, const char *path); |
8c8d726e PS |
38 | /** |
39 | * Open working tree as a repository | |
40 | * | |
41 | * Open the working directory of the working tree as a normal | |
42 | * repository that can then be worked on. | |
43 | * | |
44 | * @param out Output pointer containing opened repository | |
45 | * @param wt Working tree to open | |
46 | * @return 0 or an error code | |
47 | */ | |
48 | GIT_EXTERN(int) git_repository_open_from_worktree(git_repository **out, git_worktree *wt); | |
3315782c | 49 | |
6782245e CMN |
50 | /** |
51 | * Create a "fake" repository to wrap an object database | |
52 | * | |
53 | * Create a repository object to wrap an object database to be used | |
54 | * with the API when all you have is an object database. This doesn't | |
55 | * have any paths associated with it, so use with care. | |
56 | * | |
c9fc4a6f | 57 | * @param out pointer to the repo |
6782245e CMN |
58 | * @param odb the object database to wrap |
59 | * @return 0 or an error code | |
60 | */ | |
c9fc4a6f | 61 | GIT_EXTERN(int) git_repository_wrap_odb(git_repository **out, git_odb *odb); |
6782245e | 62 | |
fd0574e5 | 63 | /** |
9462c471 VM |
64 | * Look for a git repository and copy its path in the given buffer. |
65 | * The lookup start from base_path and walk across parent directories | |
66 | * if nothing has been found. The lookup ends when the first repository | |
67 | * is found, or when reaching a directory referenced in ceiling_dirs | |
68 | * or when the filesystem changes (in case across_fs is true). | |
fd0574e5 | 69 | * |
9462c471 VM |
70 | * The method will automatically detect if the repository is bare |
71 | * (if there is a repository). | |
fd0574e5 | 72 | * |
7a3bd1e7 CMN |
73 | * @param out A pointer to a user-allocated git_buf which will contain |
74 | * the found path. | |
fd0574e5 RG |
75 | * |
76 | * @param start_path The base path where the lookup starts. | |
77 | * | |
9462c471 VM |
78 | * @param across_fs If true, then the lookup will not stop when a |
79 | * filesystem device change is detected while exploring parent directories. | |
fd0574e5 | 80 | * |
9462c471 VM |
81 | * @param ceiling_dirs A GIT_PATH_LIST_SEPARATOR separated list of |
82 | * absolute symbolic link free paths. The lookup will stop when any | |
83 | * of this paths is reached. Note that the lookup always performs on | |
84 | * start_path no matter start_path appears in ceiling_dirs ceiling_dirs | |
85 | * might be NULL (which is equivalent to an empty string) | |
fd0574e5 | 86 | * |
e172cf08 | 87 | * @return 0 or an error code |
fd0574e5 | 88 | */ |
9462c471 | 89 | GIT_EXTERN(int) git_repository_discover( |
7a3bd1e7 | 90 | git_buf *out, |
9462c471 VM |
91 | const char *start_path, |
92 | int across_fs, | |
93 | const char *ceiling_dirs); | |
6fd195d7 | 94 | |
662880ca RB |
95 | /** |
96 | * Option flags for `git_repository_open_ext`. | |
97 | * | |
98 | * * GIT_REPOSITORY_OPEN_NO_SEARCH - Only open the repository if it can be | |
99 | * immediately found in the start_path. Do not walk up from the | |
100 | * start_path looking at parent directories. | |
ca1b6e54 RB |
101 | * * GIT_REPOSITORY_OPEN_CROSS_FS - Unless this flag is set, open will not |
102 | * continue searching across filesystem boundaries (i.e. when `st_dev` | |
103 | * changes from the `stat` system call). (E.g. Searching in a user's home | |
104 | * directory "/home/user/source/" will not return "/.git/" as the found | |
105 | * repo if "/" is a different filesystem than "/home".) | |
3fe046cf RB |
106 | * * GIT_REPOSITORY_OPEN_BARE - Open repository as a bare repo regardless |
107 | * of core.bare config, and defer loading config file for faster setup. | |
108 | * Unlike `git_repository_open_bare`, this can follow gitlinks. | |
39c6fca3 JT |
109 | * * GIT_REPOSITORY_OPEN_NO_DOTGIT - Do not check for a repository by |
110 | * appending /.git to the start_path; only open the repository if | |
111 | * start_path itself points to the git directory. | |
0dd98b69 JT |
112 | * * GIT_REPOSITORY_OPEN_FROM_ENV - Find and open a git repository, |
113 | * respecting the environment variables used by the git command-line | |
114 | * tools. If set, `git_repository_open_ext` will ignore the other | |
115 | * flags and the `ceiling_dirs` argument, and will allow a NULL `path` | |
116 | * to use `GIT_DIR` or search from the current directory. The search | |
117 | * for a repository will respect $GIT_CEILING_DIRECTORIES and | |
118 | * $GIT_DISCOVERY_ACROSS_FILESYSTEM. The opened repository will | |
119 | * respect $GIT_INDEX_FILE, $GIT_NAMESPACE, $GIT_OBJECT_DIRECTORY, and | |
120 | * $GIT_ALTERNATE_OBJECT_DIRECTORIES. In the future, this flag will | |
121 | * also cause `git_repository_open_ext` to respect $GIT_WORK_TREE and | |
122 | * $GIT_COMMON_DIR; currently, `git_repository_open_ext` with this | |
123 | * flag will error out if either $GIT_WORK_TREE or $GIT_COMMON_DIR is | |
124 | * set. | |
662880ca | 125 | */ |
c9fc4a6f | 126 | typedef enum { |
7784bcbb RB |
127 | GIT_REPOSITORY_OPEN_NO_SEARCH = (1 << 0), |
128 | GIT_REPOSITORY_OPEN_CROSS_FS = (1 << 1), | |
3fe046cf | 129 | GIT_REPOSITORY_OPEN_BARE = (1 << 2), |
39c6fca3 | 130 | GIT_REPOSITORY_OPEN_NO_DOTGIT = (1 << 3), |
0dd98b69 | 131 | GIT_REPOSITORY_OPEN_FROM_ENV = (1 << 4), |
f4a62c30 | 132 | } git_repository_open_flag_t; |
7784bcbb RB |
133 | |
134 | /** | |
135 | * Find and open a repository with extended controls. | |
662880ca | 136 | * |
c9fc4a6f | 137 | * @param out Pointer to the repo which will be opened. This can |
662880ca RB |
138 | * actually be NULL if you only want to use the error code to |
139 | * see if a repo at this path could be opened. | |
c9fc4a6f | 140 | * @param path Path to open as git repository. If the flags |
662880ca | 141 | * permit "searching", then this can be a path to a subdirectory |
0dd98b69 JT |
142 | * inside the working directory of the repository. May be NULL if |
143 | * flags is GIT_REPOSITORY_OPEN_FROM_ENV. | |
662880ca RB |
144 | * @param flags A combination of the GIT_REPOSITORY_OPEN flags above. |
145 | * @param ceiling_dirs A GIT_PATH_LIST_SEPARATOR delimited list of path | |
146 | * prefixes at which the search for a containing repository should | |
147 | * terminate. | |
148 | * @return 0 on success, GIT_ENOTFOUND if no repository could be found, | |
149 | * or -1 if there was a repository but open failed for some reason | |
150 | * (such as repo corruption or system errors). | |
7784bcbb RB |
151 | */ |
152 | GIT_EXTERN(int) git_repository_open_ext( | |
c9fc4a6f BS |
153 | git_repository **out, |
154 | const char *path, | |
155 | unsigned int flags, | |
7784bcbb RB |
156 | const char *ceiling_dirs); |
157 | ||
a442ed68 VM |
158 | /** |
159 | * Open a bare repository on the serverside. | |
160 | * | |
161 | * This is a fast open for bare repositories that will come in handy | |
162 | * if you're e.g. hosting git repositories and need to access them | |
163 | * efficiently | |
164 | * | |
165 | * @param out Pointer to the repo which will be opened. | |
166 | * @param bare_path Direct path to the bare repository | |
167 | * @return 0 on success, or an error code | |
168 | */ | |
437d3666 | 169 | GIT_EXTERN(int) git_repository_open_bare(git_repository **out, const char *bare_path); |
a442ed68 | 170 | |
3315782c VM |
171 | /** |
172 | * Free a previously allocated repository | |
6b2a1941 | 173 | * |
f0d08b7c VM |
174 | * Note that after a repository is free'd, all the objects it has spawned |
175 | * will still exist until they are manually closed by the user | |
45e79e37 | 176 | * with `git_object_free`, but accessing any of the attributes of |
f0d08b7c VM |
177 | * an object without a backing repository will result in undefined |
178 | * behavior | |
179 | * | |
3315782c VM |
180 | * @param repo repository handle to close. If NULL nothing occurs. |
181 | */ | |
182 | GIT_EXTERN(void) git_repository_free(git_repository *repo); | |
183 | ||
e1f8cad0 | 184 | /** |
40c44d2f | 185 | * Creates a new Git repository in the given folder. |
e1f8cad0 | 186 | * |
40c44d2f VM |
187 | * TODO: |
188 | * - Reinit the repository | |
e1f8cad0 | 189 | * |
c9fc4a6f | 190 | * @param out pointer to the repo which will be created or reinitialized |
e1f8cad0 | 191 | * @param path the path to the repository |
662880ca RB |
192 | * @param is_bare if true, a Git repository without a working directory is |
193 | * created at the pointed path. If false, provided path will be | |
194 | * considered as the working directory into which the .git directory | |
195 | * will be created. | |
40c44d2f | 196 | * |
e172cf08 | 197 | * @return 0 or an error code |
e1f8cad0 | 198 | */ |
662880ca | 199 | GIT_EXTERN(int) git_repository_init( |
c9fc4a6f | 200 | git_repository **out, |
662880ca RB |
201 | const char *path, |
202 | unsigned is_bare); | |
203 | ||
204 | /** | |
205 | * Option flags for `git_repository_init_ext`. | |
206 | * | |
207 | * These flags configure extra behaviors to `git_repository_init_ext`. | |
208 | * In every case, the default behavior is the zero value (i.e. flag is | |
209 | * not set). Just OR the flag values together for the `flags` parameter | |
210 | * when initializing a new repo. Details of individual values are: | |
211 | * | |
212 | * * BARE - Create a bare repository with no working directory. | |
0b170f4d | 213 | * * NO_REINIT - Return an GIT_EEXISTS error if the repo_path appears to |
662880ca RB |
214 | * already be an git repository. |
215 | * * NO_DOTGIT_DIR - Normally a "/.git/" will be appended to the repo | |
216 | * path for non-bare repos (if it is not already there), but | |
217 | * passing this flag prevents that behavior. | |
218 | * * MKDIR - Make the repo_path (and workdir_path) as needed. Init is | |
219 | * always willing to create the ".git" directory even without this | |
220 | * flag. This flag tells init to create the trailing component of | |
221 | * the repo and workdir paths as needed. | |
222 | * * MKPATH - Recursively make all components of the repo and workdir | |
223 | * paths as necessary. | |
224 | * * EXTERNAL_TEMPLATE - libgit2 normally uses internal templates to | |
225 | * initialize a new repo. This flags enables external templates, | |
226 | * looking the "template_path" from the options if set, or the | |
227 | * `init.templatedir` global config if not, or falling back on | |
228 | * "/usr/share/git-core/templates" if it exists. | |
bc737620 JM |
229 | * * GIT_REPOSITORY_INIT_RELATIVE_GITLINK - If an alternate workdir is |
230 | * specified, use relative paths for the gitdir and core.worktree. | |
662880ca | 231 | */ |
f4a62c30 | 232 | typedef enum { |
662880ca RB |
233 | GIT_REPOSITORY_INIT_BARE = (1u << 0), |
234 | GIT_REPOSITORY_INIT_NO_REINIT = (1u << 1), | |
235 | GIT_REPOSITORY_INIT_NO_DOTGIT_DIR = (1u << 2), | |
236 | GIT_REPOSITORY_INIT_MKDIR = (1u << 3), | |
237 | GIT_REPOSITORY_INIT_MKPATH = (1u << 4), | |
238 | GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE = (1u << 5), | |
bc737620 | 239 | GIT_REPOSITORY_INIT_RELATIVE_GITLINK = (1u << 6), |
f4a62c30 | 240 | } git_repository_init_flag_t; |
ca1b6e54 RB |
241 | |
242 | /** | |
243 | * Mode options for `git_repository_init_ext`. | |
244 | * | |
245 | * Set the mode field of the `git_repository_init_options` structure | |
246 | * either to the custom mode that you would like, or to one of the | |
247 | * following modes: | |
248 | * | |
249 | * * SHARED_UMASK - Use permissions configured by umask - the default. | |
250 | * * SHARED_GROUP - Use "--shared=group" behavior, chmod'ing the new repo | |
251 | * to be group writable and "g+sx" for sticky group assignment. | |
252 | * * SHARED_ALL - Use "--shared=all" behavior, adding world readability. | |
253 | * * Anything else - Set to custom value. | |
254 | */ | |
f4a62c30 | 255 | typedef enum { |
ca1b6e54 RB |
256 | GIT_REPOSITORY_INIT_SHARED_UMASK = 0, |
257 | GIT_REPOSITORY_INIT_SHARED_GROUP = 0002775, | |
258 | GIT_REPOSITORY_INIT_SHARED_ALL = 0002777, | |
f4a62c30 | 259 | } git_repository_init_mode_t; |
662880ca RB |
260 | |
261 | /** | |
262 | * Extended options structure for `git_repository_init_ext`. | |
263 | * | |
264 | * This contains extra options for `git_repository_init_ext` that enable | |
265 | * additional initialization features. The fields are: | |
266 | * | |
267 | * * flags - Combination of GIT_REPOSITORY_INIT flags above. | |
ca1b6e54 RB |
268 | * * mode - Set to one of the standard GIT_REPOSITORY_INIT_SHARED_... |
269 | * constants above, or to a custom value that you would like. | |
662880ca | 270 | * * workdir_path - The path to the working dir or NULL for default (i.e. |
ca1b6e54 RB |
271 | * repo_path parent on non-bare repos). IF THIS IS RELATIVE PATH, |
272 | * IT WILL BE EVALUATED RELATIVE TO THE REPO_PATH. If this is not | |
273 | * the "natural" working directory, a .git gitlink file will be | |
274 | * created here linking to the repo_path. | |
662880ca RB |
275 | * * description - If set, this will be used to initialize the "description" |
276 | * file in the repository, instead of using the template content. | |
277 | * * template_path - When GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE is set, | |
278 | * this contains the path to use for the template directory. If | |
279 | * this is NULL, the config or default directory options will be | |
280 | * used instead. | |
281 | * * initial_head - The name of the head to point HEAD at. If NULL, then | |
282 | * this will be treated as "master" and the HEAD ref will be set | |
283 | * to "refs/heads/master". If this begins with "refs/" it will be | |
284 | * used verbatim; otherwise "refs/heads/" will be prefixed. | |
285 | * * origin_url - If this is non-NULL, then after the rest of the | |
286 | * repository initialization is completed, an "origin" remote | |
287 | * will be added pointing to this URL. | |
288 | */ | |
ca94e031 | 289 | typedef struct { |
bde336ea | 290 | unsigned int version; |
662880ca RB |
291 | uint32_t flags; |
292 | uint32_t mode; | |
293 | const char *workdir_path; | |
294 | const char *description; | |
295 | const char *template_path; | |
296 | const char *initial_head; | |
297 | const char *origin_url; | |
298 | } git_repository_init_options; | |
299 | ||
bde336ea | 300 | #define GIT_REPOSITORY_INIT_OPTIONS_VERSION 1 |
fac43c54 | 301 | #define GIT_REPOSITORY_INIT_OPTIONS_INIT {GIT_REPOSITORY_INIT_OPTIONS_VERSION} |
bde336ea | 302 | |
b9f81997 MB |
303 | /** |
304 | * Initializes a `git_repository_init_options` with default values. Equivalent | |
305 | * to creating an instance with GIT_REPOSITORY_INIT_OPTIONS_INIT. | |
306 | * | |
702efc89 RB |
307 | * @param opts the `git_repository_init_options` struct to initialize |
308 | * @param version Version of struct; pass `GIT_REPOSITORY_INIT_OPTIONS_VERSION` | |
b9f81997 MB |
309 | * @return Zero on success; -1 on failure. |
310 | */ | |
311 | GIT_EXTERN(int) git_repository_init_init_options( | |
702efc89 RB |
312 | git_repository_init_options *opts, |
313 | unsigned int version); | |
b9f81997 | 314 | |
662880ca RB |
315 | /** |
316 | * Create a new Git repository in the given folder with extended controls. | |
317 | * | |
318 | * This will initialize a new git repository (creating the repo_path | |
319 | * if requested by flags) and working directory as needed. It will | |
320 | * auto-detect the case sensitivity of the file system and if the | |
321 | * file system supports file mode bits correctly. | |
322 | * | |
c9fc4a6f | 323 | * @param out Pointer to the repo which will be created or reinitialized. |
662880ca RB |
324 | * @param repo_path The path to the repository. |
325 | * @param opts Pointer to git_repository_init_options struct. | |
326 | * @return 0 or an error code on failure. | |
327 | */ | |
328 | GIT_EXTERN(int) git_repository_init_ext( | |
c9fc4a6f | 329 | git_repository **out, |
662880ca RB |
330 | const char *repo_path, |
331 | git_repository_init_options *opts); | |
4b8e27c8 | 332 | |
3601c4bf | 333 | /** |
334 | * Retrieve and resolve the reference pointed at by HEAD. | |
335 | * | |
ca94e031 RB |
336 | * The returned `git_reference` will be owned by caller and |
337 | * `git_reference_free()` must be called when done with it to release the | |
338 | * allocated memory and prevent a leak. | |
339 | * | |
c9fc4a6f | 340 | * @param out pointer to the reference which will be retrieved |
3601c4bf | 341 | * @param repo a repository object |
342 | * | |
605da51a | 343 | * @return 0 on success, GIT_EUNBORNBRANCH when HEAD points to a non existing |
b1a3a70e | 344 | * branch, GIT_ENOTFOUND when HEAD is missing; an error code otherwise |
3601c4bf | 345 | */ |
c9fc4a6f | 346 | GIT_EXTERN(int) git_repository_head(git_reference **out, git_repository *repo); |
3601c4bf | 347 | |
04fb12ab PS |
348 | /** |
349 | * Retrieve the referenced HEAD for the worktree | |
350 | * | |
351 | * @param out pointer to the reference which will be retrieved | |
352 | * @param repo a repository object | |
353 | * @param name name of the worktree to retrieve HEAD for | |
354 | * @return 0 when successful, error-code otherwise | |
355 | */ | |
356 | GIT_EXTERN(int) git_repository_head_for_worktree(git_reference **out, git_repository *repo, | |
357 | const char *name); | |
358 | ||
35502d2e CMN |
359 | /** |
360 | * Check if a repository's HEAD is detached | |
361 | * | |
362 | * A repository's HEAD is detached when it points directly to a commit | |
363 | * instead of a branch. | |
364 | * | |
365 | * @param repo Repo to test | |
d73c94b2 | 366 | * @return 1 if HEAD is detached, 0 if it's not; error code if there |
35502d2e CMN |
367 | * was an error. |
368 | */ | |
408d733b | 369 | GIT_EXTERN(int) git_repository_head_detached(git_repository *repo); |
35502d2e | 370 | |
04fb12ab PS |
371 | /* |
372 | * Check if a worktree's HEAD is detached | |
373 | * | |
374 | * A worktree's HEAD is detached when it points directly to a | |
375 | * commit instead of a branch. | |
376 | * | |
377 | * @param repo a repository object | |
378 | * @param name name of the worktree to retrieve HEAD for | |
379 | * @return 1 if HEAD is detached, 0 if its not; error code if | |
380 | * there was an error | |
381 | */ | |
382 | GIT_EXTERN(int) git_repository_head_detached_for_worktree(git_repository *repo, | |
383 | const char *name); | |
384 | ||
35502d2e | 385 | /** |
605da51a | 386 | * Check if the current branch is unborn |
35502d2e | 387 | * |
605da51a | 388 | * An unborn branch is one named from HEAD but which doesn't exist in |
35502d2e CMN |
389 | * the refs namespace, because it doesn't have any commit to point to. |
390 | * | |
391 | * @param repo Repo to test | |
605da51a | 392 | * @return 1 if the current branch is unborn, 0 if it's not; error |
d73c94b2 | 393 | * code if there was an error |
35502d2e | 394 | */ |
605da51a | 395 | GIT_EXTERN(int) git_repository_head_unborn(git_repository *repo); |
35502d2e | 396 | |
41233c40 VM |
397 | /** |
398 | * Check if a repository is empty | |
399 | * | |
9a294fd8 POL |
400 | * An empty repository has just been initialized and contains no references |
401 | * apart from HEAD, which must be pointing to the unborn master branch. | |
41233c40 VM |
402 | * |
403 | * @param repo Repo to test | |
404 | * @return 1 if the repository is empty, 0 if it isn't, error code | |
405 | * if the repository is corrupted | |
406 | */ | |
407 | GIT_EXTERN(int) git_repository_is_empty(git_repository *repo); | |
408 | ||
cb3269c9 PS |
409 | /** |
410 | * List of items which belong to the git repository layout | |
411 | */ | |
412 | typedef enum { | |
413 | GIT_REPOSITORY_ITEM_GITDIR, | |
414 | GIT_REPOSITORY_ITEM_WORKDIR, | |
415 | GIT_REPOSITORY_ITEM_COMMONDIR, | |
416 | GIT_REPOSITORY_ITEM_INDEX, | |
417 | GIT_REPOSITORY_ITEM_OBJECTS, | |
418 | GIT_REPOSITORY_ITEM_REFS, | |
419 | GIT_REPOSITORY_ITEM_PACKED_REFS, | |
420 | GIT_REPOSITORY_ITEM_REMOTES, | |
421 | GIT_REPOSITORY_ITEM_CONFIG, | |
422 | GIT_REPOSITORY_ITEM_INFO, | |
423 | GIT_REPOSITORY_ITEM_HOOKS, | |
424 | GIT_REPOSITORY_ITEM_LOGS, | |
425 | GIT_REPOSITORY_ITEM_MODULES, | |
426 | GIT_REPOSITORY_ITEM_WORKTREES | |
427 | } git_repository_item_t; | |
428 | ||
429 | /** | |
430 | * Get the location of a specific repository file or directory | |
431 | * | |
432 | * This function will retrieve the path of a specific repository | |
433 | * item. It will thereby honor things like the repository's | |
434 | * common directory, gitdir, etc. In case a file path cannot | |
435 | * exist for a given item (e.g. the working directory of a bare | |
9d49a43c | 436 | * repository), GIT_ENOTFOUND is returned. |
cb3269c9 PS |
437 | * |
438 | * @param out Buffer to store the path at | |
439 | * @param repo Repository to get path for | |
440 | * @param item The repository item for which to retrieve the path | |
9d49a43c | 441 | * @return 0, GIT_ENOTFOUND if the path cannot exist or an error code |
cb3269c9 | 442 | */ |
eae0bfdc | 443 | GIT_EXTERN(int) git_repository_item_path(git_buf *out, const git_repository *repo, git_repository_item_t item); |
cb3269c9 | 444 | |
6632c155 VM |
445 | /** |
446 | * Get the path of this repository | |
447 | * | |
448 | * This is the path of the `.git` folder for normal repositories, | |
449 | * or of the repository itself for bare repositories. | |
450 | * | |
451 | * @param repo A repository object | |
452 | * @return the path to the repository | |
453 | */ | |
eae0bfdc | 454 | GIT_EXTERN(const char *) git_repository_path(const git_repository *repo); |
6632c155 VM |
455 | |
456 | /** | |
457 | * Get the path of the working directory for this repository | |
458 | * | |
459 | * If the repository is bare, this function will always return | |
460 | * NULL. | |
461 | * | |
462 | * @param repo A repository object | |
463 | * @return the path to the working dir, if it exists | |
464 | */ | |
eae0bfdc | 465 | GIT_EXTERN(const char *) git_repository_workdir(const git_repository *repo); |
6632c155 | 466 | |
c09fd54e PS |
467 | /** |
468 | * Get the path of the shared common directory for this repository | |
469 | * | |
470 | * If the repository is bare is not a worktree, the git directory | |
471 | * path is returned. | |
472 | * | |
473 | * @param repo A repository object | |
474 | * @return the path to the common dir | |
475 | */ | |
eae0bfdc | 476 | GIT_EXTERN(const char *) git_repository_commondir(const git_repository *repo); |
c09fd54e | 477 | |
6632c155 VM |
478 | /** |
479 | * Set the path to the working directory for this repository | |
480 | * | |
481 | * The working directory doesn't need to be the same one | |
482 | * that contains the `.git` folder for this repository. | |
483 | * | |
484 | * If this repository is bare, setting its working directory | |
485 | * will turn it into a normal repository, capable of performing | |
486 | * all the common workdir operations (checkout, status, index | |
487 | * manipulation, etc). | |
488 | * | |
489 | * @param repo A repository object | |
490 | * @param workdir The path to a working directory | |
991a56c7 RB |
491 | * @param update_gitlink Create/update gitlink in workdir and set config |
492 | * "core.worktree" (if workdir is not the parent of the .git directory) | |
e172cf08 | 493 | * @return 0, or an error code |
6632c155 | 494 | */ |
991a56c7 RB |
495 | GIT_EXTERN(int) git_repository_set_workdir( |
496 | git_repository *repo, const char *workdir, int update_gitlink); | |
4a34b3a9 | 497 | |
fa9bcd81 | 498 | /** |
499 | * Check if a repository is bare | |
500 | * | |
501 | * @param repo Repo to test | |
28ba94ce | 502 | * @return 1 if the repository is bare, 0 otherwise. |
fa9bcd81 | 503 | */ |
eae0bfdc | 504 | GIT_EXTERN(int) git_repository_is_bare(const git_repository *repo); |
fa9bcd81 | 505 | |
79ab3ef6 PS |
506 | /** |
507 | * Check if a repository is a linked work tree | |
508 | * | |
509 | * @param repo Repo to test | |
510 | * @return 1 if the repository is a linked work tree, 0 otherwise. | |
511 | */ | |
eae0bfdc | 512 | GIT_EXTERN(int) git_repository_is_worktree(const git_repository *repo); |
79ab3ef6 | 513 | |
6632c155 VM |
514 | /** |
515 | * Get the configuration file for this repository. | |
516 | * | |
517 | * If a configuration file has not been set, the default | |
518 | * config set for the repository will be returned, including | |
519 | * global and system configurations (if they are available). | |
520 | * | |
521 | * The configuration file must be freed once it's no longer | |
522 | * being used by the user. | |
523 | * | |
ac99d86b | 524 | * @param out Pointer to store the loaded configuration |
6632c155 | 525 | * @param repo A repository object |
e172cf08 | 526 | * @return 0, or an error code |
6632c155 | 527 | */ |
9462c471 | 528 | GIT_EXTERN(int) git_repository_config(git_config **out, git_repository *repo); |
6632c155 | 529 | |
ac99d86b CMN |
530 | /** |
531 | * Get a snapshot of the repository's configuration | |
532 | * | |
533 | * Convenience function to take a snapshot from the repository's | |
b1914c36 RB |
534 | * configuration. The contents of this snapshot will not change, |
535 | * even if the underlying config files are modified. | |
536 | * | |
537 | * The configuration file must be freed once it's no longer | |
538 | * being used by the user. | |
ac99d86b CMN |
539 | * |
540 | * @param out Pointer to store the loaded configuration | |
541 | * @param repo the repository | |
542 | * @return 0, or an error code | |
543 | */ | |
544 | GIT_EXTERN(int) git_repository_config_snapshot(git_config **out, git_repository *repo); | |
545 | ||
6632c155 VM |
546 | /** |
547 | * Get the Object Database for this repository. | |
548 | * | |
549 | * If a custom ODB has not been set, the default | |
550 | * database for the repository will be returned (the one | |
551 | * located in `.git/objects`). | |
552 | * | |
553 | * The ODB must be freed once it's no longer being used by | |
554 | * the user. | |
555 | * | |
556 | * @param out Pointer to store the loaded ODB | |
557 | * @param repo A repository object | |
e172cf08 | 558 | * @return 0, or an error code |
6632c155 | 559 | */ |
9462c471 | 560 | GIT_EXTERN(int) git_repository_odb(git_odb **out, git_repository *repo); |
6632c155 | 561 | |
d00d5464 ET |
562 | /** |
563 | * Get the Reference Database Backend for this repository. | |
564 | * | |
565 | * If a custom refsdb has not been set, the default database for | |
566 | * the repository will be returned (the one that manipulates loose | |
567 | * and packed references in the `.git` directory). | |
568 | * | |
569 | * The refdb must be freed once it's no longer being used by | |
570 | * the user. | |
571 | * | |
572 | * @param out Pointer to store the loaded refdb | |
573 | * @param repo A repository object | |
574 | * @return 0, or an error code | |
575 | */ | |
576 | GIT_EXTERN(int) git_repository_refdb(git_refdb **out, git_repository *repo); | |
577 | ||
6632c155 VM |
578 | /** |
579 | * Get the Index file for this repository. | |
580 | * | |
581 | * If a custom index has not been set, the default | |
582 | * index for the repository will be returned (the one | |
583 | * located in `.git/index`). | |
584 | * | |
585 | * The index must be freed once it's no longer being used by | |
586 | * the user. | |
587 | * | |
588 | * @param out Pointer to store the loaded index | |
589 | * @param repo A repository object | |
e172cf08 | 590 | * @return 0, or an error code |
6632c155 | 591 | */ |
9462c471 | 592 | GIT_EXTERN(int) git_repository_index(git_index **out, git_repository *repo); |
6632c155 | 593 | |
074841ec | 594 | /** |
cc548c7b | 595 | * Retrieve git's prepared message |
074841ec CMN |
596 | * |
597 | * Operations such as git revert/cherry-pick/merge with the -n option | |
598 | * stop just short of creating a commit with the changes and save | |
599 | * their prepared message in .git/MERGE_MSG so the next git-commit | |
600 | * execution can present it to the user for them to amend if they | |
601 | * wish. | |
602 | * | |
603 | * Use this function to get the contents of this file. Don't forget to | |
604 | * remove the file after you create the commit. | |
e9ca852e | 605 | * |
7a3bd1e7 | 606 | * @param out git_buf to write data into |
e9ca852e | 607 | * @param repo Repository to read prepared message from |
7a3bd1e7 | 608 | * @return 0, GIT_ENOTFOUND if no message exists or an error code |
074841ec | 609 | */ |
7a3bd1e7 | 610 | GIT_EXTERN(int) git_repository_message(git_buf *out, git_repository *repo); |
074841ec CMN |
611 | |
612 | /** | |
613 | * Remove git's prepared message. | |
614 | * | |
615 | * Remove the message that `git_repository_message` retrieves. | |
616 | */ | |
617 | GIT_EXTERN(int) git_repository_message_remove(git_repository *repo); | |
618 | ||
ad2bc32f | 619 | /** |
bab0b9f2 ET |
620 | * Remove all the metadata associated with an ongoing command like merge, |
621 | * revert, cherry-pick, etc. For example: MERGE_HEAD, MERGE_MSG, etc. | |
ad2bc32f ET |
622 | * |
623 | * @param repo A repository object | |
624 | * @return 0 on success, or error | |
625 | */ | |
bab0b9f2 | 626 | GIT_EXTERN(int) git_repository_state_cleanup(git_repository *repo); |
ad2bc32f | 627 | |
7fcec834 ET |
628 | typedef int (*git_repository_fetchhead_foreach_cb)(const char *ref_name, |
629 | const char *remote_url, | |
630 | const git_oid *oid, | |
631 | unsigned int is_merge, | |
632 | void *payload); | |
633 | ||
634 | /** | |
373cf6a9 RB |
635 | * Invoke 'callback' for each entry in the given FETCH_HEAD file. |
636 | * | |
637 | * Return a non-zero value from the callback to stop the loop. | |
7fcec834 ET |
638 | * |
639 | * @param repo A repository object | |
640 | * @param callback Callback function | |
641 | * @param payload Pointer to callback data (optional) | |
373cf6a9 RB |
642 | * @return 0 on success, non-zero callback return value, GIT_ENOTFOUND if |
643 | * there is no FETCH_HEAD file, or other error code. | |
7fcec834 | 644 | */ |
373cf6a9 RB |
645 | GIT_EXTERN(int) git_repository_fetchhead_foreach( |
646 | git_repository *repo, | |
7fcec834 ET |
647 | git_repository_fetchhead_foreach_cb callback, |
648 | void *payload); | |
649 | ||
42e50b5e ET |
650 | typedef int (*git_repository_mergehead_foreach_cb)(const git_oid *oid, |
651 | void *payload); | |
652 | ||
653 | /** | |
373cf6a9 | 654 | * If a merge is in progress, invoke 'callback' for each commit ID in the |
42e50b5e ET |
655 | * MERGE_HEAD file. |
656 | * | |
373cf6a9 RB |
657 | * Return a non-zero value from the callback to stop the loop. |
658 | * | |
42e50b5e ET |
659 | * @param repo A repository object |
660 | * @param callback Callback function | |
e1967164 | 661 | * @param payload Pointer to callback data (optional) |
373cf6a9 RB |
662 | * @return 0 on success, non-zero callback return value, GIT_ENOTFOUND if |
663 | * there is no MERGE_HEAD file, or other error code. | |
42e50b5e | 664 | */ |
373cf6a9 RB |
665 | GIT_EXTERN(int) git_repository_mergehead_foreach( |
666 | git_repository *repo, | |
42e50b5e ET |
667 | git_repository_mergehead_foreach_cb callback, |
668 | void *payload); | |
669 | ||
47bfa0be RB |
670 | /** |
671 | * Calculate hash of file using repository filtering rules. | |
672 | * | |
673 | * If you simply want to calculate the hash of a file on disk with no filters, | |
674 | * you can just use the `git_odb_hashfile()` API. However, if you want to | |
675 | * hash a file in the repository and you want to apply filtering rules (e.g. | |
676 | * crlf filters) before generating the SHA, then use this function. | |
677 | * | |
5269008c RB |
678 | * Note: if the repository has `core.safecrlf` set to fail and the |
679 | * filtering triggers that failure, then this function will return an | |
680 | * error and not calculate the hash of the file. | |
681 | * | |
47bfa0be | 682 | * @param out Output value of calculated SHA |
a13fb55a | 683 | * @param repo Repository pointer |
47bfa0be RB |
684 | * @param path Path to file on disk whose contents should be hashed. If the |
685 | * repository is not NULL, this can be a relative path. | |
686 | * @param type The object type to hash as (e.g. GIT_OBJ_BLOB) | |
687 | * @param as_path The path to use to look up filtering rules. If this is | |
688 | * NULL, then the `path` parameter will be used instead. If | |
689 | * this is passed as the empty string, then no filters will be | |
690 | * applied when calculating the hash. | |
5269008c | 691 | * @return 0 on success, or an error code |
47bfa0be RB |
692 | */ |
693 | GIT_EXTERN(int) git_repository_hashfile( | |
0cb16fe9 L |
694 | git_oid *out, |
695 | git_repository *repo, | |
696 | const char *path, | |
697 | git_otype type, | |
698 | const char *as_path); | |
074841ec | 699 | |
44af67a8 | 700 | /** |
701 | * Make the repository HEAD point to the specified reference. | |
702 | * | |
703 | * If the provided reference points to a Tree or a Blob, the HEAD is | |
704 | * unaltered and -1 is returned. | |
705 | * | |
706 | * If the provided reference points to a branch, the HEAD will point | |
707 | * to that branch, staying attached, or become attached if it isn't yet. | |
708 | * If the branch doesn't exist yet, no error will be return. The HEAD | |
709 | * will then be attached to an unborn branch. | |
710 | * | |
711 | * Otherwise, the HEAD will be detached and will directly point to | |
712 | * the Commit. | |
713 | * | |
714 | * @param repo Repository pointer | |
715 | * @param refname Canonical name of the reference the HEAD should point at | |
716 | * @return 0 on success, or an error code | |
717 | */ | |
718 | GIT_EXTERN(int) git_repository_set_head( | |
719 | git_repository* repo, | |
4e498646 | 720 | const char* refname); |
44af67a8 | 721 | |
4ebe38bd | 722 | /** |
723 | * Make the repository HEAD directly point to the Commit. | |
724 | * | |
725 | * If the provided committish cannot be found in the repository, the HEAD | |
726 | * is unaltered and GIT_ENOTFOUND is returned. | |
727 | * | |
728 | * If the provided commitish cannot be peeled into a commit, the HEAD | |
729 | * is unaltered and -1 is returned. | |
730 | * | |
731 | * Otherwise, the HEAD will eventually be detached and will directly point to | |
732 | * the peeled Commit. | |
733 | * | |
734 | * @param repo Repository pointer | |
735 | * @param commitish Object id of the Commit the HEAD should point to | |
736 | * @return 0 on success, or an error code | |
737 | */ | |
738 | GIT_EXTERN(int) git_repository_set_head_detached( | |
739 | git_repository* repo, | |
4e498646 | 740 | const git_oid* commitish); |
4ebe38bd | 741 | |
62d38a1d CMN |
742 | /** |
743 | * Make the repository HEAD directly point to the Commit. | |
744 | * | |
745 | * This behaves like `git_repository_set_head_detached()` but takes an | |
746 | * annotated commit, which lets you specify which extended sha syntax | |
747 | * string was specified by a user, allowing for more exact reflog | |
748 | * messages. | |
749 | * | |
750 | * See the documentation for `git_repository_set_head_detached()`. | |
751 | * | |
752 | * @see git_repository_set_head_detached | |
753 | */ | |
754 | GIT_EXTERN(int) git_repository_set_head_detached_from_annotated( | |
755 | git_repository *repo, | |
756 | const git_annotated_commit *commitish); | |
757 | ||
3f4c3072 | 758 | /** |
759 | * Detach the HEAD. | |
760 | * | |
761 | * If the HEAD is already detached and points to a Commit, 0 is returned. | |
762 | * | |
763 | * If the HEAD is already detached and points to a Tag, the HEAD is | |
764 | * updated into making it point to the peeled Commit, and 0 is returned. | |
765 | * | |
0b170f4d | 766 | * If the HEAD is already detached and points to a non commitish, the HEAD is |
7eb222fc | 767 | * unaltered, and -1 is returned. |
3f4c3072 | 768 | * |
769 | * Otherwise, the HEAD will be detached and point to the peeled Commit. | |
770 | * | |
771 | * @param repo Repository pointer | |
605da51a | 772 | * @return 0 on success, GIT_EUNBORNBRANCH when HEAD points to a non existing |
875b16eb | 773 | * branch or an error code |
3f4c3072 | 774 | */ |
775 | GIT_EXTERN(int) git_repository_detach_head( | |
4e498646 | 776 | git_repository* repo); |
3f4c3072 | 777 | |
a295bd2d CMN |
778 | /** |
779 | * Repository state | |
780 | * | |
781 | * These values represent possible states for the repository to be in, | |
782 | * based on the current operation which is ongoing. | |
783 | */ | |
632d8b23 ET |
784 | typedef enum { |
785 | GIT_REPOSITORY_STATE_NONE, | |
786 | GIT_REPOSITORY_STATE_MERGE, | |
787 | GIT_REPOSITORY_STATE_REVERT, | |
2ea40fda | 788 | GIT_REPOSITORY_STATE_REVERT_SEQUENCE, |
0ba4dca5 | 789 | GIT_REPOSITORY_STATE_CHERRYPICK, |
2ea40fda | 790 | GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE, |
31966d20 | 791 | GIT_REPOSITORY_STATE_BISECT, |
792 | GIT_REPOSITORY_STATE_REBASE, | |
793 | GIT_REPOSITORY_STATE_REBASE_INTERACTIVE, | |
794 | GIT_REPOSITORY_STATE_REBASE_MERGE, | |
795 | GIT_REPOSITORY_STATE_APPLY_MAILBOX, | |
796 | GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE, | |
632d8b23 ET |
797 | } git_repository_state_t; |
798 | ||
799 | /** | |
800 | * Determines the status of a git repository - ie, whether an operation | |
801 | * (merge, cherry-pick, etc) is in progress. | |
802 | * | |
803 | * @param repo Repository pointer | |
804 | * @return The state of the repository | |
805 | */ | |
806 | GIT_EXTERN(int) git_repository_state(git_repository *repo); | |
807 | ||
bade5194 VM |
808 | /** |
809 | * Sets the active namespace for this Git Repository | |
810 | * | |
811 | * This namespace affects all reference operations for the repo. | |
812 | * See `man gitnamespaces` | |
813 | * | |
814 | * @param repo The repo | |
815 | * @param nmspace The namespace. This should not include the refs | |
816 | * folder, e.g. to namespace all references under `refs/namespaces/foo/`, | |
817 | * use `foo` as the namespace. | |
818 | * @return 0 on success, -1 on error | |
819 | */ | |
820 | GIT_EXTERN(int) git_repository_set_namespace(git_repository *repo, const char *nmspace); | |
821 | ||
822 | /** | |
823 | * Get the currently active namespace for this repository | |
824 | * | |
825 | * @param repo The repo | |
826 | * @return the active namespace, or NULL if there isn't one | |
827 | */ | |
828 | GIT_EXTERN(const char *) git_repository_get_namespace(git_repository *repo); | |
829 | ||
93d8f77f BS |
830 | |
831 | /** | |
832 | * Determine if the repository was a shallow clone | |
833 | * | |
834 | * @param repo The repository | |
835 | * @return 1 if shallow, zero if not | |
836 | */ | |
837 | GIT_EXTERN(int) git_repository_is_shallow(git_repository *repo); | |
838 | ||
659cf202 CMN |
839 | /** |
840 | * Retrieve the configured identity to use for reflogs | |
841 | * | |
842 | * The memory is owned by the repository and must not be freed by the | |
843 | * user. | |
844 | * | |
845 | * @param name where to store the pointer to the name | |
846 | * @param email where to store the pointer to the email | |
847 | * @param repo the repository | |
848 | */ | |
849 | GIT_EXTERN(int) git_repository_ident(const char **name, const char **email, const git_repository *repo); | |
850 | ||
851 | /** | |
852 | * Set the identity to be used for writing reflogs | |
853 | * | |
854 | * If both are set, this name and email will be used to write to the | |
855 | * reflog. Pass NULL to unset. When unset, the identity will be taken | |
856 | * from the repository's configuration. | |
857 | * | |
858 | * @param repo the repository to configure | |
859 | * @param name the name to use for the reflog entries | |
41808d04 | 860 | * @param email the email to use for the reflog entries |
659cf202 CMN |
861 | */ |
862 | GIT_EXTERN(int) git_repository_set_ident(git_repository *repo, const char *name, const char *email); | |
863 | ||
3315782c VM |
864 | /** @} */ |
865 | GIT_END_DECL | |
866 | #endif |