From c5cf3afba0cd6bfd2b769cc6e33d3c7418811d7f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 21 Aug 2024 11:34:03 -0400 Subject: [PATCH] Use consistent logic for deserializing short revisions (#6341) ## Summary Closes https://github.com/astral-sh/uv/issues/6336. --- .../uv-distribution/src/metadata/lowering.rs | 10 +- crates/uv/tests/lock.rs | 98 +++++++++++++++++++ 2 files changed, 99 insertions(+), 9 deletions(-) diff --git a/crates/uv-distribution/src/metadata/lowering.rs b/crates/uv-distribution/src/metadata/lowering.rs index dcc9014e8..4a59da473 100644 --- a/crates/uv-distribution/src/metadata/lowering.rs +++ b/crates/uv-distribution/src/metadata/lowering.rs @@ -273,15 +273,7 @@ fn git_source( ) -> Result { let reference = match (rev, tag, branch) { (None, None, None) => GitReference::DefaultBranch, - (Some(rev), None, None) => { - if rev.starts_with("refs/") { - GitReference::NamedRef(rev.clone()) - } else if rev.len() == 40 { - GitReference::FullCommit(rev.clone()) - } else { - GitReference::ShortCommit(rev.clone()) - } - } + (Some(rev), None, None) => GitReference::from_rev(rev), (None, Some(tag), None) => GitReference::Tag(tag), (None, None, Some(branch)) => GitReference::Branch(branch), _ => return Err(LoweringError::MoreThanOneGitRef), diff --git a/crates/uv/tests/lock.rs b/crates/uv/tests/lock.rs index ce941a0b2..5fe0ae477 100644 --- a/crates/uv/tests/lock.rs +++ b/crates/uv/tests/lock.rs @@ -798,6 +798,104 @@ fn lock_sdist_git_pep508() -> Result<()> { Ok(()) } +/// Lock a Git requirement using `tool.uv.sources` with a short revision. +#[test] +#[cfg(feature = "git")] +fn lock_sdist_git_short_rev() -> Result<()> { + let context = TestContext::new("3.12"); + + let pyproject_toml = context.temp_dir.child("pyproject.toml"); + pyproject_toml.write_str( + r#" + [project] + name = "project" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["uv-public-pypackage"] + + [tool.uv.sources] + uv-public-pypackage = { git = "https://github.com/astral-test/uv-public-pypackage", rev = "0dacfd6" } + "#, + )?; + + uv_snapshot!(context.filters(), context.lock(), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 2 packages in [TIME] + "###); + + let lock = fs_err::read_to_string(context.temp_dir.join("uv.lock")).unwrap(); + + insta::with_settings!({ + filters => context.filters(), + }, { + assert_snapshot!( + lock, @r###" + version = 1 + requires-python = ">=3.12" + + [options] + exclude-newer = "2024-03-25T00:00:00Z" + + [[package]] + name = "project" + version = "0.1.0" + source = { editable = "." } + dependencies = [ + { name = "uv-public-pypackage" }, + ] + + [package.metadata] + requires-dist = [{ name = "uv-public-pypackage", git = "https://github.com/astral-test/uv-public-pypackage?rev=0dacfd6" }] + + [[package]] + name = "uv-public-pypackage" + version = "0.1.0" + source = { git = "https://github.com/astral-test/uv-public-pypackage?rev=0dacfd6#0dacfd662c64cb4ceb16e6cf65a157a8b715b979" } + "### + ); + }); + + // Re-run with `--locked`. + uv_snapshot!(context.filters(), context.lock().arg("--locked"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 2 packages in [TIME] + "###); + + // Re-run with `--offline`. We shouldn't need a network connection to validate an + // already-correct lockfile with immutable metadata. + uv_snapshot!(context.filters(), context.lock().arg("--locked").arg("--offline").arg("--no-cache"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 2 packages in [TIME] + "###); + + // Install from the lockfile. + uv_snapshot!(context.filters(), context.sync().arg("--frozen"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Prepared 2 packages in [TIME] + Installed 2 packages in [TIME] + + project==0.1.0 (from file://[TEMP_DIR]/) + + uv-public-pypackage==0.1.0 (from git+https://github.com/astral-test/uv-public-pypackage@0dacfd662c64cb4ceb16e6cf65a157a8b715b979) + "###); + + Ok(()) +} + /// Lock a requirement from a direct URL to a wheel. #[test] fn lock_wheel_url() -> Result<()> {