🌐 AI搜索 & 代理 主页
blob: e3781b656d77be94e04c5229e6a190c5fc496490 [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 Hamano6b5ee132005-09-21 07:00:477const char *prefix_path(const char *prefix, int len, const char *path)
Linus Torvaldsf3327262005-08-17 03:44:328{
Junio C Hamano6b5ee132005-09-21 07:00:479 const char *orig = path;
Junio C Hamanod089eba2008-01-29 06:44:2710 char *sanitized = xmalloc(len + strlen(path) + 1);
Johannes Sixt9a13ba12008-02-19 21:29:4011 if (is_absolute_path(orig))
Junio C Hamanod089eba2008-01-29 06:44:2712 strcpy(sanitized, path);
13 else {
14 if (len)
15 memcpy(sanitized, prefix, len);
16 strcpy(sanitized + len, path);
Linus Torvaldsf3327262005-08-17 03:44:3217 }
Johannes Sixtf3cad0a2009-02-07 15:08:2818 if (normalize_path_copy(sanitized, sanitized))
Junio C Hamanod089eba2008-01-29 06:44:2719 goto error_out;
Johannes Sixt9a13ba12008-02-19 21:29:4020 if (is_absolute_path(orig)) {
Junio C Hamanod089eba2008-01-29 06:44:2721 const char *work_tree = get_git_work_tree();
22 size_t len = strlen(work_tree);
23 size_t total = strlen(sanitized) + 1;
24 if (strncmp(sanitized, work_tree, len) ||
25 (sanitized[len] != '\0' && sanitized[len] != '/')) {
26 error_out:
Dmitry Potapov62525ef2008-10-05 00:40:3627 die("'%s' is outside repository", orig);
Junio C Hamanod089eba2008-01-29 06:44:2728 }
29 if (sanitized[len] == '/')
30 len++;
31 memmove(sanitized, sanitized + len, total - len);
Linus Torvaldsf3327262005-08-17 03:44:3232 }
Junio C Hamanod089eba2008-01-29 06:44:2733 return sanitized;
Linus Torvaldsf3327262005-08-17 03:44:3234}
35
Junio C Hamanoa6080a02007-06-07 07:04:0136/*
Junio C Hamano4ca06602005-11-26 07:14:1537 * Unlike prefix_path, this should be used if the named file does
38 * not have to interact with index entry; i.e. name of a random file
39 * on the filesystem.
40 */
41const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
42{
43 static char path[PATH_MAX];
Johannes Sixt25fe2172008-03-05 20:51:2744#ifndef __MINGW32__
Steffen Prohaskaecf48312007-11-25 22:29:0345 if (!pfx || !*pfx || is_absolute_path(arg))
Junio C Hamano4ca06602005-11-26 07:14:1546 return arg;
47 memcpy(path, pfx, pfx_len);
48 strcpy(path + pfx_len, arg);
Johannes Sixt25fe2172008-03-05 20:51:2749#else
50 char *p;
51 /* don't add prefix to absolute paths, but still replace '\' by '/' */
52 if (is_absolute_path(arg))
53 pfx_len = 0;
54 else
55 memcpy(path, pfx, pfx_len);
56 strcpy(path + pfx_len, arg);
57 for (p = path + pfx_len; *p; p++)
58 if (*p == '\\')
59 *p = '/';
60#endif
Junio C Hamano4ca06602005-11-26 07:14:1561 return path;
62}
63
Linus Torvaldse23d0b42006-04-26 17:15:5464/*
65 * Verify a filename that we got as an argument for a pathspec
66 * entry. Note that a filename that begins with "-" never verifies
67 * as true, because even if such a filename were to exist, we want
68 * it to be preceded by the "--" marker (or we want the user to
69 * use a format like "./-filename")
70 */
71void verify_filename(const char *prefix, const char *arg)
72{
73 const char *name;
74 struct stat st;
75
76 if (*arg == '-')
77 die("bad flag '%s' used after filename", arg);
78 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
79 if (!lstat(name, &st))
80 return;
81 if (errno == ENOENT)
Junio C Hamanoea92f412006-04-26 22:09:2782 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
83 "Use '--' to separate paths from revisions", arg);
Thomas Rastd824cbb2009-06-27 15:58:4684 die_errno("failed to stat '%s'", arg);
Linus Torvaldse23d0b42006-04-26 17:15:5485}
86
Junio C Hamanoea92f412006-04-26 22:09:2787/*
88 * Opposite of the above: the command line did not have -- marker
89 * and we parsed the arg as a refname. It should not be interpretable
90 * as a filename.
91 */
92void verify_non_filename(const char *prefix, const char *arg)
93{
94 const char *name;
95 struct stat st;
96
Matthias Lederhofer7ae3df82007-06-03 14:48:1697 if (!is_inside_work_tree() || is_inside_git_dir())
Johannes Schindelin68025632007-01-20 02:09:3498 return;
Junio C Hamanoea92f412006-04-26 22:09:2799 if (*arg == '-')
100 return; /* flag */
101 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
102 if (!lstat(name, &st))
103 die("ambiguous argument '%s': both revision and filename\n"
104 "Use '--' to separate filenames from revisions", arg);
Junio C Hamano33a798c2007-08-06 07:20:06105 if (errno != ENOENT && errno != ENOTDIR)
Thomas Rastd824cbb2009-06-27 15:58:46106 die_errno("failed to stat '%s'", arg);
Junio C Hamanoea92f412006-04-26 22:09:27107}
108
Junio C Hamano6b5ee132005-09-21 07:00:47109const char **get_pathspec(const char *prefix, const char **pathspec)
Linus Torvaldsd288a702005-08-17 01:06:34110{
Junio C Hamano6b5ee132005-09-21 07:00:47111 const char *entry = *pathspec;
Junio C Hamanod089eba2008-01-29 06:44:27112 const char **src, **dst;
Linus Torvaldsd288a702005-08-17 01:06:34113 int prefixlen;
114
Linus Torvaldsf3327262005-08-17 03:44:32115 if (!prefix && !entry)
116 return NULL;
Linus Torvaldsd288a702005-08-17 01:06:34117
118 if (!entry) {
119 static const char *spec[2];
120 spec[0] = prefix;
121 spec[1] = NULL;
122 return spec;
123 }
124
125 /* Otherwise we have to re-write the entries.. */
Junio C Hamanod089eba2008-01-29 06:44:27126 src = pathspec;
127 dst = pathspec;
Linus Torvaldsf3327262005-08-17 03:44:32128 prefixlen = prefix ? strlen(prefix) : 0;
Junio C Hamanod089eba2008-01-29 06:44:27129 while (*src) {
130 const char *p = prefix_path(prefix, prefixlen, *src);
Dmitry Potapov62525ef2008-10-05 00:40:36131 *(dst++) = p;
Junio C Hamanod089eba2008-01-29 06:44:27132 src++;
133 }
134 *dst = NULL;
135 if (!*pathspec)
136 return NULL;
137 return pathspec;
Linus Torvaldsd288a702005-08-17 01:06:34138}
139
Linus Torvalds5f5608b2005-08-27 20:54:42140/*
Shawn O. Pearcead1a3822006-12-31 04:30:19141 * Test if it looks like we're at a git directory.
Junio C Hamano5e7bfe22005-11-25 23:43:41142 * We want to see:
Linus Torvalds5f5608b2005-08-27 20:54:42143 *
Jim Meyering790296f2008-01-03 14:18:07144 * - either an objects/ directory _or_ the proper
Linus Torvalds5f5608b2005-08-27 20:54:42145 * GIT_OBJECT_DIRECTORY environment variable
Shawn O. Pearcead1a3822006-12-31 04:30:19146 * - a refs/ directory
Junio C Hamano8098a172005-09-30 21:26:57147 * - either a HEAD symlink or a HEAD file that is formatted as
Junio C Hamanoc847f532007-01-02 07:31:08148 * a proper "ref:", or a regular file HEAD that has a properly
149 * formatted sha1 object name.
Linus Torvalds5f5608b2005-08-27 20:54:42150 */
Shawn O. Pearcead1a3822006-12-31 04:30:19151static int is_git_directory(const char *suspect)
Linus Torvalds5f5608b2005-08-27 20:54:42152{
Shawn O. Pearcead1a3822006-12-31 04:30:19153 char path[PATH_MAX];
154 size_t len = strlen(suspect);
155
156 strcpy(path, suspect);
157 if (getenv(DB_ENVIRONMENT)) {
158 if (access(getenv(DB_ENVIRONMENT), X_OK))
159 return 0;
160 }
161 else {
162 strcpy(path + len, "/objects");
163 if (access(path, X_OK))
164 return 0;
165 }
166
167 strcpy(path + len, "/refs");
168 if (access(path, X_OK))
Junio C Hamano8098a172005-09-30 21:26:57169 return 0;
Shawn O. Pearcead1a3822006-12-31 04:30:19170
171 strcpy(path + len, "/HEAD");
Junio C Hamanoc847f532007-01-02 07:31:08172 if (validate_headref(path))
Shawn O. Pearcead1a3822006-12-31 04:30:19173 return 0;
174
Junio C Hamano8098a172005-09-30 21:26:57175 return 1;
Linus Torvalds5f5608b2005-08-27 20:54:42176}
177
Johannes Schindelin68025632007-01-20 02:09:34178int is_inside_git_dir(void)
179{
Johannes Schindeline90fdc32007-08-01 00:30:14180 if (inside_git_dir < 0)
181 inside_git_dir = is_inside_dir(get_git_dir());
182 return inside_git_dir;
Matthias Lederhofer892c41b2007-06-06 07:10:42183}
Johannes Schindelin68025632007-01-20 02:09:34184
Matthias Lederhofer892c41b2007-06-06 07:10:42185int is_inside_work_tree(void)
186{
Johannes Schindeline90fdc32007-08-01 00:30:14187 if (inside_work_tree < 0)
188 inside_work_tree = is_inside_dir(get_git_work_tree());
189 return inside_work_tree;
Matthias Lederhofer892c41b2007-06-06 07:10:42190}
191
Johannes Schindeline90fdc32007-08-01 00:30:14192/*
Johannes Schindelin7efeb8f2007-08-05 13:12:53193 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
194 * The old behaviour (which we retain here) is to set the work tree root
195 * to the cwd, unless overridden by the config, the command line, or
196 * GIT_WORK_TREE.
Johannes Schindeline90fdc32007-08-01 00:30:14197 */
Johannes Schindelin7efeb8f2007-08-05 13:12:53198static const char *set_work_tree(const char *dir)
Matthias Lederhofer892c41b2007-06-06 07:10:42199{
Johannes Schindelin7efeb8f2007-08-05 13:12:53200 char buffer[PATH_MAX + 1];
Johannes Schindeline90fdc32007-08-01 00:30:14201
Johannes Schindelin7efeb8f2007-08-05 13:12:53202 if (!getcwd(buffer, sizeof(buffer)))
203 die ("Could not get the current working directory");
204 git_work_tree_cfg = xstrdup(buffer);
Johannes Schindeline90fdc32007-08-01 00:30:14205 inside_work_tree = 1;
206
Johannes Schindelin7efeb8f2007-08-05 13:12:53207 return NULL;
Johannes Schindelin68025632007-01-20 02:09:34208}
209
Junio C Hamanof3fa1832007-11-08 23:35:32210void setup_work_tree(void)
211{
Johannes Schindelin354e6532007-11-09 11:34:07212 const char *work_tree, *git_dir;
213 static int initialized = 0;
214
215 if (initialized)
216 return;
217 work_tree = get_git_work_tree();
218 git_dir = get_git_dir();
Mike Hommey59f0f2f2007-11-03 11:23:11219 if (!is_absolute_path(git_dir))
Linus Torvalds044bbbc2008-06-19 19:34:06220 git_dir = make_absolute_path(git_dir);
Mike Hommey59f0f2f2007-11-03 11:23:11221 if (!work_tree || chdir(work_tree))
222 die("This operation must be run in a work tree");
Linus Torvalds044bbbc2008-06-19 19:34:06223 set_git_dir(make_relative_path(git_dir, work_tree));
Johannes Schindelin354e6532007-11-09 11:34:07224 initialized = 1;
Mike Hommey59f0f2f2007-11-03 11:23:11225}
226
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32227static int check_repository_format_gently(int *nongit_ok)
228{
Johannes Schindelinef90d6d2008-05-14 17:46:53229 git_config(check_repository_format_version, NULL);
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32230 if (GIT_REPO_VERSION < repository_format_version) {
231 if (!nongit_ok)
232 die ("Expected git repo version <= %d, found %d",
233 GIT_REPO_VERSION, repository_format_version);
234 warning("Expected git repo version <= %d, found %d",
235 GIT_REPO_VERSION, repository_format_version);
236 warning("Please upgrade Git");
237 *nongit_ok = -1;
238 return -1;
239 }
240 return 0;
241}
242
Johannes Schindeline90fdc32007-08-01 00:30:14243/*
Lars Hjemlib44ebb12008-02-20 22:13:13244 * Try to read the location of the git directory from the .git file,
245 * return path to git directory if found.
246 */
247const char *read_gitfile_gently(const char *path)
248{
249 char *buf;
250 struct stat st;
251 int fd;
252 size_t len;
253
254 if (stat(path, &st))
255 return NULL;
256 if (!S_ISREG(st.st_mode))
257 return NULL;
258 fd = open(path, O_RDONLY);
259 if (fd < 0)
Thomas Rastd824cbb2009-06-27 15:58:46260 die_errno("Error opening '%s'", path);
Lars Hjemlib44ebb12008-02-20 22:13:13261 buf = xmalloc(st.st_size + 1);
262 len = read_in_full(fd, buf, st.st_size);
263 close(fd);
264 if (len != st.st_size)
265 die("Error reading %s", path);
266 buf[len] = '\0';
267 if (prefixcmp(buf, "gitdir: "))
268 die("Invalid gitfile format: %s", path);
269 while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
270 len--;
271 if (len < 9)
272 die("No path in gitfile: %s", path);
273 buf[len] = '\0';
274 if (!is_git_directory(buf + 8))
275 die("Not a git repository: %s", buf + 8);
276 path = make_absolute_path(buf + 8);
277 free(buf);
278 return path;
279}
280
281/*
Johannes Schindeline90fdc32007-08-01 00:30:14282 * We cannot decide in this function whether we are in the work tree or
283 * not, since the config can only be read _after_ this function was called.
284 */
Junio C Hamano4ca06602005-11-26 07:14:15285const char *setup_git_directory_gently(int *nongit_ok)
Linus Torvaldsd288a702005-08-17 01:06:34286{
Johannes Schindeline90fdc32007-08-01 00:30:14287 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
David Reiss0454dd92008-05-20 06:49:26288 const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
Linus Torvaldsd288a702005-08-17 01:06:34289 static char cwd[PATH_MAX+1];
Johannes Schindeline90fdc32007-08-01 00:30:14290 const char *gitdirenv;
Lars Hjemlib44ebb12008-02-20 22:13:13291 const char *gitfile_dir;
David Reiss0454dd92008-05-20 06:49:26292 int len, offset, ceil_offset;
Linus Torvaldsd288a702005-08-17 01:06:34293
Johannes Schindeline90fdc32007-08-01 00:30:14294 /*
SZEDER Gáboraf05d672008-03-25 21:06:26295 * Let's assume that we are in a git repository.
296 * If it turns out later that we are somewhere else, the value will be
297 * updated accordingly.
298 */
299 if (nongit_ok)
300 *nongit_ok = 0;
301
302 /*
Johannes Schindeline90fdc32007-08-01 00:30:14303 * If GIT_DIR is set explicitly, we're not going
304 * to do any discovery, but we still do repository
305 * validation.
306 */
Shawn O. Pearcead1a3822006-12-31 04:30:19307 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
Johannes Schindeline90fdc32007-08-01 00:30:14308 if (gitdirenv) {
309 if (PATH_MAX - 40 < strlen(gitdirenv))
310 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
311 if (is_git_directory(gitdirenv)) {
Johannes Schindelindd5c8af2007-10-16 23:37:36312 static char buffer[1024 + 1];
313 const char *retval;
314
Nguyễn Thái Ngọc Duy138dd1e2007-11-29 12:21:39315 if (!work_tree_env) {
316 retval = set_work_tree(gitdirenv);
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32317 /* config may override worktree */
318 if (check_repository_format_gently(nongit_ok))
319 return NULL;
Nguyễn Thái Ngọc Duy138dd1e2007-11-29 12:21:39320 return retval;
321 }
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32322 if (check_repository_format_gently(nongit_ok))
323 return NULL;
Johannes Schindelindd5c8af2007-10-16 23:37:36324 retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
325 get_git_work_tree());
326 if (!retval || !*retval)
327 return NULL;
328 set_git_dir(make_absolute_path(gitdirenv));
329 if (chdir(work_tree_env) < 0)
Thomas Rast0721c312009-06-27 15:58:47330 die_errno ("Could not chdir to '%s'", work_tree_env);
Johannes Schindelindd5c8af2007-10-16 23:37:36331 strcat(buffer, "/");
332 return retval;
Matthias Lederhofer892c41b2007-06-06 07:10:42333 }
Johannes Schindelin3a3c3fc2006-08-04 15:46:19334 if (nongit_ok) {
Matthias Lederhofer41e95f62006-07-30 01:30:29335 *nongit_ok = 1;
336 return NULL;
337 }
Shawn O. Pearcead1a3822006-12-31 04:30:19338 die("Not a git repository: '%s'", gitdirenv);
Junio C Hamano5e7bfe22005-11-25 23:43:41339 }
Linus Torvaldsd288a702005-08-17 01:06:34340
Junio C Hamanof66a4d62007-07-04 19:45:42341 if (!getcwd(cwd, sizeof(cwd)-1))
Thomas Rast0721c312009-06-27 15:58:47342 die_errno("Unable to read current working directory");
Linus Torvaldsd288a702005-08-17 01:06:34343
David Reiss0454dd92008-05-20 06:49:26344 ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
Junio C Hamano17d778e2008-07-07 09:17:23345 if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
346 ceil_offset = 1;
Linus Torvaldsd288a702005-08-17 01:06:34347
Matthias Lederhofer892c41b2007-06-06 07:10:42348 /*
Johannes Schindeline90fdc32007-08-01 00:30:14349 * Test in the following order (relative to the cwd):
Lars Hjemlib44ebb12008-02-20 22:13:13350 * - .git (file containing "gitdir: <path>")
Johannes Schindeline90fdc32007-08-01 00:30:14351 * - .git/
352 * - ./ (bare)
Lars Hjemlib44ebb12008-02-20 22:13:13353 * - ../.git
Johannes Schindeline90fdc32007-08-01 00:30:14354 * - ../.git/
355 * - ../ (bare)
356 * - ../../.git/
357 * etc.
Matthias Lederhofer892c41b2007-06-06 07:10:42358 */
Johannes Schindeline90fdc32007-08-01 00:30:14359 offset = len = strlen(cwd);
360 for (;;) {
Lars Hjemlib44ebb12008-02-20 22:13:13361 gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
362 if (gitfile_dir) {
363 if (set_git_dir(gitfile_dir))
364 die("Repository setup failed");
365 break;
366 }
Johannes Schindeline90fdc32007-08-01 00:30:14367 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
368 break;
369 if (is_git_directory(".")) {
370 inside_git_dir = 1;
371 if (!work_tree_env)
372 inside_work_tree = 0;
SZEDER Gábor72183cb2009-01-16 15:37:33373 if (offset != len) {
374 cwd[offset] = '\0';
375 setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
376 } else
377 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32378 check_repository_format_gently(nongit_ok);
Matthias Lederhofer892c41b2007-06-06 07:10:42379 return NULL;
380 }
David Reiss0454dd92008-05-20 06:49:26381 while (--offset > ceil_offset && cwd[offset] != '/');
382 if (offset <= ceil_offset) {
383 if (nongit_ok) {
384 if (chdir(cwd))
Thomas Rast0721c312009-06-27 15:58:47385 die_errno("Cannot come back to cwd");
David Reiss0454dd92008-05-20 06:49:26386 *nongit_ok = 1;
387 return NULL;
Johannes Schindeline90fdc32007-08-01 00:30:14388 }
Richard Hartmannf66bc5f2008-12-21 23:17:32389 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
David Reiss0454dd92008-05-20 06:49:26390 }
Alex Riesen7be77de2008-12-05 00:36:46391 if (chdir(".."))
Thomas Rastd824cbb2009-06-27 15:58:46392 die_errno("Cannot change to '%s/..'", cwd);
Matthias Lederhofer892c41b2007-06-06 07:10:42393 }
394
Johannes Schindeline90fdc32007-08-01 00:30:14395 inside_git_dir = 0;
396 if (!work_tree_env)
397 inside_work_tree = 1;
398 git_work_tree_cfg = xstrndup(cwd, offset);
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32399 if (check_repository_format_gently(nongit_ok))
400 return NULL;
Johannes Schindeline90fdc32007-08-01 00:30:14401 if (offset == len)
Matthias Lederhofer892c41b2007-06-06 07:10:42402 return NULL;
Matthias Lederhofer892c41b2007-06-06 07:10:42403
Johannes Schindeline90fdc32007-08-01 00:30:14404 /* Make "offset" point to past the '/', and add a '/' at the end */
405 offset++;
406 cwd[len++] = '/';
407 cwd[len] = 0;
408 return cwd + offset;
Linus Torvaldsd288a702005-08-17 01:06:34409}
Junio C Hamano5e7bfe22005-11-25 23:43:41410
Junio C Hamano94df2502006-06-10 06:09:49411int git_config_perm(const char *var, const char *value)
412{
Heikki Orsila06cbe852008-04-16 08:34:24413 int i;
414 char *endptr;
415
416 if (value == NULL)
417 return PERM_GROUP;
418
419 if (!strcmp(value, "umask"))
420 return PERM_UMASK;
421 if (!strcmp(value, "group"))
422 return PERM_GROUP;
423 if (!strcmp(value, "all") ||
424 !strcmp(value, "world") ||
425 !strcmp(value, "everybody"))
426 return PERM_EVERYBODY;
427
428 /* Parse octal numbers */
429 i = strtol(value, &endptr, 8);
430
431 /* If not an octal number, maybe true/false? */
432 if (*endptr != 0)
433 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
434
435 /*
436 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
Junio C Hamano5a688fe2009-03-25 23:19:36437 * a chmod value to restrict to.
Heikki Orsila06cbe852008-04-16 08:34:24438 */
439 switch (i) {
440 case PERM_UMASK: /* 0 */
441 return PERM_UMASK;
442 case OLD_PERM_GROUP: /* 1 */
443 return PERM_GROUP;
444 case OLD_PERM_EVERYBODY: /* 2 */
445 return PERM_EVERYBODY;
Junio C Hamano94df2502006-06-10 06:09:49446 }
Heikki Orsila06cbe852008-04-16 08:34:24447
448 /* A filemode value was given: 0xxx */
449
450 if ((i & 0600) != 0600)
451 die("Problem with core.sharedRepository filemode value "
452 "(0%.3o).\nThe owner of files must always have "
453 "read and write permissions.", i);
454
455 /*
456 * Mask filemode value. Others can not get write permission.
457 * x flags for directories are handled separately.
458 */
Junio C Hamano5a688fe2009-03-25 23:19:36459 return -(i & 0666);
Junio C Hamano94df2502006-06-10 06:09:49460}
461
Johannes Schindelinef90d6d2008-05-14 17:46:53462int check_repository_format_version(const char *var, const char *value, void *cb)
Junio C Hamanoab9cb762005-11-25 23:59:09463{
Johannes Schindelin299726d2007-07-29 23:24:38464 if (strcmp(var, "core.repositoryformatversion") == 0)
465 repository_format_version = git_config_int(var, value);
Johannes Schindelin457f06d2005-12-22 22:13:56466 else if (strcmp(var, "core.sharedrepository") == 0)
Junio C Hamano94df2502006-06-10 06:09:49467 shared_repository = git_config_perm(var, value);
Johannes Schindeline90fdc32007-08-01 00:30:14468 else if (strcmp(var, "core.bare") == 0) {
469 is_bare_repository_cfg = git_config_bool(var, value);
470 if (is_bare_repository_cfg == 1)
471 inside_work_tree = -1;
472 } else if (strcmp(var, "core.worktree") == 0) {
Junio C Hamano180483c2008-02-11 19:00:32473 if (!value)
474 return config_error_nonbool(var);
Jim Meyering8e0f7002008-01-31 17:26:32475 free(git_work_tree_cfg);
Johannes Schindeline90fdc32007-08-01 00:30:14476 git_work_tree_cfg = xstrdup(value);
477 inside_work_tree = -1;
478 }
Johannes Schindelin299726d2007-07-29 23:24:38479 return 0;
Junio C Hamanoab9cb762005-11-25 23:59:09480}
481
482int check_repository_format(void)
483{
Nguyễn Thái Ngọc Duy9459aa72007-12-05 13:33:32484 return check_repository_format_gently(NULL);
Junio C Hamanoab9cb762005-11-25 23:59:09485}
486
Junio C Hamano5e7bfe22005-11-25 23:43:41487const char *setup_git_directory(void)
488{
Junio C Hamano4ca06602005-11-26 07:14:15489 const char *retval = setup_git_directory_gently(NULL);
Johannes Schindeline90fdc32007-08-01 00:30:14490
491 /* If the work tree is not the default one, recompute prefix */
492 if (inside_work_tree < 0) {
493 static char buffer[PATH_MAX + 1];
494 char *rel;
495 if (retval && chdir(retval))
Thomas Rast0721c312009-06-27 15:58:47496 die_errno ("Could not jump back into original cwd");
Johannes Schindeline90fdc32007-08-01 00:30:14497 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
Nguyễn Thái Ngọc Duybb528632008-08-30 09:15:32498 if (rel && *rel && chdir(get_git_work_tree()))
Thomas Rast0721c312009-06-27 15:58:47499 die_errno ("Could not jump to working directory");
Johannes Schindeline90fdc32007-08-01 00:30:14500 return rel && *rel ? strcat(rel, "/") : NULL;
501 }
502
Junio C Hamano5e7bfe22005-11-25 23:43:41503 return retval;
504}