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.
7 #ifndef INCLUDE_repository_h__
8 #define INCLUDE_repository_h__
12 #include "git2/common.h"
15 #include "git2/repository.h"
16 #include "git2/object.h"
17 #include "git2/config.h"
24 #include "attrcache.h"
25 #include "submodule.h"
26 #include "diff_driver.h"
28 #define DOT_GIT ".git"
29 #define GIT_DIR DOT_GIT "/"
30 #define GIT_DIR_MODE 0755
31 #define GIT_BARE_DIR_MODE 0777
33 /* Default DOS-compatible 8.3 "short name" for a git repository, "GIT~1" */
34 #define GIT_DIR_SHORTNAME "GIT~1"
36 extern bool git_repository__fsync_gitdir
;
37 extern bool git_repository__validate_ownership
;
39 /** Cvar cache identifiers */
41 GIT_CONFIGMAP_AUTO_CRLF
= 0, /* core.autocrlf */
42 GIT_CONFIGMAP_EOL
, /* core.eol */
43 GIT_CONFIGMAP_SYMLINKS
, /* core.symlinks */
44 GIT_CONFIGMAP_IGNORECASE
, /* core.ignorecase */
45 GIT_CONFIGMAP_FILEMODE
, /* core.filemode */
46 GIT_CONFIGMAP_IGNORESTAT
, /* core.ignorestat */
47 GIT_CONFIGMAP_TRUSTCTIME
, /* core.trustctime */
48 GIT_CONFIGMAP_ABBREV
, /* core.abbrev */
49 GIT_CONFIGMAP_PRECOMPOSE
, /* core.precomposeunicode */
50 GIT_CONFIGMAP_SAFE_CRLF
, /* core.safecrlf */
51 GIT_CONFIGMAP_LOGALLREFUPDATES
, /* core.logallrefupdates */
52 GIT_CONFIGMAP_PROTECTHFS
, /* core.protectHFS */
53 GIT_CONFIGMAP_PROTECTNTFS
, /* core.protectNTFS */
54 GIT_CONFIGMAP_FSYNCOBJECTFILES
, /* core.fsyncObjectFiles */
55 GIT_CONFIGMAP_LONGPATHS
, /* core.longpaths */
56 GIT_CONFIGMAP_CACHE_MAX
60 * Configuration map value enumerations
62 * These are the values that are actually stored in the configmap cache,
63 * instead of their string equivalents. These values are internal and
64 * symbolic; make sure that none of them is set to `-1`, since that is
65 * the unique identifier for "not cached"
68 /* The value hasn't been loaded from the cache yet */
69 GIT_CONFIGMAP_NOT_CACHED
= -1,
71 /* core.safecrlf: false, 'fail', 'warn' */
72 GIT_SAFE_CRLF_FALSE
= 0,
73 GIT_SAFE_CRLF_FAIL
= 1,
74 GIT_SAFE_CRLF_WARN
= 2,
76 /* core.autocrlf: false, true, 'input; */
77 GIT_AUTO_CRLF_FALSE
= 0,
78 GIT_AUTO_CRLF_TRUE
= 1,
79 GIT_AUTO_CRLF_INPUT
= 2,
80 GIT_AUTO_CRLF_DEFAULT
= GIT_AUTO_CRLF_FALSE
,
82 /* core.eol: unset, 'crlf', 'lf', 'native' */
87 GIT_EOL_NATIVE
= GIT_EOL_CRLF
,
89 GIT_EOL_NATIVE
= GIT_EOL_LF
,
91 GIT_EOL_DEFAULT
= GIT_EOL_NATIVE
,
93 /* core.symlinks: bool */
94 GIT_SYMLINKS_DEFAULT
= GIT_CONFIGMAP_TRUE
,
96 GIT_IGNORECASE_DEFAULT
= GIT_CONFIGMAP_FALSE
,
98 GIT_FILEMODE_DEFAULT
= GIT_CONFIGMAP_TRUE
,
100 GIT_IGNORESTAT_DEFAULT
= GIT_CONFIGMAP_FALSE
,
101 /* core.trustctime */
102 GIT_TRUSTCTIME_DEFAULT
= GIT_CONFIGMAP_TRUE
,
104 GIT_ABBREV_DEFAULT
= 7,
105 /* core.precomposeunicode */
106 GIT_PRECOMPOSE_DEFAULT
= GIT_CONFIGMAP_FALSE
,
108 GIT_SAFE_CRLF_DEFAULT
= GIT_CONFIGMAP_FALSE
,
109 /* core.logallrefupdates */
110 GIT_LOGALLREFUPDATES_FALSE
= GIT_CONFIGMAP_FALSE
,
111 GIT_LOGALLREFUPDATES_TRUE
= GIT_CONFIGMAP_TRUE
,
112 GIT_LOGALLREFUPDATES_UNSET
= 2,
113 GIT_LOGALLREFUPDATES_ALWAYS
= 3,
114 GIT_LOGALLREFUPDATES_DEFAULT
= GIT_LOGALLREFUPDATES_UNSET
,
115 /* core.protectHFS */
116 GIT_PROTECTHFS_DEFAULT
= GIT_CONFIGMAP_FALSE
,
117 /* core.protectNTFS */
118 GIT_PROTECTNTFS_DEFAULT
= GIT_CONFIGMAP_TRUE
,
119 /* core.fsyncObjectFiles */
120 GIT_FSYNCOBJECTFILES_DEFAULT
= GIT_CONFIGMAP_FALSE
,
122 GIT_LONGPATHS_DEFAULT
= GIT_CONFIGMAP_FALSE
123 } git_configmap_value
;
125 /* internal repository init flags */
127 GIT_REPOSITORY_INIT__HAS_DOTGIT
= (1u << 16),
128 GIT_REPOSITORY_INIT__NATURAL_WD
= (1u << 17),
129 GIT_REPOSITORY_INIT__IS_REINIT
= (1u << 18)
132 /** Internal structure for repository object */
133 struct git_repository
{
140 git_attr_cache
*attrcache
;
141 git_diff_driver_registry
*diff_drivers
;
152 git_array_t(git_str
) reserved_names
;
155 unsigned is_worktree
:1;
157 unsigned int lru_counter
;
159 git_atomic32 attr_session_key
;
161 intptr_t configmap_cache
[GIT_CONFIGMAP_CACHE_MAX
];
162 git_strmap
*submodule_cache
;
165 GIT_INLINE(git_attr_cache
*) git_repository_attr_cache(git_repository
*repo
)
167 return repo
->attrcache
;
170 int git_repository_head_tree(git_tree
**tree
, git_repository
*repo
);
171 int git_repository_create_head(const char *git_dir
, const char *ref_name
);
173 typedef int (*git_repository_foreach_worktree_cb
)(git_repository
*, void *);
175 int git_repository_foreach_worktree(git_repository
*repo
,
176 git_repository_foreach_worktree_cb cb
,
180 * Weak pointers to repository internals.
182 * The returned pointers do not need to be freed. Do not keep
183 * permanent references to these (i.e. between API calls), since they may
184 * become invalidated if the user replaces a repository internal.
186 int git_repository_config__weakptr(git_config
**out
, git_repository
*repo
);
187 int git_repository_odb__weakptr(git_odb
**out
, git_repository
*repo
);
188 int git_repository_refdb__weakptr(git_refdb
**out
, git_repository
*repo
);
189 int git_repository_index__weakptr(git_index
**out
, git_repository
*repo
);
192 * Configuration map cache
194 * Efficient access to the most used config variables of a repository.
195 * The cache is cleared every time the config backend is replaced.
197 int git_repository__configmap_lookup(int *out
, git_repository
*repo
, git_configmap_item item
);
198 void git_repository__configmap_lookup_cache_clear(git_repository
*repo
);
200 int git_repository__item_path(git_str
*out
, const git_repository
*repo
, git_repository_item_t item
);
202 GIT_INLINE(int) git_repository__ensure_not_bare(
203 git_repository
*repo
,
204 const char *operation_name
)
206 if (!git_repository_is_bare(repo
))
210 GIT_ERROR_REPOSITORY
,
211 "cannot %s. This operation is not allowed against bare repositories.",
214 return GIT_EBAREREPO
;
217 int git_repository__set_orig_head(git_repository
*repo
, const git_oid
*orig_head
);
219 int git_repository__cleanup_files(git_repository
*repo
, const char *files
[], size_t files_len
);
221 /* The default "reserved names" for a repository */
222 extern git_str git_repository__reserved_names_win32
[];
223 extern size_t git_repository__reserved_names_win32_len
;
225 extern git_str git_repository__reserved_names_posix
[];
226 extern size_t git_repository__reserved_names_posix_len
;
229 * Gets any "reserved names" in the repository. This will return paths
230 * that should not be allowed in the repository (like ".git") to avoid
231 * conflicting with the repository path, or with alternate mechanisms to
232 * the repository path (eg, "GIT~1"). Every attempt will be made to look
233 * up all possible reserved names - if there was a conflict for the shortname
234 * GIT~1, for example, this function will try to look up the alternate
235 * shortname. If that fails, this function returns false, but out and outlen
236 * will still be populated with good defaults.
238 bool git_repository__reserved_names(
239 git_str
**out
, size_t *outlen
, git_repository
*repo
, bool include_ntfs
);
242 * The default branch for the repository; the `init.defaultBranch`
243 * configuration option, if set, or `master` if it is not.
245 int git_repository_initialbranch(git_str
*out
, git_repository
*repo
);
248 * Given a relative `path`, this makes it absolute based on the
249 * repository's working directory. This will perform validation
250 * to ensure that the path is not longer than MAX_PATH on Windows
251 * (unless `core.longpaths` is set in the repo config).
253 int git_repository_workdir_path(git_str
*out
, git_repository
*repo
, const char *path
);
255 int git_repository__extensions(char ***out
, size_t *out_len
);
256 int git_repository__set_extensions(const char **extensions
, size_t len
);
257 void git_repository__free_extensions(void);