Skip to content

Pitmaster

Git in PHP: read and write repositories, diff, merge and speak smart HTTP without shelling out to git.

Terminal
composer require pitmaster/pitmaster

Latest release v0.2.11 MIT PHP 8.2+

In practice

Open a repository, then read and write it with method calls or from the shell.

Read a repository from PHP

Pitmaster::open() locates the .git directory and returns a repository handle. Each status entry carries a separate state for the index and the working tree.

From the shell, ./vendor/bin/pitmaster mirrors a subset of git commands and works from a Composer install since 0.2.11.

PHP
use Pitmaster\Pitmaster;

$repo = Pitmaster::open('/path/to/project');

// Read the current HEAD commit
$head = $repo->head();
echo $head->message;

// List branches
foreach ($repo->branches() as $branch) {
    echo $branch . "\n";
}

// Check status
foreach ($repo->status() as $entry) {
    echo "{$entry->path}: {$entry->index->value}{$entry->worktree->value}\n";
}

No git Binary

Core operations run in PHP 8.2+ with no git binary or FFI. Hooks and the SSH fallback start processes unless you pass ['hooks' => false] or processFreeOptions().

Read & Write

Loose objects, pack files with delta resolution, the index, refs, and config. git reads what Pitmaster writes for index versions 2 and 3 and loose refs.

Diff & Merge

Myers, patience and histogram diffs with rename detection. Three-way merge with conflict markers.

Network Protocol

Clone, fetch, and push over smart HTTP or SSH, with a dumb HTTP fallback for clone and fetch. Protocol v2 is available through the lower-level clients.

Git Operations

add, commit, status, diff, merge, checkout, reset, stash, cherry-pick, revert, rebase, blame, grep.

CLI

22 git-style commands, including init, log, status, diff, add, commit, branch, merge, stash, blame and grep. Clone, fetch and push are library-only.

Oracle-Tested

761 scenarios compare results with output captured from real git and run in CI; 605 come from the test suites of git itself, libgit2, go-git, isomorphic-git, dulwich and JGit.

Full Search

Blame, grep, bisect, notes and a log path filter, without shelling out to git.

Why Pitmaster

Works without a git install

Commits, diffs, status and history are computed in PHP, so containers and hosts without a git binary can still work with repositories.

Process-free handles

Pitmaster::processFreeOptions() turns off hooks, fsmonitor hooks, clone, fetch, push and SSH process transport. Local reads and writes keep working.

Checked against git

Integration tests run against the git binary, and the support matrix counts 146 of 146 in-scope features as oracle-verified within its declared parity scope.

SHA-1 and SHA-256

Create a SHA-256 repository with Pitmaster::init($path, 'sha256') or init --object-format=sha256. Pack files are read and written; commit-graph, multi-pack index and reftable files can be read.

Compared with

Other ways to work with Git repositories from PHP, and where Pitmaster differs.

Pitmaster compared with other tools
git (command line) Good atThe reference implementation, with a rich command set that covers both high-level operations and full access to internals. Difference with PitmasterPitmaster runs inside the PHP process and core operations do not shell out to git. Its CLI mirrors only a subset of git commands.
czproject/git-php Good atA compact PHP API for init, clone, commit, merge, branches, tags and remotes. It runs on PHP 8.0 or later and drives the installed git client. Difference with PitmasterPitmaster needs no git client for core operations because it reads and writes objects, pack files and the index itself. It requires PHP 8.2 or later.
gitonomy/gitlib Good atRepository access from PHP 8.4 or later with nothing to build. It works by making shell calls to git. Difference with PitmasterPitmaster makes no shell calls for core operations, and a process-free handle disables the parts that would start a process.
libgit2 Good atA portable C implementation of the Git core methods as a linkable library, with bindings for Ruby, .NET, Python, Node.js, Rust and more. Difference with PitmasterPitmaster is plain PHP installed with Composer, with no compiled library or FFI. It imports 63 of its oracle scenarios from libgit2's test resources.

Questions and answers

Short answers from the Pitmaster documentation.

Start with the getting started guide to open your first repository from PHP, then use the API and CLI references for the rest.