如何使用Go语言获取给定仓库中某个文件的Github提交历史记录。

huangapple go评论91阅读模式
英文:

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中传递所有者名称,并更改仓库名称。

如果你遇到访问问题,可能是因为它是一个私有仓库。你也可以使用密钥对来设置访问权限。

go-github库链接

英文:

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.

huangapple
  • 本文由 发表于 2022年11月17日 09:48:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/74469237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定