2011-02-17 46 views

回答

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 

其中行程是反引号的表达。理由是该代码具有高度的可移植性,并且不允许任何其他依赖项。而git安装在环境中。

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">, ...]