A featherweight PHP class for calling git commands on your web server.
- PHP >= 7.3
composer require directorytree/gitBefore 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.
Create a new Git instance, and set the remote you want to work with:
use DirectoryTree\Git\Git;
$git = new Git($remote = 'origin');Returns true or false.
$git = new Git();
$successful = $git->pull('master');
$successful = $git->pull('v1.0.1');Returns true or false.
$git = new Git();
$successful = $git->fetch();Returns true or false.
Note: A
hardreset 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');Returns an array of remote URLs (empty array on failure):
$urls = $git->getRemote('origin');Returns an array of remotes, with their URLs (empty array on failure):
$remotes = $git->getRemotes();Returns true or false.
$success = $git->addRemote('origin', 'https://github.com/DirectoryTree/Git');Returns true or false.
$successful = $git->setRemoteUrl('origin', 'https://github.com/DirectoryTree/Git');Returns true or false.
$successful = $git->removeRemote('origin');Returns an array of tags (empty array on failure):
$tags = $git->getTags();Returns the current repository's tag (false on failure).
$currentTag = $git->getCurrentTag();Returns the current repository's latest (false on failure).
$latestTag = $git->getLatestTag();Returns the current repository's tag that is directly after the given (false on failure).
$nextTag = $git->getNextTag('v1.0.0');$previousTag = $git->getPreviousTag('v1.0.1');Returns an array of commits (empty array on failure):
$commits = $git->getCommits();
$commits = $git->getCommits(['from' => '9d26e0']);
$commits = $git->getCommits(['from' => '9d26e0', 'to' => '8bf0de6']);Shorthand for the above method.
$commits = $git->getCommitsBetween($from = '9d26e0', $to = '8bf0de6');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'));
}
}