Skip to content
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

bug: griffe check from branch against main cannot create a worktree #337

Closed
szymonmaszke opened this issue Nov 29, 2024 · 6 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@szymonmaszke
Copy link

szymonmaszke commented Nov 29, 2024

Description of the bug

Maybe I am misusing griffe, but what I am trying to do:

  • Start on a branch a created from main
  • Create python code, stage it
  • Run griffe check --against main <package>

This returns the error RuntimeError: Could not create git worktree: fatal: invalid reference: master

To Reproduce

> mkdir foo
> cd foo
> git init
> git checkout -b a
> touch foo.py # add any basic function into the file or smth like that
> griffe check --against master foo

Full traceback

Full traceback
Traceback (most recent call last):
  File "/home/xyz/.local/bin/griffe", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/xyz/.local/share/pipx/venvs/griffe/lib/python3.12/site-packages/_griffe/cli.py", line 562, in main
    return commands[subcommand](**opts_dict)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xyz/.local/share/pipx/venvs/griffe/lib/python3.12/site-packages/_griffe/cli.py", line 477, in check
    old_package = load_git(
                  ^^^^^^^^^
  File "/home/xyz/.local/share/pipx/venvs/griffe/lib/python3.12/site-packages/_griffe/loader.py", line 883, in load_git
    with tmp_worktree(repo, ref) as worktree:
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/home/xyz/.local/share/pipx/venvs/griffe/lib/python3.12/site-packages/_griffe/git.py", line 127, in tmp_worktree
    raise RuntimeError(f"Could not create git worktree: {process.stderr.decode()}")
RuntimeError: Could not create git worktree: fatal: invalid reference: master

Expected behavior

Changes introduced on the branch a to be displayed (as added function foo or a-like)

Environment information

- __System__: Linux-6.11.6-zen1-1-zen-x86_64-with-glibc2.40
- __Python__: cpython 3.12.7 (/home/XYZ/.local/share/pipx/venvs/griffe/bin/python)
- __Environment variables__:
  - `PYTHONPATH`: `/home/XYZ/.local/share/pdm/venv/lib/python3.12/site-packages/pdm/pep582:/home/XYZ/.local/share/pdm/venv/lib/python3.12/site-packages/pdm/pep582`
- __Installed packages__:
  - `griffe` v1.5.1

Also tried with griffe installed within pdm, using pep582 global as well as described here

Additional context

  • I am not using tags, my workflow doesn't use them at all, but griffe is, please correct me if I'm wrong, supposed to work with branches directly
  • Also tried using commit hashes, the same behaviour happens (new branch is created instead of switched to)
  • In some cases there are errors related to creating a new branch (this works the first time, but fails to run and cleanup after itself, for example: RuntimeError: Could not create git worktree: Preparing worktree (new branch 'initial_release') fatal: a branch named 'initial_release' already exists)
  • Was able to obtain the changes once (being on main, checking against branch a), got Deleted function foo etc., as expected
  • After switching to branch a and running against main I got the error as below
  • Pre-commit hooks are ran during the checkout done by griffe (different repository with way more involved more pdm-centric setup), is it possible to turn these off by adding --no-verify to git command or some kind of flag to customize the behaviour?)
  • pdm might be the issue as the sitecustomize.py (as described here), although I don't see a direct connection with the git command being ran by griffe

All in all, it seems like the matching mechanism for --against seems to not recognize the argument and instead of checking out a given reference/branch into tmp it tries to create a new branch (?)

@szymonmaszke szymonmaszke added the unconfirmed This bug was not reproduced yet label Nov 29, 2024
@pawamoy
Copy link
Member

pawamoy commented Nov 30, 2024

Hi @szymonmaszke, thanks for the report.

I am not using tags, my workflow doesn't use them at all, but griffe is, please correct me if I'm wrong, supposed to work with branches directly

Correct, any Git reference that you can checkout should work.

Pre-commit hooks are ran during the checkout done by griffe (different repository with way more involved more pdm-centric setup), is it possible to turn these off by adding --no-verify to git command or some kind of flag to customize the behaviour?)

This might be at the center of the issue. I didn't know there were hooks that could when merely checking out a reference. I'm not sure how we could disable hooks while checking a ref out, there help for git-checkout doesn't mention hooks at all.

