Class: GitStatistics::CLI

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

Constant Summary collapse

DEFAULT_BRANCH =
'master'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ CLI

Returns a new instance of CLI.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/git_statistics.rb', line 9

def initialize(dir)
  @repository = dir.nil? ? Rugged::Repository.discover(Dir.pwd) : Rugged::Repository.discover(dir)
  @collected = false
  @collector = nil
  @options = OpenStruct.new(
    email: false,
    merges: false,
    pretty: false,
    update: false,
    sort: 'commits',
    top: 0,
    branch: DEFAULT_BRANCH,
    verbose: false,
    debug: false,
    limit: 100
  )
  parse_options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/git_statistics.rb', line 5

def options
  @options
end

#repositoryObject (readonly)

Returns the value of attribute repository.



5
6
7
# File 'lib/git_statistics.rb', line 5

def repository
  @repository
end

Instance Method Details

#calculate!Object



52
53
54
# File 'lib/git_statistics.rb', line 52

def calculate!
  @collector.commits.calculate_statistics(options.email, options.merges)
end

#collect_and_only_updateObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/git_statistics.rb', line 36

def collect_and_only_update
  return unless options.update

  # Ensure commit directory is present
  @collector = Collector.new(repository, options.limit, false, options.pretty)
  commits_directory = repository.workdir + '.git_statistics/'
  FileUtils.mkdir_p(commits_directory)
  file_count = Utilities.number_of_matching_files(commits_directory, /\d+\.json/) - 1

  return unless file_count >= 0

  time_since = Utilities.get_modified_time(commits_directory + "#{file_count}.json").to_s
  @collector.collect(branch: options.branch, time_since: time_since)
  @collected = true
end

#executeObject



28
29
30
31
32
33
34
# File 'lib/git_statistics.rb', line 28

def execute
  determine_log_level
  collect_and_only_update
  fresh_collect! unless @collected
  calculate!
  output_results
end

#fresh_collect!Object



61
62
63
64
# File 'lib/git_statistics.rb', line 61

def fresh_collect!
  @collector = Collector.new(repository, options.limit, true, options.pretty)
  @collector.collect(branch: options.branch)
end

#output_resultsObject



56
57
58
59
# File 'lib/git_statistics.rb', line 56

def output_results
  results = Formatters::Console.new(@collector.commits)
  puts results.print_summary(options.sort, options.email, options.top)
end

#parse_optionsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/git_statistics.rb', line 66

def parse_options
  OptionParser.new do |opt|
    opt.version = VERSION
    opt.on '-e', '--email', "Use author's email instead of name" do
      options.email = true
    end
    opt.on '-m', '--merges', 'Factor in merges when calculating statistics' do
      options.merges = true
    end
    opt.on '-p', '--pretty', 'Save the commits in git_repo/.git_statistics in pretty print (larger file size)' do
      options.pretty = true
    end
    opt.on '-u', '--update', 'Update saved commits with new data' do
      options.update = true
    end
    opt.on '-s', '--sort TYPE', 'Sort authors by {commits, additions, deletions, create, delete, rename, copy, merges}' do |type|
      options.sort = type
    end
    opt.on '-t', '--top N', Float, 'Show the top N authors in results' do |value|
      options.top = value
    end
    opt.on '-b', '--branch BRANCH', 'Use the specified branch for statistics (otherwise the master branch is used)' do |branch|
      options.branch = branch
    end
    opt.on '-v', '--verbose', 'Verbose output (shows INFO level log statements)' do
      options.verbose = true
    end
    opt.on '-d', '--debug', 'Debug output (shows DEBUG level log statements)' do
      options.debug = true
    end
    opt.on '-l', '--limit MAX_COMMITS', Float, 'The maximum limit of commits to hold in memory at a time' do |number|
      options.limit = number
    end
  end.parse!
end