Add branch and tag variants to Git reference (#3374)

## Summary

Closes https://github.com/astral-sh/uv/issues/3368.
This commit is contained in:
Charlie Marsh 2024-05-04 17:13:11 -04:00 committed by GitHub
parent 69e99b3502
commit 2a212eb6a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 333 additions and 17 deletions

View file

@ -25,6 +25,12 @@ const CHECKOUT_READY_LOCK: &str = ".ok";
/// A reference to commit or commit-ish.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GitReference {
/// A specific branch.
Branch(String),
/// A specific tag.
Tag(String),
/// A specific (short) commit.
ShortCommit(String),
/// From a reference that's ambiguously a branch or tag.
BranchOrTag(String),
/// From a reference that's ambiguously a short commit, a branch, or a tag.
@ -46,7 +52,8 @@ enum RefspecStrategy {
}
impl GitReference {
/// Creates a [`GitReference`] from a revision string.
/// Creates a [`GitReference`] from an arbitrary revision string, which could represent a
/// branch, tag, commit, or named ref.
pub(crate) fn from_rev(rev: &str) -> Self {
if rev.starts_with("refs/") {
Self::NamedRef(rev.to_owned())
@ -64,9 +71,12 @@ impl GitReference {
/// Converts the [`GitReference`] to a `str`.
pub fn as_str(&self) -> Option<&str> {
match self {
Self::Tag(rev) => Some(rev),
Self::Branch(rev) => Some(rev),
Self::ShortCommit(rev) => Some(rev),
Self::BranchOrTag(rev) => Some(rev),
Self::FullCommit(rev) => Some(rev),
Self::BranchOrTagOrCommit(rev) => Some(rev),
Self::FullCommit(rev) => Some(rev),
Self::NamedRef(rev) => Some(rev),
Self::DefaultBranch => None,
}
@ -75,10 +85,13 @@ impl GitReference {
/// Converts the [`GitReference`] to a `str` that can be used as a revision.
pub(crate) fn as_rev(&self) -> &str {
match self {
Self::BranchOrTag(rev)
| Self::FullCommit(rev)
| Self::BranchOrTagOrCommit(rev)
| Self::NamedRef(rev) => rev,
Self::Tag(rev) => rev,
Self::Branch(rev) => rev,
Self::ShortCommit(rev) => rev,
Self::BranchOrTag(rev) => rev,
Self::BranchOrTagOrCommit(rev) => rev,
Self::FullCommit(rev) => rev,
Self::NamedRef(rev) => rev,
Self::DefaultBranch => "HEAD",
}
}
@ -86,6 +99,9 @@ impl GitReference {
/// Returns the kind of this reference.
pub(crate) fn kind_str(&self) -> &str {
match self {
Self::Branch(_) => "branch",
Self::Tag(_) => "tag",
Self::ShortCommit(_) => "short commit",
Self::BranchOrTag(_) => "branch or tag",
Self::FullCommit(_) => "commit",
Self::BranchOrTagOrCommit(_) => "branch, tag, or commit",
@ -262,6 +278,26 @@ impl GitReference {
pub(crate) fn resolve(&self, repo: &git2::Repository) -> Result<git2::Oid> {
let refkind = self.kind_str();
let id = match self {
Self::Tag(s) => (|| -> Result<git2::Oid> {
let refname = format!("refs/remotes/origin/tags/{s}");
let id = repo.refname_to_id(&refname)?;
let obj = repo.find_object(id, None)?;
let obj = obj.peel(ObjectType::Commit)?;
Ok(obj.id())
})()
.with_context(|| format!("failed to find tag `{s}`"))?,
Self::Branch(s) => {
let name = format!("origin/{s}");
// Resolve the remote name since that's all we're configuring in
// `fetch` below.
repo.find_branch(&name, git2::BranchType::Remote)
.ok()
.and_then(|b| b.get().target())
.ok_or_else(|| anyhow::format_err!("failed to find {refkind} `{s}`"))?
}
// Attempt to resolve the branch, then the tag.
Self::BranchOrTag(s) => {
let name = format!("origin/{s}");
@ -312,14 +348,14 @@ impl GitReference {
.ok_or_else(|| anyhow::format_err!("failed to find {refkind} `{s}`"))?
}
// We'll be using the HEAD commit
// We'll be using the HEAD commit.
Self::DefaultBranch => {
let head_id = repo.refname_to_id("refs/remotes/origin/HEAD")?;
let head = repo.find_object(head_id, None)?;
head.peel(ObjectType::Commit)?.id()
}
Self::FullCommit(s) | Self::NamedRef(s) => {
Self::FullCommit(s) | Self::ShortCommit(s) | Self::NamedRef(s) => {
let obj = repo.revparse_single(s)?;
match obj.as_tag() {
Some(tag) => tag.target_id(),
@ -949,6 +985,14 @@ pub(crate) fn fetch(
match reference {
// For branches and tags we can fetch simply one reference and copy it
// locally, no need to fetch other branches/tags.
GitReference::Branch(branch) => {
refspecs.push(format!("+refs/heads/{branch}:refs/remotes/origin/{branch}"));
}
GitReference::Tag(tag) => {
refspecs.push(format!("+refs/tags/{tag}:refs/remotes/origin/tags/{tag}"));
}
GitReference::BranchOrTag(branch_or_tag) => {
refspecs.push(format!(
"+refs/heads/{branch_or_tag}:refs/remotes/origin/{branch_or_tag}"
@ -961,7 +1005,8 @@ pub(crate) fn fetch(
// For ambiguous references, we can fetch the exact commit (if known); otherwise,
// we fetch all branches and tags.
GitReference::BranchOrTagOrCommit(branch_or_tag_or_commit) => {
GitReference::ShortCommit(branch_or_tag_or_commit)
| GitReference::BranchOrTagOrCommit(branch_or_tag_or_commit) => {
// The `oid_to_fetch` is the exact commit we want to fetch. But it could be the exact
// commit of a branch or tag. We should only fetch it directly if it's the exact commit
// of a short commit hash.
@ -1329,10 +1374,14 @@ fn github_fast_path(
let local_object = reference.resolve(repo).ok();
let github_branch_name = match reference {
GitReference::Branch(branch) => branch,
GitReference::Tag(tag) => tag,
GitReference::BranchOrTag(branch_or_tag) => branch_or_tag,
GitReference::DefaultBranch => "HEAD",
GitReference::NamedRef(rev) => rev,
GitReference::FullCommit(rev) | GitReference::BranchOrTagOrCommit(rev) => {
GitReference::FullCommit(rev)
| GitReference::ShortCommit(rev)
| GitReference::BranchOrTagOrCommit(rev) => {
// `revparse_single` (used by `resolve`) is the only way to turn
// short hash -> long hash, but it also parses other things,
// like branch and tag names, which might coincidentally be

View file

@ -104,7 +104,10 @@ impl From<GitUrl> for Url {
} else {
// Otherwise, add the branch or tag name.
match git.reference {
GitReference::BranchOrTag(rev)
GitReference::Branch(rev)
| GitReference::Tag(rev)
| GitReference::ShortCommit(rev)
| GitReference::BranchOrTag(rev)
| GitReference::NamedRef(rev)
| GitReference::FullCommit(rev)
| GitReference::BranchOrTagOrCommit(rev) => {