Add .gitattributes override mention when returning the strategy by DecimalTurn · Pull Request #7600 · github-linguist/linguist · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 106 additions & 1 deletion README.md
20 changes: 19 additions & 1 deletion lib/linguist/lazy_blob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,25 @@ def language
return @language if defined?(@language)

@language = if lang = git_attributes['linguist-language']
Language.find_by_alias(lang)
detected_language = Language.find_by_alias(lang)

# If strategies are being tracked, get the original strategy that would have been used
if detected_language && Linguist.instrumenter
# Get the original strategy by calling super (which calls Linguist.detect)
original_language = super
original_strategy_info = Linguist.instrumenter.detected_info[self.name]
original_strategy = original_strategy_info ? original_strategy_info[:strategy] : "Unknown"

if original_language == detected_language
strategy_name = "#{original_strategy} (confirmed by .gitattributes)"
else
strategy_name = "#{original_strategy} (overridden by .gitattributes)"
end

strategy = Struct.new(:name).new(strategy_name)
Linguist.instrument("linguist.detected", blob: self, strategy: strategy, language: detected_language)
end
detected_language
else
super
end
Expand Down
21 changes: 21 additions & 0 deletions test/test_basic_instrumenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,25 @@ def test_tracks_filename_strategy
assert_equal "Filename", @instrumenter.detected_info[blob.name][:strategy]
assert_equal "Dockerfile", @instrumenter.detected_info[blob.name][:language]
end

def test_tracks_override_strategy
# Simulate a blob with a gitattributes override
blob = Linguist::FileBlob.new("Gemfile", "")
# Simulate detection with gitattributes strategy showing the override
strategy = Struct.new(:name).new("Filename (overridden by .gitattributes)")
language = Struct.new(:name).new("Java")
@instrumenter.instrument("linguist.detected", blob: blob, strategy: strategy, language: language) {}
assert @instrumenter.detected_info.key?(blob.name)
assert_match(/overridden by \.gitattributes/, @instrumenter.detected_info[blob.name][:strategy])
assert_equal "Java", @instrumenter.detected_info[blob.name][:language]
end
end

def test_override_strategy_is_recorded
# This file is overridden by .gitattributes to be detectable and language Markdown
blob = sample_blob("Markdown/tender.md")
Linguist.detect(blob)
assert @instrumenter.detected_info.key?(blob.name)
assert_includes ["GitAttributes"], @instrumenter.detected_info[blob.name][:strategy]
assert_equal "Markdown", @instrumenter.detected_info[blob.name][:language]
end
139 changes: 139 additions & 0 deletions test/test_cli_integration.rb