Class: LOCat::GitLOC

Inherits:
Object
  • Object
show all
Includes:
Grit
Defined in:
lib/locat/gitloc.rb

Overview

Based on ‘git-line-count.rb` by Tieg Zaharia

Constant Summary collapse

MAX_COMMITS =
1_000_000
OUTPUT_FILE =
"gitloc.html"
PER_DAY =

FILE_EXTENSION = /.(rb|js)$/ EXCLUDED_FILES = %w(files.js to.js exclude.js).map{ |str| Regexp.escape(str) }.join(‘|’) EXCLUDED = /#EXCLUDED_FILES/i

false
DATA_POINTS =

true = show 1-commit-per-day, false = show all commits

25

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher) ⇒ GitLOC

Returns a new instance of GitLOC.



32
33
34
35
36
37
38
39
40
41
# File 'lib/locat/gitloc.rb', line 32

def initialize(matcher)
  @matcher = matcher
  @repo    = Repo.new(".")

  @data_points      = DATA_POINTS
  @output_file      = OUTPUT_FILE
  #@file_extension   = FILE_EXTENSION
  #@exclude          = EXCLUDED
  @per_day          = PER_DAY
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



27
28
29
# File 'lib/locat/gitloc.rb', line 27

def matcher
  @matcher
end

#repoObject (readonly)

Returns the value of attribute repo.



29
30
31
# File 'lib/locat/gitloc.rb', line 29

def repo
  @repo
end

Instance Method Details

#commits_with_locObject



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
101
102
103
104
105
106
107
108
109
# File 'lib/locat/gitloc.rb', line 76

def commits_with_loc
  @commits_with_loc ||= (
    table        = []
    total_count  = Hash.new{|h,k|h[k]=0}   # gets reset every commit
    current_date = nil                     # gets reset every commit

    size = repo.commits('master', MAX_COMMITS).size
    mod  = size < @data_points ? 1 : (size / @data_points).round

    # puts repo.commits[0].methods.sort.join(', ')
    repo.commits('master', MAX_COMMITS).each_with_index do |commit, index|
      next unless index % mod == 0

      total_count = Hash.new{|h,k|h[k]=0}
      this_date   = commit.committed_date.to_date
      if !@per_day || (@per_day && this_date != current_date)
        # Record this commit as end-of-day commit
        current_date = this_date
        commit.tree.contents.each do |tob|
          recursive_loc_count(nil, tob, total_count)
        end
        table << {
          :date => commit.committed_date.to_datetime,
          :id   => commit.id,
          :loc  => total_count
        }
      else
        # The day this commits falls on has already been recorded
      end
    end

    table.reverse
  )
end

#recursive_loc_count(dir, tree_or_blob, total_count = nil) ⇒ Object

Count lines by groups.



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
# File 'lib/locat/gitloc.rb', line 49

def recursive_loc_count(dir, tree_or_blob, total_count=nil)
  total_count ||= Hash.new{|h,k| h[k]=0 }
  if tree_or_blob.is_a?(Grit::Tree) # directory
    tree_or_blob.contents.each do |tob|
      dname = dir ? File.join(dir, tree_or_blob.name) : tree_or_blob.name
      recursive_loc_count(dname, tob, total_count)
    end
  elsif tree_or_blob.is_a?(Grit::Blob) # file
    file = dir ? File.join(dir, tree_or_blob.name) : tree_or_blob.name
    matcher.each do |glob, block|
      if File.fnmatch?(glob, file)
        tree_or_blob.data.lines.each do |line|
          group = block.call(file, line)
          if group
            total_count[group] += 1
          end
          #total_count['Total'] += 1
        end
      end
    end
  else
    # what is it then?
  end
  total_count
end

#timeline_tableObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/locat/gitloc.rb', line 112

def timeline_table
  #mod = 7
  th = [nil]
  commits_with_loc.each_with_index do |commit, index|
    #if index % mod == 0 || index == commits_with_loc.size - 1
    #  th << commit[:date].strftime("%-y %-m %-d")
    #else
      th << commit[:date].strftime("%y %m %d")
    #end
  end
  tg = []
  groups = []
  commits_with_loc.each do |commit|
    groups = groups | commit[:loc].keys
  end
  groups.each_with_index do |g, i|
    tg[i] = [g]
  end
  tt = ['Total']
  commits_with_loc.each do |commit|
    sum = 0
    groups.each_with_index do |g, i|
      tg[i] << commit[:loc][g]
      sum += commit[:loc][g]
    end
    tt << sum
  end
  [th] + tg + [tt]
end

#titleObject



44
45
46
# File 'lib/locat/gitloc.rb', line 44

def title
  "'#{repo.head.name}' branch LOC #{@per_day ? 'per day' : 'per commit'}"
end