at commit 52e324c8

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