diff --git a/src/Controller/HomepageController.php b/src/Controller/HomepageController.php index 495199da..0fa28651 100644 --- a/src/Controller/HomepageController.php +++ b/src/Controller/HomepageController.php @@ -30,6 +30,7 @@ namespace App\Controller; +use App\Services\GitVersionInfo; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; @@ -38,11 +39,12 @@ class HomepageController extends AbstractController /** * @Route("/", name="homepage") */ - public function homepage() + public function homepage(GitVersionInfo $versionInfo) { - return $this->render('homepage.html.twig', - [ - 'banner' => $this->getParameter('banner'), - ]); + return $this->render('homepage.html.twig', [ + 'banner' => $this->getParameter('banner'), + 'git_branch' => $versionInfo->getGitBranchName(), + 'git_commit' => $versionInfo->getGitCommitHash() + ]); } } diff --git a/src/Services/GitVersionInfo.php b/src/Services/GitVersionInfo.php new file mode 100644 index 00000000..bf56f74c --- /dev/null +++ b/src/Services/GitVersionInfo.php @@ -0,0 +1,76 @@ +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() + { + if (file_exists($this->project_dir . '/.git/HEAD')) { + $git = file($this->project_dir . '/.git/HEAD'); + $head = explode('/', $git[0], 3); + return trim($head[2]); + } + return null; // this is not a Git installation + } + + /** + * Get hash of the last git commit (on remote "origin"!) + * @note If this method does not work, try to make a "git pull" first! + * @param integer $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) + { + $filename = $this->project_dir . '/.git/refs/remotes/origin/' . $this->getGitBranchName(); + if (file_exists($filename)) { + $head = file($filename); + $hash = $head[0]; + return substr($hash, 0, $length); + } + return null; // this is not a Git installation + } +} \ No newline at end of file diff --git a/templates/homepage.html.twig b/templates/homepage.html.twig index 964343d6..81e71cb8 100644 --- a/templates/homepage.html.twig +++ b/templates/homepage.html.twig @@ -5,6 +5,9 @@

{{ partdb_title }}

{% trans %}version.caption{% endtrans %}: {{ shivas_app_version }} + {% if git_branch is not empty or git_commit is not empty %} + ({{ git_branch ?? '' }}/{{ git_commit ?? '' }}) + {% endif %}

{% if banner is not empty %}