Skip to content

Commit 8f2bda1

Browse files
authored
Merge pull request #3 from edeweerd1A/diff-support-renames
fix(diff): Support files renaming between revisions for a diff
2 parents 48dad88 + 423b64c commit 8f2bda1

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

README.adoc

Lines changed: 8 additions & 0 deletions
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+
* `difftarget` - tell what the path of the target was before the changes (in case the target file was renamed)
4243
* `ignorewhitespaces` - whether to ignore whitespaces and newlines changes in an included patch
4344

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

108+
=== Including a patch of a file that has been renamed
109+
110+
To generate a patch for changes between two revisions (b015e8dd and b015e8dd) where a file has been renamed:
111+
112+
----
113+
\include::git@path/within/repo/file.rb[revision=b015e8dd,diff=0245ac72,difftarget=path/within/repo/previous_file.rb]
114+
107115
=== Including a patch ignoring whitespaces
108116
109117
To generate a patch for changes introduced in a specific revision (b015e8dd) but ignoring the changes related to whitespaces and caret line return:

lib/asciidoctor-git-include.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ 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+
target_before = attributes.fetch('difftarget', target)
1920
ignore_whitespaces_option = attributes.value?('ignorewhitespaces') || attributes.key?('ignorewhitespaces') ? '--ignore-cr-at-eol --ignore-space-at-eol -w -b --ignore-blank-lines' : ''
2021

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

test/extension_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ class ExtensionTest < Minitest::Test
106106
assert_match(/\+messages = \["Hello", "World", "!!!"\]/, output)
107107
end
108108

109+
test 'it includes a diff for specific revisions of a file that is renamed' do
110+
input = <<-EOS
111+
include::git@test/fixtures/file-after-rename.adoc[revision=28c4082,diff=6d5eaf1,difftarget=test/fixtures/file-before-rename.adoc]
112+
EOS
113+
114+
output = render_embedded_string input
115+
116+
assert_match(/diff --git a\/test\/fixtures\/file-before-rename.adoc b\/test\/fixtures\/file-after-rename.adoc/, output)
117+
assert_match(/-Contents before rename./, output)
118+
assert_match(/\+Contents after rename./, output)
119+
end
120+
109121
test 'it includes a diff ignoring whitespaces and caret returns' do
110122
input = <<-EOS
111123
include::git@test/fixtures/lots_of_whitespaces.adoc[revision=e80ca3c,diff=2c2f9a9,ignorewhitespaces]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Contents after rename.

0 commit comments

Comments
 (0)