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\Objects\Branch; |
24
|
|
|
use GitElephant\Objects\Remote; |
25
|
|
|
use GitElephant\Repository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class FetchCommand |
29
|
|
|
*/ |
30
|
|
|
class FetchCommand extends BaseCommand |
31
|
|
|
{ |
32
|
|
|
public const GIT_FETCH_COMMAND = 'fetch'; |
33
|
|
|
public const GIT_FETCH_OPTION_TAGS = '--tags'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* constructor |
37
|
|
|
* |
38
|
|
|
* @param \GitElephant\Repository $repo The repository object this command |
39
|
|
|
* will interact with |
40
|
|
|
*/ |
41
|
2 |
|
public function __construct(Repository $repo = null) |
42
|
|
|
{ |
43
|
2 |
|
parent::__construct($repo); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Remote|string $remote |
48
|
|
|
* @param Branch|string $branch |
49
|
|
|
* @param array $options |
50
|
|
|
* |
51
|
|
|
* @throws \RuntimeException |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
2 |
|
public function fetch($remote = null, $branch = null, array $options = []): string |
55
|
|
|
{ |
56
|
2 |
|
if ($remote instanceof Remote) { |
57
|
1 |
|
$remote = $remote->getName(); |
58
|
|
|
} |
59
|
2 |
|
if ($branch instanceof Branch) { |
60
|
1 |
|
$branch = $branch->getName(); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
$normalizedOptions = $this->normalizeOptions($options, $this->fetchCmdSwitchOptions()); |
64
|
|
|
|
65
|
2 |
|
$this->clearAll(); |
66
|
2 |
|
$this->addCommandName(self::GIT_FETCH_COMMAND); |
67
|
|
|
|
68
|
2 |
|
foreach ($normalizedOptions as $value) { |
69
|
2 |
|
$this->addCommandArgument($value); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
if (!is_null($remote)) { |
73
|
1 |
|
$this->addCommandSubject($remote); |
74
|
|
|
} |
75
|
2 |
|
if (!is_null($branch)) { |
76
|
1 |
|
$this->addCommandSubject2($branch); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return $this->getCommand(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Valid options for remote command that do not require an associated value |
84
|
|
|
* |
85
|
|
|
* @return array Associative array mapping all non-value options and their respective normalized option |
86
|
|
|
*/ |
87
|
2 |
|
public function fetchCmdSwitchOptions(): array |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
2 |
|
self::GIT_FETCH_OPTION_TAGS => self::GIT_FETCH_OPTION_TAGS, |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|