class Sanctify::Repo

Attributes

git[R]
path[R]

Public Class Methods

new(args, ignored_paths: []) click to toggle source
# File lib/sanctify/repo.rb, line 6
def initialize(args, ignored_paths: [])
  @path = args[:repo]
  @to = args[:to] # The default for `to` in git.diff is nil
  @from = args[:from] || 'HEAD'
  @git = Git.open(path)
  ignored_paths ||= []
  @ignored_paths = ignored_paths.map { |pattern| Regexp.new(pattern) }
end

Public Instance Methods

added_lines() click to toggle source
# File lib/sanctify/repo.rb, line 22
def added_lines
  [].tap do |lines|
    diff.each do |f|
      next if f.type == 'deleted'
      next if should_ignore? f.path
      f.patch.split("\n").each do |line|
        # don't include leading '+'
        lines << [line[1..-1], f.path] if added_line? line
      end
    end
  end
end
diff() click to toggle source
# File lib/sanctify/repo.rb, line 15
def diff
  # The diff processing is only done in the each method
  # so we'll call this method as a singleton so we don't accidentally
  # do this more than once per instance of the repo.
  @diff ||= git.diff(@from, @to).each.to_a
end

Private Instance Methods

added_line?(line) click to toggle source
# File lib/sanctify/repo.rb, line 47
def added_line?(line)
  line.start_with?('+') && !line.start_with?('+++')
end
should_ignore?(path) click to toggle source
# File lib/sanctify/repo.rb, line 37
def should_ignore?(path)
  # Add pattern matching for filenames so users can ignore files that
  # they know contain secrets that they have accepted as false positive.
  return false if @ignored_paths.empty?
  @ignored_paths.each do |regex|
    return true if regex.match(path)
  end
  false
end