Skip to content

Document how to run pre-commit outside a git repo #2125

Closed

Description

@iainelder

pre-commit's documentation promotes the use of the pre-commit run --all-files command as a CI step.

pre-commit can also be used as a tool for continuous integration. For instance, adding pre-commit run --all-files as a CI step will ensure everything stays in tip-top shape.

Unfortunately, my CI system's working folder is not a git repo. Instead the working folder appears to be a copy of the latest commit in a normal folder.*

It's unfortunate because pre-commit by design works only with git repos.

In #1894 it is shown how to work around the issue.

Is it possible to use pre-commit on files that are git-ignored (or even outside of a git repository)?

nope. the easiest is to git init && git add . if you need to

Could this be added to the documentation under the "Usage in continuous integration" section?

I'd make a PR myself, if you could point me to the docs.

* (I don't know how common a practice this is; the CI system on my current project is quite bespoke.)

Activity

  1. asottile commented on Nov 11, 2021

    @asottile
    Member

    I don't think it needs documenting since the error messages are clear

  2. iainelder commented on Nov 11, 2021

    @iainelder
    Author

    Hey, thanks for the quick response.

    You mean this one?

    An error has occurred: FatalError: git failed. Is it installed, and are you in a Git repository directory?

    I'll grant you that it did help me track down the problem.

    After adding the following test commands to my CI script:

    • git --version
    • git rev-parse --is-inside-work-tree

    I determined that the working copy wasn't a git repo.

    The message could be clearer because it could run those checks itself to be more exact about the problem. Whether it's worth the effort to include that is another question.

    Searching the Github issues of the project lead me to the workaround you mentioned.

    In the end I guess it comes down to how much support you want to give to the use case.

    It will remain documented in the Github issues even if it doesn't make it into the main documentation.

  3. asottile commented on Nov 11, 2021

    @asottile
    Member

    yeah makes sense, this is a fairly rare case

  4. JonasVervoort commented on Jul 31, 2024

    @JonasVervoort

    I had also problems with the usage of pre-commit in combination with actions/checkout.

    jobs:
    
        format_job:
          name: clang-format
          runs-on: [self-hosted, Linux, X64, docker] 
          container:
            image: <<my-constainer-that-has-git-and-pre-commit-installed>>
          
          steps:
          - uses: actions/checkout@v4
         # git checkout is not creating a working git folder, it has dubious ownership if not configured
         # The pre-commit hook will not work on this original git folder
          - name: fix git
            run: git config --system --add safe.directory '*'
          - name: Install pre-commit 
            run:  pre-commit install
          - name: Run clang-format pre-commit check  
            run:  pre-commit run --all-files
    

    With the addition of the git config --system -add safe.directory '*' it works

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions