Skip to content

Commit 2cd83d1

Browse files
peffgitster
authored andcommitted
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: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a6f7f9a commit 2cd83d1

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

cache.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,18 @@ static inline enum object_type object_type(unsigned int mode)
365365
#define GIT_NOTES_REWRITE_REF_ENVIRONMENT "GIT_NOTES_REWRITE_REF"
366366
#define GIT_NOTES_REWRITE_MODE_ENVIRONMENT "GIT_NOTES_REWRITE_MODE"
367367

368+
/*
369+
* This environment variable is expected to contain a boolean indicating
370+
* whether we should or should not treat:
371+
*
372+
* GIT_DIR=foo.git git ...
373+
*
374+
* as if GIT_WORK_TREE=. was given. It's not expected that users will make use
375+
* of this, but we use it internally to communicate to sub-processes that we
376+
* are in a bare repo. If not set, defaults to true.
377+
*/
378+
#define GIT_IMPLICIT_WORK_TREE_ENVIRONMENT "GIT_IMPLICIT_WORK_TREE"
379+
368380
/*
369381
* Repository-local GIT_* environment variables; these will be cleared
370382
* when git spawns a sub-process that runs inside another repository.

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const char * const local_repo_env[] = {
8585
DB_ENVIRONMENT,
8686
GIT_DIR_ENVIRONMENT,
8787
GIT_WORK_TREE_ENVIRONMENT,
88+
GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
8889
GRAFT_ENVIRONMENT,
8990
INDEX_ENVIRONMENT,
9091
NO_REPLACE_OBJECTS_ENVIRONMENT,

git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
125125
static char git_dir[PATH_MAX+1];
126126
is_bare_repository_cfg = 1;
127127
setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);
128+
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
128129
if (envchanged)
129130
*envchanged = 1;
130131
} else if (!strcmp(cmd, "-c")) {

setup.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
497497
set_git_work_tree(core_worktree);
498498
}
499499
}
500+
else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
501+
/* #16d */
502+
set_git_dir(gitdirenv);
503+
free(gitfile);
504+
return NULL;
505+
}
500506
else /* #2, #10 */
501507
set_git_work_tree(".");
502508

@@ -575,6 +581,8 @@ static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongi
575581
if (check_repository_format_gently(".", nongit_ok))
576582
return NULL;
577583

584+
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
585+
578586
/* --work-tree is set without --git-dir; use discovered one */
579587
if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
580588
const char *gitdir;

t/t1510-repo-setup.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,25 @@ test_expect_success '#16c: bare .git has no worktree' '
517517
"$here/16c/.git" "(null)" "$here/16c/sub" "(null)"
518518
'
519519

520+
test_expect_success '#16d: bareness preserved across alias' '
521+
setup_repo 16d unset "" unset &&
522+
(
523+
cd 16d/.git &&
524+
test_must_fail git status &&
525+
git config alias.st status &&
526+
test_must_fail git st
527+
)
528+
'
529+
530+
test_expect_success '#16e: bareness preserved by --bare' '
531+
setup_repo 16e unset "" unset &&
532+
(
533+
cd 16e/.git &&
534+
test_must_fail git status &&
535+
test_must_fail git --bare status
536+
)
537+
'
538+
520539
test_expect_success '#17: GIT_WORK_TREE without explicit GIT_DIR is accepted (bare case)' '
521540
# Just like #16.
522541
setup_repo 17a unset "" true &&

0 commit comments

Comments
 (0)