unified_diff 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem 'minitest'
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.6.0"
7
+ gem 'simplecov'
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.0)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ minitest (2.1.0)
10
+ rake (0.9.0)
11
+ simplecov (0.4.2)
12
+ simplecov-html (~> 0.4.4)
13
+ simplecov-html (0.4.5)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ bundler (~> 1.0.0)
20
+ jeweler (~> 1.6.0)
21
+ minitest
22
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ryan Neufeld
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = unified_diff
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to unified_diff
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Ryan Neufeld. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "unified_diff"
18
+ gem.homepage = "http://github.com/rkneufeld/unified_diff"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Parse unified diffs in style}
21
+ gem.description = %Q{unified_diff parses unified diff contents into easy-to-use Ruby objects}
22
+ gem.email = "ryan@ryanneufeld.ca"
23
+ gem.authors = ["Ryan Neufeld"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "unified_diff #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,30 @@
1
+ module UnifiedDiff
2
+ class Chunk
3
+ attr_reader :old_range,:new_range,:raw_lines
4
+ def initialize(ranges)
5
+ @old_range = ranges[:old]
6
+ @new_range = ranges[:new]
7
+ @raw_lines = []
8
+ end
9
+
10
+ def insert_addition(line)
11
+ @raw_lines << "+#{line}"
12
+ end
13
+
14
+ def insert_removal(line)
15
+ @raw_lines << "-#{line}"
16
+ end
17
+
18
+ def insert_unchanged(line)
19
+ @raw_lines << " #{line}"
20
+ end
21
+
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
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ require 'time'
2
+ module UnifiedDiff
3
+ class Diff
4
+ attr_reader :original, :old_file, :old_timestamp, :new_file, :new_timestamp, :chunks
5
+ class UnifiedDiffException < Exception; end
6
+
7
+ FILE_PATTERN = /(.*)\t'{2}?(.*)'{2}?/
8
+ OLD_FILE_PATTERN = /--- #{FILE_PATTERN}/
9
+ NEW_FILE_PATTERN = /\+\+\+ #{FILE_PATTERN}/
10
+ CHUNK_PATTERN = /@@ -(\d+),(\d+) \+(\d+),(\d+) @@/
11
+ ADDED_PATTERN = /\+(.*)/
12
+ REMOVED_PATTERN = /-(.*)/
13
+ UNCHANGED_PATTERN = / (.*)/
14
+
15
+ def initialize(diff)
16
+ @original = diff
17
+ parse
18
+ end
19
+
20
+ private
21
+
22
+ def parse
23
+ @chunks = []
24
+ @original.each_line do |line|
25
+ case line
26
+ when OLD_FILE_PATTERN
27
+ @old_file, @old_timestamp = $1, Time.parse($2)
28
+ when NEW_FILE_PATTERN
29
+ @new_file, @new_timestamp = $1, Time.parse($2)
30
+ when CHUNK_PATTERN
31
+ @working_chunk = Chunk.new(:old => ($1.to_i..$2.to_i), :new => ($3.to_i..$4.to_i))
32
+ @chunks << @working_chunk
33
+ when ADDED_PATTERN
34
+ @working_chunk.insert_addition($1)
35
+ when REMOVED_PATTERN
36
+ @working_chunk.insert_removal($1)
37
+ when UNCHANGED_PATTERN
38
+ @working_chunk.insert_unchanged($1)
39
+ else
40
+ raise UnifiedDiffException.new("Unknown Line Type for Line:\n#{line}")
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,8 @@
1
+ module UnifiedDiff
2
+ def self.parse(diff)
3
+ UnifiedDiff::Diff.new(diff)
4
+ end
5
+ end
6
+
7
+ require 'unified_diff/diff'
8
+ require 'unified_diff/chunk'
data/test/helper.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ gem 'minitest'
13
+ require 'minitest/autorun'
14
+
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+
18
+ # String extensions: http://stackoverflow.com/questions/3772864/how-do-i-remove-leading-whitespace-chars-from-ruby-heredoc/5638187#5638187
19
+ class String
20
+ def unindent
21
+ gsub /^#{self[/\A\s*/]}/, ''
22
+ end
23
+ end
24
+
25
+ require 'simplecov'
26
+ SimpleCov.start do
27
+ add_filter "/test/"
28
+ end
29
+
30
+ require 'unified_diff'
@@ -0,0 +1,33 @@
1
+ require 'helper'
2
+
3
+ class TestChunk < MiniTest::Unit::TestCase
4
+ def setup
5
+ @chunk = UnifiedDiff::Chunk.new(old: (1..2), new: (1..3))
6
+ end
7
+
8
+ def test_ranges_accessible
9
+ assert_equal (1..2), @chunk.old_range
10
+ assert_equal (1..3), @chunk.new_range
11
+ end
12
+
13
+ def test_raw_lines_accessible_in_order
14
+ @chunk.insert_unchanged("foo")
15
+ @chunk.insert_addition( "bar")
16
+ @chunk.insert_removal( "baz")
17
+ assert_equal [" foo","+bar","-baz"], @chunk.raw_lines
18
+ end
19
+
20
+ def test_old_lines
21
+ @chunk.insert_unchanged("foo")
22
+ @chunk.insert_removal("bar")
23
+ @chunk.insert_addition("baz")
24
+ assert_equal %w{foo bar}, @chunk.old_lines
25
+ end
26
+
27
+ def test_new_lines
28
+ @chunk.insert_unchanged("foo")
29
+ @chunk.insert_removal("bar")
30
+ @chunk.insert_addition("baz")
31
+ assert_equal %w{foo baz}, @chunk.new_lines
32
+ end
33
+ end
@@ -0,0 +1,79 @@
1
+ require 'helper'
2
+
3
+ class TestUnifiedDiff < MiniTest::Unit::TestCase
4
+ def setup
5
+ diff = <<-DIFF.unindent
6
+ --- old.txt 2011-05-31 11:14:13.000000000 -0500
7
+ +++ new.txt 2011-05-31 11:14:44.000000000 -0500
8
+ @@ -1,5 +1,5 @@
9
+ foo
10
+ bar
11
+ -baz
12
+ +Baz
13
+ qux
14
+ quux
15
+ DIFF
16
+ @diff = UnifiedDiff.parse(diff)
17
+ end
18
+
19
+ def test_setup_method_does_something
20
+ assert_equal UnifiedDiff::Diff, @diff.class
21
+ end
22
+
23
+ def test_parses_old_information
24
+ assert_equal "old.txt", @diff.old_file
25
+ assert_equal Time.parse('2011-05-31 11:14:13.000000000 -0500'), @diff.old_timestamp
26
+ end
27
+
28
+ def test_parses_new_filename
29
+ assert_equal "new.txt", @diff.new_file
30
+ assert_equal Time.parse('2011-05-31 11:14:44.000000000 -0500'), @diff.new_timestamp
31
+ end
32
+
33
+ def test_parses_chunk_header
34
+ @chunk = @diff.chunks.first
35
+ assert_equal (1..5), @chunk.old_range
36
+ assert_equal (1..5), @chunk.new_range
37
+ end
38
+
39
+ def test_parses_unchanged_line
40
+ skip "not performing line-type logic yet"
41
+ end
42
+
43
+ def test_parses_removed_lines
44
+ skip "not performing line-type logic yet"
45
+ @chunk = @diff.chunks.first
46
+ #assert_equal [3, 'baz'], @chunk.removed.first
47
+ end
48
+
49
+ def test_parses_added_lines
50
+ skip "not performing line-type logic yet"
51
+ @chunk = @diff.chunks.first
52
+ #assert_equal [3, 'Baz'], @chunk.added.first
53
+ end
54
+
55
+ def test_raises_on_invalid_line
56
+ diff = <<-DIFF.unindent
57
+ --- old.txt 2011-05-31 11:14:13.000000000 -0500
58
+ +++ new.txt 2011-05-31 11:14:44.000000000 -0500
59
+ @@ -1,1 +1,1 @@
60
+ &foo
61
+ DIFF
62
+ assert_raises(UnifiedDiff::Diff::UnifiedDiffException) { UnifiedDiff.parse(diff) }
63
+ end
64
+
65
+ def test_parses_multiple_chunks
66
+ diff = <<-DIFF.unindent
67
+ --- old.txt 2011-05-31 11:14:13.000000000 -0500
68
+ +++ new.txt 2011-05-31 11:14:44.000000000 -0500
69
+ @@ -1,1 +1,1 @@
70
+ -foo
71
+ +bar
72
+ @@ -4,4 +4,4 @@
73
+ -foo
74
+ +bar
75
+ DIFF
76
+ @diff = UnifiedDiff.parse(diff)
77
+ assert_equal 2, @diff.chunks.length
78
+ end
79
+ end
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{unified_diff}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Ryan Neufeld}]
12
+ s.date = %q{2011-06-01}
13
+ s.description = %q{unified_diff parses unified diff contents into easy-to-use Ruby objects}
14
+ s.email = %q{ryan@ryanneufeld.ca}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/unified_diff.rb",
28
+ "lib/unified_diff/chunk.rb",
29
+ "lib/unified_diff/diff.rb",
30
+ "test/helper.rb",
31
+ "test/test_chunk.rb",
32
+ "test/test_unified_diff.rb",
33
+ "unified_diff.gemspec"
34
+ ]
35
+ s.homepage = %q{http://github.com/rkneufeld/unified_diff}
36
+ s.licenses = [%q{MIT}]
37
+ s.require_paths = [%q{lib}]
38
+ s.rubygems_version = %q{1.8.4}
39
+ s.summary = %q{Parse unified diffs in style}
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<minitest>, [">= 0"])
46
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
48
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<minitest>, [">= 0"])
51
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
53
+ s.add_dependency(%q<simplecov>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<minitest>, [">= 0"])
57
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
59
+ s.add_dependency(%q<simplecov>, [">= 0"])
60
+ end
61
+ end
62
+
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unified_diff
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Neufeld
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-01 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: minitest
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.0.0
35
+ type: :development
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.0
46
+ type: :development
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: simplecov
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id004
59
+ description: unified_diff parses unified diff contents into easy-to-use Ruby objects
60
+ email: ryan@ryanneufeld.ca
61
+ executables: []
62
+
63
+ extensions: []
64
+
65
+ extra_rdoc_files:
66
+ - LICENSE.txt
67
+ - README.rdoc
68
+ files:
69
+ - .document
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.rdoc
74
+ - Rakefile
75
+ - VERSION
76
+ - lib/unified_diff.rb
77
+ - lib/unified_diff/chunk.rb
78
+ - lib/unified_diff/diff.rb
79
+ - test/helper.rb
80
+ - test/test_chunk.rb
81
+ - test/test_unified_diff.rb
82
+ - unified_diff.gemspec
83
+ homepage: http://github.com/rkneufeld/unified_diff
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.4
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Parse unified diffs in style
110
+ test_files: []
111
+