Added command line option (‚comment‘) for the comment string; default is ‚///‚

This commit is contained in:
Stephan Esch 2013-11-19 17:08:57 +01:00
parent 3532de2807
commit c2a0e0bafe
4 changed files with 12 additions and 5 deletions

View File

@ -9,11 +9,12 @@ class GitStats::CLI < Thor
option :from, :aliases => :f, :desc => 'Commit from where statistics should start.'
option :to, :aliases => :t, :default => 'HEAD', :desc => 'Commit where statistics should stop.'
option :silent, :aliases => :s, :type => :boolean, :desc => 'Silent mode. Don\'t output anything.'
option :comment, :aliases => :c, :default => '///', :desc => 'The string which is used for comments.'
desc 'generate', 'Generates the statistics of a repository'
def generate
I18n.locale = options[:language]
GitStats::Generator.new(options[:path], options[:output], options[:from], options[:to]) { |g|
GitStats::Generator.new(options[:path], options[:output], options[:from], options[:to], options[:comment]) { |g|
g.add_command_observer { |command, result| puts "#{command}" } unless options[:silent]
}.render_all
end

View File

@ -4,10 +4,10 @@ module GitStats
delegate :add_command_observer, to: :@repo
delegate :render_all, to: :@view
def initialize(repo_path, out_path, first_commit_sha = nil, last_commit_sha = "HEAD")
def initialize(repo_path, out_path, first_commit_sha = nil, last_commit_sha = "HEAD", comment_string = "///")
validate_repo_path(repo_path)
@repo = GitData::Repo.new(path: repo_path, first_commit_sha: first_commit_sha, last_commit_sha: last_commit_sha)
@repo = GitData::Repo.new(path: repo_path, first_commit_sha: first_commit_sha, last_commit_sha: last_commit_sha, comment_string: comment_string)
view_data = StatsView::ViewData.new(@repo)
@view = StatsView::View.new(view_data, out_path)

View File

@ -17,9 +17,15 @@ module GitStats
"#{self.class} #@commit"
end
def escape_characters_in_string(string)
pattern = /(\'|\"|\.|\*|\/|\-|\\)/
string.gsub(pattern){|match|"\\" + match}
end
private
def calculate_stat
stat_line = commit.repo.run("git show #{commit.sha} | awk 'BEGIN {adds=0; dels=0} {if ($0 ~ /^\\+\\/\\/\\//) adds++; if ($0 ~ /^\-\\/\\/\\//) dels++} END {print adds \" insertions \" dels \" deletes\"}'").lines.to_a[0]
escaped_string = escape_characters_in_string(commit.repo.comment_string)
stat_line = commit.repo.run("git show #{commit.sha} | awk 'BEGIN {adds=0; dels=0} {if ($0 ~ /^\\+#{escaped_string}/) adds++; if ($0 ~ /^\-#{escaped_string}/) dels++} END {print adds \" insertions \" dels \" deletes\"}'").lines.to_a[0]
if stat_line.blank?
@insertions = @deletions = 0
else

View File

@ -6,7 +6,7 @@ module GitStats
class Repo
include HashInitializable
attr_reader :path, :first_commit_sha, :last_commit_sha
attr_reader :path, :first_commit_sha, :last_commit_sha, :comment_string
delegate :files, :files_by_extension, :files_by_extension_count, :lines_by_extension,
:files_count, :binary_files, :text_files, :lines_count, :comments_count, to: :last_commit