Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 3 | const char *prefix_path(const char *prefix, int len, const char *path) |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 4 | { |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 5 | const char *orig = path; |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 6 | for (;;) { |
| 7 | char c; |
| 8 | if (*path != '.') |
| 9 | break; |
| 10 | c = path[1]; |
| 11 | /* "." */ |
| 12 | if (!c) { |
| 13 | path++; |
| 14 | break; |
| 15 | } |
| 16 | /* "./" */ |
| 17 | if (c == '/') { |
| 18 | path += 2; |
| 19 | continue; |
| 20 | } |
| 21 | if (c != '.') |
| 22 | break; |
| 23 | c = path[2]; |
| 24 | if (!c) |
| 25 | path += 2; |
| 26 | else if (c == '/') |
| 27 | path += 3; |
| 28 | else |
| 29 | break; |
| 30 | /* ".." and "../" */ |
| 31 | /* Remove last component of the prefix */ |
| 32 | do { |
| 33 | if (!len) |
| 34 | die("'%s' is outside repository", orig); |
| 35 | len--; |
| 36 | } while (len && prefix[len-1] != '/'); |
| 37 | continue; |
| 38 | } |
| 39 | if (len) { |
| 40 | int speclen = strlen(path); |
| 41 | char *n = xmalloc(speclen + len + 1); |
| 42 | |
| 43 | memcpy(n, prefix, len); |
| 44 | memcpy(n + len, path, speclen+1); |
| 45 | path = n; |
| 46 | } |
| 47 | return path; |
| 48 | } |
| 49 | |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 50 | /* |
| 51 | * Unlike prefix_path, this should be used if the named file does |
| 52 | * not have to interact with index entry; i.e. name of a random file |
| 53 | * on the filesystem. |
| 54 | */ |
| 55 | const char *prefix_filename(const char *pfx, int pfx_len, const char *arg) |
| 56 | { |
| 57 | static char path[PATH_MAX]; |
| 58 | if (!pfx || !*pfx || arg[0] == '/') |
| 59 | return arg; |
| 60 | memcpy(path, pfx, pfx_len); |
| 61 | strcpy(path + pfx_len, arg); |
| 62 | return path; |
| 63 | } |
| 64 | |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Verify a filename that we got as an argument for a pathspec |
| 67 | * entry. Note that a filename that begins with "-" never verifies |
| 68 | * as true, because even if such a filename were to exist, we want |
| 69 | * it to be preceded by the "--" marker (or we want the user to |
| 70 | * use a format like "./-filename") |
| 71 | */ |
| 72 | void verify_filename(const char *prefix, const char *arg) |
| 73 | { |
| 74 | const char *name; |
| 75 | struct stat st; |
| 76 | |
| 77 | if (*arg == '-') |
| 78 | die("bad flag '%s' used after filename", arg); |
| 79 | name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg; |
| 80 | if (!lstat(name, &st)) |
| 81 | return; |
| 82 | if (errno == ENOENT) |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 83 | die("ambiguous argument '%s': unknown revision or path not in the working tree.\n" |
| 84 | "Use '--' to separate paths from revisions", arg); |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 85 | die("'%s': %s", arg, strerror(errno)); |
| 86 | } |
| 87 | |
Junio C Hamano | ea92f41 | 2006-04-26 15:09:27 -0700 | [diff] [blame] | 88 | /* |
| 89 | * Opposite of the above: the command line did not have -- marker |
| 90 | * and we parsed the arg as a refname. It should not be interpretable |
| 91 | * as a filename. |
| 92 | */ |
| 93 | void verify_non_filename(const char *prefix, const char *arg) |
| 94 | { |
| 95 | const char *name; |
| 96 | struct stat st; |
| 97 | |
| 98 | if (*arg == '-') |
| 99 | return; /* flag */ |
| 100 | name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg; |
| 101 | if (!lstat(name, &st)) |
| 102 | die("ambiguous argument '%s': both revision and filename\n" |
| 103 | "Use '--' to separate filenames from revisions", arg); |
| 104 | if (errno != ENOENT) |
| 105 | die("'%s': %s", arg, strerror(errno)); |
| 106 | } |
| 107 | |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 108 | const char **get_pathspec(const char *prefix, const char **pathspec) |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 109 | { |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 110 | const char *entry = *pathspec; |
| 111 | const char **p; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 112 | int prefixlen; |
| 113 | |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 114 | if (!prefix && !entry) |
| 115 | return NULL; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 116 | |
| 117 | if (!entry) { |
| 118 | static const char *spec[2]; |
| 119 | spec[0] = prefix; |
| 120 | spec[1] = NULL; |
| 121 | return spec; |
| 122 | } |
| 123 | |
| 124 | /* Otherwise we have to re-write the entries.. */ |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 125 | p = pathspec; |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 126 | prefixlen = prefix ? strlen(prefix) : 0; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 127 | do { |
Linus Torvalds | f332726 | 2005-08-16 20:44:32 -0700 | [diff] [blame] | 128 | *p = prefix_path(prefix, prefixlen, entry); |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 129 | } while ((entry = *++p) != NULL); |
| 130 | return (const char **) pathspec; |
| 131 | } |
| 132 | |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 133 | /* |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 134 | * Test if it looks like we're at a git directory. |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 135 | * We want to see: |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 136 | * |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 137 | * - either a objects/ directory _or_ the proper |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 138 | * GIT_OBJECT_DIRECTORY environment variable |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 139 | * - a refs/ directory |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 140 | * - 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] | 141 | * a proper "ref:", or a regular file HEAD that has a properly |
| 142 | * formatted sha1 object name. |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 143 | */ |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 144 | static int is_git_directory(const char *suspect) |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 145 | { |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 146 | char path[PATH_MAX]; |
| 147 | size_t len = strlen(suspect); |
| 148 | |
| 149 | strcpy(path, suspect); |
| 150 | if (getenv(DB_ENVIRONMENT)) { |
| 151 | if (access(getenv(DB_ENVIRONMENT), X_OK)) |
| 152 | return 0; |
| 153 | } |
| 154 | else { |
| 155 | strcpy(path + len, "/objects"); |
| 156 | if (access(path, X_OK)) |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | strcpy(path + len, "/refs"); |
| 161 | if (access(path, X_OK)) |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 162 | return 0; |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 163 | |
| 164 | strcpy(path + len, "/HEAD"); |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 165 | if (validate_headref(path)) |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 166 | return 0; |
| 167 | |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 168 | return 1; |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 171 | const char *setup_git_directory_gently(int *nongit_ok) |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 172 | { |
| 173 | static char cwd[PATH_MAX+1]; |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 174 | const char *gitdirenv; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 175 | int len, offset; |
| 176 | |
| 177 | /* |
| 178 | * If GIT_DIR is set explicitly, we're not going |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 179 | * to do any discovery, but we still do repository |
| 180 | * validation. |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 181 | */ |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 182 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
| 183 | if (gitdirenv) { |
| 184 | if (PATH_MAX - 40 < strlen(gitdirenv)) |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 185 | die("'$%s' too big", GIT_DIR_ENVIRONMENT); |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 186 | if (is_git_directory(gitdirenv)) |
| 187 | return NULL; |
Johannes Schindelin | 3a3c3fc | 2006-08-04 17:46:19 +0200 | [diff] [blame] | 188 | if (nongit_ok) { |
Matthias Lederhofer | 41e95f6 | 2006-07-30 03:30:29 +0200 | [diff] [blame] | 189 | *nongit_ok = 1; |
| 190 | return NULL; |
| 191 | } |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 192 | die("Not a git repository: '%s'", gitdirenv); |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 193 | } |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 194 | |
| 195 | if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/') |
| 196 | die("Unable to read current working directory"); |
| 197 | |
| 198 | offset = len = strlen(cwd); |
| 199 | for (;;) { |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 200 | if (is_git_directory(".git")) |
Linus Torvalds | 5f5608b | 2005-08-27 13:54:42 -0700 | [diff] [blame] | 201 | break; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 202 | chdir(".."); |
| 203 | do { |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 204 | if (!offset) { |
Shawn O. Pearce | ad1a382 | 2006-12-30 23:30:19 -0500 | [diff] [blame] | 205 | if (is_git_directory(cwd)) { |
| 206 | if (chdir(cwd)) |
| 207 | die("Cannot come back to cwd"); |
| 208 | setenv(GIT_DIR_ENVIRONMENT, cwd, 1); |
| 209 | return NULL; |
| 210 | } |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 211 | if (nongit_ok) { |
| 212 | if (chdir(cwd)) |
| 213 | die("Cannot come back to cwd"); |
| 214 | *nongit_ok = 1; |
| 215 | return NULL; |
| 216 | } |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 217 | die("Not a git repository"); |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 218 | } |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 219 | } while (cwd[--offset] != '/'); |
| 220 | } |
| 221 | |
| 222 | if (offset == len) |
| 223 | return NULL; |
| 224 | |
| 225 | /* Make "offset" point to past the '/', and add a '/' at the end */ |
| 226 | offset++; |
| 227 | cwd[len++] = '/'; |
| 228 | cwd[len] = 0; |
| 229 | return cwd + offset; |
| 230 | } |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 231 | |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 232 | int git_config_perm(const char *var, const char *value) |
| 233 | { |
| 234 | if (value) { |
| 235 | if (!strcmp(value, "umask")) |
| 236 | return PERM_UMASK; |
| 237 | if (!strcmp(value, "group")) |
| 238 | return PERM_GROUP; |
| 239 | if (!strcmp(value, "all") || |
| 240 | !strcmp(value, "world") || |
| 241 | !strcmp(value, "everybody")) |
| 242 | return PERM_EVERYBODY; |
| 243 | } |
| 244 | return git_config_bool(var, value); |
| 245 | } |
| 246 | |
Junio C Hamano | ab9cb76 | 2005-11-25 15:59:09 -0800 | [diff] [blame] | 247 | int check_repository_format_version(const char *var, const char *value) |
| 248 | { |
| 249 | if (strcmp(var, "core.repositoryformatversion") == 0) |
| 250 | repository_format_version = git_config_int(var, value); |
Johannes Schindelin | 457f06d | 2005-12-22 23:13:56 +0100 | [diff] [blame] | 251 | else if (strcmp(var, "core.sharedrepository") == 0) |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 252 | shared_repository = git_config_perm(var, value); |
Junio C Hamano | ab9cb76 | 2005-11-25 15:59:09 -0800 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | int check_repository_format(void) |
| 257 | { |
| 258 | git_config(check_repository_format_version); |
| 259 | if (GIT_REPO_VERSION < repository_format_version) |
| 260 | die ("Expected git repo version <= %d, found %d", |
| 261 | GIT_REPO_VERSION, repository_format_version); |
| 262 | return 0; |
| 263 | } |
| 264 | |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 265 | const char *setup_git_directory(void) |
| 266 | { |
Junio C Hamano | 4ca0660 | 2005-11-25 23:14:15 -0800 | [diff] [blame] | 267 | const char *retval = setup_git_directory_gently(NULL); |
Junio C Hamano | 22752e4 | 2005-11-25 16:08:48 -0800 | [diff] [blame] | 268 | check_repository_format(); |
Junio C Hamano | 5e7bfe2 | 2005-11-25 15:43:41 -0800 | [diff] [blame] | 269 | return retval; |
| 270 | } |