Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Then, inside your AsciiDoc file, use an `include` statement like normal, pointin
* `revision` - repository revision to use (default: `HEAD`)
* `lines` - specify the lines to include (i.e. `lines=2..5;10;12`)
* `diff` - include a patch for the given `revision`, or between two revisions (see examples)
* `difftarget` - tell what the path of the target was before the changes (in case the target file was renamed)
* `ignorewhitespaces` - whether to ignore whitespaces and newlines changes in an included patch

// tag::examples[]
Expand Down Expand Up @@ -104,6 +105,13 @@ To generate a patch for changes between two revisions (b015e8dd and b015e8dd):
\include::git@path/within/repo/file.rb[revision=b015e8dd,diff=0245ac72]
----

=== Including a patch of a file that has been renamed

To generate a patch for changes between two revisions (b015e8dd and b015e8dd) where a file has been renamed:

----
\include::git@path/within/repo/file.rb[revision=b015e8dd,diff=0245ac72,difftarget=path/within/repo/previous_file.rb]

=== Including a patch ignoring whitespaces

To generate a patch for changes introduced in a specific revision (b015e8dd) but ignoring the changes related to whitespaces and caret line return:
Expand Down
3 changes: 2 additions & 1 deletion lib/asciidoctor-git-include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ def process doc, reader, target, attributes
lines = attributes.fetch('lines', '')
as_diff = attributes.value?('diff') || attributes.key?('diff')
diff_revision = attributes.fetch('diff', "#{revision}~1")
target_before = attributes.fetch('difftarget', target)
ignore_whitespaces_option = attributes.value?('ignorewhitespaces') || attributes.key?('ignorewhitespaces') ? '--ignore-cr-at-eol --ignore-space-at-eol -w -b --ignore-blank-lines' : ''

cmd = %(git -C #{repository} show #{revision}:#{target})
if (as_diff)
cmd = %(git -C #{repository} diff #{ignore_whitespaces_option} #{diff_revision}:#{target} #{revision}:#{target})
cmd = %(git -C #{repository} diff #{ignore_whitespaces_option} #{diff_revision}:#{target_before} #{revision}:#{target})
end
content = %x(#{cmd})

Expand Down
12 changes: 12 additions & 0 deletions test/extension_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ class ExtensionTest < Minitest::Test
assert_match(/\+messages = \["Hello", "World", "!!!"\]/, output)
end

test 'it includes a diff for specific revisions of a file that is renamed' do
input = <<-EOS
include::git@test/fixtures/file-after-rename.adoc[revision=28c4082,diff=6d5eaf1,difftarget=test/fixtures/file-before-rename.adoc]
EOS

output = render_embedded_string input

assert_match(/diff --git a\/test\/fixtures\/file-before-rename.adoc b\/test\/fixtures\/file-after-rename.adoc/, output)
assert_match(/-Contents before rename./, output)
assert_match(/\+Contents after rename./, output)
end

test 'it includes a diff ignoring whitespaces and caret returns' do
input = <<-EOS
include::git@test/fixtures/lots_of_whitespaces.adoc[revision=e80ca3c,diff=2c2f9a9,ignorewhitespaces]
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/file-after-rename.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Contents after rename.