webmat / git_remote_branch

A tool to simplify working with remote branches

This URL has Read+Write access

git_remote_branch / lib / state.rb
100644 43 lines (34 sloc) 1.222 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module GitRemoteBranch
  include ::CaptureFu
  
  public
    def get_current_branch
      local_branch_information[0]
    end
    
    def local_branches
      local_branch_information[1]
    end
    
    def git_found?
      ret, msg = capture_process_output "#{GIT} --version"
      ret == 0
    end
    
  private
    # Returns an array of 2 elements: [current_branch, [all local branches]]
    def local_branch_information
      #This is sensitive to checkouts of branches specified with wrong case
      
      listing = capture_process_output("#{LOCAL_BRANCH_LISTING_COMMAND}")[1]
      
      raise(NotOnGitRepositoryError, listing.chomp) if listing =~ /Not a git repository/i
      if listing =~ /\(no branch\)/
        raise InvalidBranchError, ["Couldn't identify the current local branch. The branch listing was:",
          LOCAL_BRANCH_LISTING_COMMAND.red,
          listing].join("\n")
      end
      
      current_branch = nil
      branches = listing.split("\n").map do |line|
        current = line.include? '*'
        clean_line = line.gsub('*','').strip
        current_branch = clean_line if current
        clean_line
      end
      
      return current_branch, branches
    end
end