module Stefon::Surveyor::GitUtil

A module of usefull utils for dealing with the git cli directly

Public Class Methods

added_lines_by_file(&block) click to toggle source
# File lib/stefon/surveyor/git_util.rb, line 29
def self.added_lines_by_file(&block)
  lines_by_file(diff_as_array('+'), '++', block)
end
deleted_lines_by_file(&block) click to toggle source
# File lib/stefon/surveyor/git_util.rb, line 33
def self.deleted_lines_by_file(&block)
  lines_by_file(diff_as_array('-'), '--', block)
end
diff_as_array(mode = nil) click to toggle source
# File lib/stefon/surveyor/git_util.rb, line 20
def self.diff_as_array(mode = nil)
  # mode should be '+' for added lines, '-' for deleted lines, and
  # none for all looks at a diff, optionally matches lines starting
  # with mode char, cut off first char of each line
  %x(git diff HEAD~#{GritUtil.new.num_sui_commits} -U0 |
    #{mode ? "grep ^#{mode} | " : ''} sed 's/^.//'
  ).split("\n").map(&:strip)
end
lines_by_file(git_diff_as_array, filename_marker, block) click to toggle source
# File lib/stefon/surveyor/git_util.rb, line 37
def self.lines_by_file(git_diff_as_array, filename_marker, block)
  # delivers a line and its parent filename to a block
  git_diff_as_array.each_with_index do |e, i|
    line, lines_ahead = e, 1
    # if the line is a filename, we want it to point to its lines
    while (line[0..1] == filename_marker) &&
      (i + lines_ahead < git_diff_as_array.length)

      next_line = git_diff_as_array[i + lines_ahead]
      # next_lines should not be filenames
      break if next_line[0..1] == filename_marker
      block.call(line[5..-1], next_line)
      lines_ahead += 1
    end
  end
end
top_commiter() click to toggle source
# File lib/stefon/surveyor/git_util.rb, line 9
def self.top_commiter
  git_commiters = %x(git shortlog -s -n)
  # make a hash of authors pointing to num commits
  top_commiters = Hash[
    git_commiters.split("\n").map do |numcommits_author|
      numcommits_author.strip.split("\t").reverse
    end
  ]
  top_commiters.sort_by { |a, numlines| numlines.to_i }.first
end