2017 © Pedro Peláez
 

library gitelephant

An abstraction layer for git written in PHP 5.3

image

moonlitbc/gitelephant

An abstraction layer for git written in PHP 5.3

  • Wednesday, March 28, 2018
  • by MoonlitBC
  • Repository
  • 2 Watchers
  • 1 Stars
  • 1,469 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 74 Forks
  • 0 Open issues
  • 84 Versions
  • 50 % Grown

The README.md

GitElephant, (*1)

Latest Stable Version License Total Downloads Montly Downloads, (*2)

Build Status Dependency Status Scrutinizer Quality Score Code Coverage SensioLabsInsight, (*3)

GitElephant is an abstraction layer to manage your git repositories with php, (*4)

This library officially supports git >= 1.8, older version are supported as well, but with some caveat., (*5)

Watch a live example of what you can do with GitElephant, Symfony2 and a git repository..., (*6)

How it works

GitElephant mostly rely on the git binary to retrieve information about the repository, read the output and create an OOP layer to interact with, (*7)

Some parts are (or will be) implemented by reading directly inside the .git folder, (*8)

The api is completely transparent to the end user. You don't have to worry about which method is used., (*9)

Requirements

  • php >= 5.3.0
  • *nix system with git installed

I work on linux, but the lib should work well with every unix system, as far as a git binary is available. I don't have a windows installation to test...if someone want to help..., (*10)

Installation

composer, (*11)

To install GitElephant with composer you simply need to create a composer.json in your project root and add:, (*12)

``` json { "require": { "cypresslab/gitelephant": "~1.0" } }, (*13)


Then run ``` bash $ curl -s https://getcomposer.org/installer | php $ composer install

You have now GitElephant installed in vendor/cypresslab/gitelephant, (*14)

And an handy autoload file to include in you project in vendor/autoload.php, (*15)

How to use

``` php use GitElephant\Repository; $repo = new Repository('/path/to/git/repository'); // or the factory method $repo = Repository::open('/path/to/git/repository');, (*16)


By default GitElephant try to use the git binary on your system. the *Repository* class is the main class where you can find every method you need... **Read repository** ``` php // get the current status $repo->getStatusOutput(); // returns an array of lines of the status message

branches, (*17)

``` php $repo->getBranches(); // return an array of Branch objects $repo->getMainBranch(); // return the Branch instance of the current checked out branch $repo->getBranch('master'); // return a Branch instance by its name $develop = Branch::checkout($repo, 'develop'); $develop = Branch::checkout($repo, 'develop', true); // create and checkout, (*18)


*tags* ``` php $repo->getTags(); // array of Tag instances $repo->getTag('v1.0'); // a Tag instance by name Tag::pick($repo, 'v1.0'); // a Tag instance by name // last tag by date $repo->getLastTag();

commits, (*19)

``` php $repo->getCommit(); // get a Commit instance of the current HEAD $repo->getCommit('v1.0'); // get a Commit instance for a tag $repo->getCommit('1ac370d'); // full sha or part of it // or directly create a commit object $commit = new Commit($repo, '1ac370d'); $commit = new Commit($repo, '1ac370d'); // head commit, (*20)

// count commits $repo->countCommits('1ac370d'); // number of commits to arrive at 1ac370d // commit is countable, so, with a commit object, you can do $commit->count(); // as well as count($commit);, (*21)


*remotes* ``` php $repo->getRemote('origin'); // a Remote object $repo->getRemotes(); // array of Remote objects // Log contains a collection of commit objects // syntax: getLog(<tree-ish>, path = null, limit = 15, offset = null) $log = $repo->getLog(); $log = $repo->getLog('master', null, 5); $log = $repo->getLog('v0.1', null, 5, 10); // or directly create a log object $log = new Log($repo); $log = new Log($repo, 'v0.1', null, 5, 10); // countable $log->count(); count($log); // iterable foreach ($log as $commit) { echo $commit->getMessage(); }

status, (*22)

If you build a GitElephant\Status\Status class, you will get a nice api for getting the actual state of the working tree and staging area., (*23)

