<commit_before>
# Commits Changes to git
#
module Mail2FrontMatter
  class Committer
    begin
      require 'rugged'
      @@available = true

    rescue LoadError
      @@available = false
    end

    def self.available?
      @@available
    end

    def self.commit(metadata, _body)
      repo = Rugged::Repository.new(Mail2FrontMatter.config[:git][:path])
      index = repo.index

      # stage frontmatter/erb
      relative_path = metadata[:filepath].gsub(Mail2FrontMatter.config[:git][:path] + '/', '')

      index.add(path: relative_path,
                oid: (Rugged::Blob.from_workdir repo, relative_path),
                mode: 0100644)

      # stage attachments
      metadata[:attachments].each_pair do |_k, filemeta|
        relative_path = filemeta[:filepath].gsub(Mail2FrontMatter.config[:git][:path] + '/', '')

        index.add(path: relative_path,
                  oid: (Rugged::Blob.from_workdir repo, relative_path),
                  mode: 0100644)
      end

      # commit
      tree = index.write_tree(repo)
      index.write

      author = {
        email: Mail2FrontMatter.config[:git][:email] || metadata[:from].match(/\<(.*)\>/)[1],
        name:  Mail2FrontMatter.config[:git][:name]  || metadata[:from].match(/(.*) \<.*\>/)[1],
        time:  Time.now
      }

      Rugged::Commit.create(repo, author:     author,
                                  committer:  author,
                                  message:    "post via email, #{metadata[:subject]}",
                                  parents:    [repo.head.target],
                                  tree:       tree,
                                  update_ref: 'HEAD')

      # push
      repo.push 'origin', ['refs/heads/master']

      # return sha
      repo.references['refs/heads/master'].target_id
    end
  end
end<commit_msg>Temporary passing of credentials until I have a better long term solution and abstraction and this all gets moved to config<commit_after>
# Commits Changes to git
#
module Mail2FrontMatter
  class Committer
    begin
      require 'rugged'
      @@available = true

    rescue LoadError
      @@available = false
    end

    def self.available?
      @@available
    end

    def self.commit(metadata, _body)
      repo = Rugged::Repository.new(Mail2FrontMatter.config[:git][:path])
      index = repo.index

      # stage frontmatter/erb
      relative_path = metadata[:filepath].gsub(Mail2FrontMatter.config[:git][:path] + '/', '')

      index.add(path: relative_path,
                oid: (Rugged::Blob.from_workdir repo, relative_path),
                mode: 0100644)

      # stage attachments
      metadata[:attachments].each_pair do |_k, filemeta|
        relative_path = filemeta[:filepath].gsub(Mail2FrontMatter.config[:git][:path] + '/', '')

        index.add(path: relative_path,
                  oid: (Rugged::Blob.from_workdir repo, relative_path),
                  mode: 0100644)
      end

      # commit
      tree = index.write_tree(repo)
      index.write

      author = {
        email: Mail2FrontMatter.config[:git][:email] || metadata[:from].match(/\<(.*)\>/)[1],
        name:  Mail2FrontMatter.config[:git][:name]  || metadata[:from].match(/(.*) \<.*\>/)[1],
        time:  Time.now
      }

      Rugged::Commit.create(repo, author:     author,
                                  committer:  author,
                                  message:    "post via email, #{metadata[:subject]}",
                                  parents:    [repo.head.target],
                                  tree:       tree,
                                  update_ref: 'HEAD')

      # push
      begin
        credentials = Rugged::Credentials::SshKeyFromAgent.new(username: 'git')
        repo.push 'origin', ['refs/heads/master'], { credentials: credentials }
      rescue StandardError => e
        Mail2FrontMatter.logger.info("Could not push!")
        Mail2FrontMatter.logger.error(e)
      end

      # return sha
      repo.references['refs/heads/master'].target_id
    end
  end
end
{'fraction_non_alphanumeric': 0.12540272614622058, 'fraction_numerical': 0.011400247831474598, 'mean_word_length': 1.9416909620991254, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2994125', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1143, 'n_words': 231}