2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
8 #include "repository.h"
12 #include "git2/object.h"
13 #include "git2/sys/repository.h"
30 #include "diff_driver.h"
31 #include "annotated_commit.h"
32 #include "submodule.h"
38 # include "win32/w32_util.h"
41 bool git_repository__fsync_gitdir
= false;
44 git_repository_item_t parent
;
45 git_repository_item_t fallback
;
49 { GIT_REPOSITORY_ITEM_GITDIR
, GIT_REPOSITORY_ITEM__LAST
, NULL
, true },
50 { GIT_REPOSITORY_ITEM_WORKDIR
, GIT_REPOSITORY_ITEM__LAST
, NULL
, true },
51 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM__LAST
, NULL
, true },
52 { GIT_REPOSITORY_ITEM_GITDIR
, GIT_REPOSITORY_ITEM__LAST
, "index", false },
53 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "objects", true },
54 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "refs", true },
55 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "packed-refs", false },
56 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "remotes", true },
57 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "config", false },
58 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "info", true },
59 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "hooks", true },
60 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "logs", true },
61 { GIT_REPOSITORY_ITEM_GITDIR
, GIT_REPOSITORY_ITEM__LAST
, "modules", true },
62 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "worktrees", true }
65 static int check_repositoryformatversion(int *version
, git_config
*config
);
66 static int check_extensions(git_config
*config
, int version
);
68 #define GIT_COMMONDIR_FILE "commondir"
69 #define GIT_GITDIR_FILE "gitdir"
71 #define GIT_FILE_CONTENT_PREFIX "gitdir:"
73 #define GIT_BRANCH_DEFAULT "master"
75 #define GIT_REPO_VERSION 0
76 #define GIT_REPO_MAX_VERSION 1
78 git_buf git_repository__reserved_names_win32
[] = {
79 { DOT_GIT
, 0, CONST_STRLEN(DOT_GIT
) },
80 { GIT_DIR_SHORTNAME
, 0, CONST_STRLEN(GIT_DIR_SHORTNAME
) }
82 size_t git_repository__reserved_names_win32_len
= 2;
84 git_buf git_repository__reserved_names_posix
[] = {
85 { DOT_GIT
, 0, CONST_STRLEN(DOT_GIT
) },
87 size_t git_repository__reserved_names_posix_len
= 1;
89 static void set_odb(git_repository
*repo
, git_odb
*odb
)
92 GIT_REFCOUNT_OWN(odb
, repo
);
93 GIT_REFCOUNT_INC(odb
);
96 if ((odb
= git_atomic_swap(repo
->_odb
, odb
)) != NULL
) {
97 GIT_REFCOUNT_OWN(odb
, NULL
);
102 static void set_refdb(git_repository
*repo
, git_refdb
*refdb
)
105 GIT_REFCOUNT_OWN(refdb
, repo
);
106 GIT_REFCOUNT_INC(refdb
);
109 if ((refdb
= git_atomic_swap(repo
->_refdb
, refdb
)) != NULL
) {
110 GIT_REFCOUNT_OWN(refdb
, NULL
);
111 git_refdb_free(refdb
);
115 static void set_config(git_repository
*repo
, git_config
*config
)
118 GIT_REFCOUNT_OWN(config
, repo
);
119 GIT_REFCOUNT_INC(config
);
122 if ((config
= git_atomic_swap(repo
->_config
, config
)) != NULL
) {
123 GIT_REFCOUNT_OWN(config
, NULL
);
124 git_config_free(config
);
127 git_repository__configmap_lookup_cache_clear(repo
);
130 static void set_index(git_repository
*repo
, git_index
*index
)
133 GIT_REFCOUNT_OWN(index
, repo
);
134 GIT_REFCOUNT_INC(index
);
137 if ((index
= git_atomic_swap(repo
->_index
, index
)) != NULL
) {
138 GIT_REFCOUNT_OWN(index
, NULL
);
139 git_index_free(index
);
143 int git_repository__cleanup(git_repository
*repo
)
145 GIT_ASSERT_ARG(repo
);
147 git_repository_submodule_cache_clear(repo
);
148 git_cache_clear(&repo
->objects
);
149 git_attr_cache_flush(repo
);
151 set_config(repo
, NULL
);
152 set_index(repo
, NULL
);
154 set_refdb(repo
, NULL
);
159 void git_repository_free(git_repository
*repo
)
166 git_repository__cleanup(repo
);
168 git_cache_dispose(&repo
->objects
);
170 git_diff_driver_registry_free(repo
->diff_drivers
);
171 repo
->diff_drivers
= NULL
;
173 for (i
= 0; i
< repo
->reserved_names
.size
; i
++)
174 git_buf_dispose(git_array_get(repo
->reserved_names
, i
));
175 git_array_clear(repo
->reserved_names
);
177 git__free(repo
->gitlink
);
178 git__free(repo
->gitdir
);
179 git__free(repo
->commondir
);
180 git__free(repo
->workdir
);
181 git__free(repo
->namespace);
182 git__free(repo
->ident_name
);
183 git__free(repo
->ident_email
);
185 git__memzero(repo
, sizeof(*repo
));
189 /* Check if we have a separate commondir (e.g. we have a worktree) */
190 static int lookup_commondir(bool *separate
, git_buf
*commondir
, git_buf
*repository_path
)
192 git_buf common_link
= GIT_BUF_INIT
;
196 * If there's no commondir file, the repository path is the
197 * common path, but it needs a trailing slash.
199 if (!git_path_contains_file(repository_path
, GIT_COMMONDIR_FILE
)) {
200 if ((error
= git_buf_set(commondir
, repository_path
->ptr
, repository_path
->size
)) == 0)
201 error
= git_path_to_dir(commondir
);
209 if ((error
= git_buf_joinpath(&common_link
, repository_path
->ptr
, GIT_COMMONDIR_FILE
)) < 0 ||
210 (error
= git_futils_readbuffer(&common_link
, common_link
.ptr
)) < 0)
213 git_buf_rtrim(&common_link
);
214 if (git_path_is_relative(common_link
.ptr
)) {
215 if ((error
= git_buf_joinpath(commondir
, repository_path
->ptr
, common_link
.ptr
)) < 0)
218 git_buf_swap(commondir
, &common_link
);
221 git_buf_dispose(&common_link
);
223 /* Make sure the commondir path always has a trailing slash */
224 error
= git_path_prettify_dir(commondir
, commondir
->ptr
, NULL
);
230 GIT_INLINE(int) validate_repo_path(git_buf
*path
)
233 * The longest static path in a repository (or commondir) is the
234 * packed refs file. (Loose refs may be longer since they
235 * include the reference name, but will be validated when the
236 * path is constructed.)
238 static size_t suffix_len
=
239 CONST_STRLEN("objects/pack/pack-.pack.lock") +
242 return git_path_validate_filesystem_with_suffix(
243 path
->ptr
, path
->size
, suffix_len
);
247 * Git repository open methods
249 * Open a repository object from its path
251 static int is_valid_repository_path(bool *out
, git_buf
*repository_path
, git_buf
*common_path
)
253 bool separate_commondir
= false;
258 if ((error
= lookup_commondir(&separate_commondir
, common_path
, repository_path
)) < 0)
261 /* Ensure HEAD file exists */
262 if (git_path_contains_file(repository_path
, GIT_HEAD_FILE
) == false)
265 /* Check files in common dir */
266 if (git_path_contains_dir(common_path
, GIT_OBJECTS_DIR
) == false)
268 if (git_path_contains_dir(common_path
, GIT_REFS_DIR
) == false)
271 /* Ensure the repo (and commondir) are valid paths */
272 if ((error
= validate_repo_path(common_path
)) < 0 ||
273 (separate_commondir
&&
274 (error
= validate_repo_path(repository_path
)) < 0))
281 static git_repository
*repository_alloc(void)
283 git_repository
*repo
= git__calloc(1, sizeof(git_repository
));
286 git_cache_init(&repo
->objects
) < 0)
289 git_array_init_to_size(repo
->reserved_names
, 4);
290 if (!repo
->reserved_names
.ptr
)
293 /* set all the entries in the configmap cache to `unset` */
294 git_repository__configmap_lookup_cache_clear(repo
);
300 git_cache_dispose(&repo
->objects
);
306 int git_repository_new(git_repository
**out
)
308 git_repository
*repo
;
310 *out
= repo
= repository_alloc();
311 GIT_ERROR_CHECK_ALLOC(repo
);
314 repo
->is_worktree
= 0;
319 static int load_config_data(git_repository
*repo
, const git_config
*config
)
323 int err
= git_config_get_bool(&is_bare
, config
, "core.bare");
324 if (err
< 0 && err
!= GIT_ENOTFOUND
)
327 /* Try to figure out if it's bare, default to non-bare if it's not set */
328 if (err
!= GIT_ENOTFOUND
)
329 repo
->is_bare
= is_bare
&& !repo
->is_worktree
;
336 static int load_workdir(git_repository
*repo
, git_config
*config
, git_buf
*parent_path
)
339 git_config_entry
*ce
;
340 git_buf worktree
= GIT_BUF_INIT
;
341 git_buf path
= GIT_BUF_INIT
;
346 if ((error
= git_config__lookup_entry(
347 &ce
, config
, "core.worktree", false)) < 0)
350 if (repo
->is_worktree
) {
351 char *gitlink
= git_worktree__read_link(repo
->gitdir
, GIT_GITDIR_FILE
);
357 git_buf_attach(&worktree
, gitlink
, 0);
359 if ((git_path_dirname_r(&worktree
, worktree
.ptr
)) < 0 ||
360 git_path_to_dir(&worktree
) < 0) {
365 repo
->workdir
= git_buf_detach(&worktree
);
367 else if (ce
&& ce
->value
) {
368 if ((error
= git_path_prettify_dir(
369 &worktree
, ce
->value
, repo
->gitdir
)) < 0)
372 repo
->workdir
= git_buf_detach(&worktree
);
374 else if (parent_path
&& git_path_isdir(parent_path
->ptr
))
375 repo
->workdir
= git_buf_detach(parent_path
);
377 if (git_path_dirname_r(&worktree
, repo
->gitdir
) < 0 ||
378 git_path_to_dir(&worktree
) < 0) {
383 repo
->workdir
= git_buf_detach(&worktree
);
386 GIT_ERROR_CHECK_ALLOC(repo
->workdir
);
388 git_buf_dispose(&path
);
389 git_config_entry_free(ce
);
394 * This function returns furthest offset into path where a ceiling dir
395 * is found, so we can stop processing the path at that point.
397 * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on
398 * the stack could remove directories name limits, but at the cost of doing
399 * repeated malloc/frees inside the loop below, so let's not do it now.
401 static size_t find_ceiling_dir_offset(
403 const char *ceiling_directories
)
405 char buf
[GIT_PATH_MAX
+ 1];
406 char buf2
[GIT_PATH_MAX
+ 1];
407 const char *ceil
, *sep
;
408 size_t len
, max_len
= 0, min_len
;
410 GIT_ASSERT_ARG(path
);
412 min_len
= (size_t)(git_path_root(path
) + 1);
414 if (ceiling_directories
== NULL
|| min_len
== 0)
417 for (sep
= ceil
= ceiling_directories
; *sep
; ceil
= sep
+ 1) {
418 for (sep
= ceil
; *sep
&& *sep
!= GIT_PATH_LIST_SEPARATOR
; sep
++);
421 if (len
== 0 || len
>= sizeof(buf
) || git_path_root(ceil
) == -1)
424 strncpy(buf
, ceil
, len
);
427 if (p_realpath(buf
, buf2
) == NULL
)
431 if (len
> 0 && buf2
[len
-1] == '/')
434 if (!strncmp(path
, buf2
, len
) &&
435 (path
[len
] == '/' || !path
[len
]) &&
442 return (max_len
<= min_len
? min_len
: max_len
);
446 * Read the contents of `file_path` and set `path_out` to the repo dir that
447 * it points to. Before calling, set `path_out` to the base directory that
448 * should be used if the contents of `file_path` are a relative path.
450 static int read_gitfile(git_buf
*path_out
, const char *file_path
)
453 git_buf file
= GIT_BUF_INIT
;
454 size_t prefix_len
= strlen(GIT_FILE_CONTENT_PREFIX
);
456 GIT_ASSERT_ARG(path_out
);
457 GIT_ASSERT_ARG(file_path
);
459 if (git_futils_readbuffer(&file
, file_path
) < 0)
462 git_buf_rtrim(&file
);
463 /* apparently on Windows, some people use backslashes in paths */
464 git_path_mkposix(file
.ptr
);
466 if (git_buf_len(&file
) <= prefix_len
||
467 memcmp(git_buf_cstr(&file
), GIT_FILE_CONTENT_PREFIX
, prefix_len
) != 0)
469 git_error_set(GIT_ERROR_REPOSITORY
,
470 "the `.git` file at '%s' is malformed", file_path
);
473 else if ((error
= git_path_dirname_r(path_out
, file_path
)) >= 0) {
474 const char *gitlink
= git_buf_cstr(&file
) + prefix_len
;
475 while (*gitlink
&& git__isspace(*gitlink
)) gitlink
++;
477 error
= git_path_prettify_dir(
478 path_out
, gitlink
, git_buf_cstr(path_out
));
481 git_buf_dispose(&file
);
485 static int find_repo(
486 git_buf
*gitdir_path
,
487 git_buf
*workdir_path
,
488 git_buf
*gitlink_path
,
489 git_buf
*commondir_path
,
490 const char *start_path
,
492 const char *ceiling_dirs
)
494 git_buf path
= GIT_BUF_INIT
;
495 git_buf repo_link
= GIT_BUF_INIT
;
496 git_buf common_link
= GIT_BUF_INIT
;
498 dev_t initial_device
= 0;
500 bool in_dot_git
, is_valid
;
501 size_t ceiling_offset
= 0;
504 git_buf_clear(gitdir_path
);
506 error
= git_path_prettify(&path
, start_path
, NULL
);
510 /* in_dot_git toggles each loop:
511 * /a/b/c/.git, /a/b/c, /a/b/.git, /a/b, /a/.git, /a
512 * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, we
513 * assume we started with /a/b/c.git and don't append .git the first
515 * min_iterations indicates the number of iterations left before going
516 * further counts as a search. */
517 if (flags
& (GIT_REPOSITORY_OPEN_BARE
| GIT_REPOSITORY_OPEN_NO_DOTGIT
)) {
526 if (!(flags
& GIT_REPOSITORY_OPEN_NO_DOTGIT
)) {
528 if ((error
= git_buf_joinpath(&path
, path
.ptr
, DOT_GIT
)) < 0)
531 in_dot_git
= !in_dot_git
;
534 if (p_stat(path
.ptr
, &st
) == 0) {
535 /* check that we have not crossed device boundaries */
536 if (initial_device
== 0)
537 initial_device
= st
.st_dev
;
538 else if (st
.st_dev
!= initial_device
&&
539 !(flags
& GIT_REPOSITORY_OPEN_CROSS_FS
))
542 if (S_ISDIR(st
.st_mode
)) {
543 if ((error
= is_valid_repository_path(&is_valid
, &path
, &common_link
)) < 0)
547 if ((error
= git_path_to_dir(&path
)) < 0 ||
548 (error
= git_buf_set(gitdir_path
, path
.ptr
, path
.size
)) < 0)
552 if ((error
= git_buf_attach(gitlink_path
, git_worktree__read_link(path
.ptr
, GIT_GITDIR_FILE
), 0)) < 0)
555 git_buf_swap(&common_link
, commondir_path
);
559 } else if (S_ISREG(st
.st_mode
) && git__suffixcmp(path
.ptr
, "/" DOT_GIT
) == 0) {
560 if ((error
= read_gitfile(&repo_link
, path
.ptr
)) < 0 ||
561 (error
= is_valid_repository_path(&is_valid
, &repo_link
, &common_link
)) < 0)
565 git_buf_swap(gitdir_path
, &repo_link
);
568 if ((error
= git_buf_put(gitlink_path
, path
.ptr
, path
.size
)) < 0)
571 git_buf_swap(&common_link
, commondir_path
);
577 /* Move up one directory. If we're in_dot_git, we'll search the
578 * parent itself next. If we're !in_dot_git, we'll search .git
579 * in the parent directory next (added at the top of the loop). */
580 if ((error
= git_path_dirname_r(&path
, path
.ptr
)) < 0)
583 /* Once we've checked the directory (and .git if applicable),
584 * find the ceiling for a search. */
585 if (min_iterations
&& (--min_iterations
== 0))
586 ceiling_offset
= find_ceiling_dir_offset(path
.ptr
, ceiling_dirs
);
588 /* Check if we should stop searching here. */
589 if (min_iterations
== 0 &&
590 (path
.ptr
[ceiling_offset
] == 0 || (flags
& GIT_REPOSITORY_OPEN_NO_SEARCH
)))
594 if (workdir_path
&& !(flags
& GIT_REPOSITORY_OPEN_BARE
)) {
595 if (!git_buf_len(gitdir_path
))
596 git_buf_clear(workdir_path
);
597 else if ((error
= git_path_dirname_r(workdir_path
, path
.ptr
)) < 0 ||
598 (error
= git_path_to_dir(workdir_path
)) < 0)
602 /* If we didn't find the repository, and we don't have any other error
603 * to report, report that. */
604 if (!git_buf_len(gitdir_path
)) {
605 git_error_set(GIT_ERROR_REPOSITORY
, "could not find repository from '%s'", start_path
);
606 error
= GIT_ENOTFOUND
;
611 git_buf_dispose(&path
);
612 git_buf_dispose(&repo_link
);
613 git_buf_dispose(&common_link
);
617 int git_repository_open_bare(
618 git_repository
**repo_ptr
,
619 const char *bare_path
)
621 git_buf path
= GIT_BUF_INIT
, common_path
= GIT_BUF_INIT
;
622 git_repository
*repo
= NULL
;
626 if ((error
= git_path_prettify_dir(&path
, bare_path
, NULL
)) < 0 ||
627 (error
= is_valid_repository_path(&is_valid
, &path
, &common_path
)) < 0)
631 git_buf_dispose(&path
);
632 git_buf_dispose(&common_path
);
633 git_error_set(GIT_ERROR_REPOSITORY
, "path is not a repository: %s", bare_path
);
634 return GIT_ENOTFOUND
;
637 repo
= repository_alloc();
638 GIT_ERROR_CHECK_ALLOC(repo
);
640 repo
->gitdir
= git_buf_detach(&path
);
641 GIT_ERROR_CHECK_ALLOC(repo
->gitdir
);
642 repo
->commondir
= git_buf_detach(&common_path
);
643 GIT_ERROR_CHECK_ALLOC(repo
->commondir
);
645 /* of course we're bare! */
647 repo
->is_worktree
= 0;
648 repo
->workdir
= NULL
;
654 static int _git_repository_open_ext_from_env(
655 git_repository
**out
,
656 const char *start_path
)
658 git_repository
*repo
= NULL
;
659 git_index
*index
= NULL
;
661 git_buf dir_buf
= GIT_BUF_INIT
;
662 git_buf ceiling_dirs_buf
= GIT_BUF_INIT
;
663 git_buf across_fs_buf
= GIT_BUF_INIT
;
664 git_buf index_file_buf
= GIT_BUF_INIT
;
665 git_buf namespace_buf
= GIT_BUF_INIT
;
666 git_buf object_dir_buf
= GIT_BUF_INIT
;
667 git_buf alts_buf
= GIT_BUF_INIT
;
668 git_buf work_tree_buf
= GIT_BUF_INIT
;
669 git_buf common_dir_buf
= GIT_BUF_INIT
;
670 const char *ceiling_dirs
= NULL
;
675 error
= git__getenv(&dir_buf
, "GIT_DIR");
676 if (error
== GIT_ENOTFOUND
) {
679 } else if (error
< 0)
682 start_path
= git_buf_cstr(&dir_buf
);
683 flags
|= GIT_REPOSITORY_OPEN_NO_SEARCH
;
684 flags
|= GIT_REPOSITORY_OPEN_NO_DOTGIT
;
688 error
= git__getenv(&ceiling_dirs_buf
, "GIT_CEILING_DIRECTORIES");
689 if (error
== GIT_ENOTFOUND
)
694 ceiling_dirs
= git_buf_cstr(&ceiling_dirs_buf
);
696 error
= git__getenv(&across_fs_buf
, "GIT_DISCOVERY_ACROSS_FILESYSTEM");
697 if (error
== GIT_ENOTFOUND
)
703 error
= git_config_parse_bool(&across_fs
, git_buf_cstr(&across_fs_buf
));
707 flags
|= GIT_REPOSITORY_OPEN_CROSS_FS
;
710 error
= git__getenv(&index_file_buf
, "GIT_INDEX_FILE");
711 if (error
== GIT_ENOTFOUND
)
716 error
= git_index_open(&index
, git_buf_cstr(&index_file_buf
));
721 error
= git__getenv(&namespace_buf
, "GIT_NAMESPACE");
722 if (error
== GIT_ENOTFOUND
)
727 error
= git__getenv(&object_dir_buf
, "GIT_OBJECT_DIRECTORY");
728 if (error
== GIT_ENOTFOUND
)
733 error
= git_odb_open(&odb
, git_buf_cstr(&object_dir_buf
));
738 error
= git__getenv(&work_tree_buf
, "GIT_WORK_TREE");
739 if (error
== GIT_ENOTFOUND
)
744 git_error_set(GIT_ERROR_INVALID
, "GIT_WORK_TREE unimplemented");
749 error
= git__getenv(&work_tree_buf
, "GIT_COMMON_DIR");
750 if (error
== GIT_ENOTFOUND
)
755 git_error_set(GIT_ERROR_INVALID
, "GIT_COMMON_DIR unimplemented");
760 error
= git_repository_open_ext(&repo
, start_path
, flags
, ceiling_dirs
);
765 git_repository_set_odb(repo
, odb
);
767 error
= git__getenv(&alts_buf
, "GIT_ALTERNATE_OBJECT_DIRECTORIES");
768 if (error
== GIT_ENOTFOUND
) {
771 } else if (error
< 0)
777 error
= git_repository_odb(&odb
, repo
);
782 end
= git_buf_cstr(&alts_buf
) + git_buf_len(&alts_buf
);
783 for (sep
= alt
= alts_buf
.ptr
; sep
!= end
; alt
= sep
+1) {
784 for (sep
= alt
; *sep
&& *sep
!= GIT_PATH_LIST_SEPARATOR
; sep
++)
788 error
= git_odb_add_disk_alternate(odb
, alt
);
794 if (git_buf_len(&namespace_buf
)) {
795 error
= git_repository_set_namespace(repo
, git_buf_cstr(&namespace_buf
));
800 git_repository_set_index(repo
, index
);
807 git_repository_free(repo
);
810 git_index_free(index
);
811 git_buf_dispose(&common_dir_buf
);
812 git_buf_dispose(&work_tree_buf
);
813 git_buf_dispose(&alts_buf
);
814 git_buf_dispose(&object_dir_buf
);
815 git_buf_dispose(&namespace_buf
);
816 git_buf_dispose(&index_file_buf
);
817 git_buf_dispose(&across_fs_buf
);
818 git_buf_dispose(&ceiling_dirs_buf
);
819 git_buf_dispose(&dir_buf
);
823 static int repo_is_worktree(unsigned *out
, const git_repository
*repo
)
825 git_buf gitdir_link
= GIT_BUF_INIT
;
828 /* Worktrees cannot have the same commondir and gitdir */
829 if (repo
->commondir
&& repo
->gitdir
830 && !strcmp(repo
->commondir
, repo
->gitdir
)) {
835 if ((error
= git_buf_joinpath(&gitdir_link
, repo
->gitdir
, "gitdir")) < 0)
838 /* A 'gitdir' file inside a git directory is currently
839 * only used when the repository is a working tree. */
840 *out
= !!git_path_exists(gitdir_link
.ptr
);
842 git_buf_dispose(&gitdir_link
);
846 int git_repository_open_ext(
847 git_repository
**repo_ptr
,
848 const char *start_path
,
850 const char *ceiling_dirs
)
853 unsigned is_worktree
;
854 git_buf gitdir
= GIT_BUF_INIT
, workdir
= GIT_BUF_INIT
,
855 gitlink
= GIT_BUF_INIT
, commondir
= GIT_BUF_INIT
;
856 git_repository
*repo
= NULL
;
857 git_config
*config
= NULL
;
860 if (flags
& GIT_REPOSITORY_OPEN_FROM_ENV
)
861 return _git_repository_open_ext_from_env(repo_ptr
, start_path
);
867 &gitdir
, &workdir
, &gitlink
, &commondir
, start_path
, flags
, ceiling_dirs
);
869 if (error
< 0 || !repo_ptr
)
872 repo
= repository_alloc();
873 GIT_ERROR_CHECK_ALLOC(repo
);
875 repo
->gitdir
= git_buf_detach(&gitdir
);
876 GIT_ERROR_CHECK_ALLOC(repo
->gitdir
);
879 repo
->gitlink
= git_buf_detach(&gitlink
);
880 GIT_ERROR_CHECK_ALLOC(repo
->gitlink
);
882 if (commondir
.size
) {
883 repo
->commondir
= git_buf_detach(&commondir
);
884 GIT_ERROR_CHECK_ALLOC(repo
->commondir
);
887 if ((error
= repo_is_worktree(&is_worktree
, repo
)) < 0)
889 repo
->is_worktree
= is_worktree
;
892 * We'd like to have the config, but git doesn't particularly
893 * care if it's not there, so we need to deal with that.
896 error
= git_repository_config_snapshot(&config
, repo
);
897 if (error
< 0 && error
!= GIT_ENOTFOUND
)
900 if (config
&& (error
= check_repositoryformatversion(&version
, config
)) < 0)
903 if ((error
= check_extensions(config
, version
)) < 0)
906 if ((flags
& GIT_REPOSITORY_OPEN_BARE
) != 0)
911 ((error
= load_config_data(repo
, config
)) < 0 ||
912 (error
= load_workdir(repo
, config
, &workdir
)) < 0))
917 git_buf_dispose(&gitdir
);
918 git_buf_dispose(&workdir
);
919 git_buf_dispose(&gitlink
);
920 git_buf_dispose(&commondir
);
921 git_config_free(config
);
924 git_repository_free(repo
);
931 int git_repository_open(git_repository
**repo_out
, const char *path
)
933 return git_repository_open_ext(
934 repo_out
, path
, GIT_REPOSITORY_OPEN_NO_SEARCH
, NULL
);
937 int git_repository_open_from_worktree(git_repository
**repo_out
, git_worktree
*wt
)
939 git_buf path
= GIT_BUF_INIT
;
940 git_repository
*repo
= NULL
;
944 GIT_ASSERT_ARG(repo_out
);
948 len
= strlen(wt
->gitlink_path
);
950 if (len
<= 4 || strcasecmp(wt
->gitlink_path
+ len
- 4, ".git")) {
955 if ((err
= git_buf_set(&path
, wt
->gitlink_path
, len
- 4)) < 0)
958 if ((err
= git_repository_open(&repo
, path
.ptr
)) < 0)
964 git_buf_dispose(&path
);
969 int git_repository_wrap_odb(git_repository
**repo_out
, git_odb
*odb
)
971 git_repository
*repo
;
973 repo
= repository_alloc();
974 GIT_ERROR_CHECK_ALLOC(repo
);
976 git_repository_set_odb(repo
, odb
);
982 int git_repository_discover(
984 const char *start_path
,
986 const char *ceiling_dirs
)
988 uint32_t flags
= across_fs
? GIT_REPOSITORY_OPEN_CROSS_FS
: 0;
991 GIT_ASSERT_ARG(start_path
);
993 if ((error
= git_buf_sanitize(out
)) < 0)
996 return find_repo(out
, NULL
, NULL
, NULL
, start_path
, flags
, ceiling_dirs
);
999 static int load_config(
1001 git_repository
*repo
,
1002 const char *global_config_path
,
1003 const char *xdg_config_path
,
1004 const char *system_config_path
,
1005 const char *programdata_path
)
1008 git_buf config_path
= GIT_BUF_INIT
;
1009 git_config
*cfg
= NULL
;
1011 GIT_ASSERT_ARG(out
);
1013 if ((error
= git_config_new(&cfg
)) < 0)
1017 if ((error
= git_repository_item_path(&config_path
, repo
, GIT_REPOSITORY_ITEM_CONFIG
)) == 0)
1018 error
= git_config_add_file_ondisk(cfg
, config_path
.ptr
, GIT_CONFIG_LEVEL_LOCAL
, repo
, 0);
1020 if (error
&& error
!= GIT_ENOTFOUND
)
1023 git_buf_dispose(&config_path
);
1026 if (global_config_path
!= NULL
&&
1027 (error
= git_config_add_file_ondisk(
1028 cfg
, global_config_path
, GIT_CONFIG_LEVEL_GLOBAL
, repo
, 0)) < 0 &&
1029 error
!= GIT_ENOTFOUND
)
1032 if (xdg_config_path
!= NULL
&&
1033 (error
= git_config_add_file_ondisk(
1034 cfg
, xdg_config_path
, GIT_CONFIG_LEVEL_XDG
, repo
, 0)) < 0 &&
1035 error
!= GIT_ENOTFOUND
)
1038 if (system_config_path
!= NULL
&&
1039 (error
= git_config_add_file_ondisk(
1040 cfg
, system_config_path
, GIT_CONFIG_LEVEL_SYSTEM
, repo
, 0)) < 0 &&
1041 error
!= GIT_ENOTFOUND
)
1044 if (programdata_path
!= NULL
&&
1045 (error
= git_config_add_file_ondisk(
1046 cfg
, programdata_path
, GIT_CONFIG_LEVEL_PROGRAMDATA
, repo
, 0)) < 0 &&
1047 error
!= GIT_ENOTFOUND
)
1050 git_error_clear(); /* clear any lingering ENOTFOUND errors */
1056 git_buf_dispose(&config_path
);
1057 git_config_free(cfg
);
1062 static const char *path_unless_empty(git_buf
*buf
)
1064 return git_buf_len(buf
) > 0 ? git_buf_cstr(buf
) : NULL
;
1067 int git_repository_config__weakptr(git_config
**out
, git_repository
*repo
)
1071 if (repo
->_config
== NULL
) {
1072 git_buf global_buf
= GIT_BUF_INIT
;
1073 git_buf xdg_buf
= GIT_BUF_INIT
;
1074 git_buf system_buf
= GIT_BUF_INIT
;
1075 git_buf programdata_buf
= GIT_BUF_INIT
;
1078 git_config_find_global(&global_buf
);
1079 git_config_find_xdg(&xdg_buf
);
1080 git_config_find_system(&system_buf
);
1081 git_config_find_programdata(&programdata_buf
);
1083 /* If there is no global file, open a backend for it anyway */
1084 if (git_buf_len(&global_buf
) == 0)
1085 git_config__global_location(&global_buf
);
1087 error
= load_config(
1089 path_unless_empty(&global_buf
),
1090 path_unless_empty(&xdg_buf
),
1091 path_unless_empty(&system_buf
),
1092 path_unless_empty(&programdata_buf
));
1094 GIT_REFCOUNT_OWN(config
, repo
);
1096 if (git_atomic_compare_and_swap(&repo
->_config
, NULL
, config
) != NULL
) {
1097 GIT_REFCOUNT_OWN(config
, NULL
);
1098 git_config_free(config
);
1102 git_buf_dispose(&global_buf
);
1103 git_buf_dispose(&xdg_buf
);
1104 git_buf_dispose(&system_buf
);
1105 git_buf_dispose(&programdata_buf
);
1108 *out
= repo
->_config
;
1112 int git_repository_config(git_config
**out
, git_repository
*repo
)
1114 if (git_repository_config__weakptr(out
, repo
) < 0)
1117 GIT_REFCOUNT_INC(*out
);
1121 int git_repository_config_snapshot(git_config
**out
, git_repository
*repo
)
1126 if ((error
= git_repository_config__weakptr(&weak
, repo
)) < 0)
1129 return git_config_snapshot(out
, weak
);
1132 int git_repository_set_config(git_repository
*repo
, git_config
*config
)
1134 GIT_ASSERT_ARG(repo
);
1135 GIT_ASSERT_ARG(config
);
1137 set_config(repo
, config
);
1141 int git_repository_odb__weakptr(git_odb
**out
, git_repository
*repo
)
1145 GIT_ASSERT_ARG(repo
);
1146 GIT_ASSERT_ARG(out
);
1148 *out
= git_atomic_load(repo
->_odb
);
1150 git_buf odb_path
= GIT_BUF_INIT
;
1153 if ((error
= git_repository_item_path(&odb_path
, repo
,
1154 GIT_REPOSITORY_ITEM_OBJECTS
)) < 0 ||
1155 (error
= git_odb_new(&odb
)) < 0)
1158 GIT_REFCOUNT_OWN(odb
, repo
);
1160 if ((error
= git_odb__set_caps(odb
, GIT_ODB_CAP_FROM_OWNER
)) < 0 ||
1161 (error
= git_odb__add_default_backends(odb
, odb_path
.ptr
, 0, 0)) < 0) {
1166 if (git_atomic_compare_and_swap(&repo
->_odb
, NULL
, odb
) != NULL
) {
1167 GIT_REFCOUNT_OWN(odb
, NULL
);
1171 git_buf_dispose(&odb_path
);
1172 *out
= git_atomic_load(repo
->_odb
);
1178 int git_repository_odb(git_odb
**out
, git_repository
*repo
)
1180 if (git_repository_odb__weakptr(out
, repo
) < 0)
1183 GIT_REFCOUNT_INC(*out
);
1187 int git_repository_set_odb(git_repository
*repo
, git_odb
*odb
)
1189 GIT_ASSERT_ARG(repo
);
1190 GIT_ASSERT_ARG(odb
);
1196 int git_repository_refdb__weakptr(git_refdb
**out
, git_repository
*repo
)
1200 GIT_ASSERT_ARG(out
);
1201 GIT_ASSERT_ARG(repo
);
1203 if (repo
->_refdb
== NULL
) {
1206 error
= git_refdb_open(&refdb
, repo
);
1208 GIT_REFCOUNT_OWN(refdb
, repo
);
1210 if (git_atomic_compare_and_swap(&repo
->_refdb
, NULL
, refdb
) != NULL
) {
1211 GIT_REFCOUNT_OWN(refdb
, NULL
);
1212 git_refdb_free(refdb
);
1217 *out
= repo
->_refdb
;
1221 int git_repository_refdb(git_refdb
**out
, git_repository
*repo
)
1223 if (git_repository_refdb__weakptr(out
, repo
) < 0)
1226 GIT_REFCOUNT_INC(*out
);
1230 int git_repository_set_refdb(git_repository
*repo
, git_refdb
*refdb
)
1232 GIT_ASSERT_ARG(repo
);
1233 GIT_ASSERT_ARG(refdb
);
1235 set_refdb(repo
, refdb
);
1239 int git_repository_index__weakptr(git_index
**out
, git_repository
*repo
)
1243 GIT_ASSERT_ARG(out
);
1244 GIT_ASSERT_ARG(repo
);
1246 if (repo
->_index
== NULL
) {
1247 git_buf index_path
= GIT_BUF_INIT
;
1250 if ((error
= git_buf_joinpath(&index_path
, repo
->gitdir
, GIT_INDEX_FILE
)) < 0)
1253 error
= git_index_open(&index
, index_path
.ptr
);
1255 GIT_REFCOUNT_OWN(index
, repo
);
1257 if (git_atomic_compare_and_swap(&repo
->_index
, NULL
, index
) != NULL
) {
1258 GIT_REFCOUNT_OWN(index
, NULL
);
1259 git_index_free(index
);
1262 error
= git_index_set_caps(repo
->_index
,
1263 GIT_INDEX_CAPABILITY_FROM_OWNER
);
1266 git_buf_dispose(&index_path
);
1269 *out
= repo
->_index
;
1273 int git_repository_index(git_index
**out
, git_repository
*repo
)
1275 if (git_repository_index__weakptr(out
, repo
) < 0)
1278 GIT_REFCOUNT_INC(*out
);
1282 int git_repository_set_index(git_repository
*repo
, git_index
*index
)
1284 GIT_ASSERT_ARG(repo
);
1285 set_index(repo
, index
);
1289 int git_repository_set_namespace(git_repository
*repo
, const char *namespace)
1291 git__free(repo
->namespace);
1293 if (namespace == NULL
) {
1294 repo
->namespace = NULL
;
1298 return (repo
->namespace = git__strdup(namespace)) ? 0 : -1;
1301 const char *git_repository_get_namespace(git_repository
*repo
)
1303 return repo
->namespace;
1307 static int reserved_names_add8dot3(git_repository
*repo
, const char *path
)
1309 char *name
= git_win32_path_8dot3_name(path
);
1310 const char *def
= GIT_DIR_SHORTNAME
;
1311 const char *def_dot_git
= DOT_GIT
;
1312 size_t name_len
, def_len
= CONST_STRLEN(GIT_DIR_SHORTNAME
);
1313 size_t def_dot_git_len
= CONST_STRLEN(DOT_GIT
);
1319 name_len
= strlen(name
);
1321 if ((name_len
== def_len
&& memcmp(name
, def
, def_len
) == 0) ||
1322 (name_len
== def_dot_git_len
&& memcmp(name
, def_dot_git
, def_dot_git_len
) == 0)) {
1327 if ((buf
= git_array_alloc(repo
->reserved_names
)) == NULL
)
1330 git_buf_attach(buf
, name
, name_len
);
1334 bool git_repository__reserved_names(
1335 git_buf
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
)
1337 GIT_UNUSED(include_ntfs
);
1339 if (repo
->reserved_names
.size
== 0) {
1343 /* Add the static defaults */
1344 for (i
= 0; i
< git_repository__reserved_names_win32_len
; i
++) {
1345 if ((buf
= git_array_alloc(repo
->reserved_names
)) == NULL
)
1348 buf
->ptr
= git_repository__reserved_names_win32
[i
].ptr
;
1349 buf
->size
= git_repository__reserved_names_win32
[i
].size
;
1352 /* Try to add any repo-specific reserved names - the gitlink file
1353 * within a submodule or the repository (if the repository directory
1354 * is beneath the workdir). These are typically `.git`, but should
1355 * be protected in case they are not. Note, repo and workdir paths
1356 * are always prettified to end in `/`, so a prefixcmp is safe.
1358 if (!repo
->is_bare
) {
1359 int (*prefixcmp
)(const char *, const char *);
1360 int error
, ignorecase
;
1362 error
= git_repository__configmap_lookup(
1363 &ignorecase
, repo
, GIT_CONFIGMAP_IGNORECASE
);
1364 prefixcmp
= (error
|| ignorecase
) ? git__prefixcmp_icase
:
1367 if (repo
->gitlink
&&
1368 reserved_names_add8dot3(repo
, repo
->gitlink
) < 0)
1372 prefixcmp(repo
->gitdir
, repo
->workdir
) == 0 &&
1373 reserved_names_add8dot3(repo
, repo
->gitdir
) < 0)
1378 *out
= repo
->reserved_names
.ptr
;
1379 *outlen
= repo
->reserved_names
.size
;
1383 /* Always give good defaults, even on OOM */
1385 *out
= git_repository__reserved_names_win32
;
1386 *outlen
= git_repository__reserved_names_win32_len
;
1391 bool git_repository__reserved_names(
1392 git_buf
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
)
1397 *out
= git_repository__reserved_names_win32
;
1398 *outlen
= git_repository__reserved_names_win32_len
;
1400 *out
= git_repository__reserved_names_posix
;
1401 *outlen
= git_repository__reserved_names_posix_len
;
1408 static int check_repositoryformatversion(int *version
, git_config
*config
)
1412 error
= git_config_get_int32(version
, config
, "core.repositoryformatversion");
1413 /* git ignores this if the config variable isn't there */
1414 if (error
== GIT_ENOTFOUND
)
1420 if (GIT_REPO_MAX_VERSION
< *version
) {
1421 git_error_set(GIT_ERROR_REPOSITORY
,
1422 "unsupported repository version %d; only versions up to %d are supported",
1423 *version
, GIT_REPO_MAX_VERSION
);
1430 static const char *builtin_extensions
[] = {
1434 static git_vector user_extensions
= GIT_VECTOR_INIT
;
1436 static int check_valid_extension(const git_config_entry
*entry
, void *payload
)
1438 git_buf cfg
= GIT_BUF_INIT
;
1440 const char *extension
;
1444 GIT_UNUSED(payload
);
1446 git_vector_foreach (&user_extensions
, i
, extension
) {
1447 git_buf_clear(&cfg
);
1450 * Users can specify that they don't want to support an
1451 * extension with a '!' prefix.
1453 if ((reject
= (extension
[0] == '!')) == true)
1454 extension
= &extension
[1];
1456 if ((error
= git_buf_printf(&cfg
, "extensions.%s", extension
)) < 0)
1459 if (strcmp(entry
->name
, cfg
.ptr
) == 0) {
1467 for (i
= 0; i
< ARRAY_SIZE(builtin_extensions
); i
++) {
1468 extension
= builtin_extensions
[i
];
1470 if ((error
= git_buf_printf(&cfg
, "extensions.%s", extension
)) < 0)
1473 if (strcmp(entry
->name
, cfg
.ptr
) == 0)
1478 git_error_set(GIT_ERROR_REPOSITORY
, "unsupported extension name %s", entry
->name
);
1482 git_buf_dispose(&cfg
);
1486 static int check_extensions(git_config
*config
, int version
)
1491 return git_config_foreach_match(config
, "^extensions\\.", check_valid_extension
, NULL
);
1494 int git_repository__extensions(char ***out
, size_t *out_len
)
1496 git_vector extensions
;
1497 const char *builtin
, *user
;
1501 if (git_vector_init(&extensions
, 8, NULL
) < 0)
1504 for (i
= 0; i
< ARRAY_SIZE(builtin_extensions
); i
++) {
1507 builtin
= builtin_extensions
[i
];
1509 git_vector_foreach (&user_extensions
, j
, user
) {
1510 if (user
[0] == '!' && strcmp(builtin
, &user
[1]) == 0) {
1519 if ((extension
= git__strdup(builtin
)) == NULL
||
1520 git_vector_insert(&extensions
, extension
) < 0)
1524 git_vector_foreach (&user_extensions
, i
, user
) {
1528 if ((extension
= git__strdup(user
)) == NULL
||
1529 git_vector_insert(&extensions
, extension
) < 0)
1533 *out
= (char **)git_vector_detach(out_len
, NULL
, &extensions
);
1537 int git_repository__set_extensions(const char **extensions
, size_t len
)
1542 git_repository__free_extensions();
1544 for (i
= 0; i
< len
; i
++) {
1545 if ((extension
= git__strdup(extensions
[i
])) == NULL
||
1546 git_vector_insert(&user_extensions
, extension
) < 0)
1553 void git_repository__free_extensions(void)
1555 git_vector_free_deep(&user_extensions
);
1558 int git_repository_create_head(const char *git_dir
, const char *ref_name
)
1560 git_buf ref_path
= GIT_BUF_INIT
;
1561 git_filebuf ref
= GIT_FILEBUF_INIT
;
1565 if ((error
= git_buf_joinpath(&ref_path
, git_dir
, GIT_HEAD_FILE
)) < 0 ||
1566 (error
= git_filebuf_open(&ref
, ref_path
.ptr
, 0, GIT_REFS_FILE_MODE
)) < 0)
1569 if (git__prefixcmp(ref_name
, GIT_REFS_DIR
) == 0)
1572 fmt
= "ref: " GIT_REFS_HEADS_DIR
"%s\n";
1574 if ((error
= git_filebuf_printf(&ref
, fmt
, ref_name
)) < 0 ||
1575 (error
= git_filebuf_commit(&ref
)) < 0)
1579 git_buf_dispose(&ref_path
);
1580 git_filebuf_cleanup(&ref
);
1584 static bool is_chmod_supported(const char *file_path
)
1586 struct stat st1
, st2
;
1588 if (p_stat(file_path
, &st1
) < 0)
1591 if (p_chmod(file_path
, st1
.st_mode
^ S_IXUSR
) < 0)
1594 if (p_stat(file_path
, &st2
) < 0)
1597 return (st1
.st_mode
!= st2
.st_mode
);
1600 static bool is_filesystem_case_insensitive(const char *gitdir_path
)
1602 git_buf path
= GIT_BUF_INIT
;
1603 int is_insensitive
= -1;
1605 if (!git_buf_joinpath(&path
, gitdir_path
, "CoNfIg"))
1606 is_insensitive
= git_path_exists(git_buf_cstr(&path
));
1608 git_buf_dispose(&path
);
1609 return is_insensitive
;
1612 static bool are_symlinks_supported(const char *wd_path
)
1614 git_config
*config
= NULL
;
1615 git_buf global_buf
= GIT_BUF_INIT
;
1616 git_buf xdg_buf
= GIT_BUF_INIT
;
1617 git_buf system_buf
= GIT_BUF_INIT
;
1618 git_buf programdata_buf
= GIT_BUF_INIT
;
1622 * To emulate Git for Windows, symlinks on Windows must be explicitly
1623 * opted-in. We examine the system configuration for a core.symlinks
1624 * set to true. If found, we then examine the filesystem to see if
1625 * symlinks are _actually_ supported by the current user. If that is
1626 * _not_ set, then we do not test or enable symlink support.
1629 git_config_find_global(&global_buf
);
1630 git_config_find_xdg(&xdg_buf
);
1631 git_config_find_system(&system_buf
);
1632 git_config_find_programdata(&programdata_buf
);
1634 if (load_config(&config
, NULL
,
1635 path_unless_empty(&global_buf
),
1636 path_unless_empty(&xdg_buf
),
1637 path_unless_empty(&system_buf
),
1638 path_unless_empty(&programdata_buf
)) < 0)
1641 if (git_config_get_bool(&symlinks
, config
, "core.symlinks") < 0 || !symlinks
)
1645 if (!(symlinks
= git_path_supports_symlinks(wd_path
)))
1649 git_buf_dispose(&global_buf
);
1650 git_buf_dispose(&xdg_buf
);
1651 git_buf_dispose(&system_buf
);
1652 git_buf_dispose(&programdata_buf
);
1653 git_config_free(config
);
1654 return symlinks
!= 0;
1657 static int create_empty_file(const char *path
, mode_t mode
)
1661 if ((fd
= p_creat(path
, mode
)) < 0) {
1662 git_error_set(GIT_ERROR_OS
, "error while creating '%s'", path
);
1666 if (p_close(fd
) < 0) {
1667 git_error_set(GIT_ERROR_OS
, "error while closing '%s'", path
);
1674 static int repo_local_config(
1676 git_buf
*config_dir
,
1677 git_repository
*repo
,
1678 const char *repo_dir
)
1682 const char *cfg_path
;
1684 if (git_buf_joinpath(config_dir
, repo_dir
, GIT_CONFIG_FILENAME_INREPO
) < 0)
1686 cfg_path
= git_buf_cstr(config_dir
);
1688 /* make LOCAL config if missing */
1689 if (!git_path_isfile(cfg_path
) &&
1690 (error
= create_empty_file(cfg_path
, GIT_CONFIG_FILE_MODE
)) < 0)
1693 /* if no repo, just open that file directly */
1695 return git_config_open_ondisk(out
, cfg_path
);
1697 /* otherwise, open parent config and get that level */
1698 if ((error
= git_repository_config__weakptr(&parent
, repo
)) < 0)
1701 if (git_config_open_level(out
, parent
, GIT_CONFIG_LEVEL_LOCAL
) < 0) {
1704 if (!(error
= git_config_add_file_ondisk(
1705 parent
, cfg_path
, GIT_CONFIG_LEVEL_LOCAL
, repo
, false)))
1706 error
= git_config_open_level(out
, parent
, GIT_CONFIG_LEVEL_LOCAL
);
1709 git_config_free(parent
);
1714 static int repo_init_fs_configs(
1716 const char *cfg_path
,
1717 const char *repo_dir
,
1718 const char *work_dir
,
1719 bool update_ignorecase
)
1724 work_dir
= repo_dir
;
1726 if ((error
= git_config_set_bool(
1727 cfg
, "core.filemode", is_chmod_supported(cfg_path
))) < 0)
1730 if (!are_symlinks_supported(work_dir
)) {
1731 if ((error
= git_config_set_bool(cfg
, "core.symlinks", false)) < 0)
1733 } else if (git_config_delete_entry(cfg
, "core.symlinks") < 0)
1736 if (update_ignorecase
) {
1737 if (is_filesystem_case_insensitive(repo_dir
)) {
1738 if ((error
= git_config_set_bool(cfg
, "core.ignorecase", true)) < 0)
1740 } else if (git_config_delete_entry(cfg
, "core.ignorecase") < 0)
1744 #ifdef GIT_USE_ICONV
1745 if ((error
= git_config_set_bool(
1746 cfg
, "core.precomposeunicode",
1747 git_path_does_fs_decompose_unicode(work_dir
))) < 0)
1749 /* on non-iconv platforms, don't even set core.precomposeunicode */
1755 static int repo_init_config(
1756 const char *repo_dir
,
1757 const char *work_dir
,
1762 git_buf cfg_path
= GIT_BUF_INIT
, worktree_path
= GIT_BUF_INIT
;
1763 git_config
*config
= NULL
;
1764 bool is_bare
= ((flags
& GIT_REPOSITORY_INIT_BARE
) != 0);
1765 bool is_reinit
= ((flags
& GIT_REPOSITORY_INIT__IS_REINIT
) != 0);
1768 if ((error
= repo_local_config(&config
, &cfg_path
, NULL
, repo_dir
)) < 0)
1771 if (is_reinit
&& (error
= check_repositoryformatversion(&version
, config
)) < 0)
1774 if ((error
= check_extensions(config
, version
)) < 0)
1777 #define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \
1778 if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
1779 goto cleanup; } while (0)
1781 SET_REPO_CONFIG(bool, "core.bare", is_bare
);
1782 SET_REPO_CONFIG(int32
, "core.repositoryformatversion", GIT_REPO_VERSION
);
1784 if ((error
= repo_init_fs_configs(
1785 config
, cfg_path
.ptr
, repo_dir
, work_dir
, !is_reinit
)) < 0)
1789 SET_REPO_CONFIG(bool, "core.logallrefupdates", true);
1791 if (!(flags
& GIT_REPOSITORY_INIT__NATURAL_WD
)) {
1792 if ((error
= git_buf_sets(&worktree_path
, work_dir
)) < 0)
1795 if ((flags
& GIT_REPOSITORY_INIT_RELATIVE_GITLINK
))
1796 if ((error
= git_path_make_relative(&worktree_path
, repo_dir
)) < 0)
1799 SET_REPO_CONFIG(string
, "core.worktree", worktree_path
.ptr
);
1800 } else if (is_reinit
) {
1801 if (git_config_delete_entry(config
, "core.worktree") < 0)
1806 if (mode
== GIT_REPOSITORY_INIT_SHARED_GROUP
) {
1807 SET_REPO_CONFIG(int32
, "core.sharedrepository", 1);
1808 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1810 else if (mode
== GIT_REPOSITORY_INIT_SHARED_ALL
) {
1811 SET_REPO_CONFIG(int32
, "core.sharedrepository", 2);
1812 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1816 git_buf_dispose(&cfg_path
);
1817 git_buf_dispose(&worktree_path
);
1818 git_config_free(config
);
1823 static int repo_reinit_submodule_fs(git_submodule
*sm
, const char *n
, void *p
)
1825 git_repository
*smrepo
= NULL
;
1826 GIT_UNUSED(n
); GIT_UNUSED(p
);
1828 if (git_submodule_open(&smrepo
, sm
) < 0 ||
1829 git_repository_reinit_filesystem(smrepo
, true) < 0)
1831 git_repository_free(smrepo
);
1836 int git_repository_reinit_filesystem(git_repository
*repo
, int recurse
)
1839 git_buf path
= GIT_BUF_INIT
;
1840 git_config
*config
= NULL
;
1841 const char *repo_dir
= git_repository_path(repo
);
1843 if (!(error
= repo_local_config(&config
, &path
, repo
, repo_dir
)))
1844 error
= repo_init_fs_configs(
1845 config
, path
.ptr
, repo_dir
, git_repository_workdir(repo
), true);
1847 git_config_free(config
);
1848 git_buf_dispose(&path
);
1850 git_repository__configmap_lookup_cache_clear(repo
);
1852 if (!repo
->is_bare
&& recurse
)
1853 (void)git_submodule_foreach(repo
, repo_reinit_submodule_fs
, NULL
);
1858 static int repo_write_template(
1859 const char *git_dir
,
1860 bool allow_overwrite
,
1864 const char *content
)
1866 git_buf path
= GIT_BUF_INIT
;
1867 int fd
, error
= 0, flags
;
1869 if (git_buf_joinpath(&path
, git_dir
, file
) < 0)
1872 if (allow_overwrite
)
1873 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
1875 flags
= O_WRONLY
| O_CREAT
| O_EXCL
;
1877 fd
= p_open(git_buf_cstr(&path
), flags
, mode
);
1880 error
= p_write(fd
, content
, strlen(content
));
1884 else if (errno
!= EEXIST
)
1888 if (!error
&& hidden
) {
1889 if (git_win32__set_hidden(path
.ptr
, true) < 0)
1896 git_buf_dispose(&path
);
1899 git_error_set(GIT_ERROR_OS
,
1900 "failed to initialize repository with template '%s'", file
);
1905 static int repo_write_gitlink(
1906 const char *in_dir
, const char *to_repo
, bool use_relative_path
)
1909 git_buf buf
= GIT_BUF_INIT
;
1910 git_buf path_to_repo
= GIT_BUF_INIT
;
1913 git_path_dirname_r(&buf
, to_repo
);
1914 git_path_to_dir(&buf
);
1915 if (git_buf_oom(&buf
))
1918 /* don't write gitlink to natural workdir */
1919 if (git__suffixcmp(to_repo
, "/" DOT_GIT
"/") == 0 &&
1920 strcmp(in_dir
, buf
.ptr
) == 0)
1922 error
= GIT_PASSTHROUGH
;
1926 if ((error
= git_buf_joinpath(&buf
, in_dir
, DOT_GIT
)) < 0)
1929 if (!p_stat(buf
.ptr
, &st
) && !S_ISREG(st
.st_mode
)) {
1930 git_error_set(GIT_ERROR_REPOSITORY
,
1931 "cannot overwrite gitlink file into path '%s'", in_dir
);
1932 error
= GIT_EEXISTS
;
1936 git_buf_clear(&buf
);
1938 error
= git_buf_sets(&path_to_repo
, to_repo
);
1940 if (!error
&& use_relative_path
)
1941 error
= git_path_make_relative(&path_to_repo
, in_dir
);
1944 error
= git_buf_join(&buf
, ' ', GIT_FILE_CONTENT_PREFIX
, path_to_repo
.ptr
);
1947 error
= repo_write_template(in_dir
, true, DOT_GIT
, 0666, true, buf
.ptr
);
1950 git_buf_dispose(&buf
);
1951 git_buf_dispose(&path_to_repo
);
1955 static mode_t
pick_dir_mode(git_repository_init_options
*opts
)
1957 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_UMASK
)
1959 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_GROUP
)
1960 return (0775 | S_ISGID
);
1961 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_ALL
)
1962 return (0777 | S_ISGID
);
1966 #include "repo_template.h"
1968 static int repo_init_structure(
1969 const char *repo_dir
,
1970 const char *work_dir
,
1971 git_repository_init_options
*opts
)
1974 repo_template_item
*tpl
;
1976 ((opts
->flags
& GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE
) != 0);
1977 mode_t dmode
= pick_dir_mode(opts
);
1978 bool chmod
= opts
->mode
!= GIT_REPOSITORY_INIT_SHARED_UMASK
;
1980 /* Hide the ".git" directory */
1982 if ((opts
->flags
& GIT_REPOSITORY_INIT__HAS_DOTGIT
) != 0) {
1983 if (git_win32__set_hidden(repo_dir
, true) < 0) {
1984 git_error_set(GIT_ERROR_OS
,
1985 "failed to mark Git repository folder as hidden");
1991 /* Create the .git gitlink if appropriate */
1992 if ((opts
->flags
& GIT_REPOSITORY_INIT_BARE
) == 0 &&
1993 (opts
->flags
& GIT_REPOSITORY_INIT__NATURAL_WD
) == 0)
1995 if (repo_write_gitlink(work_dir
, repo_dir
, opts
->flags
& GIT_REPOSITORY_INIT_RELATIVE_GITLINK
) < 0)
1999 /* Copy external template if requested */
2001 git_config
*cfg
= NULL
;
2002 const char *tdir
= NULL
;
2003 bool default_template
= false;
2004 git_buf template_buf
= GIT_BUF_INIT
;
2006 if (opts
->template_path
)
2007 tdir
= opts
->template_path
;
2008 else if ((error
= git_config_open_default(&cfg
)) >= 0) {
2009 if (!git_config_get_path(&template_buf
, cfg
, "init.templatedir"))
2010 tdir
= template_buf
.ptr
;
2015 if (!(error
= git_sysdir_find_template_dir(&template_buf
)))
2016 tdir
= template_buf
.ptr
;
2017 default_template
= true;
2021 * If tdir was the empty string, treat it like tdir was a path to an
2022 * empty directory (so, don't do any copying). This is the behavior
2023 * that git(1) exhibits, although it doesn't seem to be officially
2026 if (tdir
&& git__strcmp(tdir
, "") != 0) {
2027 uint32_t cpflags
= GIT_CPDIR_COPY_SYMLINKS
|
2028 GIT_CPDIR_SIMPLE_TO_MODE
|
2029 GIT_CPDIR_COPY_DOTFILES
;
2030 if (opts
->mode
!= GIT_REPOSITORY_INIT_SHARED_UMASK
)
2031 cpflags
|= GIT_CPDIR_CHMOD_DIRS
;
2032 error
= git_futils_cp_r(tdir
, repo_dir
, cpflags
, dmode
);
2035 git_buf_dispose(&template_buf
);
2036 git_config_free(cfg
);
2039 if (!default_template
)
2042 /* if template was default, ignore error and use internal */
2044 external_tpl
= false;
2049 /* Copy internal template
2050 * - always ensure existence of dirs
2051 * - only create files if no external template was specified
2053 for (tpl
= repo_template
; !error
&& tpl
->path
; ++tpl
) {
2054 if (!tpl
->content
) {
2055 uint32_t mkdir_flags
= GIT_MKDIR_PATH
;
2057 mkdir_flags
|= GIT_MKDIR_CHMOD
;
2059 error
= git_futils_mkdir_relative(
2060 tpl
->path
, repo_dir
, dmode
, mkdir_flags
, NULL
);
2062 else if (!external_tpl
) {
2063 const char *content
= tpl
->content
;
2065 if (opts
->description
&& strcmp(tpl
->path
, GIT_DESC_FILE
) == 0)
2066 content
= opts
->description
;
2068 error
= repo_write_template(
2069 repo_dir
, false, tpl
->path
, tpl
->mode
, false, content
);
2076 static int mkdir_parent(git_buf
*buf
, uint32_t mode
, bool skip2
)
2078 /* When making parent directories during repository initialization
2079 * don't try to set gid or grant world write access
2081 return git_futils_mkdir(
2082 buf
->ptr
, mode
& ~(S_ISGID
| 0002),
2083 GIT_MKDIR_PATH
| GIT_MKDIR_VERIFY_DIR
|
2084 (skip2
? GIT_MKDIR_SKIP_LAST2
: GIT_MKDIR_SKIP_LAST
));
2087 static int repo_init_directories(
2090 const char *given_repo
,
2091 git_repository_init_options
*opts
)
2094 bool is_bare
, add_dotgit
, has_dotgit
, natural_wd
;
2097 /* There are three possible rules for what we are allowed to create:
2098 * - MKPATH means anything we need
2099 * - MKDIR means just the .git directory and its parent and the workdir
2100 * - Neither means only the .git directory can be created
2102 * There are 5 "segments" of path that we might need to deal with:
2103 * 1. The .git directory
2104 * 2. The parent of the .git directory
2105 * 3. Everything above the parent of the .git directory
2106 * 4. The working directory (often the same as #2)
2107 * 5. Everything above the working directory (often the same as #3)
2109 * For all directories created, we start with the init_mode value for
2110 * permissions and then strip off bits in some cases:
2112 * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH
2113 * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID
2114 * For all rules, we create #1 using the untouched init_mode
2117 /* set up repo path */
2119 is_bare
= ((opts
->flags
& GIT_REPOSITORY_INIT_BARE
) != 0);
2122 (opts
->flags
& GIT_REPOSITORY_INIT_NO_DOTGIT_DIR
) == 0 &&
2124 git__suffixcmp(given_repo
, "/" DOT_GIT
) != 0 &&
2125 git__suffixcmp(given_repo
, "/" GIT_DIR
) != 0;
2127 if (git_buf_joinpath(repo_path
, given_repo
, add_dotgit
? GIT_DIR
: "") < 0)
2130 has_dotgit
= (git__suffixcmp(repo_path
->ptr
, "/" GIT_DIR
) == 0);
2132 opts
->flags
|= GIT_REPOSITORY_INIT__HAS_DOTGIT
;
2134 /* set up workdir path */
2137 if (opts
->workdir_path
) {
2138 if (git_path_join_unrooted(
2139 wd_path
, opts
->workdir_path
, repo_path
->ptr
, NULL
) < 0)
2141 } else if (has_dotgit
) {
2142 if (git_path_dirname_r(wd_path
, repo_path
->ptr
) < 0)
2145 git_error_set(GIT_ERROR_REPOSITORY
, "cannot pick working directory"
2146 " for non-bare repository that isn't a '.git' directory");
2150 if (git_path_to_dir(wd_path
) < 0)
2153 git_buf_clear(wd_path
);
2158 wd_path
->size
> 0 &&
2159 wd_path
->size
+ strlen(GIT_DIR
) == repo_path
->size
&&
2160 memcmp(repo_path
->ptr
, wd_path
->ptr
, wd_path
->size
) == 0;
2162 opts
->flags
|= GIT_REPOSITORY_INIT__NATURAL_WD
;
2164 /* create directories as needed / requested */
2166 dirmode
= pick_dir_mode(opts
);
2168 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0) {
2169 /* create path #5 */
2170 if (wd_path
->size
> 0 &&
2171 (error
= mkdir_parent(wd_path
, dirmode
, false)) < 0)
2174 /* create path #3 (if not the same as #5) */
2176 (error
= mkdir_parent(repo_path
, dirmode
, has_dotgit
)) < 0)
2180 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKDIR
) != 0 ||
2181 (opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0)
2183 /* create path #4 */
2184 if (wd_path
->size
> 0 &&
2185 (error
= git_futils_mkdir(
2186 wd_path
->ptr
, dirmode
& ~S_ISGID
,
2187 GIT_MKDIR_VERIFY_DIR
)) < 0)
2190 /* create path #2 (if not the same as #4) */
2192 (error
= git_futils_mkdir(
2193 repo_path
->ptr
, dirmode
& ~S_ISGID
,
2194 GIT_MKDIR_VERIFY_DIR
| GIT_MKDIR_SKIP_LAST
)) < 0)
2198 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKDIR
) != 0 ||
2199 (opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0 ||
2202 /* create path #1 */
2203 error
= git_futils_mkdir(repo_path
->ptr
, dirmode
,
2204 GIT_MKDIR_VERIFY_DIR
| ((dirmode
& S_ISGID
) ? GIT_MKDIR_CHMOD
: 0));
2207 /* prettify both directories now that they are created */
2210 error
= git_path_prettify_dir(repo_path
, repo_path
->ptr
, NULL
);
2212 if (!error
&& wd_path
->size
> 0)
2213 error
= git_path_prettify_dir(wd_path
, wd_path
->ptr
, NULL
);
2219 static int repo_init_head(const char *repo_dir
, const char *given
)
2221 git_config
*cfg
= NULL
;
2222 git_buf head_path
= GIT_BUF_INIT
, cfg_branch
= GIT_BUF_INIT
;
2223 const char *initial_head
= NULL
;
2226 if ((error
= git_buf_joinpath(&head_path
, repo_dir
, GIT_HEAD_FILE
)) < 0)
2230 * A template may have set a HEAD; use that unless it's been
2231 * overridden by the caller's given initial head setting.
2233 if (git_path_exists(head_path
.ptr
) && !given
)
2237 initial_head
= given
;
2238 } else if ((error
= git_config_open_default(&cfg
)) >= 0 &&
2239 (error
= git_config_get_string_buf(&cfg_branch
, cfg
, "init.defaultbranch")) >= 0 &&
2241 initial_head
= cfg_branch
.ptr
;
2245 initial_head
= GIT_BRANCH_DEFAULT
;
2247 error
= git_repository_create_head(repo_dir
, initial_head
);
2250 git_config_free(cfg
);
2251 git_buf_dispose(&head_path
);
2252 git_buf_dispose(&cfg_branch
);
2257 static int repo_init_create_origin(git_repository
*repo
, const char *url
)
2262 if (!(error
= git_remote_create(&remote
, repo
, GIT_REMOTE_ORIGIN
, url
))) {
2263 git_remote_free(remote
);
2269 int git_repository_init(
2270 git_repository
**repo_out
, const char *path
, unsigned is_bare
)
2272 git_repository_init_options opts
= GIT_REPOSITORY_INIT_OPTIONS_INIT
;
2274 opts
.flags
= GIT_REPOSITORY_INIT_MKPATH
; /* don't love this default */
2276 opts
.flags
|= GIT_REPOSITORY_INIT_BARE
;
2278 return git_repository_init_ext(repo_out
, path
, &opts
);
2281 int git_repository_init_ext(
2282 git_repository
**out
,
2283 const char *given_repo
,
2284 git_repository_init_options
*opts
)
2286 git_buf repo_path
= GIT_BUF_INIT
, wd_path
= GIT_BUF_INIT
,
2287 common_path
= GIT_BUF_INIT
;
2292 GIT_ASSERT_ARG(out
);
2293 GIT_ASSERT_ARG(given_repo
);
2294 GIT_ASSERT_ARG(opts
);
2296 GIT_ERROR_CHECK_VERSION(opts
, GIT_REPOSITORY_INIT_OPTIONS_VERSION
, "git_repository_init_options");
2298 if ((error
= repo_init_directories(&repo_path
, &wd_path
, given_repo
, opts
)) < 0)
2301 wd
= (opts
->flags
& GIT_REPOSITORY_INIT_BARE
) ? NULL
: git_buf_cstr(&wd_path
);
2303 if ((error
= is_valid_repository_path(&is_valid
, &repo_path
, &common_path
)) < 0)
2307 if ((opts
->flags
& GIT_REPOSITORY_INIT_NO_REINIT
) != 0) {
2308 git_error_set(GIT_ERROR_REPOSITORY
,
2309 "attempt to reinitialize '%s'", given_repo
);
2310 error
= GIT_EEXISTS
;
2314 opts
->flags
|= GIT_REPOSITORY_INIT__IS_REINIT
;
2316 if ((error
= repo_init_config(repo_path
.ptr
, wd
, opts
->flags
, opts
->mode
)) < 0)
2319 /* TODO: reinitialize the templates */
2321 if ((error
= repo_init_structure(repo_path
.ptr
, wd
, opts
)) < 0 ||
2322 (error
= repo_init_config(repo_path
.ptr
, wd
, opts
->flags
, opts
->mode
)) < 0 ||
2323 (error
= repo_init_head(repo_path
.ptr
, opts
->initial_head
)) < 0)
2327 if ((error
= git_repository_open(out
, repo_path
.ptr
)) < 0)
2330 if (opts
->origin_url
&&
2331 (error
= repo_init_create_origin(*out
, opts
->origin_url
)) < 0)
2335 git_buf_dispose(&common_path
);
2336 git_buf_dispose(&repo_path
);
2337 git_buf_dispose(&wd_path
);
2342 int git_repository_head_detached(git_repository
*repo
)
2345 git_odb
*odb
= NULL
;
2348 if (git_repository_odb__weakptr(&odb
, repo
) < 0)
2351 if (git_reference_lookup(&ref
, repo
, GIT_HEAD_FILE
) < 0)
2354 if (git_reference_type(ref
) == GIT_REFERENCE_SYMBOLIC
) {
2355 git_reference_free(ref
);
2359 exists
= git_odb_exists(odb
, git_reference_target(ref
));
2361 git_reference_free(ref
);
2365 int git_repository_head_detached_for_worktree(git_repository
*repo
, const char *name
)
2367 git_reference
*ref
= NULL
;
2370 GIT_ASSERT_ARG(repo
);
2371 GIT_ASSERT_ARG(name
);
2373 if ((error
= git_repository_head_for_worktree(&ref
, repo
, name
)) < 0)
2376 error
= (git_reference_type(ref
) != GIT_REFERENCE_SYMBOLIC
);
2378 git_reference_free(ref
);
2383 int git_repository_head(git_reference
**head_out
, git_repository
*repo
)
2385 git_reference
*head
;
2388 GIT_ASSERT_ARG(head_out
);
2390 if ((error
= git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
)) < 0)
2393 if (git_reference_type(head
) == GIT_REFERENCE_DIRECT
) {
2398 error
= git_reference_lookup_resolved(head_out
, repo
, git_reference_symbolic_target(head
), -1);
2399 git_reference_free(head
);
2401 return error
== GIT_ENOTFOUND
? GIT_EUNBORNBRANCH
: error
;
2404 int git_repository_head_for_worktree(git_reference
**out
, git_repository
*repo
, const char *name
)
2406 git_repository
*worktree_repo
= NULL
;
2407 git_worktree
*worktree
= NULL
;
2408 git_reference
*head
= NULL
;
2411 GIT_ASSERT_ARG(out
);
2412 GIT_ASSERT_ARG(repo
);
2413 GIT_ASSERT_ARG(name
);
2417 if ((error
= git_worktree_lookup(&worktree
, repo
, name
)) < 0 ||
2418 (error
= git_repository_open_from_worktree(&worktree_repo
, worktree
)) < 0 ||
2419 (error
= git_reference_lookup(&head
, worktree_repo
, GIT_HEAD_FILE
)) < 0)
2422 if (git_reference_type(head
) != GIT_REFERENCE_DIRECT
) {
2423 if ((error
= git_reference_lookup_resolved(out
, worktree_repo
, git_reference_symbolic_target(head
), -1)) < 0)
2431 git_reference_free(head
);
2432 git_worktree_free(worktree
);
2433 git_repository_free(worktree_repo
);
2437 int git_repository_foreach_worktree(git_repository
*repo
,
2438 git_repository_foreach_worktree_cb cb
,
2441 git_strarray worktrees
= {0};
2442 git_repository
*worktree_repo
= NULL
;
2443 git_worktree
*worktree
= NULL
;
2447 /* apply operation to repository supplied when commondir is empty, implying there's
2448 * no linked worktrees to iterate, which can occur when using custom odb/refdb
2450 if (!repo
->commondir
)
2451 return cb(repo
, payload
);
2453 if ((error
= git_repository_open(&worktree_repo
, repo
->commondir
)) < 0 ||
2454 (error
= cb(worktree_repo
, payload
) != 0))
2457 git_repository_free(worktree_repo
);
2458 worktree_repo
= NULL
;
2460 if ((error
= git_worktree_list(&worktrees
, repo
)) < 0)
2463 for (i
= 0; i
< worktrees
.count
; i
++) {
2464 git_repository_free(worktree_repo
);
2465 worktree_repo
= NULL
;
2466 git_worktree_free(worktree
);
2469 if ((error
= git_worktree_lookup(&worktree
, repo
, worktrees
.strings
[i
]) < 0) ||
2470 (error
= git_repository_open_from_worktree(&worktree_repo
, worktree
)) < 0) {
2471 if (error
!= GIT_ENOTFOUND
)
2477 if ((error
= cb(worktree_repo
, payload
)) != 0)
2482 git_strarray_dispose(&worktrees
);
2483 git_repository_free(worktree_repo
);
2484 git_worktree_free(worktree
);
2488 int git_repository_head_unborn(git_repository
*repo
)
2490 git_reference
*ref
= NULL
;
2493 error
= git_repository_head(&ref
, repo
);
2494 git_reference_free(ref
);
2496 if (error
== GIT_EUNBORNBRANCH
) {
2507 static int repo_contains_no_reference(git_repository
*repo
)
2509 git_reference_iterator
*iter
;
2510 const char *refname
;
2513 if ((error
= git_reference_iterator_new(&iter
, repo
)) < 0)
2516 error
= git_reference_next_name(&refname
, iter
);
2517 git_reference_iterator_free(iter
);
2519 if (error
== GIT_ITEROVER
)
2525 int git_repository_initialbranch(git_buf
*out
, git_repository
*repo
)
2528 git_config_entry
*entry
= NULL
;
2532 if ((error
= git_repository_config__weakptr(&config
, repo
)) < 0)
2535 if ((error
= git_config_get_entry(&entry
, config
, "init.defaultbranch")) == 0 &&
2537 branch
= entry
->value
;
2539 else if (!error
|| error
== GIT_ENOTFOUND
) {
2540 branch
= GIT_BRANCH_DEFAULT
;
2546 if ((error
= git_buf_puts(out
, GIT_REFS_HEADS_DIR
)) < 0 ||
2547 (error
= git_buf_puts(out
, branch
)) < 0 ||
2548 (error
= git_reference_name_is_valid(&valid
, out
->ptr
)) < 0)
2552 git_error_set(GIT_ERROR_INVALID
, "the value of init.defaultBranch is not a valid branch name");
2557 git_config_entry_free(entry
);
2561 int git_repository_is_empty(git_repository
*repo
)
2563 git_reference
*head
= NULL
;
2564 git_buf initialbranch
= GIT_BUF_INIT
;
2567 if ((result
= git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
)) < 0 ||
2568 (result
= git_repository_initialbranch(&initialbranch
, repo
)) < 0)
2571 result
= (git_reference_type(head
) == GIT_REFERENCE_SYMBOLIC
&&
2572 strcmp(git_reference_symbolic_target(head
), initialbranch
.ptr
) == 0 &&
2573 repo_contains_no_reference(repo
));
2576 git_reference_free(head
);
2577 git_buf_dispose(&initialbranch
);
2582 static const char *resolved_parent_path(const git_repository
*repo
, git_repository_item_t item
, git_repository_item_t fallback
)
2587 case GIT_REPOSITORY_ITEM_GITDIR
:
2588 parent
= git_repository_path(repo
);
2590 case GIT_REPOSITORY_ITEM_WORKDIR
:
2591 parent
= git_repository_workdir(repo
);
2593 case GIT_REPOSITORY_ITEM_COMMONDIR
:
2594 parent
= git_repository_commondir(repo
);
2597 git_error_set(GIT_ERROR_INVALID
, "invalid item directory");
2600 if (!parent
&& fallback
!= GIT_REPOSITORY_ITEM__LAST
)
2601 return resolved_parent_path(repo
, fallback
, GIT_REPOSITORY_ITEM__LAST
);
2606 int git_repository_item_path(git_buf
*out
, const git_repository
*repo
, git_repository_item_t item
)
2608 const char *parent
= resolved_parent_path(repo
, items
[item
].parent
, items
[item
].fallback
);
2609 if (parent
== NULL
) {
2610 git_error_set(GIT_ERROR_INVALID
, "path cannot exist in repository");
2611 return GIT_ENOTFOUND
;
2614 if (git_buf_sets(out
, parent
) < 0)
2617 if (items
[item
].name
) {
2618 if (git_buf_joinpath(out
, parent
, items
[item
].name
) < 0)
2622 if (items
[item
].directory
) {
2623 if (git_path_to_dir(out
) < 0)
2630 const char *git_repository_path(const git_repository
*repo
)
2632 GIT_ASSERT_ARG_WITH_RETVAL(repo
, NULL
);
2633 return repo
->gitdir
;
2636 const char *git_repository_workdir(const git_repository
*repo
)
2638 GIT_ASSERT_ARG_WITH_RETVAL(repo
, NULL
);
2643 return repo
->workdir
;
2646 int git_repository_workdir_path(
2647 git_buf
*out
, git_repository
*repo
, const char *path
)
2651 if (!repo
->workdir
) {
2652 git_error_set(GIT_ERROR_REPOSITORY
, "repository has no working directory");
2653 return GIT_EBAREREPO
;
2656 if (!(error
= git_buf_joinpath(out
, repo
->workdir
, path
)))
2657 error
= git_path_validate_workdir_buf(repo
, out
);
2662 const char *git_repository_commondir(const git_repository
*repo
)
2664 GIT_ASSERT_ARG_WITH_RETVAL(repo
, NULL
);
2665 return repo
->commondir
;
2668 int git_repository_set_workdir(
2669 git_repository
*repo
, const char *workdir
, int update_gitlink
)
2672 git_buf path
= GIT_BUF_INIT
;
2674 GIT_ASSERT_ARG(repo
);
2675 GIT_ASSERT_ARG(workdir
);
2677 if (git_path_prettify_dir(&path
, workdir
, NULL
) < 0)
2680 if (repo
->workdir
&& strcmp(repo
->workdir
, path
.ptr
) == 0)
2683 if (update_gitlink
) {
2686 if (git_repository_config__weakptr(&config
, repo
) < 0)
2689 error
= repo_write_gitlink(path
.ptr
, git_repository_path(repo
), false);
2691 /* passthrough error means gitlink is unnecessary */
2692 if (error
== GIT_PASSTHROUGH
)
2693 error
= git_config_delete_entry(config
, "core.worktree");
2695 error
= git_config_set_string(config
, "core.worktree", path
.ptr
);
2698 error
= git_config_set_bool(config
, "core.bare", false);
2702 char *old_workdir
= repo
->workdir
;
2704 repo
->workdir
= git_buf_detach(&path
);
2707 git__free(old_workdir
);
2713 int git_repository_is_bare(const git_repository
*repo
)
2715 GIT_ASSERT_ARG(repo
);
2716 return repo
->is_bare
;
2719 int git_repository_is_worktree(const git_repository
*repo
)
2721 GIT_ASSERT_ARG(repo
);
2722 return repo
->is_worktree
;
2725 int git_repository_set_bare(git_repository
*repo
)
2730 GIT_ASSERT_ARG(repo
);
2735 if ((error
= git_repository_config__weakptr(&config
, repo
)) < 0)
2738 if ((error
= git_config_set_bool(config
, "core.bare", true)) < 0)
2741 if ((error
= git_config__update_entry(config
, "core.worktree", NULL
, true, true)) < 0)
2744 git__free(repo
->workdir
);
2745 repo
->workdir
= NULL
;
2751 int git_repository_head_tree(git_tree
**tree
, git_repository
*repo
)
2753 git_reference
*head
;
2757 if ((error
= git_repository_head(&head
, repo
)) < 0)
2760 if ((error
= git_reference_peel(&obj
, head
, GIT_OBJECT_TREE
)) < 0)
2763 *tree
= (git_tree
*)obj
;
2766 git_reference_free(head
);
2770 int git_repository__set_orig_head(git_repository
*repo
, const git_oid
*orig_head
)
2772 git_filebuf file
= GIT_FILEBUF_INIT
;
2773 git_buf file_path
= GIT_BUF_INIT
;
2774 char orig_head_str
[GIT_OID_HEXSZ
];
2777 git_oid_fmt(orig_head_str
, orig_head
);
2779 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_ORIG_HEAD_FILE
)) == 0 &&
2780 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) == 0 &&
2781 (error
= git_filebuf_printf(&file
, "%.*s\n", GIT_OID_HEXSZ
, orig_head_str
)) == 0)
2782 error
= git_filebuf_commit(&file
);
2785 git_filebuf_cleanup(&file
);
2787 git_buf_dispose(&file_path
);
2792 int git_repository_message(git_buf
*out
, git_repository
*repo
)
2794 git_buf path
= GIT_BUF_INIT
;
2798 if ((error
= git_buf_sanitize(out
)) < 0)
2801 if (git_buf_joinpath(&path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
) < 0)
2804 if ((error
= p_stat(git_buf_cstr(&path
), &st
)) < 0) {
2805 if (errno
== ENOENT
)
2806 error
= GIT_ENOTFOUND
;
2807 git_error_set(GIT_ERROR_OS
, "could not access message file");
2809 error
= git_futils_readbuffer(out
, git_buf_cstr(&path
));
2812 git_buf_dispose(&path
);
2817 int git_repository_message_remove(git_repository
*repo
)
2819 git_buf path
= GIT_BUF_INIT
;
2822 if (git_buf_joinpath(&path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
) < 0)
2825 error
= p_unlink(git_buf_cstr(&path
));
2826 git_buf_dispose(&path
);
2831 int git_repository_hashfile(
2833 git_repository
*repo
,
2836 const char *as_path
)
2839 git_filter_list
*fl
= NULL
;
2842 git_buf full_path
= GIT_BUF_INIT
;
2843 const char *workdir
= git_repository_workdir(repo
);
2845 /* as_path can be NULL */
2846 GIT_ASSERT_ARG(out
);
2847 GIT_ASSERT_ARG(path
);
2848 GIT_ASSERT_ARG(repo
);
2850 if ((error
= git_path_join_unrooted(&full_path
, path
, workdir
, NULL
)) < 0 ||
2851 (error
= git_path_validate_workdir_buf(repo
, &full_path
)) < 0)
2855 * NULL as_path means that we should derive it from the
2859 if (workdir
&& !git__prefixcmp(full_path
.ptr
, workdir
))
2860 as_path
= full_path
.ptr
+ strlen(workdir
);
2865 /* passing empty string for "as_path" indicated --no-filters */
2866 if (strlen(as_path
) > 0) {
2867 error
= git_filter_list_load(
2868 &fl
, repo
, NULL
, as_path
,
2869 GIT_FILTER_TO_ODB
, GIT_FILTER_DEFAULT
);
2875 /* at this point, error is a count of the number of loaded filters */
2877 fd
= git_futils_open_ro(full_path
.ptr
);
2883 if ((error
= git_futils_filesize(&len
, fd
)) < 0)
2886 if (!git__is_sizet(len
)) {
2887 git_error_set(GIT_ERROR_OS
, "file size overflow for 32-bit systems");
2892 error
= git_odb__hashfd_filtered(out
, fd
, (size_t)len
, type
, fl
);
2897 git_filter_list_free(fl
);
2898 git_buf_dispose(&full_path
);
2903 static int checkout_message(git_buf
*out
, git_reference
*old
, const char *new)
2905 git_buf_puts(out
, "checkout: moving from ");
2907 if (git_reference_type(old
) == GIT_REFERENCE_SYMBOLIC
)
2908 git_buf_puts(out
, git_reference__shorthand(git_reference_symbolic_target(old
)));
2910 git_buf_puts(out
, git_oid_tostr_s(git_reference_target(old
)));
2912 git_buf_puts(out
, " to ");
2914 if (git_reference__is_branch(new) ||
2915 git_reference__is_tag(new) ||
2916 git_reference__is_remote(new))
2917 git_buf_puts(out
, git_reference__shorthand(new));
2919 git_buf_puts(out
, new);
2921 if (git_buf_oom(out
))
2927 static int detach(git_repository
*repo
, const git_oid
*id
, const char *new)
2930 git_buf log_message
= GIT_BUF_INIT
;
2931 git_object
*object
= NULL
, *peeled
= NULL
;
2932 git_reference
*new_head
= NULL
, *current
= NULL
;
2934 GIT_ASSERT_ARG(repo
);
2937 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2940 if ((error
= git_object_lookup(&object
, repo
, id
, GIT_OBJECT_ANY
)) < 0)
2943 if ((error
= git_object_peel(&peeled
, object
, GIT_OBJECT_COMMIT
)) < 0)
2947 new = git_oid_tostr_s(git_object_id(peeled
));
2949 if ((error
= checkout_message(&log_message
, current
, new)) < 0)
2952 error
= git_reference_create(&new_head
, repo
, GIT_HEAD_FILE
, git_object_id(peeled
), true, git_buf_cstr(&log_message
));
2955 git_buf_dispose(&log_message
);
2956 git_object_free(object
);
2957 git_object_free(peeled
);
2958 git_reference_free(current
);
2959 git_reference_free(new_head
);
2963 int git_repository_set_head(
2964 git_repository
*repo
,
2965 const char *refname
)
2967 git_reference
*ref
= NULL
, *current
= NULL
, *new_head
= NULL
;
2968 git_buf log_message
= GIT_BUF_INIT
;
2971 GIT_ASSERT_ARG(repo
);
2972 GIT_ASSERT_ARG(refname
);
2974 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2977 if ((error
= checkout_message(&log_message
, current
, refname
)) < 0)
2980 error
= git_reference_lookup(&ref
, repo
, refname
);
2981 if (error
< 0 && error
!= GIT_ENOTFOUND
)
2984 if (ref
&& current
->type
== GIT_REFERENCE_SYMBOLIC
&& git__strcmp(current
->target
.symbolic
, ref
->name
) &&
2985 git_reference_is_branch(ref
) && git_branch_is_checked_out(ref
)) {
2986 git_error_set(GIT_ERROR_REPOSITORY
, "cannot set HEAD to reference '%s' as it is the current HEAD "
2987 "of a linked repository.", git_reference_name(ref
));
2993 if (git_reference_is_branch(ref
)) {
2994 error
= git_reference_symbolic_create(&new_head
, repo
, GIT_HEAD_FILE
,
2995 git_reference_name(ref
), true, git_buf_cstr(&log_message
));
2997 error
= detach(repo
, git_reference_target(ref
),
2998 git_reference_is_tag(ref
) || git_reference_is_remote(ref
) ? refname
: NULL
);
3000 } else if (git_reference__is_branch(refname
)) {
3001 error
= git_reference_symbolic_create(&new_head
, repo
, GIT_HEAD_FILE
, refname
,
3002 true, git_buf_cstr(&log_message
));
3006 git_buf_dispose(&log_message
);
3007 git_reference_free(current
);
3008 git_reference_free(ref
);
3009 git_reference_free(new_head
);
3013 int git_repository_set_head_detached(
3014 git_repository
*repo
,
3015 const git_oid
*commitish
)
3017 return detach(repo
, commitish
, NULL
);
3020 int git_repository_set_head_detached_from_annotated(
3021 git_repository
*repo
,
3022 const git_annotated_commit
*commitish
)
3024 GIT_ASSERT_ARG(repo
);
3025 GIT_ASSERT_ARG(commitish
);
3027 return detach(repo
, git_annotated_commit_id(commitish
), commitish
->description
);
3030 int git_repository_detach_head(git_repository
*repo
)
3032 git_reference
*old_head
= NULL
, *new_head
= NULL
, *current
= NULL
;
3033 git_object
*object
= NULL
;
3034 git_buf log_message
= GIT_BUF_INIT
;
3037 GIT_ASSERT_ARG(repo
);
3039 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
3042 if ((error
= git_repository_head(&old_head
, repo
)) < 0)
3045 if ((error
= git_object_lookup(&object
, repo
, git_reference_target(old_head
), GIT_OBJECT_COMMIT
)) < 0)
3048 if ((error
= checkout_message(&log_message
, current
, git_oid_tostr_s(git_object_id(object
)))) < 0)
3051 error
= git_reference_create(&new_head
, repo
, GIT_HEAD_FILE
, git_reference_target(old_head
),
3052 1, git_buf_cstr(&log_message
));
3055 git_buf_dispose(&log_message
);
3056 git_object_free(object
);
3057 git_reference_free(old_head
);
3058 git_reference_free(new_head
);
3059 git_reference_free(current
);
3064 * Loosely ported from git.git
3065 * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289
3067 int git_repository_state(git_repository
*repo
)
3069 git_buf repo_path
= GIT_BUF_INIT
;
3070 int state
= GIT_REPOSITORY_STATE_NONE
;
3072 GIT_ASSERT_ARG(repo
);
3074 if (git_buf_puts(&repo_path
, repo
->gitdir
) < 0)
3077 if (git_path_contains_file(&repo_path
, GIT_REBASE_MERGE_INTERACTIVE_FILE
))
3078 state
= GIT_REPOSITORY_STATE_REBASE_INTERACTIVE
;
3079 else if (git_path_contains_dir(&repo_path
, GIT_REBASE_MERGE_DIR
))
3080 state
= GIT_REPOSITORY_STATE_REBASE_MERGE
;
3081 else if (git_path_contains_file(&repo_path
, GIT_REBASE_APPLY_REBASING_FILE
))
3082 state
= GIT_REPOSITORY_STATE_REBASE
;
3083 else if (git_path_contains_file(&repo_path
, GIT_REBASE_APPLY_APPLYING_FILE
))
3084 state
= GIT_REPOSITORY_STATE_APPLY_MAILBOX
;
3085 else if (git_path_contains_dir(&repo_path
, GIT_REBASE_APPLY_DIR
))
3086 state
= GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE
;
3087 else if (git_path_contains_file(&repo_path
, GIT_MERGE_HEAD_FILE
))
3088 state
= GIT_REPOSITORY_STATE_MERGE
;
3089 else if (git_path_contains_file(&repo_path
, GIT_REVERT_HEAD_FILE
)) {
3090 state
= GIT_REPOSITORY_STATE_REVERT
;
3091 if (git_path_contains_file(&repo_path
, GIT_SEQUENCER_TODO_FILE
)) {
3092 state
= GIT_REPOSITORY_STATE_REVERT_SEQUENCE
;
3094 } else if (git_path_contains_file(&repo_path
, GIT_CHERRYPICK_HEAD_FILE
)) {
3095 state
= GIT_REPOSITORY_STATE_CHERRYPICK
;
3096 if (git_path_contains_file(&repo_path
, GIT_SEQUENCER_TODO_FILE
)) {
3097 state
= GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE
;
3099 } else if (git_path_contains_file(&repo_path
, GIT_BISECT_LOG_FILE
))
3100 state
= GIT_REPOSITORY_STATE_BISECT
;
3102 git_buf_dispose(&repo_path
);
3106 int git_repository__cleanup_files(
3107 git_repository
*repo
, const char *files
[], size_t files_len
)
3109 git_buf buf
= GIT_BUF_INIT
;
3113 for (error
= 0, i
= 0; !error
&& i
< files_len
; ++i
) {
3116 if (git_buf_joinpath(&buf
, repo
->gitdir
, files
[i
]) < 0)
3119 path
= git_buf_cstr(&buf
);
3121 if (git_path_isfile(path
)) {
3122 error
= p_unlink(path
);
3123 } else if (git_path_isdir(path
)) {
3124 error
= git_futils_rmdir_r(path
, NULL
,
3125 GIT_RMDIR_REMOVE_FILES
| GIT_RMDIR_REMOVE_BLOCKERS
);
3128 git_buf_clear(&buf
);
3131 git_buf_dispose(&buf
);
3135 static const char *state_files
[] = {
3136 GIT_MERGE_HEAD_FILE
,
3137 GIT_MERGE_MODE_FILE
,
3139 GIT_REVERT_HEAD_FILE
,
3140 GIT_CHERRYPICK_HEAD_FILE
,
3141 GIT_BISECT_LOG_FILE
,
3142 GIT_REBASE_MERGE_DIR
,
3143 GIT_REBASE_APPLY_DIR
,
3147 int git_repository_state_cleanup(git_repository
*repo
)
3149 GIT_ASSERT_ARG(repo
);
3151 return git_repository__cleanup_files(repo
, state_files
, ARRAY_SIZE(state_files
));
3154 int git_repository_is_shallow(git_repository
*repo
)
3156 git_buf path
= GIT_BUF_INIT
;
3160 if ((error
= git_buf_joinpath(&path
, repo
->gitdir
, "shallow")) < 0)
3163 error
= git_path_lstat(path
.ptr
, &st
);
3164 git_buf_dispose(&path
);
3166 if (error
== GIT_ENOTFOUND
) {
3173 return st
.st_size
== 0 ? 0 : 1;
3176 int git_repository_init_options_init(
3177 git_repository_init_options
*opts
, unsigned int version
)
3179 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3180 opts
, version
, git_repository_init_options
,
3181 GIT_REPOSITORY_INIT_OPTIONS_INIT
);
3185 #ifndef GIT_DEPRECATE_HARD
3186 int git_repository_init_init_options(
3187 git_repository_init_options
*opts
, unsigned int version
)
3189 return git_repository_init_options_init(opts
, version
);
3193 int git_repository_ident(const char **name
, const char **email
, const git_repository
*repo
)
3195 *name
= repo
->ident_name
;
3196 *email
= repo
->ident_email
;
3201 int git_repository_set_ident(git_repository
*repo
, const char *name
, const char *email
)
3203 char *tmp_name
= NULL
, *tmp_email
= NULL
;
3206 tmp_name
= git__strdup(name
);
3207 GIT_ERROR_CHECK_ALLOC(tmp_name
);
3211 tmp_email
= git__strdup(email
);
3212 GIT_ERROR_CHECK_ALLOC(tmp_email
);
3215 tmp_name
= git_atomic_swap(repo
->ident_name
, tmp_name
);
3216 tmp_email
= git_atomic_swap(repo
->ident_email
, tmp_email
);
3218 git__free(tmp_name
);
3219 git__free(tmp_email
);
3224 int git_repository_submodule_cache_all(git_repository
*repo
)
3226 GIT_ASSERT_ARG(repo
);
3227 return git_submodule_cache_init(&repo
->submodule_cache
, repo
);
3230 int git_repository_submodule_cache_clear(git_repository
*repo
)
3233 GIT_ASSERT_ARG(repo
);
3235 error
= git_submodule_cache_free(repo
->submodule_cache
);
3236 repo
->submodule_cache
= NULL
;