Skip to content

Commit fb3b7b1

Browse files
committed
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
2 parents 55f6fbe + 2cd83d1 commit fb3b7b1

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
@@ -341,9 +341,11 @@ static inline enum object_type object_type(unsigned int mode)
341341
OBJ_BLOB;
342342
}
343343

344+
/* Double-check local_repo_env below if you add to this list. */
344345
#define GIT_DIR_ENVIRONMENT "GIT_DIR"
345346
#define GIT_NAMESPACE_ENVIRONMENT "GIT_NAMESPACE"
346347
#define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
348+
#define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
347349
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
348350
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
349351
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
@@ -365,13 +367,24 @@ static inline enum object_type object_type(unsigned int mode)
365367
#define GIT_LITERAL_PATHSPECS_ENVIRONMENT "GIT_LITERAL_PATHSPECS"
366368

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

376389
extern int is_bare_repository_cfg;
377390
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
@@ -523,6 +523,12 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
523523
set_git_work_tree(core_worktree);
524524
}
525525
}
526+
else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
527+
/* #16d */
528+
set_git_dir(gitdirenv);
529+
free(gitfile);
530+
return NULL;
531+
}
526532
else /* #2, #10 */
527533
set_git_work_tree(".");
528534

@@ -601,6 +607,8 @@ static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongi
601607
if (check_repository_format_gently(".", nongit_ok))
602608
return NULL;
603609

610+
setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
611+
604612
/* --work-tree is set without --git-dir; use discovered one */
605613
if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
606614
const char *gitdir;
@@ -794,9 +802,9 @@ const char *setup_git_directory_gently(int *nongit_ok)
794802

795803
prefix = setup_git_directory_gently_1(nongit_ok);
796804
if (prefix)
797-
setenv("GIT_PREFIX", prefix, 1);
805+
setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
798806
else
799-
setenv("GIT_PREFIX", "", 1);
807+
setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
800808

801809
if (startup_info) {
802810
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)