From 3532de2807e7a1eb5d01c984202e026c55e0bac3 Mon Sep 17 00:00:00 2001 From: Stephan Esch Date: Sun, 17 Nov 2013 22:39:00 +0100 Subject: [PATCH 1/2] Added comment stats. Only lines beginning with three slashes are counted, since these stats should reflect comments used for documentation, not inline explanations. --- config/locales/de.yml | 2 ++ config/locales/en.yml | 2 ++ lib/git_stats/base.rb | 1 + lib/git_stats/git_data/comment_stat.rb | 32 +++++++++++++++++++ lib/git_stats/git_data/commit.rb | 4 +++ lib/git_stats/git_data/repo.rb | 15 ++++++++- lib/git_stats/stats_view/charts/charts.rb | 2 +- .../stats_view/charts/repo_charts.rb | 10 ++++++ lib/git_stats/stats_view/view.rb | 3 +- templates/comments/_comments.haml | 11 +++++++ templates/comments/by_date.haml | 1 + 11 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 lib/git_stats/git_data/comment_stat.rb create mode 100644 templates/comments/_comments.haml create mode 100644 templates/comments/by_date.haml diff --git a/config/locales/de.yml b/config/locales/de.yml index b651fec25..59ae1ca1b 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -12,8 +12,10 @@ de: commits_by_hour: Commits in der Stunde files: Dateien lines: Zeilen + comments: Kommentare files_by_date: Dateien nach Datum lines_by_date: Zeilen nach Datum + comments_by_date: Kommentare nach Datum files_by_extension: Dateien nach Erweiterungen lines_by_extension: Zeilen nach Erweiterungen hour_of_day: Tagesstunden diff --git a/config/locales/en.yml b/config/locales/en.yml index 41c44a929..fc63510d5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -12,8 +12,10 @@ en: commits_by_hour: Commits by hour files: Files lines: Lines + comments: Comments files_by_date: Files by date lines_by_date: Lines by date + comments_by_date: Comments by date files_by_extension: Files by extension lines_by_extension: Lines by extension hour_of_day: Hour of day diff --git a/lib/git_stats/base.rb b/lib/git_stats/base.rb index 314b2f5b7..001c2b56b 100644 --- a/lib/git_stats/base.rb +++ b/lib/git_stats/base.rb @@ -18,6 +18,7 @@ require 'git_stats/git_data/command_runner' require 'git_stats/git_data/commit' require 'git_stats/git_data/repo' require 'git_stats/git_data/short_stat' +require 'git_stats/git_data/comment_stat' require 'git_stats/stats_view/template' require 'git_stats/stats_view/view' diff --git a/lib/git_stats/git_data/comment_stat.rb b/lib/git_stats/git_data/comment_stat.rb new file mode 100644 index 000000000..4008d1f7a --- /dev/null +++ b/lib/git_stats/git_data/comment_stat.rb @@ -0,0 +1,32 @@ +# -*- encoding : utf-8 -*- +module GitStats + module GitData + class CommentStat + attr_reader :commit, :insertions, :deletions + + def initialize(commit) + @commit = commit + calculate_stat + end + + def changed_lines + insertions + deletions + end + + def to_s + "#{self.class} #@commit" + 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] + if stat_line.blank? + @insertions = @deletions = 0 + else + @insertions = stat_line[/(\d+) insertions?/, 1].to_i + @deletions = stat_line[/(\d+) deletes?/, 1].to_i + end + end + end + end +end diff --git a/lib/git_stats/git_data/commit.rb b/lib/git_stats/git_data/commit.rb index 146cd56cd..994a86fac 100644 --- a/lib/git_stats/git_data/commit.rb +++ b/lib/git_stats/git_data/commit.rb @@ -50,6 +50,10 @@ module GitStats @short_stat ||= ShortStat.new(self) end + def comment_stat + @comment_stat ||= CommentStat.new(self) + end + def to_s "#{self.class} #@sha" end diff --git a/lib/git_stats/git_data/repo.rb b/lib/git_stats/git_data/repo.rb index e81d479d0..a91ad143f 100644 --- a/lib/git_stats/git_data/repo.rb +++ b/lib/git_stats/git_data/repo.rb @@ -9,7 +9,7 @@ module GitStats attr_reader :path, :first_commit_sha, :last_commit_sha delegate :files, :files_by_extension, :files_by_extension_count, :lines_by_extension, - :files_count, :binary_files, :text_files, :lines_count, to: :last_commit + :files_count, :binary_files, :text_files, :lines_count, :comments_count, to: :last_commit def initialize(params) super(params) @@ -63,6 +63,15 @@ module GitStats }].fill_empty_days!(aggregated: true) end + def comments_count_by_date + sum = 0 + @comment_count_each_day ||= Hash[commits.map { |commit| + sum += commit.comment_stat.insertions + sum -= commit.comment_stat.deletions + [commit.date.to_date, sum] + }].fill_empty_days!(aggregated: true) + end + def last_commit commits.last end @@ -79,6 +88,10 @@ module GitStats @short_stats ||= commits.map(&:short_stat) end + def comment_stats + @comment_stats ||= commits.map(&:comment_stat) + end + def activity @activity ||= Activity.new(commits) end diff --git a/lib/git_stats/stats_view/charts/charts.rb b/lib/git_stats/stats_view/charts/charts.rb index ce3184b87..5b94c0337 100644 --- a/lib/git_stats/stats_view/charts/charts.rb +++ b/lib/git_stats/stats_view/charts/charts.rb @@ -3,7 +3,7 @@ module GitStats module StatsView module Charts class All - delegate :files_by_extension, :lines_by_extension, :files_by_date, :lines_by_date, to: :repo_charts + delegate :files_by_extension, :lines_by_extension, :files_by_date, :lines_by_date, :comments_by_date, to: :repo_charts delegate :commits_sum_by_author_by_date, :changed_lines_by_author_by_date, :insertions_by_author_by_date, :deletions_by_author_by_date, to: :authors_charts diff --git a/lib/git_stats/stats_view/charts/repo_charts.rb b/lib/git_stats/stats_view/charts/repo_charts.rb index a761cb159..3b6172259 100644 --- a/lib/git_stats/stats_view/charts/repo_charts.rb +++ b/lib/git_stats/stats_view/charts/repo_charts.rb @@ -47,6 +47,16 @@ module GitStats end end + def comments_by_date + Chart.new do |f| + f.date_chart( + data: @repo.comments_count_by_date, + title: :comments_by_date.t, + y_text: :comments.t + ) + end + end + end end end diff --git a/lib/git_stats/stats_view/view.rb b/lib/git_stats/stats_view/view.rb index 42e216ba2..d99dd0050 100644 --- a/lib/git_stats/stats_view/view.rb +++ b/lib/git_stats/stats_view/view.rb @@ -55,7 +55,8 @@ module GitStats activity: 'activity/by_date.html', authors: 'authors/best_authors.html', files: 'files/by_date.html', - lines: 'lines/by_date.html' + lines: 'lines/by_date.html', + comments: 'comments/by_date.html' } end diff --git a/templates/comments/_comments.haml b/templates/comments/_comments.haml new file mode 100644 index 000000000..766507e71 --- /dev/null +++ b/templates/comments/_comments.haml @@ -0,0 +1,11 @@ +.tabbable.tabs-left + %ul.nav.nav-tabs + %li{class: page == :comments_by_date ? "active" : ""} + %a{:href => 'by_date.html'}= :comments_by_date.t + + .tab-content + .tab-pane.active + .page-header + %h1.pagination-centered= page.t + - if page == :comments_by_date + = high_stock("comments_by_date", charts.comments_by_date) diff --git a/templates/comments/by_date.haml b/templates/comments/by_date.haml new file mode 100644 index 000000000..7305abcb1 --- /dev/null +++ b/templates/comments/by_date.haml @@ -0,0 +1 @@ += render_partial('comments/_comments', page: :comments_by_date) \ No newline at end of file From c2a0e0bafe17114c26e4a4aa40ae82adfa084e70 Mon Sep 17 00:00:00 2001 From: Stephan Esch Date: Tue, 19 Nov 2013 17:08:57 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Added=20command=20line=20option=20(?= =?UTF-8?q?=E2=80=9Acomment=E2=80=98)=20for=20the=20comment=20string;=20de?= =?UTF-8?q?fault=20is=20=E2=80=9A///=E2=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/git_stats/cli.rb | 3 ++- lib/git_stats/generator.rb | 4 ++-- lib/git_stats/git_data/comment_stat.rb | 8 +++++++- lib/git_stats/git_data/repo.rb | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/git_stats/cli.rb b/lib/git_stats/cli.rb index edbb624b2..5ad553114 100644 --- a/lib/git_stats/cli.rb +++ b/lib/git_stats/cli.rb @@ -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 diff --git a/lib/git_stats/generator.rb b/lib/git_stats/generator.rb index 60b13b84d..54a444ae5 100644 --- a/lib/git_stats/generator.rb +++ b/lib/git_stats/generator.rb @@ -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) diff --git a/lib/git_stats/git_data/comment_stat.rb b/lib/git_stats/git_data/comment_stat.rb index 4008d1f7a..0e2df709b 100644 --- a/lib/git_stats/git_data/comment_stat.rb +++ b/lib/git_stats/git_data/comment_stat.rb @@ -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 diff --git a/lib/git_stats/git_data/repo.rb b/lib/git_stats/git_data/repo.rb index a91ad143f..7a7f50135 100644 --- a/lib/git_stats/git_data/repo.rb +++ b/lib/git_stats/git_data/repo.rb @@ -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