go - 有没有办法验证 git sha 是否属于带有 go-github 客户端的 git 分支?
问题描述
想使用 go GitHub 客户端来获得类似的结果
git branch -r origin/<branch> --contains <sha>
解决方案
有没有办法验证 git sha 是否属于带有 go-github 客户端的 git 分支?
我似乎无法找到专门回答 SHA 是否属于分支的 API 端点,因此您必须在分支内迭代提交。就像是:
func main() {
ctx := context.Background()
sts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "<token>"},
)
tc := oauth2.NewClient(ctx, sts)
client := github.NewClient(tc)
repoOwner := "<owner>"
repoName := "<repo>"
branchToSearch := "<branch>"
shaToFind := "<sha>"
resultsPerPage := 25
listOptions := github.ListOptions{PerPage: resultsPerPage}
for {
rc, resp, err := client.Repositories.ListCommits(ctx,
repoOwner,
repoName,
&github.CommitsListOptions{
SHA: branchToSearch,
ListOptions: listOptions,
},
)
if err != nil {
log.Panic(err)
}
for _, c := range rc {
if *c.SHA == shaToFind {
log.Printf("FOUND commit \"%s\" in the branch \"%s\"\n", shaToFind, branchToSearch)
return
}
}
if resp.NextPage == 0 {
break
}
listOptions.Page = resp.NextPage
}
log.Printf("NOT FOUND commit \"%s\" in the branch \"%s\"\n", shaToFind, branchToSearch)
}
推荐阅读
- reactjs - React redux 对同一操作的多次调用仅触发 reducer 一次
- c++ - C ++ for循环查找“给定数字的数字总和”
- css - 如何在chartjs [角度]中在图表底部添加空间
- machine-learning - 为什么 xgboost 树的相同路径会给出 2 个不同的预测?
- swift - 一种访问城市名称列表的方法 (OpenWeatherMap)
- flutter - 如何在颤动中更改卡片的宽度?
- c++ - Boost 无法用方括号验证 IPv6
- bash - 为什么交互式 kubectl bash 会话中缺少空格/换行符
- java - 在 log4j2 GelfLayout 中提供方法名称
- python-3.x - Python-Pandas:我想要 groupby 用户和日期范围(-7)天