2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #include "repository.h"
12 #include "git2/object.h"
13 #include "git2/sys/repository.h"
30 #include "diff_driver.h"
31 #include "annotated_commit.h"
32 #include "submodule.h"
38 # include "win32/w32_util.h"
41 bool git_repository__fsync_gitdir
= false;
44 git_repository_item_t parent
;
45 git_repository_item_t fallback
;
49 { GIT_REPOSITORY_ITEM_GITDIR
, GIT_REPOSITORY_ITEM__LAST
, NULL
, true },
50 { GIT_REPOSITORY_ITEM_WORKDIR
, GIT_REPOSITORY_ITEM__LAST
, NULL
, true },
51 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM__LAST
, NULL
, true },
52 { GIT_REPOSITORY_ITEM_GITDIR
, GIT_REPOSITORY_ITEM__LAST
, "index", false },
53 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "objects", true },
54 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "refs", true },
55 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "packed-refs", false },
56 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "remotes", true },
57 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "config", false },
58 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "info", true },
59 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "hooks", true },
60 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "logs", true },
61 { GIT_REPOSITORY_ITEM_GITDIR
, GIT_REPOSITORY_ITEM__LAST
, "modules", true },
62 { GIT_REPOSITORY_ITEM_COMMONDIR
, GIT_REPOSITORY_ITEM_GITDIR
, "worktrees", true }
65 static int check_repositoryformatversion(git_config
*config
);
67 #define GIT_COMMONDIR_FILE "commondir"
68 #define GIT_GITDIR_FILE "gitdir"
70 #define GIT_FILE_CONTENT_PREFIX "gitdir:"
72 #define GIT_BRANCH_MASTER "master"
74 #define GIT_REPO_VERSION 0
76 git_buf git_repository__reserved_names_win32
[] = {
77 { DOT_GIT
, 0, CONST_STRLEN(DOT_GIT
) },
78 { GIT_DIR_SHORTNAME
, 0, CONST_STRLEN(GIT_DIR_SHORTNAME
) }
80 size_t git_repository__reserved_names_win32_len
= 2;
82 git_buf git_repository__reserved_names_posix
[] = {
83 { DOT_GIT
, 0, CONST_STRLEN(DOT_GIT
) },
85 size_t git_repository__reserved_names_posix_len
= 1;
87 static void set_odb(git_repository
*repo
, git_odb
*odb
)
90 GIT_REFCOUNT_OWN(odb
, repo
);
91 GIT_REFCOUNT_INC(odb
);
94 if ((odb
= git__swap(repo
->_odb
, odb
)) != NULL
) {
95 GIT_REFCOUNT_OWN(odb
, NULL
);
100 static void set_refdb(git_repository
*repo
, git_refdb
*refdb
)
103 GIT_REFCOUNT_OWN(refdb
, repo
);
104 GIT_REFCOUNT_INC(refdb
);
107 if ((refdb
= git__swap(repo
->_refdb
, refdb
)) != NULL
) {
108 GIT_REFCOUNT_OWN(refdb
, NULL
);
109 git_refdb_free(refdb
);
113 static void set_config(git_repository
*repo
, git_config
*config
)
116 GIT_REFCOUNT_OWN(config
, repo
);
117 GIT_REFCOUNT_INC(config
);
120 if ((config
= git__swap(repo
->_config
, config
)) != NULL
) {
121 GIT_REFCOUNT_OWN(config
, NULL
);
122 git_config_free(config
);
125 git_repository__configmap_lookup_cache_clear(repo
);
128 static void set_index(git_repository
*repo
, git_index
*index
)
131 GIT_REFCOUNT_OWN(index
, repo
);
132 GIT_REFCOUNT_INC(index
);
135 if ((index
= git__swap(repo
->_index
, index
)) != NULL
) {
136 GIT_REFCOUNT_OWN(index
, NULL
);
137 git_index_free(index
);
141 int git_repository__cleanup(git_repository
*repo
)
145 git_repository_submodule_cache_clear(repo
);
146 git_cache_clear(&repo
->objects
);
147 git_attr_cache_flush(repo
);
149 set_config(repo
, NULL
);
150 set_index(repo
, NULL
);
152 set_refdb(repo
, NULL
);
157 void git_repository_free(git_repository
*repo
)
164 git_repository__cleanup(repo
);
166 git_cache_dispose(&repo
->objects
);
168 git_diff_driver_registry_free(repo
->diff_drivers
);
169 repo
->diff_drivers
= NULL
;
171 for (i
= 0; i
< repo
->reserved_names
.size
; i
++)
172 git_buf_dispose(git_array_get(repo
->reserved_names
, i
));
173 git_array_clear(repo
->reserved_names
);
175 git__free(repo
->gitlink
);
176 git__free(repo
->gitdir
);
177 git__free(repo
->commondir
);
178 git__free(repo
->workdir
);
179 git__free(repo
->namespace);
180 git__free(repo
->ident_name
);
181 git__free(repo
->ident_email
);
183 git__memzero(repo
, sizeof(*repo
));
188 * Git repository open methods
190 * Open a repository object from its path
192 static int is_valid_repository_path(bool *out
, git_buf
*repository_path
, git_buf
*common_path
)
198 /* Check if we have a separate commondir (e.g. we have a
200 if (git_path_contains_file(repository_path
, GIT_COMMONDIR_FILE
)) {
201 git_buf common_link
= GIT_BUF_INIT
;
203 if ((error
= git_buf_joinpath(&common_link
, repository_path
->ptr
, GIT_COMMONDIR_FILE
)) < 0 ||
204 (error
= git_futils_readbuffer(&common_link
, common_link
.ptr
)) < 0)
207 git_buf_rtrim(&common_link
);
208 if (git_path_is_relative(common_link
.ptr
)) {
209 if ((error
= git_buf_joinpath(common_path
, repository_path
->ptr
, common_link
.ptr
)) < 0)
212 git_buf_swap(common_path
, &common_link
);
215 git_buf_dispose(&common_link
);
218 if ((error
= git_buf_set(common_path
, repository_path
->ptr
, repository_path
->size
)) < 0)
222 /* Make sure the commondir path always has a trailing * slash */
223 if (git_buf_rfind(common_path
, '/') != (ssize_t
)common_path
->size
- 1)
224 if ((error
= git_buf_putc(common_path
, '/')) < 0)
227 /* Ensure HEAD file exists */
228 if (git_path_contains_file(repository_path
, GIT_HEAD_FILE
) == false)
230 /* Check files in common dir */
231 if (git_path_contains_dir(common_path
, GIT_OBJECTS_DIR
) == false)
233 if (git_path_contains_dir(common_path
, GIT_REFS_DIR
) == false)
240 static git_repository
*repository_alloc(void)
242 git_repository
*repo
= git__calloc(1, sizeof(git_repository
));
245 git_cache_init(&repo
->objects
) < 0)
248 git_array_init_to_size(repo
->reserved_names
, 4);
249 if (!repo
->reserved_names
.ptr
)
252 /* set all the entries in the configmap cache to `unset` */
253 git_repository__configmap_lookup_cache_clear(repo
);
259 git_cache_dispose(&repo
->objects
);
265 int git_repository_new(git_repository
**out
)
267 git_repository
*repo
;
269 *out
= repo
= repository_alloc();
270 GIT_ERROR_CHECK_ALLOC(repo
);
273 repo
->is_worktree
= 0;
278 static int load_config_data(git_repository
*repo
, const git_config
*config
)
282 int err
= git_config_get_bool(&is_bare
, config
, "core.bare");
283 if (err
< 0 && err
!= GIT_ENOTFOUND
)
286 /* Try to figure out if it's bare, default to non-bare if it's not set */
287 if (err
!= GIT_ENOTFOUND
)
288 repo
->is_bare
= is_bare
&& !repo
->is_worktree
;
295 static int load_workdir(git_repository
*repo
, git_config
*config
, git_buf
*parent_path
)
298 git_config_entry
*ce
;
299 git_buf worktree
= GIT_BUF_INIT
;
300 git_buf path
= GIT_BUF_INIT
;
305 if ((error
= git_config__lookup_entry(
306 &ce
, config
, "core.worktree", false)) < 0)
309 if (repo
->is_worktree
) {
310 char *gitlink
= git_worktree__read_link(repo
->gitdir
, GIT_GITDIR_FILE
);
316 git_buf_attach(&worktree
, gitlink
, 0);
318 if ((git_path_dirname_r(&worktree
, worktree
.ptr
)) < 0 ||
319 git_path_to_dir(&worktree
) < 0) {
324 repo
->workdir
= git_buf_detach(&worktree
);
326 else if (ce
&& ce
->value
) {
327 if ((error
= git_path_prettify_dir(
328 &worktree
, ce
->value
, repo
->gitdir
)) < 0)
331 repo
->workdir
= git_buf_detach(&worktree
);
333 else if (parent_path
&& git_path_isdir(parent_path
->ptr
))
334 repo
->workdir
= git_buf_detach(parent_path
);
336 if (git_path_dirname_r(&worktree
, repo
->gitdir
) < 0 ||
337 git_path_to_dir(&worktree
) < 0) {
342 repo
->workdir
= git_buf_detach(&worktree
);
345 GIT_ERROR_CHECK_ALLOC(repo
->workdir
);
347 git_buf_dispose(&path
);
348 git_config_entry_free(ce
);
353 * This function returns furthest offset into path where a ceiling dir
354 * is found, so we can stop processing the path at that point.
356 * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on
357 * the stack could remove directories name limits, but at the cost of doing
358 * repeated malloc/frees inside the loop below, so let's not do it now.
360 static size_t find_ceiling_dir_offset(
362 const char *ceiling_directories
)
364 char buf
[GIT_PATH_MAX
+ 1];
365 char buf2
[GIT_PATH_MAX
+ 1];
366 const char *ceil
, *sep
;
367 size_t len
, max_len
= 0, min_len
;
371 min_len
= (size_t)(git_path_root(path
) + 1);
373 if (ceiling_directories
== NULL
|| min_len
== 0)
376 for (sep
= ceil
= ceiling_directories
; *sep
; ceil
= sep
+ 1) {
377 for (sep
= ceil
; *sep
&& *sep
!= GIT_PATH_LIST_SEPARATOR
; sep
++);
380 if (len
== 0 || len
>= sizeof(buf
) || git_path_root(ceil
) == -1)
383 strncpy(buf
, ceil
, len
);
386 if (p_realpath(buf
, buf2
) == NULL
)
390 if (len
> 0 && buf2
[len
-1] == '/')
393 if (!strncmp(path
, buf2
, len
) &&
394 (path
[len
] == '/' || !path
[len
]) &&
401 return (max_len
<= min_len
? min_len
: max_len
);
405 * Read the contents of `file_path` and set `path_out` to the repo dir that
406 * it points to. Before calling, set `path_out` to the base directory that
407 * should be used if the contents of `file_path` are a relative path.
409 static int read_gitfile(git_buf
*path_out
, const char *file_path
)
412 git_buf file
= GIT_BUF_INIT
;
413 size_t prefix_len
= strlen(GIT_FILE_CONTENT_PREFIX
);
415 assert(path_out
&& file_path
);
417 if (git_futils_readbuffer(&file
, file_path
) < 0)
420 git_buf_rtrim(&file
);
421 /* apparently on Windows, some people use backslashes in paths */
422 git_path_mkposix(file
.ptr
);
424 if (git_buf_len(&file
) <= prefix_len
||
425 memcmp(git_buf_cstr(&file
), GIT_FILE_CONTENT_PREFIX
, prefix_len
) != 0)
427 git_error_set(GIT_ERROR_REPOSITORY
,
428 "the `.git` file at '%s' is malformed", file_path
);
431 else if ((error
= git_path_dirname_r(path_out
, file_path
)) >= 0) {
432 const char *gitlink
= git_buf_cstr(&file
) + prefix_len
;
433 while (*gitlink
&& git__isspace(*gitlink
)) gitlink
++;
435 error
= git_path_prettify_dir(
436 path_out
, gitlink
, git_buf_cstr(path_out
));
439 git_buf_dispose(&file
);
443 static int find_repo(
444 git_buf
*gitdir_path
,
445 git_buf
*workdir_path
,
446 git_buf
*gitlink_path
,
447 git_buf
*commondir_path
,
448 const char *start_path
,
450 const char *ceiling_dirs
)
452 git_buf path
= GIT_BUF_INIT
;
453 git_buf repo_link
= GIT_BUF_INIT
;
454 git_buf common_link
= GIT_BUF_INIT
;
456 dev_t initial_device
= 0;
458 bool in_dot_git
, is_valid
;
459 size_t ceiling_offset
= 0;
462 git_buf_clear(gitdir_path
);
464 error
= git_path_prettify(&path
, start_path
, NULL
);
468 /* in_dot_git toggles each loop:
469 * /a/b/c/.git, /a/b/c, /a/b/.git, /a/b, /a/.git, /a
470 * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, we
471 * assume we started with /a/b/c.git and don't append .git the first
473 * min_iterations indicates the number of iterations left before going
474 * further counts as a search. */
475 if (flags
& (GIT_REPOSITORY_OPEN_BARE
| GIT_REPOSITORY_OPEN_NO_DOTGIT
)) {
484 if (!(flags
& GIT_REPOSITORY_OPEN_NO_DOTGIT
)) {
486 if ((error
= git_buf_joinpath(&path
, path
.ptr
, DOT_GIT
)) < 0)
489 in_dot_git
= !in_dot_git
;
492 if (p_stat(path
.ptr
, &st
) == 0) {
493 /* check that we have not crossed device boundaries */
494 if (initial_device
== 0)
495 initial_device
= st
.st_dev
;
496 else if (st
.st_dev
!= initial_device
&&
497 !(flags
& GIT_REPOSITORY_OPEN_CROSS_FS
))
500 if (S_ISDIR(st
.st_mode
)) {
501 if ((error
= is_valid_repository_path(&is_valid
, &path
, &common_link
)) < 0)
505 if ((error
= git_path_to_dir(&path
)) < 0 ||
506 (error
= git_buf_set(gitdir_path
, path
.ptr
, path
.size
)) < 0)
510 if ((error
= git_buf_attach(gitlink_path
, git_worktree__read_link(path
.ptr
, GIT_GITDIR_FILE
), 0)) < 0)
513 git_buf_swap(&common_link
, commondir_path
);
517 } else if (S_ISREG(st
.st_mode
) && git__suffixcmp(path
.ptr
, "/" DOT_GIT
) == 0) {
518 if ((error
= read_gitfile(&repo_link
, path
.ptr
)) < 0 ||
519 (error
= is_valid_repository_path(&is_valid
, &repo_link
, &common_link
)) < 0)
523 git_buf_swap(gitdir_path
, &repo_link
);
526 if ((error
= git_buf_put(gitlink_path
, path
.ptr
, path
.size
)) < 0)
529 git_buf_swap(&common_link
, commondir_path
);
535 /* Move up one directory. If we're in_dot_git, we'll search the
536 * parent itself next. If we're !in_dot_git, we'll search .git
537 * in the parent directory next (added at the top of the loop). */
538 if ((error
= git_path_dirname_r(&path
, path
.ptr
)) < 0)
541 /* Once we've checked the directory (and .git if applicable),
542 * find the ceiling for a search. */
543 if (min_iterations
&& (--min_iterations
== 0))
544 ceiling_offset
= find_ceiling_dir_offset(path
.ptr
, ceiling_dirs
);
546 /* Check if we should stop searching here. */
547 if (min_iterations
== 0 &&
548 (path
.ptr
[ceiling_offset
] == 0 || (flags
& GIT_REPOSITORY_OPEN_NO_SEARCH
)))
552 if (workdir_path
&& !(flags
& GIT_REPOSITORY_OPEN_BARE
)) {
553 if (!git_buf_len(gitdir_path
))
554 git_buf_clear(workdir_path
);
555 else if ((error
= git_path_dirname_r(workdir_path
, path
.ptr
)) < 0 ||
556 (error
= git_path_to_dir(workdir_path
)) < 0)
560 /* If we didn't find the repository, and we don't have any other error
561 * to report, report that. */
562 if (!git_buf_len(gitdir_path
)) {
563 git_error_set(GIT_ERROR_REPOSITORY
, "could not find repository from '%s'", start_path
);
564 error
= GIT_ENOTFOUND
;
569 git_buf_dispose(&path
);
570 git_buf_dispose(&repo_link
);
571 git_buf_dispose(&common_link
);
575 int git_repository_open_bare(
576 git_repository
**repo_ptr
,
577 const char *bare_path
)
579 git_buf path
= GIT_BUF_INIT
, common_path
= GIT_BUF_INIT
;
580 git_repository
*repo
= NULL
;
584 if ((error
= git_path_prettify_dir(&path
, bare_path
, NULL
)) < 0 ||
585 (error
= is_valid_repository_path(&is_valid
, &path
, &common_path
)) < 0)
589 git_buf_dispose(&path
);
590 git_buf_dispose(&common_path
);
591 git_error_set(GIT_ERROR_REPOSITORY
, "path is not a repository: %s", bare_path
);
592 return GIT_ENOTFOUND
;
595 repo
= repository_alloc();
596 GIT_ERROR_CHECK_ALLOC(repo
);
598 repo
->gitdir
= git_buf_detach(&path
);
599 GIT_ERROR_CHECK_ALLOC(repo
->gitdir
);
600 repo
->commondir
= git_buf_detach(&common_path
);
601 GIT_ERROR_CHECK_ALLOC(repo
->commondir
);
603 /* of course we're bare! */
605 repo
->is_worktree
= 0;
606 repo
->workdir
= NULL
;
612 static int _git_repository_open_ext_from_env(
613 git_repository
**out
,
614 const char *start_path
)
616 git_repository
*repo
= NULL
;
617 git_index
*index
= NULL
;
619 git_buf dir_buf
= GIT_BUF_INIT
;
620 git_buf ceiling_dirs_buf
= GIT_BUF_INIT
;
621 git_buf across_fs_buf
= GIT_BUF_INIT
;
622 git_buf index_file_buf
= GIT_BUF_INIT
;
623 git_buf namespace_buf
= GIT_BUF_INIT
;
624 git_buf object_dir_buf
= GIT_BUF_INIT
;
625 git_buf alts_buf
= GIT_BUF_INIT
;
626 git_buf work_tree_buf
= GIT_BUF_INIT
;
627 git_buf common_dir_buf
= GIT_BUF_INIT
;
628 const char *ceiling_dirs
= NULL
;
633 error
= git__getenv(&dir_buf
, "GIT_DIR");
634 if (error
== GIT_ENOTFOUND
) {
637 } else if (error
< 0)
640 start_path
= git_buf_cstr(&dir_buf
);
641 flags
|= GIT_REPOSITORY_OPEN_NO_SEARCH
;
642 flags
|= GIT_REPOSITORY_OPEN_NO_DOTGIT
;
646 error
= git__getenv(&ceiling_dirs_buf
, "GIT_CEILING_DIRECTORIES");
647 if (error
== GIT_ENOTFOUND
)
652 ceiling_dirs
= git_buf_cstr(&ceiling_dirs_buf
);
654 error
= git__getenv(&across_fs_buf
, "GIT_DISCOVERY_ACROSS_FILESYSTEM");
655 if (error
== GIT_ENOTFOUND
)
661 error
= git_config_parse_bool(&across_fs
, git_buf_cstr(&across_fs_buf
));
665 flags
|= GIT_REPOSITORY_OPEN_CROSS_FS
;
668 error
= git__getenv(&index_file_buf
, "GIT_INDEX_FILE");
669 if (error
== GIT_ENOTFOUND
)
674 error
= git_index_open(&index
, git_buf_cstr(&index_file_buf
));
679 error
= git__getenv(&namespace_buf
, "GIT_NAMESPACE");
680 if (error
== GIT_ENOTFOUND
)
685 error
= git__getenv(&object_dir_buf
, "GIT_OBJECT_DIRECTORY");
686 if (error
== GIT_ENOTFOUND
)
691 error
= git_odb_open(&odb
, git_buf_cstr(&object_dir_buf
));
696 error
= git__getenv(&work_tree_buf
, "GIT_WORK_TREE");
697 if (error
== GIT_ENOTFOUND
)
702 git_error_set(GIT_ERROR_INVALID
, "GIT_WORK_TREE unimplemented");
707 error
= git__getenv(&work_tree_buf
, "GIT_COMMON_DIR");
708 if (error
== GIT_ENOTFOUND
)
713 git_error_set(GIT_ERROR_INVALID
, "GIT_COMMON_DIR unimplemented");
718 error
= git_repository_open_ext(&repo
, start_path
, flags
, ceiling_dirs
);
723 git_repository_set_odb(repo
, odb
);
725 error
= git__getenv(&alts_buf
, "GIT_ALTERNATE_OBJECT_DIRECTORIES");
726 if (error
== GIT_ENOTFOUND
) {
729 } else if (error
< 0)
735 error
= git_repository_odb(&odb
, repo
);
740 end
= git_buf_cstr(&alts_buf
) + git_buf_len(&alts_buf
);
741 for (sep
= alt
= alts_buf
.ptr
; sep
!= end
; alt
= sep
+1) {
742 for (sep
= alt
; *sep
&& *sep
!= GIT_PATH_LIST_SEPARATOR
; sep
++)
746 error
= git_odb_add_disk_alternate(odb
, alt
);
752 if (git_buf_len(&namespace_buf
)) {
753 error
= git_repository_set_namespace(repo
, git_buf_cstr(&namespace_buf
));
758 git_repository_set_index(repo
, index
);
765 git_repository_free(repo
);
768 git_index_free(index
);
769 git_buf_dispose(&common_dir_buf
);
770 git_buf_dispose(&work_tree_buf
);
771 git_buf_dispose(&alts_buf
);
772 git_buf_dispose(&object_dir_buf
);
773 git_buf_dispose(&namespace_buf
);
774 git_buf_dispose(&index_file_buf
);
775 git_buf_dispose(&across_fs_buf
);
776 git_buf_dispose(&ceiling_dirs_buf
);
777 git_buf_dispose(&dir_buf
);
781 static int repo_is_worktree(unsigned *out
, const git_repository
*repo
)
783 git_buf gitdir_link
= GIT_BUF_INIT
;
786 /* Worktrees cannot have the same commondir and gitdir */
787 if (repo
->commondir
&& repo
->gitdir
788 && !strcmp(repo
->commondir
, repo
->gitdir
)) {
793 if ((error
= git_buf_joinpath(&gitdir_link
, repo
->gitdir
, "gitdir")) < 0)
796 /* A 'gitdir' file inside a git directory is currently
797 * only used when the repository is a working tree. */
798 *out
= !!git_path_exists(gitdir_link
.ptr
);
800 git_buf_dispose(&gitdir_link
);
804 int git_repository_open_ext(
805 git_repository
**repo_ptr
,
806 const char *start_path
,
808 const char *ceiling_dirs
)
811 unsigned is_worktree
;
812 git_buf gitdir
= GIT_BUF_INIT
, workdir
= GIT_BUF_INIT
,
813 gitlink
= GIT_BUF_INIT
, commondir
= GIT_BUF_INIT
;
814 git_repository
*repo
= NULL
;
815 git_config
*config
= NULL
;
817 if (flags
& GIT_REPOSITORY_OPEN_FROM_ENV
)
818 return _git_repository_open_ext_from_env(repo_ptr
, start_path
);
824 &gitdir
, &workdir
, &gitlink
, &commondir
, start_path
, flags
, ceiling_dirs
);
826 if (error
< 0 || !repo_ptr
)
829 repo
= repository_alloc();
830 GIT_ERROR_CHECK_ALLOC(repo
);
832 repo
->gitdir
= git_buf_detach(&gitdir
);
833 GIT_ERROR_CHECK_ALLOC(repo
->gitdir
);
836 repo
->gitlink
= git_buf_detach(&gitlink
);
837 GIT_ERROR_CHECK_ALLOC(repo
->gitlink
);
839 if (commondir
.size
) {
840 repo
->commondir
= git_buf_detach(&commondir
);
841 GIT_ERROR_CHECK_ALLOC(repo
->commondir
);
844 if ((error
= repo_is_worktree(&is_worktree
, repo
)) < 0)
846 repo
->is_worktree
= is_worktree
;
849 * We'd like to have the config, but git doesn't particularly
850 * care if it's not there, so we need to deal with that.
853 error
= git_repository_config_snapshot(&config
, repo
);
854 if (error
< 0 && error
!= GIT_ENOTFOUND
)
857 if (config
&& (error
= check_repositoryformatversion(config
)) < 0)
860 if ((flags
& GIT_REPOSITORY_OPEN_BARE
) != 0)
865 ((error
= load_config_data(repo
, config
)) < 0 ||
866 (error
= load_workdir(repo
, config
, &workdir
)) < 0))
871 git_buf_dispose(&gitdir
);
872 git_buf_dispose(&workdir
);
873 git_buf_dispose(&gitlink
);
874 git_buf_dispose(&commondir
);
875 git_config_free(config
);
878 git_repository_free(repo
);
885 int git_repository_open(git_repository
**repo_out
, const char *path
)
887 return git_repository_open_ext(
888 repo_out
, path
, GIT_REPOSITORY_OPEN_NO_SEARCH
, NULL
);
891 int git_repository_open_from_worktree(git_repository
**repo_out
, git_worktree
*wt
)
893 git_buf path
= GIT_BUF_INIT
;
894 git_repository
*repo
= NULL
;
898 assert(repo_out
&& wt
);
901 len
= strlen(wt
->gitlink_path
);
903 if (len
<= 4 || strcasecmp(wt
->gitlink_path
+ len
- 4, ".git")) {
908 if ((err
= git_buf_set(&path
, wt
->gitlink_path
, len
- 4)) < 0)
911 if ((err
= git_repository_open(&repo
, path
.ptr
)) < 0)
917 git_buf_dispose(&path
);
922 int git_repository_wrap_odb(git_repository
**repo_out
, git_odb
*odb
)
924 git_repository
*repo
;
926 repo
= repository_alloc();
927 GIT_ERROR_CHECK_ALLOC(repo
);
929 git_repository_set_odb(repo
, odb
);
935 int git_repository_discover(
937 const char *start_path
,
939 const char *ceiling_dirs
)
941 uint32_t flags
= across_fs
? GIT_REPOSITORY_OPEN_CROSS_FS
: 0;
945 git_buf_sanitize(out
);
947 return find_repo(out
, NULL
, NULL
, NULL
, start_path
, flags
, ceiling_dirs
);
950 static int load_config(
952 git_repository
*repo
,
953 const char *global_config_path
,
954 const char *xdg_config_path
,
955 const char *system_config_path
,
956 const char *programdata_path
)
959 git_buf config_path
= GIT_BUF_INIT
;
960 git_config
*cfg
= NULL
;
964 if ((error
= git_config_new(&cfg
)) < 0)
968 if ((error
= git_repository_item_path(&config_path
, repo
, GIT_REPOSITORY_ITEM_CONFIG
)) == 0)
969 error
= git_config_add_file_ondisk(cfg
, config_path
.ptr
, GIT_CONFIG_LEVEL_LOCAL
, repo
, 0);
971 if (error
&& error
!= GIT_ENOTFOUND
)
974 git_buf_dispose(&config_path
);
977 if (global_config_path
!= NULL
&&
978 (error
= git_config_add_file_ondisk(
979 cfg
, global_config_path
, GIT_CONFIG_LEVEL_GLOBAL
, repo
, 0)) < 0 &&
980 error
!= GIT_ENOTFOUND
)
983 if (xdg_config_path
!= NULL
&&
984 (error
= git_config_add_file_ondisk(
985 cfg
, xdg_config_path
, GIT_CONFIG_LEVEL_XDG
, repo
, 0)) < 0 &&
986 error
!= GIT_ENOTFOUND
)
989 if (system_config_path
!= NULL
&&
990 (error
= git_config_add_file_ondisk(
991 cfg
, system_config_path
, GIT_CONFIG_LEVEL_SYSTEM
, repo
, 0)) < 0 &&
992 error
!= GIT_ENOTFOUND
)
995 if (programdata_path
!= NULL
&&
996 (error
= git_config_add_file_ondisk(
997 cfg
, programdata_path
, GIT_CONFIG_LEVEL_PROGRAMDATA
, repo
, 0)) < 0 &&
998 error
!= GIT_ENOTFOUND
)
1001 git_error_clear(); /* clear any lingering ENOTFOUND errors */
1007 git_buf_dispose(&config_path
);
1008 git_config_free(cfg
);
1013 static const char *path_unless_empty(git_buf
*buf
)
1015 return git_buf_len(buf
) > 0 ? git_buf_cstr(buf
) : NULL
;
1018 int git_repository_config__weakptr(git_config
**out
, git_repository
*repo
)
1022 if (repo
->_config
== NULL
) {
1023 git_buf global_buf
= GIT_BUF_INIT
;
1024 git_buf xdg_buf
= GIT_BUF_INIT
;
1025 git_buf system_buf
= GIT_BUF_INIT
;
1026 git_buf programdata_buf
= GIT_BUF_INIT
;
1029 git_config_find_global(&global_buf
);
1030 git_config_find_xdg(&xdg_buf
);
1031 git_config_find_system(&system_buf
);
1032 git_config_find_programdata(&programdata_buf
);
1034 /* If there is no global file, open a backend for it anyway */
1035 if (git_buf_len(&global_buf
) == 0)
1036 git_config__global_location(&global_buf
);
1038 error
= load_config(
1040 path_unless_empty(&global_buf
),
1041 path_unless_empty(&xdg_buf
),
1042 path_unless_empty(&system_buf
),
1043 path_unless_empty(&programdata_buf
));
1045 GIT_REFCOUNT_OWN(config
, repo
);
1047 config
= git__compare_and_swap(&repo
->_config
, NULL
, config
);
1048 if (config
!= NULL
) {
1049 GIT_REFCOUNT_OWN(config
, NULL
);
1050 git_config_free(config
);
1054 git_buf_dispose(&global_buf
);
1055 git_buf_dispose(&xdg_buf
);
1056 git_buf_dispose(&system_buf
);
1057 git_buf_dispose(&programdata_buf
);
1060 *out
= repo
->_config
;
1064 int git_repository_config(git_config
**out
, git_repository
*repo
)
1066 if (git_repository_config__weakptr(out
, repo
) < 0)
1069 GIT_REFCOUNT_INC(*out
);
1073 int git_repository_config_snapshot(git_config
**out
, git_repository
*repo
)
1078 if ((error
= git_repository_config__weakptr(&weak
, repo
)) < 0)
1081 return git_config_snapshot(out
, weak
);
1084 int git_repository_set_config(git_repository
*repo
, git_config
*config
)
1086 assert(repo
&& config
);
1087 set_config(repo
, config
);
1091 int git_repository_odb__weakptr(git_odb
**out
, git_repository
*repo
)
1095 assert(repo
&& out
);
1097 if (repo
->_odb
== NULL
) {
1098 git_buf odb_path
= GIT_BUF_INIT
;
1101 if ((error
= git_repository_item_path(&odb_path
, repo
,
1102 GIT_REPOSITORY_ITEM_OBJECTS
)) < 0 ||
1103 (error
= git_odb_new(&odb
)) < 0)
1106 GIT_REFCOUNT_OWN(odb
, repo
);
1108 if ((error
= git_odb__set_caps(odb
, GIT_ODB_CAP_FROM_OWNER
)) < 0 ||
1109 (error
= git_odb__add_default_backends(odb
, odb_path
.ptr
, 0, 0)) < 0) {
1114 odb
= git__compare_and_swap(&repo
->_odb
, NULL
, odb
);
1116 GIT_REFCOUNT_OWN(odb
, NULL
);
1120 git_buf_dispose(&odb_path
);
1127 int git_repository_odb(git_odb
**out
, git_repository
*repo
)
1129 if (git_repository_odb__weakptr(out
, repo
) < 0)
1132 GIT_REFCOUNT_INC(*out
);
1136 int git_repository_set_odb(git_repository
*repo
, git_odb
*odb
)
1138 assert(repo
&& odb
);
1143 int git_repository_refdb__weakptr(git_refdb
**out
, git_repository
*repo
)
1147 assert(out
&& repo
);
1149 if (repo
->_refdb
== NULL
) {
1152 error
= git_refdb_open(&refdb
, repo
);
1154 GIT_REFCOUNT_OWN(refdb
, repo
);
1156 refdb
= git__compare_and_swap(&repo
->_refdb
, NULL
, refdb
);
1157 if (refdb
!= NULL
) {
1158 GIT_REFCOUNT_OWN(refdb
, NULL
);
1159 git_refdb_free(refdb
);
1164 *out
= repo
->_refdb
;
1168 int git_repository_refdb(git_refdb
**out
, git_repository
*repo
)
1170 if (git_repository_refdb__weakptr(out
, repo
) < 0)
1173 GIT_REFCOUNT_INC(*out
);
1177 int git_repository_set_refdb(git_repository
*repo
, git_refdb
*refdb
)
1179 assert(repo
&& refdb
);
1180 set_refdb(repo
, refdb
);
1184 int git_repository_index__weakptr(git_index
**out
, git_repository
*repo
)
1188 assert(out
&& repo
);
1190 if (repo
->_index
== NULL
) {
1191 git_buf index_path
= GIT_BUF_INIT
;
1194 if ((error
= git_buf_joinpath(&index_path
, repo
->gitdir
, GIT_INDEX_FILE
)) < 0)
1197 error
= git_index_open(&index
, index_path
.ptr
);
1199 GIT_REFCOUNT_OWN(index
, repo
);
1201 index
= git__compare_and_swap(&repo
->_index
, NULL
, index
);
1202 if (index
!= NULL
) {
1203 GIT_REFCOUNT_OWN(index
, NULL
);
1204 git_index_free(index
);
1207 error
= git_index_set_caps(repo
->_index
,
1208 GIT_INDEX_CAPABILITY_FROM_OWNER
);
1211 git_buf_dispose(&index_path
);
1214 *out
= repo
->_index
;
1218 int git_repository_index(git_index
**out
, git_repository
*repo
)
1220 if (git_repository_index__weakptr(out
, repo
) < 0)
1223 GIT_REFCOUNT_INC(*out
);
1227 int git_repository_set_index(git_repository
*repo
, git_index
*index
)
1230 set_index(repo
, index
);
1234 int git_repository_set_namespace(git_repository
*repo
, const char *namespace)
1236 git__free(repo
->namespace);
1238 if (namespace == NULL
) {
1239 repo
->namespace = NULL
;
1243 return (repo
->namespace = git__strdup(namespace)) ? 0 : -1;
1246 const char *git_repository_get_namespace(git_repository
*repo
)
1248 return repo
->namespace;
1252 static int reserved_names_add8dot3(git_repository
*repo
, const char *path
)
1254 char *name
= git_win32_path_8dot3_name(path
);
1255 const char *def
= GIT_DIR_SHORTNAME
;
1256 const char *def_dot_git
= DOT_GIT
;
1257 size_t name_len
, def_len
= CONST_STRLEN(GIT_DIR_SHORTNAME
);
1258 size_t def_dot_git_len
= CONST_STRLEN(DOT_GIT
);
1264 name_len
= strlen(name
);
1266 if ((name_len
== def_len
&& memcmp(name
, def
, def_len
) == 0) ||
1267 (name_len
== def_dot_git_len
&& memcmp(name
, def_dot_git
, def_dot_git_len
) == 0)) {
1272 if ((buf
= git_array_alloc(repo
->reserved_names
)) == NULL
)
1275 git_buf_attach(buf
, name
, name_len
);
1279 bool git_repository__reserved_names(
1280 git_buf
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
)
1282 GIT_UNUSED(include_ntfs
);
1284 if (repo
->reserved_names
.size
== 0) {
1288 /* Add the static defaults */
1289 for (i
= 0; i
< git_repository__reserved_names_win32_len
; i
++) {
1290 if ((buf
= git_array_alloc(repo
->reserved_names
)) == NULL
)
1293 buf
->ptr
= git_repository__reserved_names_win32
[i
].ptr
;
1294 buf
->size
= git_repository__reserved_names_win32
[i
].size
;
1297 /* Try to add any repo-specific reserved names - the gitlink file
1298 * within a submodule or the repository (if the repository directory
1299 * is beneath the workdir). These are typically `.git`, but should
1300 * be protected in case they are not. Note, repo and workdir paths
1301 * are always prettified to end in `/`, so a prefixcmp is safe.
1303 if (!repo
->is_bare
) {
1304 int (*prefixcmp
)(const char *, const char *);
1305 int error
, ignorecase
;
1307 error
= git_repository__configmap_lookup(
1308 &ignorecase
, repo
, GIT_CONFIGMAP_IGNORECASE
);
1309 prefixcmp
= (error
|| ignorecase
) ? git__prefixcmp_icase
:
1312 if (repo
->gitlink
&&
1313 reserved_names_add8dot3(repo
, repo
->gitlink
) < 0)
1317 prefixcmp(repo
->gitdir
, repo
->workdir
) == 0 &&
1318 reserved_names_add8dot3(repo
, repo
->gitdir
) < 0)
1323 *out
= repo
->reserved_names
.ptr
;
1324 *outlen
= repo
->reserved_names
.size
;
1328 /* Always give good defaults, even on OOM */
1330 *out
= git_repository__reserved_names_win32
;
1331 *outlen
= git_repository__reserved_names_win32_len
;
1336 bool git_repository__reserved_names(
1337 git_buf
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
)
1342 *out
= git_repository__reserved_names_win32
;
1343 *outlen
= git_repository__reserved_names_win32_len
;
1345 *out
= git_repository__reserved_names_posix
;
1346 *outlen
= git_repository__reserved_names_posix_len
;
1353 static int check_repositoryformatversion(git_config
*config
)
1357 error
= git_config_get_int32(&version
, config
, "core.repositoryformatversion");
1358 /* git ignores this if the config variable isn't there */
1359 if (error
== GIT_ENOTFOUND
)
1365 if (GIT_REPO_VERSION
< version
) {
1366 git_error_set(GIT_ERROR_REPOSITORY
,
1367 "unsupported repository version %d. Only versions up to %d are supported.",
1368 version
, GIT_REPO_VERSION
);
1375 int git_repository_create_head(const char *git_dir
, const char *ref_name
)
1377 git_buf ref_path
= GIT_BUF_INIT
;
1378 git_filebuf ref
= GIT_FILEBUF_INIT
;
1382 if ((error
= git_buf_joinpath(&ref_path
, git_dir
, GIT_HEAD_FILE
)) < 0 ||
1383 (error
= git_filebuf_open(&ref
, ref_path
.ptr
, 0, GIT_REFS_FILE_MODE
)) < 0)
1387 ref_name
= GIT_BRANCH_MASTER
;
1389 if (git__prefixcmp(ref_name
, GIT_REFS_DIR
) == 0)
1392 fmt
= "ref: " GIT_REFS_HEADS_DIR
"%s\n";
1394 if ((error
= git_filebuf_printf(&ref
, fmt
, ref_name
)) < 0 ||
1395 (error
= git_filebuf_commit(&ref
)) < 0)
1399 git_buf_dispose(&ref_path
);
1400 git_filebuf_cleanup(&ref
);
1404 static bool is_chmod_supported(const char *file_path
)
1406 struct stat st1
, st2
;
1408 if (p_stat(file_path
, &st1
) < 0)
1411 if (p_chmod(file_path
, st1
.st_mode
^ S_IXUSR
) < 0)
1414 if (p_stat(file_path
, &st2
) < 0)
1417 return (st1
.st_mode
!= st2
.st_mode
);
1420 static bool is_filesystem_case_insensitive(const char *gitdir_path
)
1422 git_buf path
= GIT_BUF_INIT
;
1423 int is_insensitive
= -1;
1425 if (!git_buf_joinpath(&path
, gitdir_path
, "CoNfIg"))
1426 is_insensitive
= git_path_exists(git_buf_cstr(&path
));
1428 git_buf_dispose(&path
);
1429 return is_insensitive
;
1432 static bool are_symlinks_supported(const char *wd_path
)
1434 git_config
*config
= NULL
;
1435 git_buf global_buf
= GIT_BUF_INIT
;
1436 git_buf xdg_buf
= GIT_BUF_INIT
;
1437 git_buf system_buf
= GIT_BUF_INIT
;
1438 git_buf programdata_buf
= GIT_BUF_INIT
;
1442 * To emulate Git for Windows, symlinks on Windows must be explicitly
1443 * opted-in. We examine the system configuration for a core.symlinks
1444 * set to true. If found, we then examine the filesystem to see if
1445 * symlinks are _actually_ supported by the current user. If that is
1446 * _not_ set, then we do not test or enable symlink support.
1449 git_config_find_global(&global_buf
);
1450 git_config_find_xdg(&xdg_buf
);
1451 git_config_find_system(&system_buf
);
1452 git_config_find_programdata(&programdata_buf
);
1454 if (load_config(&config
, NULL
,
1455 path_unless_empty(&global_buf
),
1456 path_unless_empty(&xdg_buf
),
1457 path_unless_empty(&system_buf
),
1458 path_unless_empty(&programdata_buf
)) < 0)
1461 if (git_config_get_bool(&symlinks
, config
, "core.symlinks") < 0 || !symlinks
)
1465 if (!(symlinks
= git_path_supports_symlinks(wd_path
)))
1469 git_buf_dispose(&global_buf
);
1470 git_buf_dispose(&xdg_buf
);
1471 git_buf_dispose(&system_buf
);
1472 git_buf_dispose(&programdata_buf
);
1473 git_config_free(config
);
1474 return symlinks
!= 0;
1477 static int create_empty_file(const char *path
, mode_t mode
)
1481 if ((fd
= p_creat(path
, mode
)) < 0) {
1482 git_error_set(GIT_ERROR_OS
, "error while creating '%s'", path
);
1486 if (p_close(fd
) < 0) {
1487 git_error_set(GIT_ERROR_OS
, "error while closing '%s'", path
);
1494 static int repo_local_config(
1496 git_buf
*config_dir
,
1497 git_repository
*repo
,
1498 const char *repo_dir
)
1502 const char *cfg_path
;
1504 if (git_buf_joinpath(config_dir
, repo_dir
, GIT_CONFIG_FILENAME_INREPO
) < 0)
1506 cfg_path
= git_buf_cstr(config_dir
);
1508 /* make LOCAL config if missing */
1509 if (!git_path_isfile(cfg_path
) &&
1510 (error
= create_empty_file(cfg_path
, GIT_CONFIG_FILE_MODE
)) < 0)
1513 /* if no repo, just open that file directly */
1515 return git_config_open_ondisk(out
, cfg_path
);
1517 /* otherwise, open parent config and get that level */
1518 if ((error
= git_repository_config__weakptr(&parent
, repo
)) < 0)
1521 if (git_config_open_level(out
, parent
, GIT_CONFIG_LEVEL_LOCAL
) < 0) {
1524 if (!(error
= git_config_add_file_ondisk(
1525 parent
, cfg_path
, GIT_CONFIG_LEVEL_LOCAL
, repo
, false)))
1526 error
= git_config_open_level(out
, parent
, GIT_CONFIG_LEVEL_LOCAL
);
1529 git_config_free(parent
);
1534 static int repo_init_fs_configs(
1536 const char *cfg_path
,
1537 const char *repo_dir
,
1538 const char *work_dir
,
1539 bool update_ignorecase
)
1544 work_dir
= repo_dir
;
1546 if ((error
= git_config_set_bool(
1547 cfg
, "core.filemode", is_chmod_supported(cfg_path
))) < 0)
1550 if (!are_symlinks_supported(work_dir
)) {
1551 if ((error
= git_config_set_bool(cfg
, "core.symlinks", false)) < 0)
1553 } else if (git_config_delete_entry(cfg
, "core.symlinks") < 0)
1556 if (update_ignorecase
) {
1557 if (is_filesystem_case_insensitive(repo_dir
)) {
1558 if ((error
= git_config_set_bool(cfg
, "core.ignorecase", true)) < 0)
1560 } else if (git_config_delete_entry(cfg
, "core.ignorecase") < 0)
1564 #ifdef GIT_USE_ICONV
1565 if ((error
= git_config_set_bool(
1566 cfg
, "core.precomposeunicode",
1567 git_path_does_fs_decompose_unicode(work_dir
))) < 0)
1569 /* on non-iconv platforms, don't even set core.precomposeunicode */
1575 static int repo_init_config(
1576 const char *repo_dir
,
1577 const char *work_dir
,
1582 git_buf cfg_path
= GIT_BUF_INIT
, worktree_path
= GIT_BUF_INIT
;
1583 git_config
*config
= NULL
;
1584 bool is_bare
= ((flags
& GIT_REPOSITORY_INIT_BARE
) != 0);
1585 bool is_reinit
= ((flags
& GIT_REPOSITORY_INIT__IS_REINIT
) != 0);
1587 if ((error
= repo_local_config(&config
, &cfg_path
, NULL
, repo_dir
)) < 0)
1590 if (is_reinit
&& (error
= check_repositoryformatversion(config
)) < 0)
1593 #define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \
1594 if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
1595 goto cleanup; } while (0)
1597 SET_REPO_CONFIG(bool, "core.bare", is_bare
);
1598 SET_REPO_CONFIG(int32
, "core.repositoryformatversion", GIT_REPO_VERSION
);
1600 if ((error
= repo_init_fs_configs(
1601 config
, cfg_path
.ptr
, repo_dir
, work_dir
, !is_reinit
)) < 0)
1605 SET_REPO_CONFIG(bool, "core.logallrefupdates", true);
1607 if (!(flags
& GIT_REPOSITORY_INIT__NATURAL_WD
)) {
1608 if ((error
= git_buf_sets(&worktree_path
, work_dir
)) < 0)
1611 if ((flags
& GIT_REPOSITORY_INIT_RELATIVE_GITLINK
))
1612 if ((error
= git_path_make_relative(&worktree_path
, repo_dir
)) < 0)
1615 SET_REPO_CONFIG(string
, "core.worktree", worktree_path
.ptr
);
1616 } else if (is_reinit
) {
1617 if (git_config_delete_entry(config
, "core.worktree") < 0)
1622 if (mode
== GIT_REPOSITORY_INIT_SHARED_GROUP
) {
1623 SET_REPO_CONFIG(int32
, "core.sharedrepository", 1);
1624 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1626 else if (mode
== GIT_REPOSITORY_INIT_SHARED_ALL
) {
1627 SET_REPO_CONFIG(int32
, "core.sharedrepository", 2);
1628 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1632 git_buf_dispose(&cfg_path
);
1633 git_buf_dispose(&worktree_path
);
1634 git_config_free(config
);
1639 static int repo_reinit_submodule_fs(git_submodule
*sm
, const char *n
, void *p
)
1641 git_repository
*smrepo
= NULL
;
1642 GIT_UNUSED(n
); GIT_UNUSED(p
);
1644 if (git_submodule_open(&smrepo
, sm
) < 0 ||
1645 git_repository_reinit_filesystem(smrepo
, true) < 0)
1647 git_repository_free(smrepo
);
1652 int git_repository_reinit_filesystem(git_repository
*repo
, int recurse
)
1655 git_buf path
= GIT_BUF_INIT
;
1656 git_config
*config
= NULL
;
1657 const char *repo_dir
= git_repository_path(repo
);
1659 if (!(error
= repo_local_config(&config
, &path
, repo
, repo_dir
)))
1660 error
= repo_init_fs_configs(
1661 config
, path
.ptr
, repo_dir
, git_repository_workdir(repo
), true);
1663 git_config_free(config
);
1664 git_buf_dispose(&path
);
1666 git_repository__configmap_lookup_cache_clear(repo
);
1668 if (!repo
->is_bare
&& recurse
)
1669 (void)git_submodule_foreach(repo
, repo_reinit_submodule_fs
, NULL
);
1674 static int repo_write_template(
1675 const char *git_dir
,
1676 bool allow_overwrite
,
1680 const char *content
)
1682 git_buf path
= GIT_BUF_INIT
;
1683 int fd
, error
= 0, flags
;
1685 if (git_buf_joinpath(&path
, git_dir
, file
) < 0)
1688 if (allow_overwrite
)
1689 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
1691 flags
= O_WRONLY
| O_CREAT
| O_EXCL
;
1693 fd
= p_open(git_buf_cstr(&path
), flags
, mode
);
1696 error
= p_write(fd
, content
, strlen(content
));
1700 else if (errno
!= EEXIST
)
1704 if (!error
&& hidden
) {
1705 if (git_win32__set_hidden(path
.ptr
, true) < 0)
1712 git_buf_dispose(&path
);
1715 git_error_set(GIT_ERROR_OS
,
1716 "failed to initialize repository with template '%s'", file
);
1721 static int repo_write_gitlink(
1722 const char *in_dir
, const char *to_repo
, bool use_relative_path
)
1725 git_buf buf
= GIT_BUF_INIT
;
1726 git_buf path_to_repo
= GIT_BUF_INIT
;
1729 git_path_dirname_r(&buf
, to_repo
);
1730 git_path_to_dir(&buf
);
1731 if (git_buf_oom(&buf
))
1734 /* don't write gitlink to natural workdir */
1735 if (git__suffixcmp(to_repo
, "/" DOT_GIT
"/") == 0 &&
1736 strcmp(in_dir
, buf
.ptr
) == 0)
1738 error
= GIT_PASSTHROUGH
;
1742 if ((error
= git_buf_joinpath(&buf
, in_dir
, DOT_GIT
)) < 0)
1745 if (!p_stat(buf
.ptr
, &st
) && !S_ISREG(st
.st_mode
)) {
1746 git_error_set(GIT_ERROR_REPOSITORY
,
1747 "cannot overwrite gitlink file into path '%s'", in_dir
);
1748 error
= GIT_EEXISTS
;
1752 git_buf_clear(&buf
);
1754 error
= git_buf_sets(&path_to_repo
, to_repo
);
1756 if (!error
&& use_relative_path
)
1757 error
= git_path_make_relative(&path_to_repo
, in_dir
);
1760 error
= git_buf_join(&buf
, ' ', GIT_FILE_CONTENT_PREFIX
, path_to_repo
.ptr
);
1763 error
= repo_write_template(in_dir
, true, DOT_GIT
, 0666, true, buf
.ptr
);
1766 git_buf_dispose(&buf
);
1767 git_buf_dispose(&path_to_repo
);
1771 static mode_t
pick_dir_mode(git_repository_init_options
*opts
)
1773 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_UMASK
)
1775 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_GROUP
)
1776 return (0775 | S_ISGID
);
1777 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_ALL
)
1778 return (0777 | S_ISGID
);
1782 #include "repo_template.h"
1784 static int repo_init_structure(
1785 const char *repo_dir
,
1786 const char *work_dir
,
1787 git_repository_init_options
*opts
)
1790 repo_template_item
*tpl
;
1792 ((opts
->flags
& GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE
) != 0);
1793 mode_t dmode
= pick_dir_mode(opts
);
1794 bool chmod
= opts
->mode
!= GIT_REPOSITORY_INIT_SHARED_UMASK
;
1796 /* Hide the ".git" directory */
1798 if ((opts
->flags
& GIT_REPOSITORY_INIT__HAS_DOTGIT
) != 0) {
1799 if (git_win32__set_hidden(repo_dir
, true) < 0) {
1800 git_error_set(GIT_ERROR_OS
,
1801 "failed to mark Git repository folder as hidden");
1807 /* Create the .git gitlink if appropriate */
1808 if ((opts
->flags
& GIT_REPOSITORY_INIT_BARE
) == 0 &&
1809 (opts
->flags
& GIT_REPOSITORY_INIT__NATURAL_WD
) == 0)
1811 if (repo_write_gitlink(work_dir
, repo_dir
, opts
->flags
& GIT_REPOSITORY_INIT_RELATIVE_GITLINK
) < 0)
1815 /* Copy external template if requested */
1817 git_config
*cfg
= NULL
;
1818 const char *tdir
= NULL
;
1819 bool default_template
= false;
1820 git_buf template_buf
= GIT_BUF_INIT
;
1822 if (opts
->template_path
)
1823 tdir
= opts
->template_path
;
1824 else if ((error
= git_config_open_default(&cfg
)) >= 0) {
1825 if (!git_config_get_path(&template_buf
, cfg
, "init.templatedir"))
1826 tdir
= template_buf
.ptr
;
1831 if (!(error
= git_sysdir_find_template_dir(&template_buf
)))
1832 tdir
= template_buf
.ptr
;
1833 default_template
= true;
1837 * If tdir was the empty string, treat it like tdir was a path to an
1838 * empty directory (so, don't do any copying). This is the behavior
1839 * that git(1) exhibits, although it doesn't seem to be officially
1842 if (tdir
&& git__strcmp(tdir
, "") != 0) {
1843 uint32_t cpflags
= GIT_CPDIR_COPY_SYMLINKS
|
1844 GIT_CPDIR_SIMPLE_TO_MODE
|
1845 GIT_CPDIR_COPY_DOTFILES
;
1846 if (opts
->mode
!= GIT_REPOSITORY_INIT_SHARED_UMASK
)
1847 cpflags
|= GIT_CPDIR_CHMOD_DIRS
;
1848 error
= git_futils_cp_r(tdir
, repo_dir
, cpflags
, dmode
);
1851 git_buf_dispose(&template_buf
);
1852 git_config_free(cfg
);
1855 if (!default_template
)
1858 /* if template was default, ignore error and use internal */
1860 external_tpl
= false;
1865 /* Copy internal template
1866 * - always ensure existence of dirs
1867 * - only create files if no external template was specified
1869 for (tpl
= repo_template
; !error
&& tpl
->path
; ++tpl
) {
1870 if (!tpl
->content
) {
1871 uint32_t mkdir_flags
= GIT_MKDIR_PATH
;
1873 mkdir_flags
|= GIT_MKDIR_CHMOD
;
1875 error
= git_futils_mkdir_relative(
1876 tpl
->path
, repo_dir
, dmode
, mkdir_flags
, NULL
);
1878 else if (!external_tpl
) {
1879 const char *content
= tpl
->content
;
1881 if (opts
->description
&& strcmp(tpl
->path
, GIT_DESC_FILE
) == 0)
1882 content
= opts
->description
;
1884 error
= repo_write_template(
1885 repo_dir
, false, tpl
->path
, tpl
->mode
, false, content
);
1892 static int mkdir_parent(git_buf
*buf
, uint32_t mode
, bool skip2
)
1894 /* When making parent directories during repository initialization
1895 * don't try to set gid or grant world write access
1897 return git_futils_mkdir(
1898 buf
->ptr
, mode
& ~(S_ISGID
| 0002),
1899 GIT_MKDIR_PATH
| GIT_MKDIR_VERIFY_DIR
|
1900 (skip2
? GIT_MKDIR_SKIP_LAST2
: GIT_MKDIR_SKIP_LAST
));
1903 static int repo_init_directories(
1906 const char *given_repo
,
1907 git_repository_init_options
*opts
)
1910 bool is_bare
, add_dotgit
, has_dotgit
, natural_wd
;
1913 /* There are three possible rules for what we are allowed to create:
1914 * - MKPATH means anything we need
1915 * - MKDIR means just the .git directory and its parent and the workdir
1916 * - Neither means only the .git directory can be created
1918 * There are 5 "segments" of path that we might need to deal with:
1919 * 1. The .git directory
1920 * 2. The parent of the .git directory
1921 * 3. Everything above the parent of the .git directory
1922 * 4. The working directory (often the same as #2)
1923 * 5. Everything above the working directory (often the same as #3)
1925 * For all directories created, we start with the init_mode value for
1926 * permissions and then strip off bits in some cases:
1928 * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH
1929 * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID
1930 * For all rules, we create #1 using the untouched init_mode
1933 /* set up repo path */
1935 is_bare
= ((opts
->flags
& GIT_REPOSITORY_INIT_BARE
) != 0);
1938 (opts
->flags
& GIT_REPOSITORY_INIT_NO_DOTGIT_DIR
) == 0 &&
1940 git__suffixcmp(given_repo
, "/" DOT_GIT
) != 0 &&
1941 git__suffixcmp(given_repo
, "/" GIT_DIR
) != 0;
1943 if (git_buf_joinpath(repo_path
, given_repo
, add_dotgit
? GIT_DIR
: "") < 0)
1946 has_dotgit
= (git__suffixcmp(repo_path
->ptr
, "/" GIT_DIR
) == 0);
1948 opts
->flags
|= GIT_REPOSITORY_INIT__HAS_DOTGIT
;
1950 /* set up workdir path */
1953 if (opts
->workdir_path
) {
1954 if (git_path_join_unrooted(
1955 wd_path
, opts
->workdir_path
, repo_path
->ptr
, NULL
) < 0)
1957 } else if (has_dotgit
) {
1958 if (git_path_dirname_r(wd_path
, repo_path
->ptr
) < 0)
1961 git_error_set(GIT_ERROR_REPOSITORY
, "cannot pick working directory"
1962 " for non-bare repository that isn't a '.git' directory");
1966 if (git_path_to_dir(wd_path
) < 0)
1969 git_buf_clear(wd_path
);
1974 wd_path
->size
> 0 &&
1975 wd_path
->size
+ strlen(GIT_DIR
) == repo_path
->size
&&
1976 memcmp(repo_path
->ptr
, wd_path
->ptr
, wd_path
->size
) == 0;
1978 opts
->flags
|= GIT_REPOSITORY_INIT__NATURAL_WD
;
1980 /* create directories as needed / requested */
1982 dirmode
= pick_dir_mode(opts
);
1984 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0) {
1985 /* create path #5 */
1986 if (wd_path
->size
> 0 &&
1987 (error
= mkdir_parent(wd_path
, dirmode
, false)) < 0)
1990 /* create path #3 (if not the same as #5) */
1992 (error
= mkdir_parent(repo_path
, dirmode
, has_dotgit
)) < 0)
1996 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKDIR
) != 0 ||
1997 (opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0)
1999 /* create path #4 */
2000 if (wd_path
->size
> 0 &&
2001 (error
= git_futils_mkdir(
2002 wd_path
->ptr
, dirmode
& ~S_ISGID
,
2003 GIT_MKDIR_VERIFY_DIR
)) < 0)
2006 /* create path #2 (if not the same as #4) */
2008 (error
= git_futils_mkdir(
2009 repo_path
->ptr
, dirmode
& ~S_ISGID
,
2010 GIT_MKDIR_VERIFY_DIR
| GIT_MKDIR_SKIP_LAST
)) < 0)
2014 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKDIR
) != 0 ||
2015 (opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0 ||
2018 /* create path #1 */
2019 error
= git_futils_mkdir(repo_path
->ptr
, dirmode
,
2020 GIT_MKDIR_VERIFY_DIR
| ((dirmode
& S_ISGID
) ? GIT_MKDIR_CHMOD
: 0));
2023 /* prettify both directories now that they are created */
2026 error
= git_path_prettify_dir(repo_path
, repo_path
->ptr
, NULL
);
2028 if (!error
&& wd_path
->size
> 0)
2029 error
= git_path_prettify_dir(wd_path
, wd_path
->ptr
, NULL
);
2035 static int repo_init_create_origin(git_repository
*repo
, const char *url
)
2040 if (!(error
= git_remote_create(&remote
, repo
, GIT_REMOTE_ORIGIN
, url
))) {
2041 git_remote_free(remote
);
2047 int git_repository_init(
2048 git_repository
**repo_out
, const char *path
, unsigned is_bare
)
2050 git_repository_init_options opts
= GIT_REPOSITORY_INIT_OPTIONS_INIT
;
2052 opts
.flags
= GIT_REPOSITORY_INIT_MKPATH
; /* don't love this default */
2054 opts
.flags
|= GIT_REPOSITORY_INIT_BARE
;
2056 return git_repository_init_ext(repo_out
, path
, &opts
);
2059 int git_repository_init_ext(
2060 git_repository
**out
,
2061 const char *given_repo
,
2062 git_repository_init_options
*opts
)
2064 git_buf repo_path
= GIT_BUF_INIT
, wd_path
= GIT_BUF_INIT
,
2065 common_path
= GIT_BUF_INIT
, head_path
= GIT_BUF_INIT
;
2070 assert(out
&& given_repo
&& opts
);
2072 GIT_ERROR_CHECK_VERSION(opts
, GIT_REPOSITORY_INIT_OPTIONS_VERSION
, "git_repository_init_options");
2074 if ((error
= repo_init_directories(&repo_path
, &wd_path
, given_repo
, opts
)) < 0)
2077 wd
= (opts
->flags
& GIT_REPOSITORY_INIT_BARE
) ? NULL
: git_buf_cstr(&wd_path
);
2079 if ((error
= is_valid_repository_path(&is_valid
, &repo_path
, &common_path
)) < 0)
2083 if ((opts
->flags
& GIT_REPOSITORY_INIT_NO_REINIT
) != 0) {
2084 git_error_set(GIT_ERROR_REPOSITORY
,
2085 "attempt to reinitialize '%s'", given_repo
);
2086 error
= GIT_EEXISTS
;
2090 opts
->flags
|= GIT_REPOSITORY_INIT__IS_REINIT
;
2092 if ((error
= repo_init_config(repo_path
.ptr
, wd
, opts
->flags
, opts
->mode
)) < 0)
2095 /* TODO: reinitialize the templates */
2097 if ((error
= repo_init_structure(repo_path
.ptr
, wd
, opts
)) < 0 ||
2098 (error
= repo_init_config(repo_path
.ptr
, wd
, opts
->flags
, opts
->mode
)) < 0 ||
2099 (error
= git_buf_joinpath(&head_path
, repo_path
.ptr
, GIT_HEAD_FILE
)) < 0)
2103 * Only set the new HEAD if the file does not exist already via
2104 * a template or if the caller has explicitly supplied an
2105 * initial HEAD value.
2107 if ((!git_path_exists(head_path
.ptr
) || opts
->initial_head
) &&
2108 (error
= git_repository_create_head(repo_path
.ptr
, opts
->initial_head
)) < 0)
2112 if ((error
= git_repository_open(out
, repo_path
.ptr
)) < 0)
2115 if (opts
->origin_url
&&
2116 (error
= repo_init_create_origin(*out
, opts
->origin_url
)) < 0)
2120 git_buf_dispose(&head_path
);
2121 git_buf_dispose(&common_path
);
2122 git_buf_dispose(&repo_path
);
2123 git_buf_dispose(&wd_path
);
2128 int git_repository_head_detached(git_repository
*repo
)
2131 git_odb
*odb
= NULL
;
2134 if (git_repository_odb__weakptr(&odb
, repo
) < 0)
2137 if (git_reference_lookup(&ref
, repo
, GIT_HEAD_FILE
) < 0)
2140 if (git_reference_type(ref
) == GIT_REFERENCE_SYMBOLIC
) {
2141 git_reference_free(ref
);
2145 exists
= git_odb_exists(odb
, git_reference_target(ref
));
2147 git_reference_free(ref
);
2151 static int get_worktree_file_path(git_buf
*out
, git_repository
*repo
, const char *worktree
, const char *file
)
2154 return git_buf_printf(out
, "%s/worktrees/%s/%s", repo
->commondir
, worktree
, file
);
2157 int git_repository_head_detached_for_worktree(git_repository
*repo
, const char *name
)
2159 git_reference
*ref
= NULL
;
2162 assert(repo
&& name
);
2164 if ((error
= git_repository_head_for_worktree(&ref
, repo
, name
)) < 0)
2167 error
= (git_reference_type(ref
) != GIT_REFERENCE_SYMBOLIC
);
2169 git_reference_free(ref
);
2174 int git_repository_head(git_reference
**head_out
, git_repository
*repo
)
2176 git_reference
*head
;
2181 if ((error
= git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
)) < 0)
2184 if (git_reference_type(head
) == GIT_REFERENCE_DIRECT
) {
2189 error
= git_reference_lookup_resolved(head_out
, repo
, git_reference_symbolic_target(head
), -1);
2190 git_reference_free(head
);
2192 return error
== GIT_ENOTFOUND
? GIT_EUNBORNBRANCH
: error
;
2195 int git_repository_head_for_worktree(git_reference
**out
, git_repository
*repo
, const char *name
)
2197 git_buf path
= GIT_BUF_INIT
;
2198 git_reference
*head
= NULL
;
2201 assert(out
&& repo
&& name
);
2205 if ((error
= get_worktree_file_path(&path
, repo
, name
, GIT_HEAD_FILE
)) < 0 ||
2206 (error
= git_reference__read_head(&head
, repo
, path
.ptr
)) < 0)
2209 if (git_reference_type(head
) != GIT_REFERENCE_DIRECT
) {
2210 git_reference
*resolved
;
2212 error
= git_reference_lookup_resolved(&resolved
, repo
, git_reference_symbolic_target(head
), -1);
2213 git_reference_free(head
);
2221 git_reference_free(head
);
2223 git_buf_dispose(&path
);
2228 int git_repository_foreach_head(git_repository
*repo
,
2229 git_repository_foreach_head_cb cb
,
2230 int flags
, void *payload
)
2232 git_strarray worktrees
= GIT_VECTOR_INIT
;
2233 git_buf path
= GIT_BUF_INIT
;
2238 if (!(flags
& GIT_REPOSITORY_FOREACH_HEAD_SKIP_REPO
)) {
2239 /* Gather HEAD of main repository */
2240 if ((error
= git_buf_joinpath(&path
, repo
->commondir
, GIT_HEAD_FILE
)) < 0 ||
2241 (error
= cb(repo
, path
.ptr
, payload
) != 0))
2245 if (!(flags
& GIT_REPOSITORY_FOREACH_HEAD_SKIP_WORKTREES
)) {
2246 if ((error
= git_worktree_list(&worktrees
, repo
)) < 0) {
2251 /* Gather HEADs of all worktrees */
2252 for (i
= 0; i
< worktrees
.count
; i
++) {
2253 if (get_worktree_file_path(&path
, repo
, worktrees
.strings
[i
], GIT_HEAD_FILE
) < 0)
2256 if ((error
= cb(repo
, path
.ptr
, payload
)) != 0)
2262 git_buf_dispose(&path
);
2263 git_strarray_free(&worktrees
);
2267 int git_repository_head_unborn(git_repository
*repo
)
2269 git_reference
*ref
= NULL
;
2272 error
= git_repository_head(&ref
, repo
);
2273 git_reference_free(ref
);
2275 if (error
== GIT_EUNBORNBRANCH
) {
2286 static int at_least_one_cb(const char *refname
, void *payload
)
2288 GIT_UNUSED(refname
);
2289 GIT_UNUSED(payload
);
2290 return GIT_PASSTHROUGH
;
2293 static int repo_contains_no_reference(git_repository
*repo
)
2295 int error
= git_reference_foreach_name(repo
, &at_least_one_cb
, NULL
);
2297 if (error
== GIT_PASSTHROUGH
)
2306 int git_repository_is_empty(git_repository
*repo
)
2308 git_reference
*head
= NULL
;
2311 if (git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
) < 0)
2314 if (git_reference_type(head
) == GIT_REFERENCE_SYMBOLIC
)
2316 (strcmp(git_reference_symbolic_target(head
),
2317 GIT_REFS_HEADS_DIR
"master") == 0) &&
2318 repo_contains_no_reference(repo
);
2320 git_reference_free(head
);
2325 static const char *resolved_parent_path(const git_repository
*repo
, git_repository_item_t item
, git_repository_item_t fallback
)
2330 case GIT_REPOSITORY_ITEM_GITDIR
:
2331 parent
= git_repository_path(repo
);
2333 case GIT_REPOSITORY_ITEM_WORKDIR
:
2334 parent
= git_repository_workdir(repo
);
2336 case GIT_REPOSITORY_ITEM_COMMONDIR
:
2337 parent
= git_repository_commondir(repo
);
2340 git_error_set(GIT_ERROR_INVALID
, "invalid item directory");
2343 if (!parent
&& fallback
!= GIT_REPOSITORY_ITEM__LAST
)
2344 return resolved_parent_path(repo
, fallback
, GIT_REPOSITORY_ITEM__LAST
);
2349 int git_repository_item_path(git_buf
*out
, const git_repository
*repo
, git_repository_item_t item
)
2351 const char *parent
= resolved_parent_path(repo
, items
[item
].parent
, items
[item
].fallback
);
2352 if (parent
== NULL
) {
2353 git_error_set(GIT_ERROR_INVALID
, "path cannot exist in repository");
2354 return GIT_ENOTFOUND
;
2357 if (git_buf_sets(out
, parent
) < 0)
2360 if (items
[item
].name
) {
2361 if (git_buf_joinpath(out
, parent
, items
[item
].name
) < 0)
2365 if (items
[item
].directory
) {
2366 if (git_path_to_dir(out
) < 0)
2373 const char *git_repository_path(const git_repository
*repo
)
2376 return repo
->gitdir
;
2379 const char *git_repository_workdir(const git_repository
*repo
)
2386 return repo
->workdir
;
2389 const char *git_repository_commondir(const git_repository
*repo
)
2392 return repo
->commondir
;
2395 int git_repository_set_workdir(
2396 git_repository
*repo
, const char *workdir
, int update_gitlink
)
2399 git_buf path
= GIT_BUF_INIT
;
2401 assert(repo
&& workdir
);
2403 if (git_path_prettify_dir(&path
, workdir
, NULL
) < 0)
2406 if (repo
->workdir
&& strcmp(repo
->workdir
, path
.ptr
) == 0)
2409 if (update_gitlink
) {
2412 if (git_repository_config__weakptr(&config
, repo
) < 0)
2415 error
= repo_write_gitlink(path
.ptr
, git_repository_path(repo
), false);
2417 /* passthrough error means gitlink is unnecessary */
2418 if (error
== GIT_PASSTHROUGH
)
2419 error
= git_config_delete_entry(config
, "core.worktree");
2421 error
= git_config_set_string(config
, "core.worktree", path
.ptr
);
2424 error
= git_config_set_bool(config
, "core.bare", false);
2428 char *old_workdir
= repo
->workdir
;
2430 repo
->workdir
= git_buf_detach(&path
);
2433 git__free(old_workdir
);
2439 int git_repository_is_bare(const git_repository
*repo
)
2442 return repo
->is_bare
;
2445 int git_repository_is_worktree(const git_repository
*repo
)
2448 return repo
->is_worktree
;
2451 int git_repository_set_bare(git_repository
*repo
)
2461 if ((error
= git_repository_config__weakptr(&config
, repo
)) < 0)
2464 if ((error
= git_config_set_bool(config
, "core.bare", true)) < 0)
2467 if ((error
= git_config__update_entry(config
, "core.worktree", NULL
, true, true)) < 0)
2470 git__free(repo
->workdir
);
2471 repo
->workdir
= NULL
;
2477 int git_repository_head_tree(git_tree
**tree
, git_repository
*repo
)
2479 git_reference
*head
;
2483 if ((error
= git_repository_head(&head
, repo
)) < 0)
2486 if ((error
= git_reference_peel(&obj
, head
, GIT_OBJECT_TREE
)) < 0)
2489 *tree
= (git_tree
*)obj
;
2492 git_reference_free(head
);
2496 int git_repository__set_orig_head(git_repository
*repo
, const git_oid
*orig_head
)
2498 git_filebuf file
= GIT_FILEBUF_INIT
;
2499 git_buf file_path
= GIT_BUF_INIT
;
2500 char orig_head_str
[GIT_OID_HEXSZ
];
2503 git_oid_fmt(orig_head_str
, orig_head
);
2505 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_ORIG_HEAD_FILE
)) == 0 &&
2506 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) == 0 &&
2507 (error
= git_filebuf_printf(&file
, "%.*s\n", GIT_OID_HEXSZ
, orig_head_str
)) == 0)
2508 error
= git_filebuf_commit(&file
);
2511 git_filebuf_cleanup(&file
);
2513 git_buf_dispose(&file_path
);
2518 int git_repository_message(git_buf
*out
, git_repository
*repo
)
2520 git_buf path
= GIT_BUF_INIT
;
2524 git_buf_sanitize(out
);
2526 if (git_buf_joinpath(&path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
) < 0)
2529 if ((error
= p_stat(git_buf_cstr(&path
), &st
)) < 0) {
2530 if (errno
== ENOENT
)
2531 error
= GIT_ENOTFOUND
;
2532 git_error_set(GIT_ERROR_OS
, "could not access message file");
2534 error
= git_futils_readbuffer(out
, git_buf_cstr(&path
));
2537 git_buf_dispose(&path
);
2542 int git_repository_message_remove(git_repository
*repo
)
2544 git_buf path
= GIT_BUF_INIT
;
2547 if (git_buf_joinpath(&path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
) < 0)
2550 error
= p_unlink(git_buf_cstr(&path
));
2551 git_buf_dispose(&path
);
2556 int git_repository_hashfile(
2558 git_repository
*repo
,
2561 const char *as_path
)
2564 git_filter_list
*fl
= NULL
;
2567 git_buf full_path
= GIT_BUF_INIT
;
2569 assert(out
&& path
&& repo
); /* as_path can be NULL */
2571 /* At some point, it would be nice if repo could be NULL to just
2572 * apply filter rules defined in system and global files, but for
2573 * now that is not possible because git_filters_load() needs it.
2576 error
= git_path_join_unrooted(
2577 &full_path
, path
, git_repository_workdir(repo
), NULL
);
2584 /* passing empty string for "as_path" indicated --no-filters */
2585 if (strlen(as_path
) > 0) {
2586 error
= git_filter_list_load(
2587 &fl
, repo
, NULL
, as_path
,
2588 GIT_FILTER_TO_ODB
, GIT_FILTER_DEFAULT
);
2595 /* at this point, error is a count of the number of loaded filters */
2597 fd
= git_futils_open_ro(full_path
.ptr
);
2603 if ((error
= git_futils_filesize(&len
, fd
)) < 0)
2606 if (!git__is_sizet(len
)) {
2607 git_error_set(GIT_ERROR_OS
, "file size overflow for 32-bit systems");
2612 error
= git_odb__hashfd_filtered(out
, fd
, (size_t)len
, type
, fl
);
2617 git_filter_list_free(fl
);
2618 git_buf_dispose(&full_path
);
2623 static int checkout_message(git_buf
*out
, git_reference
*old
, const char *new)
2625 git_buf_puts(out
, "checkout: moving from ");
2627 if (git_reference_type(old
) == GIT_REFERENCE_SYMBOLIC
)
2628 git_buf_puts(out
, git_reference__shorthand(git_reference_symbolic_target(old
)));
2630 git_buf_puts(out
, git_oid_tostr_s(git_reference_target(old
)));
2632 git_buf_puts(out
, " to ");
2634 if (git_reference__is_branch(new) ||
2635 git_reference__is_tag(new) ||
2636 git_reference__is_remote(new))
2637 git_buf_puts(out
, git_reference__shorthand(new));
2639 git_buf_puts(out
, new);
2641 if (git_buf_oom(out
))
2647 static int detach(git_repository
*repo
, const git_oid
*id
, const char *new)
2650 git_buf log_message
= GIT_BUF_INIT
;
2651 git_object
*object
= NULL
, *peeled
= NULL
;
2652 git_reference
*new_head
= NULL
, *current
= NULL
;
2656 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2659 if ((error
= git_object_lookup(&object
, repo
, id
, GIT_OBJECT_ANY
)) < 0)
2662 if ((error
= git_object_peel(&peeled
, object
, GIT_OBJECT_COMMIT
)) < 0)
2666 new = git_oid_tostr_s(git_object_id(peeled
));
2668 if ((error
= checkout_message(&log_message
, current
, new)) < 0)
2671 error
= git_reference_create(&new_head
, repo
, GIT_HEAD_FILE
, git_object_id(peeled
), true, git_buf_cstr(&log_message
));
2674 git_buf_dispose(&log_message
);
2675 git_object_free(object
);
2676 git_object_free(peeled
);
2677 git_reference_free(current
);
2678 git_reference_free(new_head
);
2682 int git_repository_set_head(
2683 git_repository
* repo
,
2684 const char* refname
)
2686 git_reference
*ref
= NULL
, *current
= NULL
, *new_head
= NULL
;
2687 git_buf log_message
= GIT_BUF_INIT
;
2690 assert(repo
&& refname
);
2692 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2695 if ((error
= checkout_message(&log_message
, current
, refname
)) < 0)
2698 error
= git_reference_lookup(&ref
, repo
, refname
);
2699 if (error
< 0 && error
!= GIT_ENOTFOUND
)
2702 if (ref
&& current
->type
== GIT_REFERENCE_SYMBOLIC
&& git__strcmp(current
->target
.symbolic
, ref
->name
) &&
2703 git_reference_is_branch(ref
) && git_branch_is_checked_out(ref
)) {
2704 git_error_set(GIT_ERROR_REPOSITORY
, "cannot set HEAD to reference '%s' as it is the current HEAD "
2705 "of a linked repository.", git_reference_name(ref
));
2711 if (git_reference_is_branch(ref
)) {
2712 error
= git_reference_symbolic_create(&new_head
, repo
, GIT_HEAD_FILE
,
2713 git_reference_name(ref
), true, git_buf_cstr(&log_message
));
2715 error
= detach(repo
, git_reference_target(ref
),
2716 git_reference_is_tag(ref
) || git_reference_is_remote(ref
) ? refname
: NULL
);
2718 } else if (git_reference__is_branch(refname
)) {
2719 error
= git_reference_symbolic_create(&new_head
, repo
, GIT_HEAD_FILE
, refname
,
2720 true, git_buf_cstr(&log_message
));
2724 git_buf_dispose(&log_message
);
2725 git_reference_free(current
);
2726 git_reference_free(ref
);
2727 git_reference_free(new_head
);
2731 int git_repository_set_head_detached(
2732 git_repository
* repo
,
2733 const git_oid
* commitish
)
2735 return detach(repo
, commitish
, NULL
);
2738 int git_repository_set_head_detached_from_annotated(
2739 git_repository
*repo
,
2740 const git_annotated_commit
*commitish
)
2742 assert(repo
&& commitish
);
2744 return detach(repo
, git_annotated_commit_id(commitish
), commitish
->description
);
2747 int git_repository_detach_head(git_repository
* repo
)
2749 git_reference
*old_head
= NULL
, *new_head
= NULL
, *current
= NULL
;
2750 git_object
*object
= NULL
;
2751 git_buf log_message
= GIT_BUF_INIT
;
2756 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2759 if ((error
= git_repository_head(&old_head
, repo
)) < 0)
2762 if ((error
= git_object_lookup(&object
, repo
, git_reference_target(old_head
), GIT_OBJECT_COMMIT
)) < 0)
2765 if ((error
= checkout_message(&log_message
, current
, git_oid_tostr_s(git_object_id(object
)))) < 0)
2768 error
= git_reference_create(&new_head
, repo
, GIT_HEAD_FILE
, git_reference_target(old_head
),
2769 1, git_buf_cstr(&log_message
));
2772 git_buf_dispose(&log_message
);
2773 git_object_free(object
);
2774 git_reference_free(old_head
);
2775 git_reference_free(new_head
);
2776 git_reference_free(current
);
2781 * Loosely ported from git.git
2782 * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289
2784 int git_repository_state(git_repository
*repo
)
2786 git_buf repo_path
= GIT_BUF_INIT
;
2787 int state
= GIT_REPOSITORY_STATE_NONE
;
2791 if (git_buf_puts(&repo_path
, repo
->gitdir
) < 0)
2794 if (git_path_contains_file(&repo_path
, GIT_REBASE_MERGE_INTERACTIVE_FILE
))
2795 state
= GIT_REPOSITORY_STATE_REBASE_INTERACTIVE
;
2796 else if (git_path_contains_dir(&repo_path
, GIT_REBASE_MERGE_DIR
))
2797 state
= GIT_REPOSITORY_STATE_REBASE_MERGE
;
2798 else if (git_path_contains_file(&repo_path
, GIT_REBASE_APPLY_REBASING_FILE
))
2799 state
= GIT_REPOSITORY_STATE_REBASE
;
2800 else if (git_path_contains_file(&repo_path
, GIT_REBASE_APPLY_APPLYING_FILE
))
2801 state
= GIT_REPOSITORY_STATE_APPLY_MAILBOX
;
2802 else if (git_path_contains_dir(&repo_path
, GIT_REBASE_APPLY_DIR
))
2803 state
= GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE
;
2804 else if (git_path_contains_file(&repo_path
, GIT_MERGE_HEAD_FILE
))
2805 state
= GIT_REPOSITORY_STATE_MERGE
;
2806 else if (git_path_contains_file(&repo_path
, GIT_REVERT_HEAD_FILE
)) {
2807 state
= GIT_REPOSITORY_STATE_REVERT
;
2808 if (git_path_contains_file(&repo_path
, GIT_SEQUENCER_TODO_FILE
)) {
2809 state
= GIT_REPOSITORY_STATE_REVERT_SEQUENCE
;
2811 } else if (git_path_contains_file(&repo_path
, GIT_CHERRYPICK_HEAD_FILE
)) {
2812 state
= GIT_REPOSITORY_STATE_CHERRYPICK
;
2813 if (git_path_contains_file(&repo_path
, GIT_SEQUENCER_TODO_FILE
)) {
2814 state
= GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE
;
2816 } else if (git_path_contains_file(&repo_path
, GIT_BISECT_LOG_FILE
))
2817 state
= GIT_REPOSITORY_STATE_BISECT
;
2819 git_buf_dispose(&repo_path
);
2823 int git_repository__cleanup_files(
2824 git_repository
*repo
, const char *files
[], size_t files_len
)
2826 git_buf buf
= GIT_BUF_INIT
;
2830 for (error
= 0, i
= 0; !error
&& i
< files_len
; ++i
) {
2833 if (git_buf_joinpath(&buf
, repo
->gitdir
, files
[i
]) < 0)
2836 path
= git_buf_cstr(&buf
);
2838 if (git_path_isfile(path
)) {
2839 error
= p_unlink(path
);
2840 } else if (git_path_isdir(path
)) {
2841 error
= git_futils_rmdir_r(path
, NULL
,
2842 GIT_RMDIR_REMOVE_FILES
| GIT_RMDIR_REMOVE_BLOCKERS
);
2845 git_buf_clear(&buf
);
2848 git_buf_dispose(&buf
);
2852 static const char *state_files
[] = {
2853 GIT_MERGE_HEAD_FILE
,
2854 GIT_MERGE_MODE_FILE
,
2856 GIT_REVERT_HEAD_FILE
,
2857 GIT_CHERRYPICK_HEAD_FILE
,
2858 GIT_BISECT_LOG_FILE
,
2859 GIT_REBASE_MERGE_DIR
,
2860 GIT_REBASE_APPLY_DIR
,
2864 int git_repository_state_cleanup(git_repository
*repo
)
2868 return git_repository__cleanup_files(repo
, state_files
, ARRAY_SIZE(state_files
));
2871 int git_repository_is_shallow(git_repository
*repo
)
2873 git_buf path
= GIT_BUF_INIT
;
2877 if ((error
= git_buf_joinpath(&path
, repo
->gitdir
, "shallow")) < 0)
2880 error
= git_path_lstat(path
.ptr
, &st
);
2881 git_buf_dispose(&path
);
2883 if (error
== GIT_ENOTFOUND
) {
2890 return st
.st_size
== 0 ? 0 : 1;
2893 int git_repository_init_options_init(
2894 git_repository_init_options
*opts
, unsigned int version
)
2896 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
2897 opts
, version
, git_repository_init_options
,
2898 GIT_REPOSITORY_INIT_OPTIONS_INIT
);
2902 int git_repository_init_init_options(
2903 git_repository_init_options
*opts
, unsigned int version
)
2905 return git_repository_init_options_init(opts
, version
);
2908 int git_repository_ident(const char **name
, const char **email
, const git_repository
*repo
)
2910 *name
= repo
->ident_name
;
2911 *email
= repo
->ident_email
;
2916 int git_repository_set_ident(git_repository
*repo
, const char *name
, const char *email
)
2918 char *tmp_name
= NULL
, *tmp_email
= NULL
;
2921 tmp_name
= git__strdup(name
);
2922 GIT_ERROR_CHECK_ALLOC(tmp_name
);
2926 tmp_email
= git__strdup(email
);
2927 GIT_ERROR_CHECK_ALLOC(tmp_email
);
2930 tmp_name
= git__swap(repo
->ident_name
, tmp_name
);
2931 tmp_email
= git__swap(repo
->ident_email
, tmp_email
);
2933 git__free(tmp_name
);
2934 git__free(tmp_email
);
2939 int git_repository_submodule_cache_all(git_repository
*repo
)
2945 if ((error
= git_strmap_new(&repo
->submodule_cache
)))
2948 error
= git_submodule__map(repo
, repo
->submodule_cache
);
2952 int git_repository_submodule_cache_clear(git_repository
*repo
)
2956 if (repo
->submodule_cache
== NULL
) {
2959 git_strmap_foreach_value(repo
->submodule_cache
, sm
, {
2960 git_submodule_free(sm
);
2962 git_strmap_free(repo
->submodule_cache
);
2963 repo
->submodule_cache
= 0;