Module: Stefon::Surveyor::GitUtil

Defined in:
lib/stefon/surveyor/git_util.rb

Overview

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

Class Method Summary collapse

Class Method Details

.added_lines_by_file(&block) ⇒ Object



29
30
31
# 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) ⇒ Object



33
34
35
# 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) ⇒ Object



20
21
22
23
24
25
26
27
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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_commiterObject



9
10
11
12
13
14
15
16
17
18
# 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