mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-02-12 04:29:36 +00:00
Centralized git logic from InstallationTypeDetector and UpdateChecker in GitVersionInfoProvider service
This commit is contained in:
parent
7ff07a7ab4
commit
6dbead6d10
11 changed files with 242 additions and 214 deletions
|
|
@ -1,83 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Misc;
|
||||
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
|
||||
class GitVersionInfo
|
||||
{
|
||||
protected string $project_dir;
|
||||
|
||||
public function __construct(KernelInterface $kernel)
|
||||
{
|
||||
$this->project_dir = $kernel->getProjectDir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Git branch name of the installed system.
|
||||
*
|
||||
* @return string|null The current git branch name. Null, if this is no Git installation
|
||||
*/
|
||||
public function getGitBranchName(): ?string
|
||||
{
|
||||
if (is_file($this->project_dir.'/.git/HEAD')) {
|
||||
$git = file($this->project_dir.'/.git/HEAD');
|
||||
$head = explode('/', $git[0], 3);
|
||||
|
||||
if (!isset($head[2])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return trim($head[2]);
|
||||
}
|
||||
|
||||
return null; // this is not a Git installation
|
||||
}
|
||||
|
||||
/**
|
||||
* Get hash of the last git commit (on remote "origin"!).
|
||||
*
|
||||
* If this method does not work, try to make a "git pull" first!
|
||||
*
|
||||
* @param int $length if this is smaller than 40, only the first $length characters will be returned
|
||||
*
|
||||
* @return string|null The hash of the last commit, null If this is no Git installation
|
||||
*/
|
||||
public function getGitCommitHash(int $length = 7): ?string
|
||||
{
|
||||
$filename = $this->project_dir.'/.git/refs/remotes/origin/'.$this->getGitBranchName();
|
||||
if (is_file($filename)) {
|
||||
$head = file($filename);
|
||||
|
||||
if (!isset($head[0])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hash = $head[0];
|
||||
|
||||
return substr($hash, 0, $length);
|
||||
}
|
||||
|
||||
return null; // this is not a Git installation
|
||||
}
|
||||
}
|
||||
135
src/Services/System/GitVersionInfoProvider.php
Normal file
135
src/Services/System/GitVersionInfoProvider.php
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\System;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
* This service provides information about the current Git installation (if any).
|
||||
*/
|
||||
final readonly class GitVersionInfoProvider
|
||||
{
|
||||
public function __construct(#[Autowire(param: 'kernel.project_dir')] private string $project_dir)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the project directory is a Git repository.
|
||||
* @return bool
|
||||
*/
|
||||
public function isGitRepo(): bool
|
||||
{
|
||||
return is_dir($this->getGitDirectory());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the Git directory of the installed system without a trailing slash.
|
||||
* Even if this is no Git installation, the path is returned.
|
||||
* @return string The path to the Git directory of the installed system
|
||||
*/
|
||||
public function getGitDirectory(): string
|
||||
{
|
||||
return $this->project_dir . '/.git';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Git branch name of the installed system.
|
||||
*
|
||||
* @return string|null The current git branch name. Null, if this is no Git installation
|
||||
*/
|
||||
public function getBranchName(): ?string
|
||||
{
|
||||
if (is_file($this->getGitDirectory() . '/HEAD')) {
|
||||
$git = file($this->getGitDirectory() . '/HEAD');
|
||||
$head = explode('/', $git[0], 3);
|
||||
|
||||
if (!isset($head[2])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return trim($head[2]);
|
||||
}
|
||||
|
||||
return null; // this is not a Git installation
|
||||
}
|
||||
|
||||
/**
|
||||
* Get hash of the last git commit (on remote "origin"!).
|
||||
*
|
||||
* If this method does not work, try to make a "git pull" first!
|
||||
*
|
||||
* @param int $length if this is smaller than 40, only the first $length characters will be returned
|
||||
*
|
||||
* @return string|null The hash of the last commit, null If this is no Git installation
|
||||
*/
|
||||
public function getCommitHash(int $length = 8): ?string
|
||||
{
|
||||
$filename = $this->getGitDirectory() . '/refs/remotes/origin/'.$this->getBranchName();
|
||||
if (is_file($filename)) {
|
||||
$head = file($filename);
|
||||
|
||||
if (!isset($head[0])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hash = $head[0];
|
||||
|
||||
return substr($hash, 0, $length);
|
||||
}
|
||||
|
||||
return null; // this is not a Git installation
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Git remote URL of the installed system.
|
||||
*/
|
||||
public function getRemoteURL(): ?string
|
||||
{
|
||||
// Get remote URL
|
||||
$configFile = $this->getGitDirectory() . '/config';
|
||||
if (file_exists($configFile)) {
|
||||
$config = file_get_contents($configFile);
|
||||
if (preg_match('#url = (.+)#', $config, $matches)) {
|
||||
return trim($matches[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return null; // this is not a Git installation
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if there are local changes in the Git repository.
|
||||
* Attention: This runs a git command, which might be slow!
|
||||
* @return bool|null True if there are local changes, false if not, null if this is not a Git installation
|
||||
*/
|
||||
public function hasLocalChanges(): ?bool
|
||||
{
|
||||
$process = new Process(['git', 'status', '--porcelain'], $this->project_dir);
|
||||
$process->run();
|
||||
if (!$process->isSuccessful()) {
|
||||
return null; // this is not a Git installation
|
||||
}
|
||||
return !empty(trim($process->getOutput()));
|
||||
}
|
||||
}
|
||||
65
src/Services/System/InstallationType.php
Normal file
65
src/Services/System/InstallationType.php
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\System;
|
||||
|
||||
/**
|
||||
* Detects the installation type of Part-DB to determine the appropriate update strategy.
|
||||
*/
|
||||
enum InstallationType: string
|
||||
{
|
||||
case GIT = 'git';
|
||||
case DOCKER = 'docker';
|
||||
case ZIP_RELEASE = 'zip_release';
|
||||
case UNKNOWN = 'unknown';
|
||||
|
||||
public function getLabel(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::GIT => 'Git Clone',
|
||||
self::DOCKER => 'Docker',
|
||||
self::ZIP_RELEASE => 'Release Archive (ZIP File)',
|
||||
self::UNKNOWN => 'Unknown',
|
||||
};
|
||||
}
|
||||
|
||||
public function supportsAutoUpdate(): bool
|
||||
{
|
||||
return match ($this) {
|
||||
self::GIT => true,
|
||||
self::DOCKER => false,
|
||||
// ZIP_RELEASE auto-update not yet implemented
|
||||
self::ZIP_RELEASE => false,
|
||||
self::UNKNOWN => false,
|
||||
};
|
||||
}
|
||||
|
||||
public function getUpdateInstructions(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::GIT => 'Run: php bin/console partdb:update',
|
||||
self::DOCKER => 'Pull the new Docker image and recreate the container: docker-compose pull && docker-compose up -d',
|
||||
self::ZIP_RELEASE => 'Download the new release ZIP from GitHub, extract it over your installation, and run: php bin/console doctrine:migrations:migrate && php bin/console cache:clear',
|
||||
self::UNKNOWN => 'Unable to determine installation type. Please update manually.',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -26,51 +26,9 @@ namespace App\Services\System;
|
|||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
/**
|
||||
* Detects the installation type of Part-DB to determine the appropriate update strategy.
|
||||
*/
|
||||
enum InstallationType: string
|
||||
readonly class InstallationTypeDetector
|
||||
{
|
||||
case GIT = 'git';
|
||||
case DOCKER = 'docker';
|
||||
case ZIP_RELEASE = 'zip_release';
|
||||
case UNKNOWN = 'unknown';
|
||||
|
||||
public function getLabel(): string
|
||||
{
|
||||
return match($this) {
|
||||
self::GIT => 'Git Clone',
|
||||
self::DOCKER => 'Docker',
|
||||
self::ZIP_RELEASE => 'Release Archive',
|
||||
self::UNKNOWN => 'Unknown',
|
||||
};
|
||||
}
|
||||
|
||||
public function supportsAutoUpdate(): bool
|
||||
{
|
||||
return match($this) {
|
||||
self::GIT => true,
|
||||
self::DOCKER => false,
|
||||
// ZIP_RELEASE auto-update not yet implemented
|
||||
self::ZIP_RELEASE => false,
|
||||
self::UNKNOWN => false,
|
||||
};
|
||||
}
|
||||
|
||||
public function getUpdateInstructions(): string
|
||||
{
|
||||
return match($this) {
|
||||
self::GIT => 'Run: php bin/console partdb:update',
|
||||
self::DOCKER => 'Pull the new Docker image and recreate the container: docker-compose pull && docker-compose up -d',
|
||||
self::ZIP_RELEASE => 'Download the new release ZIP from GitHub, extract it over your installation, and run: php bin/console doctrine:migrations:migrate && php bin/console cache:clear',
|
||||
self::UNKNOWN => 'Unable to determine installation type. Please update manually.',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class InstallationTypeDetector
|
||||
{
|
||||
public function __construct(#[Autowire(param: 'kernel.project_dir')] private readonly string $project_dir)
|
||||
public function __construct(#[Autowire(param: 'kernel.project_dir')] private string $project_dir, private GitVersionInfoProvider $gitVersionInfoProvider)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -129,7 +87,7 @@ class InstallationTypeDetector
|
|||
*/
|
||||
public function isGitInstall(): bool
|
||||
{
|
||||
return is_dir($this->project_dir . '/.git');
|
||||
return $this->gitVersionInfoProvider->isGitRepo();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -169,51 +127,21 @@ class InstallationTypeDetector
|
|||
|
||||
/**
|
||||
* Get Git-specific information.
|
||||
* @return array{branch: string|null, commit: string|null, remote_url: string|null, has_local_changes: bool}
|
||||
*/
|
||||
private function getGitInfo(): array
|
||||
{
|
||||
$info = [
|
||||
'branch' => null,
|
||||
'commit' => null,
|
||||
'remote_url' => null,
|
||||
'has_local_changes' => false,
|
||||
return [
|
||||
'branch' => $this->gitVersionInfoProvider->getBranchName(),
|
||||
'commit' => $this->gitVersionInfoProvider->getCommitHash(8),
|
||||
'remote_url' => $this->gitVersionInfoProvider->getRemoteURL(),
|
||||
'has_local_changes' => $this->gitVersionInfoProvider->hasLocalChanges() ?? false,
|
||||
];
|
||||
|
||||
// Get branch
|
||||
$headFile = $this->project_dir . '/.git/HEAD';
|
||||
if (file_exists($headFile)) {
|
||||
$head = file_get_contents($headFile);
|
||||
if (preg_match('#ref: refs/heads/(.+)#', $head, $matches)) {
|
||||
$info['branch'] = trim($matches[1]);
|
||||
}
|
||||
}
|
||||
|
||||
// Get remote URL
|
||||
$configFile = $this->project_dir . '/.git/config';
|
||||
if (file_exists($configFile)) {
|
||||
$config = file_get_contents($configFile);
|
||||
if (preg_match('#url = (.+)#', $config, $matches)) {
|
||||
$info['remote_url'] = trim($matches[1]);
|
||||
}
|
||||
}
|
||||
|
||||
// Get commit hash
|
||||
$process = new Process(['git', 'rev-parse', '--short', 'HEAD'], $this->project_dir);
|
||||
$process->run();
|
||||
if ($process->isSuccessful()) {
|
||||
$info['commit'] = trim($process->getOutput());
|
||||
}
|
||||
|
||||
// Check for local changes
|
||||
$process = new Process(['git', 'status', '--porcelain'], $this->project_dir);
|
||||
$process->run();
|
||||
$info['has_local_changes'] = !empty(trim($process->getOutput()));
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Docker-specific information.
|
||||
* @return array{container_id: string|null, image: string|null}
|
||||
*/
|
||||
private function getDockerInfo(): array
|
||||
{
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ class UpdateChecker
|
|||
private readonly CacheInterface $updateCache, private readonly VersionManagerInterface $versionManager,
|
||||
private readonly PrivacySettings $privacySettings, private readonly LoggerInterface $logger,
|
||||
private readonly InstallationTypeDetector $installationTypeDetector,
|
||||
private readonly GitVersionInfoProvider $gitVersionInfoProvider,
|
||||
#[Autowire(param: 'kernel.debug')] private readonly bool $is_dev_mode,
|
||||
#[Autowire(param: 'kernel.project_dir')] private readonly string $project_dir)
|
||||
{
|
||||
|
|
@ -84,34 +85,15 @@ class UpdateChecker
|
|||
'is_git_install' => false,
|
||||
];
|
||||
|
||||
$gitDir = $this->project_dir . '/.git';
|
||||
|
||||
if (!is_dir($gitDir)) {
|
||||
if (!$this->gitVersionInfoProvider->isGitRepo()) {
|
||||
return $info;
|
||||
}
|
||||
|
||||
$info['is_git_install'] = true;
|
||||
|
||||
// Get branch from HEAD file
|
||||
$headFile = $gitDir . '/HEAD';
|
||||
if (file_exists($headFile)) {
|
||||
$head = file_get_contents($headFile);
|
||||
if (preg_match('#ref: refs/heads/(.+)#', $head, $matches)) {
|
||||
$info['branch'] = trim($matches[1]);
|
||||
}
|
||||
}
|
||||
|
||||
// Get current commit
|
||||
$process = new Process(['git', 'rev-parse', '--short', 'HEAD'], $this->project_dir);
|
||||
$process->run();
|
||||
if ($process->isSuccessful()) {
|
||||
$info['commit'] = trim($process->getOutput());
|
||||
}
|
||||
|
||||
// Check for local changes
|
||||
$process = new Process(['git', 'status', '--porcelain'], $this->project_dir);
|
||||
$process->run();
|
||||
$info['has_local_changes'] = !empty(trim($process->getOutput()));
|
||||
$info['branch'] = $this->gitVersionInfoProvider->getBranchName();
|
||||
$info['commit'] = $this->gitVersionInfoProvider->getCommitHash(8);
|
||||
$info['has_local_changes'] = $this->gitVersionInfoProvider->hasLocalChanges();
|
||||
|
||||
// Get commits behind (fetch first)
|
||||
if ($info['branch']) {
|
||||
|
|
@ -151,7 +133,7 @@ class UpdateChecker
|
|||
/**
|
||||
* Force refresh git information by invalidating cache.
|
||||
*/
|
||||
public function refreshGitInfo(): void
|
||||
public function refreshVersionInfo(): void
|
||||
{
|
||||
$gitInfo = $this->getGitInfo();
|
||||
if ($gitInfo['branch']) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue