aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/credentials/credentials_command/diffing.rb
blob: b7330178a3d9893e76b0a8ab33b4ab376fa4aac4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

module Rails::Command::CredentialsCommand::Diffing # :nodoc:
  class Error < StandardError; end

  def enable_diffing
    if enabled?
      say "Already enabled!"
    else
      enable
      say "Diffing enabled! Editing a credentials file will display a diff of what actually changed."
    end
  rescue Error
    say "Couldn't setup Git to enable credentials diffing."
  end

  private
    def enabled?
      system "git config --get diff.rails_credentials.textconv", out: File::NULL
    end

    def enable
      raise Error unless system("git config diff.rails_credentials.textconv 'bin/rails credentials:diff'")

      Rails.root.join(".gitattributes").write(<<~end_of_template, mode: "a")
        config/credentials/*.yml.enc diff=rails_credentials
        config/credentials.yml.enc diff=rails_credentials
      end_of_template
    end
end