{"id":207139,"date":"2025-07-08T10:09:46","date_gmt":"2025-07-08T02:09:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207139\/"},"modified":"2025-07-08T10:09:46","modified_gmt":"2025-07-08T02:09:46","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-golang-%e8%ae%a1%e7%ae%97%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84-git-blob-sha","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207139\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 golang \u8ba1\u7b97\u5b57\u7b26\u4e32\u7684 git blob sha"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u4f7f\u7528 golang \u8ba1\u7b97\u5b57\u7b26\u4e32\u7684 git blob sha<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 12:27:40<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 golang \u8ba1\u7b97\u5b57\u7b26\u4e32\u7684 git blob sha\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5982\u4f55\u8ba1\u7b97\u5b83\u7684 <strong>git<\/strong> BLOB SHA\u3002\u6211\u5c1d\u8bd5\u4f7f\u7528golang\u63d0\u4f9b\u7684api\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e0a\u7684sha\uff0c\u4f46\u5b83\u4e0egit blob sha\u4e0d\u5339\u914d\u3002<\/p>\n<p>\u5982\u4f55\u4f7f\u7528 golang \u83b7\u53d6\u4e0e git blob sha \u76f8\u540c\u7684 sha<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>git \u8ba1\u7b97 blob sha \u7684\u65b9\u5f0f\u6709\u70b9\u4e0d\u540c\u3002\u5b83\u5728\u524d\u9762\u6dfb\u52a0\u4e00\u4e2a\u6807\u5934 <code>blob &lt;length&gt;\\x00&lt;content&gt;<\/code><\/p>\n<p>\u8fd9\u91cc<code>&lt;length&gt;<\/code>\u662f\u5185\u5bb9\u7684\u5b57\u8282\u957f\u5ea6\uff0c<code>\\x00<\/code>\u662f\u7ed3\u675f\u5206\u9694\u7b26\uff0c<code>&lt;content&gt;<\/code>\u662f\u5b9e\u9645\u7684\u5185\u5bb9\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u6211\u521b\u5efa\u4e86\u4e0b\u9762\u7684\u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u7ed9\u5b9a\u5b57\u7b26\u4e32\u7684 git blob sha<\/p>\n<pre>func computeGitBlobSHA(s string) string {\n    p := fmt.Sprintf(\"blob %d\\x00\", len(s))\n    h := sha1.New()\n    h.Write([]byte(p))\n    h.Write([]byte(s))\n    return hex.EncodeToString(h.Sum([]byte(nil)))\n}<\/pre>\n<p>\u56e0\u6b64\u8c03\u7528 <code>computegitblobsha(\"hello\")<\/code> \u5c06\u8fd4\u56de <code>5ab2f8a4323abafb10abb68657d9d39f1a775057<\/code>\uff0c\u8fd9\u662f\u5b9e\u9645\u7684 git sha\u3002<\/p>\n<p>\u8bf7\u62e8\u6253 \u67e5\u770b\u6b64\u4fe1\u606f<\/p>\n<p>\u5982\u679c\u9700\u8981\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>echo -n 'hello' | \u8fdb\u884c\u9a8c\u8bc1\u3002 git hash-object --stdin<\/code><\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 golang \u8ba1\u7b97\u5b57\u7b26\u4e32\u7684 git blob sha\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207139","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207139"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207139\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}