1
0
This repository has been archived on 2024-10-13. You can view files and clone it, but cannot push or open issues or pull requests.
git-time-tracker/git_time_tracker/source/git_repo.py
Jon Michael Aanes 41e574c971
All checks were successful
Test Python / Test (push) Successful in 22s
Ruff
2024-08-25 23:41:42 +02:00

56 lines
1.5 KiB
Python

import datetime
import logging
from collections.abc import Iterator
from pathlib import Path
import git
from ..data import HIDDEN_LABEL_TOTAL, WorkSample
logger = logging.getLogger(__name__)
def determine_default_branch(repo: git.Repo):
try:
repo.commit('main')
return 'main'
except:
return 'master'
def determine_project_name(repo: git.Repo) -> str:
remotes = repo.remotes
if len(remotes) > 0:
return remotes.origin.url.removeprefix('git@gitfub.space:')
return Path(repo.working_tree_dir).name
def get_samples_from_project(repo: git.Repo) -> Iterator[WorkSample]:
project_name = determine_project_name(repo)
assert project_name is not None
# TODO: Branch on main or master or default
repo.commit()
for commit in repo.iter_commits(determine_default_branch(repo)):
labels = [HIDDEN_LABEL_TOTAL]
labels.append('project:' + project_name)
labels.append('author:' + commit.author.email)
yield WorkSample(
datetime.datetime.fromtimestamp(commit.authored_date, tz=datetime.UTC),
tuple(labels),
)
yield WorkSample(
datetime.datetime.fromtimestamp(commit.committed_date, tz=datetime.UTC),
tuple(labels),
)
del labels
def iterate_samples_from_git_repository(repo_path: Path) -> Iterator[WorkSample]:
try:
yield from get_samples_from_project(git.Repo(repo_path))
except git.exc.InvalidGitRepositoryError:
logger.warning('Ignoring non-repo %s', repo_path)