3 const char *prefix_path(const char *prefix, int len, const char *path)
5 const char *orig = path;
31 /* Remove last component of the prefix */
34 die("'%s' is outside repository", orig);
36 } while (len && prefix[len-1] != '/');
40 int speclen = strlen(path);
41 char *n = xmalloc(speclen + len + 1);
43 memcpy(n, prefix, len);
44 memcpy(n + len, path, speclen+1);
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
55 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
57 static char path[PATH_MAX];
58 if (!pfx || !*pfx || arg[0] == '/')
60 memcpy(path, pfx, pfx_len);
61 strcpy(path + pfx_len, arg);
65 const char **get_pathspec(const char *prefix, const char **pathspec)
67 const char *entry = *pathspec;
71 if (!prefix && !entry)
75 static const char *spec[2];
81 /* Otherwise we have to re-write the entries.. */
83 prefixlen = prefix ? strlen(prefix) : 0;
85 *p = prefix_path(prefix, prefixlen, entry);
86 } while ((entry = *++p) != NULL);
87 return (const char **) pathspec;
91 * Test if it looks like we're at the top level git directory.
94 * - either a .git/objects/ directory _or_ the proper
95 * GIT_OBJECT_DIRECTORY environment variable
96 * - a refs/ directory under ".git"
97 * - either a HEAD symlink or a HEAD file that is formatted as
100 static int is_toplevel_directory(void)
102 if (access(".git/refs/", X_OK) ||
103 access(getenv(DB_ENVIRONMENT) ?
104 getenv(DB_ENVIRONMENT) : ".git/objects/", X_OK) ||
105 validate_symref(".git/HEAD"))
110 const char *setup_git_directory_gently(int *nongit_ok)
112 static char cwd[PATH_MAX+1];
116 * If GIT_DIR is set explicitly, we're not going
117 * to do any discovery, but we still do repository
120 if (getenv(GIT_DIR_ENVIRONMENT)) {
122 int len = strlen(getenv(GIT_DIR_ENVIRONMENT));
123 if (sizeof(path) - 40 < len)
124 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
125 memcpy(path, getenv(GIT_DIR_ENVIRONMENT), len);
127 strcpy(path + len, "/refs");
128 if (access(path, X_OK))
129 goto bad_dir_environ;
130 strcpy(path + len, "/HEAD");
131 if (validate_symref(path))
132 goto bad_dir_environ;
133 if (getenv(DB_ENVIRONMENT)) {
134 if (access(getenv(DB_ENVIRONMENT), X_OK))
135 goto bad_dir_environ;
138 strcpy(path + len, "/objects");
139 if (access(path, X_OK))
140 goto bad_dir_environ;
145 die("Not a git repository: '%s'", path);
148 if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
149 die("Unable to read current working directory");
151 offset = len = strlen(cwd);
153 if (is_toplevel_directory())
160 die("Cannot come back to cwd");
164 die("Not a git repository");
166 } while (cwd[--offset] != '/');
172 /* Make "offset" point to past the '/', and add a '/' at the end */
179 int check_repository_format_version(const char *var, const char *value)
181 if (strcmp(var, "core.repositoryformatversion") == 0)
182 repository_format_version = git_config_int(var, value);
183 else if (strcmp(var, "core.sharedrepository") == 0)
184 shared_repository = git_config_bool(var, value);
188 int check_repository_format(void)
190 git_config(check_repository_format_version);
191 if (GIT_REPO_VERSION < repository_format_version)
192 die ("Expected git repo version <= %d, found %d",
193 GIT_REPO_VERSION, repository_format_version);
197 const char *setup_git_directory(void)
199 const char *retval = setup_git_directory_gently(NULL);
200 check_repository_format();