A Python library for listing Git commits from local repositories with advanced filtering options.
Install from PyPI:
pip install git-commits
Or using uv:
uv add git-commits
For development installation:
git clone https://github.com/theArjun/git-commits.git
cd git-commits
uv install -e .
from git_commits import list_git_commits, get_repo_authors
# List all commits from current repository
commits = list_git_commits(".")
# Print first few commits
for commit in commits[:3]:
print(f"{commit.short_sha} - {commit.author_name}: {commit.message}")
# List all unique authors in the repository
authors = get_repo_authors(".")
for author in authors:
print(f"{author.name} <{author.email}>")
list_git_commits(repo_path, **kwargs)List Git commits from a specified local repository with filtering options.
repo_path (str): The absolute or relative path to the local Git repositoryauthor (str, optional): Filter commits by authorโs name or email (case-insensitive partial matching)since (Union[str, datetime], optional): Filter commits authored after this dateuntil (Union[str, datetime], optional): Filter commits authored before this datetimezone (str, optional): Timezone string for interpreting string dates (defaults to โUTCโ)all_branches (bool, optional): If True, search across all branches (defaults to False)List of GitCommit objects with the following attributes:
sha (str): Full SHA-1 hash of the commitshort_sha (str): First 7 characters of the SHA-1 hashauthor_name (str): Name of the commit authorauthor_email (str): Email of the commit authorauthored_datetime (datetime): Timezone-aware datetime when the commit was authoredmessage (str): Full commit messageget_repo_authors(repo_path)Get a list of unique authors from a Git repository.
repo_path (str): The absolute or relative path to the local Git repositoryList of RepoAuthor objects with the following attributes:
name (str): Name of the authoremail (str): Email of the authorfrom git_commits import get_repo_authors
authors = get_repo_authors("/path/to/repo")
for author in authors:
print(f"{author.name} <{author.email}>")
from git_commits import list_git_commits
# List all commits
commits = list_git_commits("/path/to/repo")
# List commits from current directory
commits = list_git_commits(".")
# Filter by author name (case-insensitive)
commits = list_git_commits(".", author="john")
# Filter by email
commits = list_git_commits(".", author="[email protected]")
# Commits since a specific date
commits = list_git_commits(".", since="2024-01-01")
# Commits in the last week
commits = list_git_commits(".", since="1 week ago")
# Commits between dates
commits = list_git_commits(
".",
since="2024-01-01",
until="2024-12-31"
)
# Relative dates
commits = list_git_commits(".", since="yesterday")
commits = list_git_commits(".", since="2 months ago")
from datetime import datetime
import pytz
# Using timezone-aware datetime objects
eastern = pytz.timezone('America/New_York')
since_dt = eastern.localize(datetime(2024, 1, 1))
until_dt = datetime.now(eastern)
commits = list_git_commits(
".",
since=since_dt,
until=until_dt
)
# Interpret string dates in a specific timezone
commits = list_git_commits(
".",
since="2024-01-01 09:00:00",
timezone="America/New_York"
)
# Include commits from all branches (including remote-tracking)
commits = list_git_commits(".", all_branches=True)
# Combine multiple filters
commits = list_git_commits(
".",
author="john",
since="1 month ago",
until="now",
timezone="UTC",
all_branches=True
)
commits = list_git_commits(".")
for commit in commits:
print(f"SHA: {commit.sha}")
print(f"Short SHA: {commit.short_sha}")
print(f"Author: {commit.author_name} <{commit.author_email}>")
print(f"Date: {commit.authored_datetime}")
print(f"Message: {commit.message}")
print("-" * 50)
The library supports various date string formats:
"2024-01-01", "2024-01-01 15:30:00""yesterday", "today", "now""1 week ago", "2 months ago", "3 days ago"dateparserThe library gracefully handles various error conditions:
In case of errors, an informative message is printed and an empty list is returned.
# This will print an error message and return []
commits = list_git_commits("/invalid/path")
git clone https://github.com/theArjun/git-commits.git
cd git-commits
uv install -e ".[dev]"
python main.py
pytest
black src/ tests/
isort src/ tests/
mypy src/
This project is configured to be published using uv. Hereโs how to publish:
uv --version # Should be 1.0.1+
pyproject.toml:
[project]
version = "1.0.1" # Increment as needed
uv build
uv publish
More on publishing can be found here
uv run --with git-commits --no-project -- python -c "import git_commits"
MIT License - see LICENSE file for details.