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