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"
39 bool git_repository__fsync_gitdir
= false;
42 git_repository_item_t parent
;
46 { GIT_REPOSITORY_ITEM_GITDIR
, NULL
, true },
47 { GIT_REPOSITORY_ITEM_WORKDIR
, NULL
, true },
48 { GIT_REPOSITORY_ITEM_COMMONDIR
, NULL
, true },
49 { GIT_REPOSITORY_ITEM_GITDIR
, "index", false },
50 { GIT_REPOSITORY_ITEM_COMMONDIR
, "objects", true },
51 { GIT_REPOSITORY_ITEM_COMMONDIR
, "refs", true },
52 { GIT_REPOSITORY_ITEM_COMMONDIR
, "packed-refs", false },
53 { GIT_REPOSITORY_ITEM_COMMONDIR
, "remotes", true },
54 { GIT_REPOSITORY_ITEM_COMMONDIR
, "config", false },
55 { GIT_REPOSITORY_ITEM_COMMONDIR
, "info", true },
56 { GIT_REPOSITORY_ITEM_COMMONDIR
, "hooks", true },
57 { GIT_REPOSITORY_ITEM_COMMONDIR
, "logs", true },
58 { GIT_REPOSITORY_ITEM_GITDIR
, "modules", true },
59 { GIT_REPOSITORY_ITEM_COMMONDIR
, "worktrees", true }
62 static int check_repositoryformatversion(git_config
*config
);
64 #define GIT_COMMONDIR_FILE "commondir"
65 #define GIT_GITDIR_FILE "gitdir"
67 #define GIT_FILE_CONTENT_PREFIX "gitdir:"
69 #define GIT_BRANCH_MASTER "master"
71 #define GIT_REPO_VERSION 0
73 git_buf git_repository__reserved_names_win32
[] = {
74 { DOT_GIT
, 0, CONST_STRLEN(DOT_GIT
) },
75 { GIT_DIR_SHORTNAME
, 0, CONST_STRLEN(GIT_DIR_SHORTNAME
) }
77 size_t git_repository__reserved_names_win32_len
= 2;
79 git_buf git_repository__reserved_names_posix
[] = {
80 { DOT_GIT
, 0, CONST_STRLEN(DOT_GIT
) },
82 size_t git_repository__reserved_names_posix_len
= 1;
84 static void set_odb(git_repository
*repo
, git_odb
*odb
)
87 GIT_REFCOUNT_OWN(odb
, repo
);
88 GIT_REFCOUNT_INC(odb
);
91 if ((odb
= git__swap(repo
->_odb
, odb
)) != NULL
) {
92 GIT_REFCOUNT_OWN(odb
, NULL
);
97 static void set_refdb(git_repository
*repo
, git_refdb
*refdb
)
100 GIT_REFCOUNT_OWN(refdb
, repo
);
101 GIT_REFCOUNT_INC(refdb
);
104 if ((refdb
= git__swap(repo
->_refdb
, refdb
)) != NULL
) {
105 GIT_REFCOUNT_OWN(refdb
, NULL
);
106 git_refdb_free(refdb
);
110 static void set_config(git_repository
*repo
, git_config
*config
)
113 GIT_REFCOUNT_OWN(config
, repo
);
114 GIT_REFCOUNT_INC(config
);
117 if ((config
= git__swap(repo
->_config
, config
)) != NULL
) {
118 GIT_REFCOUNT_OWN(config
, NULL
);
119 git_config_free(config
);
122 git_repository__cvar_cache_clear(repo
);
125 static void set_index(git_repository
*repo
, git_index
*index
)
128 GIT_REFCOUNT_OWN(index
, repo
);
129 GIT_REFCOUNT_INC(index
);
132 if ((index
= git__swap(repo
->_index
, index
)) != NULL
) {
133 GIT_REFCOUNT_OWN(index
, NULL
);
134 git_index_free(index
);
138 void git_repository__cleanup(git_repository
*repo
)
142 git_repository_submodule_cache_clear(repo
);
143 git_cache_clear(&repo
->objects
);
144 git_attr_cache_flush(repo
);
146 set_config(repo
, NULL
);
147 set_index(repo
, NULL
);
149 set_refdb(repo
, NULL
);
152 void git_repository_free(git_repository
*repo
)
159 git_repository__cleanup(repo
);
161 git_cache_free(&repo
->objects
);
163 git_diff_driver_registry_free(repo
->diff_drivers
);
164 repo
->diff_drivers
= NULL
;
166 for (i
= 0; i
< repo
->reserved_names
.size
; i
++)
167 git_buf_free(git_array_get(repo
->reserved_names
, i
));
168 git_array_clear(repo
->reserved_names
);
170 git__free(repo
->gitlink
);
171 git__free(repo
->gitdir
);
172 git__free(repo
->commondir
);
173 git__free(repo
->workdir
);
174 git__free(repo
->namespace);
175 git__free(repo
->ident_name
);
176 git__free(repo
->ident_email
);
178 git__memzero(repo
, sizeof(*repo
));
183 * Git repository open methods
185 * Open a repository object from its path
187 static bool valid_repository_path(git_buf
*repository_path
, git_buf
*common_path
)
189 /* Check if we have a separate commondir (e.g. we have a
191 if (git_path_contains_file(repository_path
, GIT_COMMONDIR_FILE
)) {
192 git_buf common_link
= GIT_BUF_INIT
;
193 git_buf_joinpath(&common_link
, repository_path
->ptr
, GIT_COMMONDIR_FILE
);
195 git_futils_readbuffer(&common_link
, common_link
.ptr
);
196 git_buf_rtrim(&common_link
);
198 if (git_path_is_relative(common_link
.ptr
)) {
199 git_buf_joinpath(common_path
, repository_path
->ptr
, common_link
.ptr
);
201 git_buf_swap(common_path
, &common_link
);
204 git_buf_free(&common_link
);
207 git_buf_set(common_path
, repository_path
->ptr
, repository_path
->size
);
210 /* Make sure the commondir path always has a trailing * slash */
211 if (git_buf_rfind(common_path
, '/') != (ssize_t
)common_path
->size
- 1)
212 git_buf_putc(common_path
, '/');
214 /* Ensure HEAD file exists */
215 if (git_path_contains_file(repository_path
, GIT_HEAD_FILE
) == false)
218 /* Check files in common dir */
219 if (git_path_contains_dir(common_path
, GIT_OBJECTS_DIR
) == false)
221 if (git_path_contains_dir(common_path
, GIT_REFS_DIR
) == false)
227 static git_repository
*repository_alloc(void)
229 git_repository
*repo
= git__calloc(1, sizeof(git_repository
));
232 git_cache_init(&repo
->objects
) < 0)
235 git_array_init_to_size(repo
->reserved_names
, 4);
236 if (!repo
->reserved_names
.ptr
)
239 /* set all the entries in the cvar cache to `unset` */
240 git_repository__cvar_cache_clear(repo
);
246 git_cache_free(&repo
->objects
);
252 int git_repository_new(git_repository
**out
)
254 git_repository
*repo
;
256 *out
= repo
= repository_alloc();
257 GITERR_CHECK_ALLOC(repo
);
260 repo
->is_worktree
= 0;
265 static int load_config_data(git_repository
*repo
, const git_config
*config
)
269 /* Try to figure out if it's bare, default to non-bare if it's not set */
270 if (git_config_get_bool(&is_bare
, config
, "core.bare") < 0)
273 repo
->is_bare
= is_bare
;
278 static int load_workdir(git_repository
*repo
, git_config
*config
, git_buf
*parent_path
)
281 git_config_entry
*ce
;
282 git_buf worktree
= GIT_BUF_INIT
;
283 git_buf path
= GIT_BUF_INIT
;
288 if ((error
= git_config__lookup_entry(
289 &ce
, config
, "core.worktree", false)) < 0)
292 if (repo
->is_worktree
) {
293 char *gitlink
= git_worktree__read_link(repo
->gitdir
, GIT_GITDIR_FILE
);
299 git_buf_attach(&worktree
, gitlink
, 0);
301 if ((git_path_dirname_r(&worktree
, worktree
.ptr
)) < 0 ||
302 git_path_to_dir(&worktree
) < 0) {
307 repo
->workdir
= git_buf_detach(&worktree
);
309 else if (ce
&& ce
->value
) {
310 if ((error
= git_path_prettify_dir(
311 &worktree
, ce
->value
, repo
->gitdir
)) < 0)
314 repo
->workdir
= git_buf_detach(&worktree
);
316 else if (parent_path
&& git_path_isdir(parent_path
->ptr
))
317 repo
->workdir
= git_buf_detach(parent_path
);
319 if (git_path_dirname_r(&worktree
, repo
->gitdir
) < 0 ||
320 git_path_to_dir(&worktree
) < 0) {
325 repo
->workdir
= git_buf_detach(&worktree
);
328 GITERR_CHECK_ALLOC(repo
->workdir
);
331 git_config_entry_free(ce
);
336 * This function returns furthest offset into path where a ceiling dir
337 * is found, so we can stop processing the path at that point.
339 * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on
340 * the stack could remove directories name limits, but at the cost of doing
341 * repeated malloc/frees inside the loop below, so let's not do it now.
343 static size_t find_ceiling_dir_offset(
345 const char *ceiling_directories
)
347 char buf
[GIT_PATH_MAX
+ 1];
348 char buf2
[GIT_PATH_MAX
+ 1];
349 const char *ceil
, *sep
;
350 size_t len
, max_len
= 0, min_len
;
354 min_len
= (size_t)(git_path_root(path
) + 1);
356 if (ceiling_directories
== NULL
|| min_len
== 0)
359 for (sep
= ceil
= ceiling_directories
; *sep
; ceil
= sep
+ 1) {
360 for (sep
= ceil
; *sep
&& *sep
!= GIT_PATH_LIST_SEPARATOR
; sep
++);
363 if (len
== 0 || len
>= sizeof(buf
) || git_path_root(ceil
) == -1)
366 strncpy(buf
, ceil
, len
);
369 if (p_realpath(buf
, buf2
) == NULL
)
373 if (len
> 0 && buf2
[len
-1] == '/')
376 if (!strncmp(path
, buf2
, len
) &&
377 (path
[len
] == '/' || !path
[len
]) &&
384 return (max_len
<= min_len
? min_len
: max_len
);
388 * Read the contents of `file_path` and set `path_out` to the repo dir that
389 * it points to. Before calling, set `path_out` to the base directory that
390 * should be used if the contents of `file_path` are a relative path.
392 static int read_gitfile(git_buf
*path_out
, const char *file_path
)
395 git_buf file
= GIT_BUF_INIT
;
396 size_t prefix_len
= strlen(GIT_FILE_CONTENT_PREFIX
);
398 assert(path_out
&& file_path
);
400 if (git_futils_readbuffer(&file
, file_path
) < 0)
403 git_buf_rtrim(&file
);
404 /* apparently on Windows, some people use backslashes in paths */
405 git_path_mkposix(file
.ptr
);
407 if (git_buf_len(&file
) <= prefix_len
||
408 memcmp(git_buf_cstr(&file
), GIT_FILE_CONTENT_PREFIX
, prefix_len
) != 0)
410 giterr_set(GITERR_REPOSITORY
,
411 "the `.git` file at '%s' is malformed", file_path
);
414 else if ((error
= git_path_dirname_r(path_out
, file_path
)) >= 0) {
415 const char *gitlink
= git_buf_cstr(&file
) + prefix_len
;
416 while (*gitlink
&& git__isspace(*gitlink
)) gitlink
++;
418 error
= git_path_prettify_dir(
419 path_out
, gitlink
, git_buf_cstr(path_out
));
426 static int find_repo(
427 git_buf
*gitdir_path
,
428 git_buf
*workdir_path
,
429 git_buf
*gitlink_path
,
430 git_buf
*commondir_path
,
431 const char *start_path
,
433 const char *ceiling_dirs
)
436 git_buf path
= GIT_BUF_INIT
;
437 git_buf repo_link
= GIT_BUF_INIT
;
438 git_buf common_link
= GIT_BUF_INIT
;
440 dev_t initial_device
= 0;
443 size_t ceiling_offset
= 0;
445 git_buf_clear(gitdir_path
);
447 error
= git_path_prettify(&path
, start_path
, NULL
);
451 /* in_dot_git toggles each loop:
452 * /a/b/c/.git, /a/b/c, /a/b/.git, /a/b, /a/.git, /a
453 * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, we
454 * assume we started with /a/b/c.git and don't append .git the first
456 * min_iterations indicates the number of iterations left before going
457 * further counts as a search. */
458 if (flags
& (GIT_REPOSITORY_OPEN_BARE
| GIT_REPOSITORY_OPEN_NO_DOTGIT
)) {
467 if (!(flags
& GIT_REPOSITORY_OPEN_NO_DOTGIT
)) {
469 error
= git_buf_joinpath(&path
, path
.ptr
, DOT_GIT
);
473 in_dot_git
= !in_dot_git
;
476 if (p_stat(path
.ptr
, &st
) == 0) {
477 /* check that we have not crossed device boundaries */
478 if (initial_device
== 0)
479 initial_device
= st
.st_dev
;
480 else if (st
.st_dev
!= initial_device
&&
481 !(flags
& GIT_REPOSITORY_OPEN_CROSS_FS
))
484 if (S_ISDIR(st
.st_mode
)) {
485 if (valid_repository_path(&path
, &common_link
)) {
486 git_path_to_dir(&path
);
487 git_buf_set(gitdir_path
, path
.ptr
, path
.size
);
490 git_buf_attach(gitlink_path
,
491 git_worktree__read_link(path
.ptr
, GIT_GITDIR_FILE
), 0);
493 git_buf_swap(&common_link
, commondir_path
);
498 else if (S_ISREG(st
.st_mode
) && git__suffixcmp(path
.ptr
, "/" DOT_GIT
) == 0) {
499 error
= read_gitfile(&repo_link
, path
.ptr
);
502 if (valid_repository_path(&repo_link
, &common_link
)) {
503 git_buf_swap(gitdir_path
, &repo_link
);
506 error
= git_buf_put(gitlink_path
, path
.ptr
, path
.size
);
508 git_buf_swap(&common_link
, commondir_path
);
514 /* Move up one directory. If we're in_dot_git, we'll search the
515 * parent itself next. If we're !in_dot_git, we'll search .git
516 * in the parent directory next (added at the top of the loop). */
517 if (git_path_dirname_r(&path
, path
.ptr
) < 0) {
522 /* Once we've checked the directory (and .git if applicable),
523 * find the ceiling for a search. */
524 if (min_iterations
&& (--min_iterations
== 0))
525 ceiling_offset
= find_ceiling_dir_offset(path
.ptr
, ceiling_dirs
);
527 /* Check if we should stop searching here. */
528 if (min_iterations
== 0
529 && (path
.ptr
[ceiling_offset
] == 0
530 || (flags
& GIT_REPOSITORY_OPEN_NO_SEARCH
)))
534 if (!error
&& workdir_path
&& !(flags
& GIT_REPOSITORY_OPEN_BARE
)) {
535 if (!git_buf_len(gitdir_path
))
536 git_buf_clear(workdir_path
);
538 git_path_dirname_r(workdir_path
, path
.ptr
);
539 git_path_to_dir(workdir_path
);
541 if (git_buf_oom(workdir_path
))
545 /* If we didn't find the repository, and we don't have any other error
546 * to report, report that. */
547 if (!git_buf_len(gitdir_path
) && !error
) {
548 giterr_set(GITERR_REPOSITORY
,
549 "could not find repository from '%s'", start_path
);
550 error
= GIT_ENOTFOUND
;
554 git_buf_free(&repo_link
);
555 git_buf_free(&common_link
);
559 int git_repository_open_bare(
560 git_repository
**repo_ptr
,
561 const char *bare_path
)
564 git_buf path
= GIT_BUF_INIT
, common_path
= GIT_BUF_INIT
;
565 git_repository
*repo
= NULL
;
567 if ((error
= git_path_prettify_dir(&path
, bare_path
, NULL
)) < 0)
570 if (!valid_repository_path(&path
, &common_path
)) {
572 git_buf_free(&common_path
);
573 giterr_set(GITERR_REPOSITORY
, "path is not a repository: %s", bare_path
);
574 return GIT_ENOTFOUND
;
577 repo
= repository_alloc();
578 GITERR_CHECK_ALLOC(repo
);
580 repo
->gitdir
= git_buf_detach(&path
);
581 GITERR_CHECK_ALLOC(repo
->gitdir
);
582 repo
->commondir
= git_buf_detach(&common_path
);
583 GITERR_CHECK_ALLOC(repo
->commondir
);
585 /* of course we're bare! */
587 repo
->is_worktree
= 0;
588 repo
->workdir
= NULL
;
594 static int _git_repository_open_ext_from_env(
595 git_repository
**out
,
596 const char *start_path
)
598 git_repository
*repo
= NULL
;
599 git_index
*index
= NULL
;
601 git_buf dir_buf
= GIT_BUF_INIT
;
602 git_buf ceiling_dirs_buf
= GIT_BUF_INIT
;
603 git_buf across_fs_buf
= GIT_BUF_INIT
;
604 git_buf index_file_buf
= GIT_BUF_INIT
;
605 git_buf namespace_buf
= GIT_BUF_INIT
;
606 git_buf object_dir_buf
= GIT_BUF_INIT
;
607 git_buf alts_buf
= GIT_BUF_INIT
;
608 git_buf work_tree_buf
= GIT_BUF_INIT
;
609 git_buf common_dir_buf
= GIT_BUF_INIT
;
610 const char *ceiling_dirs
= NULL
;
615 error
= git__getenv(&dir_buf
, "GIT_DIR");
616 if (error
== GIT_ENOTFOUND
) {
619 } else if (error
< 0)
622 start_path
= git_buf_cstr(&dir_buf
);
623 flags
|= GIT_REPOSITORY_OPEN_NO_SEARCH
;
624 flags
|= GIT_REPOSITORY_OPEN_NO_DOTGIT
;
628 error
= git__getenv(&ceiling_dirs_buf
, "GIT_CEILING_DIRECTORIES");
629 if (error
== GIT_ENOTFOUND
)
634 ceiling_dirs
= git_buf_cstr(&ceiling_dirs_buf
);
636 error
= git__getenv(&across_fs_buf
, "GIT_DISCOVERY_ACROSS_FILESYSTEM");
637 if (error
== GIT_ENOTFOUND
)
643 error
= git_config_parse_bool(&across_fs
, git_buf_cstr(&across_fs_buf
));
647 flags
|= GIT_REPOSITORY_OPEN_CROSS_FS
;
650 error
= git__getenv(&index_file_buf
, "GIT_INDEX_FILE");
651 if (error
== GIT_ENOTFOUND
)
656 error
= git_index_open(&index
, git_buf_cstr(&index_file_buf
));
661 error
= git__getenv(&namespace_buf
, "GIT_NAMESPACE");
662 if (error
== GIT_ENOTFOUND
)
667 error
= git__getenv(&object_dir_buf
, "GIT_OBJECT_DIRECTORY");
668 if (error
== GIT_ENOTFOUND
)
673 error
= git_odb_open(&odb
, git_buf_cstr(&object_dir_buf
));
678 error
= git__getenv(&work_tree_buf
, "GIT_WORK_TREE");
679 if (error
== GIT_ENOTFOUND
)
684 giterr_set(GITERR_INVALID
, "GIT_WORK_TREE unimplemented");
689 error
= git__getenv(&work_tree_buf
, "GIT_COMMON_DIR");
690 if (error
== GIT_ENOTFOUND
)
695 giterr_set(GITERR_INVALID
, "GIT_COMMON_DIR unimplemented");
700 error
= git_repository_open_ext(&repo
, start_path
, flags
, ceiling_dirs
);
705 git_repository_set_odb(repo
, odb
);
707 error
= git__getenv(&alts_buf
, "GIT_ALTERNATE_OBJECT_DIRECTORIES");
708 if (error
== GIT_ENOTFOUND
) {
711 } else if (error
< 0)
717 error
= git_repository_odb(&odb
, repo
);
722 end
= git_buf_cstr(&alts_buf
) + git_buf_len(&alts_buf
);
723 for (sep
= alt
= alts_buf
.ptr
; sep
!= end
; alt
= sep
+1) {
724 for (sep
= alt
; *sep
&& *sep
!= GIT_PATH_LIST_SEPARATOR
; sep
++)
728 error
= git_odb_add_disk_alternate(odb
, alt
);
734 if (git_buf_len(&namespace_buf
)) {
735 error
= git_repository_set_namespace(repo
, git_buf_cstr(&namespace_buf
));
740 git_repository_set_index(repo
, index
);
747 git_repository_free(repo
);
750 git_index_free(index
);
751 git_buf_free(&common_dir_buf
);
752 git_buf_free(&work_tree_buf
);
753 git_buf_free(&alts_buf
);
754 git_buf_free(&object_dir_buf
);
755 git_buf_free(&namespace_buf
);
756 git_buf_free(&index_file_buf
);
757 git_buf_free(&across_fs_buf
);
758 git_buf_free(&ceiling_dirs_buf
);
759 git_buf_free(&dir_buf
);
763 static int repo_is_worktree(unsigned *out
, const git_repository
*repo
)
765 git_buf gitdir_link
= GIT_BUF_INIT
;
768 /* Worktrees cannot have the same commondir and gitdir */
769 if (repo
->commondir
&& repo
->gitdir
770 && !strcmp(repo
->commondir
, repo
->gitdir
)) {
775 if ((error
= git_buf_joinpath(&gitdir_link
, repo
->gitdir
, "gitdir")) < 0)
778 /* A 'gitdir' file inside a git directory is currently
779 * only used when the repository is a working tree. */
780 *out
= !!git_path_exists(gitdir_link
.ptr
);
782 git_buf_free(&gitdir_link
);
786 int git_repository_open_ext(
787 git_repository
**repo_ptr
,
788 const char *start_path
,
790 const char *ceiling_dirs
)
793 unsigned is_worktree
;
794 git_buf gitdir
= GIT_BUF_INIT
, workdir
= GIT_BUF_INIT
,
795 gitlink
= GIT_BUF_INIT
, commondir
= GIT_BUF_INIT
;
796 git_repository
*repo
;
797 git_config
*config
= NULL
;
799 if (flags
& GIT_REPOSITORY_OPEN_FROM_ENV
)
800 return _git_repository_open_ext_from_env(repo_ptr
, start_path
);
806 &gitdir
, &workdir
, &gitlink
, &commondir
, start_path
, flags
, ceiling_dirs
);
808 if (error
< 0 || !repo_ptr
)
811 repo
= repository_alloc();
812 GITERR_CHECK_ALLOC(repo
);
814 repo
->gitdir
= git_buf_detach(&gitdir
);
815 GITERR_CHECK_ALLOC(repo
->gitdir
);
818 repo
->gitlink
= git_buf_detach(&gitlink
);
819 GITERR_CHECK_ALLOC(repo
->gitlink
);
821 if (commondir
.size
) {
822 repo
->commondir
= git_buf_detach(&commondir
);
823 GITERR_CHECK_ALLOC(repo
->commondir
);
826 if ((error
= repo_is_worktree(&is_worktree
, repo
)) < 0)
828 repo
->is_worktree
= is_worktree
;
831 * We'd like to have the config, but git doesn't particularly
832 * care if it's not there, so we need to deal with that.
835 error
= git_repository_config_snapshot(&config
, repo
);
836 if (error
< 0 && error
!= GIT_ENOTFOUND
)
839 if (config
&& (error
= check_repositoryformatversion(config
)) < 0)
842 if ((flags
& GIT_REPOSITORY_OPEN_BARE
) != 0)
847 ((error
= load_config_data(repo
, config
)) < 0 ||
848 (error
= load_workdir(repo
, config
, &workdir
)) < 0))
853 git_buf_free(&gitdir
);
854 git_buf_free(&workdir
);
855 git_config_free(config
);
858 git_repository_free(repo
);
865 int git_repository_open(git_repository
**repo_out
, const char *path
)
867 return git_repository_open_ext(
868 repo_out
, path
, GIT_REPOSITORY_OPEN_NO_SEARCH
, NULL
);
871 int git_repository_open_from_worktree(git_repository
**repo_out
, git_worktree
*wt
)
873 git_buf path
= GIT_BUF_INIT
;
874 git_repository
*repo
= NULL
;
877 assert(repo_out
&& wt
);
880 len
= strlen(wt
->gitlink_path
);
882 if (len
<= 4 || strcasecmp(wt
->gitlink_path
+ len
- 4, ".git")) {
887 if ((err
= git_buf_set(&path
, wt
->gitlink_path
, len
- 4)) < 0)
890 if ((err
= git_repository_open(&repo
, path
.ptr
)) < 0)
901 int git_repository_wrap_odb(git_repository
**repo_out
, git_odb
*odb
)
903 git_repository
*repo
;
905 repo
= repository_alloc();
906 GITERR_CHECK_ALLOC(repo
);
908 git_repository_set_odb(repo
, odb
);
914 int git_repository_discover(
916 const char *start_path
,
918 const char *ceiling_dirs
)
920 uint32_t flags
= across_fs
? GIT_REPOSITORY_OPEN_CROSS_FS
: 0;
924 git_buf_sanitize(out
);
926 return find_repo(out
, NULL
, NULL
, NULL
, start_path
, flags
, ceiling_dirs
);
929 static int load_config(
931 git_repository
*repo
,
932 const char *global_config_path
,
933 const char *xdg_config_path
,
934 const char *system_config_path
,
935 const char *programdata_path
)
938 git_buf config_path
= GIT_BUF_INIT
;
939 git_config
*cfg
= NULL
;
943 if ((error
= git_config_new(&cfg
)) < 0)
946 error
= git_repository_item_path(&config_path
, repo
, GIT_REPOSITORY_ITEM_CONFIG
);
950 if ((error
= git_config_add_file_ondisk(
951 cfg
, config_path
.ptr
, GIT_CONFIG_LEVEL_LOCAL
, 0)) < 0 &&
952 error
!= GIT_ENOTFOUND
)
955 git_buf_free(&config_path
);
957 if (global_config_path
!= NULL
&&
958 (error
= git_config_add_file_ondisk(
959 cfg
, global_config_path
, GIT_CONFIG_LEVEL_GLOBAL
, 0)) < 0 &&
960 error
!= GIT_ENOTFOUND
)
963 if (xdg_config_path
!= NULL
&&
964 (error
= git_config_add_file_ondisk(
965 cfg
, xdg_config_path
, GIT_CONFIG_LEVEL_XDG
, 0)) < 0 &&
966 error
!= GIT_ENOTFOUND
)
969 if (system_config_path
!= NULL
&&
970 (error
= git_config_add_file_ondisk(
971 cfg
, system_config_path
, GIT_CONFIG_LEVEL_SYSTEM
, 0)) < 0 &&
972 error
!= GIT_ENOTFOUND
)
975 if (programdata_path
!= NULL
&&
976 (error
= git_config_add_file_ondisk(
977 cfg
, programdata_path
, GIT_CONFIG_LEVEL_PROGRAMDATA
, 0)) < 0 &&
978 error
!= GIT_ENOTFOUND
)
981 giterr_clear(); /* clear any lingering ENOTFOUND errors */
987 git_buf_free(&config_path
);
988 git_config_free(cfg
);
993 static const char *path_unless_empty(git_buf
*buf
)
995 return git_buf_len(buf
) > 0 ? git_buf_cstr(buf
) : NULL
;
998 int git_repository_config__weakptr(git_config
**out
, git_repository
*repo
)
1002 if (repo
->_config
== NULL
) {
1003 git_buf global_buf
= GIT_BUF_INIT
;
1004 git_buf xdg_buf
= GIT_BUF_INIT
;
1005 git_buf system_buf
= GIT_BUF_INIT
;
1006 git_buf programdata_buf
= GIT_BUF_INIT
;
1009 git_config_find_global(&global_buf
);
1010 git_config_find_xdg(&xdg_buf
);
1011 git_config_find_system(&system_buf
);
1012 git_config_find_programdata(&programdata_buf
);
1014 /* If there is no global file, open a backend for it anyway */
1015 if (git_buf_len(&global_buf
) == 0)
1016 git_config__global_location(&global_buf
);
1018 error
= load_config(
1020 path_unless_empty(&global_buf
),
1021 path_unless_empty(&xdg_buf
),
1022 path_unless_empty(&system_buf
),
1023 path_unless_empty(&programdata_buf
));
1025 GIT_REFCOUNT_OWN(config
, repo
);
1027 config
= git__compare_and_swap(&repo
->_config
, NULL
, config
);
1028 if (config
!= NULL
) {
1029 GIT_REFCOUNT_OWN(config
, NULL
);
1030 git_config_free(config
);
1034 git_buf_free(&global_buf
);
1035 git_buf_free(&xdg_buf
);
1036 git_buf_free(&system_buf
);
1037 git_buf_free(&programdata_buf
);
1040 *out
= repo
->_config
;
1044 int git_repository_config(git_config
**out
, git_repository
*repo
)
1046 if (git_repository_config__weakptr(out
, repo
) < 0)
1049 GIT_REFCOUNT_INC(*out
);
1053 int git_repository_config_snapshot(git_config
**out
, git_repository
*repo
)
1058 if ((error
= git_repository_config__weakptr(&weak
, repo
)) < 0)
1061 return git_config_snapshot(out
, weak
);
1064 void git_repository_set_config(git_repository
*repo
, git_config
*config
)
1066 assert(repo
&& config
);
1067 set_config(repo
, config
);
1070 int git_repository_odb__weakptr(git_odb
**out
, git_repository
*repo
)
1074 assert(repo
&& out
);
1076 if (repo
->_odb
== NULL
) {
1077 git_buf odb_path
= GIT_BUF_INIT
;
1080 if ((error
= git_repository_item_path(&odb_path
, repo
,
1081 GIT_REPOSITORY_ITEM_OBJECTS
)) < 0 ||
1082 (error
= git_odb_new(&odb
)) < 0)
1085 GIT_REFCOUNT_OWN(odb
, repo
);
1087 if ((error
= git_odb__set_caps(odb
, GIT_ODB_CAP_FROM_OWNER
)) < 0 ||
1088 (error
= git_odb__add_default_backends(odb
, odb_path
.ptr
, 0, 0)) < 0) {
1093 odb
= git__compare_and_swap(&repo
->_odb
, NULL
, odb
);
1095 GIT_REFCOUNT_OWN(odb
, NULL
);
1099 git_buf_free(&odb_path
);
1106 int git_repository_odb(git_odb
**out
, git_repository
*repo
)
1108 if (git_repository_odb__weakptr(out
, repo
) < 0)
1111 GIT_REFCOUNT_INC(*out
);
1115 void git_repository_set_odb(git_repository
*repo
, git_odb
*odb
)
1117 assert(repo
&& odb
);
1121 int git_repository_refdb__weakptr(git_refdb
**out
, git_repository
*repo
)
1125 assert(out
&& repo
);
1127 if (repo
->_refdb
== NULL
) {
1130 error
= git_refdb_open(&refdb
, repo
);
1132 GIT_REFCOUNT_OWN(refdb
, repo
);
1134 refdb
= git__compare_and_swap(&repo
->_refdb
, NULL
, refdb
);
1135 if (refdb
!= NULL
) {
1136 GIT_REFCOUNT_OWN(refdb
, NULL
);
1137 git_refdb_free(refdb
);
1142 *out
= repo
->_refdb
;
1146 int git_repository_refdb(git_refdb
**out
, git_repository
*repo
)
1148 if (git_repository_refdb__weakptr(out
, repo
) < 0)
1151 GIT_REFCOUNT_INC(*out
);
1155 void git_repository_set_refdb(git_repository
*repo
, git_refdb
*refdb
)
1157 assert(repo
&& refdb
);
1158 set_refdb(repo
, refdb
);
1161 int git_repository_index__weakptr(git_index
**out
, git_repository
*repo
)
1165 assert(out
&& repo
);
1167 if (repo
->_index
== NULL
) {
1168 git_buf index_path
= GIT_BUF_INIT
;
1171 if ((error
= git_buf_joinpath(&index_path
, repo
->gitdir
, GIT_INDEX_FILE
)) < 0)
1174 error
= git_index_open(&index
, index_path
.ptr
);
1176 GIT_REFCOUNT_OWN(index
, repo
);
1178 index
= git__compare_and_swap(&repo
->_index
, NULL
, index
);
1179 if (index
!= NULL
) {
1180 GIT_REFCOUNT_OWN(index
, NULL
);
1181 git_index_free(index
);
1184 error
= git_index_set_caps(repo
->_index
, GIT_INDEXCAP_FROM_OWNER
);
1187 git_buf_free(&index_path
);
1190 *out
= repo
->_index
;
1194 int git_repository_index(git_index
**out
, git_repository
*repo
)
1196 if (git_repository_index__weakptr(out
, repo
) < 0)
1199 GIT_REFCOUNT_INC(*out
);
1203 void git_repository_set_index(git_repository
*repo
, git_index
*index
)
1206 set_index(repo
, index
);
1209 int git_repository_set_namespace(git_repository
*repo
, const char *namespace)
1211 git__free(repo
->namespace);
1213 if (namespace == NULL
) {
1214 repo
->namespace = NULL
;
1218 return (repo
->namespace = git__strdup(namespace)) ? 0 : -1;
1221 const char *git_repository_get_namespace(git_repository
*repo
)
1223 return repo
->namespace;
1227 static int reserved_names_add8dot3(git_repository
*repo
, const char *path
)
1229 char *name
= git_win32_path_8dot3_name(path
);
1230 const char *def
= GIT_DIR_SHORTNAME
;
1231 const char *def_dot_git
= DOT_GIT
;
1232 size_t name_len
, def_len
= CONST_STRLEN(GIT_DIR_SHORTNAME
);
1233 size_t def_dot_git_len
= CONST_STRLEN(DOT_GIT
);
1239 name_len
= strlen(name
);
1241 if ((name_len
== def_len
&& memcmp(name
, def
, def_len
) == 0) ||
1242 (name_len
== def_dot_git_len
&& memcmp(name
, def_dot_git
, def_dot_git_len
) == 0)) {
1247 if ((buf
= git_array_alloc(repo
->reserved_names
)) == NULL
)
1250 git_buf_attach(buf
, name
, name_len
);
1254 bool git_repository__reserved_names(
1255 git_buf
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
)
1257 GIT_UNUSED(include_ntfs
);
1259 if (repo
->reserved_names
.size
== 0) {
1263 /* Add the static defaults */
1264 for (i
= 0; i
< git_repository__reserved_names_win32_len
; i
++) {
1265 if ((buf
= git_array_alloc(repo
->reserved_names
)) == NULL
)
1268 buf
->ptr
= git_repository__reserved_names_win32
[i
].ptr
;
1269 buf
->size
= git_repository__reserved_names_win32
[i
].size
;
1272 /* Try to add any repo-specific reserved names - the gitlink file
1273 * within a submodule or the repository (if the repository directory
1274 * is beneath the workdir). These are typically `.git`, but should
1275 * be protected in case they are not. Note, repo and workdir paths
1276 * are always prettified to end in `/`, so a prefixcmp is safe.
1278 if (!repo
->is_bare
) {
1279 int (*prefixcmp
)(const char *, const char *);
1280 int error
, ignorecase
;
1282 error
= git_repository__cvar(
1283 &ignorecase
, repo
, GIT_CVAR_IGNORECASE
);
1284 prefixcmp
= (error
|| ignorecase
) ? git__prefixcmp_icase
:
1287 if (repo
->gitlink
&&
1288 reserved_names_add8dot3(repo
, repo
->gitlink
) < 0)
1292 prefixcmp(repo
->gitdir
, repo
->workdir
) == 0 &&
1293 reserved_names_add8dot3(repo
, repo
->gitdir
) < 0)
1298 *out
= repo
->reserved_names
.ptr
;
1299 *outlen
= repo
->reserved_names
.size
;
1303 /* Always give good defaults, even on OOM */
1305 *out
= git_repository__reserved_names_win32
;
1306 *outlen
= git_repository__reserved_names_win32_len
;
1311 bool git_repository__reserved_names(
1312 git_buf
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
)
1317 *out
= git_repository__reserved_names_win32
;
1318 *outlen
= git_repository__reserved_names_win32_len
;
1320 *out
= git_repository__reserved_names_posix
;
1321 *outlen
= git_repository__reserved_names_posix_len
;
1328 static int check_repositoryformatversion(git_config
*config
)
1332 error
= git_config_get_int32(&version
, config
, "core.repositoryformatversion");
1333 /* git ignores this if the config variable isn't there */
1334 if (error
== GIT_ENOTFOUND
)
1340 if (GIT_REPO_VERSION
< version
) {
1341 giterr_set(GITERR_REPOSITORY
,
1342 "unsupported repository version %d. Only versions up to %d are supported.",
1343 version
, GIT_REPO_VERSION
);
1350 int git_repository_create_head(const char *git_dir
, const char *ref_name
)
1352 git_buf ref_path
= GIT_BUF_INIT
;
1353 git_filebuf ref
= GIT_FILEBUF_INIT
;
1356 if (git_buf_joinpath(&ref_path
, git_dir
, GIT_HEAD_FILE
) < 0 ||
1357 git_filebuf_open(&ref
, ref_path
.ptr
, 0, GIT_REFS_FILE_MODE
) < 0)
1361 ref_name
= GIT_BRANCH_MASTER
;
1363 if (git__prefixcmp(ref_name
, GIT_REFS_DIR
) == 0)
1366 fmt
= "ref: " GIT_REFS_HEADS_DIR
"%s\n";
1368 if (git_filebuf_printf(&ref
, fmt
, ref_name
) < 0 ||
1369 git_filebuf_commit(&ref
) < 0)
1372 git_buf_free(&ref_path
);
1376 git_buf_free(&ref_path
);
1377 git_filebuf_cleanup(&ref
);
1381 static bool is_chmod_supported(const char *file_path
)
1383 struct stat st1
, st2
;
1385 if (p_stat(file_path
, &st1
) < 0)
1388 if (p_chmod(file_path
, st1
.st_mode
^ S_IXUSR
) < 0)
1391 if (p_stat(file_path
, &st2
) < 0)
1394 return (st1
.st_mode
!= st2
.st_mode
);
1397 static bool is_filesystem_case_insensitive(const char *gitdir_path
)
1399 git_buf path
= GIT_BUF_INIT
;
1400 int is_insensitive
= -1;
1402 if (!git_buf_joinpath(&path
, gitdir_path
, "CoNfIg"))
1403 is_insensitive
= git_path_exists(git_buf_cstr(&path
));
1405 git_buf_free(&path
);
1406 return is_insensitive
;
1409 static bool are_symlinks_supported(const char *wd_path
)
1411 git_buf path
= GIT_BUF_INIT
;
1414 int symlinks_supported
= -1;
1416 if ((fd
= git_futils_mktmp(&path
, wd_path
, 0666)) < 0 ||
1418 p_unlink(path
.ptr
) < 0 ||
1419 p_symlink("testing", path
.ptr
) < 0 ||
1420 p_lstat(path
.ptr
, &st
) < 0)
1421 symlinks_supported
= false;
1423 symlinks_supported
= (S_ISLNK(st
.st_mode
) != 0);
1425 (void)p_unlink(path
.ptr
);
1426 git_buf_free(&path
);
1428 return symlinks_supported
;
1431 static int create_empty_file(const char *path
, mode_t mode
)
1435 if ((fd
= p_creat(path
, mode
)) < 0) {
1436 giterr_set(GITERR_OS
, "error while creating '%s'", path
);
1440 if (p_close(fd
) < 0) {
1441 giterr_set(GITERR_OS
, "error while closing '%s'", path
);
1448 static int repo_local_config(
1450 git_buf
*config_dir
,
1451 git_repository
*repo
,
1452 const char *repo_dir
)
1456 const char *cfg_path
;
1458 if (git_buf_joinpath(config_dir
, repo_dir
, GIT_CONFIG_FILENAME_INREPO
) < 0)
1460 cfg_path
= git_buf_cstr(config_dir
);
1462 /* make LOCAL config if missing */
1463 if (!git_path_isfile(cfg_path
) &&
1464 (error
= create_empty_file(cfg_path
, GIT_CONFIG_FILE_MODE
)) < 0)
1467 /* if no repo, just open that file directly */
1469 return git_config_open_ondisk(out
, cfg_path
);
1471 /* otherwise, open parent config and get that level */
1472 if ((error
= git_repository_config__weakptr(&parent
, repo
)) < 0)
1475 if (git_config_open_level(out
, parent
, GIT_CONFIG_LEVEL_LOCAL
) < 0) {
1478 if (!(error
= git_config_add_file_ondisk(
1479 parent
, cfg_path
, GIT_CONFIG_LEVEL_LOCAL
, false)))
1480 error
= git_config_open_level(out
, parent
, GIT_CONFIG_LEVEL_LOCAL
);
1483 git_config_free(parent
);
1488 static int repo_init_fs_configs(
1490 const char *cfg_path
,
1491 const char *repo_dir
,
1492 const char *work_dir
,
1493 bool update_ignorecase
)
1498 work_dir
= repo_dir
;
1500 if ((error
= git_config_set_bool(
1501 cfg
, "core.filemode", is_chmod_supported(cfg_path
))) < 0)
1504 if (!are_symlinks_supported(work_dir
)) {
1505 if ((error
= git_config_set_bool(cfg
, "core.symlinks", false)) < 0)
1507 } else if (git_config_delete_entry(cfg
, "core.symlinks") < 0)
1510 if (update_ignorecase
) {
1511 if (is_filesystem_case_insensitive(repo_dir
)) {
1512 if ((error
= git_config_set_bool(cfg
, "core.ignorecase", true)) < 0)
1514 } else if (git_config_delete_entry(cfg
, "core.ignorecase") < 0)
1518 #ifdef GIT_USE_ICONV
1519 if ((error
= git_config_set_bool(
1520 cfg
, "core.precomposeunicode",
1521 git_path_does_fs_decompose_unicode(work_dir
))) < 0)
1523 /* on non-iconv platforms, don't even set core.precomposeunicode */
1529 static int repo_init_config(
1530 const char *repo_dir
,
1531 const char *work_dir
,
1536 git_buf cfg_path
= GIT_BUF_INIT
, worktree_path
= GIT_BUF_INIT
;
1537 git_config
*config
= NULL
;
1538 bool is_bare
= ((flags
& GIT_REPOSITORY_INIT_BARE
) != 0);
1539 bool is_reinit
= ((flags
& GIT_REPOSITORY_INIT__IS_REINIT
) != 0);
1541 if ((error
= repo_local_config(&config
, &cfg_path
, NULL
, repo_dir
)) < 0)
1544 if (is_reinit
&& (error
= check_repositoryformatversion(config
)) < 0)
1547 #define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \
1548 if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
1549 goto cleanup; } while (0)
1551 SET_REPO_CONFIG(bool, "core.bare", is_bare
);
1552 SET_REPO_CONFIG(int32
, "core.repositoryformatversion", GIT_REPO_VERSION
);
1554 if ((error
= repo_init_fs_configs(
1555 config
, cfg_path
.ptr
, repo_dir
, work_dir
, !is_reinit
)) < 0)
1559 SET_REPO_CONFIG(bool, "core.logallrefupdates", true);
1561 if (!(flags
& GIT_REPOSITORY_INIT__NATURAL_WD
)) {
1562 if ((error
= git_buf_sets(&worktree_path
, work_dir
)) < 0)
1565 if ((flags
& GIT_REPOSITORY_INIT_RELATIVE_GITLINK
))
1566 if ((error
= git_path_make_relative(&worktree_path
, repo_dir
)) < 0)
1569 SET_REPO_CONFIG(string
, "core.worktree", worktree_path
.ptr
);
1570 } else if (is_reinit
) {
1571 if (git_config_delete_entry(config
, "core.worktree") < 0)
1576 if (mode
== GIT_REPOSITORY_INIT_SHARED_GROUP
) {
1577 SET_REPO_CONFIG(int32
, "core.sharedrepository", 1);
1578 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1580 else if (mode
== GIT_REPOSITORY_INIT_SHARED_ALL
) {
1581 SET_REPO_CONFIG(int32
, "core.sharedrepository", 2);
1582 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1586 git_buf_free(&cfg_path
);
1587 git_buf_free(&worktree_path
);
1588 git_config_free(config
);
1593 static int repo_reinit_submodule_fs(git_submodule
*sm
, const char *n
, void *p
)
1595 git_repository
*smrepo
= NULL
;
1596 GIT_UNUSED(n
); GIT_UNUSED(p
);
1598 if (git_submodule_open(&smrepo
, sm
) < 0 ||
1599 git_repository_reinit_filesystem(smrepo
, true) < 0)
1601 git_repository_free(smrepo
);
1606 int git_repository_reinit_filesystem(git_repository
*repo
, int recurse
)
1609 git_buf path
= GIT_BUF_INIT
;
1610 git_config
*config
= NULL
;
1611 const char *repo_dir
= git_repository_path(repo
);
1613 if (!(error
= repo_local_config(&config
, &path
, repo
, repo_dir
)))
1614 error
= repo_init_fs_configs(
1615 config
, path
.ptr
, repo_dir
, git_repository_workdir(repo
), true);
1617 git_config_free(config
);
1618 git_buf_free(&path
);
1620 git_repository__cvar_cache_clear(repo
);
1622 if (!repo
->is_bare
&& recurse
)
1623 (void)git_submodule_foreach(repo
, repo_reinit_submodule_fs
, NULL
);
1628 static int repo_write_template(
1629 const char *git_dir
,
1630 bool allow_overwrite
,
1634 const char *content
)
1636 git_buf path
= GIT_BUF_INIT
;
1637 int fd
, error
= 0, flags
;
1639 if (git_buf_joinpath(&path
, git_dir
, file
) < 0)
1642 if (allow_overwrite
)
1643 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
1645 flags
= O_WRONLY
| O_CREAT
| O_EXCL
;
1647 fd
= p_open(git_buf_cstr(&path
), flags
, mode
);
1650 error
= p_write(fd
, content
, strlen(content
));
1654 else if (errno
!= EEXIST
)
1658 if (!error
&& hidden
) {
1659 if (git_win32__set_hidden(path
.ptr
, true) < 0)
1666 git_buf_free(&path
);
1669 giterr_set(GITERR_OS
,
1670 "failed to initialize repository with template '%s'", file
);
1675 static int repo_write_gitlink(
1676 const char *in_dir
, const char *to_repo
, bool use_relative_path
)
1679 git_buf buf
= GIT_BUF_INIT
;
1680 git_buf path_to_repo
= GIT_BUF_INIT
;
1683 git_path_dirname_r(&buf
, to_repo
);
1684 git_path_to_dir(&buf
);
1685 if (git_buf_oom(&buf
))
1688 /* don't write gitlink to natural workdir */
1689 if (git__suffixcmp(to_repo
, "/" DOT_GIT
"/") == 0 &&
1690 strcmp(in_dir
, buf
.ptr
) == 0)
1692 error
= GIT_PASSTHROUGH
;
1696 if ((error
= git_buf_joinpath(&buf
, in_dir
, DOT_GIT
)) < 0)
1699 if (!p_stat(buf
.ptr
, &st
) && !S_ISREG(st
.st_mode
)) {
1700 giterr_set(GITERR_REPOSITORY
,
1701 "cannot overwrite gitlink file into path '%s'", in_dir
);
1702 error
= GIT_EEXISTS
;
1706 git_buf_clear(&buf
);
1708 error
= git_buf_sets(&path_to_repo
, to_repo
);
1710 if (!error
&& use_relative_path
)
1711 error
= git_path_make_relative(&path_to_repo
, in_dir
);
1714 error
= git_buf_join(&buf
, ' ', GIT_FILE_CONTENT_PREFIX
, path_to_repo
.ptr
);
1717 error
= repo_write_template(in_dir
, true, DOT_GIT
, 0666, true, buf
.ptr
);
1721 git_buf_free(&path_to_repo
);
1725 static mode_t
pick_dir_mode(git_repository_init_options
*opts
)
1727 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_UMASK
)
1729 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_GROUP
)
1730 return (0775 | S_ISGID
);
1731 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_ALL
)
1732 return (0777 | S_ISGID
);
1736 #include "repo_template.h"
1738 static int repo_init_structure(
1739 const char *repo_dir
,
1740 const char *work_dir
,
1741 git_repository_init_options
*opts
)
1744 repo_template_item
*tpl
;
1746 ((opts
->flags
& GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE
) != 0);
1747 mode_t dmode
= pick_dir_mode(opts
);
1748 bool chmod
= opts
->mode
!= GIT_REPOSITORY_INIT_SHARED_UMASK
;
1750 /* Hide the ".git" directory */
1752 if ((opts
->flags
& GIT_REPOSITORY_INIT__HAS_DOTGIT
) != 0) {
1753 if (git_win32__set_hidden(repo_dir
, true) < 0) {
1754 giterr_set(GITERR_OS
,
1755 "failed to mark Git repository folder as hidden");
1761 /* Create the .git gitlink if appropriate */
1762 if ((opts
->flags
& GIT_REPOSITORY_INIT_BARE
) == 0 &&
1763 (opts
->flags
& GIT_REPOSITORY_INIT__NATURAL_WD
) == 0)
1765 if (repo_write_gitlink(work_dir
, repo_dir
, opts
->flags
& GIT_REPOSITORY_INIT_RELATIVE_GITLINK
) < 0)
1769 /* Copy external template if requested */
1771 git_config
*cfg
= NULL
;
1772 const char *tdir
= NULL
;
1773 bool default_template
= false;
1774 git_buf template_buf
= GIT_BUF_INIT
;
1776 if (opts
->template_path
)
1777 tdir
= opts
->template_path
;
1778 else if ((error
= git_config_open_default(&cfg
)) >= 0) {
1779 if (!git_config_get_path(&template_buf
, cfg
, "init.templatedir"))
1780 tdir
= template_buf
.ptr
;
1785 if (!(error
= git_sysdir_find_template_dir(&template_buf
)))
1786 tdir
= template_buf
.ptr
;
1787 default_template
= true;
1791 uint32_t cpflags
= GIT_CPDIR_COPY_SYMLINKS
|
1792 GIT_CPDIR_SIMPLE_TO_MODE
|
1793 GIT_CPDIR_COPY_DOTFILES
;
1794 if (opts
->mode
!= GIT_REPOSITORY_INIT_SHARED_UMASK
)
1795 cpflags
|= GIT_CPDIR_CHMOD_DIRS
;
1796 error
= git_futils_cp_r(tdir
, repo_dir
, cpflags
, dmode
);
1799 git_buf_free(&template_buf
);
1800 git_config_free(cfg
);
1803 if (!default_template
)
1806 /* if template was default, ignore error and use internal */
1808 external_tpl
= false;
1813 /* Copy internal template
1814 * - always ensure existence of dirs
1815 * - only create files if no external template was specified
1817 for (tpl
= repo_template
; !error
&& tpl
->path
; ++tpl
) {
1818 if (!tpl
->content
) {
1819 uint32_t mkdir_flags
= GIT_MKDIR_PATH
;
1821 mkdir_flags
|= GIT_MKDIR_CHMOD
;
1823 error
= git_futils_mkdir_relative(
1824 tpl
->path
, repo_dir
, dmode
, mkdir_flags
, NULL
);
1826 else if (!external_tpl
) {
1827 const char *content
= tpl
->content
;
1829 if (opts
->description
&& strcmp(tpl
->path
, GIT_DESC_FILE
) == 0)
1830 content
= opts
->description
;
1832 error
= repo_write_template(
1833 repo_dir
, false, tpl
->path
, tpl
->mode
, false, content
);
1840 static int mkdir_parent(git_buf
*buf
, uint32_t mode
, bool skip2
)
1842 /* When making parent directories during repository initialization
1843 * don't try to set gid or grant world write access
1845 return git_futils_mkdir(
1846 buf
->ptr
, mode
& ~(S_ISGID
| 0002),
1847 GIT_MKDIR_PATH
| GIT_MKDIR_VERIFY_DIR
|
1848 (skip2
? GIT_MKDIR_SKIP_LAST2
: GIT_MKDIR_SKIP_LAST
));
1851 static int repo_init_directories(
1854 const char *given_repo
,
1855 git_repository_init_options
*opts
)
1858 bool is_bare
, add_dotgit
, has_dotgit
, natural_wd
;
1861 /* There are three possible rules for what we are allowed to create:
1862 * - MKPATH means anything we need
1863 * - MKDIR means just the .git directory and its parent and the workdir
1864 * - Neither means only the .git directory can be created
1866 * There are 5 "segments" of path that we might need to deal with:
1867 * 1. The .git directory
1868 * 2. The parent of the .git directory
1869 * 3. Everything above the parent of the .git directory
1870 * 4. The working directory (often the same as #2)
1871 * 5. Everything above the working directory (often the same as #3)
1873 * For all directories created, we start with the init_mode value for
1874 * permissions and then strip off bits in some cases:
1876 * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH
1877 * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID
1878 * For all rules, we create #1 using the untouched init_mode
1881 /* set up repo path */
1883 is_bare
= ((opts
->flags
& GIT_REPOSITORY_INIT_BARE
) != 0);
1886 (opts
->flags
& GIT_REPOSITORY_INIT_NO_DOTGIT_DIR
) == 0 &&
1888 git__suffixcmp(given_repo
, "/" DOT_GIT
) != 0 &&
1889 git__suffixcmp(given_repo
, "/" GIT_DIR
) != 0;
1891 if (git_buf_joinpath(repo_path
, given_repo
, add_dotgit
? GIT_DIR
: "") < 0)
1894 has_dotgit
= (git__suffixcmp(repo_path
->ptr
, "/" GIT_DIR
) == 0);
1896 opts
->flags
|= GIT_REPOSITORY_INIT__HAS_DOTGIT
;
1898 /* set up workdir path */
1901 if (opts
->workdir_path
) {
1902 if (git_path_join_unrooted(
1903 wd_path
, opts
->workdir_path
, repo_path
->ptr
, NULL
) < 0)
1905 } else if (has_dotgit
) {
1906 if (git_path_dirname_r(wd_path
, repo_path
->ptr
) < 0)
1909 giterr_set(GITERR_REPOSITORY
, "cannot pick working directory"
1910 " for non-bare repository that isn't a '.git' directory");
1914 if (git_path_to_dir(wd_path
) < 0)
1917 git_buf_clear(wd_path
);
1922 wd_path
->size
> 0 &&
1923 wd_path
->size
+ strlen(GIT_DIR
) == repo_path
->size
&&
1924 memcmp(repo_path
->ptr
, wd_path
->ptr
, wd_path
->size
) == 0;
1926 opts
->flags
|= GIT_REPOSITORY_INIT__NATURAL_WD
;
1928 /* create directories as needed / requested */
1930 dirmode
= pick_dir_mode(opts
);
1932 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0) {
1933 /* create path #5 */
1934 if (wd_path
->size
> 0 &&
1935 (error
= mkdir_parent(wd_path
, dirmode
, false)) < 0)
1938 /* create path #3 (if not the same as #5) */
1940 (error
= mkdir_parent(repo_path
, dirmode
, has_dotgit
)) < 0)
1944 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKDIR
) != 0 ||
1945 (opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0)
1947 /* create path #4 */
1948 if (wd_path
->size
> 0 &&
1949 (error
= git_futils_mkdir(
1950 wd_path
->ptr
, dirmode
& ~S_ISGID
,
1951 GIT_MKDIR_VERIFY_DIR
)) < 0)
1954 /* create path #2 (if not the same as #4) */
1956 (error
= git_futils_mkdir(
1957 repo_path
->ptr
, dirmode
& ~S_ISGID
,
1958 GIT_MKDIR_VERIFY_DIR
| GIT_MKDIR_SKIP_LAST
)) < 0)
1962 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKDIR
) != 0 ||
1963 (opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0 ||
1966 /* create path #1 */
1967 error
= git_futils_mkdir(repo_path
->ptr
, dirmode
,
1968 GIT_MKDIR_VERIFY_DIR
| ((dirmode
& S_ISGID
) ? GIT_MKDIR_CHMOD
: 0));
1971 /* prettify both directories now that they are created */
1974 error
= git_path_prettify_dir(repo_path
, repo_path
->ptr
, NULL
);
1976 if (!error
&& wd_path
->size
> 0)
1977 error
= git_path_prettify_dir(wd_path
, wd_path
->ptr
, NULL
);
1983 static int repo_init_create_origin(git_repository
*repo
, const char *url
)
1988 if (!(error
= git_remote_create(&remote
, repo
, GIT_REMOTE_ORIGIN
, url
))) {
1989 git_remote_free(remote
);
1995 int git_repository_init(
1996 git_repository
**repo_out
, const char *path
, unsigned is_bare
)
1998 git_repository_init_options opts
= GIT_REPOSITORY_INIT_OPTIONS_INIT
;
2000 opts
.flags
= GIT_REPOSITORY_INIT_MKPATH
; /* don't love this default */
2002 opts
.flags
|= GIT_REPOSITORY_INIT_BARE
;
2004 return git_repository_init_ext(repo_out
, path
, &opts
);
2007 int git_repository_init_ext(
2008 git_repository
**out
,
2009 const char *given_repo
,
2010 git_repository_init_options
*opts
)
2013 git_buf repo_path
= GIT_BUF_INIT
, wd_path
= GIT_BUF_INIT
,
2014 common_path
= GIT_BUF_INIT
;
2017 assert(out
&& given_repo
&& opts
);
2019 GITERR_CHECK_VERSION(opts
, GIT_REPOSITORY_INIT_OPTIONS_VERSION
, "git_repository_init_options");
2021 error
= repo_init_directories(&repo_path
, &wd_path
, given_repo
, opts
);
2025 wd
= (opts
->flags
& GIT_REPOSITORY_INIT_BARE
) ? NULL
: git_buf_cstr(&wd_path
);
2026 if (valid_repository_path(&repo_path
, &common_path
)) {
2028 if ((opts
->flags
& GIT_REPOSITORY_INIT_NO_REINIT
) != 0) {
2029 giterr_set(GITERR_REPOSITORY
,
2030 "attempt to reinitialize '%s'", given_repo
);
2031 error
= GIT_EEXISTS
;
2035 opts
->flags
|= GIT_REPOSITORY_INIT__IS_REINIT
;
2037 error
= repo_init_config(
2038 repo_path
.ptr
, wd
, opts
->flags
, opts
->mode
);
2040 /* TODO: reinitialize the templates */
2043 if (!(error
= repo_init_structure(
2044 repo_path
.ptr
, wd
, opts
)) &&
2045 !(error
= repo_init_config(
2046 repo_path
.ptr
, wd
, opts
->flags
, opts
->mode
)))
2047 error
= git_repository_create_head(
2048 repo_path
.ptr
, opts
->initial_head
);
2053 error
= git_repository_open(out
, repo_path
.ptr
);
2055 if (!error
&& opts
->origin_url
)
2056 error
= repo_init_create_origin(*out
, opts
->origin_url
);
2059 git_buf_free(&common_path
);
2060 git_buf_free(&repo_path
);
2061 git_buf_free(&wd_path
);
2066 int git_repository_head_detached(git_repository
*repo
)
2069 git_odb
*odb
= NULL
;
2072 if (git_repository_odb__weakptr(&odb
, repo
) < 0)
2075 if (git_reference_lookup(&ref
, repo
, GIT_HEAD_FILE
) < 0)
2078 if (git_reference_type(ref
) == GIT_REF_SYMBOLIC
) {
2079 git_reference_free(ref
);
2083 exists
= git_odb_exists(odb
, git_reference_target(ref
));
2085 git_reference_free(ref
);
2089 static int get_worktree_file_path(git_buf
*out
, git_repository
*repo
, const char *worktree
, const char *file
)
2092 return git_buf_printf(out
, "%s/worktrees/%s/%s", repo
->commondir
, worktree
, file
);
2095 int git_repository_head_detached_for_worktree(git_repository
*repo
, const char *name
)
2097 git_reference
*ref
= NULL
;
2100 assert(repo
&& name
);
2102 if ((error
= git_repository_head_for_worktree(&ref
, repo
, name
)) < 0)
2105 error
= (git_reference_type(ref
) != GIT_REF_SYMBOLIC
);
2107 git_reference_free(ref
);
2112 int git_repository_head(git_reference
**head_out
, git_repository
*repo
)
2114 git_reference
*head
;
2117 if ((error
= git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
)) < 0)
2120 if (git_reference_type(head
) == GIT_REF_OID
) {
2125 error
= git_reference_lookup_resolved(head_out
, repo
, git_reference_symbolic_target(head
), -1);
2126 git_reference_free(head
);
2128 return error
== GIT_ENOTFOUND
? GIT_EUNBORNBRANCH
: error
;
2131 int git_repository_head_for_worktree(git_reference
**out
, git_repository
*repo
, const char *name
)
2133 git_buf path
= GIT_BUF_INIT
;
2134 git_reference
*head
= NULL
;
2137 assert(out
&& repo
&& name
);
2141 if ((error
= get_worktree_file_path(&path
, repo
, name
, GIT_HEAD_FILE
)) < 0 ||
2142 (error
= git_reference__read_head(&head
, repo
, path
.ptr
)) < 0)
2145 if (git_reference_type(head
) != GIT_REF_OID
) {
2146 git_reference
*resolved
;
2148 error
= git_reference_lookup_resolved(&resolved
, repo
, git_reference_symbolic_target(head
), -1);
2149 git_reference_free(head
);
2157 git_reference_free(head
);
2159 git_buf_free(&path
);
2164 int git_repository_foreach_head(git_repository
*repo
, git_repository_foreach_head_cb cb
, void *payload
)
2166 git_strarray worktrees
= GIT_VECTOR_INIT
;
2167 git_buf path
= GIT_BUF_INIT
;
2171 /* Execute callback for HEAD of commondir */
2172 if ((error
= git_buf_joinpath(&path
, repo
->commondir
, GIT_HEAD_FILE
)) < 0 ||
2173 (error
= cb(repo
, path
.ptr
, payload
) != 0))
2176 if ((error
= git_worktree_list(&worktrees
, repo
)) < 0) {
2181 /* Execute callback for all worktree HEADs */
2182 for (i
= 0; i
< worktrees
.count
; i
++) {
2183 if (get_worktree_file_path(&path
, repo
, worktrees
.strings
[i
], GIT_HEAD_FILE
) < 0)
2186 if ((error
= cb(repo
, path
.ptr
, payload
)) != 0)
2191 git_buf_free(&path
);
2192 git_strarray_free(&worktrees
);
2196 int git_repository_head_unborn(git_repository
*repo
)
2198 git_reference
*ref
= NULL
;
2201 error
= git_repository_head(&ref
, repo
);
2202 git_reference_free(ref
);
2204 if (error
== GIT_EUNBORNBRANCH
) {
2215 static int at_least_one_cb(const char *refname
, void *payload
)
2217 GIT_UNUSED(refname
);
2218 GIT_UNUSED(payload
);
2219 return GIT_PASSTHROUGH
;
2222 static int repo_contains_no_reference(git_repository
*repo
)
2224 int error
= git_reference_foreach_name(repo
, &at_least_one_cb
, NULL
);
2226 if (error
== GIT_PASSTHROUGH
)
2235 int git_repository_is_empty(git_repository
*repo
)
2237 git_reference
*head
= NULL
;
2240 if (git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
) < 0)
2243 if (git_reference_type(head
) == GIT_REF_SYMBOLIC
)
2245 (strcmp(git_reference_symbolic_target(head
),
2246 GIT_REFS_HEADS_DIR
"master") == 0) &&
2247 repo_contains_no_reference(repo
);
2249 git_reference_free(head
);
2254 int git_repository_item_path(git_buf
*out
, git_repository
*repo
, git_repository_item_t item
)
2258 switch (items
[item
].parent
) {
2259 case GIT_REPOSITORY_ITEM_GITDIR
:
2260 parent
= git_repository_path(repo
);
2262 case GIT_REPOSITORY_ITEM_WORKDIR
:
2263 parent
= git_repository_workdir(repo
);
2265 case GIT_REPOSITORY_ITEM_COMMONDIR
:
2266 parent
= git_repository_commondir(repo
);
2269 giterr_set(GITERR_INVALID
, "Invalid item directory");
2273 if (parent
== NULL
) {
2274 giterr_set(GITERR_INVALID
, "Path cannot exist in repository");
2278 if (git_buf_sets(out
, parent
) < 0)
2281 if (items
[item
].name
) {
2282 if (git_buf_joinpath(out
, parent
, items
[item
].name
) < 0)
2286 if (items
[item
].directory
) {
2287 if (git_path_to_dir(out
) < 0)
2294 const char *git_repository_path(git_repository
*repo
)
2297 return repo
->gitdir
;
2300 const char *git_repository_workdir(git_repository
*repo
)
2307 return repo
->workdir
;
2310 const char *git_repository_commondir(git_repository
*repo
)
2313 return repo
->commondir
;
2316 int git_repository_set_workdir(
2317 git_repository
*repo
, const char *workdir
, int update_gitlink
)
2320 git_buf path
= GIT_BUF_INIT
;
2322 assert(repo
&& workdir
);
2324 if (git_path_prettify_dir(&path
, workdir
, NULL
) < 0)
2327 if (repo
->workdir
&& strcmp(repo
->workdir
, path
.ptr
) == 0)
2330 if (update_gitlink
) {
2333 if (git_repository_config__weakptr(&config
, repo
) < 0)
2336 error
= repo_write_gitlink(path
.ptr
, git_repository_path(repo
), false);
2338 /* passthrough error means gitlink is unnecessary */
2339 if (error
== GIT_PASSTHROUGH
)
2340 error
= git_config_delete_entry(config
, "core.worktree");
2342 error
= git_config_set_string(config
, "core.worktree", path
.ptr
);
2345 error
= git_config_set_bool(config
, "core.bare", false);
2349 char *old_workdir
= repo
->workdir
;
2351 repo
->workdir
= git_buf_detach(&path
);
2354 git__free(old_workdir
);
2360 int git_repository_is_bare(git_repository
*repo
)
2363 return repo
->is_bare
;
2366 int git_repository_is_worktree(git_repository
*repo
)
2369 return repo
->is_worktree
;
2372 int git_repository_set_bare(git_repository
*repo
)
2382 if ((error
= git_repository_config__weakptr(&config
, repo
)) < 0)
2385 if ((error
= git_config_set_bool(config
, "core.bare", true)) < 0)
2388 if ((error
= git_config__update_entry(config
, "core.worktree", NULL
, true, true)) < 0)
2391 git__free(repo
->workdir
);
2392 repo
->workdir
= NULL
;
2398 int git_repository_head_tree(git_tree
**tree
, git_repository
*repo
)
2400 git_reference
*head
;
2404 if ((error
= git_repository_head(&head
, repo
)) < 0)
2407 if ((error
= git_reference_peel(&obj
, head
, GIT_OBJ_TREE
)) < 0)
2410 *tree
= (git_tree
*)obj
;
2413 git_reference_free(head
);
2417 int git_repository__set_orig_head(git_repository
*repo
, const git_oid
*orig_head
)
2419 git_filebuf file
= GIT_FILEBUF_INIT
;
2420 git_buf file_path
= GIT_BUF_INIT
;
2421 char orig_head_str
[GIT_OID_HEXSZ
];
2424 git_oid_fmt(orig_head_str
, orig_head
);
2426 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_ORIG_HEAD_FILE
)) == 0 &&
2427 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_FORCE
, GIT_MERGE_FILE_MODE
)) == 0 &&
2428 (error
= git_filebuf_printf(&file
, "%.*s\n", GIT_OID_HEXSZ
, orig_head_str
)) == 0)
2429 error
= git_filebuf_commit(&file
);
2432 git_filebuf_cleanup(&file
);
2434 git_buf_free(&file_path
);
2439 int git_repository_message(git_buf
*out
, git_repository
*repo
)
2441 git_buf path
= GIT_BUF_INIT
;
2445 git_buf_sanitize(out
);
2447 if (git_buf_joinpath(&path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
) < 0)
2450 if ((error
= p_stat(git_buf_cstr(&path
), &st
)) < 0) {
2451 if (errno
== ENOENT
)
2452 error
= GIT_ENOTFOUND
;
2453 giterr_set(GITERR_OS
, "could not access message file");
2455 error
= git_futils_readbuffer(out
, git_buf_cstr(&path
));
2458 git_buf_free(&path
);
2463 int git_repository_message_remove(git_repository
*repo
)
2465 git_buf path
= GIT_BUF_INIT
;
2468 if (git_buf_joinpath(&path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
) < 0)
2471 error
= p_unlink(git_buf_cstr(&path
));
2472 git_buf_free(&path
);
2477 int git_repository_hashfile(
2479 git_repository
*repo
,
2482 const char *as_path
)
2485 git_filter_list
*fl
= NULL
;
2488 git_buf full_path
= GIT_BUF_INIT
;
2490 assert(out
&& path
&& repo
); /* as_path can be NULL */
2492 /* At some point, it would be nice if repo could be NULL to just
2493 * apply filter rules defined in system and global files, but for
2494 * now that is not possible because git_filters_load() needs it.
2497 error
= git_path_join_unrooted(
2498 &full_path
, path
, git_repository_workdir(repo
), NULL
);
2505 /* passing empty string for "as_path" indicated --no-filters */
2506 if (strlen(as_path
) > 0) {
2507 error
= git_filter_list_load(
2508 &fl
, repo
, NULL
, as_path
,
2509 GIT_FILTER_TO_ODB
, GIT_FILTER_DEFAULT
);
2516 /* at this point, error is a count of the number of loaded filters */
2518 fd
= git_futils_open_ro(full_path
.ptr
);
2524 len
= git_futils_filesize(fd
);
2530 if (!git__is_sizet(len
)) {
2531 giterr_set(GITERR_OS
, "file size overflow for 32-bit systems");
2536 error
= git_odb__hashfd_filtered(out
, fd
, (size_t)len
, type
, fl
);
2541 git_filter_list_free(fl
);
2542 git_buf_free(&full_path
);
2547 static int checkout_message(git_buf
*out
, git_reference
*old
, const char *new)
2549 git_buf_puts(out
, "checkout: moving from ");
2551 if (git_reference_type(old
) == GIT_REF_SYMBOLIC
)
2552 git_buf_puts(out
, git_reference__shorthand(git_reference_symbolic_target(old
)));
2554 git_buf_puts(out
, git_oid_tostr_s(git_reference_target(old
)));
2556 git_buf_puts(out
, " to ");
2558 if (git_reference__is_branch(new) ||
2559 git_reference__is_tag(new) ||
2560 git_reference__is_remote(new))
2561 git_buf_puts(out
, git_reference__shorthand(new));
2563 git_buf_puts(out
, new);
2565 if (git_buf_oom(out
))
2571 static int detach(git_repository
*repo
, const git_oid
*id
, const char *new)
2574 git_buf log_message
= GIT_BUF_INIT
;
2575 git_object
*object
= NULL
, *peeled
= NULL
;
2576 git_reference
*new_head
= NULL
, *current
= NULL
;
2580 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2583 if ((error
= git_object_lookup(&object
, repo
, id
, GIT_OBJ_ANY
)) < 0)
2586 if ((error
= git_object_peel(&peeled
, object
, GIT_OBJ_COMMIT
)) < 0)
2590 new = git_oid_tostr_s(git_object_id(peeled
));
2592 if ((error
= checkout_message(&log_message
, current
, new)) < 0)
2595 error
= git_reference_create(&new_head
, repo
, GIT_HEAD_FILE
, git_object_id(peeled
), true, git_buf_cstr(&log_message
));
2598 git_buf_free(&log_message
);
2599 git_object_free(object
);
2600 git_object_free(peeled
);
2601 git_reference_free(current
);
2602 git_reference_free(new_head
);
2606 int git_repository_set_head(
2607 git_repository
* repo
,
2608 const char* refname
)
2610 git_reference
*ref
= NULL
, *current
= NULL
, *new_head
= NULL
;
2611 git_buf log_message
= GIT_BUF_INIT
;
2614 assert(repo
&& refname
);
2616 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2619 if ((error
= checkout_message(&log_message
, current
, refname
)) < 0)
2622 error
= git_reference_lookup(&ref
, repo
, refname
);
2623 if (error
< 0 && error
!= GIT_ENOTFOUND
)
2626 if (ref
&& current
->type
== GIT_REF_SYMBOLIC
&& git__strcmp(current
->target
.symbolic
, ref
->name
) &&
2627 git_reference_is_branch(ref
) && git_branch_is_checked_out(ref
)) {
2628 giterr_set(GITERR_REPOSITORY
, "cannot set HEAD to reference '%s' as it is the current HEAD "
2629 "of a linked repository.", git_reference_name(ref
));
2635 if (git_reference_is_branch(ref
)) {
2636 error
= git_reference_symbolic_create(&new_head
, repo
, GIT_HEAD_FILE
,
2637 git_reference_name(ref
), true, git_buf_cstr(&log_message
));
2639 error
= detach(repo
, git_reference_target(ref
),
2640 git_reference_is_tag(ref
) || git_reference_is_remote(ref
) ? refname
: NULL
);
2642 } else if (git_reference__is_branch(refname
)) {
2643 error
= git_reference_symbolic_create(&new_head
, repo
, GIT_HEAD_FILE
, refname
,
2644 true, git_buf_cstr(&log_message
));
2648 git_buf_free(&log_message
);
2649 git_reference_free(current
);
2650 git_reference_free(ref
);
2651 git_reference_free(new_head
);
2655 int git_repository_set_head_detached(
2656 git_repository
* repo
,
2657 const git_oid
* commitish
)
2659 return detach(repo
, commitish
, NULL
);
2662 int git_repository_set_head_detached_from_annotated(
2663 git_repository
*repo
,
2664 const git_annotated_commit
*commitish
)
2666 assert(repo
&& commitish
);
2668 return detach(repo
, git_annotated_commit_id(commitish
), commitish
->description
);
2671 int git_repository_detach_head(git_repository
* repo
)
2673 git_reference
*old_head
= NULL
, *new_head
= NULL
, *current
= NULL
;
2674 git_object
*object
= NULL
;
2675 git_buf log_message
= GIT_BUF_INIT
;
2680 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2683 if ((error
= git_repository_head(&old_head
, repo
)) < 0)
2686 if ((error
= git_object_lookup(&object
, repo
, git_reference_target(old_head
), GIT_OBJ_COMMIT
)) < 0)
2689 if ((error
= checkout_message(&log_message
, current
, git_oid_tostr_s(git_object_id(object
)))) < 0)
2692 error
= git_reference_create(&new_head
, repo
, GIT_HEAD_FILE
, git_reference_target(old_head
),
2693 1, git_buf_cstr(&log_message
));
2696 git_buf_free(&log_message
);
2697 git_object_free(object
);
2698 git_reference_free(old_head
);
2699 git_reference_free(new_head
);
2700 git_reference_free(current
);
2705 * Loosely ported from git.git
2706 * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289
2708 int git_repository_state(git_repository
*repo
)
2710 git_buf repo_path
= GIT_BUF_INIT
;
2711 int state
= GIT_REPOSITORY_STATE_NONE
;
2715 if (git_buf_puts(&repo_path
, repo
->gitdir
) < 0)
2718 if (git_path_contains_file(&repo_path
, GIT_REBASE_MERGE_INTERACTIVE_FILE
))
2719 state
= GIT_REPOSITORY_STATE_REBASE_INTERACTIVE
;
2720 else if (git_path_contains_dir(&repo_path
, GIT_REBASE_MERGE_DIR
))
2721 state
= GIT_REPOSITORY_STATE_REBASE_MERGE
;
2722 else if (git_path_contains_file(&repo_path
, GIT_REBASE_APPLY_REBASING_FILE
))
2723 state
= GIT_REPOSITORY_STATE_REBASE
;
2724 else if (git_path_contains_file(&repo_path
, GIT_REBASE_APPLY_APPLYING_FILE
))
2725 state
= GIT_REPOSITORY_STATE_APPLY_MAILBOX
;
2726 else if (git_path_contains_dir(&repo_path
, GIT_REBASE_APPLY_DIR
))
2727 state
= GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE
;
2728 else if (git_path_contains_file(&repo_path
, GIT_MERGE_HEAD_FILE
))
2729 state
= GIT_REPOSITORY_STATE_MERGE
;
2730 else if (git_path_contains_file(&repo_path
, GIT_REVERT_HEAD_FILE
)) {
2731 state
= GIT_REPOSITORY_STATE_REVERT
;
2732 if (git_path_contains_file(&repo_path
, GIT_SEQUENCER_TODO_FILE
)) {
2733 state
= GIT_REPOSITORY_STATE_REVERT_SEQUENCE
;
2735 } else if (git_path_contains_file(&repo_path
, GIT_CHERRYPICK_HEAD_FILE
)) {
2736 state
= GIT_REPOSITORY_STATE_CHERRYPICK
;
2737 if (git_path_contains_file(&repo_path
, GIT_SEQUENCER_TODO_FILE
)) {
2738 state
= GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE
;
2740 } else if (git_path_contains_file(&repo_path
, GIT_BISECT_LOG_FILE
))
2741 state
= GIT_REPOSITORY_STATE_BISECT
;
2743 git_buf_free(&repo_path
);
2747 int git_repository__cleanup_files(
2748 git_repository
*repo
, const char *files
[], size_t files_len
)
2750 git_buf buf
= GIT_BUF_INIT
;
2754 for (error
= 0, i
= 0; !error
&& i
< files_len
; ++i
) {
2757 if (git_buf_joinpath(&buf
, repo
->gitdir
, files
[i
]) < 0)
2760 path
= git_buf_cstr(&buf
);
2762 if (git_path_isfile(path
)) {
2763 error
= p_unlink(path
);
2764 } else if (git_path_isdir(path
)) {
2765 error
= git_futils_rmdir_r(path
, NULL
,
2766 GIT_RMDIR_REMOVE_FILES
| GIT_RMDIR_REMOVE_BLOCKERS
);
2769 git_buf_clear(&buf
);
2776 static const char *state_files
[] = {
2777 GIT_MERGE_HEAD_FILE
,
2778 GIT_MERGE_MODE_FILE
,
2780 GIT_REVERT_HEAD_FILE
,
2781 GIT_CHERRYPICK_HEAD_FILE
,
2782 GIT_BISECT_LOG_FILE
,
2783 GIT_REBASE_MERGE_DIR
,
2784 GIT_REBASE_APPLY_DIR
,
2788 int git_repository_state_cleanup(git_repository
*repo
)
2792 return git_repository__cleanup_files(repo
, state_files
, ARRAY_SIZE(state_files
));
2795 int git_repository_is_shallow(git_repository
*repo
)
2797 git_buf path
= GIT_BUF_INIT
;
2801 if ((error
= git_buf_joinpath(&path
, repo
->gitdir
, "shallow")) < 0)
2804 error
= git_path_lstat(path
.ptr
, &st
);
2805 git_buf_free(&path
);
2807 if (error
== GIT_ENOTFOUND
) {
2814 return st
.st_size
== 0 ? 0 : 1;
2817 int git_repository_init_init_options(
2818 git_repository_init_options
*opts
, unsigned int version
)
2820 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
2821 opts
, version
, git_repository_init_options
,
2822 GIT_REPOSITORY_INIT_OPTIONS_INIT
);
2826 int git_repository_ident(const char **name
, const char **email
, const git_repository
*repo
)
2828 *name
= repo
->ident_name
;
2829 *email
= repo
->ident_email
;
2834 int git_repository_set_ident(git_repository
*repo
, const char *name
, const char *email
)
2836 char *tmp_name
= NULL
, *tmp_email
= NULL
;
2839 tmp_name
= git__strdup(name
);
2840 GITERR_CHECK_ALLOC(tmp_name
);
2844 tmp_email
= git__strdup(email
);
2845 GITERR_CHECK_ALLOC(tmp_email
);
2848 tmp_name
= git__swap(repo
->ident_name
, tmp_name
);
2849 tmp_email
= git__swap(repo
->ident_email
, tmp_email
);
2851 git__free(tmp_name
);
2852 git__free(tmp_email
);
2857 int git_repository_submodule_cache_all(git_repository
*repo
)
2863 if ((error
= git_strmap_alloc(&repo
->submodule_cache
)))
2866 error
= git_submodule__map(repo
, repo
->submodule_cache
);
2870 int git_repository_submodule_cache_clear(git_repository
*repo
)
2874 if (repo
->submodule_cache
== NULL
) {
2877 git_strmap_foreach_value(repo
->submodule_cache
, sm
, {
2878 git_submodule_free(sm
);
2880 git_strmap_free(repo
->submodule_cache
);
2881 repo
->submodule_cache
= 0;