|
| 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 | +} |
0 commit comments