Module: Bugspots

Defined in:
lib/bugspots/scanner.rb,
lib/bugspots/version.rb

Defined Under Namespace

Classes: Fix, Spot

Constant Summary collapse

ScoreThreshold =

Filter all results before this threshold

0.01
VERSION =
"0.2.1"

Class Method Summary collapse

Class Method Details

.scan(repo, branch = "master", fileExt = ".php", depth = 500, regex = nil) ⇒ Object



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
# File 'lib/bugspots/scanner.rb', line 10

def self.scan(repo, branch = "master", fileExt = ".php", depth = 500, regex = nil)

  regex ||= /\b(fix(es|ed)?|close(s|d)?)\b/i
  fixes = []

  repo = Rugged::Repository.new(repo)
  unless repo.branches.each_name(:local).sort.find { |b| b == branch }
    raise ArgumentError, "no such branch in the repo: #{branch}"
  end

  walker = Rugged::Walker.new(repo)
  walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE)
  walker.push(repo.branches[branch].target)
  walker.each do |commit|
    if commit.message =~ regex
      files = commit.diff(commit.parents.first).deltas.collect do |d|
        d.old_file[:path]
      end
      fixes << Fix.new(commit.message.split("\n").first, commit.time, files)
    end
  end

  hotspots = Hash.new(0)
  fixes.each do |fix|
    fix.files.each do |file|
      
      if File.extname(file).eql? fileExt
      
        # The timestamp used in the equation is normalized from 0 to 1, where
        # 0 is the earliest point in the code base, and 1 is now (where now is
        # when the algorithm was run). Note that the score changes over time
        # with this algorithm due to the moving normalization; it's not meant
        # to provide some objective score, only provide a means of comparison
        # between one file and another at any one point in time
        t = 1 - ((Time.now - fix.date).to_f / (Time.now - fixes.first.date))
        hotspots[file] += 1/(1+Math.exp((-12*t)+12))
      end
    end
  end

  # filter out the files with very low scores
  hotspots.each do |file, score|
    if score <= ScoreThreshold
      hotspots.delete(file)
    end
  end

  spots = hotspots.sort_by {|k,v| v}.reverse.collect do |spot|
    Spot.new(spot.first, sprintf('%.4f', spot.last))
  end

  return fixes, spots
end