Sha256: 2d39d5476a24e142e00de2c4794f041f6dffa7026153cffb6643df2610a2d76c

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require "open3"

module CobraCommander
  # List of files changed in a git repository in the current branch in relation
  # to the give base branch
  #
  # @private
  #
  class GitChanged
    include Enumerable

    Error = Class.new(StandardError)
    InvalidSelectionError = Class.new(Error)

    def initialize(path, base_branch)
      @path = path
      @base_branch = base_branch
    end

    def each(&block)
      changes.each(&block)
    end

  private

    def changes
      @changes ||= Dir.chdir(@path) do
        git_dir, _, result = Open3.capture3("git", "rev-parse", "--git-dir")
        validate_result!(result)

        diff, _, result = Open3.capture3("git", "diff", "--name-only", @base_branch)
        validate_result!(result)

        git_dir = Pathname.new(git_dir)
        diff.split("\n").map { |f| git_dir.dirname.join(f) }
      end
    end

    def validate_result!(result)
      raise InvalidSelectionError, "Specified branch #{@base_branch} could not be found" if result.exitstatus == 128
      raise Error, "Uknown git error: #{result.inspect}" if result.exitstatus > 0
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cobra_commander-1.1.0 lib/cobra_commander/git_changed.rb
cobra_commander-1.0.1 lib/cobra_commander/git_changed.rb
cobra_commander-1.0.0 lib/cobra_commander/git_changed.rb