Class: RailsErrorDashboard::Services::GitBlameReader

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/services/git_blame_reader.rb

Overview

Reads git blame information for specific file lines Executes git blame command and parses porcelain output

Examples:

reader = GitBlameReader.new("/path/to/file.rb", 42)
blame = reader.read_blame
# => { author: "John Doe", email: "john@example.com", date: Time, sha: "abc123", line: "code" }

Constant Summary collapse

COMMAND_TIMEOUT =

seconds

5
PORCELAIN_FIELDS =
%w[
  author
  author-mail
  author-time
  author-tz
  committer
  committer-mail
  committer-time
  committer-tz
  summary
  filename
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, line_number) ⇒ GitBlameReader

Initialize a new git blame reader

Parameters:

  • file_path (String)

    Path to the source file

  • line_number (Integer)

    Target line number



36
37
38
39
40
# File 'lib/rails_error_dashboard/services/git_blame_reader.rb', line 36

def initialize(file_path, line_number)
  @file_path = file_path
  @line_number = line_number.to_i
  @error = nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



30
31
32
# File 'lib/rails_error_dashboard/services/git_blame_reader.rb', line 30

def error
  @error
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



30
31
32
# File 'lib/rails_error_dashboard/services/git_blame_reader.rb', line 30

def file_path
  @file_path
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



30
31
32
# File 'lib/rails_error_dashboard/services/git_blame_reader.rb', line 30

def line_number
  @line_number
end

Instance Method Details

#git_available?Boolean

Check if git is available on the system

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
# File 'lib/rails_error_dashboard/services/git_blame_reader.rb', line 71

def git_available?
  @git_available ||= begin
    _stdout, _stderr, status = Open3.capture3("git", "--version")
    status.success?
  rescue StandardError
    false
  end
end

#read_blameHash?

Read git blame information for the target line

Returns:

  • (Hash, nil)

    Blame data hash or nil if unavailable



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails_error_dashboard/services/git_blame_reader.rb', line 45

def read_blame
  unless git_available?
    @error = "Git not available"
    return nil
  end

  unless File.exist?(file_path)
    @error = "File not found"
    return nil
  end

  # Execute git blame command
  output = execute_git_blame
  return nil unless output

  # Parse porcelain format output
  parse_blame_output(output)
rescue StandardError => e
  @error = "Error reading git blame: #{e.message}"
  RailsErrorDashboard::Logger.error("GitBlameReader error for #{file_path}:#{line_number} - #{e.message}")
  nil
end