ggioffreda / git
git 和 git-flow 的 PHP 高级包装器。
Requires
- php: >=5.4
- symfony/process: >=2.4,<4.0
Requires (Dev)
- symfony/filesystem: >=2.4,<4.0
This package is not auto-updated.
Last update: 2024-09-24 02:42:16 UTC
README
该组件帮助与 Git 命令行工具和 Git-Flow 扩展交互。
此工具使用 Symfony Process Component (https://github.com/symfony/Process) 构建,但可用于任何 PHP 项目。要求在 composer.json 文件中指定
- PHP 版本 >=5.4
- symfony/process 版本 >= 2.4
- symfony/filesystem 版本 >= 2.2(仅适用于开发环境中的测试运行)
- git
- git-flow(仅适用于使用 Git-Flow 包装器扩展)
安装
该组件可通过 composer 安装。从命令行运行
$ composer require "ggioffreda/git" "~0.1"
或将以下内容添加到您的 composer.json 文件中的 require 部分
"require": {
... other requirements ...,
"ggioffreda/git": "~0.1"
}
有关更多信息,请查看 Packagist 上的项目页面 Packagist。
使用 - Git
为 Git 实现的快捷方式和自解释方法的列表
- init,如果尚未初始化,则初始化 Git 仓库;
- config,设置或请求一个配置变量,当获取它时,您需要随后调用 output 方法以获取值;
- add,将匹配提供的模式的文件添加到下一个提交;
- rm,删除匹配提供的模式的文件到下一个提交;
- mv,将文件从原位置移动到目标位置;
- diff,返回整个项目或指定模式的 diff 命令的输出;
- show,返回指定对象的 show 命令的输出;
- commit,将更改提交到当前分支;
- branchAdd,创建具有给定名称的新分支;
- branchDelete,删除具有给定名称的分支;
- branchList,列出 Git 仓库的分支;
- checkout,检出所需的实体(可以是 Git 允许的任何内容,如分支、文件或哈希);
- status,返回 Git 项目的 Git 状态命令的输出;
- merge,将给定的分支合并到当前分支;
- log,返回 Git log 命令的输出,默认为最后 10 个提交;
- push,将提交推送到远程仓库;
- pull,从远程仓库拉取提交;
- fetch,获取远程分支;
- remoteAdd,添加远程;
- remoteRename,重命名远程;
- remoteRemove,删除远程;
- remoteSetHead,为远程设置 HEAD;
- remoteSetBranches,为远程设置分支;
- remoteGetUrl,获取远程的 URL;
- remoteSetUrl,设置远程的 URL;
- remoteShow,显示远程的信息;
- remotePrune,修剪远程;
- flow,访问 git-flow 包装器扩展。
为 Git 提供的其他方法列表
- 运行,允许您运行任何您能想到的自定义Git命令;
- getConfiguration,返回Git配置键值对的数组;
- getBranches,返回带有相关最后提交哈希和消息的分支数组;
- getStatuses,返回带有状态的非提交更改数组(在“porcelain”Git风味中);
- getLogs,返回带有相关消息的最后提交的数组,您可以指定数组的尺寸;
- output,返回在Git项目中执行的最后命令的输出;
- history,返回在Git项目中执行的所有命令及其相关输出的数组。
Git的静态方法和基本功能列表
- create(静态),返回指定路径和Git可执行文件(可选)的新实例;
- cloneRemote(静态),返回一个新实例,在本地路径中克隆远程仓库;
- getPath,返回Git项目的路径;
- getDefaults,返回快捷方法(见上述快捷方法列表)的默认选项;
- setDefaults,设置快捷方法(见上述快捷方法列表)的默认选项。
当Git命令由于错误的选项或未知原因执行失败时,任何方法都可以返回一个Gioffreda\Component\Git\Exception\GitProcessException,而如果错误发生在解析命令输出时,异常将是Gioffreda\Component\Git\Exception\GitParsingOutputException。两者共享相同的父类,因此如果需要可以一次性捕获Gioffreda\Component\Git\Exception\GitException。
使用方法 - GitFlow
为Git-Flow实现的一系列快捷和自解释方法列表
- init,使用默认值初始化Git项目的git-flow,要使用自定义值,请调用run方法,并提供所需的选项;
- featureList,列出现有的功能;
- featureStart,开始一个新的功能,可选地基于其他基而不是“develop”;
- featureFinish,完成功能;
- featurePublish,开始共享功能;
- featureTrack,开始跟踪功能;
- featureDiff,显示不在“develop”中的所有更改;
- featureRebase,基于“develop”进行变基;
- featureCheckout,切换到功能分支;
- featurePull,从远程仓库拉取功能;
- releaseList,列出现有的发布;
- releaseStart,开始新的发布;
- releaseFinish,完成发布;
- releasePublish,开始共享发布;
- releaseTrack,开始跟踪发布;
- hotfixList,列出现有的hotfixes;
- hotfixStart,开始一个新的hotfix,可选地基于不同于“master”的基;
- hotfixFinish,完成hotfix;
- supportList,列出现有的支持分支;
- supportStart,开始一个新的支持分支;
- getConfiguration,返回git-flow当前配置;
- getVersion,返回git-flow当前版本;
- run,允许您通过git-flow运行任何自定义命令;
- output,返回最后执行命令的输出,它是父Git::output()方法的别名;
- extend(静态),返回给定Git项目的git-flow扩展包装器。
请注意,如果git-flow扩展不可用,所有上述方法(但output和extend)将抛出异常。
示例
对于真实世界的示例,请查看:Git-Guardian。它是一个命令行界面,用于从GitHub或BitBucket备份所有远程仓库。
以下示例展示了如何使用组件。所有非获取方法(不用于读取属性或命令输出)都实现了一个流畅的接口,以提高可读性
<?php namespace MyNamespace; use Gioffreda\Component\Git\Git; class MyJob { public function doSomething($remoteUri, $localPath) { // cloning a remote repository $git = Git::cloneRemote($remoteUri, $localPath); // switches to the develop branch $git->checkout('develop'); // your logic here, change some files // ... $git // adds all the files ->add('.') // commits the changes to the develop branch ->commit('Changed some files') // switches to the master branch ->checkout('master') // merges the develop branch into the master branch ->merge('develop') // commits the changes into the master branch ->commit('Merged the changes into master.') // pushes the changes to the remote repository using a custom command line ->run(['push', '--all']) ; // or you can use a local one even if not initialized yet // new Git project using a custom executable $git = Git::create($localPath, '/usr/local/custom/git'); $git // this will initialize the Git project if not initialized already ->init() // adds all the files in the folder ./src ->add('./src') // commits the changes ->commit('Initial commit (only sources).') ; // retrieves the last commits hashes and messages $logs = $git->getLogs(); // retrieves the list of branches with latest commit hash and message $branches = $git->getBranches() // using git-flow, initializing it first $git->flow()->init(); // starts a new feature $git->flow()->featureStart('test1'); // your logic here, change some files // ... $git // mark the changes so they'll be committed ->add('.') // commits the the changes ->commit('feature finished') // finishes the feature ->flow()->featureFinish('test1') ; } }
资源
您可以使用以下命令运行单元测试(需要phpunit)
$ cd path/to/Gioffreda/Component/Git/
$ composer.phar install
$ phpunit
许可
本软件以下列许可协议分发:GNU GPL v2、GNU GPL v3和MIT许可协议。您可以选择最适合您需求的许可协议。