diff-lcs 1.3 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Contributing.md +84 -48
- data/History.md +334 -154
- data/Manifest.txt +23 -1
- data/README.rdoc +10 -10
- data/Rakefile +85 -21
- data/bin/htmldiff +7 -4
- data/bin/ldiff +4 -1
- data/lib/diff/lcs/array.rb +1 -1
- data/lib/diff/lcs/backports.rb +9 -0
- data/lib/diff/lcs/block.rb +1 -1
- data/lib/diff/lcs/callbacks.rb +15 -12
- data/lib/diff/lcs/change.rb +30 -37
- data/lib/diff/lcs/htmldiff.rb +17 -16
- data/lib/diff/lcs/hunk.rb +156 -74
- data/lib/diff/lcs/internals.rb +43 -42
- data/lib/diff/lcs/ldiff.rb +46 -42
- data/lib/diff/lcs/string.rb +1 -1
- data/lib/diff/lcs.rb +188 -174
- data/lib/diff-lcs.rb +1 -1
- data/spec/change_spec.rb +31 -7
- data/spec/diff_spec.rb +16 -12
- data/spec/fixtures/aX +1 -0
- data/spec/fixtures/bXaX +1 -0
- data/spec/fixtures/ldiff/output.diff +4 -0
- data/spec/fixtures/ldiff/output.diff-c +7 -0
- data/spec/fixtures/ldiff/output.diff-e +3 -0
- data/spec/fixtures/ldiff/output.diff-f +3 -0
- data/spec/fixtures/ldiff/output.diff-u +5 -0
- data/spec/fixtures/ldiff/output.diff.chef +4 -0
- data/spec/fixtures/ldiff/output.diff.chef-c +15 -0
- data/spec/fixtures/ldiff/output.diff.chef-e +3 -0
- data/spec/fixtures/ldiff/output.diff.chef-f +3 -0
- data/spec/fixtures/ldiff/output.diff.chef-u +9 -0
- data/spec/fixtures/ldiff/output.diff.chef2 +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-c +20 -0
- data/spec/fixtures/ldiff/output.diff.chef2-d +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-e +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-f +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-u +16 -0
- data/spec/fixtures/new-chef +4 -0
- data/spec/fixtures/new-chef2 +17 -0
- data/spec/fixtures/old-chef +4 -0
- data/spec/fixtures/old-chef2 +14 -0
- data/spec/hunk_spec.rb +37 -26
- data/spec/issues_spec.rb +115 -10
- data/spec/lcs_spec.rb +10 -10
- data/spec/ldiff_spec.rb +71 -31
- data/spec/patch_spec.rb +93 -99
- data/spec/sdiff_spec.rb +89 -89
- data/spec/spec_helper.rb +118 -65
- data/spec/traverse_balanced_spec.rb +173 -173
- data/spec/traverse_sequences_spec.rb +29 -31
- metadata +54 -33
- data/autotest/discover.rb +0 -1
data/lib/diff/lcs/ldiff.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
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-
|
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,
|
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(
|
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(
|
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 |
|
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 |
|
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(
|
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 |
|
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 |
|
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
|
-
|
93
|
-
|
94
|
-
data_new = IO.read(file_new)
|
101
|
+
data_old = IO.read(file_old)
|
102
|
+
data_new = IO.read(file_new)
|
95
103
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
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
|
-
|
105
|
-
|
106
|
-
|
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
|
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
|
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
|
153
|
+
next if @lines.positive? and hunk.merge(oldhunk)
|
152
154
|
|
153
|
-
output << oldhunk.diff(@format)
|
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
|
-
|
162
|
+
last = oldhunk.diff(@format, true)
|
163
|
+
last << "\n" if last.respond_to?(:end_with?) && !last.end_with?("\n")
|
160
164
|
|
161
|
-
|
162
|
-
|
163
|
-
|
165
|
+
output << last
|
166
|
+
|
167
|
+
output.reverse_each { |e| real_output << e.diff(:ed_finish) } if @format == :ed
|
164
168
|
|
165
|
-
|
169
|
+
1
|
166
170
|
end
|
167
171
|
end
|
data/lib/diff/lcs/string.rb
CHANGED