Skip to content

Language Savant. If your repository's language is being reported incorrectly, send us a pull request!

License

Notifications You must be signed in to change notification settings

david-waltermire/linguist

 
 

Repository files navigation

Linguist

Build Status

This library is used on GitHub.com to detect blob languages, ignore binary or vendored files, suppress generated files in diffs, and generate language breakdown graphs.

See Troubleshooting and CONTRIBUTING.md before filing an issue or creating a pull request.

How Linguist works

Linguist takes the list of languages it knows from languages.yml and uses a number of methods to try and determine the language used by each file, and the overall repository breakdown.

Linguist starts by going through all the files in a repository and excludes all files that it determines to be binary data, vendored code, generated code, documentation, or are defined as data (e.g. SQL) or prose (e.g. Markdown) languages, whilst taking into account any overrides.

If an explicit language override has been used, that language is used for the matching files. The language of each remaining file is then determined using the following strategies, in order, with each step either identifying the precise language or reducing the number of likely languages passed down to the next strategy:

  • Vim or Emacs modeline,
  • commonly used filename,
  • shell shebang,
  • file extension,
  • heuristics,
  • naïve Bayesian classification

The result of this analysis is used to produce the language stats bar which displays the languages percentages for the files in the repository. The percentages are calculated based on the bytes of code for each language as reported by the List Languages API.

language stats bar

How Linguist works on GitHub.com

When you push changes to a repository on GitHub.com, a low priority background job is enqueued to analyze your repository as explained above. The results of this analysis are cached for the lifetime of your repository and are only updated when the repository is updated. As this analysis is performed by a low priority background job, it can take a while, particularly during busy periods, for your language statistics bar to reflect your changes.

Usage

Install the gem:

$ gem install github-linguist

Dependencies

Linguist uses the charlock_holmes character encoding detection library which in turn uses ICU, and the libgit2 bindings for Ruby provided by rugged. These components have their own dependencies - icu4c, and cmake and pkg-config respectively - which you may need to install before you can install Linguist.

For example, on macOS with Homebrew: brew install cmake pkg-config icu4c and on Ubuntu: apt-get install cmake pkg-config libicu-dev.

Application usage

Linguist can be used in your application as follows:

require 'rugged'
require 'linguist'

repo = Rugged::Repository.new('.')
project = Linguist::Repository.new(repo, repo.head.target_id)
project.language       #=> "Ruby"
project.languages      #=> { "Ruby" => 119387 }

Command line usage

A repository's languages stats can also be assessed from the command line using the linguist executable. Without any options, linguist will output the breakdown that correlates to what is shown in the language stats bar. The --breakdown flag will additionally show the breakdown of files by language.

You can try running linguist on the root directory in this repository itself:

$ bundle exec bin/linguist --breakdown
68.57%  Ruby
22.90%  C
6.93%   Go
1.21%   Lex
0.39%   Shell

Ruby:
Gemfile
Rakefile
bin/git-linguist
bin/linguist
ext/linguist/extconf.rb
github-linguist.gemspec
lib/linguist.rb

Troubleshooting

My repository is detected as the wrong language

If the language stats bar is reporting a language that you don't expect:

  1. Click on the name of the language in the stats bar to see a list of the files that are identified as that language.
    Keep in mind this performs a search so the code search restrictions may result in files identified in the language statistics not appearing in the search results. Installing Linguist locally and running it from the command line will give you accurate results.
  2. If you see files that you didn't write in the search results, consider moving the files into one of the paths for vendored code, or use the manual overrides feature to ignore them.
  3. If the files are misclassified, search for open issues to see if anyone else has already reported the issue. Any information you can add, especially links to public repositories, is helpful. You can also use the manual overrides feature to correctly classify them in your repository.
  4. If there are no reported issues of this misclassification, open an issue and include a link to the repository or a sample of the code that is being misclassified.

Keep in mind that the repository language stats are only updated when you push changes, and the results are cached for the lifetime of your repository. If you have not made any changes to your repository in a while, you may find pushing another change will correct the stats.

My repository isn't showing my language

