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);
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")
72 void verify_filename(const char *prefix, const char *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))
83 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
84 "Use '--' to separate paths from revisions", arg);
85 die("'%s': %s", arg, strerror(errno));
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
93 void verify_non_filename(const char *prefix, const char *arg)
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);
105 die("'%s': %s", arg, strerror(errno));
108 const char **get_pathspec(const char *prefix, const char **pathspec)
110 const char *entry = *pathspec;
114 if (!prefix && !entry)
118 static const char *spec[2];
124 /* Otherwise we have to re-write the entries.. */
126 prefixlen = prefix ? strlen(prefix) : 0;
128 *p = prefix_path(prefix, prefixlen, entry);
129 } while ((entry = *++p) != NULL);
130 return (const char **) pathspec;
134 * Test if it looks like we're at a git directory.
137 * - either a objects/ directory _or_ the proper
138 * GIT_OBJECT_DIRECTORY environment variable
139 * - a refs/ directory
140 * - either a HEAD symlink or a HEAD file that is formatted as
143 static int is_git_directory(const char *suspect)
146 size_t len = strlen(suspect);
148 strcpy(path, suspect);
149 if (getenv(DB_ENVIRONMENT)) {
150 if (access(getenv(DB_ENVIRONMENT), X_OK))
154 strcpy(path + len, "/objects");
155 if (access(path, X_OK))
159 strcpy(path + len, "/refs");
160 if (access(path, X_OK))
163 strcpy(path + len, "/HEAD");
164 if (validate_symref(path))
170 const char *setup_git_directory_gently(int *nongit_ok)
172 static char cwd[PATH_MAX+1];
173 const char *gitdirenv;
177 * If GIT_DIR is set explicitly, we're not going
178 * to do any discovery, but we still do repository
181 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
183 if (PATH_MAX - 40 < strlen(gitdirenv))
184 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
185 if (is_git_directory(gitdirenv))
191 die("Not a git repository: '%s'", gitdirenv);
194 if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
195 die("Unable to read current working directory");
197 offset = len = strlen(cwd);
199 if (is_git_directory(".git"))
204 if (is_git_directory(cwd)) {
206 die("Cannot come back to cwd");
207 setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
212 die("Cannot come back to cwd");
216 die("Not a git repository");
218 } while (cwd[--offset] != '/');
224 /* Make "offset" point to past the '/', and add a '/' at the end */
231 int git_config_perm(const char *var, const char *value)
234 if (!strcmp(value, "umask"))
236 if (!strcmp(value, "group"))
238 if (!strcmp(value, "all") ||
239 !strcmp(value, "world") ||
240 !strcmp(value, "everybody"))
241 return PERM_EVERYBODY;
243 return git_config_bool(var, value);
246 int check_repository_format_version(const char *var, const char *value)
248 if (strcmp(var, "core.repositoryformatversion") == 0)
249 repository_format_version = git_config_int(var, value);
250 else if (strcmp(var, "core.sharedrepository") == 0)
251 shared_repository = git_config_perm(var, value);
255 int check_repository_format(void)
257 git_config(check_repository_format_version);
258 if (GIT_REPO_VERSION < repository_format_version)
259 die ("Expected git repo version <= %d, found %d",
260 GIT_REPO_VERSION, repository_format_version);
264 const char *setup_git_directory(void)
266 const char *retval = setup_git_directory_gently(NULL);
267 check_repository_format();