Skip to content

Commit e3b26db

Browse files
Configurable inheritance of environment variables (#173)
1 parent 5448d79 commit e3b26db

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/Gitonomy/Git/Repository.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class Repository
8686
*/
8787
protected $environmentVariables;
8888

89+
/**
90+
* @var bool
91+
*/
92+
protected $inheritEnvironmentVariables;
93+
8994
/**
9095
* Timeout that should be set for every running process.
9196
*
@@ -113,14 +118,14 @@ class Repository
113118
*/
114119
public function __construct($dir, $options = [])
115120
{
116-
$is_windows = defined('PHP_WINDOWS_VERSION_BUILD');
117121
$options = array_merge([
118-
'working_dir' => null,
119-
'debug' => true,
120-
'logger' => null,
121-
'environment_variables' => $is_windows ? ['PATH' => getenv('path')] : [],
122-
'command' => 'git',
123-
'process_timeout' => 3600,
122+
'working_dir' => null,
123+
'debug' => true,
124+
'logger' => null,
125+
'command' => 'git',
126+
'environment_variables' => [],
127+
'inherit_environment_variables' => false,
128+
'process_timeout' => 3600,
124129
], $options);
125130

126131
if (null !== $options['logger'] && !$options['logger'] instanceof LoggerInterface) {
@@ -131,10 +136,16 @@ public function __construct($dir, $options = [])
131136
$this->initDir($dir, $options['working_dir']);
132137

133138
$this->objects = [];
139+
$this->command = $options['command'];
134140
$this->debug = (bool) $options['debug'];
135-
$this->environmentVariables = $options['environment_variables'];
136141
$this->processTimeout = $options['process_timeout'];
137-
$this->command = $options['command'];
142+
143+
if (defined('PHP_WINDOWS_VERSION_BUILD') && isset($_SERVER['PATH']) && !isset($options['environment_variables']['PATH'])) {
144+
$options['environment_variables']['PATH'] = $_SERVER['PATH'];
145+
}
146+
147+
$this->environmentVariables = $options['environment_variables'];
148+
$this->inheritEnvironmentVariables = $options['inherit_environment_variables'];
138149

139150
if (true === $this->debug && null !== $this->logger) {
140151
$this->logger->debug(sprintf('Repository created (git dir: "%s", working dir: "%s")', $this->gitDir, $this->workingDir ?: 'none'));
@@ -620,7 +631,13 @@ private function getProcess($command, $args = [])
620631
$base[] = $command;
621632

622633
$process = new Process(array_merge($base, $args));
623-
$process->setEnv($this->environmentVariables);
634+
635+
if ($this->inheritEnvironmentVariables) {
636+
$process->setEnv(array_replace($_SERVER, $this->environmentVariables));
637+
} else {
638+
$process->setEnv($this->environmentVariables);
639+
}
640+
624641
$process->setTimeout($this->processTimeout);
625642
$process->setIdleTimeout($this->processTimeout);
626643

0 commit comments

Comments
 (0)