Skip to content

Commit eeecf39

Browse files
committed
Merge branch 'jk/alias-in-bare' into maint
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
2 parents e6658b9 + 2cd83d1 commit eeecf39

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

cache.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,11 @@ static inline enum object_type object_type(unsigned int mode)
330330
OBJ_BLOB;
331331
}
332332

333+
/* Double-check local_repo_env below if you add to this list. */
333334
#define GIT_DIR_ENVIRONMENT "GIT_DIR"
334335
#define GIT_NAMESPACE_ENVIRONMENT "GIT_NAMESPACE"
335336
#define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
337+
#define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
336338
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
337339
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
338340
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
@@ -354,13 +356,24 @@ static inline enum object_type object_type(unsigned int mode)
354356
#define GIT_LITERAL_PATHSPECS_ENVIRONMENT "GIT_LITERAL_PATHSPECS"
355357

356358
/*
357-
* Repository-local GIT_* environment variables
358-
* The array is NULL-terminated to simplify its usage in contexts such
359-
* environment creation or simple walk of the list.
360-
* The number of non-NULL entries is available as a macro.
359+
* This environment variable is expected to contain a boolean indicating
360+
* whether we should or should not treat:
361+
*
362+
* GIT_DIR=foo.git git ...
363+
*
364+
* as if GIT_WORK_TREE=. was given. It's not expected that users will make use
365+
* of this, but we use it internally to communicate to sub-processes that we
366+
* are in a bare repo. If not set, defaults to true.
367+
*/
368+
#define GIT_IMPLICIT_WORK_TREE_ENVIRONMENT "GIT_IMPLICIT_WORK_TREE"
369+
370+
/*
371+
* Repository-local GIT_* environment variables; these will be cleared
372+
* when git spawns a sub-process that runs inside another repository.
373+
* The array is NULL-terminated, which makes it easy to pass in the "env"
374+
* parameter of a run-command invocation, or to do a simple walk.
361375
*/
362-
#define LOCAL_REPO_ENV_SIZE 9
363-
extern const char *const local_repo_env[LOCAL_REPO_ENV_SIZE + 1];
376+
extern const char * const local_repo_env[];
364377

365378
extern int is_bare_repository_cfg;
366379
extern int is_bare_repository(void);

environment.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,20 @@ static const char *git_dir;
8383
static char *git_object_dir, *git_index_file, *git_graft_file;
8484

8585
/*
86-
* Repository-local GIT_* environment variables
87-
* Remember to update local_repo_env_size in cache.h when
88-
* the size of the list changes
86+
* Repository-local GIT_* environment variables; see cache.h for details.
8987
*/
90-
const char * const local_repo_env[LOCAL_REPO_ENV_SIZE + 1] = {
88+
const char * const local_repo_env[] = {
9189
ALTERNATE_DB_ENVIRONMENT,
9290
CONFIG_ENVIRONMENT,
9391
CONFIG_DATA_ENVIRONMENT,
9492
DB_ENVIRONMENT,
9593
GIT_DIR_ENVIRONMENT,
9694
GIT_WORK_TREE_ENVIRONMENT,
95+
GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
9796
GRAFT_ENVIRONMENT,
9897
INDEX_ENVIRONMENT,
9998
NO_REPLACE_OBJECTS_ENVIRONMENT,
99+
GIT_PREFIX_ENVIRONMENT,
100100
NULL
101101
};
102102

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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
525525
set_git_work_tree(core_worktree);
526526
}
527527
}
528+
else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
529+
/* #16d */
530+
set_git_dir(gitdirenv);
531+
free(gitfile);
532+
return NULL;
533+
}
528534
else /* #2, #10 */
529535
set_git_work_tree(".");
530536

@@ -603,6 +609,8 @@ static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongi
603609
if (check_repository_format_gently(".", nongit_ok))
604610
return NULL;
605611

612+
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
613+
606614
/* --work-tree is set without --git-dir; use discovered one */
607615
if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
608616
const char *gitdir;
@@ -796,9 +804,9 @@ const char *setup_git_directory_gently(int *nongit_ok)
796804

797805
prefix = setup_git_directory_gently_1(nongit_ok);
798806
if (prefix)
799-
setenv("GIT_PREFIX", prefix, 1);
807+
setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
800808
else
801-
setenv("GIT_PREFIX", "", 1);
809+
setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
802810

803811
if (startup_info) {
804812
startup_info->have_repository = !nongit_ok || !*nongit_ok;

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)