diff-lcs 1.3 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +5 -5
  2. data/Contributing.md +84 -48
  3. data/History.md +334 -154
  4. data/Manifest.txt +23 -1
  5. data/README.rdoc +10 -10
  6. data/Rakefile +85 -21
  7. data/bin/htmldiff +7 -4
  8. data/bin/ldiff +4 -1
  9. data/lib/diff/lcs/array.rb +1 -1
  10. data/lib/diff/lcs/backports.rb +9 -0
  11. data/lib/diff/lcs/block.rb +1 -1
  12. data/lib/diff/lcs/callbacks.rb +15 -12
  13. data/lib/diff/lcs/change.rb +30 -37
  14. data/lib/diff/lcs/htmldiff.rb +17 -16
  15. data/lib/diff/lcs/hunk.rb +156 -74
  16. data/lib/diff/lcs/internals.rb +43 -42
  17. data/lib/diff/lcs/ldiff.rb +46 -42
  18. data/lib/diff/lcs/string.rb +1 -1
  19. data/lib/diff/lcs.rb +188 -174
  20. data/lib/diff-lcs.rb +1 -1
  21. data/spec/change_spec.rb +31 -7
  22. data/spec/diff_spec.rb +16 -12
  23. data/spec/fixtures/aX +1 -0
  24. data/spec/fixtures/bXaX +1 -0
  25. data/spec/fixtures/ldiff/output.diff +4 -0
  26. data/spec/fixtures/ldiff/output.diff-c +7 -0
  27. data/spec/fixtures/ldiff/output.diff-e +3 -0
  28. data/spec/fixtures/ldiff/output.diff-f +3 -0
  29. data/spec/fixtures/ldiff/output.diff-u +5 -0
  30. data/spec/fixtures/ldiff/output.diff.chef +4 -0
  31. data/spec/fixtures/ldiff/output.diff.chef-c +15 -0
  32. data/spec/fixtures/ldiff/output.diff.chef-e +3 -0
  33. data/spec/fixtures/ldiff/output.diff.chef-f +3 -0
  34. data/spec/fixtures/ldiff/output.diff.chef-u +9 -0
  35. data/spec/fixtures/ldiff/output.diff.chef2 +7 -0
  36. data/spec/fixtures/ldiff/output.diff.chef2-c +20 -0
  37. data/spec/fixtures/ldiff/output.diff.chef2-d +7 -0
  38. data/spec/fixtures/ldiff/output.diff.chef2-e +7 -0
  39. data/spec/fixtures/ldiff/output.diff.chef2-f +7 -0
  40. data/spec/fixtures/ldiff/output.diff.chef2-u +16 -0
  41. data/spec/fixtures/new-chef +4 -0
  42. data/spec/fixtures/new-chef2 +17 -0
  43. data/spec/fixtures/old-chef +4 -0
  44. data/spec/fixtures/old-chef2 +14 -0
  45. data/spec/hunk_spec.rb +37 -26
  46. data/spec/issues_spec.rb +115 -10
  47. data/spec/lcs_spec.rb +10 -10
  48. data/spec/ldiff_spec.rb +71 -31
  49. data/spec/patch_spec.rb +93 -99
  50. data/spec/sdiff_spec.rb +89 -89
  51. data/spec/spec_helper.rb +118 -65
  52. data/spec/traverse_balanced_spec.rb +173 -173
  53. data/spec/traverse_sequences_spec.rb +29 -31
  54. metadata +54 -33
  55. data/autotest/discover.rb +0 -1
@@ -1,4 +1,4 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'optparse'
4
4
  require 'ostruct'
@@ -7,7 +7,7 @@ require 'diff/lcs/hunk'
7
7
  module Diff::LCS::Ldiff #:nodoc:
8
8
  BANNER = <<-COPYRIGHT
9
9
  ldiff #{Diff::LCS::VERSION}
