Class: Debtective::FindCommit

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

Overview

find the commit that introduced a line of code

Defined Under Namespace

Classes: Author, Commit

Constant Summary collapse

SPECIAL_CHARACTER_REGEX =
/(?!\w|\s|#|:).+/

Instance Method Summary collapse

Constructor Details

#initialize(pathname, code) ⇒ FindCommit

Returns a new instance of FindCommit.

Parameters:

  • pathname (Pathname)

    file path

  • code (String)

    line of code



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

def initialize(pathname, code)
  @pathname = pathname
  @code = code
end

Instance Method Details

#authorDebtective::FindCommit::Author



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

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

#callDebtective::FindCommit::Commit



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

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

#commitGit::Object::Commit

Returns:

  • (Git::Object::Commit)


45
46
47
# File 'lib/debtective/find_commit.rb', line 45

def commit
  git.gcommit(sha)
end

#gitGit::Base

Returns:

  • (Git::Base)


40
41
42
# File 'lib/debtective/find_commit.rb', line 40

def git
  Git.open(".")
end

#safe_codeString

characters “ and ‘ can break the git command

Returns:

  • (String)


62
63
64
# File 'lib/debtective/find_commit.rb', line 62

def safe_code
  @code.gsub(/"/, "\\\"").gsub("`", "\\\\`")
end

#shaString

commit sha

Returns:

  • (String)


51
52
53
54
55
56
57
58
# File 'lib/debtective/find_commit.rb', line 51

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)


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

def time
  commit.date
end