From f3278ec1db0eaae7dc6342c589555177ffeb5767 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Tue, 9 Oct 2018 22:38:58 +0100 Subject: [PATCH] Basic implementation for commit provider --- commits/__init__.py | 73 +++++++++++++++++++++++++++++++++++++++++++++ commits/__main__.py | 5 ++++ requirements.txt | 1 + 3 files changed, 79 insertions(+) create mode 100644 commits/__init__.py create mode 100644 requirements.txt diff --git a/commits/__init__.py b/commits/__init__.py new file mode 100644 index 0000000..83d8db9 --- /dev/null +++ b/commits/__init__.py @@ -0,0 +1,73 @@ +from datetime import datetime +from typing import List, NamedTuple, Optional +from os.path import basename, islink, isdir, join +from os import listdir + +import git # type: ignore + +# TODO do something smarter... later +# TODO def run against bitbucket and gh backups +SOURCES = [ + '***REMOVED***', + '***REMOVED***', + '***REMOVED***', + '***REMOVED***', + '***REMOVED***', + '***REMOVED***', +] + +THINGS = [ + '***REMOVED***', + '***REMOVED***', + '***REMOVED***', + '***REMOVED***', +] + +def by_me(actor): + aa = actor.email + " " + actor.name + if actor.email in ('***REMOVED***', '***REMOVED***@gmail.com'): + return True + if actor.name in ('***REMOVED***',): + return True + for thing in THINGS: + if thing in aa: + print("WARNING!!!", actor) + return True + return False + +class Commit(NamedTuple): + dt: datetime + message: str + repo: str + # TODO filter so they are authored by me + +def iter_commits(repo: str): + # TODO other branches? + rr = basename(repo) + gr = git.Repo(repo) + for c in gr.head.reference.log(): + if by_me(c.actor): + yield Commit( + dt=c.time, + message=c.message, # TODO strip off 'commit: '? (there are also 'merge') + repo=rr, + ) + +def is_git_repo(d: str): + dotgit = join(d, '.git') + return isdir(dotgit) + +# TODO eh. traverse all of filesystem?? or only specific dirs for now? +def iter_all_commits(): + for src in SOURCES: + # TODO warn if doesn't exist? + for d in listdir(src): + pr = join(src, d) + if is_git_repo(pr): + for c in iter_commits(pr): + yield c + + +def get_all_commits(): + ss = set(iter_all_commits()) + return list(sorted(ss, key=lambda c: c.dt)) diff --git a/commits/__main__.py b/commits/__main__.py index e69de29..ee2eee1 100644 --- a/commits/__main__.py +++ b/commits/__main__.py @@ -0,0 +1,5 @@ +from commits import iter_commits, iter_all_commits, get_all_commits + +# TODO cache? +for c in get_all_commits(): # ('***REMOVED***'): + print(c) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..59348f9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +gitpython