-
Notifications
You must be signed in to change notification settings - Fork 14
/
AGitCommit.php
executable file
·224 lines (201 loc) · 4.05 KB
/
AGitCommit.php
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Represents a git commit.
*
* @author Charles Pick
* @author Jonas Girnatis <dermusterknabe@gmail.com>
* @package packages.git
*/
class AGitCommit extends CComponent {
/**
* The commit hash
* @var string
*/
public $hash;
/**
* The repository this commit is on
* @var AGitRepository
*/
public $repository;
/**
* The name of the commit author
* @var string
*/
protected $_authorName;
/**
* The commit author's email address
* @var string
*/
protected $_authorEmail;
/**
* The time of the commit
* @var string
*/
protected $_time;
/**
* The commit subject
* @var string
*/
protected $_subject;
/**
* The commit message
* @var string
*/
protected $_message;
/**
* The commit notes
* @var string
*/
protected $_notes;
/**
* Holds an array of files included in this commit
* @var array
*/
protected $_files;
/**
* Holds an array of parent commits
* @var AGitCommit[]
*/
protected $_parents;
/**
* Constructor
* @param AGitRepository $repository the git repository
*/
public function __construct($hash, AGitRepository $repository)
{
$this->hash = $hash;
$this->repository = $repository;
}
/**
* The name of the commit author
* @var string
*/
public function getAuthorName()
{
if(is_null($this->_authorName)){
$this->loadData();
}
return $this->_authorName;
}
/**
* The commit author's email address
* @var string
*/
public function getAuthorEmail()
{
if(is_null($this->_authorEmail)){
$this->loadData();
}
return $this->_authorEmail;
}
/**
* The time of the commit
* @var string
*/
public function getTime()
{
if(is_null($this->_time)){
$this->loadData();
}
return $this->_time;
}
/**
* The commit subject
* @var string
*/
public function getSubject()
{
if(is_null($this->_subject)){
$this->loadData();
}
return $this->_subject;
}
/**
* The commit message
* @var string
*/
public function getMessage()
{
if(is_null($this->_message)){
$this->loadData();
}
return $this->_message;
}
/**
* The commit notes
* @var string
*/
public function getNotes()
{
if(is_null($this->_notes)){
$this->loadData();
}
return $this->_notes;
}
/**
* Gets a list of parent commits
* @return AGitCommit[] array of parent commits
*/
public function getParents()
{
if ($this->_parents === null) {
$this->_parents = array();
$command = 'log --pretty=%P -n 1 '.$this->hash;
foreach(explode(' ',$this->repository->run($command)) as $commitHash) {
if (!empty($commitHash)) {
$this->_parents[$commitHash] = $this->repository->getCommit($commitHash);
}
}
}
return $this->_parents;
}
/**
* Gets a list of files affected by this commit
* @return array the files affected by this commit
*/
public function getFiles()
{
if ($this->_files === null) {
$command = 'show --pretty="format:" --name-only '.$this->hash;
foreach(explode("\n",$this->repository->run($command)) as $line) {
$this->_files[] = trim($line);
}
}
return $this->_files;
}
/**
* Retrieves the contents of a file at a specific commit. If binary, it will
* return the binary content.
*
* @param string $filename
* @return string
*/
public function getFileContents($filename)
{
$command = sprintf('show --raw %s:%s', $this->hash, $filename);
return $this->repository->run($command);
}
/**
* Loads the metadata for the commit
*/
protected function loadData()
{
$delimiter = "|||---|||---|||";
$command = 'show --pretty=format:"%an'.$delimiter.'%ae'.$delimiter.'%cd'.$delimiter.'%s'.$delimiter.'%B'.$delimiter.'%N" ' . $this->hash;
$response = $this->repository->run($command);
$parts = explode($delimiter,$response);
$this->_authorName = array_shift($parts);
$this->_authorEmail = array_shift($parts);
$this->_time = array_shift($parts);
$this->_subject = array_shift($parts);
$this->_message = array_shift($parts);
$this->_notes = array_shift($parts);
}
/**
* @return string commit hash
*/
public function __toString()
{
return $this->hash;
}
}