Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit ed0ecfc

Browse files
committed
Merge pull request #57 from codeclimate/git_fallback
Fallback to git when calculating blob id
2 parents 786b820 + cec0f60 commit ed0ecfc

File tree

5 files changed

+68
-21
lines changed

5 files changed

+68
-21
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module CodeClimate
2+
module TestReporter
3+
4+
class CalculateBlob
5+
6+
def initialize(file_path)
7+
@file_path = file_path
8+
end
9+
10+
def blob_id
11+
calculate_with_file or calculate_with_git
12+
end
13+
14+
private
15+
16+
def calculate_with_file
17+
File.open(@file_path, "rb") do |file|
18+
header = "blob #{file.size}\0"
19+
content = file.read
20+
store = header + content
21+
22+
return Digest::SHA1.hexdigest(store)
23+
end
24+
rescue EncodingError
25+
puts "WARNING: Unable to read #{@file_path}\nUsing git for blob calculation"
26+
nil
27+
end
28+
29+
def calculate_with_git
30+
output = `git hash-object -t blob #{@file_path}`.chomp
31+
raise 'ERROR: Failed to calculate blob with git' unless $?.success?
32+
33+
output
34+
end
35+
36+
end
37+
38+
end
39+
end

lib/code_climate/test_reporter/formatter.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def to_payload(result)
4949

5050
{
5151
name: short_filename(file.filename),
52-
blob_id: calculate_blob_id(file.filename),
52+
blob_id: CalculateBlob.new(file.filename).blob_id,
5353
coverage: file.coverage.to_json,
5454
covered_percent: round(file.covered_percent, 2),
5555
covered_strength: round(file.covered_strength, 2),
@@ -81,15 +81,6 @@ def to_payload(result)
8181
}
8282
end
8383

84-
def calculate_blob_id(path)
85-
File.open(path, "rb") do |file|
86-
header = "blob #{file.size}\0"
87-
content = file.read.force_encoding("iso-8859-1").encode("utf-8", replace: nil)
88-
store = header + content
89-
90-
return Digest::SHA1.hexdigest(store)
91-
end
92-
end
9384

9485
def short_filename(filename)
9586
return filename unless ::SimpleCov.root

lib/codeclimate-test-reporter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require "code_climate/test_reporter"
2+
require "code_climate/test_reporter/calculate_blob"
23
require "code_climate/test_reporter/version"
34
require "code_climate/test_reporter/client"
45
require "code_climate/test_reporter/formatter"
56
require "code_climate/test_reporter/configuration"
67
require "code_climate/test_reporter/git"
78
require "code_climate/test_reporter/ci"
8-

spec/lib/calculate_blob_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'spec_helper'
2+
3+
module CodeClimate::TestReporter
4+
5+
describe CalculateBlob do
6+
7+
subject { CalculateBlob.new(fixture) }
8+
let(:fixture) { File.expand_path("../../fixtures/encoding_test.rb", __FILE__) }
9+
10+
it 'hex digests content of file' do
11+
expect(subject.blob_id).to_not be_nil
12+
end
13+
14+
context 'encoding error' do
15+
16+
let(:fixture) { File.expand_path("../../fixtures/encoding_test_iso.rb", __FILE__) }
17+
18+
it 'falls back to git' do
19+
expect(File).to receive(:open).and_raise(EncodingError)
20+
expect(subject.blob_id).to eq('eb82c22dadb9c47a7fed87211623f6856e112f46')
21+
end
22+
23+
end
24+
25+
end
26+
27+
end

spec/lib/formatter_spec.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,5 @@ module CodeClimate::TestReporter
137137
end
138138
end
139139
end
140-
141-
describe "#calculate_blob_id" do
142-
it "forces UTF-8 as encoding for the file content" do
143-
blob_id = formatter.calculate_blob_id(File.expand_path("../../fixtures/encoding_test_iso.rb", __FILE__))
144-
expect(blob_id).to_not be_nil
145-
146-
blob_id = formatter.calculate_blob_id(File.expand_path("../../fixtures/encoding_test.rb", __FILE__))
147-
expect(blob_id).to_not be_nil
148-
end
149-
end
150140
end
151141
end

0 commit comments

Comments
 (0)