Sha256: 623a8bf50a71320618a68f4054237dc63d207f8308d08567495aa6af5ab0240f

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

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 escape_characters_in_string(string)
        pattern = %r{['".*/\\-]}
        string.gsub(pattern) { |match| "\\#{match}" }
      end

      private

      def calculate_stat
        escaped_string = escape_characters_in_string(commit.repo.comment_string)
        command = "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\"}'"
        stat_line = commit.repo.run(command).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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nova_git_stats-2.4.1 lib/git_stats/git_data/comment_stat.rb
nova_git_stats-2.4.0 lib/git_stats/git_data/comment_stat.rb
nova_git_stats-2.3.0 lib/git_stats/git_data/comment_stat.rb