class GitDiffParser::Patch

Parsed patch

Constants

MODIFIED_LINE
NOT_REMOVED_LINE
RANGE_INFORMATION_LINE
REMOVED_LINE

Attributes

body[RW]
file[RW]
secure_hash[RW]

Public Class Methods

new(body, options = {}) click to toggle source

@param body [String] patch section in `git diff`.

GitHub's pull request file's patch.
GitHub's commit file's patch.

 <<-BODY
 @@ -11,7 +11,7 @@ def valid?

    def run
      api.create_pending_status(*api_params, 'Hound is working...')
 -    @style_guide.check(pull_request_additions)
 +    @style_guide.check(api.pull_request_files(@pull_request))
      build = repo.builds.create!(violations: @style_guide.violations)
      update_api_status(build)
    end
 @@ -19,6 +19,7 @@ def run
    private

    def update_api_status(build = nil)
 +    # might not need this after using Rubocop and fetching individual files.
      sleep 1
      if @style_guide.violations.any?
        api.create_failure_status(*api_params, 'Hound does not approve', build_url(build))
 BODY

@param options [Hash] options @option options [String] :file file path @option options [String] 'file' file path @option options [String] :secure_hash target sha1 hash @option options [String] 'secure_hash' target sha1 hash

@see developer.github.com/v3/repos/commits/#get-a-single-commit @see developer.github.com/v3/pulls/#list-pull-requests-files

# File lib/git_diff_parser/patch.rb, line 50
def initialize(body, options = {})
  @body = body || ''
  @file = options[:file] || options['file'] if options[:file] || options['file']
  @secure_hash = options[:secure_hash] || options['secure_hash'] if options[:secure_hash] || options['secure_hash']
end

Public Instance Methods

changed_line_numbers() click to toggle source

@return [Array<Integer>] changed line numbers

# File lib/git_diff_parser/patch.rb, line 103
def changed_line_numbers
  changed_lines.map(&:number)
end
changed_lines() click to toggle source

@return [Array<Line>] changed lines

# File lib/git_diff_parser/patch.rb, line 57
def changed_lines
  line_number = 0

  lines.each_with_index.inject([]) do |lines, (content, patch_position)|
    case content
    when RANGE_INFORMATION_LINE
      line_number = Regexp.last_match[:line_number].to_i
    when MODIFIED_LINE
      line = Line.new(
        content: content,
        number: line_number,
        patch_position: patch_position
      )
      lines << line
      line_number += 1
    when NOT_REMOVED_LINE
      line_number += 1
    end

    lines
  end
end
find_patch_position_by_line_number(line_number) click to toggle source

@param line_number [Integer] line number

@return [Integer, nil] patch position

# File lib/git_diff_parser/patch.rb, line 110
def find_patch_position_by_line_number(line_number)
  target = changed_lines.find { |line| line.number == line_number }
  return nil unless target
  target.patch_position
end
removed_lines() click to toggle source

@return [Array<Line>] removed lines

# File lib/git_diff_parser/patch.rb, line 81
def removed_lines
  line_number = 0

  lines.each_with_index.inject([]) do |lines, (content, patch_position)|
    case content
    when RANGE_INFORMATION_LINE
      line_number = Regexp.last_match[:line_number].to_i
    when REMOVED_LINE
      line = Line.new(
        content: content,
        number: line_number,
        patch_position: patch_position
      )
      lines << line
      line_number += 1
    end

    lines
  end
end

Private Instance Methods

lines() click to toggle source
# File lib/git_diff_parser/patch.rb, line 118
def lines
  @body.lines
end