leapt / git-wrapper
An object oriented wrapper to run any Git command
Requires
- php: ^8.2
- symfony/process: ^6.4 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^10.5.1
README
Allows to manage a Git repository with PHP. Provides an object-oriented wrapper to run any Git command.
Requirements
- PHP >= 8.2
- Git >= 1.5
Instantiate a Repository
use Leapt\GitWrapper\Repository; $repository = new Repository('/path/to/the/git/repo');
It does NOT create a Git repo, but a PHP object to manipulate an existing Git repo.
Create a Git repository
If the Git repository does not exist yet on file system, Repository
can create it for you.
$repository = Repository::create('/path/to/the/git/repo');
It runs git init
and returns a Repository
object.
Run git commands
git
commands can be run with the same syntax as in the CLI. Some examples:
// change current branch to main $repository->git('checkout main'); // pull from a remote $repository->git('pull origin main'); // add a remote repo $repository->git('remote add origin https://github.com/leapt/git-wrapper.git');
There are no limitations, you can run any git commands.
The git()
method returns the output string:
echo $repository->git('log --oneline');
e30b70b Move test repo to system tmp dir, introduce PHPGit_Command
01fabb1 Add test repo
12a95e6 Add base class with basic unit test
58e7769 Fix readme
c14c9ec Initial commit
The git()
method throws a GitRuntimeException
if the command is invalid:
$repository->git('unknown'); // this git command does NOT exist: throw GitRuntimeException
Get branches information
Some shortcut methods are provided to deal with branches in a convenient way.
Get the branches list
$branches = $repository->getBranches(); // returns ['main', 'other_branch']
Get the current branch
$branch = $repository->getCurrentBranch(); // returns 'main'
Know if the repo has a given branch
$hasBranch = $repository->hasBranch('main'); // returns true
Get tags information
Get the tags list:
$tags = $repository->getTags(); // returns ['first_release', 'v2']
Get commits information
You can get an array of the last commits on the current branch.
$commits = $repository->getCommits(15); // returns an array of the 15 last commits
Internally, this method runs git log
with formatted output. The return value should look like:
Array ( [0] => Array ( [id] => affb0e84a11b4180b0fa0e5d36bdac73584f0d71 [tree] => 4b825dc642cb6eb9a060e54bf8d69288fbee4904 [author] => Array ( [name] => ornicar [email] => myemail@gmail.com ) [authored_date] => 2010-09-22 19:17:35 +0200 [committer] => Array ( [name] => ornicar [email] => myemail@gmail.com ) [committed_date] => 2010-09-22 19:17:35 +0200 [message] => My commit message ) [1] => Array ( ...
The first commit is the most recent one.
You can also retrieve the last commit by calling $repository->getLastCommit()
.
Debug mode
Repository
constructor's second parameter lets you enable debug mode.
When debug mode is on, commands and their output are displayed.
$repository = new Repository('/path/to/the/git/repo', true);
Configure
Repository
can be configured by passing an array of options to the constructor's third parameter.
Change git executable path
You may need to provide the path to the git executable.
$repo = new Repository('/path/to/the/git/repo', false, ['git_executable' => '/usr/bin/git']);
On most Unix system, it's /usr/bin/git
. On Windows, it may be C:\Program Files\Git\bin
.
Change the command class
By default, the Repository
class will use the Command
class to implement Git commands.
By replacing this option, you can use your own command implementation
(which must implement the Leapt\GitWrapper\CommandInterface
):
use Leapt\GitWrapper\Repository; $repository = new Repository('/path/to/the/git/repo', false, ['command_class' => YourCommand::class]);
Contributing
Feel free to contribute, like sending pull requests to add features/tests or creating issues :)
Note there are a few helpers to maintain code quality, that you can run using these commands:
PHPStan
composer install --working-dir=tools/phpstan tools/phpstan/vendor/bin/phpstan analyze
PHP CS Fixer
composer install --working-dir=tools/php-cs-fixer # Check what can be fixed tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff # Fix them tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --diff
PHPUnit
vendor/bin/phpunit # Run tests composer test # An alias to run tests
Note: tests rely on the default branch being main
. This can be updated using the following command:
git config --global init.defaultBranch main
History
This package is a maintained fork of the ornicar/php-git-repo package.