diff --git a/src/Service/Git/GitService.php b/src/Service/Git/GitService.php index 401895c..2fb9b75 100644 --- a/src/Service/Git/GitService.php +++ b/src/Service/Git/GitService.php @@ -1,6 +1,6 @@ * Alle Rechte vorbehalten. @@ -12,6 +12,10 @@ use App\Traits\LoggerTrait; use League\Uri\Uri; use Symfony\Component\Process\Process; +/** + * Provides functionalities for managing Git repositories, including cloning, + * fetching, checking out, and retrieving information about branches and releases. + */ class GitService { use LoggerTrait; @@ -24,6 +28,12 @@ class GitService } + /** + * Sets the working directory to the specified path. + * + * @param string $dir The path to set as the working directory. + * @return self The current instance for method chaining. + */ public function setWorkingDir(string $dir): self { $this->workingDir = $dir; @@ -31,12 +41,29 @@ class GitService } + /** + * Checks if the specified directory within the working directory is a cloned Git repository. + * + * @param string $dir The relative path to the directory to check. Defaults to '.git'. + * @return bool True if the directory exists and is a cloned Git repository, false otherwise. + */ public function isCloned(string $dir = '.git'): bool { return is_dir($this->workingDir . DIRECTORY_SEPARATOR . $dir); } + /** + * Clones a Git repository into the specified target directory with optional configurations. + * + * @param string $repoUrl The URL of the Git repository to be cloned. + * @param string $targetDir The target directory where the repository will be cloned. Defaults to an empty string, leading to the use of the repository's default folder name. + * @param array $options Optional configurations for the cloning process. Supports 'auth_basic' for basic authentication with keys: + * - 'username': The username for authentication. + * - 'password': The password for authentication. + * Other options will be passed directly to the `git clone` command. + * @return void + */ public function cloneRepo(string $repoUrl, string $targetDir = '', array $options = []): void { if(!empty($options['auth_basic'])) @@ -57,22 +84,45 @@ class GitService } + /** + * Checks out the specified branch in the Git repository located in the given directory. + * + * @param string $dir The relative path to the directory containing the Git repository. + * @param string $branch The name of the branch to check out. Defaults to 'master'. + * @return void + */ public function checkoutRepo(string $dir, string $branch = 'master'): void { - $process = new Process(['git', 'checkout', '-f', $branch]); - $process->setWorkingDirectory($this->workingDir . DIRECTORY_SEPARATOR . $dir); - $process->mustRun(); + new Process( + command: ['git', 'checkout', '-f', $branch], + cwd: $this->workingDir . DIRECTORY_SEPARATOR . $dir, + )->mustRun(); } + /** + * Fetches all branches and tags from the Git repository within the specified directory. + * + * @param string $dir The relative path to the directory containing the Git repository. + * @param array $options Additional options for configuring the fetch process (currently unused). + * @return void + */ public function fetchRepo(string $dir, array $options = []): void { - $process = Process::fromShellCommandline('git --all --tags'); - $process->setWorkingDirectory($this->workingDir . DIRECTORY_SEPARATOR . $dir); - $process->mustRun(); + Process::fromShellCommandline( + command: 'git --all --tags', + cwd: $this->workingDir . DIRECTORY_SEPARATOR . $dir, + )->mustRun(); } + /** + * Updates Git references in the specified repository directory. + * + * @param string $dir The relative path to the directory containing the Git repository. + * @param array $options Additional options for the operation (not currently utilized). + * @return void + */ public function updateRef(string $dir, array $options = []): void { Process::fromShellCommandline( @@ -82,6 +132,13 @@ class GitService } + /** + * Executes a Git pull command to update all branches and fetch all tags in the specified repository directory. + * + * @param string $targetDir The relative path to the directory containing the Git repository. + * @param array $options An array of additional options (currently unused). + * @return void + */ public function pullRepo(string $targetDir, array $options = []): void { Process::fromShellCommandline( @@ -91,36 +148,66 @@ class GitService } + /** + * Retrieves the default branch name of the Git repository within the specified directory. + * + * @param string $dir The relative path to the directory containing the Git repository. + * @return string The default branch name, or 'master' if none is found. + */ public function getRepoDefaultBranch(string $dir): string { - $process = Process::fromShellCommandline('git symbolic-ref --short HEAD'); - $process->setWorkingDirectory($this->workingDir . DIRECTORY_SEPARATOR . $dir); - $process->mustRun(); + $process = Process::fromShellCommandline( + command: 'git symbolic-ref --short HEAD', + cwd: $this->workingDir . DIRECTORY_SEPARATOR . $dir + )->mustRun(); return trim($process->getOutput()) ?: 'master'; } + /** + * Retrieves the current release tag from the Git repository within the specified directory. + * + * @param string $dir The relative path to the directory containing the Git repository. + * @return string The current release tag. + */ public function getCurrentRelease(string $dir): string { - $process = Process::fromShellCommandline('git describe --tags'); - $process->setWorkingDirectory($this->workingDir . DIRECTORY_SEPARATOR . $dir); - $process->mustRun(); + $process = Process::fromShellCommandline( + command: 'git describe --tags', + cwd: $this->workingDir . DIRECTORY_SEPARATOR . $dir + )->mustRun(); return trim($process->getOutput()); } + /** + * Retrieves the latest release tag from the Git repository within the specified directory. + * + * @param string $dir The relative path to the directory containing the Git repository. + * @return string The latest release tag. + */ public function getLatestRelease(string $dir): string { - $process = Process::fromShellCommandline('git tag --sort=committerdate --list "v[0-9]*" "[0-9]*.[0-9]*.[0-9]*" | tail -1'); - $process->setWorkingDirectory($this->workingDir . DIRECTORY_SEPARATOR . $dir); - $process->mustRun(); + $process = Process::fromShellCommandline( + command: 'git tag --sort=committerdate --list "v[0-9]*" "[0-9]*.[0-9]*.[0-9]*" | tail -1', + cwd: $this->workingDir . DIRECTORY_SEPARATOR . $dir + )->mustRun(); return trim($process->getOutput()); } + /** + * Checks out the latest release version in the specified directory. + * + * Compares the current release version with the latest available version. + * If the current version is already up to date, the update process is skipped. + * Otherwise, the latest version is downloaded and checked out. + * + * @param string $dir The directory where the release is located. + */ public function checkoutLatestRelease(string $dir): void { $currentRelease = $this->getCurrentRelease($dir);