rtfpessoa / diff2html

Pretty diff to html javascript library (diff2html)
https://diff2html.xyz
MIT License
2.8k stars 273 forks source link

binary file diff has displyed diff-content of previous file #472

Closed endruz closed 1 year ago

endruz commented 1 year ago
<html>
    <head>
        <!-- Stylesheet -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/github.min.css" />
        <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" />

        <!-- Javascripts -->
        <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
        <script>
            document.addEventListener('DOMContentLoaded', () => {
  const diffString = `diff -ur /tmp/a/test.json /tmp/b/test.json
--- /tmp/a/test.json
+++ /tmp/b/test.json
@@ -1 +1 @@
-{"list": [1, 2]}
+{"list": [1, 2, 3]}
Binary files /tmp/a/image.gif and /tmp/b/image.gif differ
diff -ur /tmp/a/test.html /tmp/b/test.html
--- /tmp/a/test.html
+++ /tmp/b/test.html
@@ -1 +1 @@
-<a>test</a>
+<a>new test</a>`;

  const targetElement = document.getElementById('myDiffElement');
  const configuration = { drawFileList: true, matching: 'lines', highlight: true };
  const diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);
  diff2htmlUi.draw();
  diff2htmlUi.highlightCode();
});
        </script>
    </head>
    <body>
        <div id="myDiffElement"></div>
    </body>
</html>

in this case, we can find that the diff-content of test.json is displayed in image.gif part.

binary file diff has displyed diff-content of previous file, like https://github.com/rtfpessoa/diff2html/issues/119 and https://github.com/rtfpessoa/diff2html/issues/233.

it is a pity that these two issues have no follow-up content 😭 so I decided to describe it again

rtfpessoa commented 1 year ago

πŸ‘‹ AFAIK that does not seem like a valid unified diff. This project aims to support unified diffs including the super set from git diff.

You can read more about it here https://www.gnu.org/software/diffutils/manual/html_node/Example-Unified.html and here https://git-scm.com/docs/diff-format

In my understanding it seems like you are missing the --- and +++ on the binary files. Can you explain how you got that diff?

endruz commented 1 year ago

maybe i modifid the diff result and made it wiredπŸ˜‚ i will show you a true example:

$ diff -ur a b
diff -ur a/htest.html b/htest.html
--- a/htest.html        2023-01-10 09:43:04.284427636 +0800
+++ b/htest.html        2023-01-10 09:43:10.308388990 +0800
@@ -1 +1 @@
-<a>test</a>
+<a>new test</a>
Binary files a/image.gif and b/image.gif differ
diff -ur a/test.json b/test.json
--- a/test.json 2023-01-10 09:43:07.832404870 +0800
+++ b/test.json 2023-01-10 09:43:12.708373605 +0800
@@ -1 +1 @@
-{"list": [1, 2]}
+{"list": [1, 2, 3]}
$ tree .
.
β”œβ”€β”€ a
β”‚Β Β  β”œβ”€β”€ htest.html
β”‚Β Β  β”œβ”€β”€ image.gif
β”‚Β Β  └── test.json
└── b
    β”œβ”€β”€ htest.html
    β”œβ”€β”€ image.gif
    └── test.json

2 directories, 6 files
$ cat a/htest.html
<a>test</a>
$ cat b/htest.html
<a>new test</a>
$ cat a/test.json
{"list": [1, 2]}
$ cat b/test.json
{"list": [1, 2, 3]}

In my understanding it seems like you are missing the --- and +++ on the binary files.

if i use diff -ur, there doesn't seem to be the --- and +++ on the binary files. πŸ€”

rtfpessoa commented 1 year ago

Can you run diff -ur a b > /tmp/test.diff and then cat /tmp/test.diff?

endruz commented 1 year ago

okay, it looks like this:

$ diff -ur a b > /tmp/test.diff
$ cat /tmp/test.diff
diff -ur a/htest.html b/htest.html
--- a/htest.html        2023-01-10 09:43:04.284427636 +0800
+++ b/htest.html        2023-01-10 09:43:10.308388990 +0800
@@ -1 +1 @@
-<a>test</a>
+<a>new test</a>
Binary files a/image.gif and b/image.gif differ
diff -ur a/test.json b/test.json
--- a/test.json 2023-01-10 09:43:07.832404870 +0800
+++ b/test.json 2023-01-10 09:43:12.708373605 +0800
@@ -1 +1 @@
-{"list": [1, 2]}
+{"list": [1, 2, 3]}
rtfpessoa commented 1 year ago

TBH this really seems like an invalid unified diff, but since it seemed easy to to cover I added support.

Hopefully this will not introduce any other problems.