Commit 2cd83d10 authored by Jeff King's avatar Jeff King Committed by Junio C Hamano

setup: suppress implicit "." work-tree for bare repos

If an explicit GIT_DIR is given without a working tree, we
implicitly assume that the current working directory should
be used as the working tree. E.g.,:

  GIT_DIR=/some/repo.git git status

would compare against the cwd.

Unfortunately, we fool this rule for sub-invocations of git
by setting GIT_DIR internally ourselves. For example:

  git init foo
  cd foo/.git
  git status ;# fails, as we expect
  git config alias.st status
  git status ;# does not fail, but should

What happens is that we run setup_git_directory when doing
alias lookup (since we need to see the config), set GIT_DIR
as a result, and then leave GIT_WORK_TREE blank (because we
do not have one). Then when we actually run the status
command, we do setup_git_directory again, which sees our
explicit GIT_DIR and uses the cwd as an implicit worktree.

It's tempting to argue that we should be suppressing that
second invocation of setup_git_directory, as it could use
the values we already found in memory. However, the problem
still exists for sub-processes (e.g., if "git status" were
an external command).

You can see another example with the "--bare" option, which
sets GIT_DIR explicitly. For example:

  git init foo
  cd foo/.git
  git status ;# fails
  git --bare status ;# does NOT fail

We need some way of telling sub-processes "even though
GIT_DIR is set, do not use cwd as an implicit working tree".
We could do it by putting a special token into
GIT_WORK_TREE, but the obvious choice (an empty string) has
some portability problems.

Instead, we add a new boolean variable, GIT_IMPLICIT_WORK_TREE,
which suppresses the use of cwd as a working tree when
GIT_DIR is set. We trigger the new variable when we know we
are in a bare setting.

The variable is left intentionally undocumented, as this is
an internal detail (for now, anyway). If somebody comes up
with a good alternate use for it, and once we are confident
we have shaken any bugs out of it, we can consider promoting
it further.
Signed-off-by: default avatarJeff King <peff@peff.net>
Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
parent a6f7f9a3
...@@ -365,6 +365,18 @@ static inline enum object_type object_type(unsigned int mode) ...@@ -365,6 +365,18 @@ static inline enum object_type object_type(unsigned int mode)
#define GIT_NOTES_REWRITE_REF_ENVIRONMENT "GIT_NOTES_REWRITE_REF" #define GIT_NOTES_REWRITE_REF_ENVIRONMENT "GIT_NOTES_REWRITE_REF"
#define GIT_NOTES_REWRITE_MODE_ENVIRONMENT "GIT_NOTES_REWRITE_MODE" #define GIT_NOTES_REWRITE_MODE_ENVIRONMENT "GIT_NOTES_REWRITE_MODE"
/*
* This environment variable is expected to contain a boolean indicating
* whether we should or should not treat:
*
* GIT_DIR=foo.git git ...
*
* as if GIT_WORK_TREE=. was given. It's not expected that users will make use
* of this, but we use it internally to communicate to sub-processes that we
* are in a bare repo. If not set, defaults to true.
*/
#define GIT_IMPLICIT_WORK_TREE_ENVIRONMENT "GIT_IMPLICIT_WORK_TREE"
/* /*
* Repository-local GIT_* environment variables; these will be cleared * Repository-local GIT_* environment variables; these will be cleared
* when git spawns a sub-process that runs inside another repository. * when git spawns a sub-process that runs inside another repository.
......
...@@ -85,6 +85,7 @@ const char * const local_repo_env[] = { ...@@ -85,6 +85,7 @@ const char * const local_repo_env[] = {
DB_ENVIRONMENT, DB_ENVIRONMENT,
GIT_DIR_ENVIRONMENT, GIT_DIR_ENVIRONMENT,
GIT_WORK_TREE_ENVIRONMENT, GIT_WORK_TREE_ENVIRONMENT,
GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
GRAFT_ENVIRONMENT, GRAFT_ENVIRONMENT,
INDEX_ENVIRONMENT, INDEX_ENVIRONMENT,
NO_REPLACE_OBJECTS_ENVIRONMENT, NO_REPLACE_OBJECTS_ENVIRONMENT,
......
...@@ -125,6 +125,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged) ...@@ -125,6 +125,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
static char git_dir[PATH_MAX+1]; static char git_dir[PATH_MAX+1];
is_bare_repository_cfg = 1; is_bare_repository_cfg = 1;
setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0); setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
if (envchanged) if (envchanged)
*envchanged = 1; *envchanged = 1;
} else if (!strcmp(cmd, "-c")) { } else if (!strcmp(cmd, "-c")) {
......
...@@ -497,6 +497,12 @@ static const char *setup_explicit_git_dir(const char *gitdirenv, ...@@ -497,6 +497,12 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
set_git_work_tree(core_worktree); set_git_work_tree(core_worktree);
} }
} }
else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
/* #16d */
set_git_dir(gitdirenv);
free(gitfile);
return NULL;
}
else /* #2, #10 */ else /* #2, #10 */
set_git_work_tree("."); set_git_work_tree(".");
...@@ -575,6 +581,8 @@ static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongi ...@@ -575,6 +581,8 @@ static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongi
if (check_repository_format_gently(".", nongit_ok)) if (check_repository_format_gently(".", nongit_ok))
return NULL; return NULL;
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
/* --work-tree is set without --git-dir; use discovered one */ /* --work-tree is set without --git-dir; use discovered one */
if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
const char *gitdir; const char *gitdir;
......
...@@ -517,6 +517,25 @@ test_expect_success '#16c: bare .git has no worktree' ' ...@@ -517,6 +517,25 @@ test_expect_success '#16c: bare .git has no worktree' '
"$here/16c/.git" "(null)" "$here/16c/sub" "(null)" "$here/16c/.git" "(null)" "$here/16c/sub" "(null)"
' '
test_expect_success '#16d: bareness preserved across alias' '
setup_repo 16d unset "" unset &&
(
cd 16d/.git &&
test_must_fail git status &&
git config alias.st status &&
test_must_fail git st
)
'
test_expect_success '#16e: bareness preserved by --bare' '
setup_repo 16e unset "" unset &&
(
cd 16e/.git &&
test_must_fail git status &&
test_must_fail git --bare status
)
'
test_expect_success '#17: GIT_WORK_TREE without explicit GIT_DIR is accepted (bare case)' ' test_expect_success '#17: GIT_WORK_TREE without explicit GIT_DIR is accepted (bare case)' '
# Just like #16. # Just like #16.
setup_repo 17a unset "" true && setup_repo 17a unset "" true &&
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment