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