Class: Git

Inherits:
Object
  • Object
show all
Defined in:
lib/gitstats/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, base, ref = 'HEAD', debug = false, cachefile = nil) ⇒ Git

Returns a new instance of Git.



6
7
8
9
10
11
12
# File 'lib/gitstats/git.rb', line 6

def initialize(name, base, ref = 'HEAD', debug = false, cachefile = nil)
  @name = name
  @base = base
  @ref = ref
  @debug = debug
  @cachefile = cachefile
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



3
4
5
# File 'lib/gitstats/git.rb', line 3

def base
  @base
end

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/gitstats/git.rb', line 2

def name
  @name
end

#refObject (readonly)

Returns the value of attribute ref.



4
5
6
# File 'lib/gitstats/git.rb', line 4

def ref
  @ref
end

Instance Method Details

#close_cacheObject



19
20
21
22
23
24
# File 'lib/gitstats/git.rb', line 19

def close_cache
  unless @cache.nil?
    @cache.close
    @cache = nil
  end
end

#get_commits(last = nil, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
105
106
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
# File 'lib/gitstats/git.rb', line 50

def get_commits(last = nil, &block)
  if last.nil?
    range = @ref
    unless @cachefile.nil?
      begin
        read_cache do |commit|
          block.call(commit)
          last = commit
        end
        range = "#{last[:hash]}..#{@ref}"
      rescue
      end
    end
  else
    range = "#{last}..#{@ref}"
  end

  open_cache unless @cachefile.nil?

  commit = nil
  sh("git log --reverse --summary --numstat --encoding=utf-8 --pretty=format:\"HEADER: %at %ai %H %T %aN <%aE>\" #{range}") do |line|
    # My `git log` sometimes returns non utf-8 strings...
    # The workaround tries to recover from such scenarios.
    unless line.valid_encoding?
      line = line.force_encoding('ISO-8859-15').encode!('UTF-8', :invalid => :replace, :undef => :replace, :replace => "")
    end

    if line =~ /^HEADER:/
      unless commit.nil?
        write_cache(commit) unless @cachefile.nil?
        block.call(commit)
      end

      parts = line.split(' ', 8)
      parts.shift

      commit = Hash.new
      commit[:time] = Time.at(parts[0].to_i)
      commit[:timezone] = parts[3]
      commit[:hash] = parts[4]
      commit[:tree] = parts[5]
      name = nil
      email = ''
      match = /^(.+) <(.+)>$/.match(parts[6])
      if match.nil?
        name = parts[6]
      else
        name, email = match.captures
      end
      commit[:author] = Author.new(name, email)
      commit[:files_added] = 0
      commit[:files_deleted] = 0
      commit[:lines_added] = 0
      commit[:lines_deleted] = 0
    elsif line == ''
      write_cache(commit) unless @cachefile.nil?
      block.call(commit)
      commit = nil
    elsif line =~ /^ /
      if line =~ /^ create/
        commit[:files_added] += 1
      elsif line =~ /^ delete/
        commit[:files_deleted] += 1
      end
    else
      match = /^(\d+)\s+(\d+)/.match(line)
      unless match.nil?
        added, deleted = match.captures
        commit[:lines_added] += added.to_i
        commit[:lines_deleted] += deleted.to_i
      end
    end
  end

  unless commit.nil?
    write_cache(commit) unless @cachefile.nil?
    block.call(commit)
  end

ensure
  close_cache unless @cachefile.nil?
end

#get_files(ref = nil, &block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/gitstats/git.rb', line 133

def get_files(ref = nil, &block)
  ref ||= @ref

  sh("git ls-tree -r -l #{ref}").split(/\n/).each do |line|
    parts = line.split(/\s+/, 5)
    next if parts[1] != 'blob'

    file = Hash.new
    file[:hash] = parts[2]
    file[:size] = parts[3].to_i
    file[:name] = parts[4]

    block.call(file)
  end
end

#open_cacheObject



14
15
16
17
# File 'lib/gitstats/git.rb', line 14

def open_cache
  FileUtils.mkdir_p(File.dirname(@cachefile))
  @cache = File.new(@cachefile, 'a')
end

#read_cacheObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gitstats/git.rb', line 38

def read_cache
  f = File.new(@cachefile)
  while(!f.eof?)
    tmp = f.read(2)
    len = (tmp[0] << 8) + tmp[1]
    obj = f.read(len)
    raise "Read short object" if obj.size != len
    yield Marshal.load(obj)
  end
  f.close
end

#write_cache(commit) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gitstats/git.rb', line 26

def write_cache(commit)
  obj = Marshal.dump(commit)
  raise "Object too large" if obj.size > 65535

  str = ((obj.size >> 8) & 0xff).chr
  str += (obj.size & 0xff).chr
  str += obj

  @cache.write(str)
  @cache.flush
end