繁体   English   中英

有没有办法验证 git sha 是否属于带有 go-github 客户端的 git 分支?

[英]is there a way to verify if a git sha belongs to a git branch with go-github client?

想使用 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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM