2017 © Pedro Peláez
 

library git-wrapper

A PHP wrapper around the Git command line utility.

image

cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  • PHP
  • 61 Dependents
  • 0 Suggesters
  • 48 Forks
  • 4 Open issues
  • 34 Versions
  • 5 % Grown

The README.md

PHP Wrapper around GIT

Total Downloads Latest Stable Version, (*1)

Git Wrapper provides a readable API that abstracts challenges of executing Git commands from within a PHP process for you., (*2)

  • It's built upon the Symfony\Process to execute the Git command with cross-platform support and uses the best-in-breed techniques available to PHP.
  • This library also provides an SSH wrapper script and API method for developers to easily specify a private key other than default by using the technique from StackOverflow.
  • Finally, various commands are expected to be executed in the directory containing the working copy. The library handles this transparently so the developer doesn't have to think about it.

Install

composer require cpliakas/git-wrapper

Usage

use GitWrapper\GitWrapper;

// Initialize the library. If the path to the Git binary is not passed as 
// the first argument when instantiating GitWrapper, it is auto-discovered.
require_once __DIR__ . '/vendor/autoload.php';

$gitWrapper = new GitWrapper();

// Optionally specify a private key other than one of the defaults
$gitWrapper->setPrivateKey('/path/to/private/key');

// Clone a repo into `/path/to/working/copy`, get a working copy object
$git = $gitWrapper->cloneRepository('git://github.com/cpliakas/git-wrapper.git', '/path/to/working/copy');

// Create a file in the working copy
touch('/path/to/working/copy/text.txt');

// Add it, commit it, and push the change
$git->add('test.txt');
$git->commit('Added the test.txt file as per the examples.');
$git->push();

// Render the output for operation
echo $git->push();

// Stream output of subsequent Git commands in real time to STDOUT and STDERR.
$gitWrapper->streamOutput();

// Execute an arbitrary git command.
// The following is synonymous with `git config -l`
$gitWrapper->git('config -l');

All command methods adhere to the following paradigm:, (*3)

$git->command($arg1, $arg2, ..., $options);

Replace command with the Git command being executed, e.g. checkout, push, etc. The $arg* parameters are a variable number of arguments as they would be passed to the Git command line tool. $options is an optional array of command line options in the following format:, (*4)

$options = [
    'verbose' => true,   // Passes the "--verbose" flag.
    't' => 'my-branch',  // Passes the "-t my-branch" option.
];

Logging

Use the logger listener with PSR-3 compatible loggers such as Monolog to log commands that are executed., (*5)

<?php

use GitWrapper\EventSubscriber\GitLoggerEventSubscriber;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// Log to a file named "git.log"
$logger = new Logger('git');
$logger->pushHandler(new StreamHandler('git.log', Logger::DEBUG));

// Instantiate the subscriber, add the logger to it, and register it.
$gitWrapper->addLoggerEventSubscriber(new GitLoggerEventSubscriber($logger));

$git = $gitWrapper->cloneRepository('git://github.com/cpliakas/git-wrapper.git', '/path/to/working/copy');

// The "git.log" file now has info about the command that was executed above.

Gotchas

There are a few "gotchas" that are out of scope for this library to solve but might prevent a successful implementation of running Git via PHP., (*6)

Missing HOME Environment Variable

Sometimes the HOME environment variable is not set in the Git process that is spawned by PHP. This will cause many Git operations to fail. It is advisable to set the HOME environment variable to a path outside of the document root that the web server has write access to. Note that this environment variable is only set for the process running Git and NOT the PHP process that is spawns it., (*7)

$gitWrapper->setEnvVar('HOME', '/path/to/a/private/writable/dir');

It is important that the storage is persistent as the ~/.gitconfig file will be written to this location. See the following "gotcha" for why this is important., (*8)

Missing Identity And Configurations

Many repositories require that a name and email address are specified. This data is set by running git config [name] [value] on the command line, and the configurations are usually stored in the ~/.gitconfig file. When executing Git via PHP, however, the process might have a different home directory than the user who normally runs git via the command line. Therefore no identity is sent to the repository, and it will likely throw an error., (*9)

// Set configuration options globally.
$gitWrapper->git('config --global user.name "User name"');
$gitWrapper->git('config --global user.email user@example.com');

// Set configuration options per repository.
$git->config('user.name', 'User name');
$git->config('user.email', 'user@example.com');

Commits To Repositories With No Changes

Running git commit on a repository with no changes fails with exception. To prevent that, check changes like:, (*10)

if ($git->hasChanges()) {
    $git->commit('Committed the changes.');
}

Permissions Of The GIT_SSH Wrapper Script

On checkout, the bin/git-ssh-wrapper.sh script should be executable. If it is not, git commands will fail if a non-default private key is specified., (*11)

$ chmod +x ./bin/git-ssh-wrapper.sh

Timeout

There is a default timeout of 60 seconds. This might cause "issues" when you use the clone feature of bigger projects or with slow internet., (*12)

$this->gitWrapper = new GitWrapper();
$this->gitWrapper->setTimeout(120);

The Versions

31/12 2017
26/12 2017

dev-symfony-update

dev-symfony-update https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Chris Pliakas

git

26/12 2017

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Chris Pliakas

git

22/01 2014

1.2.x-dev

1.2.9999999.9999999-dev https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Chris Pliakas

git

22/01 2014

1.1.x-dev

1.1.9999999.9999999-dev https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Chris Pliakas

git

22/01 2014

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

15/12 2013

1.0.3

1.0.3.0 https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

15/12 2013

1.1.2

1.1.2.0 https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Chris Pliakas

git

01/10 2013

1.0.2

1.0.2.0 https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

01/10 2013

1.1.1

1.1.1.0 https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Chris Pliakas

git

17/09 2013

1.1.0

1.1.0.0 https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Chris Pliakas

git

12/09 2013

1.0.1

1.0.1.0 https://github.com/cpliakas/git-wrapper

A PHP wrapper around the Git command line utility.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

05/09 2013

1.0.0

1.0.0.0 https://github.com/cpliakas/git-wrapper

A command line wrapper around Git.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

06/03 2013

1.0.0-RC1

1.0.0.0-RC1 https://github.com/cpliakas/git-wrapper

A command line wrapper aroung Git.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

05/03 2013

1.0.0beta7

1.0.0.0-beta7 https://github.com/cpliakas/git-wrapper

A command line wrapper aroung Git.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

01/02 2013

1.0.0beta6

1.0.0.0-beta6 https://github.com/cpliakas/git-wrapper

A command line wrapper aroung Git.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

23/01 2013

1.0.0beta5

1.0.0.0-beta5 https://github.com/cpliakas/git-wrapper

A command line wrapper aroung Git.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

21/01 2013

1.0.0beta4

1.0.0.0-beta4 https://github.com/cpliakas/git-wrapper

A command line wrapper aroung Git.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

14/01 2013

1.0.0beta3

1.0.0.0-beta3 https://github.com/cpliakas/git-wrapper

A command line wrapper aroung Git.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

11/01 2013

1.0.0beta2

1.0.0.0-beta2 https://github.com/cpliakas/git-wrapper

A command line wrapper aroung Git.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git

10/01 2013

1.0.0beta1

1.0.0.0-beta1 https://github.com/cpliakas/git-wrapper

A command line wrapper aroung Git.

  Sources   Download

GPL-3.0

The Requires

 

by Chris Pliakas

git