unified_diff 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +5 -1
- data/VERSION +1 -1
- data/lib/unified_diff/chunk.rb +36 -10
- data/lib/unified_diff/diff.rb +7 -7
- data/test/test_chunk.rb +16 -16
- data/test/test_unified_diff.rb +14 -14
- data/unified_diff.gemspec +1 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
= unified_diff
|
2
2
|
|
3
|
-
|
3
|
+
unified_diff is a simple library to parse unified diffs into clean ruby objects.
|
4
|
+
|
5
|
+
Simply UnifiedDiff.parse(diff_contents) and you'll get back a Diff object with metadata from your diff as well as a list of chunks accesible via Diff#chunks.
|
6
|
+
|
7
|
+
More features to follow.
|
4
8
|
|
5
9
|
== Contributing to unified_diff
|
6
10
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/unified_diff/chunk.rb
CHANGED
@@ -1,30 +1,56 @@
|
|
1
1
|
module UnifiedDiff
|
2
2
|
class Chunk
|
3
|
-
attr_reader :
|
3
|
+
attr_reader :original_range,:modified_range,:raw_lines
|
4
|
+
|
5
|
+
# Create a new chunk with the specified modified and original ranges
|
6
|
+
#
|
7
|
+
# @param [Hash] A hash with keys for original and modified line range
|
8
|
+
# @return [Array] the lines in the modified version of the chunk
|
4
9
|
def initialize(ranges)
|
5
|
-
@
|
6
|
-
@
|
10
|
+
@original_range = ranges[:original]
|
11
|
+
@modified_range = ranges[:modified]
|
7
12
|
@raw_lines = []
|
8
13
|
end
|
9
14
|
|
15
|
+
|
16
|
+
# Return an array of lines that are present in the modified version of the chunk
|
17
|
+
#
|
18
|
+
# @return [Array] the lines in the modified version of the chunk
|
19
|
+
def modified_lines
|
20
|
+
@raw_lines.select {|line| line[0] == ' ' || line[0] == '+' }.map {|line| line[1..-1] }
|
21
|
+
end
|
22
|
+
|
23
|
+
# Return an array of lines that are present in the original version of the chunk
|
24
|
+
#
|
25
|
+
# @return [Array] the lines in the original version of the chunk
|
26
|
+
def original_lines
|
27
|
+
@raw_lines.select {|line| line[0] == ' ' || line[0] == '-' }.map {|line| line[1..-1] }
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
# Insert a new addition line into the list of lines for this chunk
|
32
|
+
#
|
33
|
+
# @param [String] the line to be inserted (without '+' tag)
|
34
|
+
# @return [Array] the new list of raw lines
|
10
35
|
def insert_addition(line)
|
11
36
|
@raw_lines << "+#{line}"
|
12
37
|
end
|
13
38
|
|
39
|
+
# Insert a new removal line into the list of lines for this chunk
|
40
|
+
#
|
41
|
+
# @param [String] the line to be inserted (without '-' tag)
|
42
|
+
# @return [Array] the new list of raw lines
|
14
43
|
def insert_removal(line)
|
15
44
|
@raw_lines << "-#{line}"
|
16
45
|
end
|
17
46
|
|
47
|
+
# Insert a new unchanged line into the list of lines for this chunk
|
48
|
+
#
|
49
|
+
# @param [String] the line to be inserted (without ' ' tag)
|
50
|
+
# @return [Array] the new list of raw lines
|
18
51
|
def insert_unchanged(line)
|
19
52
|
@raw_lines << " #{line}"
|
20
53
|
end
|
21
54
|
|
22
|
-
def new_lines
|
23
|
-
@raw_lines.select {|line| line[0] == ' ' || line[0] == '+' }.map {|line| line[1..-1] }
|
24
|
-
end
|
25
|
-
|
26
|
-
def old_lines
|
27
|
-
@raw_lines.select {|line| line[0] == ' ' || line[0] == '-' }.map {|line| line[1..-1] }
|
28
|
-
end
|
29
55
|
end
|
30
56
|
end
|
data/lib/unified_diff/diff.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'time'
|
2
2
|
module UnifiedDiff
|
3
3
|
class Diff
|
4
|
-
attr_reader :original, :
|
4
|
+
attr_reader :original, :original_file, :original_timestamp, :modified_file, :modified_timestamp, :chunks
|
5
5
|
class UnifiedDiffException < Exception; end
|
6
6
|
|
7
7
|
FILE_PATTERN = /(.*)\t'{2}?(.*)'{2}?/
|
@@ -24,18 +24,18 @@ module UnifiedDiff
|
|
24
24
|
@original.each_line do |line|
|
25
25
|
case line
|
26
26
|
when OLD_FILE_PATTERN
|
27
|
-
@
|
27
|
+
@original_file, @original_timestamp = $1, Time.parse($2)
|
28
28
|
when NEW_FILE_PATTERN
|
29
|
-
@
|
29
|
+
@modified_file, @modified_timestamp = $1, Time.parse($2)
|
30
30
|
when CHUNK_PATTERN
|
31
|
-
@working_chunk = Chunk.new(:
|
31
|
+
@working_chunk = Chunk.new(original: ($1.to_i..$2.to_i), modified: ($3.to_i..$4.to_i))
|
32
32
|
@chunks << @working_chunk
|
33
33
|
when ADDED_PATTERN
|
34
|
-
@working_chunk.insert_addition
|
34
|
+
@working_chunk.send(:insert_addition, $1)
|
35
35
|
when REMOVED_PATTERN
|
36
|
-
@working_chunk.insert_removal
|
36
|
+
@working_chunk.send(:insert_removal, $1)
|
37
37
|
when UNCHANGED_PATTERN
|
38
|
-
@working_chunk.insert_unchanged
|
38
|
+
@working_chunk.send(:insert_unchanged, $1)
|
39
39
|
else
|
40
40
|
raise UnifiedDiffException.new("Unknown Line Type for Line:\n#{line}")
|
41
41
|
end
|
data/test/test_chunk.rb
CHANGED
@@ -2,32 +2,32 @@ require 'helper'
|
|
2
2
|
|
3
3
|
class TestChunk < MiniTest::Unit::TestCase
|
4
4
|
def setup
|
5
|
-
@chunk = UnifiedDiff::Chunk.new(
|
5
|
+
@chunk = UnifiedDiff::Chunk.new(original: (1..2), modified: (1..3))
|
6
6
|
end
|
7
7
|
|
8
8
|
def test_ranges_accessible
|
9
|
-
assert_equal (1..2), @chunk.
|
10
|
-
assert_equal (1..3), @chunk.
|
9
|
+
assert_equal (1..2), @chunk.original_range
|
10
|
+
assert_equal (1..3), @chunk.modified_range
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_raw_lines_accessible_in_order
|
14
|
-
@chunk.insert_unchanged
|
15
|
-
@chunk.insert_addition
|
16
|
-
@chunk.insert_removal
|
14
|
+
@chunk.send(:insert_unchanged,"foo")
|
15
|
+
@chunk.send(:insert_addition, "bar")
|
16
|
+
@chunk.send(:insert_removal, "baz")
|
17
17
|
assert_equal [" foo","+bar","-baz"], @chunk.raw_lines
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
@chunk.insert_unchanged
|
22
|
-
@chunk.insert_removal
|
23
|
-
@chunk.insert_addition
|
24
|
-
assert_equal %w{foo bar}, @chunk.
|
20
|
+
def test_original_lines
|
21
|
+
@chunk.send(:insert_unchanged,"foo")
|
22
|
+
@chunk.send(:insert_removal,"bar")
|
23
|
+
@chunk.send(:insert_addition,"baz")
|
24
|
+
assert_equal %w{foo bar}, @chunk.original_lines
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
28
|
-
@chunk.insert_unchanged
|
29
|
-
@chunk.insert_removal
|
30
|
-
@chunk.insert_addition
|
31
|
-
assert_equal %w{foo baz}, @chunk.
|
27
|
+
def test_modified_lines
|
28
|
+
@chunk.send(:insert_unchanged,"foo")
|
29
|
+
@chunk.send(:insert_removal,"bar")
|
30
|
+
@chunk.send(:insert_addition,"baz")
|
31
|
+
assert_equal %w{foo baz}, @chunk.modified_lines
|
32
32
|
end
|
33
33
|
end
|
data/test/test_unified_diff.rb
CHANGED
@@ -3,8 +3,8 @@ require 'helper'
|
|
3
3
|
class TestUnifiedDiff < MiniTest::Unit::TestCase
|
4
4
|
def setup
|
5
5
|
diff = <<-DIFF.unindent
|
6
|
-
---
|
7
|
-
+++
|
6
|
+
--- original.txt 2011-05-31 11:14:13.000000000 -0500
|
7
|
+
+++ modified.txt 2011-05-31 11:14:44.000000000 -0500
|
8
8
|
@@ -1,5 +1,5 @@
|
9
9
|
foo
|
10
10
|
bar
|
@@ -20,20 +20,20 @@ class TestUnifiedDiff < MiniTest::Unit::TestCase
|
|
20
20
|
assert_equal UnifiedDiff::Diff, @diff.class
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
24
|
-
assert_equal "
|
25
|
-
assert_equal Time.parse('2011-05-31 11:14:13.000000000 -0500'), @diff.
|
23
|
+
def test_parses_original_information
|
24
|
+
assert_equal "original.txt", @diff.original_file
|
25
|
+
assert_equal Time.parse('2011-05-31 11:14:13.000000000 -0500'), @diff.original_timestamp
|
26
26
|
end
|
27
27
|
|
28
|
-
def
|
29
|
-
assert_equal "
|
30
|
-
assert_equal Time.parse('2011-05-31 11:14:44.000000000 -0500'), @diff.
|
28
|
+
def test_parses_modified_filename
|
29
|
+
assert_equal "modified.txt", @diff.modified_file
|
30
|
+
assert_equal Time.parse('2011-05-31 11:14:44.000000000 -0500'), @diff.modified_timestamp
|
31
31
|
end
|
32
32
|
|
33
33
|
def test_parses_chunk_header
|
34
34
|
@chunk = @diff.chunks.first
|
35
|
-
assert_equal (1..5), @chunk.
|
36
|
-
assert_equal (1..5), @chunk.
|
35
|
+
assert_equal (1..5), @chunk.original_range
|
36
|
+
assert_equal (1..5), @chunk.modified_range
|
37
37
|
end
|
38
38
|
|
39
39
|
def test_parses_unchanged_line
|
@@ -54,8 +54,8 @@ class TestUnifiedDiff < MiniTest::Unit::TestCase
|
|
54
54
|
|
55
55
|
def test_raises_on_invalid_line
|
56
56
|
diff = <<-DIFF.unindent
|
57
|
-
---
|
58
|
-
+++
|
57
|
+
--- original.txt 2011-05-31 11:14:13.000000000 -0500
|
58
|
+
+++ modified.txt 2011-05-31 11:14:44.000000000 -0500
|
59
59
|
@@ -1,1 +1,1 @@
|
60
60
|
&foo
|
61
61
|
DIFF
|
@@ -64,8 +64,8 @@ class TestUnifiedDiff < MiniTest::Unit::TestCase
|
|
64
64
|
|
65
65
|
def test_parses_multiple_chunks
|
66
66
|
diff = <<-DIFF.unindent
|
67
|
-
---
|
68
|
-
+++
|
67
|
+
--- original.txt 2011-05-31 11:14:13.000000000 -0500
|
68
|
+
+++ modified.txt 2011-05-31 11:14:44.000000000 -0500
|
69
69
|
@@ -1,1 +1,1 @@
|
70
70
|
-foo
|
71
71
|
+bar
|
data/unified_diff.gemspec
CHANGED