1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GitElephant - An abstraction layer for git written in PHP |
5
|
|
|
* Copyright (C) 2013 Matteo Giachino |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program. If not, see [http://www.gnu.org/licenses/]. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace GitElephant\Command; |
22
|
|
|
|
23
|
|
|
use GitElephant\Command\Remote\AddSubCommand; |
24
|
|
|
use GitElephant\Command\Remote\ShowSubCommand; |
25
|
|
|
use GitElephant\Repository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class RemoteCommand |
29
|
|
|
* |
30
|
|
|
* remote command generator |
31
|
|
|
* |
32
|
|
|
* @package GitElephant\Objects |
33
|
|
|
* @author David Neimeyer <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class RemoteCommand extends BaseCommand |
36
|
|
|
{ |
37
|
|
|
public const GIT_REMOTE = 'remote'; |
38
|
|
|
public const GIT_REMOTE_OPTION_VERBOSE = '--verbose'; |
39
|
|
|
public const GIT_REMOTE_OPTION_VERBOSE_SHORT = '-v'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* constructor |
43
|
|
|
* |
44
|
|
|
* @param \GitElephant\Repository $repo The repository object this command |
45
|
|
|
* will interact with |
46
|
|
|
*/ |
47
|
13 |
|
public function __construct(Repository $repo = null) |
48
|
|
|
{ |
49
|
13 |
|
parent::__construct($repo); |
50
|
13 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Build the remote command |
54
|
|
|
* |
55
|
|
|
* NOTE: git-remote is most useful when using its subcommands, therefore |
56
|
|
|
* in practice you will likely pass a SubCommandCommand object. This |
57
|
|
|
* class provide "convenience" methods that do this for you. |
58
|
|
|
* |
59
|
|
|
* @param \GitElephant\Command\SubCommandCommand $subcommand A subcommand object |
60
|
|
|
* @param array $options Options for the main git-remote command |
61
|
|
|
* |
62
|
|
|
* @throws \RuntimeException |
63
|
|
|
* @return string Command string to pass to caller |
64
|
|
|
*/ |
65
|
13 |
|
public function remote(SubCommandCommand $subcommand = null, array $options = []): string |
66
|
|
|
{ |
67
|
13 |
|
$normalizedOptions = $this->normalizeOptions($options, $this->remoteCmdSwitchOptions()); |
68
|
|
|
|
69
|
13 |
|
$this->clearAll(); |
70
|
|
|
|
71
|
13 |
|
$this->addCommandName(self::GIT_REMOTE); |
72
|
|
|
|
73
|
13 |
|
foreach ($normalizedOptions as $value) { |
74
|
3 |
|
$this->addCommandArgument($value); |
75
|
|
|
} |
76
|
13 |
|
if ($subcommand !== null) { |
77
|
10 |
|
$this->addCommandSubject($subcommand); |
78
|
|
|
} |
79
|
|
|
|
80
|
13 |
|
return $this->getCommand(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Valid options for remote command that do not require an associated value |
85
|
|
|
* |
86
|
|
|
* @return array Associative array mapping all non-value options and their respective normalized option |
87
|
|
|
*/ |
88
|
13 |
|
public function remoteCmdSwitchOptions(): array |
89
|
|
|
{ |
90
|
|
|
return [ |
91
|
13 |
|
self::GIT_REMOTE_OPTION_VERBOSE => self::GIT_REMOTE_OPTION_VERBOSE, |
92
|
13 |
|
self::GIT_REMOTE_OPTION_VERBOSE_SHORT => self::GIT_REMOTE_OPTION_VERBOSE, |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* git-remote --verbose command |
98
|
|
|
* |
99
|
|
|
* @throws \RuntimeException |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
3 |
|
public function verbose(): string |
103
|
|
|
{ |
104
|
3 |
|
return $this->remote(null, [self::GIT_REMOTE_OPTION_VERBOSE]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* git-remote show [name] command |
109
|
|
|
* |
110
|
|
|
* NOTE: for technical reasons $name is optional, however under normal |
111
|
|
|
* implementation it SHOULD be passed! |
112
|
|
|
* |
113
|
|
|
* @param string $name |
114
|
|
|
* @param bool $queryRemotes |
115
|
|
|
* |
116
|
|
|
* @throws \RuntimeException |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
3 |
|
public function show($name = null, bool $queryRemotes = true): string |
120
|
|
|
{ |
121
|
3 |
|
$subcmd = new ShowSubCommand(); |
122
|
3 |
|
$subcmd->prepare($name, $queryRemotes); |
123
|
|
|
|
124
|
3 |
|
return $this->remote($subcmd); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* git-remote add [options] <name> <url> |
129
|
|
|
* |
130
|
|
|
* @param string $name remote name |
131
|
|
|
* @param string $url URL of remote |
132
|
|
|
* @param array $options options for the add subcommand |
133
|
|
|
* |
134
|
|
|
* @throws \RuntimeException |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
8 |
|
public function add($name, $url, $options = []): string |
138
|
|
|
{ |
139
|
8 |
|
$subcmd = new AddSubCommand(); |
140
|
8 |
|
$subcmd->prepare($name, $url, $options); |
141
|
|
|
|
142
|
8 |
|
return $this->remote($subcmd); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|