可以将其他数据放入git commit对象吗?

问题描述 投票:7回答:2

在每个关于git对象的教程网站上,我将看到一个示例,其中git cat-file显示提交对象本身的样子,它通常类似于以下内容:

tree 552acd444696ccb1c3afe68a55ae8b20ece2b0e6
parent 6a1d380780a83ef5f49523777c5e8d801b7b9ba2
author John Doe <[email protected]> 1326496982 -0600
committer John Doe <[email protected]> 1326496982 -0600

Some commit message here.

如果顶部有其他(单行)数据字段,这会混淆git工具吗?我假设即使没有,这些字段也不会出现在git log中。

如果它可以有其他字段,哪个git plumbing命令可以让你创建它? git commit-tree似乎不灵活。

git git-commit
2个回答
4
投票

将数据附加到提交的传统方式是“预告片”:提交消息末尾的特殊格式化行。由Signed-of-by git commit -s添加的(docu)线是这样的预告片。

Git对这些有一些直接的支持:

  • git interpret-trailers (docu)是添加或解析这些行的助手
  • git log有一个格式占位符%(trailers[:options]) (docu)
  • git for-each-ref也有一个%(trailers)占位符(docu)

预告片与git notes有一些不同之处:

  • 预告片是提交消息的一部分,因此被哈希覆盖。因此它们是不可改变的。 git notes是提交(或其他东西)的附加组件。
  • 预告片数据被克隆并推送到远程存储库或从远程存储库推送。 git notes不是默认的。
  • 预告片数据由git cherry-pickgit rebase和其他人移动。 git notes ......有时候。

3
投票

git notes将为任何提交添加任意注释。用法:git notes add <SHA>,例如: git notes add HEAD。这将启动默认文本编辑器并允许您输入注释。您也可以使用与commit相同的方式从CLI指定消息,即git notes add -m "my message" HEAD

如果你在记笔记后运行git log,你会看到:

commit 06ca03a20bb01203e2d6b8996e365f46cb6d59bd
Author: Example User <[email protected]>
Date:   Mon March 28 14:50:15 2016 -0700

    commit message

Notes:
    my message

Notes也可以给出名称空间。 This article有完整的文档。


0
投票

是的,你可以在那里完全放置更多名为“git commit extra headers”的字段。它们会出现在git cat-file -p中,但我认为首先使用标准git客户端将它们放在那里的唯一方法是编辑git源并重新编译。

commit.h中,有一个额外标题的struct,以及在提交时添加它们的函数,以及稍后读出它们。

struct commit_extra_header {
        struct commit_extra_header *next;
        char *key;
        char *value;
        size_t len;
};

int commit_tree_extended(const char *msg, size_t msg_len,
                        const struct object_id *tree,
                        struct commit_list *parents,
                        struct object_id *ret, const char *author,
                        const char *sign_commit,
                        struct commit_extra_header *);

struct commit_extra_header *read_commit_extra_headers(struct commit *, const char **);

commit.c包含“标准”标题字段列表:

static inline int standard_header_field(const char *field, size_t len)
{
        return ((len == 4 && !memcmp(field, "tree", 4)) ||
                (len == 6 && !memcmp(field, "parent", 6)) ||
                (len == 6 && !memcmp(field, "author", 6)) ||
                (len == 9 && !memcmp(field, "committer", 9)) ||
                (len == 8 && !memcmp(field, "encoding", 8)));
}

其他一切都显示为“额外”标题。

快速查看源代码,我发现的唯一当前用途是用于在提交时包含GPG签名的gpgsig头。

Kiln Harmony,现已停产,在这里使用kilnhgusernamekilnhgrawdescription等领域。

© www.soinside.com 2019 - 2024. All rights reserved.