Live data from Hacker News

Difftastic: A diff that understands syntax

github.com

11–20 of 224 posts

Re: Difftastic: A diff that understands syntax

#12

Is there a good reason why diff tools generally don’t use AST?

Because it is much easier, you don't have to build and maintain parsers for hundreds of languages. And you don't need need just any parser, you need very robust ones that can deal with malformed files well. Or, if you only pick a small set of supported languages, your diff tool will not work on most files or have to fall back to a structure-agnostic algorithm. Also not all text files even follow any useful grammar at all.

Finally, even if you have a syntax tree, that is just part of the solution, probably the smaller one. Detecting three lines of code wrapped in a new if statement is easy but also doesn't benefit much from a syntax-aware algorithm. But once you changes names and signatures, extract methods, introduce constants, and so on it will become progressively harder to match subtrees and one is probably quickly approaching the territory of NP-hard and undecidable problems.

Re: Difftastic: A diff that understands syntax

#13
post #5

This looks absolutely amazing. One thing I do find interesting (and a wish were different) is that only programming languages are supported, rather than data formats as well. For example, two JSON documents may be valid but formatted slightly differently, or a common task for me is comparing two YAML files. Comparing config files that have a well defined syntax and or can be abstracted into a tree (JSON, YAML, TOML,…

JSON is supported. HTML and XML are missing, too.

You're right. I missed JSON.

Sadly YAML, TOML and the others I mentioned are not there (yet?)

Re: Difftastic: A diff that understands syntax

#14
Looks really cool, but there was no instructions on how to install it.

I would recommend putting an installation guide in your readme, and it being a full installation guide.

I followed the link to your manual and then it told me to install your tool using a tool called "cargo" with no reference on how to install cargo. At this point I gave up. Lazy, maybe, but for a convenience tool like this I want a convenient installation.

Re: Difftastic: A diff that understands syntax

#16

I paid and used SemanticMerge quite successfully when we had a complex Git workflow with lots of conflicts. https://semanticmerge.com/ Since moving to short lived feature branches it is less useful to me.

SemanticMerge sounded interesting enough so I wanted to check it out, but to my surprise there is no Buy or Download link anywhere on the site. The only thing that might do it is a Login link, but I don't want to create an account just to see how much the thing costs. Is it only sold in bulk to companies? I find it bizarre that there isn't even a "contact sales" button.

Re: Difftastic: A diff that understands syntax

#17
post #14

Looks really cool, but there was no instructions on how to install it. I would recommend putting an installation guide in your readme, and it being a full installation guide. I followed the link to your manual and then it told me to install your tool using a tool called "cargo" with no reference on how to install cargo. At this point I gave up. Lazy, maybe, but for a convenience tool like this I want a convenient ins…

Looks like you need to install the Rust programming language and compile it. It worked for me. Not sure if I like the installation method. It seems the executable is portable though.

Re: Difftastic: A diff that understands syntax

#18
post #12

Is there a good reason why diff tools generally don’t use AST?

Because it is much easier, you don't have to build and maintain parsers for hundreds of languages. And you don't need need just any parser, you need very robust ones that can deal with malformed files well. Or, if you only pick a small set of supported languages, your diff tool will not work on most files or have to fall back to a structure-agnostic algorithm. Also not all text files even follow any useful grammar at…

This tool is built on tree-sitter (https://tree-sitter.github.io/tree-sitter/), so presumably it doesn't need to maintain parsers at all.

I've thought before this is how diffing should be done, and speculated that tree-sitter would make it more feasible.

At this point, whenever I think some language-aware tool ought to exist, my first thought is "Does the language server protocol or tree-sitter make this more feasible?"

Re: Difftastic: A diff that understands syntax

#19

If you have consistent code style and formatting this tool is unnecessary. I think that solution is better, you get a more consistent code base that is easier to read for humans. (Also diffs will be faster to compute)

Even if you are consistent, having unchanged indented text show up differently is very clever. I often end up reviewing a diff that moves a basic block into a conditional branch and have to scan each line to see if it changed.

Re: Difftastic: A diff that understands syntax

#20

If you have consistent code style and formatting this tool is unnecessary. I think that solution is better, you get a more consistent code base that is easier to read for humans. (Also diffs will be faster to compute)

> If you have consistent code style and formatting this tool is unnecessary

I disagree. I struggle to replicate it right now using a simple test, but I've seen the following rather infuriating and counter intuitive behaviour from Git/GNU diff. If you have a simple if statement such as:

    if (bla) {
      // do something
    }
And you were to add another statement at the end, after the closing curly brace, e.g.:

    if (bla) {
      // do something
    }

    if (bla2) {
      // do something else
    }
Git/GNU diff will sometimes show the following diff:

    diff --git 1/left 2/right
    index c2ea6f1..dc0e1c2 100644
    --- 1/left
    +++ 2/right
    @@ -1,3 +1,6 @@
     if (bla) {
       // do something
    +}
    +if (bla2) {
    +  // do something else
     }
This is basic example, but there's other similar things. For a simple change like the above, this isn't a huge issue, but for a bigger patch sets, it can take a minute to understand what is really going on.
Post reply on HN