Class: Git
- Inherits:
-
Object
- Object
- Git
- Defined in:
- lib/gitstats/git.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ref ⇒ Object
readonly
Returns the value of attribute ref.
Instance Method Summary collapse
- #close_cache ⇒ Object
- #get_commits(last = nil, &block) ⇒ Object
- #get_files(ref = nil, &block) ⇒ Object
-
#initialize(name, base, ref = 'HEAD', debug = false, cachefile = nil) ⇒ Git
constructor
A new instance of Git.
- #open_cache ⇒ Object
- #read_cache ⇒ Object
- #write_cache(commit) ⇒ Object
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
#base ⇒ Object (readonly)
Returns the value of attribute base.
3 4 5 |
# File 'lib/gitstats/git.rb', line 3 def base @base end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
2 3 4 |
# File 'lib/gitstats/git.rb', line 2 def name @name end |
#ref ⇒ Object (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_cache ⇒ Object
18 19 20 21 22 23 |
# File 'lib/gitstats/git.rb', line 18 def close_cache unless @cache.nil? @cache.close @cache = nil end end |
#get_commits(last = nil, &block) ⇒ Object
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 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 |
# File 'lib/gitstats/git.rb', line 49 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 --pretty=format:\"HEADER: %at %ai %H %T %aN <%aE>\" #{range}") do |line| 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/gitstats/git.rb', line 126 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_cache ⇒ Object
14 15 16 |
# File 'lib/gitstats/git.rb', line 14 def open_cache @cache = File.new(@cachefile, 'a') end |
#read_cache ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gitstats/git.rb', line 37 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
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/gitstats/git.rb', line 25 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 |