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{ 9const char*orig = path; 10for(;;) { 11char c; 12if(*path !='.') 13break; 14 c = path[1]; 15/* "." */ 16if(!c) { 17 path++; 18break; 19} 20/* "./" */ 21if(c =='/') { 22 path +=2; 23continue; 24} 25if(c !='.') 26break; 27 c = path[2]; 28if(!c) 29 path +=2; 30else if(c =='/') 31 path +=3; 32else 33break; 34/* ".." and "../" */ 35/* Remove last component of the prefix */ 36do{ 37if(!len) 38die("'%s' is outside repository", orig); 39 len--; 40}while(len && prefix[len-1] !='/'); 41continue; 42} 43if(len) { 44int speclen =strlen(path); 45char*n =xmalloc(speclen + len +1); 46 47memcpy(n, prefix, len); 48memcpy(n + len, path, speclen+1); 49 path = n; 50} 51return 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{ 61static char path[PATH_MAX]; 62if(!pfx || !*pfx || arg[0] =='/') 63return arg; 64memcpy(path, pfx, pfx_len); 65strcpy(path + pfx_len, arg); 66return 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 */ 76voidverify_filename(const char*prefix,const char*arg) 77{ 78const char*name; 79struct stat st; 80 81if(*arg =='-') 82die("bad flag '%s' used after filename", arg); 83 name = prefix ?prefix_filename(prefix,strlen(prefix), arg) : arg; 84if(!lstat(name, &st)) 85return; 86if(errno == ENOENT) 87die("ambiguous argument '%s': unknown revision or path not in the working tree.\n" 88"Use '--' to separate paths from revisions", arg); 89die("'%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 */ 97voidverify_non_filename(const char*prefix,const char*arg) 98{ 99const char*name; 100struct stat st; 101 102if(!is_inside_work_tree() ||is_inside_git_dir()) 103return; 104if(*arg =='-') 105return;/* flag */ 106 name = prefix ?prefix_filename(prefix,strlen(prefix), arg) : arg; 107if(!lstat(name, &st)) 108die("ambiguous argument '%s': both revision and filename\n" 109"Use '--' to separate filenames from revisions", arg); 110if(errno != ENOENT && errno != ENOTDIR) 111die("'%s':%s", arg,strerror(errno)); 112} 113 114const char**get_pathspec(const char*prefix,const char**pathspec) 115{ 116const char*entry = *pathspec; 117const char**p; 118int prefixlen; 119 120if(!prefix && !entry) 121return NULL; 122 123if(!entry) { 124static const char*spec[2]; 125 spec[0] = prefix; 126 spec[1] = NULL; 127return spec; 128} 129 130/* Otherwise we have to re-write the entries.. */ 131 p = pathspec; 132 prefixlen = prefix ?strlen(prefix) :0; 133do{ 134*p =prefix_path(prefix, prefixlen, entry); 135}while((entry = *++p) != NULL); 136return(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 intis_git_directory(const char*suspect) 151{ 152char path[PATH_MAX]; 153size_t len =strlen(suspect); 154 155strcpy(path, suspect); 156if(getenv(DB_ENVIRONMENT)) { 157if(access(getenv(DB_ENVIRONMENT), X_OK)) 158return0; 159} 160else{ 161strcpy(path + len,"/objects"); 162if(access(path, X_OK)) 163return0; 164} 165 166strcpy(path + len,"/refs"); 167if(access(path, X_OK)) 168return0; 169 170strcpy(path + len,"/HEAD"); 171if(validate_headref(path)) 172return0; 173 174return1; 175} 176 177intis_inside_git_dir(void) 178{ 179if(inside_git_dir <0) 180 inside_git_dir =is_inside_dir(get_git_dir()); 181return inside_git_dir; 182} 183 184intis_inside_work_tree(void) 185{ 186if(inside_work_tree <0) 187 inside_work_tree =is_inside_dir(get_git_work_tree()); 188return inside_work_tree; 189} 190 191/* 192 * set_work_tree() is only ever called if you set GIT_DIR explicitely. 193 * The old behaviour (which we retain here) is to set the work tree root 194 * to the cwd, unless overridden by the config, the command line, or 195 * GIT_WORK_TREE. 196 */ 197static const char*set_work_tree(const char*dir) 198{ 199char buffer[PATH_MAX +1]; 200 201if(!getcwd(buffer,sizeof(buffer))) 202die("Could not get the current working directory"); 203 git_work_tree_cfg =xstrdup(buffer); 204 inside_work_tree =1; 205 206return NULL; 207} 208 209/* 210 * We cannot decide in this function whether we are in the work tree or 211 * not, since the config can only be read _after_ this function was called. 212 */ 213const char*setup_git_directory_gently(int*nongit_ok) 214{ 215const char*work_tree_env =getenv(GIT_WORK_TREE_ENVIRONMENT); 216static char cwd[PATH_MAX+1]; 217const char*gitdirenv; 218int len, offset; 219 220/* 221 * If GIT_DIR is set explicitly, we're not going 222 * to do any discovery, but we still do repository 223 * validation. 224 */ 225 gitdirenv =getenv(GIT_DIR_ENVIRONMENT); 226if(gitdirenv) { 227if(PATH_MAX -40<strlen(gitdirenv)) 228die("'$%s' too big", GIT_DIR_ENVIRONMENT); 229if(is_git_directory(gitdirenv)) { 230static char buffer[1024+1]; 231const char*retval; 232 233if(!work_tree_env) 234returnset_work_tree(gitdirenv); 235 retval =get_relative_cwd(buffer,sizeof(buffer) -1, 236get_git_work_tree()); 237if(!retval || !*retval) 238return NULL; 239set_git_dir(make_absolute_path(gitdirenv)); 240if(chdir(work_tree_env) <0) 241die("Could not chdir to%s", work_tree_env); 242strcat(buffer,"/"); 243return retval; 244} 245if(nongit_ok) { 246*nongit_ok =1; 247return NULL; 248} 249die("Not a git repository: '%s'", gitdirenv); 250} 251 252if(!getcwd(cwd,sizeof(cwd)-1)) 253die("Unable to read current working directory"); 254 255/* 256 * Test in the following order (relative to the cwd): 257 * - .git/ 258 * - ./ (bare) 259 * - ../.git/ 260 * - ../ (bare) 261 * - ../../.git/ 262 * etc. 263 */ 264 offset = len =strlen(cwd); 265for(;;) { 266if(is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) 267break; 268if(is_git_directory(".")) { 269 inside_git_dir =1; 270if(!work_tree_env) 271 inside_work_tree =0; 272setenv(GIT_DIR_ENVIRONMENT,".",1); 273return NULL; 274} 275chdir(".."); 276do{ 277if(!offset) { 278if(nongit_ok) { 279if(chdir(cwd)) 280die("Cannot come back to cwd"); 281*nongit_ok =1; 282return NULL; 283} 284die("Not a git repository"); 285} 286}while(cwd[--offset] !='/'); 287} 288 289 inside_git_dir =0; 290if(!work_tree_env) 291 inside_work_tree =1; 292 git_work_tree_cfg =xstrndup(cwd, offset); 293if(offset == len) 294return NULL; 295 296/* Make "offset" point to past the '/', and add a '/' at the end */ 297 offset++; 298 cwd[len++] ='/'; 299 cwd[len] =0; 300return cwd + offset; 301} 302 303intgit_config_perm(const char*var,const char*value) 304{ 305if(value) { 306int i; 307if(!strcmp(value,"umask")) 308return PERM_UMASK; 309if(!strcmp(value,"group")) 310return PERM_GROUP; 311if(!strcmp(value,"all") || 312!strcmp(value,"world") || 313!strcmp(value,"everybody")) 314return PERM_EVERYBODY; 315 i =atoi(value); 316if(i >1) 317return i; 318} 319returngit_config_bool(var, value); 320} 321 322intcheck_repository_format_version(const char*var,const char*value) 323{ 324if(strcmp(var,"core.repositoryformatversion") ==0) 325 repository_format_version =git_config_int(var, value); 326else if(strcmp(var,"core.sharedrepository") ==0) 327 shared_repository =git_config_perm(var, value); 328else if(strcmp(var,"core.bare") ==0) { 329 is_bare_repository_cfg =git_config_bool(var, value); 330if(is_bare_repository_cfg ==1) 331 inside_work_tree = -1; 332}else if(strcmp(var,"core.worktree") ==0) { 333if(git_work_tree_cfg) 334free(git_work_tree_cfg); 335 git_work_tree_cfg =xstrdup(value); 336 inside_work_tree = -1; 337} 338return0; 339} 340 341intcheck_repository_format(void) 342{ 343git_config(check_repository_format_version); 344if(GIT_REPO_VERSION < repository_format_version) 345die("Expected git repo version <=%d, found%d", 346 GIT_REPO_VERSION, repository_format_version); 347return0; 348} 349 350const char*setup_git_directory(void) 351{ 352const char*retval =setup_git_directory_gently(NULL); 353check_repository_format(); 354 355/* If the work tree is not the default one, recompute prefix */ 356if(inside_work_tree <0) { 357static char buffer[PATH_MAX +1]; 358char*rel; 359if(retval &&chdir(retval)) 360die("Could not jump back into original cwd"); 361 rel =get_relative_cwd(buffer, PATH_MAX,get_git_work_tree()); 362return rel && *rel ?strcat(rel,"/") : NULL; 363} 364 365return retval; 366}