1#include "cache.h" 2#include "dir.h" 3 4static int inside_git_dir = -1; 5static int inside_work_tree = -1; 6 7const char *prefix_path(const char *prefix, int len, const char *path) 8{ 9 const char *orig = path; 10 for (;;) { 11 char c; 12 if (*path != '.') 13 break; 14 c = path[1]; 15 /* "." */ 16 if (!c) { 17 path++; 18 break; 19 } 20 /* "./" */ 21 if (c == '/') { 22 path += 2; 23 continue; 24 } 25 if (c != '.') 26 break; 27 c = path[2]; 28 if (!c) 29 path += 2; 30 else if (c == '/') 31 path += 3; 32 else 33 break; 34 /* ".." and "../" */ 35 /* Remove last component of the prefix */ 36 do { 37 if (!len) 38 die("'%s' is outside repository", orig); 39 len--; 40 } while (len && prefix[len-1] != '/'); 41 continue; 42 } 43 if (len) { 44 int speclen = strlen(path); 45 char *n = xmalloc(speclen + len + 1); 46 47 memcpy(n, prefix, len); 48 memcpy(n + len, path, speclen+1); 49 path = n; 50 } 51 return path; 52} 53 54/* 55 * Unlike prefix_path, this should be used if the named file does 56 * not have to interact with index entry; i.e. name of a random file 57 * on the filesystem. 58 */ 59const char *prefix_filename(const char *pfx, int pfx_len, const char *arg) 60{ 61 static char path[PATH_MAX]; 62 if (!pfx || !*pfx || arg[0] == '/') 63 return arg; 64 memcpy(path, pfx, pfx_len); 65 strcpy(path + pfx_len, arg); 66 return path; 67} 68 69/* 70 * Verify a filename that we got as an argument for a pathspec 71 * entry. Note that a filename that begins with "-" never verifies 72 * as true, because even if such a filename were to exist, we want 73 * it to be preceded by the "--" marker (or we want the user to 74 * use a format like "./-filename") 75 */ 76void verify_filename(const char *prefix, const char *arg) 77{ 78 const char *name; 79 struct stat st; 80 81 if (*arg == '-') 82 die("bad flag '%s' used after filename", arg); 83 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg; 84 if (!lstat(name, &st)) 85 return; 86 if (errno == ENOENT) 87 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n" 88 "Use '--' to separate paths from revisions", arg); 89 die("'%s': %s", arg, strerror(errno)); 90} 91 92/* 93 * Opposite of the above: the command line did not have -- marker 94 * and we parsed the arg as a refname. It should not be interpretable 95 * as a filename. 96 */ 97void verify_non_filename(const char *prefix, const char *arg) 98{ 99 const char *name; 100 struct stat st; 101 102 if (!is_inside_work_tree() || is_inside_git_dir()) 103 return; 104 if (*arg == '-') 105 return; /* flag */ 106 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg; 107 if (!lstat(name, &st)) 108 die("ambiguous argument '%s': both revision and filename\n" 109 "Use '--' to separate filenames from revisions", arg); 110 if (errno != ENOENT) 111 die("'%s': %s", arg, strerror(errno)); 112} 113 114const char **get_pathspec(const char *prefix, const char **pathspec) 115{ 116 const char *entry = *pathspec; 117 const char **p; 118 int prefixlen; 119 120 if (!prefix && !entry) 121 return NULL; 122 123 if (!entry) { 124 static const char *spec[2]; 125 spec[0] = prefix; 126 spec[1] = NULL; 127 return spec; 128 } 129 130 /* Otherwise we have to re-write the entries.. */ 131 p = pathspec; 132 prefixlen = prefix ? strlen(prefix) : 0; 133 do { 134 *p = prefix_path(prefix, prefixlen, entry); 135 } while ((entry = *++p) != NULL); 136 return (const char **) pathspec; 137} 138 139/* 140 * Test if it looks like we're at a git directory. 141 * We want to see: 142 * 143 * - either a objects/ directory _or_ the proper 144 * GIT_OBJECT_DIRECTORY environment variable 145 * - a refs/ directory 146 * - either a HEAD symlink or a HEAD file that is formatted as 147 * a proper "ref:", or a regular file HEAD that has a properly 148 * formatted sha1 object name. 149 */ 150static int is_git_directory(const char *suspect) 151{ 152 char path[PATH_MAX]; 153 size_t len = strlen(suspect); 154 155 strcpy(path, suspect); 156 if (getenv(DB_ENVIRONMENT)) { 157 if (access(getenv(DB_ENVIRONMENT), X_OK)) 158 return 0; 159 } 160 else { 161 strcpy(path + len, "/objects"); 162 if (access(path, X_OK)) 163 return 0; 164 } 165 166 strcpy(path + len, "/refs"); 167 if (access(path, X_OK)) 168 return 0; 169 170 strcpy(path + len, "/HEAD"); 171 if (validate_headref(path)) 172 return 0; 173 174 return 1; 175} 176 177int is_inside_git_dir(void) 178{ 179 if (inside_git_dir < 0) 180 inside_git_dir = is_inside_dir(get_git_dir()); 181 return inside_git_dir; 182} 183 184int is_inside_work_tree(void) 185{ 186 if (inside_work_tree < 0) 187 inside_work_tree = is_inside_dir(get_git_work_tree()); 188 return inside_work_tree; 189} 190 191/* 192 * If no worktree was given, and we are outside of a default work tree, 193 * now is the time to set it. 194 * 195 * In other words, if the user calls git with something like 196 * 197 * git --git-dir=/some/where/else/.git bla 198 * 199 * default to /some/where/else as working directory; if the specified 200 * git-dir does not end in "/.git", the cwd is used as working directory. 201 */ 202const char *set_work_tree(const char *dir) 203{ 204 char dir_buffer[PATH_MAX]; 205 static char buffer[PATH_MAX + 1], *rel = NULL; 206 int len, postfix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1; 207 208 /* strip the variable 'dir' of the postfix "/.git" if it has it */ 209 len = strlen(dir); 210 if (len > postfix_len && !strcmp(dir + len - postfix_len, 211 "/" DEFAULT_GIT_DIR_ENVIRONMENT)) { 212 strncpy(dir_buffer, dir, len - postfix_len); 213 214 /* are we inside the default work tree? */ 215 rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer); 216 } 217 /* if rel is set, the cwd is _not_ the current working tree */ 218 if (rel && *rel) { 219 if (!is_absolute_path(dir)) 220 set_git_dir(make_absolute_path(dir)); 221 dir = dir_buffer; 222 chdir(dir); 223 strcat(rel, "/"); 224 inside_git_dir = 0; 225 } else { 226 rel = NULL; 227 dir = getcwd(buffer, sizeof(buffer)); 228 } 229 git_work_tree_cfg = xstrdup(dir); 230 inside_work_tree = 1; 231 232 return rel; 233} 234 235/* 236 * We cannot decide in this function whether we are in the work tree or 237 * not, since the config can only be read _after_ this function was called. 238 */ 239const char *setup_git_directory_gently(int *nongit_ok) 240{ 241 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); 242 static char cwd[PATH_MAX+1]; 243 const char *gitdirenv; 244 int len, offset; 245 246 /* 247 * If GIT_DIR is set explicitly, we're not going 248 * to do any discovery, but we still do repository 249 * validation. 250 */ 251 gitdirenv = getenv(GIT_DIR_ENVIRONMENT); 252 if (gitdirenv) { 253 if (PATH_MAX - 40 < strlen(gitdirenv)) 254 die("'$%s' too big", GIT_DIR_ENVIRONMENT); 255 if (is_git_directory(gitdirenv)) { 256 if (!work_tree_env) 257 return set_work_tree(gitdirenv); 258 return NULL; 259 } 260 if (nongit_ok) { 261 *nongit_ok = 1; 262 return NULL; 263 } 264 die("Not a git repository: '%s'", gitdirenv); 265 } 266 267 if (!getcwd(cwd, sizeof(cwd)-1)) 268 die("Unable to read current working directory"); 269 270 /* 271 * Test in the following order (relative to the cwd): 272 * - .git/ 273 * - ./ (bare) 274 * - ../.git/ 275 * - ../ (bare) 276 * - ../../.git/ 277 * etc. 278 */ 279 offset = len = strlen(cwd); 280 for (;;) { 281 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) 282 break; 283 if (is_git_directory(".")) { 284 inside_git_dir = 1; 285 if (!work_tree_env) 286 inside_work_tree = 0; 287 setenv(GIT_DIR_ENVIRONMENT, ".", 1); 288 return NULL; 289 } 290 chdir(".."); 291 do { 292 if (!offset) { 293 if (nongit_ok) { 294 if (chdir(cwd)) 295 die("Cannot come back to cwd"); 296 *nongit_ok = 1; 297 return NULL; 298 } 299 die("Not a git repository"); 300 } 301 } while (cwd[--offset] != '/'); 302 } 303 304 inside_git_dir = 0; 305 if (!work_tree_env) 306 inside_work_tree = 1; 307 git_work_tree_cfg = xstrndup(cwd, offset); 308 if (offset == len) 309 return NULL; 310 311 /* Make "offset" point to past the '/', and add a '/' at the end */ 312 offset++; 313 cwd[len++] = '/'; 314 cwd[len] = 0; 315 return cwd + offset; 316} 317 318int git_config_perm(const char *var, const char *value) 319{ 320 if (value) { 321 int i; 322 if (!strcmp(value, "umask")) 323 return PERM_UMASK; 324 if (!strcmp(value, "group")) 325 return PERM_GROUP; 326 if (!strcmp(value, "all") || 327 !strcmp(value, "world") || 328 !strcmp(value, "everybody")) 329 return PERM_EVERYBODY; 330 i = atoi(value); 331 if (i > 1) 332 return i; 333 } 334 return git_config_bool(var, value); 335} 336 337int check_repository_format_version(const char *var, const char *value) 338{ 339 if (strcmp(var, "core.repositoryformatversion") == 0) 340 repository_format_version = git_config_int(var, value); 341 else if (strcmp(var, "core.sharedrepository") == 0) 342 shared_repository = git_config_perm(var, value); 343 else if (strcmp(var, "core.bare") == 0) { 344 is_bare_repository_cfg = git_config_bool(var, value); 345 if (is_bare_repository_cfg == 1) 346 inside_work_tree = -1; 347 } else if (strcmp(var, "core.worktree") == 0) { 348 if (git_work_tree_cfg) 349 free(git_work_tree_cfg); 350 git_work_tree_cfg = xstrdup(value); 351 inside_work_tree = -1; 352 } 353 return 0; 354} 355 356int check_repository_format(void) 357{ 358 git_config(check_repository_format_version); 359 if (GIT_REPO_VERSION < repository_format_version) 360 die ("Expected git repo version <= %d, found %d", 361 GIT_REPO_VERSION, repository_format_version); 362 return 0; 363} 364 365const char *setup_git_directory(void) 366{ 367 const char *retval = setup_git_directory_gently(NULL); 368 check_repository_format(); 369 370 /* If the work tree is not the default one, recompute prefix */ 371 if (inside_work_tree < 0) { 372 static char buffer[PATH_MAX + 1]; 373 char *rel; 374 if (retval && chdir(retval)) 375 die ("Could not jump back into original cwd"); 376 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree()); 377 return rel && *rel ? strcat(rel, "/") : NULL; 378 } 379 380 return retval; 381}