grimzy/git-info

用于从工作目录检索Git信息的实用库。

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

README

Build Status Maintainability Test Coverage StyleCI

用于从工作目录检索Git信息的实用库。

最低要求: PHP 7.1

安装

composer require git-info

使用方法

// 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 = 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'
// ]

向GitInfo添加命令

在实例化时

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

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

使用 static 方法

// 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

工作目录

设置工作目录

您可以在创建新的GitInfo实例时设置工作目录

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

默认的工作目录是通过使用 getcwd() 设置的。

获取工作目录

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

测试

$ composer test

许可证

GitInfo遵循MIT许可证