class Stefon::Surveyor::GritUtil

A class that abstracts dealing with the grit gem while respecting/knowing some restrictions, namely excluding authors

Attributes

last_xenocommit[R]

The last commit not made by the user (sui ~ self) but by another person (xeno ~ not self / not origin). This is important to take into account when the user makes multiple commits when working on someone else's project, to ensure that diffs do not include recent changes made by the user in her last few commits

num_sui_commits[R]

The number of commits by the user (sui ~ self / origin). This is important to take into account when the user makes multiple commits when working on someone else's project, to ensure that diffs do not include recent changes made by the user in her last few commits

repo[R]

Public Class Methods

new() click to toggle source

sets num_sui_commits and last_xenocommit attributes, if the current user is the only commiter in the repo, these are set to the most recent commit, since in this case, stefon would always recommend the current user anyway. In the future, an error may be raised here

# File lib/stefon/surveyor/grit_util.rb, line 29
def initialize
  @repo = Grit::Repo.new find_git_repo
  commits = @repo.commits(GitUtil::CURRENT_BRANCH)
  @num_sui_commits = commits.find_index do |commit|
    commit.author.name != @repo.config['user.name']
  end
  @num_sui_commits ||= 0
  @last_xeno_commit = commits[@num_sui_commits]
end

Public Instance Methods

blame_for(filename) click to toggle source
# File lib/stefon/surveyor/grit_util.rb, line 47
def blame_for(filename)
  @repo.blame(filename, @last_xeno_commit)
end
file_valid_top_author(blame, filename) click to toggle source
# File lib/stefon/surveyor/grit_util.rb, line 57
def file_valid_top_author(blame, filename)
  authored_lines = Hash.new(0)
  blame.lines.each do |l|
    author = l.commit.author.name
    authored_lines[author] += 1 if valid?(author)
  end
  top_author_line_pair = authored_lines.max_by { |a, numlines| numlines }
  top_author_line_pair.first if top_author_line_pair
end
find_git_repo() click to toggle source
# File lib/stefon/surveyor/grit_util.rb, line 39
def find_git_repo
  until File.exist?('.git')
    fail 'Could not find a git repository!' if Dir.pwd == '/'
    Dir.chdir '..'
  end
  Dir.pwd
end
valid_line_author(blame, line) click to toggle source
# File lib/stefon/surveyor/grit_util.rb, line 51
def valid_line_author(blame, line)
  matched_line = blame.lines.find { |l| l.line.strip == line }
  author = matched_line.commit.author.name if matched_line
  author if valid?(author)
end