[英]Implement git branch --contains with rugged library
I'm working with a ruby script that execute the following git command on a given git repository.我正在使用一个 ruby 脚本,该脚本在给定的 git 存储库上执行以下 git 命令。
branches = `git branch -a --contains #{tag_name}`
This approach has some drawbacks with command output (that may change in different git versions) and is subject to git binary version on the hosting machine, so I was trying to see if it's possible to replace that command using rugged but I wasn't able to find anything similar to that.这种方法在命令输出方面有一些缺点(可能会在不同的 git 版本中发生变化)并且受主机上的 git 二进制版本的影响,所以我试图看看是否可以使用坚固的命令替换该命令,但我无法找到任何类似的东西。
Maybe in rugged there's no way to implement --contains
flag, but I think it should be pretty easy to implement this behavior:也许在粗犷中没有办法实现--contains
标志,但我认为实现这种行为应该很容易:
Given any git commit-ish (a tag, a commit sha, etc.) how to get (with rugged) the list of branches (both local and remote) that contains that commit-ish?给定任何 git commit-ish (标签、提交 sha 等),如何(粗略地)获取包含该 commit-ish 的分支列表(本地和远程)?
I need to implement something like github commit show page, ie tag xyz is contained in master, develop, branch_xx
我需要实现类似 github 提交显示页面的内容,即tag xyz is contained in master, develop, branch_xx
Finally solved with 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.