Skip to content

Add config class to handle git config #103

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

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
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,20 @@ $repo->setRemoteUrl('remote-name', 'new-repository-url');
$repo->setRemoteUrl('upstream', 'https://github.com/czproject/git-php.git');
```

Config
------

``` php
// get config object of repo
$config = $repo->getGitConfig();

// set a option
$config->set('option.name', 'value', ['--options']);

// get a value for option (returns NULL if not set)
$config->set('option.name', ['--options']);
```

**Troubleshooting - How to provide username and password for commands**

1) use SSH instead of HTTPS - https://stackoverflow.com/a/8588786
Expand Down
73 changes: 73 additions & 0 deletions src/GitConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace CzProject\GitPhp;

class GitConfig
{
/** @var GitRepository */
protected $repository;

/** @var IRunner */
protected $runner;


public function __construct(GitRepository $repository, IRunner $runner = NULL)
{
$this->repository = $repository;
$this->runner = $runner !== NULL ? $runner : new Runners\CliRunner;
}


/**
* @param string $name
* @param string $value
* @param array|string $params
* @throws GitException
* @return static
*/
public function set($name, $value, $params = [])
{
$this->run('config', $params, (string) $name, (string) $value);
return $this;
}


/**
* @param string $name
* @param array|string $params
* @return string|null NULL if the option is not set
* @throws GitException
*/
public function get($name, $params = [])
{
try {
$result = $this->run('config', $params, (string)$name);
return rtrim($result->getOutputAsString());
} catch (GitException $e) {
if (false === $e->getRunnerResult()->hasOutput()
&& false === $e->getRunnerResult()->hasErrorOutput()
) {
return NULL;
}
throw $e;
}
}


/**
* Runs command and returns result.
* @param mixed ...$args
* @return RunnerResult
* @throws GitException
*/
private function run(...$args)
{
$result = $this->runner->run($this->repository->getRepositoryPath(), $args);

if (!$result->isOk()) {
throw new GitException("Command '{$result->getCommand()}' failed (exit-code {$result->getExitCode()}).", $result->getExitCode(), NULL, $result);
}

return $result;
}
}
14 changes: 12 additions & 2 deletions src/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class GitRepository
{
/** @var string */
/** @var string */
protected $repository;

/** @var IRunner */
Expand Down Expand Up @@ -42,7 +42,17 @@ public function getRepositoryPath()
}


/**
/**
* @return GitConfig
*/
public function getGitConfig()
{
return new GitConfig($this, $this->runner);
}



/**
* Creates a tag.
* `git tag <name>`
* @param string $name
Expand Down
27 changes: 27 additions & 0 deletions tests/GitPhp/GitConfig.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use CzProject\GitPhp\GitConfig;
use Tester\Assert;
use CzProject\GitPhp\Git;
use CzProject\GitPhp\Tests\AssertRunner;

require __DIR__ . '/bootstrap.php';

$runner = new AssertRunner(__DIR__);
$git = new Git($runner);

$repo = $git->open(__DIR__);
$config = $repo->getGitConfig();
Assert::same(GitConfig::class, get_class($config));

$runner->assert(['config', 'user.name', 'tester']);
$config->set('user.name', 'tester');

$runner->assert(['config', '--global', 'user.name', 'tester']);
$config->set('user.name', 'tester', '--global');

$runner->assert(['config', 'user.name']);
$config->get('user.name');

$runner->assert(['config', '--local', 'user.name']);
$config->get('user.name', ['--local']);