some old WIP on distinguishing timestamps + add test

This commit is contained in:
Dima Gerasimov 2019-04-05 23:07:30 +01:00
parent 47ec112a40
commit be7c0a1dc8
4 changed files with 33 additions and 14 deletions

View file

@ -41,13 +41,18 @@ def by_me(c):
return False return False
class Commit(NamedTuple): class Commit(NamedTuple):
dt: datetime commited_dt: datetime
authored_dt: datetime
message: str message: str
repo: str repo: str
sha: str sha: str
ref: Optional[str]=None ref: Optional[str]=None
# TODO filter so they are authored by me # TODO filter so they are authored by me
@property
def dt(self) -> datetime:
return self.commited_dt
# TODO not sure, maybe a better idea to move it to timeline? # TODO not sure, maybe a better idea to move it to timeline?
def fix_datetime(dt) -> datetime: def fix_datetime(dt) -> datetime:
# git module got it's own tzinfo object.. and it's pretty weird # git module got it's own tzinfo object.. and it's pretty weird
@ -62,13 +67,14 @@ from kython.ktyping import PathIsh
def iter_commits(repo: PathIsh, ref=None): def iter_commits(repo: PathIsh, ref=None):
# TODO other branches? # TODO other branches?
repo = Path(repo) repo = Path(repo)
rr = repo.stem rr = repo.name
gr = git.Repo(repo) gr = git.Repo(repo)
# without path might not handle pull heads properly # without path might not handle pull heads properly
for c in gr.iter_commits(rev=ref.path): for c in gr.iter_commits(rev=ref.path):
if by_me(c): if by_me(c):
yield Commit( yield Commit(
dt=fix_datetime(c.committed_datetime), # TODO authored?? commited_dt=fix_datetime(c.committed_datetime),
authored_dt=fix_datetime(c.authored_datetime),
message=c.message.strip(), message=c.message.strip(),
repo=rr, repo=rr,
sha=c.hexsha, sha=c.hexsha,
@ -76,7 +82,6 @@ def iter_commits(repo: PathIsh, ref=None):
) )
def iter_all_ref_commits(repo: Path): def iter_all_ref_commits(repo: Path):
# TODO hmm, git library has got way of determining git..
gr = git.Repo(str(repo)) gr = git.Repo(str(repo))
for r in gr.references: for r in gr.references:
yield from iter_commits(repo=repo, ref=r) yield from iter_commits(repo=repo, ref=r)
@ -86,12 +91,18 @@ def is_git_repo(d: str):
dotgit = join(d, '.git') dotgit = join(d, '.git')
return isdir(dotgit) return isdir(dotgit)
from git.repo.fun import is_git_dir # type: ignore
def iter_all_git_repos(dd: PathIsh) -> Iterator[Path]: def iter_all_git_repos(dd: PathIsh) -> Iterator[Path]:
# TODO would that cover all repos??? # TODO would that cover all repos???
dd = Path(dd) dd = Path(dd)
for xx in dd.glob('**/refs/heads/'): for xx in dd.glob('**/HEAD'): # ugh
yield xx.parent.parent c = xx.parent
if not is_git_dir(c):
continue
if c.name == '.git':
c = c.parent
yield c
# TODO is it only used in wcommits? # TODO is it only used in wcommits?
@ -125,3 +136,12 @@ def get_all_commits():
res[c.sha] = min(nn, c, key=lambda c: c.sha) res[c.sha] = min(nn, c, key=lambda c: c.sha)
return list(sorted(res.values(), key=lambda c: c.dt)) return list(sorted(res.values(), key=lambda c: c.dt))
def main():
for c in get_all_commits(): # ('***REMOVED***'):
print(c)
if __name__ == '__main__':
main()

View file

@ -1,5 +0,0 @@
from commits import iter_commits, iter_all_commits, get_all_commits
# TODO cache?
for c in get_all_commits(): # ('***REMOVED***'):
print(c)

7
commits/test.py Normal file
View file

@ -0,0 +1,7 @@
from . import get_all_commits
# TODO shit. why can't it just be in __init__.py??
def test():
commits = get_all_commits()
assert len(commits) > 10

3
run
View file

@ -1,3 +0,0 @@
#!/bin/bash
set -eu
python3 -m commits