Skip to content

Contribute a git hook to pre-commit.com #467

Closed

Description

@katrinleinweber

Update

A broader solution than discusses here can be found in lorenzwalthert/pre-commit-hooks.

Original post

I read in #452 that git integration is not a priority, but maybe a small hook that just styles the files that one has git added / staged would be a minimal, viable step into that direction. Maybe as a contribution to pre-commit.com?

Do you think it would be possible to shell-script styler in that way? I played around a bit, but ran into problems.

Activity

  1. katrinleinweber commented on Jan 28, 2019

    @katrinleinweber
    ContributorAuthor
    for f in $(git diff --name-only --cached | grep '.R')
    do
    	Rscript -e "styler::style_file($f)"
    done
    

    yields Error in dirname(path) : object 'R' not found. Probably, because the files are of course in an R/ subdirectory.

  2. lorenzwalthert commented on Jan 29, 2019

    @lorenzwalthert
    Collaborator

    Hi @katrinleinweber, thanks.

    I read in #452 that git integration is not a priority

    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?

  3. katrinleinweber commented on Jan 29, 2019

    @katrinleinweber
    ContributorAuthor

    Thanks for contextualising the idea! Yes, that would certainly be more work.

    the git hook would not be implemented in the source code of styler

    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.

  4. katrinleinweber commented on Jan 29, 2019

    @katrinleinweber
    ContributorAuthor
  5. asottile commented on Jan 29, 2019

    @asottile

    Exciting! Yeah the work to make this happen would be in two parts:

    1. Implement r as a language in pre-commit
    2. Add a .pre-commit-hooks.yaml file in this repository

    Does styler provide an executable when installed? and/or what's the usual interface that people run tools written in R?

  6. lorenzwalthert commented on Feb 16, 2019

    @lorenzwalthert
    Collaborator

    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 Rscript -e "styler::style_file(...)".

  7. michaelquinn32 commented on Feb 17, 2019

    @michaelquinn32
    Contributor

    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.

  8. bfgray3 commented on Apr 3, 2019

    @bfgray3

    I'd like to add my vote in support of a pre-commit hook.

  9. jackwasey commented on Apr 12, 2019

    @jackwasey

    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 codemeta gives, instead of doing it for you. It is also a slow process for a bigger package, although checking whether styling is needed is probably just as slow as actually performing it, until there is some caching in styler.

  10. lorenzwalthert commented on Apr 12, 2019

    @lorenzwalthert
    Collaborator

    Also note this reference: #452.

  11. lorenzwalthert commented on Apr 12, 2019

    @lorenzwalthert
    Collaborator

    @jackwasey indeed that would be less invasive. Indeed a style_pkg() call as a pre-commit hook would be is quite slow currently, we have implemented neither multi-processing and caching. Please refer to #370 and #491 and #320.

    Edit:* Of course, we'd only style the files changed, not the whole package, as drafted above.

  12. 6 remaining items

  13. lorenzwalthert commented on May 12, 2019

    @lorenzwalthert
    Collaborator

    [...] Neat!

    Not sure, I think it's a bit hacky 😄. But thanks for looking into it @asottile. Yes, I was wondering about using language: script but I could not easily find an example so I just decided to go for language: python for a first pass. So if we choose to use language: script, how would that affect my repo structure? Could my repo at https://github.com/lorenzwalthert/styler_precommit then just contain a single script run-r-styler (with shebang, as you suggested above) instead of a whole package? Because I don't fully understand this part of the documentation and how this relates to your example above:

    Script hooks provide a way to write simple scripts which validate files. The entry should be a path relative to the root of the hook repository.

    Maybe then I don't even have to use python in my code because with R, we can use Rscript with shebang, so could I have

    # !/usr/bin/env Rscript
    
    # R code only
    

    Correct?
    Since you are in this thread, are there any recommendations regarding naming conventions of repositories and hook ids you'd suggest to use for styler so we can make it match conventions of pre-commit for other linters and style fixers? I saw there is autopep8 and flake support, so maybe check out those?

    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.

  14. asottile commented on May 12, 2019

    @asottile

    Maybe then I don't even have to use python in my code because with R, we can use Rscript with shebang, so could I have

    # !/usr/bin/env Rscript
    
    # R code only
    

    Correct?

    Yep! That would be even simpler :D

    Since you are in this thread, are there any recommendations regarding naming conventions of repositories and hook ids you'd suggest to use for styler so we can make it match conventions of pre-commit for other linters and style fixers? I saw there is autopep8 and flake support, so maybe check out those?

    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)

    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

    Ah neat, hadn't even thought about that -- it's really surprising to me that R packages can't have commandline tools :S

  15. katrinleinweber commented on May 12, 2019

    @katrinleinweber
    ContributorAuthor

    1k thanks for getting this up and running :-) I followed #467 (comment) just now and it worked fine and committed the style-modifications.

    What are the advantages of overcommit @katrinleinweber?

    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.

  16. lorenzwalthert commented on May 12, 2019

    @lorenzwalthert
    Collaborator

    @asottile I think when using language: script, another disadvantage is that the user has to manually make the script executable and the directory where it lays is something like ~/.cache/pre-commit/repof4lOTz/bin/executable, so it's hard to even give the user clear instructions when they are not experienced command line users. At least in my case that seemed necessary. Any workaround for this?
    Solution: Just make the file executable and commit because the permission is an attribute of the file.

  17. asottile commented on May 12, 2019

    @asottile

    You should be able to check it in executable chmod +x script && git add script

  18. lorenzwalthert commented on May 21, 2019

    @lorenzwalthert
    Collaborator

    Summary:

    Put a .pre-commit-config.yaml in your repo root with this content:

    -   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 devtools::document() and you are welcomed to contribute others. See https://github.com/lorenzwalthert/pre-commit-hooks.

    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.

  19. lorenzwalthert commented on May 21, 2019

    @lorenzwalthert
    Collaborator

    Need to tweet / communicate this before closing.

  20. katrinleinweber commented on May 22, 2019

    @katrinleinweber
    ContributorAuthor

    Thank you for building this @lorenzwalthert & @asottile :-)

  21. asottile commented on May 22, 2019

    @asottile

    no problem! happy to help :D -- @lorenzwalthert did most of the heavy lifting to make it happen -- I just assisted :)

  22. lorenzwalthert commented on Jun 19, 2019

    @lorenzwalthert
    Collaborator

    In case you were using the hook: There was a bug in styler (#521) that made it impossible to run the hook correctly on R files in different directories. It is fixed in #522 so if you want this to work correctly, get the dev version of styler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions