Class: GitFame::Base

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Helper
Defined in:
lib/git_fame/base.rb

Instance Method Summary collapse

Methods included from Helper

#number_with_delimiter

Constructor Details

#initialize(args) ⇒ Base

@args String Absolute path to git repository @args String What should #authors be sorted by? @args Boolean Should counts be grouped by file extension? @args String Comma-separated list of paths in the repo

which should be excluded

@args String Branch to run from @args date after @args date before



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/git_fame/base.rb', line 40

def initialize(args)
  @default_settings = {
    branch: "master",
    sorting: "loc",
    ignore_types: ["image", "binary"]
  }
  @progressbar  = args.fetch(:progressbar, false)
  @file_authors = Hash.new { |h,k| h[k] = {} }
  # Create array out of comma separated list
  @exclude = args.fetch(:exclude, "").split(",").
    map{ |path| path.strip.sub(/\A\//, "") }
  @extensions = args.fetch(:extensions, "").split(",")
  # Default sorting option is by loc
  @include = args.fetch(:include, "").split(",")
  @sort = args.fetch(:sort, @default_settings.fetch(:sorting))
  @repository = args.fetch(:repository)
  @by_type = args.fetch(:by_type, false)
  @branch = args.fetch(:branch, nil)
  @everything = args.fetch(:everything, false)
  @timeout = args.fetch(:timeout, CMD_TIMEOUT)

  # Figure out what branch the caller is using
  if present?(@branch = args[:branch])
    unless branch_exists?(@branch)
      raise Error, "Branch '#{@branch}' does not exist"
    end
  else
    @branch = default_branch
  end

  @after = args.fetch(:after, nil)
  @before = args.fetch(:before, nil)
  [@after, @before].each do |date|
    if date and not valid_date?(date)
      raise Error, "#{date} is not a valid date"
    end
  end

  # Fields that should be visible in the final table
  # Used by #csv_puts, #to_csv and #pretty_puts
  # Format: [ [ :method_on_author, "custom column name" ] ]
  @visible_fields = [
    :name,
    :loc,
    :commits,
    :files,
    [:distribution, "distribution (%)"]
  ]
  @wopt = args.fetch(:whitespace, false) ? "-w" : ""
  @authors = {}
  @cache = {}
  @verbose = args.fetch(:verbose, false)

  populate
end

Instance Method Details

#authorsObject



164
165
166
167
168
# File 'lib/git_fame/base.rb', line 164

def authors
  unique_authors.sort_by do |author|
    @sort == "name" ? author.send(@sort) : -1 * author.raw(@sort)
  end
end

#commitsObject

Returns Fixnum Total number of commits.

Returns:

  • Fixnum Total number of commits



150
151
152
# File 'lib/git_fame/base.rb', line 150

def commits
  authors.inject(0) { |result, author| author.raw(:commits) + result }
end

#csv_putsObject

Prints CSV



115
116
117
# File 'lib/git_fame/base.rb', line 115

def csv_puts
  puts to_csv
end

#file_listObject

TODO: Rename

Returns:

  • Array list of repo files processed



145
# File 'lib/git_fame/base.rb', line 145

def file_list; used_files; end

#filesObject

TODO: Rename this

Returns:

  • Fixnum Total number of files



137
138
139
# File 'lib/git_fame/base.rb', line 137

def files
  used_files.count
end

#locObject

Returns Fixnum Total number of lines.

Returns:

  • Fixnum Total number of lines



157
158
159
# File 'lib/git_fame/base.rb', line 157

def loc
  authors.inject(0) { |result, author| author.raw(:loc) + result }
end

#pretty_putsObject

Generates pretty output



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/git_fame/base.rb', line 99

def pretty_puts
  extend Hirb::Console
  Hirb.enable({ pager: false })
  puts "\nStatistics based on #{commit_range.to_s(true)}"
  puts "Active files: #{number_with_delimiter(files)}"
  puts "Active lines: #{number_with_delimiter(loc)}"
  puts "Total commits: #{number_with_delimiter(commits)}\n"
  unless @everything
    puts "\nNote: Files matching MIME type #{ignore_types.join(", ")} has been ignored\n\n"
  end
  table(authors, fields: printable_fields)
end

#to_csvObject

Generate csv output



122
123
124
125
126
127
128
129
130
131
# File 'lib/git_fame/base.rb', line 122

def to_csv
  CSV.generate do |csv|
    csv << fields
    authors.each do |author|
      csv << fields.map do |f|
        author.send(f)
      end
    end
  end
end