|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of SebastianFeldmann\Git. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace SebastianFeldmann\Git\Command; |
|
13
|
|
|
|
|
14
|
|
|
use SebastianFeldmann\Cli\Command; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Base |
|
18
|
|
|
* |
|
19
|
|
|
* @package SebastianFeldmann\Git |
|
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
21
|
|
|
* @link https://github.com/sebastianfeldmann/git |
|
22
|
|
|
* @since Class available since Release 0.9.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class Base implements Command |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Repository root directory. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $repositoryRoot; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Base constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $root |
|
37
|
52 |
|
*/ |
|
38
|
|
|
public function __construct(string $root = '') |
|
39
|
52 |
|
{ |
|
40
|
52 |
|
$this->repositoryRoot = $root; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Return cli command to execute. |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
29 |
|
*/ |
|
48
|
|
|
public function getCommand(): string |
|
49
|
|
|
{ |
|
50
|
29 |
|
$command = 'git' |
|
51
|
29 |
|
. $this->getRootOption() |
|
52
|
29 |
|
. ' ' |
|
53
|
29 |
|
. $this->getGitCommand(); |
|
54
|
|
|
return $command; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Return list of acceptable exit codes. |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
1 |
|
*/ |
|
62
|
|
|
public function getAcceptableExitCodes(): array |
|
63
|
1 |
|
{ |
|
64
|
|
|
return [0]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Do we need the -C option. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
29 |
|
*/ |
|
72
|
|
|
protected function getRootOption(): string |
|
73
|
29 |
|
{ |
|
74
|
|
|
$option = ''; |
|
75
|
29 |
|
// if root is set |
|
76
|
|
|
if (!empty($this->repositoryRoot)) { |
|
77
|
1 |
|
// and it's not the current working directory |
|
78
|
1 |
|
if (getcwd() !== $this->repositoryRoot) { |
|
79
|
|
|
$option = ' -C ' . escapeshellarg($this->repositoryRoot); |
|
80
|
|
|
} |
|
81
|
29 |
|
} |
|
82
|
|
|
return $option; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Should a option be used or not. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $option |
|
89
|
|
|
* @param bool $switch |
|
90
|
|
|
* @return string |
|
91
|
5 |
|
*/ |
|
92
|
|
|
protected function useOption(string $option, bool $switch): string |
|
93
|
5 |
|
{ |
|
94
|
|
|
return ($switch ? ' ' . $option : ''); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Auto cast method. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
4 |
|
*/ |
|
103
|
|
|
public function __toString(): string |
|
104
|
4 |
|
{ |
|
105
|
|
|
return $this->getCommand(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return the command to execute. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
* @throws \RuntimeException |
|
113
|
|
|
*/ |
|
114
|
|
|
abstract protected function getGitCommand(): string; |
|
115
|
|
|
} |
|
116
|
|
|
|