如何将项目特定信息添加到 Git 提交注释中?

发布于 2024-09-08 15:57:17 字数 1126 浏览 5 评论 0原文

使用 Git,如果您要提交,它会在提交消息下包含一个被注释掉的部分。其中包含有关编写提交消息的说明以及正在更改的文件列表。像这样:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:  important-file.txt 
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   some-other-thing.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       untracked.txt

是否可以向该文件添加更多材料?一个明确的用例是那些使用问题跟踪软件的用户,例如 TracRedmine 等指定额外的语法元素。例如,Redmine 用户可以包含问题编号和一些特殊关键字来将问题标记为已解决:关键字是“refs”(和“references”)和“fixes”(或“closes”) - 但我经常发现这很困难记住关键词。

那么,如果可以在提交指令的底部附加一些特定于项目的文本,那就会很方便。

有没有现有的方法可以做到这一点,或者我需要破解一种方法吗?作为一个附带问题,其他 VCS(例如 Mercurial)是否有类似的行为?

With Git, if you are committing, it includes a section under the commit message that is commented out. This contains instructions on writing a commit message as well as a list of files that are changing. Like this:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:  important-file.txt 
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   some-other-thing.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       untracked.txt

Is it possible to add further material to this file? One clear use case is for those using issue tracking software like Trac, Redmine and the like is specifying extra syntactical elements. Redmine users, for instance, can include the issue number and some special keywords to mark issues as resolved: the keywords are "refs" (and "references") and "fixes" (or "closes") - but I often find it difficult to remember the keywords.

It would be handy, then, if one could append some project-specific text at the bottom of the commit instructions.

Is there any existing way of doing this or do I need to hack one in? As a side issue, do any other VCSes (like Mercurial) have similar behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飘逸的'云 2024-09-15 15:57:17

我使用了 commit.template(如此处所述)。我本可以使用prepare-commit-msg钩子,但是提交模板可以使这项工作稍微容易一些,而且我可以将提交模板保留在项目的版本控制中。

I used a commit.template (as described here). I could have used a prepare-commit-msg hook, but a commit template does the job slightly easier, plus I can keep the commit template in the version control for the project.

别把无礼当个性 2024-09-15 15:57:17

选项 1:

正如其他人提到的,使用 Git 执行此操作的直接方法是 prepare-commit-消息 钩子。

然而,该策略存在一些必须考虑的问题。来自 Pro Git 第 7.4 章:

[客户端挂钩脚本] 不是
与a的克隆一起转移
项目,您必须分发这些
以其他方式编写脚本,然后有
您的用户将它们复制到他们的
.git/hooks 目录并制作它们
可执行的。您可以分发这些
项目内或
单独的项目,但是没有办法
自动设置它们。

选项2:

Tom Morris,使用提交模板

选项 3:

您可以自定义构建 Git,其中包含附加说明。当前提交消息中包含的指令是硬编码在 Git 源代码中的。请参阅从

此方法可能不是首选方法,因为每次发布新版本的 Git 时都必须应用补丁。

选项 4:

为 Git 创建一个补丁,添加此功能并将其提交到邮件列表。如果您(或其他人)决定尝试此操作,我会首先从列表中寻求有关如何进行的建议。


Mercurial:

Mercurial 中的 Hook 脚本的行为方式类似。摘自《Mercurial:权威》的 第 10 章指导:

在 Mercurial 中,挂钩不是修订版
受控,并且不会传播
您克隆或从存储库中提取。
原因很简单:一个钩子
是完全任意的一块
可执行代码。它在你的下面运行
用户身份,具有您的特权
级别,在您的机器上。

对于任何人来说这都是极其鲁莽的
分布式版本控制系统
实施版本控制的钩子,
因为这将提供一个容易
可利用的方式颠覆
修订版用户帐户
控制系统。

Option 1:

As others have mentioned, the direct way to do this with Git is the prepare-commit-message hook.

However, there are issues with that strategy that must be considered. From Chapter 7.4 of Pro Git:

[Client-side hook scripts] aren’t
transferred with a clone of a
project, you must distribute these
scripts some other way and then have
your users copy them to their
.git/hooks directory and make them
executable. You can distribute these
hooks within the project or in a
separate project, but there is no way
to set them up automatically.

Option 2:

As mentioned by Tom Morris, use a commit template.

Option 3:

You could do a custom build of Git which includes your additional instructions. The instructions included in the current commit message are hard-coded in the Git source code. See $GIT_SRC/builtin/commit.c starting at line 655.

This method is probably not preferred since you would have to apply the patch every time a new version of Git is released.

Option 4:

Create a patch for Git which adds this feature and submit it to the mailing list. If you (or others) decide to try this, I would first ask for advice from the list on how to proceed.


Mercurial:

Hook scripts in Mercurial behave in a similar manner. From Chapter 10 of Mercurial: The Definitive Guide:

In Mercurial, hooks are not revision
controlled, and do not propagate when
you clone, or pull from, a repository.
The reason for this is simple: a hook
is a completely arbitrary piece of
executable code. It runs under your
user identity, with your privilege
level, on your machine.

It would be extremely reckless for any
distributed revision control system to
implement revision-controlled hooks,
as this would offer an easily
exploitable way to subvert the
accounts of users of the revision
control system.

温柔一刀 2024-09-15 15:57:17

您可以通过 prepare-commit-message hook

You can tweak the commit message via the prepare-commit-message hook.

樱花细雨 2024-09-15 15:57:17

我很确定您可以为此使用prepare-commit-msg 挂钩。查看 githooks 了解更多信息。

I'm pretty sure you could use a prepare-commit-msg hook for this. Take a look at githooks for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文