Skip to main content

A Python library for listing Git commits from local repositories with advanced filtering options

Project description

Git Commits Library

A Python library for listing Git commits from local repositories with advanced filtering options.

Features

  • 📝 List commits from any local Git repository
  • 👤 Filter commits by author (name or email, case-insensitive)
  • 📅 Filter commits by date range with flexible date parsing
  • 🌍 Timezone support for date filtering
  • 🌿 Support for searching across all branches
  • 🛡️ Robust error handling
  • 📦 Clean dataclass-based return objects

Installation

Install from PyPI:

pip install git-commits

Or using uv:

uv add git-commits

For development installation:

git clone https://github.com/yourusername/git-commits.git
cd git-commits
uv install -e .

Quick Start

from git_commits import list_git_commits

# 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}")

API Reference

list_git_commits(repo_path, **kwargs)

List Git commits from a specified local repository with filtering options.

Parameters

  • repo_path (str): The absolute or relative path to the local Git repository
  • author (str, optional): Filter commits by author's name or email (case-insensitive partial matching)
  • since (Union[str, datetime], optional): Filter commits authored after this date
  • until (Union[str, datetime], optional): Filter commits authored before this date
  • timezone (str, optional): Timezone string for interpreting string dates (defaults to "UTC")
  • all_branches (bool, optional): If True, search across all branches (defaults to False)

Returns

List of GitCommit objects with the following attributes:

  • sha (str): Full SHA-1 hash of the commit
  • short_sha (str): First 7 characters of the SHA-1 hash
  • author_name (str): Name of the commit author
  • author_email (str): Email of the commit author
  • authored_datetime (datetime): Timezone-aware datetime when the commit was authored
  • message (str): Full commit message

Usage Examples

Basic Usage

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

# Filter by author name (case-insensitive)
commits = list_git_commits(".", author="john")

# Filter by email
commits = list_git_commits(".", author="john@example.com")

Date Filtering

Using String Dates

# 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")

Using Datetime Objects

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
)

Timezone Support

# Interpret string dates in a specific timezone
commits = list_git_commits(
    ".",
    since="2024-01-01 09:00:00",
    timezone="America/New_York"
)

Search Across All Branches

# Include commits from all branches (including remote-tracking)
commits = list_git_commits(".", all_branches=True)

Combined Filters

# Combine multiple filters
commits = list_git_commits(
    ".",
    author="john",
    since="1 month ago",
    until="now",
    timezone="UTC",
    all_branches=True
)

Working with Results

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)

Supported Date Formats

The library supports various date string formats:

  • ISO dates: "2024-01-01", "2024-01-01 15:30:00"
  • Relative dates: "yesterday", "today", "now"
  • Relative periods: "1 week ago", "2 months ago", "3 days ago"
  • Natural language: Most date strings parseable by python-dateutil

Error Handling

The library gracefully handles various error conditions:

  • Invalid repository paths
  • Non-Git repositories
  • Invalid date strings
  • Permission errors

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")

Requirements

  • Python 3.9+
  • GitPython >= 3.1.40
  • python-dateutil >= 2.8.2
  • pytz >= 2024.1

Development

Setting up for Development

  1. Clone the repository:
git clone https://github.com/yourusername/git-commits.git
cd git-commits
  1. Install with development dependencies using uv:
uv install -e ".[dev]"
  1. Run the examples:
python main.py
  1. Run tests (when available):
pytest
  1. Format code:
black src/ tests/
isort src/ tests/
  1. Type checking:
mypy src/

Publishing to PyPI

This project is configured to be published using uv. Here's how to publish:

  1. Ensure you have the latest version of uv:
uv --version  # Should be 0.1.0+
  1. Update the version in pyproject.toml:
[project]
version = "0.1.1"  # Increment as needed
  1. Build the package:
uv build
  1. Publish to TestPyPI first (recommended):
uv publish --index-url https://test.pypi.org/simple/ --repository-url https://test.pypi.org/legacy/
  1. Test the TestPyPI installation:
pip install --index-url https://test.pypi.org/simple/ git-commits
  1. Publish to PyPI:
uv publish

Authentication

For publishing, you'll need to set up authentication:

  1. Create API tokens on PyPI/TestPyPI
  2. Configure uv with your credentials:
# For PyPI
uv config set credentials.pypi.username __token__
uv config set credentials.pypi.password pypi-your-api-token-here

# For TestPyPI  
uv config set credentials.testpypi.username __token__
uv config set credentials.testpypi.password pypi-your-test-api-token-here

Alternatively, you can use environment variables:

export UV_PUBLISH_USERNAME="__token__"
export UV_PUBLISH_PASSWORD="pypi-your-api-token-here"

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

git_commits-0.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

git_commits-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file git_commits-0.1.0.tar.gz.

File metadata

  • Download URL: git_commits-0.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for git_commits-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0120c5d64b80abd3a3c60077e2cb40b4e00f0633706d5c0556b06f34f8360cce
MD5 56508fcfb3b16ac0c8b519da658c57ac
BLAKE2b-256 8769b91d01a9535618aa48dff262af232e816ccfd9c38d216bbb16167270e88d

See more details on using hashes here.

File details

Details for the file git_commits-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for git_commits-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f84ca220e9d7896e684cea464c305e5da6abf2acfc02b790f7537aa60946349
MD5 64af1de56c1ec3f8f0ba62075df18af2
BLAKE2b-256 cc2437b578f0f9de553d44992a9f119aeec72fd7c447c54da9b5eec90fb758cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page