class CodeClimate::TestReporter::CalculateBlob

Public Class Methods

new(file_path) click to toggle source
# File lib/code_climate/test_reporter/calculate_blob.rb, line 6
def initialize(file_path)
  @file_path = file_path
end

Public Instance Methods

blob_id() click to toggle source
# File lib/code_climate/test_reporter/calculate_blob.rb, line 10
def blob_id
  calculate_with_file or calculate_with_git
end

Private Instance Methods

calculate_with_file() click to toggle source
# File lib/code_climate/test_reporter/calculate_blob.rb, line 16
def calculate_with_file
  File.open(@file_path, "rb") do |file|
    header = "blob #{file.size}\00""
    content = file.read
    store = header + content

    return Digest::SHA1.hexdigest(store)
  end
rescue EncodingError
  puts "WARNING: Unable to read #{@file_path}\nUsing git for blob calculation"
  nil
end
calculate_with_git() click to toggle source
# File lib/code_climate/test_reporter/calculate_blob.rb, line 29
def calculate_with_git
  output = %xgit hash-object -t blob #{@file_path}`.chomp
  raise 'ERROR: Failed to calculate blob with git' unless $?.success?

  output
end