I don't think PDM and its PEP 582 setup has anything to do with this 🙂 (I've been a user myself).


So, your example indeed reproduces the error, but I think that is expected. By the time you run griffe check in this new repo, you don't actually have a main branch, so it's only normal that Griffe cannot check a main branch out. I could try to investigate in your actual repository if it is public.

@szymonmaszke
Copy link
Author

szymonmaszke commented Nov 30, 2024

Hey @pawamoy, thank you for a quick response!

Pre-commit hooks

This might be at the center of the issue. I didn't know there were hooks that could when merely checking out a reference.

I was surprised as well, here are the hook types and this behavior is related to post-checkout which is relatively less used.

I'm not sure how we could disable hooks while checking a ref out, there help for git-checkout doesn't mention hooks at all.

As described in this SO answer you could do a checkout without running hooks (tested myself) via:

> git -c core.hooksPath=/dev/null checkout <BRANCH>

which might not work on Windows though due to /dev/null device (possible resolutions described here or so).

Error

So, your example indeed reproduces the error, but I think that is expected. By the time you run griffe check in this new repo, you don't actually have a main branch, so it's only normal that Griffe cannot check a main branch out.

If I understand you correctly - I ran it against the master (which exists and is git default branch), maybe the comment # possibly change to master, does not work either way was not clear (updated my original post to reflect all the steps correctly, my apologies).

And AFAICT these steps should work, correct me if I'm wrong (also you can treat is a reproducible case, please notice the comments as well):

> mkdir foo
> cd foo
> git init
> git checkout -b a
> touch foo.py
> git add foo.py # this step should not matter
> griffe check --against master foo

content of foo.py (although I don't think it matters at all):

def my_func():
    pass

which returns:

Traceback (most recent call last):
  File "/home/XYZ/.local/bin/griffe", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/XYZ/.local/share/pipx/venvs/griffe/lib/python3.12/site-packages/_griffe/cli.py", line 562, in main
    return commands[subcommand](**opts_dict)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/XYZ/.local/share/pipx/venvs/griffe/lib/python3.12/site-packages/_griffe/cli.py", line 477, in check
    old_package = load_git(
                  ^^^^^^^^^
  File "/home/XYZ/.local/share/pipx/venvs/griffe/lib/python3.12/site-packages/_griffe/loader.py", line 883, in load_git
    with tmp_worktree(repo, ref) as worktree:
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/home/XYZ/.local/share/pipx/venvs/griffe/lib/python3.12/site-packages/_griffe/git.py", line 127, in tmp_worktree
    raise RuntimeError(f"Could not create git worktree: {process.stderr.decode()}")
RuntimeError: Could not create git worktree: fatal: invalid reference: master

Additional info

  • Tested without global PEP582 PDM - same behavior
  • Python 3.12.7
  • git version 2.47.1
  • Error itself does not seem to be related to pre-commit hooks at all (or, if so, in a very non-obvious way), in all cases it is still unable to create git worktree for some reason (?)

Please let me know if I could be of any further assistance.

@szymonmaszke
Copy link
Author

szymonmaszke commented Nov 30, 2024

@pawamoy invited you to the repository with MCVE here.

Steps:

  1. Clone
  2. git checkout foo
  3. griffe check foo --against main

Both branches are checkoutable, but the same error persists on my workstation.

Different branch name, filename and function name does not matter in this case either (just verified).

@pawamoy
Copy link
Member

pawamoy commented Nov 30, 2024

Thanks! OK so that's a regression, and I know how to fix it (easy fix).

@pawamoy pawamoy added bug Something isn't working and removed unconfirmed This bug was not reproduced yet labels Nov 30, 2024
pawamoy added a commit that referenced this issue Nov 30, 2024
@pawamoy
Copy link
Member

pawamoy commented Nov 30, 2024

Could you try again with Griffe installed from main branch 🙂?

@szymonmaszke
Copy link
Author

Could you try again with Griffe installed from main branch 🙂?

Thank you, now the checkout works correctly. I am not sure about output and breaking changes detection, I will raise a separate issue after a few more tests if necessary though.

Thank you for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants