0
检查git分支是否存在于使用ruby的本地git仓库中的最佳方法是什么?我的红宝石技能不是很好,所以想知道最好的方法去做:)谢谢。用ruby检查本地存储库中是否存在git分支?
检查git分支是否存在于使用ruby的本地git仓库中的最佳方法是什么?我的红宝石技能不是很好,所以想知道最好的方法去做:)谢谢。用ruby检查本地存储库中是否存在git分支?
最后我想出这样的:
# Check if a Branch Exists
def branch_exists(branch)
branches = run("git branch",false)
regex = Regexp.new('[\\n\\s\\*]+' + Regexp.escape(branch.to_s) + '\\n')
result = ((branches =~ regex) ? true : false)
return result
end
其中行程是反引号的表达。理由是该代码具有高度的可移植性,并且不允许任何其他依赖项。而git安装在环境中。
有一些用于访问git存储库的ruby库,一个是grit。
安装使用[sudo] gem install grit
。
$ irb
>> require 'grit'
=> true
>> repo = Grit::Repo.new('/path/to/your/repo/')
=> #<Grit::Repo "/path/to/your/repo/.git">
>> repo.branches
=> [#<Grit::Head "master">, #<Grit::Head "some-topic-branch">, ...]