Skip to content

Commit 1b2ccd5

Browse files
committed
feat(diff): give capability to ignore whitespaces and caret returns in a diff
Solves #2
1 parent e80ca3c commit 1b2ccd5

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Diff for: README.adoc

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Then, inside your AsciiDoc file, use an `include` statement like normal, pointin
3939
* `revision` - repository revision to use (default: `HEAD`)
4040
* `lines` - specify the lines to include (i.e. `lines=2..5;10;12`)
4141
* `diff` - include a patch for the given `revision`, or between two revisions (see examples)
42+
* `ignorewhitespaces` - whether to ignore whitespaces and newlines changes in an included patch
4243

4344
// tag::examples[]
4445

@@ -103,4 +104,12 @@ To generate a patch for changes between two revisions (b015e8dd and b015e8dd):
103104
\include::git@path/within/repo/file.rb[revision=b015e8dd,diff=0245ac72]
104105
----
105106

107+
=== Including a patch ignoring whitespaces
108+
109+
To generate a patch for changes introduced in a specific revision (b015e8dd) but ignoring the changes related to whitespaces and caret line return:
110+
111+
----
112+
\include::git@path/within/repo/file.rb[revision=b015e8dd,diff,ignorewhitespaces]
113+
----
114+
106115
// end::examples[]

Diff for: lib/asciidoctor-git-include.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ def process doc, reader, target, attributes
1616
lines = attributes.fetch('lines', '')
1717
as_diff = attributes.value?('diff') || attributes.key?('diff')
1818
diff_revision = attributes.fetch('diff', "#{revision}~1")
19+
ignore_whitespaces_option = attributes.value?('ignorewhitespaces') || attributes.key?('ignorewhitespaces') ? '--ignore-cr-at-eol --ignore-space-at-eol -w -b --ignore-blank-lines' : ''
1920

2021
cmd = %(git -C #{repository} show #{revision}:#{target})
2122
if (as_diff)
22-
cmd = %(git -C #{repository} diff #{diff_revision}:#{target} #{revision}:#{target})
23+
cmd = %(git -C #{repository} diff #{ignore_whitespaces_option} #{diff_revision}:#{target} #{revision}:#{target})
2324
end
2425
content = %x(#{cmd})
2526

Diff for: test/extension_test.rb

+13
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ class ExtensionTest < Minitest::Test
105105
assert_match(/-messages = \["Hello"\]/, output)
106106
assert_match(/\+messages = \["Hello", "World", "!!!"\]/, output)
107107
end
108+
109+
test 'it includes a diff ignoring whitespaces and caret returns' do
110+
input = <<-EOS
111+
include::git@test/fixtures/lots_of_whitespaces.adoc[revision=e80ca3c,diff=2c2f9a9,ignorewhitespaces]
112+
EOS
113+
114+
output = render_embedded_string input
115+
116+
assert_match(/diff --git a\/test\/fixtures\/lots_of_whitespaces.adoc b\/test\/fixtures\/lots_of_whitespaces.adoc/, output)
117+
assert_match(/-Another line with more content/, output)
118+
assert_match(/\+Another line with more content that has changed/, output)
119+
refute_match(/-Some line with some content/, output)
120+
end
108121
end
109122

110123
def given_file_committed_to_fresh_repo(file_name, content)

0 commit comments

Comments
 (0)