From 03a7caa6205f96b99a45f375122081749dfcaa2a Mon Sep 17 00:00:00 2001 From: Netkas Date: Fri, 16 Dec 2022 00:38:46 -0500 Subject: [PATCH] Added GitClient to \ncc\Classes https://git.n64.cc/nosial/ncc/-/issues/28 --- src/ncc/Classes/GitClient.php | 118 ++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/ncc/Classes/GitClient.php diff --git a/src/ncc/Classes/GitClient.php b/src/ncc/Classes/GitClient.php new file mode 100644 index 0000000..ec94a30 --- /dev/null +++ b/src/ncc/Classes/GitClient.php @@ -0,0 +1,118 @@ +setTimeout(3600); // 1 hour + $process->run(function ($type, $buffer) + { + if (Process::ERR === $type) + { + Console::outWarning($buffer); + } + else + { + Console::outVerbose($buffer); + } + }); + + if (!$process->isSuccessful()) + throw new GitCloneException($process->getErrorOutput()); + + Console::outVerbose('Repository cloned to ' . $path); + return $path; + } + + /** + * Checks out a specific branch or tag. + * + * @param string $path + * @param string $branch + * @throws GitCheckoutException + */ + public static function checkout(string $path, string $branch) + { + $process = new Process(["git", "checkout", $branch], $path); + $process->setTimeout(3600); // 1 hour + $process->run(function ($type, $buffer) + { + if (Process::ERR === $type) + { + Console::outWarning($buffer); + } + else + { + Console::outVerbose($buffer); + } + }); + + if (!$process->isSuccessful()) + throw new GitCheckoutException($process->getErrorOutput()); + + Console::outVerbose('Checked out branch ' . $branch); + } + + /** + * Returns an array of tags that are available in the repository. + * + * @param string $path + * @return array + * @throws GitTagsException + */ + public static function getTags(string $path): array + { + $process = new Process(["git", "fetch", '--all', '--tags'] , $path); + $process->setTimeout(3600); // 1 hour + $process->run(function ($type, $buffer) + { + if (Process::ERR === $type) + { + Console::outWarning($buffer); + } + else + { + Console::outVerbose($buffer); + } + }); + + if (!$process->isSuccessful()) + throw new GitTagsException($process->getErrorOutput()); + + $process = new Process(['git', '--no-pager', 'tag', '-l'] , $path); + + $process->run(function ($type, $buffer) + { + if (Process::ERR === $type) + Console::outWarning($buffer); + + }); + + if (!$process->isSuccessful()) + throw new GitTagsException($process->getErrorOutput()); + + $tags = explode(PHP_EOL, $process->getOutput()); + return array_filter($tags); + } + + } \ No newline at end of file