如何使用Go获取存储库中给定文件的Github提交历史



就像标题所说的,我的问题是如何使用Go以编程方式获取给定存储库中给定文件的Github提交历史

似乎您需要从golang访问GitHubapi。有很多库,但我建议使用go-github.

你可以这样做

package main
import (
"context"
"github.com/google/go-github/github"
)
func main() {
var username string = "MayukhSobo"
client := github.NewClient(nil)
commits, response, err := client.Repositories.ListCommits(context.Background(), username, "Awesome-Snippets", nil)
if err != nil && response.StatusCode != 200 {
panic(err)
}
for _, commit := range commits {
// commit.SHA
// commit.Files
// You can use the commit
}
}

如果您试图访问其他公共repo,您需要在username中传递所有者名称并更改repo名称。

如果你遇到访问问题,它可能是一个私有的回购。您还可以使用密钥对来设置访问权限。

最新更新