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
;
48 { GIT_REPOSITORY_ITEM_GITDIR
, NULL
, true },
49 { GIT_REPOSITORY_ITEM_WORKDIR
, NULL
, true },
50 { GIT_REPOSITORY_ITEM_COMMONDIR
, NULL
, true },
51 { GIT_REPOSITORY_ITEM_GITDIR
, "index", false },
52 { GIT_REPOSITORY_ITEM_COMMONDIR
, "objects", true },
53 { GIT_REPOSITORY_ITEM_COMMONDIR
, "refs", true },
54 { GIT_REPOSITORY_ITEM_COMMONDIR
, "packed-refs", false },
55 { GIT_REPOSITORY_ITEM_COMMONDIR
, "remotes", true },
56 { GIT_REPOSITORY_ITEM_COMMONDIR
, "config", false },
57 { GIT_REPOSITORY_ITEM_COMMONDIR
, "info", true },
58 { GIT_REPOSITORY_ITEM_COMMONDIR
, "hooks", true },
59 { GIT_REPOSITORY_ITEM_COMMONDIR
, "logs", true },
60 { GIT_REPOSITORY_ITEM_GITDIR
, "modules", true },
61 { GIT_REPOSITORY_ITEM_COMMONDIR
, "worktrees", true }
64 static int check_repositoryformatversion(git_config
*config
);
66 #define GIT_COMMONDIR_FILE "commondir"
67 #define GIT_GITDIR_FILE "gitdir"
69 #define GIT_FILE_CONTENT_PREFIX "gitdir:"
71 #define GIT_BRANCH_MASTER "master"
73 #define GIT_REPO_VERSION 0
75 git_buf git_repository__reserved_names_win32
[] = {
76 { DOT_GIT
, 0, CONST_STRLEN(DOT_GIT
) },
77 { GIT_DIR_SHORTNAME
, 0, CONST_STRLEN(GIT_DIR_SHORTNAME
) }
79 size_t git_repository__reserved_names_win32_len
= 2;
81 git_buf git_repository__reserved_names_posix
[] = {
82 { DOT_GIT
, 0, CONST_STRLEN(DOT_GIT
) },
84 size_t git_repository__reserved_names_posix_len
= 1;
86 static void set_odb(git_repository
*repo
, git_odb
*odb
)
89 GIT_REFCOUNT_OWN(odb
, repo
);
90 GIT_REFCOUNT_INC(odb
);
93 if ((odb
= git__swap(repo
->_odb
, odb
)) != NULL
) {
94 GIT_REFCOUNT_OWN(odb
, NULL
);
99 static void set_refdb(git_repository
*repo
, git_refdb
*refdb
)
102 GIT_REFCOUNT_OWN(refdb
, repo
);
103 GIT_REFCOUNT_INC(refdb
);
106 if ((refdb
= git__swap(repo
->_refdb
, refdb
)) != NULL
) {
107 GIT_REFCOUNT_OWN(refdb
, NULL
);
108 git_refdb_free(refdb
);
112 static void set_config(git_repository
*repo
, git_config
*config
)
115 GIT_REFCOUNT_OWN(config
, repo
);
116 GIT_REFCOUNT_INC(config
);
119 if ((config
= git__swap(repo
->_config
, config
)) != NULL
) {
120 GIT_REFCOUNT_OWN(config
, NULL
);
121 git_config_free(config
);
124 git_repository__cvar_cache_clear(repo
);
127 static void set_index(git_repository
*repo
, git_index
*index
)
130 GIT_REFCOUNT_OWN(index
, repo
);
131 GIT_REFCOUNT_INC(index
);
134 if ((index
= git__swap(repo
->_index
, index
)) != NULL
) {
135 GIT_REFCOUNT_OWN(index
, NULL
);
136 git_index_free(index
);
140 void git_repository__cleanup(git_repository
*repo
)
144 git_repository_submodule_cache_clear(repo
);
145 git_cache_clear(&repo
->objects
);
146 git_attr_cache_flush(repo
);
148 set_config(repo
, NULL
);
149 set_index(repo
, NULL
);
151 set_refdb(repo
, NULL
);
154 void git_repository_free(git_repository
*repo
)
161 git_repository__cleanup(repo
);
163 git_cache_free(&repo
->objects
);
165 git_diff_driver_registry_free(repo
->diff_drivers
);
166 repo
->diff_drivers
= NULL
;
168 for (i
= 0; i
< repo
->reserved_names
.size
; i
++)
169 git_buf_dispose(git_array_get(repo
->reserved_names
, i
));
170 git_array_clear(repo
->reserved_names
);
172 git__free(repo
->gitlink
);
173 git__free(repo
->gitdir
);
174 git__free(repo
->commondir
);
175 git__free(repo
->workdir
);
176 git__free(repo
->namespace);
177 git__free(repo
->ident_name
);
178 git__free(repo
->ident_email
);
180 git__memzero(repo
, sizeof(*repo
));
185 * Git repository open methods
187 * Open a repository object from its path
189 static bool valid_repository_path(git_buf
*repository_path
, git_buf
*common_path
)
191 /* Check if we have a separate commondir (e.g. we have a
193 if (git_path_contains_file(repository_path
, GIT_COMMONDIR_FILE
)) {
194 git_buf common_link
= GIT_BUF_INIT
;
195 git_buf_joinpath(&common_link
, repository_path
->ptr
, GIT_COMMONDIR_FILE
);
197 git_futils_readbuffer(&common_link
, common_link
.ptr
);
198 git_buf_rtrim(&common_link
);
200 if (git_path_is_relative(common_link
.ptr
)) {
201 git_buf_joinpath(common_path
, repository_path
->ptr
, common_link
.ptr
);
203 git_buf_swap(common_path
, &common_link
);
206 git_buf_dispose(&common_link
);
209 git_buf_set(common_path
, repository_path
->ptr
, repository_path
->size
);
212 /* Make sure the commondir path always has a trailing * slash */
213 if (git_buf_rfind(common_path
, '/') != (ssize_t
)common_path
->size
- 1)
214 git_buf_putc(common_path
, '/');
216 /* Ensure HEAD file exists */
217 if (git_path_contains_file(repository_path
, GIT_HEAD_FILE
) == false)
220 /* Check files in common dir */
221 if (git_path_contains_dir(common_path
, GIT_OBJECTS_DIR
) == false)
223 if (git_path_contains_dir(common_path
, GIT_REFS_DIR
) == false)
229 static git_repository
*repository_alloc(void)
231 git_repository
*repo
= git__calloc(1, sizeof(git_repository
));
234 git_cache_init(&repo
->objects
) < 0)
237 git_array_init_to_size(repo
->reserved_names
, 4);
238 if (!repo
->reserved_names
.ptr
)
241 /* set all the entries in the cvar cache to `unset` */
242 git_repository__cvar_cache_clear(repo
);
248 git_cache_free(&repo
->objects
);
254 int git_repository_new(git_repository
**out
)
256 git_repository
*repo
;
258 *out
= repo
= repository_alloc();
259 GIT_ERROR_CHECK_ALLOC(repo
);
262 repo
->is_worktree
= 0;
267 static int load_config_data(git_repository
*repo
, const git_config
*config
)
271 int err
= git_config_get_bool(&is_bare
, config
, "core.bare");
272 if (err
< 0 && err
!= GIT_ENOTFOUND
)
275 /* Try to figure out if it's bare, default to non-bare if it's not set */
276 if (err
!= GIT_ENOTFOUND
)
277 repo
->is_bare
= is_bare
&& !repo
->is_worktree
;
284 static int load_workdir(git_repository
*repo
, git_config
*config
, git_buf
*parent_path
)
287 git_config_entry
*ce
;
288 git_buf worktree
= GIT_BUF_INIT
;
289 git_buf path
= GIT_BUF_INIT
;
294 if ((error
= git_config__lookup_entry(
295 &ce
, config
, "core.worktree", false)) < 0)
298 if (repo
->is_worktree
) {
299 char *gitlink
= git_worktree__read_link(repo
->gitdir
, GIT_GITDIR_FILE
);
305 git_buf_attach(&worktree
, gitlink
, 0);
307 if ((git_path_dirname_r(&worktree
, worktree
.ptr
)) < 0 ||
308 git_path_to_dir(&worktree
) < 0) {
313 repo
->workdir
= git_buf_detach(&worktree
);
315 else if (ce
&& ce
->value
) {
316 if ((error
= git_path_prettify_dir(
317 &worktree
, ce
->value
, repo
->gitdir
)) < 0)
320 repo
->workdir
= git_buf_detach(&worktree
);
322 else if (parent_path
&& git_path_isdir(parent_path
->ptr
))
323 repo
->workdir
= git_buf_detach(parent_path
);
325 if (git_path_dirname_r(&worktree
, repo
->gitdir
) < 0 ||
326 git_path_to_dir(&worktree
) < 0) {
331 repo
->workdir
= git_buf_detach(&worktree
);
334 GIT_ERROR_CHECK_ALLOC(repo
->workdir
);
336 git_buf_dispose(&path
);
337 git_config_entry_free(ce
);
342 * This function returns furthest offset into path where a ceiling dir
343 * is found, so we can stop processing the path at that point.
345 * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on
346 * the stack could remove directories name limits, but at the cost of doing
347 * repeated malloc/frees inside the loop below, so let's not do it now.
349 static size_t find_ceiling_dir_offset(
351 const char *ceiling_directories
)
353 char buf
[GIT_PATH_MAX
+ 1];
354 char buf2
[GIT_PATH_MAX
+ 1];
355 const char *ceil
, *sep
;
356 size_t len
, max_len
= 0, min_len
;
360 min_len
= (size_t)(git_path_root(path
) + 1);
362 if (ceiling_directories
== NULL
|| min_len
== 0)
365 for (sep
= ceil
= ceiling_directories
; *sep
; ceil
= sep
+ 1) {
366 for (sep
= ceil
; *sep
&& *sep
!= GIT_PATH_LIST_SEPARATOR
; sep
++);
369 if (len
== 0 || len
>= sizeof(buf
) || git_path_root(ceil
) == -1)
372 strncpy(buf
, ceil
, len
);
375 if (p_realpath(buf
, buf2
) == NULL
)
379 if (len
> 0 && buf2
[len
-1] == '/')
382 if (!strncmp(path
, buf2
, len
) &&
383 (path
[len
] == '/' || !path
[len
]) &&
390 return (max_len
<= min_len
? min_len
: max_len
);
394 * Read the contents of `file_path` and set `path_out` to the repo dir that
395 * it points to. Before calling, set `path_out` to the base directory that
396 * should be used if the contents of `file_path` are a relative path.
398 static int read_gitfile(git_buf
*path_out
, const char *file_path
)
401 git_buf file
= GIT_BUF_INIT
;
402 size_t prefix_len
= strlen(GIT_FILE_CONTENT_PREFIX
);
404 assert(path_out
&& file_path
);
406 if (git_futils_readbuffer(&file
, file_path
) < 0)
409 git_buf_rtrim(&file
);
410 /* apparently on Windows, some people use backslashes in paths */
411 git_path_mkposix(file
.ptr
);
413 if (git_buf_len(&file
) <= prefix_len
||
414 memcmp(git_buf_cstr(&file
), GIT_FILE_CONTENT_PREFIX
, prefix_len
) != 0)
416 git_error_set(GIT_ERROR_REPOSITORY
,
417 "the `.git` file at '%s' is malformed", file_path
);
420 else if ((error
= git_path_dirname_r(path_out
, file_path
)) >= 0) {
421 const char *gitlink
= git_buf_cstr(&file
) + prefix_len
;
422 while (*gitlink
&& git__isspace(*gitlink
)) gitlink
++;
424 error
= git_path_prettify_dir(
425 path_out
, gitlink
, git_buf_cstr(path_out
));
428 git_buf_dispose(&file
);
432 static int find_repo(
433 git_buf
*gitdir_path
,
434 git_buf
*workdir_path
,
435 git_buf
*gitlink_path
,
436 git_buf
*commondir_path
,
437 const char *start_path
,
439 const char *ceiling_dirs
)
442 git_buf path
= GIT_BUF_INIT
;
443 git_buf repo_link
= GIT_BUF_INIT
;
444 git_buf common_link
= GIT_BUF_INIT
;
446 dev_t initial_device
= 0;
449 size_t ceiling_offset
= 0;
451 git_buf_clear(gitdir_path
);
453 error
= git_path_prettify(&path
, start_path
, NULL
);
457 /* in_dot_git toggles each loop:
458 * /a/b/c/.git, /a/b/c, /a/b/.git, /a/b, /a/.git, /a
459 * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, we
460 * assume we started with /a/b/c.git and don't append .git the first
462 * min_iterations indicates the number of iterations left before going
463 * further counts as a search. */
464 if (flags
& (GIT_REPOSITORY_OPEN_BARE
| GIT_REPOSITORY_OPEN_NO_DOTGIT
)) {
473 if (!(flags
& GIT_REPOSITORY_OPEN_NO_DOTGIT
)) {
475 error
= git_buf_joinpath(&path
, path
.ptr
, DOT_GIT
);
479 in_dot_git
= !in_dot_git
;
482 if (p_stat(path
.ptr
, &st
) == 0) {
483 /* check that we have not crossed device boundaries */
484 if (initial_device
== 0)
485 initial_device
= st
.st_dev
;
486 else if (st
.st_dev
!= initial_device
&&
487 !(flags
& GIT_REPOSITORY_OPEN_CROSS_FS
))
490 if (S_ISDIR(st
.st_mode
)) {
491 if (valid_repository_path(&path
, &common_link
)) {
492 git_path_to_dir(&path
);
493 git_buf_set(gitdir_path
, path
.ptr
, path
.size
);
496 git_buf_attach(gitlink_path
,
497 git_worktree__read_link(path
.ptr
, GIT_GITDIR_FILE
), 0);
499 git_buf_swap(&common_link
, commondir_path
);
504 else if (S_ISREG(st
.st_mode
) && git__suffixcmp(path
.ptr
, "/" DOT_GIT
) == 0) {
505 error
= read_gitfile(&repo_link
, path
.ptr
);
508 if (valid_repository_path(&repo_link
, &common_link
)) {
509 git_buf_swap(gitdir_path
, &repo_link
);
512 error
= git_buf_put(gitlink_path
, path
.ptr
, path
.size
);
514 git_buf_swap(&common_link
, commondir_path
);
520 /* Move up one directory. If we're in_dot_git, we'll search the
521 * parent itself next. If we're !in_dot_git, we'll search .git
522 * in the parent directory next (added at the top of the loop). */
523 if (git_path_dirname_r(&path
, path
.ptr
) < 0) {
528 /* Once we've checked the directory (and .git if applicable),
529 * find the ceiling for a search. */
530 if (min_iterations
&& (--min_iterations
== 0))
531 ceiling_offset
= find_ceiling_dir_offset(path
.ptr
, ceiling_dirs
);
533 /* Check if we should stop searching here. */
534 if (min_iterations
== 0
535 && (path
.ptr
[ceiling_offset
] == 0
536 || (flags
& GIT_REPOSITORY_OPEN_NO_SEARCH
)))
540 if (!error
&& workdir_path
&& !(flags
& GIT_REPOSITORY_OPEN_BARE
)) {
541 if (!git_buf_len(gitdir_path
))
542 git_buf_clear(workdir_path
);
544 git_path_dirname_r(workdir_path
, path
.ptr
);
545 git_path_to_dir(workdir_path
);
547 if (git_buf_oom(workdir_path
))
551 /* If we didn't find the repository, and we don't have any other error
552 * to report, report that. */
553 if (!git_buf_len(gitdir_path
) && !error
) {
554 git_error_set(GIT_ERROR_REPOSITORY
,
555 "could not find repository from '%s'", start_path
);
556 error
= GIT_ENOTFOUND
;
559 git_buf_dispose(&path
);
560 git_buf_dispose(&repo_link
);
561 git_buf_dispose(&common_link
);
565 int git_repository_open_bare(
566 git_repository
**repo_ptr
,
567 const char *bare_path
)
570 git_buf path
= GIT_BUF_INIT
, common_path
= GIT_BUF_INIT
;
571 git_repository
*repo
= NULL
;
573 if ((error
= git_path_prettify_dir(&path
, bare_path
, NULL
)) < 0)
576 if (!valid_repository_path(&path
, &common_path
)) {
577 git_buf_dispose(&path
);
578 git_buf_dispose(&common_path
);
579 git_error_set(GIT_ERROR_REPOSITORY
, "path is not a repository: %s", bare_path
);
580 return GIT_ENOTFOUND
;
583 repo
= repository_alloc();
584 GIT_ERROR_CHECK_ALLOC(repo
);
586 repo
->gitdir
= git_buf_detach(&path
);
587 GIT_ERROR_CHECK_ALLOC(repo
->gitdir
);
588 repo
->commondir
= git_buf_detach(&common_path
);
589 GIT_ERROR_CHECK_ALLOC(repo
->commondir
);
591 /* of course we're bare! */
593 repo
->is_worktree
= 0;
594 repo
->workdir
= NULL
;
600 static int _git_repository_open_ext_from_env(
601 git_repository
**out
,
602 const char *start_path
)
604 git_repository
*repo
= NULL
;
605 git_index
*index
= NULL
;
607 git_buf dir_buf
= GIT_BUF_INIT
;
608 git_buf ceiling_dirs_buf
= GIT_BUF_INIT
;
609 git_buf across_fs_buf
= GIT_BUF_INIT
;
610 git_buf index_file_buf
= GIT_BUF_INIT
;
611 git_buf namespace_buf
= GIT_BUF_INIT
;
612 git_buf object_dir_buf
= GIT_BUF_INIT
;
613 git_buf alts_buf
= GIT_BUF_INIT
;
614 git_buf work_tree_buf
= GIT_BUF_INIT
;
615 git_buf common_dir_buf
= GIT_BUF_INIT
;
616 const char *ceiling_dirs
= NULL
;
621 error
= git__getenv(&dir_buf
, "GIT_DIR");
622 if (error
== GIT_ENOTFOUND
) {
625 } else if (error
< 0)
628 start_path
= git_buf_cstr(&dir_buf
);
629 flags
|= GIT_REPOSITORY_OPEN_NO_SEARCH
;
630 flags
|= GIT_REPOSITORY_OPEN_NO_DOTGIT
;
634 error
= git__getenv(&ceiling_dirs_buf
, "GIT_CEILING_DIRECTORIES");
635 if (error
== GIT_ENOTFOUND
)
640 ceiling_dirs
= git_buf_cstr(&ceiling_dirs_buf
);
642 error
= git__getenv(&across_fs_buf
, "GIT_DISCOVERY_ACROSS_FILESYSTEM");
643 if (error
== GIT_ENOTFOUND
)
649 error
= git_config_parse_bool(&across_fs
, git_buf_cstr(&across_fs_buf
));
653 flags
|= GIT_REPOSITORY_OPEN_CROSS_FS
;
656 error
= git__getenv(&index_file_buf
, "GIT_INDEX_FILE");
657 if (error
== GIT_ENOTFOUND
)
662 error
= git_index_open(&index
, git_buf_cstr(&index_file_buf
));
667 error
= git__getenv(&namespace_buf
, "GIT_NAMESPACE");
668 if (error
== GIT_ENOTFOUND
)
673 error
= git__getenv(&object_dir_buf
, "GIT_OBJECT_DIRECTORY");
674 if (error
== GIT_ENOTFOUND
)
679 error
= git_odb_open(&odb
, git_buf_cstr(&object_dir_buf
));
684 error
= git__getenv(&work_tree_buf
, "GIT_WORK_TREE");
685 if (error
== GIT_ENOTFOUND
)
690 git_error_set(GIT_ERROR_INVALID
, "GIT_WORK_TREE unimplemented");
695 error
= git__getenv(&work_tree_buf
, "GIT_COMMON_DIR");
696 if (error
== GIT_ENOTFOUND
)
701 git_error_set(GIT_ERROR_INVALID
, "GIT_COMMON_DIR unimplemented");
706 error
= git_repository_open_ext(&repo
, start_path
, flags
, ceiling_dirs
);
711 git_repository_set_odb(repo
, odb
);
713 error
= git__getenv(&alts_buf
, "GIT_ALTERNATE_OBJECT_DIRECTORIES");
714 if (error
== GIT_ENOTFOUND
) {
717 } else if (error
< 0)
723 error
= git_repository_odb(&odb
, repo
);
728 end
= git_buf_cstr(&alts_buf
) + git_buf_len(&alts_buf
);
729 for (sep
= alt
= alts_buf
.ptr
; sep
!= end
; alt
= sep
+1) {
730 for (sep
= alt
; *sep
&& *sep
!= GIT_PATH_LIST_SEPARATOR
; sep
++)
734 error
= git_odb_add_disk_alternate(odb
, alt
);
740 if (git_buf_len(&namespace_buf
)) {
741 error
= git_repository_set_namespace(repo
, git_buf_cstr(&namespace_buf
));
746 git_repository_set_index(repo
, index
);
753 git_repository_free(repo
);
756 git_index_free(index
);
757 git_buf_dispose(&common_dir_buf
);
758 git_buf_dispose(&work_tree_buf
);
759 git_buf_dispose(&alts_buf
);
760 git_buf_dispose(&object_dir_buf
);
761 git_buf_dispose(&namespace_buf
);
762 git_buf_dispose(&index_file_buf
);
763 git_buf_dispose(&across_fs_buf
);
764 git_buf_dispose(&ceiling_dirs_buf
);
765 git_buf_dispose(&dir_buf
);
769 static int repo_is_worktree(unsigned *out
, const git_repository
*repo
)
771 git_buf gitdir_link
= GIT_BUF_INIT
;
774 /* Worktrees cannot have the same commondir and gitdir */
775 if (repo
->commondir
&& repo
->gitdir
776 && !strcmp(repo
->commondir
, repo
->gitdir
)) {
781 if ((error
= git_buf_joinpath(&gitdir_link
, repo
->gitdir
, "gitdir")) < 0)
784 /* A 'gitdir' file inside a git directory is currently
785 * only used when the repository is a working tree. */
786 *out
= !!git_path_exists(gitdir_link
.ptr
);
788 git_buf_dispose(&gitdir_link
);
792 int git_repository_open_ext(
793 git_repository
**repo_ptr
,
794 const char *start_path
,
796 const char *ceiling_dirs
)
799 unsigned is_worktree
;
800 git_buf gitdir
= GIT_BUF_INIT
, workdir
= GIT_BUF_INIT
,
801 gitlink
= GIT_BUF_INIT
, commondir
= GIT_BUF_INIT
;
802 git_repository
*repo
;
803 git_config
*config
= NULL
;
805 if (flags
& GIT_REPOSITORY_OPEN_FROM_ENV
)
806 return _git_repository_open_ext_from_env(repo_ptr
, start_path
);
812 &gitdir
, &workdir
, &gitlink
, &commondir
, start_path
, flags
, ceiling_dirs
);
814 if (error
< 0 || !repo_ptr
)
817 repo
= repository_alloc();
818 GIT_ERROR_CHECK_ALLOC(repo
);
820 repo
->gitdir
= git_buf_detach(&gitdir
);
821 GIT_ERROR_CHECK_ALLOC(repo
->gitdir
);
824 repo
->gitlink
= git_buf_detach(&gitlink
);
825 GIT_ERROR_CHECK_ALLOC(repo
->gitlink
);
827 if (commondir
.size
) {
828 repo
->commondir
= git_buf_detach(&commondir
);
829 GIT_ERROR_CHECK_ALLOC(repo
->commondir
);
832 if ((error
= repo_is_worktree(&is_worktree
, repo
)) < 0)
834 repo
->is_worktree
= is_worktree
;
837 * We'd like to have the config, but git doesn't particularly
838 * care if it's not there, so we need to deal with that.
841 error
= git_repository_config_snapshot(&config
, repo
);
842 if (error
< 0 && error
!= GIT_ENOTFOUND
)
845 if (config
&& (error
= check_repositoryformatversion(config
)) < 0)
848 if ((flags
& GIT_REPOSITORY_OPEN_BARE
) != 0)
853 ((error
= load_config_data(repo
, config
)) < 0 ||
854 (error
= load_workdir(repo
, config
, &workdir
)) < 0))
859 git_buf_dispose(&gitdir
);
860 git_buf_dispose(&workdir
);
861 git_config_free(config
);
864 git_repository_free(repo
);
871 int git_repository_open(git_repository
**repo_out
, const char *path
)
873 return git_repository_open_ext(
874 repo_out
, path
, GIT_REPOSITORY_OPEN_NO_SEARCH
, NULL
);
877 int git_repository_open_from_worktree(git_repository
**repo_out
, git_worktree
*wt
)
879 git_buf path
= GIT_BUF_INIT
;
880 git_repository
*repo
= NULL
;
883 assert(repo_out
&& wt
);
886 len
= strlen(wt
->gitlink_path
);
888 if (len
<= 4 || strcasecmp(wt
->gitlink_path
+ len
- 4, ".git")) {
893 if ((err
= git_buf_set(&path
, wt
->gitlink_path
, len
- 4)) < 0)
896 if ((err
= git_repository_open(&repo
, path
.ptr
)) < 0)
902 git_buf_dispose(&path
);
907 int git_repository_wrap_odb(git_repository
**repo_out
, git_odb
*odb
)
909 git_repository
*repo
;
911 repo
= repository_alloc();
912 GIT_ERROR_CHECK_ALLOC(repo
);
914 git_repository_set_odb(repo
, odb
);
920 int git_repository_discover(
922 const char *start_path
,
924 const char *ceiling_dirs
)
926 uint32_t flags
= across_fs
? GIT_REPOSITORY_OPEN_CROSS_FS
: 0;
930 git_buf_sanitize(out
);
932 return find_repo(out
, NULL
, NULL
, NULL
, start_path
, flags
, ceiling_dirs
);
935 static int load_config(
937 git_repository
*repo
,
938 const char *global_config_path
,
939 const char *xdg_config_path
,
940 const char *system_config_path
,
941 const char *programdata_path
)
944 git_buf config_path
= GIT_BUF_INIT
;
945 git_config
*cfg
= NULL
;
949 if ((error
= git_config_new(&cfg
)) < 0)
953 if ((error
= git_repository_item_path(&config_path
, repo
, GIT_REPOSITORY_ITEM_CONFIG
)) == 0)
954 error
= git_config_add_file_ondisk(cfg
, config_path
.ptr
, GIT_CONFIG_LEVEL_LOCAL
, repo
, 0);
956 if (error
&& error
!= GIT_ENOTFOUND
)
959 git_buf_dispose(&config_path
);
962 if (global_config_path
!= NULL
&&
963 (error
= git_config_add_file_ondisk(
964 cfg
, global_config_path
, GIT_CONFIG_LEVEL_GLOBAL
, repo
, 0)) < 0 &&
965 error
!= GIT_ENOTFOUND
)
968 if (xdg_config_path
!= NULL
&&
969 (error
= git_config_add_file_ondisk(
970 cfg
, xdg_config_path
, GIT_CONFIG_LEVEL_XDG
, repo
, 0)) < 0 &&
971 error
!= GIT_ENOTFOUND
)
974 if (system_config_path
!= NULL
&&
975 (error
= git_config_add_file_ondisk(
976 cfg
, system_config_path
, GIT_CONFIG_LEVEL_SYSTEM
, repo
, 0)) < 0 &&
977 error
!= GIT_ENOTFOUND
)
980 if (programdata_path
!= NULL
&&
981 (error
= git_config_add_file_ondisk(
982 cfg
, programdata_path
, GIT_CONFIG_LEVEL_PROGRAMDATA
, repo
, 0)) < 0 &&
983 error
!= GIT_ENOTFOUND
)
986 git_error_clear(); /* clear any lingering ENOTFOUND errors */
992 git_buf_dispose(&config_path
);
993 git_config_free(cfg
);
998 static const char *path_unless_empty(git_buf
*buf
)
1000 return git_buf_len(buf
) > 0 ? git_buf_cstr(buf
) : NULL
;
1003 int git_repository_config__weakptr(git_config
**out
, git_repository
*repo
)
1007 if (repo
->_config
== NULL
) {
1008 git_buf global_buf
= GIT_BUF_INIT
;
1009 git_buf xdg_buf
= GIT_BUF_INIT
;
1010 git_buf system_buf
= GIT_BUF_INIT
;
1011 git_buf programdata_buf
= GIT_BUF_INIT
;
1014 git_config_find_global(&global_buf
);
1015 git_config_find_xdg(&xdg_buf
);
1016 git_config_find_system(&system_buf
);
1017 git_config_find_programdata(&programdata_buf
);
1019 /* If there is no global file, open a backend for it anyway */
1020 if (git_buf_len(&global_buf
) == 0)
1021 git_config__global_location(&global_buf
);
1023 error
= load_config(
1025 path_unless_empty(&global_buf
),
1026 path_unless_empty(&xdg_buf
),
1027 path_unless_empty(&system_buf
),
1028 path_unless_empty(&programdata_buf
));
1030 GIT_REFCOUNT_OWN(config
, repo
);
1032 config
= git__compare_and_swap(&repo
->_config
, NULL
, config
);
1033 if (config
!= NULL
) {
1034 GIT_REFCOUNT_OWN(config
, NULL
);
1035 git_config_free(config
);
1039 git_buf_dispose(&global_buf
);
1040 git_buf_dispose(&xdg_buf
);
1041 git_buf_dispose(&system_buf
);
1042 git_buf_dispose(&programdata_buf
);
1045 *out
= repo
->_config
;
1049 int git_repository_config(git_config
**out
, git_repository
*repo
)
1051 if (git_repository_config__weakptr(out
, repo
) < 0)
1054 GIT_REFCOUNT_INC(*out
);
1058 int git_repository_config_snapshot(git_config
**out
, git_repository
*repo
)
1063 if ((error
= git_repository_config__weakptr(&weak
, repo
)) < 0)
1066 return git_config_snapshot(out
, weak
);
1069 void git_repository_set_config(git_repository
*repo
, git_config
*config
)
1071 assert(repo
&& config
);
1072 set_config(repo
, config
);
1075 int git_repository_odb__weakptr(git_odb
**out
, git_repository
*repo
)
1079 assert(repo
&& out
);
1081 if (repo
->_odb
== NULL
) {
1082 git_buf odb_path
= GIT_BUF_INIT
;
1085 if ((error
= git_repository_item_path(&odb_path
, repo
,
1086 GIT_REPOSITORY_ITEM_OBJECTS
)) < 0 ||
1087 (error
= git_odb_new(&odb
)) < 0)
1090 GIT_REFCOUNT_OWN(odb
, repo
);
1092 if ((error
= git_odb__set_caps(odb
, GIT_ODB_CAP_FROM_OWNER
)) < 0 ||
1093 (error
= git_odb__add_default_backends(odb
, odb_path
.ptr
, 0, 0)) < 0) {
1098 odb
= git__compare_and_swap(&repo
->_odb
, NULL
, odb
);
1100 GIT_REFCOUNT_OWN(odb
, NULL
);
1104 git_buf_dispose(&odb_path
);
1111 int git_repository_odb(git_odb
**out
, git_repository
*repo
)
1113 if (git_repository_odb__weakptr(out
, repo
) < 0)
1116 GIT_REFCOUNT_INC(*out
);
1120 void git_repository_set_odb(git_repository
*repo
, git_odb
*odb
)
1122 assert(repo
&& odb
);
1126 int git_repository_refdb__weakptr(git_refdb
**out
, git_repository
*repo
)
1130 assert(out
&& repo
);
1132 if (repo
->_refdb
== NULL
) {
1135 error
= git_refdb_open(&refdb
, repo
);
1137 GIT_REFCOUNT_OWN(refdb
, repo
);
1139 refdb
= git__compare_and_swap(&repo
->_refdb
, NULL
, refdb
);
1140 if (refdb
!= NULL
) {
1141 GIT_REFCOUNT_OWN(refdb
, NULL
);
1142 git_refdb_free(refdb
);
1147 *out
= repo
->_refdb
;
1151 int git_repository_refdb(git_refdb
**out
, git_repository
*repo
)
1153 if (git_repository_refdb__weakptr(out
, repo
) < 0)
1156 GIT_REFCOUNT_INC(*out
);
1160 void git_repository_set_refdb(git_repository
*repo
, git_refdb
*refdb
)
1162 assert(repo
&& refdb
);
1163 set_refdb(repo
, refdb
);
1166 int git_repository_index__weakptr(git_index
**out
, git_repository
*repo
)
1170 assert(out
&& repo
);
1172 if (repo
->_index
== NULL
) {
1173 git_buf index_path
= GIT_BUF_INIT
;
1176 if ((error
= git_buf_joinpath(&index_path
, repo
->gitdir
, GIT_INDEX_FILE
)) < 0)
1179 error
= git_index_open(&index
, index_path
.ptr
);
1181 GIT_REFCOUNT_OWN(index
, repo
);
1183 index
= git__compare_and_swap(&repo
->_index
, NULL
, index
);
1184 if (index
!= NULL
) {
1185 GIT_REFCOUNT_OWN(index
, NULL
);
1186 git_index_free(index
);
1189 error
= git_index_set_caps(repo
->_index
,
1190 GIT_INDEX_CAPABILITY_FROM_OWNER
);
1193 git_buf_dispose(&index_path
);
1196 *out
= repo
->_index
;
1200 int git_repository_index(git_index
**out
, git_repository
*repo
)
1202 if (git_repository_index__weakptr(out
, repo
) < 0)
1205 GIT_REFCOUNT_INC(*out
);
1209 void git_repository_set_index(git_repository
*repo
, git_index
*index
)
1212 set_index(repo
, index
);
1215 int git_repository_set_namespace(git_repository
*repo
, const char *namespace)
1217 git__free(repo
->namespace);
1219 if (namespace == NULL
) {
1220 repo
->namespace = NULL
;
1224 return (repo
->namespace = git__strdup(namespace)) ? 0 : -1;
1227 const char *git_repository_get_namespace(git_repository
*repo
)
1229 return repo
->namespace;
1233 static int reserved_names_add8dot3(git_repository
*repo
, const char *path
)
1235 char *name
= git_win32_path_8dot3_name(path
);
1236 const char *def
= GIT_DIR_SHORTNAME
;
1237 const char *def_dot_git
= DOT_GIT
;
1238 size_t name_len
, def_len
= CONST_STRLEN(GIT_DIR_SHORTNAME
);
1239 size_t def_dot_git_len
= CONST_STRLEN(DOT_GIT
);
1245 name_len
= strlen(name
);
1247 if ((name_len
== def_len
&& memcmp(name
, def
, def_len
) == 0) ||
1248 (name_len
== def_dot_git_len
&& memcmp(name
, def_dot_git
, def_dot_git_len
) == 0)) {
1253 if ((buf
= git_array_alloc(repo
->reserved_names
)) == NULL
)
1256 git_buf_attach(buf
, name
, name_len
);
1260 bool git_repository__reserved_names(
1261 git_buf
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
)
1263 GIT_UNUSED(include_ntfs
);
1265 if (repo
->reserved_names
.size
== 0) {
1269 /* Add the static defaults */
1270 for (i
= 0; i
< git_repository__reserved_names_win32_len
; i
++) {
1271 if ((buf
= git_array_alloc(repo
->reserved_names
)) == NULL
)
1274 buf
->ptr
= git_repository__reserved_names_win32
[i
].ptr
;
1275 buf
->size
= git_repository__reserved_names_win32
[i
].size
;
1278 /* Try to add any repo-specific reserved names - the gitlink file
1279 * within a submodule or the repository (if the repository directory
1280 * is beneath the workdir). These are typically `.git`, but should
1281 * be protected in case they are not. Note, repo and workdir paths
1282 * are always prettified to end in `/`, so a prefixcmp is safe.
1284 if (!repo
->is_bare
) {
1285 int (*prefixcmp
)(const char *, const char *);
1286 int error
, ignorecase
;
1288 error
= git_repository__cvar(
1289 &ignorecase
, repo
, GIT_CVAR_IGNORECASE
);
1290 prefixcmp
= (error
|| ignorecase
) ? git__prefixcmp_icase
:
1293 if (repo
->gitlink
&&
1294 reserved_names_add8dot3(repo
, repo
->gitlink
) < 0)
1298 prefixcmp(repo
->gitdir
, repo
->workdir
) == 0 &&
1299 reserved_names_add8dot3(repo
, repo
->gitdir
) < 0)
1304 *out
= repo
->reserved_names
.ptr
;
1305 *outlen
= repo
->reserved_names
.size
;
1309 /* Always give good defaults, even on OOM */
1311 *out
= git_repository__reserved_names_win32
;
1312 *outlen
= git_repository__reserved_names_win32_len
;
1317 bool git_repository__reserved_names(
1318 git_buf
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
)
1323 *out
= git_repository__reserved_names_win32
;
1324 *outlen
= git_repository__reserved_names_win32_len
;
1326 *out
= git_repository__reserved_names_posix
;
1327 *outlen
= git_repository__reserved_names_posix_len
;
1334 static int check_repositoryformatversion(git_config
*config
)
1338 error
= git_config_get_int32(&version
, config
, "core.repositoryformatversion");
1339 /* git ignores this if the config variable isn't there */
1340 if (error
== GIT_ENOTFOUND
)
1346 if (GIT_REPO_VERSION
< version
) {
1347 git_error_set(GIT_ERROR_REPOSITORY
,
1348 "unsupported repository version %d. Only versions up to %d are supported.",
1349 version
, GIT_REPO_VERSION
);
1356 int git_repository_create_head(const char *git_dir
, const char *ref_name
)
1358 git_buf ref_path
= GIT_BUF_INIT
;
1359 git_filebuf ref
= GIT_FILEBUF_INIT
;
1362 if (git_buf_joinpath(&ref_path
, git_dir
, GIT_HEAD_FILE
) < 0 ||
1363 git_filebuf_open(&ref
, ref_path
.ptr
, 0, GIT_REFS_FILE_MODE
) < 0)
1367 ref_name
= GIT_BRANCH_MASTER
;
1369 if (git__prefixcmp(ref_name
, GIT_REFS_DIR
) == 0)
1372 fmt
= "ref: " GIT_REFS_HEADS_DIR
"%s\n";
1374 if (git_filebuf_printf(&ref
, fmt
, ref_name
) < 0 ||
1375 git_filebuf_commit(&ref
) < 0)
1378 git_buf_dispose(&ref_path
);
1382 git_buf_dispose(&ref_path
);
1383 git_filebuf_cleanup(&ref
);
1387 static bool is_chmod_supported(const char *file_path
)
1389 struct stat st1
, st2
;
1391 if (p_stat(file_path
, &st1
) < 0)
1394 if (p_chmod(file_path
, st1
.st_mode
^ S_IXUSR
) < 0)
1397 if (p_stat(file_path
, &st2
) < 0)
1400 return (st1
.st_mode
!= st2
.st_mode
);
1403 static bool is_filesystem_case_insensitive(const char *gitdir_path
)
1405 git_buf path
= GIT_BUF_INIT
;
1406 int is_insensitive
= -1;
1408 if (!git_buf_joinpath(&path
, gitdir_path
, "CoNfIg"))
1409 is_insensitive
= git_path_exists(git_buf_cstr(&path
));
1411 git_buf_dispose(&path
);
1412 return is_insensitive
;
1415 static bool are_symlinks_supported(const char *wd_path
)
1417 git_config
*config
= NULL
;
1418 git_buf global_buf
= GIT_BUF_INIT
;
1419 git_buf xdg_buf
= GIT_BUF_INIT
;
1420 git_buf system_buf
= GIT_BUF_INIT
;
1421 git_buf programdata_buf
= GIT_BUF_INIT
;
1422 git_buf path
= GIT_BUF_INIT
;
1428 * To emulate Git for Windows, symlinks on Windows must be explicitly
1429 * opted-in. We examine the system configuration for a core.symlinks
1430 * set to true. If found, we then examine the filesystem to see if
1431 * symlinks are _actually_ supported by the current user. If that is
1432 * _not_ set, then we do not test or enable symlink support.
1435 git_config_find_global(&global_buf
);
1436 git_config_find_xdg(&xdg_buf
);
1437 git_config_find_system(&system_buf
);
1438 git_config_find_programdata(&programdata_buf
);
1440 if (load_config(&config
, NULL
,
1441 path_unless_empty(&global_buf
),
1442 path_unless_empty(&xdg_buf
),
1443 path_unless_empty(&system_buf
),
1444 path_unless_empty(&programdata_buf
)) < 0)
1447 if (git_config_get_bool(&symlinks
, config
, "core.symlinks") < 0 || !symlinks
)
1451 if ((fd
= git_futils_mktmp(&path
, wd_path
, 0666)) < 0 ||
1453 p_unlink(path
.ptr
) < 0 ||
1454 p_symlink("testing", path
.ptr
) < 0 ||
1455 p_lstat(path
.ptr
, &st
) < 0)
1458 symlinks
= (S_ISLNK(st
.st_mode
) != 0);
1460 (void)p_unlink(path
.ptr
);
1463 git_buf_dispose(&global_buf
);
1464 git_buf_dispose(&xdg_buf
);
1465 git_buf_dispose(&system_buf
);
1466 git_buf_dispose(&programdata_buf
);
1467 git_buf_dispose(&path
);
1468 git_config_free(config
);
1469 return symlinks
!= 0;
1472 static int create_empty_file(const char *path
, mode_t mode
)
1476 if ((fd
= p_creat(path
, mode
)) < 0) {
1477 git_error_set(GIT_ERROR_OS
, "error while creating '%s'", path
);
1481 if (p_close(fd
) < 0) {
1482 git_error_set(GIT_ERROR_OS
, "error while closing '%s'", path
);
1489 static int repo_local_config(
1491 git_buf
*config_dir
,
1492 git_repository
*repo
,
1493 const char *repo_dir
)
1497 const char *cfg_path
;
1499 if (git_buf_joinpath(config_dir
, repo_dir
, GIT_CONFIG_FILENAME_INREPO
) < 0)
1501 cfg_path
= git_buf_cstr(config_dir
);
1503 /* make LOCAL config if missing */
1504 if (!git_path_isfile(cfg_path
) &&
1505 (error
= create_empty_file(cfg_path
, GIT_CONFIG_FILE_MODE
)) < 0)
1508 /* if no repo, just open that file directly */
1510 return git_config_open_ondisk(out
, cfg_path
);
1512 /* otherwise, open parent config and get that level */
1513 if ((error
= git_repository_config__weakptr(&parent
, repo
)) < 0)
1516 if (git_config_open_level(out
, parent
, GIT_CONFIG_LEVEL_LOCAL
) < 0) {
1519 if (!(error
= git_config_add_file_ondisk(
1520 parent
, cfg_path
, GIT_CONFIG_LEVEL_LOCAL
, repo
, false)))
1521 error
= git_config_open_level(out
, parent
, GIT_CONFIG_LEVEL_LOCAL
);
1524 git_config_free(parent
);
1529 static int repo_init_fs_configs(
1531 const char *cfg_path
,
1532 const char *repo_dir
,
1533 const char *work_dir
,
1534 bool update_ignorecase
)
1539 work_dir
= repo_dir
;
1541 if ((error
= git_config_set_bool(
1542 cfg
, "core.filemode", is_chmod_supported(cfg_path
))) < 0)
1545 if (!are_symlinks_supported(work_dir
)) {
1546 if ((error
= git_config_set_bool(cfg
, "core.symlinks", false)) < 0)
1548 } else if (git_config_delete_entry(cfg
, "core.symlinks") < 0)
1551 if (update_ignorecase
) {
1552 if (is_filesystem_case_insensitive(repo_dir
)) {
1553 if ((error
= git_config_set_bool(cfg
, "core.ignorecase", true)) < 0)
1555 } else if (git_config_delete_entry(cfg
, "core.ignorecase") < 0)
1559 #ifdef GIT_USE_ICONV
1560 if ((error
= git_config_set_bool(
1561 cfg
, "core.precomposeunicode",
1562 git_path_does_fs_decompose_unicode(work_dir
))) < 0)
1564 /* on non-iconv platforms, don't even set core.precomposeunicode */
1570 static int repo_init_config(
1571 const char *repo_dir
,
1572 const char *work_dir
,
1577 git_buf cfg_path
= GIT_BUF_INIT
, worktree_path
= GIT_BUF_INIT
;
1578 git_config
*config
= NULL
;
1579 bool is_bare
= ((flags
& GIT_REPOSITORY_INIT_BARE
) != 0);
1580 bool is_reinit
= ((flags
& GIT_REPOSITORY_INIT__IS_REINIT
) != 0);
1582 if ((error
= repo_local_config(&config
, &cfg_path
, NULL
, repo_dir
)) < 0)
1585 if (is_reinit
&& (error
= check_repositoryformatversion(config
)) < 0)
1588 #define SET_REPO_CONFIG(TYPE, NAME, VAL) do { \
1589 if ((error = git_config_set_##TYPE(config, NAME, VAL)) < 0) \
1590 goto cleanup; } while (0)
1592 SET_REPO_CONFIG(bool, "core.bare", is_bare
);
1593 SET_REPO_CONFIG(int32
, "core.repositoryformatversion", GIT_REPO_VERSION
);
1595 if ((error
= repo_init_fs_configs(
1596 config
, cfg_path
.ptr
, repo_dir
, work_dir
, !is_reinit
)) < 0)
1600 SET_REPO_CONFIG(bool, "core.logallrefupdates", true);
1602 if (!(flags
& GIT_REPOSITORY_INIT__NATURAL_WD
)) {
1603 if ((error
= git_buf_sets(&worktree_path
, work_dir
)) < 0)
1606 if ((flags
& GIT_REPOSITORY_INIT_RELATIVE_GITLINK
))
1607 if ((error
= git_path_make_relative(&worktree_path
, repo_dir
)) < 0)
1610 SET_REPO_CONFIG(string
, "core.worktree", worktree_path
.ptr
);
1611 } else if (is_reinit
) {
1612 if (git_config_delete_entry(config
, "core.worktree") < 0)
1617 if (mode
== GIT_REPOSITORY_INIT_SHARED_GROUP
) {
1618 SET_REPO_CONFIG(int32
, "core.sharedrepository", 1);
1619 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1621 else if (mode
== GIT_REPOSITORY_INIT_SHARED_ALL
) {
1622 SET_REPO_CONFIG(int32
, "core.sharedrepository", 2);
1623 SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true);
1627 git_buf_dispose(&cfg_path
);
1628 git_buf_dispose(&worktree_path
);
1629 git_config_free(config
);
1634 static int repo_reinit_submodule_fs(git_submodule
*sm
, const char *n
, void *p
)
1636 git_repository
*smrepo
= NULL
;
1637 GIT_UNUSED(n
); GIT_UNUSED(p
);
1639 if (git_submodule_open(&smrepo
, sm
) < 0 ||
1640 git_repository_reinit_filesystem(smrepo
, true) < 0)
1642 git_repository_free(smrepo
);
1647 int git_repository_reinit_filesystem(git_repository
*repo
, int recurse
)
1650 git_buf path
= GIT_BUF_INIT
;
1651 git_config
*config
= NULL
;
1652 const char *repo_dir
= git_repository_path(repo
);
1654 if (!(error
= repo_local_config(&config
, &path
, repo
, repo_dir
)))
1655 error
= repo_init_fs_configs(
1656 config
, path
.ptr
, repo_dir
, git_repository_workdir(repo
), true);
1658 git_config_free(config
);
1659 git_buf_dispose(&path
);
1661 git_repository__cvar_cache_clear(repo
);
1663 if (!repo
->is_bare
&& recurse
)
1664 (void)git_submodule_foreach(repo
, repo_reinit_submodule_fs
, NULL
);
1669 static int repo_write_template(
1670 const char *git_dir
,
1671 bool allow_overwrite
,
1675 const char *content
)
1677 git_buf path
= GIT_BUF_INIT
;
1678 int fd
, error
= 0, flags
;
1680 if (git_buf_joinpath(&path
, git_dir
, file
) < 0)
1683 if (allow_overwrite
)
1684 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
1686 flags
= O_WRONLY
| O_CREAT
| O_EXCL
;
1688 fd
= p_open(git_buf_cstr(&path
), flags
, mode
);
1691 error
= p_write(fd
, content
, strlen(content
));
1695 else if (errno
!= EEXIST
)
1699 if (!error
&& hidden
) {
1700 if (git_win32__set_hidden(path
.ptr
, true) < 0)
1707 git_buf_dispose(&path
);
1710 git_error_set(GIT_ERROR_OS
,
1711 "failed to initialize repository with template '%s'", file
);
1716 static int repo_write_gitlink(
1717 const char *in_dir
, const char *to_repo
, bool use_relative_path
)
1720 git_buf buf
= GIT_BUF_INIT
;
1721 git_buf path_to_repo
= GIT_BUF_INIT
;
1724 git_path_dirname_r(&buf
, to_repo
);
1725 git_path_to_dir(&buf
);
1726 if (git_buf_oom(&buf
))
1729 /* don't write gitlink to natural workdir */
1730 if (git__suffixcmp(to_repo
, "/" DOT_GIT
"/") == 0 &&
1731 strcmp(in_dir
, buf
.ptr
) == 0)
1733 error
= GIT_PASSTHROUGH
;
1737 if ((error
= git_buf_joinpath(&buf
, in_dir
, DOT_GIT
)) < 0)
1740 if (!p_stat(buf
.ptr
, &st
) && !S_ISREG(st
.st_mode
)) {
1741 git_error_set(GIT_ERROR_REPOSITORY
,
1742 "cannot overwrite gitlink file into path '%s'", in_dir
);
1743 error
= GIT_EEXISTS
;
1747 git_buf_clear(&buf
);
1749 error
= git_buf_sets(&path_to_repo
, to_repo
);
1751 if (!error
&& use_relative_path
)
1752 error
= git_path_make_relative(&path_to_repo
, in_dir
);
1755 error
= git_buf_join(&buf
, ' ', GIT_FILE_CONTENT_PREFIX
, path_to_repo
.ptr
);
1758 error
= repo_write_template(in_dir
, true, DOT_GIT
, 0666, true, buf
.ptr
);
1761 git_buf_dispose(&buf
);
1762 git_buf_dispose(&path_to_repo
);
1766 static mode_t
pick_dir_mode(git_repository_init_options
*opts
)
1768 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_UMASK
)
1770 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_GROUP
)
1771 return (0775 | S_ISGID
);
1772 if (opts
->mode
== GIT_REPOSITORY_INIT_SHARED_ALL
)
1773 return (0777 | S_ISGID
);
1777 #include "repo_template.h"
1779 static int repo_init_structure(
1780 const char *repo_dir
,
1781 const char *work_dir
,
1782 git_repository_init_options
*opts
)
1785 repo_template_item
*tpl
;
1787 ((opts
->flags
& GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE
) != 0);
1788 mode_t dmode
= pick_dir_mode(opts
);
1789 bool chmod
= opts
->mode
!= GIT_REPOSITORY_INIT_SHARED_UMASK
;
1791 /* Hide the ".git" directory */
1793 if ((opts
->flags
& GIT_REPOSITORY_INIT__HAS_DOTGIT
) != 0) {
1794 if (git_win32__set_hidden(repo_dir
, true) < 0) {
1795 git_error_set(GIT_ERROR_OS
,
1796 "failed to mark Git repository folder as hidden");
1802 /* Create the .git gitlink if appropriate */
1803 if ((opts
->flags
& GIT_REPOSITORY_INIT_BARE
) == 0 &&
1804 (opts
->flags
& GIT_REPOSITORY_INIT__NATURAL_WD
) == 0)
1806 if (repo_write_gitlink(work_dir
, repo_dir
, opts
->flags
& GIT_REPOSITORY_INIT_RELATIVE_GITLINK
) < 0)
1810 /* Copy external template if requested */
1812 git_config
*cfg
= NULL
;
1813 const char *tdir
= NULL
;
1814 bool default_template
= false;
1815 git_buf template_buf
= GIT_BUF_INIT
;
1817 if (opts
->template_path
)
1818 tdir
= opts
->template_path
;
1819 else if ((error
= git_config_open_default(&cfg
)) >= 0) {
1820 if (!git_config_get_path(&template_buf
, cfg
, "init.templatedir"))
1821 tdir
= template_buf
.ptr
;
1826 if (!(error
= git_sysdir_find_template_dir(&template_buf
)))
1827 tdir
= template_buf
.ptr
;
1828 default_template
= true;
1832 * If tdir was the empty string, treat it like tdir was a path to an
1833 * empty directory (so, don't do any copying). This is the behavior
1834 * that git(1) exhibits, although it doesn't seem to be officially
1837 if (tdir
&& git__strcmp(tdir
, "") != 0) {
1838 uint32_t cpflags
= GIT_CPDIR_COPY_SYMLINKS
|
1839 GIT_CPDIR_SIMPLE_TO_MODE
|
1840 GIT_CPDIR_COPY_DOTFILES
;
1841 if (opts
->mode
!= GIT_REPOSITORY_INIT_SHARED_UMASK
)
1842 cpflags
|= GIT_CPDIR_CHMOD_DIRS
;
1843 error
= git_futils_cp_r(tdir
, repo_dir
, cpflags
, dmode
);
1846 git_buf_dispose(&template_buf
);
1847 git_config_free(cfg
);
1850 if (!default_template
)
1853 /* if template was default, ignore error and use internal */
1855 external_tpl
= false;
1860 /* Copy internal template
1861 * - always ensure existence of dirs
1862 * - only create files if no external template was specified
1864 for (tpl
= repo_template
; !error
&& tpl
->path
; ++tpl
) {
1865 if (!tpl
->content
) {
1866 uint32_t mkdir_flags
= GIT_MKDIR_PATH
;
1868 mkdir_flags
|= GIT_MKDIR_CHMOD
;
1870 error
= git_futils_mkdir_relative(
1871 tpl
->path
, repo_dir
, dmode
, mkdir_flags
, NULL
);
1873 else if (!external_tpl
) {
1874 const char *content
= tpl
->content
;
1876 if (opts
->description
&& strcmp(tpl
->path
, GIT_DESC_FILE
) == 0)
1877 content
= opts
->description
;
1879 error
= repo_write_template(
1880 repo_dir
, false, tpl
->path
, tpl
->mode
, false, content
);
1887 static int mkdir_parent(git_buf
*buf
, uint32_t mode
, bool skip2
)
1889 /* When making parent directories during repository initialization
1890 * don't try to set gid or grant world write access
1892 return git_futils_mkdir(
1893 buf
->ptr
, mode
& ~(S_ISGID
| 0002),
1894 GIT_MKDIR_PATH
| GIT_MKDIR_VERIFY_DIR
|
1895 (skip2
? GIT_MKDIR_SKIP_LAST2
: GIT_MKDIR_SKIP_LAST
));
1898 static int repo_init_directories(
1901 const char *given_repo
,
1902 git_repository_init_options
*opts
)
1905 bool is_bare
, add_dotgit
, has_dotgit
, natural_wd
;
1908 /* There are three possible rules for what we are allowed to create:
1909 * - MKPATH means anything we need
1910 * - MKDIR means just the .git directory and its parent and the workdir
1911 * - Neither means only the .git directory can be created
1913 * There are 5 "segments" of path that we might need to deal with:
1914 * 1. The .git directory
1915 * 2. The parent of the .git directory
1916 * 3. Everything above the parent of the .git directory
1917 * 4. The working directory (often the same as #2)
1918 * 5. Everything above the working directory (often the same as #3)
1920 * For all directories created, we start with the init_mode value for
1921 * permissions and then strip off bits in some cases:
1923 * For MKPATH, we create #3 (and #5) paths without S_ISGID or S_IWOTH
1924 * For MKPATH and MKDIR, we create #2 (and #4) without S_ISGID
1925 * For all rules, we create #1 using the untouched init_mode
1928 /* set up repo path */
1930 is_bare
= ((opts
->flags
& GIT_REPOSITORY_INIT_BARE
) != 0);
1933 (opts
->flags
& GIT_REPOSITORY_INIT_NO_DOTGIT_DIR
) == 0 &&
1935 git__suffixcmp(given_repo
, "/" DOT_GIT
) != 0 &&
1936 git__suffixcmp(given_repo
, "/" GIT_DIR
) != 0;
1938 if (git_buf_joinpath(repo_path
, given_repo
, add_dotgit
? GIT_DIR
: "") < 0)
1941 has_dotgit
= (git__suffixcmp(repo_path
->ptr
, "/" GIT_DIR
) == 0);
1943 opts
->flags
|= GIT_REPOSITORY_INIT__HAS_DOTGIT
;
1945 /* set up workdir path */
1948 if (opts
->workdir_path
) {
1949 if (git_path_join_unrooted(
1950 wd_path
, opts
->workdir_path
, repo_path
->ptr
, NULL
) < 0)
1952 } else if (has_dotgit
) {
1953 if (git_path_dirname_r(wd_path
, repo_path
->ptr
) < 0)
1956 git_error_set(GIT_ERROR_REPOSITORY
, "cannot pick working directory"
1957 " for non-bare repository that isn't a '.git' directory");
1961 if (git_path_to_dir(wd_path
) < 0)
1964 git_buf_clear(wd_path
);
1969 wd_path
->size
> 0 &&
1970 wd_path
->size
+ strlen(GIT_DIR
) == repo_path
->size
&&
1971 memcmp(repo_path
->ptr
, wd_path
->ptr
, wd_path
->size
) == 0;
1973 opts
->flags
|= GIT_REPOSITORY_INIT__NATURAL_WD
;
1975 /* create directories as needed / requested */
1977 dirmode
= pick_dir_mode(opts
);
1979 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0) {
1980 /* create path #5 */
1981 if (wd_path
->size
> 0 &&
1982 (error
= mkdir_parent(wd_path
, dirmode
, false)) < 0)
1985 /* create path #3 (if not the same as #5) */
1987 (error
= mkdir_parent(repo_path
, dirmode
, has_dotgit
)) < 0)
1991 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKDIR
) != 0 ||
1992 (opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0)
1994 /* create path #4 */
1995 if (wd_path
->size
> 0 &&
1996 (error
= git_futils_mkdir(
1997 wd_path
->ptr
, dirmode
& ~S_ISGID
,
1998 GIT_MKDIR_VERIFY_DIR
)) < 0)
2001 /* create path #2 (if not the same as #4) */
2003 (error
= git_futils_mkdir(
2004 repo_path
->ptr
, dirmode
& ~S_ISGID
,
2005 GIT_MKDIR_VERIFY_DIR
| GIT_MKDIR_SKIP_LAST
)) < 0)
2009 if ((opts
->flags
& GIT_REPOSITORY_INIT_MKDIR
) != 0 ||
2010 (opts
->flags
& GIT_REPOSITORY_INIT_MKPATH
) != 0 ||
2013 /* create path #1 */
2014 error
= git_futils_mkdir(repo_path
->ptr
, dirmode
,
2015 GIT_MKDIR_VERIFY_DIR
| ((dirmode
& S_ISGID
) ? GIT_MKDIR_CHMOD
: 0));
2018 /* prettify both directories now that they are created */
2021 error
= git_path_prettify_dir(repo_path
, repo_path
->ptr
, NULL
);
2023 if (!error
&& wd_path
->size
> 0)
2024 error
= git_path_prettify_dir(wd_path
, wd_path
->ptr
, NULL
);
2030 static int repo_init_create_origin(git_repository
*repo
, const char *url
)
2035 if (!(error
= git_remote_create(&remote
, repo
, GIT_REMOTE_ORIGIN
, url
))) {
2036 git_remote_free(remote
);
2042 int git_repository_init(
2043 git_repository
**repo_out
, const char *path
, unsigned is_bare
)
2045 git_repository_init_options opts
= GIT_REPOSITORY_INIT_OPTIONS_INIT
;
2047 opts
.flags
= GIT_REPOSITORY_INIT_MKPATH
; /* don't love this default */
2049 opts
.flags
|= GIT_REPOSITORY_INIT_BARE
;
2051 return git_repository_init_ext(repo_out
, path
, &opts
);
2054 int git_repository_init_ext(
2055 git_repository
**out
,
2056 const char *given_repo
,
2057 git_repository_init_options
*opts
)
2060 git_buf repo_path
= GIT_BUF_INIT
, wd_path
= GIT_BUF_INIT
,
2061 common_path
= GIT_BUF_INIT
;
2064 assert(out
&& given_repo
&& opts
);
2066 GIT_ERROR_CHECK_VERSION(opts
, GIT_REPOSITORY_INIT_OPTIONS_VERSION
, "git_repository_init_options");
2068 error
= repo_init_directories(&repo_path
, &wd_path
, given_repo
, opts
);
2072 wd
= (opts
->flags
& GIT_REPOSITORY_INIT_BARE
) ? NULL
: git_buf_cstr(&wd_path
);
2073 if (valid_repository_path(&repo_path
, &common_path
)) {
2075 if ((opts
->flags
& GIT_REPOSITORY_INIT_NO_REINIT
) != 0) {
2076 git_error_set(GIT_ERROR_REPOSITORY
,
2077 "attempt to reinitialize '%s'", given_repo
);
2078 error
= GIT_EEXISTS
;
2082 opts
->flags
|= GIT_REPOSITORY_INIT__IS_REINIT
;
2084 error
= repo_init_config(
2085 repo_path
.ptr
, wd
, opts
->flags
, opts
->mode
);
2087 /* TODO: reinitialize the templates */
2090 if (!(error
= repo_init_structure(
2091 repo_path
.ptr
, wd
, opts
)) &&
2092 !(error
= repo_init_config(
2093 repo_path
.ptr
, wd
, opts
->flags
, opts
->mode
)))
2094 error
= git_repository_create_head(
2095 repo_path
.ptr
, opts
->initial_head
);
2100 error
= git_repository_open(out
, repo_path
.ptr
);
2102 if (!error
&& opts
->origin_url
)
2103 error
= repo_init_create_origin(*out
, opts
->origin_url
);
2106 git_buf_dispose(&common_path
);
2107 git_buf_dispose(&repo_path
);
2108 git_buf_dispose(&wd_path
);
2113 int git_repository_head_detached(git_repository
*repo
)
2116 git_odb
*odb
= NULL
;
2119 if (git_repository_odb__weakptr(&odb
, repo
) < 0)
2122 if (git_reference_lookup(&ref
, repo
, GIT_HEAD_FILE
) < 0)
2125 if (git_reference_type(ref
) == GIT_REFERENCE_SYMBOLIC
) {
2126 git_reference_free(ref
);
2130 exists
= git_odb_exists(odb
, git_reference_target(ref
));
2132 git_reference_free(ref
);
2136 static int get_worktree_file_path(git_buf
*out
, git_repository
*repo
, const char *worktree
, const char *file
)
2139 return git_buf_printf(out
, "%s/worktrees/%s/%s", repo
->commondir
, worktree
, file
);
2142 int git_repository_head_detached_for_worktree(git_repository
*repo
, const char *name
)
2144 git_reference
*ref
= NULL
;
2147 assert(repo
&& name
);
2149 if ((error
= git_repository_head_for_worktree(&ref
, repo
, name
)) < 0)
2152 error
= (git_reference_type(ref
) != GIT_REFERENCE_SYMBOLIC
);
2154 git_reference_free(ref
);
2159 int git_repository_head(git_reference
**head_out
, git_repository
*repo
)
2161 git_reference
*head
;
2166 if ((error
= git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
)) < 0)
2169 if (git_reference_type(head
) == GIT_REFERENCE_DIRECT
) {
2174 error
= git_reference_lookup_resolved(head_out
, repo
, git_reference_symbolic_target(head
), -1);
2175 git_reference_free(head
);
2177 return error
== GIT_ENOTFOUND
? GIT_EUNBORNBRANCH
: error
;
2180 int git_repository_head_for_worktree(git_reference
**out
, git_repository
*repo
, const char *name
)
2182 git_buf path
= GIT_BUF_INIT
;
2183 git_reference
*head
= NULL
;
2186 assert(out
&& repo
&& name
);
2190 if ((error
= get_worktree_file_path(&path
, repo
, name
, GIT_HEAD_FILE
)) < 0 ||
2191 (error
= git_reference__read_head(&head
, repo
, path
.ptr
)) < 0)
2194 if (git_reference_type(head
) != GIT_REFERENCE_DIRECT
) {
2195 git_reference
*resolved
;
2197 error
= git_reference_lookup_resolved(&resolved
, repo
, git_reference_symbolic_target(head
), -1);
2198 git_reference_free(head
);
2206 git_reference_free(head
);
2208 git_buf_dispose(&path
);
2213 int git_repository_foreach_head(git_repository
*repo
, git_repository_foreach_head_cb cb
, void *payload
)
2215 git_strarray worktrees
= GIT_VECTOR_INIT
;
2216 git_buf path
= GIT_BUF_INIT
;
2220 /* Execute callback for HEAD of commondir */
2221 if ((error
= git_buf_joinpath(&path
, repo
->commondir
, GIT_HEAD_FILE
)) < 0 ||
2222 (error
= cb(repo
, path
.ptr
, payload
) != 0))
2225 if ((error
= git_worktree_list(&worktrees
, repo
)) < 0) {
2230 /* Execute callback for all worktree HEADs */
2231 for (i
= 0; i
< worktrees
.count
; i
++) {
2232 if (get_worktree_file_path(&path
, repo
, worktrees
.strings
[i
], GIT_HEAD_FILE
) < 0)
2235 if ((error
= cb(repo
, path
.ptr
, payload
)) != 0)
2240 git_buf_dispose(&path
);
2241 git_strarray_free(&worktrees
);
2245 int git_repository_head_unborn(git_repository
*repo
)
2247 git_reference
*ref
= NULL
;
2250 error
= git_repository_head(&ref
, repo
);
2251 git_reference_free(ref
);
2253 if (error
== GIT_EUNBORNBRANCH
) {
2264 static int at_least_one_cb(const char *refname
, void *payload
)
2266 GIT_UNUSED(refname
);
2267 GIT_UNUSED(payload
);
2268 return GIT_PASSTHROUGH
;
2271 static int repo_contains_no_reference(git_repository
*repo
)
2273 int error
= git_reference_foreach_name(repo
, &at_least_one_cb
, NULL
);
2275 if (error
== GIT_PASSTHROUGH
)
2284 int git_repository_is_empty(git_repository
*repo
)
2286 git_reference
*head
= NULL
;
2289 if (git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
) < 0)
2292 if (git_reference_type(head
) == GIT_REFERENCE_SYMBOLIC
)
2294 (strcmp(git_reference_symbolic_target(head
),
2295 GIT_REFS_HEADS_DIR
"master") == 0) &&
2296 repo_contains_no_reference(repo
);
2298 git_reference_free(head
);
2303 int git_repository_item_path(git_buf
*out
, const git_repository
*repo
, git_repository_item_t item
)
2307 switch (items
[item
].parent
) {
2308 case GIT_REPOSITORY_ITEM_GITDIR
:
2309 parent
= git_repository_path(repo
);
2311 case GIT_REPOSITORY_ITEM_WORKDIR
:
2312 parent
= git_repository_workdir(repo
);
2314 case GIT_REPOSITORY_ITEM_COMMONDIR
:
2315 parent
= git_repository_commondir(repo
);
2318 git_error_set(GIT_ERROR_INVALID
, "invalid item directory");
2322 if (parent
== NULL
) {
2323 git_error_set(GIT_ERROR_INVALID
, "path cannot exist in repository");
2324 return GIT_ENOTFOUND
;
2327 if (git_buf_sets(out
, parent
) < 0)
2330 if (items
[item
].name
) {
2331 if (git_buf_joinpath(out
, parent
, items
[item
].name
) < 0)
2335 if (items
[item
].directory
) {
2336 if (git_path_to_dir(out
) < 0)
2343 const char *git_repository_path(const git_repository
*repo
)
2346 return repo
->gitdir
;
2349 const char *git_repository_workdir(const git_repository
*repo
)
2356 return repo
->workdir
;
2359 const char *git_repository_commondir(const git_repository
*repo
)
2362 return repo
->commondir
;
2365 int git_repository_set_workdir(
2366 git_repository
*repo
, const char *workdir
, int update_gitlink
)
2369 git_buf path
= GIT_BUF_INIT
;
2371 assert(repo
&& workdir
);
2373 if (git_path_prettify_dir(&path
, workdir
, NULL
) < 0)
2376 if (repo
->workdir
&& strcmp(repo
->workdir
, path
.ptr
) == 0)
2379 if (update_gitlink
) {
2382 if (git_repository_config__weakptr(&config
, repo
) < 0)
2385 error
= repo_write_gitlink(path
.ptr
, git_repository_path(repo
), false);
2387 /* passthrough error means gitlink is unnecessary */
2388 if (error
== GIT_PASSTHROUGH
)
2389 error
= git_config_delete_entry(config
, "core.worktree");
2391 error
= git_config_set_string(config
, "core.worktree", path
.ptr
);
2394 error
= git_config_set_bool(config
, "core.bare", false);
2398 char *old_workdir
= repo
->workdir
;
2400 repo
->workdir
= git_buf_detach(&path
);
2403 git__free(old_workdir
);
2409 int git_repository_is_bare(const git_repository
*repo
)
2412 return repo
->is_bare
;
2415 int git_repository_is_worktree(const git_repository
*repo
)
2418 return repo
->is_worktree
;
2421 int git_repository_set_bare(git_repository
*repo
)
2431 if ((error
= git_repository_config__weakptr(&config
, repo
)) < 0)
2434 if ((error
= git_config_set_bool(config
, "core.bare", true)) < 0)
2437 if ((error
= git_config__update_entry(config
, "core.worktree", NULL
, true, true)) < 0)
2440 git__free(repo
->workdir
);
2441 repo
->workdir
= NULL
;
2447 int git_repository_head_tree(git_tree
**tree
, git_repository
*repo
)
2449 git_reference
*head
;
2453 if ((error
= git_repository_head(&head
, repo
)) < 0)
2456 if ((error
= git_reference_peel(&obj
, head
, GIT_OBJECT_TREE
)) < 0)
2459 *tree
= (git_tree
*)obj
;
2462 git_reference_free(head
);
2466 int git_repository__set_orig_head(git_repository
*repo
, const git_oid
*orig_head
)
2468 git_filebuf file
= GIT_FILEBUF_INIT
;
2469 git_buf file_path
= GIT_BUF_INIT
;
2470 char orig_head_str
[GIT_OID_HEXSZ
];
2473 git_oid_fmt(orig_head_str
, orig_head
);
2475 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_ORIG_HEAD_FILE
)) == 0 &&
2476 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_FORCE
, GIT_MERGE_FILE_MODE
)) == 0 &&
2477 (error
= git_filebuf_printf(&file
, "%.*s\n", GIT_OID_HEXSZ
, orig_head_str
)) == 0)
2478 error
= git_filebuf_commit(&file
);
2481 git_filebuf_cleanup(&file
);
2483 git_buf_dispose(&file_path
);
2488 int git_repository_message(git_buf
*out
, git_repository
*repo
)
2490 git_buf path
= GIT_BUF_INIT
;
2494 git_buf_sanitize(out
);
2496 if (git_buf_joinpath(&path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
) < 0)
2499 if ((error
= p_stat(git_buf_cstr(&path
), &st
)) < 0) {
2500 if (errno
== ENOENT
)
2501 error
= GIT_ENOTFOUND
;
2502 git_error_set(GIT_ERROR_OS
, "could not access message file");
2504 error
= git_futils_readbuffer(out
, git_buf_cstr(&path
));
2507 git_buf_dispose(&path
);
2512 int git_repository_message_remove(git_repository
*repo
)
2514 git_buf path
= GIT_BUF_INIT
;
2517 if (git_buf_joinpath(&path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
) < 0)
2520 error
= p_unlink(git_buf_cstr(&path
));
2521 git_buf_dispose(&path
);
2526 int git_repository_hashfile(
2528 git_repository
*repo
,
2531 const char *as_path
)
2534 git_filter_list
*fl
= NULL
;
2537 git_buf full_path
= GIT_BUF_INIT
;
2539 assert(out
&& path
&& repo
); /* as_path can be NULL */
2541 /* At some point, it would be nice if repo could be NULL to just
2542 * apply filter rules defined in system and global files, but for
2543 * now that is not possible because git_filters_load() needs it.
2546 error
= git_path_join_unrooted(
2547 &full_path
, path
, git_repository_workdir(repo
), NULL
);
2554 /* passing empty string for "as_path" indicated --no-filters */
2555 if (strlen(as_path
) > 0) {
2556 error
= git_filter_list_load(
2557 &fl
, repo
, NULL
, as_path
,
2558 GIT_FILTER_TO_ODB
, GIT_FILTER_DEFAULT
);
2565 /* at this point, error is a count of the number of loaded filters */
2567 fd
= git_futils_open_ro(full_path
.ptr
);
2573 len
= git_futils_filesize(fd
);
2579 if (!git__is_sizet(len
)) {
2580 git_error_set(GIT_ERROR_OS
, "file size overflow for 32-bit systems");
2585 error
= git_odb__hashfd_filtered(out
, fd
, (size_t)len
, type
, fl
);
2590 git_filter_list_free(fl
);
2591 git_buf_dispose(&full_path
);
2596 static int checkout_message(git_buf
*out
, git_reference
*old
, const char *new)
2598 git_buf_puts(out
, "checkout: moving from ");
2600 if (git_reference_type(old
) == GIT_REFERENCE_SYMBOLIC
)
2601 git_buf_puts(out
, git_reference__shorthand(git_reference_symbolic_target(old
)));
2603 git_buf_puts(out
, git_oid_tostr_s(git_reference_target(old
)));
2605 git_buf_puts(out
, " to ");
2607 if (git_reference__is_branch(new) ||
2608 git_reference__is_tag(new) ||
2609 git_reference__is_remote(new))
2610 git_buf_puts(out
, git_reference__shorthand(new));
2612 git_buf_puts(out
, new);
2614 if (git_buf_oom(out
))
2620 static int detach(git_repository
*repo
, const git_oid
*id
, const char *new)
2623 git_buf log_message
= GIT_BUF_INIT
;
2624 git_object
*object
= NULL
, *peeled
= NULL
;
2625 git_reference
*new_head
= NULL
, *current
= NULL
;
2629 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2632 if ((error
= git_object_lookup(&object
, repo
, id
, GIT_OBJECT_ANY
)) < 0)
2635 if ((error
= git_object_peel(&peeled
, object
, GIT_OBJECT_COMMIT
)) < 0)
2639 new = git_oid_tostr_s(git_object_id(peeled
));
2641 if ((error
= checkout_message(&log_message
, current
, new)) < 0)
2644 error
= git_reference_create(&new_head
, repo
, GIT_HEAD_FILE
, git_object_id(peeled
), true, git_buf_cstr(&log_message
));
2647 git_buf_dispose(&log_message
);
2648 git_object_free(object
);
2649 git_object_free(peeled
);
2650 git_reference_free(current
);
2651 git_reference_free(new_head
);
2655 int git_repository_set_head(
2656 git_repository
* repo
,
2657 const char* refname
)
2659 git_reference
*ref
= NULL
, *current
= NULL
, *new_head
= NULL
;
2660 git_buf log_message
= GIT_BUF_INIT
;
2663 assert(repo
&& refname
);
2665 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2668 if ((error
= checkout_message(&log_message
, current
, refname
)) < 0)
2671 error
= git_reference_lookup(&ref
, repo
, refname
);
2672 if (error
< 0 && error
!= GIT_ENOTFOUND
)
2675 if (ref
&& current
->type
== GIT_REFERENCE_SYMBOLIC
&& git__strcmp(current
->target
.symbolic
, ref
->name
) &&
2676 git_reference_is_branch(ref
) && git_branch_is_checked_out(ref
)) {
2677 git_error_set(GIT_ERROR_REPOSITORY
, "cannot set HEAD to reference '%s' as it is the current HEAD "
2678 "of a linked repository.", git_reference_name(ref
));
2684 if (git_reference_is_branch(ref
)) {
2685 error
= git_reference_symbolic_create(&new_head
, repo
, GIT_HEAD_FILE
,
2686 git_reference_name(ref
), true, git_buf_cstr(&log_message
));
2688 error
= detach(repo
, git_reference_target(ref
),
2689 git_reference_is_tag(ref
) || git_reference_is_remote(ref
) ? refname
: NULL
);
2691 } else if (git_reference__is_branch(refname
)) {
2692 error
= git_reference_symbolic_create(&new_head
, repo
, GIT_HEAD_FILE
, refname
,
2693 true, git_buf_cstr(&log_message
));
2697 git_buf_dispose(&log_message
);
2698 git_reference_free(current
);
2699 git_reference_free(ref
);
2700 git_reference_free(new_head
);
2704 int git_repository_set_head_detached(
2705 git_repository
* repo
,
2706 const git_oid
* commitish
)
2708 return detach(repo
, commitish
, NULL
);
2711 int git_repository_set_head_detached_from_annotated(
2712 git_repository
*repo
,
2713 const git_annotated_commit
*commitish
)
2715 assert(repo
&& commitish
);
2717 return detach(repo
, git_annotated_commit_id(commitish
), commitish
->description
);
2720 int git_repository_detach_head(git_repository
* repo
)
2722 git_reference
*old_head
= NULL
, *new_head
= NULL
, *current
= NULL
;
2723 git_object
*object
= NULL
;
2724 git_buf log_message
= GIT_BUF_INIT
;
2729 if ((error
= git_reference_lookup(¤t
, repo
, GIT_HEAD_FILE
)) < 0)
2732 if ((error
= git_repository_head(&old_head
, repo
)) < 0)
2735 if ((error
= git_object_lookup(&object
, repo
, git_reference_target(old_head
), GIT_OBJECT_COMMIT
)) < 0)
2738 if ((error
= checkout_message(&log_message
, current
, git_oid_tostr_s(git_object_id(object
)))) < 0)
2741 error
= git_reference_create(&new_head
, repo
, GIT_HEAD_FILE
, git_reference_target(old_head
),
2742 1, git_buf_cstr(&log_message
));
2745 git_buf_dispose(&log_message
);
2746 git_object_free(object
);
2747 git_reference_free(old_head
);
2748 git_reference_free(new_head
);
2749 git_reference_free(current
);
2754 * Loosely ported from git.git
2755 * https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L198-289
2757 int git_repository_state(git_repository
*repo
)
2759 git_buf repo_path
= GIT_BUF_INIT
;
2760 int state
= GIT_REPOSITORY_STATE_NONE
;
2764 if (git_buf_puts(&repo_path
, repo
->gitdir
) < 0)
2767 if (git_path_contains_file(&repo_path
, GIT_REBASE_MERGE_INTERACTIVE_FILE
))
2768 state
= GIT_REPOSITORY_STATE_REBASE_INTERACTIVE
;
2769 else if (git_path_contains_dir(&repo_path
, GIT_REBASE_MERGE_DIR
))
2770 state
= GIT_REPOSITORY_STATE_REBASE_MERGE
;
2771 else if (git_path_contains_file(&repo_path
, GIT_REBASE_APPLY_REBASING_FILE
))
2772 state
= GIT_REPOSITORY_STATE_REBASE
;
2773 else if (git_path_contains_file(&repo_path
, GIT_REBASE_APPLY_APPLYING_FILE
))
2774 state
= GIT_REPOSITORY_STATE_APPLY_MAILBOX
;
2775 else if (git_path_contains_dir(&repo_path
, GIT_REBASE_APPLY_DIR
))
2776 state
= GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE
;
2777 else if (git_path_contains_file(&repo_path
, GIT_MERGE_HEAD_FILE
))
2778 state
= GIT_REPOSITORY_STATE_MERGE
;
2779 else if (git_path_contains_file(&repo_path
, GIT_REVERT_HEAD_FILE
)) {
2780 state
= GIT_REPOSITORY_STATE_REVERT
;
2781 if (git_path_contains_file(&repo_path
, GIT_SEQUENCER_TODO_FILE
)) {
2782 state
= GIT_REPOSITORY_STATE_REVERT_SEQUENCE
;
2784 } else if (git_path_contains_file(&repo_path
, GIT_CHERRYPICK_HEAD_FILE
)) {
2785 state
= GIT_REPOSITORY_STATE_CHERRYPICK
;
2786 if (git_path_contains_file(&repo_path
, GIT_SEQUENCER_TODO_FILE
)) {
2787 state
= GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE
;
2789 } else if (git_path_contains_file(&repo_path
, GIT_BISECT_LOG_FILE
))
2790 state
= GIT_REPOSITORY_STATE_BISECT
;
2792 git_buf_dispose(&repo_path
);
2796 int git_repository__cleanup_files(
2797 git_repository
*repo
, const char *files
[], size_t files_len
)
2799 git_buf buf
= GIT_BUF_INIT
;
2803 for (error
= 0, i
= 0; !error
&& i
< files_len
; ++i
) {
2806 if (git_buf_joinpath(&buf
, repo
->gitdir
, files
[i
]) < 0)
2809 path
= git_buf_cstr(&buf
);
2811 if (git_path_isfile(path
)) {
2812 error
= p_unlink(path
);
2813 } else if (git_path_isdir(path
)) {
2814 error
= git_futils_rmdir_r(path
, NULL
,
2815 GIT_RMDIR_REMOVE_FILES
| GIT_RMDIR_REMOVE_BLOCKERS
);
2818 git_buf_clear(&buf
);
2821 git_buf_dispose(&buf
);
2825 static const char *state_files
[] = {
2826 GIT_MERGE_HEAD_FILE
,
2827 GIT_MERGE_MODE_FILE
,
2829 GIT_REVERT_HEAD_FILE
,
2830 GIT_CHERRYPICK_HEAD_FILE
,
2831 GIT_BISECT_LOG_FILE
,
2832 GIT_REBASE_MERGE_DIR
,
2833 GIT_REBASE_APPLY_DIR
,
2837 int git_repository_state_cleanup(git_repository
*repo
)
2841 return git_repository__cleanup_files(repo
, state_files
, ARRAY_SIZE(state_files
));
2844 int git_repository_is_shallow(git_repository
*repo
)
2846 git_buf path
= GIT_BUF_INIT
;
2850 if ((error
= git_buf_joinpath(&path
, repo
->gitdir
, "shallow")) < 0)
2853 error
= git_path_lstat(path
.ptr
, &st
);
2854 git_buf_dispose(&path
);
2856 if (error
== GIT_ENOTFOUND
) {
2863 return st
.st_size
== 0 ? 0 : 1;
2866 int git_repository_init_init_options(
2867 git_repository_init_options
*opts
, unsigned int version
)
2869 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
2870 opts
, version
, git_repository_init_options
,
2871 GIT_REPOSITORY_INIT_OPTIONS_INIT
);
2875 int git_repository_ident(const char **name
, const char **email
, const git_repository
*repo
)
2877 *name
= repo
->ident_name
;
2878 *email
= repo
->ident_email
;
2883 int git_repository_set_ident(git_repository
*repo
, const char *name
, const char *email
)
2885 char *tmp_name
= NULL
, *tmp_email
= NULL
;
2888 tmp_name
= git__strdup(name
);
2889 GIT_ERROR_CHECK_ALLOC(tmp_name
);
2893 tmp_email
= git__strdup(email
);
2894 GIT_ERROR_CHECK_ALLOC(tmp_email
);
2897 tmp_name
= git__swap(repo
->ident_name
, tmp_name
);
2898 tmp_email
= git__swap(repo
->ident_email
, tmp_email
);
2900 git__free(tmp_name
);
2901 git__free(tmp_email
);
2906 int git_repository_submodule_cache_all(git_repository
*repo
)
2912 if ((error
= git_strmap_alloc(&repo
->submodule_cache
)))
2915 error
= git_submodule__map(repo
, repo
->submodule_cache
);
2919 int git_repository_submodule_cache_clear(git_repository
*repo
)
2923 if (repo
->submodule_cache
== NULL
) {
2926 git_strmap_foreach_value(repo
->submodule_cache
, sm
, {
2927 git_submodule_free(sm
);
2929 git_strmap_free(repo
->submodule_cache
);
2930 repo
->submodule_cache
= 0;