forked from hit-moodle/moodle-assignment-type_github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_analyzer.php
148 lines (116 loc) · 4.12 KB
/
git_analyzer.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
<?php
class git_analyzer {
private $worktree;
private $cmd;
private $workspace;
private static $_analyzers = array();
private function __construct($worktree, $workspace = null) {
$this->cmd = git_command::init($workspace);
$this->worktree = $worktree;
$this->workspace = $this->cmd->get_workspace();
}
public static function init($worktree, $workspace = null) {
if (empty($worktree)) {
return null;
}
if (empty(self::$_analyzers[$worktree])) {
self::$_analyzers[$worktree] = new git_analyzer($worktree, $workspace);
}
return self::$_analyzers[$worktree];
}
function worktree_exists() {
$dir = getcwd();
chdir("$this->workspace");
$result = is_dir("$this->worktree");
clearstatcache(true, "$this->worktree");
chdir("$dir");
return $result;
}
function pull($url = null) {
$params = $this->cmd->prepare_params();
$params->worktree = $this->worktree;
if (!empty($url)) {
$command = 'clone';
$params->url = $url;
} else {
$command = 'pull';
}
return $this->cmd->exec($command, $params);
}
function get_log($custom_params = array()) {
$params = $this->cmd->prepare_params();
$params->worktree = $this->worktree;
$default_params = array(
'--shortstat',
'--no-merges',
'--pretty=format:"[C:%H][A:%an]%n[E:%ae][D:%at][%s]%n"',
);
$params->other = array_merge($default_params, $custom_params);
$response = $this->cmd->exec('log', $params);
if (!$response) {
return null;
}
preg_match_all('/\[C:([^\]]+)\]\[A:([^\n]+)\]\n\[E:([^\]]+)\]\[D:([^\]]+)\]\[([^\n]*)\]\n'.
'\s*(\d+) files? changed,?\s*((\d+) insertions\(\+\))?,?\s*((\d+) deletions\(-\))?/i',
$response, $matches, PREG_SET_ORDER);
if (!$matches) {
return null;
}
$logs = array();
foreach($matches as $match) {
$log = new stdClass();
$log->commit = $match[1];
$log->author = $match[2];
$log->email = $match[3];
$log->date = $match[4];
$log->subject = $match[5];
$log->files = $match[6];
$log->insertions = $match[8] ? $match[8] : 0;
$log->deletions = $match[10] ? $match[10] : 0;
$logs[$log->commit] = $this->convert_encoding($log);
}
return $logs;
}
function get_branches() {
$params = $this->cmd->prepare_params();
$params->worktree = $this->worktree;
$output = $this->cmd->exec('branch', $params);
return array_filter(preg_replace('/[\s\*]/', '', explode("\n", $output)));
}
function get_remote() {
$params = $this->cmd->prepare_params();
$params->worktree = $this->worktree;
$params->other = array('-v');
$output = $this->cmd->exec('remote', $params);
$remote = array();
preg_match_all('/[^\s]+\s+([^\s]+)\s+\(fetch\)/', $output, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
$remote[] = $match[1];
}
return $remote;
}
function get_log_by_range($since = '', $until = '') {
$params = array();
if ($since) {
$params[] = "--since={$since}";
}
if ($until) {
$params[] = "--until={$until}";
}
return $this->get_log($params);
}
function show_commit() {
}
function delete() {
$params = $this->cmd->prepare_params();
$params->worktree = $this->worktree;
return $this->cmd->exec('delete', $params);
}
private function convert_encoding($log) {
// Try to change this to fit different conditions
$from_encoding = array('UTF-8, ASCII, EUC-CN');
$log->author = mb_convert_encoding($log->author, 'UTF-8', $from_encoding);
$log->subject = mb_convert_encoding($log->subject, 'UTF-8', $from_encoding);
return $log;
}
}