Class: LinterChanges::GitDiff
- Inherits:
-
Object
- Object
- LinterChanges::GitDiff
- Defined in:
- lib/git_diff.rb
Constant Summary collapse
- DEFAULT_TARGET_BRANCH =
'origin/master'
Instance Method Summary collapse
- #changed_files ⇒ Object
- #changed_lines_contains?(file:, pattern:) ⇒ Boolean
-
#initialize(target_branch: nil) ⇒ GitDiff
constructor
A new instance of GitDiff.
Constructor Details
#initialize(target_branch: nil) ⇒ GitDiff
Returns a new instance of GitDiff.
9 10 11 12 |
# File 'lib/git_diff.rb', line 9 def initialize(target_branch: nil) @target_branch = target_branch || ENV['CHANGE_TARGET'] || DEFAULT_TARGET_BRANCH Logger.debug "Target branch: #{@target_branch}" end |
Instance Method Details
#changed_files ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/git_diff.rb', line 14 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 |
#changed_lines_contains?(file:, pattern:) ⇒ Boolean
29 30 31 32 33 34 35 36 37 |
# File 'lib/git_diff.rb', line 29 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 |