rodziu/php-git

Pure PHP Git client (read only).

2.1.2 2024-05-23 11:40 UTC

This package is not auto-updated.

Last update: 2024-09-26 13:14:51 UTC


README

Readonly Git client implementation, that allows one to clone or read Git repository data without native Git client installed.

Prerequisites

  • PHP >= 8.2,
  • ext-zlib - to properly decompress git objects.

Usage

You can interact with Git repository by instantiating GitRepository class.

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');

Get current HEAD

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$head = $gitRepository->getHead();
$head->getCommitHash(); // commit hash that current head points to
$head->getBranch(); // current branch, null if head is detached

Get list of available local or remote branches

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$gitRepository->getBranches(); // returns an array of local branch names
$gitRepository->getBranches(remote: true); // returns an array of remote branch names

Get list of available tags

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
foreach ($gitRepository->getTags() as $tag) {
    $tag; // \Rodziu\Git\Objects\Tag or \Rodziu\Git\Objects\AnnotatedTag
    $tag->getName();
    $tag->getTaggedObjectHash();
    ...
} 

Iterate git log starting from given commit-ish

GitRepository->getLog(?string $commitIsh = null): \Generator

If argument is omitted, log will start from current HEAD.

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
foreach ($gitRepository->getLog() as $commit) {
    $commit; // \Rodziu\Git\Objects\Commit object 
    $commit->getMessage();
    $commit->getCommitDate();
    ...
}

// get origin/master branch log
$gitRepository->getLog('origin/master');

Get commit object by hash

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$gitRepository->getCommit('commit-hash'); // \Rodziu\Git\Objects\Commit object
...

git describe

Give an object a human readable name based on an available ref

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$gitRepository->describe(); // describe current HEAD with annotated tags
$gitRepository->describe('commit-ish', all: true); // describe given ref as in git describe --all
$gitRepository->describe('commit-ish', tags: true); // describe given ref as in git describe --tags

git clone

Fetch repository info and all objects up to current HEAD, then checkout its working tree to /destination/path/repository-name.

\Rodziu\Git\GitRepository::cloneRepository(
    'https://your.repository.url/repository-name.git',
    '/destination/path/'
);

git fetch

Update repository data from remote

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$gitRepository->fetch('origin');

git checkout

Checkout working tree to given branch, tag or commit

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$gitRepository->checkout('commit-ish');