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 <Johannes.Schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Johannes Schindelin 2007-08-05 14:12:53 +01:00 committed by Junio C Hamano
parent 524e5ffcf4
commit 7efeb8f098
2 changed files with 14 additions and 44 deletions

52
setup.c
View File

@ -189,53 +189,21 @@ int is_inside_work_tree(void)
} }


/* /*
* If no worktree was given, and we are outside of a default work tree, * set_work_tree() is only ever called if you set GIT_DIR explicitely.
* now is the time to set it. * 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
* In other words, if the user calls git with something like * GIT_WORK_TREE.
*
* 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) static const char *set_work_tree(const char *dir)
{ {
char dir_buffer[PATH_MAX], *rel = NULL; char buffer[PATH_MAX + 1];
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 */ if (!getcwd(buffer, sizeof(buffer)))
len = strlen(dir); die ("Could not get the current working directory");
if (len > suffix_len && git_work_tree_cfg = xstrdup(buffer);
!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;
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);
inside_work_tree = 1; inside_work_tree = 1;


return rel; return NULL;
} }


/* /*

View File

@ -28,6 +28,8 @@ test_rev_parse() {
[ $# -eq 0 ] && return [ $# -eq 0 ] && return
} }


# label is-bare is-inside-git is-inside-work prefix

test_rev_parse toplevel false false true '' test_rev_parse toplevel false false true ''


cd .git || exit 1 cd .git || exit 1
@ -53,13 +55,13 @@ export GIT_DIR=../.git
export GIT_CONFIG="$(pwd)"/../.git/config export GIT_CONFIG="$(pwd)"/../.git/config


git config core.bare false git config core.bare false
test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true work/ test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true ''


git config core.bare true git config core.bare true
test_rev_parse 'GIT_DIR=../.git, core.bare = true' true false false '' test_rev_parse 'GIT_DIR=../.git, core.bare = true' true false false ''


git config --unset core.bare git config --unset core.bare
test_rev_parse 'GIT_DIR=../.git, core.bare undefined' false false true work/ test_rev_parse 'GIT_DIR=../.git, core.bare undefined' false false true ''


mv ../.git ../repo.git || exit 1 mv ../.git ../repo.git || exit 1
export GIT_DIR=../repo.git export GIT_DIR=../repo.git