grimzy/git-info

Utility library to retrieve Git information from a working directory.

v1.0.0 2019-10-16 02:47 UTC

README

Build Status Maintainability Test Coverage StyleCI

Utility library to retrieve Git information from a working directory.

Minimum requirement: PHP 7.1

Installation

composer require git-info

Usage

// Instantiate GitInfo
$gitInfo = new \Grimzy\GitInfo\GitInfo();

// Run all registered commands
$info = $gitInfo->getInfo();

// $info = [
//     'commit-hash-long' => 'd93287e02a3b7823623f383ffb443d686b41e5ae',
//     'commit-hash-short' => 'd93287',
//     'author-name' => 'John Doe',
//     'author-email' => 'john.doe@git-info',
//     'author-date' => '2018-08-17T20:58:21-04:00',
//     'subject' => 'Release v1.2.3'
//     'branch' => 'master'
//     'version' => 'v1.2.3'
// ]


// Run a subset of commands
$info = $gitInfo->getInfo(['branch', 'commit-hash-short', 'author-date']);

// $info = [
//     'branch' => 'master'
//     'commit-hash-short' => 'd93287',
//     'author-date' => '2018-08-17T20:58:21-04:00'
// ]


// Run one command
$info = $gitInfo->getInfo('version');

// $info = 'v1.2.3'

Commands

Get registered commands

$commands = GitInfo::getCommands();

// Default commands:
// $commands = [
//     'commit-hash-long'  => 'git log -1 --pretty=%H',
//     'commit-hash-short' => 'git log -1 --pretty=%h',
//     'author-name'       => 'git log -1 --pretty=%aN',
//     'author-email'      => 'git log -1 --pretty=%aE',
//     'author-date'       => 'git log -1 --pretty=%aI',
//     'subject'           => 'git log -1 --pretty=%s',
//     'branch'            => 'git rev-parse --abbrev-ref HEAD',
//     'version'           => 'git describe --always --tags --abbrev=0'
// ]

Add commands to GitInfo

When instantiating:

$gitInfo = new \Grimzy\GitInfo\GitInfo(null, [
    'status' => 'git status'
]);

$info = $gitInfo->getInfo('status');
// $info = THE STATUS

Using static method:

// Add the status command
GitInfo::addCommand('status', 'git status');

// All instances of GitInfo (existing and newly created) will now have a status command
$gitInfo = new GitInfo();
$info = $gitInfo->getInfo('status');

// $info = THE STATUS

Working directory

Set the working directory

You can set the working directory when instantiating a new instance of GitInfo:

// When instantiating GitInfo
$gitInfo = new \Grimzy\GitInfo\GitInfo('absolute/or/relative/path');

The default working directory is set using getcwd().

Get the working directory

$gitInfo = new \Grimzy\GitInfo\GitInfo();
$gwd = $gitInfo->getWorkingDirectory();
// $gwd = '/absolute/path/to/working/directory'

Tests

$ composer test

Licence

GitInfo is licensed under the MIT License.