makers99/wp-cli-shared-patches

将补丁应用到WordPress插件中,以便更新到新的供应商版本。


README

共享补丁WP-CLI包将补丁引入WordPress。它允许您在不丢失之前应用于插件的热修复的情况下,更新您的插件到新版本,如果插件供应商没有将其纳入新官方版本。

如果您在团队中并行维护许多WordPress站点,并且不知道哪些热修复被应用到您未参与的其他项目中,这将特别有用。团队可以共享补丁,并在安装更新时自动重新应用它们。

命令

wp plugin patch [<plugin...> | --all]

将补丁应用到一个或多个插件。

允许插件更新到更高版本,同时保留尚未被维护者包含到上游版本的错误修复。

该命令遵循wp plugin update的概要。它可以接受一个或多个要应用补丁的插件名称,或者选项--all

$ wp plugin patch example-plugin other-plugin
$ wp plugin patch --all

补丁作为此包的一部分共享,请参阅文件夹./patches/

wp patch create <plugin> <commit> <type> <keywords>

从Git提交创建新的补丁文件。

$ wp patch create example-plugin c03314 fix keywords-context-info

所有补丁都必须是git format-patch格式的"补丁序列"。它们将使用git am应用。更多关于这一点在创建补丁章节中。

安装

作为WP-CLI包安装(用户特定)

  1. 确保您已安装并设置WP-CLI到您的$PATH
  2. 为当前登录的系统用户安装包。
    wp package install makers99/wp-cli-shared-patches

作为Git子模块安装

  1. 在您的项目根目录下,将包添加为子模块。

    git submodule add --name wp-cli-shared-patches git@github.com:makers99/wp-cli-shared-patches.git .wp-cli/packages/shared-patches
  2. 为早期WP-CLI引导注册命令。

    echo -e "require:\n  - .wp-cli/packages/shared-patches/package.php" >> wp-cli.yml

    或手动

    vi wp-cli.yml
    require:
      - .wp-cli/packages/shared-patches/package.php

使用Composer安装

  1. 使用Composer安装包。

    composer require --dev makers99/wp-cli-shared-patches:dev-master
  2. 为早期WP-CLI引导注册命令。

    echo -e "require:\n  - vendor/makers99/wp-cli-shared-patches/package.php" >> wp-cli.yml

    或手动

    vi wp-cli.yml
    require:
      - vendor/makers99/wp-cli-shared-patches/package.php

需求

  • PHP 7.4或更高版本。

创建补丁

所有补丁都是从现有提交创建的,因此作者、提交者、日期和进一步上下文都包含在补丁文件中。

补丁文件只包含指定提交的更改。如果您有多个提交,请将您的更改压缩成一个单独的提交。

每个补丁文件名必须遵循以下结构,由点分隔

  1. 插件名称(文件夹)。
  2. 补丁编号,从0000开始。自动生成。
  3. 更改类型,可以是"fix""feature"
  4. 提供近似上下文的键词,由连字符分隔。
$ wp patch create example-plugin c03314 fix keywords-context-info

这将在/patches文件夹中创建一个新的补丁文件,您可以将它添加到仓库中。

这是该命令在幕后如何工作的

$ export PATCHES_DIR=.wp-cli/packages/shared-patches/patches
$ export COMMIT=abcdefgh
$ cd wp-content/plugins/example-plugin
$ git format-patch --relative --stdout $COMMIT~1..$COMMIT > \
  $PATCHES_DIR/example-plugin.0001.fix.relevant-context-keywords.patch

(仅为了清晰起见使用变量PATCHES_DIRCOMMIT)

注意事项

  • 在GitHub上,您可以将.patch追加到提交(或PR)的URL末尾,以获取补丁序列格式的更改。(示例)

    • 如果提交来自网站项目仓库而不是插件仓库,您需要调整 .patch 文件中的文件路径使其相对于插件文件夹。

    • 请确保下载原始的原始文件内容,不要复制/粘贴浏览器页面上的内容,因为浏览器中的 HTML/web 格式不包括关键的空白字符。您可以使用浏览器控制台的 选项卡下载原始原始响应。

  • git format-patch 不包括合并提交。您可以按照以下 https://stackoverflow.com/a/8840381/811306 中的说明创建与 git am 兼容的补丁。

记录上游信息

如果上游供应商仓库或问题跟踪器中存在公开的问题或 PR,请确保将其添加到补丁的提交描述中

Upstream: https://github.com/makers99/wp-cli-shared-patches/pull/10

(URL 应指向上游供应商问题。)

理想情况下,在初始/原始提交信息中添加此信息。

解决补丁冲突

重复错误信息中看到的失败命令;例如

$ git am --directory 'wp-content/plugins/woocommerce-german-market' --3way --keep-cr '.wp-cli/packages/shared-patches/patches/woocommerce-german-market.0001.feature.shop-standards-delivery-times.patch'

这会导致一个 git am 合并冲突,您可以像解决其他合并或变基冲突一样解决它。

$ git am --show-current-patch
$ vi wp-content/plugins/woocommerce-german-market/inc/WGM_Template.php
# Resolve conflict markers as with a regular merge

$ git status
$ git add wp-content/plugins/woocommerce-german-market/inc/WGM_Template.php
$ git status

$ git am --continue

$ wp patch create woocommerce-german-market d8e0e9b67 feature shop-standards-delivery-times
$ cd .wp-cli/packages/shared-patches/patches/
$ rm woocommerce-german-market.0001.feature.shop-standards-delivery-times.patch
$ mv woocommerce-german-market.0005.feature.shop-standards-delivery-times.patch woocommerce-german-market.0001.feature.shop-standards-delivery-times.patch
$ git status
$ git add woocommerce-german-market.0001.feature.shop-standards-delivery-times.patch
$ git commit -m "Updated woocommerce-german-market shop-standards delivery times patch."
$ git push
$ cd -

但是,如果您看到冲突标记但没有实际更改(受补丁影响的所有文件),那么补丁已经被纳入新的上游版本。

$ git am --show-current-patch
$ vi wp-content/plugins/woocommerce-german-market/inc/WGM_Installation.php
# Search for conflict markers:
/<<<<<

$ vi wp-content/plugins/woocommerce-german-market/WooCommerce-German-Market.php
# Search for conflict markers:
/<<<<<

$ git status
$ git am --abort

$ cd .wp-cli/packages/shared-patches/patches
$ git rm woocommerce-german-market.0004.fix.performance-admin_url-string-translations.patch
$ git commit -m "Removed patch woocommerce-german-market.0004.fix.performance-admin_url-string-translations."
$ cd -

解决补丁错误

有时补丁可能会因为 diff 环境中的其他更改而出现错误。测试以下 git am 选项,看看是否可以继续应用补丁

    --ignore-whitespace   ignore changes in whitespace when finding context
    -C <n>                ensure at least <n> lines of context match

示例

$ git am --directory local-plugins/wp-lister-amazon --3way --keep-cr .wp-cli/packages/shared-patches/patches/wp-lister-amazon.0001.*.patch
Applying: Added order param to filter hook to amazon lister plugin.
error: sha1 information is lacking or useless (local-plugins/wp-lister-amazon/classes/integration/Woo_OrderBuilder.php).
error: could not build fake ancestor
Patch failed at 0001 Added order param to filter hook to amazon lister plugin.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

$ git am --directory local-plugins/wp-lister-amazon --3way --keep-cr --ignore-whitespace -C 0 .wp-cli/packages/shared-patches/patches/wp-lister-amazon.0001.*.patch
Applying: Added order param to filter hook to amazon lister plugin.