Class: LintFu::SourceControl::Git

Inherits:
LintFu::SourceControlProvider show all
Defined in:
lib/lint_fu/source_control/git.rb

Constant Summary collapse

BLAME_REGEXP =
/^(.*) \((.+) [0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\s+([0-9]+)\)/

Instance Method Summary collapse

Methods inherited from LintFu::SourceControlProvider

for_directory, inherited

Constructor Details

#initialize(path) ⇒ Git

Returns a new instance of Git.



6
7
8
9
10
11
12
13
14
# File 'lib/lint_fu/source_control/git.rb', line 6

def initialize(path)
  super(path)

  `git version`
  raise ProviderNotInstalled, self unless $?.success?

  dot_git = File.join(path, '.git')
  raise ProviderError.new(self, path) unless File.directory?(dot_git)
end

Instance Method Details

#blame(file, line) ⇒ Object

Return

An array containing [author, commit_ref]


18
19
20
21
22
23
24
25
26
27
# File 'lib/lint_fu/source_control/git.rb', line 18

def blame(file, line)
  #commit, author, line_no,
  output = `git blame --date=short -w -L #{line} #{file}`
  match = BLAME_REGEXP.match(output)
  if $?.success? && match && (match[3].to_i == line)
    return [ match[1].strip, match[2].strip ]
  else
    raise ProviderError, output
  end
end

#excerpt(file, range, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lint_fu/source_control/git.rb', line 29

def excerpt(file, range, options={})
  blame   = options.has_key?(:blame)   ? options[:blame] : true

  return super unless blame 

  Dir.chdir(@root) do
    start_line = range.first
    end_line   = range.last
    relative_path = File.relative_path(@root, file)

    output = `git blame --date=short #{blame ? '' : '-s'} -w -L #{start_line},#{end_line} #{relative_path} 2> /dev/null`
    return output if $?.success?

    #HACK: if git blame failed, assume we need to bound according to end of file
    file_length = `wc -l #{relative_path}`.split[0].to_i
    end_line = file_length
    output = `git blame --date=short #{blame ? '' : '-s'} -w -L #{start_line},#{end_line} #{relative_path}`
    return output.split("\n") if $?.success?

    raise ProviderError.new("'git blame' failed with code #{$?.exitstatus}: #{output}")
  end
end