<commit_before>module Match
  class Encrypt
    require 'security'

    def server_name(git_url)
      ["match", git_url].join("_")
    end

    def password(git_url)
      password = ENV["<PASSWORD>"]
      unless password
        item = Security::InternetPassword.find(server: server_name(git_url))
        password = item.password if item
      end

      unless password
        UI.important "Enter the passphrase that should be used to encrypt/decrypt your certificates"
        UI.important "This passphrase is specific per repository and will be stored in your local keychain"
        UI.important "Make sure to remember the password, as you'll need it when you run match on a different machine"
        while password.to_s.length == 0
          password = ask("Passphrase for Git Repo: ".yellow) { |q| q.echo = "*" }
          password2 = ask("Type passphrase again: ".yellow) { |q| q.echo = "*" }
          if password != password2
            UI.error("Passhprases differ. Try again")
            password = ""
          end
        end
        store_password(git_url, password)
      end

      return password
    end

    def store_password(git_url, password)
      Security::InternetPassword.add(server_name(git_url), "", password)
    end

    # removes the password from the keychain again
    def clear_password(git_url)
      Security::InternetPassword.delete(server: server_name(git_url))
    end

    def encrypt_repo(path: nil, git_url: nil)
      iterate(path) do |current|
        crypt(path: current,
          password: password(git_url),
           encrypt: true)
        UI.success "🔒  Encrypted '#{File.basename(current)}'" if $verbose
      end
      UI.success "🔒  Successfully encrypted certificates repo"
    end

    def decrypt_repo(path: nil, git_url: nil, manual_password: nil)
      iterate(path) do |current|<commit_msg>Reuse single function to ask passphrase twice<commit_after>module Match
  class Encrypt
    require 'security'

    def server_name(git_url)
      ["match", git_url].join("_")
    end

    def password(git_url)
      password = ENV["<PASSWORD>"]
      unless password
        item = Security::InternetPassword.find(server: server_name(git_url))
        password = item.password if item
      end

      unless password
        UI.important "Enter the passphrase that should be used to encrypt/decrypt your certificates"
        UI.important "This passphrase is specific per repository and will be stored in your local keychain"
        UI.important "Make sure to remember the password, as you'll need it when you run match on a different machine"
        password = ChangePassword.ask_password()
        store_password(git_url, password)
      end

      return password
    end

    def store_password(git_url, password)
      Security::InternetPassword.add(server_name(git_url), "", password)
    end

    # removes the password from the keychain again
    def clear_password(git_url)
      Security::InternetPassword.delete(server: server_name(git_url))
    end

    def encrypt_repo(path: nil, git_url: nil)
      iterate(path) do |current|
        crypt(path: current,
          password: password(git_url),
           encrypt: true)
        UI.success "🔒  Encrypted '#{File.basename(current)}'" if $verbose
      end
      UI.success "🔒  Successfully encrypted certificates repo"
    end

    def decrypt_repo(path: nil, git_url: nil, manual_password: nil)
      iterate(path) do |current|
{'fraction_non_alphanumeric': 0.08601216333622937, 'fraction_numerical': 0.0008688097306689834, 'mean_word_length': 2.816574585635359, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '311816', 'n_tokens_mistral': 999, 'n_tokens_neox': 950, 'n_words': 305}