Raw
1 #ifndef SETUP_H
2 #define SETUP_H
3
4 #include "refs.h"
5 #include "string-list.h"
6
7 int is_inside_git_dir(struct repository *repo);
8 int is_inside_work_tree(struct repository *repo);
9 int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
10 int get_common_dir(struct strbuf *sb, const char *gitdir);
11
12 /*
13 * Return true if the given path is a git directory; note that this _just_
14 * looks at the directory itself. If you want to know whether "foo/.git"
15 * is a repository, you must feed that path, not just "foo".
16 */
17 int is_git_directory(const char *path);
18
19 /*
20 * Return 1 if the given path is the root of a git repository or
21 * submodule, else 0. Will not return 1 for bare repositories with the
22 * exception of creating a bare repository in "foo/.git" and calling
23 * is_git_repository("foo").
24 *
25 * If we run into read errors, we err on the side of saying "yes, it is",
26 * as we usually consider sub-repos precious, and would prefer to err on the
27 * side of not disrupting or deleting them.
28 */
29 int is_nonbare_repository_dir(struct strbuf *path);
30
31 #define READ_GITFILE_ERR_STAT_FAILED 1
32 #define READ_GITFILE_ERR_NOT_A_FILE 2
33 #define READ_GITFILE_ERR_OPEN_FAILED 3
34 #define READ_GITFILE_ERR_READ_FAILED 4
35 #define READ_GITFILE_ERR_INVALID_FORMAT 5
36 #define READ_GITFILE_ERR_NO_PATH 6
37 #define READ_GITFILE_ERR_NOT_A_REPO 7
38 #define READ_GITFILE_ERR_TOO_LARGE 8
39 #define READ_GITFILE_ERR_MISSING 9
40 #define READ_GITFILE_ERR_IS_A_DIR 10
41 void read_gitfile_error_die(int error_code, const char *path);
42 const char *read_gitfile_gently(const char *path, int *return_error_code);
43 #define read_gitfile(path) read_gitfile_gently((path), NULL)
44 const char *resolve_gitdir_gently(const char *suspect, int *return_error_code);
45 #define resolve_gitdir(path) resolve_gitdir_gently((path), NULL)
46
47 /*
48 * Check if a repository is safe and die if it is not, by verifying the
49 * ownership of the worktree (if any), the git directory, and the gitfile (if
50 * any).
51 *
52 * Exemptions for known-safe repositories can be added via `safe.directory`
53 * config settings; for non-bare repositories, their worktree needs to be
54 * added, for bare ones their git directory.
55 */
56 void die_upon_dubious_ownership(const char *gitfile, const char *worktree,
57 const char *gitdir);
58
59 void setup_work_tree(struct repository *repo);
60
61 /*
62 * discover_git_directory_reason() is similar to discover_git_directory(),
63 * except it returns an enum value instead. It is important to note that
64 * a zero-valued return here is actually GIT_DIR_NONE, which is different
65 * from discover_git_directory.
66 */
67 enum discovery_result {
68 GIT_DIR_EXPLICIT = 1,
69 GIT_DIR_DISCOVERED = 2,
70 GIT_DIR_BARE = 3,
71 /* these are errors */
72 GIT_DIR_HIT_CEILING = -1,
73 GIT_DIR_HIT_MOUNT_POINT = -2,
74 GIT_DIR_INVALID_GITFILE = -3,
75 GIT_DIR_INVALID_OWNERSHIP = -4,
76 GIT_DIR_DISALLOWED_BARE = -5,
77 GIT_DIR_INVALID_FORMAT = -6,
78 GIT_DIR_CWD_FAILURE = -7,
79 };
80 enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
81 struct strbuf *gitdir);
82
83 /*
84 * Find the commondir and gitdir of the repository that contains the current
85 * working directory, without changing the working directory or other global
86 * state. The result is appended to commondir and gitdir. If the discovered
87 * gitdir does not correspond to a worktree, then 'commondir' and 'gitdir' will
88 * both have the same result appended to the buffer. The return value is
89 * either 0 upon success and -1 if no repository was found.
90 */
91 static inline int discover_git_directory(struct strbuf *commondir,
92 struct strbuf *gitdir)
93 {
94 if (discover_git_directory_reason(commondir, gitdir) <= 0)
95 return -1;
96 return 0;
97 }
98
99 /* Flags that can be passed to `enter_repo()`. */
100 enum {
101 /*
102 * Callers that require exact paths (as opposed to allowing known
103 * suffixes like ".git", ".git/.git" to be omitted) can set this bit.
104 */
105 ENTER_REPO_STRICT = (1<<0),
106
107 /*
108 * Callers that are willing to run without ownership check can set this
109 * bit.
110 */
111 ENTER_REPO_ANY_OWNER_OK = (1<<1),
112 };
113
114 /*
115 * Discover and enter a repository.
116 *
117 * First, one directory to try is determined by the following algorithm.
118 *
119 * (0) If "strict" is given, the path is used as given and no DWIM is
120 * done. Otherwise:
121 * (1) "~/path" to mean path under the running user's home directory;
122 * (2) "~user/path" to mean path under named user's home directory;
123 * (3) "relative/path" to mean cwd relative directory; or
124 * (4) "/absolute/path" to mean absolute directory.
125 *
126 * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git"
127 * in this order. We select the first one that is a valid git repository, and
128 * chdir() to it. If none match, or we fail to chdir, we return NULL.
129 *
130 * If all goes well, we return the directory we used to chdir() (but
131 * before ~user is expanded), avoiding getcwd() resolving symbolic
132 * links. User relative paths are also returned as they are given,
133 * except DWIM suffixing.
134 */
135 const char *enter_repo(struct repository *repo, const char *path, unsigned flags);
136
137 const char *setup_git_directory_gently(struct repository *repo, int *);
138 const char *setup_git_directory(struct repository *repo);
139 char *prefix_path(struct repository *repo, const char *prefix, int len, const char *path);
140 char *prefix_path_gently(struct repository *repo, const char *prefix, int len, int *remaining, const char *path);
141
142 int check_filename(const char *prefix, const char *name);
143 void verify_filename(struct repository *repo,
144 const char *prefix,
145 const char *name,
146 int diagnose_misspelt_rev);
147 void verify_non_filename(struct repository *repo, const char *prefix, const char *name);
148 int path_inside_repo(struct repository *repo, const char *prefix, const char *path);
149
150 void sanitize_stdfds(void);
151
152 /*
153 * Daemonize the current process by forking and then exiting the parent
154 * process. Returns 0 when successful, in which case the parent process will
155 * have exited and it's the child process that continues to run the code.
156 * Otherwise, a negative error code is returned and the parent process will
157 * continue execution.
158 *
159 * Note that this function will also perform the following changes:
160 *
161 * - Standard file descriptors in the child process are closed.
162 * - The child process is made a session leader via setsid(3p).
163 * - All tempfiles owned by the parent process are reassigned to the
164 * daemonized child process.
165 */
166 int daemonize(void);
167
168 /*
169 * GIT_REPO_VERSION is the version we write by default. The
170 * _READ variant is the highest number we know how to
171 * handle.
172 */
173 #define GIT_REPO_VERSION 0
174 #define GIT_REPO_VERSION_READ 1
175
176 /*
177 * You _have_ to initialize a `struct repository_format` using
178 * `= REPOSITORY_FORMAT_INIT` before calling `read_repository_format()`.
179 */
180 struct repository_format {
181 int version;
182 int precious_objects;
183 char *partial_clone; /* value of extensions.partialclone */
184 int worktree_config;
185 int relative_worktrees;
186 int submodule_path_cfg;
187 int is_bare;
188 int hash_algo;
189 int compat_hash_algo;
190 enum ref_storage_format ref_storage_format;
191 char *ref_storage_payload;
192 int sparse_index;
193 char *work_tree;
194 struct string_list unknown_extensions;
195 struct string_list v1_only_extensions;
196 };
197
198 /*
199 * Always use this to initialize a `struct repository_format`
200 * to a well-defined, default state before calling
201 * `read_repository()`.
202 */
203 #define REPOSITORY_FORMAT_INIT \
204 { \
205 .version = -1, \
206 .is_bare = -1, \
207 .hash_algo = GIT_HASH_DEFAULT, \
208 .ref_storage_format = REF_STORAGE_FORMAT_FILES, \
209 .unknown_extensions = STRING_LIST_INIT_DUP, \
210 .v1_only_extensions = STRING_LIST_INIT_DUP, \
211 }
212
213 /*
214 * Read the repository format characteristics from the config file "path" into
215 * "format" struct. Returns the numeric version. On error, or if no version is
216 * found in the configuration, -1 is returned, format->version is set to -1,
217 * and all other fields in the struct are set to the default configuration
218 * (REPOSITORY_FORMAT_INIT). Always initialize the struct using
219 * REPOSITORY_FORMAT_INIT before calling this function.
220 */
221 int read_repository_format(struct repository_format *format, const char *path);
222
223 /*
224 * Free the memory held onto by `format`, but not the struct itself.
225 * (No need to use this after `read_repository_format()` fails.)
226 */
227 void clear_repository_format(struct repository_format *format);
228
229 /*
230 * Verify that the repository described by repository_format is something we
231 * can read. If it is, return 0. Otherwise, return -1, and "err" will describe
232 * any errors encountered.
233 */
234 int verify_repository_format(const struct repository_format *format,
235 struct strbuf *err);
236
237 enum apply_repository_format_flags {
238 /*
239 * Honor environment variables when applying the repository format to
240 * the repository. For now, this only covers environment variables that
241 * relate to the object database.
242 */
243 APPLY_REPOSITORY_FORMAT_HONOR_ENV = (1 << 0),
244
245 /*
246 * Usually, the object database is created after the repository format
247 * was applied. This step is skipped if this flag is set, which leaves
248 * us with a partially-working repository.
249 *
250 * This is useful when initializing a new repository.
251 */
252 APPLY_REPOSITORY_FORMAT_SKIP_ODB_CREATION = (1 << 1),
253 };
254
255 /*
256 * Apply the given repository format to the repo. This initializes extensions
257 * and basic data structures required for normal operation. Returns 0 on
258 * success, a negative error code when the format is not valid as determined by
259 * `verify_repository_format()`.
260 */
261 int apply_repository_format(struct repository *repo,
262 const struct repository_format *format,
263 enum apply_repository_format_flags flags,
264 struct strbuf *err);
265
266 const char *get_template_dir(const char *option_template);
267
268 #define INIT_DB_QUIET (1 << 0)
269 #define INIT_DB_EXIST_OK (1 << 1)
270 #define INIT_DB_SKIP_REFDB (1 << 2)
271
272 int init_db(struct repository *repo,
273 const char *git_dir,
274 const char *real_git_dir,
275 const char *worktree,
276 const char *template_dir, int hash_algo,
277 enum ref_storage_format ref_storage_format,
278 const char *initial_branch, int init_shared_repository,
279 unsigned int flags);
280 void initialize_repository_version(struct repository *repo,
281 int hash_algo,
282 enum ref_storage_format ref_storage_format,
283 int reinit);
284 void create_reference_database(struct repository *repo, const char *initial_branch, int quiet);
285
286 /*
287 * NOTE NOTE NOTE!!
288 *
289 * PERM_UMASK, OLD_PERM_GROUP and OLD_PERM_EVERYBODY enumerations must
290 * not be changed. Old repositories have core.sharedrepository written in
291 * numeric format, and therefore these values are preserved for compatibility
292 * reasons.
293 */
294 enum sharedrepo {
295 PERM_UMASK = 0,
296 OLD_PERM_GROUP = 1,
297 OLD_PERM_EVERYBODY = 2,
298 PERM_GROUP = 0660,
299 PERM_EVERYBODY = 0664
300 };
301 int git_config_perm(const char *var, const char *value);
302
303 struct startup_info {
304 /*
305 * Whether the user is asking us to treat the repository as bare via
306 * `git --bare`, even if it's not.
307 */
308 bool force_bare_repository;
309
310 int have_repository;
311 const char *original_cwd;
312 };
313 extern struct startup_info *startup_info;
314 extern const char *tmp_original_cwd;
315
316 #endif /* SETUP_H */