Skip to content

Commit 1ba7e11

Browse files
committed
Add support for consistent environment handling, to allow overrides like GIT_SSH.
Signed-off-by: Robin H. Johnson <[email protected]>
1 parent 63dd618 commit 1ba7e11

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Git.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public static function is_repo($var) {
104104
class GitRepo {
105105

106106
protected $repo_path = null;
107+
protected $envopts = array();
107108

108109
/**
109110
* Create a new git repository
@@ -230,7 +231,26 @@ protected function run_command($command) {
230231
2 => array('pipe', 'w'),
231232
);
232233
$pipes = array();
233-
$resource = proc_open($command, $descriptorspec, $pipes, $this->repo_path);
234+
/* Depending on the value of variables_order, $_ENV may be empty.
235+
* In that case, we have to explicitly set the new variables with
236+
* putenv, and call proc_open with env=null to inherit the reset
237+
* of the system.
238+
*
239+
* This is kind of crappy because we cannot easily restore just those
240+
* variables afterwards.
241+
*
242+
* If $_ENV is not empty, then we can just copy it and be done with it.
243+
*/
244+
if(count($_ENV) === 0) {
245+
$env = NULL;
246+
foreach($this->envopts as $k => $v) {
247+
putenv(sprintf("%s=%s",$k,$v));
248+
}
249+
} else {
250+
$env = array_merge($_ENV, $this->envopts);
251+
}
252+
$cwd = $this->repo_path;
253+
$resource = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
234254

235255
$stdout = stream_get_contents($pipes[1]);
236256
$stderr = stream_get_contents($pipes[2]);
@@ -523,6 +543,16 @@ public function set_description($new) {
523543
public function get_description() {
524544
return file_get_contents($this->repo_path."/.git/description");
525545
}
546+
547+
/**
548+
* Sets custom environment options for calling Git
549+
*
550+
* @param string key
551+
* @param string value
552+
*/
553+
public function setenv($key, $value) {
554+
$this->envopts[$key] = $value;
555+
}
526556

527557
}
528558

0 commit comments

Comments
 (0)