Skip to content

Commit ea45e74

Browse files
committed
Add "confirmed by gitattributes"
1 parent 00d12b7 commit ea45e74

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,25 @@ Ruby:
180180
181181
```
182182

183-
If a file's language was overridden using `.gitattributes`, the strategy will show the original detection method along with an override note (fictional example):
183+
If a file's language is affected by `.gitattributes`, the strategy will show the original detection method along with a note indicating whether the gitattributes setting changed the result or confirmed it.
184+
185+
For instance, if you had the following .gitattributes overrides in your repo:
186+
187+
```gitattributes
188+
189+
*.ts linguist-language=JavaScript
190+
*.js linguist-language=JavaScript
191+
192+
```
193+
194+
the output of Linguist would be something like this:
184195

185196
```console
186197
100.00% 217 JavaScript
187198

188199
JavaScript:
189200
demo.ts [Heuristics (overridden by .gitattributes)]
201+
demo.js [Extension (confirmed by .gitattributes)]
190202
```
191203

192204
##### `--json`
@@ -241,17 +253,30 @@ lib/linguist.rb: 105 lines (96 sloc)
241253
strategy: Extension
242254
```
243255

244-
If a file's language was overridden using `.gitattributes`, the strategy will show the original detection method along with an override note:
256+
If a file's language is affected by `.gitattributes`, the strategy will show whether the gitattributes setting changed the result or confirmed it:
245257

258+
In this ficticious example, it says "confirmed by .gitattributes" since the detection process (using the Filename strategy) would have given the same output as the override:
246259
```console
247-
$ github-linguist --strategies .devcontainer/devcontainer.json
248260
.devcontainer/devcontainer.json: 27 lines (27 sloc)
249261
type: Text
250262
mime type: application/json
251263
language: JSON with Comments
252-
strategy: Filename (overridden by .gitattributes)
264+
strategy: Filename (confirmed by .gitattributes)
253265
```
254266

267+
In this other example, it says "overridden by .gitattributes" since the gitattributes setting changes the detected language to something different:
268+
269+
```console
270+
$ github-linguist --strategies test/attributes/test.rb
271+
test/attributes/test.rb: 13 lines (11 sloc)
272+
type: Text
273+
mime type: application/x-ruby
274+
language: Java
275+
strategy: Extension (overridden by .gitattributes)
276+
```
277+
278+
Here, the `.rb` file would normally be detected as Ruby by the Extension strategy, but `.gitattributes` overrides it to be detected as Java instead.
279+
255280
##### `--json`
256281

257282
Using the `--json` flag will give you the output for a single file in JSON format:

lib/linguist/lazy_blob.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,19 @@ def language
7575
@language = if lang = git_attributes['linguist-language']
7676
detected_language = Language.find_by_alias(lang)
7777

78-
# If strategies are being tracked, get the overridden strategy that would have been used with a note that it was overridden
78+
# If strategies are being tracked, get the original strategy that would have been used
7979
if detected_language && Linguist.instrumenter
80-
# Get the overridden strategy by calling super (which calls Linguist.detect)
81-
super
82-
overridden_strategy_info = Linguist.instrumenter.detected_info[self.name]
83-
overridden_strategy = overridden_strategy_info ? overridden_strategy_info[:strategy] : "Unknown"
80+
# Get the original strategy by calling super (which calls Linguist.detect)
81+
original_language = super
82+
original_strategy_info = Linguist.instrumenter.detected_info[self.name]
83+
original_strategy = original_strategy_info ? original_strategy_info[:strategy] : "Unknown"
84+
85+
if original_language == detected_language
86+
strategy_name = "#{original_strategy} (confirmed by .gitattributes)"
87+
else
88+
strategy_name = "#{original_strategy} (overridden by .gitattributes)"
89+
end
8490

85-
strategy_name = "#{overridden_strategy} (overridden by .gitattributes)"
8691
strategy = Struct.new(:name).new(strategy_name)
8792
Linguist.instrument("linguist.detected", blob: self, strategy: strategy, language: detected_language)
8893
end

0 commit comments

Comments
 (0)