Class: GitTopic::Formatter::Branches

Inherits:
Object
  • Object
show all
Includes:
Helper, Term::ANSIColor
Defined in:
lib/git_topic/formatter/branches.rb

Overview

summarizes branches information

Defined Under Namespace

Classes: Branch

Constant Summary collapse

BRANCH_FORMAT =
/
  \s*(?<current_exp>\*\ )?
  (?<branch_name>\S+)\s+
  (?<rev>\S+)\s+(.*)
/x

Instance Method Summary collapse

Methods included from Helper

#truncate

Constructor Details

#initialize(options) ⇒ Branches

Returns a new instance of Branches.



13
14
15
# File 'lib/git_topic/formatter/branches.rb', line 13

def initialize(options)
  @all = options[:all]
end

Instance Method Details

#branch_format(branch_name, current_branch) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/git_topic/formatter/branches.rb', line 83

def branch_format(branch_name, current_branch)
  if branch_name == current_branch
    "* #{green}#{bold}%-20s#{clear}"
  else
    "  #{bold}%-20s#{clear}"
  end
end

#get_description_of(branch) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/git_topic/formatter/branches.rb', line 75

def get_description_of(branch)
  config_key = "branch.#{branch.name}.description"
  command = "git config #{config_key}"
  _stdin, stdout, _stderr, _wait_thr = *Open3.popen3(command)
  return nil if stdout.eof?
  stdout.readline
end

#parse_branch(line) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/git_topic/formatter/branches.rb', line 56

def parse_branch(line)
  matched = line.match(BRANCH_FORMAT)
  raise 'cannot parse branch' unless matched
  branch_name = matched[:branch_name]
  rev = matched[:rev]
  current_branch = matched[:current_exp] ? branch_name : nil
  [branch_name, rev, current_branch]
end

#parse_branchesObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git_topic/formatter/branches.rb', line 38

def parse_branches
  branches = []
  current_branch = nil
  _stdin, stdout, _stderr, _wait_thr = *Open3.popen3('git branch -v')
  stdout.each do |line|
    branch_name, rev, current_candidate = parse_branch(line)
    current_branch ||= current_candidate
    branches << Branch.new(branch_name, rev)
  end
  [branches, current_branch]
end


18
19
20
21
22
23
# File 'lib/git_topic/formatter/branches.rb', line 18

def print
  puts "#{bold}[Branches]#{clear}" if @all
  branches, current_branch = parse_branches
  print_header(branches.first)
  print_contents(branches, current_branch)
end


32
33
34
35
36
# File 'lib/git_topic/formatter/branches.rb', line 32

def print_contents(branches, current_branch)
  branches.each do |branch|
    print_line(current_branch, branch)
  end
end


25
26
27
28
29
30
# File 'lib/git_topic/formatter/branches.rb', line 25

def print_header(branch)
  rev_length = branch.rev.length
  header_format = "  %-20s %-#{rev_length}s %s"
  puts format(header_format, :Branch, :Rev, :Summary)
  puts '-' * 80
end


65
66
67
68
69
70
71
72
73
# File 'lib/git_topic/formatter/branches.rb', line 65

def print_line(current_branch, branch)
  branch_name = branch.name
  rev = branch.rev
  description = get_description_of branch
  return if description.nil?
  branch_format = branch_format(branch_name, current_branch)
  truncated_name = truncate(branch_name)
  puts format("#{branch_format} %s %s", truncated_name, rev, description)
end