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\NodeObject; |
24
|
|
|
use GitElephant\Objects\TreeishInterface; |
25
|
|
|
use GitElephant\Repository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* cat-file command generator |
29
|
|
|
* |
30
|
|
|
* @author Matteo Giachino <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class CatFileCommand extends BaseCommand |
33
|
|
|
{ |
34
|
|
|
public const GIT_CAT_FILE = 'cat-file'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* constructor |
38
|
|
|
* |
39
|
|
|
* @param \GitElephant\Repository $repo The repository object this command |
40
|
|
|
* will interact with |
41
|
|
|
*/ |
42
|
2 |
|
public function __construct(Repository $repo = null) |
43
|
|
|
{ |
44
|
2 |
|
parent::__construct($repo); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* command to show content of a Object at a given Treeish point |
49
|
|
|
* |
50
|
|
|
* @param \GitElephant\Objects\NodeObject $object a Object instance |
51
|
|
|
* @param \GitElephant\Objects\TreeishInterface|string $treeish an object with TreeishInterface interface |
52
|
|
|
* |
53
|
|
|
* @throws \RuntimeException |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
2 |
|
public function content(NodeObject $object, $treeish): string |
57
|
|
|
{ |
58
|
2 |
|
$this->clearAll(); |
59
|
2 |
|
$sha = $treeish instanceof TreeishInterface ? $treeish->getSha() : $treeish; |
60
|
2 |
|
$this->addCommandName(static::GIT_CAT_FILE); |
61
|
|
|
// pretty format |
62
|
2 |
|
$this->addCommandArgument('-p'); |
63
|
2 |
|
$this->addCommandSubject($sha . ':' . $object->getFullPath()); |
64
|
|
|
|
65
|
2 |
|
return $this->getCommand(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* output an object content given it's sha |
70
|
|
|
* |
71
|
|
|
* @param string $sha |
72
|
|
|
* |
73
|
|
|
* @throws \RuntimeException |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function contentBySha($sha): string |
77
|
|
|
{ |
78
|
|
|
$this->clearAll(); |
79
|
|
|
$this->addCommandName(static::GIT_CAT_FILE); |
80
|
|
|
$this->addCommandArgument('-p'); |
81
|
|
|
$this->addCommandSubject($sha); |
82
|
|
|
|
83
|
|
|
return $this->getCommand(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|