``` php $status = $repo->getStatus(); $status = GitElephant\Status\Status::get($repo); // it's the same..., (*24)

$status->all(); // A PhpCollection of StatusFile objects $status->untracked(); $status->modified(); $status->added(); $status->deleted(); $status->renamed(); $status->copied();, (*25)


all this methods returns a [PhpCollection](https://github.com/schmittjoh/php-collection) of StatusFile objects a StatusFile instance has all the information about the tree node changes. File names (and new file names for renamed objects), index and working tree status, and also a "git style" description like: *added to index* or *deleted in work tree* **Manage repository** You could also use GitElephant to manage your git repositories via PHP. Your web server user (like www-data) needs to have access to the folder of the git repository ``` php $repo->init(); // init $repo->cloneFrom("git://github.com/matteosister/GitElephant.git"); // clone // stage changes $repo->stage('file1.php'); $repo->stage(); // stage all // commit $repo->commit('my first commit'); $repo->commit('my first commit', true); // commit and stage every pending changes in the working tree // remotes $repo->addRemote('awesome', 'git://github.com/matteosister/GitElephant.git'); // checkout $repo->checkout($repo->getTag('v1.0')); // checkout a tag $repo->checkout('master'); // checkout master // manage branches $repo->createBranch('develop'); // create a develop branch from current checked out branch $repo->createBranch('develop', 'master'); // create a develop branch from master $repo->deleteBranch('develop'); // delete the develop branch $repo->checkoutAllRemoteBranches('origin'); // checkout all the branches from the remote repository // manage tags // create a tag named v1.0 from master with the given tag message $repo->createTag('v1.0', 'master', 'my first release!'); // create a tag named v1.0 from the current checked out branch with the given tag message $repo->createTag('v1.0', null, 'my first release!'); // create a tag from a Commit object $repo->createTag($repo->getCommit());

Remote repositories, (*26)

If you need to access remote repository you have to install the ssh2 extension and pass a new Caller to the repository. this is a new feature...consider this in a testing phase, (*27)

``` php $repo = new Repository('/path/to/git/repository'); $connection = ssh_connect('host', 'port'); // authorize the connection with the method you want ssh2_auth_password($connection, 'user', 'password'); $caller = new CallerSSH2($connection, '/path/to/git/binary/on/server'); $repo = Repository::open('/path/to/git/repository'); $repo->setCaller($caller);, (*28)


A versioned tree of files ------------------------- A git repository is a tree structure versioned in time. So if you need to represent a repository in a, let's say, web browser, you will need a tree representation of the repository, at a given point in history. **Tree class** ``` php $tree = $repo->getTree(); // retrieve the actual *HEAD* tree $tree = $repo->getTree($repo->getCommit('1ac370d')); // retrieve a tree for a given commit $tree = $repo->getTree('master', 'lib/vendor'); // retrieve a tree for a given path // generate a tree $tree = new Tree($repo);

The Tree class implements ArrayAccess, Countable and Iterator interfaces., (*29)

You can use it as an array of git objects., (*30)

``` php foreach ($tree as $treeObject) { echo $treeObject; }, (*31)


A Object instance is a php representation of a node in a git tree ``` php echo $treeObject; // the name of the object (folder, file or link) $treeObject->getType(); // one class constant of Object::TYPE_BLOB, Object::TYPE_TREE and Object::TYPE_LINK $treeObject->getSha(); $treeObject->getSize(); $treeObject->getName(); $treeObject->getSize(); $treeObject->getPath();

You can also pass a tree object to the repository to get its subtree, (*32)

``` php $subtree = $repo->getTree('master', $treeObject);, (*33)


Diffs ----- If you want to check a Diff between two commits the Diff class comes in ``` php // get the diff between the given commit and it parent $diff = $repo->getDiff($repo->getCommit()); // get the diff between two commits $diff = $repo->getDiff($repo->getCommit('1ac370d'), $repo->getCommit('8fb7281')); // same as before for a given path $diff = $repo->getDiff($repo->getCommit('1ac370d'), $repo->getCommit('8fb7281'), 'lib/vendor'); // or even pass a Object $diff = $repo->getDiff($repo->getCommit('1ac370d'), $repo->getCommit('8fb7281'), $treeObject); // alternatively you could directly use the sha of the commit $diff = $repo->getDiff('1ac370d', '8fb7281'); // manually generate a Diff object $diff = Diff::create($repo); // defaults to the last commit // or as explained before $diff = Diff::create($repo, '1ac370d', '8fb7281');

The Diff class implements ArrayAccess, Countable and Iterator interfaces, (*34)

You can iterate over DiffObject, (*35)

