-
Notifications
You must be signed in to change notification settings - Fork 3
/
load-git-stats
executable file
·101 lines (91 loc) · 3.14 KB
/
load-git-stats
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
#!/usr/bin/env php
<?php
error_reporting(E_ALL);
set_error_handler(function ($severity, $message, $file, $line) {
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $file, $line);
}
});
$config = parse_ini_file(__DIR__ . '/config.ini');
$selfPath = realpath($argv[0]);
if (count($argv) == 1) {
$dir = sys_get_temp_dir() . '/' . uniqid('load-git-stats-', true);
echo "Cloning...\n";
passthru('git clone ' . escapeshellarg($config['git_url']) . ' ' . escapeshellarg($dir), $exitCode);
if (0 != $exitCode) {
echo 'Error while clone repo';
exit(1);
}
chdir($dir);
passthru(escapeshellcmd($selfPath) . ' --load-cwd', $exitCode);
if (0 != $exitCode) {
echo 'Error while reading stats';
exit(1);
}
echo "Cleaning directory\n";
passthru('rm -Rf ' . escapeshellarg($dir));
exit(0);
}
if (count($argv) == 2 && $argv[1] == '--load-cwd') {
echo "Reading git log...\n";
passthru('git log --ignore-all-space -l10000 -M --numstat --date=short --pretty="%x00%at%x00%H%x00%an%x00%s%x00" | ' . escapeshellcmd($selfPath) . ' --with-input', $exitCode);
exit($exitCode);
}
if (count($argv) != 2 || $argv[1] != '--with-input') {
echo 'Unknown error';
exit(1);
}
$pdo = new PDO("mysql:host={$config['mysql_host']}", $config['mysql_user'], $config['mysql_pass'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_AUTOCOMMIT => 0
]);
try {
$pdo->exec('CREATE DATABASE `' . $config['mysql_db'] . '`');
} catch (PDOException $e) { }
$pdo->exec('USE `' . $config['mysql_db'] . '`');
$pdo->exec('DROP TABLE IF EXISTS `files_committed`');
$pdo->exec('CREATE TABLE `files_committed` (date timestamp, hash text, author text, lines_added integer, lines_deleted integer, file text, message text)');
try {
$pdo->exec('CREATE VIEW `files_committed_clean` AS SELECT * FROM `files_committed`');
} catch (PDOException $e) { }
$stdin = fopen('php://stdin', 'rb');
$hash = null;
$date = null;
$author = null;
$message = null;
$batchSize = 0;
while (($line = fgets($stdin)) !== false) {
if (substr($line, 0, 1) == "\x00") {
if (substr_count($line, "\x00") != 5) {
echo 'Error while parsing header log: "' . $line . '"';
exit(1);
}
list($_, $date, $hash, $author, $message) = explode("\x00", $line);
continue;
}
$line = trim($line);
if ($line == '') {
continue;
}
if (! preg_match('/^(\d+|-)\s+(\d+|-)\s+(.*)$/', $line, $match)) {
echo 'Error while parsing file stat log: "' . $line . '"';
exit(1);
}
list($_, $linesAdded, $linesDeleted, $file) = $match;
$pdo->exec('insert into `files_committed` values (' . implode(', ', [
"FROM_UNIXTIME({$pdo->quote($date)})",
$pdo->quote($hash),
$pdo->quote($author),
$pdo->quote($linesAdded),
$pdo->quote($linesDeleted),
$pdo->quote($file),
$pdo->quote($message),
]) . ')');
$batchSize += 1;
if ($batchSize > 1000) {
$pdo->exec('COMMIT');
$batchSize = 0;
}
}
$pdo->exec('COMMIT');