Sha256: aeb8ad83fb8997af9b5147fb96b53e64201fa3c9124cab1db771d7c7c40e405a

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require "pathname"
require "shellwords"

module Codeowners
  class Git
    def initialize(base_directory)
      @base_directory = Pathname.new(::File.expand_path(base_directory))
    end

    def contributors(file, debug = false)
      require "codeowners/git/contributors"
      output = git(["log", "--numstat", %(--pretty=format:"author:%aN email:%ae"), "--no-color", "--", escape(file)])
      print_debug(output, debug)

      Contributors.call(file, output)
    end

    private

    def git(command_and_args)
      execute(["git", "--git-dir=#{git_directory}", "--work-tree=#{work_tree}", "-c", "'color.ui=false'"] + command_and_args)
    end

    def work_tree
      escape(@base_directory.to_s)
    end

    def git_directory
      escape(@base_directory.join(".git").to_s)
    end

    def escape(string)
      Shellwords.shellescape(string)
    end

    def execute(command, env: {}, error: ->(err) { raise Codeowners::SystemCallError.new(err) })
      require "open3"

      Open3.popen3(env, command.join(" ")) do |_, stdout, stderr, wait_thr|
        error.call(stderr.read) unless wait_thr.value.success?
        return stdout.read
      end
    end

    def print_debug(output, debug)
      return unless debug

      puts output
      puts "\n" * 10
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
codeowners-0.0.5 lib/codeowners/git.rb
codeowners-0.0.4 lib/codeowners/git.rb
codeowners-0.0.3 lib/codeowners/git.rb
codeowners-0.0.2 lib/codeowners/git.rb