【问题标题】:Implement git branch --contains with rugged library使用坚固的库实现 git branch --contains
【发布时间】:2019-07-16 13:08:36
【问题描述】:

我正在使用一个 ruby​​ 脚本,它在给定的 git 存储库上执行以下 git 命令。

  branches = `git branch -a --contains #{tag_name}`

这种方法在命令输出方面有一些缺点(可能会在不同的 git 版本中发生变化)并且受主机上的 git 二进制版本的影响,所以我试图看看是否可以使用 rugged 替换该命令但是我找不到类似的东西。

也许在崎岖不平的环境中没有办法实现--contains 标志,但我认为实现这种行为应该很容易:

给定任何 git commit-ish(标签、提交 sha 等)如何获取(使用崎岖不平的)包含该 commit-ish 的分支列表(本地和远程) ?

我需要实现 github commit show page 之类的东西,即tag xyz is contained in master, develop, branch_xx

【问题讨论】:

标签: ruby git branch rugged


【解决方案1】:

终于用this code解决了:

def branches_for_tag(tag_name, repo_path = Dir.pwd)
  @branches ||= begin
    repo = Rugged::Repository.new(repo_path)
    # Convert tag to sha1 if matching tag found
    full_sha = repo.tags[tag_name] ? repo.tags[tag_name].target_id : tag_name
    logger.debug "Inspecting repo at #{repo.path}, branches are #{repo.branches.map(&:name)}"
    # descendant_of? does not return true for it self, i.e. repo.descendant_of?(x, x) will return false for every commit
    # @see https://github.com/libgit2/libgit2/pull/4362
    repo.branches.select { |branch| repo.descendant_of?(branch.target_id, full_sha) || full_sha == branch.target_id }
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 2014-11-25
    • 1970-01-01
    • 2018-10-18
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多