我试图理解git如何计算refs的哈希值.
$ git ls-remote https://github.com/git/git
....
29932f3915935d773dc8d52c292cadd81c81071d refs/tags/v2.4.2
9eabf5b536662000f79978c4d1b6e4eff5c8d785 refs/tags/v2.4.2^{}
....
Run Code Online (Sandbox Code Playgroud)
在本地克隆回购.refs/tags/v2.4.2^{}
通过sha 检查ref
$ git cat-file -p 9eabf5b536662000f79978c4d1b6e4eff5c8d785
tree 655a20f99af32926cbf6d8fab092506ddd70e49c
parent df08eb357dd7f432c3dcbe0ef4b3212a38b4aeff
author Junio C Hamano <gitster@pobox.com> 1432673399 -0700
committer Junio C Hamano <gitster@pobox.com> 1432673399 -0700
Git 2.4.2
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Run Code Online (Sandbox Code Playgroud)
复制解压缩的内容,以便我们可以散列它.(AFAIK git在散列时使用未压缩的版本)
git cat-file -p 9eabf5b536662000f79978c4d1b6e4eff5c8d785 > fi
Run Code Online (Sandbox Code Playgroud)
让我们使用git自己的哈希命令对内容进行SHA-1
git hash-object fi
3cf741bbdbcdeed65e5371912742e854a035e665
Run Code Online (Sandbox Code Playgroud)
为什么输出不是[9e]abf5b536662000f79978c4d1b6e4eff5c8d785
?我理解前两个字符(9e
)是十六进制的长度.我该如何散列内容fi
以便我可以获得git ref abf5b536662000f79978c4d1b6e4eff5c8d785
?
Mat*_*aun 37
This video by John Williams gives an overview of what data goes into the calculation of a Git commit hash. Here\'s a screenshot from the video:
\nTo get a deeper understanding of this aspect of Git, I reimplemented the steps that produce a Git commit hash in Rust, without using Git. It currently works for getting the hash when committing a single file. The answers here were helpful in achieving this, thanks.
\nThe source code of this answer is available here. Execute it with cargo run
.
These are the individual pieces of data we need to compute to arrive at a Git commit hash:
\nhash-object
provides this ID.ls-tree
, but their format in the tree object is slightly different: [mode] [file name]\\0[object ID]
tree [size of object entries]\\0[object entries]
. In Git, get the tree hash with: git cat-file commit HEAD | head -n1
cat-file
. This includes the tree object hash and commit information like author, time, commit message, and the parent commit hash if it\'s not the first commit.Each step depends on the previous one. Let\'s start with the first.
\nThe first step is to reimplement Git\'s hash-object
, as in git hash-object your_file
.
We create the object hash from our file by concatenating and hashing these data:
\n\\0
in printf
and Rust, followed byIn Bash:
\nfile_name="your_file";\nprintf "blob $(wc -c < "$file_name")\\0$(cat "$file_name")" | sha1sum\n
Run Code Online (Sandbox Code Playgroud)\nIn Rust:
\n// Get the object ID\nfn git_hash_object(file_content: &[u8]) -> Vec<u8> {\n let file_size = file_content.len().to_string();\n let hash_input = [\n "blob ".as_bytes(),\n file_size.as_bytes(),\n b"\\0",\n file_content,\n ]\n .concat();\n to_sha1(&hash_input)\n}\n
Run Code Online (Sandbox Code Playgroud)\nI\'m using crate sha1 version 0.10.5 in to_sha1
:
fn to_sha1(hash_me: &[u8]) -> Vec<u8> {\n use sha1::{Digest, Sha1};\n\n let mut hasher = Sha1::new();\n hasher.update(hash_me);\n hasher.finalize().to_vec()\n}\n
Run Code Online (Sandbox Code Playgroud)\nObject entries are part of Git\'s tree object. Tree objects represent files and directories.
\nObject entries for files have this form: [mode] [file name]\\0[object ID]
We assume the file is a regular, non-executable file, which translates to mode 100644 in Git. See this for more on modes.
\nThis Rust function takes the result of the previous function git_hash_object
as the parameter object_id
:
fn object_entry(file_name: &str, object_id: &[u8]) -> Vec<u8> {\n // It\'s a regular, non-executable file\n let mode = "100644";\n\n // [mode] [file name]\\0[object ID]\n let object_entry = [\n mode.as_bytes(),\n b" ",\n file_name.as_bytes(),\n b"\\0",\n object_id,\n ]\n .concat();\n\n object_entry\n}\n
Run Code Online (Sandbox Code Playgroud)\nI tried to write the equivalent of object_entry
in Bash, but Bash variables cannot contain null bytes. There are probably ways around that limitation, but I decided for now that if I can\'t have variables in Bash, the code would get quite difficult to understand. Edits providing a readable Bash equivalent are welcome.
As mentioned above, tree objects represent files and directories in Git. You can see the hash of your tree object by running, for example, git cat-file commit HEAD | head -n1
.
The tree object has this form: tree [size of object entries]\\0[object entries]
In our case we only have a single object_entry
, calculated in the previous step:
fn tree_object_hash(object_entry: &[u8]) -> String {\n let object_entry_size = object_entry.len().to_string();\n\n let tree_object = [\n "tree ".as_bytes(),\n object_entry_size.as_bytes(),\n b"\\0",\n object_entry,\n ]\n .concat();\n\n to_hex_str(&to_sha1(&tree_object))\n}\n
Run Code Online (Sandbox Code Playgroud)\nWhere to_hex_str
is defined as:
// Converts bytes to their hexadecimal representation.\nfn to_hex_str(bytes: &[u8]) -> String {\n bytes.iter().map(|byte| format!("{byte:02x}")).collect()\n}\n
Run Code Online (Sandbox Code Playgroud)\nIn a Git repo, you can look at the contents of the tree object with ls-tree
. For example, running git ls-tree HEAD
will produce lines like these:
100644 blob b8c0d74ef5ccd3dab583add7b3f5367efe4bf823 your_file\n
Run Code Online (Sandbox Code Playgroud)\nWhile those lines contain the data of an object entry (the mode, the object ID, and the file name), they are in a different order and include a tab character as well as the string "blob" which is input to the object ID, not the object entry. Object entries have this form: [mode] [file name]\\0[object ID]
The last step creates the commit hash.
\nThe data we hash using SHA-1 includes:
\nYou can see all of that data with git cat-file commit HEAD
, for example:
tree a76b2df314b47956268b0c39c88a3b2365fb87eb\nparent 9881a96ab93a3493c4f5002f17b4a1ba3308b58b\nauthor Matthias Braun <m.braun@example.com> 1625338354 +0200\ncommitter Matthias Braun <m.braun@example.com> 1625338354 +0200\n\nSecond commit (that\'s the commit message)\n
Run Code Online (Sandbox Code Playgroud)\nYou might have guessed that 1625338354
is a timestamp. In this case it\'s the number of seconds since the Unix epoch. You can convert from the date and time format of git log
, such as "Sat Jul 3 20:52:34 2021", to Unix epoch seconds with date
:
100644 blob b8c0d74ef5ccd3dab583add7b3f5367efe4bf823 your_file\n
Run Code Online (Sandbox Code Playgroud)\nThe time zone is denoted as +0200
in this example.
Based on the output of cat-file
, you can create the Git commit hash using this Bash command (which uses git cat-file
, so it\'s no reimplementation):
tree a76b2df314b47956268b0c39c88a3b2365fb87eb\nparent 9881a96ab93a3493c4f5002f17b4a1ba3308b58b\nauthor Matthias Braun <m.braun@example.com> 1625338354 +0200\ncommitter Matthias Braun <m.braun@example.com> 1625338354 +0200\n\nSecond commit (that\'s the commit message)\n
Run Code Online (Sandbox Code Playgroud)\nThe Bash command illustrates that\xe2\x80\x94similar to the steps before\xe2\x80\x94what we hash is:
\ncat-file
which is detailed above. Followed bycat-file
) with a line break at the end.In case you kept score: Creating a Git commit hash involves using SHA-1 at least three times.
\nBelow is the Rust function for creating the Git commit hash. It uses the tree_object_hash
produced in the previous step and a struct CommitMetaData
which contains the rest of the data you see when calling git cat-file commit HEAD
. The function also takes care of whether the commit has a parent commit or not.
date --date=\'Sat Jul 3 20:52:34 2021\' +"%s"\n
Run Code Online (Sandbox Code Playgroud)\nHere\'s CommitMetaData
:
cat_file_output=$(git cat-file commit HEAD);\nprintf "commit $(wc -c <<< "$cat_file_output")\\0$cat_file_output\\n" | sha1sum\n
Run Code Online (Sandbox Code Playgroud)\nThis function creates CommitMetaData
where author and committer info are identical, which will be convenient when we run the program later:
fn commit_hash(commit: &CommitMetaData, tree_object_hash: &str) -> Vec<u8> {\n let author_line = format!(\n "{} {}",\n commit.author_name_and_email, commit.author_timestamp_and_timezone\n );\n let committer_line = format!(\n "{} {}",\n commit.committer_name_and_email, commit.committer_timestamp_and_timezone\n );\n\n // If it\'s the first commit, which has no parent,\n // the line starting with "parent" is omitted\n let parent_commit_line = match commit.parent_commit_hash {\n Some(parent_commit_hash) => format!("\\nparent {parent_commit_hash}"),\n None => "".to_string(),\n };\n let git_cat_file_str = format!(\n "tree {}{}\\nauthor {}\\ncommitter {}\\n\\n{}\\n",\n tree_object_hash, parent_commit_line, author_line, committer_line, commit.commit_message\n );\n\n let git_cat_file_len = git_cat_file_str.len().to_string();\n\n let commit_object = [\n "commit ".as_bytes(),\n git_cat_file_len.as_bytes(),\n b"\\0",\n git_cat_file_str.as_bytes(),\n ]\n .concat();\n\n // Return the Git commit hash\n to_sha1(&commit_object)\n}\n
Run Code Online (Sandbox Code Playgroud)\nAs a summary and reminder, creating a Git commit hash consists of getting:
\nhash-object
provides this ID.ls-tree
, but their format in the tree object is slightly different: [mode] [file name]\\0[object ID]
tree [size of object entries]\\0[object entries]
. In Git, get the tree hash with: git cat-file commit HEAD | head -n1
cat-file
. This includes the tree object hash and commit information like author, time, commit message, and the parent commit hash if it\'s not the first commit.In Rust:
\n#[derive(Debug, Copy, Clone)]\npub struct CommitMetaData<\'a> {\n pub(crate) author_name_and_email: &\'a str,\n pub(crate) author_timestamp_and_timezone: &\'a str,\n pub(crate) committer_name_and_email: &\'a str,\n pub(crate) committer_timestamp_and_timezone: &\'a str,\n pub(crate) commit_message: &\'a str,\n // All commits after the first one have a parent commit\n pub(crate) parent_commit_hash: Option<&\'a str>,\n}\n
Run Code Online (Sandbox Code Playgroud)\nWith the functions above, you can create a file\'s Git commit hash in Rust, without Git:
\npub fn simple_commit<\'a>(\n author_name_and_email: &\'a str,\n author_timestamp_and_timezone: &\'a str,\n commit_message: &\'a str,\n parent_commit_hash: Option<&\'a str>,\n) -> CommitMetaData<\'a> {\n CommitMetaData {\n author_name_and_email,\n author_timestamp_and_timezone,\n committer_name_and_email: author_name_and_email,\n committer_timestamp_and_timezone: author_timestamp_and_timezone,\n commit_message,\n parent_commit_hash,\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\nTo create the hash of the second commit, you take the hash of the first commit and put it into the CommitMetaData
of the second commit:
pub fn get_commit_hash(\n file_name: &str,\n file_content: &[u8],\n commit: &CommitMetaData\n) -> String {\n let file_object_id = git_hash_object(file_content);\n let object_entry = object_entry(file_name, &file_object_id);\n let tree_object_hash = tree_object_hash(&object_entry);\n\n let commit_hash = commit_hash(commit, &tree_object_hash);\n to_hex_str(&commit_hash)\n}\n
Run Code Online (Sandbox Code Playgroud)\n除了此处的其他答案及其链接之外,这些是创建我的有限重新实现的一些有用资源:
\ngit hash-object
。Von*_*onC 11
如" 如何形成git commit sha1 "中所述,公式为:
(printf "<type> %s\0" $(git cat-file <type> <ref> | wc -c); git cat-file <type> <ref>)|sha1sum
Run Code Online (Sandbox Code Playgroud)
在提交 9eabf5b536662000f79978c4d1b6e4eff5c8d785 的情况下(也就是v2.4.2^{}
引用树):
(printf "commit %s\0" $(git cat-file commit 9eabf5b536662000f79978c4d1b6e4eff5c8d785 | wc -c); git cat-file commit 9eabf5b536662000f79978c4d1b6e4eff5c8d785 )|sha1sum
Run Code Online (Sandbox Code Playgroud)
这将给出9eabf5b536662000f79978c4d1b6e4eff5c8d785.
如:
(printf "commit %s\0" $(git cat-file commit v2.4.2{} | wc -c); git cat-file commit v2.4.2{})|sha1sum
Run Code Online (Sandbox Code Playgroud)
(仍然是9eabf5b536662000f79978c4d1b6e4eff5c8d785)
同样,计算标签v2.4.2的SHA1将是:
(printf "tag %s\0" $(git cat-file tag v2.4.2 | wc -c); git cat-file tag v2.4.2)|sha1sum
Run Code Online (Sandbox Code Playgroud)
这将给出29932f3915935d773dc8d52c292cadd81c81071d.
这里有点混乱.Git使用不同类型的对象:blob,trees和commit.以下命令:
git cat-file -t <hash>
Run Code Online (Sandbox Code Playgroud)
告诉您给定哈希的对象类型.因此,在您的示例中,散列9eabf5b536662000f79978c4d1b6e4eff5c8d785对应于提交对象.
现在,当你想出自己,运行这个:
git cat-file -p 9eabf5b536662000f79978c4d1b6e4eff5c8d785
Run Code Online (Sandbox Code Playgroud)
根据类型(在本例中为提交)提供对象的内容.
但是这个:
git hash-object fi
Run Code Online (Sandbox Code Playgroud)
...计算blob的哈希,其内容是上一个命令的输出(在您的示例中),但它可能是其他任何内容(如"hello world!").试试这个:
echo "blob 277\0$(cat fi)" | shasum
Run Code Online (Sandbox Code Playgroud)
输出与上一个命令相同.这基本上是Git哈希的一个blob.因此,通过散列fi,您将生成blob对象.但正如我们所见,9eabf5b536662000f79978c4d1b6e4eff5c8d785是一个提交,而不是一个blob.所以,你不能为了得到相同的哈希而散列fi.
提交的哈希基于其他一些使其独特的信息(例如提交者,作者,日期等).以下文章准确地告诉您提交的哈希是什么:
因此,您可以通过提供与文章中指定的所有数据完全相同的值来获得相同的哈希值.
这可能也有帮助: