Skip to content

Commit

Permalink
Merge pull request #3 from edeweerd1A/diff-support-renames
Browse files Browse the repository at this point in the history
fix(diff): Support files renaming between revisions for a diff
  • Loading branch information
jakzal authored Feb 8, 2025
2 parents 48dad88 + 423b64c commit 8f2bda1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
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.

0 comments on commit 8f2bda1

Please sign in to comment.