| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 1 | #include "cache.h" |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 2 | #include "dir.h" |
| 3 | |
| 4 | static int inside_git_dir = -1; |
| 5 | static int inside_work_tree = -1; |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 6 | |
| Nguyễn Thái Ngọc Duy | edc54fb | 2010-11-11 14:08:03 | [diff] [blame] | 7 | char *prefix_path(const char *prefix, int len, const char *path) |
| Linus Torvalds | f332726 | 2005-08-17 03:44:32 | [diff] [blame] | 8 | { |
| Junio C Hamano | 6b5ee13 | 2005-09-21 07:00:47 | [diff] [blame] | 9 | const char *orig = path; |
| Carlo Marcelo Arenas Belon | 18e051a | 2010-12-27 10:54:37 | [diff] [blame] | 10 | char *sanitized; |
| 11 | if (is_absolute_path(orig)) { |
| Carlos Martín Nieto | e2a57aa | 2011-03-17 11:26:46 | [diff] [blame] | 12 | const char *temp = real_path(path); |
| Carlo Marcelo Arenas Belon | 18e051a | 2010-12-27 10:54:37 | [diff] [blame] | 13 | sanitized = xmalloc(len + strlen(temp) + 1); |
| 14 | strcpy(sanitized, temp); |
| 15 | } else { |
| 16 | sanitized = xmalloc(len + strlen(path) + 1); |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 17 | if (len) |
| 18 | memcpy(sanitized, prefix, len); |
| 19 | strcpy(sanitized + len, path); |
| Linus Torvalds | f332726 | 2005-08-17 03:44:32 | [diff] [blame] | 20 | } |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 21 | if (normalize_path_copy(sanitized, sanitized)) |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 22 | goto error_out; |
| Johannes Sixt | 9a13ba1 | 2008-02-19 21:29:40 | [diff] [blame] | 23 | if (is_absolute_path(orig)) { |
| Nguyễn Thái Ngọc Duy | 72ec8ba | 2010-02-14 15:44:44 | [diff] [blame] | 24 | size_t root_len, len, total; |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 25 | const char *work_tree = get_git_work_tree(); |
| Junio C Hamano | b3100fd | 2009-12-16 18:50:09 | [diff] [blame] | 26 | if (!work_tree) |
| 27 | goto error_out; |
| 28 | len = strlen(work_tree); |
| Nguyễn Thái Ngọc Duy | 72ec8ba | 2010-02-14 15:44:44 | [diff] [blame] | 29 | root_len = offset_1st_component(work_tree); |
| Junio C Hamano | b3100fd | 2009-12-16 18:50:09 | [diff] [blame] | 30 | total = strlen(sanitized) + 1; |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 31 | if (strncmp(sanitized, work_tree, len) || |
| Nguyễn Thái Ngọc Duy | 72ec8ba | 2010-02-14 15:44:44 | [diff] [blame] | 32 | (len > root_len && sanitized[len] != '\0' && sanitized[len] != '/')) { |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 33 | error_out: |
| Dmitry Potapov | 62525ef | 2008-10-05 00:40:36 | [diff] [blame] | 34 | die("'%s' is outside repository", orig); |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 35 | } |
| 36 | if (sanitized[len] == '/') |
| 37 | len++; |
| 38 | memmove(sanitized, sanitized + len, total - len); |
| Linus Torvalds | f332726 | 2005-08-17 03:44:32 | [diff] [blame] | 39 | } |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 40 | return sanitized; |
| Linus Torvalds | f332726 | 2005-08-17 03:44:32 | [diff] [blame] | 41 | } |
| 42 | |
| Junio C Hamano | a6080a0 | 2007-06-07 07:04:01 | [diff] [blame] | 43 | /* |
| Junio C Hamano | 4ca0660 | 2005-11-26 07:14:15 | [diff] [blame] | 44 | * Unlike prefix_path, this should be used if the named file does |
| 45 | * not have to interact with index entry; i.e. name of a random file |
| 46 | * on the filesystem. |
| 47 | */ |
| 48 | const char *prefix_filename(const char *pfx, int pfx_len, const char *arg) |
| 49 | { |
| 50 | static char path[PATH_MAX]; |
| Frank Li | 71064e3 | 2009-09-16 08:20:22 | [diff] [blame] | 51 | #ifndef WIN32 |
| Thomas Rast | 9e5f5d4 | 2010-10-17 19:23:21 | [diff] [blame] | 52 | if (!pfx_len || is_absolute_path(arg)) |
| Junio C Hamano | 4ca0660 | 2005-11-26 07:14:15 | [diff] [blame] | 53 | return arg; |
| 54 | memcpy(path, pfx, pfx_len); |
| 55 | strcpy(path + pfx_len, arg); |
| Johannes Sixt | 25fe217 | 2008-03-05 20:51:27 | [diff] [blame] | 56 | #else |
| 57 | char *p; |
| 58 | /* don't add prefix to absolute paths, but still replace '\' by '/' */ |
| 59 | if (is_absolute_path(arg)) |
| 60 | pfx_len = 0; |
| Thomas Rast | 9e5f5d4 | 2010-10-17 19:23:21 | [diff] [blame] | 61 | else if (pfx_len) |
| Johannes Sixt | 25fe217 | 2008-03-05 20:51:27 | [diff] [blame] | 62 | memcpy(path, pfx, pfx_len); |
| 63 | strcpy(path + pfx_len, arg); |
| 64 | for (p = path + pfx_len; *p; p++) |
| 65 | if (*p == '\\') |
| 66 | *p = '/'; |
| 67 | #endif |
| Junio C Hamano | 4ca0660 | 2005-11-26 07:14:15 | [diff] [blame] | 68 | return path; |
| 69 | } |
| 70 | |
| Junio C Hamano | c6e8c80 | 2009-10-18 07:27:24 | [diff] [blame] | 71 | int check_filename(const char *prefix, const char *arg) |
| 72 | { |
| 73 | const char *name; |
| 74 | struct stat st; |
| 75 | |
| 76 | name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg; |
| 77 | if (!lstat(name, &st)) |
| 78 | return 1; /* file exists */ |
| 79 | if (errno == ENOENT || errno == ENOTDIR) |
| 80 | return 0; /* file does not exist */ |
| 81 | die_errno("failed to stat '%s'", arg); |
| 82 | } |
| 83 | |
| Matthieu Moy | 009fee4 | 2009-12-07 10:10:50 | [diff] [blame] | 84 | static void NORETURN die_verify_filename(const char *prefix, const char *arg) |
| 85 | { |
| 86 | unsigned char sha1[20]; |
| 87 | unsigned mode; |
| 88 | /* try a detailed diagnostic ... */ |
| 89 | get_sha1_with_mode_1(arg, sha1, &mode, 0, prefix); |
| 90 | /* ... or fall back the most general message. */ |
| 91 | die("ambiguous argument '%s': unknown revision or path not in the working tree.\n" |
| 92 | "Use '--' to separate paths from revisions", arg); |
| 93 | |
| 94 | } |
| 95 | |
| Linus Torvalds | e23d0b4 | 2006-04-26 17:15:54 | [diff] [blame] | 96 | /* |
| 97 | * Verify a filename that we got as an argument for a pathspec |
| 98 | * entry. Note that a filename that begins with "-" never verifies |
| 99 | * as true, because even if such a filename were to exist, we want |
| 100 | * it to be preceded by the "--" marker (or we want the user to |
| 101 | * use a format like "./-filename") |
| 102 | */ |
| 103 | void verify_filename(const char *prefix, const char *arg) |
| 104 | { |
| Linus Torvalds | e23d0b4 | 2006-04-26 17:15:54 | [diff] [blame] | 105 | if (*arg == '-') |
| 106 | die("bad flag '%s' used after filename", arg); |
| Junio C Hamano | c6e8c80 | 2009-10-18 07:27:24 | [diff] [blame] | 107 | if (check_filename(prefix, arg)) |
| Linus Torvalds | e23d0b4 | 2006-04-26 17:15:54 | [diff] [blame] | 108 | return; |
| Matthieu Moy | 009fee4 | 2009-12-07 10:10:50 | [diff] [blame] | 109 | die_verify_filename(prefix, arg); |
| Linus Torvalds | e23d0b4 | 2006-04-26 17:15:54 | [diff] [blame] | 110 | } |
| 111 | |
| Junio C Hamano | ea92f41 | 2006-04-26 22:09:27 | [diff] [blame] | 112 | /* |
| 113 | * Opposite of the above: the command line did not have -- marker |
| 114 | * and we parsed the arg as a refname. It should not be interpretable |
| 115 | * as a filename. |
| 116 | */ |
| 117 | void verify_non_filename(const char *prefix, const char *arg) |
| 118 | { |
| Matthias Lederhofer | 7ae3df8 | 2007-06-03 14:48:16 | [diff] [blame] | 119 | if (!is_inside_work_tree() || is_inside_git_dir()) |
| Johannes Schindelin | 6802563 | 2007-01-20 02:09:34 | [diff] [blame] | 120 | return; |
| Junio C Hamano | ea92f41 | 2006-04-26 22:09:27 | [diff] [blame] | 121 | if (*arg == '-') |
| 122 | return; /* flag */ |
| Junio C Hamano | c6e8c80 | 2009-10-18 07:27:24 | [diff] [blame] | 123 | if (!check_filename(prefix, arg)) |
| 124 | return; |
| 125 | die("ambiguous argument '%s': both revision and filename\n" |
| 126 | "Use '--' to separate filenames from revisions", arg); |
| Junio C Hamano | ea92f41 | 2006-04-26 22:09:27 | [diff] [blame] | 127 | } |
| 128 | |
| Junio C Hamano | 6b5ee13 | 2005-09-21 07:00:47 | [diff] [blame] | 129 | const char **get_pathspec(const char *prefix, const char **pathspec) |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 130 | { |
| Junio C Hamano | 6b5ee13 | 2005-09-21 07:00:47 | [diff] [blame] | 131 | const char *entry = *pathspec; |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 132 | const char **src, **dst; |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 133 | int prefixlen; |
| 134 | |
| Linus Torvalds | f332726 | 2005-08-17 03:44:32 | [diff] [blame] | 135 | if (!prefix && !entry) |
| 136 | return NULL; |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 137 | |
| 138 | if (!entry) { |
| 139 | static const char *spec[2]; |
| 140 | spec[0] = prefix; |
| 141 | spec[1] = NULL; |
| 142 | return spec; |
| 143 | } |
| 144 | |
| 145 | /* Otherwise we have to re-write the entries.. */ |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 146 | src = pathspec; |
| 147 | dst = pathspec; |
| Linus Torvalds | f332726 | 2005-08-17 03:44:32 | [diff] [blame] | 148 | prefixlen = prefix ? strlen(prefix) : 0; |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 149 | while (*src) { |
| 150 | const char *p = prefix_path(prefix, prefixlen, *src); |
| Dmitry Potapov | 62525ef | 2008-10-05 00:40:36 | [diff] [blame] | 151 | *(dst++) = p; |
| Junio C Hamano | d089eba | 2008-01-29 06:44:27 | [diff] [blame] | 152 | src++; |
| 153 | } |
| 154 | *dst = NULL; |
| 155 | if (!*pathspec) |
| 156 | return NULL; |
| 157 | return pathspec; |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 158 | } |
| 159 | |
| Linus Torvalds | 5f5608b | 2005-08-27 20:54:42 | [diff] [blame] | 160 | /* |
| Shawn O. Pearce | ad1a382 | 2006-12-31 04:30:19 | [diff] [blame] | 161 | * Test if it looks like we're at a git directory. |
| Junio C Hamano | 5e7bfe2 | 2005-11-25 23:43:41 | [diff] [blame] | 162 | * We want to see: |
| Linus Torvalds | 5f5608b | 2005-08-27 20:54:42 | [diff] [blame] | 163 | * |
| Jim Meyering | 790296f | 2008-01-03 14:18:07 | [diff] [blame] | 164 | * - either an objects/ directory _or_ the proper |
| Linus Torvalds | 5f5608b | 2005-08-27 20:54:42 | [diff] [blame] | 165 | * GIT_OBJECT_DIRECTORY environment variable |
| Shawn O. Pearce | ad1a382 | 2006-12-31 04:30:19 | [diff] [blame] | 166 | * - a refs/ directory |
| Junio C Hamano | 8098a17 | 2005-09-30 21:26:57 | [diff] [blame] | 167 | * - either a HEAD symlink or a HEAD file that is formatted as |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 168 | * a proper "ref:", or a regular file HEAD that has a properly |
| 169 | * formatted sha1 object name. |
| Linus Torvalds | 5f5608b | 2005-08-27 20:54:42 | [diff] [blame] | 170 | */ |
| Shawn O. Pearce | ad1a382 | 2006-12-31 04:30:19 | [diff] [blame] | 171 | static int is_git_directory(const char *suspect) |
| Linus Torvalds | 5f5608b | 2005-08-27 20:54:42 | [diff] [blame] | 172 | { |
| Shawn O. Pearce | ad1a382 | 2006-12-31 04:30:19 | [diff] [blame] | 173 | char path[PATH_MAX]; |
| 174 | size_t len = strlen(suspect); |
| 175 | |
| Greg Brockman | 3c9d041 | 2010-07-20 04:46:21 | [diff] [blame] | 176 | if (PATH_MAX <= len + strlen("/objects")) |
| 177 | die("Too long path: %.*s", 60, suspect); |
| Shawn O. Pearce | ad1a382 | 2006-12-31 04:30:19 | [diff] [blame] | 178 | strcpy(path, suspect); |
| 179 | if (getenv(DB_ENVIRONMENT)) { |
| 180 | if (access(getenv(DB_ENVIRONMENT), X_OK)) |
| 181 | return 0; |
| 182 | } |
| 183 | else { |
| 184 | strcpy(path + len, "/objects"); |
| 185 | if (access(path, X_OK)) |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | strcpy(path + len, "/refs"); |
| 190 | if (access(path, X_OK)) |
| Junio C Hamano | 8098a17 | 2005-09-30 21:26:57 | [diff] [blame] | 191 | return 0; |
| Shawn O. Pearce | ad1a382 | 2006-12-31 04:30:19 | [diff] [blame] | 192 | |
| 193 | strcpy(path + len, "/HEAD"); |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 194 | if (validate_headref(path)) |
| Shawn O. Pearce | ad1a382 | 2006-12-31 04:30:19 | [diff] [blame] | 195 | return 0; |
| 196 | |
| Junio C Hamano | 8098a17 | 2005-09-30 21:26:57 | [diff] [blame] | 197 | return 1; |
| Linus Torvalds | 5f5608b | 2005-08-27 20:54:42 | [diff] [blame] | 198 | } |
| 199 | |
| Johannes Schindelin | 6802563 | 2007-01-20 02:09:34 | [diff] [blame] | 200 | int is_inside_git_dir(void) |
| 201 | { |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 202 | if (inside_git_dir < 0) |
| 203 | inside_git_dir = is_inside_dir(get_git_dir()); |
| 204 | return inside_git_dir; |
| Matthias Lederhofer | 892c41b | 2007-06-06 07:10:42 | [diff] [blame] | 205 | } |
| Johannes Schindelin | 6802563 | 2007-01-20 02:09:34 | [diff] [blame] | 206 | |
| Matthias Lederhofer | 892c41b | 2007-06-06 07:10:42 | [diff] [blame] | 207 | int is_inside_work_tree(void) |
| 208 | { |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 209 | if (inside_work_tree < 0) |
| 210 | inside_work_tree = is_inside_dir(get_git_work_tree()); |
| 211 | return inside_work_tree; |
| Matthias Lederhofer | 892c41b | 2007-06-06 07:10:42 | [diff] [blame] | 212 | } |
| 213 | |
| Junio C Hamano | f3fa183 | 2007-11-08 23:35:32 | [diff] [blame] | 214 | void setup_work_tree(void) |
| 215 | { |
| Johannes Schindelin | 354e653 | 2007-11-09 11:34:07 | [diff] [blame] | 216 | const char *work_tree, *git_dir; |
| 217 | static int initialized = 0; |
| 218 | |
| 219 | if (initialized) |
| 220 | return; |
| 221 | work_tree = get_git_work_tree(); |
| 222 | git_dir = get_git_dir(); |
| Mike Hommey | 59f0f2f | 2007-11-03 11:23:11 | [diff] [blame] | 223 | if (!is_absolute_path(git_dir)) |
| Carlos Martín Nieto | e2a57aa | 2011-03-17 11:26:46 | [diff] [blame] | 224 | git_dir = real_path(get_git_dir()); |
| Mike Hommey | 59f0f2f | 2007-11-03 11:23:11 | [diff] [blame] | 225 | if (!work_tree || chdir(work_tree)) |
| 226 | die("This operation must be run in a work tree"); |
| Nguyễn Thái Ngọc Duy | 0ed7481 | 2010-12-27 01:26:04 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * Make sure subsequent git processes find correct worktree |
| 230 | * if $GIT_WORK_TREE is set relative |
| 231 | */ |
| 232 | if (getenv(GIT_WORK_TREE_ENVIRONMENT)) |
| 233 | setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); |
| 234 | |
| Carlos Martín Nieto | e2a57aa | 2011-03-17 11:26:46 | [diff] [blame] | 235 | set_git_dir(relative_path(git_dir, work_tree)); |
| Johannes Schindelin | 354e653 | 2007-11-09 11:34:07 | [diff] [blame] | 236 | initialized = 1; |
| Mike Hommey | 59f0f2f | 2007-11-03 11:23:11 | [diff] [blame] | 237 | } |
| 238 | |
| Nguyễn Thái Ngọc Duy | 337e51c | 2010-11-26 15:32:34 | [diff] [blame] | 239 | static int check_repository_format_gently(const char *gitdir, int *nongit_ok) |
| Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 13:33:32 | [diff] [blame] | 240 | { |
| Nguyễn Thái Ngọc Duy | 337e51c | 2010-11-26 15:32:34 | [diff] [blame] | 241 | char repo_config[PATH_MAX+1]; |
| 242 | |
| 243 | /* |
| 244 | * git_config() can't be used here because it calls git_pathdup() |
| 245 | * to get $GIT_CONFIG/config. That call will make setup_git_env() |
| 246 | * set git_dir to ".git". |
| 247 | * |
| 248 | * We are in gitdir setup, no git dir has been found useable yet. |
| 249 | * Use a gentler version of git_config() to check if this repo |
| 250 | * is a good one. |
| 251 | */ |
| 252 | snprintf(repo_config, PATH_MAX, "%s/config", gitdir); |
| 253 | git_config_early(check_repository_format_version, NULL, repo_config); |
| Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 13:33:32 | [diff] [blame] | 254 | if (GIT_REPO_VERSION < repository_format_version) { |
| 255 | if (!nongit_ok) |
| 256 | die ("Expected git repo version <= %d, found %d", |
| 257 | GIT_REPO_VERSION, repository_format_version); |
| 258 | warning("Expected git repo version <= %d, found %d", |
| 259 | GIT_REPO_VERSION, repository_format_version); |
| 260 | warning("Please upgrade Git"); |
| 261 | *nongit_ok = -1; |
| 262 | return -1; |
| 263 | } |
| 264 | return 0; |
| 265 | } |
| 266 | |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 267 | /* |
| Lars Hjemli | b44ebb1 | 2008-02-20 22:13:13 | [diff] [blame] | 268 | * Try to read the location of the git directory from the .git file, |
| 269 | * return path to git directory if found. |
| 270 | */ |
| 271 | const char *read_gitfile_gently(const char *path) |
| 272 | { |
| 273 | char *buf; |
| Brad King | 40c813e | 2010-01-09 03:36:41 | [diff] [blame] | 274 | char *dir; |
| 275 | const char *slash; |
| Lars Hjemli | b44ebb1 | 2008-02-20 22:13:13 | [diff] [blame] | 276 | struct stat st; |
| 277 | int fd; |
| 278 | size_t len; |
| 279 | |
| 280 | if (stat(path, &st)) |
| 281 | return NULL; |
| 282 | if (!S_ISREG(st.st_mode)) |
| 283 | return NULL; |
| 284 | fd = open(path, O_RDONLY); |
| 285 | if (fd < 0) |
| Thomas Rast | d824cbb | 2009-06-27 15:58:46 | [diff] [blame] | 286 | die_errno("Error opening '%s'", path); |
| Lars Hjemli | b44ebb1 | 2008-02-20 22:13:13 | [diff] [blame] | 287 | buf = xmalloc(st.st_size + 1); |
| 288 | len = read_in_full(fd, buf, st.st_size); |
| 289 | close(fd); |
| 290 | if (len != st.st_size) |
| 291 | die("Error reading %s", path); |
| 292 | buf[len] = '\0'; |
| 293 | if (prefixcmp(buf, "gitdir: ")) |
| 294 | die("Invalid gitfile format: %s", path); |
| 295 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') |
| 296 | len--; |
| 297 | if (len < 9) |
| 298 | die("No path in gitfile: %s", path); |
| 299 | buf[len] = '\0'; |
| Brad King | 40c813e | 2010-01-09 03:36:41 | [diff] [blame] | 300 | dir = buf + 8; |
| 301 | |
| 302 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { |
| 303 | size_t pathlen = slash+1 - path; |
| 304 | size_t dirlen = pathlen + len - 8; |
| 305 | dir = xmalloc(dirlen + 1); |
| 306 | strncpy(dir, path, pathlen); |
| 307 | strncpy(dir + pathlen, buf + 8, len - 8); |
| 308 | dir[dirlen] = '\0'; |
| 309 | free(buf); |
| 310 | buf = dir; |
| 311 | } |
| 312 | |
| 313 | if (!is_git_directory(dir)) |
| 314 | die("Not a git repository: %s", dir); |
| Carlos Martín Nieto | e2a57aa | 2011-03-17 11:26:46 | [diff] [blame] | 315 | path = real_path(dir); |
| Brad King | 40c813e | 2010-01-09 03:36:41 | [diff] [blame] | 316 | |
| Lars Hjemli | b44ebb1 | 2008-02-20 22:13:13 | [diff] [blame] | 317 | free(buf); |
| 318 | return path; |
| 319 | } |
| 320 | |
| Jonathan Nieder | e4e3034 | 2010-07-24 11:19:44 | [diff] [blame] | 321 | static const char *setup_explicit_git_dir(const char *gitdirenv, |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 322 | char *cwd, int len, |
| 323 | int *nongit_ok) |
| Jonathan Nieder | e4e3034 | 2010-07-24 11:19:44 | [diff] [blame] | 324 | { |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 325 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
| 326 | const char *worktree; |
| 327 | char *gitfile; |
| Jonathan Nieder | e4e3034 | 2010-07-24 11:19:44 | [diff] [blame] | 328 | |
| 329 | if (PATH_MAX - 40 < strlen(gitdirenv)) |
| 330 | die("'$%s' too big", GIT_DIR_ENVIRONMENT); |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 331 | |
| 332 | gitfile = (char*)read_gitfile_gently(gitdirenv); |
| 333 | if (gitfile) { |
| 334 | gitfile = xstrdup(gitfile); |
| 335 | gitdirenv = gitfile; |
| 336 | } |
| 337 | |
| Jonathan Nieder | e4e3034 | 2010-07-24 11:19:44 | [diff] [blame] | 338 | if (!is_git_directory(gitdirenv)) { |
| 339 | if (nongit_ok) { |
| 340 | *nongit_ok = 1; |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 341 | free(gitfile); |
| Jonathan Nieder | e4e3034 | 2010-07-24 11:19:44 | [diff] [blame] | 342 | return NULL; |
| 343 | } |
| 344 | die("Not a git repository: '%s'", gitdirenv); |
| 345 | } |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 346 | |
| 347 | if (check_repository_format_gently(gitdirenv, nongit_ok)) { |
| 348 | free(gitfile); |
| 349 | return NULL; |
| Jonathan Nieder | e4e3034 | 2010-07-24 11:19:44 | [diff] [blame] | 350 | } |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 351 | |
| 352 | /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ |
| 353 | if (work_tree_env) |
| 354 | set_git_work_tree(work_tree_env); |
| 355 | else if (is_bare_repository_cfg > 0) { |
| 356 | if (git_work_tree_cfg) /* #22.2, #30 */ |
| 357 | die("core.bare and core.worktree do not make sense"); |
| 358 | |
| 359 | /* #18, #26 */ |
| 360 | set_git_dir(gitdirenv); |
| 361 | free(gitfile); |
| Jonathan Nieder | e4e3034 | 2010-07-24 11:19:44 | [diff] [blame] | 362 | return NULL; |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 363 | } |
| 364 | else if (git_work_tree_cfg) { /* #6, #14 */ |
| 365 | if (is_absolute_path(git_work_tree_cfg)) |
| 366 | set_git_work_tree(git_work_tree_cfg); |
| 367 | else { |
| 368 | char core_worktree[PATH_MAX]; |
| 369 | if (chdir(gitdirenv)) |
| 370 | die_errno("Could not chdir to '%s'", gitdirenv); |
| 371 | if (chdir(git_work_tree_cfg)) |
| 372 | die_errno("Could not chdir to '%s'", git_work_tree_cfg); |
| 373 | if (!getcwd(core_worktree, PATH_MAX)) |
| 374 | die_errno("Could not get directory '%s'", git_work_tree_cfg); |
| 375 | if (chdir(cwd)) |
| 376 | die_errno("Could not come back to cwd"); |
| 377 | set_git_work_tree(core_worktree); |
| 378 | } |
| 379 | } |
| 380 | else /* #2, #10 */ |
| 381 | set_git_work_tree("."); |
| 382 | |
| 383 | /* set_git_work_tree() must have been called by now */ |
| 384 | worktree = get_git_work_tree(); |
| 385 | |
| 386 | /* both get_git_work_tree() and cwd are already normalized */ |
| 387 | if (!strcmp(cwd, worktree)) { /* cwd == worktree */ |
| 388 | set_git_dir(gitdirenv); |
| 389 | free(gitfile); |
| Jonathan Nieder | e4e3034 | 2010-07-24 11:19:44 | [diff] [blame] | 390 | return NULL; |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | if (!prefixcmp(cwd, worktree) && |
| 394 | cwd[strlen(worktree)] == '/') { /* cwd inside worktree */ |
| Carlos Martín Nieto | e2a57aa | 2011-03-17 11:26:46 | [diff] [blame] | 395 | set_git_dir(real_path(gitdirenv)); |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 396 | if (chdir(worktree)) |
| 397 | die_errno("Could not chdir to '%s'", worktree); |
| 398 | cwd[len++] = '/'; |
| 399 | cwd[len] = '\0'; |
| 400 | free(gitfile); |
| 401 | return cwd + strlen(worktree) + 1; |
| 402 | } |
| 403 | |
| 404 | /* cwd outside worktree */ |
| 405 | set_git_dir(gitdirenv); |
| 406 | free(gitfile); |
| 407 | return NULL; |
| Jonathan Nieder | e4e3034 | 2010-07-24 11:19:44 | [diff] [blame] | 408 | } |
| 409 | |
| Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 15:32:38 | [diff] [blame] | 410 | static const char *setup_discovered_git_dir(const char *gitdir, |
| 411 | char *cwd, int offset, int len, |
| 412 | int *nongit_ok) |
| Jonathan Nieder | 93a0054 | 2010-07-24 11:20:15 | [diff] [blame] | 413 | { |
| Nguyễn Thái Ngọc Duy | 337e51c | 2010-11-26 15:32:34 | [diff] [blame] | 414 | if (check_repository_format_gently(gitdir, nongit_ok)) |
| Nguyễn Thái Ngọc Duy | 98937be | 2010-07-24 12:11:58 | [diff] [blame] | 415 | return NULL; |
| Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 15:32:38 | [diff] [blame] | 416 | |
| Jonathan Nieder | 4868b2e | 2011-01-19 12:42:30 | [diff] [blame] | 417 | /* --work-tree is set without --git-dir; use discovered one */ |
| 418 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { |
| 419 | if (offset != len && !is_absolute_path(gitdir)) |
| Carlos Martín Nieto | e2a57aa | 2011-03-17 11:26:46 | [diff] [blame] | 420 | gitdir = xstrdup(real_path(gitdir)); |
| Jonathan Nieder | 4868b2e | 2011-01-19 12:42:30 | [diff] [blame] | 421 | if (chdir(cwd)) |
| 422 | die_errno("Could not come back to cwd"); |
| 423 | return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok); |
| 424 | } |
| 425 | |
| Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 15:32:38 | [diff] [blame] | 426 | /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ |
| 427 | if (is_bare_repository_cfg > 0) { |
| Carlos Martín Nieto | e2a57aa | 2011-03-17 11:26:46 | [diff] [blame] | 428 | set_git_dir(offset == len ? gitdir : real_path(gitdir)); |
| Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 15:32:38 | [diff] [blame] | 429 | if (chdir(cwd)) |
| 430 | die_errno("Could not come back to cwd"); |
| 431 | return NULL; |
| Jonathan Nieder | 93a0054 | 2010-07-24 11:20:15 | [diff] [blame] | 432 | } |
| Jonathan Nieder | 93a0054 | 2010-07-24 11:20:15 | [diff] [blame] | 433 | |
| Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 15:32:38 | [diff] [blame] | 434 | /* #0, #1, #5, #8, #9, #12, #13 */ |
| 435 | set_git_work_tree("."); |
| 436 | if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) |
| 437 | set_git_dir(gitdir); |
| Nguyễn Thái Ngọc Duy | 98937be | 2010-07-24 12:11:58 | [diff] [blame] | 438 | inside_git_dir = 0; |
| Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 15:32:38 | [diff] [blame] | 439 | inside_work_tree = 1; |
| Nguyễn Thái Ngọc Duy | 98937be | 2010-07-24 12:11:58 | [diff] [blame] | 440 | if (offset == len) |
| 441 | return NULL; |
| 442 | |
| 443 | /* Make "offset" point to past the '/', and add a '/' at the end */ |
| 444 | offset++; |
| 445 | cwd[len++] = '/'; |
| 446 | cwd[len] = 0; |
| 447 | return cwd + offset; |
| 448 | } |
| 449 | |
| Nguyễn Thái Ngọc Duy | 1cd8031 | 2010-11-26 15:32:36 | [diff] [blame] | 450 | /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ |
| 451 | static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok) |
| Jonathan Nieder | 68698da | 2010-07-24 11:25:32 | [diff] [blame] | 452 | { |
| 453 | int root_len; |
| 454 | |
| Nguyễn Thái Ngọc Duy | 1cd8031 | 2010-11-26 15:32:36 | [diff] [blame] | 455 | if (check_repository_format_gently(".", nongit_ok)) |
| 456 | return NULL; |
| 457 | |
| Jonathan Nieder | 4868b2e | 2011-01-19 12:42:30 | [diff] [blame] | 458 | /* --work-tree is set without --git-dir; use discovered one */ |
| 459 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { |
| 460 | const char *gitdir; |
| 461 | |
| 462 | gitdir = offset == len ? "." : xmemdupz(cwd, offset); |
| 463 | if (chdir(cwd)) |
| 464 | die_errno("Could not come back to cwd"); |
| 465 | return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok); |
| 466 | } |
| 467 | |
| Jonathan Nieder | 68698da | 2010-07-24 11:25:32 | [diff] [blame] | 468 | inside_git_dir = 1; |
| Nguyễn Thái Ngọc Duy | 1cd8031 | 2010-11-26 15:32:36 | [diff] [blame] | 469 | inside_work_tree = 0; |
| Jonathan Nieder | 68698da | 2010-07-24 11:25:32 | [diff] [blame] | 470 | if (offset != len) { |
| Nguyễn Thái Ngọc Duy | 8fc0ae8 | 2010-07-24 11:29:41 | [diff] [blame] | 471 | if (chdir(cwd)) |
| 472 | die_errno("Cannot come back to cwd"); |
| Jonathan Nieder | 68698da | 2010-07-24 11:25:32 | [diff] [blame] | 473 | root_len = offset_1st_component(cwd); |
| 474 | cwd[offset > root_len ? offset : root_len] = '\0'; |
| 475 | set_git_dir(cwd); |
| Nguyễn Thái Ngọc Duy | 337e51c | 2010-11-26 15:32:34 | [diff] [blame] | 476 | } |
| Nguyễn Thái Ngọc Duy | 1cd8031 | 2010-11-26 15:32:36 | [diff] [blame] | 477 | else |
| Jonathan Nieder | 68698da | 2010-07-24 11:25:32 | [diff] [blame] | 478 | set_git_dir("."); |
| Jonathan Nieder | 68698da | 2010-07-24 11:25:32 | [diff] [blame] | 479 | return NULL; |
| 480 | } |
| 481 | |
| Jonathan Nieder | f161ede | 2010-07-24 11:26:41 | [diff] [blame] | 482 | static const char *setup_nongit(const char *cwd, int *nongit_ok) |
| 483 | { |
| 484 | if (!nongit_ok) |
| 485 | die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT); |
| 486 | if (chdir(cwd)) |
| 487 | die_errno("Cannot come back to cwd"); |
| 488 | *nongit_ok = 1; |
| 489 | return NULL; |
| 490 | } |
| 491 | |
| Jonathan Nieder | 60c98d1 | 2010-07-24 11:27:58 | [diff] [blame] | 492 | static dev_t get_device_or_die(const char *path, const char *prefix) |
| 493 | { |
| 494 | struct stat buf; |
| 495 | if (stat(path, &buf)) |
| 496 | die_errno("failed to stat '%s%s%s'", |
| 497 | prefix ? prefix : "", |
| 498 | prefix ? "/" : "", path); |
| 499 | return buf.st_dev; |
| 500 | } |
| 501 | |
| Lars Hjemli | b44ebb1 | 2008-02-20 22:13:13 | [diff] [blame] | 502 | /* |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 503 | * We cannot decide in this function whether we are in the work tree or |
| 504 | * not, since the config can only be read _after_ this function was called. |
| 505 | */ |
| Nguyễn Thái Ngọc Duy | a60645f | 2010-08-06 02:46:33 | [diff] [blame] | 506 | static const char *setup_git_directory_gently_1(int *nongit_ok) |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 507 | { |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 508 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 509 | static char cwd[PATH_MAX+1]; |
| Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 15:32:38 | [diff] [blame] | 510 | const char *gitdirenv, *ret; |
| 511 | char *gitfile; |
| Nguyễn Thái Ngọc Duy | 98937be | 2010-07-24 12:11:58 | [diff] [blame] | 512 | int len, offset, ceil_offset; |
| Raja R Harinath | c7d1d1b | 2010-07-13 09:02:00 | [diff] [blame] | 513 | dev_t current_device = 0; |
| 514 | int one_filesystem = 1; |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 515 | |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 516 | /* |
| SZEDER Gábor | af05d67 | 2008-03-25 21:06:26 | [diff] [blame] | 517 | * Let's assume that we are in a git repository. |
| 518 | * If it turns out later that we are somewhere else, the value will be |
| 519 | * updated accordingly. |
| 520 | */ |
| 521 | if (nongit_ok) |
| 522 | *nongit_ok = 0; |
| 523 | |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 524 | if (!getcwd(cwd, sizeof(cwd)-1)) |
| 525 | die_errno("Unable to read current working directory"); |
| 526 | offset = len = strlen(cwd); |
| 527 | |
| SZEDER Gábor | af05d67 | 2008-03-25 21:06:26 | [diff] [blame] | 528 | /* |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 529 | * If GIT_DIR is set explicitly, we're not going |
| 530 | * to do any discovery, but we still do repository |
| 531 | * validation. |
| 532 | */ |
| Shawn O. Pearce | ad1a382 | 2006-12-31 04:30:19 | [diff] [blame] | 533 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
| Jonathan Nieder | e4e3034 | 2010-07-24 11:19:44 | [diff] [blame] | 534 | if (gitdirenv) |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 535 | return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok); |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 536 | |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 537 | ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs); |
| Junio C Hamano | 17d778e | 2008-07-07 09:17:23 | [diff] [blame] | 538 | if (ceil_offset < 0 && has_dos_drive_prefix(cwd)) |
| 539 | ceil_offset = 1; |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 540 | |
| Matthias Lederhofer | 892c41b | 2007-06-06 07:10:42 | [diff] [blame] | 541 | /* |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 542 | * Test in the following order (relative to the cwd): |
| Lars Hjemli | b44ebb1 | 2008-02-20 22:13:13 | [diff] [blame] | 543 | * - .git (file containing "gitdir: <path>") |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 544 | * - .git/ |
| 545 | * - ./ (bare) |
| Lars Hjemli | b44ebb1 | 2008-02-20 22:13:13 | [diff] [blame] | 546 | * - ../.git |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 547 | * - ../.git/ |
| 548 | * - ../ (bare) |
| 549 | * - ../../.git/ |
| 550 | * etc. |
| Matthias Lederhofer | 892c41b | 2007-06-06 07:10:42 | [diff] [blame] | 551 | */ |
| Junio C Hamano | cf87463 | 2010-04-04 21:49:31 | [diff] [blame] | 552 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
| Jonathan Nieder | 60c98d1 | 2010-07-24 11:27:58 | [diff] [blame] | 553 | if (one_filesystem) |
| 554 | current_device = get_device_or_die(".", NULL); |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 555 | for (;;) { |
| Nguyễn Thái Ngọc Duy | 9951d3b | 2010-11-26 15:32:38 | [diff] [blame] | 556 | gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); |
| 557 | if (gitfile) |
| 558 | gitdirenv = gitfile = xstrdup(gitfile); |
| 559 | else { |
| 560 | if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) |
| 561 | gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; |
| 562 | } |
| 563 | |
| 564 | if (gitdirenv) { |
| 565 | ret = setup_discovered_git_dir(gitdirenv, |
| 566 | cwd, offset, len, |
| 567 | nongit_ok); |
| 568 | free(gitfile); |
| 569 | return ret; |
| 570 | } |
| 571 | free(gitfile); |
| 572 | |
| Jonathan Nieder | 68698da | 2010-07-24 11:25:32 | [diff] [blame] | 573 | if (is_git_directory(".")) |
| Nguyễn Thái Ngọc Duy | 1cd8031 | 2010-11-26 15:32:36 | [diff] [blame] | 574 | return setup_bare_git_dir(cwd, offset, len, nongit_ok); |
| 575 | |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 576 | while (--offset > ceil_offset && cwd[offset] != '/'); |
| Jonathan Nieder | f161ede | 2010-07-24 11:26:41 | [diff] [blame] | 577 | if (offset <= ceil_offset) |
| 578 | return setup_nongit(cwd, nongit_ok); |
| Lars R. Damerow | 8030e44 | 2010-03-17 19:55:53 | [diff] [blame] | 579 | if (one_filesystem) { |
| Jonathan Nieder | 60c98d1 | 2010-07-24 11:27:58 | [diff] [blame] | 580 | dev_t parent_device = get_device_or_die("..", cwd); |
| 581 | if (parent_device != current_device) { |
| Lars R. Damerow | 8030e44 | 2010-03-17 19:55:53 | [diff] [blame] | 582 | if (nongit_ok) { |
| 583 | if (chdir(cwd)) |
| 584 | die_errno("Cannot come back to cwd"); |
| 585 | *nongit_ok = 1; |
| 586 | return NULL; |
| 587 | } |
| 588 | cwd[offset] = '\0'; |
| 589 | die("Not a git repository (or any parent up to mount parent %s)\n" |
| Junio C Hamano | cf87463 | 2010-04-04 21:49:31 | [diff] [blame] | 590 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd); |
| Lars R. Damerow | 8030e44 | 2010-03-17 19:55:53 | [diff] [blame] | 591 | } |
| 592 | } |
| Lars R. Damerow | 502ffe3 | 2010-03-17 19:55:52 | [diff] [blame] | 593 | if (chdir("..")) { |
| 594 | cwd[offset] = '\0'; |
| Thomas Rast | d824cbb | 2009-06-27 15:58:46 | [diff] [blame] | 595 | die_errno("Cannot change to '%s/..'", cwd); |
| Lars R. Damerow | 502ffe3 | 2010-03-17 19:55:52 | [diff] [blame] | 596 | } |
| Matthias Lederhofer | 892c41b | 2007-06-06 07:10:42 | [diff] [blame] | 597 | } |
| Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 598 | } |
| Junio C Hamano | 5e7bfe2 | 2005-11-25 23:43:41 | [diff] [blame] | 599 | |
| Nguyễn Thái Ngọc Duy | a60645f | 2010-08-06 02:46:33 | [diff] [blame] | 600 | const char *setup_git_directory_gently(int *nongit_ok) |
| 601 | { |
| 602 | const char *prefix; |
| 603 | |
| 604 | prefix = setup_git_directory_gently_1(nongit_ok); |
| Nguyễn Thái Ngọc Duy | f07d6a1 | 2010-12-01 23:33:22 | [diff] [blame] | 605 | if (startup_info) { |
| Nguyễn Thái Ngọc Duy | a60645f | 2010-08-06 02:46:33 | [diff] [blame] | 606 | startup_info->have_repository = !nongit_ok || !*nongit_ok; |
| Nguyễn Thái Ngọc Duy | f07d6a1 | 2010-12-01 23:33:22 | [diff] [blame] | 607 | startup_info->prefix = prefix; |
| 608 | } |
| Nguyễn Thái Ngọc Duy | a60645f | 2010-08-06 02:46:33 | [diff] [blame] | 609 | return prefix; |
| 610 | } |
| 611 | |
| Junio C Hamano | 94df250 | 2006-06-10 06:09:49 | [diff] [blame] | 612 | int git_config_perm(const char *var, const char *value) |
| 613 | { |
| Heikki Orsila | 06cbe85 | 2008-04-16 08:34:24 | [diff] [blame] | 614 | int i; |
| 615 | char *endptr; |
| 616 | |
| 617 | if (value == NULL) |
| 618 | return PERM_GROUP; |
| 619 | |
| 620 | if (!strcmp(value, "umask")) |
| 621 | return PERM_UMASK; |
| 622 | if (!strcmp(value, "group")) |
| 623 | return PERM_GROUP; |
| 624 | if (!strcmp(value, "all") || |
| 625 | !strcmp(value, "world") || |
| 626 | !strcmp(value, "everybody")) |
| 627 | return PERM_EVERYBODY; |
| 628 | |
| 629 | /* Parse octal numbers */ |
| 630 | i = strtol(value, &endptr, 8); |
| 631 | |
| 632 | /* If not an octal number, maybe true/false? */ |
| 633 | if (*endptr != 0) |
| 634 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; |
| 635 | |
| 636 | /* |
| 637 | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is |
| Junio C Hamano | 5a688fe | 2009-03-25 23:19:36 | [diff] [blame] | 638 | * a chmod value to restrict to. |
| Heikki Orsila | 06cbe85 | 2008-04-16 08:34:24 | [diff] [blame] | 639 | */ |
| 640 | switch (i) { |
| 641 | case PERM_UMASK: /* 0 */ |
| 642 | return PERM_UMASK; |
| 643 | case OLD_PERM_GROUP: /* 1 */ |
| 644 | return PERM_GROUP; |
| 645 | case OLD_PERM_EVERYBODY: /* 2 */ |
| 646 | return PERM_EVERYBODY; |
| Junio C Hamano | 94df250 | 2006-06-10 06:09:49 | [diff] [blame] | 647 | } |
| Heikki Orsila | 06cbe85 | 2008-04-16 08:34:24 | [diff] [blame] | 648 | |
| 649 | /* A filemode value was given: 0xxx */ |
| 650 | |
| 651 | if ((i & 0600) != 0600) |
| 652 | die("Problem with core.sharedRepository filemode value " |
| 653 | "(0%.3o).\nThe owner of files must always have " |
| 654 | "read and write permissions.", i); |
| 655 | |
| 656 | /* |
| 657 | * Mask filemode value. Others can not get write permission. |
| 658 | * x flags for directories are handled separately. |
| 659 | */ |
| Junio C Hamano | 5a688fe | 2009-03-25 23:19:36 | [diff] [blame] | 660 | return -(i & 0666); |
| Junio C Hamano | 94df250 | 2006-06-10 06:09:49 | [diff] [blame] | 661 | } |
| 662 | |
| Johannes Schindelin | ef90d6d | 2008-05-14 17:46:53 | [diff] [blame] | 663 | int check_repository_format_version(const char *var, const char *value, void *cb) |
| Junio C Hamano | ab9cb76 | 2005-11-25 23:59:09 | [diff] [blame] | 664 | { |
| Johannes Schindelin | 299726d | 2007-07-29 23:24:38 | [diff] [blame] | 665 | if (strcmp(var, "core.repositoryformatversion") == 0) |
| 666 | repository_format_version = git_config_int(var, value); |
| Johannes Schindelin | 457f06d | 2005-12-22 22:13:56 | [diff] [blame] | 667 | else if (strcmp(var, "core.sharedrepository") == 0) |
| Junio C Hamano | 94df250 | 2006-06-10 06:09:49 | [diff] [blame] | 668 | shared_repository = git_config_perm(var, value); |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 669 | else if (strcmp(var, "core.bare") == 0) { |
| 670 | is_bare_repository_cfg = git_config_bool(var, value); |
| 671 | if (is_bare_repository_cfg == 1) |
| 672 | inside_work_tree = -1; |
| 673 | } else if (strcmp(var, "core.worktree") == 0) { |
| Junio C Hamano | 180483c | 2008-02-11 19:00:32 | [diff] [blame] | 674 | if (!value) |
| 675 | return config_error_nonbool(var); |
| Jim Meyering | 8e0f700 | 2008-01-31 17:26:32 | [diff] [blame] | 676 | free(git_work_tree_cfg); |
| Johannes Schindelin | e90fdc3 | 2007-08-01 00:30:14 | [diff] [blame] | 677 | git_work_tree_cfg = xstrdup(value); |
| 678 | inside_work_tree = -1; |
| 679 | } |
| Johannes Schindelin | 299726d | 2007-07-29 23:24:38 | [diff] [blame] | 680 | return 0; |
| Junio C Hamano | ab9cb76 | 2005-11-25 23:59:09 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | int check_repository_format(void) |
| 684 | { |
| Nguyễn Thái Ngọc Duy | 337e51c | 2010-11-26 15:32:34 | [diff] [blame] | 685 | return check_repository_format_gently(get_git_dir(), NULL); |
| Junio C Hamano | ab9cb76 | 2005-11-25 23:59:09 | [diff] [blame] | 686 | } |
| 687 | |
| Clemens Buchacher | e1e5ec8 | 2010-06-05 08:04:20 | [diff] [blame] | 688 | /* |
| 689 | * Returns the "prefix", a path to the current working directory |
| 690 | * relative to the work tree root, or NULL, if the current working |
| 691 | * directory is not a strict subdirectory of the work tree root. The |
| 692 | * prefix always ends with a '/' character. |
| 693 | */ |
| Junio C Hamano | 5e7bfe2 | 2005-11-25 23:43:41 | [diff] [blame] | 694 | const char *setup_git_directory(void) |
| 695 | { |
| Nguyễn Thái Ngọc Duy | b3f66fd | 2010-11-26 15:32:39 | [diff] [blame] | 696 | return setup_git_directory_gently(NULL); |
| Junio C Hamano | 5e7bfe2 | 2005-11-25 23:43:41 | [diff] [blame] | 697 | } |