Class: Gitra::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/gitra/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



10
11
12
# File 'lib/gitra/cli.rb', line 10

def initialize
  @tracker = Tracker.new '.'
end

Instance Method Details

#analyze(reference_branch, matching = :all) ⇒ Object



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
# File 'lib/gitra/cli.rb', line 14

def analyze(reference_branch, matching = :all)
  reference_branch ||= @tracker.current_branch

  $stdout.puts "---- Analyzing branches in relation to #{reference_branch.yellow} ----"
  branches = ([reference_branch] + @tracker.branches).uniq!

  $stdout.puts "-- Using branches matching #{matching.map {|m| m.source}.join(', ').yellow} --" unless matching == :all
  branches = branches.select { |branch| matching == :all or matching.inject(false) { |acc, match| acc || branch =~ match } }

  $stdout.print "Analysing #{branches.size} branches: "
  branch_analysis = branches.map do |branch|
    $stdout.print '.'
    $stdout.flush
    unmerged = @tracker.branch(branch).commits_since(reference_branch).collect { |c| c.sha }
    behind = @tracker.branch(reference_branch).commits_since(branch).collect { |c| c.sha }
    {:name => branch, :behind => behind.size, :unmerged => unmerged.size}
  end
  $stdout.puts

  name_max_size = branch_analysis.map { |item| item[:name].size }.sort.last
  branch_analysis.each do |item|
    state = []
    state << "#{item[:unmerged]} ahead (unmerged)".red if item[:unmerged] > 0
    state << "(but #{item[:behind]} behind)" if item[:unmerged] > 0 and item[:behind] > 0
    state << 'merged'.green if state.empty?
    $stdout.puts "%#{name_max_size}s %s" % [item[:name], state.join(', ')]
  end
end

#history(since_revision) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gitra/cli.rb', line 43

def history(since_revision)
  current = @tracker.current_branch

  $stdout.puts "---- Analyzing commit history from #{since_revision.yellow} to #{current.yellow} ----"
  commits = @tracker.branch(current).commits_since(since_revision)

  $stdout.print "Analysing #{commits.size} commits: "
  commit_parser = Gitra::Parser.new('.gitra-rules.yml')
  commits.each do |commit|
    $stdout.print '.'
    $stdout.flush
    commit_parser.use(commit)
  end
  $stdout.puts

  commit_parser.result.each_pair do |type, ids|
    ids.sort.each do |id, commits|
      description = commits.map { |c| c.sha.slice(0..10) }.join(', ')
      $stdout.puts "%28s - %s" % ["#{type.to_s} #{id.to_s}".yellow, description]
    end
  end
end