-
-
Notifications
You must be signed in to change notification settings - Fork 933
[Question] How to get default branch (HEAD) from remote repository #1118
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
Comments
Solution: def get_commit_hash(git_url, ref_name=None):
if ref_name:
ref_name = "refs/heads/" + ref_name
else:
ref_name = "HEAD"
g = git.cmd.Git()
remote_refs = {}
for ref in g.ls_remote(git_url).split('\n'):
hash_ref_list = ref.split('\t')
remote_refs[hash_ref_list[1]] = hash_ref_list[0]
return remote_refs[ref_name] |
Would be nice to have this as an API. |
Neither of these is an equivalent of |
I'm a bit confused about what you mean about HEAD not known? This code has been working in production for 3 years |
=> No HEAD symlink exists. It needs to be created manually in this case. |
I managed to get it working. For some reason the above git.ls_remote didn't work, but this does: Then the relevant lines in the output looks like this, so the rest rest of the code above works again:
|
Question
Hi,
I have a service which monitors git repos for changes.
I'd like to be able to find out which branch is the default branch without cloning the repo. The way to do this with Git is to check for the HEAD reference, which you can do with one of the following commands:
Attempt
Here is my current code:
I've tried:
origin.refs.HEAD
origin.refs.head
origin.head
The text was updated successfully, but these errors were encountered: