Skip to content

Commit acd2567

Browse files
committed
ref #17: git analyzer with get_log method
1 parent a641770 commit acd2567

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

modules/git_analyzer.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
class git_analyzer {
4+
5+
private $work_tree;
6+
7+
private $cmd;
8+
9+
private $workspace;
10+
11+
function __construct($workspace = null) {
12+
$this->cmd = new git_command($workspace);
13+
$this->workspace = $this->cmd->get_workspace();
14+
}
15+
16+
function set_work_tree($work_tree) {
17+
18+
$dir = getcwd();
19+
chdir("$this->workspace");
20+
if (is_dir("$work_tree")) {
21+
$this->work_tree = $work_tree;
22+
}
23+
chdir("$dir");
24+
}
25+
26+
function get_log() {
27+
28+
if (empty($this->work_tree)) {
29+
return null;
30+
}
31+
32+
$params = $this->cmd->prepare_params();
33+
$params->work_tree = $this->work_tree;
34+
$params->other = array(
35+
'--shortstat',
36+
'--no-merges',
37+
'--pretty=format:"[C:%H][A:%an][E:%ae][D:%at][%s]%n"',
38+
);
39+
40+
$response = $this->cmd->exec('log', $params);
41+
if (!$response) {
42+
return null;
43+
}
44+
preg_match_all('/\[C:([^\]]+)\]\[A:([^\]]+)\]\[E:([^\]]+)\]\[D:([^\]]+)\]\[([^\n]*)\]\n'.
45+
'\s*(\d+) files changed, (\d+) insertions\(\+\), (\d+) deletions\(-\)/i',
46+
$response, $matches, PREG_SET_ORDER);
47+
if (!$matches) {
48+
return null;
49+
}
50+
51+
$logs = array();
52+
foreach($matches as $match) {
53+
$log = new stdClass();
54+
$log->commit = $match[1];
55+
$log->author = $match[2];
56+
$log->email = $match[3];
57+
$log->date = $match[4];
58+
$log->subject = $match[5];
59+
$log->files = $match[6];
60+
$log->insertions = $match[7];
61+
$log->deletions = $match[8];
62+
$logs[$log->commit] = $log;
63+
}
64+
return $logs;
65+
}
66+
67+
function get_log_by_range() {
68+
}
69+
70+
function get_log_by_commit() {
71+
}
72+
73+
function get_detail_by_commit() {
74+
}
75+
}

modules/git_command.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ function __construct($workspace = null) {
1919
$this->workspace = $workspace;
2020
}
2121

22+
function get_workspace() {
23+
24+
return $this->workspace;
25+
}
26+
2227
function prepare_params() {
2328

2429
$param = new stdClass();
@@ -60,6 +65,7 @@ private function git_clone($param) {
6065
private function git_pull($param) {
6166

6267
if (!is_dir("$param->work_tree")) {
68+
clearstatcache();
6369
return false;
6470
}
6571

@@ -71,6 +77,7 @@ private function git_pull($param) {
7177
private function git_log($param) {
7278

7379
if (!is_dir("$param->work_tree")) {
80+
clearstatcache();
7481
return false;
7582
}
7683

0 commit comments

Comments
 (0)