Sha256: 66a7979fdb5ac70e6ce613b2ec84ee4edfcd374cfdb5f58f2e93c62768972a1d

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# lib/your_linter_gem/git_diff.rb

require 'open3'

module LinterChanges
  class GitDiff
    DEFAULT_TARGET_BRANCH = 'origin/master'

    def initialize(target_branch: nil)
      @target_branch = target_branch || ENV['CHANGE_TARGET'] || DEFAULT_TARGET_BRANCH
      Logger.debug "Target branch: #{@target_branch}"
    end

    def changed_files
      cmd = "git diff --name-only #{@target_branch}...HEAD"
      Logger.debug "Executing command: #{cmd}"

      stdout, stderr, status = Open3.capture3(cmd)
      unless status.success?
        Logger.error "Error obtaining git changes: #{stderr}"
        exit 1
      end

      files = stdout.strip.split("\n")
      Logger.debug "Changed files: #{files.join(', ')}"
      files
    end

    def changed_lines_contains? file:, pattern:
      cmd = "git diff #{@target_branch}...HEAD -- #{file}"
      stdout, stderr, status = Open3.capture3(cmd)
      unless status.success?
        Logger.error "Error obtaining git diff for #{file}: #{stderr}"
        exit 1
      end
      stdout.include? pattern
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
linter_changes-0.2.0 lib/git_diff.rb
linter_changes-0.1.0 lib/git_diff.rb