Rails 5.2 の Credentials で diff を復号化してから比較する

先日、こんなブログを書きました。

sinsoku.hatenablog.com

この gem の機能に「diff を平文で表示できる」というものがあるけど、 gem 無くても実現出来ちゃったので紹介する。

1. 復号化するスクリプトを用意する

bin/decrypt を用意する。

#!/usr/bin/env ruby

system("rails r 'puts Rails.application.encrypted(\"#{ARGV[0]}\").read'")

実行権限をつけておく。

$ chmod +x bin/decrypt

2. Git の設定をする

まず、 .gitattributes を用意する。

$ echo config/credentials.yml.enc diff=rails_credentials >> .gitattributes

次に、 git config を設定する。

$ git config diff.rails_credentials.textconv bin/decrypt

3. credentials を変更し、git diff を実行する

$ rails credentials:edit
( change something )
$ git diff
Running via Spring preloader in process 42724
Running via Spring preloader in process 42756
diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc
index 1d31b4b..e701793 100644
--- a/config/credentials.yml.enc
+++ b/config/credentials.yml.enc
@@ -4,3 +4,5 @@
 
 # Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
 secret_key_base: 76804339ec1fcd2243da42662528f60dd5044ccb7f9f2479aa10dca307d12f706517ea22f013170f0dec98c8d6f022a62f54c0f031b88587608f272ce9d18c08
+
+foo: hello

こういう Tips みたいなのって rails/rails にプルリク投げた方が良いものなんだろうか。 Issue じゃないしなぁ...。