10
- Copyright 2004-2014 Austin Ziegler
10
+ Copyright 2004-2019 Austin Ziegler
11
11
 
12
12
  Part of Diff::LCS.
13
13
  https://github.com/halostatue/diff-lcs
@@ -15,7 +15,7 @@ ldiff #{Diff::LCS::VERSION}
15
15
  This program is free software. It may be redistributed and/or modified under
16
16
  the terms of the GPL version 2 (or later), the Perl Artistic licence, or the
17
17
  MIT licence.
18
- COPYRIGHT
18
+ COPYRIGHT
19
19
  end
20
20
 
21
21
  class << Diff::LCS::Ldiff
@@ -23,33 +23,42 @@ class << Diff::LCS::Ldiff
23
23
  attr_reader :file_old, :file_new #:nodoc:
24
24
  attr_reader :data_old, :data_new #:nodoc:
25
25
 
26
- def run(args, input = $stdin, output = $stdout, error = $stderr) #:nodoc:
26
+ def run(args, _input = $stdin, output = $stdout, error = $stderr) #:nodoc:
27
27
  @binary = nil
28
28
 
29
29
  args.options do |o|
30
30
  o.banner = "Usage: #{File.basename($0)} [options] oldfile newfile"
31
- o.separator ""
32
- o.on('-c', '-C', '--context [LINES]', Numeric, 'Displays a context diff with LINES lines', 'of context. Default 3 lines.') do |ctx|
31
+ o.separator ''
32
+ o.on(
33
+ '-c', '-C', '--context [LINES]', Integer,
34
+ 'Displays a context diff with LINES lines', 'of context. Default 3 lines.'
35
+ ) do |ctx|
33
36
  @format = :context
34
37
  @lines = ctx || 3
35
38
  end
36
- o.on('-u', '-U', '--unified [LINES]', Numeric, 'Displays a unified diff with LINES lines', 'of context. Default 3 lines.') do |ctx|
39
+ o.on(
40
+ '-u', '-U', '--unified [LINES]', Integer,
41
+ 'Displays a unified diff with LINES lines', 'of context. Default 3 lines.'
42
+ ) do |ctx|
37
43
  @format = :unified
38
44
  @lines = ctx || 3
39
45
  end
40
- o.on('-e', 'Creates an \'ed\' script to change', 'oldfile to newfile.') do |ctx|
46
+ o.on('-e', 'Creates an \'ed\' script to change', 'oldfile to newfile.') do |_ctx|
41
47
  @format = :ed
42
48
  end
43
- o.on('-f', 'Creates an \'ed\' script to change', 'oldfile to newfile in reverse order.') do |ctx|
49
+ o.on('-f', 'Creates an \'ed\' script to change', 'oldfile to newfile in reverse order.') do |_ctx|
44
50
  @format = :reverse_ed
45
51
  end
46
- o.on('-a', '--text', 'Treat the files as text and compare them', 'line-by-line, even if they do not seem', 'to be text.') do |txt|
52
+ o.on(
53
+ '-a', '--text',
54
+ 'Treat the files as text and compare them', 'line-by-line, even if they do not seem', 'to be text.'
55
+ ) do |_txt|
47
56
  @binary = false
48
57
  end
49
- o.on('--binary', 'Treats the files as binary.') do |bin|
58
+ o.on('--binary', 'Treats the files as binary.') do |_bin|
50
59
  @binary = true
51
60
  end
52
- o.on('-q', '--brief', 'Report only whether or not the files', 'differ, not the details.') do |ctx|
61
+ o.on('-q', '--brief', 'Report only whether or not the files', 'differ, not the details.') do |_ctx|
53
62
  @format = :report
54
63
  end
55
64
  o.on_tail('--help', 'Shows this text.') do
@@ -60,7 +69,7 @@ class << Diff::LCS::Ldiff
60
69
  error << Diff::LCS::Ldiff::BANNER
61
70
  return 0
62
71
  end
