2024-08-25 21:39:40 +00:00
|
|
|
import datetime
|
|
|
|
import logging
|
2024-08-25 21:41:42 +00:00
|
|
|
from collections.abc import Iterator
|
2024-08-25 21:39:40 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2024-08-25 21:41:42 +00:00
|
|
|
import git
|
|
|
|
|
|
|
|
from ..data import HIDDEN_LABEL_TOTAL, WorkSample
|
2024-08-25 21:39:40 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2024-08-25 21:41:42 +00:00
|
|
|
|
2024-08-25 21:39:40 +00:00
|
|
|
def determine_default_branch(repo: git.Repo):
|
|
|
|
try:
|
|
|
|
repo.commit('main')
|
|
|
|
return 'main'
|
|
|
|
except:
|
|
|
|
return 'master'
|
|
|
|
|
2024-08-25 21:41:42 +00:00
|
|
|
|
2024-08-25 21:39:40 +00:00
|
|
|
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
|
|
|
|
|
2024-08-25 21:41:42 +00:00
|
|
|
|
2024-08-25 21:39:40 +00:00
|
|
|
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)
|
2024-08-25 22:14:12 +00:00
|
|
|
|
2024-08-25 22:57:51 +00:00
|
|
|
authored_date = datetime.datetime.fromtimestamp(
|
2024-09-20 22:32:19 +00:00
|
|
|
commit.authored_date,
|
|
|
|
tz=datetime.UTC,
|
2024-08-25 22:57:51 +00:00
|
|
|
)
|
|
|
|
committed_date = datetime.datetime.fromtimestamp(
|
2024-09-20 22:32:19 +00:00
|
|
|
commit.committed_date,
|
|
|
|
tz=datetime.UTC,
|
2024-08-25 22:57:51 +00:00
|
|
|
)
|
2024-08-25 22:14:12 +00:00
|
|
|
|
2024-08-25 21:39:40 +00:00
|
|
|
yield WorkSample(
|
2024-08-25 22:57:51 +00:00
|
|
|
labels=tuple(labels),
|
|
|
|
start_at=None,
|
|
|
|
end_at=authored_date,
|
2024-08-25 21:39:40 +00:00
|
|
|
)
|
2024-08-25 22:14:12 +00:00
|
|
|
if authored_date != committed_date:
|
|
|
|
yield WorkSample(
|
2024-08-25 22:57:51 +00:00
|
|
|
labels=tuple(labels),
|
|
|
|
start_at=None,
|
|
|
|
end_at=committed_date,
|
2024-08-25 22:14:12 +00:00
|
|
|
)
|
2024-08-25 21:39:40 +00:00
|
|
|
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)
|