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.



27
28
29
30
31
32
33
34
35
36
# File 'lib/locat/gitloc.rb', line 27

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.



22
23
24
# File 'lib/locat/gitloc.rb', line 22

def matcher
  @matcher
end

#repoObject (readonly)

Returns the value of attribute repo.



24
25
26
# File 'lib/locat/gitloc.rb', line 24

def repo
  @repo
end

Instance Method Details

#commits_with_locObject



71
72
73
74
75
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
# File 'lib/locat/gitloc.rb', line 71

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.



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

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/locat/gitloc.rb', line 107

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



39
40
41
# File 'lib/locat/gitloc.rb', line 39

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