Sha256: f1219c38c3cfbd4bbf21441faa7193dbd313541f45a75dc6360e848ed398b28c

Contents?: true

Size: 1.62 KB

Versions: 71

Compression:

Stored size: 1.62 KB

Contents

module Git
  
  # object that holds all the available branches
  class Branches

    include Enumerable
    
    def initialize(base)
      @branches = {}
      
      @base = base
            
      @base.lib.branches_all.each do |b|
        @branches[b[0]] = Git::Branch.new(@base, b[0])
      end
    end

    def local
      self.select { |b| !b.remote }
    end
    
    def remote
      self.select { |b| b.remote }
    end
    
    # array like methods

    def size
      @branches.size
    end    
    
    def each(&block)
      @branches.values.each(&block)
    end
    
    # Returns the target branch
    #
    # Example:
    #   Given (git branch -a):
    #    master
    #    remotes/working/master
    #
    #   g.branches['master'].full #=> 'master'
    #   g.branches['working/master'].full => 'remotes/working/master'
    #   g.branches['remotes/working/master'].full => 'remotes/working/master'
    #
    # @param [#to_s] branch_name the target branch name.
    # @return [Git::Branch] the target branch.
    def [](branch_name)
      @branches.values.inject(@branches) do |branches, branch|
        branches[branch.full] ||= branch

        # This is how Git (version 1.7.9.5) works. 
        # Lets you ignore the 'remotes' if its at the beginning of the branch full name (even if is not a real remote branch). 
        branches[branch.full.sub('remotes/', '')] ||= branch if branch.full =~ /^remotes\/.+/
        
        branches
      end[branch_name.to_s]
    end
    
    def to_s
      out = ''
      @branches.each do |k, b|
        out << (b.current ? '* ' : '  ') << b.to_s << "\n"
      end
      out
    end
    
  end

end

Version data entries

71 entries across 71 versions & 15 rubygems

Version Path
git-1.6.0.pre1 lib/git/branches.rb
sigterm_extensions-0.0.4 lib/git/branches.rb
git-ce-1.5.0.2 lib/git/branches.rb
git-ce-1.5.0.1 lib/git/branches.rb
git-ce-1.5.0 lib/git/branches.rb
git-1.5.0 lib/git/branches.rb
git-1.4.0 lib/git/branches.rb
ruby-git-fw-new-1.3.3 lib/git/branches.rb
ruby-git-yz-1.3.3 lib/git/branches.rb
git-ng-1.4.0 lib/git/branches.rb
cvss-suite-1.0.8 vendor/cache/ruby/2.2.0/gems/git-1.3.0/lib/git/branches.rb
ruby-git-fw-1.3.2 lib/git/branches.rb
ruby-git-fw-1.3.1 lib/git/branches.rb
ruby-git-fw-1.3.0 lib/git/branches.rb
cvss-suite-1.0.7 vendor/cache/ruby/2.2.0/gems/git-1.3.0/lib/git/branches.rb
cvss-suite-1.0.6 vendor/cache/ruby/2.2.0/gems/git-1.3.0/lib/git/branches.rb
cvss-suite-1.0.5 vendor/cache/ruby/2.2.0/gems/git-1.3.0/lib/git/branches.rb
cvss-suite-1.0.4 vendor/cache/ruby/2.2.0/gems/git-1.3.0/lib/git/branches.rb
cvss-suite-1.0.3 vendor/cache/ruby/2.2.0/gems/git-1.3.0/lib/git/branches.rb
cvss-suite-1.0.2 vendor/cache/ruby/2.2.0/gems/git-1.3.0/lib/git/branches.rb