Skip to content

[FR][bug?] pre-commit hook repo self-test #1258

Closed

Description

@webknjaz

Given a repo with .pre-commit-hooks.yaml defined (like https://github.com/ansible/ansible-lint), I want to integrate testing of the hooks declared in it.

I can do pre-commit try-repo https://github.com/ansible/ansible-lint.git but this hits the remote which I want to avoid. I know that Git itself can work with local fs paths (like /path/to/.git) perfectly fine.

So I tried:

$ pre-commit try-repo .git -vvv
➜ pre-commit try-repo .git -vvv
[WARNING] Creating temporary repo with uncommitted changes...
An unexpected error has occurred: CalledProcessError: Command: ('/usr/bin/git', 'add', '-u')
Return code: 128
Expected return code: 0
Output: (none)
Errors: 
    fatal: this operation must be run in a work tree
    

Check the log at ~/.cache/pre-commit/pre-commit.log

The log doesn't reveal anything more than the fact that the Git command failed.

$ cat ~/.cache/pre-commit/pre-commit.log
An unexpected error has occurred: CalledProcessError: Command: ('/usr/bin/git', 'add', '-u')
Return code: 128
Expected return code: 0
Output: (none)
Errors: 
    fatal: this operation must be run in a work tree
    

Traceback (most recent call last):
  File "~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/pre_commit/error_handler.py", line 46, in error_handler
    yield
  File "~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/pre_commit/main.py", line 296, in main
    return try_repo(args)
  File "~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/pre_commit/commands/try_repo.py", line 55, in try_repo
    repo, ref = _repo_ref(tempdir, args.repo, args.ref)
  File "~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/pre_commit/commands/try_repo.py", line 45, in _repo_ref
    cmd_output('git', 'add', '-u', cwd=repo, env=env)
  File "~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/pre_commit/util.py", line 153, in cmd_output
    returncode, cmd, retcode, output=(stdout, stderr),
pre_commit.util.CalledProcessError: Command: ('/usr/bin/git', 'add', '-u')
Return code: 128
Expected return code: 0
Output: (none)
Errors: 
    fatal: this operation must be run in a work tree

It must be pretty easy to fix.

Activity

  1. asottile commented on Dec 30, 2019

    @asottile
    Member

    I believe you want pre-commit try-repo .

  2. asottile commented on Dec 30, 2019

    @asottile
    Member

    the docs go a little bit into this:

  3. webknjaz commented on Dec 30, 2019

    @webknjaz
    Author

    This is what stopped me. I guess I forgot to check it this time because I saw some issues in the past...

    Errors: 
        Directory '.' is not installable. File 'setup.py' not found.
        You are using pip version 10.0.1, however version 19.3.1 is available.
        You should consider upgrading via the 'pip install --upgrade pip' command.

    Trying now...

    Anyway, it may be a good idea to emit some hint if somebody tries repo==.git

  4. webknjaz commented on Dec 30, 2019

    @webknjaz
    Author

    It says:

    Since the repo property of .pre-commit-config.yaml can refer to anything that git clone ... understands

    which breaks with .git meaning that the statement is not completely true in all cases...

    Looks like this issue deserves a bug label after all.

  5. webknjaz commented on Dec 30, 2019

    @webknjaz
    Author

    Anyway, thanks for the docs! Using . works locally and I wanted this specifically for a smoke test in CI so I'm good. Feel free to close this issue if you don't intend to commit any fixes.

  6. asottile commented on Dec 30, 2019

    @asottile
    Member

    that setup error probably means you have an old version of virtualenv which did not understand pyproject.toml-based installations (therefore the old version of pip)

    I'm still not sure how you got to trying .git -- none of the docs show anything like that :S

  7. webknjaz commented on Dec 30, 2019

    @webknjaz
    Author

    I'm still not sure how you got to trying .git

    I explained it above: I know that git command understands it (any path to a bare Git repo) so I gave it a shot :)

    that setup error probably means you have an old version of virtualenv

    yep, already fixed by creating a new one.

    I think I was confused because I hit multiple unrelated errors and that started clouding my judgments...

  8. asottile commented on Dec 30, 2019

    @asottile
    Member

    oddly enough, the statement about repo appears correct, but try-repo has different semantics for local directories:

    $ pre-commit  run --all-files
    [INFO] Installing environment for /home/asottile/workspace/pre-commit/.git.
    [INFO] Once installed this environment will be reused.
    [INFO] This may take a few minutes...
    Validate Pre-Commit Manifest.........................(no files to check)Skipped
  9. webknjaz commented on Dec 30, 2019

    @webknjaz
    Author

    Maybe if you convert that to abspath in try-repo, it'd fix the issue.

  10. asottile commented on Dec 30, 2019

    @asottile
    Member

    this is returning True for bare repos, probably because git diff makes no sense there:

    if os.path.exists(repo) and git.has_diff('HEAD', repo=repo):

  11. asottile commented on Jan 2, 2020

    @asottile
    Member

    This has been released as part of v1.21.0 -- thanks again for the issue 🎉!

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions