4 static int inside_git_dir
= -1;
5 static int inside_work_tree
= -1;
7 char *prefix_path(const char *prefix
, int len
, const char *path
)
9 const char *orig
= path
;
10 char *sanitized
= xmalloc(len
+ strlen(path
) + 1);
11 if (is_absolute_path(orig
))
12 strcpy(sanitized
, path
);
15 memcpy(sanitized
, prefix
, len
);
16 strcpy(sanitized
+ len
, path
);
18 if (normalize_path_copy(sanitized
, sanitized
))
20 if (is_absolute_path(orig
)) {
21 size_t root_len
, len
, total
;
22 const char *work_tree
= get_git_work_tree();
25 len
= strlen(work_tree
);
26 root_len
= offset_1st_component(work_tree
);
27 total
= strlen(sanitized
) + 1;
28 if (strncmp(sanitized
, work_tree
, len
) ||
29 (len
> root_len
&& sanitized
[len
] != '\0' && sanitized
[len
] != '/')) {
31 die("'%s' is outside repository", orig
);
33 if (sanitized
[len
] == '/')
35 memmove(sanitized
, sanitized
+ len
, total
- len
);
41 * Unlike prefix_path, this should be used if the named file does
42 * not have to interact with index entry; i.e. name of a random file
45 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
47 static char path
[PATH_MAX
];
49 if (!pfx_len
|| is_absolute_path(arg
))
51 memcpy(path
, pfx
, pfx_len
);
52 strcpy(path
+ pfx_len
, arg
);
55 /* don't add prefix to absolute paths, but still replace '\' by '/' */
56 if (is_absolute_path(arg
))
59 memcpy(path
, pfx
, pfx_len
);
60 strcpy(path
+ pfx_len
, arg
);
61 for (p
= path
+ pfx_len
; *p
; p
++)
68 int check_filename(const char *prefix
, const char *arg
)
73 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
74 if (!lstat(name
, &st
))
75 return 1; /* file exists */
76 if (errno
== ENOENT
|| errno
== ENOTDIR
)
77 return 0; /* file does not exist */
78 die_errno("failed to stat '%s'", arg
);
81 static void NORETURN
die_verify_filename(const char *prefix
, const char *arg
)
83 unsigned char sha1
[20];
85 /* try a detailed diagnostic ... */
86 get_sha1_with_mode_1(arg
, sha1
, &mode
, 0, prefix
);
87 /* ... or fall back the most general message. */
88 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
89 "Use '--' to separate paths from revisions", arg
);
94 * Verify a filename that we got as an argument for a pathspec
95 * entry. Note that a filename that begins with "-" never verifies
96 * as true, because even if such a filename were to exist, we want
97 * it to be preceded by the "--" marker (or we want the user to
98 * use a format like "./-filename")
100 void verify_filename(const char *prefix
, const char *arg
)
103 die("bad flag '%s' used after filename", arg
);
104 if (check_filename(prefix
, arg
))
106 die_verify_filename(prefix
, arg
);
110 * Opposite of the above: the command line did not have -- marker
111 * and we parsed the arg as a refname. It should not be interpretable
114 void verify_non_filename(const char *prefix
, const char *arg
)
116 if (!is_inside_work_tree() || is_inside_git_dir())
120 if (!check_filename(prefix
, arg
))
122 die("ambiguous argument '%s': both revision and filename\n"
123 "Use '--' to separate filenames from revisions", arg
);
126 const char **get_pathspec(const char *prefix
, const char **pathspec
)
128 const char *entry
= *pathspec
;
129 const char **src
, **dst
;
132 if (!prefix
&& !entry
)
136 static const char *spec
[2];
142 /* Otherwise we have to re-write the entries.. */
145 prefixlen
= prefix
? strlen(prefix
) : 0;
147 const char *p
= prefix_path(prefix
, prefixlen
, *src
);
158 * Test if it looks like we're at a git directory.
161 * - either an objects/ directory _or_ the proper
162 * GIT_OBJECT_DIRECTORY environment variable
163 * - a refs/ directory
164 * - either a HEAD symlink or a HEAD file that is formatted as
165 * a proper "ref:", or a regular file HEAD that has a properly
166 * formatted sha1 object name.
168 static int is_git_directory(const char *suspect
)
171 size_t len
= strlen(suspect
);
173 if (PATH_MAX
<= len
+ strlen("/objects"))
174 die("Too long path: %.*s", 60, suspect
);
175 strcpy(path
, suspect
);
176 if (getenv(DB_ENVIRONMENT
)) {
177 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
181 strcpy(path
+ len
, "/objects");
182 if (access(path
, X_OK
))
186 strcpy(path
+ len
, "/refs");
187 if (access(path
, X_OK
))
190 strcpy(path
+ len
, "/HEAD");
191 if (validate_headref(path
))
197 int is_inside_git_dir(void)
199 if (inside_git_dir
< 0)
200 inside_git_dir
= is_inside_dir(get_git_dir());
201 return inside_git_dir
;
204 int is_inside_work_tree(void)
206 if (inside_work_tree
< 0)
207 inside_work_tree
= is_inside_dir(get_git_work_tree());
208 return inside_work_tree
;
211 void setup_work_tree(void)
213 const char *work_tree
, *git_dir
;
214 static int initialized
= 0;
218 work_tree
= get_git_work_tree();
219 git_dir
= get_git_dir();
220 if (!is_absolute_path(git_dir
))
221 git_dir
= make_absolute_path(git_dir
);
222 if (!work_tree
|| chdir(work_tree
))
223 die("This operation must be run in a work tree");
226 * Make sure subsequent git processes find correct worktree
227 * if $GIT_WORK_TREE is set relative
229 if (getenv(GIT_WORK_TREE_ENVIRONMENT
))
230 setenv(GIT_WORK_TREE_ENVIRONMENT
, ".", 1);
232 set_git_dir(make_relative_path(git_dir
, work_tree
));
236 static int check_repository_format_gently(const char *gitdir
, int *nongit_ok
)
238 char repo_config
[PATH_MAX
+1];
241 * git_config() can't be used here because it calls git_pathdup()
242 * to get $GIT_CONFIG/config. That call will make setup_git_env()
243 * set git_dir to ".git".
245 * We are in gitdir setup, no git dir has been found useable yet.
246 * Use a gentler version of git_config() to check if this repo
249 snprintf(repo_config
, PATH_MAX
, "%s/config", gitdir
);
250 git_config_early(check_repository_format_version
, NULL
, repo_config
);
251 if (GIT_REPO_VERSION
< repository_format_version
) {
253 die ("Expected git repo version <= %d, found %d",
254 GIT_REPO_VERSION
, repository_format_version
);
255 warning("Expected git repo version <= %d, found %d",
256 GIT_REPO_VERSION
, repository_format_version
);
257 warning("Please upgrade Git");
265 * Try to read the location of the git directory from the .git file,
266 * return path to git directory if found.
268 const char *read_gitfile_gently(const char *path
)
279 if (!S_ISREG(st
.st_mode
))
281 fd
= open(path
, O_RDONLY
);
283 die_errno("Error opening '%s'", path
);
284 buf
= xmalloc(st
.st_size
+ 1);
285 len
= read_in_full(fd
, buf
, st
.st_size
);
287 if (len
!= st
.st_size
)
288 die("Error reading %s", path
);
290 if (prefixcmp(buf
, "gitdir: "))
291 die("Invalid gitfile format: %s", path
);
292 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
295 die("No path in gitfile: %s", path
);
299 if (!is_absolute_path(dir
) && (slash
= strrchr(path
, '/'))) {
300 size_t pathlen
= slash
+1 - path
;
301 size_t dirlen
= pathlen
+ len
- 8;
302 dir
= xmalloc(dirlen
+ 1);
303 strncpy(dir
, path
, pathlen
);
304 strncpy(dir
+ pathlen
, buf
+ 8, len
- 8);
310 if (!is_git_directory(dir
))
311 die("Not a git repository: %s", dir
);
312 path
= make_absolute_path(dir
);
318 static const char *setup_explicit_git_dir(const char *gitdirenv
,
322 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
323 const char *worktree
;
326 if (PATH_MAX
- 40 < strlen(gitdirenv
))
327 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
329 gitfile
= (char*)read_gitfile_gently(gitdirenv
);
331 gitfile
= xstrdup(gitfile
);
335 if (!is_git_directory(gitdirenv
)) {
341 die("Not a git repository: '%s'", gitdirenv
);
344 if (check_repository_format_gently(gitdirenv
, nongit_ok
)) {
349 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
351 set_git_work_tree(work_tree_env
);
352 else if (is_bare_repository_cfg
> 0) {
353 if (git_work_tree_cfg
) /* #22.2, #30 */
354 die("core.bare and core.worktree do not make sense");
357 set_git_dir(gitdirenv
);
361 else if (git_work_tree_cfg
) { /* #6, #14 */
362 if (is_absolute_path(git_work_tree_cfg
))
363 set_git_work_tree(git_work_tree_cfg
);
365 char core_worktree
[PATH_MAX
];
366 if (chdir(gitdirenv
))
367 die_errno("Could not chdir to '%s'", gitdirenv
);
368 if (chdir(git_work_tree_cfg
))
369 die_errno("Could not chdir to '%s'", git_work_tree_cfg
);
370 if (!getcwd(core_worktree
, PATH_MAX
))
371 die_errno("Could not get directory '%s'", git_work_tree_cfg
);
373 die_errno("Could not come back to cwd");
374 set_git_work_tree(core_worktree
);
378 set_git_work_tree(".");
380 /* set_git_work_tree() must have been called by now */
381 worktree
= get_git_work_tree();
383 /* both get_git_work_tree() and cwd are already normalized */
384 if (!strcmp(cwd
, worktree
)) { /* cwd == worktree */
385 set_git_dir(gitdirenv
);
390 if (!prefixcmp(cwd
, worktree
) &&
391 cwd
[strlen(worktree
)] == '/') { /* cwd inside worktree */
392 set_git_dir(make_absolute_path(gitdirenv
));
394 die_errno("Could not chdir to '%s'", worktree
);
398 return cwd
+ strlen(worktree
) + 1;
401 /* cwd outside worktree */
402 set_git_dir(gitdirenv
);
407 static const char *setup_discovered_git_dir(const char *gitdir
,
408 char *cwd
, int offset
, int len
,
411 if (check_repository_format_gently(gitdir
, nongit_ok
))
414 /* --work-tree is set without --git-dir; use discovered one */
415 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
416 if (offset
!= len
&& !is_absolute_path(gitdir
))
417 gitdir
= xstrdup(make_absolute_path(gitdir
));
419 die_errno("Could not come back to cwd");
420 return setup_explicit_git_dir(gitdir
, cwd
, len
, nongit_ok
);
423 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
424 if (is_bare_repository_cfg
> 0) {
425 set_git_dir(offset
== len
? gitdir
: make_absolute_path(gitdir
));
427 die_errno("Could not come back to cwd");
431 /* #0, #1, #5, #8, #9, #12, #13 */
432 set_git_work_tree(".");
433 if (strcmp(gitdir
, DEFAULT_GIT_DIR_ENVIRONMENT
))
436 inside_work_tree
= 1;
440 /* Make "offset" point to past the '/', and add a '/' at the end */
447 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
448 static const char *setup_bare_git_dir(char *cwd
, int offset
, int len
, int *nongit_ok
)
452 if (check_repository_format_gently(".", nongit_ok
))
455 /* --work-tree is set without --git-dir; use discovered one */
456 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
459 gitdir
= offset
== len
? "." : xmemdupz(cwd
, offset
);
461 die_errno("Could not come back to cwd");
462 return setup_explicit_git_dir(gitdir
, cwd
, len
, nongit_ok
);
466 inside_work_tree
= 0;
469 die_errno("Cannot come back to cwd");
470 root_len
= offset_1st_component(cwd
);
471 cwd
[offset
> root_len
? offset
: root_len
] = '\0';
479 static const char *setup_nongit(const char *cwd
, int *nongit_ok
)
482 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT
);
484 die_errno("Cannot come back to cwd");
489 static dev_t
get_device_or_die(const char *path
, const char *prefix
)
492 if (stat(path
, &buf
))
493 die_errno("failed to stat '%s%s%s'",
494 prefix
? prefix
: "",
495 prefix
? "/" : "", path
);
500 * We cannot decide in this function whether we are in the work tree or
501 * not, since the config can only be read _after_ this function was called.
503 static const char *setup_git_directory_gently_1(int *nongit_ok
)
505 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
506 static char cwd
[PATH_MAX
+1];
507 const char *gitdirenv
, *ret
;
509 int len
, offset
, ceil_offset
;
510 dev_t current_device
= 0;
511 int one_filesystem
= 1;
514 * Let's assume that we are in a git repository.
515 * If it turns out later that we are somewhere else, the value will be
516 * updated accordingly.
521 if (!getcwd(cwd
, sizeof(cwd
)-1))
522 die_errno("Unable to read current working directory");
523 offset
= len
= strlen(cwd
);
526 * If GIT_DIR is set explicitly, we're not going
527 * to do any discovery, but we still do repository
530 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
532 return setup_explicit_git_dir(gitdirenv
, cwd
, len
, nongit_ok
);
534 ceil_offset
= longest_ancestor_length(cwd
, env_ceiling_dirs
);
535 if (ceil_offset
< 0 && has_dos_drive_prefix(cwd
))
539 * Test in the following order (relative to the cwd):
540 * - .git (file containing "gitdir: <path>")
549 one_filesystem
= !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
551 current_device
= get_device_or_die(".", NULL
);
553 gitfile
= (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT
);
555 gitdirenv
= gitfile
= xstrdup(gitfile
);
557 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
558 gitdirenv
= DEFAULT_GIT_DIR_ENVIRONMENT
;
562 ret
= setup_discovered_git_dir(gitdirenv
,
570 if (is_git_directory("."))
571 return setup_bare_git_dir(cwd
, offset
, len
, nongit_ok
);
573 while (--offset
> ceil_offset
&& cwd
[offset
] != '/');
574 if (offset
<= ceil_offset
)
575 return setup_nongit(cwd
, nongit_ok
);
576 if (one_filesystem
) {
577 dev_t parent_device
= get_device_or_die("..", cwd
);
578 if (parent_device
!= current_device
) {
581 die_errno("Cannot come back to cwd");
586 die("Not a git repository (or any parent up to mount parent %s)\n"
587 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd
);
592 die_errno("Cannot change to '%s/..'", cwd
);
597 const char *setup_git_directory_gently(int *nongit_ok
)
601 prefix
= setup_git_directory_gently_1(nongit_ok
);
603 startup_info
->have_repository
= !nongit_ok
|| !*nongit_ok
;
604 startup_info
->prefix
= prefix
;
609 int git_config_perm(const char *var
, const char *value
)
617 if (!strcmp(value
, "umask"))
619 if (!strcmp(value
, "group"))
621 if (!strcmp(value
, "all") ||
622 !strcmp(value
, "world") ||
623 !strcmp(value
, "everybody"))
624 return PERM_EVERYBODY
;
626 /* Parse octal numbers */
627 i
= strtol(value
, &endptr
, 8);
629 /* If not an octal number, maybe true/false? */
631 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
634 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
635 * a chmod value to restrict to.
638 case PERM_UMASK
: /* 0 */
640 case OLD_PERM_GROUP
: /* 1 */
642 case OLD_PERM_EVERYBODY
: /* 2 */
643 return PERM_EVERYBODY
;
646 /* A filemode value was given: 0xxx */
648 if ((i
& 0600) != 0600)
649 die("Problem with core.sharedRepository filemode value "
650 "(0%.3o).\nThe owner of files must always have "
651 "read and write permissions.", i
);
654 * Mask filemode value. Others can not get write permission.
655 * x flags for directories are handled separately.
660 int check_repository_format_version(const char *var
, const char *value
, void *cb
)
662 if (strcmp(var
, "core.repositoryformatversion") == 0)
663 repository_format_version
= git_config_int(var
, value
);
664 else if (strcmp(var
, "core.sharedrepository") == 0)
665 shared_repository
= git_config_perm(var
, value
);
666 else if (strcmp(var
, "core.bare") == 0) {
667 is_bare_repository_cfg
= git_config_bool(var
, value
);
668 if (is_bare_repository_cfg
== 1)
669 inside_work_tree
= -1;
670 } else if (strcmp(var
, "core.worktree") == 0) {
672 return config_error_nonbool(var
);
673 free(git_work_tree_cfg
);
674 git_work_tree_cfg
= xstrdup(value
);
675 inside_work_tree
= -1;
680 int check_repository_format(void)
682 return check_repository_format_gently(get_git_dir(), NULL
);
686 * Returns the "prefix", a path to the current working directory
687 * relative to the work tree root, or NULL, if the current working
688 * directory is not a strict subdirectory of the work tree root. The
689 * prefix always ends with a '/' character.
691 const char *setup_git_directory(void)
693 return setup_git_directory_gently(NULL
);