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

Error in `repo.active_branch` if branch is in DETACHED HEAD state #633

Open
cgi1 opened this issue May 29, 2017 · 5 comments
Open

Error in `repo.active_branch` if branch is in DETACHED HEAD state #633

cgi1 opened this issue May 29, 2017 · 5 comments
Labels

Comments

@cgi1
Copy link

@cgi1 cgi1 commented May 29, 2017

Error in repo.active_branch if branch is in DETACHED HEAD state:

{TypeError}HEAD is a detached symbolic reference as it points to '0fa1f2a7d9dd9fb63fa775d2d97ad72f56f1d819'

The branch is None in def _get_ref_info(cls, repo, ref_path): because of

# its a commit
        if repo.re_hexsha_only.match(tokens[0]):
            return (tokens[0], None)

def active_branch(self) could return HEAD-{commit-hash} instead like:

    try:
        active_branch = repo.active_branch
    except:
        active_branch = 'DETACHED_' + repo.head.object.hexsha
@cgi1
Copy link
Author

@cgi1 cgi1 commented May 30, 2017

Looks similar to #629

@Byron
Copy link
Member

@Byron Byron commented Jun 10, 2017

I am not sure it would be right to return a commit instead. The function is called active_branch, and I would assume people expect it to return a branch, never a commit. If HEAD is detached, there is no branch to return.
What do you think?

@cgi1
Copy link
Author

@cgi1 cgi1 commented Jun 13, 2017

Hm good point, I don´t have a pefect answer.. To return no branch seems to be ok.

@Byron
Copy link
Member

@Byron Byron commented Jun 17, 2017

I think returning no branch would break existing code which expects an exception. On the other hand, the exception is not documented, so in a way it's running under the radar anyway.

Maybe it could be adjusted to return None if there is no active branch instead, which would make the calling code a little less cumbersome and safer, as it could start to expect that.

Would you like to contribute a respective PR?

@peterbe
Copy link
Contributor

@peterbe peterbe commented Aug 16, 2019

I stumbled on this too. With submodules.
My solution wasn't to try to rely on a try: except: ... but I made it always check out at least some branch first before it started switching to the branch I ultimately wanted to. I.e. always start with repo.heads[config["master_branch"]].checkout()

akinnane added a commit to alphagov/gds-pre-commit that referenced this issue Mar 31, 2020
When concourse checks out a repo it leaves it in a detached head
state. This was causing errors as python git has slightly undefined
behaviour in this case.

```
File "detect_checker.py", line 125, in _load_repo
    self.parent_branch = self.repo.active_branch
File "/home/a/venv/local/lib/python3.7/site-packages/git/repo/base.py", line 703, in active_branch
    return self.head.reference
File "/home/a/venv/local/lib/python3.7/site-packages/git/refs/symbolic.py", line 272, 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 '6e589de2864ee310a46a343ae9d656405e6b8c84'
```

gitpython-developers/GitPython#633

This PR changes it use the hexsha instead which works correctly with
detached heads and normal branches.

Co-authored-by: detnon <sam.detnon@digital.cabinet-office.gov.uk>
Co-authored-by: Alex Kinnane <17098249+akinnane@users.noreply.github.com>
akinnane added a commit to alphagov/gds-pre-commit that referenced this issue Mar 31, 2020
When concourse checks out a repo it leaves it in a detached head
state. This was causing errors as python git has slightly undefined
behaviour in this case.

```
File "detect_checker.py", line 125, in _load_repo
    self.parent_branch = self.repo.active_branch
File "/home/a/venv/local/lib/python3.7/site-packages/git/repo/base.py", line 703, in active_branch
    return self.head.reference
File "/home/a/venv/local/lib/python3.7/site-packages/git/refs/symbolic.py", line 272, 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 '6e589de2864ee310a46a343ae9d656405e6b8c84'
```

gitpython-developers/GitPython#633

This PR changes it use the hexsha instead which works correctly with
detached heads and normal branches.

Co-authored-by: detnon <sam.detnon@digital.cabinet-office.gov.uk>
Co-authored-by: Alex Kinnane <17098249+akinnane@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.