gitonomy/gitlib
Gitonomy Gitlib is a PHP library for interacting with Git repositories programmatically. Read commits, trees, branches, tags and diffs; run Git commands via a clean API and work with local repos from your apps, tools, or CI scripts.
Creating a Repository object is possible, providing a path argument to the constructor:
$repository = new Repository('/path/to/repo');
The constructor of Repository takes an additional parameter: $options.
This parameter can be used used to tune behavior of library.
Available options are:
Psr\Log\LoggerInterface)git) Specify command to execute to run gitAn example:
$repository = new Repository('/path/to/repo', [
'debug' => true,
'logger' => new Monolog\Logger(),
]);
On a Repository object, you can call method isBare to test if your repository is bare or not:
$repository->isBare();
To know how much size a repository is using on your drive, you can use
getSize method on a Repository object.
warning
This command was only tested with linux.
The returned size is in kilobytes:
$size = $repository->getSize();
echo 'Your repository size is '.$size.'KB';
HEAD represents in git the version you are working on (in working
tree). Your HEAD can be attached (using a reference) or detached
(using a commit).
$head = $repository->getHead(); // Commit or Reference
$head = $repository->getHeadCommit(); // Commit
if ($repository->isHeadDetached()) {
echo 'Sorry man'.PHP_EOL;
}
If you are developing, you may appreciate to have a logger inside repository, telling you every executed command.
You call method setLogger as an option on repository creation:
$repository->setLogger(new Monolog\Logger('repository'));
$repository->run('fetch', ['--all']);
You can also specify as an option on repository creation:
$logger = new MonologLogger('repository');
$repository = new Repository('/path/foo', ['logger' => $logger]);
$repository->run('fetch', ['--all']);
This will output:
info run command: fetch "--all"
debug last command (fetch) duration: 23.24ms
debug last command (fetch) return code: 0
debug last command (fetch) output: Fetching origin
Gitlib throws an exception when something seems wrong. If a git command exits
with a non-zero code, then execution will be stopped, and a RuntimeException
will be thrown. If you want to prevent this, set the debug option to false.
This will make Repository log errors and return empty data instead of
throwing exceptions.
$repository = new Repository('/tmp/foo', ['debug' => false, 'logger' => $logger]);
note
If you plan to disable debug, you should rely on the logger to keep a trace of the failing cases.
You can pass the option command to specify which command to use to run git
calls. If you have a git binary located somewhere else, use this option to
specify to gitlib path to your git binary:
$repository = new Gitonomy\Git\Repository('/tmp/foo', ['command' => '/home/alice/bin/git']);
It is possible to send environment variables to the git commands.
$repository = new Gitonomy\Git\Repository('/tmp/foo', ['environment_variables' => ['GIT_']])
How can I help you explore Laravel packages today?