-
Notifications
You must be signed in to change notification settings - Fork 71
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
Contribute a git hook to pre-commit.com #467
Comments
yields |
Hi @katrinleinweber, thanks.
Thanks for considering old issues. I did not mean I am strictly against it, but I at least won't develop it myself. If I understand correctly, the git hock would not be implemented in the source code of styler, right? I think it's a cool thing. I am not sure but I believe you could simply defer the path expansion to R? for f in $(git diff --name-only --cached | grep '.R')
do
Rscript -e "styler::style_file(file.path('R', $f))"
done It is unfortunate that pre-commit.com does not support R because the above script might have problems due portability across operation systems. Are you on Windows? I can try the script on my Mac. Maybe we can add support for R to pre-commit.com too, although that might be more work? |
Thanks for contextualising the idea! Yes, that would certainly be more work.
I guessed so (over in pre-commit/pre-commit-hooks), but pre-commit's contributing docu says: "get the .pre-commit-hooks.yaml files added to popular linters". I guess that means styler as well ;-) OTOH, those files are present in so many more, normal projects. Hm... I'll inquire about the process of adding R support on their end. |
Apparently, they need an |
Exciting! Yeah the work to make this happen would be in two parts:
Does |
If you mean a bash-like command when you say executable, the answer is no. I don't know of any R package that does so to be honest. In almost all cases, people execute tools written in R in the R console (e.g. when using RStudio) or they invoke R from the command line with |
Pre-commits seem better placed somewhere like usethis, which contains development tools that wrap styler. Plus, that package already sets up a user's git for a project. |
I'd like to add my vote in support of a pre-commit hook. |
A less invasive approach would be simply to report whether files would be styled, thus preventing a commit. Since it is possible that styler could generate invalid code, at least some people would prefer a message, like |
Also note this reference: #452. |
@jackwasey indeed that would be less invasive. Indeed a Edit:* Of course, we'd only style the files changed, not the whole package, as drafted above. |
For all people who can't wait for a git hook, have a look at the starters package, they seem to have implemented such a hook for styler: lockedata/starters#138. cc: @jonmcalder if you are interested in contributing here, most welcome. We also discussed how to add R as a language for pre-commit.com (pre-commit/pre-commit#926), but it seems to quite some work/overhead. |
I think that's a fair point. There might be other pre-commits one would like to implement, not just for styler. Ideally we'd add R as a language to pre-commit.com or a similar framework. Feel free to open an issue in usethis for this. |
An alternative might be https://github.com/sds/overcommit?
True, but is usethis also relevant for non-package projects? Git & Styler are, IMHO. Plus, I generally think that integrating what we want into an upstream framework is more fruitful in the long run than integrating it into R-specific tooling only. |
Yes. Maybe it is worth looking into infrastructure that usethis has for git hooks, like To me, it does not really matter to which framework we contribute it. What are the advantages of |
Bottom line: It worked! Now, just create a
In full length: I just did some more research on pre-commit.com and I think I now understand the crux. For python pre-commit hooks, it says in the documentation:
This is exactly what we see in the pre-commit/pre_commit_hoocks: https://github.com/pre-commit/pre-commit-hooks/blob/master/setup.cfg. The python functions are exposed as command line executables. In R, there is no way we could create a command line executable from an R package. This was apparently a deliberate design decision (https://stackoverflow.com/questions/44566100/installing-executable-scripts-with-r-package). Anyways, because pre-commit.com does not yet support R and there is no way we could create the necessary executable anyways, I just decided to take the ugly approach for an experiment: Create a python pre-commit that calls a subprocess that calls R. It is worth noticing that all these things are triggered by This is very experimental! We do not guarantee this to continue to work. We need to find a better way and find out 1) where to best host the python package, 2) how to name the executable 3) error handling 4) do testing, documentation and other things. If you want to give it a try, most welcomed. |
Neat! One thing that you could do is use Then you could have an entrypoint like: #!/usr/bin/env python
# (same contents as your current script) And then a config like: - id: ...
language: script
entry: ./bin/run-r-styler |
Not sure, I think it's a bit hacky 😄. But thanks for looking into it @asottile. Yes, I was wondering about using
Maybe then I don't even have to use python in my code because with R, we can use
Correct? An advantage of using the python package could be that we'd be able to expose a command line executable for styler that could be used independently of pre-commit, which is not possible with an R package. Related: #494. |
Yep! That would be even simpler :D
There isn't currently any sort of naming convention -- though they can be added to our list of repositories on the website via this -- ends up here. It's ~generally preferred to have it in the actual repo of the tool -- but in this case it's probably better separated since the versioning won't properly lock (which is what is ~mostly expected of other repos)
Ah neat, hadn't even thought about that -- it's really surprising to me that R packages can't have commandline tools :S |
1k thanks for getting this up and running :-) I followed #467 (comment) just now and it worked fine and committed the style-modifications.
If I had noticed any clearly relevant one, I would have written so initially ;-) "It's in Ruby" could be considered an advantage, but since academia choose Python and most R folks are more familiar with that, I didn't consider it relevant. Just a note for completeness sake. |
|
You should be able to check it in executable |
Summary: Put a - repo: https://github.com/lorenzwalthert/pre-commit-hooks
rev: 2bff9322dfab68f08e4c722c6caee7800e966b2a
hooks:
- id: styler-style-files And you are all set. There is another hook for Edit: For this to work correctly in all cases (see below), you need the dev version of styler or the CRAN release after version 1.1.1. |
Need to tweet / communicate this before closing. |
Thank you for building this @lorenzwalthert & @asottile :-) |
no problem! happy to help :D -- @lorenzwalthert did most of the heavy lifting to make it happen -- I just assisted :) |
Update
A broader solution than discusses here can be found in lorenzwalthert/pre-commit-hooks.
The text was updated successfully, but these errors were encountered: