英文:
How to use Go to get the Github commit history of a given file of a repository
问题
如标题所述,我在这里的问题是如何使用Go编程语言以编程方式获取给定存储库中给定文件的Github提交历史记录。
英文:
Like the title said, my question here is how to use Go to programmatically get the Github commit history of a given file in a given repository
答案1
得分: 1
似乎你需要从golang访问GitHub API。有很多库可供使用,但我建议使用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
// 你可以使用commit进行操作
}
}
如果你想访问其他公共仓库,需要在username
中传递所有者名称,并更改仓库名称。
如果你遇到访问问题,可能是因为它是一个私有仓库。你也可以使用密钥对来设置访问权限。
英文:
It seems that you need to access GitHub
api from golang. There are a plenty of libraries but I would recommend using go-github.
Here is how you can try doing that
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
}
}
If you are trying to access the some other public repo, you need to pass the owner name in the username
and change the repo name.
If you face access issues, it can be probably it is a private repo. You can also use the key pair to set up the access.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论