Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 568 lines (513 sloc) 13.585 kb
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
1 #include "cache.h"
e90fdc3 @dscho Clean up work-tree handling
dscho authored
2 #include "dir.h"
3
4 static int inside_git_dir = -1;
5 static int inside_work_tree = -1;
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
6
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
7 static int sanitary_path_copy(char *dst, const char *src)
f332726 [PATCH] Improve handling of "." and ".." in git-diff-*
Linus Torvalds authored
8 {
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
9 char *dst0 = dst;
10
11 if (*src == '/') {
12 *dst++ = '/';
13 while (*src == '/')
14 src++;
15 }
16
f332726 [PATCH] Improve handling of "." and ".." in git-diff-*
Linus Torvalds authored
17 for (;;) {
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
18 char c = *src;
19
20 /*
21 * A path component that begins with . could be
22 * special:
23 * (1) "." and ends -- ignore and terminate.
24 * (2) "./" -- ignore them, eat slash and continue.
25 * (3) ".." and ends -- strip one and terminate.
26 * (4) "../" -- strip one, eat slash and continue.
27 */
28 if (c == '.') {
29 switch (src[1]) {
30 case '\0':
31 /* (1) */
32 src++;
33 break;
34 case '/':
35 /* (2) */
36 src += 2;
37 while (*src == '/')
38 src++;
39 continue;
40 case '.':
41 switch (src[2]) {
42 case '\0':
43 /* (3) */
44 src += 2;
45 goto up_one;
46 case '/':
47 /* (4) */
48 src += 3;
49 while (*src == '/')
50 src++;
51 goto up_one;
52 }
53 }
f332726 [PATCH] Improve handling of "." and ".." in git-diff-*
Linus Torvalds authored
54 }
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
55
56 /* copy up to the next '/', and eat all '/' */
57 while ((c = *src++) != '\0' && c != '/')
58 *dst++ = c;
f332726 [PATCH] Improve handling of "." and ".." in git-diff-*
Linus Torvalds authored
59 if (c == '/') {
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
60 *dst++ = c;
61 while (c == '/')
62 c = *src++;
63 src--;
64 } else if (!c)
f332726 [PATCH] Improve handling of "." and ".." in git-diff-*
Linus Torvalds authored
65 break;
66 continue;
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
67
68 up_one:
69 /*
70 * dst0..dst is prefix portion, and dst[-1] is '/';
71 * go up one level.
72 */
73 dst -= 2; /* go past trailing '/' if any */
74 if (dst < dst0)
75 return -1;
76 while (1) {
77 if (dst <= dst0)
78 break;
79 c = *dst--;
80 if (c == '/') {
81 dst += 2;
82 break;
83 }
84 }
f332726 [PATCH] Improve handling of "." and ".." in git-diff-*
Linus Torvalds authored
85 }
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
86 *dst = '\0';
87 return 0;
88 }
a6080a0 @gitster War on whitespace
gitster authored
89
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
90 const char *prefix_path(const char *prefix, int len, const char *path)
91 {
92 const char *orig = path;
93 char *sanitized = xmalloc(len + strlen(path) + 1);
9a13ba1 prefix_path: use is_absolute_path() instead of *orig == '/'
Johannes Sixt authored
94 if (is_absolute_path(orig))
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
95 strcpy(sanitized, path);
96 else {
97 if (len)
98 memcpy(sanitized, prefix, len);
99 strcpy(sanitized + len, path);
f332726 [PATCH] Improve handling of "." and ".." in git-diff-*
Linus Torvalds authored
100 }
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
101 if (sanitary_path_copy(sanitized, sanitized))
102 goto error_out;
9a13ba1 prefix_path: use is_absolute_path() instead of *orig == '/'
Johannes Sixt authored
103 if (is_absolute_path(orig)) {
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
104 const char *work_tree = get_git_work_tree();
105 size_t len = strlen(work_tree);
106 size_t total = strlen(sanitized) + 1;
107 if (strncmp(sanitized, work_tree, len) ||
108 (sanitized[len] != '\0' && sanitized[len] != '/')) {
109 error_out:
110 error("'%s' is outside repository", orig);
111 free(sanitized);
112 return NULL;
113 }
114 if (sanitized[len] == '/')
115 len++;
116 memmove(sanitized, sanitized + len, total - len);
117 }
118 return sanitized;
f332726 [PATCH] Improve handling of "." and ".." in git-diff-*
Linus Torvalds authored
119 }
120
a6080a0 @gitster War on whitespace
gitster authored
121 /*
4ca0660 working from subdirectory: preparation
Junio C Hamano authored
122 * Unlike prefix_path, this should be used if the named file does
123 * not have to interact with index entry; i.e. name of a random file
124 * on the filesystem.
125 */
126 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
127 {
128 static char path[PATH_MAX];
ecf4831 @sprohaska Use is_absolute_path() in diff-lib.c, lockfile.c, setup.c, trace.c
sprohaska authored
129 if (!pfx || !*pfx || is_absolute_path(arg))
4ca0660 working from subdirectory: preparation
Junio C Hamano authored
130 return arg;
131 memcpy(path, pfx, pfx_len);
132 strcpy(path + pfx_len, arg);
133 return path;
134 }
135
e23d0b4 Fix filename verification when in a subdirectory
Linus Torvalds authored
136 /*
137 * Verify a filename that we got as an argument for a pathspec
138 * entry. Note that a filename that begins with "-" never verifies
139 * as true, because even if such a filename were to exist, we want
140 * it to be preceded by the "--" marker (or we want the user to
141 * use a format like "./-filename")
142 */
143 void verify_filename(const char *prefix, const char *arg)
144 {
145 const char *name;
146 struct stat st;
147
148 if (*arg == '-')
149 die("bad flag '%s' used after filename", arg);
150 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
151 if (!lstat(name, &st))
152 return;
153 if (errno == ENOENT)
ea92f41 revision parsing: make "rev -- paths" checks stronger.
Junio C Hamano authored
154 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
155 "Use '--' to separate paths from revisions", arg);
e23d0b4 Fix filename verification when in a subdirectory
Linus Torvalds authored
156 die("'%s': %s", arg, strerror(errno));
157 }
158
ea92f41 revision parsing: make "rev -- paths" checks stronger.
Junio C Hamano authored
159 /*
160 * Opposite of the above: the command line did not have -- marker
161 * and we parsed the arg as a refname. It should not be interpretable
162 * as a filename.
163 */
164 void verify_non_filename(const char *prefix, const char *arg)
165 {
166 const char *name;
167 struct stat st;
168
7ae3df8 @matled Use new semantics of is_bare/inside_git_dir/inside_work_tree
matled authored
169 if (!is_inside_work_tree() || is_inside_git_dir())
6802563 @dscho Do not verify filenames in a bare repository
dscho authored
170 return;
ea92f41 revision parsing: make "rev -- paths" checks stronger.
Junio C Hamano authored
171 if (*arg == '-')
172 return; /* flag */
173 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
174 if (!lstat(name, &st))
175 die("ambiguous argument '%s': both revision and filename\n"
176 "Use '--' to separate filenames from revisions", arg);
33a798c @gitster setup.c:verify_non_filename(): don't die unnecessarily while disambiguat...
gitster authored
177 if (errno != ENOENT && errno != ENOTDIR)
ea92f41 revision parsing: make "rev -- paths" checks stronger.
Junio C Hamano authored
178 die("'%s': %s", arg, strerror(errno));
179 }
180
6b5ee13 Diff clean-up.
Junio C Hamano authored
181 const char **get_pathspec(const char *prefix, const char **pathspec)
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
182 {
6b5ee13 Diff clean-up.
Junio C Hamano authored
183 const char *entry = *pathspec;
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
184 const char **src, **dst;
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
185 int prefixlen;
186
f332726 [PATCH] Improve handling of "." and ".." in git-diff-*
Linus Torvalds authored
187 if (!prefix && !entry)
188 return NULL;
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
189
190 if (!entry) {
191 static const char *spec[2];
192 spec[0] = prefix;
193 spec[1] = NULL;
194 return spec;
195 }
196
197 /* Otherwise we have to re-write the entries.. */
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
198 src = pathspec;
199 dst = pathspec;
f332726 [PATCH] Improve handling of "." and ".." in git-diff-*
Linus Torvalds authored
200 prefixlen = prefix ? strlen(prefix) : 0;
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
201 while (*src) {
202 const char *p = prefix_path(prefix, prefixlen, *src);
203 if (p)
204 *(dst++) = p;
3296766 @gitster get_pathspec(): die when an out-of-tree path is given
gitster authored
205 else
206 exit(128); /* error message already given */
d089eba @gitster setup: sanitize absolute and funny paths in get_pathspec()
gitster authored
207 src++;
208 }
209 *dst = NULL;
210 if (!*pathspec)
211 return NULL;
212 return pathspec;
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
213 }
214
5f5608b [PATCH] Make .git directory validation code test HEAD
Linus Torvalds authored
215 /*
ad1a382 @spearce Automatically detect a bare git repository.
spearce authored
216 * Test if it looks like we're at a git directory.
5e7bfe2 setup_git_directory: make sure GIT_DIR is a valid repository.
Junio C Hamano authored
217 * We want to see:
5f5608b [PATCH] Make .git directory validation code test HEAD
Linus Torvalds authored
218 *
790296f @meyering Fix grammar nits in documentation and in code comments.
meyering authored
219 * - either an objects/ directory _or_ the proper
5f5608b [PATCH] Make .git directory validation code test HEAD
Linus Torvalds authored
220 * GIT_OBJECT_DIRECTORY environment variable
ad1a382 @spearce Automatically detect a bare git repository.
spearce authored
221 * - a refs/ directory
8098a17 Add git-symbolic-ref
Junio C Hamano authored
222 * - either a HEAD symlink or a HEAD file that is formatted as
c847f53 Detached HEAD (experimental)
Junio C Hamano authored
223 * a proper "ref:", or a regular file HEAD that has a properly
224 * formatted sha1 object name.
5f5608b [PATCH] Make .git directory validation code test HEAD
Linus Torvalds authored
225 */
ad1a382 @spearce Automatically detect a bare git repository.
spearce authored
226 static int is_git_directory(const char *suspect)
5f5608b [PATCH] Make .git directory validation code test HEAD
Linus Torvalds authored
227 {
ad1a382 @spearce Automatically detect a bare git repository.
spearce authored
228 char path[PATH_MAX];
229 size_t len = strlen(suspect);
230
231 strcpy(path, suspect);
232 if (getenv(DB_ENVIRONMENT)) {
233 if (access(getenv(DB_ENVIRONMENT), X_OK))
234 return 0;
235 }
236 else {
237 strcpy(path + len, "/objects");
238 if (access(path, X_OK))
239 return 0;
240 }
241
242 strcpy(path + len, "/refs");
243 if (access(path, X_OK))
8098a17 Add git-symbolic-ref
Junio C Hamano authored
244 return 0;
ad1a382 @spearce Automatically detect a bare git repository.
spearce authored
245
246 strcpy(path + len, "/HEAD");
c847f53 Detached HEAD (experimental)
Junio C Hamano authored
247 if (validate_headref(path))
ad1a382 @spearce Automatically detect a bare git repository.
spearce authored
248 return 0;
249
8098a17 Add git-symbolic-ref
Junio C Hamano authored
250 return 1;
5f5608b [PATCH] Make .git directory validation code test HEAD
Linus Torvalds authored
251 }
252
6802563 @dscho Do not verify filenames in a bare repository
dscho authored
253 int is_inside_git_dir(void)
254 {
e90fdc3 @dscho Clean up work-tree handling
dscho authored
255 if (inside_git_dir < 0)
256 inside_git_dir = is_inside_dir(get_git_dir());
257 return inside_git_dir;
892c41b @matled introduce GIT_WORK_TREE to specify the work tree
matled authored
258 }
259
260 int is_inside_work_tree(void)
261 {
e90fdc3 @dscho Clean up work-tree handling
dscho authored
262 if (inside_work_tree < 0)
263 inside_work_tree = is_inside_dir(get_git_work_tree());
264 return inside_work_tree;
892c41b @matled introduce GIT_WORK_TREE to specify the work tree
matled authored
265 }
266
e90fdc3 @dscho Clean up work-tree handling
dscho authored
267 /*
7efeb8f @dscho Reinstate the old behaviour when GIT_DIR is set and GIT_WORK_TREE is uns...
dscho authored
268 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
269 * The old behaviour (which we retain here) is to set the work tree root
270 * to the cwd, unless overridden by the config, the command line, or
271 * GIT_WORK_TREE.
e90fdc3 @dscho Clean up work-tree handling
dscho authored
272 */
7efeb8f @dscho Reinstate the old behaviour when GIT_DIR is set and GIT_WORK_TREE is uns...
dscho authored
273 static const char *set_work_tree(const char *dir)
892c41b @matled introduce GIT_WORK_TREE to specify the work tree
matled authored
274 {
7efeb8f @dscho Reinstate the old behaviour when GIT_DIR is set and GIT_WORK_TREE is uns...
dscho authored
275 char buffer[PATH_MAX + 1];
e90fdc3 @dscho Clean up work-tree handling
dscho authored
276
7efeb8f @dscho Reinstate the old behaviour when GIT_DIR is set and GIT_WORK_TREE is uns...
dscho authored
277 if (!getcwd(buffer, sizeof(buffer)))
278 die ("Could not get the current working directory");
279 git_work_tree_cfg = xstrdup(buffer);
e90fdc3 @dscho Clean up work-tree handling
dscho authored
280 inside_work_tree = 1;
281
7efeb8f @dscho Reinstate the old behaviour when GIT_DIR is set and GIT_WORK_TREE is uns...
dscho authored
282 return NULL;
6802563 @dscho Do not verify filenames in a bare repository
dscho authored
283 }
284
f3fa183 @gitster Style: place opening brace of a function definition at column 1
gitster authored
285 void setup_work_tree(void)
286 {
354e653 @dscho builtin-blame: set up the work_tree before the first file access
dscho authored
287 const char *work_tree, *git_dir;
288 static int initialized = 0;
289
290 if (initialized)
291 return;
292 work_tree = get_git_work_tree();
293 git_dir = get_git_dir();
59f0f2f @glandium Refactor working tree setup
glandium authored
294 if (!is_absolute_path(git_dir))
295 set_git_dir(make_absolute_path(git_dir));
296 if (!work_tree || chdir(work_tree))
297 die("This operation must be run in a work tree");
354e653 @dscho builtin-blame: set up the work_tree before the first file access
dscho authored
298 initialized = 1;
59f0f2f @glandium Refactor working tree setup
glandium authored
299 }
300
9459aa7 @pclouds Do check_repository_format() early (re-fix)
pclouds authored
301 static int check_repository_format_gently(int *nongit_ok)
302 {
303 git_config(check_repository_format_version);
304 if (GIT_REPO_VERSION < repository_format_version) {
305 if (!nongit_ok)
306 die ("Expected git repo version <= %d, found %d",
307 GIT_REPO_VERSION, repository_format_version);
308 warning("Expected git repo version <= %d, found %d",
309 GIT_REPO_VERSION, repository_format_version);
310 warning("Please upgrade Git");
311 *nongit_ok = -1;
312 return -1;
313 }
314 return 0;
315 }
316
e90fdc3 @dscho Clean up work-tree handling
dscho authored
317 /*
b44ebb1 @hjemli Add platform-independent .git "symlink"
hjemli authored
318 * Try to read the location of the git directory from the .git file,
319 * return path to git directory if found.
320 */
321 const char *read_gitfile_gently(const char *path)
322 {
323 char *buf;
324 struct stat st;
325 int fd;
326 size_t len;
327
328 if (stat(path, &st))
329 return NULL;
330 if (!S_ISREG(st.st_mode))
331 return NULL;
332 fd = open(path, O_RDONLY);
333 if (fd < 0)
334 die("Error opening %s: %s", path, strerror(errno));
335 buf = xmalloc(st.st_size + 1);
336 len = read_in_full(fd, buf, st.st_size);
337 close(fd);
338 if (len != st.st_size)
339 die("Error reading %s", path);
340 buf[len] = '\0';
341 if (prefixcmp(buf, "gitdir: "))
342 die("Invalid gitfile format: %s", path);
343 while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
344 len--;
345 if (len < 9)
346 die("No path in gitfile: %s", path);
347 buf[len] = '\0';
348 if (!is_git_directory(buf + 8))
349 die("Not a git repository: %s", buf + 8);
350 path = make_absolute_path(buf + 8);
351 free(buf);
352 return path;
353 }
354
355 /*
e90fdc3 @dscho Clean up work-tree handling
dscho authored
356 * We cannot decide in this function whether we are in the work tree or
357 * not, since the config can only be read _after_ this function was called.
358 */
4ca0660 working from subdirectory: preparation
Junio C Hamano authored
359 const char *setup_git_directory_gently(int *nongit_ok)
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
360 {
e90fdc3 @dscho Clean up work-tree handling
dscho authored
361 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
362 static char cwd[PATH_MAX+1];
e90fdc3 @dscho Clean up work-tree handling
dscho authored
363 const char *gitdirenv;
b44ebb1 @hjemli Add platform-independent .git "symlink"
hjemli authored
364 const char *gitfile_dir;
e90fdc3 @dscho Clean up work-tree handling
dscho authored
365 int len, offset;
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
366
e90fdc3 @dscho Clean up work-tree handling
dscho authored
367 /*
af05d67 @szeder Always set *nongit_ok in setup_git_directory_gently()
szeder authored
368 * Let's assume that we are in a git repository.
369 * If it turns out later that we are somewhere else, the value will be
370 * updated accordingly.
371 */
372 if (nongit_ok)
373 *nongit_ok = 0;
374
375 /*
e90fdc3 @dscho Clean up work-tree handling
dscho authored
376 * If GIT_DIR is set explicitly, we're not going
377 * to do any discovery, but we still do repository
378 * validation.
379 */
ad1a382 @spearce Automatically detect a bare git repository.
spearce authored
380 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
e90fdc3 @dscho Clean up work-tree handling
dscho authored
381 if (gitdirenv) {
382 if (PATH_MAX - 40 < strlen(gitdirenv))
383 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
384 if (is_git_directory(gitdirenv)) {
dd5c8af @dscho Fix setup_git_directory_gently() with relative GIT_DIR & GIT_WORK_TREE
dscho authored
385 static char buffer[1024 + 1];
386 const char *retval;
387
138dd1e @pclouds Do check_repository_format() early
pclouds authored
388 if (!work_tree_env) {
389 retval = set_work_tree(gitdirenv);
9459aa7 @pclouds Do check_repository_format() early (re-fix)
pclouds authored
390 /* config may override worktree */
391 if (check_repository_format_gently(nongit_ok))
392 return NULL;
138dd1e @pclouds Do check_repository_format() early
pclouds authored
393 return retval;
394 }
9459aa7 @pclouds Do check_repository_format() early (re-fix)
pclouds authored
395 if (check_repository_format_gently(nongit_ok))
396 return NULL;
dd5c8af @dscho Fix setup_git_directory_gently() with relative GIT_DIR & GIT_WORK_TREE
dscho authored
397 retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
398 get_git_work_tree());
399 if (!retval || !*retval)
400 return NULL;
401 set_git_dir(make_absolute_path(gitdirenv));
402 if (chdir(work_tree_env) < 0)
403 die ("Could not chdir to %s", work_tree_env);
404 strcat(buffer, "/");
405 return retval;
892c41b @matled introduce GIT_WORK_TREE to specify the work tree
matled authored
406 }
3a3c3fc @dscho Fix crash when GIT_DIR is invalid
dscho authored
407 if (nongit_ok) {
41e95f6 @matled setup_git_directory_gently: do not barf when GIT_DIR is given.
matled authored
408 *nongit_ok = 1;
409 return NULL;
410 }
ad1a382 @spearce Automatically detect a bare git repository.
spearce authored
411 die("Not a git repository: '%s'", gitdirenv);
5e7bfe2 setup_git_directory: make sure GIT_DIR is a valid repository.
Junio C Hamano authored
412 }
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
413
f66a4d6 @gitster Do not check if getcwd() result begins with a slash.
gitster authored
414 if (!getcwd(cwd, sizeof(cwd)-1))
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
415 die("Unable to read current working directory");
416
892c41b @matled introduce GIT_WORK_TREE to specify the work tree
matled authored
417 /*
e90fdc3 @dscho Clean up work-tree handling
dscho authored
418 * Test in the following order (relative to the cwd):
b44ebb1 @hjemli Add platform-independent .git "symlink"
hjemli authored
419 * - .git (file containing "gitdir: <path>")
e90fdc3 @dscho Clean up work-tree handling
dscho authored
420 * - .git/
421 * - ./ (bare)
b44ebb1 @hjemli Add platform-independent .git "symlink"
hjemli authored
422 * - ../.git
e90fdc3 @dscho Clean up work-tree handling
dscho authored
423 * - ../.git/
424 * - ../ (bare)
425 * - ../../.git/
426 * etc.
892c41b @matled introduce GIT_WORK_TREE to specify the work tree
matled authored
427 */
e90fdc3 @dscho Clean up work-tree handling
dscho authored
428 offset = len = strlen(cwd);
429 for (;;) {
b44ebb1 @hjemli Add platform-independent .git "symlink"
hjemli authored
430 gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
431 if (gitfile_dir) {
432 if (set_git_dir(gitfile_dir))
433 die("Repository setup failed");
434 break;
435 }
e90fdc3 @dscho Clean up work-tree handling
dscho authored
436 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
437 break;
438 if (is_git_directory(".")) {
439 inside_git_dir = 1;
440 if (!work_tree_env)
441 inside_work_tree = 0;
442 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
9459aa7 @pclouds Do check_repository_format() early (re-fix)
pclouds authored
443 check_repository_format_gently(nongit_ok);
892c41b @matled introduce GIT_WORK_TREE to specify the work tree
matled authored
444 return NULL;
445 }
e90fdc3 @dscho Clean up work-tree handling
dscho authored
446 chdir("..");
447 do {
448 if (!offset) {
449 if (nongit_ok) {
450 if (chdir(cwd))
451 die("Cannot come back to cwd");
452 *nongit_ok = 1;
453 return NULL;
454 }
455 die("Not a git repository");
456 }
457 } while (cwd[--offset] != '/');
892c41b @matled introduce GIT_WORK_TREE to specify the work tree
matled authored
458 }
459
e90fdc3 @dscho Clean up work-tree handling
dscho authored
460 inside_git_dir = 0;
461 if (!work_tree_env)
462 inside_work_tree = 1;
463 git_work_tree_cfg = xstrndup(cwd, offset);
9459aa7 @pclouds Do check_repository_format() early (re-fix)
pclouds authored
464 if (check_repository_format_gently(nongit_ok))
465 return NULL;
e90fdc3 @dscho Clean up work-tree handling
dscho authored
466 if (offset == len)
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
467 return NULL;
468
e90fdc3 @dscho Clean up work-tree handling
dscho authored
469 /* Make "offset" point to past the '/', and add a '/' at the end */
470 offset++;
471 cwd[len++] = '/';
472 cwd[len] = 0;
473 return cwd + offset;
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored
474 }
5e7bfe2 setup_git_directory: make sure GIT_DIR is a valid repository.
Junio C Hamano authored
475
94df250 shared repository: optionally allow reading to "others".
Junio C Hamano authored
476 int git_config_perm(const char *var, const char *value)
477 {
06cbe85 @heikkiorsila Make core.sharedRepository more generic
heikkiorsila authored
478 int i;
479 char *endptr;
480
481 if (value == NULL)
482 return PERM_GROUP;
483
484 if (!strcmp(value, "umask"))
485 return PERM_UMASK;
486 if (!strcmp(value, "group"))
487 return PERM_GROUP;
488 if (!strcmp(value, "all") ||
489 !strcmp(value, "world") ||
490 !strcmp(value, "everybody"))
491 return PERM_EVERYBODY;
492
493 /* Parse octal numbers */
494 i = strtol(value, &endptr, 8);
495
496 /* If not an octal number, maybe true/false? */
497 if (*endptr != 0)
498 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
499
500 /*
501 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
502 * a chmod value.
503 */
504 switch (i) {
505 case PERM_UMASK: /* 0 */
506 return PERM_UMASK;
507 case OLD_PERM_GROUP: /* 1 */
508 return PERM_GROUP;
509 case OLD_PERM_EVERYBODY: /* 2 */
510 return PERM_EVERYBODY;
94df250 shared repository: optionally allow reading to "others".
Junio C Hamano authored
511 }
06cbe85 @heikkiorsila Make core.sharedRepository more generic
heikkiorsila authored
512
513 /* A filemode value was given: 0xxx */
514
515 if ((i & 0600) != 0600)
516 die("Problem with core.sharedRepository filemode value "
517 "(0%.3o).\nThe owner of files must always have "
518 "read and write permissions.", i);
519
520 /*
521 * Mask filemode value. Others can not get write permission.
522 * x flags for directories are handled separately.
523 */
524 return i & 0666;
94df250 shared repository: optionally allow reading to "others".
Junio C Hamano authored
525 }
526
ab9cb76 Repository format version check.
Junio C Hamano authored
527 int check_repository_format_version(const char *var, const char *value)
528 {
299726d @dscho white space fixes in setup.c
dscho authored
529 if (strcmp(var, "core.repositoryformatversion") == 0)
530 repository_format_version = git_config_int(var, value);
457f06d @dscho Introduce core.sharedrepository
dscho authored
531 else if (strcmp(var, "core.sharedrepository") == 0)
94df250 shared repository: optionally allow reading to "others".
Junio C Hamano authored
532 shared_repository = git_config_perm(var, value);
e90fdc3 @dscho Clean up work-tree handling
dscho authored
533 else if (strcmp(var, "core.bare") == 0) {
534 is_bare_repository_cfg = git_config_bool(var, value);
535 if (is_bare_repository_cfg == 1)
536 inside_work_tree = -1;
537 } else if (strcmp(var, "core.worktree") == 0) {
180483c @gitster setup.c: guard config parser from value=NULL
gitster authored
538 if (!value)
539 return config_error_nonbool(var);
8e0f700 Avoid unnecessary "if-before-free" tests.
Jim Meyering authored
540 free(git_work_tree_cfg);
e90fdc3 @dscho Clean up work-tree handling
dscho authored
541 git_work_tree_cfg = xstrdup(value);
542 inside_work_tree = -1;
543 }
299726d @dscho white space fixes in setup.c
dscho authored
544 return 0;
ab9cb76 Repository format version check.
Junio C Hamano authored
545 }
546
547 int check_repository_format(void)
548 {
9459aa7 @pclouds Do check_repository_format() early (re-fix)
pclouds authored
549 return check_repository_format_gently(NULL);
ab9cb76 Repository format version check.
Junio C Hamano authored
550 }
551
5e7bfe2 setup_git_directory: make sure GIT_DIR is a valid repository.
Junio C Hamano authored
552 const char *setup_git_directory(void)
553 {
4ca0660 working from subdirectory: preparation
Junio C Hamano authored
554 const char *retval = setup_git_directory_gently(NULL);
e90fdc3 @dscho Clean up work-tree handling
dscho authored
555
556 /* If the work tree is not the default one, recompute prefix */
557 if (inside_work_tree < 0) {
558 static char buffer[PATH_MAX + 1];
559 char *rel;
560 if (retval && chdir(retval))
561 die ("Could not jump back into original cwd");
562 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
563 return rel && *rel ? strcat(rel, "/") : NULL;
564 }
565
5e7bfe2 setup_git_directory: make sure GIT_DIR is a valid repository.
Junio C Hamano authored
566 return retval;
567 }
Something went wrong with that request. Please try again.