Skip to content

Instantly share code, notes, and snippets.

@fbrnc
Last active November 16, 2019 21:55
Show Gist options
  • Save fbrnc/5a037c31408f01a57553d8e3202774a1 to your computer and use it in GitHub Desktop.
Save fbrnc/5a037c31408f01a57553d8e3202774a1 to your computer and use it in GitHub Desktop.
git log to Elasticsearch
#/bin/bash
cd /var/hackathon/projects
for i in `find . -maxdepth 1 -mindepth 1 -type d`; do
project=`echo $i | sed 's/\.\///g'`
cd "/var/hackathon/projects/$project"
echo "Updating project $project"
git pull
php /var/hackathon/gitlog2elasticsearch.php http://localhost:9200/hackathon/commit/ "$project" '2 days ago'
done
<?php
if (empty($argv[1])) {
echo "Please provide Elasticsearch endpoint (e.g. http://localhost:9200/myindex/mytype/\n";
exit(1);
}
$elasticSearchEndpoint = rtrim($argv[1], '/') . '/';
$repo = null;
if (!empty($argv[2])) {
$repo = $argv[2];
}
$since = '2 days ago';
if (!empty($argv[3])) {
$since = $argv[3];
}
$output = [];
exec("git log --since='$since' --numstat --pretty=format:'Commit:%H%nAuthor:%an%nEmail:%ae%nDate:%ad%nMessage:%s%n'", $output);
$data = [];
$currentCommit = null;
foreach ($output as $line) {
if (trim($line) == '') continue;
if (preg_match('/^(-|\d+)\s+(-|\d+)\s+/', $line, $matches)) { // line generated by '--numstat'
$data[$currentCommit]['Insertions'] += intval($matches[1]);
$data[$currentCommit]['Deletions'] += intval($matches[2]);
} else { // line generated by '--pretty=format:...'
list($key, $value) = explode(':', $line, 2);
if ($key == 'Commit') {
$currentCommit = $value;
$data[$currentCommit]['Commit'] = $currentCommit;
$data[$currentCommit]['Insertions'] = 0;
$data[$currentCommit]['Deletions'] = 0;
if (!empty($repo)) {
$data[$currentCommit]['Repo'] = $repo;
}
} elseif ($key == 'Date') {
$data[$currentCommit]['@timestamp'] = date(DATE_ISO8601, strtotime($value));
} else {
$data[$currentCommit][$key] = $value;
}
}
}
// transfer data to Elasticsearch
foreach ($data as $commit => $item) {
$data_string = json_encode($item);
$ch = curl_init($elasticSearchEndpoint.$commit);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
echo "$result\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment