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.
9 #include "git2/object.h"
10 #include "git2/refdb.h"
11 #include "git2/sys/repository.h"
14 #include "repository.h"
28 #include "diff_driver.h"
29 #include "annotated_commit.h"
30 #include "submodule.h"
36 # include "win32/w32_util.h"
40 git_repository_item_t parent
;
44 { GIT_REPOSITORY_ITEM_GITDIR
, NULL
, true },
45 { GIT_REPOSITORY_ITEM_WORKDIR
, NULL
, true },
46 { GIT_REPOSITORY_ITEM_COMMONDIR
, NULL
, true },
47 { GIT_REPOSITORY_ITEM_GITDIR
, "index", false },
48 { GIT_REPOSITORY_ITEM_COMMONDIR
, "objects", true },
49 { GIT_REPOSITORY_ITEM_COMMONDIR
, "refs", true },
50 { GIT_REPOSITORY_ITEM_COMMONDIR
, "packed-refs", false },
51 { GIT_REPOSITORY_ITEM_COMMONDIR
, "remotes", true },
52 { GIT_REPOSITORY_ITEM_COMMONDIR
, "config", false },
53 { GIT_REPOSITORY_ITEM_COMMONDIR
, "info", true },
54 { GIT_REPOSITORY_ITEM_COMMONDIR
, "hooks", true },
55 { GIT_REPOSITORY_ITEM_COMMONDIR
, "logs", true },
56 { GIT_REPOSITORY_ITEM_GITDIR
, "modules", true },
57 { GIT_REPOSITORY_ITEM_COMMONDIR
, "worktrees", true }
60 static int check_repositoryformatversion(git_config
*config
);
62 #define GIT_COMMONDIR_FILE "commondir"
63 #define GIT_GITDIR_FILE "gitdir"
65 #define GIT_FILE_CONTENT_PREFIX "gitdir:"
67 #define GIT_BRANCH_MASTER "master"
69 #define GIT_REPO_VERSION 0
71 git_buf git_repository__reserved_names_win32
[] = {
72 { DOT_GIT
, 0, CONST_STRLEN(DOT_GIT
) },
73 { GIT_DIR_SHORTNAME
, 0, CONST_STRLEN(GIT_DIR_SHORTNAME
) }
75 size_t git_repository__reserved_names_win32_len
= 2;
77 git_buf git_repository__reserved_names_posix
[] = {
78 { DOT_GIT
, 0, CONST_STRLEN(DOT_GIT
) },
80 size_t git_repository__reserved_names_posix_len
= 1;
82 static void set_odb(git_repository
*repo
, git_odb
*odb
)
85 GIT_REFCOUNT_OWN(odb
, repo
);
86 GIT_REFCOUNT_INC(odb
);
89 if ((odb
= git__swap(repo
->_odb
, odb
)) != NULL
) {
90 GIT_REFCOUNT_OWN(odb
, NULL
);
95 static void set_refdb(git_repository
*repo
, git_refdb
*refdb
)
98 GIT_REFCOUNT_OWN(refdb
, repo
);
99 GIT_REFCOUNT_INC(refdb
);
102 if ((refdb
= git__swap(repo
->_refdb
, refdb
)) != NULL
) {
103 GIT_REFCOUNT_OWN(refdb
, NULL
);
104 git_refdb_free(refdb
);
108 static void set_config(git_repository
*repo
, git_config
*config
)
111 GIT_REFCOUNT_OWN(config
, repo
);
112 GIT_REFCOUNT_INC(config
);
115 if ((config
= git__swap(repo
->_config
, config
)) != NULL
) {
116 GIT_REFCOUNT_OWN(config
, NULL
);
117 git_config_free(config
);
120 git_repository__cvar_cache_clear(repo
);
123 static void set_index(git_repository
*repo
, git_index
*index
)
126 GIT_REFCOUNT_OWN(index
, repo
);
127 GIT_REFCOUNT_INC(index
);
130 if ((index
= git__swap(repo
->_index
, index
)) != NULL
) {
131 GIT_REFCOUNT_OWN(index
, NULL
);
132 git_index_free(index
);
136 void git_repository__cleanup(git_repository
*repo
)
140 git_repository_submodule_cache_clear(repo
);
141 git_cache_clear(&repo
->objects
);
142 git_attr_cache_flush(repo
);
144 set_config(repo
, NULL
);
145 set_index(repo
, NULL
);
147 set_refdb(repo
, NULL
);
150 void git_repository_free(git_repository
*repo
)
157 git_repository__cleanup(repo
);
159 git_cache_free(&repo
->objects
);
161 git_diff_driver_registry_free(repo
->diff_drivers
);
162 repo
->diff_drivers
= NULL
;
164 for (i
= 0; i
< repo
->reserved_names
.size
; i
++)
165 git_buf_free(git_array_get(repo
->reserved_names
, i
));
166 git_array_clear(repo
->reserved_names
);
168 git__free(repo
->gitlink
);
169 git__free(repo
->gitdir
);
170 git__free(repo
->commondir
);
171 git__free(repo
->workdir
);
172 git__free(repo
->namespace);
173 git__free(repo
->ident_name
);
174 git__free(repo
->ident_email
);
176 git__memzero(repo
, sizeof(*repo
));
181 * Git repository open methods
183 * Open a repository object from its path
185 static bool valid_repository_path(git_buf
*repository_path
, git_buf
*common_path
)
187 /* Check if we have a separate commondir (e.g. we have a
189 if (git_path_contains_file(repository_path
, GIT_COMMONDIR_FILE
)) {
190 git_buf common_link
= GIT_BUF_INIT
;
191 git_buf_joinpath(&common_link
, repository_path
->ptr
, GIT_COMMONDIR_FILE
);
193 git_futils_readbuffer(&common_link
, common_link
.ptr
);
194 git_buf_rtrim(&common_link
);
196 if (git_path_is_relative(common_link
.ptr
)) {
197 git_buf_joinpath(common_path
, repository_path
->ptr
, common_link
.ptr
);
199 git_buf_swap(common_path
, &common_link
);
202 git_buf_free(&common_link
);
205 git_buf_set(common_path
, repository_path
->ptr
, repository_path
->size
);
208 /* Make sure the commondir path always has a trailing * slash */
209 if (git_buf_rfind(common_path
, '/') != (ssize_t
)common_path
->size
- 1)
210 git_buf_putc(common_path
, '/');
212 /* Ensure HEAD file exists */
213 if (git_path_contains_file(repository_path
, GIT_HEAD_FILE
) == false)
216 /* Check files in common dir */
217 if (git_path_contains_dir(common_path
, GIT_OBJECTS_DIR
) == false)
219 if (git_path_contains_dir(common_path
, GIT_REFS_DIR
) == false)
225 static git_repository
*repository_alloc(void)
227 git_repository
*repo
= git__calloc(1, sizeof(git_repository
));
230 git_cache_init(&repo
->objects
) < 0)
233 git_array_init_to_size(repo
->reserved_names
, 4);
234 if (!repo
->reserved_names
.ptr
)
237 /* set all the entries in the cvar cache to `unset` */
238 git_repository__cvar_cache_clear(repo
);
244 git_cache_free(&repo
->objects
);
250 int git_repository_new(git_repository
**out
)
252 git_repository
*repo
;
254 *out
= repo
= repository_alloc();
255 GITERR_CHECK_ALLOC(repo
);
258 repo
->is_worktree
= 0;
263 static int load_config_data(git_repository
*repo
, const git_config
*config
)
267 /* Try to figure out if it's bare, default to non-bare if it's not set */
268 if (git_config_get_bool(&is_bare
, config
, "core.bare") < 0)
271 repo
->is_bare
= is_bare
;
276 static int load_workdir(git_repository
*repo
, git_config
*config
, git_buf
*parent_path
)
279 git_config_entry
*ce
;
280 git_buf worktree
= GIT_BUF_INIT
;
281 git_buf path
= GIT_BUF_INIT
;
286 if ((error
= git_config__lookup_entry(
287 &ce
, config
, "core.worktree", false)) < 0)
290 if (repo
->is_worktree
) {
291 char *gitlink
= git_worktree__read_link(repo
->gitdir
, GIT_GITDIR_FILE
);
297 git_buf_attach(&worktree
, gitlink
, 0);
299 if ((git_path_dirname_r(&worktree
, worktree
.ptr
)) < 0 ||
300 git_path_to_dir(&worktree
) < 0) {
305 repo
->workdir
= git_buf_detach(&worktree
);
307 else if (ce
&& ce
->value
) {
308 if ((error
= git_path_prettify_dir(
309 &worktree
, ce
->value
, repo
->gitdir
)) < 0)
312 repo
->workdir
= git_buf_detach(&worktree
);
314 else if (parent_path
&& git_path_isdir(parent_path
->ptr
))
315 repo
->workdir
= git_buf_detach(parent_path
);
317 if (git_path_dirname_r(&worktree
, repo
->gitdir
) < 0 ||
318 git_path_to_dir(&worktree
) < 0) {
323 repo
->workdir
= git_buf_detach(&worktree
);
326 GITERR_CHECK_ALLOC(repo
->workdir
);
329 git_config_entry_free(ce
);
334 * This function returns furthest offset into path where a ceiling dir
335 * is found, so we can stop processing the path at that point.
337 * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on
338 * the stack could remove directories name limits, but at the cost of doing
339 * repeated malloc/frees inside the loop below, so let's not do it now.
341 static size_t find_ceiling_dir_offset(
343 const char *ceiling_directories
)
345 char buf
[GIT_PATH_MAX
+ 1];
346 char buf2
[GIT_PATH_MAX
+ 1];
347 const char *ceil
, *sep
;
348 size_t len
, max_len
= 0, min_len
;
352 min_len
= (size_t)(git_path_root(path
) + 1);
354 if (ceiling_directories
== NULL
|| min_len
== 0)
357 for (sep
= ceil
= ceiling_directories
; *sep
; ceil
= sep
+ 1) {
358 for (sep
= ceil
; *sep
&& *sep
!= GIT_PATH_LIST_SEPARATOR
; sep
++);
361 if (len
== 0 || len
>= sizeof(buf
) || git_path_root(ceil
) == -1)
364 strncpy(buf
, ceil
, len
);
367 if (p_realpath(buf
, buf2
) == NULL
)
371 if (len
> 0 && buf2
[len
-1] == '/')
374 if (!strncmp(path
, buf2
, len
) &&
375 (path
[len
] == '/' || !path
[len
]) &&
382 return (max_len
<= min_len
? min_len
: max_len
);
386 * Read the contents of `file_path` and set `path_out` to the repo dir that
387 * it points to. Before calling, set `path_out` to the base directory that
388 * should be used if the contents of `file_path` are a relative path.
390 static int read_gitfile(git_buf
*path_out
, const char *file_path
)
393 git_buf file
= GIT_BUF_INIT
;
394 size_t prefix_len
= strlen(GIT_FILE_CONTENT_PREFIX
);
396 assert(path_out
&& file_path
);
398 if (git_futils_readbuffer(&file
, file_path
) < 0)
401 git_buf_rtrim(&file
);
402 /* apparently on Windows, some people use backslashes in paths */
403 git_path_mkposix(file
.ptr
);
405 if (git_buf_len(&file
) <= prefix_len
||
406 memcmp(git_buf_cstr(&file
), GIT_FILE_CONTENT_PREFIX
, prefix_len
) != 0)
408 giterr_set(GITERR_REPOSITORY
,
409 "the `.git` file at '%s' is malformed", file_path
);
412 else if ((error
= git_path_dirname_r(path_out
, file_path
)) >= 0) {
413 const char *gitlink
= git_buf_cstr(&file
) + prefix_len
;
414 while (*gitlink
&& git__isspace(*gitlink
)) gitlink
++;
416 error
= git_path_prettify_dir(
417 path_out
, gitlink
, git_buf_cstr(path_out
));
424 static int find_repo(
426 git_buf
*parent_path
,
428 git_buf
*common_path
,
429 const char *start_path
,
431 const char *ceiling_dirs
)
434 git_buf path
= GIT_BUF_INIT
;
435 git_buf repo_link
= GIT_BUF_INIT
;
436 git_buf common_link
= GIT_BUF_INIT
;
438 dev_t initial_device
= 0;
441 size_t ceiling_offset
= 0;
443 git_buf_free(repo_path
);
445 error
= git_path_prettify(&path
, start_path
, NULL
);
449 /* in_dot_git toggles each loop:
450 * /a/b/c/.git, /a/b/c, /a/b/.git, /a/b, /a/.git, /a
451 * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, we
452 * assume we started with /a/b/c.git and don't append .git the first
454 * min_iterations indicates the number of iterations left before going
455 * further counts as a search. */
456 if (flags
& (GIT_REPOSITORY_OPEN_BARE
| GIT_REPOSITORY_OPEN_NO_DOTGIT
)) {
465 if (!(flags
& GIT_REPOSITORY_OPEN_NO_DOTGIT
)) {
467 error
= git_buf_joinpath(&path
, path
.ptr
, DOT_GIT
);
471 in_dot_git
= !in_dot_git
;
474 if (p_stat(path
.ptr
, &st
) == 0) {
475 /* check that we have not crossed device boundaries */
476 if (initial_device
== 0)
477 initial_device
= st
.st_dev
;
478 else if (st
.st_dev
!= initial_device
&&
479 !(flags
& GIT_REPOSITORY_OPEN_CROSS_FS
))
482 if (S_ISDIR(st
.st_mode
)) {
483 if (valid_repository_path(&path
, &common_link
)) {
484 git_path_to_dir(&path
);
485 git_buf_set(repo_path
, path
.ptr
, path
.size
);
488 git_buf_attach(link_path
,
489 git_worktree__read_link(path
.ptr
, GIT_GITDIR_FILE
), 0);
491 git_buf_swap(&common_link
, common_path
);
496 else if (S_ISREG(st
.st_mode
) && git__suffixcmp(path
.ptr
, "/" DOT_GIT
) == 0) {
497 error
= read_gitfile(&repo_link
, path
.ptr
);
500 if (valid_repository_path(&repo_link
, &common_link
)) {
501 git_buf_swap(repo_path
, &repo_link
);
504 error
= git_buf_put(link_path
, path
.ptr
, path
.size
);
506 git_buf_swap(&common_link
, common_path
);
512 /* Move up one directory. If we're in_dot_git, we'll search the
513 * parent itself next. If we're !in_dot_git, we'll search .git
514 * in the parent directory next (added at the top of the loop). */
515 if (git_path_dirname_r(&path
, path
.ptr
) < 0) {
520 /* Once we've checked the directory (and .git if applicable),
521 * find the ceiling for a search. */
522 if (min_iterations
&& (--min_iterations
== 0))
523 ceiling_offset
= find_ceiling_dir_offset(path
.ptr
, ceiling_dirs
);
525 /* Check if we should stop searching here. */
526 if (min_iterations
== 0
527 && (path
.ptr
[ceiling_offset
] == 0
528 || (flags
& GIT_REPOSITORY_OPEN_NO_SEARCH
)))
532 if (!error
&& parent_path
&& !(flags
& GIT_REPOSITORY_OPEN_BARE
)) {
533 if (!git_buf_len(repo_path
))
534 git_buf_clear(parent_path
);
536 git_path_dirname_r(parent_path
, path
.ptr
);
537 git_path_to_dir(parent_path
);
539 if (git_buf_oom(parent_path
))
543 /* If we didn't find the repository, and we don't have any other error
544 * to report, report that. */
545 if (!git_buf_len(repo_path
) && !error
) {
546 giterr_set(GITERR_REPOSITORY
,
547 "could not find repository from '%s'", start_path
);
548 error
= GIT_ENOTFOUND
;
552 git_buf_free(&repo_link
);
553 git_buf_free(&common_link
);
557 int git_repository_open_bare(
558 git_repository
**repo_ptr
,
559 const char *bare_path
)
562 git_buf path
= GIT_BUF_INIT
, common_path
= GIT_BUF_INIT
;
563 git_repository
*repo
= NULL
;
565 if ((error
= git_path_prettify_dir(&path
, bare_path
, NULL
)) < 0)
568 if (!valid_repository_path(&path
, &common_path
)) {
570 git_buf_free(&common_path
);
571 giterr_set(GITERR_REPOSITORY
, "path is not a repository: %s", bare_path
);
572 return GIT_ENOTFOUND
;
575 repo
= repository_alloc();
576 GITERR_CHECK_ALLOC(repo
);
578 repo
->gitdir
= git_buf_detach(&path
);
579 GITERR_CHECK_ALLOC(repo
->gitdir
);
580 repo
->commondir
= git_buf_detach(&common_path
);
581 GITERR_CHECK_ALLOC(repo
->commondir
);
583 /* of course we're bare! */
585 repo
->is_worktree
= 0;
586 repo
->workdir
= NULL
;
592 static int _git_repository_open_ext_from_env(
593 git_repository
**out
,
594 const char *start_path
)
596 git_repository
*repo
= NULL
;
597 git_index
*index
= NULL
;
599 git_buf dir_buf
= GIT_BUF_INIT
;
600 git_buf ceiling_dirs_buf
= GIT_BUF_INIT
;
601 git_buf across_fs_buf
= GIT_BUF_INIT
;
602 git_buf index_file_buf
= GIT_BUF_INIT
;
603 git_buf namespace_buf
= GIT_BUF_INIT
;
604 git_buf object_dir_buf
= GIT_BUF_INIT
;
605 git_buf alts_buf
= GIT_BUF_INIT
;
606 git_buf work_tree_buf
= GIT_BUF_INIT
;
607 git_buf common_dir_buf
= GIT_BUF_INIT
;
608 const char *ceiling_dirs
= NULL
;
613 error
= git__getenv(&dir_buf
, "GIT_DIR");
614 if (error
== GIT_ENOTFOUND
) {
617 } else if (error
< 0)
620 start_path
= git_buf_cstr(&dir_buf
);
621 flags
|= GIT_REPOSITORY_OPEN_NO_SEARCH
;
622 flags
|= GIT_REPOSITORY_OPEN_NO_DOTGIT
;
626 error
= git__getenv(&ceiling_dirs_buf
, "GIT_CEILING_DIRECTORIES");
627 if (error
== GIT_ENOTFOUND
)
632 ceiling_dirs
= git_buf_cstr(&ceiling_dirs_buf
);
634 error
= git__getenv(&across_fs_buf
, "GIT_DISCOVERY_ACROSS_FILESYSTEM");
635 if (error
== GIT_ENOTFOUND
)
641 error
= git_config_parse_bool(&across_fs
, git_buf_cstr(&across_fs_buf
));
645 flags
|= GIT_REPOSITORY_OPEN_CROSS_FS
;
648 error
= git__getenv(&index_file_buf
, "GIT_INDEX_FILE");
649 if (error
== GIT_ENOTFOUND
)
654 error
= git_index_open(&index
, git_buf_cstr(&index_file_buf
));
659 error
= git__getenv(&namespace_buf
, "GIT_NAMESPACE");
660 if (error
== GIT_ENOTFOUND
)
665 error
= git__getenv(&object_dir_buf
, "GIT_OBJECT_DIRECTORY");
666 if (error
== GIT_ENOTFOUND
)
671 error
= git_odb_open(&odb
, git_buf_cstr(&object_dir_buf
));
676 error
= git__getenv(&work_tree_buf
, "GIT_WORK_TREE");
677 if (error
== GIT_ENOTFOUND
)
682 giterr_set(GITERR_INVALID
, "GIT_WORK_TREE unimplemented");
687 error
= git__getenv(&work_tree_buf
, "GIT_COMMON_DIR");
688 if (error
== GIT_ENOTFOUND
)
693 giterr_set(GITERR_INVALID
, "GIT_COMMON_DIR unimplemented");
698 error
= git_repository_open_ext(&repo
, start_path
, flags
, ceiling_dirs
);
703 git_repository_set_odb(repo
, odb
);
705 error
= git__getenv(&alts_buf
, "GIT_ALTERNATE_OBJECT_DIRECTORIES");
706 if (error
== GIT_ENOTFOUND
) {
709 } else if (error
< 0)
715 error
= git_repository_odb(&odb
, repo
);
720 end
= git_buf_cstr(&alts_buf
) + git_buf_len(&alts_buf
);
721 for (sep
= alt
= alts_buf
.ptr
; sep
!= end
; alt
= sep
+1) {
722 for (sep
= alt
; *sep
&& *sep
!= GIT_PATH_LIST_SEPARATOR
; sep
++)
726 error
= git_odb_add_disk_alternate(odb
, alt
);
732 if (git_buf_len(&namespace_buf
)) {
733 error
= git_repository_set_namespace(repo
, git_buf_cstr(&namespace_buf
));
738 git_repository_set_index(repo
, index
);
745 git_repository_free(repo
);
748 git_index_free(index
);
749 git_buf_free(&common_dir_buf
);
750 git_buf_free(&work_tree_buf
);
751 git_buf_free(&alts_buf
);
752 git_buf_free(&object_dir_buf
);
753 git_buf_free(&namespace_buf
);
754 git_buf_free(&index_file_buf
);
755 git_buf_free(&across_fs_buf
);
756 git_buf_free(&ceiling_dirs_buf
);
757 git_buf_free(&dir_buf
);
761 int git_repository_open_ext(
762 git_repository
**repo_ptr
,
763 const char *start_path
,
765 const char *ceiling_dirs
)
768 git_buf path
= GIT_BUF_INIT
, parent
= GIT_BUF_INIT
,
769 link_path
= GIT_BUF_INIT
, common_path
= GIT_BUF_INIT
;
770 git_repository
*repo
;
771 git_config
*config
= NULL
;
773 if (flags
& GIT_REPOSITORY_OPEN_FROM_ENV
)
774 return _git_repository_open_ext_from_env(repo_ptr
, start_path
);
780 &path
, &parent
, &link_path
, &common_path
, start_path
, flags
, ceiling_dirs
);
782 if (error
< 0 || !repo_ptr
)
785 repo
= repository_alloc();
786 GITERR_CHECK_ALLOC(repo
);
788 repo
->gitdir
= git_buf_detach(&path
);
789 GITERR_CHECK_ALLOC(repo
->gitdir
);
791 if (link_path
.size
) {
792 repo
->gitlink
= git_buf_detach(&link_path
);
793 GITERR_CHECK_ALLOC(repo
->gitlink
);
795 if (common_path
.size
) {
796 repo
->commondir
= git_buf_detach(&common_path
);
797 GITERR_CHECK_ALLOC(repo
->commondir
);
800 if ((error
= git_buf_joinpath(&path
, repo
->gitdir
, "gitdir")) < 0)
802 /* A 'gitdir' file inside a git directory is currently
803 * only used when the repository is a working tree. */
804 if (git_path_exists(path
.ptr
))
805 repo
->is_worktree
= 1;
808 * We'd like to have the config, but git doesn't particularly
809 * care if it's not there, so we need to deal with that.
812 error
= git_repository_config_snapshot(&config
, repo
);
813 if (error
< 0 && error
!= GIT_ENOTFOUND
)
816 if (config
&& (error
= check_repositoryformatversion(config
)) < 0)
819 if ((flags
& GIT_REPOSITORY_OPEN_BARE
) != 0)
824 ((error
= load_config_data(repo
, config
)) < 0 ||
825 (error
= load_workdir(repo
, config
, &parent
)) < 0))
831 git_buf_free(&parent
);
832 git_config_free(config
);
835 git_repository_free(repo
);
842 int git_repository_open(git_repository
**repo_out
, const char *path
)
844 return git_repository_open_ext(
845 repo_out
, path
, GIT_REPOSITORY_OPEN_NO_SEARCH
, NULL
);
848 int git_repository_open_from_worktree(git_repository
**repo_out
, git_worktree
*wt
)
850 git_buf path
= GIT_BUF_INIT
;
851 git_repository
*repo
= NULL
;
854 assert(repo_out
&& wt
);
857 len
= strlen(wt
->gitlink_path
);
859 if (len
<= 4 || strcasecmp(wt
->gitlink_path
+ len
- 4, ".git")) {
864 if ((err
= git_buf_set(&path
, wt
->gitlink_path
, len
- 4)) < 0)
867 if ((err
= git_repository_open(&repo
, path
.ptr
)) < 0)
878 int git_repository_wrap_odb(git_repository
**repo_out
, git_odb
*odb
)
880 git_repository
*repo
;
882 repo
= repository_alloc();
883 GITERR_CHECK_ALLOC(repo
);
885 git_repository_set_odb(repo
, odb
);
891 int git_repository_discover(
893 const char *start_path
,
895 const char *ceiling_dirs
)
897 uint32_t flags
= across_fs
? GIT_REPOSITORY_OPEN_CROSS_FS
: 0;
901 git_buf_sanitize(out
);
903 return find_repo(out
, NULL
, NULL
, NULL
, start_path
, flags
, ceiling_dirs
);
906 static int load_config(
908 git_repository
*repo
,
909 const char *global_config_path
,
910 const char *xdg_config_path
,
911 const char *system_config_path
,
912 const char *programdata_path
)
915 git_buf config_path
= GIT_BUF_INIT
;
916 git_config
*cfg
= NULL
;
920 if ((error
= git_config_new(&cfg
)) < 0)
923 error
= git_repository_item_path(&config_path
, repo
, GIT_REPOSITORY_ITEM_CONFIG
);
927 if ((error
= git_config_add_file_ondisk(
928 cfg
, config_path
.ptr
, GIT_CONFIG_LEVEL_LOCAL
, 0)) < 0 &&
929 error
!= GIT_ENOTFOUND
)
932 git_buf_free(&config_path
);
934 if (global_config_path
!= NULL
&&
935 (error
= git_config_add_file_ondisk(
936 cfg
, global_config_path
, GIT_CONFIG_LEVEL_GLOBAL
, 0)) < 0 &&
937 error
!= GIT_ENOTFOUND
)
940 if (xdg_config_path
!= NULL
&&
941 (error
= git_config_add_file_ondisk(
942 cfg
, xdg_config_path
, GIT_CONFIG_LEVEL_XDG
, 0)) < 0 &&
943 error
!= GIT_ENOTFOUND
)
946 if (system_config_path
!= NULL
&&
947 (error
= git_config_add_file_ondisk(
948 cfg
, system_config_path
, GIT_CONFIG_LEVEL_SYSTEM
, 0)) < 0 &&
949 error
!= GIT_ENOTFOUND
)
952 if (programdata_path
!= NULL
&&
953 (error
= git_config_add_file_ondisk(
954 cfg
, programdata_path
, GIT_CONFIG_LEVEL_PROGRAMDATA
, 0)) < 0 &&
955 error
!= GIT_ENOTFOUND
)
958 giterr_clear(); /* clear any lingering ENOTFOUND errors */
964 git_buf_free(&config_path
);
965 git_config_free(cfg
);
970 static const char *path_unless_empty(git_buf
*buf
)
972 return git_buf_len(buf
) > 0 ? git_buf_cstr(buf
) : NULL
;
975 int git_repository_config__weakptr(git_config
**out
, git_repository
*repo
)
979 if (repo
->_config
== NULL
) {
980 git_buf global_buf
= GIT_BUF_INIT
;
981 git_buf xdg_buf
= GIT_BUF_INIT
;
982 git_buf system_buf
= GIT_BUF_INIT
;
983 git_buf programdata_buf
= GIT_BUF_INIT
;
986 git_config_find_global(&global_buf
);
987 git_config_find_xdg(&xdg_buf
);
988 git_config_find_system(&system_buf
);
989 git_config_find_programdata(&programdata_buf
);
991 /* If there is no global file, open a backend for it anyway */
992 if (git_buf_len(&global_buf
) == 0)
993 git_config__global_location(&global_buf
);
997 path_unless_empty(&global_buf
),
998 path_unless_empty(&xdg_buf
),
999 path_unless_empty(&system_buf
),
1000 path_unless_empty(&programdata_buf
));
1002 GIT_REFCOUNT_OWN(config
, repo
);
1004 config
= git__compare_and_swap(&repo
->_config
, NULL
, config
);
1005 if (config
!= NULL
) {
1006 GIT_REFCOUNT_OWN(config
, NULL
);
1007 git_config_free(config
);
1011 git_buf_free(&global_buf
);
1012 git_buf_free(&xdg_buf
);
1013 git_buf_free(&system_buf
);
1014 git_buf_free(&programdata_buf
);
1017 *out
= repo
->_config
;
1021 int git_repository_config(git_config
**out
, git_repository
*repo
)
1023 if (git_repository_config__weakptr(out
, repo
) < 0)
1026 GIT_REFCOUNT_INC(*out
);
1030 int git_repository_config_snapshot(git_config
**out
, git_repository
*repo
)
1035 if ((error
= git_repository_config__weakptr(&weak
, repo
)) < 0)
1038 return git_config_snapshot(out
, weak
);
1041 void git_repository_set_config(git_repository
*repo
, git_config
*config
)
1043 assert(repo
&& config
);
1044 set_config(repo
, config
);
1047 int git_repository_odb__weakptr(git_odb
**out
, git_repository
*repo
)
1051 assert(repo
&& out
);
1053 if (repo
->_odb
== NULL
) {
1054 git_buf odb_path
= GIT_BUF_INIT
;
1057 if ((error
= git_repository_item_path(&odb_path
, repo
,
1058 GIT_REPOSITORY_ITEM_OBJECTS
)) < 0 ||
1059 (error
= git_odb_new(&odb
)) < 0)
1062 GIT_REFCOUNT_OWN(odb
, repo
);
1064 if ((error
= git_odb__set_caps(odb
, GIT_ODB_CAP_FROM_OWNER
)) < 0 ||
1065 (error
= git_odb__add_default_backends(odb
, odb_path
.ptr
, 0, 0)) < 0) {
1070 odb
= git__compare_and_swap(&repo
->_odb
, NULL
, odb
);
1072 GIT_REFCOUNT_OWN(odb
, NULL
);
1076 git_buf_free(&odb_path
);
1083 int git_repository_odb(git_odb
**out
, git_repository
*repo
)
1085 if (git_repository_odb__weakptr(out
, repo
) < 0)
1088 GIT_REFCOUNT_INC(*out
);
1092 void git_repository_set_odb(git_repository
*repo
, git_odb
*odb
)
1094 assert(repo
&& odb
);
1098 int git_repository_refdb__weakptr(git_refdb
**out
, git_repository
*repo
)
1102 assert(out
&& repo
);
1104 if (repo
->_refdb
== NULL
) {
1107 error
= git_refdb_open(&refdb
, repo
);
1109 GIT_REFCOUNT_OWN(refdb
, repo
);
1111 refdb
= git__compare_and_swap(&repo
->_refdb
, NULL
, refdb
);
1112 if (refdb
!= NULL
) {
1113 GIT_REFCOUNT_OWN(refdb
, NULL
);
1114 git_refdb_free(refdb
);
1119 *out
= repo
->_refdb
;
1123 int git_repository_refdb(git_refdb
**out
, git_repository
*repo
)
1125 if (git_repository_refdb__weakptr(out
, repo
) < 0)
1128 GIT_REFCOUNT_INC(*out
);
1132 void git_repository_set_refdb(git_repository
*repo
, git_refdb
*refdb
)
1134 assert(repo
&& refdb
);
1135 set_refdb(repo
, refdb
);
1138 int git_repository_index__weakptr(git_index
**out
, git_repository
*repo
)
1142 assert(out
&& repo
);
1144 if (repo
->_index
== NULL
) {
1145 git_buf index_path
= GIT_BUF_INIT
;
1148 if ((error
= git_buf_joinpath(&index_path
, repo
->gitdir
, GIT_INDEX_FILE
)) < 0)
1151 error
= git_index_open(&index
, index_path
.ptr
);
1153 GIT_REFCOUNT_OWN(index
, repo
);
1155 index
= git__compare_and_swap(&repo
->_index
, NULL
, index
);
1156 if (index
!= NULL
) {
1157 GIT_REFCOUNT_OWN(index
, NULL
);
1158 git_index_free(index
);
1161 error
= git_index_set_caps(repo
->_index
, GIT_INDEXCAP_FROM_OWNER
);
1164 git_buf_free(&index_path
);
1167 *out
= repo
->_index
;
1171 int git_repository_index(git_index
**out
, git_repository
*repo
)
1173 if (git_repository_index__weakptr(out
, repo
) < 0)
1176 GIT_REFCOUNT_INC(*out
);
1180 void git_repository_set_index(git_repository
*repo
, git_index
*index
)
1183 set_index(repo
, index
);
1186 int git_repository_set_namespace(git_repository
*repo
, const char *namespace)
1188 git__free(repo
->namespace);
1190 if (namespace == NULL
) {
1191 repo
->namespace = NULL
;
1195 return (repo
->namespace = git__strdup(namespace)) ? 0 : -1;
1198 const char *git_repository_get_namespace(git_repository
*repo
)
1200 return repo
->namespace;
1204 static int reserved_names_add8dot3(git_repository
*repo
, const char *path
)
1206 char *name
= git_win32_path_8dot3_name(path
);
1207 const char *def
= GIT_DIR_SHORTNAME
;
1208 const char *def_dot_git
= DOT_GIT
;
1209 size_t name_len
, def_len
= CONST_STRLEN(GIT_DIR_SHORTNAME
);
1210 size_t def_dot_git_len
= CONST_STRLEN(DOT_GIT
);
1216 name_len
= strlen(name
);
1218 if ((name_len
== def_len
&& memcmp(name
, def
, def_len
) == 0) ||
1219 (name_len
== def_dot_git_len
&& memcmp(name
, def_dot_git
, def_dot_git_len
) == 0)) {
1224 if ((buf
= git_array_alloc(repo
->reserved_names
)) == NULL
)
1227 git_buf_attach(buf
, name
, name_len
);
1231 bool git_repository__reserved_names(
1232 git_buf
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
)
1234 GIT_UNUSED(include_ntfs
);
1236 if (repo
->reserved_names
.size
== 0) {
1240 /* Add the static defaults */
1241 for (i
= 0; i
< git_repository__reserved_names_win32_len
; i
++) {
1242 if ((buf
= git_array_alloc(repo
->reserved_names
)) == NULL
)
1245 buf
->ptr
= git_repository__reserved_names_win32
[i
].ptr
;
1246 buf
->size
= git_repository__reserved_names_win32
[i
].size
;
1249 /* Try to add any repo-specific reserved names - the gitlink file
1250 * within a submodule or the repository (if the repository directory
1251 * is beneath the workdir). These are typically `.git`, but should
1252 * be protected in case they are not. Note, repo and workdir paths
1253 * are always prettified to end in `/`, so a prefixcmp is safe.
1255 if (!repo
->is_bare
) {
1256 int (*prefixcmp
)(const char *, const char *);
1257 int error
, ignorecase
;
1259 error
= git_repository__cvar(
1260 &ignorecase
, repo
, GIT_CVAR_IGNORECASE
);
1261 prefixcmp
= (error
|| ignorecase
) ? git__prefixcmp_icase
:
1264 if (repo
->gitlink
&&
1265 reserved_names_add8dot3(repo
, repo
->gitlink
) < 0)
1269 prefixcmp(repo
->gitdir
, repo
->workdir
) == 0 &&
1270 reserved_names_add8dot3(repo
, repo
->gitdir
) < 0)
1275 *out
= repo
->reserved_names
.ptr
;
1276 *outlen
= repo
->reserved_names
.size
;
1280 /* Always give good defaults, even on OOM */
1282 *out
= git_repository__reserved_names_win32
;
1283 *outlen
= git_repository__reserved_names_win32_len
;
1288 bool git_repository__reserved_names(
1289 git_buf
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
)
1294 *out
= git_repository__reserved_names_win32
;
1295 *outlen
= git_repository__reserved_names_win32_len
;
1297 *out
= git_repository__reserved_names_posix
;
1298 *outlen
= git_repository__reserved_names_posix_len
;
1305 static int check_repositoryformatversion(git_config
*config
)
1309 error
= git_config_get_int32(&version
, config
, "core.repositoryformatversion");
1310 /* git ignores this if the config variable isn't there */
1311 if (error
== GIT_ENOTFOUND
)
1317 if (GIT_REPO_VERSION
< version
) {
1318 giterr_set(GITERR_REPOSITORY
,
1319 "unsupported repository version %d. Only versions up to %d are supported.",
1320 version
, GIT_REPO_VERSION
);
1327 int git_repository_create_head(const char *git_dir
, const char *ref_name
)
1329 git_buf ref_path
= GIT_BUF_INIT
;
1330 git_filebuf ref
= GIT_FILEBUF_INIT
;
1333 if (git_buf_joinpath(&ref_path
, git_dir
, GIT_HEAD_FILE
) < 0 ||
1334 git_filebuf_open(&ref
, ref_path
.ptr
, 0, GIT_REFS_FILE_MODE
) < 0)
1338 ref_name
= GIT_BRANCH_MASTER
;
1340 if (git__prefixcmp(ref_name
, GIT_REFS_DIR
) == 0)
1343 fmt
= "ref: " GIT_REFS_HEADS_DIR
"%s\n";
1345 if (git_filebuf_printf(&ref
, fmt
, ref_name
) < 0 ||
1346 git_filebuf_commit(&ref
) < 0)
1349 git_buf_free(&ref_path
);
1353 git_buf_free(&ref_path
);
1354 git_filebuf_cleanup(&ref
);
1358 static bool is_chmod_supported(const char *file_path
)
1360 struct stat st1
, st2
;
1362 if (p_stat(file_path
, &st1
) < 0)
1365 if (p_chmod(file_path
, st1
.st_mode
^ S_IXUSR
) < 0)
1368 if (p_stat(file_path
, &st2
) < 0)
1371 return (st1
.st_mode
!= st2
.st_mode
);
1374 static bool is_filesystem_case_insensitive(const char *gitdir_path
)
1376 git_buf path
= GIT_BUF_INIT
;
1377 int is_insensitive
= -1;
1379 if (!git_buf_joinpath(&path
, gitdir_path
, "CoNfIg"))
1380 is_insensitive
= git_path_exists(git_buf_cstr(&path
));
1382 git_buf_free(&path
);
1383 return is_insensitive
;
1386 static bool are_symlinks_supported(const char *wd_path
)
1388 git_buf path
= GIT_BUF_INIT
;
1391 int symlinks_supported
= -1;
1393 if ((fd
= git_futils_mktmp(&path
, wd_path
, 0666)) < 0 ||
1395 p_unlink(path
.ptr
) < 0 ||
1396 p_symlink("testing", path
.ptr
) < 0 ||
1397 p_lstat(path
.ptr
, &st
) < 0)
1398 symlinks_supported
= false;
1400 symlinks_supported
= (S_ISLNK(st
.st_mode
) != 0);
1402 (void)p_unlink(path
.ptr
);
1403 git_buf_free(&path
);
1405 return symlinks_supported
;
1408 static int create_empty_file(const char *path
, mode_t mode
)
1412 if ((fd
= p_creat(path
, mode
)) < 0) {
1413 giterr_set(GITERR_OS
, "error while creating '%s'", path
);
1417 if (p_close(fd
) < 0) {
1418 giterr_set(GITERR_OS
, "error while closing '%s'", path
);
1425 static int repo_local_config(
1427 git_buf
*config_dir
,
1428 git_repository
*repo
,
1429 const char *repo_dir
)
1433 const char *cfg_path
;
1435 if (git_buf_joinpath(config_dir
, repo_dir
, GIT_CONFIG_FILENAME_INREPO
) < 0)
1437 cfg_path
= git_buf_cstr(config_dir
);
1439 /* make LOCAL config if missing */
1440 if (!git_path_isfile(cfg_path
) &&
1441 (error
= create_empty_file(cfg_path
, GIT_CONFIG_FILE_MODE
)) < 0)
1444 /* if no repo, just open that file directly */
1446 return git_config_open_ondisk(out
, cfg_path
);
1448 /* otherwise, open parent config and get that level */
1449 if ((error
= git_repository_config__weakptr(&parent
, repo
)) < 0)
1452 if (git_config_open_level(out
, parent
, GIT_CONFIG_LEVEL_LOCAL
) < 0) {
1455 if (!(error
= git_config_add_file_ondisk(
1456 parent
, cfg_path
, GIT_CONFIG_LEVEL_LOCAL
, false)))
1457 error
= git_config_open_level(out
, parent
, GIT_CONFIG_LEVEL_LOCAL
);
1460 git_config_free(parent
);
1465 static int repo_init_fs_configs(
1467 const char *cfg_path
,
1468 const char *repo_dir
,
1469 const char *work_dir
,
1470 bool update_ignorecase
)
1475 work_dir
= repo_dir
;
1477 if ((error
= git_config_set_bool(
1478 cfg
, "core.filemode", is_chmod_supported(cfg_path
))) < 0)
1481 if (!are_symlinks_supported(work_dir
)) {
1482 if ((error
= git_config_set_bool(cfg
, "core.symlinks", false)) < 0)
1484 } else if (git_config_delete_entry(cfg
, "core.symlinks") < 0)
1487 if (update_ignorecase
) {
1488 if (is_filesystem_case_insensitive(repo_dir
)) {
1489 if ((error
= git_config_set_bool(cfg
, "core.ignorecase", true)) < 0)
1491 } else if (git_config_delete_entry(cfg
, "core.ignorecase") < 0)
1495 #ifdef GIT_USE_ICONV
1496 if ((error
= git_config_set_bool(
1497 cfg
, "core.precomposeunicode",
1498 git_path_does_fs_decompose_unicode(work_dir
))) < 0)
1500 /* on non-iconv platforms, don't even set core.precomposeunicode */
1506 static int repo_init_config(
1507 const char *repo_dir
,
1508 const char *work_dir
,
1513 git_buf cfg_path
= GIT_BUF_INIT
, worktree_path
= GIT_BUF_INIT
;
1514 git_config
*config
= NULL
;
1515 bool is_bare
= ((flags
& GIT_REPOSITORY_INIT_BARE
) != 0);
1516 bool is_reinit
= ((flags
& GIT_REPOSITORY_INIT__IS_REINIT
) != 0);
1518 if ((error
= repo_local_config(&config
, &cfg_path
, NULL
, repo_dir
)) < 0)
1521 if (is_reinit
&& (error
= check_repositoryformatversion(config
)) < 0)
1524 #define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \
1525 if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
1526 goto cleanup; } while (0)
1528 SET_REPO_CONFIG(bool, "core.bare", is_bare
);
1529 SET_REPO_CONFIG(int32
, "core.repositoryformatversion", GIT_REPO_VERSION
);
1531 if ((error
= repo_init_fs_configs(
1532 config
, cfg_path
.ptr
, repo_dir
, work_dir
, !is_reinit
)) < 0)
1536 SET_REPO_CONFIG(bool, "core.logallrefupdates", true);
1538 if (!(flags
& GIT_REPOSITORY_INIT__NATURAL_WD
)) {
1539 if ((error
= git_buf_sets(&worktree_path
, work_dir
)) < 0)
1542 if ((flags
& GIT_REPOSITORY_INIT_RELATIVE_GITLINK
))
1543 if ((error
= git_path_make_relative(&worktree_path
, repo_dir
)) < 0)
1546 SET_REPO_CONFIG(string
, "core.worktree", worktree_path
.ptr
);
1547 } else if (is_reinit
) {
1548 if (git_config_delete_entry(config
, "core.worktree") < 0)
1553 if (mode
== GIT_REPOSITORY_INIT_SHARED_GROUP
) {
1554 SET_REPO_CONFIG(int32
, "core.sharedrepository", 1);
1555 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1557 else if (mode
== GIT_REPOSITORY_INIT_SHARED_ALL
) {
1558 SET_REPO_CONFIG(int32
, "core.sharedrepository", 2);
1559 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1563 git_buf_free(&cfg_path
);
1564 git_buf_free(&worktree_path
);
1565 git_config_free(config
);
1570 static int repo_reinit_submodule_fs(git_submodule
*sm
, const char *n
, void *p
)
1572 git_repository
*smrepo
= NULL
;
1573 GIT_UNUSED(n
); GIT_UNUSED(p
);
1575 if (git_submodule_open(&smrepo
, sm
) < 0 ||
1576 git_repository_reinit_filesystem(smrepo
, true) < 0)
1578 git_repository_free(smrepo
);
1583 int git_repository_reinit_filesystem(git_repository
*repo
, int recurse
)
1586 git_buf path
= GIT_BUF_INIT
;
1587 git_config
*config
= NULL
;
1588 const char *repo_dir
= git_repository_path(repo
);
1590 if (!(error
= repo_local_config(&config
, &path
, repo
, repo_dir
)))
1591 error
= repo_init_fs_configs(
1592 config
, path
.ptr
, repo_dir
, git_repository_workdir(repo
), true);
1594 git_config_free(config
);
1595 git_buf_free(&path
);
1597 git_repository__cvar_cache_clear(repo
);
1599 if (!repo
->is_bare
&& recurse
)
1600 (void)git_submodule_foreach(repo
, repo_reinit_submodule_fs
, NULL
);
1605 static int repo_write_template(
1606 const char *git_dir
,
1607 bool allow_overwrite
,
1611 const char *content
)
1613 git_buf path
= GIT_BUF_INIT
;
1614 int fd
, error
= 0, flags
;
1616 if (git_buf_joinpath(&path
, git_dir
, file
) < 0)
1619 if (allow_overwrite
)
1620 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
1622 flags
= O_WRONLY
| O_CREAT
| O_EXCL
;
1624 fd
= p_open(git_buf_cstr(&path
), flags
, mode
);
1627 error
= p_write(fd
, content
, strlen(content
));
1631 else if (errno
!= EEXIST
)
1635 if (!error
&& hidden
) {
1636 if (git_win32__set_hidden(path
.ptr
, true) < 0)
1643 git_buf_free(&path
);
1646 giterr_set(GITERR_OS
,
1647 "failed to initialize repository with template '%s'", file
);
1652 static int repo_write_gitlink(
1653 const char *in_dir
, const char *to_repo
, bool use_relative_path
)
1656 git_buf buf
= GIT_BUF_INIT
;
1657 git_buf path_to_repo
= GIT_BUF_INIT
;
1660 git_path_dirname_r(&buf
, to_repo
);
1661 git_path_to_dir(&buf
);
1662 if (git_buf_oom(&buf
))
1665 /* don't write gitlink to natural workdir */
1666 if (git__suffixcmp(to_repo
, "/" DOT_GIT
"/") == 0 &&
1667 strcmp(in_dir
, buf
.ptr
) == 0)
1669 error
= GIT_PASSTHROUGH
;
1673 if ((error
= git_buf_joinpath(&buf
, in_dir
, DOT_GIT
)) < 0)
1676 if (!p_stat(buf
.ptr
, &st
) && !S_ISREG(st
.st_mode
)) {
1677 giterr_set(GITERR_REPOSITORY
,
1678 "cannot overwrite gitlink file into path '%s'", in_dir
);
1679 error
= GIT_EEXISTS
;
1683 git_buf_clear(&buf
);
1685 error
= git_buf_sets(&path_to_repo
, to_repo
);
1687 if (!error
&& use_relative_path
)
1688 error
= git_path_make_relative(&path_to_repo
, in_dir
);
1691 error
= git_buf_join(&buf
, ' ', GIT_FILE_CONTENT_PREFIX
, path_to_repo
.ptr
);
1694 error
= repo_write_template(in_dir
, true, DOT_GIT
, 0666, true, buf
.ptr
);
1698 git_buf_free(&path_to_repo
);
1702 static mode_t
pick_dir_mode(git_repository_init_options
*opts
)
1704 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_UMASK
)
1706 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_GROUP
)
1707 return (0775 | S_ISGID
);
1708 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_ALL
)
1709 return (0777 | S_ISGID
);
1713 #include "repo_template.h"
1715 static int repo_init_structure(
1716 const char *repo_dir
,
1717 const char *work_dir
,
1718 git_repository_init_options
*opts
)
1721 repo_template_item
*tpl
;
1723 ((opts
->flags
& GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE
) != 0);
1724 mode_t dmode
= pick_dir_mode(opts
);
1725 bool chmod
= opts
->mode
!= GIT_REPOSITORY_INIT_SHARED_UMASK
;
1727 /* Hide the ".git" directory */
1729 if ((opts
->flags
& GIT_REPOSITORY_INIT__HAS_DOTGIT
) != 0) {
1730 if (git_win32__set_hidden(repo_dir
, true) < 0) {
1731 giterr_set(GITERR_OS
,
1732 "failed to mark Git repository folder as hidden");
1738 /* Create the .git gitlink if appropriate */
1739 if ((opts
->flags
& GIT_REPOSITORY_INIT_BARE
) == 0 &&
1740 (opts
->flags
& GIT_REPOSITORY_INIT__NATURAL_WD
) == 0)
1742 if (repo_write_gitlink(work_dir
, repo_dir
, opts
->flags
& GIT_REPOSITORY_INIT_RELATIVE_GITLINK
) < 0)
1746 /* Copy external template if requested */
1748 git_config
*cfg
= NULL
;
1749 const char *tdir
= NULL
;
1750 bool default_template
= false;
1751 git_buf template_buf
= GIT_BUF_INIT
;
1753 if (opts
->template_path
)
1754 tdir
= opts
->template_path
;
1755 else if ((error
= git_config_open_default(&cfg
)) >= 0) {
1756 if (!git_config_get_path(&template_buf
, cfg
, "init.templatedir"))
1757 tdir
= template_buf
.ptr
;
1762 if (!(error
= git_sysdir_find_template_dir(&template_buf
)))
1763 tdir
= template_buf
.ptr
;
1764 default_template
= true;
1768 uint32_t cpflags
= GIT_CPDIR_COPY_SYMLINKS
|
1769 GIT_CPDIR_SIMPLE_TO_MODE
|
1770 GIT_CPDIR_COPY_DOTFILES
;
1771 if (opts
->mode
!= GIT_REPOSITORY_INIT_SHARED_UMASK
)
1772 cpflags
|= GIT_CPDIR_CHMOD_DIRS
;
1773 error
= git_futils_cp_r(tdir
, repo_dir
, cpflags
, dmode
);
1776 git_buf_free(&template_buf
);
1777 git_config_free(cfg
);
1780 if (!default_template
)
1783 /* if template was default, ignore error and use internal */
1785 external_tpl
= false;
1790 /* Copy internal template
1791 * - always ensure existence of dirs
1792 * - only create files if no external template was specified
1794 for (tpl
= repo_template
; !error
&& tpl
->path
; ++tpl
) {
1795 if (!tpl
->content
) {
1796 uint32_t mkdir_flags
= GIT_MKDIR_PATH
;
1798 mkdir_flags
|= GIT_MKDIR_CHMOD
;
1800 error
= git_futils_mkdir_relative(
1801 tpl
->path
, repo_dir
, dmode
, mkdir_flags
, NULL
);
1803 else if (!external_tpl
) {
1804 const char *content
= tpl
->content
;
1806 if (opts
->description
&& strcmp(tpl
->path
, GIT_DESC_FILE
) == 0)
1807 content
= opts
->description
;
1809 error
= repo_write_template(
1810 repo_dir
, false, tpl
->path
, tpl
->mode
, false, content
);
1817 static int mkdir_parent(git_buf
*buf
, uint32_t mode
, bool skip2
)
1819 /* When making parent directories during repository initialization
1820 * don't try to set gid or grant world write access
1822 return git_futils_mkdir(
1823 buf
->ptr
, mode
& ~(S_ISGID
| 0002),
1824 GIT_MKDIR_PATH
| GIT_MKDIR_VERIFY_DIR
|
1825 (skip2
? GIT_MKDIR_SKIP_LAST2
: GIT_MKDIR_SKIP_LAST
));
1828 static int repo_init_directories(
1831 const char *given_repo
,
1832 git_repository_init_options
*opts
)
1835 bool is_bare
, add_dotgit
, has_dotgit
, natural_wd
;
1838 /* There are three possible rules for what we are allowed to create:
1839 * - MKPATH means anything we need
1840 * - MKDIR means just the .git directory and its parent and the workdir
1841 * - Neither means only the .git directory can be created
1843 * There are 5 "segments" of path that we might need to deal with:
1844 * 1. The .git directory
1845 * 2. The parent of the .git directory
1846 * 3. Everything above the parent of the .git directory
1847 * 4. The working directory (often the same as #2)
1848 * 5. Everything above the working directory (often the same as #3)
1850 * For all directories created, we start with the init_mode value for
1851 * permissions and then strip off bits in some cases:
1853 * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH
1854 * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID
1855 * For all rules, we create #1 using the untouched init_mode
1858 /* set up repo path */
1860 is_bare
= ((opts
->flags
& GIT_REPOSITORY_INIT_BARE
) != 0);
1863 (opts
->flags
& GIT_REPOSITORY_INIT_NO_DOTGIT_DIR
) == 0 &&
1865 git__suffixcmp(given_repo
, "/" DOT_GIT
) != 0 &&
1866 git__suffixcmp(given_repo
, "/" GIT_DIR
) != 0;
1868 if (git_buf_joinpath(repo_path
, given_repo
, add_dotgit
? GIT_DIR
: "") < 0)
1871 has_dotgit
= (git__suffixcmp(repo_path
->ptr
, "/" GIT_DIR
) == 0);
1873 opts
->flags
|= GIT_REPOSITORY_INIT__HAS_DOTGIT
;
1875 /* set up workdir path */
1878 if (opts
->workdir_path
) {
1879 if (git_path_join_unrooted(
1880 wd_path
, opts
->workdir_path
, repo_path
->ptr
, NULL
) < 0)
1882 } else if (has_dotgit
) {
1883 if (git_path_dirname_r(wd_path
, repo_path
->ptr
) < 0)
1886 giterr_set(GITERR_REPOSITORY
, "cannot pick working directory"
1887 " for non-bare repository that isn't a '.git' directory");
1891 if (git_path_to_dir(wd_path
) < 0)
1894 git_buf_clear(wd_path
);
1899 wd_path
->size
> 0 &&
1900 wd_path
->size
+ strlen(GIT_DIR
) == repo_path
->size
&&
1901 memcmp(repo_path
->ptr
, wd_path
->ptr
, wd_path
->size
) == 0;
1903 opts
->flags
|= GIT_REPOSITORY_INIT__NATURAL_WD
;
1905 /* create directories as needed / requested */
1907 dirmode
= pick_dir_mode(opts
);
1909 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0) {
1910 /* create path #5 */
1911 if (wd_path
->size
> 0 &&
1912 (error
= mkdir_parent(wd_path
, dirmode
, false)) < 0)
1915 /* create path #3 (if not the same as #5) */
1917 (error
= mkdir_parent(repo_path
, dirmode
, has_dotgit
)) < 0)
1921 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKDIR
) != 0 ||
1922 (opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0)
1924 /* create path #4 */
1925 if (wd_path
->size
> 0 &&
1926 (error
= git_futils_mkdir(
1927 wd_path
->ptr
, dirmode
& ~S_ISGID
,
1928 GIT_MKDIR_VERIFY_DIR
)) < 0)
1931 /* create path #2 (if not the same as #4) */
1933 (error
= git_futils_mkdir(
1934 repo_path
->ptr
, dirmode
& ~S_ISGID
,
1935 GIT_MKDIR_VERIFY_DIR
| GIT_MKDIR_SKIP_LAST
)) < 0)
1939 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKDIR
) != 0 ||
1940 (opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0 ||
1943 /* create path #1 */
1944 error
= git_futils_mkdir(repo_path
->ptr
, dirmode
,
1945 GIT_MKDIR_VERIFY_DIR
| ((dirmode
& S_ISGID
) ? GIT_MKDIR_CHMOD
: 0));
1948 /* prettify both directories now that they are created */
1951 error
= git_path_prettify_dir(repo_path
, repo_path
->ptr
, NULL
);
1953 if (!error
&& wd_path
->size
> 0)
1954 error
= git_path_prettify_dir(wd_path
, wd_path
->ptr
, NULL
);
1960 static int repo_init_create_origin(git_repository
*repo
, const char *url
)
1965 if (!(error
= git_remote_create(&remote
, repo
, GIT_REMOTE_ORIGIN
, url
))) {
1966 git_remote_free(remote
);
1972 int git_repository_init(
1973 git_repository
**repo_out
, const char *path
, unsigned is_bare
)
1975 git_repository_init_options opts
= GIT_REPOSITORY_INIT_OPTIONS_INIT
;
1977 opts
.flags
= GIT_REPOSITORY_INIT_MKPATH
; /* don't love this default */
1979 opts
.flags
|= GIT_REPOSITORY_INIT_BARE
;
1981 return git_repository_init_ext(repo_out
, path
, &opts
);
1984 int git_repository_init_ext(
1985 git_repository
**out
,
1986 const char *given_repo
,
1987 git_repository_init_options
*opts
)
1990 git_buf repo_path
= GIT_BUF_INIT
, wd_path
= GIT_BUF_INIT
,
1991 common_path
= GIT_BUF_INIT
;
1994 assert(out
&& given_repo
&& opts
);
1996 GITERR_CHECK_VERSION(opts
, GIT_REPOSITORY_INIT_OPTIONS_VERSION
, "git_repository_init_options");
1998 error
= repo_init_directories(&repo_path
, &wd_path
, given_repo
, opts
);
2002 wd
= (opts
->flags
& GIT_REPOSITORY_INIT_BARE
) ? NULL
: git_buf_cstr(&wd_path
);
2003 if (valid_repository_path(&repo_path
, &common_path
)) {
2005 if ((opts
->flags
& GIT_REPOSITORY_INIT_NO_REINIT
) != 0) {
2006 giterr_set(GITERR_REPOSITORY
,
2007 "attempt to reinitialize '%s'", given_repo
);
2008 error
= GIT_EEXISTS
;
2012 opts
->flags
|= GIT_REPOSITORY_INIT__IS_REINIT
;
2014 error
= repo_init_config(
2015 repo_path
.ptr
, wd
, opts
->flags
, opts
->mode
);
2017 /* TODO: reinitialize the templates */
2020 if (!(error
= repo_init_structure(
2021 repo_path
.ptr
, wd
, opts
)) &&
2022 !(error
= repo_init_config(
2023 repo_path
.ptr
, wd
, opts
->flags
, opts
->mode
)))
2024 error
= git_repository_create_head(
2025 repo_path
.ptr
, opts
->initial_head
);
2030 error
= git_repository_open(out
, repo_path
.ptr
);
2032 if (!error
&& opts
->origin_url
)
2033 error
= repo_init_create_origin(*out
, opts
->origin_url
);
2036 git_buf_free(&common_path
);
2037 git_buf_free(&repo_path
);
2038 git_buf_free(&wd_path
);
2043 int git_repository_head_detached(git_repository
*repo
)
2046 git_odb
*odb
= NULL
;
2049 if (git_repository_odb__weakptr(&odb
, repo
) < 0)
2052 if (git_reference_lookup(&ref
, repo
, GIT_HEAD_FILE
) < 0)
2055 if (git_reference_type(ref
) == GIT_REF_SYMBOLIC
) {
2056 git_reference_free(ref
);
2060 exists
= git_odb_exists(odb
, git_reference_target(ref
));
2062 git_reference_free(ref
);
2066 static int read_worktree_head(git_buf
*out
, git_repository
*repo
, const char *name
)
2068 git_buf path
= GIT_BUF_INIT
;
2071 assert(out
&& repo
&& name
);
2075 if ((err
= git_buf_printf(&path
, "%s/worktrees/%s/HEAD", repo
->commondir
, name
)) < 0)
2077 if (!git_path_exists(path
.ptr
))
2083 if ((err
= git_futils_readbuffer(out
, path
.ptr
)) < 0)
2088 git_buf_free(&path
);
2093 int git_repository_head_detached_for_worktree(git_repository
*repo
, const char *name
)
2095 git_buf buf
= GIT_BUF_INIT
;
2098 assert(repo
&& name
);
2100 if (read_worktree_head(&buf
, repo
, name
) < 0)
2103 ret
= git__strncmp(buf
.ptr
, GIT_SYMREF
, strlen(GIT_SYMREF
)) != 0;
2109 int git_repository_head(git_reference
**head_out
, git_repository
*repo
)
2111 git_reference
*head
;
2114 if ((error
= git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
)) < 0)
2117 if (git_reference_type(head
) == GIT_REF_OID
) {
2122 error
= git_reference_lookup_resolved(head_out
, repo
, git_reference_symbolic_target(head
), -1);
2123 git_reference_free(head
);
2125 return error
== GIT_ENOTFOUND
? GIT_EUNBORNBRANCH
: error
;
2128 int git_repository_head_for_worktree(git_reference
**out
, git_repository
*repo
, const char *name
)
2130 git_buf buf
= GIT_BUF_INIT
;
2131 git_reference
*head
;
2134 assert(out
&& repo
&& name
);
2138 if (git_repository_head_detached_for_worktree(repo
, name
))
2140 if ((err
= read_worktree_head(&buf
, repo
, name
)) < 0)
2143 /* We can only resolve symbolic references */
2144 if (git__strncmp(buf
.ptr
, GIT_SYMREF
, strlen(GIT_SYMREF
)))
2149 git_buf_consume(&buf
, buf
.ptr
+ strlen(GIT_SYMREF
));
2151 if ((err
= git_reference_lookup(&head
, repo
, buf
.ptr
)) < 0)
2153 if (git_reference_type(head
) == GIT_REF_OID
)
2160 err
= git_reference_lookup_resolved(
2161 out
, repo
, git_reference_symbolic_target(head
), -1);
2162 git_reference_free(head
);
2170 int git_repository_head_unborn(git_repository
*repo
)
2172 git_reference
*ref
= NULL
;
2175 error
= git_repository_head(&ref
, repo
);
2176 git_reference_free(ref
);
2178 if (error
== GIT_EUNBORNBRANCH
) {
2189 static int at_least_one_cb(const char *refname
, void *payload
)
2191 GIT_UNUSED(refname
);
2192 GIT_UNUSED(payload
);
2193 return GIT_PASSTHROUGH
;
2196 static int repo_contains_no_reference(git_repository
*repo
)
2198 int error
= git_reference_foreach_name(repo
, &at_least_one_cb
, NULL
);
2200 if (error
== GIT_PASSTHROUGH
)
2209 int git_repository_is_empty(git_repository
*repo
)
2211 git_reference
*head
= NULL
;
2214 if (git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
) < 0)
2217 if (git_reference_type(head
) == GIT_REF_SYMBOLIC
)
2219 (strcmp(git_reference_symbolic_target(head
),
2220 GIT_REFS_HEADS_DIR
"master") == 0) &&
2221 repo_contains_no_reference(repo
);
2223 git_reference_free(head
);
2228 int git_repository_item_path(git_buf
*out
, git_repository
*repo
, git_repository_item_t item
)
2232 switch (items
[item
].parent
) {
2233 case GIT_REPOSITORY_ITEM_GITDIR
:
2234 parent
= git_repository_path(repo
);
2236 case GIT_REPOSITORY_ITEM_WORKDIR
:
2237 parent
= git_repository_workdir(repo
);
2239 case GIT_REPOSITORY_ITEM_COMMONDIR
:
2240 parent
= git_repository_commondir(repo
);
2243 giterr_set(GITERR_INVALID
, "Invalid item directory");
2247 if (parent
== NULL
) {
2248 giterr_set(GITERR_INVALID
, "Path cannot exist in repository");
2252 if (git_buf_sets(out
, parent
) < 0)
2255 if (items
[item
].name
) {
2256 if (git_buf_joinpath(out
, parent
, items
[item
].name
) < 0)
2260 if (items
[item
].directory
) {
2261 if (git_path_to_dir(out
) < 0)
2268 const char *git_repository_path(git_repository
*repo
)
2271 return repo
->gitdir
;
2274 const char *git_repository_workdir(git_repository
*repo
)
2281 return repo
->workdir
;
2284 const char *git_repository_commondir(git_repository
*repo
)
2287 return repo
->commondir
;
2290 int git_repository_set_workdir(
2291 git_repository
*repo
, const char *workdir
, int update_gitlink
)
2294 git_buf path
= GIT_BUF_INIT
;
2296 assert(repo
&& workdir
);
2298 if (git_path_prettify_dir(&path
, workdir
, NULL
) < 0)
2301 if (repo
->workdir
&& strcmp(repo
->workdir
, path
.ptr
) == 0)
2304 if (update_gitlink
) {
2307 if (git_repository_config__weakptr(&config
, repo
) < 0)
2310 error
= repo_write_gitlink(path
.ptr
, git_repository_path(repo
), false);
2312 /* passthrough error means gitlink is unnecessary */
2313 if (error
== GIT_PASSTHROUGH
)
2314 error
= git_config_delete_entry(config
, "core.worktree");
2316 error
= git_config_set_string(config
, "core.worktree", path
.ptr
);
2319 error
= git_config_set_bool(config
, "core.bare", false);
2323 char *old_workdir
= repo
->workdir
;
2325 repo
->workdir
= git_buf_detach(&path
);
2328 git__free(old_workdir
);
2334 int git_repository_is_bare(git_repository
*repo
)
2337 return repo
->is_bare
;
2340 int git_repository_is_worktree(git_repository
*repo
)
2343 return repo
->is_worktree
;
2346 int git_repository_set_bare(git_repository
*repo
)
2356 if ((error
= git_repository_config__weakptr(&config
, repo
)) < 0)
2359 if ((error
= git_config_set_bool(config
, "core.bare", true)) < 0)
2362 if ((error
= git_config__update_entry(config
, "core.worktree", NULL
, true, true)) < 0)
2365 git__free(repo
->workdir
);
2366 repo
->workdir
= NULL
;
2372 int git_repository_head_tree(git_tree
**tree
, git_repository
*repo
)
2374 git_reference
*head
;
2378 if ((error
= git_repository_head(&head
, repo
)) < 0)
2381 if ((error
= git_reference_peel(&obj
, head
, GIT_OBJ_TREE
)) < 0)
2384 *tree
= (git_tree
*)obj
;
2387 git_reference_free(head
);
2391 int git_repository__set_orig_head(git_repository
*repo
, const git_oid
*orig_head
)
2393 git_filebuf file
= GIT_FILEBUF_INIT
;
2394 git_buf file_path
= GIT_BUF_INIT
;
2395 char orig_head_str
[GIT_OID_HEXSZ
];
2398 git_oid_fmt(orig_head_str
, orig_head
);
2400 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_ORIG_HEAD_FILE
)) == 0 &&
2401 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_FORCE
, GIT_MERGE_FILE_MODE
)) == 0 &&
2402 (error
= git_filebuf_printf(&file
, "%.*s\n", GIT_OID_HEXSZ
, orig_head_str
)) == 0)
2403 error
= git_filebuf_commit(&file
);
2406 git_filebuf_cleanup(&file
);
2408 git_buf_free(&file_path
);
2413 int git_repository_message(git_buf
*out
, git_repository
*repo
)
2415 git_buf path
= GIT_BUF_INIT
;
2419 git_buf_sanitize(out
);
2421 if (git_buf_joinpath(&path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
) < 0)
2424 if ((error
= p_stat(git_buf_cstr(&path
), &st
)) < 0) {
2425 if (errno
== ENOENT
)
2426 error
= GIT_ENOTFOUND
;
2427 giterr_set(GITERR_OS
, "could not access message file");
2429 error
= git_futils_readbuffer(out
, git_buf_cstr(&path
));
2432 git_buf_free(&path
);
2437 int git_repository_message_remove(git_repository
*repo
)
2439 git_buf path
= GIT_BUF_INIT
;
2442 if (git_buf_joinpath(&path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
) < 0)
2445 error
= p_unlink(git_buf_cstr(&path
));
2446 git_buf_free(&path
);
2451 int git_repository_hashfile(
2453 git_repository
*repo
,
2456 const char *as_path
)
2459 git_filter_list
*fl
= NULL
;
2462 git_buf full_path
= GIT_BUF_INIT
;
2464 assert(out
&& path
&& repo
); /* as_path can be NULL */
2466 /* At some point, it would be nice if repo could be NULL to just
2467 * apply filter rules defined in system and global files, but for
2468 * now that is not possible because git_filters_load() needs it.
2471 error
= git_path_join_unrooted(
2472 &full_path
, path
, git_repository_workdir(repo
), NULL
);
2479 /* passing empty string for "as_path" indicated --no-filters */
2480 if (strlen(as_path
) > 0) {
2481 error
= git_filter_list_load(
2482 &fl
, repo
, NULL
, as_path
,
2483 GIT_FILTER_TO_ODB
, GIT_FILTER_DEFAULT
);
2490 /* at this point, error is a count of the number of loaded filters */
2492 fd
= git_futils_open_ro(full_path
.ptr
);
2498 len
= git_futils_filesize(fd
);
2504 if (!git__is_sizet(len
)) {
2505 giterr_set(GITERR_OS
, "file size overflow for 32-bit systems");
2510 error
= git_odb__hashfd_filtered(out
, fd
, (size_t)len
, type
, fl
);
2515 git_filter_list_free(fl
);
2516 git_buf_free(&full_path
);
2521 static int checkout_message(git_buf
*out
, git_reference
*old
, const char *new)
2523 git_buf_puts(out
, "checkout: moving from ");
2525 if (git_reference_type(old
) == GIT_REF_SYMBOLIC
)
2526 git_buf_puts(out
, git_reference__shorthand(git_reference_symbolic_target(old
)));
2528 git_buf_puts(out
, git_oid_tostr_s(git_reference_target(old
)));
2530 git_buf_puts(out
, " to ");
2532 if (git_reference__is_branch(new))
2533 git_buf_puts(out
, git_reference__shorthand(new));
2535 git_buf_puts(out
, new);
2537 if (git_buf_oom(out
))
2543 int git_repository_set_head(
2544 git_repository
* repo
,
2545 const char* refname
)
2547 git_reference
*ref
= NULL
, *current
= NULL
, *new_head
= NULL
;
2548 git_buf log_message
= GIT_BUF_INIT
;
2551 assert(repo
&& refname
);
2553 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2556 if ((error
= checkout_message(&log_message
, current
, refname
)) < 0)
2559 error
= git_reference_lookup(&ref
, repo
, refname
);
2560 if (error
< 0 && error
!= GIT_ENOTFOUND
)
2563 if (ref
&& current
->type
== GIT_REF_SYMBOLIC
&& git__strcmp(current
->target
.symbolic
, ref
->name
) &&
2564 git_reference_is_branch(ref
) && git_branch_is_checked_out(ref
)) {
2570 if (git_reference_is_branch(ref
)) {
2571 error
= git_reference_symbolic_create(&new_head
, repo
, GIT_HEAD_FILE
,
2572 git_reference_name(ref
), true, git_buf_cstr(&log_message
));
2574 error
= git_repository_set_head_detached(repo
, git_reference_target(ref
));
2576 } else if (git_reference__is_branch(refname
)) {
2577 error
= git_reference_symbolic_create(&new_head
, repo
, GIT_HEAD_FILE
, refname
,
2578 true, git_buf_cstr(&log_message
));
2582 git_buf_free(&log_message
);
2583 git_reference_free(current
);
2584 git_reference_free(ref
);
2585 git_reference_free(new_head
);
2589 static int detach(git_repository
*repo
, const git_oid
*id
, const char *from
)
2592 git_buf log_message
= GIT_BUF_INIT
;
2593 git_object
*object
= NULL
, *peeled
= NULL
;
2594 git_reference
*new_head
= NULL
, *current
= NULL
;
2598 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2601 if ((error
= git_object_lookup(&object
, repo
, id
, GIT_OBJ_ANY
)) < 0)
2604 if ((error
= git_object_peel(&peeled
, object
, GIT_OBJ_COMMIT
)) < 0)
2608 from
= git_oid_tostr_s(git_object_id(peeled
));
2610 if ((error
= checkout_message(&log_message
, current
, from
)) < 0)
2613 error
= git_reference_create(&new_head
, repo
, GIT_HEAD_FILE
, git_object_id(peeled
), true, git_buf_cstr(&log_message
));
2616 git_buf_free(&log_message
);
2617 git_object_free(object
);
2618 git_object_free(peeled
);
2619 git_reference_free(current
);
2620 git_reference_free(new_head
);
2624 int git_repository_set_head_detached(
2625 git_repository
* repo
,
2626 const git_oid
* commitish
)
2628 return detach(repo
, commitish
, NULL
);
2631 int git_repository_set_head_detached_from_annotated(
2632 git_repository
*repo
,
2633 const git_annotated_commit
*commitish
)
2635 assert(repo
&& commitish
);
2637 return detach(repo
, git_annotated_commit_id(commitish
), commitish
->description
);
2640 int git_repository_detach_head(git_repository
* repo
)
2642 git_reference
*old_head
= NULL
, *new_head
= NULL
, *current
= NULL
;
2643 git_object
*object
= NULL
;
2644 git_buf log_message
= GIT_BUF_INIT
;
2649 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2652 if ((error
= git_repository_head(&old_head
, repo
)) < 0)
2655 if ((error
= git_object_lookup(&object
, repo
, git_reference_target(old_head
), GIT_OBJ_COMMIT
)) < 0)
2658 if ((error
= checkout_message(&log_message
, current
, git_oid_tostr_s(git_object_id(object
)))) < 0)
2661 error
= git_reference_create(&new_head
, repo
, GIT_HEAD_FILE
, git_reference_target(old_head
),
2662 1, git_buf_cstr(&log_message
));
2665 git_buf_free(&log_message
);
2666 git_object_free(object
);
2667 git_reference_free(old_head
);
2668 git_reference_free(new_head
);
2669 git_reference_free(current
);
2674 * Loosely ported from git.git
2675 * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289
2677 int git_repository_state(git_repository
*repo
)
2679 git_buf repo_path
= GIT_BUF_INIT
;
2680 int state
= GIT_REPOSITORY_STATE_NONE
;
2684 if (git_buf_puts(&repo_path
, repo
->gitdir
) < 0)
2687 if (git_path_contains_file(&repo_path
, GIT_REBASE_MERGE_INTERACTIVE_FILE
))
2688 state
= GIT_REPOSITORY_STATE_REBASE_INTERACTIVE
;
2689 else if (git_path_contains_dir(&repo_path
, GIT_REBASE_MERGE_DIR
))
2690 state
= GIT_REPOSITORY_STATE_REBASE_MERGE
;
2691 else if (git_path_contains_file(&repo_path
, GIT_REBASE_APPLY_REBASING_FILE
))
2692 state
= GIT_REPOSITORY_STATE_REBASE
;
2693 else if (git_path_contains_file(&repo_path
, GIT_REBASE_APPLY_APPLYING_FILE
))
2694 state
= GIT_REPOSITORY_STATE_APPLY_MAILBOX
;
2695 else if (git_path_contains_dir(&repo_path
, GIT_REBASE_APPLY_DIR
))
2696 state
= GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE
;
2697 else if (git_path_contains_file(&repo_path
, GIT_MERGE_HEAD_FILE
))
2698 state
= GIT_REPOSITORY_STATE_MERGE
;
2699 else if (git_path_contains_file(&repo_path
, GIT_REVERT_HEAD_FILE
)) {
2700 state
= GIT_REPOSITORY_STATE_REVERT
;
2701 if (git_path_contains_file(&repo_path
, GIT_SEQUENCER_TODO_FILE
)) {
2702 state
= GIT_REPOSITORY_STATE_REVERT_SEQUENCE
;
2704 } else if (git_path_contains_file(&repo_path
, GIT_CHERRYPICK_HEAD_FILE
)) {
2705 state
= GIT_REPOSITORY_STATE_CHERRYPICK
;
2706 if (git_path_contains_file(&repo_path
, GIT_SEQUENCER_TODO_FILE
)) {
2707 state
= GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE
;
2709 } else if (git_path_contains_file(&repo_path
, GIT_BISECT_LOG_FILE
))
2710 state
= GIT_REPOSITORY_STATE_BISECT
;
2712 git_buf_free(&repo_path
);
2716 int git_repository__cleanup_files(
2717 git_repository
*repo
, const char *files
[], size_t files_len
)
2719 git_buf buf
= GIT_BUF_INIT
;
2723 for (error
= 0, i
= 0; !error
&& i
< files_len
; ++i
) {
2726 if (git_buf_joinpath(&buf
, repo
->gitdir
, files
[i
]) < 0)
2729 path
= git_buf_cstr(&buf
);
2731 if (git_path_isfile(path
)) {
2732 error
= p_unlink(path
);
2733 } else if (git_path_isdir(path
)) {
2734 error
= git_futils_rmdir_r(path
, NULL
,
2735 GIT_RMDIR_REMOVE_FILES
| GIT_RMDIR_REMOVE_BLOCKERS
);
2738 git_buf_clear(&buf
);
2745 static const char *state_files
[] = {
2746 GIT_MERGE_HEAD_FILE
,
2747 GIT_MERGE_MODE_FILE
,
2749 GIT_REVERT_HEAD_FILE
,
2750 GIT_CHERRYPICK_HEAD_FILE
,
2751 GIT_BISECT_LOG_FILE
,
2752 GIT_REBASE_MERGE_DIR
,
2753 GIT_REBASE_APPLY_DIR
,
2757 int git_repository_state_cleanup(git_repository
*repo
)
2761 return git_repository__cleanup_files(repo
, state_files
, ARRAY_SIZE(state_files
));
2764 int git_repository_is_shallow(git_repository
*repo
)
2766 git_buf path
= GIT_BUF_INIT
;
2770 if ((error
= git_buf_joinpath(&path
, repo
->gitdir
, "shallow")) < 0)
2773 error
= git_path_lstat(path
.ptr
, &st
);
2774 git_buf_free(&path
);
2776 if (error
== GIT_ENOTFOUND
) {
2783 return st
.st_size
== 0 ? 0 : 1;
2786 int git_repository_init_init_options(
2787 git_repository_init_options
*opts
, unsigned int version
)
2789 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
2790 opts
, version
, git_repository_init_options
,
2791 GIT_REPOSITORY_INIT_OPTIONS_INIT
);
2795 int git_repository_ident(const char **name
, const char **email
, const git_repository
*repo
)
2797 *name
= repo
->ident_name
;
2798 *email
= repo
->ident_email
;
2803 int git_repository_set_ident(git_repository
*repo
, const char *name
, const char *email
)
2805 char *tmp_name
= NULL
, *tmp_email
= NULL
;
2808 tmp_name
= git__strdup(name
);
2809 GITERR_CHECK_ALLOC(tmp_name
);
2813 tmp_email
= git__strdup(email
);
2814 GITERR_CHECK_ALLOC(tmp_email
);
2817 tmp_name
= git__swap(repo
->ident_name
, tmp_name
);
2818 tmp_email
= git__swap(repo
->ident_email
, tmp_email
);
2820 git__free(tmp_name
);
2821 git__free(tmp_email
);
2826 int git_repository_submodule_cache_all(git_repository
*repo
)
2832 if ((error
= git_strmap_alloc(&repo
->submodule_cache
)))
2835 error
= git_submodule__map(repo
, repo
->submodule_cache
);
2839 int git_repository_submodule_cache_clear(git_repository
*repo
)
2843 if (repo
->submodule_cache
== NULL
) {
2846 git_strmap_foreach_value(repo
->submodule_cache
, sm
, {
2847 git_submodule_free(sm
);
2849 git_strmap_free(repo
->submodule_cache
);
2850 repo
->submodule_cache
= 0;