| Elijah Newren | e38da48 | 2023-03-21 06:26:05 | [diff] [blame] | 1 | #ifndef SETUP_H |
| 2 | #define SETUP_H |
| 3 | |
| 4 | #include "string-list.h" |
| 5 | |
| 6 | int is_inside_git_dir(void); |
| 7 | int is_inside_work_tree(void); |
| 8 | int get_common_dir_noenv(struct strbuf *sb, const char *gitdir); |
| 9 | int get_common_dir(struct strbuf *sb, const char *gitdir); |
| 10 | |
| 11 | /* |
| 12 | * Return true if the given path is a git directory; note that this _just_ |
| 13 | * looks at the directory itself. If you want to know whether "foo/.git" |
| 14 | * is a repository, you must feed that path, not just "foo". |
| 15 | */ |
| 16 | int is_git_directory(const char *path); |
| 17 | |
| 18 | /* |
| 19 | * Return 1 if the given path is the root of a git repository or |
| 20 | * submodule, else 0. Will not return 1 for bare repositories with the |
| 21 | * exception of creating a bare repository in "foo/.git" and calling |
| 22 | * is_git_repository("foo"). |
| 23 | * |
| 24 | * If we run into read errors, we err on the side of saying "yes, it is", |
| 25 | * as we usually consider sub-repos precious, and would prefer to err on the |
| 26 | * side of not disrupting or deleting them. |
| 27 | */ |
| 28 | int is_nonbare_repository_dir(struct strbuf *path); |
| 29 | |
| 30 | #define READ_GITFILE_ERR_STAT_FAILED 1 |
| 31 | #define READ_GITFILE_ERR_NOT_A_FILE 2 |
| 32 | #define READ_GITFILE_ERR_OPEN_FAILED 3 |
| 33 | #define READ_GITFILE_ERR_READ_FAILED 4 |
| 34 | #define READ_GITFILE_ERR_INVALID_FORMAT 5 |
| 35 | #define READ_GITFILE_ERR_NO_PATH 6 |
| 36 | #define READ_GITFILE_ERR_NOT_A_REPO 7 |
| 37 | #define READ_GITFILE_ERR_TOO_LARGE 8 |
| 38 | void read_gitfile_error_die(int error_code, const char *path, const char *dir); |
| 39 | const char *read_gitfile_gently(const char *path, int *return_error_code); |
| 40 | #define read_gitfile(path) read_gitfile_gently((path), NULL) |
| 41 | const char *resolve_gitdir_gently(const char *suspect, int *return_error_code); |
| 42 | #define resolve_gitdir(path) resolve_gitdir_gently((path), NULL) |
| 43 | |
| 44 | void setup_work_tree(void); |
| Derrick Stolee | 26ae8da | 2023-08-28 13:52:25 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * discover_git_directory_reason() is similar to discover_git_directory(), |
| 48 | * except it returns an enum value instead. It is important to note that |
| 49 | * a zero-valued return here is actually GIT_DIR_NONE, which is different |
| 50 | * from discover_git_directory. |
| 51 | */ |
| 52 | enum discovery_result { |
| 53 | GIT_DIR_EXPLICIT = 1, |
| 54 | GIT_DIR_DISCOVERED = 2, |
| 55 | GIT_DIR_BARE = 3, |
| 56 | /* these are errors */ |
| 57 | GIT_DIR_HIT_CEILING = -1, |
| 58 | GIT_DIR_HIT_MOUNT_POINT = -2, |
| 59 | GIT_DIR_INVALID_GITFILE = -3, |
| 60 | GIT_DIR_INVALID_OWNERSHIP = -4, |
| 61 | GIT_DIR_DISALLOWED_BARE = -5, |
| 62 | GIT_DIR_INVALID_FORMAT = -6, |
| 63 | GIT_DIR_CWD_FAILURE = -7, |
| 64 | }; |
| 65 | enum discovery_result discover_git_directory_reason(struct strbuf *commondir, |
| 66 | struct strbuf *gitdir); |
| 67 | |
| Elijah Newren | e38da48 | 2023-03-21 06:26:05 | [diff] [blame] | 68 | /* |
| 69 | * Find the commondir and gitdir of the repository that contains the current |
| 70 | * working directory, without changing the working directory or other global |
| 71 | * state. The result is appended to commondir and gitdir. If the discovered |
| 72 | * gitdir does not correspond to a worktree, then 'commondir' and 'gitdir' will |
| 73 | * both have the same result appended to the buffer. The return value is |
| Derrick Stolee | 26ae8da | 2023-08-28 13:52:25 | [diff] [blame] | 74 | * either 0 upon success and -1 if no repository was found. |
| Elijah Newren | e38da48 | 2023-03-21 06:26:05 | [diff] [blame] | 75 | */ |
| Derrick Stolee | 26ae8da | 2023-08-28 13:52:25 | [diff] [blame] | 76 | static inline int discover_git_directory(struct strbuf *commondir, |
| 77 | struct strbuf *gitdir) |
| 78 | { |
| 79 | if (discover_git_directory_reason(commondir, gitdir) <= 0) |
| 80 | return -1; |
| 81 | return 0; |
| 82 | } |
| 83 | |
| Elijah Newren | e38da48 | 2023-03-21 06:26:05 | [diff] [blame] | 84 | const char *setup_git_directory_gently(int *); |
| 85 | const char *setup_git_directory(void); |
| 86 | char *prefix_path(const char *prefix, int len, const char *path); |
| 87 | char *prefix_path_gently(const char *prefix, int len, int *remaining, const char *path); |
| 88 | |
| 89 | int check_filename(const char *prefix, const char *name); |
| 90 | void verify_filename(const char *prefix, |
| 91 | const char *name, |
| 92 | int diagnose_misspelt_rev); |
| 93 | void verify_non_filename(const char *prefix, const char *name); |
| 94 | int path_inside_repo(const char *prefix, const char *path); |
| 95 | |
| 96 | void sanitize_stdfds(void); |
| 97 | int daemonize(void); |
| 98 | |
| 99 | /* |
| 100 | * GIT_REPO_VERSION is the version we write by default. The |
| 101 | * _READ variant is the highest number we know how to |
| 102 | * handle. |
| 103 | */ |
| 104 | #define GIT_REPO_VERSION 0 |
| 105 | #define GIT_REPO_VERSION_READ 1 |
| 106 | |
| 107 | /* |
| 108 | * You _have_ to initialize a `struct repository_format` using |
| 109 | * `= REPOSITORY_FORMAT_INIT` before calling `read_repository_format()`. |
| 110 | */ |
| 111 | struct repository_format { |
| 112 | int version; |
| 113 | int precious_objects; |
| 114 | char *partial_clone; /* value of extensions.partialclone */ |
| 115 | int worktree_config; |
| 116 | int is_bare; |
| 117 | int hash_algo; |
| 118 | int sparse_index; |
| 119 | char *work_tree; |
| 120 | struct string_list unknown_extensions; |
| 121 | struct string_list v1_only_extensions; |
| 122 | }; |
| 123 | |
| 124 | /* |
| 125 | * Always use this to initialize a `struct repository_format` |
| 126 | * to a well-defined, default state before calling |
| 127 | * `read_repository()`. |
| 128 | */ |
| 129 | #define REPOSITORY_FORMAT_INIT \ |
| 130 | { \ |
| 131 | .version = -1, \ |
| 132 | .is_bare = -1, \ |
| 133 | .hash_algo = GIT_HASH_SHA1, \ |
| 134 | .unknown_extensions = STRING_LIST_INIT_DUP, \ |
| 135 | .v1_only_extensions = STRING_LIST_INIT_DUP, \ |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Read the repository format characteristics from the config file "path" into |
| 140 | * "format" struct. Returns the numeric version. On error, or if no version is |
| 141 | * found in the configuration, -1 is returned, format->version is set to -1, |
| 142 | * and all other fields in the struct are set to the default configuration |
| 143 | * (REPOSITORY_FORMAT_INIT). Always initialize the struct using |
| 144 | * REPOSITORY_FORMAT_INIT before calling this function. |
| 145 | */ |
| 146 | int read_repository_format(struct repository_format *format, const char *path); |
| 147 | |
| 148 | /* |
| 149 | * Free the memory held onto by `format`, but not the struct itself. |
| 150 | * (No need to use this after `read_repository_format()` fails.) |
| 151 | */ |
| 152 | void clear_repository_format(struct repository_format *format); |
| 153 | |
| 154 | /* |
| 155 | * Verify that the repository described by repository_format is something we |
| 156 | * can read. If it is, return 0. Otherwise, return -1, and "err" will describe |
| 157 | * any errors encountered. |
| 158 | */ |
| 159 | int verify_repository_format(const struct repository_format *format, |
| 160 | struct strbuf *err); |
| 161 | |
| 162 | /* |
| 163 | * Check the repository format version in the path found in get_git_dir(), |
| 164 | * and die if it is a version we don't understand. Generally one would |
| 165 | * set_git_dir() before calling this, and use it only for "are we in a valid |
| 166 | * repo?". |
| 167 | * |
| 168 | * If successful and fmt is not NULL, fill fmt with data. |
| 169 | */ |
| 170 | void check_repository_format(struct repository_format *fmt); |
| 171 | |
| Elijah Newren | e8cf8ef | 2023-05-16 06:33:44 | [diff] [blame] | 172 | #define INIT_DB_QUIET 0x0001 |
| 173 | #define INIT_DB_EXIST_OK 0x0002 |
| 174 | |
| 175 | int init_db(const char *git_dir, const char *real_git_dir, |
| 176 | const char *template_dir, int hash_algo, |
| 177 | const char *initial_branch, int init_shared_repository, |
| 178 | unsigned int flags); |
| 179 | void initialize_repository_version(int hash_algo, int reinit); |
| 180 | |
| Elijah Newren | e38da48 | 2023-03-21 06:26:05 | [diff] [blame] | 181 | /* |
| 182 | * NOTE NOTE NOTE!! |
| 183 | * |
| 184 | * PERM_UMASK, OLD_PERM_GROUP and OLD_PERM_EVERYBODY enumerations must |
| 185 | * not be changed. Old repositories have core.sharedrepository written in |
| 186 | * numeric format, and therefore these values are preserved for compatibility |
| 187 | * reasons. |
| 188 | */ |
| 189 | enum sharedrepo { |
| 190 | PERM_UMASK = 0, |
| 191 | OLD_PERM_GROUP = 1, |
| 192 | OLD_PERM_EVERYBODY = 2, |
| 193 | PERM_GROUP = 0660, |
| 194 | PERM_EVERYBODY = 0664 |
| 195 | }; |
| 196 | int git_config_perm(const char *var, const char *value); |
| 197 | |
| 198 | struct startup_info { |
| 199 | int have_repository; |
| 200 | const char *prefix; |
| 201 | const char *original_cwd; |
| 202 | }; |
| 203 | extern struct startup_info *startup_info; |
| 204 | extern const char *tmp_original_cwd; |
| 205 | |
| 206 | #endif /* SETUP_H */ |