Skip to content

Commit befbf8c

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

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

README.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,12 @@ To generate a patch for changes between two revisions (b015e8dd and b015e8dd):
103103
\include::git@path/within/repo/file.rb[revision=b015e8dd,diff=0245ac72]
104104
----
105105

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

lib/asciidoctor-git-include.rb

Lines changed: 2 additions & 1 deletion
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

test/extension_test.rb

Lines changed: 13 additions & 0 deletions
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)