Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [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-16 18:06:34 -0700 | [diff] [blame] | 6 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 7 | const char *prefix_path(const char *prefix, int len, const char *path) |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 8 | { |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 9 | const char *orig = path; |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 10 | char *sanitized = xmalloc(len + strlen(path) + 1); |
Johannes Sixt | 9a13ba1 | 2008-02-19 22:29:40 +0100 | [diff] [blame] | 11 | if (is_absolute_path(orig)) |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 12 | strcpy(sanitized, path); |
| 13 | else { |
| 14 | if (len) |
| 15 | memcpy(sanitized, prefix, len); |
| 16 | strcpy(sanitized + len, path); |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 17 | } |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 18 | if (normalize_path_copy(sanitized, sanitized)) |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 19 | goto error_out; |
Johannes Sixt | 9a13ba1 | 2008-02-19 22:29:40 +0100 | [diff] [blame] | 20 | if (is_absolute_path(orig)) { |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 21 | const char *work_tree = get_git_work_tree(); |
| 22 | size_t len = strlen(work_tree); |
| 23 | size_t total = strlen(sanitized) + 1; |
| 24 | if (strncmp(sanitized, work_tree, len) || |
| 25 | (sanitized[len] != '\0' && sanitized[len] != '/')) { |
| 26 | error_out: |
Dmitry Potapov | 62525ef | 2008-10-05 04:40:36 +0400 | [diff] [blame] | 27 | die("'%s' is outside repository", orig); |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 28 | } |
| 29 | if (sanitized[len] == '/') |
| 30 | len++; |
| 31 | memmove(sanitized, sanitized + len, total - len); |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 32 | } |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 33 | return sanitized; |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 36 | /* |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 37 | * Unlike prefix_path, this should be used if the named file does |
| 38 | * not have to interact with index entry; i.e. name of a random file |
| 39 | * on the filesystem. |
| 40 | */ |
| 41 | const char *prefix_filename(const char *pfx, int pfx_len, const char *arg) |
| 42 | { |
| 43 | static char path[PATH_MAX]; |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 44 | #ifndef __MINGW32__ |
Steffen Prohaska | ecf4831 | 2007-11-25 23:29:03 +0100 | [diff] [blame] | 45 | if (!pfx || !*pfx || is_absolute_path(arg)) |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 46 | return arg; |
| 47 | memcpy(path, pfx, pfx_len); |
| 48 | strcpy(path + pfx_len, arg); |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 49 | #else |
| 50 | char *p; |
| 51 | /* don't add prefix to absolute paths, but still replace '\' by '/' */ |
| 52 | if (is_absolute_path(arg)) |
| 53 | pfx_len = 0; |
| 54 | else |
| 55 | memcpy(path, pfx, pfx_len); |
| 56 | strcpy(path + pfx_len, arg); |
| 57 | for (p = path + pfx_len; *p; p++) |
| 58 | if (*p == '\\') |
| 59 | *p = '/'; |
| 60 | #endif |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 61 | return path; |
| 62 | } |
| 63 | |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 64 | /* |
| 65 | * Verify a filename that we got as an argument for a pathspec |
| 66 | * entry. Note that a filename that begins with "-" never verifies |
| 67 | * as true, because even if such a filename were to exist, we want |
| 68 | * it to be preceded by the "--" marker (or we want the user to |
| 69 | * use a format like "./-filename") |
| 70 | */ |
| 71 | void verify_filename(const char *prefix, const char *arg) |
| 72 | { |
| 73 | const char *name; |
| 74 | struct stat st; |
| 75 | |
| 76 | if (*arg == '-') |
| 77 | die("bad flag '%s' used after filename", arg); |
| 78 | name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg; |
| 79 | if (!lstat(name, &st)) |
| 80 | return; |
| 81 | if (errno == ENOENT) |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 82 | die("ambiguous argument '%s': unknown revision or path not in the working tree.\n" |
| 83 | "Use '--' to separate paths from revisions", arg); |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 84 | die("'%s': %s", arg, strerror(errno)); |
| 85 | } |
| 86 | |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 87 | /* |
| 88 | * Opposite of the above: the command line did not have -- marker |
| 89 | * and we parsed the arg as a refname. It should not be interpretable |
| 90 | * as a filename. |
| 91 | */ |
| 92 | void verify_non_filename(const char *prefix, const char *arg) |
| 93 | { |
| 94 | const char *name; |
| 95 | struct stat st; |
| 96 | |
Matthias Lederhofer | 7ae3df8 | 2007-06-03 16:48:16 +0200 | [diff] [blame] | 97 | if (!is_inside_work_tree() || is_inside_git_dir()) |
Johannes Schindelin | 6802563 | 2007-01-20 03:09:34 +0100 | [diff] [blame] | 98 | return; |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 99 | if (*arg == '-') |
| 100 | return; /* flag */ |
| 101 | name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg; |
| 102 | if (!lstat(name, &st)) |
| 103 | die("ambiguous argument '%s': both revision and filename\n" |
| 104 | "Use '--' to separate filenames from revisions", arg); |
Junio C Hamano | 33a798c | 2007-08-06 00:20:06 -0700 | [diff] [blame] | 105 | if (errno != ENOENT && errno != ENOTDIR) |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 106 | die("'%s': %s", arg, strerror(errno)); |
| 107 | } |
| 108 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 109 | const char **get_pathspec(const char *prefix, const char **pathspec) |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 110 | { |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 111 | const char *entry = *pathspec; |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 112 | const char **src, **dst; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 113 | int prefixlen; |
| 114 | |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 115 | if (!prefix && !entry) |
| 116 | return NULL; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 117 | |
| 118 | if (!entry) { |
| 119 | static const char *spec[2]; |
| 120 | spec[0] = prefix; |
| 121 | spec[1] = NULL; |
| 122 | return spec; |
| 123 | } |
| 124 | |
| 125 | /* Otherwise we have to re-write the entries.. */ |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 126 | src = pathspec; |
| 127 | dst = pathspec; |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 128 | prefixlen = prefix ? strlen(prefix) : 0; |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 129 | while (*src) { |
| 130 | const char *p = prefix_path(prefix, prefixlen, *src); |
Dmitry Potapov | 62525ef | 2008-10-05 04:40:36 +0400 | [diff] [blame] | 131 | *(dst++) = p; |
Junio C Hamano | d089eba | 2008-01-28 22:44:27 -0800 | [diff] [blame] | 132 | src++; |
| 133 | } |
| 134 | *dst = NULL; |
| 135 | if (!*pathspec) |
| 136 | return NULL; |
| 137 | return pathspec; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 140 | /* |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 141 | * Test if it looks like we're at a git directory. |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 142 | * We want to see: |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 143 | * |
Jim Meyering | 790296f | 2008-01-03 15:18:07 +0100 | [diff] [blame] | 144 | * - either an objects/ directory _or_ the proper |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 145 | * GIT_OBJECT_DIRECTORY environment variable |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 146 | * - a refs/ directory |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 147 | * - 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] | 148 | * a proper "ref:", or a regular file HEAD that has a properly |
| 149 | * formatted sha1 object name. |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 150 | */ |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 151 | static int is_git_directory(const char *suspect) |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 152 | { |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 153 | char path[PATH_MAX]; |
| 154 | size_t len = strlen(suspect); |
| 155 | |
| 156 | strcpy(path, suspect); |
| 157 | if (getenv(DB_ENVIRONMENT)) { |
| 158 | if (access(getenv(DB_ENVIRONMENT), X_OK)) |
| 159 | return 0; |
| 160 | } |
| 161 | else { |
| 162 | strcpy(path + len, "/objects"); |
| 163 | if (access(path, X_OK)) |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | strcpy(path + len, "/refs"); |
| 168 | if (access(path, X_OK)) |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 169 | return 0; |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 170 | |
| 171 | strcpy(path + len, "/HEAD"); |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 172 | if (validate_headref(path)) |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 173 | return 0; |
| 174 | |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 175 | return 1; |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Johannes Schindelin | 6802563 | 2007-01-20 03:09:34 +0100 | [diff] [blame] | 178 | int is_inside_git_dir(void) |
| 179 | { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 180 | if (inside_git_dir < 0) |
| 181 | inside_git_dir = is_inside_dir(get_git_dir()); |
| 182 | return inside_git_dir; |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 183 | } |
Johannes Schindelin | 6802563 | 2007-01-20 03:09:34 +0100 | [diff] [blame] | 184 | |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 185 | int is_inside_work_tree(void) |
| 186 | { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 187 | if (inside_work_tree < 0) |
| 188 | inside_work_tree = is_inside_dir(get_git_work_tree()); |
| 189 | return inside_work_tree; |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 192 | /* |
Johannes Schindelin | 7efeb8f | 2007-08-05 14:12:53 +0100 | [diff] [blame] | 193 | * set_work_tree() is only ever called if you set GIT_DIR explicitely. |
| 194 | * The old behaviour (which we retain here) is to set the work tree root |
| 195 | * to the cwd, unless overridden by the config, the command line, or |
| 196 | * GIT_WORK_TREE. |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 197 | */ |
Johannes Schindelin | 7efeb8f | 2007-08-05 14:12:53 +0100 | [diff] [blame] | 198 | static const char *set_work_tree(const char *dir) |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 199 | { |
Johannes Schindelin | 7efeb8f | 2007-08-05 14:12:53 +0100 | [diff] [blame] | 200 | char buffer[PATH_MAX + 1]; |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 201 | |
Johannes Schindelin | 7efeb8f | 2007-08-05 14:12:53 +0100 | [diff] [blame] | 202 | if (!getcwd(buffer, sizeof(buffer))) |
| 203 | die ("Could not get the current working directory"); |
| 204 | git_work_tree_cfg = xstrdup(buffer); |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 205 | inside_work_tree = 1; |
| 206 | |
Johannes Schindelin | 7efeb8f | 2007-08-05 14:12:53 +0100 | [diff] [blame] | 207 | return NULL; |
Johannes Schindelin | 6802563 | 2007-01-20 03:09:34 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 210 | void setup_work_tree(void) |
| 211 | { |
Johannes Schindelin | 354e653 | 2007-11-09 11:34:07 +0000 | [diff] [blame] | 212 | const char *work_tree, *git_dir; |
| 213 | static int initialized = 0; |
| 214 | |
| 215 | if (initialized) |
| 216 | return; |
| 217 | work_tree = get_git_work_tree(); |
| 218 | git_dir = get_git_dir(); |
Mike Hommey | 59f0f2f | 2007-11-03 12:23:11 +0100 | [diff] [blame] | 219 | if (!is_absolute_path(git_dir)) |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 220 | git_dir = make_absolute_path(git_dir); |
Mike Hommey | 59f0f2f | 2007-11-03 12:23:11 +0100 | [diff] [blame] | 221 | if (!work_tree || chdir(work_tree)) |
| 222 | die("This operation must be run in a work tree"); |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 223 | set_git_dir(make_relative_path(git_dir, work_tree)); |
Johannes Schindelin | 354e653 | 2007-11-09 11:34:07 +0000 | [diff] [blame] | 224 | initialized = 1; |
Mike Hommey | 59f0f2f | 2007-11-03 12:23:11 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 20:33:32 +0700 | [diff] [blame] | 227 | static int check_repository_format_gently(int *nongit_ok) |
| 228 | { |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 229 | git_config(check_repository_format_version, NULL); |
Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 20:33:32 +0700 | [diff] [blame] | 230 | if (GIT_REPO_VERSION < repository_format_version) { |
| 231 | if (!nongit_ok) |
| 232 | die ("Expected git repo version <= %d, found %d", |
| 233 | GIT_REPO_VERSION, repository_format_version); |
| 234 | warning("Expected git repo version <= %d, found %d", |
| 235 | GIT_REPO_VERSION, repository_format_version); |
| 236 | warning("Please upgrade Git"); |
| 237 | *nongit_ok = -1; |
| 238 | return -1; |
| 239 | } |
| 240 | return 0; |
| 241 | } |
| 242 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 243 | /* |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 244 | * Try to read the location of the git directory from the .git file, |
| 245 | * return path to git directory if found. |
| 246 | */ |
| 247 | const char *read_gitfile_gently(const char *path) |
| 248 | { |
| 249 | char *buf; |
| 250 | struct stat st; |
| 251 | int fd; |
| 252 | size_t len; |
| 253 | |
| 254 | if (stat(path, &st)) |
| 255 | return NULL; |
| 256 | if (!S_ISREG(st.st_mode)) |
| 257 | return NULL; |
| 258 | fd = open(path, O_RDONLY); |
| 259 | if (fd < 0) |
| 260 | die("Error opening %s: %s", path, strerror(errno)); |
| 261 | buf = xmalloc(st.st_size + 1); |
| 262 | len = read_in_full(fd, buf, st.st_size); |
| 263 | close(fd); |
| 264 | if (len != st.st_size) |
| 265 | die("Error reading %s", path); |
| 266 | buf[len] = '\0'; |
| 267 | if (prefixcmp(buf, "gitdir: ")) |
| 268 | die("Invalid gitfile format: %s", path); |
| 269 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') |
| 270 | len--; |
| 271 | if (len < 9) |
| 272 | die("No path in gitfile: %s", path); |
| 273 | buf[len] = '\0'; |
| 274 | if (!is_git_directory(buf + 8)) |
| 275 | die("Not a git repository: %s", buf + 8); |
| 276 | path = make_absolute_path(buf + 8); |
| 277 | free(buf); |
| 278 | return path; |
| 279 | } |
| 280 | |
| 281 | /* |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 282 | * We cannot decide in this function whether we are in the work tree or |
| 283 | * not, since the config can only be read _after_ this function was called. |
| 284 | */ |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 285 | const char *setup_git_directory_gently(int *nongit_ok) |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 286 | { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 287 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 288 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 289 | static char cwd[PATH_MAX+1]; |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 290 | const char *gitdirenv; |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 291 | const char *gitfile_dir; |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 292 | int len, offset, ceil_offset; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 293 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 294 | /* |
SZEDER Gábor | af05d67 | 2008-03-25 22:06:26 +0100 | [diff] [blame] | 295 | * Let's assume that we are in a git repository. |
| 296 | * If it turns out later that we are somewhere else, the value will be |
| 297 | * updated accordingly. |
| 298 | */ |
| 299 | if (nongit_ok) |
| 300 | *nongit_ok = 0; |
| 301 | |
| 302 | /* |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 303 | * If GIT_DIR is set explicitly, we're not going |
| 304 | * to do any discovery, but we still do repository |
| 305 | * validation. |
| 306 | */ |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 307 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 308 | if (gitdirenv) { |
| 309 | if (PATH_MAX - 40 < strlen(gitdirenv)) |
| 310 | die("'$%s' too big", GIT_DIR_ENVIRONMENT); |
| 311 | if (is_git_directory(gitdirenv)) { |
Johannes Schindelin | dd5c8af | 2007-10-17 00:37:36 +0100 | [diff] [blame] | 312 | static char buffer[1024 + 1]; |
| 313 | const char *retval; |
| 314 | |
Nguyễn Thái Ngọc Duy | 138dd1e | 2007-11-29 19:21:39 +0700 | [diff] [blame] | 315 | if (!work_tree_env) { |
| 316 | retval = set_work_tree(gitdirenv); |
Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 20:33:32 +0700 | [diff] [blame] | 317 | /* config may override worktree */ |
| 318 | if (check_repository_format_gently(nongit_ok)) |
| 319 | return NULL; |
Nguyễn Thái Ngọc Duy | 138dd1e | 2007-11-29 19:21:39 +0700 | [diff] [blame] | 320 | return retval; |
| 321 | } |
Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 20:33:32 +0700 | [diff] [blame] | 322 | if (check_repository_format_gently(nongit_ok)) |
| 323 | return NULL; |
Johannes Schindelin | dd5c8af | 2007-10-17 00:37:36 +0100 | [diff] [blame] | 324 | retval = get_relative_cwd(buffer, sizeof(buffer) - 1, |
| 325 | get_git_work_tree()); |
| 326 | if (!retval || !*retval) |
| 327 | return NULL; |
| 328 | set_git_dir(make_absolute_path(gitdirenv)); |
| 329 | if (chdir(work_tree_env) < 0) |
| 330 | die ("Could not chdir to %s", work_tree_env); |
| 331 | strcat(buffer, "/"); |
| 332 | return retval; |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 333 | } |
Johannes Schindelin | 3a3c3fc | 2006-08-04 17:46:19 +0200 | [diff] [blame] | 334 | if (nongit_ok) { |
Matthias Lederhofer | 41e95f6 | 2006-07-30 03:30:29 +0200 | [diff] [blame] | 335 | *nongit_ok = 1; |
| 336 | return NULL; |
| 337 | } |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 338 | die("Not a git repository: '%s'", gitdirenv); |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 339 | } |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 340 | |
Junio C Hamano | f66a4d6 | 2007-07-04 12:45:42 -0700 | [diff] [blame] | 341 | if (!getcwd(cwd, sizeof(cwd)-1)) |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 342 | die("Unable to read current working directory"); |
| 343 | |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 344 | ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs); |
Junio C Hamano | 17d778e | 2008-07-07 02:17:23 -0700 | [diff] [blame] | 345 | if (ceil_offset < 0 && has_dos_drive_prefix(cwd)) |
| 346 | ceil_offset = 1; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 347 | |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 348 | /* |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 349 | * Test in the following order (relative to the cwd): |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 350 | * - .git (file containing "gitdir: <path>") |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 351 | * - .git/ |
| 352 | * - ./ (bare) |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 353 | * - ../.git |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 354 | * - ../.git/ |
| 355 | * - ../ (bare) |
| 356 | * - ../../.git/ |
| 357 | * etc. |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 358 | */ |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 359 | offset = len = strlen(cwd); |
| 360 | for (;;) { |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 361 | gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); |
| 362 | if (gitfile_dir) { |
| 363 | if (set_git_dir(gitfile_dir)) |
| 364 | die("Repository setup failed"); |
| 365 | break; |
| 366 | } |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 367 | if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) |
| 368 | break; |
| 369 | if (is_git_directory(".")) { |
| 370 | inside_git_dir = 1; |
| 371 | if (!work_tree_env) |
| 372 | inside_work_tree = 0; |
SZEDER Gábor | 72183cb | 2009-01-16 16:37:33 +0100 | [diff] [blame] | 373 | if (offset != len) { |
| 374 | cwd[offset] = '\0'; |
| 375 | setenv(GIT_DIR_ENVIRONMENT, cwd, 1); |
| 376 | } else |
| 377 | setenv(GIT_DIR_ENVIRONMENT, ".", 1); |
Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 20:33:32 +0700 | [diff] [blame] | 378 | check_repository_format_gently(nongit_ok); |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 379 | return NULL; |
| 380 | } |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 381 | while (--offset > ceil_offset && cwd[offset] != '/'); |
| 382 | if (offset <= ceil_offset) { |
| 383 | if (nongit_ok) { |
| 384 | if (chdir(cwd)) |
| 385 | die("Cannot come back to cwd"); |
| 386 | *nongit_ok = 1; |
| 387 | return NULL; |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 388 | } |
Richard Hartmann | f66bc5f | 2008-12-22 00:17:32 +0100 | [diff] [blame] | 389 | die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT); |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 390 | } |
Alex Riesen | 7be77de | 2008-12-05 01:36:46 +0100 | [diff] [blame] | 391 | if (chdir("..")) |
| 392 | die("Cannot change to %s/..: %s", cwd, strerror(errno)); |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 395 | inside_git_dir = 0; |
| 396 | if (!work_tree_env) |
| 397 | inside_work_tree = 1; |
| 398 | git_work_tree_cfg = xstrndup(cwd, offset); |
Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 20:33:32 +0700 | [diff] [blame] | 399 | if (check_repository_format_gently(nongit_ok)) |
| 400 | return NULL; |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 401 | if (offset == len) |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 402 | return NULL; |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 403 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 404 | /* Make "offset" point to past the '/', and add a '/' at the end */ |
| 405 | offset++; |
| 406 | cwd[len++] = '/'; |
| 407 | cwd[len] = 0; |
| 408 | return cwd + offset; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 409 | } |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 410 | |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 411 | int git_config_perm(const char *var, const char *value) |
| 412 | { |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 413 | int i; |
| 414 | char *endptr; |
| 415 | |
| 416 | if (value == NULL) |
| 417 | return PERM_GROUP; |
| 418 | |
| 419 | if (!strcmp(value, "umask")) |
| 420 | return PERM_UMASK; |
| 421 | if (!strcmp(value, "group")) |
| 422 | return PERM_GROUP; |
| 423 | if (!strcmp(value, "all") || |
| 424 | !strcmp(value, "world") || |
| 425 | !strcmp(value, "everybody")) |
| 426 | return PERM_EVERYBODY; |
| 427 | |
| 428 | /* Parse octal numbers */ |
| 429 | i = strtol(value, &endptr, 8); |
| 430 | |
| 431 | /* If not an octal number, maybe true/false? */ |
| 432 | if (*endptr != 0) |
| 433 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; |
| 434 | |
| 435 | /* |
| 436 | * 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] | 437 | * a chmod value to restrict to. |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 438 | */ |
| 439 | switch (i) { |
| 440 | case PERM_UMASK: /* 0 */ |
| 441 | return PERM_UMASK; |
| 442 | case OLD_PERM_GROUP: /* 1 */ |
| 443 | return PERM_GROUP; |
| 444 | case OLD_PERM_EVERYBODY: /* 2 */ |
| 445 | return PERM_EVERYBODY; |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 446 | } |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 447 | |
| 448 | /* A filemode value was given: 0xxx */ |
| 449 | |
| 450 | if ((i & 0600) != 0600) |
| 451 | die("Problem with core.sharedRepository filemode value " |
| 452 | "(0%.3o).\nThe owner of files must always have " |
| 453 | "read and write permissions.", i); |
| 454 | |
| 455 | /* |
| 456 | * Mask filemode value. Others can not get write permission. |
| 457 | * x flags for directories are handled separately. |
| 458 | */ |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 459 | return -(i & 0666); |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 462 | int check_repository_format_version(const char *var, const char *value, void *cb) |
Junio C Hamano | ab9cb76 | 2005-11-25 15:59:09 -0800 | [diff] [blame] | 463 | { |
Johannes Schindelin | 299726d | 2007-07-30 00:24:38 +0100 | [diff] [blame] | 464 | if (strcmp(var, "core.repositoryformatversion") == 0) |
| 465 | repository_format_version = git_config_int(var, value); |
Johannes Schindelin | 457f06d | 2005-12-22 23:13:56 +0100 | [diff] [blame] | 466 | else if (strcmp(var, "core.sharedrepository") == 0) |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 467 | shared_repository = git_config_perm(var, value); |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 468 | else if (strcmp(var, "core.bare") == 0) { |
| 469 | is_bare_repository_cfg = git_config_bool(var, value); |
| 470 | if (is_bare_repository_cfg == 1) |
| 471 | inside_work_tree = -1; |
| 472 | } else if (strcmp(var, "core.worktree") == 0) { |
Junio C Hamano | 180483c | 2008-02-11 11:00:32 -0800 | [diff] [blame] | 473 | if (!value) |
| 474 | return config_error_nonbool(var); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 475 | free(git_work_tree_cfg); |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 476 | git_work_tree_cfg = xstrdup(value); |
| 477 | inside_work_tree = -1; |
| 478 | } |
Johannes Schindelin | 299726d | 2007-07-30 00:24:38 +0100 | [diff] [blame] | 479 | return 0; |
Junio C Hamano | ab9cb76 | 2005-11-25 15:59:09 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | int check_repository_format(void) |
| 483 | { |
Nguyễn Thái Ngọc Duy | 9459aa7 | 2007-12-05 20:33:32 +0700 | [diff] [blame] | 484 | return check_repository_format_gently(NULL); |
Junio C Hamano | ab9cb76 | 2005-11-25 15:59:09 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 487 | const char *setup_git_directory(void) |
| 488 | { |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 489 | const char *retval = setup_git_directory_gently(NULL); |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 490 | |
| 491 | /* If the work tree is not the default one, recompute prefix */ |
| 492 | if (inside_work_tree < 0) { |
| 493 | static char buffer[PATH_MAX + 1]; |
| 494 | char *rel; |
| 495 | if (retval && chdir(retval)) |
| 496 | die ("Could not jump back into original cwd"); |
| 497 | rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree()); |
Nguyễn Thái Ngọc Duy | bb52863 | 2008-08-30 16:15:32 +0700 | [diff] [blame] | 498 | if (rel && *rel && chdir(get_git_work_tree())) |
| 499 | die ("Could not jump to working directory"); |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 500 | return rel && *rel ? strcat(rel, "/") : NULL; |
| 501 | } |
| 502 | |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 503 | return retval; |
| 504 | } |