1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of SebastianFeldmann\Git. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace SebastianFeldmann\Git\Command; |
11
|
|
|
|
12
|
|
|
use SebastianFeldmann\Cli\Command; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Base |
16
|
|
|
* |
17
|
|
|
* @package SebastianFeldmann\Git |
18
|
|
|
*/ |
19
|
|
|
abstract class Base implements Command |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Repository root directory |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $repositoryRoot; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Base constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $root |
32
|
|
|
*/ |
33
|
19 |
|
public function __construct(string $root = '') |
34
|
|
|
{ |
35
|
19 |
|
$this->repositoryRoot = $root; |
36
|
19 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return cli command to execute. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
13 |
|
public function getCommand() : string |
44
|
|
|
{ |
45
|
|
|
$command = 'git' |
46
|
13 |
|
. $this->getRootOption() |
47
|
13 |
|
. ' ' |
48
|
13 |
|
. $this->getGitCommand(); |
49
|
|
|
/* |
|
|
|
|
50
|
|
|
if (DIRECTORY_SEPARATOR == '/') { |
51
|
|
|
$command = 'LC_ALL=en_US.UTF-8 ' . $command; |
52
|
|
|
} |
53
|
|
|
*/ |
54
|
13 |
|
return $command; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Do we need the -C option. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
13 |
|
protected function getRootOption() : string |
63
|
|
|
{ |
64
|
13 |
|
$option = ''; |
65
|
|
|
// if root is set |
66
|
13 |
|
if (!empty($this->repositoryRoot)) { |
67
|
|
|
// and it's not the current working directory |
68
|
1 |
|
if (getcwd() !== $this->repositoryRoot) { |
69
|
1 |
|
$option = ' -C ' . escapeshellarg($this->repositoryRoot); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
13 |
|
return $option; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Auto cast method. |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
4 |
|
public function __toString() : string |
82
|
|
|
{ |
83
|
4 |
|
return $this->getCommand(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return the command to execute. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
* @throws \RuntimeException |
91
|
|
|
*/ |
92
|
|
|
protected abstract function getGitCommand() : string; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.