class Airbrake::Filters::GitRevisionFilter

Attaches current git revision to `context`. @api private @since v2.11.0

Constants

PREFIX

@return [String]

Attributes

weight[R]

@return [Integer]

Public Class Methods

new(root_directory) click to toggle source

@param [String] root_directory

# File lib/airbrake-ruby/filters/git_revision_filter.rb, line 14
def initialize(root_directory)
  @git_path = File.join(root_directory, '.git')
  @revision = nil
  @weight = 116
end

Public Instance Methods

call(notice) click to toggle source

@macro call_filter

# File lib/airbrake-ruby/filters/git_revision_filter.rb, line 21
def call(notice)
  return if notice[:context].key?(:revision)

  if @revision
    notice[:context][:revision] = @revision
    return
  end

  return unless File.exist?(@git_path)

  @revision = find_revision
  return unless @revision
  notice[:context][:revision] = @revision
end

Private Instance Methods

find_from_packed_refs(head) click to toggle source
# File lib/airbrake-ruby/filters/git_revision_filter.rb, line 52
def find_from_packed_refs(head)
  packed_refs_path = File.join(@git_path, 'packed-refs')
  return head unless File.exist?(packed_refs_path)

  File.readlines(packed_refs_path).each do |line|
    next if %w[# ^].include?(line[0])
    next unless (parts = line.split(' ')).size == 2
    return parts.first if parts.last == head
  end

  nil
end
find_revision() click to toggle source
# File lib/airbrake-ruby/filters/git_revision_filter.rb, line 38
def find_revision
  head_path = File.join(@git_path, 'HEAD')
  return unless File.exist?(head_path)

  head = File.read(head_path)
  return head unless head.start_with?(PREFIX)
  head = head.chomp[PREFIX.size..-1]

  ref_path = File.join(@git_path, head)
  return File.read(ref_path).chomp if File.exist?(ref_path)

  find_from_packed_refs(head)
end