| <?php |
| /******************************************************************************* |
| * Copyright (c) 2011, 2012 Eclipse Foundation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Wayne Beaton (Eclipse Foundation)- initial API and implementation |
| *******************************************************************************/ |
| require_once dirname(__FILE__) . '/git-functions.inc'; |
| |
| class GitCommitProcessor extends GitLogProcessor { |
| var $record; |
| |
| function __construct() { |
| } |
| |
| function startCommit($ref) { |
| $this->record = array('size' => 0); |
| } |
| |
| function data($ref, $key, $value) { |
| $this->record[$key] = $value; |
| } |
| |
| function file($ref, $filename, $filetype, $size) { |
| $this->record['files'][] = array( |
| 'name' => $filename, |
| 'type' => $filetype, |
| 'size' => $size |
| ); |
| } |
| |
| function endCommit($ref) { |
| print_r($this->record); |
| $committer = $this->record['committer']; |
| $author = $this->record['author']; |
| |
| $login = getCommitterId($author); |
| if (!$login) $login = getCommitterId($committer); |
| if (!$login) $login = $this->record['committer']; |
| |
| $year = date('Y', $this->record['date']); |
| $yearmonth = date('Ym', $this->record['date']); |
| $yearmonthday = date('Ymd', $this->record['date']); |
| $date = date("Y/m/d", $this->record['date']); |
| |
| $top = $this->record['top']; |
| $project = $this->record['project']; |
| |
| $company = getCommitterCompany($this->record['committer']); |
| if (!$company) $company = 'unknown'; |
| |
| foreach($this->record['files'] as $file) { |
| $filename = $file['name']; |
| $filetype = $file['type']; |
| $change_size = $file['size']; |
| $message_size = strlen($file['comment']); |
| } |
| echo "$date\t$year\t$yearmonth\t$yearmonthday\t$top\t$project\t$filename\t$filetype\t$commit\t$change_size\t$message_size\t$login\t$company\n"; |
| |
| } |
| } |
| |
| /** |
| * Create the gitlogcache table if it does not already exist. |
| * @internal |
| */ |
| function ensureGitLogCacheTableExists() { |
| $sql = " |
| create table if not exists gitlogcache ( |
| repo varchar(128) not null, |
| ref varchar(40) not null, |
| committer varchar(50) not null, |
| author varchar(128) not null, |
| authorName varchar(128), |
| size int unsigned, |
| comment varchar(1024) not null, |
| commitDate datetime not null, |
| CONSTRAINT UNIQUE (repo, ref), |
| INDEX (repo), |
| INDEX (ref), |
| INDEX (commitDate)) ENGINE=InnoDB; |
| "; |
| dashboard_sql($sql); |
| } |
| |
| function ensureGitLogCacheLogTableExists() { |
| $sql = " |
| create table if not exists gitlogcachelog ( |
| repo varchar(128) not null, |
| updateDate datetime not null, |
| INDEX (repo)) ENGINE=InnoDB; |
| "; |
| dashboard_sql($sql); |
| } |
| |
| /** |
| * @internal |
| * @param string $repo file path of a Git repository |
| * @return int The date of the most recent commit |
| */ |
| function findLastCommitDate($repo) { |
| $sql = "select max(updateDate) as date from gitlogcachelog where repo='$repo'"; |
| $result = dashboard_sql($sql); |
| $row = mysql_fetch_assoc($result); |
| |
| // The result will either be a date, or null |
| // strtotime(null) == the beginning of time |
| return strtotime($row['date']); |
| } |
| |
| /** |
| * This function updates the cache with information extracted from the |
| * repository. Scans the repository for refs added after the date of the |
| * most recent commit we already have in the cache. There is a risk that |
| * some commits may be lost if they are pushed after the cache is updated |
| * with commits that occurred more recently. |
| * |
| * @param string $repo file path of a Git repository |
| */ |
| function updateGitLogCache($repo, $fullscan = false) { |
| ensureGitLogCacheTableExists(); |
| ensureGitLogCacheLogTableExists(); |
| |
| $currentDate = date('Y-m-d H:i:s'); |
| |
| $date = $fullscan ? null : findLastCommitDate($repo); |
| $since = $date ? '--since="' . date('Y-m-d H:i:s', $date) .'"' : ''; |
| $path = escapeshellcmd($repo); |
| $gitCommand = "/usr/local/bin/git --git-dir=$path log --branches --numstat --format=fuller $since --reverse"; |
| logMessage("Git", $gitCommand); |
| $handle = popen($gitCommand, 'r'); |
| parseGitLog($handle, new GitAuthorProcessor($repo)); |
| pclose($handle); |
| |
| // Log the current date (as captured before we started the scan) |
| $sql = "insert into gitlogcachelog (repo, updateDate) values ('$repo', '$currentDate')"; |
| dashboard_sql($sql); |
| } |
| ?> |