4 static int inside_git_dir
= -1;
5 static int inside_work_tree
= -1;
7 static int sanitary_path_copy(char *dst
, const char *src
)
21 * A path component that begins with . could be
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.
32 } else if (src
[1] == '/') {
38 } else if (src
[1] == '.') {
43 } else if (src
[2] == '/') {
53 /* copy up to the next '/', and eat all '/' */
54 while ((c
= *src
++) != '\0' && c
!= '/')
67 * dst0..dst is prefix portion, and dst[-1] is '/';
70 dst
-= 2; /* go past trailing '/' if any */
87 const char *prefix_path(const char *prefix
, int len
, const char *path
)
89 const char *orig
= path
;
90 char *sanitized
= xmalloc(len
+ strlen(path
) + 1);
91 if (is_absolute_path(orig
))
92 strcpy(sanitized
, path
);
95 memcpy(sanitized
, prefix
, len
);
96 strcpy(sanitized
+ len
, path
);
98 if (sanitary_path_copy(sanitized
, sanitized
))
100 if (is_absolute_path(orig
)) {
101 const char *work_tree
= get_git_work_tree();
102 size_t len
= strlen(work_tree
);
103 size_t total
= strlen(sanitized
) + 1;
104 if (strncmp(sanitized
, work_tree
, len
) ||
105 (sanitized
[len
] != '\0' && sanitized
[len
] != '/')) {
107 error("'%s' is outside repository", orig
);
111 if (sanitized
[len
] == '/')
113 memmove(sanitized
, sanitized
+ len
, total
- len
);
119 * Unlike prefix_path, this should be used if the named file does
120 * not have to interact with index entry; i.e. name of a random file
123 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
125 static char path
[PATH_MAX
];
126 if (!pfx
|| !*pfx
|| is_absolute_path(arg
))
128 memcpy(path
, pfx
, pfx_len
);
129 strcpy(path
+ pfx_len
, arg
);
134 * Verify a filename that we got as an argument for a pathspec
135 * entry. Note that a filename that begins with "-" never verifies
136 * as true, because even if such a filename were to exist, we want
137 * it to be preceded by the "--" marker (or we want the user to
138 * use a format like "./-filename")
140 void verify_filename(const char *prefix
, const char *arg
)
146 die("bad flag '%s' used after filename", arg
);
147 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
148 if (!lstat(name
, &st
))
151 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
152 "Use '--' to separate paths from revisions", arg
);
153 die("'%s': %s", arg
, strerror(errno
));
157 * Opposite of the above: the command line did not have -- marker
158 * and we parsed the arg as a refname. It should not be interpretable
161 void verify_non_filename(const char *prefix
, const char *arg
)
166 if (!is_inside_work_tree() || is_inside_git_dir())
170 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
171 if (!lstat(name
, &st
))
172 die("ambiguous argument '%s': both revision and filename\n"
173 "Use '--' to separate filenames from revisions", arg
);
174 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
175 die("'%s': %s", arg
, strerror(errno
));
178 const char **get_pathspec(const char *prefix
, const char **pathspec
)
180 const char *entry
= *pathspec
;
181 const char **src
, **dst
;
184 if (!prefix
&& !entry
)
188 static const char *spec
[2];
194 /* Otherwise we have to re-write the entries.. */
197 prefixlen
= prefix
? strlen(prefix
) : 0;
199 const char *p
= prefix_path(prefix
, prefixlen
, *src
);
203 exit(128); /* error message already given */
213 * Test if it looks like we're at a git directory.
216 * - either an objects/ directory _or_ the proper
217 * GIT_OBJECT_DIRECTORY environment variable
218 * - a refs/ directory
219 * - either a HEAD symlink or a HEAD file that is formatted as
220 * a proper "ref:", or a regular file HEAD that has a properly
221 * formatted sha1 object name.
223 static int is_git_directory(const char *suspect
)
226 size_t len
= strlen(suspect
);
228 strcpy(path
, suspect
);
229 if (getenv(DB_ENVIRONMENT
)) {
230 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
234 strcpy(path
+ len
, "/objects");
235 if (access(path
, X_OK
))
239 strcpy(path
+ len
, "/refs");
240 if (access(path
, X_OK
))
243 strcpy(path
+ len
, "/HEAD");
244 if (validate_headref(path
))
250 int is_inside_git_dir(void)
252 if (inside_git_dir
< 0)
253 inside_git_dir
= is_inside_dir(get_git_dir());
254 return inside_git_dir
;
257 int is_inside_work_tree(void)
259 if (inside_work_tree
< 0)
260 inside_work_tree
= is_inside_dir(get_git_work_tree());
261 return inside_work_tree
;
265 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
266 * The old behaviour (which we retain here) is to set the work tree root
267 * to the cwd, unless overridden by the config, the command line, or
270 static const char *set_work_tree(const char *dir
)
272 char buffer
[PATH_MAX
+ 1];
274 if (!getcwd(buffer
, sizeof(buffer
)))
275 die ("Could not get the current working directory");
276 git_work_tree_cfg
= xstrdup(buffer
);
277 inside_work_tree
= 1;
282 void setup_work_tree(void)
284 const char *work_tree
, *git_dir
;
285 static int initialized
= 0;
289 work_tree
= get_git_work_tree();
290 git_dir
= get_git_dir();
291 if (!is_absolute_path(git_dir
))
292 set_git_dir(make_absolute_path(git_dir
));
293 if (!work_tree
|| chdir(work_tree
))
294 die("This operation must be run in a work tree");
298 static int check_repository_format_gently(int *nongit_ok
)
300 git_config(check_repository_format_version
, NULL
);
301 if (GIT_REPO_VERSION
< repository_format_version
) {
303 die ("Expected git repo version <= %d, found %d",
304 GIT_REPO_VERSION
, repository_format_version
);
305 warning("Expected git repo version <= %d, found %d",
306 GIT_REPO_VERSION
, repository_format_version
);
307 warning("Please upgrade Git");
315 * Try to read the location of the git directory from the .git file,
316 * return path to git directory if found.
318 const char *read_gitfile_gently(const char *path
)
327 if (!S_ISREG(st
.st_mode
))
329 fd
= open(path
, O_RDONLY
);
331 die("Error opening %s: %s", path
, strerror(errno
));
332 buf
= xmalloc(st
.st_size
+ 1);
333 len
= read_in_full(fd
, buf
, st
.st_size
);
335 if (len
!= st
.st_size
)
336 die("Error reading %s", path
);
338 if (prefixcmp(buf
, "gitdir: "))
339 die("Invalid gitfile format: %s", path
);
340 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
343 die("No path in gitfile: %s", path
);
345 if (!is_git_directory(buf
+ 8))
346 die("Not a git repository: %s", buf
+ 8);
347 path
= make_absolute_path(buf
+ 8);
353 * We cannot decide in this function whether we are in the work tree or
354 * not, since the config can only be read _after_ this function was called.
356 const char *setup_git_directory_gently(int *nongit_ok
)
358 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
359 static char cwd
[PATH_MAX
+1];
360 const char *gitdirenv
;
361 const char *gitfile_dir
;
365 * Let's assume that we are in a git repository.
366 * If it turns out later that we are somewhere else, the value will be
367 * updated accordingly.
373 * If GIT_DIR is set explicitly, we're not going
374 * to do any discovery, but we still do repository
377 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
379 if (PATH_MAX
- 40 < strlen(gitdirenv
))
380 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
381 if (is_git_directory(gitdirenv
)) {
382 static char buffer
[1024 + 1];
385 if (!work_tree_env
) {
386 retval
= set_work_tree(gitdirenv
);
387 /* config may override worktree */
388 if (check_repository_format_gently(nongit_ok
))
392 if (check_repository_format_gently(nongit_ok
))
394 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
395 get_git_work_tree());
396 if (!retval
|| !*retval
)
398 set_git_dir(make_absolute_path(gitdirenv
));
399 if (chdir(work_tree_env
) < 0)
400 die ("Could not chdir to %s", work_tree_env
);
408 die("Not a git repository: '%s'", gitdirenv
);
411 if (!getcwd(cwd
, sizeof(cwd
)-1))
412 die("Unable to read current working directory");
415 * Test in the following order (relative to the cwd):
416 * - .git (file containing "gitdir: <path>")
425 offset
= len
= strlen(cwd
);
427 gitfile_dir
= read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT
);
429 if (set_git_dir(gitfile_dir
))
430 die("Repository setup failed");
433 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
435 if (is_git_directory(".")) {
438 inside_work_tree
= 0;
439 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
440 check_repository_format_gently(nongit_ok
);
448 die("Cannot come back to cwd");
452 die("Not a git repository");
454 } while (cwd
[--offset
] != '/');
459 inside_work_tree
= 1;
460 git_work_tree_cfg
= xstrndup(cwd
, offset
);
461 if (check_repository_format_gently(nongit_ok
))
466 /* Make "offset" point to past the '/', and add a '/' at the end */
473 int git_config_perm(const char *var
, const char *value
)
481 if (!strcmp(value
, "umask"))
483 if (!strcmp(value
, "group"))
485 if (!strcmp(value
, "all") ||
486 !strcmp(value
, "world") ||
487 !strcmp(value
, "everybody"))
488 return PERM_EVERYBODY
;
490 /* Parse octal numbers */
491 i
= strtol(value
, &endptr
, 8);
493 /* If not an octal number, maybe true/false? */
495 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
498 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
502 case PERM_UMASK
: /* 0 */
504 case OLD_PERM_GROUP
: /* 1 */
506 case OLD_PERM_EVERYBODY
: /* 2 */
507 return PERM_EVERYBODY
;
510 /* A filemode value was given: 0xxx */
512 if ((i
& 0600) != 0600)
513 die("Problem with core.sharedRepository filemode value "
514 "(0%.3o).\nThe owner of files must always have "
515 "read and write permissions.", i
);
518 * Mask filemode value. Others can not get write permission.
519 * x flags for directories are handled separately.
524 int check_repository_format_version(const char *var
, const char *value
, void *cb
)
526 if (strcmp(var
, "core.repositoryformatversion") == 0)
527 repository_format_version
= git_config_int(var
, value
);
528 else if (strcmp(var
, "core.sharedrepository") == 0)
529 shared_repository
= git_config_perm(var
, value
);
530 else if (strcmp(var
, "core.bare") == 0) {
531 is_bare_repository_cfg
= git_config_bool(var
, value
);
532 if (is_bare_repository_cfg
== 1)
533 inside_work_tree
= -1;
534 } else if (strcmp(var
, "core.worktree") == 0) {
536 return config_error_nonbool(var
);
537 free(git_work_tree_cfg
);
538 git_work_tree_cfg
= xstrdup(value
);
539 inside_work_tree
= -1;
544 int check_repository_format(void)
546 return check_repository_format_gently(NULL
);
549 const char *setup_git_directory(void)
551 const char *retval
= setup_git_directory_gently(NULL
);
553 /* If the work tree is not the default one, recompute prefix */
554 if (inside_work_tree
< 0) {
555 static char buffer
[PATH_MAX
+ 1];
557 if (retval
&& chdir(retval
))
558 die ("Could not jump back into original cwd");
559 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
560 return rel
&& *rel
? strcat(rel
, "/") : NULL
;