63
- o.on_tail ""
72
+ o.on_tail ''
64
73
  o.on_tail 'By default, runs produces an "old-style" diff, with output like UNIX diff.'
65
74
  o.parse!
66
75
  end
@@ -89,25 +98,19 @@ class << Diff::LCS::Ldiff
89
98
  # items we've read from each file will differ by FLD (could be 0).
90
99
  file_length_difference = 0
91
100
 
92
- if @binary.nil? or @binary
93
- data_old = IO.read(file_old)
94
- data_new = IO.read(file_new)
101
+ data_old = IO.read(file_old)
102
+ data_new = IO.read(file_new)
95
103
 
96
- # Test binary status
97
- if @binary.nil?
98
- old_txt = data_old[0, 4096].scan(/\0/).empty?
99
- new_txt = data_new[0, 4096].scan(/\0/).empty?
100
- @binary = (not old_txt) or (not new_txt)
101
- old_txt = new_txt = nil
102
- end
104
+ # Test binary status
105
+ if @binary.nil?
106
+ old_txt = data_old[0, 4096].scan(/\0/).empty?
107
+ new_txt = data_new[0, 4096].scan(/\0/).empty?
108
+ @binary = !old_txt || !new_txt
109
+ end
103
110
 
104
- unless @binary
105
- data_old = data_old.split($/).map { |e| e.chomp }
106
- data_new = data_new.split($/).map { |e| e.chomp }
107
- end
108
- else
109
- data_old = IO.readlines(file_old).map { |e| e.chomp }
110
- data_new = IO.readlines(file_new).map { |e| e.chomp }
111
+ unless @binary
112
+ data_old = data_old.lines.to_a
113
+ data_new = data_new.lines.to_a
111
114
  end
112
115
 
113
116
  # diff yields lots of pieces, each of which is basically a Block object
@@ -126,9 +129,9 @@ class << Diff::LCS::Ldiff
126
129
  end
127
130
 
128
131
  if (@format == :unified) or (@format == :context)
129
- ft = File.stat(file_old).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z')
132
+ ft = File.stat(file_old).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.000000000 %z')
130
133
  output << "#{char_old} #{file_old}\t#{ft}\n"
131
- ft = File.stat(file_new).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z')
134
+ ft = File.stat(file_new).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.000000000 %z')
132
135
  output << "#{char_new} #{file_new}\t#{ft}\n"
133
136
  end
134
137
 
@@ -142,26 +145,27 @@ class << Diff::LCS::Ldiff
142
145
  end
143
146
 
144
147
  diffs.each do |piece|
145
- begin
146
- hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, @lines,
147
- file_length_difference)
148
+ begin # rubocop:disable Style/RedundantBegin
149
+ hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, @lines, file_length_difference)
148
150
  file_length_difference = hunk.file_length_difference
149
151
 
150
152
  next unless oldhunk
151
- next if (@lines > 0) and hunk.merge(oldhunk)
153
+ next if @lines.positive? and hunk.merge(oldhunk)
152
154
 
153
- output << oldhunk.diff(@format) << "\n"
155
+ output << oldhunk.diff(@format)
156
+ output << "\n" if @format == :unified
154
157
  ensure
155
158
  oldhunk = hunk
156
159
  end
157
160
  end
158
161
 
159
- output << oldhunk.diff(@format) << "\n"
162
+ last = oldhunk.diff(@format, true)
163
+ last << "\n" if last.respond_to?(:end_with?) && !last.end_with?("\n")
160
164
 
161
- if @format == :ed
162
- output.reverse_each { |e| real_output << e.diff(:ed_finish) }
163
- end
165
+ output << last
166
+
167
+ output.reverse_each { |e| real_output << e.diff(:ed_finish) } if @format == :ed
164
168
 
165
- return 1
169
+ 1
166
170
  end
167
171
  end
@@ -1,4 +1,4 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  class String
4
4
  include Diff::LCS