{"id":207164,"date":"2025-07-08T11:12:43","date_gmt":"2025-07-08T03:12:43","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207164\/"},"modified":"2025-07-08T11:12:43","modified_gmt":"2025-07-08T03:12:43","slug":"%e6%9c%89%e6%b2%a1%e6%9c%89%e5%8a%9e%e6%b3%95%e7%94%a8-go-github-%e5%ae%a2%e6%88%b7%e7%ab%af%e9%aa%8c%e8%af%81-git-sha-%e6%98%af%e5%90%a6%e5%b1%9e%e4%ba%8e-git-%e5%88%86%e6%94%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207164\/","title":{"rendered":"\u6709\u6ca1\u6709\u529e\u6cd5\u7528 go-github \u5ba2\u6237\u7aef\u9a8c\u8bc1 git sha \u662f\u5426\u5c5e\u4e8e git \u5206\u652f\uff1f"},"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>\u6709\u6ca1\u6709\u529e\u6cd5\u7528 go-github \u5ba2\u6237\u7aef\u9a8c\u8bc1 git sha \u662f\u5426\u5c5e\u4e8e git \u5206\u652f\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 15:00:22<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6709\u6ca1\u6709\u529e\u6cd5\u7528 go-github \u5ba2\u6237\u7aef\u9a8c\u8bc1 git sha \u662f\u5426\u5c5e\u4e8e git \u5206\u652f\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u60f3\u4f7f\u7528 go GitHub \u5ba2\u6237\u7aef\u6765\u83b7\u53d6\u5982\u4e0b\u7ed3\u679c <code>git\u5206\u652f-r origin\/&lt;branch&gt; --\u5305\u542b&lt;sha&gt;<\/code><\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u4f3c\u4e4e\u65e0\u6cd5\u627e\u5230\u4e00\u4e2a api \u7aef\u70b9\u6765\u4e13\u95e8\u56de\u7b54 sha \u662f\u5426\u5c5e\u4e8e\u5206\u652f\uff0c\u56e0\u6b64\u60a8\u5fc5\u987b\u5728\u5206\u652f\u5185\u8fed\u4ee3\u63d0\u4ea4\u3002\u50cf\u8fd9\u6837\u7684\u4e1c\u897f\uff1a<\/p>\n<pre>func main() {\n    ctx := context.Background()\n    sts := oauth2.StaticTokenSource(\n        &amp;oauth2.Token{AccessToken: \"&lt;token&gt;\"},\n    )\n    tc := oauth2.NewClient(ctx, sts)\n    client := github.NewClient(tc)\n\n    repoOwner := \"&lt;owner&gt;\"\n    repoName := \"&lt;repo&gt;\"\n    branchToSearch := \"&lt;branch&gt;\"\n    shaToFind := \"&lt;sha&gt;\"\n    resultsPerPage := 25\n\n    listOptions := github.ListOptions{PerPage: resultsPerPage}\n\n    for {\n        rc, resp, err := client.Repositories.ListCommits(ctx,\n            repoOwner,\n            repoName,\n            &amp;github.CommitsListOptions{\n                SHA:         branchToSearch,\n                ListOptions: listOptions,\n            },\n        )\n        if err != nil {\n            log.Panic(err)\n        }\n\n        for _, c := range rc {\n            if *c.SHA == shaToFind {\n                log.Printf(\"FOUND commit \\\"%s\\\" in the branch \\\"%s\\\"\\n\", shaToFind, branchToSearch)\n                return\n            }\n        }\n\n        if resp.NextPage == 0 {\n            break\n        }\n        listOptions.Page = resp.NextPage\n    }\n\n    log.Printf(\"NOT FOUND commit \\\"%s\\\" in the branch \\\"%s\\\"\\n\", shaToFind, branchToSearch)\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u6709\u6ca1\u6709\u529e\u6cd5\u7528 go-github \u5ba2\u6237\u7aef\u9a8c\u8bc1 git sha \u662f\u5426\u5c5e\u4e8e git \u5206\u652f\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\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-207164","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207164","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=207164"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207164\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}