Skip to content

Commit 7f8e722

Browse files
dschoGit for Windows Build Agent
authored andcommitted
mingw: unset PERL5LIB by default
Git for Windows ships with its own Perl interpreter, and insists on using it, so it will most likely wreak havoc if PERL5LIB is set before launching Git. Let's just unset that environment variables when spawning processes. To make this feature extensible (and overrideable), there is a new config setting `core.unsetenvvars` that allows specifying a comma-separated list of names to unset before spawning processes. Reported by Gabriel Fuhrmann. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent d0a98bc commit 7f8e722

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,12 @@ relatively high IO latencies. When enabled, Git will do the
865865
index comparison to the filesystem data in parallel, allowing
866866
overlapping IO's. Defaults to true.
867867

868+
core.unsetenvvars::
869+
EXPERIMENTAL, Windows-only: comma-separated list of environment
870+
variables' names that need to be unset before spawning any other
871+
process. Defaults to `PERL5LIB` to account for the fact that Git
872+
for Windows insists on using its own Perl interpreter.
873+
868874
core.createObject::
869875
You can set this to 'link', in which case a hardlink followed by
870876
a delete of the source are used to make sure that object creation

compat/mingw.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ enum hide_dotfiles_type {
210210
};
211211

212212
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
213+
static char *unset_environment_variables;
213214

214215
int mingw_core_config(const char *var, const char *value, void *cb)
215216
{
@@ -221,6 +222,12 @@ int mingw_core_config(const char *var, const char *value, void *cb)
221222
return 0;
222223
}
223224

225+
if (!strcmp(var, "core.unsetenvvars")) {
226+
free(unset_environment_variables);
227+
unset_environment_variables = xstrdup(value);
228+
return 0;
229+
}
230+
224231
return 0;
225232
}
226233

@@ -1054,6 +1061,27 @@ static wchar_t *make_environment_block(char **deltaenv)
10541061
return wenvblk;
10551062
}
10561063

1064+
static void do_unset_environment_variables(void)
1065+
{
1066+
static int done;
1067+
char *p = unset_environment_variables;
1068+
1069+
if (done || !p)
1070+
return;
1071+
done = 1;
1072+
1073+
for (;;) {
1074+
char *comma = strchr(p, ',');
1075+
1076+
if (comma)
1077+
*comma = '\0';
1078+
unsetenv(p);
1079+
if (!comma)
1080+
break;
1081+
p = comma + 1;
1082+
}
1083+
}
1084+
10571085
struct pinfo_t {
10581086
struct pinfo_t *next;
10591087
pid_t pid;
@@ -1072,9 +1100,12 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
10721100
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
10731101
unsigned flags = CREATE_UNICODE_ENVIRONMENT;
10741102
BOOL ret;
1103+
HANDLE cons;
1104+
1105+
do_unset_environment_variables();
10751106

10761107
/* Determine whether or not we are associated to a console */
1077-
HANDLE cons = CreateFile("CONOUT$", GENERIC_WRITE,
1108+
cons = CreateFile("CONOUT$", GENERIC_WRITE,
10781109
FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
10791110
FILE_ATTRIBUTE_NORMAL, NULL);
10801111
if (cons == INVALID_HANDLE_VALUE) {
@@ -2208,6 +2239,8 @@ void mingw_startup(void)
22082239
/* fix Windows specific environment settings */
22092240
setup_windows_environment();
22102241

2242+
unset_environment_variables = xstrdup("PERL5LIB");
2243+
22112244
/* initialize critical section for waitpid pinfo_t list */
22122245
InitializeCriticalSection(&pinfo_cs);
22132246

t/t0028-core-unsetenvvars.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
3+
test_description='test the Windows-only core.unsetenvvars setting'
4+
5+
. ./test-lib.sh
6+
7+
if ! test_have_prereq MINGW
8+
then
9+
skip_all='skipping Windows-specific tests'
10+
test_done
11+
fi
12+
13+
test_expect_success 'setup' '
14+
mkdir -p "$TRASH_DIRECTORY/.git/hooks" &&
15+
write_script "$TRASH_DIRECTORY/.git/hooks/pre-commit" <<-\EOF
16+
echo $HOBBES >&2
17+
EOF
18+
'
19+
20+
test_expect_success 'core.unsetenvvars works' '
21+
HOBBES=Calvin &&
22+
export HOBBES &&
23+
git commit --allow-empty -m with 2>err &&
24+
grep Calvin err &&
25+
git -c core.unsetenvvars=FINDUS,HOBBES,CALVIN \
26+
commit --allow-empty -m without 2>err &&
27+
! grep Calvin err
28+
'
29+
30+
test_done

0 commit comments

Comments
 (0)