From 892c41b98ae2e6baf3aa13901cb10db9ac67d2f3 Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Wed, 6 Jun 2007 09:10:42 +0200 Subject: introduce GIT_WORK_TREE to specify the work tree setup_gdg is used as abbreviation for setup_git_directory_gently. The work tree can be specified using the environment variable GIT_WORK_TREE and the config option core.worktree (the environment variable has precendence over the config option). Additionally there is a command line option --work-tree which sets the environment variable. setup_gdg does the following now: GIT_DIR unspecified repository in .git directory parent directory of the .git directory is used as work tree, GIT_WORK_TREE is ignored GIT_DIR unspecified repository in cwd GIT_DIR is set to cwd see the cases with GIT_DIR specified what happens next and also see the note below GIT_DIR specified GIT_WORK_TREE/core.worktree unspecified cwd is used as work tree GIT_DIR specified GIT_WORK_TREE/core.worktree specified the specified work tree is used Note on the case where GIT_DIR is unspecified and repository is in cwd: GIT_WORK_TREE is used but is_inside_git_dir is always true. I did it this way because setup_gdg might be called multiple times (e.g. when doing alias expansion) and in successive calls setup_gdg should do the same thing every time. Meaning of is_bare/is_inside_work_tree/is_inside_git_dir: (1) is_bare_repository A repository is bare if core.bare is true or core.bare is unspecified and the name suggests it is bare (directory not named .git). The bare option disables a few protective checks which are useful with a working tree. Currently this changes if a repository is bare: updates of HEAD are allowed git gc packs the refs the reflog is disabled by default (2) is_inside_work_tree True if the cwd is inside the associated working tree (if there is one), false otherwise. (3) is_inside_git_dir True if the cwd is inside the git directory, false otherwise. Before this patch is_inside_git_dir was always true for bare repositories. When setup_gdg finds a repository git_config(git_default_config) is always called. This ensure that is_bare_repository makes use of core.bare and does not guess even though core.bare is specified. inside_work_tree and inside_git_dir are set if setup_gdg finds a repository. The is_inside_work_tree and is_inside_git_dir functions will die if they are called before a successful call to setup_gdg. Signed-off-by: Matthias Lederhofer Signed-off-by: Junio C Hamano --- setup.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 156 insertions(+), 55 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index a45ea8309a..7e32de229d 100644 --- a/setup.c +++ b/setup.c @@ -174,41 +174,93 @@ static int inside_git_dir = -1; int is_inside_git_dir(void) { - if (inside_git_dir < 0) { - char buffer[1024]; - - if (is_bare_repository()) - return (inside_git_dir = 1); - if (getcwd(buffer, sizeof(buffer))) { - const char *git_dir = get_git_dir(), *cwd = buffer; - while (*git_dir && *git_dir == *cwd) { - git_dir++; - cwd++; - } - inside_git_dir = !*git_dir; - } else - inside_git_dir = 0; + if (inside_git_dir >= 0) + return inside_git_dir; + die("BUG: is_inside_git_dir called before setup_git_directory"); +} + +static int inside_work_tree = -1; + +int is_inside_work_tree(void) +{ + if (inside_git_dir >= 0) + return inside_work_tree; + die("BUG: is_inside_work_tree called before setup_git_directory"); +} + +static char *gitworktree_config; + +static int git_setup_config(const char *var, const char *value) +{ + if (!strcmp(var, "core.worktree")) { + if (gitworktree_config) + strlcpy(gitworktree_config, value, PATH_MAX); + return 0; } - return inside_git_dir; + return git_default_config(var, value); } const char *setup_git_directory_gently(int *nongit_ok) { static char cwd[PATH_MAX+1]; - const char *gitdirenv; - int len, offset; + char worktree[PATH_MAX+1], gitdir[PATH_MAX+1]; + const char *gitdirenv, *gitworktree; + int wt_rel_gitdir = 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)) + if (!gitdirenv) { + int len, offset; + + if (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/') + die("Unable to read current working directory"); + + offset = len = strlen(cwd); + for (;;) { + if (is_git_directory(".git")) + break; + if (offset == 0) { + offset = -1; + break; + } + chdir(".."); + while (cwd[--offset] != '/') + ; /* do nothing */ + } + + if (offset >= 0) { + inside_work_tree = 1; + git_config(git_default_config); + if (offset == len) { + inside_git_dir = 0; + return NULL; + } + + cwd[len++] = '/'; + cwd[len] = '\0'; + inside_git_dir = !prefixcmp(cwd + offset + 1, ".git/"); + return cwd + offset + 1; + } + + if (chdir(cwd)) + die("Cannot come back to cwd"); + if (!is_git_directory(".")) { + if (nongit_ok) { + *nongit_ok = 1; + return NULL; + } + die("Not a git repository"); + } + setenv(GIT_DIR_ENVIRONMENT, cwd, 1); + } + + if (PATH_MAX - 40 < strlen(gitdirenv)) { + if (nongit_ok) { + *nongit_ok = 1; return NULL; + } + die("$%s too big", GIT_DIR_ENVIRONMENT); + } + if (!is_git_directory(gitdirenv)) { if (nongit_ok) { *nongit_ok = 1; return NULL; @@ -218,41 +270,90 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/') die("Unable to read current working directory"); + if (chdir(gitdirenv)) { + if (nongit_ok) { + *nongit_ok = 1; + return NULL; + } + die("Cannot change directory to $%s '%s'", + GIT_DIR_ENVIRONMENT, gitdirenv); + } + if (!getcwd(gitdir, sizeof(gitdir)-1) || gitdir[0] != '/') + die("Unable to read current working directory"); + if (chdir(cwd)) + die("Cannot come back to cwd"); - offset = len = strlen(cwd); - for (;;) { - if (is_git_directory(".git")) - break; - chdir(".."); - do { - if (!offset) { - if (is_git_directory(cwd)) { - if (chdir(cwd)) - die("Cannot come back to cwd"); - setenv(GIT_DIR_ENVIRONMENT, cwd, 1); - inside_git_dir = 1; - return NULL; - } - if (nongit_ok) { - if (chdir(cwd)) - die("Cannot come back to cwd"); - *nongit_ok = 1; - return NULL; - } - die("Not a git repository"); + /* + * In case there is a work tree we may change the directory, + * therefore make GIT_DIR an absolute path. + */ + if (gitdirenv[0] != '/') { + setenv(GIT_DIR_ENVIRONMENT, gitdir, 1); + gitdirenv = getenv(GIT_DIR_ENVIRONMENT); + if (PATH_MAX - 40 < strlen(gitdirenv)) { + if (nongit_ok) { + *nongit_ok = 1; + return NULL; } - } while (cwd[--offset] != '/'); + die("$%s too big after expansion to absolute path", + GIT_DIR_ENVIRONMENT); + } + } + + strcat(cwd, "/"); + strcat(gitdir, "/"); + inside_git_dir = !prefixcmp(cwd, gitdir); + + gitworktree = getenv(GIT_WORK_TREE_ENVIRONMENT); + if (!gitworktree) { + gitworktree_config = worktree; + worktree[0] = '\0'; + } + git_config(git_setup_config); + if (!gitworktree) { + gitworktree_config = NULL; + if (worktree[0]) + gitworktree = worktree; + if (gitworktree && gitworktree[0] != '/') + wt_rel_gitdir = 1; + } + + if (wt_rel_gitdir && chdir(gitdirenv)) + die("Cannot change directory to $%s '%s'", + GIT_DIR_ENVIRONMENT, gitdirenv); + if (gitworktree && chdir(gitworktree)) { + if (nongit_ok) { + if (wt_rel_gitdir && chdir(cwd)) + die("Cannot come back to cwd"); + *nongit_ok = 1; + return NULL; + } + if (wt_rel_gitdir) + die("Cannot change directory to working tree '%s'" + " from $%s", gitworktree, GIT_DIR_ENVIRONMENT); + else + die("Cannot change directory to working tree '%s'", + gitworktree); } + if (!getcwd(worktree, sizeof(worktree)-1) || worktree[0] != '/') + die("Unable to read current working directory"); + strcat(worktree, "/"); + inside_work_tree = !prefixcmp(cwd, worktree); - if (offset == len) + if (gitworktree && inside_work_tree && !prefixcmp(worktree, gitdir) && + strcmp(worktree, gitdir)) { + inside_git_dir = 0; + } + + if (!inside_work_tree) { + if (chdir(cwd)) + die("Cannot come back to cwd"); return NULL; + } - /* Make "offset" point to past the '/', and add a '/' at the end */ - offset++; - cwd[len++] = '/'; - cwd[len] = 0; - inside_git_dir = !prefixcmp(cwd + offset, ".git/"); - return cwd + offset; + if (!strcmp(cwd, worktree)) + return NULL; + return cwd+strlen(worktree); } int git_config_perm(const char *var, const char *value) -- cgit v1.2.1 From 7ae3df8c0aa3b7337ae9ac7b6184ac05985bf996 Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Sun, 3 Jun 2007 16:48:16 +0200 Subject: Use new semantics of is_bare/inside_git_dir/inside_work_tree Up to now to check for a working tree this was used: !is_bare && !inside_git_dir (the check for bare is redundant because is_inside_git_dir returned already 1 for bare repositories). Now the check is: inside_work_tree && !inside_git_dir Signed-off-by: Matthias Lederhofer Signed-off-by: Junio C Hamano --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 7e32de229d..14a4d955b5 100644 --- a/setup.c +++ b/setup.c @@ -95,7 +95,7 @@ void verify_non_filename(const char *prefix, const char *arg) const char *name; struct stat st; - if (is_inside_git_dir()) + if (!is_inside_work_tree() || is_inside_git_dir()) return; if (*arg == '-') return; /* flag */ -- cgit v1.2.1 From f4f51add2712293b7bc9e7aaebf6a589bb37c7c7 Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Wed, 6 Jun 2007 23:29:59 +0200 Subject: setup_git_directory: fix segfault if repository is found in cwd Additionally there was a similar part calling setenv and getenv in the same way which missed a check if getenv succeeded. Signed-off-by: Matthias Lederhofer Signed-off-by: Junio C Hamano --- setup.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 14a4d955b5..dba8012659 100644 --- a/setup.c +++ b/setup.c @@ -251,6 +251,9 @@ const char *setup_git_directory_gently(int *nongit_ok) die("Not a git repository"); } setenv(GIT_DIR_ENVIRONMENT, cwd, 1); + gitdirenv = getenv(GIT_DIR_ENVIRONMENT); + if (!gitdirenv) + die("getenv after setenv failed"); } if (PATH_MAX - 40 < strlen(gitdirenv)) { @@ -290,6 +293,8 @@ const char *setup_git_directory_gently(int *nongit_ok) if (gitdirenv[0] != '/') { setenv(GIT_DIR_ENVIRONMENT, gitdir, 1); gitdirenv = getenv(GIT_DIR_ENVIRONMENT); + if (!gitdirenv) + die("getenv after setenv failed"); if (PATH_MAX - 40 < strlen(gitdirenv)) { if (nongit_ok) { *nongit_ok = 1; -- cgit v1.2.1 From a6080a0a44d5ead84db3dabbbc80e82df838533d Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 7 Jun 2007 00:04:01 -0700 Subject: War on whitespace This uses "git-apply --whitespace=strip" to fix whitespace errors that have crept in to our source files over time. There are a few files that need to have trailing whitespaces (most notably, test vectors). The results still passes the test, and build result in Documentation/ area is unchanged. Signed-off-by: Junio C Hamano --- setup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index a45ea8309a..14f62c42e3 100644 --- a/setup.c +++ b/setup.c @@ -39,7 +39,7 @@ const char *prefix_path(const char *prefix, int len, const char *path) if (len) { int speclen = strlen(path); char *n = xmalloc(speclen + len + 1); - + memcpy(n, prefix, len); memcpy(n + len, path, speclen+1); path = n; @@ -47,7 +47,7 @@ const char *prefix_path(const char *prefix, int len, const char *path) return path; } -/* +/* * Unlike prefix_path, this should be used if the named file does * not have to interact with index entry; i.e. name of a random file * on the filesystem. -- cgit v1.2.1 From f66a4d68d2066998605f5910c3c8dd1e4a25fc7f Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 4 Jul 2007 12:45:42 -0700 Subject: Do not check if getcwd() result begins with a slash. In user space, and for getcwd(), the check to see if the resulting path begins with a '/' does not make sense. This is merely a mistake by Linus who is so used to code for the kernel, where a d_path() return value pathname can be either a real path, or something like "pipe:[8003]", and the difference is the '/' at the beginning. Pointed out by Dscho, Matthias Lederhofer and clarified by Linus. Signed-off-by: Junio C Hamano --- setup.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 01f74d4644..bb26f3af96 100644 --- a/setup.c +++ b/setup.c @@ -211,7 +211,7 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!gitdirenv) { int len, offset; - if (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/') + if (!getcwd(cwd, sizeof(cwd)-1)) die("Unable to read current working directory"); offset = len = strlen(cwd); @@ -271,7 +271,7 @@ const char *setup_git_directory_gently(int *nongit_ok) die("Not a git repository: '%s'", gitdirenv); } - if (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/') + if (!getcwd(cwd, sizeof(cwd)-1)) die("Unable to read current working directory"); if (chdir(gitdirenv)) { if (nongit_ok) { @@ -281,7 +281,7 @@ const char *setup_git_directory_gently(int *nongit_ok) die("Cannot change directory to $%s '%s'", GIT_DIR_ENVIRONMENT, gitdirenv); } - if (!getcwd(gitdir, sizeof(gitdir)-1) || gitdir[0] != '/') + if (!getcwd(gitdir, sizeof(gitdir)-1)) die("Unable to read current working directory"); if (chdir(cwd)) die("Cannot come back to cwd"); @@ -340,7 +340,7 @@ const char *setup_git_directory_gently(int *nongit_ok) die("Cannot change directory to working tree '%s'", gitworktree); } - if (!getcwd(worktree, sizeof(worktree)-1) || worktree[0] != '/') + if (!getcwd(worktree, sizeof(worktree)-1)) die("Unable to read current working directory"); strcat(worktree, "/"); inside_work_tree = !prefixcmp(cwd, worktree); -- cgit v1.2.1 From 835252272e0192bd26983e22428480c5c89775fb Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 11 Jul 2007 15:18:17 +0100 Subject: Fix core.sharedRepository = 2 For compatibility reasons, "git init --shared=all" does not write "all" into the config, but a number. In the shared setup, you really have to support even older clients on the _same_ repository. But git_config_perm() did not pick up on it. Also, "git update-server-info" failed to pick up on the shared permissions. This patch fixes both issues, and adds a test to prove it. Signed-off-by: Johannes Schindelin Tested-by: martin f krafft Signed-off-by: Junio C Hamano --- setup.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'setup.c') diff --git a/setup.c b/setup.c index bb26f3af96..7b07144af7 100644 --- a/setup.c +++ b/setup.c @@ -364,6 +364,7 @@ const char *setup_git_directory_gently(int *nongit_ok) int git_config_perm(const char *var, const char *value) { if (value) { + int i; if (!strcmp(value, "umask")) return PERM_UMASK; if (!strcmp(value, "group")) @@ -372,6 +373,9 @@ int git_config_perm(const char *var, const char *value) !strcmp(value, "world") || !strcmp(value, "everybody")) return PERM_EVERYBODY; + i = atoi(value); + if (i > 1) + return i; } return git_config_bool(var, value); } -- cgit v1.2.1 From 299726d53841984450524f4ade428112e51c853e Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 30 Jul 2007 00:24:38 +0100 Subject: white space fixes in setup.c Some lines were not indented by tabs but by spaces. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- setup.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 7b07144af7..b54d65fd07 100644 --- a/setup.c +++ b/setup.c @@ -382,11 +382,11 @@ int git_config_perm(const char *var, const char *value) int check_repository_format_version(const char *var, const char *value) { - if (strcmp(var, "core.repositoryformatversion") == 0) - repository_format_version = git_config_int(var, value); + if (strcmp(var, "core.repositoryformatversion") == 0) + repository_format_version = git_config_int(var, value); else if (strcmp(var, "core.sharedrepository") == 0) shared_repository = git_config_perm(var, value); - return 0; + return 0; } int check_repository_format(void) -- cgit v1.2.1 From e90fdc39b6903502192b2dd11e5503cea721a1ad Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 1 Aug 2007 01:30:14 +0100 Subject: Clean up work-tree handling The old version of work-tree support was an unholy mess, barely readable, and not to the point. For example, why do you have to provide a worktree, when it is not used? As in "git status". Now it works. Another riddle was: if you can have work trees inside the git dir, why are some programs complaining that they need a work tree? IOW it is allowed to call $ git --git-dir=../ --work-tree=. bla when you really want to. In this case, you are both in the git directory and in the working tree. So, programs have to actually test for the right thing, namely if they are inside a working tree, and not if they are inside a git directory. Also, GIT_DIR=../.git should behave the same as if no GIT_DIR was specified, unless there is a repository in the current working directory. It does now. The logic to determine if a repository is bare, or has a work tree (tertium non datur), is this: --work-tree=bla overrides GIT_WORK_TREE, which overrides core.bare = true, which overrides core.worktree, which overrides GIT_DIR/.. when GIT_DIR ends in /.git, which overrides the directory in which .git/ was found. In related news, a long standing bug was fixed: when in .git/bla/x.git/, which is a bare repository, git formerly assumed ../.. to be the appropriate git dir. This problem was reported by Shawn Pearce to have caused much pain, where a colleague mistakenly ran "git init" in "/" a long time ago, and bare repositories just would not work. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- setup.c | 279 +++++++++++++++++++++++++++++----------------------------------- 1 file changed, 127 insertions(+), 152 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index b54d65fd07..3653092ab6 100644 --- a/setup.c +++ b/setup.c @@ -1,4 +1,8 @@ #include "cache.h" +#include "dir.h" + +static int inside_git_dir = -1; +static int inside_work_tree = -1; const char *prefix_path(const char *prefix, int len, const char *path) { @@ -170,100 +174,89 @@ static int is_git_directory(const char *suspect) return 1; } -static int inside_git_dir = -1; - int is_inside_git_dir(void) { - if (inside_git_dir >= 0) - return inside_git_dir; - die("BUG: is_inside_git_dir called before setup_git_directory"); + if (inside_git_dir < 0) + inside_git_dir = is_inside_dir(get_git_dir()); + return inside_git_dir; } -static int inside_work_tree = -1; - int is_inside_work_tree(void) { - if (inside_git_dir >= 0) - return inside_work_tree; - die("BUG: is_inside_work_tree called before setup_git_directory"); + if (inside_work_tree < 0) + inside_work_tree = is_inside_dir(get_git_work_tree()); + return inside_work_tree; } -static char *gitworktree_config; - -static int git_setup_config(const char *var, const char *value) +/* + * If no worktree was given, and we are outside of a default work tree, + * now is the time to set it. + * + * In other words, if the user calls git with something like + * + * git --git-dir=/some/where/else/.git bla + * + * default to /some/where/else as working directory; if the specified + * git-dir does not end in "/.git", the cwd is used as working directory. + */ +const char *set_work_tree(const char *dir) { - if (!strcmp(var, "core.worktree")) { - if (gitworktree_config) - strlcpy(gitworktree_config, value, PATH_MAX); - return 0; + char dir_buffer[PATH_MAX]; + static char buffer[PATH_MAX + 1], *rel = NULL; + int len, postfix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1; + + /* strip the variable 'dir' of the postfix "/.git" if it has it */ + len = strlen(dir); + if (len > postfix_len && !strcmp(dir + len - postfix_len, + "/" DEFAULT_GIT_DIR_ENVIRONMENT)) { + strncpy(dir_buffer, dir, len - postfix_len); + + /* are we inside the default work tree? */ + rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer); + } + /* if rel is set, the cwd is _not_ the current working tree */ + if (rel && *rel) { + if (!is_absolute_path(dir)) + set_git_dir(make_absolute_path(dir)); + dir = dir_buffer; + chdir(dir); + strcat(rel, "/"); + inside_git_dir = 0; + } else { + rel = NULL; + dir = getcwd(buffer, sizeof(buffer)); } - return git_default_config(var, value); + git_work_tree_cfg = xstrdup(dir); + inside_work_tree = 1; + + return rel; } +/* + * 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); static char cwd[PATH_MAX+1]; - char worktree[PATH_MAX+1], gitdir[PATH_MAX+1]; - const char *gitdirenv, *gitworktree; - int wt_rel_gitdir = 0; + const char *gitdirenv; + int len, offset; + /* + * 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) { - int len, offset; - - if (!getcwd(cwd, sizeof(cwd)-1)) - die("Unable to read current working directory"); - - offset = len = strlen(cwd); - for (;;) { - if (is_git_directory(".git")) - break; - if (offset == 0) { - offset = -1; - break; - } - chdir(".."); - while (cwd[--offset] != '/') - ; /* do nothing */ - } - - if (offset >= 0) { - inside_work_tree = 1; - git_config(git_default_config); - if (offset == len) { - inside_git_dir = 0; - return NULL; - } - - cwd[len++] = '/'; - cwd[len] = '\0'; - inside_git_dir = !prefixcmp(cwd + offset + 1, ".git/"); - return cwd + offset + 1; - } - - if (chdir(cwd)) - die("Cannot come back to cwd"); - if (!is_git_directory(".")) { - if (nongit_ok) { - *nongit_ok = 1; - return NULL; - } - die("Not a git repository"); - } - setenv(GIT_DIR_ENVIRONMENT, cwd, 1); - gitdirenv = getenv(GIT_DIR_ENVIRONMENT); - if (!gitdirenv) - die("getenv after setenv failed"); - } - - if (PATH_MAX - 40 < strlen(gitdirenv)) { - if (nongit_ok) { - *nongit_ok = 1; + if (gitdirenv) { + if (PATH_MAX - 40 < strlen(gitdirenv)) + die("'$%s' too big", GIT_DIR_ENVIRONMENT); + if (is_git_directory(gitdirenv)) { + if (!work_tree_env) + return set_work_tree(gitdirenv); return NULL; } - die("$%s too big", GIT_DIR_ENVIRONMENT); - } - if (!is_git_directory(gitdirenv)) { if (nongit_ok) { *nongit_ok = 1; return NULL; @@ -273,92 +266,53 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!getcwd(cwd, sizeof(cwd)-1)) die("Unable to read current working directory"); - if (chdir(gitdirenv)) { - if (nongit_ok) { - *nongit_ok = 1; - return NULL; - } - die("Cannot change directory to $%s '%s'", - GIT_DIR_ENVIRONMENT, gitdirenv); - } - if (!getcwd(gitdir, sizeof(gitdir)-1)) - die("Unable to read current working directory"); - if (chdir(cwd)) - die("Cannot come back to cwd"); /* - * In case there is a work tree we may change the directory, - * therefore make GIT_DIR an absolute path. + * Test in the following order (relative to the cwd): + * - .git/ + * - ./ (bare) + * - ../.git/ + * - ../ (bare) + * - ../../.git/ + * etc. */ - if (gitdirenv[0] != '/') { - setenv(GIT_DIR_ENVIRONMENT, gitdir, 1); - gitdirenv = getenv(GIT_DIR_ENVIRONMENT); - if (!gitdirenv) - die("getenv after setenv failed"); - if (PATH_MAX - 40 < strlen(gitdirenv)) { - if (nongit_ok) { - *nongit_ok = 1; - return NULL; - } - die("$%s too big after expansion to absolute path", - GIT_DIR_ENVIRONMENT); - } - } - - strcat(cwd, "/"); - strcat(gitdir, "/"); - inside_git_dir = !prefixcmp(cwd, gitdir); - - gitworktree = getenv(GIT_WORK_TREE_ENVIRONMENT); - if (!gitworktree) { - gitworktree_config = worktree; - worktree[0] = '\0'; - } - git_config(git_setup_config); - if (!gitworktree) { - gitworktree_config = NULL; - if (worktree[0]) - gitworktree = worktree; - if (gitworktree && gitworktree[0] != '/') - wt_rel_gitdir = 1; - } - - if (wt_rel_gitdir && chdir(gitdirenv)) - die("Cannot change directory to $%s '%s'", - GIT_DIR_ENVIRONMENT, gitdirenv); - if (gitworktree && chdir(gitworktree)) { - if (nongit_ok) { - if (wt_rel_gitdir && chdir(cwd)) - die("Cannot come back to cwd"); - *nongit_ok = 1; + offset = len = strlen(cwd); + for (;;) { + 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; + setenv(GIT_DIR_ENVIRONMENT, ".", 1); return NULL; } - if (wt_rel_gitdir) - die("Cannot change directory to working tree '%s'" - " from $%s", gitworktree, GIT_DIR_ENVIRONMENT); - else - die("Cannot change directory to working tree '%s'", - gitworktree); - } - if (!getcwd(worktree, sizeof(worktree)-1)) - die("Unable to read current working directory"); - strcat(worktree, "/"); - inside_work_tree = !prefixcmp(cwd, worktree); - - if (gitworktree && inside_work_tree && !prefixcmp(worktree, gitdir) && - strcmp(worktree, gitdir)) { - inside_git_dir = 0; + chdir(".."); + do { + if (!offset) { + if (nongit_ok) { + if (chdir(cwd)) + die("Cannot come back to cwd"); + *nongit_ok = 1; + return NULL; + } + die("Not a git repository"); + } + } while (cwd[--offset] != '/'); } - if (!inside_work_tree) { - if (chdir(cwd)) - die("Cannot come back to cwd"); + inside_git_dir = 0; + if (!work_tree_env) + inside_work_tree = 1; + git_work_tree_cfg = xstrndup(cwd, offset); + if (offset == len) return NULL; - } - if (!strcmp(cwd, worktree)) - return NULL; - return cwd+strlen(worktree); + /* Make "offset" point to past the '/', and add a '/' at the end */ + offset++; + cwd[len++] = '/'; + cwd[len] = 0; + return cwd + offset; } int git_config_perm(const char *var, const char *value) @@ -386,6 +340,16 @@ int check_repository_format_version(const char *var, const char *value) repository_format_version = git_config_int(var, value); else if (strcmp(var, "core.sharedrepository") == 0) shared_repository = git_config_perm(var, value); + else if (strcmp(var, "core.bare") == 0) { + is_bare_repository_cfg = git_config_bool(var, value); + if (is_bare_repository_cfg == 1) + inside_work_tree = -1; + } else if (strcmp(var, "core.worktree") == 0) { + if (git_work_tree_cfg) + free(git_work_tree_cfg); + git_work_tree_cfg = xstrdup(value); + inside_work_tree = -1; + } return 0; } @@ -402,5 +366,16 @@ const char *setup_git_directory(void) { const char *retval = setup_git_directory_gently(NULL); check_repository_format(); + + /* If the work tree is not the default one, recompute prefix */ + if (inside_work_tree < 0) { + static char buffer[PATH_MAX + 1]; + char *rel; + if (retval && chdir(retval)) + die ("Could not jump back into original cwd"); + rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree()); + return rel && *rel ? strcat(rel, "/") : NULL; + } + return retval; } -- cgit v1.2.1 From 6490a3383f1d0d96c122069e510ef1af1d019fbb Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 2 Aug 2007 15:10:56 -0700 Subject: Fix work-tree related breakages In set_work_tree(), variable rel needs to be reinitialized to NULL on every call (it should not be static). Make sure the incoming dir variable is not too long before copying to the temporary buffer, and make sure chdir to the resulting directory succeeds. This was spotted and fixed by Alex and Johannes in a handful patch exchanges. Here is the final version. Signed-off-by: Junio C Hamano Acked-by: Johannes Schindelin --- setup.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 3653092ab6..4945eb3134 100644 --- a/setup.c +++ b/setup.c @@ -201,26 +201,32 @@ int is_inside_work_tree(void) */ const char *set_work_tree(const char *dir) { - char dir_buffer[PATH_MAX]; - static char buffer[PATH_MAX + 1], *rel = NULL; - int len, postfix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1; + char dir_buffer[PATH_MAX], *rel = NULL; + static char buffer[PATH_MAX + 1]; + int len, suffix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1; /* strip the variable 'dir' of the postfix "/.git" if it has it */ len = strlen(dir); - if (len > postfix_len && !strcmp(dir + len - postfix_len, - "/" DEFAULT_GIT_DIR_ENVIRONMENT)) { - strncpy(dir_buffer, dir, len - postfix_len); + if (len > suffix_len && + !strcmp(dir + len - suffix_len, "/" DEFAULT_GIT_DIR_ENVIRONMENT)) { + if ((len - suffix_len) >= sizeof(dir_buffer)) + die("directory name too long"); + memcpy(dir_buffer, dir, len - suffix_len); + dir_buffer[len - suffix_len] = '\0'; /* are we inside the default work tree? */ rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer); } + /* if rel is set, the cwd is _not_ the current working tree */ if (rel && *rel) { if (!is_absolute_path(dir)) set_git_dir(make_absolute_path(dir)); dir = dir_buffer; - chdir(dir); - strcat(rel, "/"); + if (chdir(dir)) + die("cannot chdir to %s: %s", dir, strerror(errno)); + else + strcat(rel, "/"); inside_git_dir = 0; } else { rel = NULL; -- cgit v1.2.1 From 33a798c880f9a8bed1fe95531349e5e5ef1e0cd2 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 6 Aug 2007 00:20:06 -0700 Subject: setup.c:verify_non_filename(): don't die unnecessarily while disambiguating If you have a working tree _file_ "foo", attempt to refer to a branch "foo/bar" without -- to disambiguate, like this: $ git log foo/bar tried to make sure that foo/bar cannot be naming a working tree file "foo/bar" (in which case we would say "which one do you want? A rev or a working tree file? clarify with -- please"). We run lstat("foo/bar") to check that. If it does not succeed, there is no ambiguity. That is good. But we also checked the error status for the lstat() and expected it to fail with ENOENT. In this particular case, however, it fails with ENOTDIR. That should be treated as "expected error" as well. Signed-off-by: Junio C Hamano --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index a45ea8309a..2b8e8c0e5e 100644 --- a/setup.c +++ b/setup.c @@ -103,7 +103,7 @@ void verify_non_filename(const char *prefix, const char *arg) if (!lstat(name, &st)) die("ambiguous argument '%s': both revision and filename\n" "Use '--' to separate filenames from revisions", arg); - if (errno != ENOENT) + if (errno != ENOENT && errno != ENOTDIR) die("'%s': %s", arg, strerror(errno)); } -- cgit v1.2.1 From 7efeb8f09866ddd09485c0e6f371a6cbba3d2a0a Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 5 Aug 2007 14:12:53 +0100 Subject: Reinstate the old behaviour when GIT_DIR is set and GIT_WORK_TREE is unset The old behaviour was to unilaterally default to the cwd is the work tree when GIT_DIR was set, but GIT_WORK_TREE wasn't, no matter if we are inside the GIT_DIR, or if GIT_DIR is actually something like ../../../.git. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- setup.c | 52 ++++++++++------------------------------------------ 1 file changed, 10 insertions(+), 42 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index b55b82c99e..06004f1587 100644 --- a/setup.c +++ b/setup.c @@ -189,53 +189,21 @@ int is_inside_work_tree(void) } /* - * If no worktree was given, and we are outside of a default work tree, - * now is the time to set it. - * - * In other words, if the user calls git with something like - * - * git --git-dir=/some/where/else/.git bla - * - * default to /some/where/else as working directory; if the specified - * git-dir does not end in "/.git", the cwd is used as working directory. + * set_work_tree() is only ever called if you set GIT_DIR explicitely. + * The old behaviour (which we retain here) is to set the work tree root + * to the cwd, unless overridden by the config, the command line, or + * GIT_WORK_TREE. */ -const char *set_work_tree(const char *dir) +static const char *set_work_tree(const char *dir) { - char dir_buffer[PATH_MAX], *rel = NULL; - static char buffer[PATH_MAX + 1]; - int len, suffix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1; - - /* strip the variable 'dir' of the postfix "/.git" if it has it */ - len = strlen(dir); - if (len > suffix_len && - !strcmp(dir + len - suffix_len, "/" DEFAULT_GIT_DIR_ENVIRONMENT)) { - if ((len - suffix_len) >= sizeof(dir_buffer)) - die("directory name too long"); - memcpy(dir_buffer, dir, len - suffix_len); - dir_buffer[len - suffix_len] = '\0'; - - /* are we inside the default work tree? */ - rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer); - } + char buffer[PATH_MAX + 1]; - /* if rel is set, the cwd is _not_ the current working tree */ - if (rel && *rel) { - if (!is_absolute_path(dir)) - set_git_dir(make_absolute_path(dir)); - dir = dir_buffer; - if (chdir(dir)) - die("cannot chdir to %s: %s", dir, strerror(errno)); - else - strcat(rel, "/"); - inside_git_dir = 0; - } else { - rel = NULL; - dir = getcwd(buffer, sizeof(buffer)); - } - git_work_tree_cfg = xstrdup(dir); + if (!getcwd(buffer, sizeof(buffer))) + die ("Could not get the current working directory"); + git_work_tree_cfg = xstrdup(buffer); inside_work_tree = 1; - return rel; + return NULL; } /* -- cgit v1.2.1 From dd5c8af176bb935a0b01a7dc2d5e022565c3aac3 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 17 Oct 2007 00:37:36 +0100 Subject: Fix setup_git_directory_gently() with relative GIT_DIR & GIT_WORK_TREE There are a few programs, such as config and diff, which allow running without a git repository. Therefore, they have to call setup_git_directory_gently(). However, when GIT_DIR and GIT_WORK_TREE were set, and the current directory was a subdirectory of the work tree, setup_git_directory_gently() would return a bogus NULL prefix. This patch fixes that. Noticed by REPLeffect on IRC. Signed-off-by: Johannes Schindelin Signed-off-by: Shawn O. Pearce --- setup.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 06004f1587..145eca50f4 100644 --- a/setup.c +++ b/setup.c @@ -227,9 +227,20 @@ const char *setup_git_directory_gently(int *nongit_ok) 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) return set_work_tree(gitdirenv); - 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 ("Could not chdir to %s", work_tree_env); + strcat(buffer, "/"); + return retval; } if (nongit_ok) { *nongit_ok = 1; -- cgit v1.2.1 From 1e8a1954519a070e92cb73f756b271664e1cc4d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sat, 3 Nov 2007 20:18:06 +0700 Subject: Add missing inside_work_tree setting in setup_git_directory_gently MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When both GIT_DIR and GIT_WORK_TREE are set, and setup_git_directory_gently() changes the current working directory accordingly, it should also set inside_work_tree = 1. Without this, work_tree handling code in setup_git_directory() will be activated. If you stay in root work tree (no prefix), it does not harm. It does if you work from a subdirectory though. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- setup.c | 1 + 1 file changed, 1 insertion(+) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 145eca50f4..6f8f769584 100644 --- a/setup.c +++ b/setup.c @@ -240,6 +240,7 @@ const char *setup_git_directory_gently(int *nongit_ok) if (chdir(work_tree_env) < 0) die ("Could not chdir to %s", work_tree_env); strcat(buffer, "/"); + inside_work_tree = 1; return retval; } if (nongit_ok) { -- cgit v1.2.1 From 59f0f2f33a420e9a14bb8cef20d38f508f0d098e Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Sat, 3 Nov 2007 12:23:11 +0100 Subject: Refactor working tree setup Create a setup_work_tree() that can be used from any command requiring a working tree conditionally. Signed-off-by: Mike Hommey Acked-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- setup.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 145eca50f4..df006d9c4a 100644 --- a/setup.c +++ b/setup.c @@ -206,6 +206,15 @@ static const char *set_work_tree(const char *dir) return NULL; } +void setup_work_tree(void) { + const char *work_tree = get_git_work_tree(); + const char *git_dir = get_git_dir(); + if (!is_absolute_path(git_dir)) + set_git_dir(make_absolute_path(git_dir)); + if (!work_tree || chdir(work_tree)) + die("This operation must be run in a work tree"); +} + /* * 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. -- cgit v1.2.1 From f3fa1838024dc0f3741d2f9654af92f6a1bb34bd Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 8 Nov 2007 15:35:32 -0800 Subject: Style: place opening brace of a function definition at column 1 Signed-off-by: Junio C Hamano --- setup.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index df006d9c4a..1e2c55d076 100644 --- a/setup.c +++ b/setup.c @@ -206,7 +206,8 @@ static const char *set_work_tree(const char *dir) return NULL; } -void setup_work_tree(void) { +void setup_work_tree(void) +{ const char *work_tree = get_git_work_tree(); const char *git_dir = get_git_dir(); if (!is_absolute_path(git_dir)) -- cgit v1.2.1 From 354e6534c1569d0d23937add8e7b55bf87bbaf20 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 9 Nov 2007 11:34:07 +0000 Subject: builtin-blame: set up the work_tree before the first file access We check in cmd_blame() if the specified path is there, but we failed to set up the working tree before that. While at it, make setup_work_tree() just return if it was run before. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- setup.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 1e2c55d076..43cd3f94ea 100644 --- a/setup.c +++ b/setup.c @@ -208,12 +208,18 @@ static const char *set_work_tree(const char *dir) void setup_work_tree(void) { - const char *work_tree = get_git_work_tree(); - const char *git_dir = get_git_dir(); + const char *work_tree, *git_dir; + static int initialized = 0; + + if (initialized) + return; + work_tree = get_git_work_tree(); + git_dir = get_git_dir(); if (!is_absolute_path(git_dir)) set_git_dir(make_absolute_path(git_dir)); if (!work_tree || chdir(work_tree)) die("This operation must be run in a work tree"); + initialized = 1; } /* -- cgit v1.2.1 From ecf4831d89ba6986249e1bf232b2e2f5f7536223 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 25 Nov 2007 23:29:03 +0100 Subject: Use is_absolute_path() in diff-lib.c, lockfile.c, setup.c, trace.c Using the helper function to test for absolute paths makes porting easier. Signed-off-by: Steffen Prohaska Signed-off-by: Junio C Hamano --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 43cd3f94ea..2c7b5cb200 100644 --- a/setup.c +++ b/setup.c @@ -59,7 +59,7 @@ const char *prefix_path(const char *prefix, int len, const char *path) const char *prefix_filename(const char *pfx, int pfx_len, const char *arg) { static char path[PATH_MAX]; - if (!pfx || !*pfx || arg[0] == '/') + if (!pfx || !*pfx || is_absolute_path(arg)) return arg; memcpy(path, pfx, pfx_len); strcpy(path + pfx_len, arg); -- cgit v1.2.1 From 138dd1e990cef5ac0176426016ad5e1f8e5dff58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Thu, 29 Nov 2007 19:21:39 +0700 Subject: Do check_repository_format() early MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Repository version check is only performed when setup_git_directory() is called. This makes sure setup_git_directory_gently() does the check too. Signed-off-by: Nguyễn Thái Ngọc Duy Acked-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- setup.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 6f8f769584..8e4630ebfe 100644 --- a/setup.c +++ b/setup.c @@ -230,8 +230,14 @@ const char *setup_git_directory_gently(int *nongit_ok) static char buffer[1024 + 1]; const char *retval; - if (!work_tree_env) - return set_work_tree(gitdirenv); + if (!work_tree_env) { + retval = set_work_tree(gitdirenv); + /* config may override worktree + * see set_work_tree comment */ + check_repository_format(); + return retval; + } + check_repository_format(); retval = get_relative_cwd(buffer, sizeof(buffer) - 1, get_git_work_tree()); if (!retval || !*retval) @@ -271,6 +277,7 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!work_tree_env) inside_work_tree = 0; setenv(GIT_DIR_ENVIRONMENT, ".", 1); + check_repository_format(); return NULL; } chdir(".."); @@ -291,6 +298,7 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!work_tree_env) inside_work_tree = 1; git_work_tree_cfg = xstrndup(cwd, offset); + check_repository_format(); if (offset == len) return NULL; @@ -351,7 +359,6 @@ int check_repository_format(void) const char *setup_git_directory(void) { const char *retval = setup_git_directory_gently(NULL); - check_repository_format(); /* If the work tree is not the default one, recompute prefix */ if (inside_work_tree < 0) { -- cgit v1.2.1 From 9459aa77a032621a29d53605542844641cca843a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 5 Dec 2007 20:33:32 +0700 Subject: Do check_repository_format() early (re-fix) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This pushes check_repository_format() (actually _gently() version) to setup_git_directory_gently() in order to prevent from using unsupported repositories. New setup_git_directory_gently()'s behaviour is stop searching for a valid gitdir and return as if there is no gitdir if a unsupported repository is found. Warning will be thrown in these cases. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- setup.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 8e4630ebfe..067f1be3f9 100644 --- a/setup.c +++ b/setup.c @@ -206,6 +206,22 @@ static const char *set_work_tree(const char *dir) return NULL; } +static int check_repository_format_gently(int *nongit_ok) +{ + git_config(check_repository_format_version); + if (GIT_REPO_VERSION < repository_format_version) { + if (!nongit_ok) + die ("Expected git repo version <= %d, found %d", + GIT_REPO_VERSION, repository_format_version); + warning("Expected git repo version <= %d, found %d", + GIT_REPO_VERSION, repository_format_version); + warning("Please upgrade Git"); + *nongit_ok = -1; + return -1; + } + return 0; +} + /* * 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. @@ -232,12 +248,13 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!work_tree_env) { retval = set_work_tree(gitdirenv); - /* config may override worktree - * see set_work_tree comment */ - check_repository_format(); + /* config may override worktree */ + if (check_repository_format_gently(nongit_ok)) + return NULL; return retval; } - check_repository_format(); + if (check_repository_format_gently(nongit_ok)) + return NULL; retval = get_relative_cwd(buffer, sizeof(buffer) - 1, get_git_work_tree()); if (!retval || !*retval) @@ -246,7 +263,6 @@ const char *setup_git_directory_gently(int *nongit_ok) if (chdir(work_tree_env) < 0) die ("Could not chdir to %s", work_tree_env); strcat(buffer, "/"); - inside_work_tree = 1; return retval; } if (nongit_ok) { @@ -277,7 +293,7 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!work_tree_env) inside_work_tree = 0; setenv(GIT_DIR_ENVIRONMENT, ".", 1); - check_repository_format(); + check_repository_format_gently(nongit_ok); return NULL; } chdir(".."); @@ -298,7 +314,8 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!work_tree_env) inside_work_tree = 1; git_work_tree_cfg = xstrndup(cwd, offset); - check_repository_format(); + if (check_repository_format_gently(nongit_ok)) + return NULL; if (offset == len) return NULL; @@ -349,16 +366,13 @@ int check_repository_format_version(const char *var, const char *value) int check_repository_format(void) { - git_config(check_repository_format_version); - if (GIT_REPO_VERSION < repository_format_version) - die ("Expected git repo version <= %d, found %d", - GIT_REPO_VERSION, repository_format_version); - return 0; + return check_repository_format_gently(NULL); } const char *setup_git_directory(void) { const char *retval = setup_git_directory_gently(NULL); + check_repository_format(); /* If the work tree is not the default one, recompute prefix */ if (inside_work_tree < 0) { -- cgit v1.2.1 From bb4e3527b5d59a2db406dfa6519927a97d61770d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 9 Dec 2007 17:14:39 +0700 Subject: Remove repo version check from setup_git_directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit setup_git_directory_gently has done the check already. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- setup.c | 1 - 1 file changed, 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index e96a316452..b59dbe7f51 100644 --- a/setup.c +++ b/setup.c @@ -388,7 +388,6 @@ int check_repository_format(void) const char *setup_git_directory(void) { const char *retval = setup_git_directory_gently(NULL); - check_repository_format(); /* If the work tree is not the default one, recompute prefix */ if (inside_work_tree < 0) { -- cgit v1.2.1 From 790296fd8863d649054096191d33bacbbf5c2115 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 3 Jan 2008 15:18:07 +0100 Subject: Fix grammar nits in documentation and in code comments. Signed-off-by: Jim Meyering Signed-off-by: Junio C Hamano --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index b59dbe7f51..adede16a4d 100644 --- a/setup.c +++ b/setup.c @@ -140,7 +140,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec) * Test if it looks like we're at a git directory. * We want to see: * - * - either a objects/ directory _or_ the proper + * - either an objects/ directory _or_ the proper * GIT_OBJECT_DIRECTORY environment variable * - a refs/ directory * - either a HEAD symlink or a HEAD file that is formatted as -- cgit v1.2.1 From d089ebaad5315325d67db30176df1bbd7754fda9 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 28 Jan 2008 22:44:27 -0800 Subject: setup: sanitize absolute and funny paths in get_pathspec() The prefix_path() function called from get_pathspec() is responsible for translating list of user-supplied pathspecs to list of pathspecs that is relative to the root of the work tree. When working inside a subdirectory, the user-supplied pathspecs are taken to be relative to the current subdirectory. Among special path components in pathspecs, we used to accept and interpret only "." ("the directory", meaning a no-op) and ".." ("up one level") at the beginning. Everything else was passed through as-is. For example, if you are in Documentation/ directory of the project, you can name Documentation/howto/maintain-git.txt as: howto/maintain-git.txt ../Documentation/howto/maitain-git.txt ../././Documentation/howto/maitain-git.txt but not as: howto/./maintain-git.txt $(pwd)/howto/maintain-git.txt This patch updates prefix_path() in several ways: - If the pathspec is not absolute, prefix (i.e. the current subdirectory relative to the root of the work tree, with terminating slash, if not empty) and the pathspec is concatenated first and used in the next step. Otherwise, that absolute pathspec is used in the next step. - Then special path components "." (no-op) and ".." (up one level) are interpreted to simplify the path. It is an error to have too many ".." to cause the intermediate result to step outside of the input to this step. - If the original pathspec was not absolute, the result from the previous step is the resulting "sanitized" pathspec. Otherwise, the result from the previous step is still absolute, and it is an error if it does not begin with the directory that corresponds to the root of the work tree. The directory is stripped away from the result and is returned. - In any case, the resulting pathspec in the array get_pathspec() returns omit the ones that caused errors. With this patch, the last two examples also behave as expected. Signed-off-by: Junio C Hamano --- setup.c | 158 +++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 116 insertions(+), 42 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index adede16a4d..23c9a11be5 100644 --- a/setup.c +++ b/setup.c @@ -4,51 +4,118 @@ static int inside_git_dir = -1; static int inside_work_tree = -1; -const char *prefix_path(const char *prefix, int len, const char *path) +static int sanitary_path_copy(char *dst, const char *src) { - const char *orig = path; + char *dst0 = dst; + + if (*src == '/') { + *dst++ = '/'; + while (*src == '/') + src++; + } + for (;;) { - char c; - if (*path != '.') - break; - c = path[1]; - /* "." */ - if (!c) { - path++; - break; + char c = *src; + + /* + * A path component that begins with . could be + * special: + * (1) "." and ends -- ignore and terminate. + * (2) "./" -- ignore them, eat slash and continue. + * (3) ".." and ends -- strip one and terminate. + * (4) "../" -- strip one, eat slash and continue. + */ + if (c == '.') { + switch (src[1]) { + case '\0': + /* (1) */ + src++; + break; + case '/': + /* (2) */ + src += 2; + while (*src == '/') + src++; + continue; + case '.': + switch (src[2]) { + case '\0': + /* (3) */ + src += 2; + goto up_one; + case '/': + /* (4) */ + src += 3; + while (*src == '/') + src++; + goto up_one; + } + } } - /* "./" */ + + /* copy up to the next '/', and eat all '/' */ + while ((c = *src++) != '\0' && c != '/') + *dst++ = c; if (c == '/') { - path += 2; - continue; - } - if (c != '.') + *dst++ = c; + while (c == '/') + c = *src++; + src--; + } else if (!c) break; - c = path[2]; - if (!c) - path += 2; - else if (c == '/') - path += 3; - else - break; - /* ".." and "../" */ - /* Remove last component of the prefix */ - do { - if (!len) - die("'%s' is outside repository", orig); - len--; - } while (len && prefix[len-1] != '/'); continue; + + up_one: + /* + * dst0..dst is prefix portion, and dst[-1] is '/'; + * go up one level. + */ + dst -= 2; /* go past trailing '/' if any */ + if (dst < dst0) + return -1; + while (1) { + if (dst <= dst0) + break; + c = *dst--; + if (c == '/') { + dst += 2; + break; + } + } } - if (len) { - int speclen = strlen(path); - char *n = xmalloc(speclen + len + 1); + *dst = '\0'; + return 0; +} - memcpy(n, prefix, len); - memcpy(n + len, path, speclen+1); - path = n; +const char *prefix_path(const char *prefix, int len, const char *path) +{ + const char *orig = path; + char *sanitized = xmalloc(len + strlen(path) + 1); + if (*orig == '/') + strcpy(sanitized, path); + else { + if (len) + memcpy(sanitized, prefix, len); + strcpy(sanitized + len, path); } - return path; + if (sanitary_path_copy(sanitized, sanitized)) + goto error_out; + if (*orig == '/') { + const char *work_tree = get_git_work_tree(); + size_t len = strlen(work_tree); + size_t total = strlen(sanitized) + 1; + if (strncmp(sanitized, work_tree, len) || + (sanitized[len] != '\0' && sanitized[len] != '/')) { + error_out: + error("'%s' is outside repository", orig); + free(sanitized); + return NULL; + } + if (sanitized[len] == '/') + len++; + memmove(sanitized, sanitized + len, total - len); + } + return sanitized; } /* @@ -114,7 +181,7 @@ void verify_non_filename(const char *prefix, const char *arg) const char **get_pathspec(const char *prefix, const char **pathspec) { const char *entry = *pathspec; - const char **p; + const char **src, **dst; int prefixlen; if (!prefix && !entry) @@ -128,12 +195,19 @@ const char **get_pathspec(const char *prefix, const char **pathspec) } /* Otherwise we have to re-write the entries.. */ - p = pathspec; + src = pathspec; + dst = pathspec; prefixlen = prefix ? strlen(prefix) : 0; - do { - *p = prefix_path(prefix, prefixlen, entry); - } while ((entry = *++p) != NULL); - return (const char **) pathspec; + while (*src) { + const char *p = prefix_path(prefix, prefixlen, *src); + if (p) + *(dst++) = p; + src++; + } + *dst = NULL; + if (!*pathspec) + return NULL; + return pathspec; } /* -- cgit v1.2.1 From 180483c5c94a37c9fe304dded99d3db56d2d2b38 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 11 Feb 2008 11:00:32 -0800 Subject: setup.c: guard config parser from value=NULL core.worktree expects a string value Signed-off-by: Junio C Hamano --- setup.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'setup.c') diff --git a/setup.c b/setup.c index adede16a4d..4509598d57 100644 --- a/setup.c +++ b/setup.c @@ -372,6 +372,8 @@ int check_repository_format_version(const char *var, const char *value) if (is_bare_repository_cfg == 1) inside_work_tree = -1; } else if (strcmp(var, "core.worktree") == 0) { + if (!value) + return config_error_nonbool(var); if (git_work_tree_cfg) free(git_work_tree_cfg); git_work_tree_cfg = xstrdup(value); -- cgit v1.2.1 From 9a13ba1bed0e573a7c4523c0c8ed8465c9f341bb Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Tue, 19 Feb 2008 22:29:40 +0100 Subject: prefix_path: use is_absolute_path() instead of *orig == '/' Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- setup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index bc80301c42..dc247a84c4 100644 --- a/setup.c +++ b/setup.c @@ -91,7 +91,7 @@ const char *prefix_path(const char *prefix, int len, const char *path) { const char *orig = path; char *sanitized = xmalloc(len + strlen(path) + 1); - if (*orig == '/') + if (is_absolute_path(orig)) strcpy(sanitized, path); else { if (len) @@ -100,7 +100,7 @@ const char *prefix_path(const char *prefix, int len, const char *path) } if (sanitary_path_copy(sanitized, sanitized)) goto error_out; - if (*orig == '/') { + if (is_absolute_path(orig)) { const char *work_tree = get_git_work_tree(); size_t len = strlen(work_tree); size_t total = strlen(sanitized) + 1; -- cgit v1.2.1 From 8e0f70033b2bd1679a6e5971978fdc3ee09bdb72 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 31 Jan 2008 18:26:32 +0100 Subject: Avoid unnecessary "if-before-free" tests. This change removes all obvious useless if-before-free tests. E.g., it replaces code like this: if (some_expression) free (some_expression); with the now-equivalent: free (some_expression); It is equivalent not just because POSIX has required free(NULL) to work for a long time, but simply because it has worked for so long that no reasonable porting target fails the test. Here's some evidence from nearly 1.5 years ago: http://www.winehq.org/pipermail/wine-patches/2006-October/031544.html FYI, the change below was prepared by running the following: git ls-files -z | xargs -0 \ perl -0x3b -pi -e \ 's/\bif\s*\(\s*(\S+?)(?:\s*!=\s*NULL)?\s*\)\s+(free\s*\(\s*\1\s*\))/$2/s' Note however, that it doesn't handle brace-enclosed blocks like "if (x) { free (x); }". But that's ok, since there were none like that in git sources. Beware: if you do use the above snippet, note that it can produce syntactically invalid C code. That happens when the affected "if"-statement has a matching "else". E.g., it would transform this if (x) free (x); else foo (); into this: free (x); else foo (); There were none of those here, either. If you're interested in automating detection of the useless tests, you might like the useless-if-before-free script in gnulib: [it *does* detect brace-enclosed free statements, and has a --name=S option to make it detect free-like functions with different names] http://git.sv.gnu.org/gitweb/?p=gnulib.git;a=blob;f=build-aux/useless-if-before-free Addendum: Remove one more (in imap-send.c), spotted by Jean-Luc Herren . Signed-off-by: Jim Meyering Signed-off-by: Junio C Hamano --- setup.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index dc247a84c4..89c81e54e6 100644 --- a/setup.c +++ b/setup.c @@ -448,8 +448,7 @@ int check_repository_format_version(const char *var, const char *value) } else if (strcmp(var, "core.worktree") == 0) { if (!value) return config_error_nonbool(var); - if (git_work_tree_cfg) - free(git_work_tree_cfg); + free(git_work_tree_cfg); git_work_tree_cfg = xstrdup(value); inside_work_tree = -1; } -- cgit v1.2.1 From 3296766eb5531ef051ae392114de5d75556f5613 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 6 Mar 2008 23:18:08 -0800 Subject: get_pathspec(): die when an out-of-tree path is given An earlier commit d089ebaa (setup: sanitize absolute and funny paths) made get_pathspec() aware of absolute paths, but with a botched interface that forced the callers to count the resulting pathspecs in order to detect an error of giving a path that is outside the work tree. This fixes it, by dying inside the function. We had ls-tree test that relied on a misfeature in the original implementation of its pathspec handling. Leading slashes were silently removed from them. However we allow giving absolute pathnames (people want to cut and paste from elsewhere) that are inside work tree these days, so a pathspec that begin with slash _should_ be treated as a full path. The test is adjusted to match the updated rule for get_pathspec(). Earlier I mistook three tests given by Robin that they should succeed, but these are attempts to add path outside work tree, which should fail loudly. These tests also have been fixed. Signed-off-by: Junio C Hamano --- setup.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 89c81e54e6..41e298b8f5 100644 --- a/setup.c +++ b/setup.c @@ -202,6 +202,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec) const char *p = prefix_path(prefix, prefixlen, *src); if (p) *(dst++) = p; + else + exit(128); /* error message already given */ src++; } *dst = NULL; -- cgit v1.2.1 From af05d67939f306a68c5f81ed30f65a82ae025661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Tue, 25 Mar 2008 22:06:26 +0100 Subject: Always set *nongit_ok in setup_git_directory_gently() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit setup_git_directory_gently() only modified the value of its *nongit_ok argument if we were not in a git repository. Now it will always set it to 0 when we are inside a repository. Also remove now unnecessary initializations in the callers of this function. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- setup.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 41e298b8f5..3d2d9580f3 100644 --- a/setup.c +++ b/setup.c @@ -325,6 +325,14 @@ const char *setup_git_directory_gently(int *nongit_ok) const char *gitdirenv; int len, 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 -- cgit v1.2.1 From b44ebb19e3234c5dffe9869ceac5408bb44c2e20 Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Wed, 20 Feb 2008 23:13:13 +0100 Subject: Add platform-independent .git "symlink" This patch allows .git to be a regular textfile containing the path of the real git directory (prefixed with "gitdir: "), which can be useful on platforms lacking support for real symlinks. Signed-off-by: Lars Hjemli Signed-off-by: Junio C Hamano --- setup.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 3d2d9580f3..53580fde43 100644 --- a/setup.c +++ b/setup.c @@ -314,6 +314,44 @@ static int check_repository_format_gently(int *nongit_ok) return 0; } +/* + * 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("Error opening %s: %s", path, strerror(errno)); + 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; +} + /* * 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. @@ -323,6 +361,7 @@ const char *setup_git_directory_gently(int *nongit_ok) const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); static char cwd[PATH_MAX+1]; const char *gitdirenv; + const char *gitfile_dir; int len, offset; /* @@ -377,8 +416,10 @@ const char *setup_git_directory_gently(int *nongit_ok) /* * Test in the following order (relative to the cwd): + * - .git (file containing "gitdir: ") * - .git/ * - ./ (bare) + * - ../.git * - ../.git/ * - ../ (bare) * - ../../.git/ @@ -386,6 +427,12 @@ const char *setup_git_directory_gently(int *nongit_ok) */ 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(".")) { -- cgit v1.2.1 From 06cbe8550324e0fd2290839bf3b9a92aa53b70ab Mon Sep 17 00:00:00 2001 From: Heikki Orsila Date: Wed, 16 Apr 2008 11:34:24 +0300 Subject: Make core.sharedRepository more generic git init --shared=0xxx, where '0xxx' is an octal number, will create a repository with file modes set to '0xxx'. Users with a safe umask value (0077) can use this option to force file modes. For example, '0640' is a group-readable but not group-writable regardless of user's umask value. Values compatible with old Git versions are written as they were before, for compatibility reasons. That is, "1" for "group" and "2" for "everybody". "git config core.sharedRepository 0xxx" is also handled. Signed-off-by: Heikki Orsila Signed-off-by: Junio C Hamano --- setup.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 14 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 3d2d9580f3..1b4fa6a8c4 100644 --- a/setup.c +++ b/setup.c @@ -428,21 +428,53 @@ const char *setup_git_directory_gently(int *nongit_ok) int git_config_perm(const char *var, const char *value) { - if (value) { - int i; - if (!strcmp(value, "umask")) - return PERM_UMASK; - if (!strcmp(value, "group")) - return PERM_GROUP; - if (!strcmp(value, "all") || - !strcmp(value, "world") || - !strcmp(value, "everybody")) - return PERM_EVERYBODY; - i = atoi(value); - if (i > 1) - return i; + int i; + char *endptr; + + if (value == NULL) + return PERM_GROUP; + + if (!strcmp(value, "umask")) + return PERM_UMASK; + if (!strcmp(value, "group")) + return PERM_GROUP; + if (!strcmp(value, "all") || + !strcmp(value, "world") || + !strcmp(value, "everybody")) + return PERM_EVERYBODY; + + /* Parse octal numbers */ + i = strtol(value, &endptr, 8); + + /* If not an octal number, maybe true/false? */ + if (*endptr != 0) + return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; + + /* + * Treat values 0, 1 and 2 as compatibility cases, otherwise it is + * a chmod value. + */ + switch (i) { + case PERM_UMASK: /* 0 */ + return PERM_UMASK; + case OLD_PERM_GROUP: /* 1 */ + return PERM_GROUP; + case OLD_PERM_EVERYBODY: /* 2 */ + return PERM_EVERYBODY; } - return git_config_bool(var, value); + + /* A filemode value was given: 0xxx */ + + if ((i & 0600) != 0600) + die("Problem with core.sharedRepository filemode value " + "(0%.3o).\nThe owner of files must always have " + "read and write permissions.", i); + + /* + * Mask filemode value. Others can not get write permission. + * x flags for directories are handled separately. + */ + return i & 0666; } int check_repository_format_version(const char *var, const char *value) -- cgit v1.2.1 From ef90d6d4208a5130185b04f06e5f90a5f9959fe3 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 14 May 2008 18:46:53 +0100 Subject: Provide git_config with a callback-data parameter git_config() only had a function parameter, but no callback data parameter. This assumes that all callback functions only modify global variables. With this patch, every callback gets a void * parameter, and it is hoped that this will help the libification effort. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- setup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index b8fd476395..d630e374e7 100644 --- a/setup.c +++ b/setup.c @@ -300,7 +300,7 @@ void setup_work_tree(void) static int check_repository_format_gently(int *nongit_ok) { - git_config(check_repository_format_version); + git_config(check_repository_format_version, NULL); if (GIT_REPO_VERSION < repository_format_version) { if (!nongit_ok) die ("Expected git repo version <= %d, found %d", @@ -524,7 +524,7 @@ int git_config_perm(const char *var, const char *value) return i & 0666; } -int check_repository_format_version(const char *var, const char *value) +int check_repository_format_version(const char *var, const char *value, void *cb) { if (strcmp(var, "core.repositoryformatversion") == 0) repository_format_version = git_config_int(var, value); -- cgit v1.2.1 From 0454dd93bfb2334355ec62fff670d8c6cb3570a1 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Mon, 19 May 2008 23:49:26 -0700 Subject: Add support for GIT_CEILING_DIRECTORIES Make git recognize a new environment variable that prevents it from chdir'ing up into specified directories when looking for a GIT_DIR. Useful for avoiding slow network directories. For example, I use git in an environment where homedirs are automounted and "ls /home/nonexistent" takes about 9 seconds. Setting GIT_CEILING_DIRS="/home" allows "git help -a" (for bash completion) and "git symbolic-ref" (for my shell prompt) to run in a reasonable time. Signed-off-by: David Reiss Signed-off-by: Junio C Hamano --- setup.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index b8fd476395..c14b10636f 100644 --- a/setup.c +++ b/setup.c @@ -359,10 +359,11 @@ const char *read_gitfile_gently(const char *path) 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; + int len, offset, ceil_offset; /* * Let's assume that we are in a git repository. @@ -414,6 +415,8 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!getcwd(cwd, sizeof(cwd)-1)) die("Unable to read current working directory"); + ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs); + /* * Test in the following order (relative to the cwd): * - .git (file containing "gitdir: ") @@ -444,17 +447,16 @@ const char *setup_git_directory_gently(int *nongit_ok) return NULL; } chdir(".."); - do { - if (!offset) { - if (nongit_ok) { - if (chdir(cwd)) - die("Cannot come back to cwd"); - *nongit_ok = 1; - return NULL; - } - die("Not a git repository"); + while (--offset > ceil_offset && cwd[offset] != '/'); + if (offset <= ceil_offset) { + if (nongit_ok) { + if (chdir(cwd)) + die("Cannot come back to cwd"); + *nongit_ok = 1; + return NULL; } - } while (cwd[--offset] != '/'); + die("Not a git repository"); + } } inside_git_dir = 0; -- cgit v1.2.1 From 450f437fb0fe276a6e946c281c768118e39dc9e7 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Mon, 19 May 2008 23:49:34 -0700 Subject: Eliminate an unnecessary chdir("..") In the case where setup_git_directory_gently fails, avoid the last chdir("..") by moving it after the ceil_offset check. Signed-off-by: David Reiss Signed-off-by: Junio C Hamano --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index c14b10636f..045ca20b32 100644 --- a/setup.c +++ b/setup.c @@ -446,7 +446,6 @@ const char *setup_git_directory_gently(int *nongit_ok) check_repository_format_gently(nongit_ok); return NULL; } - chdir(".."); while (--offset > ceil_offset && cwd[offset] != '/'); if (offset <= ceil_offset) { if (nongit_ok) { @@ -457,6 +456,7 @@ const char *setup_git_directory_gently(int *nongit_ok) } die("Not a git repository"); } + chdir(".."); } inside_git_dir = 0; -- cgit v1.2.1 From 044bbbcb63281dfdb78344ada2c44c96122dc822 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 19 Jun 2008 12:34:06 -0700 Subject: Make git_dir a path relative to work_tree in setup_work_tree() Once we find the absolute paths for git_dir and work_tree, we can make git_dir a relative path since we know pwd will be work_tree. This should save the kernel some time traversing the path to work_tree all the time if git_dir is inside work_tree. Daniel's patch didn't apply for me as-is, so I recreated it with some differences, and here are the numbers from ten runs each. There is some IO for me - probably due to more-or-less random flushing of the journal - so the variation is bigger than I'd like, but whatever: Before: real 0m8.135s real 0m7.933s real 0m8.080s real 0m7.954s real 0m7.949s real 0m8.112s real 0m7.934s real 0m8.059s real 0m7.979s real 0m8.038s After: real 0m7.685s real 0m7.968s real 0m7.703s real 0m7.850s real 0m7.995s real 0m7.817s real 0m7.963s real 0m7.955s real 0m7.848s real 0m7.969s Now, going by "best of ten" (on the assumption that the longer numbers are all due to IO), I'm saying a 7.933s -> 7.685s reduction, and it does seem to be outside of the noise (ie the "after" case never broke 8s, while the "before" case did so half the time). So looks like about 3% to me. Doing it for a slightly smaller test-case (just the "arch" subdirectory) gets more stable numbers probably due to not filling the journal with metadata updates, so we have: Before: real 0m1.633s real 0m1.633s real 0m1.633s real 0m1.632s real 0m1.632s real 0m1.630s real 0m1.634s real 0m1.631s real 0m1.632s real 0m1.632s After: real 0m1.610s real 0m1.609s real 0m1.610s real 0m1.608s real 0m1.607s real 0m1.610s real 0m1.609s real 0m1.611s real 0m1.608s real 0m1.611s where I'ld just take the averages and say 1.632 vs 1.610, which is just over 1% peformance improvement. So it's not in the noise, but it's not as big as I initially thought and measured. (That said, it obviously depends on how deep the working directory path is too, and whether it is behind NFS or something else that might need to cause more work to look up). Signed-off-by: Junio C Hamano --- setup.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index d630e374e7..3b111ea7cf 100644 --- a/setup.c +++ b/setup.c @@ -292,9 +292,10 @@ void setup_work_tree(void) work_tree = get_git_work_tree(); git_dir = get_git_dir(); if (!is_absolute_path(git_dir)) - set_git_dir(make_absolute_path(git_dir)); + git_dir = make_absolute_path(git_dir); if (!work_tree || chdir(work_tree)) die("This operation must be run in a work tree"); + set_git_dir(make_relative_path(git_dir, work_tree)); initialized = 1; } -- cgit v1.2.1 From 4cd148d83f852363363e921c4925e67601654ff6 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sat, 1 Mar 2008 21:11:14 +0100 Subject: setup.c: Prepare for Windows directory separators. This turns two switch/case statements into an if-else-if cascade because we later do not want to have case '/': #ifdef __MINGW32__ case '\\': #endif but use a predicate is_dir_sep(foo) in order to check for the directory separator. Signed-off-by: Johannes Sixt --- setup.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index d630e374e7..5fc89fd7da 100644 --- a/setup.c +++ b/setup.c @@ -26,24 +26,21 @@ static int sanitary_path_copy(char *dst, const char *src) * (4) "../" -- strip one, eat slash and continue. */ if (c == '.') { - switch (src[1]) { - case '\0': + if (!src[1]) { /* (1) */ src++; - break; - case '/': + } else if (src[1] == '/') { /* (2) */ src += 2; while (*src == '/') src++; continue; - case '.': - switch (src[2]) { - case '\0': + } else if (src[1] == '.') { + if (!src[2]) { /* (3) */ src += 2; goto up_one; - case '/': + } else if (src[2] == '/') { /* (4) */ src += 3; while (*src == '/') -- cgit v1.2.1 From 25fe217b86ca40c53e710d776e120dfa0d81f60b Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Wed, 5 Mar 2008 21:51:27 +0100 Subject: Windows: Treat Windows style path names. GIT's guts work with a forward slash as a path separators. We do not change that. Rather we make sure that only "normalized" paths enter the depths of the machinery. We have to translate backslashes to forward slashes in the prefix and in command line arguments. Fortunately, all of them are passed through functions in setup.c. A macro has_dos_drive_path() is defined that checks whether a path begins with a drive letter+colon combination. This predicate is always false on Unix. Another macro is_dir_sep() abstracts that a backslash is also a directory separator on Windows. Signed-off-by: Johannes Sixt --- setup.c | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 5fc89fd7da..8bb7b10174 100644 --- a/setup.c +++ b/setup.c @@ -6,11 +6,17 @@ static int inside_work_tree = -1; static int sanitary_path_copy(char *dst, const char *src) { - char *dst0 = dst; + char *dst0; - if (*src == '/') { + if (has_dos_drive_prefix(src)) { + *dst++ = *src++; + *dst++ = *src++; + } + dst0 = dst; + + if (is_dir_sep(*src)) { *dst++ = '/'; - while (*src == '/') + while (is_dir_sep(*src)) src++; } @@ -29,10 +35,10 @@ static int sanitary_path_copy(char *dst, const char *src) if (!src[1]) { /* (1) */ src++; - } else if (src[1] == '/') { + } else if (is_dir_sep(src[1])) { /* (2) */ src += 2; - while (*src == '/') + while (is_dir_sep(*src)) src++; continue; } else if (src[1] == '.') { @@ -40,10 +46,10 @@ static int sanitary_path_copy(char *dst, const char *src) /* (3) */ src += 2; goto up_one; - } else if (src[2] == '/') { + } else if (is_dir_sep(src[2])) { /* (4) */ src += 3; - while (*src == '/') + while (is_dir_sep(*src)) src++; goto up_one; } @@ -51,11 +57,11 @@ static int sanitary_path_copy(char *dst, const char *src) } /* copy up to the next '/', and eat all '/' */ - while ((c = *src++) != '\0' && c != '/') + while ((c = *src++) != '\0' && !is_dir_sep(c)) *dst++ = c; - if (c == '/') { - *dst++ = c; - while (c == '/') + if (is_dir_sep(c)) { + *dst++ = '/'; + while (is_dir_sep(c)) c = *src++; src--; } else if (!c) @@ -74,7 +80,7 @@ static int sanitary_path_copy(char *dst, const char *src) if (dst <= dst0) break; c = *dst--; - if (c == '/') { + if (c == '/') { /* MinGW: cannot be '\\' anymore */ dst += 2; break; } @@ -123,10 +129,23 @@ const char *prefix_path(const char *prefix, int len, const char *path) const char *prefix_filename(const char *pfx, int pfx_len, const char *arg) { static char path[PATH_MAX]; +#ifndef __MINGW32__ if (!pfx || !*pfx || is_absolute_path(arg)) return arg; memcpy(path, pfx, pfx_len); strcpy(path + pfx_len, arg); +#else + char *p; + /* don't add prefix to absolute paths, but still replace '\' by '/' */ + if (is_absolute_path(arg)) + pfx_len = 0; + else + memcpy(path, pfx, pfx_len); + strcpy(path + pfx_len, arg); + for (p = path + pfx_len; *p; p++) + if (*p == '\\') + *p = '/'; +#endif return path; } @@ -360,6 +379,7 @@ const char *setup_git_directory_gently(int *nongit_ok) const char *gitdirenv; const char *gitfile_dir; int len, offset; + int minoffset = 0; /* * Let's assume that we are in a git repository. @@ -410,6 +430,8 @@ const char *setup_git_directory_gently(int *nongit_ok) if (!getcwd(cwd, sizeof(cwd)-1)) die("Unable to read current working directory"); + if (has_dos_drive_prefix(cwd)) + minoffset = 2; /* * Test in the following order (relative to the cwd): @@ -442,7 +464,7 @@ const char *setup_git_directory_gently(int *nongit_ok) } chdir(".."); do { - if (!offset) { + if (offset <= minoffset) { if (nongit_ok) { if (chdir(cwd)) die("Cannot come back to cwd"); @@ -451,7 +473,7 @@ const char *setup_git_directory_gently(int *nongit_ok) } die("Not a git repository"); } - } while (cwd[--offset] != '/'); + } while (offset > minoffset && cwd[--offset] != '/'); } inside_git_dir = 0; -- cgit v1.2.1 From bb528633b34ac9c338a7761f3e1d251e0c560ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sat, 30 Aug 2008 16:15:32 +0700 Subject: setup_git_directory(): fix move to worktree toplevel directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When setup_git_directory() returns successfully, it is supposed to move current working directory to worktree toplevel directory. However, the code recomputing prefix inside setup_git_directory() has to move cwd back to original working directory, in order to get new prefix. After that, it should move cwd back to worktree toplevel directory as expected. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- setup.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 6cf909463d..2e3248a0c4 100644 --- a/setup.c +++ b/setup.c @@ -581,6 +581,8 @@ const char *setup_git_directory(void) if (retval && chdir(retval)) die ("Could not jump back into original cwd"); rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree()); + if (rel && *rel && chdir(get_git_work_tree())) + die ("Could not jump to working directory"); return rel && *rel ? strcat(rel, "/") : NULL; } -- cgit v1.2.1 From 62525ef78e8aec776f44da525030d76b52f14a57 Mon Sep 17 00:00:00 2001 From: Dmitry Potapov Date: Sun, 5 Oct 2008 04:40:36 +0400 Subject: make prefix_path() never return NULL There are 9 places where prefix_path is called, and only in one of them the returned pointer was checked to be non-zero and only to call exit(128) as it is usually done by die(). In other 8 places, the returned value was not checked and it caused SIGSEGV when a path outside of the working tree was used. For instance, running git update-index --add /some/path/outside caused SIGSEGV. This patch changes prefix_path() to die if the path is outside of the repository, so it never returns NULL. Signed-off-by: Dmitry Potapov Signed-off-by: Shawn O. Pearce --- setup.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 2e3248a0c4..78a8041ff0 100644 --- a/setup.c +++ b/setup.c @@ -110,9 +110,7 @@ const char *prefix_path(const char *prefix, int len, const char *path) if (strncmp(sanitized, work_tree, len) || (sanitized[len] != '\0' && sanitized[len] != '/')) { error_out: - error("'%s' is outside repository", orig); - free(sanitized); - return NULL; + die("'%s' is outside repository", orig); } if (sanitized[len] == '/') len++; @@ -216,10 +214,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec) prefixlen = prefix ? strlen(prefix) : 0; while (*src) { const char *p = prefix_path(prefix, prefixlen, *src); - if (p) - *(dst++) = p; - else - exit(128); /* error message already given */ + *(dst++) = p; src++; } *dst = NULL; -- cgit v1.2.1 From 7be77de266b5cbcbd28d69a04df6f0b167b87cf6 Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Fri, 5 Dec 2008 01:36:46 +0100 Subject: Make chdir failures visible Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- setup.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 78a8041ff0..833ced2269 100644 --- a/setup.c +++ b/setup.c @@ -470,7 +470,8 @@ const char *setup_git_directory_gently(int *nongit_ok) } die("Not a git repository"); } - chdir(".."); + if (chdir("..")) + die("Cannot change to %s/..: %s", cwd, strerror(errno)); } inside_git_dir = 0; -- cgit v1.2.1 From f66bc5f928194366ee5eb78ef18a3562fb1bb7cf Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Mon, 22 Dec 2008 00:17:32 +0100 Subject: Always show which directory is not a git repository Unify all fatal: Not a git repository error messages so they include path information. Signed-off-by: Richard Hartmann Signed-off-by: Junio C Hamano --- setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 833ced2269..6b277b6a11 100644 --- a/setup.c +++ b/setup.c @@ -468,7 +468,7 @@ const char *setup_git_directory_gently(int *nongit_ok) *nongit_ok = 1; return NULL; } - die("Not a git repository"); + die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT); } if (chdir("..")) die("Cannot change to %s/..: %s", cwd, strerror(errno)); -- cgit v1.2.1 From 72183cb297f614dc2df1c0ed08afac1be27ec35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SZEDER=20G=C3=A1bor?= Date: Fri, 16 Jan 2009 16:37:33 +0100 Subject: Fix gitdir detection when in subdir of gitdir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the current working directory is a subdirectory of the gitdir (e.g. /.git/refs/), then setup_git_directory_gently() will climb its parent directories until it finds itself in a gitdir. However, no matter how many parent directories it climbs, it sets 'GIT_DIR_ENVIRONMENT' to ".", which is obviously wrong. This behaviour affected at least 'git rev-parse --git-dir' and hence caused some errors in bash completion (e.g. customized command prompt when on a detached head and completion of refs). To fix this, we set the absolute path of the found gitdir instead. Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- setup.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 78a8041ff0..dd7c039f0d 100644 --- a/setup.c +++ b/setup.c @@ -456,7 +456,11 @@ const char *setup_git_directory_gently(int *nongit_ok) inside_git_dir = 1; if (!work_tree_env) inside_work_tree = 0; - setenv(GIT_DIR_ENVIRONMENT, ".", 1); + 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; } -- cgit v1.2.1 From f3cad0ad82e24966bf7bcc8a47670c54c30e4b18 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sat, 7 Feb 2009 16:08:28 +0100 Subject: Move sanitary_path_copy() to path.c and rename it to normalize_path_copy() This function and normalize_absolute_path() do almost the same thing. The former already works on Windows, but the latter crashes. In subsequent changes we will remove normalize_absolute_path(). Here we make the replacement function reusable. On the way we rename it to reflect that it does some path normalization. Apart from that this is only moving around code. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- setup.c | 88 +---------------------------------------------------------------- 1 file changed, 1 insertion(+), 87 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 78a8041ff0..59735c1450 100644 --- a/setup.c +++ b/setup.c @@ -4,92 +4,6 @@ static int inside_git_dir = -1; static int inside_work_tree = -1; -static int sanitary_path_copy(char *dst, const char *src) -{ - char *dst0; - - if (has_dos_drive_prefix(src)) { - *dst++ = *src++; - *dst++ = *src++; - } - dst0 = dst; - - if (is_dir_sep(*src)) { - *dst++ = '/'; - while (is_dir_sep(*src)) - src++; - } - - for (;;) { - char c = *src; - - /* - * A path component that begins with . could be - * special: - * (1) "." and ends -- ignore and terminate. - * (2) "./" -- ignore them, eat slash and continue. - * (3) ".." and ends -- strip one and terminate. - * (4) "../" -- strip one, eat slash and continue. - */ - if (c == '.') { - if (!src[1]) { - /* (1) */ - src++; - } else if (is_dir_sep(src[1])) { - /* (2) */ - src += 2; - while (is_dir_sep(*src)) - src++; - continue; - } else if (src[1] == '.') { - if (!src[2]) { - /* (3) */ - src += 2; - goto up_one; - } else if (is_dir_sep(src[2])) { - /* (4) */ - src += 3; - while (is_dir_sep(*src)) - src++; - goto up_one; - } - } - } - - /* copy up to the next '/', and eat all '/' */ - while ((c = *src++) != '\0' && !is_dir_sep(c)) - *dst++ = c; - if (is_dir_sep(c)) { - *dst++ = '/'; - while (is_dir_sep(c)) - c = *src++; - src--; - } else if (!c) - break; - continue; - - up_one: - /* - * dst0..dst is prefix portion, and dst[-1] is '/'; - * go up one level. - */ - dst -= 2; /* go past trailing '/' if any */ - if (dst < dst0) - return -1; - while (1) { - if (dst <= dst0) - break; - c = *dst--; - if (c == '/') { /* MinGW: cannot be '\\' anymore */ - dst += 2; - break; - } - } - } - *dst = '\0'; - return 0; -} - const char *prefix_path(const char *prefix, int len, const char *path) { const char *orig = path; @@ -101,7 +15,7 @@ const char *prefix_path(const char *prefix, int len, const char *path) memcpy(sanitized, prefix, len); strcpy(sanitized + len, path); } - if (sanitary_path_copy(sanitized, sanitized)) + if (normalize_path_copy(sanitized, sanitized)) goto error_out; if (is_absolute_path(orig)) { const char *work_tree = get_git_work_tree(); -- cgit v1.2.1 From 5a688fe4706462dfa0a7932ef0c82c335c47e9ab Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 25 Mar 2009 16:19:36 -0700 Subject: "core.sharedrepository = 0mode" should set, not loosen This fixes the behaviour of octal notation to how it is defined in the documentation, while keeping the traditional "loosen only" semantics intact for "group" and "everybody". Three main points of this patch are: - For an explicit octal notation, the internal shared_repository variable is set to a negative value, so that we can tell "group" (which is to "OR" in 0660) and 0660 (which is to "SET" to 0660); - git-init did not set shared_repository variable early enough to affect the initial creation of many files, notably copied templates and the configuration. We set it very early when a command-line option specifies a custom value. - Many codepaths create files inside $GIT_DIR by various ways that all involve mkstemp(), and then call move_temp_to_file() to rename it to its final destination. We can add adjust_shared_perm() call here; for the traditional "loosen-only", this would be a no-op for many codepaths because the mode is already loose enough, but with the new behaviour it makes a difference. Signed-off-by: Junio C Hamano --- setup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 6c2deda184..ebd60de9ce 100644 --- a/setup.c +++ b/setup.c @@ -434,7 +434,7 @@ int git_config_perm(const char *var, const char *value) /* * Treat values 0, 1 and 2 as compatibility cases, otherwise it is - * a chmod value. + * a chmod value to restrict to. */ switch (i) { case PERM_UMASK: /* 0 */ @@ -456,7 +456,7 @@ int git_config_perm(const char *var, const char *value) * Mask filemode value. Others can not get write permission. * x flags for directories are handled separately. */ - return i & 0666; + return -(i & 0666); } int check_repository_format_version(const char *var, const char *value, void *cb) -- cgit v1.2.1 From d824cbba02a4061400a0e382f9bd241fbbff34f0 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Sat, 27 Jun 2009 17:58:46 +0200 Subject: Convert existing die(..., strerror(errno)) to die_errno() Change calls to die(..., strerror(errno)) to use the new die_errno(). In the process, also make slight style adjustments: at least state _something_ about the function that failed (instead of just printing the pathname), and put paths in single quotes. Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- setup.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index ebd60de9ce..4d27f28c8f 100644 --- a/setup.c +++ b/setup.c @@ -81,7 +81,7 @@ void verify_filename(const char *prefix, const char *arg) if (errno == ENOENT) die("ambiguous argument '%s': unknown revision or path not in the working tree.\n" "Use '--' to separate paths from revisions", arg); - die("'%s': %s", arg, strerror(errno)); + die_errno("failed to stat '%s'", arg); } /* @@ -103,7 +103,7 @@ void verify_non_filename(const char *prefix, const char *arg) die("ambiguous argument '%s': both revision and filename\n" "Use '--' to separate filenames from revisions", arg); if (errno != ENOENT && errno != ENOTDIR) - die("'%s': %s", arg, strerror(errno)); + die_errno("failed to stat '%s'", arg); } const char **get_pathspec(const char *prefix, const char **pathspec) @@ -257,7 +257,7 @@ const char *read_gitfile_gently(const char *path) return NULL; fd = open(path, O_RDONLY); if (fd < 0) - die("Error opening %s: %s", path, strerror(errno)); + die_errno("Error opening '%s'", path); buf = xmalloc(st.st_size + 1); len = read_in_full(fd, buf, st.st_size); close(fd); @@ -389,7 +389,7 @@ const char *setup_git_directory_gently(int *nongit_ok) die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT); } if (chdir("..")) - die("Cannot change to %s/..: %s", cwd, strerror(errno)); + die_errno("Cannot change to '%s/..'", cwd); } inside_git_dir = 0; -- cgit v1.2.1 From 0721c314a5c8fddc877140ab5a333c42c62f780d Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Sat, 27 Jun 2009 17:58:47 +0200 Subject: Use die_errno() instead of die() when checking syscalls Lots of die() calls did not actually report the kind of error, which can leave the user confused as to the real problem. Use die_errno() where we check a system/library call that sets errno on failure, or one of the following that wrap such calls: Function Passes on error from -------- -------------------- odb_pack_keep open read_ancestry fopen read_in_full xread strbuf_read xread strbuf_read_file open or strbuf_read_file strbuf_readlink readlink write_in_full xwrite Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- setup.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'setup.c') diff --git a/setup.c b/setup.c index 4d27f28c8f..e3781b656d 100644 --- a/setup.c +++ b/setup.c @@ -327,7 +327,7 @@ const char *setup_git_directory_gently(int *nongit_ok) return NULL; set_git_dir(make_absolute_path(gitdirenv)); if (chdir(work_tree_env) < 0) - die ("Could not chdir to %s", work_tree_env); + die_errno ("Could not chdir to '%s'", work_tree_env); strcat(buffer, "/"); return retval; } @@ -339,7 +339,7 @@ const char *setup_git_directory_gently(int *nongit_ok) } if (!getcwd(cwd, sizeof(cwd)-1)) - die("Unable to read current working directory"); + 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)) @@ -382,7 +382,7 @@ const char *setup_git_directory_gently(int *nongit_ok) if (offset <= ceil_offset) { if (nongit_ok) { if (chdir(cwd)) - die("Cannot come back to cwd"); + die_errno("Cannot come back to cwd"); *nongit_ok = 1; return NULL; } @@ -493,10 +493,10 @@ const char *setup_git_directory(void) static char buffer[PATH_MAX + 1]; char *rel; if (retval && chdir(retval)) - die ("Could not jump back into original cwd"); + die_errno ("Could not jump back into original cwd"); rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree()); if (rel && *rel && chdir(get_git_work_tree())) - die ("Could not jump to working directory"); + die_errno ("Could not jump to working directory"); return rel && *rel ? strcat(rel, "/") : NULL; } -- cgit v1.2.1