cypresslab / gitelephant-bundle
Symfony2 的 GitElephant bundle
Requires
- php: >=7.2.0
- cypresslab/gitelephant: ^4.0
- symfony/config: ^4.0|^5.0
- symfony/console: ^4.0|^5.0
- symfony/dependency-injection: ^4.0|^5.0
- symfony/framework-bundle: ^4.0|^5.0
- symfony/http-foundation: ^4.0|^5.0
- symfony/http-kernel: ^4.0|^5.0
This package is auto-updated.
Last update: 2024-08-29 03:35:13 UTC
README
这是一个用于在 Symfony 项目中使用 GitElephant 库 的简单 bundle。
安装方法
方法 1 - composer for Symfony 2.1 及以上版本(推荐)
- 将以下行添加到
composer.json
文件中
{ "require": { "cypresslab/gitelephant-bundle": "dev-master" } }
- 执行 composer update 命令
$ composer update
- 在 kernel 文件中注册 bundle
app/AppKernel.php
class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ...other bundles new Cypress\GitElephantBundle\CypressGitElephantBundle(), ); // ...other bundles return $bundles; } }
出于安全考虑,建议仅在开发环境中注册此 bundle。
class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ...other bundles ); if (in_array($this->getEnvironment(), array('dev', 'test'))) { // ...other development and testing bundles $bundles[] = new Cypress\GitElephantBundle\CypressGitElephantBundle(); } return $bundles; } }
方法 2 - 子模块
您还可以使用 git 和子模块来管理这两个 git 仓库。如果您不知道自己在做什么,可能会一团糟,但我就个人而言更喜欢这种方式。
$ git submodule add git://github.com/matteosister/GitElephant.git src/git-elephant $ git submodule add git://github.com/matteosister/GitElephantBundle.git src/Cypress/GitElephantBundle
这两个命令将在您的 "src" 文件夹内克隆两个仓库。您可以在您的 symfony 项目中使用任何文件夹。只需记住更新 app/autoload.php 文件,并在 app/AppKernel.php 中激活 bundle。
该 bundle 的命名空间为 "Cypress"。您必须将 bundle 克隆到 Cypress 文件夹中,否则它将无法工作。您的 autoload 文件应指向包含 Cypress 文件夹的文件夹。
在 autoload 中,GitElephant 的命名空间应指向 GitElephant 仓库中的 "src" 文件夹。
要实际克隆子模块,请输入以下命令
$ git submodule update --init
有关 git 子模块的更多信息,请参阅 Pro Git 书籍中的专门部分,由 Scott Chacon 编写。
使用方法
要使用此 bundle,您必须在 app/config/config.yml 文件中的 cypress_git_elephant 节下定义两个参数
cypress_git_elephant:
binary_path: /usr/local/bin/git
repositories:
"GitElephant": "/home/matteo/libraries/GitElephant"
"Bootstrap": "/home/matteo/libraries/Bootstrap"
# ... other repositories
binary_path:(可选)是您的 git 可执行文件路径。如果您不提供此路径,GitElephant 将尝试使用 "which git" 来争辩正确的可执行文件。请记住,此库仅适用于 *nix 文件系统。
现在,在您的控制器中,您可以通过依赖注入轻松访问 GitElephant 库
repositories:(至少需要一个)是一个包含 key:一个仓库名称,value:仓库路径的哈希表
仓库路径也可以是裸仓库(对于 web 服务器很有用)。但没有检出副本,您将无法修改仓库状态。您将能够显示仓库,但不能创建新的提交等。
class AwesomeController extends Controller { /** * @Route("/", name="repository_root") * @Template() * * @param \Symfony\Component\HttpFoundation\Request $request * @return array */ public function rootAction(Request $request) { // Repository instance $repositories = $this->get('cypress_git_elephant.repository_collection'); // There is also an handy alias $repositories = $this->get('git_repositories'); // $repositories is an instance of GitElephant\Cypress\GitElephantBundle\Collection\GitElephantRepositoryCollection // it has the Countable, ArrayAccess and Iterator interfaces. So you can do: $num_repos = count($repositories); //number of repositories $git_elephant = $repositories->get('GitElephant'); // retrieve a Repository instance by its name (defined in config.yml) // iterate foreach ($repositories as $repo) { $repo->getLog(); } } }
请参阅 GitElephant 的文档以了解您可以使用 Repository 类做什么,或 观看此 bundle 构建的演示站点 和 相关代码。
Web 调试工具栏
作为额外功能,由于 GitElephant 库的存在,您可以在 Symfony2 工具栏中直接查看任何仓库的分支。
将以下内容添加到您的 dev 配置文件 app/config/config_dev.yml
cypress_git_elephant:
enable_profiler: true
profiler_repository_path: "%kernel.root_dir%/../"
如果您使用 git 与 Symfony2 配合使用,则使用上述配置,您可以直接在浏览器中查看您所在的分支。单击图标,您将获得您所在分支的最后 10 个提交的列表。
可用的控制台命令
cypress:git:commit
此命令用于提交(默认为全部阶段)当前分支的所有更改并推送到所有远程仓库。
$ php app/console cypress:git:commit [--no-push] [--no-stage-all] [--all] message
cypress:git:tag
此命令用于标记当前提交并将其推送到所有远程仓库。
$ php app/console cypress:git:tag [--no-push] [--all] tag [comment]
cypress:git:merge
此命令将(默认不使用快速前进)从源分支(默认为devel)合并到目标分支(默认为master),并推送到所有远程仓库。
$ php app/console cypress:git:merge [--no-push] [--fast-forward] [--all] [source] [destination]
cypress:git:hit
组合命令,用于从源分支合并到目标分支而不使用快速前进选项,标记目标分支并将其推送到所有远程仓库。
$ php app/console cypress:git:hit [--no-push] [--fast-forward] [--all] tag [comment] [source] [destination]
示例
还有演示包,可以查看其实际应用。