Skip to content

[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

Closed
rubenwardy opened this issue Jan 29, 2021 · 6 comments
Closed

[Question] How to get default branch (HEAD) from remote repository #1118

rubenwardy opened this issue Jan 29, 2021 · 6 comments

Comments

@rubenwardy
Copy link

rubenwardy commented Jan 29, 2021

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:

git ls-remote --symref origin HEAD
git remote show origin | grep "HEAD branch" | cut -d ":" -f 2

Attempt

Here is my current code:

def get_commit_hash(git_url, ref_name=None):
	assert ref_name != ""

	git_dir = os.path.join(tempfile.gettempdir(), randomString(10))

	repo = git.Repo.init(git_dir)
	origin: git.Remote = repo.create_remote("origin", url=git_url)
	assert origin.exists()
	origin.fetch()

	if ref_name:
		ref: git.Reference = origin.refs[ref_name]
	else:
		# How to get default branch here?
		ref: git.Reference = None

	return ref.commit.hexsha

I've tried:

  • origin.refs.HEAD
  • origin.refs.head
  • origin.head
@rubenwardy
Copy link
Author

rubenwardy commented Jan 29, 2021

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]

@rubenwardy rubenwardy changed the title [Question] How to get HEAD reference from remote repository [Question] How to get default branch (HEAD) from remote repository Jan 29, 2021
@flying-sheep
Copy link

Would be nice to have this as an API.

@agowa
Copy link

agowa commented Oct 8, 2024

Neither of these is an equivalent of git remote set-head origin -a and therefore these will only work if it a HEAD is already known...

@rubenwardy
Copy link
Author

I'm a bit confused about what you mean about HEAD not known? This code has been working in production for 3 years

@agowa
Copy link

agowa commented Oct 8, 2024

@rubenwardy

  1. Create a bare repository (or use an existing repo but a new and different remote for the rest)
  2. Add a remote
  3. fetch the remote

=> No HEAD symlink exists. It needs to be created manually in this case. git remote set-head origin -a would do this. also git remote show origin shows it. But it does appear that there is no gitPython command for this. But the above code doesn't work in this case.

@agowa
Copy link

agowa commented Oct 8, 2024

I managed to get it working. For some reason the above git.ls_remote didn't work, but this does: repo.git.ls_remote("--symref","origin"), maybe there is something strange with the remote repository or server?

Then the relevant lines in the output looks like this, so the rest rest of the code above works again:

ref: refs/heads/master\tHEAD\n
adc83b19e793491b1c6ea0fd8b46cd9f32e592fc\tHEAD\n

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

3 participants