forked from patrikf/glip
/
GitCommit.php
266 lines (233 loc) · 8 KB
/
GitCommit.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/*
* Copyright (C) 2008, 2009 Patrik Fimml, Sjoerd de Jong
*
* This file is part of glip.
*
* glip is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
* glip is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with glip. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Glip;
/**
* GitCommit resembles commit objects of a git repos
*
* The Array access allows manipulation of the tree inside the commit
*
* @package default
* @author The Young Shepherd
**/
class GitCommit extends GitObject implements \ArrayAccess, \IteratorAggregate, \Countable {
protected
$data = array(
'tree' => null, // (GitTree) The tree referenced by this commit
'parents' => array(), // (array of GitCommit) Parent commits of this commit
'author' => null, // (GitCommitStamp) The author of this commit
'committer' => null, // (GitCommitStamp) The committer of this commit
'summary' => "", // (string) Commit summary, i.e. the first line of the commit message
'detail' => "" // (string) Everything after the first line of the commit message
),
$commitHistory = null; // cache for history of this object
/**
* Constructor, takes extra arguments for lazy loading git objects
*
* @return void
* @author Sjoerd de Jong
**/
public function __construct($git, $sha = null) {
if ($git instanceof GitCommit || is_array($git)) {
//assume a new commit to be based on a previous commit
//$git represents the parents of the new object
$parents = is_array($git) ? $git : array($git);
$firstParent = $parents[0];
parent::__construct($firstParent->getGit());
$this->parents = $parents;
$this->tree = clone $firstParent->tree;
} else {
parent::__construct($git, $sha);
}
}
public function unserialize($data) {
$lines = explode("\n", $data);
unset($data);
$meta = array('parent' => array());
while (($line = array_shift($lines)) != '') {
$parts = explode(' ', $line, 2);
if (!isset($meta[$parts[0]]))
$meta[$parts[0]] = array($parts[1]);
else
$meta[$parts[0]][] = $parts[1];
}
$this->data['tree'] = new GitTree($this->git, $meta['tree'][0]);
$parents = array();
foreach ($meta['parent'] as $sha) {
$parents[] = new GitCommit($this->git, $sha);
}
$this->data['parents'] = $parents;
$this->data['author'] = new GitCommitStamp();
$this->data['author']->unserialize($meta['author'][0]);
$this->data['committer'] = new GitCommitStamp();
$this->data['committer']->unserialize($meta['committer'][0]);
$this->data['summary'] = array_shift($lines);
$this->data['detail'] = implode("\n", $lines);
}
public function setMessage($message) {
$message = explode("\n",$message,2);
$this->summary = isset($message[0]) ? $message[0] : "";
$this->detail = isset($message[1]) ? $message[1] : "";
}
protected function _serialize() {
$s = sprintf("tree %s\n", $this->tree->getSha()->hex());
foreach ($this->parents as $parent) {
$s .= sprintf("parent %s\n", $parent->getSha()->hex());
}
$s .= sprintf("author %s\n", $this->author->serialize());
$s .= sprintf("committer %s\n", $this->committer->serialize());
$s .= "\n";
$s .= $this->summary."\n".$this->detail;
return $s;
}
/**
* returns path of an object
*
* @param $commitTip The commit from where to start searching
* @return array of strings for each part of the path, empty array if not found
**/
public function getPath(GitPathObject $obj) {
return $this->tree->getPath($obj);
}
/**
* @brief Get commit history in topological order.
*
* @returns (array of GitCommit)
*/
public function getHistory(GitCommit $commitTip = null) {
if (is_null($this->commitHistory)) {
/* count incoming edges */
$inc = array();
$queue = array($this);
while (($commit = array_shift($queue)) !== NULL) {
foreach ($commit->parents as $parent) {
if (!isset($inc[(string)$parent])) {
$inc[(string)$parent] = 1;
$queue[] = $parent;
} else {
$inc[(string)$parent]++;
}
}
}
$queue = array($this);
$this->commitHistory = array();
while (($commit = array_pop($queue)) !== NULL) {
array_unshift($this->commitHistory, $commit);
foreach ($commit->parents as $parent) {
if (--$inc[(string)$parent] == 0) {
$queue[] = $parent;
}
}
}
}
return $this->commitHistory;
}
/**
* writes the object to disc
* also writes the subtree & parents to disk
*
* @return void
* @author The Young Shepherd
**/
public function write() {
if (is_null($this->author)) {
$this->author = new GitCommitStamp();
}
if (is_null($this->committer)) {
$this->committer = new GitCommitStamp();
}
if (!$this->exists()) {
$this->data['tree']->write();
foreach ($this->data['parents'] as $parent) {
$parent->write();
}
}
return parent::write();
}
public function __clone() {
parent::__clone();
$this->data['tree'] = clone $this->tree;
}
static public function treeDiff($a, $b) {
return GitTree::treeDiff($a ? $a->tree : NULL, $b ? $b->tree : NULL);
}
/**
* Returns if the supplied path exists (implements the ArrayAccess interface)
*
* @param string $index The relative path to the node
*
* @return bool true if the error exists, false otherwise
*/
public function offsetExists($path) {
return $this->tree->offsetExists($path);
}
/**
* Returns the node associated with the supplied path (implements the ArrayAccess interface).
*
* @param string $index The path of the object to get
*
* @return string
*/
public function offsetGet($path) {
return $this->tree->offsetGet($path);
}
/**
* Sets the object at path (implements the ArrayAccess interface).
*
* @param string $path
* @param string $object
*
*/
public function offsetSet($path, $object) {
if (!$object instanceof GitBlob) {
$blob = new GitBlob($this->git);
$blob->data = (string)$object;
$object = $blob;
}
$this->tree->offsetSet($path, $object);
}
/**
* Removes the node from the path recursively
*
* @param string $path
*/
public function offsetUnset($path) {
$this->tree->offsetUnset($path);
}
/**
* implements iterator interface
*
* @return void
* @author The Young Shepherd
**/
public function getIterator() {
return $this->tree->getIterator();
}
/**
* implements countable interface
*
* @return void
* @author The Young Shepherd
**/
public function count() {
return $this->tree->count();
}
public function getTypeName() {
return 'commit';
}
}