Completed
Push — master ( 3c1663...329d8c )
by Jan
04:21
created

GitVersionInfo::getGitCommitHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Services;
33
34
35
use Symfony\Component\HttpKernel\KernelInterface;
36
37
class GitVersionInfo
38
{
39
    protected $project_dir;
40
41
    public function __construct(KernelInterface $kernel)
42
    {
43
        $this->project_dir = $kernel->getProjectDir();
44
    }
45
46
    /**
47
     * Get the Git branch name of the installed system
48
     * @return  string|null       The current git branch name. Null, if this is no Git installation
49
     */
50
    public function getGitBranchName()
51
    {
52
        if (file_exists($this->project_dir . '/.git/HEAD')) {
53
            $git = file($this->project_dir . '/.git/HEAD');
54
            $head = explode('/', $git[0], 3);
55
            return trim($head[2]);
56
        }
57
        return null; // this is not a Git installation
58
    }
59
60
    /**
61
     * Get hash of the last git commit (on remote "origin"!)
62
     * @note    If this method does not work, try to make a "git pull" first!
63
     * @param integer $length       if this is smaller than 40, only the first $length characters will be returned
64
     * @return string|null       The hash of the last commit, null If this is no Git installation
65
     */
66
    public function getGitCommitHash(int $length = 7)
67
    {
68
        $filename = $this->project_dir . '/.git/refs/remotes/origin/' . $this->getGitBranchName();
69
        if (file_exists($filename)) {
70
            $head = file($filename);
71
            $hash = $head[0];
72
            return substr($hash, 0, $length);
73
        }
74
        return null; // this is not a Git installation
75
    }
76
}