Sha256: 4dcb1ce4b5692ea8e5c4210d8355af3eb09847a6233c2d820394901143d6702a

Contents?: true

Size: 883 Bytes

Versions: 2

Compression:

Stored size: 883 Bytes

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

    InvalidSelectionError = Class.new(StandardError)

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

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

  private

    def changes
      @changes ||= begin
        diff, _, result = Dir.chdir(@repo_root) do
          Open3.capture3("git", "diff", "--name-only", @base_branch)
        end

        raise InvalidSelectionError, "Specified branch #{@base_branch} could not be found" if result.exitstatus == 128

        diff.split("\n").map do |f|
          File.join(@repo_root, f)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cobra_commander-0.15.1 lib/cobra_commander/git_changed.rb
cobra_commander-0.15.0 lib/cobra_commander/git_changed.rb