Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to run code formatters #45

Closed
JacobHayes opened this issue Jan 11, 2020 · 7 comments
Closed

Ability to run code formatters #45

JacobHayes opened this issue Jan 11, 2020 · 7 comments

Comments

@JacobHayes
Copy link

Hi, thanks for this tool. Everything is pointing me here, but I can't seem to find a way to run things like black with this tool. With git filter-branch or filter-lamely, I can use the --tree-filter to run something like:

FILTER_BRANCH_SQUELCH_WARNING=1 time git filter-branch --tree-filter '\
    git show $GIT_COMMIT --name-status\
        | grep "^[AM]" \
        | grep "\.py$" \
        | cut -f2 \
        | xargs black \
          || echo “Error formatting, possibly invalid python“ \
' -- --all

Given every command is directing me to use stock git-filter-repo, what's the right way to do this? It'd be nice to only run the command on files matching a pattern (ex: *.py), but still keep unmatched files.

@newren
Copy link
Owner

newren commented Jan 11, 2020

filter-repo avoids checking out any of the files onto disk, which is part of its speedup. In that sense filter-repo's tree-filter is a bit easier to use here since your linting program probably expects the file on disk in normal format rather than trying to read it from git's history. However, contrib/filter-repo-demos/lint-history was specifically written to show how to lint files in history. You could just call it as
lint-history black
and have it lint every non-binary file in history for you, or you could tweak it to only call the linter for .py files (while keeping all other files as-is).

Also, the lint-history example actually includes three different implementations of how to do it: one in a comment showing how you could do it without writing a full-fledged python script that imports filter-repo, one which does it as a blob callback assuming that it doesn't matter to the lint program what the name of the file is (it just writes every file to .git/info/tmpfile and then runs the linter on that), and a third one that does it as a commit callback which makes sure the basename of the file is the same. You'll only need to use one of the three, but all three are in the demo since it's just trying to demonstrate how things could be done.

@JacobHayes
Copy link
Author

Thanks @newren, I'll try that out! It was close to working with:

git filter-repo --force --blob-callback '
  import black
  blob.data = black.format_str(blob.data.decode(), mode=black.FileMode()).encode()
'

but I needed to only reformat the python files and couldn't figure out how to limit it. Should be able to put something together with the lint-history commit-callback version!

@JacobHayes
Copy link
Author

Perfect, got it working. Thanks again!

@shenker
Copy link

shenker commented Jan 12, 2020

Thanks for this! If anybody finds this useful, I extended @JacobHayes 's code to also reformat Jupyter notebook cells: https://github.com/shenker/nbcleanse/blob/master/nbcleanse.py

@PavelPolyakov
Copy link

Thanks @newren and people in this thread for the inspiration, here is another adaptation of the lint example.
This time it is meant to format Java code using formatter-maven-plugin and custom style (for example) .

Files are processed in batches. Path to the style should be changed to the real one.

Here is gist: 🔗 .

@newren
Copy link
Owner

newren commented Feb 19, 2020

Thanks everyone for posting examples here; I put a comment pointing at this issue in the lint-history code so that others will be more likely to find all your examples.

@ankostis
Copy link

ankostis commented May 8, 2023

Adapted your script into #464 that allows to add new or replace files produced from shell commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants