Allows to manage a Git repository with PHP. Provides an object-oriented wrapper to run any Git command.
- PHP >= 8.2
- Git >= 1.5
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.
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.
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 GitRuntimeExceptionSome shortcut methods are provided to deal with branches in a convenient way.
$branches = $repository->getBranches();
// returns ['main', 'other_branch']$branch = $repository->getCurrentBranch();
// returns 'main'$hasBranch = $repository->hasBranch('main');
// returns true$tags = $repository->getTags();
// returns ['first_release', 'v2']You can get an array of the last commits on the current branch.
$commits = $repository->getCommits(15);
// returns an array of the 15 last commitsInternally, 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] => [email protected]
)
[authored_date] => 2010-09-22 19:17:35 +0200
[committer] => Array
(
[name] => ornicar
[email] => [email protected]
)
[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().
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);Repository can be configured by passing an array of options to the constructor's third parameter.
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.
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]);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:
composer install --working-dir=tools/phpstan
tools/phpstan/vendor/bin/phpstan analyzecomposer 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 --diffvendor/bin/phpunit # Run tests
composer test # An alias to run testsNote: tests rely on the default branch being main. This can be updated using the following command:
git config --global init.defaultBranch mainThis package is a maintained fork of the ornicar/php-git-repo package.