auto reformat in git hooks?

Hi,

 

For multiple developers using VS or QtCreator to edit C++ files we used (1) beautifier plugin to run clang-format in ui  (e.g. upon saving a file) and (2) git hooks to run the beautifier before commit just in case someone edits a file without IDE. Both, formatting in IDE and in git-hooks have to produce exactly the same results.

 

What is the easiest way to get the same effect for python file if we used PyCharm  as IDE?

 

(1) there are no beautifier plugins, right? if we use PyCharm for editing, we have to use PyCharm formatter (we cannot run e.g. yapf automatically from ui instead)? right? Or is there any way to replace PyCharm formatter by another beautifier?

 

If there is no choice in ui, then we are forced to use "pycharm.bat format" from command line for git hooks. For this, we would use style settings per project, and in "pycharm.bat format -f xxx" we can provide project's .idea\codeStyles\Project.xml. BUT if I run it just like that, I have to close PyCharm first before committing anything :( This is, obviously,  not acceptable.

 

(2) git hooks need to run pycharm.bat while the code is still open in IDE. https://intellij-support.jetbrains.com/hc/en-us/community/posts/205799739-How-to-start-another-instance-of-PyCharm- seems to suggest that we could: 

* unpack a second copy of PyCharm (we cannot use installer, right?)

* modify idea.properties in bin folder (we need to change both idea.config.path and idea.system.path, right? anything else?)

* make sure that start menu has a shortcut to the first copy, while PATH contains a path to the second copy

 

This seems irritatingly silly. Still: would it work? :) Is there any other/better way?

4 comments
Comment actions Permalink

"second copy of PyCharm" - hmm, the download page for windows offers only .exe installers, and these recognize that PyCharm is already installed and offer only one option:  uninstall. :P

* Is there a way to get binaries not through installer? Can I just copy the whole installation folder and edit idea.properties in the copy?

* if not, is there a way to run pycharm.bat in such a way that it would e.g. perform formatting using an already running instance of pycharm? (with only one installation folder)

0
Comment actions Permalink

>there are no beautifier plugins, right? if we use PyCharm for editing, we have to use PyCharm formatter (we cannot run e.g. yapf automatically from ui instead)? right? Or is there any way to replace PyCharm formatter by another beautifier?

Unfortunately not, please see/vote for related feature request: https://youtrack.jetbrains.com/issue/PY-39475

>If there is no choice in ui, then we are forced to use "pycharm.bat format" from command line for git hooks

Could you elaborate why you won't use "reformat before commit" option in [File | Settings | Version Control | Commit Dialog] ?

0
Comment actions Permalink

1. voted

 

2. "why you won't use "reformat before commit" option in [File | Settings..."

Because if you have some developers using PyCharm and some not, you still want to enforce EXACTLY the same style (and not keep reformatting each other's code). It's easier to agree that we all will install some tool and that it will be in the PATH, than to force everyone to use the same editor.

Because if someone does a quick "tiny" fix using vi (or whatever other editor) and commits by hand, you still want to enforce the style.

Because if someone has her own favorite tool to rebase, resolve conflicts, cherry-pick, stash, switch branches, git diff, choose what to stage and commit and whatever, they should not be forced to do this from always only a tool like PyCharm for which this is not the core functionality.

 

 

0
Comment actions Permalink

Thanks for elaborating. 

Well, installing a second instance of PyCharm is certainly sub-optimal solution and the formatter may not run because it will detect that JVM of the same version is already running.

I would suggest using an external formatter in this case as a git hook. 

0

Please sign in to leave a comment.