Class: GitStatistics::GitStatistics

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGitStatistics

Returns a new instance of GitStatistics.



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

def initialize
  @options = OpenStruct.new(
    email: false,
    merges: false,
    pretty: false,
    update: false,
    sort: "commits",
    top: 0,
    branch: false,
    verbose: false,
    debug: false,
    limit: 100
  )
  parse_options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/git_statistics.rb', line 7

def options
  @options
end

Instance Method Details

#executeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/git_statistics.rb', line 24

def execute
  if options.debug
    Log.level = Logger::DEBUG
    Log.use_debug
  elsif options.verbose
    Log.level = Logger::INFO
  end

  # Collect data (incremental or fresh) based on presence of old data
  if options.update
    # Ensure commit directory is present
    collector = Collector.new(options.limit, false, options.pretty)
    commits_directory = File.join(collector.repo_path, ".git_statistics")
    FileUtils.mkdir_p(commits_directory)
    file_count = Utilities.number_of_matching_files(commits_directory, /\d+\.json/) - 1

    if file_count >= 0
      time = Utilities.get_modified_time(commits_directory + "#{file_count}.json")
      # Only use --since if there is data present
      collector.collect(options.branch, "--since=\"#{time}\"")
      collected = true
    end
  end

  # If no data was collected as there was no present data then start fresh
  unless collected
    collector = Collector.new(options.limit, true, options.pretty)
    collector.collect(options.branch)
  end

  # Calculate statistics
  collector.commits.calculate_statistics(options.email, options.merges)

  # Print results
  results = Formatters::Console.new(collector.commits)
  puts results.print_summary(options.sort, options.email, options.top)
end

#parse_optionsObject



62
63
64
65
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
# File 'lib/git_statistics.rb', line 62

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", "Use current branch for statistics (otherwise all branches)" do
      options.branch = true
    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