Class: GitDiffParser::Patch

Inherits:
Object
  • Object
show all
Defined in:
lib/git_diff_parser/patch.rb

Overview

Parsed patch

Constant Summary collapse

RANGE_INFORMATION_LINE =
/^@@ .+\+(?<line_number>\d+),/
MODIFIED_LINE =
/^\+(?!\+|\+)/
REMOVED_LINE =
/^[-]/
NOT_REMOVED_LINE =
/^[^-]/
NO_NEWLINE_MESSAGE =
/^\\ No newline at end of file$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, options = {}) ⇒ Patch

Returns a new instance of Patch.

Parameters:

  • 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
    
  • options (Hash) (defaults to: {})

    options

Options Hash (options):

  • :file (String)

    file path

  • 'file' (String)

    file path

  • :secure_hash (String)

    target sha1 hash

  • 'secure_hash' (String)

    target sha1 hash

See Also:



51
52
53
54
55
# File 'lib/git_diff_parser/patch.rb', line 51

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

Instance Attribute Details

#bodyString?

Returns patch section in ‘git diff` or nil.

Returns:

  • (String, nil)

    patch section in ‘git diff` or nil

See Also:



11
12
13
# File 'lib/git_diff_parser/patch.rb', line 11

def body
  @body
end

#fileString?

Returns file path or nil.

Returns:

  • (String, nil)

    file path or nil



11
12
13
# File 'lib/git_diff_parser/patch.rb', line 11

def file
  @file
end

#secure_hashString?

Returns target sha1 hash or nil.

Returns:

  • (String, nil)

    target sha1 hash or nil



11
12
13
# File 'lib/git_diff_parser/patch.rb', line 11

def secure_hash
  @secure_hash
end

Instance Method Details

#changed_line_numbersArray<Integer>

Returns changed line numbers.

Returns:

  • (Array<Integer>)

    changed line numbers



106
107
108
# File 'lib/git_diff_parser/patch.rb', line 106

def changed_line_numbers
  changed_lines.map(&:number)
end

#changed_linesArray<Line>

Returns changed lines.

Returns:

  • (Array<Line>)

    changed lines



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/git_diff_parser/patch.rb', line 58

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 NO_NEWLINE_MESSAGE
      # nop
    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) ⇒ Integer?

Returns patch position.

Parameters:

  • line_number (Integer)

    line number

Returns:

  • (Integer, nil)

    patch position



113
114
115
116
117
# File 'lib/git_diff_parser/patch.rb', line 113

def (line_number)
  target = changed_lines.find { |line| line.number == line_number }
  return nil unless target
  target.patch_position
end

#removed_linesArray<Line>

Returns removed lines.

Returns:

  • (Array<Line>)

    removed lines



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/git_diff_parser/patch.rb', line 84

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