/* * Try to read the location of the git directory from the .git file, * return path to git directory if found. */ const char *read_gitfile_gently(const char *path) { char *buf; struct stat st; int fd; size_t len; if (stat(path, &st)) return NULL; if (!S_ISREG(st.st_mode)) return NULL; fd = open(path, O_RDONLY); if (fd < 0) die_errno("Error opening '%s'", path); buf = xmalloc(st.st_size + 1); len = read_in_full(fd, buf, st.st_size); close(fd); if (len != st.st_size) die("Error reading %s", path); buf[len] = '\0'; if (prefixcmp(buf, "gitdir: ")) die("Invalid gitfile format: %s", path); while (buf[len - 1] == '\n' || buf[len - 1] == '\r') len--; if (len < 9) die("No path in gitfile: %s", path); buf[len] = '\0'; if (!is_git_directory(buf + 8)) die("Not a git repository: %s", buf + 8); path = make_absolute_path(buf + 8); free(buf); return path; }
/* * Try to read the location of the git directory from the .git file, * return path to git directory if found. */ const char *read_gitfile(const char *path) { char *buf; char *dir; const char *slash; struct stat st; int fd; ssize_t len; if (stat(path, &st)) return NULL; if (!S_ISREG(st.st_mode)) return NULL; fd = open(path, O_RDONLY); if (fd < 0) die_errno("Error opening '%s'", path); buf = xmalloc(st.st_size + 1); len = read_in_full(fd, buf, st.st_size); close(fd); if (len != st.st_size) die("Error reading %s", path); buf[len] = '\0'; if (!starts_with(buf, "gitdir: ")) die("Invalid gitfile format: %s", path); while (buf[len - 1] == '\n' || buf[len - 1] == '\r') len--; if (len < 9) die("No path in gitfile: %s", path); buf[len] = '\0'; dir = buf + 8; if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { size_t pathlen = slash+1 - path; size_t dirlen = pathlen + len - 8; dir = xmalloc(dirlen + 1); strncpy(dir, path, pathlen); strncpy(dir + pathlen, buf + 8, len - 8); dir[dirlen] = '\0'; free(buf); buf = dir; } if (!is_git_directory(dir)) die("Not a git repository: %s", dir); update_linked_gitdir(path, dir); path = real_path(dir); free(buf); return path; }
int is_nonbare_repository_dir(struct strbuf *path) { int ret = 0; int gitfile_error; size_t orig_path_len = path->len; assert(orig_path_len != 0); strbuf_complete(path, '/'); strbuf_addstr(path, ".git"); if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf)) ret = 1; if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED || gitfile_error == READ_GITFILE_ERR_READ_FAILED) ret = 1; strbuf_setlen(path, orig_path_len); return ret; }
/* * Prep grep structures for a submodule grep * sha1: the sha1 of the submodule or NULL if using the working tree * filename: name of the submodule including tree name of parent * path: location of the submodule */ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1, const char *filename, const char *path) { if (!is_submodule_initialized(path)) return 0; if (!is_submodule_populated(path)) { /* * If searching history, check for the presense of the * submodule's gitdir before skipping the submodule. */ if (sha1) { const struct submodule *sub = submodule_from_path(null_sha1, path); if (sub) path = git_path("modules/%s", sub->name); if (!(is_directory(path) && is_git_directory(path))) return 0; } else { return 0; } } #ifndef NO_PTHREADS if (num_threads) { add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, sha1); return 0; } else #endif { struct work_item w; int hit; grep_source_init(&w.source, GREP_SOURCE_SUBMODULE, filename, path, sha1); strbuf_init(&w.out, 0); opt->output_priv = &w; hit = grep_submodule_launch(opt, &w.source); write_or_die(1, w.out.buf, w.out.len); grep_source_clear(&w.source); strbuf_release(&w.out); return hit; } }
static const char *get_repo_path_1(struct strbuf *path, int *is_bundle) { static char *suffix[] = { "/.git", "", ".git/.git", ".git" }; static char *bundle_suffix[] = { ".bundle", "" }; size_t baselen = path->len; struct stat st; int i; for (i = 0; i < ARRAY_SIZE(suffix); i++) { strbuf_setlen(path, baselen); strbuf_addstr(path, suffix[i]); if (stat(path->buf, &st)) continue; if (S_ISDIR(st.st_mode) && is_git_directory(path->buf)) { *is_bundle = 0; return path->buf; } else if (S_ISREG(st.st_mode) && st.st_size > 8) { /* Is it a "gitfile"? */ char signature[8]; const char *dst; int len, fd = open(path->buf, O_RDONLY); if (fd < 0) continue; len = read_in_full(fd, signature, 8); close(fd); if (len != 8 || strncmp(signature, "gitdir: ", 8)) continue; dst = read_gitfile(path->buf); if (dst) { *is_bundle = 0; return dst; } } } for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) { strbuf_setlen(path, baselen); strbuf_addstr(path, bundle_suffix[i]); if (!stat(path->buf, &st) && S_ISREG(st.st_mode)) { *is_bundle = 1; return path->buf; } } return NULL; }
static int do_submodule_path(struct strbuf *buf, const char *path, const char *fmt, va_list args) { const char *git_dir; struct strbuf git_submodule_common_dir = STRBUF_INIT; struct strbuf git_submodule_dir = STRBUF_INIT; const struct submodule *sub; int err = 0; strbuf_addstr(buf, path); strbuf_complete(buf, '/'); strbuf_addstr(buf, ".git"); git_dir = read_gitfile(buf->buf); if (git_dir) { strbuf_reset(buf); strbuf_addstr(buf, git_dir); } if (!is_git_directory(buf->buf)) { gitmodules_config(); sub = submodule_from_path(null_sha1, path); if (!sub) { err = SUBMODULE_PATH_ERR_NOT_CONFIGURED; goto cleanup; } strbuf_reset(buf); strbuf_git_path(buf, "%s/%s", "modules", sub->name); } strbuf_addch(buf, '/'); strbuf_addbuf(&git_submodule_dir, buf); strbuf_vaddf(buf, fmt, args); if (get_common_dir_noenv(&git_submodule_common_dir, git_submodule_dir.buf)) update_common_dir(buf, git_submodule_dir.len, git_submodule_common_dir.buf); strbuf_cleanup_path(buf); cleanup: strbuf_release(&git_submodule_dir); strbuf_release(&git_submodule_common_dir); return err; }
static char *get_repo_path(const char *repo, int *is_bundle) { static char *suffix[] = { "/.git", "", ".git/.git", ".git" }; static char *bundle_suffix[] = { ".bundle", "" }; struct stat st; int i; for (i = 0; i < ARRAY_SIZE(suffix); i++) { const char *path; path = mkpath("%s%s", repo, suffix[i]); if (stat(path, &st)) continue; if (S_ISDIR(st.st_mode) && is_git_directory(path)) { *is_bundle = 0; return xstrdup(absolute_path(path)); } else if (S_ISREG(st.st_mode) && st.st_size > 8) { /* Is it a "gitfile"? */ char signature[8]; int len, fd = open(path, O_RDONLY); if (fd < 0) continue; len = read_in_full(fd, signature, 8); close(fd); if (len != 8 || strncmp(signature, "gitdir: ", 8)) continue; path = read_gitfile(path); if (path) { *is_bundle = 0; return xstrdup(absolute_path(path)); } } } for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) { const char *path; path = mkpath("%s%s", repo, bundle_suffix[i]); if (!stat(path, &st) && S_ISREG(st.st_mode)) { *is_bundle = 1; return xstrdup(absolute_path(path)); } } return NULL; }
int find_git_root_directory(char* cwd, size_t cwd_size) { int offset; if (!getcwd(cwd, cwd_size - 1)) { fprintf(stderr, "Unable to read cwd"); return 1; } offset = strlen(cwd); while (offset > 1) { if (is_git_directory(cwd)) { printf(".git found: %s/.git\n", cwd); return 0; } while (offset-- && cwd[offset] != '/') ; cwd[offset] = '\0'; } return 0; }
/* * First, one directory to try is determined by the following algorithm. * * (0) If "strict" is given, the path is used as given and no DWIM is * done. Otherwise: * (1) "~/path" to mean path under the running user's home directory; * (2) "~user/path" to mean path under named user's home directory; * (3) "relative/path" to mean cwd relative directory; or * (4) "/absolute/path" to mean absolute directory. * * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git" * in this order. We select the first one that is a valid git repository, and * chdir() to it. If none match, or we fail to chdir, we return NULL. * * If all goes well, we return the directory we used to chdir() (but * before ~user is expanded), avoiding getcwd() resolving symbolic * links. User relative paths are also returned as they are given, * except DWIM suffixing. */ const char *enter_repo(const char *path, int strict) { static struct strbuf validated_path = STRBUF_INIT; static struct strbuf used_path = STRBUF_INIT; if (!path) return NULL; if (!strict) { static const char *suffix[] = { "/.git", "", ".git/.git", ".git", NULL, }; const char *gitfile; int len = strlen(path); int i; while ((1 < len) && (path[len-1] == '/')) len--; /* * We can handle arbitrary-sized buffers, but this remains as a * sanity check on untrusted input. */ if (PATH_MAX <= len) return NULL; strbuf_reset(&used_path); strbuf_reset(&validated_path); strbuf_add(&used_path, path, len); strbuf_add(&validated_path, path, len); if (used_path.buf[0] == '~') { char *newpath = expand_user_path(used_path.buf); if (!newpath) return NULL; strbuf_attach(&used_path, newpath, strlen(newpath), strlen(newpath)); } for (i = 0; suffix[i]; i++) { struct stat st; size_t baselen = used_path.len; strbuf_addstr(&used_path, suffix[i]); if (!stat(used_path.buf, &st) && (S_ISREG(st.st_mode) || (S_ISDIR(st.st_mode) && is_git_directory(used_path.buf)))) { strbuf_addstr(&validated_path, suffix[i]); break; } strbuf_setlen(&used_path, baselen); } if (!suffix[i]) return NULL; gitfile = read_gitfile(used_path.buf); if (gitfile) { strbuf_reset(&used_path); strbuf_addstr(&used_path, gitfile); } if (chdir(used_path.buf)) return NULL; path = validated_path.buf; } else { const char *gitfile = read_gitfile(path); if (gitfile) path = gitfile; if (chdir(path)) return NULL; } if (is_git_directory(".")) { set_git_dir("."); check_repository_format(); return path; } return NULL; }
/* * We cannot decide in this function whether we are in the work tree or * not, since the config can only be read _after_ this function was called. */ const char *setup_git_directory_gently(int *nongit_ok) { const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); static char cwd[PATH_MAX+1]; const char *gitdirenv; const char *gitfile_dir; int len, offset, ceil_offset; /* * Let's assume that we are in a git repository. * If it turns out later that we are somewhere else, the value will be * updated accordingly. */ if (nongit_ok) *nongit_ok = 0; /* * If GIT_DIR is set explicitly, we're not going * to do any discovery, but we still do repository * validation. */ gitdirenv = getenv(GIT_DIR_ENVIRONMENT); if (gitdirenv) { if (PATH_MAX - 40 < strlen(gitdirenv)) die("'$%s' too big", GIT_DIR_ENVIRONMENT); if (is_git_directory(gitdirenv)) { static char buffer[1024 + 1]; const char *retval; if (!work_tree_env) { retval = set_work_tree(gitdirenv); /* config may override worktree */ if (check_repository_format_gently(nongit_ok)) return NULL; return retval; } if (check_repository_format_gently(nongit_ok)) return NULL; retval = get_relative_cwd(buffer, sizeof(buffer) - 1, get_git_work_tree()); if (!retval || !*retval) return NULL; set_git_dir(make_absolute_path(gitdirenv)); if (chdir(work_tree_env) < 0) die_errno ("Could not chdir to '%s'", work_tree_env); strcat(buffer, "/"); return retval; } if (nongit_ok) { *nongit_ok = 1; return NULL; } die("Not a git repository: '%s'", gitdirenv); } if (!getcwd(cwd, sizeof(cwd)-1)) die_errno("Unable to read current working directory"); ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs); if (ceil_offset < 0 && has_dos_drive_prefix(cwd)) ceil_offset = 1; /* * Test in the following order (relative to the cwd): * - .git (file containing "gitdir: <path>") * - .git/ * - ./ (bare) * - ../.git * - ../.git/ * - ../ (bare) * - ../../.git/ * etc. */ offset = len = strlen(cwd); for (;;) { gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); if (gitfile_dir) { if (set_git_dir(gitfile_dir)) die("Repository setup failed"); break; } if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) break; if (is_git_directory(".")) { inside_git_dir = 1; if (!work_tree_env) inside_work_tree = 0; if (offset != len) { cwd[offset] = '\0'; setenv(GIT_DIR_ENVIRONMENT, cwd, 1); } else setenv(GIT_DIR_ENVIRONMENT, ".", 1); check_repository_format_gently(nongit_ok); return NULL; } while (--offset > ceil_offset && cwd[offset] != '/'); if (offset <= ceil_offset) { if (nongit_ok) { if (chdir(cwd)) die_errno("Cannot come back to cwd"); *nongit_ok = 1; return NULL; } die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT); } if (chdir("..")) die_errno("Cannot change to '%s/..'", cwd); } inside_git_dir = 0; if (!work_tree_env) inside_work_tree = 1; git_work_tree_cfg = xstrndup(cwd, offset); if (check_repository_format_gently(nongit_ok)) return NULL; if (offset == len) return NULL; /* Make "offset" point to past the '/', and add a '/' at the end */ offset++; cwd[len++] = '/'; cwd[len] = 0; return cwd + offset; }
/* * We cannot decide in this function whether we are in the work tree or * not, since the config can only be read _after_ this function was called. */ static const char *setup_git_directory_gently_1(int *nongit_ok) { const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; static struct strbuf cwd = STRBUF_INIT; const char *gitdirenv, *ret; char *gitfile; int offset, offset_parent, ceil_offset = -1; dev_t current_device = 0; int one_filesystem = 1; /* * We may have read an incomplete configuration before * setting-up the git directory. If so, clear the cache so * that the next queries to the configuration reload complete * configuration (including the per-repo config file that we * ignored previously). */ git_config_clear(); /* * Let's assume that we are in a git repository. * If it turns out later that we are somewhere else, the value will be * updated accordingly. */ if (nongit_ok) *nongit_ok = 0; if (strbuf_getcwd(&cwd)) die_errno("Unable to read current working directory"); offset = cwd.len; /* * If GIT_DIR is set explicitly, we're not going * to do any discovery, but we still do repository * validation. */ gitdirenv = getenv(GIT_DIR_ENVIRONMENT); if (gitdirenv) return setup_explicit_git_dir(gitdirenv, &cwd, nongit_ok); if (env_ceiling_dirs) { int empty_entry_found = 0; string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1); filter_string_list(&ceiling_dirs, 0, canonicalize_ceiling_entry, &empty_entry_found); ceil_offset = longest_ancestor_length(cwd.buf, &ceiling_dirs); string_list_clear(&ceiling_dirs, 0); } if (ceil_offset < 0 && has_dos_drive_prefix(cwd.buf)) ceil_offset = 1; /* * Test in the following order (relative to the cwd): * - .git (file containing "gitdir: <path>") * - .git/ * - ./ (bare) * - ../.git * - ../.git/ * - ../ (bare) * - ../../.git/ * etc. */ one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); if (one_filesystem) current_device = get_device_or_die(".", NULL, 0); for (;;) { gitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT); if (gitfile) gitdirenv = gitfile = xstrdup(gitfile); else { if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; } if (gitdirenv) { ret = setup_discovered_git_dir(gitdirenv, &cwd, offset, nongit_ok); free(gitfile); return ret; } free(gitfile); if (is_git_directory(".")) return setup_bare_git_dir(&cwd, offset, nongit_ok); offset_parent = offset; while (--offset_parent > ceil_offset && cwd.buf[offset_parent] != '/'); if (offset_parent <= ceil_offset) return setup_nongit(cwd.buf, nongit_ok); if (one_filesystem) { dev_t parent_device = get_device_or_die("..", cwd.buf, offset); if (parent_device != current_device) { if (nongit_ok) { if (chdir(cwd.buf)) die_errno("Cannot come back to cwd"); *nongit_ok = 1; return NULL; } strbuf_setlen(&cwd, offset); die("Not a git repository (or any parent up to mount point %s)\n" "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd.buf); } } if (chdir("..")) { strbuf_setlen(&cwd, offset); die_errno("Cannot change to '%s/..'", cwd.buf); } offset = offset_parent; } }
static const char *setup_explicit_git_dir(const char *gitdirenv, struct strbuf *cwd, int *nongit_ok) { const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); const char *worktree; char *gitfile; int offset; if (PATH_MAX - 40 < strlen(gitdirenv)) die("'$%s' too big", GIT_DIR_ENVIRONMENT); gitfile = (char*)read_gitfile(gitdirenv); if (gitfile) { gitfile = xstrdup(gitfile); gitdirenv = gitfile; } if (!is_git_directory(gitdirenv)) { if (nongit_ok) { *nongit_ok = 1; free(gitfile); return NULL; } die("Not a git repository: '%s'", gitdirenv); } if (check_repository_format_gently(gitdirenv, nongit_ok)) { free(gitfile); return NULL; } /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ if (work_tree_env) set_git_work_tree(work_tree_env); else if (is_bare_repository_cfg > 0) { if (git_work_tree_cfg) { /* #22.2, #30 */ warning("core.bare and core.worktree do not make sense"); work_tree_config_is_bogus = 1; } /* #18, #26 */ set_git_dir(gitdirenv); free(gitfile); return NULL; } else if (git_work_tree_cfg) { /* #6, #14 */ if (is_absolute_path(git_work_tree_cfg)) set_git_work_tree(git_work_tree_cfg); else { char *core_worktree; if (chdir(gitdirenv)) die_errno("Could not chdir to '%s'", gitdirenv); if (chdir(git_work_tree_cfg)) die_errno("Could not chdir to '%s'", git_work_tree_cfg); core_worktree = xgetcwd(); if (chdir(cwd->buf)) die_errno("Could not come back to cwd"); set_git_work_tree(core_worktree); free(core_worktree); } } else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) { /* #16d */ set_git_dir(gitdirenv); free(gitfile); return NULL; } else /* #2, #10 */ set_git_work_tree("."); /* set_git_work_tree() must have been called by now */ worktree = get_git_work_tree(); /* both get_git_work_tree() and cwd are already normalized */ if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */ set_git_dir(gitdirenv); free(gitfile); return NULL; } offset = dir_inside_of(cwd->buf, worktree); if (offset >= 0) { /* cwd inside worktree? */ set_git_dir(real_path(gitdirenv)); if (chdir(worktree)) die_errno("Could not chdir to '%s'", worktree); strbuf_addch(cwd, '/'); free(gitfile); return cwd->buf + offset; } /* cwd outside worktree */ set_git_dir(gitdirenv); free(gitfile); return NULL; }
/* * Try to read the location of the git directory from the .git file, * return path to git directory if found. * * On failure, if return_error_code is not NULL, return_error_code * will be set to an error code and NULL will be returned. If * return_error_code is NULL the function will die instead (for most * cases). */ const char *read_gitfile_gently(const char *path, int *return_error_code) { const int max_file_size = 1 << 20; /* 1MB */ int error_code = 0; char *buf = NULL; char *dir = NULL; const char *slash; struct stat st; int fd; ssize_t len; if (stat(path, &st)) { error_code = READ_GITFILE_ERR_STAT_FAILED; goto cleanup_return; } if (!S_ISREG(st.st_mode)) { error_code = READ_GITFILE_ERR_NOT_A_FILE; goto cleanup_return; } if (st.st_size > max_file_size) { error_code = READ_GITFILE_ERR_TOO_LARGE; goto cleanup_return; } fd = open(path, O_RDONLY); if (fd < 0) { error_code = READ_GITFILE_ERR_OPEN_FAILED; goto cleanup_return; } buf = xmalloc(st.st_size + 1); len = read_in_full(fd, buf, st.st_size); close(fd); if (len != st.st_size) { error_code = READ_GITFILE_ERR_READ_FAILED; goto cleanup_return; } buf[len] = '\0'; if (!starts_with(buf, "gitdir: ")) { error_code = READ_GITFILE_ERR_INVALID_FORMAT; goto cleanup_return; } while (buf[len - 1] == '\n' || buf[len - 1] == '\r') len--; if (len < 9) { error_code = READ_GITFILE_ERR_NO_PATH; goto cleanup_return; } buf[len] = '\0'; dir = buf + 8; if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { size_t pathlen = slash+1 - path; dir = xstrfmt("%.*s%.*s", (int)pathlen, path, (int)(len - 8), buf + 8); free(buf); buf = dir; } if (!is_git_directory(dir)) { error_code = READ_GITFILE_ERR_NOT_A_REPO; goto cleanup_return; } update_linked_gitdir(path, dir); path = real_path(dir); cleanup_return: if (return_error_code) *return_error_code = error_code; else if (error_code) { switch (error_code) { case READ_GITFILE_ERR_STAT_FAILED: case READ_GITFILE_ERR_NOT_A_FILE: /* non-fatal; follow return path */ break; case READ_GITFILE_ERR_OPEN_FAILED: die_errno("Error opening '%s'", path); case READ_GITFILE_ERR_TOO_LARGE: die("Too large to be a .git file: '%s'", path); case READ_GITFILE_ERR_READ_FAILED: die("Error reading %s", path); case READ_GITFILE_ERR_INVALID_FORMAT: die("Invalid gitfile format: %s", path); case READ_GITFILE_ERR_NO_PATH: die("No path in gitfile: %s", path); case READ_GITFILE_ERR_NOT_A_REPO: die("Not a git repository: %s", dir); default: assert(0); } } free(buf); return error_code ? NULL : path; }
const char *resolve_gitdir(const char *suspect) { if (is_git_directory(suspect)) return suspect; return read_gitfile(suspect); }
/* * First, one directory to try is determined by the following algorithm. * * (0) If "strict" is given, the path is used as given and no DWIM is * done. Otherwise: * (1) "~/path" to mean path under the running user's home directory; * (2) "~user/path" to mean path under named user's home directory; * (3) "relative/path" to mean cwd relative directory; or * (4) "/absolute/path" to mean absolute directory. * * Unless "strict" is given, we try access() for existence of "%s.git/.git", * "%s/.git", "%s.git", "%s" in this order. The first one that exists is * what we try. * * Second, we try chdir() to that. Upon failure, we return NULL. * * Then, we try if the current directory is a valid git repository. * Upon failure, we return NULL. * * If all goes well, we return the directory we used to chdir() (but * before ~user is expanded), avoiding getcwd() resolving symbolic * links. User relative paths are also returned as they are given, * except DWIM suffixing. */ const char *enter_repo(const char *path, int strict) { static char used_path[PATH_MAX]; static char validated_path[PATH_MAX]; if (!path) return NULL; if (!strict) { static const char *suffix[] = { "/.git", "", ".git/.git", ".git", NULL, }; const char *gitfile; int len = strlen(path); int i; while ((1 < len) && (path[len-1] == '/')) len--; if (PATH_MAX <= len) return NULL; strncpy(used_path, path, len); used_path[len] = 0 ; strcpy(validated_path, used_path); if (used_path[0] == '~') { char *newpath = expand_user_path(used_path); if (!newpath || (PATH_MAX - 10 < strlen(newpath))) { free(newpath); return NULL; } /* * Copy back into the static buffer. A pity * since newpath was not bounded, but other * branches of the if are limited by PATH_MAX * anyway. */ strcpy(used_path, newpath); free(newpath); } else if (PATH_MAX - 10 < len) return NULL; len = strlen(used_path); for (i = 0; suffix[i]; i++) { struct stat st; strcpy(used_path + len, suffix[i]); if (!stat(used_path, &st) && (S_ISREG(st.st_mode) || (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) { strcat(validated_path, suffix[i]); break; } } if (!suffix[i]) return NULL; gitfile = read_gitfile(used_path) ; if (gitfile) strcpy(used_path, gitfile); if (chdir(used_path)) return NULL; path = validated_path; } else if (chdir(path)) return NULL; if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && validate_headref("HEAD") == 0) { set_git_dir("."); check_repository_format(); return path; } return NULL; }
static const char *setup_explicit_git_dir(const char *gitdirenv, char *cwd, int len, int *nongit_ok) { const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); const char *worktree; char *gitfile; if (PATH_MAX - 40 < strlen(gitdirenv)) die("'$%s' too big", GIT_DIR_ENVIRONMENT); gitfile = (char*)read_gitfile_gently(gitdirenv); if (gitfile) { gitfile = xstrdup(gitfile); gitdirenv = gitfile; } if (!is_git_directory(gitdirenv)) { if (nongit_ok) { *nongit_ok = 1; free(gitfile); return NULL; } die("Not a git repository: '%s'", gitdirenv); } if (check_repository_format_gently(gitdirenv, nongit_ok)) { free(gitfile); return NULL; } /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ if (work_tree_env) set_git_work_tree(work_tree_env); else if (is_bare_repository_cfg > 0) { if (git_work_tree_cfg) /* #22.2, #30 */ die("core.bare and core.worktree do not make sense"); /* #18, #26 */ set_git_dir(gitdirenv); free(gitfile); return NULL; } else if (git_work_tree_cfg) { /* #6, #14 */ if (is_absolute_path(git_work_tree_cfg)) set_git_work_tree(git_work_tree_cfg); else { char core_worktree[PATH_MAX]; if (chdir(gitdirenv)) die_errno("Could not chdir to '%s'", gitdirenv); if (chdir(git_work_tree_cfg)) die_errno("Could not chdir to '%s'", git_work_tree_cfg); if (!getcwd(core_worktree, PATH_MAX)) die_errno("Could not get directory '%s'", git_work_tree_cfg); if (chdir(cwd)) die_errno("Could not come back to cwd"); set_git_work_tree(core_worktree); } } else /* #2, #10 */ set_git_work_tree("."); /* set_git_work_tree() must have been called by now */ worktree = get_git_work_tree(); /* both get_git_work_tree() and cwd are already normalized */ if (!strcmp(cwd, worktree)) { /* cwd == worktree */ set_git_dir(gitdirenv); free(gitfile); return NULL; } if (!prefixcmp(cwd, worktree) && cwd[strlen(worktree)] == '/') { /* cwd inside worktree */ set_git_dir(real_path(gitdirenv)); if (chdir(worktree)) die_errno("Could not chdir to '%s'", worktree); cwd[len++] = '/'; cwd[len] = '\0'; free(gitfile); return cwd + strlen(worktree) + 1; } /* cwd outside worktree */ set_git_dir(gitdirenv); free(gitfile); return NULL; }
/* * We cannot decide in this function whether we are in the work tree or * not, since the config can only be read _after_ this function was called. */ static const char *setup_git_directory_gently_1(int *nongit_ok) { const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); static char cwd[PATH_MAX+1]; const char *gitdirenv, *ret; char *gitfile; int len, offset, ceil_offset; dev_t current_device = 0; int one_filesystem = 1; /* * Let's assume that we are in a git repository. * If it turns out later that we are somewhere else, the value will be * updated accordingly. */ if (nongit_ok) *nongit_ok = 0; if (!getcwd(cwd, sizeof(cwd)-1)) die_errno("Unable to read current working directory"); offset = len = strlen(cwd); /* * If GIT_DIR is set explicitly, we're not going * to do any discovery, but we still do repository * validation. */ gitdirenv = getenv(GIT_DIR_ENVIRONMENT); if (gitdirenv) return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok); ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs); if (ceil_offset < 0 && has_dos_drive_prefix(cwd)) ceil_offset = 1; /* * Test in the following order (relative to the cwd): * - .git (file containing "gitdir: <path>") * - .git/ * - ./ (bare) * - ../.git * - ../.git/ * - ../ (bare) * - ../../.git/ * etc. */ one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); if (one_filesystem) current_device = get_device_or_die(".", NULL); for (;;) { gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); if (gitfile) gitdirenv = gitfile = xstrdup(gitfile); else { if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; } if (gitdirenv) { ret = setup_discovered_git_dir(gitdirenv, cwd, offset, len, nongit_ok); free(gitfile); return ret; } free(gitfile); if (is_git_directory(".")) return setup_bare_git_dir(cwd, offset, len, nongit_ok); while (--offset > ceil_offset && cwd[offset] != '/'); if (offset <= ceil_offset) return setup_nongit(cwd, nongit_ok); if (one_filesystem) { dev_t parent_device = get_device_or_die("..", cwd); if (parent_device != current_device) { if (nongit_ok) { if (chdir(cwd)) die_errno("Cannot come back to cwd"); *nongit_ok = 1; return NULL; } cwd[offset] = '\0'; die("Not a git repository (or any parent up to mount parent %s)\n" "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd); } } if (chdir("..")) { cwd[offset] = '\0'; die_errno("Cannot change to '%s/..'", cwd); } } }