Linguist does not consider vendored code, generated code, documentation, or data (e.g. SQL) or prose (e.g. Markdown) languages (as defined by the type attribute in languages.yml) when calculating the repository language statistics.

If the language statistics bar is not showing your language at all, it could be for a few reasons:

  1. Linguist doesn't know about your language.
  2. The extension you have chosen is not associated with your language in languages.yml.
  3. All the files in your repository fall into one of the categories listed above that Linguist excludes by default.

If Linguist doesn't know about the language or the extension you're using, consider contributing to Linguist by opening a pull request to add support for your language or extension. For everything else, you can use the manual overrides feature to tell Linguist to include your files in the language statistics.

There's a problem with the syntax highlighting of a file

Linguist detects the language of a file but the actual syntax-highlighting is powered by a set of language grammars which are included in this project as a set of submodules as listed here.

If you experience an issue with the syntax-highlighting on GitHub, please report the issue to the upstream grammar repository, not here. Grammars are updated every time we build the Linguist gem so upstream bug fixes are automatically incorporated as they are fixed.

Overrides

Linguist supports a number of different custom override strategies for language definitions and file paths.

Using gitattributes

Add a .gitattributes file to your project and use standard git-style path matchers for the files you want to override using the linguist-documentation, linguist-language, linguist-vendored, linguist-generated and linguist-detectable attributes. .gitattributes will be used to determine language statistics and will be used to syntax highlight files. You can also manually set syntax highlighting using Vim or Emacs modelines.

$ cat .gitattributes
*.rb linguist-language=Java

Vendored code

Checking code you didn't write, such as JavaScript libraries, into your git repo is a common practice, but this often inflates your project's language stats and may even cause your project to be labeled as another language. By default, Linguist treats all of the paths defined in vendor.yml as vendored and therefore doesn't include them in the language statistics for a repository.

Use the linguist-vendored attribute to vendor or un-vendor paths.

$ cat .gitattributes
special-vendored-path/* linguist-vendored
jquery.js linguist-vendored=false

Documentation

Just like vendored files, Linguist excludes documentation files from your project's language stats. documentation.yml lists common documentation paths and excludes them from the language statistics for your repository.

Use the linguist-documentation attribute to mark or unmark paths as documentation.

$ cat .gitattributes
project-docs/* linguist-documentation
docs/formatter.rb linguist-documentation=false

Generated code

Not all plain text files are true source files. Generated files like minified JavaScript and compiled CoffeeScript can be detected and excluded from language stats. As an added bonus, unlike vendored and documentation files, these files are suppressed in diffs. generated.rb lists common generated paths and excludes them from the language statistics of your repository.

Use the linguist-generated attribute to mark or unmark paths as generated.

$ cat .gitattributes
Api.elm linguist-generated=true

Detectable

Only programming languages are included in the language statistics. Languages of a different type (as defined in languages.yml) are not "detectable" causing them not to be included in the language statistics.

Use the linguist-detectable attribute to mark or unmark paths as detectable.

$ cat .gitattributes
*.kicad_pcb linguist-detectable=true
*.sch linguist-detectable=true
tools/export_bom.py linguist-detectable=false

Using Emacs or Vim modelines

If you do not want to use .gitattributes to override the syntax highlighting used on GitHub.com, you can use Vim or Emacs style modelines to set the language for a single file. Modelines can be placed anywhere within a file and are respected when determining how to syntax-highlight a file on GitHub.com

Vim
# Some examples of various styles:
vim: syntax=java
vim: set syntax=ruby:
vim: set filetype=prolog:
vim: set ft=cpp:
Emacs
-*- mode: php;-*-

Contributing

Please check out our contributing guidelines.

License

The language grammars included in this gem are covered by their repositories' respective licenses. vendor/README.md lists the repository for each grammar.

All other files are covered by the MIT license, see LICENSE.

About

Language Savant. If your repository's language is being reported incorrectly, send us a pull request!

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 68.6%
  • C 22.6%
  • Go 7.1%
  • Lex 1.2%
  • Shell 0.5%