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\TreeishInterface; |
24
|
|
|
use GitElephant\Repository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Diff command generator |
28
|
|
|
* |
29
|
|
|
* @author Matteo Giachino <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class DiffCommand extends BaseCommand |
32
|
|
|
{ |
33
|
|
|
public const DIFF_COMMAND = 'diff'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* constructor |
37
|
|
|
* |
38
|
|
|
* @param \GitElephant\Repository $repo The repository object this command |
39
|
|
|
* will interact with |
40
|
|
|
*/ |
41
|
3 |
|
public function __construct(Repository $repo = null) |
42
|
|
|
{ |
43
|
3 |
|
parent::__construct($repo); |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* build a diff command |
48
|
|
|
* |
49
|
|
|
* @param TreeishInterface $of the reference to diff |
50
|
|
|
* @param TreeishInterface|null $with the source reference to diff with $of, if not specified is the current HEAD |
51
|
|
|
* @param string|null $path the path to diff, if not specified the full repository |
52
|
|
|
* |
53
|
|
|
* @throws \RuntimeException |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
3 |
|
public function diff($of, $with = null, $path = null): string |
57
|
|
|
{ |
58
|
3 |
|
$this->clearAll(); |
59
|
3 |
|
$this->addCommandName(self::DIFF_COMMAND); |
60
|
|
|
// Instead of the first handful of characters, show the full pre- and post-image blob object names on the |
61
|
|
|
// "index" line when generating patch format output |
62
|
3 |
|
$this->addCommandArgument('--full-index'); |
63
|
3 |
|
$this->addCommandArgument('--no-color'); |
64
|
|
|
// Disallow external diff drivers |
65
|
3 |
|
$this->addCommandArgument('--no-ext-diff'); |
66
|
|
|
// Detect renames |
67
|
3 |
|
$this->addCommandArgument('-M'); |
68
|
3 |
|
$this->addCommandArgument('--dst-prefix=DST/'); |
69
|
3 |
|
$this->addCommandArgument('--src-prefix=SRC/'); |
70
|
|
|
|
71
|
3 |
|
$subject = ''; |
72
|
|
|
|
73
|
3 |
|
if (is_null($with)) { |
74
|
3 |
|
$subject .= $of . '^..' . $of; |
75
|
|
|
} else { |
76
|
1 |
|
$subject .= $with . '..' . $of; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
if (!is_null($path)) { |
80
|
1 |
|
if (!is_string($path)) { |
81
|
|
|
/** @var Object $path */ |
82
|
|
|
$path = $path->getPath(); |
83
|
|
|
} |
84
|
1 |
|
$this->addPath($path); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
$this->addCommandSubject($subject); |
88
|
|
|
|
89
|
3 |
|
return $this->getCommand(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.