Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/setup.h # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / setup.h` 386 lines ยท 14199 bytes Raw Blame History 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 enum object_storage_format object_storage_format; 194 char *object_storage_payload; 195 int sparse_index; 196 char *work_tree; 197 struct string_list unknown_extensions; 198 struct string_list v1_only_extensions; 199 }; 200 201 /* 202 * Always use this to initialize a `struct repository_format` 203 * to a well-defined, default state before calling 204 * `read_repository()`. 205 */ 206 #define REPOSITORY_FORMAT_INIT \ 207 { \ 208 .version = -1, \ 209 .is_bare = -1, \ 210 .hash_algo = GIT_HASH_DEFAULT, \ 211 .ref_storage_format = REF_STORAGE_FORMAT_FILES, \ 212 .object_storage_format = OBJECT_STORAGE_FORMAT_FILES, \ 213 .unknown_extensions = STRING_LIST_INIT_DUP, \ 214 .v1_only_extensions = STRING_LIST_INIT_DUP, \ 215 } 216 217 /* 218 * Read the repository format characteristics from the config file "path" into 219 * "format" struct. Returns the numeric version. On error, or if no version is 220 * found in the configuration, -1 is returned, format->version is set to -1, 221 * and all other fields in the struct are set to the default configuration 222 * (REPOSITORY_FORMAT_INIT). Always initialize the struct using 223 * REPOSITORY_FORMAT_INIT before calling this function. 224 */ 225 int read_repository_format(struct repository_format *format, const char *path); 226 227 /* 228 * Free the memory held onto by `format`, but not the struct itself. 229 * (No need to use this after `read_repository_format()` fails.) 230 */ 231 void clear_repository_format(struct repository_format *format); 232 233 /* 234 * Verify that the repository described by repository_format is something we 235 * can read. If it is, return 0. Otherwise, return -1, and "err" will describe 236 * any errors encountered. 237 */ 238 int verify_repository_format(const struct repository_format *format, 239 struct strbuf *err); 240 241 enum apply_repository_format_flags { 242 /* 243 * Honor environment variables when applying the repository format to 244 * the repository. For now, this only covers environment variables that 245 * relate to the object database. 246 */ 247 APPLY_REPOSITORY_FORMAT_HONOR_ENV = (1 << 0), 248 }; 249 250 /* 251 * Apply the given repository format to the repo. This initializes extensions 252 * required for normal operation. Returns 0 on success, a negative error code 253 * when the format is not valid as determined by 254 * `verify_repository_format()`. 255 */ 256 int apply_repository_format(struct repository *repo, 257 const struct repository_format *format, 258 enum apply_repository_format_flags flags, 259 struct strbuf *err); 260 261 const char *get_template_dir(const char *option_template); 262 263 /* 264 * Create the repository by creating the necessary directory structures, 265 * setting up the configuration and configuring the repository's format. If 266 * `template_dir` is set, copy over templates from that directory. Furthermore, 267 * if and only if `reinit_ok` is a non-NULL pointer, then the function may 268 * reinitialize a preexisting repository. In that case, the pointer will be set 269 * to `1` in case the repo was reinitialized and `0` if it didn't exist yet. 270 * 271 * Note that this function does not create the reference and object databases. 272 */ 273 void create_repository(struct repository *repo, 274 const char *git_dir, 275 const char *real_git_dir, 276 const char *worktree, 277 const char *template_dir, 278 int hash_algo, 279 const char *ref_storage_format_uri, 280 const char *object_storage_format_uri, 281 int init_shared_repository, 282 int *reinit_ok); 283 284 void initialize_repository_version(struct repository *repo, 285 int hash_algo, 286 enum ref_storage_format ref_storage_format, 287 enum object_storage_format object_storage_format, 288 int reinit); 289 290 /* 291 * Create the reference database for the repository. The repository and its ref 292 * storage format must have already been configured properly before calling 293 * this function. When set, `initial_branch` overrides the default branch that 294 * HEAD will point to. 295 */ 296 void create_reference_database(struct repository *repo, const char *initial_branch, int quiet); 297 298 /* 299 * Create the object database for the repository. The repository must have 300 * already been configured properly before calling this function. When set, 301 * `alternates` is the list of alternates that should be written into the 302 * object database. 303 */ 304 void create_object_database(struct repository *repo, const struct strvec *alternates); 305 306 /* 307 * NOTE NOTE NOTE!! 308 * 309 * PERM_UMASK, OLD_PERM_GROUP and OLD_PERM_EVERYBODY enumerations must 310 * not be changed. Old repositories have core.sharedrepository written in 311 * numeric format, and therefore these values are preserved for compatibility 312 * reasons. 313 */ 314 enum sharedrepo { 315 PERM_UMASK = 0, 316 OLD_PERM_GROUP = 1, 317 OLD_PERM_EVERYBODY = 2, 318 PERM_GROUP = 0660, 319 PERM_EVERYBODY = 0664 320 }; 321 int git_config_perm(const char *var, const char *value); 322 323 struct startup_info { 324 /* 325 * Whether the user is asking us to treat the repository as bare via 326 * `git --bare`, even if it's not. 327 */ 328 bool force_bare_repository; 329 330 int have_repository; 331 const char *original_cwd; 332 }; 333 extern struct startup_info *startup_info; 334 extern const char *tmp_original_cwd; 335 336 /* Path allowlist */ 337 338 struct path_allowlist_cb_data { 339 const char *key; 340 }; 341 342 /* 343 * Check the allowlist entry in `allowed` against `target_path`, 344 * updating `*matches` accordingly. 345 * 346 * `allowed` is a single entry of an allowlist of paths, typically one 347 * value of a multi-valued config variable, already expanded by 348 * git_config_pathname(). `target_path` is the (normalized) path being 349 * tested. `*matches` is updated in place: 350 * 351 * - an empty `allowed` resets it to 'false' (so a later, more 352 * specific config scope can clear entries from a broader one), 353 * - "*" sets it to 'true' (allow everything), 354 * - "" sets it to 'true' if equals `target_path`, 355 * - "" + "/" + "*" sets it to 'true' if is a leading 356 * directory of `target_path`, 357 * - anything else leaves `*matches` unchanged. 358 * 359 * `allow_path` is called with `allowed` and `allow_path_cbdata`, and 360 * should return 'true' if the entry is acceptable to the caller. It 361 * lets each caller decide which paths it is willing to consider, and 362 * whether to warn about the ones it rejects. Returning 'false' leaves 363 * `*matches` unchanged. 364 * 365 * Callers are expected to invoke this once per allowlist entry, 366 * typically from a protected-config callback, so that untrusted 367 * repository config cannot influence the decision. 368 */ 369 void path_allowlist_apply(const char *allowed, const char *target_path, 370 bool *matches, 371 bool (*allow_path)(const char *path, void *cbdata), 372 void *allow_path_cbdata); 373 374 /* 375 * Apply one value of a multi-valued config variable holding an 376 * allowlist of paths, expanding it with git_config_pathname() before 377 * checking it against `target_path`. Empty and "*" values are passed 378 * through without expansion, as interpolating them is not 379 * meaningful. See path_allowlist_apply(). 380 */ 381 void path_allowlist_config_apply(const char *key, const char *value, 382 const char *target_path, bool *matches, 383 bool (*allow_path)(const char *path, void *cbdata), 384 void *allow_path_cbdata); 385 386 #endif /* SETUP_H */