Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 1 | #ifndef SETUP_H |
| 2 | #define SETUP_H |
| 3 | |
Patrick Steinhardt | 318efb9 | 2024-06-06 07:29:01 +0200 | [diff] [blame] | 4 | #include "refs.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 5 | #include "string-list.h" |
| 6 | |
| 7 | int is_inside_git_dir(void); |
| 8 | int is_inside_work_tree(void); |
| 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 | void read_gitfile_error_die(int error_code, const char *path, const char *dir); |
| 40 | const char *read_gitfile_gently(const char *path, int *return_error_code); |
| 41 | #define read_gitfile(path) read_gitfile_gently((path), NULL) |
| 42 | const char *resolve_gitdir_gently(const char *suspect, int *return_error_code); |
| 43 | #define resolve_gitdir(path) resolve_gitdir_gently((path), NULL) |
| 44 | |
Johannes Schindelin | f5b2af0 | 2024-04-17 11:38:18 +0200 | [diff] [blame] | 45 | /* |
| 46 | * Check if a repository is safe and die if it is not, by verifying the |
| 47 | * ownership of the worktree (if any), the git directory, and the gitfile (if |
| 48 | * any). |
| 49 | * |
| 50 | * Exemptions for known-safe repositories can be added via `safe.directory` |
| 51 | * config settings; for non-bare repositories, their worktree needs to be |
| 52 | * added, for bare ones their git directory. |
| 53 | */ |
| 54 | void die_upon_dubious_ownership(const char *gitfile, const char *worktree, |
| 55 | const char *gitdir); |
| 56 | |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 57 | void setup_work_tree(void); |
Derrick Stolee | 26ae8da | 2023-08-28 13:52:25 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * discover_git_directory_reason() is similar to discover_git_directory(), |
| 61 | * except it returns an enum value instead. It is important to note that |
| 62 | * a zero-valued return here is actually GIT_DIR_NONE, which is different |
| 63 | * from discover_git_directory. |
| 64 | */ |
| 65 | enum discovery_result { |
| 66 | GIT_DIR_EXPLICIT = 1, |
| 67 | GIT_DIR_DISCOVERED = 2, |
| 68 | GIT_DIR_BARE = 3, |
| 69 | /* these are errors */ |
| 70 | GIT_DIR_HIT_CEILING = -1, |
| 71 | GIT_DIR_HIT_MOUNT_POINT = -2, |
| 72 | GIT_DIR_INVALID_GITFILE = -3, |
| 73 | GIT_DIR_INVALID_OWNERSHIP = -4, |
| 74 | GIT_DIR_DISALLOWED_BARE = -5, |
| 75 | GIT_DIR_INVALID_FORMAT = -6, |
| 76 | GIT_DIR_CWD_FAILURE = -7, |
| 77 | }; |
| 78 | enum discovery_result discover_git_directory_reason(struct strbuf *commondir, |
| 79 | struct strbuf *gitdir); |
| 80 | |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 81 | /* |
| 82 | * Find the commondir and gitdir of the repository that contains the current |
| 83 | * working directory, without changing the working directory or other global |
| 84 | * state. The result is appended to commondir and gitdir. If the discovered |
| 85 | * gitdir does not correspond to a worktree, then 'commondir' and 'gitdir' will |
| 86 | * both have the same result appended to the buffer. The return value is |
Derrick Stolee | 26ae8da | 2023-08-28 13:52:25 +0000 | [diff] [blame] | 87 | * either 0 upon success and -1 if no repository was found. |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 88 | */ |
Derrick Stolee | 26ae8da | 2023-08-28 13:52:25 +0000 | [diff] [blame] | 89 | static inline int discover_git_directory(struct strbuf *commondir, |
| 90 | struct strbuf *gitdir) |
| 91 | { |
| 92 | if (discover_git_directory_reason(commondir, gitdir) <= 0) |
| 93 | return -1; |
| 94 | return 0; |
| 95 | } |
| 96 | |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 97 | const char *setup_git_directory_gently(int *); |
| 98 | const char *setup_git_directory(void); |
| 99 | char *prefix_path(const char *prefix, int len, const char *path); |
| 100 | char *prefix_path_gently(const char *prefix, int len, int *remaining, const char *path); |
| 101 | |
| 102 | int check_filename(const char *prefix, const char *name); |
| 103 | void verify_filename(const char *prefix, |
| 104 | const char *name, |
| 105 | int diagnose_misspelt_rev); |
| 106 | void verify_non_filename(const char *prefix, const char *name); |
| 107 | int path_inside_repo(const char *prefix, const char *path); |
| 108 | |
| 109 | void sanitize_stdfds(void); |
| 110 | int daemonize(void); |
| 111 | |
| 112 | /* |
| 113 | * GIT_REPO_VERSION is the version we write by default. The |
| 114 | * _READ variant is the highest number we know how to |
| 115 | * handle. |
| 116 | */ |
| 117 | #define GIT_REPO_VERSION 0 |
| 118 | #define GIT_REPO_VERSION_READ 1 |
| 119 | |
| 120 | /* |
| 121 | * You _have_ to initialize a `struct repository_format` using |
| 122 | * `= REPOSITORY_FORMAT_INIT` before calling `read_repository_format()`. |
| 123 | */ |
| 124 | struct repository_format { |
| 125 | int version; |
| 126 | int precious_objects; |
| 127 | char *partial_clone; /* value of extensions.partialclone */ |
| 128 | int worktree_config; |
| 129 | int is_bare; |
| 130 | int hash_algo; |
brian m. carlson | 9ae702f | 2023-10-01 21:40:25 -0500 | [diff] [blame] | 131 | int compat_hash_algo; |
Patrick Steinhardt | 318efb9 | 2024-06-06 07:29:01 +0200 | [diff] [blame] | 132 | enum ref_storage_format ref_storage_format; |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 133 | int sparse_index; |
| 134 | char *work_tree; |
| 135 | struct string_list unknown_extensions; |
| 136 | struct string_list v1_only_extensions; |
| 137 | }; |
| 138 | |
| 139 | /* |
| 140 | * Always use this to initialize a `struct repository_format` |
| 141 | * to a well-defined, default state before calling |
| 142 | * `read_repository()`. |
| 143 | */ |
| 144 | #define REPOSITORY_FORMAT_INIT \ |
| 145 | { \ |
| 146 | .version = -1, \ |
| 147 | .is_bare = -1, \ |
| 148 | .hash_algo = GIT_HASH_SHA1, \ |
Patrick Steinhardt | 173761e | 2023-12-29 08:26:39 +0100 | [diff] [blame] | 149 | .ref_storage_format = REF_STORAGE_FORMAT_FILES, \ |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 150 | .unknown_extensions = STRING_LIST_INIT_DUP, \ |
| 151 | .v1_only_extensions = STRING_LIST_INIT_DUP, \ |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Read the repository format characteristics from the config file "path" into |
| 156 | * "format" struct. Returns the numeric version. On error, or if no version is |
| 157 | * found in the configuration, -1 is returned, format->version is set to -1, |
| 158 | * and all other fields in the struct are set to the default configuration |
| 159 | * (REPOSITORY_FORMAT_INIT). Always initialize the struct using |
| 160 | * REPOSITORY_FORMAT_INIT before calling this function. |
| 161 | */ |
| 162 | int read_repository_format(struct repository_format *format, const char *path); |
| 163 | |
| 164 | /* |
| 165 | * Free the memory held onto by `format`, but not the struct itself. |
| 166 | * (No need to use this after `read_repository_format()` fails.) |
| 167 | */ |
| 168 | void clear_repository_format(struct repository_format *format); |
| 169 | |
| 170 | /* |
| 171 | * Verify that the repository described by repository_format is something we |
| 172 | * can read. If it is, return 0. Otherwise, return -1, and "err" will describe |
| 173 | * any errors encountered. |
| 174 | */ |
| 175 | int verify_repository_format(const struct repository_format *format, |
| 176 | struct strbuf *err); |
| 177 | |
| 178 | /* |
| 179 | * Check the repository format version in the path found in get_git_dir(), |
| 180 | * and die if it is a version we don't understand. Generally one would |
| 181 | * set_git_dir() before calling this, and use it only for "are we in a valid |
| 182 | * repo?". |
| 183 | * |
| 184 | * If successful and fmt is not NULL, fill fmt with data. |
| 185 | */ |
| 186 | void check_repository_format(struct repository_format *fmt); |
| 187 | |
Johannes Schindelin | f5b2af0 | 2024-04-17 11:38:18 +0200 | [diff] [blame] | 188 | const char *get_template_dir(const char *option_template); |
| 189 | |
Patrick Steinhardt | 56cd033 | 2023-12-12 08:00:46 +0100 | [diff] [blame] | 190 | #define INIT_DB_QUIET (1 << 0) |
| 191 | #define INIT_DB_EXIST_OK (1 << 1) |
| 192 | #define INIT_DB_SKIP_REFDB (1 << 2) |
Elijah Newren | e8cf8ef | 2023-05-16 06:33:44 +0000 | [diff] [blame] | 193 | |
| 194 | int init_db(const char *git_dir, const char *real_git_dir, |
| 195 | const char *template_dir, int hash_algo, |
Patrick Steinhardt | 318efb9 | 2024-06-06 07:29:01 +0200 | [diff] [blame] | 196 | enum ref_storage_format ref_storage_format, |
Elijah Newren | e8cf8ef | 2023-05-16 06:33:44 +0000 | [diff] [blame] | 197 | const char *initial_branch, int init_shared_repository, |
| 198 | unsigned int flags); |
Patrick Steinhardt | d7497a4 | 2023-12-29 08:26:47 +0100 | [diff] [blame] | 199 | void initialize_repository_version(int hash_algo, |
Patrick Steinhardt | 318efb9 | 2024-06-06 07:29:01 +0200 | [diff] [blame] | 200 | enum ref_storage_format ref_storage_format, |
Patrick Steinhardt | d7497a4 | 2023-12-29 08:26:47 +0100 | [diff] [blame] | 201 | int reinit); |
Patrick Steinhardt | 318efb9 | 2024-06-06 07:29:01 +0200 | [diff] [blame] | 202 | void create_reference_database(enum ref_storage_format ref_storage_format, |
Patrick Steinhardt | 173761e | 2023-12-29 08:26:39 +0100 | [diff] [blame] | 203 | const char *initial_branch, int quiet); |
Elijah Newren | e8cf8ef | 2023-05-16 06:33:44 +0000 | [diff] [blame] | 204 | |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 205 | /* |
| 206 | * NOTE NOTE NOTE!! |
| 207 | * |
| 208 | * PERM_UMASK, OLD_PERM_GROUP and OLD_PERM_EVERYBODY enumerations must |
| 209 | * not be changed. Old repositories have core.sharedrepository written in |
| 210 | * numeric format, and therefore these values are preserved for compatibility |
| 211 | * reasons. |
| 212 | */ |
| 213 | enum sharedrepo { |
| 214 | PERM_UMASK = 0, |
| 215 | OLD_PERM_GROUP = 1, |
| 216 | OLD_PERM_EVERYBODY = 2, |
| 217 | PERM_GROUP = 0660, |
| 218 | PERM_EVERYBODY = 0664 |
| 219 | }; |
| 220 | int git_config_perm(const char *var, const char *value); |
| 221 | |
| 222 | struct startup_info { |
| 223 | int have_repository; |
| 224 | const char *prefix; |
| 225 | const char *original_cwd; |
| 226 | }; |
| 227 | extern struct startup_info *startup_info; |
| 228 | extern const char *tmp_original_cwd; |
| 229 | |
| 230 | #endif /* SETUP_H */ |