-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscanner.rb
66 lines (58 loc) · 2.03 KB
/
scanner.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'open3'
require 'gitcommit'
require 'gitauthor'
# runs through a git repo and records the individual commits for later usage with Reporter
class Scanner
attr_reader :repo
attr_reader :commit_ct
attr_reader :commits
# constructed with the path to a git repo
# optional which specifies a git-revision range, ex: "HEAD..HEAD~50"
def initialize(repo, which=nil)
@repo = repo
@commits = []
@commit_ct = 0
@which = which
raise "cannot execute /usr/bin/git" unless File.executable?("/usr/bin/git")
raise "#{repo} is not a git repo" unless File.directory?("#{repo}/.git")
Dir.chdir(@repo) do
puts "scanning..." if @@options.verbose
pre_scan()
puts "processing #{@commit_ct} commits..." if @@options.verbose
log_scan()
puts "generating report..." if @@options.verbose
end
end
# count how many commits we have to process
def pre_scan()
Open3.popen3("git log --pretty=oneline") do |stdin,stdout,stderr|
stdout.each_line do |commit|
(hash, comment) = commit.split(' ', 1)
@commit_ct += 1
end
end
end
# scan the git logs
def log_scan()
commit = nil
cmd = "git log --numstat #{@which}"
puts cmd if @@options.verbose
Open3.popen3(cmd) do |stdin,stdout,stderr|
stdout.each_line do |line|
if line =~/^commit\s*(.*)/
@commits << commit unless commit.nil?
commit = GitCommit.new($1)
elsif line =~ /^Author:\s*(.+)/
commit.author = GitAuthor.new($1)
elsif line =~ /^Date:\s*(.+)/
commit.time = $1
elsif line =~ /^(\d+)\s+(\d+)\s+(.+)/
commit.changes << GitChange.new(commit,$3,$1,$2)
elsif line =~ /^\s*(.+)/
commit.comments << $1
end
end
end
@commits << commit unless commit.nil?
end
end