Class: Blaml::GitBlamer

Inherits:
Object
  • Object
show all
Defined in:
lib/blaml/git_blamer.rb

Overview

The interface for git repo interaction. Write your own for other scm tools!

Constant Summary collapse

DATE_MATCHER =
%r{(\d+[-\s:]){6}[^\s]+}
META_MATCHER =
%r{([^\s]+)\s+([^\s]+)\s+\(([^\s]+)\s+(#{DATE_MATCHER})\s+(\d+)\)\s}

Class Method Summary collapse

Class Method Details

.blame(filepath) ⇒ Object

Returns the blamed contents of the given file.



19
20
21
22
23
24
25
26
# File 'lib/blaml/git_blamer.rb', line 19

def self.blame filepath
  filepath, filename = File.split filepath

  blame_str = `cd #{filepath} && git blame -f #{filename}`
  raise blame_str unless $?.success?

  blame_str
end

.parse(str) ⇒ Object

Parses the given string for blame data, returns a hash with blame data or nil if unparsable.

Hash keys returned are:

:file

String - The filepath

:line

Integer - The line this data came from

:author

String - The username of the author of the change

:commit

String - The revision identifier of the commit

:updated_at

Time - The time when the commit was made



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/blaml/git_blamer.rb', line 40

def self.parse str
  return unless str =~ META_MATCHER

  {
    :file       => $2,
    :line       => $6.to_i,
    :author     => $3,
    :commit     => $1,
    :updated_at => Time.parse($4)
  }
end