* Alle Rechte vorbehalten. */ namespace App\Service\Git; 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; protected string $workingDir; public function __construct( ){ $this->workingDir = sys_get_temp_dir(); } /** * 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; return $this; } /** * 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'])) { $repoUrl = Uri::new($repoUrl) ->withUsername($options['auth_basic']['username'] ?? '') ->withPassword($options['auth_basic']['password'] ?? '') ->toString() ; unset ($options['auth_basic']); } new Process( command: ['git', 'clone', ...$options, $repoUrl, $targetDir], cwd: $this->workingDir, )->mustRun(); } /** * 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 { 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::fromShellCommandline( command: 'git fetch --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( command: 'git update-ref', cwd: $this->workingDir . DIRECTORY_SEPARATOR . $dir )->mustRun(); } /** * 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( command: 'git pull --all --tags --force', cwd: $this->workingDir . DIRECTORY_SEPARATOR . $targetDir )->mustRun(); } /** * 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( 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( 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( 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); $latestRelease = $this->getLatestRelease($dir); // Update abbrechen, wenn Version bereits aktuell if($currentRelease === $latestRelease) { return; } // neueste Version herunterladen und auschecken $this->checkoutRepo($dir, $latestRelease); } }