Class: Covered::Git::BranchChanges

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

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ BranchChanges

Returns a new instance of BranchChanges.



5
6
7
# File 'lib/covered/git/branch_changes.rb', line 5

def initialize(root)
  @root = root
end

Instance Method Details

#default_branchObject



13
14
15
# File 'lib/covered/git/branch_changes.rb', line 13

def default_branch
  "main"
end

#lines_modified(branch = nil) ⇒ Object

Compute the lines modified for a given branch, returning a hash of paths to a set of line numbers.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/covered/git/branch_changes.rb', line 19

def lines_modified(branch = nil)
  branch ||= default_branch
  result = Hash.new{|k,v| k[v] = Set.new}
  
  diff = repository.diff(repository.rev_parse(branch), repository.last_commit)
  
  diff.each_patch do |patch|
    path = patch.delta.new_file[:path]
    
    patch.each_hunk do |hunk|
      hunk.each_line do |line|
        result[path] << line.new_lineno if line.addition?
      end
    end
  end

  result.default = nil
  return result.freeze
end

#repositoryObject



9
10
11
# File 'lib/covered/git/branch_changes.rb', line 9

def repository
  @repository ||= Rugged::Repository.discover(@root)
end