2019-10-24 02:20:38 +02:00
|
|
|
import datetime
|
|
|
|
from git import Repo
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import click
|
|
|
|
|
|
|
|
from elasticsearch import Elasticsearch
|
|
|
|
|
|
|
|
|
|
|
|
class GitLogger:
|
|
|
|
"""to provide a log as dict of commits which are json printable"""
|
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
"""Create a GitStepper with the path to the git repository (not a bare repository)"""
|
|
|
|
self.repo = Repo(path)
|
|
|
|
|
|
|
|
def log(self):
|
|
|
|
"""return a dict of commits"""
|
|
|
|
commits = (self.repo.commit(logEntry) for logEntry in self.repo.iter_commits())
|
|
|
|
return (self.to_dict(x) for x in commits)
|
|
|
|
|
2023-04-26 09:23:56 +02:00
|
|
|
def to_dict(self, commit):
|
2019-10-24 02:20:38 +02:00
|
|
|
"""create a dict out of a commit that is easy to json serialize"""
|
|
|
|
time_difference = commit.authored_datetime - commit.committed_datetime
|
|
|
|
return {
|
2023-04-26 09:23:56 +02:00
|
|
|
"author_email": commit.author.email,
|
|
|
|
"author_name": commit.author.name,
|
|
|
|
"authored_date": commit.authored_datetime.isoformat(),
|
|
|
|
"files": [
|
|
|
|
{"file": file, "changes": changes}
|
|
|
|
for file, changes in commit.stats.files.items()
|
|
|
|
],
|
|
|
|
"committed_date": commit.committed_datetime.isoformat(),
|
|
|
|
"committer_email": commit.committer.email,
|
|
|
|
"committer_name": commit.committer.name,
|
|
|
|
"date_difference_in_seconds": abs(time_difference.total_seconds()),
|
|
|
|
"encoding": commit.encoding,
|
|
|
|
"hash": commit.hexsha,
|
|
|
|
"message": commit.message,
|
|
|
|
"summary": commit.summary,
|
|
|
|
"size": commit.size,
|
|
|
|
"stats_total": commit.stats.total,
|
|
|
|
"parents": [parent.hexsha for parent in commit.parents],
|
2019-10-24 02:20:38 +02:00
|
|
|
}
|
|
|
|
|
2023-04-26 09:23:56 +02:00
|
|
|
|
2019-10-24 02:20:38 +02:00
|
|
|
@click.command()
|
2023-04-26 09:23:56 +02:00
|
|
|
@click.argument("path", type=click.Path(exists=True), envvar="PWD")
|
2019-10-24 02:20:38 +02:00
|
|
|
@click.option("--host")
|
|
|
|
@click.option("--index")
|
|
|
|
@click.option("--port", default=9200)
|
|
|
|
def main(path, host, index, port):
|
|
|
|
if host:
|
|
|
|
if not index:
|
|
|
|
print("--index need to be set")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
print("Sending commits from %s to %s on index %s" % (path, host, index))
|
2023-04-26 09:23:56 +02:00
|
|
|
es = Elasticsearch([{"host": host, "port": port}])
|
2019-10-24 02:20:38 +02:00
|
|
|
for entry in GitLogger(path).log():
|
|
|
|
try:
|
2023-04-26 09:23:56 +02:00
|
|
|
es.index(index=index, doc_type="commit", id=entry["hash"], body=entry)
|
2019-10-24 02:20:38 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(json.dumps(entry))
|
|
|
|
print(e.info)
|
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
for entry in GitLogger(path).log():
|
2023-04-26 09:23:56 +02:00
|
|
|
print(json.dumps(entry))
|
|
|
|
|
2019-10-24 02:20:38 +02:00
|
|
|
|
2023-04-26 09:23:56 +02:00
|
|
|
if __name__ == "__main__":
|
2019-10-24 02:20:38 +02:00
|
|
|
main()
|