🌐 AI搜索 & 代理 主页
blob: 2afdba414a073705440f887593a1b5daa1023758 [file] [log] [blame]
Linus Torvaldsd288a702005-08-17 01:06:341#include "cache.h"
2
Junio C Hamano6b5ee132005-09-21 07:00:473const char *prefix_path(const char *prefix, int len, const char *path)
Linus Torvaldsf3327262005-08-17 03:44:324{
Junio C Hamano6b5ee132005-09-21 07:00:475 const char *orig = path;
Linus Torvaldsf3327262005-08-17 03:44:326 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 Hamano4ca06602005-11-26 07:14:1550/*
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 */
55const 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 Torvaldse23d0b42006-04-26 17:15:5465/*
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 */
72void 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 Hamanoea92f412006-04-26 22:09:2783 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
84 "Use '--' to separate paths from revisions", arg);
Linus Torvaldse23d0b42006-04-26 17:15:5485 die("'%s': %s", arg, strerror(errno));
86}
87
Junio C Hamanoea92f412006-04-26 22:09:2788/*
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 */
93void 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 Hamano6b5ee132005-09-21 07:00:47108const char **get_pathspec(const char *prefix, const char **pathspec)
Linus Torvaldsd288a702005-08-17 01:06:34109{
Junio C Hamano6b5ee132005-09-21 07:00:47110 const char *entry = *pathspec;
111 const char **p;
Linus Torvaldsd288a702005-08-17 01:06:34112 int prefixlen;
113
Linus Torvaldsf3327262005-08-17 03:44:32114 if (!prefix && !entry)
115 return NULL;
Linus Torvaldsd288a702005-08-17 01:06:34116
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 Torvaldsd288a702005-08-17 01:06:34125 p = pathspec;
Linus Torvaldsf3327262005-08-17 03:44:32126 prefixlen = prefix ? strlen(prefix) : 0;
Linus Torvaldsd288a702005-08-17 01:06:34127 do {
Linus Torvaldsf3327262005-08-17 03:44:32128 *p = prefix_path(prefix, prefixlen, entry);
Linus Torvaldsd288a702005-08-17 01:06:34129 } while ((entry = *++p) != NULL);
130 return (const char **) pathspec;
131}
132
Linus Torvalds5f5608b2005-08-27 20:54:42133/*
Junio C Hamano5e7bfe22005-11-25 23:43:41134 * Test if it looks like we're at the top level git directory.
135 * We want to see:
Linus Torvalds5f5608b2005-08-27 20:54:42136 *
Linus Torvalds5f5608b2005-08-27 20:54:42137 * - either a .git/objects/ directory _or_ the proper
138 * GIT_OBJECT_DIRECTORY environment variable
Junio C Hamano8098a172005-09-30 21:26:57139 * - a refs/ directory under ".git"
140 * - either a HEAD symlink or a HEAD file that is formatted as
141 * a proper "ref:".
Linus Torvalds5f5608b2005-08-27 20:54:42142 */
143static int is_toplevel_directory(void)
144{
Junio C Hamano8098a172005-09-30 21:26:57145 if (access(".git/refs/", X_OK) ||
146 access(getenv(DB_ENVIRONMENT) ?
147 getenv(DB_ENVIRONMENT) : ".git/objects/", X_OK) ||
148 validate_symref(".git/HEAD"))
149 return 0;
150 return 1;
Linus Torvalds5f5608b2005-08-27 20:54:42151}
152
Junio C Hamano4ca06602005-11-26 07:14:15153const char *setup_git_directory_gently(int *nongit_ok)
Linus Torvaldsd288a702005-08-17 01:06:34154{
155 static char cwd[PATH_MAX+1];
156 int len, offset;
157
158 /*
159 * If GIT_DIR is set explicitly, we're not going
Junio C Hamano5e7bfe22005-11-25 23:43:41160 * to do any discovery, but we still do repository
161 * validation.
Linus Torvaldsd288a702005-08-17 01:06:34162 */
Junio C Hamano5e7bfe22005-11-25 23:43:41163 if (getenv(GIT_DIR_ENVIRONMENT)) {
164 char path[PATH_MAX];
165 int len = strlen(getenv(GIT_DIR_ENVIRONMENT));
166 if (sizeof(path) - 40 < len)
167 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
168 memcpy(path, getenv(GIT_DIR_ENVIRONMENT), len);
169
170 strcpy(path + len, "/refs");
171 if (access(path, X_OK))
172 goto bad_dir_environ;
173 strcpy(path + len, "/HEAD");
174 if (validate_symref(path))
175 goto bad_dir_environ;
176 if (getenv(DB_ENVIRONMENT)) {
Tommi Virtanen0738fc22005-11-30 15:37:10177 if (access(getenv(DB_ENVIRONMENT), X_OK))
Junio C Hamano5e7bfe22005-11-25 23:43:41178 goto bad_dir_environ;
179 }
180 else {
181 strcpy(path + len, "/objects");
182 if (access(path, X_OK))
183 goto bad_dir_environ;
184 }
Linus Torvaldsd288a702005-08-17 01:06:34185 return NULL;
Junio C Hamano5e7bfe22005-11-25 23:43:41186 bad_dir_environ:
Johannes Schindelin3a3c3fc2006-08-04 15:46:19187 if (nongit_ok) {
Matthias Lederhofer41e95f62006-07-30 01:30:29188 *nongit_ok = 1;
189 return NULL;
190 }
Junio C Hamano5e7bfe22005-11-25 23:43:41191 path[len] = 0;
192 die("Not a git repository: '%s'", path);
193 }
Linus Torvaldsd288a702005-08-17 01:06:34194
195 if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
196 die("Unable to read current working directory");
197
198 offset = len = strlen(cwd);
199 for (;;) {
Linus Torvalds5f5608b2005-08-27 20:54:42200 if (is_toplevel_directory())
201 break;
Linus Torvaldsd288a702005-08-17 01:06:34202 chdir("..");
203 do {
Junio C Hamano4ca06602005-11-26 07:14:15204 if (!offset) {
205 if (nongit_ok) {
206 if (chdir(cwd))
207 die("Cannot come back to cwd");
208 *nongit_ok = 1;
209 return NULL;
210 }
Linus Torvaldsd288a702005-08-17 01:06:34211 die("Not a git repository");
Junio C Hamano4ca06602005-11-26 07:14:15212 }
Linus Torvaldsd288a702005-08-17 01:06:34213 } while (cwd[--offset] != '/');
214 }
215
216 if (offset == len)
217 return NULL;
218
219 /* Make "offset" point to past the '/', and add a '/' at the end */
220 offset++;
221 cwd[len++] = '/';
222 cwd[len] = 0;
223 return cwd + offset;
224}
Junio C Hamano5e7bfe22005-11-25 23:43:41225
Junio C Hamano94df2502006-06-10 06:09:49226int git_config_perm(const char *var, const char *value)
227{
228 if (value) {
229 if (!strcmp(value, "umask"))
230 return PERM_UMASK;
231 if (!strcmp(value, "group"))
232 return PERM_GROUP;
233 if (!strcmp(value, "all") ||
234 !strcmp(value, "world") ||
235 !strcmp(value, "everybody"))
236 return PERM_EVERYBODY;
237 }
238 return git_config_bool(var, value);
239}
240
Junio C Hamanoab9cb762005-11-25 23:59:09241int check_repository_format_version(const char *var, const char *value)
242{
243 if (strcmp(var, "core.repositoryformatversion") == 0)
244 repository_format_version = git_config_int(var, value);
Johannes Schindelin457f06d2005-12-22 22:13:56245 else if (strcmp(var, "core.sharedrepository") == 0)
Junio C Hamano94df2502006-06-10 06:09:49246 shared_repository = git_config_perm(var, value);
Junio C Hamanoab9cb762005-11-25 23:59:09247 return 0;
248}
249
250int check_repository_format(void)
251{
252 git_config(check_repository_format_version);
253 if (GIT_REPO_VERSION < repository_format_version)
254 die ("Expected git repo version <= %d, found %d",
255 GIT_REPO_VERSION, repository_format_version);
256 return 0;
257}
258
Junio C Hamano5e7bfe22005-11-25 23:43:41259const char *setup_git_directory(void)
260{
Junio C Hamano4ca06602005-11-26 07:14:15261 const char *retval = setup_git_directory_gently(NULL);
Junio C Hamano22752e42005-11-26 00:08:48262 check_repository_format();
Junio C Hamano5e7bfe22005-11-25 23:43:41263 return retval;
264}