Skip to content

Commit

Permalink
Merge pull request #13 from CatanaCorp/ec-hunk-more-info
Browse files Browse the repository at this point in the history
Extract more info from the hunk header:
  • Loading branch information
Edouard-chin authored Aug 18, 2024
2 parents c6572c8 + 0b18284 commit 85ce9c8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/github_diff_parser/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def lines
#
# @example Representation of the previous_lino_start and new_lino_start in a Git Diff
# @@ -6,5 +6,6 @@ def test1 # => The first 6 is the previous_lino_start, the second is the new_lino_start
def add_hunk(previous_lino_start, new_lino_start)
hunks << Hunk.new(previous_lino_start, new_lino_start)
def add_hunk(previous_lino_start, new_lino_start, context)
hunks << Hunk.new(previous_lino_start, new_lino_start, context)
end

# Add a line belonging to the previously processed Git Hunk.
Expand Down
25 changes: 24 additions & 1 deletion lib/github_diff_parser/hunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ class Hunk
# @return [Integer] (see #initialize)
attr_reader :new_file_start_line

# @return [String] (see #initialize)
attr_reader :context

# @param previous_file_start_line [String] the starting line number of the hunk for the original file
# @param new_file_start_line [String] the starting line number of the hunk for the new file
# @param context [String]
#
# @example Representation of the previous_file_start_line and new_file_start_line in a Git Diff
# @@ -6,5 +6,6 @@ def test1 # => The first 6 is the previous_file_start_line the second is the new_file_start_line
def initialize(previous_file_start_line, new_file_start_line)
def initialize(previous_file_start_line, new_file_start_line, context)
@previous_file_start_line = Integer(previous_file_start_line)
@new_file_start_line = Integer(new_file_start_line)
@context = context
@lines = []
end

Expand Down Expand Up @@ -82,5 +87,23 @@ def find_previous_line(line_number)
def find_current_line(line_number)
lines.find { |line| line.current_number == line_number }
end

# The number of lines in the previous version.
#
# @example
# "@@ +3,4 -3,8 @@" This would return "4"
# "@@ +3 -3 @@" This would return "1"
def previous_line_count
contextual_lines.count + deletion_lines.count
end

# The number of lines in the new version.
#
# @example
# "@@ +3,4 -3,8 @@" This would return "8"
# "@@ +3 -3 @@" This would return "1"
def new_line_count
contextual_lines.count + addition_lines.count
end
end
end
2 changes: 1 addition & 1 deletion lib/github_diff_parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def process_diff_file_mode(match_data)
def add_hunk_to_diff(match_data)
validate_diff

@current_diff.add_hunk(match_data[:previous_lino_start], match_data[:new_lino_start])
@current_diff.add_hunk(match_data[:previous_lino_start], match_data[:new_lino_start], match_data[:context])
end

# Called when encountering a `-text` or `+text` or ` text` in the Git Diff output.
Expand Down
2 changes: 1 addition & 1 deletion lib/github_diff_parser/regexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ module Regexes
@@\s # Match '@@ '
-(?<previous_lino_start>\d+)(,\d+)?\s # Match '-1,11 ' or match '-1 ' and capture the '1' part
\+(?<new_lino_start>\d+)(,\d+)?\s # Match '+1,34 ' or match '+1 ' and capture the '1' part
@@.* # Match '@@ Any text'
@@\s?(?<context>.*) # Match '@@ Any text' and capture the 'Any text' part
\Z # End of line
}x

Expand Down
21 changes: 21 additions & 0 deletions test/github_diff_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def test_line_added
assert_equal(6, hunk.lines.count)
assert_equal(5, hunk.contextual_lines.count)
assert_equal(1, hunk.addition_lines.count)
assert_equal("def test1", hunk.context)
assert_equal(5, hunk.previous_line_count)
assert_equal(6, hunk.new_line_count)

expected_lines = [
{
Expand Down Expand Up @@ -81,6 +84,9 @@ def test_line_removed
assert_equal(6, hunk.lines.count)
assert_equal(5, hunk.contextual_lines.count)
assert_equal(1, hunk.deletion_lines.count)
assert_equal("def test1", hunk.context)
assert_equal(6, hunk.previous_line_count)
assert_equal(5, hunk.new_line_count)

expected_lines = [
{
Expand Down Expand Up @@ -147,6 +153,9 @@ def test_line_changed
assert_equal(5, hunk.contextual_lines.count)
assert_equal(1, hunk.deletion_lines.count)
assert_equal(1, hunk.addition_lines.count)
assert_equal("def test1", hunk.context)
assert_equal(6, hunk.previous_line_count)
assert_equal(6, hunk.new_line_count)

expected_lines = [
{
Expand Down Expand Up @@ -219,6 +228,9 @@ def test_file_added
hunk = parsed_diff.hunks.first
assert_equal(10, hunk.lines.count)
assert_equal(10, hunk.addition_lines.count)
assert_equal("", hunk.context)
assert_equal(0, hunk.previous_line_count)
assert_equal(10, hunk.new_line_count)

expected_lines = [
{
Expand Down Expand Up @@ -312,6 +324,9 @@ def test_file_removed
hunk = parsed_diff.hunks.first
assert_equal(11, hunk.lines.count)
assert_equal(11, hunk.deletion_lines.count)
assert_equal("", hunk.context)
assert_equal(11, hunk.previous_line_count)
assert_equal(0, hunk.new_line_count)

expected_lines = [
{
Expand Down Expand Up @@ -428,6 +443,9 @@ def test_rails_diff
assert_equal(7, hunk.lines.count)
assert_equal(6, hunk.contextual_lines.count)
assert_equal(1, hunk.deletion_lines.count)
assert_equal("class Railtie < Rails::Railtie # :nodoc:", hunk.context)
assert_equal(7, hunk.previous_line_count)
assert_equal(6, hunk.new_line_count)

expected_lines = [
{
Expand Down Expand Up @@ -484,6 +502,9 @@ def test_rails_diff
assert_equal(6, hunk.contextual_lines.count)
assert_equal(3, hunk.deletion_lines.count)
assert_equal(9, hunk.addition_lines.count)
assert_equal("class Railtie < Rails::Railtie # :nodoc:", hunk.context)
assert_equal(9, hunk.previous_line_count)
assert_equal(15, hunk.new_line_count)

expected_lines = [
{
Expand Down

0 comments on commit 85ce9c8

Please sign in to comment.