rpatch 0.0.1

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.
@@ -0,0 +1,44 @@
1
+ require 'optparse'
2
+ require 'rpatch/patch'
3
+ require 'rpatch/version'
4
+
5
+ module Rpatch
6
+ class Runner
7
+ class <<self
8
+ def runner
9
+ path = "."
10
+ patches = []
11
+ patch_level = 1
12
+
13
+ OptionParser.new do |opts|
14
+ opts.banner = "Usage: rpatch [options] [originalfile [patchfile]]"
15
+ opts.on("-p", "--strip num", "Patch level") {|v| patch_level = v.to_i}
16
+ opts.on("-v", "--version", "Show version") do
17
+ puts "Version #{Rpatch::VERSION}"
18
+ exit 0
19
+ end
20
+ opts.on_tail("-h", "--help", "Show this message") do
21
+ puts opts
22
+ exit 0
23
+ end
24
+ end.parse!
25
+
26
+ case ARGV.size
27
+ when 0
28
+ patches << STDIN
29
+ when 1
30
+ path = ARGV.shift
31
+ patches << STDIN
32
+ else
33
+ path = ARGV.shift
34
+ until ARGV.empty? do
35
+ patches << ARGV.shift
36
+ end
37
+ end
38
+
39
+ result = Patch::apply(path, patches, patch_level)
40
+ exit 1 unless result
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Rpatch
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,234 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+
4
+ require 'spec_helper'
5
+
6
+ describe Rpatch::PatchEntry do
7
+
8
+ it "filename and patch level (1)" do
9
+ old = "a/foo/bar/src/file.c"
10
+ new = "b/foo/bar/src/file.c"
11
+ entry = Rpatch::PatchEntry.new(old, new, 0)
12
+ entry.oldfile.should be == "a/foo/bar/src/file.c"
13
+ entry.newfile.should be == "b/foo/bar/src/file.c"
14
+ entry = Rpatch::PatchEntry.new(old, new, 1)
15
+ entry.oldfile.should be == "foo/bar/src/file.c"
16
+ entry.newfile.should be == "foo/bar/src/file.c"
17
+ entry = Rpatch::PatchEntry.new(old, new, 3)
18
+ entry.oldfile.should be == "src/file.c"
19
+ entry.newfile.should be == "src/file.c"
20
+ end
21
+
22
+ it "filename and patch level (2)" do
23
+ old = "/dev/null"
24
+ new = "b/foo/bar/src/file.c"
25
+ entry = Rpatch::PatchEntry.new(old, new, 1)
26
+ entry.oldfile.should eq "/dev/null"
27
+ entry.newfile.should eq "foo/bar/src/file.c"
28
+ old = "a/foo/bar/src/file.c"
29
+ new = "/dev/null"
30
+ entry = Rpatch::PatchEntry.new(old, new, 1)
31
+ entry.oldfile.should eq "foo/bar/src/file.c"
32
+ entry.newfile.should eq "/dev/null"
33
+ end
34
+
35
+ it "patch on file (1)" do
36
+ before = <<-EOF
37
+ When I hack files using GNU patch,
38
+ sometimes fail because of the change of upstream files.
39
+ So comes rpatch.
40
+
41
+ Happy hacking.
42
+ EOF
43
+
44
+ diff = <<-EOF
45
+ @@ add copyright
46
+ +/*
47
+ + * Copyright (c) 2013 Jiang Xin
48
+ + */
49
+ +
50
+ When I hack files using GNU patch,
51
+ sometimes fail because of the change of upstream files.
52
+ @@ add notes
53
+ +If patch can ignore blank lines, support regex patterns
54
+ +in patch, it will be nice.
55
+ So comes rpatch.
56
+
57
+ @@ add signature
58
+ Happy hacking.
59
+ +--
60
+ +jiangxin
61
+ EOF
62
+
63
+ after = <<-EOF
64
+ /*
65
+ * Copyright (c) 2013 Jiang Xin
66
+ */
67
+
68
+ When I hack files using GNU patch,
69
+ sometimes fail because of the change of upstream files.
70
+ If patch can ignore blank lines, support regex patterns
71
+ in patch, it will be nice.
72
+ So comes rpatch.
73
+
74
+ Happy hacking.
75
+ --
76
+ jiangxin
77
+ EOF
78
+
79
+ old = "a/foo/bar/src/file.c"
80
+ new = "b/foo/bar/src/file.c"
81
+ entry = Rpatch::PatchEntry.new(old, new, 1)
82
+ diff.lines.each do |line|
83
+ entry.feed_line line
84
+ end
85
+ entry.patch_on_message(before).should == after
86
+ end
87
+
88
+ it "patch on file (2)" do
89
+ before = <<-EOF
90
+ Subject: about rpatch
91
+
92
+ When I hack files using GNU patch,
93
+ sometimes fail because of the change of upstream files.
94
+ So comes rpatch.
95
+
96
+ Happy hacking.
97
+ EOF
98
+
99
+ diff = <<-EOF
100
+ @@ add copyright
101
+ +/*
102
+ + * Copyright (c) 2013 Jiang Xin
103
+ + */
104
+ +
105
+ When I hack files using GNU patch,
106
+ sometimes fail because of the change of upstream files.
107
+ @@ add notes
108
+ +If patch can ignore blank lines, support regex patterns
109
+ +in patch, it will be nice.
110
+ So comes rpatch.
111
+
112
+ @@ add signature
113
+ Happy hacking.
114
+ +--
115
+ +jiangxin
116
+ EOF
117
+
118
+ after = <<-EOF
119
+ Subject: about rpatch
120
+
121
+ /*
122
+ * Copyright (c) 2013 Jiang Xin
123
+ */
124
+
125
+ When I hack files using GNU patch,
126
+ sometimes fail because of the change of upstream files.
127
+ If patch can ignore blank lines, support regex patterns
128
+ in patch, it will be nice.
129
+ So comes rpatch.
130
+
131
+ Happy hacking.
132
+ --
133
+ jiangxin
134
+ EOF
135
+
136
+ old = "a/foo/bar/src/file.c"
137
+ new = "b/foo/bar/src/file.c"
138
+ entry = Rpatch::PatchEntry.new(old, new, 1)
139
+ diff.lines.each do |line|
140
+ entry.feed_line line
141
+ end
142
+ entry.patch_on_message(before).should == after
143
+ end
144
+
145
+ it "patch on file (3)" do
146
+ before = ''
147
+
148
+ diff = <<-EOF
149
+ @@ initial
150
+ +/*
151
+ + * Copyright (c) 2013 Jiang Xin
152
+ + */
153
+ +
154
+ +When I hack files using GNU patch,
155
+ +sometimes fail because of the change of upstream files.
156
+ +If patch can ignore blank lines, support regex patterns
157
+ +in patch, it will be nice.
158
+ +So comes rpatch.
159
+ +
160
+ +Happy hacking.
161
+ +--
162
+ +jiangxin
163
+ EOF
164
+
165
+ after = <<-EOF
166
+ /*
167
+ * Copyright (c) 2013 Jiang Xin
168
+ */
169
+
170
+ When I hack files using GNU patch,
171
+ sometimes fail because of the change of upstream files.
172
+ If patch can ignore blank lines, support regex patterns
173
+ in patch, it will be nice.
174
+ So comes rpatch.
175
+
176
+ Happy hacking.
177
+ --
178
+ jiangxin
179
+ EOF
180
+
181
+ old = "a/foo/bar/src/file.c"
182
+ new = "b/foo/bar/src/file.c"
183
+ entry = Rpatch::PatchEntry.new(old, new, 1)
184
+ diff.lines.each do |line|
185
+ entry.feed_line line
186
+ end
187
+ entry.patch_on_message(before).should == after
188
+ end
189
+
190
+ it "patch on file (4)" do
191
+ before = <<-EOF
192
+ /*
193
+ * Copyright (c) 2013 Jiang Xin
194
+ */
195
+
196
+ When I hack files using GNU patch,
197
+ sometimes fail because of the change of upstream files.
198
+ If patch can ignore blank lines, support regex patterns
199
+ in patch, it will be nice.
200
+ So comes rpatch.
201
+
202
+ Happy hacking.
203
+ --
204
+ jiangxin
205
+ EOF
206
+
207
+ diff = <<-EOF
208
+ @@ remove all
209
+ -/*
210
+ - * Copyright (c) 2013 Jiang Xin
211
+ - */
212
+ -
213
+ -When I hack files using GNU patch,
214
+ -sometimes fail because of the change of upstream files.
215
+ -If patch can ignore blank lines, support regex patterns
216
+ -in patch, it will be nice.
217
+ -So comes rpatch.
218
+ -
219
+ -Happy hacking.
220
+ ---
221
+ -jiangxin
222
+ EOF
223
+
224
+ after = ''
225
+
226
+ old = "a/foo/bar/src/file.c"
227
+ new = "b/foo/bar/src/file.c"
228
+ entry = Rpatch::PatchEntry.new(old, new, 1)
229
+ diff.lines.each do |line|
230
+ entry.feed_line line
231
+ end
232
+ entry.patch_on_message(before).should == after
233
+ end
234
+ end
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+
4
+ require 'spec_helper'
5
+ require 'stringio'
6
+
7
+ describe Rpatch::Patch do
8
+ let(:diff) do
9
+ <<-EOF
10
+ diff -ru before/readme.txt after/readme.txt
11
+ --- a/readme.txt 2013-11-03 22:17:02.000000000 +0800
12
+ +++ b/readme.txt 2013-11-03 21:10:46.000000000 +0800
13
+ @@ remove two leading lines
14
+ -I have a script to patch text files, it's more like
15
+ RE:-.* Why not (patch|GNU patch)?
16
+ When I hack files using GNU patch,
17
+ RE: sometimes fail because .*
18
+ @@ add copyright
19
+ +/*
20
+ + * Copyright (c) 2013 Jiang Xin
21
+ + */
22
+ +
23
+ When I hack files using GNU patch,
24
+ RE: sometimes fail because .*
25
+ @@ add notes
26
+ +If patch can ignore blank lines, support regex patterns
27
+ +in patch, it will be nice.
28
+ So comes rpatch.
29
+
30
+ @@ add signature
31
+ Happy hacking.
32
+ +--
33
+ +jiangxin
34
+ EOF
35
+ end
36
+
37
+ it "patch on file" do
38
+ before = <<-EOF
39
+ I have a script to patch text files, it's more like
40
+ "grep" and "seed -s". Why not GNU patch?
41
+ When I hack files using GNU patch,
42
+ sometimes fail because of the change of upstream files.
43
+ So comes rpatch.
44
+
45
+ Happy hacking.
46
+ EOF
47
+
48
+ after = <<-EOF
49
+ /*
50
+ * Copyright (c) 2013 Jiang Xin
51
+ */
52
+
53
+ When I hack files using GNU patch,
54
+ sometimes fail because of the change of upstream files.
55
+ If patch can ignore blank lines, support regex patterns
56
+ in patch, it will be nice.
57
+ So comes rpatch.
58
+
59
+ Happy hacking.
60
+ --
61
+ jiangxin
62
+ EOF
63
+
64
+ inputfile = StringIO.new(before, "r")
65
+ patchfile = StringIO.new(diff, "r")
66
+ output = ''
67
+ outputfile = StringIO.new(output, "w")
68
+ patch = Rpatch::Patch.new(patchfile, 1)
69
+ patch.apply_to(inputfile, outputfile)
70
+ output.should == after
71
+ end
72
+
73
+ end
@@ -0,0 +1,267 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+
4
+ require 'spec_helper'
5
+ require 'stringio'
6
+
7
+ describe Rpatch::Patch do
8
+ let(:diff) do
9
+ <<-EOF
10
+ diff -ru before/readme.txt after/readme.txt
11
+ --- a/readme.txt 2013-11-03 22:17:02.000000000 +0800
12
+ +++ b/readme.txt 2013-11-03 21:10:46.000000000 +0800
13
+ @@ add copyright
14
+ +/*
15
+ + * Copyright (c) 2013 Jiang Xin
16
+ + */
17
+ +
18
+ When I hack files using GNU patch,
19
+ sometimes fail because of the change of upstream files.
20
+ @@ add notes
21
+ +If patch can ignore blank lines, support regex patterns
22
+ +in patch, it will be nice.
23
+ So comes rpatch.
24
+
25
+ @@ add signature
26
+ Happy hacking.
27
+ +--
28
+ +jiangxin
29
+ EOF
30
+ end
31
+
32
+ it "patch on file (1): normal patch" do
33
+ before = <<-EOF
34
+ When I hack files using GNU patch,
35
+ sometimes fail because of the change of upstream files.
36
+ So comes rpatch.
37
+
38
+ Happy hacking.
39
+ EOF
40
+
41
+ after = <<-EOF
42
+ /*
43
+ * Copyright (c) 2013 Jiang Xin
44
+ */
45
+
46
+ When I hack files using GNU patch,
47
+ sometimes fail because of the change of upstream files.
48
+ If patch can ignore blank lines, support regex patterns
49
+ in patch, it will be nice.
50
+ So comes rpatch.
51
+
52
+ Happy hacking.
53
+ --
54
+ jiangxin
55
+ EOF
56
+
57
+ inputfile = StringIO.new(before, "r")
58
+ patchfile = StringIO.new(diff, "r")
59
+ output = ''
60
+ outputfile = StringIO.new(output, "w")
61
+ patch = Rpatch::Patch.new(patchfile, 1)
62
+ patch.apply_to(inputfile, outputfile)
63
+ output.should == after
64
+ end
65
+
66
+ it "patch on file (2): partial patched" do
67
+ before = <<-EOF
68
+ When I hack files using GNU patch,
69
+ sometimes fail because of the change of upstream files.
70
+ If patch can ignore blank lines, support regex patterns
71
+ in patch, it will be nice.
72
+ So comes rpatch.
73
+
74
+ Happy hacking.
75
+ EOF
76
+
77
+ after = <<-EOF
78
+ /*
79
+ * Copyright (c) 2013 Jiang Xin
80
+ */
81
+
82
+ When I hack files using GNU patch,
83
+ sometimes fail because of the change of upstream files.
84
+ If patch can ignore blank lines, support regex patterns
85
+ in patch, it will be nice.
86
+ So comes rpatch.
87
+
88
+ Happy hacking.
89
+ --
90
+ jiangxin
91
+ EOF
92
+
93
+ inputfile = StringIO.new(before, "r")
94
+ patchfile = StringIO.new(diff, "r")
95
+ output = ''
96
+ outputfile = StringIO.new(output, "w")
97
+ patch = Rpatch::Patch.new(patchfile, 1)
98
+ patch.apply_to(inputfile, outputfile)
99
+ output.should == after
100
+ end
101
+
102
+ it "patch on file (3): already patched" do
103
+ after = <<-EOF
104
+ /*
105
+ * Copyright (c) 2013 Jiang Xin
106
+ */
107
+
108
+ When I hack files using GNU patch,
109
+ sometimes fail because of the change of upstream files.
110
+ If patch can ignore blank lines, support regex patterns
111
+ in patch, it will be nice.
112
+ So comes rpatch.
113
+
114
+ Happy hacking.
115
+ --
116
+ jiangxin
117
+ EOF
118
+
119
+ before = after.dup
120
+
121
+ inputfile = StringIO.new(before, "r")
122
+ patchfile = StringIO.new(diff, "r")
123
+ output = ''
124
+ outputfile = StringIO.new(output, "w")
125
+ patch = Rpatch::Patch.new(patchfile, 1)
126
+ patch.apply_to(inputfile, outputfile)
127
+ output.should == after
128
+ end
129
+
130
+ it "patch on file (4): blank lines and white spaces" do
131
+ before = <<-EOF
132
+ When I hack files using GNU patch,
133
+
134
+ sometimes fail because of the change of upstream files.
135
+
136
+ So comes rpatch.
137
+
138
+ Happy hacking.
139
+
140
+ EOF
141
+
142
+ after = <<-EOF
143
+ /*
144
+ * Copyright (c) 2013 Jiang Xin
145
+ */
146
+
147
+ When I hack files using GNU patch,
148
+
149
+ sometimes fail because of the change of upstream files.
150
+
151
+ If patch can ignore blank lines, support regex patterns
152
+ in patch, it will be nice.
153
+ So comes rpatch.
154
+
155
+ Happy hacking.
156
+ --
157
+ jiangxin
158
+
159
+ EOF
160
+
161
+ inputfile = StringIO.new(before, "r")
162
+ patchfile = StringIO.new(diff, "r")
163
+ output = ''
164
+ outputfile = StringIO.new(output, "w")
165
+ patch = Rpatch::Patch.new(patchfile, 1)
166
+ patch.apply_to(inputfile, outputfile)
167
+ output.should == after
168
+ end
169
+
170
+ it "patch on file (5): patch on blank files" do
171
+ before = ''
172
+
173
+ diff = <<-EOF
174
+ diff -ru before/readme.txt after/readme.txt
175
+ --- a/readme.txt 2013-11-03 22:17:02.000000000 +0800
176
+ +++ b/readme.txt 2013-11-03 21:10:46.000000000 +0800
177
+ @@ initial
178
+ +hello
179
+ +world
180
+ EOF
181
+
182
+ after = <<-EOF
183
+ hello
184
+ world
185
+ EOF
186
+
187
+
188
+ inputfile = StringIO.new(before, "r")
189
+ patchfile = StringIO.new(diff, "r")
190
+ output = ''
191
+ outputfile = StringIO.new(output, "w")
192
+ patch = Rpatch::Patch.new(patchfile, 1)
193
+ patch.apply_to(inputfile, outputfile)
194
+ output.should == after
195
+ end
196
+
197
+ it "bad patch header, so nothing patched (1)" do
198
+ before = ''
199
+
200
+ diff = <<-EOF
201
+ diff -ru before/readme.txt after/readme.txt
202
+ @@ initial
203
+ +hello
204
+ +world
205
+ EOF
206
+
207
+ after = <<-EOF
208
+ EOF
209
+
210
+
211
+ inputfile = StringIO.new(before, "r")
212
+ patchfile = StringIO.new(diff, "r")
213
+ output = ''
214
+ outputfile = StringIO.new(output, "w")
215
+ patch = Rpatch::Patch.new(patchfile, 1)
216
+ patch.apply_to(inputfile, outputfile)
217
+ output.should == after
218
+ end
219
+
220
+ it "patch failed (1): patch format error" do
221
+ diff = <<-EOF
222
+ diff -ru before/readme.txt after/readme.txt
223
+ --- a/readme.txt 2013-11-03 22:17:02.000000000 +0800
224
+ +++ b/readme.txt 2013-11-03 21:10:46.000000000 +0800
225
+ @@ initial
226
+ lines of a patch must start with
227
+ space
228
+ +plus
229
+ -minus
230
+ RE: regexp match
231
+ RE:-regexp match
232
+ others are bad.
233
+ BAD PATCH SYNTAX.
234
+ EOF
235
+
236
+ patchfile = StringIO.new(diff, "r")
237
+ expect {
238
+ Rpatch::Patch.new(patchfile, 1)
239
+ }.to raise_exception Rpatch::PatchFormatError, /BAD PATCH SYNTAX./
240
+
241
+ end
242
+
243
+ it "patch failed (2): apply_to return false" do
244
+ before = <<-EOF
245
+ bad
246
+ EOF
247
+
248
+ diff = <<-EOF
249
+ diff -ru before/readme.txt after/readme.txt
250
+ --- a/readme.txt 2013-11-03 22:17:02.000000000 +0800
251
+ +++ b/readme.txt 2013-11-03 21:10:46.000000000 +0800
252
+ @@ initial
253
+ hello
254
+ +world
255
+ EOF
256
+
257
+ inputfile = StringIO.new(before, "r")
258
+ patchfile = StringIO.new(diff, "r")
259
+ output = ''
260
+ outputfile = StringIO.new(output, "w")
261
+
262
+ patch = Rpatch::Patch.new(patchfile, 1)
263
+ patch.apply_to(inputfile, outputfile).should be == false
264
+ output.should be == before
265
+ end
266
+
267
+ end