Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 2 | #include "repository.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 4 | #include "dir.h" |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 5 | #include "string-list.h" |
Jeff King | 8500e0d | 2018-03-30 14:35:08 -0400 | [diff] [blame] | 6 | #include "chdir-notify.h" |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 7 | |
| 8 | static int inside_git_dir = -1; |
| 9 | static int inside_work_tree = -1; |
Jeff King | fada767 | 2015-05-29 02:49:10 -0400 | [diff] [blame] | 10 | static int work_tree_config_is_bogus; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 11 | |
Jeff King | 46c3cd4 | 2016-03-05 17:10:27 -0500 | [diff] [blame] | 12 | static struct startup_info the_startup_info; |
| 13 | struct startup_info *startup_info = &the_startup_info; |
| 14 | |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 15 | /* |
Martin Erik Werner | ddc2a62 | 2014-02-04 15:25:19 +0100 | [diff] [blame] | 16 | * The input parameter must contain an absolute path, and it must already be |
| 17 | * normalized. |
| 18 | * |
| 19 | * Find the part of an absolute path that lies inside the work tree by |
| 20 | * dereferencing symlinks outside the work tree, for example: |
| 21 | * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file |
| 22 | * /dir/file (work tree is /) -> dir/file |
| 23 | * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2 |
| 24 | * /dir/repolink/file (repolink points to /dir/repo) -> file |
| 25 | * /dir/repo (exactly equal to work tree) -> (empty string) |
| 26 | */ |
| 27 | static int abspath_part_inside_repo(char *path) |
| 28 | { |
| 29 | size_t len; |
| 30 | size_t wtlen; |
| 31 | char *path0; |
| 32 | int off; |
| 33 | const char *work_tree = get_git_work_tree(); |
| 34 | |
| 35 | if (!work_tree) |
| 36 | return -1; |
| 37 | wtlen = strlen(work_tree); |
| 38 | len = strlen(path); |
Martin Erik Werner | 6127ff6 | 2014-04-24 15:06:09 +0200 | [diff] [blame] | 39 | off = offset_1st_component(path); |
Martin Erik Werner | ddc2a62 | 2014-02-04 15:25:19 +0100 | [diff] [blame] | 40 | |
| 41 | /* check if work tree is already the prefix */ |
| 42 | if (wtlen <= len && !strncmp(path, work_tree, wtlen)) { |
| 43 | if (path[wtlen] == '/') { |
| 44 | memmove(path, path + wtlen + 1, len - wtlen); |
| 45 | return 0; |
| 46 | } else if (path[wtlen - 1] == '/' || path[wtlen] == '\0') { |
| 47 | /* work tree is the root, or the whole path */ |
| 48 | memmove(path, path + wtlen, len - wtlen + 1); |
| 49 | return 0; |
| 50 | } |
| 51 | /* work tree might match beginning of a symlink to work tree */ |
| 52 | off = wtlen; |
| 53 | } |
| 54 | path0 = path; |
Martin Erik Werner | 6127ff6 | 2014-04-24 15:06:09 +0200 | [diff] [blame] | 55 | path += off; |
Martin Erik Werner | ddc2a62 | 2014-02-04 15:25:19 +0100 | [diff] [blame] | 56 | |
| 57 | /* check each '/'-terminated level */ |
| 58 | while (*path) { |
| 59 | path++; |
| 60 | if (*path == '/') { |
| 61 | *path = '\0'; |
| 62 | if (strcmp(real_path(path0), work_tree) == 0) { |
| 63 | memmove(path0, path + 1, len - (path - path0)); |
| 64 | return 0; |
| 65 | } |
| 66 | *path = '/'; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* check whole path */ |
| 71 | if (strcmp(real_path(path0), work_tree) == 0) { |
| 72 | *path0 = '\0'; |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | /* |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 80 | * Normalize "path", prepending the "prefix" for relative paths. If |
| 81 | * remaining_prefix is not NULL, return the actual prefix still |
| 82 | * remains in the path. For example, prefix = sub1/sub2/ and path is |
| 83 | * |
| 84 | * foo -> sub1/sub2/foo (full prefix) |
| 85 | * ../foo -> sub1/foo (remaining prefix is sub1/) |
| 86 | * ../../bar -> bar (no remaining prefix) |
| 87 | * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix) |
| 88 | * `pwd`/../bar -> sub1/bar (no remaining prefix) |
| 89 | */ |
| 90 | char *prefix_path_gently(const char *prefix, int len, |
| 91 | int *remaining_prefix, const char *path) |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 92 | { |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 93 | const char *orig = path; |
Carlo Marcelo Arenas Belon | 18e051a | 2010-12-27 02:54:37 -0800 | [diff] [blame] | 94 | char *sanitized; |
| 95 | if (is_absolute_path(orig)) { |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 96 | sanitized = xmallocz(strlen(path)); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 97 | if (remaining_prefix) |
| 98 | *remaining_prefix = 0; |
Martin Erik Werner | 655ee9e | 2014-02-04 15:25:20 +0100 | [diff] [blame] | 99 | if (normalize_path_copy_len(sanitized, path, remaining_prefix)) { |
| 100 | free(sanitized); |
| 101 | return NULL; |
| 102 | } |
| 103 | if (abspath_part_inside_repo(sanitized)) { |
| 104 | free(sanitized); |
| 105 | return NULL; |
| 106 | } |
Carlo Marcelo Arenas Belon | 18e051a | 2010-12-27 02:54:37 -0800 | [diff] [blame] | 107 | } else { |
Junio C Hamano | 24041d6 | 2016-04-07 12:38:18 -0700 | [diff] [blame] | 108 | sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 109 | if (remaining_prefix) |
| 110 | *remaining_prefix = len; |
Martin Erik Werner | 655ee9e | 2014-02-04 15:25:20 +0100 | [diff] [blame] | 111 | if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) { |
Jeff King | 546e0fd | 2012-06-21 14:09:50 -0400 | [diff] [blame] | 112 | free(sanitized); |
| 113 | return NULL; |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 114 | } |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 115 | } |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 116 | return sanitized; |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Jeff King | 546e0fd | 2012-06-21 14:09:50 -0400 | [diff] [blame] | 119 | char *prefix_path(const char *prefix, int len, const char *path) |
| 120 | { |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 121 | char *r = prefix_path_gently(prefix, len, NULL, path); |
Jeff King | 546e0fd | 2012-06-21 14:09:50 -0400 | [diff] [blame] | 122 | if (!r) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 123 | die(_("'%s' is outside repository"), path); |
Jeff King | 546e0fd | 2012-06-21 14:09:50 -0400 | [diff] [blame] | 124 | return r; |
| 125 | } |
| 126 | |
| 127 | int path_inside_repo(const char *prefix, const char *path) |
| 128 | { |
| 129 | int len = prefix ? strlen(prefix) : 0; |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 130 | char *r = prefix_path_gently(prefix, len, NULL, path); |
Jeff King | 546e0fd | 2012-06-21 14:09:50 -0400 | [diff] [blame] | 131 | if (r) { |
| 132 | free(r); |
| 133 | return 1; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
Junio C Hamano | c6e8c80 | 2009-10-18 00:27:24 -0700 | [diff] [blame] | 138 | int check_filename(const char *prefix, const char *arg) |
| 139 | { |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 140 | char *to_free = NULL; |
Junio C Hamano | c6e8c80 | 2009-10-18 00:27:24 -0700 | [diff] [blame] | 141 | struct stat st; |
| 142 | |
Jeff King | d51c6ee | 2017-05-26 15:07:42 -0400 | [diff] [blame] | 143 | if (skip_prefix(arg, ":/", &arg)) { |
| 144 | if (!*arg) /* ":/" is root dir, always exists */ |
Nguyễn Thái Ngọc Duy | 4db86e8 | 2013-01-21 20:00:48 +0700 | [diff] [blame] | 145 | return 1; |
Jeff King | a08cbcd | 2017-05-26 15:07:31 -0400 | [diff] [blame] | 146 | prefix = NULL; |
Jeff King | 42471bc | 2017-05-26 15:08:39 -0400 | [diff] [blame] | 147 | } else if (skip_prefix(arg, ":!", &arg) || |
| 148 | skip_prefix(arg, ":^", &arg)) { |
| 149 | if (!*arg) /* excluding everything is silly, but allowed */ |
| 150 | return 1; |
Jeff King | a08cbcd | 2017-05-26 15:07:31 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (prefix) |
| 154 | arg = to_free = prefix_filename(prefix, arg); |
| 155 | |
| 156 | if (!lstat(arg, &st)) { |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 157 | free(to_free); |
Junio C Hamano | c6e8c80 | 2009-10-18 00:27:24 -0700 | [diff] [blame] | 158 | return 1; /* file exists */ |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 159 | } |
Junio C Hamano | 93dd544 | 2017-06-13 13:47:06 -0700 | [diff] [blame] | 160 | if (is_missing_file_error(errno)) { |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 161 | free(to_free); |
Junio C Hamano | c6e8c80 | 2009-10-18 00:27:24 -0700 | [diff] [blame] | 162 | return 0; /* file does not exist */ |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 163 | } |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 164 | die_errno(_("failed to stat '%s'"), arg); |
Junio C Hamano | c6e8c80 | 2009-10-18 00:27:24 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 167 | static void NORETURN die_verify_filename(const char *prefix, |
| 168 | const char *arg, |
| 169 | int diagnose_misspelt_rev) |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 170 | { |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 171 | if (!diagnose_misspelt_rev) |
Vasco Almeida | ab33a76 | 2016-06-17 20:21:06 +0000 | [diff] [blame] | 172 | die(_("%s: no such path in the working tree.\n" |
| 173 | "Use 'git <command> -- <path>...' to specify paths that do not exist locally."), |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 174 | arg); |
Junio C Hamano | 0e539dc | 2011-05-10 12:05:01 -0700 | [diff] [blame] | 175 | /* |
| 176 | * Saying "'(icase)foo' does not exist in the index" when the |
| 177 | * user gave us ":(icase)foo" is just stupid. A magic pathspec |
| 178 | * begins with a colon and is followed by a non-alnum; do not |
Junio C Hamano | 8c135ea | 2012-07-02 11:01:25 -0700 | [diff] [blame] | 179 | * let maybe_die_on_misspelt_object_name() even trigger. |
Junio C Hamano | 0e539dc | 2011-05-10 12:05:01 -0700 | [diff] [blame] | 180 | */ |
| 181 | if (!(arg[0] == ':' && !isalnum(arg[1]))) |
Junio C Hamano | 8c135ea | 2012-07-02 11:01:25 -0700 | [diff] [blame] | 182 | maybe_die_on_misspelt_object_name(arg, prefix); |
Junio C Hamano | 0e539dc | 2011-05-10 12:05:01 -0700 | [diff] [blame] | 183 | |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 184 | /* ... or fall back the most general message. */ |
Vasco Almeida | ab33a76 | 2016-06-17 20:21:06 +0000 | [diff] [blame] | 185 | die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n" |
| 186 | "Use '--' to separate paths from revisions, like this:\n" |
| 187 | "'git <command> [<revision>...] -- [<file>...]'"), arg); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 188 | |
| 189 | } |
| 190 | |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 191 | /* |
Jeff King | c99eddd | 2017-05-26 15:10:31 -0400 | [diff] [blame] | 192 | * Check for arguments that don't resolve as actual files, |
| 193 | * but which look sufficiently like pathspecs that we'll consider |
| 194 | * them such for the purposes of rev/pathspec DWIM parsing. |
| 195 | */ |
| 196 | static int looks_like_pathspec(const char *arg) |
| 197 | { |
| 198 | /* anything with a wildcard character */ |
| 199 | if (!no_wildcard(arg)) |
| 200 | return 1; |
| 201 | |
| 202 | /* long-form pathspec magic */ |
| 203 | if (starts_with(arg, ":(")) |
| 204 | return 1; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 210 | * Verify a filename that we got as an argument for a pathspec |
| 211 | * entry. Note that a filename that begins with "-" never verifies |
| 212 | * as true, because even if such a filename were to exist, we want |
| 213 | * it to be preceded by the "--" marker (or we want the user to |
| 214 | * use a format like "./-filename") |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 215 | * |
| 216 | * The "diagnose_misspelt_rev" is used to provide a user-friendly |
| 217 | * diagnosis when dying upon finding that "name" is not a pathname. |
| 218 | * If set to 1, the diagnosis will try to diagnose "name" as an |
| 219 | * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis |
| 220 | * will only complain about an inexisting file. |
| 221 | * |
| 222 | * This function is typically called to check that a "file or rev" |
| 223 | * argument is unambiguous. In this case, the caller will want |
| 224 | * diagnose_misspelt_rev == 1 when verifying the first non-rev |
| 225 | * argument (which could have been a revision), and |
| 226 | * diagnose_misspelt_rev == 0 for the next ones (because we already |
| 227 | * saw a filename, there's not ambiguity anymore). |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 228 | */ |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 229 | void verify_filename(const char *prefix, |
| 230 | const char *arg, |
| 231 | int diagnose_misspelt_rev) |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 232 | { |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 233 | if (*arg == '-') |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 234 | die(_("option '%s' must come before non-option arguments"), arg); |
Jeff King | 2cb47ab | 2017-05-26 15:10:53 -0400 | [diff] [blame] | 235 | if (looks_like_pathspec(arg) || check_filename(prefix, arg)) |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 236 | return; |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 237 | die_verify_filename(prefix, arg, diagnose_misspelt_rev); |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 240 | /* |
| 241 | * Opposite of the above: the command line did not have -- marker |
| 242 | * and we parsed the arg as a refname. It should not be interpretable |
| 243 | * as a filename. |
| 244 | */ |
| 245 | void verify_non_filename(const char *prefix, const char *arg) |
| 246 | { |
Matthias Lederhofer | 7ae3df8 | 2007-06-03 16:48:16 +0200 | [diff] [blame] | 247 | if (!is_inside_work_tree() || is_inside_git_dir()) |
Johannes Schindelin | 6802563 | 2007-01-20 03:09:34 +0100 | [diff] [blame] | 248 | return; |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 249 | if (*arg == '-') |
| 250 | return; /* flag */ |
Junio C Hamano | c6e8c80 | 2009-10-18 00:27:24 -0700 | [diff] [blame] | 251 | if (!check_filename(prefix, arg)) |
| 252 | return; |
Vasco Almeida | ab33a76 | 2016-06-17 20:21:06 +0000 | [diff] [blame] | 253 | die(_("ambiguous argument '%s': both revision and filename\n" |
| 254 | "Use '--' to separate paths from revisions, like this:\n" |
| 255 | "'git <command> [<revision>...] -- [<file>...]'"), arg); |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Nguyễn Thái Ngọc Duy | 31e26eb | 2014-11-30 15:24:44 +0700 | [diff] [blame] | 258 | int get_common_dir(struct strbuf *sb, const char *gitdir) |
Nguyễn Thái Ngọc Duy | 4dc4e14 | 2014-11-30 15:24:41 +0700 | [diff] [blame] | 259 | { |
Max Kirillov | 11f9dd7 | 2015-09-14 01:17:42 +0300 | [diff] [blame] | 260 | const char *git_env_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT); |
| 261 | if (git_env_common_dir) { |
| 262 | strbuf_addstr(sb, git_env_common_dir); |
| 263 | return 1; |
| 264 | } else { |
| 265 | return get_common_dir_noenv(sb, gitdir); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) |
| 270 | { |
Nguyễn Thái Ngọc Duy | 4dc4e14 | 2014-11-30 15:24:41 +0700 | [diff] [blame] | 271 | struct strbuf data = STRBUF_INIT; |
| 272 | struct strbuf path = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 31e26eb | 2014-11-30 15:24:44 +0700 | [diff] [blame] | 273 | int ret = 0; |
Max Kirillov | 11f9dd7 | 2015-09-14 01:17:42 +0300 | [diff] [blame] | 274 | |
Nguyễn Thái Ngọc Duy | 4dc4e14 | 2014-11-30 15:24:41 +0700 | [diff] [blame] | 275 | strbuf_addf(&path, "%s/commondir", gitdir); |
| 276 | if (file_exists(path.buf)) { |
| 277 | if (strbuf_read_file(&data, path.buf, 0) <= 0) |
| 278 | die_errno(_("failed to read %s"), path.buf); |
| 279 | while (data.len && (data.buf[data.len - 1] == '\n' || |
| 280 | data.buf[data.len - 1] == '\r')) |
| 281 | data.len--; |
| 282 | data.buf[data.len] = '\0'; |
| 283 | strbuf_reset(&path); |
| 284 | if (!is_absolute_path(data.buf)) |
| 285 | strbuf_addf(&path, "%s/", gitdir); |
| 286 | strbuf_addbuf(&path, &data); |
René Scharfe | 33ad9dd | 2017-02-25 17:00:33 +0100 | [diff] [blame] | 287 | strbuf_add_real_path(sb, path.buf); |
Nguyễn Thái Ngọc Duy | 31e26eb | 2014-11-30 15:24:44 +0700 | [diff] [blame] | 288 | ret = 1; |
Brandon Williams | 4ac9006 | 2016-12-12 10:16:55 -0800 | [diff] [blame] | 289 | } else { |
Nguyễn Thái Ngọc Duy | 4dc4e14 | 2014-11-30 15:24:41 +0700 | [diff] [blame] | 290 | strbuf_addstr(sb, gitdir); |
Brandon Williams | 4ac9006 | 2016-12-12 10:16:55 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Nguyễn Thái Ngọc Duy | 4dc4e14 | 2014-11-30 15:24:41 +0700 | [diff] [blame] | 293 | strbuf_release(&data); |
| 294 | strbuf_release(&path); |
Nguyễn Thái Ngọc Duy | 31e26eb | 2014-11-30 15:24:44 +0700 | [diff] [blame] | 295 | return ret; |
Nguyễn Thái Ngọc Duy | 4dc4e14 | 2014-11-30 15:24:41 +0700 | [diff] [blame] | 296 | } |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 297 | |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 298 | /* |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 299 | * Test if it looks like we're at a git directory. |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 300 | * We want to see: |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 301 | * |
Jim Meyering | 790296f | 2008-01-03 15:18:07 +0100 | [diff] [blame] | 302 | * - either an objects/ directory _or_ the proper |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 303 | * GIT_OBJECT_DIRECTORY environment variable |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 304 | * - a refs/ directory |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 305 | * - either a HEAD symlink or a HEAD file that is formatted as |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 306 | * a proper "ref:", or a regular file HEAD that has a properly |
| 307 | * formatted sha1 object name. |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 308 | */ |
Jeff King | b3256eb | 2012-02-02 16:59:13 -0500 | [diff] [blame] | 309 | int is_git_directory(const char *suspect) |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 310 | { |
Nguyễn Thái Ngọc Duy | 1d186b6 | 2014-11-30 15:24:40 +0700 | [diff] [blame] | 311 | struct strbuf path = STRBUF_INIT; |
| 312 | int ret = 0; |
| 313 | size_t len; |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 314 | |
Nguyễn Thái Ngọc Duy | 4dc4e14 | 2014-11-30 15:24:41 +0700 | [diff] [blame] | 315 | /* Check worktree-related signatures */ |
Jeff King | fa4d8c7 | 2017-11-03 13:58:02 +0100 | [diff] [blame] | 316 | strbuf_addstr(&path, suspect); |
| 317 | strbuf_complete(&path, '/'); |
| 318 | strbuf_addstr(&path, "HEAD"); |
Nguyễn Thái Ngọc Duy | 4dc4e14 | 2014-11-30 15:24:41 +0700 | [diff] [blame] | 319 | if (validate_headref(path.buf)) |
| 320 | goto done; |
| 321 | |
| 322 | strbuf_reset(&path); |
| 323 | get_common_dir(&path, suspect); |
Nguyễn Thái Ngọc Duy | 1d186b6 | 2014-11-30 15:24:40 +0700 | [diff] [blame] | 324 | len = path.len; |
Nguyễn Thái Ngọc Duy | 4dc4e14 | 2014-11-30 15:24:41 +0700 | [diff] [blame] | 325 | |
| 326 | /* Check non-worktree-related signatures */ |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 327 | if (getenv(DB_ENVIRONMENT)) { |
| 328 | if (access(getenv(DB_ENVIRONMENT), X_OK)) |
Nguyễn Thái Ngọc Duy | 1d186b6 | 2014-11-30 15:24:40 +0700 | [diff] [blame] | 329 | goto done; |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 330 | } |
| 331 | else { |
Nguyễn Thái Ngọc Duy | 4dc4e14 | 2014-11-30 15:24:41 +0700 | [diff] [blame] | 332 | strbuf_setlen(&path, len); |
Nguyễn Thái Ngọc Duy | 1d186b6 | 2014-11-30 15:24:40 +0700 | [diff] [blame] | 333 | strbuf_addstr(&path, "/objects"); |
| 334 | if (access(path.buf, X_OK)) |
| 335 | goto done; |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Nguyễn Thái Ngọc Duy | 1d186b6 | 2014-11-30 15:24:40 +0700 | [diff] [blame] | 338 | strbuf_setlen(&path, len); |
| 339 | strbuf_addstr(&path, "/refs"); |
| 340 | if (access(path.buf, X_OK)) |
| 341 | goto done; |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 342 | |
Nguyễn Thái Ngọc Duy | 1d186b6 | 2014-11-30 15:24:40 +0700 | [diff] [blame] | 343 | ret = 1; |
| 344 | done: |
| 345 | strbuf_release(&path); |
| 346 | return ret; |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Jeff King | ffd036b | 2016-01-22 17:27:33 -0500 | [diff] [blame] | 349 | int is_nonbare_repository_dir(struct strbuf *path) |
| 350 | { |
| 351 | int ret = 0; |
| 352 | int gitfile_error; |
| 353 | size_t orig_path_len = path->len; |
| 354 | assert(orig_path_len != 0); |
| 355 | strbuf_complete(path, '/'); |
| 356 | strbuf_addstr(path, ".git"); |
| 357 | if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf)) |
| 358 | ret = 1; |
| 359 | if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED || |
| 360 | gitfile_error == READ_GITFILE_ERR_READ_FAILED) |
| 361 | ret = 1; |
| 362 | strbuf_setlen(path, orig_path_len); |
| 363 | return ret; |
| 364 | } |
| 365 | |
Johannes Schindelin | 6802563 | 2007-01-20 03:09:34 +0100 | [diff] [blame] | 366 | int is_inside_git_dir(void) |
| 367 | { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 368 | if (inside_git_dir < 0) |
| 369 | inside_git_dir = is_inside_dir(get_git_dir()); |
| 370 | return inside_git_dir; |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 371 | } |
Johannes Schindelin | 6802563 | 2007-01-20 03:09:34 +0100 | [diff] [blame] | 372 | |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 373 | int is_inside_work_tree(void) |
| 374 | { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 375 | if (inside_work_tree < 0) |
| 376 | inside_work_tree = is_inside_dir(get_git_work_tree()); |
| 377 | return inside_work_tree; |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 378 | } |
| 379 | |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 380 | void setup_work_tree(void) |
| 381 | { |
Jeff King | 8500e0d | 2018-03-30 14:35:08 -0400 | [diff] [blame] | 382 | const char *work_tree; |
Johannes Schindelin | 354e653 | 2007-11-09 11:34:07 +0000 | [diff] [blame] | 383 | static int initialized = 0; |
| 384 | |
| 385 | if (initialized) |
| 386 | return; |
Jeff King | fada767 | 2015-05-29 02:49:10 -0400 | [diff] [blame] | 387 | |
| 388 | if (work_tree_config_is_bogus) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 389 | die(_("unable to set up work tree using invalid config")); |
Jeff King | fada767 | 2015-05-29 02:49:10 -0400 | [diff] [blame] | 390 | |
Johannes Schindelin | 354e653 | 2007-11-09 11:34:07 +0000 | [diff] [blame] | 391 | work_tree = get_git_work_tree(); |
Jeff King | 8500e0d | 2018-03-30 14:35:08 -0400 | [diff] [blame] | 392 | if (!work_tree || chdir_notify(work_tree)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 393 | die(_("this operation must be run in a work tree")); |
Nguyễn Thái Ngọc Duy | 0ed7481 | 2010-12-27 08:26:04 +0700 | [diff] [blame] | 394 | |
| 395 | /* |
| 396 | * Make sure subsequent git processes find correct worktree |
| 397 | * if $GIT_WORK_TREE is set relative |
| 398 | */ |
| 399 | if (getenv(GIT_WORK_TREE_ENVIRONMENT)) |
| 400 | setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); |
| 401 | |
Johannes Schindelin | 354e653 | 2007-11-09 11:34:07 +0000 | [diff] [blame] | 402 | initialized = 1; |
Mike Hommey | 59f0f2f | 2007-11-03 12:23:11 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Nguyễn Thái Ngọc Duy | 58b284a | 2018-10-21 16:02:28 +0200 | [diff] [blame] | 405 | static int read_worktree_config(const char *var, const char *value, void *vdata) |
| 406 | { |
| 407 | struct repository_format *data = vdata; |
| 408 | |
| 409 | if (strcmp(var, "core.bare") == 0) { |
| 410 | data->is_bare = git_config_bool(var, value); |
| 411 | } else if (strcmp(var, "core.worktree") == 0) { |
| 412 | if (!value) |
| 413 | return config_error_nonbool(var); |
| 414 | data->work_tree = xstrdup(value); |
| 415 | } |
| 416 | return 0; |
| 417 | } |
| 418 | |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 419 | static int check_repo_format(const char *var, const char *value, void *vdata) |
Nguyễn Thái Ngọc Duy | 31e26eb | 2014-11-30 15:24:44 +0700 | [diff] [blame] | 420 | { |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 421 | struct repository_format *data = vdata; |
Jeff King | 00a09d5 | 2015-06-23 06:53:58 -0400 | [diff] [blame] | 422 | const char *ext; |
| 423 | |
Nguyễn Thái Ngọc Duy | 31e26eb | 2014-11-30 15:24:44 +0700 | [diff] [blame] | 424 | if (strcmp(var, "core.repositoryformatversion") == 0) |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 425 | data->version = git_config_int(var, value); |
Jeff King | 00a09d5 | 2015-06-23 06:53:58 -0400 | [diff] [blame] | 426 | else if (skip_prefix(var, "extensions.", &ext)) { |
| 427 | /* |
| 428 | * record any known extensions here; otherwise, |
| 429 | * we fall through to recording it as unknown, and |
| 430 | * check_repository_format will complain |
| 431 | */ |
| 432 | if (!strcmp(ext, "noop")) |
| 433 | ; |
Jeff King | 067fbd4 | 2015-06-23 06:54:11 -0400 | [diff] [blame] | 434 | else if (!strcmp(ext, "preciousobjects")) |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 435 | data->precious_objects = git_config_bool(var, value); |
Jonathan Tan | 75b97fe | 2017-12-05 16:58:43 +0000 | [diff] [blame] | 436 | else if (!strcmp(ext, "partialclone")) { |
| 437 | if (!value) |
| 438 | return config_error_nonbool(var); |
| 439 | data->partial_clone = xstrdup(value); |
Nguyễn Thái Ngọc Duy | 58b284a | 2018-10-21 16:02:28 +0200 | [diff] [blame] | 440 | } else if (!strcmp(ext, "worktreeconfig")) |
| 441 | data->worktree_config = git_config_bool(var, value); |
| 442 | else |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 443 | string_list_append(&data->unknown_extensions, ext); |
Jeff King | 00a09d5 | 2015-06-23 06:53:58 -0400 | [diff] [blame] | 444 | } |
Nguyễn Thái Ngọc Duy | 58b284a | 2018-10-21 16:02:28 +0200 | [diff] [blame] | 445 | |
| 446 | return read_worktree_config(var, value, vdata); |
Nguyễn Thái Ngọc Duy | 31e26eb | 2014-11-30 15:24:44 +0700 | [diff] [blame] | 447 | } |
| 448 | |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 449 | static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok) |
Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 20:33:32 +0700 | [diff] [blame] | 450 | { |
Nguyễn Thái Ngọc Duy | 7d0fb0d | 2014-11-30 15:24:42 +0700 | [diff] [blame] | 451 | struct strbuf sb = STRBUF_INIT; |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 452 | struct strbuf err = STRBUF_INIT; |
Jeff King | 652f18e | 2016-03-11 17:37:14 -0500 | [diff] [blame] | 453 | int has_common; |
Nguyễn Thái Ngọc Duy | 337e51c | 2010-11-26 22:32:34 +0700 | [diff] [blame] | 454 | |
Jeff King | 652f18e | 2016-03-11 17:37:14 -0500 | [diff] [blame] | 455 | has_common = get_common_dir(&sb, gitdir); |
Nguyễn Thái Ngọc Duy | e61a509 | 2014-11-30 15:24:43 +0700 | [diff] [blame] | 456 | strbuf_addstr(&sb, "/config"); |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 457 | read_repository_format(candidate, sb.buf); |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 458 | strbuf_release(&sb); |
Nguyễn Thái Ngọc Duy | e61a509 | 2014-11-30 15:24:43 +0700 | [diff] [blame] | 459 | |
Nguyễn Thái Ngọc Duy | 337e51c | 2010-11-26 22:32:34 +0700 | [diff] [blame] | 460 | /* |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 461 | * For historical use of check_repository_format() in git-init, |
| 462 | * we treat a missing config as a silent "ok", even when nongit_ok |
| 463 | * is unset. |
Nguyễn Thái Ngọc Duy | 337e51c | 2010-11-26 22:32:34 +0700 | [diff] [blame] | 464 | */ |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 465 | if (candidate->version < 0) |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 466 | return 0; |
| 467 | |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 468 | if (verify_repository_format(candidate, &err) < 0) { |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 469 | if (nongit_ok) { |
| 470 | warning("%s", err.buf); |
| 471 | strbuf_release(&err); |
| 472 | *nongit_ok = -1; |
| 473 | return -1; |
| 474 | } |
| 475 | die("%s", err.buf); |
Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 20:33:32 +0700 | [diff] [blame] | 476 | } |
Jeff King | 00a09d5 | 2015-06-23 06:53:58 -0400 | [diff] [blame] | 477 | |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 478 | repository_format_precious_objects = candidate->precious_objects; |
Junio C Hamano | f3d618d | 2018-02-13 13:39:03 -0800 | [diff] [blame] | 479 | repository_format_partial_clone = candidate->partial_clone; |
Nguyễn Thái Ngọc Duy | 58b284a | 2018-10-21 16:02:28 +0200 | [diff] [blame] | 480 | repository_format_worktree_config = candidate->worktree_config; |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 481 | string_list_clear(&candidate->unknown_extensions, 0); |
Nguyễn Thái Ngọc Duy | 58b284a | 2018-10-21 16:02:28 +0200 | [diff] [blame] | 482 | |
| 483 | if (repository_format_worktree_config) { |
| 484 | /* |
| 485 | * pick up core.bare and core.worktree from per-worktree |
| 486 | * config if present |
| 487 | */ |
| 488 | strbuf_addf(&sb, "%s/config.worktree", gitdir); |
| 489 | git_config_from_file(read_worktree_config, sb.buf, candidate); |
| 490 | strbuf_release(&sb); |
| 491 | has_common = 0; |
| 492 | } |
| 493 | |
Jeff King | 652f18e | 2016-03-11 17:37:14 -0500 | [diff] [blame] | 494 | if (!has_common) { |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 495 | if (candidate->is_bare != -1) { |
| 496 | is_bare_repository_cfg = candidate->is_bare; |
Jeff King | 652f18e | 2016-03-11 17:37:14 -0500 | [diff] [blame] | 497 | if (is_bare_repository_cfg == 1) |
| 498 | inside_work_tree = -1; |
| 499 | } |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 500 | if (candidate->work_tree) { |
Jeff King | 652f18e | 2016-03-11 17:37:14 -0500 | [diff] [blame] | 501 | free(git_work_tree_cfg); |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 502 | git_work_tree_cfg = candidate->work_tree; |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 503 | inside_work_tree = -1; |
Jeff King | 652f18e | 2016-03-11 17:37:14 -0500 | [diff] [blame] | 504 | } |
| 505 | } else { |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 506 | free(candidate->work_tree); |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
Jeff King | 652f18e | 2016-03-11 17:37:14 -0500 | [diff] [blame] | 512 | int read_repository_format(struct repository_format *format, const char *path) |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 513 | { |
| 514 | memset(format, 0, sizeof(*format)); |
| 515 | format->version = -1; |
| 516 | format->is_bare = -1; |
brian m. carlson | 78a6766 | 2017-11-12 21:28:53 +0000 | [diff] [blame] | 517 | format->hash_algo = GIT_HASH_SHA1; |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 518 | string_list_init(&format->unknown_extensions, 1); |
Jeff King | 652f18e | 2016-03-11 17:37:14 -0500 | [diff] [blame] | 519 | git_config_from_file(check_repo_format, path, format); |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 520 | return format->version; |
| 521 | } |
| 522 | |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 523 | int verify_repository_format(const struct repository_format *format, |
| 524 | struct strbuf *err) |
| 525 | { |
| 526 | if (GIT_REPO_VERSION_READ < format->version) { |
Jeff King | 274db84 | 2016-03-11 17:37:22 -0500 | [diff] [blame] | 527 | strbuf_addf(err, _("Expected git repo version <= %d, found %d"), |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 528 | GIT_REPO_VERSION_READ, format->version); |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | if (format->version >= 1 && format->unknown_extensions.nr) { |
Jeff King | 00a09d5 | 2015-06-23 06:53:58 -0400 | [diff] [blame] | 533 | int i; |
| 534 | |
Jeff King | 274db84 | 2016-03-11 17:37:22 -0500 | [diff] [blame] | 535 | strbuf_addstr(err, _("unknown repository extensions found:")); |
Jeff King | 00a09d5 | 2015-06-23 06:53:58 -0400 | [diff] [blame] | 536 | |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 537 | for (i = 0; i < format->unknown_extensions.nr; i++) |
| 538 | strbuf_addf(err, "\n\t%s", |
| 539 | format->unknown_extensions.items[i].string); |
| 540 | return -1; |
Jeff King | 00a09d5 | 2015-06-23 06:53:58 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Jeff King | 2cc7c2c | 2016-03-11 17:37:07 -0500 | [diff] [blame] | 543 | return 0; |
Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 20:33:32 +0700 | [diff] [blame] | 544 | } |
| 545 | |
Stefan Beller | 5f29433 | 2017-01-24 15:56:50 -0800 | [diff] [blame] | 546 | void read_gitfile_error_die(int error_code, const char *path, const char *dir) |
| 547 | { |
| 548 | switch (error_code) { |
| 549 | case READ_GITFILE_ERR_STAT_FAILED: |
| 550 | case READ_GITFILE_ERR_NOT_A_FILE: |
| 551 | /* non-fatal; follow return path */ |
| 552 | break; |
| 553 | case READ_GITFILE_ERR_OPEN_FAILED: |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 554 | die_errno(_("error opening '%s'"), path); |
Stefan Beller | 5f29433 | 2017-01-24 15:56:50 -0800 | [diff] [blame] | 555 | case READ_GITFILE_ERR_TOO_LARGE: |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 556 | die(_("too large to be a .git file: '%s'"), path); |
Stefan Beller | 5f29433 | 2017-01-24 15:56:50 -0800 | [diff] [blame] | 557 | case READ_GITFILE_ERR_READ_FAILED: |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 558 | die(_("error reading %s"), path); |
Stefan Beller | 5f29433 | 2017-01-24 15:56:50 -0800 | [diff] [blame] | 559 | case READ_GITFILE_ERR_INVALID_FORMAT: |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 560 | die(_("invalid gitfile format: %s"), path); |
Stefan Beller | 5f29433 | 2017-01-24 15:56:50 -0800 | [diff] [blame] | 561 | case READ_GITFILE_ERR_NO_PATH: |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 562 | die(_("no path in gitfile: %s"), path); |
Stefan Beller | 5f29433 | 2017-01-24 15:56:50 -0800 | [diff] [blame] | 563 | case READ_GITFILE_ERR_NOT_A_REPO: |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 564 | die(_("not a git repository: %s"), dir); |
Stefan Beller | 5f29433 | 2017-01-24 15:56:50 -0800 | [diff] [blame] | 565 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 566 | BUG("unknown error code"); |
Stefan Beller | 5f29433 | 2017-01-24 15:56:50 -0800 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 570 | /* |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 571 | * Try to read the location of the git directory from the .git file, |
Han-Wen Nienhuys | ea1d875 | 2017-09-26 13:21:49 +0200 | [diff] [blame] | 572 | * return path to git directory if found. The return value comes from |
| 573 | * a shared buffer. |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 574 | * |
| 575 | * On failure, if return_error_code is not NULL, return_error_code |
| 576 | * will be set to an error code and NULL will be returned. If |
| 577 | * return_error_code is NULL the function will die instead (for most |
| 578 | * cases). |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 579 | */ |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 580 | const char *read_gitfile_gently(const char *path, int *return_error_code) |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 581 | { |
Erik Elfström | 921bdd9 | 2015-06-15 21:39:52 +0200 | [diff] [blame] | 582 | const int max_file_size = 1 << 20; /* 1MB */ |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 583 | int error_code = 0; |
| 584 | char *buf = NULL; |
| 585 | char *dir = NULL; |
Brad King | 40c813e | 2010-01-08 22:36:41 -0500 | [diff] [blame] | 586 | const char *slash; |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 587 | struct stat st; |
| 588 | int fd; |
Jeff King | b1905ae | 2011-05-26 12:28:44 -0400 | [diff] [blame] | 589 | ssize_t len; |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 590 | |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 591 | if (stat(path, &st)) { |
Johannes Schindelin | 5c4003c | 2017-03-13 21:12:18 +0100 | [diff] [blame] | 592 | /* NEEDSWORK: discern between ENOENT vs other errors */ |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 593 | error_code = READ_GITFILE_ERR_STAT_FAILED; |
| 594 | goto cleanup_return; |
| 595 | } |
| 596 | if (!S_ISREG(st.st_mode)) { |
| 597 | error_code = READ_GITFILE_ERR_NOT_A_FILE; |
| 598 | goto cleanup_return; |
| 599 | } |
Erik Elfström | 921bdd9 | 2015-06-15 21:39:52 +0200 | [diff] [blame] | 600 | if (st.st_size > max_file_size) { |
| 601 | error_code = READ_GITFILE_ERR_TOO_LARGE; |
| 602 | goto cleanup_return; |
| 603 | } |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 604 | fd = open(path, O_RDONLY); |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 605 | if (fd < 0) { |
| 606 | error_code = READ_GITFILE_ERR_OPEN_FAILED; |
| 607 | goto cleanup_return; |
| 608 | } |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 609 | buf = xmallocz(st.st_size); |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 610 | len = read_in_full(fd, buf, st.st_size); |
| 611 | close(fd); |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 612 | if (len != st.st_size) { |
| 613 | error_code = READ_GITFILE_ERR_READ_FAILED; |
| 614 | goto cleanup_return; |
| 615 | } |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 616 | if (!starts_with(buf, "gitdir: ")) { |
| 617 | error_code = READ_GITFILE_ERR_INVALID_FORMAT; |
| 618 | goto cleanup_return; |
| 619 | } |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 620 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') |
| 621 | len--; |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 622 | if (len < 9) { |
| 623 | error_code = READ_GITFILE_ERR_NO_PATH; |
| 624 | goto cleanup_return; |
| 625 | } |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 626 | buf[len] = '\0'; |
Brad King | 40c813e | 2010-01-08 22:36:41 -0500 | [diff] [blame] | 627 | dir = buf + 8; |
| 628 | |
| 629 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { |
| 630 | size_t pathlen = slash+1 - path; |
Jeff King | 75faa45 | 2015-09-24 17:07:03 -0400 | [diff] [blame] | 631 | dir = xstrfmt("%.*s%.*s", (int)pathlen, path, |
| 632 | (int)(len - 8), buf + 8); |
Brad King | 40c813e | 2010-01-08 22:36:41 -0500 | [diff] [blame] | 633 | free(buf); |
| 634 | buf = dir; |
| 635 | } |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 636 | if (!is_git_directory(dir)) { |
| 637 | error_code = READ_GITFILE_ERR_NOT_A_REPO; |
| 638 | goto cleanup_return; |
| 639 | } |
Carlos Martín Nieto | e2a57aa | 2011-03-17 12:26:46 +0100 | [diff] [blame] | 640 | path = real_path(dir); |
Brad King | 40c813e | 2010-01-08 22:36:41 -0500 | [diff] [blame] | 641 | |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 642 | cleanup_return: |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 643 | if (return_error_code) |
| 644 | *return_error_code = error_code; |
Stefan Beller | 5f29433 | 2017-01-24 15:56:50 -0800 | [diff] [blame] | 645 | else if (error_code) |
| 646 | read_gitfile_error_die(error_code, path, dir); |
Erik Elfström | a93beda | 2015-06-09 20:24:35 +0200 | [diff] [blame] | 647 | |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 648 | free(buf); |
Jeff King | 38ae878 | 2015-06-26 05:03:31 -0400 | [diff] [blame] | 649 | return error_code ? NULL : path; |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Jonathan Nieder | e4e3034 | 2010-07-24 06:19:44 -0500 | [diff] [blame] | 652 | static const char *setup_explicit_git_dir(const char *gitdirenv, |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 653 | struct strbuf *cwd, |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 654 | struct repository_format *repo_fmt, |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 655 | int *nongit_ok) |
Jonathan Nieder | e4e3034 | 2010-07-24 06:19:44 -0500 | [diff] [blame] | 656 | { |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 657 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
| 658 | const char *worktree; |
| 659 | char *gitfile; |
Nguyễn Thái Ngọc Duy | 9b125da | 2011-03-26 16:04:24 +0700 | [diff] [blame] | 660 | int offset; |
Jonathan Nieder | e4e3034 | 2010-07-24 06:19:44 -0500 | [diff] [blame] | 661 | |
| 662 | if (PATH_MAX - 40 < strlen(gitdirenv)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 663 | die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT); |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 664 | |
Junio C Hamano | 13d6ec9 | 2011-08-22 14:04:56 -0700 | [diff] [blame] | 665 | gitfile = (char*)read_gitfile(gitdirenv); |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 666 | if (gitfile) { |
| 667 | gitfile = xstrdup(gitfile); |
| 668 | gitdirenv = gitfile; |
| 669 | } |
| 670 | |
Jonathan Nieder | e4e3034 | 2010-07-24 06:19:44 -0500 | [diff] [blame] | 671 | if (!is_git_directory(gitdirenv)) { |
| 672 | if (nongit_ok) { |
| 673 | *nongit_ok = 1; |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 674 | free(gitfile); |
Jonathan Nieder | e4e3034 | 2010-07-24 06:19:44 -0500 | [diff] [blame] | 675 | return NULL; |
| 676 | } |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 677 | die(_("not a git repository: '%s'"), gitdirenv); |
Jonathan Nieder | e4e3034 | 2010-07-24 06:19:44 -0500 | [diff] [blame] | 678 | } |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 679 | |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 680 | if (check_repository_format_gently(gitdirenv, repo_fmt, nongit_ok)) { |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 681 | free(gitfile); |
| 682 | return NULL; |
Jonathan Nieder | e4e3034 | 2010-07-24 06:19:44 -0500 | [diff] [blame] | 683 | } |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 684 | |
| 685 | /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ |
| 686 | if (work_tree_env) |
| 687 | set_git_work_tree(work_tree_env); |
| 688 | else if (is_bare_repository_cfg > 0) { |
Jeff King | fada767 | 2015-05-29 02:49:10 -0400 | [diff] [blame] | 689 | if (git_work_tree_cfg) { |
| 690 | /* #22.2, #30 */ |
| 691 | warning("core.bare and core.worktree do not make sense"); |
| 692 | work_tree_config_is_bogus = 1; |
| 693 | } |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 694 | |
| 695 | /* #18, #26 */ |
| 696 | set_git_dir(gitdirenv); |
| 697 | free(gitfile); |
Jonathan Nieder | e4e3034 | 2010-07-24 06:19:44 -0500 | [diff] [blame] | 698 | return NULL; |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 699 | } |
| 700 | else if (git_work_tree_cfg) { /* #6, #14 */ |
| 701 | if (is_absolute_path(git_work_tree_cfg)) |
| 702 | set_git_work_tree(git_work_tree_cfg); |
| 703 | else { |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 704 | char *core_worktree; |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 705 | if (chdir(gitdirenv)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 706 | die_errno(_("cannot chdir to '%s'"), gitdirenv); |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 707 | if (chdir(git_work_tree_cfg)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 708 | die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg); |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 709 | core_worktree = xgetcwd(); |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 710 | if (chdir(cwd->buf)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 711 | die_errno(_("cannot come back to cwd")); |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 712 | set_git_work_tree(core_worktree); |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 713 | free(core_worktree); |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 714 | } |
| 715 | } |
Jeff King | 2cd83d1 | 2013-03-08 04:32:22 -0500 | [diff] [blame] | 716 | else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) { |
| 717 | /* #16d */ |
| 718 | set_git_dir(gitdirenv); |
| 719 | free(gitfile); |
| 720 | return NULL; |
| 721 | } |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 722 | else /* #2, #10 */ |
| 723 | set_git_work_tree("."); |
| 724 | |
| 725 | /* set_git_work_tree() must have been called by now */ |
| 726 | worktree = get_git_work_tree(); |
| 727 | |
| 728 | /* both get_git_work_tree() and cwd are already normalized */ |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 729 | if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */ |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 730 | set_git_dir(gitdirenv); |
| 731 | free(gitfile); |
Jonathan Nieder | e4e3034 | 2010-07-24 06:19:44 -0500 | [diff] [blame] | 732 | return NULL; |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 733 | } |
| 734 | |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 735 | offset = dir_inside_of(cwd->buf, worktree); |
Nguyễn Thái Ngọc Duy | 9b125da | 2011-03-26 16:04:24 +0700 | [diff] [blame] | 736 | if (offset >= 0) { /* cwd inside worktree? */ |
Carlos Martín Nieto | e2a57aa | 2011-03-17 12:26:46 +0100 | [diff] [blame] | 737 | set_git_dir(real_path(gitdirenv)); |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 738 | if (chdir(worktree)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 739 | die_errno(_("cannot chdir to '%s'"), worktree); |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 740 | strbuf_addch(cwd, '/'); |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 741 | free(gitfile); |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 742 | return cwd->buf + offset; |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /* cwd outside worktree */ |
| 746 | set_git_dir(gitdirenv); |
| 747 | free(gitfile); |
| 748 | return NULL; |
Jonathan Nieder | e4e3034 | 2010-07-24 06:19:44 -0500 | [diff] [blame] | 749 | } |
| 750 | |
Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 22:32:38 +0700 | [diff] [blame] | 751 | static const char *setup_discovered_git_dir(const char *gitdir, |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 752 | struct strbuf *cwd, int offset, |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 753 | struct repository_format *repo_fmt, |
Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 22:32:38 +0700 | [diff] [blame] | 754 | int *nongit_ok) |
Jonathan Nieder | 93a0054 | 2010-07-24 06:20:15 -0500 | [diff] [blame] | 755 | { |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 756 | if (check_repository_format_gently(gitdir, repo_fmt, nongit_ok)) |
Nguyễn Thái Ngọc Duy | 98937be | 2010-07-24 07:11:58 -0500 | [diff] [blame] | 757 | return NULL; |
Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 22:32:38 +0700 | [diff] [blame] | 758 | |
Jonathan Nieder | 4868b2e | 2011-01-19 06:42:30 -0600 | [diff] [blame] | 759 | /* --work-tree is set without --git-dir; use discovered one */ |
| 760 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { |
Johannes Schindelin | 2d4dcf2 | 2017-05-04 15:56:51 +0200 | [diff] [blame] | 761 | char *to_free = NULL; |
| 762 | const char *ret; |
| 763 | |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 764 | if (offset != cwd->len && !is_absolute_path(gitdir)) |
Johannes Schindelin | 2d4dcf2 | 2017-05-04 15:56:51 +0200 | [diff] [blame] | 765 | gitdir = to_free = real_pathdup(gitdir, 1); |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 766 | if (chdir(cwd->buf)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 767 | die_errno(_("cannot come back to cwd")); |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 768 | ret = setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
Johannes Schindelin | 2d4dcf2 | 2017-05-04 15:56:51 +0200 | [diff] [blame] | 769 | free(to_free); |
| 770 | return ret; |
Jonathan Nieder | 4868b2e | 2011-01-19 06:42:30 -0600 | [diff] [blame] | 771 | } |
| 772 | |
Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 22:32:38 +0700 | [diff] [blame] | 773 | /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ |
| 774 | if (is_bare_repository_cfg > 0) { |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 775 | set_git_dir(offset == cwd->len ? gitdir : real_path(gitdir)); |
| 776 | if (chdir(cwd->buf)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 777 | die_errno(_("cannot come back to cwd")); |
Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 22:32:38 +0700 | [diff] [blame] | 778 | return NULL; |
Jonathan Nieder | 93a0054 | 2010-07-24 06:20:15 -0500 | [diff] [blame] | 779 | } |
Jonathan Nieder | 93a0054 | 2010-07-24 06:20:15 -0500 | [diff] [blame] | 780 | |
Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 22:32:38 +0700 | [diff] [blame] | 781 | /* #0, #1, #5, #8, #9, #12, #13 */ |
| 782 | set_git_work_tree("."); |
| 783 | if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) |
| 784 | set_git_dir(gitdir); |
Nguyễn Thái Ngọc Duy | 98937be | 2010-07-24 07:11:58 -0500 | [diff] [blame] | 785 | inside_git_dir = 0; |
Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 22:32:38 +0700 | [diff] [blame] | 786 | inside_work_tree = 1; |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 787 | if (offset == cwd->len) |
Nguyễn Thái Ngọc Duy | 98937be | 2010-07-24 07:11:58 -0500 | [diff] [blame] | 788 | return NULL; |
| 789 | |
Johannes Schindelin | df380d5 | 2017-03-13 21:09:44 +0100 | [diff] [blame] | 790 | /* Make "offset" point past the '/' (already the case for root dirs) */ |
| 791 | if (offset != offset_1st_component(cwd->buf)) |
| 792 | offset++; |
| 793 | /* Add a '/' at the end */ |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 794 | strbuf_addch(cwd, '/'); |
| 795 | return cwd->buf + offset; |
Nguyễn Thái Ngọc Duy | 98937be | 2010-07-24 07:11:58 -0500 | [diff] [blame] | 796 | } |
| 797 | |
Nguyễn Thái Ngọc Duy | 1cd8031 | 2010-11-26 22:32:36 +0700 | [diff] [blame] | 798 | /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 799 | static const char *setup_bare_git_dir(struct strbuf *cwd, int offset, |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 800 | struct repository_format *repo_fmt, |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 801 | int *nongit_ok) |
Jonathan Nieder | 68698da | 2010-07-24 06:25:32 -0500 | [diff] [blame] | 802 | { |
| 803 | int root_len; |
| 804 | |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 805 | if (check_repository_format_gently(".", repo_fmt, nongit_ok)) |
Nguyễn Thái Ngọc Duy | 1cd8031 | 2010-11-26 22:32:36 +0700 | [diff] [blame] | 806 | return NULL; |
| 807 | |
Jeff King | 2cd83d1 | 2013-03-08 04:32:22 -0500 | [diff] [blame] | 808 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
| 809 | |
Jonathan Nieder | 4868b2e | 2011-01-19 06:42:30 -0600 | [diff] [blame] | 810 | /* --work-tree is set without --git-dir; use discovered one */ |
| 811 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { |
Johannes Schindelin | da6f847 | 2017-05-04 15:56:47 +0200 | [diff] [blame] | 812 | static const char *gitdir; |
Jonathan Nieder | 4868b2e | 2011-01-19 06:42:30 -0600 | [diff] [blame] | 813 | |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 814 | gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset); |
| 815 | if (chdir(cwd->buf)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 816 | die_errno(_("cannot come back to cwd")); |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 817 | return setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
Jonathan Nieder | 4868b2e | 2011-01-19 06:42:30 -0600 | [diff] [blame] | 818 | } |
| 819 | |
Jonathan Nieder | 68698da | 2010-07-24 06:25:32 -0500 | [diff] [blame] | 820 | inside_git_dir = 1; |
Nguyễn Thái Ngọc Duy | 1cd8031 | 2010-11-26 22:32:36 +0700 | [diff] [blame] | 821 | inside_work_tree = 0; |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 822 | if (offset != cwd->len) { |
| 823 | if (chdir(cwd->buf)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 824 | die_errno(_("cannot come back to cwd")); |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 825 | root_len = offset_1st_component(cwd->buf); |
| 826 | strbuf_setlen(cwd, offset > root_len ? offset : root_len); |
| 827 | set_git_dir(cwd->buf); |
Nguyễn Thái Ngọc Duy | 337e51c | 2010-11-26 22:32:34 +0700 | [diff] [blame] | 828 | } |
Nguyễn Thái Ngọc Duy | 1cd8031 | 2010-11-26 22:32:36 +0700 | [diff] [blame] | 829 | else |
Jonathan Nieder | 68698da | 2010-07-24 06:25:32 -0500 | [diff] [blame] | 830 | set_git_dir("."); |
Jonathan Nieder | 68698da | 2010-07-24 06:25:32 -0500 | [diff] [blame] | 831 | return NULL; |
| 832 | } |
| 833 | |
Jonathan Nieder | f161ede | 2010-07-24 06:26:41 -0500 | [diff] [blame] | 834 | static const char *setup_nongit(const char *cwd, int *nongit_ok) |
| 835 | { |
| 836 | if (!nongit_ok) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 837 | die(_("not a git repository (or any of the parent directories): %s"), DEFAULT_GIT_DIR_ENVIRONMENT); |
Jonathan Nieder | f161ede | 2010-07-24 06:26:41 -0500 | [diff] [blame] | 838 | if (chdir(cwd)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 839 | die_errno(_("cannot come back to cwd")); |
Jonathan Nieder | f161ede | 2010-07-24 06:26:41 -0500 | [diff] [blame] | 840 | *nongit_ok = 1; |
| 841 | return NULL; |
| 842 | } |
| 843 | |
Clemens Buchacher | 2565b43 | 2012-04-13 01:11:36 +0200 | [diff] [blame] | 844 | static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len) |
Jonathan Nieder | 60c98d1 | 2010-07-24 06:27:58 -0500 | [diff] [blame] | 845 | { |
| 846 | struct stat buf; |
Clemens Buchacher | 2565b43 | 2012-04-13 01:11:36 +0200 | [diff] [blame] | 847 | if (stat(path, &buf)) { |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 848 | die_errno(_("failed to stat '%*s%s%s'"), |
Clemens Buchacher | 2565b43 | 2012-04-13 01:11:36 +0200 | [diff] [blame] | 849 | prefix_len, |
Jonathan Nieder | 60c98d1 | 2010-07-24 06:27:58 -0500 | [diff] [blame] | 850 | prefix ? prefix : "", |
| 851 | prefix ? "/" : "", path); |
Clemens Buchacher | 2565b43 | 2012-04-13 01:11:36 +0200 | [diff] [blame] | 852 | } |
Jonathan Nieder | 60c98d1 | 2010-07-24 06:27:58 -0500 | [diff] [blame] | 853 | return buf.st_dev; |
| 854 | } |
| 855 | |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 856 | /* |
Michael Haggerty | 1b77d83 | 2012-10-28 17:16:26 +0100 | [diff] [blame] | 857 | * A "string_list_each_func_t" function that canonicalizes an entry |
| 858 | * from GIT_CEILING_DIRECTORIES using real_path_if_valid(), or |
Michael Haggerty | 7ec30aa | 2013-02-20 10:09:24 +0100 | [diff] [blame] | 859 | * discards it if unusable. The presence of an empty entry in |
| 860 | * GIT_CEILING_DIRECTORIES turns off canonicalization for all |
| 861 | * subsequent entries. |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 862 | */ |
Michael Haggerty | 1b77d83 | 2012-10-28 17:16:26 +0100 | [diff] [blame] | 863 | static int canonicalize_ceiling_entry(struct string_list_item *item, |
Michael Haggerty | 7ec30aa | 2013-02-20 10:09:24 +0100 | [diff] [blame] | 864 | void *cb_data) |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 865 | { |
Michael Haggerty | 7ec30aa | 2013-02-20 10:09:24 +0100 | [diff] [blame] | 866 | int *empty_entry_found = cb_data; |
Michael Haggerty | 1b77d83 | 2012-10-28 17:16:26 +0100 | [diff] [blame] | 867 | char *ceil = item->string; |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 868 | |
Michael Haggerty | 7ec30aa | 2013-02-20 10:09:24 +0100 | [diff] [blame] | 869 | if (!*ceil) { |
| 870 | *empty_entry_found = 1; |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 871 | return 0; |
Michael Haggerty | 7ec30aa | 2013-02-20 10:09:24 +0100 | [diff] [blame] | 872 | } else if (!is_absolute_path(ceil)) { |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 873 | return 0; |
Michael Haggerty | 7ec30aa | 2013-02-20 10:09:24 +0100 | [diff] [blame] | 874 | } else if (*empty_entry_found) { |
| 875 | /* Keep entry but do not canonicalize it */ |
| 876 | return 1; |
| 877 | } else { |
Johannes Schindelin | ce83ead | 2017-03-08 16:43:40 +0100 | [diff] [blame] | 878 | char *real_path = real_pathdup(ceil, 0); |
Brandon Williams | 4ac9006 | 2016-12-12 10:16:55 -0800 | [diff] [blame] | 879 | if (!real_path) { |
Michael Haggerty | 7ec30aa | 2013-02-20 10:09:24 +0100 | [diff] [blame] | 880 | return 0; |
Brandon Williams | 4ac9006 | 2016-12-12 10:16:55 -0800 | [diff] [blame] | 881 | } |
Michael Haggerty | 7ec30aa | 2013-02-20 10:09:24 +0100 | [diff] [blame] | 882 | free(item->string); |
Brandon Williams | 4ac9006 | 2016-12-12 10:16:55 -0800 | [diff] [blame] | 883 | item->string = real_path; |
Michael Haggerty | 7ec30aa | 2013-02-20 10:09:24 +0100 | [diff] [blame] | 884 | return 1; |
| 885 | } |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 886 | } |
| 887 | |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 888 | enum discovery_result { |
| 889 | GIT_DIR_NONE = 0, |
| 890 | GIT_DIR_EXPLICIT, |
| 891 | GIT_DIR_DISCOVERED, |
| 892 | GIT_DIR_BARE, |
| 893 | /* these are errors */ |
| 894 | GIT_DIR_HIT_CEILING = -1, |
Johannes Schindelin | 01017dc | 2017-03-13 21:11:22 +0100 | [diff] [blame] | 895 | GIT_DIR_HIT_MOUNT_POINT = -2, |
| 896 | GIT_DIR_INVALID_GITFILE = -3 |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 897 | }; |
| 898 | |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 899 | /* |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 900 | * We cannot decide in this function whether we are in the work tree or |
| 901 | * not, since the config can only be read _after_ this function was called. |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 902 | * |
| 903 | * Also, we avoid changing any global state (such as the current working |
| 904 | * directory) to allow early callers. |
| 905 | * |
| 906 | * The directory where the search should start needs to be passed in via the |
| 907 | * `dir` parameter; upon return, the `dir` buffer will contain the path of |
| 908 | * the directory where the search ended, and `gitdir` will contain the path of |
| 909 | * the discovered .git/ directory, if any. If `gitdir` is not absolute, it |
| 910 | * is relative to `dir` (i.e. *not* necessarily the cwd). |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 911 | */ |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 912 | static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir, |
Johannes Schindelin | 01017dc | 2017-03-13 21:11:22 +0100 | [diff] [blame] | 913 | struct strbuf *gitdir, |
| 914 | int die_on_error) |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 915 | { |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 916 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 917 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 918 | const char *gitdirenv; |
| 919 | int ceil_offset = -1, min_offset = has_dos_drive_prefix(dir->buf) ? 3 : 1; |
Raja R Harinath | c7d1d1b | 2010-07-13 14:32:00 +0530 | [diff] [blame] | 920 | dev_t current_device = 0; |
| 921 | int one_filesystem = 1; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 922 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 923 | /* |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 924 | * If GIT_DIR is set explicitly, we're not going |
| 925 | * to do any discovery, but we still do repository |
| 926 | * validation. |
| 927 | */ |
| 928 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
| 929 | if (gitdirenv) { |
| 930 | strbuf_addstr(gitdir, gitdirenv); |
| 931 | return GIT_DIR_EXPLICIT; |
| 932 | } |
| 933 | |
| 934 | if (env_ceiling_dirs) { |
| 935 | int empty_entry_found = 0; |
| 936 | |
| 937 | string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1); |
| 938 | filter_string_list(&ceiling_dirs, 0, |
| 939 | canonicalize_ceiling_entry, &empty_entry_found); |
| 940 | ceil_offset = longest_ancestor_length(dir->buf, &ceiling_dirs); |
| 941 | string_list_clear(&ceiling_dirs, 0); |
| 942 | } |
| 943 | |
| 944 | if (ceil_offset < 0) |
| 945 | ceil_offset = min_offset - 2; |
| 946 | |
| 947 | /* |
| 948 | * Test in the following order (relative to the dir): |
| 949 | * - .git (file containing "gitdir: <path>") |
| 950 | * - .git/ |
| 951 | * - ./ (bare) |
| 952 | * - ../.git |
| 953 | * - ../.git/ |
| 954 | * - ../ (bare) |
Junio C Hamano | bdae4af | 2017-12-19 11:33:58 -0800 | [diff] [blame] | 955 | * - ../../.git |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 956 | * etc. |
| 957 | */ |
| 958 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
| 959 | if (one_filesystem) |
| 960 | current_device = get_device_or_die(dir->buf, NULL, 0); |
| 961 | for (;;) { |
Johannes Schindelin | 01017dc | 2017-03-13 21:11:22 +0100 | [diff] [blame] | 962 | int offset = dir->len, error_code = 0; |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 963 | |
| 964 | if (offset > min_offset) |
| 965 | strbuf_addch(dir, '/'); |
| 966 | strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT); |
Johannes Schindelin | 01017dc | 2017-03-13 21:11:22 +0100 | [diff] [blame] | 967 | gitdirenv = read_gitfile_gently(dir->buf, die_on_error ? |
| 968 | NULL : &error_code); |
| 969 | if (!gitdirenv) { |
| 970 | if (die_on_error || |
| 971 | error_code == READ_GITFILE_ERR_NOT_A_FILE) { |
Johannes Schindelin | 5c4003c | 2017-03-13 21:12:18 +0100 | [diff] [blame] | 972 | /* NEEDSWORK: fail if .git is not file nor dir */ |
Johannes Schindelin | 01017dc | 2017-03-13 21:11:22 +0100 | [diff] [blame] | 973 | if (is_git_directory(dir->buf)) |
| 974 | gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; |
| 975 | } else if (error_code != READ_GITFILE_ERR_STAT_FAILED) |
| 976 | return GIT_DIR_INVALID_GITFILE; |
| 977 | } |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 978 | strbuf_setlen(dir, offset); |
| 979 | if (gitdirenv) { |
| 980 | strbuf_addstr(gitdir, gitdirenv); |
| 981 | return GIT_DIR_DISCOVERED; |
| 982 | } |
| 983 | |
| 984 | if (is_git_directory(dir->buf)) { |
| 985 | strbuf_addstr(gitdir, "."); |
| 986 | return GIT_DIR_BARE; |
| 987 | } |
| 988 | |
| 989 | if (offset <= min_offset) |
| 990 | return GIT_DIR_HIT_CEILING; |
| 991 | |
| 992 | while (--offset > ceil_offset && !is_dir_sep(dir->buf[offset])) |
| 993 | ; /* continue */ |
| 994 | if (offset <= ceil_offset) |
| 995 | return GIT_DIR_HIT_CEILING; |
| 996 | |
| 997 | strbuf_setlen(dir, offset > min_offset ? offset : min_offset); |
| 998 | if (one_filesystem && |
| 999 | current_device != get_device_or_die(dir->buf, NULL, offset)) |
| 1000 | return GIT_DIR_HIT_MOUNT_POINT; |
| 1001 | } |
| 1002 | } |
| 1003 | |
Brandon Williams | d3fb71b | 2017-06-14 11:07:37 -0700 | [diff] [blame] | 1004 | int discover_git_directory(struct strbuf *commondir, |
| 1005 | struct strbuf *gitdir) |
Johannes Schindelin | 16ac8b8 | 2017-03-13 21:10:45 +0100 | [diff] [blame] | 1006 | { |
| 1007 | struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT; |
| 1008 | size_t gitdir_offset = gitdir->len, cwd_len; |
Brandon Williams | d3fb71b | 2017-06-14 11:07:37 -0700 | [diff] [blame] | 1009 | size_t commondir_offset = commondir->len; |
Johannes Schindelin | 16ac8b8 | 2017-03-13 21:10:45 +0100 | [diff] [blame] | 1010 | struct repository_format candidate; |
| 1011 | |
| 1012 | if (strbuf_getcwd(&dir)) |
Brandon Williams | d3fb71b | 2017-06-14 11:07:37 -0700 | [diff] [blame] | 1013 | return -1; |
Johannes Schindelin | 16ac8b8 | 2017-03-13 21:10:45 +0100 | [diff] [blame] | 1014 | |
| 1015 | cwd_len = dir.len; |
Johannes Schindelin | 01017dc | 2017-03-13 21:11:22 +0100 | [diff] [blame] | 1016 | if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) { |
Johannes Schindelin | 16ac8b8 | 2017-03-13 21:10:45 +0100 | [diff] [blame] | 1017 | strbuf_release(&dir); |
Brandon Williams | d3fb71b | 2017-06-14 11:07:37 -0700 | [diff] [blame] | 1018 | return -1; |
Johannes Schindelin | 16ac8b8 | 2017-03-13 21:10:45 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /* |
| 1022 | * The returned gitdir is relative to dir, and if dir does not reflect |
| 1023 | * the current working directory, we simply make the gitdir absolute. |
| 1024 | */ |
| 1025 | if (dir.len < cwd_len && !is_absolute_path(gitdir->buf + gitdir_offset)) { |
| 1026 | /* Avoid a trailing "/." */ |
| 1027 | if (!strcmp(".", gitdir->buf + gitdir_offset)) |
| 1028 | strbuf_setlen(gitdir, gitdir_offset); |
| 1029 | else |
| 1030 | strbuf_addch(&dir, '/'); |
| 1031 | strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len); |
| 1032 | } |
| 1033 | |
Brandon Williams | d3fb71b | 2017-06-14 11:07:37 -0700 | [diff] [blame] | 1034 | get_common_dir(commondir, gitdir->buf + gitdir_offset); |
| 1035 | |
Johannes Schindelin | 16ac8b8 | 2017-03-13 21:10:45 +0100 | [diff] [blame] | 1036 | strbuf_reset(&dir); |
Brandon Williams | d3fb71b | 2017-06-14 11:07:37 -0700 | [diff] [blame] | 1037 | strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset); |
Johannes Schindelin | 16ac8b8 | 2017-03-13 21:10:45 +0100 | [diff] [blame] | 1038 | read_repository_format(&candidate, dir.buf); |
| 1039 | strbuf_release(&dir); |
| 1040 | |
| 1041 | if (verify_repository_format(&candidate, &err) < 0) { |
| 1042 | warning("ignoring git dir '%s': %s", |
| 1043 | gitdir->buf + gitdir_offset, err.buf); |
| 1044 | strbuf_release(&err); |
Brandon Williams | d3fb71b | 2017-06-14 11:07:37 -0700 | [diff] [blame] | 1045 | strbuf_setlen(commondir, commondir_offset); |
Johannes Schindelin | 69743f9 | 2017-06-14 13:35:26 +0200 | [diff] [blame] | 1046 | strbuf_setlen(gitdir, gitdir_offset); |
Brandon Williams | d3fb71b | 2017-06-14 11:07:37 -0700 | [diff] [blame] | 1047 | return -1; |
Johannes Schindelin | 16ac8b8 | 2017-03-13 21:10:45 +0100 | [diff] [blame] | 1048 | } |
| 1049 | |
Brandon Williams | d3fb71b | 2017-06-14 11:07:37 -0700 | [diff] [blame] | 1050 | return 0; |
Johannes Schindelin | 16ac8b8 | 2017-03-13 21:10:45 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 1053 | const char *setup_git_directory_gently(int *nongit_ok) |
| 1054 | { |
| 1055 | static struct strbuf cwd = STRBUF_INIT; |
| 1056 | struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT; |
Brandon Williams | f9ee2fc | 2017-08-02 12:49:23 -0700 | [diff] [blame] | 1057 | const char *prefix; |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 1058 | struct repository_format repo_fmt; |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 1059 | |
| 1060 | /* |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1061 | * We may have read an incomplete configuration before |
| 1062 | * setting-up the git directory. If so, clear the cache so |
| 1063 | * that the next queries to the configuration reload complete |
| 1064 | * configuration (including the per-repo config file that we |
| 1065 | * ignored previously). |
| 1066 | */ |
| 1067 | git_config_clear(); |
| 1068 | |
| 1069 | /* |
SZEDER Gábor | af05d67 | 2008-03-25 22:06:26 +0100 | [diff] [blame] | 1070 | * Let's assume that we are in a git repository. |
| 1071 | * If it turns out later that we are somewhere else, the value will be |
| 1072 | * updated accordingly. |
| 1073 | */ |
| 1074 | if (nongit_ok) |
| 1075 | *nongit_ok = 0; |
| 1076 | |
René Scharfe | 7333ed1 | 2014-07-28 20:26:40 +0200 | [diff] [blame] | 1077 | if (strbuf_getcwd(&cwd)) |
Vasco Almeida | 2ff30e6 | 2016-08-08 11:15:59 +0000 | [diff] [blame] | 1078 | die_errno(_("Unable to read current working directory")); |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 1079 | strbuf_addbuf(&dir, &cwd); |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 1080 | |
Johannes Schindelin | 01017dc | 2017-03-13 21:11:22 +0100 | [diff] [blame] | 1081 | switch (setup_git_directory_gently_1(&dir, &gitdir, 1)) { |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 1082 | case GIT_DIR_NONE: |
| 1083 | prefix = NULL; |
| 1084 | break; |
| 1085 | case GIT_DIR_EXPLICIT: |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 1086 | prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok); |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 1087 | break; |
| 1088 | case GIT_DIR_DISCOVERED: |
| 1089 | if (dir.len < cwd.len && chdir(dir.buf)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 1090 | die(_("cannot change to '%s'"), dir.buf); |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 1091 | prefix = setup_discovered_git_dir(gitdir.buf, &cwd, dir.len, |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 1092 | &repo_fmt, nongit_ok); |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 1093 | break; |
| 1094 | case GIT_DIR_BARE: |
| 1095 | if (dir.len < cwd.len && chdir(dir.buf)) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 1096 | die(_("cannot change to '%s'"), dir.buf); |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 1097 | prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok); |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 1098 | break; |
| 1099 | case GIT_DIR_HIT_CEILING: |
| 1100 | prefix = setup_nongit(cwd.buf, nongit_ok); |
| 1101 | break; |
| 1102 | case GIT_DIR_HIT_MOUNT_POINT: |
| 1103 | if (nongit_ok) { |
| 1104 | *nongit_ok = 1; |
| 1105 | strbuf_release(&cwd); |
| 1106 | strbuf_release(&dir); |
| 1107 | return NULL; |
| 1108 | } |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 1109 | die(_("not a git repository (or any parent up to mount point %s)\n" |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 1110 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."), |
| 1111 | dir.buf); |
| 1112 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1113 | BUG("unhandled setup_git_directory_1() result"); |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
David Aguilar | 1f5d271 | 2011-05-25 20:37:12 -0700 | [diff] [blame] | 1116 | if (prefix) |
Jeff King | a6f7f9a | 2013-03-08 04:30:25 -0500 | [diff] [blame] | 1117 | setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1); |
David Aguilar | 1f5d271 | 2011-05-25 20:37:12 -0700 | [diff] [blame] | 1118 | else |
Jeff King | a6f7f9a | 2013-03-08 04:30:25 -0500 | [diff] [blame] | 1119 | setenv(GIT_PREFIX_ENVIRONMENT, "", 1); |
David Aguilar | 1f5d271 | 2011-05-25 20:37:12 -0700 | [diff] [blame] | 1120 | |
Jeff King | 46c3cd4 | 2016-03-05 17:10:27 -0500 | [diff] [blame] | 1121 | startup_info->have_repository = !nongit_ok || !*nongit_ok; |
| 1122 | startup_info->prefix = prefix; |
| 1123 | |
Brandon Williams | 73f192c | 2017-06-20 12:19:32 -0700 | [diff] [blame] | 1124 | /* |
| 1125 | * Not all paths through the setup code will call 'set_git_dir()' (which |
| 1126 | * directly sets up the environment) so in order to guarantee that the |
| 1127 | * environment is in a consistent state after setup, explicitly setup |
| 1128 | * the environment if we have a repository. |
| 1129 | * |
| 1130 | * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some |
| 1131 | * code paths so we also need to explicitly setup the environment if |
| 1132 | * the user has set GIT_DIR. It may be beneficial to disallow bogus |
| 1133 | * GIT_DIR values at some point in the future. |
| 1134 | */ |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 1135 | if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT)) { |
| 1136 | if (!the_repository->gitdir) { |
| 1137 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); |
| 1138 | if (!gitdir) |
| 1139 | gitdir = DEFAULT_GIT_DIR_ENVIRONMENT; |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 1140 | setup_git_env(gitdir); |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 1141 | } |
brian m. carlson | 78a6766 | 2017-11-12 21:28:53 +0000 | [diff] [blame] | 1142 | if (startup_info->have_repository) |
| 1143 | repo_set_hash_algo(the_repository, repo_fmt.hash_algo); |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 1144 | } |
Brandon Williams | 73f192c | 2017-06-20 12:19:32 -0700 | [diff] [blame] | 1145 | |
Johannes Schindelin | ce9b8aa | 2017-03-13 21:10:42 +0100 | [diff] [blame] | 1146 | strbuf_release(&dir); |
| 1147 | strbuf_release(&gitdir); |
| 1148 | |
Nguyễn Thái Ngọc Duy | a60645f | 2010-08-05 21:46:33 -0500 | [diff] [blame] | 1149 | return prefix; |
| 1150 | } |
| 1151 | |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 1152 | int git_config_perm(const char *var, const char *value) |
| 1153 | { |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 1154 | int i; |
| 1155 | char *endptr; |
| 1156 | |
| 1157 | if (value == NULL) |
| 1158 | return PERM_GROUP; |
| 1159 | |
| 1160 | if (!strcmp(value, "umask")) |
| 1161 | return PERM_UMASK; |
| 1162 | if (!strcmp(value, "group")) |
| 1163 | return PERM_GROUP; |
| 1164 | if (!strcmp(value, "all") || |
| 1165 | !strcmp(value, "world") || |
| 1166 | !strcmp(value, "everybody")) |
| 1167 | return PERM_EVERYBODY; |
| 1168 | |
| 1169 | /* Parse octal numbers */ |
| 1170 | i = strtol(value, &endptr, 8); |
| 1171 | |
| 1172 | /* If not an octal number, maybe true/false? */ |
| 1173 | if (*endptr != 0) |
| 1174 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; |
| 1175 | |
| 1176 | /* |
| 1177 | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 1178 | * a chmod value to restrict to. |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 1179 | */ |
| 1180 | switch (i) { |
| 1181 | case PERM_UMASK: /* 0 */ |
| 1182 | return PERM_UMASK; |
| 1183 | case OLD_PERM_GROUP: /* 1 */ |
| 1184 | return PERM_GROUP; |
| 1185 | case OLD_PERM_EVERYBODY: /* 2 */ |
| 1186 | return PERM_EVERYBODY; |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 1187 | } |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 1188 | |
| 1189 | /* A filemode value was given: 0xxx */ |
| 1190 | |
| 1191 | if ((i & 0600) != 0600) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 1192 | die(_("problem with core.sharedRepository filemode value " |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 1193 | "(0%.3o).\nThe owner of files must always have " |
Vasco Almeida | 2ff30e6 | 2016-08-08 11:15:59 +0000 | [diff] [blame] | 1194 | "read and write permissions."), i); |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 1195 | |
| 1196 | /* |
| 1197 | * Mask filemode value. Others can not get write permission. |
| 1198 | * x flags for directories are handled separately. |
| 1199 | */ |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 1200 | return -(i & 0666); |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Jeff King | 4b0d1ee | 2016-03-11 17:36:45 -0500 | [diff] [blame] | 1203 | void check_repository_format(void) |
Junio C Hamano | ab9cb76 | 2005-11-25 15:59:09 -0800 | [diff] [blame] | 1204 | { |
brian m. carlson | abade65 | 2017-11-12 21:28:51 +0000 | [diff] [blame] | 1205 | struct repository_format repo_fmt; |
| 1206 | check_repository_format_gently(get_git_dir(), &repo_fmt, NULL); |
Jeff King | f1c126b | 2016-03-05 17:11:34 -0500 | [diff] [blame] | 1207 | startup_info->have_repository = 1; |
Junio C Hamano | ab9cb76 | 2005-11-25 15:59:09 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
Clemens Buchacher | e1e5ec8 | 2010-06-05 10:04:20 +0200 | [diff] [blame] | 1210 | /* |
| 1211 | * Returns the "prefix", a path to the current working directory |
| 1212 | * relative to the work tree root, or NULL, if the current working |
| 1213 | * directory is not a strict subdirectory of the work tree root. The |
| 1214 | * prefix always ends with a '/' character. |
| 1215 | */ |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 1216 | const char *setup_git_directory(void) |
| 1217 | { |
Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 22:32:39 +0700 | [diff] [blame] | 1218 | return setup_git_directory_gently(NULL); |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 1219 | } |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 1220 | |
Stefan Beller | 40d9632 | 2017-01-24 15:56:49 -0800 | [diff] [blame] | 1221 | const char *resolve_gitdir_gently(const char *suspect, int *return_error_code) |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 1222 | { |
| 1223 | if (is_git_directory(suspect)) |
| 1224 | return suspect; |
Stefan Beller | 40d9632 | 2017-01-24 15:56:49 -0800 | [diff] [blame] | 1225 | return read_gitfile_gently(suspect, return_error_code); |
Fredrik Gustafsson | abc0682 | 2011-08-15 23:17:46 +0200 | [diff] [blame] | 1226 | } |
Thomas Rast | 1d999dd | 2013-07-16 11:27:36 +0200 | [diff] [blame] | 1227 | |
| 1228 | /* if any standard file descriptor is missing open it to /dev/null */ |
| 1229 | void sanitize_stdfds(void) |
| 1230 | { |
| 1231 | int fd = open("/dev/null", O_RDWR, 0); |
| 1232 | while (fd != -1 && fd < 2) |
| 1233 | fd = dup(fd); |
| 1234 | if (fd == -1) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 1235 | die_errno(_("open /dev/null or dup failed")); |
Thomas Rast | 1d999dd | 2013-07-16 11:27:36 +0200 | [diff] [blame] | 1236 | if (fd > 2) |
| 1237 | close(fd); |
| 1238 | } |
Nguyễn Thái Ngọc Duy | de0957c | 2014-02-08 14:08:51 +0700 | [diff] [blame] | 1239 | |
| 1240 | int daemonize(void) |
| 1241 | { |
| 1242 | #ifdef NO_POSIX_GOODIES |
| 1243 | errno = ENOSYS; |
| 1244 | return -1; |
| 1245 | #else |
| 1246 | switch (fork()) { |
| 1247 | case 0: |
| 1248 | break; |
| 1249 | case -1: |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 1250 | die_errno(_("fork failed")); |
Nguyễn Thái Ngọc Duy | de0957c | 2014-02-08 14:08:51 +0700 | [diff] [blame] | 1251 | default: |
| 1252 | exit(0); |
| 1253 | } |
| 1254 | if (setsid() == -1) |
Alexander Shopov | fc045fe | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 1255 | die_errno(_("setsid failed")); |
Nguyễn Thái Ngọc Duy | de0957c | 2014-02-08 14:08:51 +0700 | [diff] [blame] | 1256 | close(0); |
| 1257 | close(1); |
| 1258 | close(2); |
| 1259 | sanitize_stdfds(); |
| 1260 | return 0; |
| 1261 | #endif |
| 1262 | } |