-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathGit.php
More file actions
146 lines (119 loc) · 4.35 KB
/
Git.php
File metadata and controls
146 lines (119 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace TryLib\RepoManager;
use TryLib\RepoManager;
use RuntimeException;
class Git implements RepoManager {
protected $repo_path;
protected $cmd_runner;
protected $branch;
protected $remote;
private $remote_branch;
public function __construct($repo_path, $cmd_runner) {
$this->repo_path = $repo_path;
$this->cmd_runner = $cmd_runner;
$this->branch = null;
$this->remote = null;
$this->remote_branch = null;
}
function cleanRef($ref) {
return rtrim(str_replace('refs/heads/', '', $ref));
}
function parseLocalBranch() {
$this->cmd_runner->chdir($this->repo_path);
$ret = $this->cmd_runner->run('git rev-parse --abbrev-ref HEAD', true, true);
$output = $this->cmd_runner->getOutput();
if ($ret || $output === 'HEAD') {
return '';
} else {
return $this->cleanRef($output);
}
}
function getLocalBranch() {
if (is_null($this->branch)) {
$this->branch = $this->parseLocalBranch();
}
return $this->branch;
}
function getConfig($prop) {
$this->cmd_runner->chdir($this->repo_path);
$ret = $this->cmd_runner->run("git config '$prop'", true, true);
if ($ret) {
return null;
} else {
return $this->cleanRef($this->cmd_runner->getOutput());
}
}
function getRemote($default=null) {
if (is_null($this->remote)) {
$branch = $this->getLocalBranch();
$this->remote = $this->getConfig("branch.$branch.remote");
}
if (is_null($this->remote) && !is_null($default)) {
$this->remote = $default;
}
return $this->remote;
}
function setRemoteBranch($remote_branch) {
$this->remote_branch = $remote_branch;
}
function getRemoteBranch() {
if (!is_null($this->remote_branch)) {
return $this->remote_branch;
}
$local_branch = $this->getLocalBranch();
$this->remote_branch = $this->getConfig("branch.$local_branch.merge");
if (is_null($this->remote_branch)) {
// try to see if a remote branch exists with the same name as the local branch
$remote_url = $this->getConfig('remote.origin.url');
$cmd = 'git ls-remote --exit-code ' . $remote_url . ' refs/heads/' . $local_branch;
$ret = $this->cmd_runner->run($cmd, true, true);
if ($ret === 0) {
$this->remote_branch = $local_branch;
} else {
throw new RuntimeException(
"No remote branch was found corresponding to the local branch $local_branch!"
. " You may want to specify a remote branch from the command line with --branch.");
}
}
$this->cmd_runner->info("Diffing against remote branch {$this->remote_branch}.");
return $this->remote_branch;
}
function getUpstream() {
$remote = $this->getRemote("origin");
$remote_branch = $this->getRemoteBranch();
return $remote . "/" . $remote_branch;
}
function generateDiff($staged_only = false, $safelist = null, $lines_of_context = false) {
$patch = $this->repo_path . "/patch.diff";
$args = [
"--binary",
"--no-color",
$this->getUpstream(),
];
if ($staged_only) {
$args[] = "--staged";
}
# User is requesting additional lines of context around changes.
if ($lines_of_context) {
$args[] = "-U{$lines_of_context}";
}
if (is_array($safelist)) {
$args[] = implode(' ', $safelist);
}
$this->cmd_runner->chdir($this->repo_path);
$ret = $this->cmd_runner->run('git -c diff.noprefix=false diff ' . implode(' ', $args) . ' > ' . $patch, false, true);
if ($ret) {
$this->cmd_runner->terminate(
'An error was encountered generating the diff - run \'git fetch\' and try again'
);
}
return $patch;
}
function runPreChecks(array $pre_checks) {
$upstream = $this->getUpstream();
foreach ($pre_checks as $c) {
$this->cmd_runner->chdir($this->repo_path);
$c->check($this->cmd_runner, $this->repo_path, $upstream);
}
}
}