.gitattributesをいじって特定のファイルをgithubのPRのdiffの対象から外す方法のメモ

.gitattributesをいじって特定のファイルをgithubのPRのdiffの対象から外す方法のメモ。

TL;DR

.gitattributes

<隠したいファイルのディレクトリ>/* linguist-generated

はじめに

例えばコード生成をしたり、利用の過程で複数のファイルを1つにまとめたファイルを作ったりすることがある。これらをrepositoryに含めた際にPRに含まれて邪魔に感じることがある。

今までdiffを隠せるのは、特定のファイルや例えば// Generated by xxxxのような特定のコメントなどが存在するものだけだと思っていた。任意のファイルを対象にできることが分かったのでそれのメモ。

linguist?

github.com

github上でのファイルの取扱いを決めるコードは公開されている。

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

気に入らなかったらPRを送ってくれとのこと。

linguist-generated

linguistのREADMEにdiffの対象から外す方法が書かれている(正確に言うとデフォルトではdiffの表示部分が折り畳まれるようになる。隠す方法といった方が正しいかもしれない)。

Overridesのセクションの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.

というわけで以下のような形で.gitattributesで指定してあげれば良い (Api.elmを生成された結果のファイルと見做してPRのdiff表示を隠す例)。

.gitattributes

Api.elm linguist-generated=true

ちなみにデフォルトで除外されるものは generated.rb に記述されている。

実際にdiffが気になるファイルを隠してみる

例えば以下の様な形でswagger doc(openAPI doc)が生成されていた時にbundle部分を隠したい。

srcディレクトリとbundleディレクトリが存在してsrcディレクトリにあるファイルをまとめたファイルをbundleディレクトリに置く。ここではxapp.yamlとyapp.yamlが生成されたファイル。これらをdiffの対象から省きたい。

swagger/
├── bundle
│   ├── xapp.yaml
│   └── yapp.yaml
└── src
    ├── xapp
    │   ├── api_bar.yaml
    │   ├── api_foo.yaml
    │   └── main.yaml
    └── yapp
        ├── api_boo.yaml
        └── main.yaml

以下の様な.gitattributesを書いてoverrideしてあげる。

.gitattributes

swagger/bundle/* linguist-generated

これでbundleディレクトリのものは全てdiffの対象から外れる。

生成されたファイルのdiffは隠される

やりましたね。

ちなみに

ちなみにその他設定できる属性には以下の様なものがある。

  • linguist-detectable
  • linguist-documentation
  • linguist-generated
  • linguist-language
  • linguist-vendored

おしまい。