Repository navigation
Expand file tree
/
Copy pathCommitLoad_Raw.class.php
More file actions
143 lines (128 loc) · 3.23 KB
/
Copy pathCommitLoad_Raw.class.php
File metadata and controls
143 lines (128 loc) · 3.23 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
<?php
/**
* Commit load strategy using raw git objects
*
* @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2012 Christopher Han
* @package GitPHP
* @subpackage Git\Commit
*/
class GitPHP_CommitLoad_Raw extends GitPHP_CommitLoad_Base
{
/**
* Object loader
*
* @var GitPHP_GitObjectLoader
*/
protected $objectLoader;
/**
* Constructor
*
* @param GitPHP_GitObjectLoader $objectLoader object loader
* @param GitPHP_GitExe $exe git exe
*/
public function __construct($objectLoader, $exe)
{
if (!$objectLoader)
throw new Exception('Git object loader is required');
$this->objectLoader = $objectLoader;
parent::__construct($exe);
}
/**
* Gets the data for a commit
*
* @param GitPHP_Commit $commit commit
* @return array commit data
*/
public function Load($commit)
{
if (!$commit)
return;
$abbreviatedHash = null;
$tree = null;
$parents = array();
$author = null;
$authorEpoch = null;
$authorTimezone = null;
$committer = null;
$committerEpoch = null;
$committerTimezone = null;
$title = null;
$comment = array();
$data = $this->objectLoader->GetObject($commit->GetHash());
if (empty($data))
return;
$lines = explode("\n", $data);
$linecount = count($lines);
$i = 0;
$encoding = null;
/* Commit header */
for ($i = 0; $i < $linecount; $i++) {
$line = $lines[$i];
if (preg_match('/^tree ([0-9a-fA-F]{40})$/', $line, $regs)) {
/* Tree */
$tree = $regs[1];
} else if (preg_match('/^parent ([0-9a-fA-F]{40})$/', $line, $regs)) {
/* Parent */
$parents[] = $regs[1];
} else if (preg_match('/^author (.*) ([0-9]+) (.*)$/', $line, $regs)) {
/* author data */
$author = $regs[1];
$authorEpoch = $regs[2];
$authorTimezone = $regs[3];
} else if (preg_match('/^committer (.*) ([0-9]+) (.*)$/', $line, $regs)) {
/* committer data */
$committer = $regs[1];
$committerEpoch = $regs[2];
$committerTimezone = $regs[3];
} else if (preg_match('/^encoding (.+)$/', $line, $regs)) {
$gitEncoding = trim($regs[1]);
if ((strlen($gitEncoding) > 0) && function_exists('mb_list_encodings')) {
$supportedEncodings = mb_list_encodings();
$encIdx = array_search(strtolower($gitEncoding), array_map('strtolower', $supportedEncodings));
if ($encIdx !== false) {
$encoding = $supportedEncodings[$encIdx];
}
}
$encoding = trim($regs[1]);
} else if (strlen($line) == 0) {
break;
}
}
/* Commit body */
for ($i += 1; $i < $linecount; $i++) {
$trimmed = trim($lines[$i]);
if ((strlen($trimmed) > 0) && (strlen($encoding) > 0) && function_exists('mb_convert_encoding')) {
$trimmed = mb_convert_encoding($trimmed, 'UTF-8', $encoding);
}
if (empty($title) && (strlen($trimmed) > 0))
$title = $trimmed;
if (!empty($title)) {
if ((strlen($trimmed) > 0) || ($i < ($linecount-1)))
$comment[] = $trimmed;
}
}
return array(
$abbreviatedHash,
$tree,
$parents,
$author,
$authorEpoch,
$authorTimezone,
$committer,
$committerEpoch,
$committerTimezone,
$title,
$comment
);
}
/**
* Whether this load strategy loads the abbreviated hash
*
* @return boolean
*/
public function LoadsAbbreviatedHash()
{
return false;
}
}