我想要手動獲取GIT提交的SHA1提交哈希,但某些工作不正常。C#計算GIT提交散列
首先我們有一個看起來像這樣的標準提交信息:
tree f594b3f6d9ae291c83902f3992aa36872aa70d68
parent 0000004bf6d464667df5150b4526083886947d92
author User <[email protected]> 1390620460.46263 +0000
committer User <[email protected]> 1390620460.46263 +0000
Commit Message
我們稱之爲的「commitMessage」
的規範說得到承諾說哈希,我們必須SHA1:
- 字符串 「提交」
- 加一個空格 「」
- 加字節在commitMessage
- 數加一空字節
- 加上commitMessage
所以(僞代碼OBV)
SHA1("commit" + " " + numBytes(commitMessage) + '\0' + commitMessage);
而且這是我在C# 實施(我知道這是相當笨拙的)
var commitBody = "tree " + treeHash + "\n\n" +
"parent " + parentHash + "\n\n" +
"author User <[email protected]> " + date + "\n" +
"committer User <[email protected]> " + date + "\n\n" +
"My Commit Message\n";
var blob = "commit " + Encoding.UTF8.GetByteCount(commitBody);
// This is the string "commit " (with a space) + byte count
var first = Encoding.UTF8.GetBytes(blob);
// This is just the null byte
var second = new byte[1];
second[0] = (byte)0;
// This is the commitMessage
var third = Encoding.UTF8.GetBytes(commitBody);
// Merge first, second, third into bytez as a byte array
var bytez = new byte[first.Length + second.Length + third.Length];
Buffer.BlockCopy(first, 0, bytez, 0, first.Length);
Buffer.BlockCopy(second, 0, bytez, first.Length, second.Length);
Buffer.BlockCopy(third, 0, bytez, first.Length + second.Length, third.Length);
// Debug Print
Console.WriteLine(Encoding.UTF8.GetString(bytez));
// Compute the hash and print it
var sss = SHA1.Create();
var myssh = GetString(sss.ComputeHash(bytez));
Console.WriteLine(myssh);
返回的散列與從GIT返回的散列不同。我實際上並沒有期待任何人知道如何做到這一點,因爲這不是通常會做的事情,但我想我會問。
感謝您的幫助:d
雞蛋裏挑骨頭:我想你指的對象,不是一團糟,因爲這關於提交 – alternative
已修復,你說得對! –