获取特定分支的标签

使用go-git:有没有办法只获取特定分支的(轻量级和带注释的)标签?

由于我主要对主分支的标签感兴趣,所以类似的东西git tag --merged也足够了。

使用基本的 go-git 方法似乎不可能,例如Tags()......


温温酱
浏览 154回答 1
1回答

慕村9548890

不完全是一个简短的解决方案,但以下代码通过以下方式实现了目标:读取整个分支的提交哈希值。读取存储库的所有标签。检查并仅打印散列位于分支中的标签。注意:尚未尝试使用带注释的标签。但应该很接近。package mainimport (&nbsp; &nbsp; "log"&nbsp; &nbsp; "github.com/src-d/go-billy/memfs"&nbsp; &nbsp; "gopkg.in/src-d/go-git.v4"&nbsp; &nbsp; "gopkg.in/src-d/go-git.v4/plumbing"&nbsp; &nbsp; "gopkg.in/src-d/go-git.v4/plumbing/object"&nbsp; &nbsp; "gopkg.in/src-d/go-git.v4/storage/memory")func getBranchHashes(repo *git.Repository, branchName string) (hashes map[plumbing.Hash]bool, err error) {&nbsp; &nbsp; // get branch reference name&nbsp; &nbsp; branch, err := repo.Branch("master")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; // get reference of the reference name&nbsp; &nbsp; ref, err := repo.Reference(branch.Merge, true)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; // retrieve logs from the branch reference commit&nbsp; &nbsp; // (default order is depth first)&nbsp; &nbsp; logs, err := repo.Log(&git.LogOptions{&nbsp; &nbsp; &nbsp; &nbsp; From: ref.Hash(),&nbsp; &nbsp; })&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; defer logs.Close()&nbsp; &nbsp; // a channel to collect all hashes&nbsp; &nbsp; chHash := make(chan plumbing.Hash)&nbsp; &nbsp; chErr := make(chan error)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; err = logs.ForEach(func(commit *object.Commit) (err error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chHash <- commit.Hash&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chErr <- err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; close(chErr)&nbsp; &nbsp; &nbsp; &nbsp; close(chHash)&nbsp; &nbsp; }()&nbsp; &nbsp; // make all hashes into a map&nbsp; &nbsp; hashes = make(map[plumbing.Hash]bool)hashLoop:&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case err = <-chErr:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break hashLoop&nbsp; &nbsp; &nbsp; &nbsp; case h := <-chHash:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hashes[h] = true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return}type TagRef struct {&nbsp; &nbsp; Hash plumbing.Hash&nbsp; &nbsp; Name string}func main() {&nbsp; &nbsp; // Filesystem abstraction based on memory&nbsp; &nbsp; fs := memfs.New()&nbsp; &nbsp; // Git objects storer based on memory&nbsp; &nbsp; storer := memory.NewStorage()&nbsp; &nbsp; // Clones the repository into the worktree (fs) and storer all the .git&nbsp; &nbsp; // content into the storer&nbsp; &nbsp; repo, err := git.Clone(storer, fs, &git.CloneOptions{&nbsp; &nbsp; &nbsp; &nbsp; URL: "https://github.com/yookoala/gofast.git",&nbsp; &nbsp; })&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; hashes, err := getBranchHashes(repo, "master")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; // get all tags in the repo&nbsp; &nbsp; tags, err := repo.Tags()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; tagRefs := make(chan TagRef)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; err = tags.ForEach(func(ref *plumbing.Reference) (err error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if annotedTag, err := repo.TagObject(ref.Hash()); err != plumbing.ErrObjectNotFound {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if annotedTag.TargetType == plumbing.CommitObject {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tagRefs <- TagRef{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hash: annotedTag.Target,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name: ref.Name().Short(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tagRefs <- TagRef{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hash: ref.Hash(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name: ref.Name().Short(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; close(tagRefs)&nbsp; &nbsp; }()&nbsp; &nbsp; for tagRef := range tagRefs {&nbsp; &nbsp; &nbsp; &nbsp; if _, ok := hashes[tagRef.Hash]; ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Printf("tag: %s, hash: %s", tagRef.Name, tagRef.Hash)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go