]> git.proxmox.com Git - libgit2.git/blob - src/repository.h
1edcc842b38869a0a84e43f376e61c421458b7b5
[libgit2.git] / src / repository.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7 #ifndef INCLUDE_repository_h__
8 #define INCLUDE_repository_h__
9
10 #include "common.h"
11
12 #include "git2/common.h"
13 #include "git2/oid.h"
14 #include "git2/odb.h"
15 #include "git2/repository.h"
16 #include "git2/object.h"
17 #include "git2/config.h"
18
19 #include "array.h"
20 #include "cache.h"
21 #include "refs.h"
22 #include "buffer.h"
23 #include "object.h"
24 #include "attrcache.h"
25 #include "submodule.h"
26 #include "diff_driver.h"
27
28 #define DOT_GIT ".git"
29 #define GIT_DIR DOT_GIT "/"
30 #define GIT_DIR_MODE 0755
31 #define GIT_BARE_DIR_MODE 0777
32
33 /* Default DOS-compatible 8.3 "short name" for a git repository, "GIT~1" */
34 #define GIT_DIR_SHORTNAME "GIT~1"
35
36 extern bool git_repository__fsync_gitdir;
37
38 /** Cvar cache identifiers */
39 typedef enum {
40 GIT_CVAR_AUTO_CRLF = 0, /* core.autocrlf */
41 GIT_CVAR_EOL, /* core.eol */
42 GIT_CVAR_SYMLINKS, /* core.symlinks */
43 GIT_CVAR_IGNORECASE, /* core.ignorecase */
44 GIT_CVAR_FILEMODE, /* core.filemode */
45 GIT_CVAR_IGNORESTAT, /* core.ignorestat */
46 GIT_CVAR_TRUSTCTIME, /* core.trustctime */
47 GIT_CVAR_ABBREV, /* core.abbrev */
48 GIT_CVAR_PRECOMPOSE, /* core.precomposeunicode */
49 GIT_CVAR_SAFE_CRLF, /* core.safecrlf */
50 GIT_CVAR_LOGALLREFUPDATES, /* core.logallrefupdates */
51 GIT_CVAR_PROTECTHFS, /* core.protectHFS */
52 GIT_CVAR_PROTECTNTFS, /* core.protectNTFS */
53 GIT_CVAR_FSYNCOBJECTFILES, /* core.fsyncObjectFiles */
54 GIT_CVAR_CACHE_MAX
55 } git_cvar_cached;
56
57 /**
58 * CVAR value enumerations
59 *
60 * These are the values that are actually stored in the cvar cache, instead
61 * of their string equivalents. These values are internal and symbolic;
62 * make sure that none of them is set to `-1`, since that is the unique
63 * identifier for "not cached"
64 */
65 typedef enum {
66 /* The value hasn't been loaded from the cache yet */
67 GIT_CVAR_NOT_CACHED = -1,
68
69 /* core.safecrlf: false, 'fail', 'warn' */
70 GIT_SAFE_CRLF_FALSE = 0,
71 GIT_SAFE_CRLF_FAIL = 1,
72 GIT_SAFE_CRLF_WARN = 2,
73
74 /* core.autocrlf: false, true, 'input; */
75 GIT_AUTO_CRLF_FALSE = 0,
76 GIT_AUTO_CRLF_TRUE = 1,
77 GIT_AUTO_CRLF_INPUT = 2,
78 GIT_AUTO_CRLF_DEFAULT = GIT_AUTO_CRLF_FALSE,
79
80 /* core.eol: unset, 'crlf', 'lf', 'native' */
81 GIT_EOL_UNSET = 0,
82 GIT_EOL_CRLF = 1,
83 GIT_EOL_LF = 2,
84 #ifdef GIT_WIN32
85 GIT_EOL_NATIVE = GIT_EOL_CRLF,
86 #else
87 GIT_EOL_NATIVE = GIT_EOL_LF,
88 #endif
89 GIT_EOL_DEFAULT = GIT_EOL_NATIVE,
90
91 /* core.symlinks: bool */
92 GIT_SYMLINKS_DEFAULT = GIT_CVAR_TRUE,
93 /* core.ignorecase */
94 GIT_IGNORECASE_DEFAULT = GIT_CVAR_FALSE,
95 /* core.filemode */
96 GIT_FILEMODE_DEFAULT = GIT_CVAR_TRUE,
97 /* core.ignorestat */
98 GIT_IGNORESTAT_DEFAULT = GIT_CVAR_FALSE,
99 /* core.trustctime */
100 GIT_TRUSTCTIME_DEFAULT = GIT_CVAR_TRUE,
101 /* core.abbrev */
102 GIT_ABBREV_DEFAULT = 7,
103 /* core.precomposeunicode */
104 GIT_PRECOMPOSE_DEFAULT = GIT_CVAR_FALSE,
105 /* core.safecrlf */
106 GIT_SAFE_CRLF_DEFAULT = GIT_CVAR_FALSE,
107 /* core.logallrefupdates */
108 GIT_LOGALLREFUPDATES_FALSE = GIT_CVAR_FALSE,
109 GIT_LOGALLREFUPDATES_TRUE = GIT_CVAR_TRUE,
110 GIT_LOGALLREFUPDATES_UNSET = 2,
111 GIT_LOGALLREFUPDATES_ALWAYS = 3,
112 GIT_LOGALLREFUPDATES_DEFAULT = GIT_LOGALLREFUPDATES_UNSET,
113 /* core.protectHFS */
114 GIT_PROTECTHFS_DEFAULT = GIT_CVAR_FALSE,
115 /* core.protectNTFS */
116 GIT_PROTECTNTFS_DEFAULT = GIT_CVAR_FALSE,
117 /* core.fsyncObjectFiles */
118 GIT_FSYNCOBJECTFILES_DEFAULT = GIT_CVAR_FALSE,
119 } git_cvar_value;
120
121 /* internal repository init flags */
122 enum {
123 GIT_REPOSITORY_INIT__HAS_DOTGIT = (1u << 16),
124 GIT_REPOSITORY_INIT__NATURAL_WD = (1u << 17),
125 GIT_REPOSITORY_INIT__IS_REINIT = (1u << 18),
126 };
127
128 /** Internal structure for repository object */
129 struct git_repository {
130 git_odb *_odb;
131 git_refdb *_refdb;
132 git_config *_config;
133 git_index *_index;
134
135 git_cache objects;
136 git_attr_cache *attrcache;
137 git_diff_driver_registry *diff_drivers;
138
139 char *gitlink;
140 char *gitdir;
141 char *commondir;
142 char *workdir;
143 char *namespace;
144
145 char *ident_name;
146 char *ident_email;
147
148 git_array_t(git_buf) reserved_names;
149
150 unsigned is_bare:1;
151 unsigned is_worktree:1;
152
153 unsigned int lru_counter;
154
155 git_atomic attr_session_key;
156
157 git_cvar_value cvar_cache[GIT_CVAR_CACHE_MAX];
158 git_strmap *submodule_cache;
159 };
160
161 GIT_INLINE(git_attr_cache *) git_repository_attr_cache(git_repository *repo)
162 {
163 return repo->attrcache;
164 }
165
166 int git_repository_head_tree(git_tree **tree, git_repository *repo);
167 int git_repository_create_head(const char *git_dir, const char *ref_name);
168
169 /*
170 * Called for each HEAD.
171 *
172 * Can return either 0, causing the iteration over HEADs to
173 * continue, or a non-0 value causing the iteration to abort. The
174 * return value is passed back to the caller of
175 * `git_repository_foreach_head`
176 */
177 typedef int (*git_repository_foreach_head_cb)(git_repository *repo, const char *path, void *payload);
178
179 /*
180 * Iterate over repository and all worktree HEADs.
181 *
182 * This function will be called for the repository HEAD and for
183 * all HEADS of linked worktrees. For each HEAD, the callback is
184 * executed with the given payload. The return value equals the
185 * return value of the last executed callback function.
186 */
187 int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload);
188
189 /*
190 * Weak pointers to repository internals.
191 *
192 * The returned pointers do not need to be freed. Do not keep
193 * permanent references to these (i.e. between API calls), since they may
194 * become invalidated if the user replaces a repository internal.
195 */
196 int git_repository_config__weakptr(git_config **out, git_repository *repo);
197 int git_repository_odb__weakptr(git_odb **out, git_repository *repo);
198 int git_repository_refdb__weakptr(git_refdb **out, git_repository *repo);
199 int git_repository_index__weakptr(git_index **out, git_repository *repo);
200
201 /*
202 * CVAR cache
203 *
204 * Efficient access to the most used config variables of a repository.
205 * The cache is cleared every time the config backend is replaced.
206 */
207 int git_repository__cvar(int *out, git_repository *repo, git_cvar_cached cvar);
208 void git_repository__cvar_cache_clear(git_repository *repo);
209
210 GIT_INLINE(int) git_repository__ensure_not_bare(
211 git_repository *repo,
212 const char *operation_name)
213 {
214 if (!git_repository_is_bare(repo))
215 return 0;
216
217 git_error_set(
218 GIT_ERROR_REPOSITORY,
219 "cannot %s. This operation is not allowed against bare repositories.",
220 operation_name);
221
222 return GIT_EBAREREPO;
223 }
224
225 int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head);
226
227 int git_repository__cleanup_files(git_repository *repo, const char *files[], size_t files_len);
228
229 /* The default "reserved names" for a repository */
230 extern git_buf git_repository__reserved_names_win32[];
231 extern size_t git_repository__reserved_names_win32_len;
232
233 extern git_buf git_repository__reserved_names_posix[];
234 extern size_t git_repository__reserved_names_posix_len;
235
236 /*
237 * Gets any "reserved names" in the repository. This will return paths
238 * that should not be allowed in the repository (like ".git") to avoid
239 * conflicting with the repository path, or with alternate mechanisms to
240 * the repository path (eg, "GIT~1"). Every attempt will be made to look
241 * up all possible reserved names - if there was a conflict for the shortname
242 * GIT~1, for example, this function will try to look up the alternate
243 * shortname. If that fails, this function returns false, but out and outlen
244 * will still be populated with good defaults.
245 */
246 bool git_repository__reserved_names(
247 git_buf **out, size_t *outlen, git_repository *repo, bool include_ntfs);
248
249 #endif