|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* GitElephant - An abstraction layer for git written in PHP |
|
4
|
|
|
* Copyright (C) 2013 Matteo Giachino |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License |
|
17
|
|
|
* along with this program. If not, see [http://www.gnu.org/licenses/]. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace GitElephant\Command; |
|
21
|
|
|
|
|
22
|
|
|
use \GitElephant\Objects\Branch; |
|
23
|
|
|
use \GitElephant\Objects\TreeishInterface; |
|
24
|
|
|
use \GitElephant\Objects\NodeObject; |
|
25
|
|
|
use \GitElephant\Repository; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* ls-tree command generator |
|
29
|
|
|
* |
|
30
|
|
|
* @author Matteo Giachino <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class LsTreeCommand extends BaseCommand |
|
33
|
|
|
{ |
|
34
|
|
|
const LS_TREE_COMMAND = 'ls-tree'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* constructor |
|
38
|
|
|
* |
|
39
|
|
|
* @param \GitElephant\Repository $repo The repository object this command |
|
40
|
|
|
* will interact with |
|
41
|
|
|
*/ |
|
42
|
15 |
|
public function __construct(Repository $repo = null) |
|
43
|
|
|
{ |
|
44
|
15 |
|
parent::__construct($repo); |
|
45
|
15 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* build a ls-tree command |
|
49
|
|
|
* |
|
50
|
|
|
* @param string|Branch $ref The reference to build the tree from |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \RuntimeException |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function fullTree($ref = 'HEAD') |
|
56
|
|
|
{ |
|
57
|
1 |
|
$what = $ref; |
|
58
|
1 |
|
if ($ref instanceof TreeishInterface) { |
|
59
|
1 |
|
$what = $ref->getSha(); |
|
60
|
|
|
} |
|
61
|
1 |
|
$this->clearAll(); |
|
62
|
1 |
|
$this->addCommandName(self::LS_TREE_COMMAND); |
|
63
|
|
|
// recurse |
|
64
|
1 |
|
$this->addCommandArgument('-r'); |
|
65
|
|
|
// show trees |
|
66
|
1 |
|
$this->addCommandArgument('-t'); |
|
67
|
1 |
|
$this->addCommandArgument('-l'); |
|
68
|
1 |
|
$this->addCommandSubject($what); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
1 |
|
return $this->getCommand(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* tree of a given path |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $ref reference |
|
77
|
|
|
* @param string|NodeObject $path path |
|
78
|
|
|
* |
|
79
|
|
|
* @throws \RuntimeException |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
15 |
|
public function tree($ref = 'HEAD', $path = null) |
|
83
|
|
|
{ |
|
84
|
15 |
|
if ($path instanceof NodeObject) { |
|
85
|
9 |
|
$subjectPath = $path->getFullPath() . ($path->isTree() ? '/' : ''); |
|
86
|
|
|
} |
|
87
|
|
|
else { |
|
88
|
15 |
|
$subjectPath = $path; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
15 |
|
$what = $ref; |
|
92
|
15 |
|
if ($ref instanceof TreeishInterface) { |
|
93
|
1 |
|
$what = $ref->getSha(); |
|
94
|
|
|
} |
|
95
|
15 |
|
$this->clearAll(); |
|
96
|
15 |
|
$this->addCommandName(self::LS_TREE_COMMAND); |
|
97
|
15 |
|
$this->addCommandArgument('-l'); |
|
98
|
15 |
|
$subject = $what; |
|
99
|
15 |
|
$this->addCommandSubject($subject); |
|
100
|
15 |
|
$this->addPath($subjectPath); |
|
101
|
|
|
|
|
102
|
15 |
|
return $this->getCommand(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* build ls-tree command that list all |
|
107
|
|
|
* |
|
108
|
|
|
* @param null|string $ref the reference to build the tree from |
|
109
|
|
|
* |
|
110
|
|
|
* @throws \RuntimeException |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function listAll($ref = null) |
|
114
|
|
|
{ |
|
115
|
1 |
|
if (is_null($ref)) { |
|
116
|
1 |
|
$ref = 'HEAD'; |
|
117
|
|
|
} |
|
118
|
1 |
|
$this->clearAll(); |
|
119
|
|
|
|
|
120
|
1 |
|
$this->addCommandName(self::LS_TREE_COMMAND); |
|
121
|
1 |
|
$this->addCommandSubject($ref); |
|
122
|
|
|
|
|
123
|
1 |
|
return $this->getCommand(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.