dyoung522/devtools-jdiff

View on GitHub
lib/jira_diff/git.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'open3'

module JIRADiff
  class Git

    def initialize(dir = '.')
      raise StandardError, "Directory '#{dir}' is not valid" unless Dir.exist?(dir)
      raise RuntimeError, "Doesn't look like '#{dir}' is a Git repository" unless Dir.exist?(File.join(dir, '.git'))

      @working_dir = dir
    end

    def log(branch)
      raise RuntimeError, "Invalid branch: #{branch}" unless branch_valid? branch
      run_command("\\git --no-pager log --no-merges --pretty='%H|%B\1' #{branch}").split("\1")
    end

    def branch_valid?(branch)
      run_command("\\git branch --all --list #{branch}").split("/n")[0] =~ /#{branch}/
    end

    private

    def run_command(cmd)
      Open3.popen3(cmd, chdir: @working_dir) do |_i, o, _e, _t|
        o.read
      end
    end
  end
end