Skip to content

git_commits

Load and format Git commits from a local repository.

This module provides functionality to: - Load commits from a local Git repository. - Format commit information into a context string suitable for generating brag documents.

Classes:

Name Description
GitCommitsSource

A class to load Git commits from a local repository.

GitCommitsSource dataclass 🔗

Bases: DataSource[GitCommit]

A class to load Git commits from a local repository.

Attributes:

Name Type Description
path Path

A Path object representing the local repository.

author str

The username of the author whose commits are being fetched.

from_date datetime | None

An optional datetime object representing the start date for fetching commits.

to_date datetime | None

An optional datetime object representing the end date for fetching commits.

Source code in src/brag/sources/git_commits.py
@dataclass(frozen=True, slots=True)
class GitCommitsSource(DataSource[GitCommit]):
    """A class to load Git commits from a local repository.

    Attributes:
        path: A Path object representing the local repository.
        author: The username of the author whose commits are being fetched.
        from_date: An optional datetime object representing the start date for fetching commits.
        to_date: An optional datetime object representing the end date for fetching commits.
    """

    path: Path
    author: str
    from_date: datetime | None = None
    to_date: datetime | None = None

    def __iter__(self) -> Iterator[GitCommit]:
        return (self._repo.git.show(commit) for commit in self._commit_shas)

    def __len__(self) -> int:
        return len(self._commit_shas)

    @cached_property
    def _commit_shas(self) -> tuple[GitCommit, ...]:
        # Build kwargs for filtering commits
        kwargs: dict[str, Any] = {"author": self.author}

        # Add date filters if specified
        if self.from_date is not None:
            kwargs["since"] = self.from_date
        if self.to_date is not None:
            kwargs["until"] = self.to_date

        # Get commits for the specified author
        commits_iter = self._repo.iter_commits(**kwargs)
        return tuple(commit.hexsha for commit in commits_iter)

    @property
    def _repo(self) -> Repo:
        return Repo(self.path)