aboutsummaryrefslogtreecommitdiff
path: root/setup.c
diff options
context:
space:
mode:
Diffstat (limited to 'setup.c')
-rw-r--r--setup.c150
1 files changed, 103 insertions, 47 deletions
diff --git a/setup.c b/setup.c
index 2435186e4..8cc34186c 100644
--- a/setup.c
+++ b/setup.c
@@ -1,4 +1,6 @@
#include "cache.h"
+#include "repository.h"
+#include "config.h"
#include "dir.h"
#include "string-list.h"
@@ -134,23 +136,27 @@ int path_inside_repo(const char *prefix, const char *path)
int check_filename(const char *prefix, const char *arg)
{
- const char *name;
char *to_free = NULL;
struct stat st;
- if (starts_with(arg, ":/")) {
- if (arg[2] == '\0') /* ":/" is root dir, always exists */
+ if (skip_prefix(arg, ":/", &arg)) {
+ if (!*arg) /* ":/" is root dir, always exists */
return 1;
- name = arg + 2;
- } else if (prefix)
- name = to_free = prefix_filename(prefix, arg);
- else
- name = arg;
- if (!lstat(name, &st)) {
+ prefix = NULL;
+ } else if (skip_prefix(arg, ":!", &arg) ||
+ skip_prefix(arg, ":^", &arg)) {
+ if (!*arg) /* excluding everything is silly, but allowed */
+ return 1;
+ }
+
+ if (prefix)
+ arg = to_free = prefix_filename(prefix, arg);
+
+ if (!lstat(arg, &st)) {
free(to_free);
return 1; /* file exists */
}
- if (errno == ENOENT || errno == ENOTDIR) {
+ if (is_missing_file_error(errno)) {
free(to_free);
return 0; /* file does not exist */
}
@@ -182,6 +188,24 @@ static void NORETURN die_verify_filename(const char *prefix,
}
/*
+ * Check for arguments that don't resolve as actual files,
+ * but which look sufficiently like pathspecs that we'll consider
+ * them such for the purposes of rev/pathspec DWIM parsing.
+ */
+static int looks_like_pathspec(const char *arg)
+{
+ /* anything with a wildcard character */
+ if (!no_wildcard(arg))
+ return 1;
+
+ /* long-form pathspec magic */
+ if (starts_with(arg, ":("))
+ return 1;
+
+ return 0;
+}
+
+/*
* Verify a filename that we got as an argument for a pathspec
* entry. Note that a filename that begins with "-" never verifies
* as true, because even if such a filename were to exist, we want
@@ -206,8 +230,8 @@ void verify_filename(const char *prefix,
int diagnose_misspelt_rev)
{
if (*arg == '-')
- die("bad flag '%s' used after filename", arg);
- if (check_filename(prefix, arg) || !no_wildcard(arg))
+ die("option '%s' must come before non-option arguments", arg);
+ if (looks_like_pathspec(arg) || check_filename(prefix, arg))
return;
die_verify_filename(prefix, arg, diagnose_misspelt_rev);
}
@@ -288,7 +312,9 @@ int is_git_directory(const char *suspect)
size_t len;
/* Check worktree-related signatures */
- strbuf_addf(&path, "%s/HEAD", suspect);
+ strbuf_addstr(&path, suspect);
+ strbuf_complete(&path, '/');
+ strbuf_addstr(&path, "HEAD");
if (validate_headref(path.buf))
goto done;
@@ -408,16 +434,15 @@ static int check_repo_format(const char *var, const char *value, void *vdata)
return 0;
}
-static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
+static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok)
{
struct strbuf sb = STRBUF_INIT;
struct strbuf err = STRBUF_INIT;
- struct repository_format candidate;
int has_common;
has_common = get_common_dir(&sb, gitdir);
strbuf_addstr(&sb, "/config");
- read_repository_format(&candidate, sb.buf);
+ read_repository_format(candidate, sb.buf);
strbuf_release(&sb);
/*
@@ -425,10 +450,10 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
* we treat a missing config as a silent "ok", even when nongit_ok
* is unset.
*/
- if (candidate.version < 0)
+ if (candidate->version < 0)
return 0;
- if (verify_repository_format(&candidate, &err) < 0) {
+ if (verify_repository_format(candidate, &err) < 0) {
if (nongit_ok) {
warning("%s", err.buf);
strbuf_release(&err);
@@ -438,21 +463,21 @@ static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
die("%s", err.buf);
}
- repository_format_precious_objects = candidate.precious_objects;
- string_list_clear(&candidate.unknown_extensions, 0);
+ repository_format_precious_objects = candidate->precious_objects;
+ string_list_clear(&candidate->unknown_extensions, 0);
if (!has_common) {
- if (candidate.is_bare != -1) {
- is_bare_repository_cfg = candidate.is_bare;
+ if (candidate->is_bare != -1) {
+ is_bare_repository_cfg = candidate->is_bare;
if (is_bare_repository_cfg == 1)
inside_work_tree = -1;
}
- if (candidate.work_tree) {
+ if (candidate->work_tree) {
free(git_work_tree_cfg);
- git_work_tree_cfg = candidate.work_tree;
+ git_work_tree_cfg = candidate->work_tree;
inside_work_tree = -1;
}
} else {
- free(candidate.work_tree);
+ free(candidate->work_tree);
}
return 0;
@@ -463,6 +488,7 @@ int read_repository_format(struct repository_format *format, const char *path)
memset(format, 0, sizeof(*format));
format->version = -1;
format->is_bare = -1;
+ format->hash_algo = GIT_HASH_SHA1;
string_list_init(&format->unknown_extensions, 1);
git_config_from_file(check_repo_format, path, format);
return format->version;
@@ -517,7 +543,8 @@ void read_gitfile_error_die(int error_code, const char *path, const char *dir)
/*
* Try to read the location of the git directory from the .git file,
- * return path to git directory if found.
+ * return path to git directory if found. The return value comes from
+ * a shared buffer.
*
* 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
@@ -598,6 +625,7 @@ cleanup_return:
static const char *setup_explicit_git_dir(const char *gitdirenv,
struct strbuf *cwd,
+ struct repository_format *repo_fmt,
int *nongit_ok)
{
const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
@@ -623,7 +651,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
die("Not a git repository: '%s'", gitdirenv);
}
- if (check_repository_format_gently(gitdirenv, nongit_ok)) {
+ if (check_repository_format_gently(gitdirenv, repo_fmt, nongit_ok)) {
free(gitfile);
return NULL;
}
@@ -696,9 +724,10 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
static const char *setup_discovered_git_dir(const char *gitdir,
struct strbuf *cwd, int offset,
+ struct repository_format *repo_fmt,
int *nongit_ok)
{
- if (check_repository_format_gently(gitdir, nongit_ok))
+ if (check_repository_format_gently(gitdir, repo_fmt, nongit_ok))
return NULL;
/* --work-tree is set without --git-dir; use discovered one */
@@ -710,7 +739,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,
gitdir = to_free = real_pathdup(gitdir, 1);
if (chdir(cwd->buf))
die_errno("Could not come back to cwd");
- ret = setup_explicit_git_dir(gitdir, cwd, nongit_ok);
+ ret = setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok);
free(to_free);
return ret;
}
@@ -742,11 +771,12 @@ static const char *setup_discovered_git_dir(const char *gitdir,
/* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
+ struct repository_format *repo_fmt,
int *nongit_ok)
{
int root_len;
- if (check_repository_format_gently(".", nongit_ok))
+ if (check_repository_format_gently(".", repo_fmt, nongit_ok))
return NULL;
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
@@ -758,7 +788,7 @@ static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset);
if (chdir(cwd->buf))
die_errno("Could not come back to cwd");
- return setup_explicit_git_dir(gitdir, cwd, nongit_ok);
+ return setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok);
}
inside_git_dir = 1;
@@ -896,7 +926,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
* - ../.git
* - ../.git/
* - ../ (bare)
- * - ../../.git/
+ * - ../../.git
* etc.
*/
one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
@@ -945,19 +975,21 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
}
}
-const char *discover_git_directory(struct strbuf *gitdir)
+int discover_git_directory(struct strbuf *commondir,
+ struct strbuf *gitdir)
{
struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT;
size_t gitdir_offset = gitdir->len, cwd_len;
+ size_t commondir_offset = commondir->len;
struct repository_format candidate;
if (strbuf_getcwd(&dir))
- return NULL;
+ return -1;
cwd_len = dir.len;
if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
strbuf_release(&dir);
- return NULL;
+ return -1;
}
/*
@@ -973,8 +1005,10 @@ const char *discover_git_directory(struct strbuf *gitdir)
strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len);
}
+ get_common_dir(commondir, gitdir->buf + gitdir_offset);
+
strbuf_reset(&dir);
- strbuf_addf(&dir, "%s/config", gitdir->buf + gitdir_offset);
+ strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset);
read_repository_format(&candidate, dir.buf);
strbuf_release(&dir);
@@ -982,18 +1016,20 @@ const char *discover_git_directory(struct strbuf *gitdir)
warning("ignoring git dir '%s': %s",
gitdir->buf + gitdir_offset, err.buf);
strbuf_release(&err);
+ strbuf_setlen(commondir, commondir_offset);
strbuf_setlen(gitdir, gitdir_offset);
- return NULL;
+ return -1;
}
- return gitdir->buf + gitdir_offset;
+ return 0;
}
const char *setup_git_directory_gently(int *nongit_ok)
{
static struct strbuf cwd = STRBUF_INIT;
struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT;
- const char *prefix, *env_prefix;
+ const char *prefix;
+ struct repository_format repo_fmt;
/*
* We may have read an incomplete configuration before
@@ -1021,18 +1057,18 @@ const char *setup_git_directory_gently(int *nongit_ok)
prefix = NULL;
break;
case GIT_DIR_EXPLICIT:
- prefix = setup_explicit_git_dir(gitdir.buf, &cwd, nongit_ok);
+ prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok);
break;
case GIT_DIR_DISCOVERED:
if (dir.len < cwd.len && chdir(dir.buf))
die(_("Cannot change to '%s'"), dir.buf);
prefix = setup_discovered_git_dir(gitdir.buf, &cwd, dir.len,
- nongit_ok);
+ &repo_fmt, nongit_ok);
break;
case GIT_DIR_BARE:
if (dir.len < cwd.len && chdir(dir.buf))
die(_("Cannot change to '%s'"), dir.buf);
- prefix = setup_bare_git_dir(&cwd, dir.len, nongit_ok);
+ prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok);
break;
case GIT_DIR_HIT_CEILING:
prefix = setup_nongit(cwd.buf, nongit_ok);
@@ -1051,10 +1087,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
die("BUG: unhandled setup_git_directory_1() result");
}
- env_prefix = getenv(GIT_TOPLEVEL_PREFIX_ENVIRONMENT);
- if (env_prefix)
- prefix = env_prefix;
-
if (prefix)
setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
else
@@ -1063,6 +1095,29 @@ const char *setup_git_directory_gently(int *nongit_ok)
startup_info->have_repository = !nongit_ok || !*nongit_ok;
startup_info->prefix = prefix;
+ /*
+ * Not all paths through the setup code will call 'set_git_dir()' (which
+ * directly sets up the environment) so in order to guarantee that the
+ * environment is in a consistent state after setup, explicitly setup
+ * the environment if we have a repository.
+ *
+ * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some
+ * code paths so we also need to explicitly setup the environment if
+ * the user has set GIT_DIR. It may be beneficial to disallow bogus
+ * GIT_DIR values at some point in the future.
+ */
+ if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT)) {
+ if (!the_repository->gitdir) {
+ const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
+ if (!gitdir)
+ gitdir = DEFAULT_GIT_DIR_ENVIRONMENT;
+ repo_set_gitdir(the_repository, gitdir);
+ setup_git_env();
+ }
+ if (startup_info->have_repository)
+ repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
+ }
+
strbuf_release(&dir);
strbuf_release(&gitdir);
@@ -1122,7 +1177,8 @@ int git_config_perm(const char *var, const char *value)
void check_repository_format(void)
{
- check_repository_format_gently(get_git_dir(), NULL);
+ struct repository_format repo_fmt;
+ check_repository_format_gently(get_git_dir(), &repo_fmt, NULL);
startup_info->have_repository = 1;
}