简体   繁体   English

如何在 Golang 中使用 git2go 获取 github 中的最新标签?

[英]How to get latest tag in github using git2go in Golang?

I am trying to get the latest github tag from my local repository in golang.我正在尝试从 golang 的本地存储库中获取最新的 github 标记。 For now I am getting all the tags, code is below:现在我得到了所有的标签,代码如下:

repo, err := git.OpenRepository("/home/defiant/Temp/repo")
    checkErr(err)
    re, err := repo.Tags.List()
    checkErr(err)
    fmt.Println(re)

The result returned is of the format:返回的结果格式为:

[0.1 1.3 1.4]

Here 1.3 is the latest tag.这里 1.3 是最新的标签。 So can someone please help me out in getting only the latest tag?那么有人可以帮我只获取最新的标签吗?

The bit you are looking for is the signature object (it has a when field).您正在寻找的位是signature对象(它有一个when字段)。 On a commit, you might be interested in either the author or the committer .在提交时,您可能对authorcommitter感兴趣。 If you are using annotated tags themselves - they have the tagger field which is when the commit was tagged.如果您自己使用带annotated标签 - 它们具有tagger提交时的tagger字段。

If you have only annotated tags (ie no lightweight) this should get you the timestamps for the tags:如果您只有带注释的标签(即没有轻量级),这应该为您提供标签的时间戳:

repo, err := git.OpenRepository("/home/defiant/Temp/repo")
if err != nil {
    panic(err)
}
err = repo.Tags.Foreach(func(name string, id *git.Oid) error {
    tag, err := repo.LookupTag(id)
    if err != nil {
        return err
    }
    log.Info(tag.Tagger().When)
    return nil
})

Working with lightweight tags you will need to resolve the commit directly - you should be able to do this with LookupCommit and explicitly choose the committer or the author .使用轻量级标签,您将需要直接解决提交问题——您应该能够使用LookupCommit来做到这一点,并明确选择committerauthor

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

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