使用ruby检查本地存储库中是否存在git分支?

时间:2011-02-17 07:16:05

标签: ruby git branch

使用ruby检查本地git存储库中是否存在git branch的最佳方法是什么?我的红宝石技能不是那么好,所以想知道最好的方法:)谢谢。

2 个答案:

答案 0 :(得分:5)

有用于访问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">, ...]

答案 1 :(得分:0)

我最终想出了这个:

# 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

其中run是反引号表达式。原因是代码是高度可移植的,不允许任何其他依赖。而git安装在环境中。

相关问题