1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdelespierre\GitStats\Services; |
4
|
|
|
|
5
|
|
|
use Bdelespierre\GitStats\Interfaces\ExecutableFinderInterface; |
6
|
|
|
use Bdelespierre\GitStats\Interfaces\GitServiceInterface; |
7
|
|
|
use Bdelespierre\GitStats\Interfaces\ProcessServiceInterface; |
8
|
|
|
use Symfony\Component\Process\ExecutableFinder; |
|
|
|
|
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
class GitService implements GitServiceInterface |
12
|
|
|
{ |
13
|
|
|
protected ProcessServiceInterface $process; |
14
|
|
|
protected ExecutableFinderInterface $finder; |
15
|
|
|
protected ?string $git; |
16
|
|
|
|
17
|
|
|
public function __construct( |
18
|
|
|
ProcessServiceInterface $process, |
19
|
|
|
ExecutableFinderInterface $finder |
20
|
|
|
) { |
21
|
|
|
$this->process = $process; |
22
|
|
|
$this->finder = $finder; |
23
|
|
|
$this->git = $this->getGitPath(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function git(array $command): Process |
27
|
|
|
{ |
28
|
|
|
$process = $this->process->make([$this->git, ...$command]); |
29
|
|
|
$process->run(); |
30
|
|
|
|
31
|
|
|
return $process; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function getGitPath(): ?string |
35
|
|
|
{ |
36
|
|
|
return $this->finder->find('git'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function isGitAvailable(): bool |
40
|
|
|
{ |
41
|
|
|
return $this->git != null; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function isGitRepository(): bool |
45
|
|
|
{ |
46
|
|
|
return $this->git(["rev-parse", "--git-dir"])->isSuccessful(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function updateIndex(): bool |
50
|
|
|
{ |
51
|
|
|
return $this->git(["update-index", "-q", "--ignore-submodules", "--refresh"])->isSuccessful(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function hasUnstagedChanges(): bool |
55
|
|
|
{ |
56
|
|
|
return ! $this->git(["diff-files", "--quiet", "--ignore-submodules", "--"])->isSuccessful(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function hasUncommittedChanges(): bool |
60
|
|
|
{ |
61
|
|
|
return ! $this->git(["diff-index", "--cached", "--quiet", "HEAD", "--ignore-submodules", "--"])->isSuccessful(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function isValidBranch(string $branch): bool |
65
|
|
|
{ |
66
|
|
|
return $this->git(["rev-parse", "--verify", $branch])->isSuccessful(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function checkout(string $commit): bool |
70
|
|
|
{ |
71
|
|
|
return $this->git(["checkout", $commit])->isSuccessful(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getCommits(string $branch): iterable |
75
|
|
|
{ |
76
|
|
|
return array_filter(explode(PHP_EOL, $this->git(["rev-list", $branch], true)->getOutput())); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getCommitTimestamp(string $commit): int |
80
|
|
|
{ |
81
|
|
|
return (int) trim($this->git(["show", "-s", "--format=%ct", $commit])->getOutput()); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: