4

Is there a way that I can get the most recent commit on a remote repository using gitpython? I do not want to perform operations like a pull or merge on my local branch. I also do not want to depend on the master branch on my local to get this information. All I have is a valid repo out there, and I am using repo.remotes.origin.url to get the information.

With just the repo URL, can I get the most recent commit on that repository?

4
  • How about fetch, would that be okay? Commented Jun 23, 2020 at 0:48
  • @matt well, yeah that could work. I am wondering if I can somehow achieve this without having to deal with github token Commented Jun 23, 2020 at 1:08
  • @Shrav did my response below answer your question, or do you want to keep digging? I know it's not ideal, but such is life. Commented Jun 23, 2020 at 21:56
  • @DV82XL Thank you! It did answer my question. However, my use case is a little different as I have to do this on Buildkite without having to mount the ssh creds. I ended up creating a new plugin that sets up the repo before doing the git operations inside a docker container. Commented Jun 25, 2020 at 11:55

2 Answers 2

6

Using gitpython, you can't do this without a local clone. Git is a distributed system, so it's designed for users to operate on their local repos. These answer gives some decent explanations and alternatives:

Using gitpython - requires local repo

You can do a shallow clone (for speed), get latest commit SHA using git rev-parse or git ls-remote, then delete the local repo.

import git
from pathlib import Path

repo_url = 'https://github.com/path/to/your/repo.git'
local_repo_dir = Path('/path/to/your/repo')

# delete the repo if it exists, perform shallow clone, get SHA, delete repo
local_repo_dir.unlink(missing_ok=True)
repo = git.Repo.clone_from(repo_url, local_repo_dir, depth=1)
sha = repo.rev_parse('origin/master')
local_repo_dir.unlink()
print(sha)

Using python subprocess - does not require local repo

This simpler solution uses git ls-remote, which does not require a local clone. The following uses subprocess to get the SHA-1 of the given branch from the remote repo without a local clone. Note that the SHA needs to be extracted from the output response by splitting at the first tab.

import subprocess
import re

repo_url = 'https://github.com/path/to/your/repo.git'
process = subprocess.Popen(["git", "ls-remote", repo_url], stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
sha = re.split(r'\t+', stdout.decode('ascii'))[0]
print(sha)
Sign up to request clarification or add additional context in comments.

2 Comments

git ls-remote doesn't require a local clone, you can give it a remote URL as an argument. I don't think gitpython can deal with it though.
That's right. git ls-remote would have to be called using a subprocess call.
5

You can do it with gitpython without creating a local repository first:

remote_heads = git.cmd.Git().ls_remote(repo_url, heads=True)

1 Comment

Although this is not documented, it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.