Skip to content

A featherweight PHP class for calling Git commands.

License

Notifications You must be signed in to change notification settings

DirectoryTree/Git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Git

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

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'));
    }
}

About

A featherweight PHP class for calling Git commands.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages