Skip to content

Exception on repo().active_branch.name #1479

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

Closed
grumpyp opened this issue Aug 19, 2022 · 4 comments
Closed

Exception on repo().active_branch.name #1479

grumpyp opened this issue Aug 19, 2022 · 4 comments

Comments

@grumpyp
Copy link

grumpyp commented Aug 19, 2022

Setup

Running on GitPython==3.1.27

MacOS monterey, Python 3.10.5

Reproduce the bug

git checkout <commit> -> leads to a detached HEAD

from pathlib import Path
from git import Repo

def repo() -> 'Repo':
    from git import Repo  # type: ignore
    return Repo(Path(__file__).parent, search_parent_directories=True)

repository = repo()
repository.active_branch.name    

Error log

❯ python3 test.py
Traceback (most recent call last):
  File "/test.py", line 9, in <module>
    repository.active_branch.name
  File "/venv/lib/python3.10/site-packages/git/repo/base.py", line 797, in active_branch
    return self.head.reference
  File "/venv/lib/python3.10/site-packages/git/refs/symbolic.py", line 294, in _get_reference
    raise TypeError("%s is a detached symbolic reference as it points to %r" % (self, sha))
TypeError: HEAD is a detached symbolic reference as it points to '7sfaslkhfuhajkfnjaksdhfc4ede32c'

visual proof

image

@Byron
Copy link
Member

Byron commented Aug 20, 2022

This is intended behaviour, despite not being 'nice'.

The exception is not just any exception, but it states exactly why there is no active branch:

TypeError: HEAD is a detached symbolic reference as it points to '7sfaslkhfuhajkfnjaksdhfc4ede32c'

At this point, there is no way to fix this property, but in case you would like to add a new one, like try_active_branch or something that returns None instead, PRs are always welcome.

@Byron Byron closed this as not planned Won't fix, can't repro, duplicate, stale Aug 20, 2022
@grumpyp
Copy link
Author

grumpyp commented Aug 20, 2022

Hi @Byron, probably a fix in the documentation would help users as well.

I was looking for an error in my code for a while until I noticed that it's not my fault.

Thanks.

@Byron
Copy link
Member

Byron commented Aug 20, 2022

Definitely, if exceptions are used to communicate errors they should be documented, and a PR for that is welcome as well.

@grumpyp grumpyp mentioned this issue Aug 20, 2022
@grumpyp
Copy link
Author

grumpyp commented Aug 20, 2022

@Byron opened a PR here

For everyone else:

You can prevent the execution of the Exception by doing something like this:

if repo().head.is_detached:
   print("foo")

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

No branches or pull requests

2 participants