google/yapf

yapf-diff (similar to clang-format-diff) ?

pankdm opened this issue · 8 comments

I am using clang-format a lot. And it's very convenient to do so via clang-format-diff script (quick description from https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/clang-format-diff.py):

ClangFormat Diff Reformatter
============================

This script reads input from a unified diff and reformats all the changed
lines. This is useful to reformat all the lines touched by a specific patch.
Example usage for git/svn users:

  git diff -U0 HEAD^ | clang-format-diff.py -p1 -i
  svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i

It would great to have similar script for yapf. Looks like currently yapf doesn't have this mode available.

Do you have plans on supporting this?

We don't currently have something like this available for YAPF. It's definitely something we'd like to have, but just haven't had time to do it. I'll put the enhancement label on it.

If you're interested in contributing, it would probably just be taking the clang-format-diff script and modifying it a bit for YAPF. :-)

hayd commented

https://github.com/hayd/pep8radius does this, IIRC there's from_diff.

mwek commented

I modified the clang-format-diff.py to work with yapf and Python 3:
https://gist.github.com/mwek/59aefeefc812dea39c93c068eb30b491

Feel free to add this to the repo.

So several years later, any progress on this front? We'd love to enforce yapf on presubmit, but don't want to have people getting errors for files unrelated to their PRs. Could yapf-diff.py be added to this repository? We're about to copy and fix up mwek's gist. It actually seems like the right way to do this is to have a generic tool that works for clang-format, yapf, and anything else that accepts similar options. This tool's job would just be to turn a unified diff into filenames + lines and forward that and any extra args to a binary. Requires a bit more configuration, but way more flexible.

A few days ago I was also looking into this. I downloaded the latest copy of clang-format-diff.py, took the required changes from the gist above, tried to align the command line arguments with the yapf arguments and formatted the result with yapf 0.30.0. I just opened a pull request with the resulting code. It would be great if we could get this functionality supported!

Nice. We're doing the same thing and putting it in the third_party section of our project (iree-org/iree#3482). I suspect the LLVM license might be a concern for contributing this to yapf (even though it's almost the same as Apache2, having multiple licenses is practically-speaking a headache). Our version actually works with clang-format and yapf, which seems nice, although the fact that they use slightly different formatting for the --lines argument makes it significantly more annoying.

SKalt commented

Ages ago I hacked together https://github.com/luxresearch/yapf-diff as a wrapper around yapf 0.26. It's currently unlicensed, but I'll see if I can slap an apache-2 on the codebase.

Bonuses of that codebase:

  • comes with a test suite that might even be called comprehensive
  • formatted under yapf's self-formatting rules
SKalt commented

The above now has an apache-2 licence.