<commit_before>require 'pathname'

module Waddup

  class Source::Git < Waddup::Source
    extend Waddup::Extension::System

    attr_accessor :author, :repos

    # Retrieves author and repositories on initialization
    #
    # Arguments
    #
    #   :base_path (defaults to current working directory)
    #
    def initialize(base_path = Dir.pwd)
      @author = `git config --get user.name`.chomp
      @repos = Dir["#{base_path}/**/.git"]
    end

    # Aggregates events from all repositories
    #
    # Arguments:
    #
    #   :from (datetime)
    #   :to   (datetime)
    #
    def events(from, to)
      events = repos.map do |repo|
        events_for_repo from, to, repo
      end
      events.flatten!
    end

    # See: https://www.kernel.org/pub/software/scm/git/docs/git-log.html#_pretty_formats
    GIT_FORMAT = '%h %ai %s'

    # Pattern to extract from the above format
    EXTRACT_PATTERN = /([0-9a-f]{7}) ([-0-9: ]+\+\d{4}) (.+)/i

    # Aggregates events for given repository
    #
    # Arguments:
    #
    #   :from (datetime)
    #   :to   (datetime)
    #   :repo (path)
    #
    def events_for_repo(from, to, repo)
      log = `git log --author='#{author}' --since='#{from.iso8601}' --until='#{to.iso8601}' --format='format:#{GIT_FORMAT}'`
      log.scan(EXTRACT_PATTERN).map do |hash, datetime, subject|
        Waddup::Event.new do |e|
          e.at = DateTime.parse(datetime)
          e.source = self
          e.subject = subject
        end
      end
    end

    # Requires Git to be installed successfully
    def self.usable?
      run 'git --version', quietly: true
      $?.success?
    end

  end

end<commit_msg>Use system extension to run git-log<commit_after>require 'pathname'

module Waddup

  class Source::Git < Waddup::Source
    include Waddup::Extension::System
    extend Waddup::Extension::System

    attr_accessor :author, :repos

    # Retrieves author and repositories on initialization
    #
    # Arguments
    #
    #   :base_path (defaults to current working directory)
    #
    def initialize(base_path = Dir.pwd)
      @author = `git config --get user.name`.chomp
      @repos = Dir["#{base_path}/**/.git"]
    end

    # Aggregates events from all repositories
    #
    # Arguments:
    #
    #   :from (datetime)
    #   :to   (datetime)
    #
    def events(from, to)
      events = repos.map do |repo|
        events_for_repo from, to, repo
      end
      events.flatten!
    end

    # See: https://www.kernel.org/pub/software/scm/git/docs/git-log.html#_pretty_formats
    GIT_FORMAT = '%h %ai %s'

    # Pattern to extract from the above format
    EXTRACT_PATTERN = /([0-9a-f]{7}) ([-0-9: ]+\+\d{4}) (.+)/i

    # Aggregates events for given repository
    #
    # Arguments:
    #
    #   :from (datetime)
    #   :to   (datetime)
    #   :repo (path)
    #
    def events_for_repo(from, to, repo)
      log = run "git log --author='#{author}' --since='#{from.iso8601}' --until='#{to.iso8601}' --format='format:#{GIT_FORMAT}'"
      log.scan(EXTRACT_PATTERN).map do |hash, datetime, subject|
        Waddup::Event.new do |e|
          e.at = DateTime.parse(datetime)
          e.source = self
          e.subject = subject
        end
      end
    end

    # Requires Git to be installed successfully
    def self.usable?
      run 'git --version', quietly: true
      $?.success?
    end

  end

end
{'fraction_non_alphanumeric': 0.1335109532267614, 'fraction_numerical': 0.008288928359976317, 'mean_word_length': 2.2678916827853, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '740922', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1130, 'n_words': 277}