diff --git a/src/CommandProcessor.php b/src/CommandProcessor.php index 83372b3..a7077bc 100644 --- a/src/CommandProcessor.php +++ b/src/CommandProcessor.php @@ -11,6 +11,7 @@ class CommandProcessor /** @var bool */ private $isWindows; + protected $configs = []; /** @@ -32,6 +33,15 @@ public function __construct($mode = self::MODE_DETECT) } } + /** + * Add a configuration parameter to the command. + * @param string $name + * @param mixed $value + */ + public function addConfig($name, $value) + { + $this->configs[] = [$name, $value]; + } /** * @param string $app @@ -43,6 +53,15 @@ public function process($app, array $args, array $env = NULL) { $cmd = []; + foreach ($this->configs as $config) { + list($name, $value) = $config; + if ($value == NULL) { + $cmd[] = "-c $name"; + } else { + $cmd[] = "-c $name=" . $this->escapeArgument($value); + } + } + foreach ($args as $arg) { if (is_array($arg)) { foreach ($arg as $key => $value) { diff --git a/src/Git.php b/src/Git.php index 575c7c9..73e9894 100644 --- a/src/Git.php +++ b/src/Git.php @@ -14,6 +14,15 @@ public function __construct(IRunner $runner = NULL) $this->runner = $runner !== NULL ? $runner : new Runners\CliRunner; } + /** + * Add a configuration parameter to the command. + * @param string $name + * @param mixed $value + */ + public function addConfig($name, $value) + { + $this->runner->addConfig($name, $value); + } /** * @param string $directory diff --git a/src/GitRepository.php b/src/GitRepository.php index 2ef6f83..324f525 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -32,6 +32,15 @@ public function __construct($repository, IRunner $runner = NULL) $this->runner = $runner !== NULL ? $runner : new Runners\CliRunner; } + /** + * Add a configuration parameter to the command. + * @param string $name + * @param mixed $value + */ + public function addConfig($name, $value) + { + $this->runner->addConfig($name, $value); + } /** * @return string diff --git a/src/IRunner.php b/src/IRunner.php index 453ff68..f63cac9 100644 --- a/src/IRunner.php +++ b/src/IRunner.php @@ -5,6 +5,13 @@ interface IRunner { + /** + * Add a configuration parameter to the command. + * @param string $name + * @param mixed $value + */ + function addConfig($name, $value); + /** * @param string $cwd * @param array $args diff --git a/src/Runners/CliRunner.php b/src/Runners/CliRunner.php index 0acacd7..64595f5 100644 --- a/src/Runners/CliRunner.php +++ b/src/Runners/CliRunner.php @@ -26,6 +26,15 @@ public function __construct($gitBinary = 'git') $this->commandProcessor = new CommandProcessor; } + /** + * Add a configuration parameter to the command. + * @param string $name + * @param mixed $value + */ + public function addConfig($name, $value) + { + $this->commandProcessor->addConfig($name, $value); + } /** * @return RunnerResult diff --git a/src/Runners/MemoryRunner.php b/src/Runners/MemoryRunner.php index 2a06d43..a3161ab 100644 --- a/src/Runners/MemoryRunner.php +++ b/src/Runners/MemoryRunner.php @@ -29,6 +29,15 @@ public function __construct($cwd) $this->commandProcessor = new CommandProcessor; } + /** + * Add a configuration parameter to the command. + * @param string $name + * @param mixed $value + */ + public function addConfig($name, $value) + { + $this->commandProcessor->addConfig($name, $value); + } /** * @param array $args diff --git a/src/Runners/OldGitRunner.php b/src/Runners/OldGitRunner.php index cb07136..4142a0d 100644 --- a/src/Runners/OldGitRunner.php +++ b/src/Runners/OldGitRunner.php @@ -16,6 +16,15 @@ public function __construct(IRunner $runner = NULL) $this->runner = $runner !== NULL ? $runner : new CliRunner; } + /** + * Add a configuration parameter to the command. + * @param string $name + * @param mixed $value + */ + public function addConfig($name, $value) + { + $this->runner->addConfig($name, $value); + } public function run($cwd, array $args, array $env = NULL) { diff --git a/tests/GitPhp/CommandProcessor.phpt b/tests/GitPhp/CommandProcessor.phpt index 936c0a5..f10d797 100644 --- a/tests/GitPhp/CommandProcessor.phpt +++ b/tests/GitPhp/CommandProcessor.phpt @@ -101,3 +101,20 @@ test(function () { }, InvalidArgumentException::class, "Invalid mode 'INVALID'."); }); + +test(function () { + $processor = new CommandProcessor(CommandProcessor::MODE_NON_WINDOWS); + $processor->addConfig('core.sshCommand', "ssh -i '/path/to/private key file' -o StrictHostKeyChecking=no -o IdentitiesOnly=yes"); + $processor->addConfig('test', NULL); + + Assert::same(implode(' ', [ + 'git', + "-c core.sshCommand='ssh -i '\''/path/to/private key file'\'' -o StrictHostKeyChecking=no -o IdentitiesOnly=yes'", + '-c test', + 'clone', + "'https://github.com/test/test.git'", + 'test', + ]), $processor->process('git', ['clone', 'https://github.com/test/test.git', 'test'])); + + print($processor->process('git', ['clone', 'https://github.com/test/test.git', 'test'])); +}); diff --git a/tests/libs/AssertRunner.php b/tests/libs/AssertRunner.php index db7d1e2..65ba3fd 100644 --- a/tests/libs/AssertRunner.php +++ b/tests/libs/AssertRunner.php @@ -29,6 +29,15 @@ public function __construct($cwd) $this->commandProcessor = new CommandProcessor; } + /** + * Add a configuration parameter to the command. + * @param string $name + * @param mixed $value + */ + public function addConfig($name, $value) + { + $this->commandProcessor->addConfig($name, $value); + } /** * @param mixed[] $expectedArgs