ornicar / php-git-repo
此包已被废弃,不再维护。未建议替代包。
一个面向对象的Git命令运行封装器
dev-master
2013-01-14 19:43 UTC
Requires
- php: >=5.2
This package is not auto-updated.
Last update: 2021-10-11 13:17:58 UTC
README
使用PHP管理Git仓库。提供面向对象的Git命令封装器。
要求
- PHP >= 5.2 (PHP 5.3同样可用)
- Git >= 1.5
实例化PHPGit_Repository
$repo = new PHPGit_Repository('/path/to/the/git/repo');
它不会创建Git仓库,而是创建一个PHP对象来操作现有的Git仓库。
创建Git仓库
如果Git仓库在文件系统中尚不存在,PHPGit_Repository可以为您创建它。
$repo = PHPGit_Repository::create('/path/to/the/git/repo');
它会运行git init
并返回一个PHPGit_Repository对象。
运行Git命令
可以使用与CLI中相同的语法运行git命令。以下是一些示例
// change current branch to master
$repo->git('checkout master');
// pull from a remote
$repo->git('pull origin master');
// add a remote repo
$repo->git('remote add origin git://github.com/ornicar/php-git-repo.git');
没有任何限制,您可以使用任何git命令。
git()方法返回输出字符串
echo $repo->git('log --oneline');
e30b70b Move test repo to system tmp dir, introduce PHPGit_Command
01fabb1 Add test repo
12a95e6 Add base class with basic unit test
58e7769 Fix readme
c14c9ec Initial commit
如果命令无效,git()方法会抛出GitRuntimeException异常
$repo->git('wtf'); // this git command does NOT exist: throw GitRuntimeException
获取分支信息
提供了一些快捷方法,以便方便地处理分支。
获取分支列表
$branches = $repo->getBranches();
// returns array('master', 'other_branch')
获取当前分支
$branch = $repo->getCurrentBranch();
// returns 'master'
检查仓库是否有指定的分支
$hasBranch = $repo->hasBranch('master');
// returns true
获取标签信息
获取标签列表
$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
(
...
第一个提交是最新的。
调试模式
PHPGit_Repository
构造函数的第二个参数允许您启用调试模式。当调试模式开启时,会显示命令及其输出。
$repo = new PHPGit_Repository('/path/to/the/git/repo', true);
配置
可以通过将选项数组传递给构造函数的第三个参数来配置PHPGit_Repository
。
更改git可执行路径
可能需要提供 git 可执行文件的路径。
$repo = new PHPGit_Repository('/path/to/the/git/repo', false, array('git_executable' => '/usr/bin/git'));
在大多数 Unix 系统,路径为 /usr/bin/git
。在 Windows 上,可能是 C:\Program Files\Git\bin
。
更改命令类
默认情况下,PHPGit_Repository
将使用 PHPGit_Command
类来实现 Git 命令。通过替换此选项,您可以使用自己的命令实现。
$repo = new PHPGit_Repository('/path/to/the/git/repo', false, array('command_class' => 'myGitCommand'));
运行测试套件
所有代码都经过全面的单元测试。要在您的服务器上运行测试,从 CLI 运行:
php /path/to/php-git-repo/prove.php