🌐 AI搜索 & 代理 主页
blob: 6b277b6a11d3d8419d79a93628b9923001a065a6 [file] [log] [blame]
Linus Torvaldsd288a702005-08-17 01:06:341#include "cache.h"
Johannes Schindeline90fdc32007-08-01 00:30:142#include "dir.h"
3
4static int inside_git_dir = -1;
5static int inside_work_tree = -1;
Linus Torvaldsd288a702005-08-17 01:06:346
Junio C Hamanod089eba2008-01-29 06:44:277static int sanitary_path_copy(char *dst, const char *src)
8{
Johannes Sixt25fe2172008-03-05 20:51:279 char *dst0;
Junio C Hamanod089eba2008-01-29 06:44:2710
Johannes Sixt25fe2172008-03-05 20:51:2711 if (has_dos_drive_prefix(src)) {
12 *dst++ = *src++;
13 *dst++ = *src++;
14 }
15 dst0 = dst;
16
17 if (is_dir_sep(*src)) {
Junio C Hamanod089eba2008-01-29 06:44:2718 *dst++ = '/';
Johannes Sixt25fe2172008-03-05 20:51:2719 while (is_dir_sep(*src))
Junio C Hamanod089eba2008-01-29 06:44:2720 src++;
21 }
22
23 for (;;) {
24 char c = *src;
25
26 /*
27 * A path component that begins with . could be
28 * special:
29 * (1) "." and ends -- ignore and terminate.
30 * (2) "./" -- ignore them, eat slash and continue.
31 * (3) ".." and ends -- strip one and terminate.
32 * (4) "../" -- strip one, eat slash and continue.
33 */
34 if (c == '.') {
Johannes Sixt4cd148d2008-03-01 20:11:1435 if (!src[1]) {
Junio C Hamanod089eba2008-01-29 06:44:2736 /* (1) */
37 src++;
Johannes Sixt25fe2172008-03-05 20:51:2738 } else if (is_dir_sep(src[1])) {
Junio C Hamanod089eba2008-01-29 06:44:2739 /* (2) */
40 src += 2;
Johannes Sixt25fe2172008-03-05 20:51:2741 while (is_dir_sep(*src))
Junio C Hamanod089eba2008-01-29 06:44:2742 src++;
43 continue;
Johannes Sixt4cd148d2008-03-01 20:11:1444 } else if (src[1] == '.') {
45 if (!src[2]) {
Junio C Hamanod089eba2008-01-29 06:44:2746 /* (3) */
47 src += 2;
48 goto up_one;
Johannes Sixt25fe2172008-03-05 20:51:2749 } else if (is_dir_sep(src[2])) {
Junio C Hamanod089eba2008-01-29 06:44:2750 /* (4) */
51 src += 3;
Johannes Sixt25fe2172008-03-05 20:51:2752 while (is_dir_sep(*src))
Junio C Hamanod089eba2008-01-29 06:44:2753 src++;
54 goto up_one;
55 }
56 }
57 }
58
59 /* copy up to the next '/', and eat all '/' */
Johannes Sixt25fe2172008-03-05 20:51:2760 while ((c = *src++) != '\0' && !is_dir_sep(c))
Junio C Hamanod089eba2008-01-29 06:44:2761 *dst++ = c;
Johannes Sixt25fe2172008-03-05 20:51:2762 if (is_dir_sep(c)) {
63 *dst++ = '/';
64 while (is_dir_sep(c))
Junio C Hamanod089eba2008-01-29 06:44:2765 c = *src++;
66 src--;
67 } else if (!c)
68 break;
69 continue;
70
71 up_one:
72 /*
73 * dst0..dst is prefix portion, and dst[-1] is '/';
74 * go up one level.
75 */
76 dst -= 2; /* go past trailing '/' if any */
77 if (dst < dst0)
78 return -1;
79 while (1) {
80 if (dst <= dst0)
81 break;
82 c = *dst--;
Johannes Sixt25fe2172008-03-05 20:51:2783 if (c == '/') { /* MinGW: cannot be '\\' anymore */
Junio C Hamanod089eba2008-01-29 06:44:2784 dst += 2;
85 break;
86 }
87 }
88 }
89 *dst = '\0';
90 return 0;
91}
92
Junio C Hamano6b5ee132005-09-21 07:00:4793const char *prefix_path(const char *prefix, int len, const char *path)
Linus Torvaldsf3327262005-08-17 03:44:3294{
Junio C Hamano6b5ee132005-09-21 07:00:4795 const char *orig = path;
Junio C Hamanod089eba2008-01-29 06:44:2796 char *sanitized = xmalloc(len + strlen(path) + 1);
Johannes Sixt9a13ba12008-02-19 21:29:4097 if (is_absolute_path(orig))
Junio C Hamanod089eba2008-01-29 06:44:2798 strcpy(sanitized, path);
99 else {
100 if (len)
101 memcpy(sanitized, prefix, len);
102 strcpy(sanitized + len, path);
Linus Torvaldsf3327262005-08-17 03:44:32103 }
Junio C Hamanod089eba2008-01-29 06:44:27104 if (sanitary_path_copy(sanitized, sanitized))
105 goto error_out;
Johannes Sixt9a13ba12008-02-19 21:29:40106 if (is_absolute_path(orig)) {
Junio C Hamanod089eba2008-01-29 06:44:27107 const char *work_tree = get_git_work_tree();
108 size_t len = strlen(work_tree);
109 size_t total = strlen(sanitized) + 1;
110 if (strncmp(sanitized, work_tree, len) ||
111 (sanitized[len] != '\0' && sanitized[len] != '/')) {
112 error_out:
Dmitry Potapov62525ef2008-10-05 00:40:36113 die("'%s' is outside repository", orig);
Junio C Hamanod089eba2008-01-29 06:44:27114 }
115 if (sanitized[len] == '/')
116 len++;
117 memmove(sanitized, sanitized + len, total - len);
Linus Torvaldsf3327262005-08-17 03:44:32118 }
Junio C Hamanod089eba2008-01-29 06:44:27119 return sanitized;
Linus Torvaldsf3327262005-08-17 03:44:32120}
121
Junio C Hamanoa6080a02007-06-07 07:04:01122/*
Junio C Hamano4ca06602005-11-26 07:14:15123 * Unlike prefix_path, this should be used if the named file does
124 * not have to interact with index entry; i.e. name of a random file
125 * on the filesystem.
126 */
127const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
128{
129 static char path[PATH_MAX];
Johannes Sixt25fe2172008-03-05 20:51:27130#ifndef __MINGW32__
Steffen Prohaskaecf48312007-11-25 22:29:03131 if (!pfx || !*pfx || is_absolute_path(arg))
Junio C Hamano4ca06602005-11-26 07:14:15132 return arg;
133 memcpy(path, pfx, pfx_len);
134 strcpy(path + pfx_len, arg);
Johannes Sixt25fe2172008-03-05 20:51:27135#else
136 char *p;
137 /* don't add prefix to absolute paths, but still replace '\' by '/' */
138 if (is_absolute_path(arg))
139 pfx_len = 0;
140 else
141 memcpy(path, pfx, pfx_len);
142 strcpy(path + pfx_len, arg);
143 for (p = path + pfx_len; *p; p++)
144 if (*p == '\\')
145 *p = '/';
146#endif
Junio C Hamano4ca06602005-11-26 07:14:15147 return path;
148}
149
Linus Torvaldse23d0b42006-04-26 17:15:54150/*
151 * Verify a filename that we got as an argument for a pathspec
152 * entry. Note that a filename that begins with "-" never verifies
153 * as true, because even if such a filename were to exist, we want
154 * it to be preceded by the "--" marker (or we want the user to
155 * use a format like "./-filename")
156 */
157void verify_filename(const char *prefix, const char *arg)
158{
159 const char *name;
160 struct stat st;
161
162 if (*arg == '-')
163 die("bad flag '%s' used after filename", arg);
164 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
165 if (!lstat(name, &st))
166 return;
167 if (errno == ENOENT)
Junio C Hamanoea92f412006-04-26 22:09:27168 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
169 "Use '--' to separate paths from revisions", arg);
Linus Torvaldse23d0b42006-04-26 17:15:54170 die("'%s': %s", arg, strerror(errno));
171}
172
Junio C Hamanoea92f412006-04-26 22:09:27173/*
174 * Opposite of the above: the command line did not have -- marker
175 * and we parsed the arg as a refname. It should not be interpretable
176 * as a filename.
177 */
178void verify_non_filename(const char *prefix, const char *arg)
179{
180 const char *name;
181 struct stat st;
182
Matthias Lederhofer7ae3df82007-06-03 14:48:16183 if (!is_inside_work_tree() || is_inside_git_dir())
Johannes Schindelin68025632007-01-20 02:09:34184 return;
Junio C Hamanoea92f412006-04-26 22:09:27185 if (*arg == '-')
186 return; /* flag */
187 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
188 if (!lstat(name, &st))
189 die("ambiguous argument '%s': both revision and filename\n"
190 "Use '--' to separate filenames from revisions", arg);
Junio C Hamano33a798c2007-08-06 07:20:06191 if (errno != ENOENT && errno != ENOTDIR)
Junio C Hamanoea92f412006-04-26 22:09:27192 die("'%s': %s", arg, strerror(errno));
193}
194
Junio C Hamano6b5ee132005-09-21 07:00:47195const char **get_pathspec(const char *prefix, const char **pathspec)
Linus Torvaldsd288a702005-08-17 01:06:34196{
Junio C Hamano6b5ee132005-09-21 07:00:47197 const char *entry = *pathspec;
Junio C Hamanod089eba2008-01-29 06:44:27198 const char **src, **dst;
Linus Torvaldsd288a702005-08-17 01:06:34199 int prefixlen;
200
Linus Torvaldsf3327262005-08-17 03:44:32201 if (!prefix && !entry)
202 return NULL;
Linus Torvaldsd288a702005-08-17 01:06:34203
204 if (!entry) {
205 static const char *spec[2];
206 spec[0] = prefix;
207 spec[1] = NULL;
208 return spec;
209 }
210
211 /* Otherwise we have to re-write the entries.. */
Junio C Hamanod089eba2008-01-29 06:44:27212 src = pathspec;
213 dst = pathspec;
Linus Torvaldsf3327262005-08-17 03:44:32214 prefixlen = prefix ? strlen(prefix) : 0;
Junio C Hamanod089eba2008-01-29 06:44:27215 while (*src) {
216 const char *p = prefix_path(prefix, prefixlen, *src);
Dmitry Potapov62525ef2008-10-05 00:40:36217 *(dst++) = p;
Junio C Hamanod089eba2008-01-29 06:44:27218 src++;
219 }
220 *dst = NULL;
221 if (!*pathspec)
222 return NULL;
223 return pathspec;
Linus Torvaldsd288a702005-08-17 01:06:34224}
225
Linus Torvalds5f5608b2005-08-27 20:54:42226/*
Shawn O. Pearcead1a3822006-12-31 04:30:19227 * Test if it looks like we're at a git directory.
Junio C Hamano5e7bfe22005-11-25 23:43:41228 * We want to see:
Linus Torvalds5f5608b2005-08-27 20:54:42229 *
Jim Meyering790296f2008-01-03 14:18:07230 * - either an objects/ directory _or_ the proper
Linus Torvalds5f5608b2005-08-27 20:54:42231 * GIT_OBJECT_DIRECTORY environment variable
Shawn O. Pearcead1a3822006-12-31 04:30:19232 * - a refs/ directory
Junio C Hamano8098a172005-09-30 21:26:57233 * - either a HEAD symlink or a HEAD file that is formatted as
Junio C Hamanoc847f532007-01-02 07:31:08234 * a proper "ref:", or a regular file HEAD that has a properly
235 * formatted sha1 object name.
Linus Torvalds5f5608b2005-08-27 20:54:42236 */
Shawn O. Pearcead1a3822006-12-31 04:30:19237static int is_git_directory(const char *suspect)
Linus Torvalds5f5608b2005-08-27 20:54:42238{
Shawn O. Pearcead1a3822006-12-31 04:30:19239 char path[PATH_MAX];
240 size_t len = strlen(suspect);
241
242 strcpy(path, suspect);
243 if (getenv(DB_ENVIRONMENT)) {
244 if (access(getenv(DB_ENVIRONMENT), X_OK))
245 return 0;
246 }
247 else {
248 strcpy(path + len, "/objects");
249 if (access(path, X_OK))
250 return 0;
251 }
252
253 strcpy(path + len, "/refs");
254 if (access(path, X_OK))
Junio C Hamano8098a172005-09-30 21:26:57255 return 0;
Shawn O. Pearcead1a3822006-12-31 04:30:19256
257 strcpy(path + len, "/HEAD");
Junio C Hamanoc847f532007-01-02 07:31:08258 if (validate_headref(path))
Shawn O. Pearcead1a3822006-12-31 04:30:19259 return 0;
260
Junio C Hamano8098a172005-09-30 21:26:57261 return 1;
Linus Torvalds5f5608b2005-08-27 20:54:42262}
263
Johannes Schindelin68025632007-01-20 02:09:34264int is_inside_git_dir(void)
265{
Johannes Schindeline90fdc32007-08-01 00:30:14266 if (inside_git_dir < 0)
267 inside_git_dir = is_inside_dir(get_git_dir());
268 return inside_git_dir;
Matthias Lederhofer892c41b2007-06-06 07:10:42269}
Johannes Schindelin68025632007-01-20 02:09:34270
Matthias Lederhofer892c41b2007-06-06 07:10:42271int is_inside_work_tree(void)
272{
Johannes Schindeline90fdc32007-08-01 00:30:14273 if (inside_work_tree < 0)
274 inside_work_tree = is_inside_dir(get_git_work_tree());
275 return inside_work_tree;
Matthias Lederhofer892c41b2007-06-06 07:10:42276}
277
Johannes Schindeline90fdc32007-08-01 00:30:14278/*
Johannes Schindelin7efeb8f2007-08-05 13:12:53279 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
280 * The old behaviour (which we retain here) is to set the work tree root
281 * to the cwd, unless overridden by the config, the command line, or
282 * GIT_WORK_TREE.
Johannes Schindeline90fdc32007-08-01 00:30:14283 */
Johannes Schindelin7efeb8f2007-08-05 13:12:53284static const char *set_work_tree(const char *dir)
Matthias Lederhofer892c41b2007-06-06 07:10:42285{
Johannes Schindelin7efeb8f2007-08-05 13:12:53286 char buffer[PATH_MAX + 1];
Johannes Schindeline90fdc32007-08-01 00:30:14287
Johannes Schindelin7efeb8f2007-08-05 13:12:53288 if (!getcwd(buffer, sizeof(buffer)))
289 die ("Could not get the current working directory");
290 git_work_tree_cfg = xstrdup(buffer);
Johannes Schindeline90fdc32007-08-01 00:30:14291 inside_work_tree = 1;
292
Johannes Schindelin7efeb8f2007-08-05 13:12:53293 return NULL;
Johannes Schindelin68025632007-01-20 02:09:34294}
295
Junio C Hamanof3fa1832007-11-08 23:35:32296void setup_work_tree(void)
297{
Johannes Schindelin354e6532007-11-09 11:34:07298 const char *work_tree, *git_dir;
299 static int initialized = 0;
300
301 if (initialized)
302 return;
303 work_tree = get_git_work_tree();
304 git_dir = get_git_dir();
Mike Hommey59f0f2f2007-11-03 11:23:11305 if (!is_absolute_path(git_dir))
Linus Torvalds044bbbc2008-06-19 19:34:06306 git_dir = make_absolute_path(git_dir);
Mike Hommey59f0f2f2007-11-03 11:23:11307 if (!work_tree || chdir(work_tree))
308 die("This operation must be run in a work tree");
Linus Torvalds044bbbc2008-06-19 19:34:06309 set_git_dir(make_relative_path(git_dir, work_tree));
Johannes Schindelin354e6532007-11-09 11:34:07310 initialized = 1;
Mike Hommey59f0f2f2007-11-03 11:23:11311}
312
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32313static int check_repository_format_gently(int *nongit_ok)
314{
Johannes Schindelinef90d6d2008-05-14 17:46:53315 git_config(check_repository_format_version, NULL);
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32316 if (GIT_REPO_VERSION < repository_format_version) {
317 if (!nongit_ok)
318 die ("Expected git repo version <= %d, found %d",
319 GIT_REPO_VERSION, repository_format_version);
320 warning("Expected git repo version <= %d, found %d",
321 GIT_REPO_VERSION, repository_format_version);
322 warning("Please upgrade Git");
323 *nongit_ok = -1;
324 return -1;
325 }
326 return 0;
327}
328
Johannes Schindeline90fdc32007-08-01 00:30:14329/*
Lars Hjemlib44ebb12008-02-20 22:13:13330 * Try to read the location of the git directory from the .git file,
331 * return path to git directory if found.
332 */
333const char *read_gitfile_gently(const char *path)
334{
335 char *buf;
336 struct stat st;
337 int fd;
338 size_t len;
339
340 if (stat(path, &st))
341 return NULL;
342 if (!S_ISREG(st.st_mode))
343 return NULL;
344 fd = open(path, O_RDONLY);
345 if (fd < 0)
346 die("Error opening %s: %s", path, strerror(errno));
347 buf = xmalloc(st.st_size + 1);
348 len = read_in_full(fd, buf, st.st_size);
349 close(fd);
350 if (len != st.st_size)
351 die("Error reading %s", path);
352 buf[len] = '\0';
353 if (prefixcmp(buf, "gitdir: "))
354 die("Invalid gitfile format: %s", path);
355 while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
356 len--;
357 if (len < 9)
358 die("No path in gitfile: %s", path);
359 buf[len] = '\0';
360 if (!is_git_directory(buf + 8))
361 die("Not a git repository: %s", buf + 8);
362 path = make_absolute_path(buf + 8);
363 free(buf);
364 return path;
365}
366
367/*
Johannes Schindeline90fdc32007-08-01 00:30:14368 * We cannot decide in this function whether we are in the work tree or
369 * not, since the config can only be read _after_ this function was called.
370 */
Junio C Hamano4ca06602005-11-26 07:14:15371const char *setup_git_directory_gently(int *nongit_ok)
Linus Torvaldsd288a702005-08-17 01:06:34372{
Johannes Schindeline90fdc32007-08-01 00:30:14373 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
David Reiss0454dd92008-05-20 06:49:26374 const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
Linus Torvaldsd288a702005-08-17 01:06:34375 static char cwd[PATH_MAX+1];
Johannes Schindeline90fdc32007-08-01 00:30:14376 const char *gitdirenv;
Lars Hjemlib44ebb12008-02-20 22:13:13377 const char *gitfile_dir;
David Reiss0454dd92008-05-20 06:49:26378 int len, offset, ceil_offset;
Linus Torvaldsd288a702005-08-17 01:06:34379
Johannes Schindeline90fdc32007-08-01 00:30:14380 /*
SZEDER Gáboraf05d672008-03-25 21:06:26381 * Let's assume that we are in a git repository.
382 * If it turns out later that we are somewhere else, the value will be
383 * updated accordingly.
384 */
385 if (nongit_ok)
386 *nongit_ok = 0;
387
388 /*
Johannes Schindeline90fdc32007-08-01 00:30:14389 * If GIT_DIR is set explicitly, we're not going
390 * to do any discovery, but we still do repository
391 * validation.
392 */
Shawn O. Pearcead1a3822006-12-31 04:30:19393 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
Johannes Schindeline90fdc32007-08-01 00:30:14394 if (gitdirenv) {
395 if (PATH_MAX - 40 < strlen(gitdirenv))
396 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
397 if (is_git_directory(gitdirenv)) {
Johannes Schindelindd5c8af2007-10-16 23:37:36398 static char buffer[1024 + 1];
399 const char *retval;
400
Nguyễn Thái Ngọc Duy138dd1e2007-11-29 12:21:39401 if (!work_tree_env) {
402 retval = set_work_tree(gitdirenv);
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32403 /* config may override worktree */
404 if (check_repository_format_gently(nongit_ok))
405 return NULL;
Nguyễn Thái Ngọc Duy138dd1e2007-11-29 12:21:39406 return retval;
407 }
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32408 if (check_repository_format_gently(nongit_ok))
409 return NULL;
Johannes Schindelindd5c8af2007-10-16 23:37:36410 retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
411 get_git_work_tree());
412 if (!retval || !*retval)
413 return NULL;
414 set_git_dir(make_absolute_path(gitdirenv));
415 if (chdir(work_tree_env) < 0)
416 die ("Could not chdir to %s", work_tree_env);
417 strcat(buffer, "/");
418 return retval;
Matthias Lederhofer892c41b2007-06-06 07:10:42419 }
Johannes Schindelin3a3c3fc2006-08-04 15:46:19420 if (nongit_ok) {
Matthias Lederhofer41e95f62006-07-30 01:30:29421 *nongit_ok = 1;
422 return NULL;
423 }
Shawn O. Pearcead1a3822006-12-31 04:30:19424 die("Not a git repository: '%s'", gitdirenv);
Junio C Hamano5e7bfe22005-11-25 23:43:41425 }
Linus Torvaldsd288a702005-08-17 01:06:34426
Junio C Hamanof66a4d62007-07-04 19:45:42427 if (!getcwd(cwd, sizeof(cwd)-1))
Linus Torvaldsd288a702005-08-17 01:06:34428 die("Unable to read current working directory");
429
David Reiss0454dd92008-05-20 06:49:26430 ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
Junio C Hamano17d778e2008-07-07 09:17:23431 if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
432 ceil_offset = 1;
Linus Torvaldsd288a702005-08-17 01:06:34433
Matthias Lederhofer892c41b2007-06-06 07:10:42434 /*
Johannes Schindeline90fdc32007-08-01 00:30:14435 * Test in the following order (relative to the cwd):
Lars Hjemlib44ebb12008-02-20 22:13:13436 * - .git (file containing "gitdir: <path>")
Johannes Schindeline90fdc32007-08-01 00:30:14437 * - .git/
438 * - ./ (bare)
Lars Hjemlib44ebb12008-02-20 22:13:13439 * - ../.git
Johannes Schindeline90fdc32007-08-01 00:30:14440 * - ../.git/
441 * - ../ (bare)
442 * - ../../.git/
443 * etc.
Matthias Lederhofer892c41b2007-06-06 07:10:42444 */
Johannes Schindeline90fdc32007-08-01 00:30:14445 offset = len = strlen(cwd);
446 for (;;) {
Lars Hjemlib44ebb12008-02-20 22:13:13447 gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
448 if (gitfile_dir) {
449 if (set_git_dir(gitfile_dir))
450 die("Repository setup failed");
451 break;
452 }
Johannes Schindeline90fdc32007-08-01 00:30:14453 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
454 break;
455 if (is_git_directory(".")) {
456 inside_git_dir = 1;
457 if (!work_tree_env)
458 inside_work_tree = 0;
459 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32460 check_repository_format_gently(nongit_ok);
Matthias Lederhofer892c41b2007-06-06 07:10:42461 return NULL;
462 }
David Reiss0454dd92008-05-20 06:49:26463 while (--offset > ceil_offset && cwd[offset] != '/');
464 if (offset <= ceil_offset) {
465 if (nongit_ok) {
466 if (chdir(cwd))
467 die("Cannot come back to cwd");
468 *nongit_ok = 1;
469 return NULL;
Johannes Schindeline90fdc32007-08-01 00:30:14470 }
Richard Hartmannf66bc5f2008-12-21 23:17:32471 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
David Reiss0454dd92008-05-20 06:49:26472 }
Alex Riesen7be77de2008-12-05 00:36:46473 if (chdir(".."))
474 die("Cannot change to %s/..: %s", cwd, strerror(errno));
Matthias Lederhofer892c41b2007-06-06 07:10:42475 }
476
Johannes Schindeline90fdc32007-08-01 00:30:14477 inside_git_dir = 0;
478 if (!work_tree_env)
479 inside_work_tree = 1;
480 git_work_tree_cfg = xstrndup(cwd, offset);
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32481 if (check_repository_format_gently(nongit_ok))
482 return NULL;
Johannes Schindeline90fdc32007-08-01 00:30:14483 if (offset == len)
Matthias Lederhofer892c41b2007-06-06 07:10:42484 return NULL;
Matthias Lederhofer892c41b2007-06-06 07:10:42485
Johannes Schindeline90fdc32007-08-01 00:30:14486 /* Make "offset" point to past the '/', and add a '/' at the end */
487 offset++;
488 cwd[len++] = '/';
489 cwd[len] = 0;
490 return cwd + offset;
Linus Torvaldsd288a702005-08-17 01:06:34491}
Junio C Hamano5e7bfe22005-11-25 23:43:41492
Junio C Hamano94df2502006-06-10 06:09:49493int git_config_perm(const char *var, const char *value)
494{
Heikki Orsila06cbe852008-04-16 08:34:24495 int i;
496 char *endptr;
497
498 if (value == NULL)
499 return PERM_GROUP;
500
501 if (!strcmp(value, "umask"))
502 return PERM_UMASK;
503 if (!strcmp(value, "group"))
504 return PERM_GROUP;
505 if (!strcmp(value, "all") ||
506 !strcmp(value, "world") ||
507 !strcmp(value, "everybody"))
508 return PERM_EVERYBODY;
509
510 /* Parse octal numbers */
511 i = strtol(value, &endptr, 8);
512
513 /* If not an octal number, maybe true/false? */
514 if (*endptr != 0)
515 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
516
517 /*
518 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
519 * a chmod value.
520 */
521 switch (i) {
522 case PERM_UMASK: /* 0 */
523 return PERM_UMASK;
524 case OLD_PERM_GROUP: /* 1 */
525 return PERM_GROUP;
526 case OLD_PERM_EVERYBODY: /* 2 */
527 return PERM_EVERYBODY;
Junio C Hamano94df2502006-06-10 06:09:49528 }
Heikki Orsila06cbe852008-04-16 08:34:24529
530 /* A filemode value was given: 0xxx */
531
532 if ((i & 0600) != 0600)
533 die("Problem with core.sharedRepository filemode value "
534 "(0%.3o).\nThe owner of files must always have "
535 "read and write permissions.", i);
536
537 /*
538 * Mask filemode value. Others can not get write permission.
539 * x flags for directories are handled separately.
540 */
541 return i & 0666;
Junio C Hamano94df2502006-06-10 06:09:49542}
543
Johannes Schindelinef90d6d2008-05-14 17:46:53544int check_repository_format_version(const char *var, const char *value, void *cb)
Junio C Hamanoab9cb762005-11-25 23:59:09545{
Johannes Schindelin299726d2007-07-29 23:24:38546 if (strcmp(var, "core.repositoryformatversion") == 0)
547 repository_format_version = git_config_int(var, value);
Johannes Schindelin457f06d2005-12-22 22:13:56548 else if (strcmp(var, "core.sharedrepository") == 0)
Junio C Hamano94df2502006-06-10 06:09:49549 shared_repository = git_config_perm(var, value);
Johannes Schindeline90fdc32007-08-01 00:30:14550 else if (strcmp(var, "core.bare") == 0) {
551 is_bare_repository_cfg = git_config_bool(var, value);
552 if (is_bare_repository_cfg == 1)
553 inside_work_tree = -1;
554 } else if (strcmp(var, "core.worktree") == 0) {
Junio C Hamano180483c2008-02-11 19:00:32555 if (!value)
556 return config_error_nonbool(var);
Jim Meyering8e0f7002008-01-31 17:26:32557 free(git_work_tree_cfg);
Johannes Schindeline90fdc32007-08-01 00:30:14558 git_work_tree_cfg = xstrdup(value);
559 inside_work_tree = -1;
560 }
Johannes Schindelin299726d2007-07-29 23:24:38561 return 0;
Junio C Hamanoab9cb762005-11-25 23:59:09562}
563
564int check_repository_format(void)
565{
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32566 return check_repository_format_gently(NULL);
Junio C Hamanoab9cb762005-11-25 23:59:09567}
568
Junio C Hamano5e7bfe22005-11-25 23:43:41569const char *setup_git_directory(void)
570{
Junio C Hamano4ca06602005-11-26 07:14:15571 const char *retval = setup_git_directory_gently(NULL);
Johannes Schindeline90fdc32007-08-01 00:30:14572
573 /* If the work tree is not the default one, recompute prefix */
574 if (inside_work_tree < 0) {
575 static char buffer[PATH_MAX + 1];
576 char *rel;
577 if (retval && chdir(retval))
578 die ("Could not jump back into original cwd");
579 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
Nguyễn Thái Ngọc Duybb528632008-08-30 09:15:32580 if (rel && *rel && chdir(get_git_work_tree()))
581 die ("Could not jump to working directory");
Johannes Schindeline90fdc32007-08-01 00:30:14582 return rel && *rel ? strcat(rel, "/") : NULL;
583 }
584
Junio C Hamano5e7bfe22005-11-25 23:43:41585 return retval;
586}