From 6ccc7369c13680721908c9d55c544475550c882f Mon Sep 17 00:00:00 2001 From: sd Date: Sun, 9 Feb 2025 20:27:47 +0100 Subject: [PATCH] Add config class to handle git config --- readme.md | 14 +++++++ src/GitConfig.php | 73 +++++++++++++++++++++++++++++++++++++ src/GitRepository.php | 14 ++++++- tests/GitPhp/GitConfig.phpt | 27 ++++++++++++++ 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/GitConfig.php create mode 100644 tests/GitPhp/GitConfig.phpt diff --git a/readme.md b/readme.md index 88b031a..998a74a 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/src/GitConfig.php b/src/GitConfig.php new file mode 100644 index 0000000..8369228 --- /dev/null +++ b/src/GitConfig.php @@ -0,0 +1,73 @@ +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; + } +} \ No newline at end of file diff --git a/src/GitRepository.php b/src/GitRepository.php index cd8d5fa..9a0acf3 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -5,7 +5,7 @@ class GitRepository { - /** @var string */ + /** @var string */ protected $repository; /** @var IRunner */ @@ -42,7 +42,17 @@ public function getRepositoryPath() } - /** + /** + * @return GitConfig + */ + public function getGitConfig() + { + return new GitConfig($this, $this->runner); + } + + + + /** * Creates a tag. * `git tag ` * @param string $name diff --git a/tests/GitPhp/GitConfig.phpt b/tests/GitPhp/GitConfig.phpt new file mode 100644 index 0000000..1f41e5c --- /dev/null +++ b/tests/GitPhp/GitConfig.phpt @@ -0,0 +1,27 @@ +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']);