Call Git commands using PHP

v1.1.0 2024-03-15 16:59 UTC

This package is auto-updated.

Last update: 2024-06-02 20:05:39 UTC


README

A featherweight PHP class for calling git commands on your web server.

68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6469726563746f7279747265652f6769742f72756e2d74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f4469726563746f7279547265652f4769742e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4469726563746f7279547265652f4769742e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4469726563746f7279547265652f4769742e7376673f7374796c653d666c61742d737175617265

Requirements

  • PHP >= 7.3

Installation

composer require directorytree/git

Before getting started, you must ensure you change PHP's working directory to the root of your Git repository using chdir():

// The current working directory:
chdir(getcwd());

// A specific directory:
chdir('/usr/sbin/httpd');

This package also assumes that Git has been installed and is available in your systems PATH, so it can be called globally.

Usage

Create a new Git instance, and set the remote you want to work with:

use DirectoryTree\Git\Git;

$git = new Git($remote = 'origin');

Available Commands

Pull

Returns true or false.

$git = new Git();

$successful = $git->pull('master');

$successful = $git->pull('v1.0.1');

Fetch

Returns true or false.

$git = new Git();

$successful = $git->fetch();

Reset

Returns true or false.

Note: A hard reset will always be performed by default unless specified otherwise.

$git = new Git();

$successful = $git->reset($commitOrTag = 'v0.0.9');

$successful = $git->reset($commitOrTag = 'v0.0.9', $mode = 'soft');

Remotes

Get

Returns an array of remote URLs (empty array on failure):

$urls = $git->getRemote('origin');
Get All

Returns an array of remotes, with their URLs (empty array on failure):

$remotes = $git->getRemotes();
Add

Returns true or false.

$success = $git->addRemote('origin', 'https://github.com/DirectoryTree/Git');
Set URL

Returns true or false.

$successful = $git->setRemoteUrl('origin', 'https://github.com/DirectoryTree/Git');
Remove

Returns true or false.

$successful = $git->removeRemote('origin');

Tags

Get All

Returns an array of tags (empty array on failure):

$tags = $git->getTags();
Get Current

Returns the current repository's tag (false on failure).

$currentTag = $git->getCurrentTag();
Get Latest

Returns the current repository's latest (false on failure).

$latestTag = $git->getLatestTag();
Get Next

Returns the current repository's tag that is directly after the given (false on failure).

$nextTag = $git->getNextTag('v1.0.0');
Get Previous
$previousTag = $git->getPreviousTag('v1.0.1');

Commits

Get All

Returns an array of commits (empty array on failure):

$commits = $git->getCommits();

$commits = $git->getCommits(['from' => '9d26e0']);

$commits = $git->getCommits(['from' => '9d26e0', 'to' => '8bf0de6']);
Get Between

Shorthand for the above method.

$commits = $git->getCommitsBetween($from = '9d26e0', $to = '8bf0de6');

Testing

Git uses the PHP package TitasGailius/terminal for running all commands. This means you can utilize it's testing framework for executing all Git commands:

use DirectoryTree\Git\Git;

class GitTest extends TestCase
{
    public function test_git_pull()
    {
        Terminal::fake([
            'git pull {{ $remote }} {{ $commit }} --ff-only' => Terminal::response()->successful()
        ]);

        $this->assertTrue((new Git)->pull('v1.0.0'));
    }
}