itsis / php-git-hg-repo
一个面向对象的包装器,可以运行任何Git命令
Requires
- php: >=5.2
This package is not auto-updated.
Last update: 2024-09-19 18:39:41 UTC
README
使用PHP管理Git仓库。提供面向对象的包装器以运行任何Git命令。
要求
- PHP >= 5.2(PHP 5.3运行良好)
- Git >= 1.5
- Hg >= 3.3.2
实例化PHPGit
$repo = new PHPGit('/path/to/the/git/repo');
它不会创建Git仓库,而是创建一个PHP对象来操作现有的Git仓库。
创建Git或HG仓库
如果Git仓库在文件系统中尚未存在,PHPGit可以为您创建它。
$repo = PHPGit::create('/path/to/the/git/repo');
它运行git init并返回一个PHPGit对象。
如果HG仓库在文件系统中尚未存在,PHPHg可以为您创建它。
$repo = PHPHg::create('/path/to/the/hg/repo');
它运行hg init并返回一个PHPGit对象。
运行git/hg命令
git/hg命令可以使用与CLI相同的语法运行。一些示例
// change current branch to master
$repo->git('checkout master');
$repo->hg('up master')
// pull from a remote
$repo->git('pull origin master');
$repo->hg('pull origin master')
// add a remote repo
$repo->git('remote add origin git://github.com/ornicar/php-git-repo.git');
没有限制,您可以运行任何git/hg命令。
git()/hg()方法返回输出字符串
echo $repo->git('log --oneline');
e30b70b Move test repo to system tmp dir, introduce Command
01fabb1 Add test repo
12a95e6 Add base class with basic unit test
58e7769 Fix readme
c14c9ec Initial commit
echo $repo->hg('status');
? README.md
M test.php
如果命令无效,git()/hg()方法会抛出GitRuntimeException
$repo->git('wtf'); // this git command does NOT exist: throw GitRuntimeException
$repos->hg('wtf'); // this git command does NOT exist: throw HgRuntimeException
获取分支信息
提供了一些快捷方法,以便方便地处理分支。
获取分支列表
目前,只有git有关于分支的命令
$branches = $repo->getBranches();
// returns array('master', 'other_branch')
获取当前分支
$branch = $repo->getCurrentBranch();
// returns 'master'
了解仓库是否具有给定的分支
$hasBranch = $repo->hasBranch('master');
// returns true
获取标签信息
目前,只有git有关于标签的命令
获取标签列表
$tags = $repo->getTags();
// returns array('first_release', 'v2')
获取提交信息
您可以得到当前分支上最后提交的数组。
$commits = $repo->getCommits(15);
// returns an array of the 15 last commits
内部,此方法运行git log带有格式化的输出。返回值应如下所示
Array
(
[0] => Array
(
[id] => affb0e84a11b4180b0fa0e5d36bdac73584f0d71
[tree] => 4b825dc642cb6eb9a060e54bf8d69288fbee4904
[author] => Array
(
[name] => ornicar
[email] => myemail@gmail.com
)
[authored_date] => 2010-09-22 19:17:35 +0200
[commiter] => Array
(
[name] => ornicar
[email] => myemail@gmail.com
)
[committed_date] => 2010-09-22 19:17:35 +0200
[message] => My commit message
)
[1] => Array
(
...
第一个提交是最新的。
内部,此方法运行hg log -l带有格式化的输出。返回值应如下所示:String(" changeset: 113:87c310edec6f tag: tip user: Blondeau Gabriel blondeau.gabriel@gmail.com date: Wed Apr 22 08:17:38 2015 +0000 summary: README.md edited online with Bitbucket")
changeset: 112:281ec79f27cc
user: Blondeau Gabriel <blondeau.gabriel@gmail.com>
date: Wed Apr 22 08:16:50 2015 +0000
summary: test.php edited online with Bitbucket
changeset: 111:0588a1ea0385
user: Blondeau Gabriel <blondeau.gabriel@gmail.com>
date: Wed Apr 22 08:14:21 2015 +0000
summary: test.php edited online with Bitbucket ...")
调试模式
PHPGit / PHPHg构造函数的第二个参数让您可以启用调试模式。当调试模式开启时,会显示命令及其输出。
$repo = new PHPGit('/path/to/the/git/repo', true);
$repo = new PHPHg('/path/to/the/hg/repo', true);
配置
PHPGit / PHPHg可以通过将选项数组传递给构造函数的第三个参数来配置。
更改git可执行文件路径
您可能需要提供git可执行文件的路径。
$repo = new PHPGit('/path/to/the/git/repo', false, array('git_executable' => '/usr/bin/git'));
$repo = new PHPHg('/path/to/the/hg/repo', false, array('hg_executable' => '/usr/bin/hg'));
在大多数Unix系统中,它是/usr/bin/git。在Windows中,它可能是C:\Program Files\Git\bin。在大多数Unix系统中,它是/usr/bin/hg。在Windows中,它可能是C:\Program Files\Hg\bin。//我使用tortoise所以:C:\Program Files\TortoiseHg\hg
更改命令类
默认情况下,PHPGit / PHPHg将使用Command类来实现Git命令。通过替换此选项,您可以使用自己的命令实现
$repo = new PHPGit('/path/to/the/git/repo', false, array('command_class' => 'myGitCommand'));
$repo = new PHPHg('/path/to/the/hg/repo', false, array('command_class' => 'myHgCommand'));
运行测试套件
所有代码都是完全单元测试的。要运行服务器上的测试,从CLI运行
php /path/to/php-git-repo/prove.php