如何使用 git-send-mail 给开源社区提交 Patch

需求背景

如果参与 Linux、QEMU 或者 OpenSBI 等开源项目,不能通过在 GitHub 或者 Gitlab 平台提交pull request。而是需要将修改的代码,通过 Patch 形式提交到对应的listserv供 Maintainer 审核。那么如何创建 Patch 并发送呢?

这里以向 OpenSBI 提交一个 Patch 为例。

创建 Patch

首先将官方 Repository,Fork 到自己的 GitHub:

回到自己的主页,找到刚刚 Fork 的 Repository,将其 Clone 到本地:

修改代码与正常开发流程一直,修改完在git commit时需要加上Signed-off-by字段,因为 Merge 代码的人通常不是提交代码的人,有该字段才能证明是你修改了对应的代码。

-s参数会自动加上Signed-off-by字段:

$ git commit -s

doc:fix some typos

Signed-off-by: dominic <dominic@gmail.com>

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Sep 27 21:11:41 2022 +0800
#
# On branch master
# Your branch is up to date with 'origin/master'.
#
# Changes to be committed:
#       modified:   docs/domain_support.md
#       modified:   docs/library_usage.md
#       modified:   docs/platform_requirements.md
#       modified:   docs/pmu_support.md

生成.patch文件:

$ git format-patch HEAD^
0001-doc-fix-some-typos.patch

在当前目录下会生成一个0001-doc-fix-some-typos.patch文件:

$ git status 
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        0001-doc-fix-some-typos.patch

nothing added to commit but untracked files present (use "git add" to track)

这个文件就是我们要发送的文件,文件内容就是我们的代码修改,以及作者等信息:

$ cat 0001-doc-fix-some-typos.patch 
From d404cb82f4c4aca15dcd35855d0bc96c5b4431d5 Mon Sep 17 00:00:00 2001
From: Dunky-Z <xxxxxxxxx@qq.com>
Date: Tue, 27 Sep 2022 21:11:41 +0800
Subject: [PATCH] doc:fix some typos

Signed-off-by: dominic <dominic@gmail.com>
---
 docs/domain_support.md        |  6 +++---
 docs/library_usage.md         |  2 +-
 docs/platform_requirements.md |  2 +-
 docs/pmu_support.md           | 10 +++++-----
 4 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/docs/domain_support.md b/docs/domain_support.md
index 73931f1..8963b57 100644
--- a/docs/domain_support.md
+++ b/docs/domain_support.md
@@ -2,7 +2,7 @@ OpenSBI Domain Support
 ======================
...

配置 send-email

安装 git-email

通过git直接发送 Patch 需要使用git-email工具,得手动安装:

sudo apt install git-email

Windows 平台在安装 Git 时默认已安装

生成 smtp 授权码

登录QQ 邮箱 - 帐户

开启 IMAP/SMTP 服务,并生成授权码:

根据提示发送短信:

记录下生成的授权码:

配置.gitconfig

Ubuntu 平台:~/.gitconfig
Windows 平台:C:\Users\用户名\.gitconfig

[sendemail]
 smtpencryption = tls
    smtpserver = smtp.qq.com
    smtpuser = dominic_riscx@qq.com
 smtpserverport = 587
    from = dominic_riscx@qq.com
 smtppass = xxxxxx
 cc = dominic@gmail.com
 #to = opensbi@lists.infradead.org

为了方便复制,单独注释:

[sendemail]
 smtpencryption = # 加密方式,保持默认
    smtpserver =     # smtp 服务器地址,保持默认
    smtpuser =       # 邮箱地址,改为 QQ 邮箱地址,也就是用哪个邮箱发送,就填哪个
 smtpserverport = # 端口号,保持默认
    from =           # 同 smtpuser
 smtppass =       # 上文生成的 smtp 授权码
 cc =             # 抄送的邮箱地址
 #to = opensbi@lists.infradead.org 
    # 要发送的地址,这个字段我注释了,因为怕以后发邮件默认发到这个地址,这个字段可以在发送时单独填写

发送 Patch

#$ git send-email patch文件名
$ git send-email  0001-doc-fix-some-typos.patch
0001-doc-fix-some-typos.patch
# 提示往哪里发送,填写要接收的邮箱即可,我这里填写的是OpenSBI接收Patch的地址
To whom should the emails be sent (if anyone)? opensbi@lists.infradead.org
Message-ID to be used as In-Reply-To for the first email (if any)? 
# 回车,保存默认(我还不清楚这里的作用)
(mbox) Adding cc: Dunky-Z <xxxxxxxxx@qq.com> from line 'From: Dunky-Z <xxxxxxxxx@qq.com>'
(body) Adding cc: dominic <dominic@gmail.com> from line 'Signed-off-by: dominic <dominic@gmail.com>'   

From: dominic_riscx@qq.com
To: opensbi@lists.infradead.org
Cc: dominic@gmail.com,
        Dunky-Z <xxxxxxxxx@qq.com>
Subject: [PATCH] doc:fix some typos
Date: Wed, 28 Sep 2022 10:35:30 +0800
Message-Id: <20220928023530.2344-1-dominic_riscx@qq.com>
X-Mailer: git-send-email 2.34.1.windows.1
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit

    The Cc list above has been expanded by additional
    addresses found in the patch commit message. By default
    send-email prompts before sending whenever this occurs.
    This behavior is controlled by the sendemail.confirm
    configuration setting.

    For additional information, run 'git send-email --help'.
    To retain the current behavior, but squelch this message,
    run 'git config --global sendemail.confirm auto'.

Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): y

# y 确认发送
OK. Log says:
Server: smtp.qq.com
MAIL FROM:<dominic_riscx@qq.com>
RCPT TO:<opensbi@lists.infradead.org>
RCPT TO:<dominic@gmail.com>
RCPT TO:<xxxxxxxxx@qq.com>
From: dominic_riscx@qq.com
To: opensbi@lists.infradead.org
Cc: dominic@gmail.com,
        Dunky-Z <xxxxxxxxx@qq.com>
Subject: [PATCH] doc:fix some typos
Date: Wed, 28 Sep 2022 10:35:30 +0800
Message-Id: <20220928023530.2344-1-dominic_riscx@qq.com>
X-Mailer: git-send-email 2.34.1.windows.1
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit

Result: 250

# 发送成功

前往The opensbi Archives,找到对应的月份,点击Theread,即可找到自己发送的 Patch,每个开源社区一般都会在如何提交 PR 的文档里公开 Patch Archive 网址,这里是以 OpenSBI 的网址。

前往邮箱发送记录中也可以找到对应的 Patch 信息:

以上就是完整的提交 Patch 过程。