【问题标题】:Get Tags of specific Branch获取特定分支的标签
【发布时间】:2019-09-16 09:47:21
【问题描述】:

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

因为我主要对 master 分支的标签感兴趣,所以像 git tag --merged 这样的标签也足够了。

使用像Tags()这样的基本go-git方法似乎不可能...

【问题讨论】:

  • 啊,你的问题是专门针对go-git的?
  • 是的,很抱歉造成误解...
  • 支持每 github.com/src-d/go-git/blob/master/COMPATIBILITY.md - tag 操作。 git tag --merged <branchname> 不行吗?
  • 没有“分支标签”这样的东西。标签只是标签:它们只是某些特定提交的名称。然而,branch 这个词是模棱两可的:参见stackoverflow.com/questions/25068543/…——如果你的意思是分支names,它们再次只是特定提交的名称,但这些名称进化,即随着时间的推移改变哪个具体的提交。
  • 也就是说,注意git tag --merged 的意思是: 对于每个标签,检查标签是否标识了作为当前提交的祖先的提交;如果是这样,打印该标签 所以它只是:for t := range tags { if t.is_ancestor(current) { print t } },或多或少。 (如何将其映射到 go-git,我不确定。)请注意,Git 自己的 is-ancestor 测试将任何提交 H 视为 H 的祖先:它实际上是一个先验或等于运算符。

标签: git go go-git


【解决方案1】:

不完全是一个简短的解决方案,但以下代码通过以下方式实现了目标:

  1. 读取整个分支的提交哈希。
  2. 读取存储库的所有标签。
  3. 检查并仅打印哈希在分支中的标签。

注意:尚未尝试使用带注释的标签。但它应该很接近。

package main

import (
    "log"

    "github.com/src-d/go-billy/memfs"
    "gopkg.in/src-d/go-git.v4"
    "gopkg.in/src-d/go-git.v4/plumbing"
    "gopkg.in/src-d/go-git.v4/plumbing/object"
    "gopkg.in/src-d/go-git.v4/storage/memory"
)

func getBranchHashes(repo *git.Repository, branchName string) (hashes map[plumbing.Hash]bool, err error) {

    // get branch reference name
    branch, err := repo.Branch("master")
    if err != nil {
        return
    }

    // get reference of the reference name
    ref, err := repo.Reference(branch.Merge, true)
    if err != nil {
        return
    }

    // retrieve logs from the branch reference commit
    // (default order is depth first)
    logs, err := repo.Log(&git.LogOptions{
        From: ref.Hash(),
    })
    if err != nil {
        return
    }
    defer logs.Close()

    // a channel to collect all hashes
    chHash := make(chan plumbing.Hash)
    chErr := make(chan error)
    go func() {
        err = logs.ForEach(func(commit *object.Commit) (err error) {
            chHash <- commit.Hash
            return
        })
        if err != nil {
            chErr <- err
        }
        close(chErr)
        close(chHash)
    }()

    // make all hashes into a map
    hashes = make(map[plumbing.Hash]bool)
hashLoop:
    for {
        select {
        case err = <-chErr:
            if err != nil {
                return
            }
            break hashLoop
        case h := <-chHash:
            hashes[h] = true
        }
    }
    return
}

type TagRef struct {
    Hash plumbing.Hash
    Name string
}

func main() {
    // Filesystem abstraction based on memory
    fs := memfs.New()

    // Git objects storer based on memory
    storer := memory.NewStorage()

    // Clones the repository into the worktree (fs) and storer all the .git
    // content into the storer
    repo, err := git.Clone(storer, fs, &git.CloneOptions{
        URL: "https://github.com/yookoala/gofast.git",
    })
    if err != nil {
        log.Fatal(err)
    }

    hashes, err := getBranchHashes(repo, "master")
    if err != nil {
        log.Fatal(err)
    }

    // get all tags in the repo
    tags, err := repo.Tags()
    if err != nil {
        log.Fatal(err)
    }

    tagRefs := make(chan TagRef)
    go func() {
        err = tags.ForEach(func(ref *plumbing.Reference) (err error) {
            if annotedTag, err := repo.TagObject(ref.Hash()); err != plumbing.ErrObjectNotFound {
                if annotedTag.TargetType == plumbing.CommitObject {
                    tagRefs <- TagRef{
                        Hash: annotedTag.Target,
                        Name: ref.Name().Short(),
                    }
                }
                return nil
            }
            tagRefs <- TagRef{
                Hash: ref.Hash(),
                Name: ref.Name().Short(),
            }
            return
        })
        if err != nil {
            log.Fatal(err)
        }

        close(tagRefs)
    }()

    for tagRef := range tagRefs {
        if _, ok := hashes[tagRef.Hash]; ok {
            log.Printf("tag: %s, hash: %s", tagRef.Name, tagRef.Hash)
        }
    }
}

【讨论】:

    猜你喜欢
    • 2015-08-09
    • 1970-01-01
    • 2020-07-04
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2013-09-10
    相关资源
    最近更新 更多