Merge branch 'jk/alias-in-bare'

An aliased command spawned from a bare repository that does not say
it is bare with "core.bare = yes" is treated as non-bare by mistake.

* jk/alias-in-bare:
  setup: suppress implicit "." work-tree for bare repos
  environment: add GIT_PREFIX to local_repo_env
  cache.h: drop LOCAL_REPO_ENV_SIZE
This commit is contained in:
Junio C Hamano 2013-03-25 14:00:44 -07:00
commit fb3b7b1f95
5 changed files with 53 additions and 12 deletions

25
cache.h
View file

@ -341,9 +341,11 @@ static inline enum object_type object_type(unsigned int mode)
OBJ_BLOB;
}
/* Double-check local_repo_env below if you add to this list. */
#define GIT_DIR_ENVIRONMENT "GIT_DIR"
#define GIT_NAMESPACE_ENVIRONMENT "GIT_NAMESPACE"
#define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
#define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
@ -365,13 +367,24 @@ static inline enum object_type object_type(unsigned int mode)
#define GIT_LITERAL_PATHSPECS_ENVIRONMENT "GIT_LITERAL_PATHSPECS"
/*
* Repository-local GIT_* environment variables
* The array is NULL-terminated to simplify its usage in contexts such
* environment creation or simple walk of the list.
* The number of non-NULL entries is available as a macro.
* 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 LOCAL_REPO_ENV_SIZE 9
extern const char *const local_repo_env[LOCAL_REPO_ENV_SIZE + 1];
#define GIT_IMPLICIT_WORK_TREE_ENVIRONMENT "GIT_IMPLICIT_WORK_TREE"
/*
* Repository-local GIT_* environment variables; these will be cleared
* when git spawns a sub-process that runs inside another repository.
* The array is NULL-terminated, which makes it easy to pass in the "env"
* parameter of a run-command invocation, or to do a simple walk.
*/
extern const char * const local_repo_env[];
extern int is_bare_repository_cfg;
extern int is_bare_repository(void);

View file

@ -83,20 +83,20 @@ static const char *git_dir;
static char *git_object_dir, *git_index_file, *git_graft_file;
/*
* Repository-local GIT_* environment variables
* Remember to update local_repo_env_size in cache.h when
* the size of the list changes
* Repository-local GIT_* environment variables; see cache.h for details.
*/
const char * const local_repo_env[LOCAL_REPO_ENV_SIZE + 1] = {
const char * const local_repo_env[] = {
ALTERNATE_DB_ENVIRONMENT,
CONFIG_ENVIRONMENT,
CONFIG_DATA_ENVIRONMENT,
DB_ENVIRONMENT,
GIT_DIR_ENVIRONMENT,
GIT_WORK_TREE_ENVIRONMENT,
GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
GRAFT_ENVIRONMENT,
INDEX_ENVIRONMENT,
NO_REPLACE_OBJECTS_ENVIRONMENT,
GIT_PREFIX_ENVIRONMENT,
NULL
};

1
git.c
View file

@ -125,6 +125,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
static char git_dir[PATH_MAX+1];
is_bare_repository_cfg = 1;
setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
if (envchanged)
*envchanged = 1;
} else if (!strcmp(cmd, "-c")) {

12
setup.c
View file

@ -523,6 +523,12 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
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 */
set_git_work_tree(".");
@ -601,6 +607,8 @@ static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongi
if (check_repository_format_gently(".", nongit_ok))
return NULL;
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
/* --work-tree is set without --git-dir; use discovered one */
if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
const char *gitdir;
@ -794,9 +802,9 @@ const char *setup_git_directory_gently(int *nongit_ok)
prefix = setup_git_directory_gently_1(nongit_ok);
if (prefix)
setenv("GIT_PREFIX", prefix, 1);
setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
else
setenv("GIT_PREFIX", "", 1);
setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
if (startup_info) {
startup_info->have_repository = !nongit_ok || !*nongit_ok;

View file

@ -517,6 +517,25 @@ test_expect_success '#16c: bare .git has no worktree' '
"$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)' '
# Just like #16.
setup_repo 17a unset "" true &&