Class: Debtective::Comments::FindCommit

Inherits:
Object
  • Object
show all
Defined in:
lib/debtective/comments/find_commit.rb

Overview

Find the commit that introduced the given line of code

Defined Under Namespace

Classes: Author, Commit

Instance Method Summary collapse

Constructor Details

#initialize(pathname:, line:) ⇒ FindCommit

Returns a new instance of FindCommit.

Parameters:

  • pathname (Pathname)

    file path

  • line (String)

    line of code



15
16
17
18
# File 'lib/debtective/comments/find_commit.rb', line 15

def initialize(pathname:, line:)
  @pathname = pathname
  @line = line
end

Instance Method Details

#authorDebtective::Comments::FindCommit::Author



29
30
31
# File 'lib/debtective/comments/find_commit.rb', line 29

def author
  Author.new(commit.author.email, commit.author.name)
end

#callDebtective::Comments::FindCommit::Commit



21
22
23
24
25
26
# File 'lib/debtective/comments/find_commit.rb', line 21

def call
  Commit.new(sha, author, time)
rescue Git::GitExecuteError
  author = Author.new(nil, nil)
  Commit.new(nil, author, nil)
end

#shaString

Returns:

  • (String)


39
40
41
42
43
44
45
46
# File 'lib/debtective/comments/find_commit.rb', line 39

def sha
  @sha ||=
    begin
      cmd = "git log -S \"#{safe_code}\" #{@pathname}"
      stdout, _stderr, _status = ::Open3.capture3(cmd)
      stdout[/commit (\w{40})\n/, 1]
    end
end

#timeTime

Returns:

  • (Time)


34
35
36
# File 'lib/debtective/comments/find_commit.rb', line 34

def time
  commit.date
end