Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'addConfig()'. Now we can specify '-c' arguments. #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/CommandProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CommandProcessor

/** @var bool */
private $isWindows;
protected $configs = [];


/**
Expand All @@ -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
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/IRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $args
Expand Down
9 changes: 9 additions & 0 deletions src/Runners/CliRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/Runners/MemoryRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $args
Expand Down
9 changes: 9 additions & 0 deletions src/Runners/OldGitRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
17 changes: 17 additions & 0 deletions tests/GitPhp/CommandProcessor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
});
9 changes: 9 additions & 0 deletions tests/libs/AssertRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down