Skip to content

Commit 482c84a

Browse files
committed
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 a308a02 commit 482c84a

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
@@ -760,6 +760,12 @@ relatively high IO latencies. When enabled, Git will do the
760760
index comparison to the filesystem data in parallel, allowing
761761
overlapping IO's. Defaults to true.
762762

763+
core.unsetenvvars::
764+
EXPERIMENTAL, Windows-only: comma-separated list of environment
765+
variables' names that need to be unset before spawning any other
766+
process. Defaults to `PERL5LIB` to account for the fact that Git
767+
for Windows insists on using its own Perl interpreter.
768+
763769
core.createObject::
764770
You can set this to 'link', in which case a hardlink followed by
765771
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)
215216
{
@@ -221,6 +222,12 @@ int mingw_core_config(const char *var, const char *value)
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

@@ -1090,6 +1097,27 @@ static wchar_t *make_environment_block(char **deltaenv)
10901097
return wenvblk;
10911098
}
10921099

1100+
static void do_unset_environment_variables(void)
1101+
{
1102+
static int done;
1103+
char *p = unset_environment_variables;
1104+
1105+
if (done || !p)
1106+
return;
1107+
done = 1;
1108+
1109+
for (;;) {
1110+
char *comma = strchr(p, ',');
1111+
1112+
if (comma)
1113+
*comma = '\0';
1114+
unsetenv(p);
1115+
if (!comma)
1116+
break;
1117+
p = comma + 1;
1118+
}
1119+
}
1120+
10931121
struct pinfo_t {
10941122
struct pinfo_t *next;
10951123
pid_t pid;
@@ -1108,9 +1136,12 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
11081136
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
11091137
unsigned flags = CREATE_UNICODE_ENVIRONMENT;
11101138
BOOL ret;
1139+
HANDLE cons;
1140+
1141+
do_unset_environment_variables();
11111142

11121143
/* Determine whether or not we are associated to a console */
1113-
HANDLE cons = CreateFile("CONOUT$", GENERIC_WRITE,
1144+
cons = CreateFile("CONOUT$", GENERIC_WRITE,
11141145
FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
11151146
FILE_ATTRIBUTE_NORMAL, NULL);
11161147
if (cons == INVALID_HANDLE_VALUE) {
@@ -2251,6 +2282,8 @@ void mingw_startup(void)
22512282
/* fix Windows specific environment settings */
22522283
setup_windows_environment();
22532284

2285+
unset_environment_variables = xstrdup("PERL5LIB");
2286+
22542287
/* initialize critical section for waitpid pinfo_t list */
22552288
InitializeCriticalSection(&pinfo_cs);
22562289

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)