Class: GitStatistics::CLI
- Inherits:
-
Object
- Object
- GitStatistics::CLI
- Defined in:
- lib/git_statistics.rb
Constant Summary collapse
- DEFAULT_BRANCH =
'master'
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
Instance Method Summary collapse
- #calculate! ⇒ Object
- #collect_and_only_update ⇒ Object
- #execute ⇒ Object
- #fresh_collect! ⇒ Object
-
#initialize(dir) ⇒ CLI
constructor
A new instance of CLI.
- #output_results ⇒ Object
- #parse_options ⇒ Object
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 ) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/git_statistics.rb', line 5 def @options end |
#repository ⇒ Object (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(.email, .merges) end |
#collect_and_only_update ⇒ Object
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 .update # Ensure commit directory is present @collector = Collector.new(repository, .limit, false, .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: .branch, time_since: time_since) @collected = true end |
#execute ⇒ Object
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, .limit, true, .pretty) @collector.collect(branch: .branch) end |
#output_results ⇒ Object
56 57 58 59 |
# File 'lib/git_statistics.rb', line 56 def output_results results = Formatters::Console.new(@collector.commits) puts results.print_summary(.sort, .email, .top) end |
#parse_options ⇒ Object
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 OptionParser.new do |opt| opt.version = VERSION opt.on '-e', '--email', "Use author's email instead of name" do .email = true end opt.on '-m', '--merges', 'Factor in merges when calculating statistics' do .merges = true end opt.on '-p', '--pretty', 'Save the commits in git_repo/.git_statistics in pretty print (larger file size)' do .pretty = true end opt.on '-u', '--update', 'Update saved commits with new data' do .update = true end opt.on '-s', '--sort TYPE', 'Sort authors by {commits, additions, deletions, create, delete, rename, copy, merges}' do |type| .sort = type end opt.on '-t', '--top N', Float, 'Show the top N authors in results' do |value| .top = value end opt.on '-b', '--branch BRANCH', 'Use the specified branch for statistics (otherwise the master branch is used)' do |branch| .branch = branch end opt.on '-v', '--verbose', 'Verbose output (shows INFO level log statements)' do .verbose = true end opt.on '-d', '--debug', 'Debug output (shows DEBUG level log statements)' do .debug = true end opt.on '-l', '--limit MAX_COMMITS', Float, 'The maximum limit of commits to hold in memory at a time' do |number| .limit = number end end.parse! end |