pixelbrackets/patchbot

自动将补丁分发到各种Git仓库

2.0.0 2022-03-14 20:35 UTC

README

Logo

Version Build Status Made With License Contribution

一个用于自动将补丁分发到各种Git仓库的工具。

Screenshot

⭐ 你喜欢这个包吗?请给它加星或发送一条推文。⭐

愿景

该项目提供了一种工具,可以尽可能少地手动工作,将更改分发到多个Git仓库。

当我不得不将相同的手动更改应用到我的许多仓库时,这种需求产生了

  • 重命名具有特定名称模式的文件,仅在条件匹配的情况下删除一行代码,替换所有文档中的链接,执行另一个工具,然后更改文件,等等。这并不是一个普通的 Git补丁文件 可以解决的问题,但可以通过迁移脚本来实现自动化。
  • 创建一个功能分支,提交所有更改并附上良好的提交信息,推送分支,等待测试变绿,打开拉取请求。
  • 在许多其他仓库中重复相同的步骤。

想法是只更改一次,并将重复的部分移动到工具中。节省时间,防止粗心大意造成的错误,避免单调乏味的工作。

📝 请查看这篇 博客文章,其中包含实际案例,以及Patchbot如何帮助减少您自己的Git仓库中的技术债务。

有关示例命令,请参阅 »使用方法«

该包遵循KISS原则。

要求

  • PHP
  • Git

安装

💡 使用 骨架包 来立即创建一个示例项目。

  • composer create-project pixelbrackets/patchbot-skeleton

安装Patchbot的Packagist条目 https://packagist.org.cn/packages/pixelbrackets/patchbot/

  • composer require pixelbrackets/patchbot

访问权限

🔑 运行Patchbot的用户需要访问目标仓库。

确保运行Patchbot的用户允许克隆和推送所有目标仓库。

Patchbot允许Git原生支持的远程连接协议:FILE, HTTP/HTTPS, SSH

推荐使用SSH协议。

HTTPS凭据

默认情况下,Git不会存储任何凭据。因此,使用HTTPS URI通过Patchbot连接到仓库时将提示输入用户名和密码。

要避免在通过HTTPS URI使用Patchbot时出现这些密码提示,您有两个选择

  • 允许Git在内存中存储凭据一段时间
    • 然后对于每个主机只弹出一次密码提示
    • 将凭据保存在内存中的示例命令(15分钟)
      git config --global credential.helper 'cache --timeout=900'
      
  • 强制Git使用SSH协议检出而不是HTTP/HTTPS
    • 必须为每个主机进行配置
    • 设置GitHub、GitLab和BitBucket替换的示例命令
      git config --global url."ssh://git@github.com/".insteadOf "https://github.com/"
      git config --global url."ssh://git@gitlab.com/".insteadOf "https://gitlab.com/"
      git config --global url."ssh://git@bitbucket.org/".insteadOf "https://bitbucket.org/"
      

源代码

https://gitlab.com/pixelbrackets/patchbot/

镜像 https://github.com/pixelbrackets/patchbot/

使用方法

Patchbot对指定的Git仓库应用补丁。

这意味着它将克隆仓库,创建一个功能分支,运行指定的PHP补丁脚本,使用指定的提交信息提交更改,并将分支推送到远程仓库。

Patchbot使用精简的文件结构来组织补丁(请参阅骨架包)。

目录patches包含所有您的“补丁目录”。

每个补丁目录至少包含一个名为patch.php的PHP脚本和一个名为commit-message.txt的提交信息。

示例文件结构

.
|-- patches
|   |-- template
|   |   |-- commit-message.txt
|   |   `-- patch.php
|   `-- yet-another-patch
|       |-- commit-message.txt
|       `-- patch.php
|-- vendor
|   `-- bin
|       `-- patchbot
|-- composer.json
`-- README.md

这样,迁移脚本可以一次性创建并应用到多个仓库,或者在需要时随时应用。

应用补丁

将补丁目录的名称作为patch-name和Git仓库作为repository-url传递给patchbot脚本。

示例命令:将目录template中的补丁脚本应用到仓库https://git.example.com/repository

./vendor/bin/patchbot patch --patch-name=template --repository-url=https://git.example.com/repository

示例命令:将目录template中的补丁脚本应用到仓库ssh://git@git.example.com/repository.git

./vendor/bin/patchbot patch --patch-name=template --repository-url=ssh://git@git.example.com/repository.git

自定义选项

要基于默认主分支之外的development分支创建特性分支,请使用此命令

./vendor/bin/patchbot patch --source-branch=development --patch-name=template --repository-url=https://git.example.com/repository

Patchbot将为特性分支使用随机名称。若要使用自定义名称如feature-1337-add-license-file,请运行

./vendor/bin/patchbot patch --branch-name=feature-1337-add-license-file --patch-name=template --repository-url=https://git.example.com/repository

建议让CI运行所有测试。这就是为什么Patchbot默认创建特性分支的原因。如果您想在创建提交之前手动审查复杂变更,则可以使用halt-before-commit选项

./vendor/bin/patchbot patch --halt-before-commit --patch-name=template --repository-url=https://git.example.com/repository