``` php foreach ($diff as $diffObject) { // mode is a constant of the DiffObject class // DiffObject::MODE_INDEX an index change // DiffObject::MODE_MODE a mode change // DiffObject::MODE_NEW_FILE a new file change // DiffObject::MODE_DELETED_FILE a deleted file change echo $diffObject->getMode(); }, (*36)


A DiffObject is a class that implements *ArrayAccess*, *Countable* and *Iterator* interfaces. It represent a file, folder or submodule changed in the Diff. Every DiffObject can have multiple chunks of changes. For example:
added 3 lines at line 20
deleted 4 lines at line 560

You can iterate over DiffObject to get DiffChunks. DiffChunks are the last steps of the Diff process, they are a collection of DiffChunkLine Objects ``` php foreach ($diffObject as $diffChunk) { if (count($diffChunk) > 0) { echo "change detected from line ".$diffChunk->getDestStartLine()." to ".$diffChunk->getDestEndLine(); foreach ($diffChunk as $diffChunkLine) { echo $diffChunkLine; // output the line content } } }

Testing

The library is fully tested with PHPUnit., (*37)

Go to the base library folder and install the dev dependencies with composer, and then run the phpunitt test suite, (*38)

bash $ composer --dev install $ ./vendor/bin/phpunit # phpunit test suite, (*39)

If you want to run the test suite you should have all the dependencies loaded., (*40)

Symfony2

There is a GitElephantBundle to use this library inside a Symfony2 project., (*41)

Dependencies

for tests, (*42)

Code style

Want to contribute?

You are my new hero!, (*43)

Just remember:, (*44)

  • PSR2 coding standard
  • test everything you develop with phpunit
  • if you don't use gitflow, just remember to branch from "develop" and send your PR there. Please do not send pull requests on the master branch.

Author

Matteo Giachino (twitter), (*45)

Many thanks to all the contributors, (*46)

Thanks

Many thanks to Linus and all those who have worked/contributed in any way to git. Because it's awesome!!! I can't imagine being a developer without it., (*47)

Logo design by Stefano Lodovico, (*48)

Analytics, (*49)

The Versions

28/03 2018

dev-develop

dev-develop http://gitelephant.cypresslab.net/

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Martin Wong

git

28/03 2018

v2018.1.4

2018.1.4.0 http://gitelephant.cypresslab.net/

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Martin Wong

git

12/03 2018

v2018.1.3

2018.1.3.0 http://gitelephant.cypresslab.net/

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Martin Wong

git

21/02 2018

v2018.1.2

2018.1.2.0 http://gitelephant.cypresslab.net/

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Martin Wong

git

21/02 2018

v2018.1.1

2018.1.1.0 http://gitelephant.cypresslab.net/

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Martin Wong

git

10/02 2018

v2018.1

2018.1.0.0 http://gitelephant.cypresslab.net/

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Martin Wong

git

30/11 2017

v2017.11.2

2017.11.2.0 http://gitelephant.cypresslab.net/

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Martin Wong

git

06/11 2017

v2017.11.1

2017.11.1.0 http://gitelephant.cypresslab.net/

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

by Martin Wong

git

26/01 2016
11/08 2014

dev-feature/global-command-flags

dev-feature/global-command-flags http://gitelephant.cypresslab.net/

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

git

06/08 2014
03/08 2014

dev-feature/remote-repo-vagrant-tests

dev-feature/remote-repo-vagrant-tests http://gitelephant.cypresslab.net/

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

git

26/06 2014

dev-John-Schlick-RevParse

dev-John-Schlick-RevParse http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

git

07/06 2013
19/05 2013
19/05 2013
23/02 2013

0.9.15

0.9.15.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

23/02 2013

0.9.14

0.9.14.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

19/01 2013

0.9.13

0.9.13.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

19/01 2013

0.9.12

0.9.12.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

06/01 2013

0.9.11

0.9.11.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

27/12 2012

0.9.10

0.9.10.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

25/12 2012

0.9.9

0.9.9.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

23/12 2012

0.9.8

0.9.8.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

23/12 2012

0.9.7

0.9.7.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

18/12 2012

0.9.6

0.9.6.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

02/12 2012

0.9.5

0.9.5.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

07/11 2012

0.9.3

0.9.3.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

03/11 2012
28/10 2012
03/10 2012
31/08 2012

0.8.4

0.8.4.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

20/08 2012

0.8.3

0.8.3.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

The Development Requires

git

30/06 2012

0.8.2

0.8.2.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

git

24/04 2012

0.8.1

0.8.1.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

LGPLv3

The Requires

 

git

11/03 2012
16/01 2012

0.6.1

0.6.1.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git

14/01 2012

0.6.0

0.6.0.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git

14/01 2012

0.5.4

0.5.4.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git

13/01 2012

0.5.3

0.5.3.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git

12/01 2012

0.5.2

0.5.2.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git

11/01 2012

0.5.1

0.5.1.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git

11/01 2012

0.5.0

0.5.0.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git

11/01 2012

0.4.2

0.4.2.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git

11/01 2012

0.4.1

0.4.1.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git

11/01 2012

0.4

0.4.0.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git

10/01 2012

0.3

0.3.0.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

  • php >=5.3.0

 

git

08/01 2012

0.2

0.2.0.0 http://matteosister.github.com/GitElephant

An abstraction layer for git written in PHP 5.3

  Sources   Download

GPL3

The Requires

 

git