blob: 8f91038281152201c5a360363804abb7c01aa1ad [file] [log] [blame]
<?php
/*******************************************************************************
* Copyright (c) 2013 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__) . '/../common/git-functions.inc';
class GitLogCapture extends GitLogProcessor {
var $project;
var $path;
var $record;
function __construct($project, $path) {
$this->project = $project;
$this->path = $path;
$this->createTables();
$sql = "insert ignore into GitRepo (project, path) values ('$project', '$path')";
$this->execute($sql);
;
}
function startCommit($ref) {
echo "\n#Commit: $ref\n";
$sql = "insert ignore into GitCommit (path, ref) values ('$this->path', '$ref')";
$this->execute($sql);
$this->record = array();
}
function data($ref, $key, $value) {
$this->record[$key] = $value;
}
function addFile($ref, $filename, $added, $removed) {
$filename = addSlashes($filename);
$sql = "insert ignore into GitCommitFile (path, ref, file, added, removed) values ('$this->path', '$ref', '$filename', '$added', '$removed')";
$this->execute($sql);
}
function endCommit($ref) {
$date = date('Y-m-d H:i:s', $this->record['authorDate']);
$comment = $this->summarizeComment($this->record['comment']);
$comment = addSlashes($comment);
$changeId = $this->getChangeId($comment);
$sql = "
update GitCommit
set date='$date', comment='$comment', changeId=$changeId
where path='$this->path' and ref='$ref'
";
$this->execute($sql);
foreach($this->getAuthors() as $name => $email) {
$sql = "insert ignore into GitCommitAuthor (path, ref, name, email) values ('$this->path', '$ref', '$name', '$email')";
$this->execute($sql);
}
}
function getAuthors() {
$authors = array();
$name = @$this->record['authorName'];
$email = @$this->record['author'];
$authors[addSlashes($name)] = $email;
if (preg_match_all('/Also-By:(.+)<([^>]+)>/i', $this->record['comment'], $matches)) {
for($count=0; $count<count($matches[0]); $count++) {
$name = trim($matches[1][$count]);
$email = trim($matches[2][$count]);
$authors[addSlashes($name)] = $email;
}
}
return $authors;
}
function summarizeComment($comment) {
$lines = explode("\n", $comment);
return $lines[0];
}
/**
* Get the Gerrit change id from the commit comment (if it has been specified).
*/
function getChangeId($comment) {
if (!preg_match('/Change\-Id: (\w+)/', $comment, $matches)) return 'NULL';
return $matches[1];
}
function execute($sql) {
$sql = preg_replace('/\s+/', ' ', trim($sql));
echo "$sql;\n";
}
function createTables() {
$this->createGitRepoTable();
$this->createGitCommitTable();
$this->createGitCommitFileTable();
$this->createGitCommitAuthorTable();
}
function createGitRepoTable() {
$sql = "
create table if not exists GitRepo (
project varchar(128) not null,
path varchar(128) not null,
CONSTRAINT UNIQUE (project, path),
INDEX (project),
INDEX (path)) ENGINE=InnoDB
";
$this->execute($sql);
}
function createGitCommitTable() {
$sql = "
create table if not exists GitCommit (
path varchar(128) not null,
ref varchar(40) not null,
changeId varchar(60),
date datetime not null,
comment varchar(1024) not null,
CONSTRAINT UNIQUE (path, ref),
INDEX (path),
INDEX (ref)) ENGINE=InnoDB
";
$this->execute($sql);
}
function createGitCommitFileTable() {
$sql = "
create table if not exists GitCommitFile (
path varchar(128) not null,
ref varchar(40) not null,
file varchar(128) not null,
added int not null,
removed int not null,
CONSTRAINT UNIQUE (path, ref, file),
INDEX (path),
INDEX (ref)) ENGINE=InnoDB
";
$this->execute($sql);
}
function createGitCommitAuthorTable() {
$sql = "
create table if not exists GitCommitAuthor (
path varchar(128) not null,
ref varchar(40) not null,
name varchar(128) not null,
email varchar(128) not null,
CONSTRAINT UNIQUE (path, ref, email),
INDEX (path),
INDEX (ref),
INDEX (name),
INDEX (email)) ENGINE=InnoDB
";
$this->execute($sql);
}
}
?>