要更详细,请在每个命令中添加-v。要调试,请添加-vvv。这将显示Patchbot应用的所有步骤和命令。使用标志--no-ansi将移除输出格式化。

合并特性分支

✨️Patchbot故意创建特性分支来应用补丁。

当您审查了特性分支且所有CI测试都成功时,则可以使用Patchbot再次合并特性分支。

示例命令:将分支bugfix-add-missing-lock-file合并到仓库https://git.example.com/repository中的分支main

./vendor/bin/patchbot merge --source=bugfix-add-missing-lock-file --target=main --repository-url=https://git.example.com/repository

添加新的补丁

示例命令:创建名为add-changelog-file的目录以及补丁所需的所有文件(名称会自动进行缩略化)

./vendor/bin/patchbot create --patch-name="Add CHANGELOG file"

或者手动复制示例文件夹template,并根据需要重命名。

现在替换patch.php中的补丁代码和commit-message.txt中的提交信息。

🛡️Patchbot在隔离模式下运行补丁脚本,因此可以在没有Patchbot的情况下运行脚本。

💡提示:切换到现有的项目仓库,运行php <patch目录路径>/patch.php并逐步开发补丁。当开发完成后,提交它并使用Patchbot将补丁分发到所有其他仓库。

补丁代码将在目标仓库的根目录作用域中执行,请记住在文件搜索时这一点。

分享补丁

在补丁目录中创建的补丁可能非常特定于您的组织或领域。因此,在您的组织中分享补丁的最佳方式是将补丁项目作为Git仓库共享。

然而,由于此工具的动机之一是重用迁移脚本,您可以与他人共享通用脚本。

一种可能的方法是为单个补丁创建GitHub Gist。

示例命令使用CLI gem gist 上传template补丁

cd patches/template/
gist -d "Patchbot Patch »template« - Just a template without changes" patch.php commit-message.txt

🔎搜索带有Patchbot标签的Gists

导入共享补丁

手动复制所有文件以从其他来源导入现有补丁。

如果源是Git仓库,那么只需要Git克隆命令即可。

示例命令:导入Gist https://gist.github.com/pixelbrackets/98664b79c788766e4248f16e268c5745作为补丁add-editorconfig

git clone --depth=1 https://gist.github.com/pixelbrackets/98664b79c788766e4248f16e268c5745 patches/add-editorconfig/
rm -r patches/add-editorconfig/.git

批处理

要将补丁应用于1个或20个仓库,您可以多次运行Patchbot脚本并使用不同的URL。对于300个仓库,您可能需要使用批处理模式。

此模式将为一系列仓库触发patchmerge命令。期望的列表是名为repositories.csv的CSV文件。

repositories.csv - 示例文件内容,包含要使用的仓库 & 分支

repository-url,main-branch
https://git.example.com/projecta,main
https://git.example.com/projectb,main
https://git.example.com/projectc,development

patch子命令允许所有patch命令的选项,除了repository-urlsource-branch。这两个选项都由repositories.csv文件提供。

以下命令将补丁脚本update-changelog应用于repositories.csv文件第一列中的所有仓库URL,并根据第二列中的名称创建功能分支。

./vendor/bin/patchbot batch patch --patch-name=update-changelog

merge子命令也允许所有merge命令的选项,除了repository-urltarget。这两个选项都由repositories.csv文件提供。

下一个命令将功能分支feature-add-phpcs-rules合并到repositories.csv文件第二列中的分支名称,以及第一列的所有仓库。

./vendor/bin/patchbot batch merge --source=feature-add-phpcs-rules

不同的分支名称

当在patchmerge子命令中使用的分支名称不同,或者需要将功能分支合并到几个阶段分支时,您可能需要提供一个包含所有分支的文件,并传递指定的分支列名称作为选项branch-column

repositories.csv - 示例文件内容,具有多个分支列

repository-url,main,development,integration-stage,test-stage
https://git.example.com/projecta,main,development,integration,testing
https://git.example.com/projectb,main,dev,stage/integration,stage/test
https://git.example.com/projectc,live,development,stage/integration,stage/test

将补丁rename-changelog应用到基于列development中给出的分支名称的功能分支feature-rename-changelog

./vendor/bin/patchbot batch patch --branch-column=development patch-name=rename-changelog branch-name=feature-rename-changelog

现在将功能分支合并到列test-stage中给出的分支名称,然后合并到列integration-stage中给出的分支。

./vendor/bin/patchbot batch merge --branch-column=test-stage source=feature-rename-changelog
./vendor/bin/patchbot batch merge --branch-column=integration-stage source=feature-rename-changelog

许可证

GNU通用公共许可证版本2或更高版本

GNU通用公共许可证可以在https://gnu.ac.cn/copyleft/gpl.html找到。

作者

丹·恩滕茨(mail@pixelbrackets.de / @pixelbrackets

CHANGELOG.md

贡献

此脚本是开源的,因此请使用、分享、修复、扩展或分叉它。

贡献受到欢迎!

反馈

请发送一些反馈,并分享此包如何证明对您有用,或者您如何帮助改进它。