Skip to content

Serialize GitReference::Branch("master") as GitReference::DefaultBranch #8475

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/cargo/core/resolver/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ use serde::de;
use serde::ser;
use serde::{Deserialize, Serialize};

use crate::core::{Dependency, Package, PackageId, SourceId, Workspace};
use crate::core::{Dependency, GitReference, Package, PackageId, SourceId, Workspace};
use crate::util::errors::{CargoResult, CargoResultExt};
use crate::util::interning::InternedString;
use crate::util::{internal, Graph};
Expand Down Expand Up @@ -659,6 +659,13 @@ pub fn encodable_package_id(id: PackageId, state: &EncodeState<'_>) -> Encodable
fn encode_source(id: SourceId) -> Option<SourceId> {
if id.is_path() {
None
} else if let Some(git_reference) = id.git_reference() {
match git_reference {
GitReference::Branch(branch) if branch == "master" => {
Some(id.with_default_git_reference())
}
_ => Some(id),
}
} else {
Some(id)
}
Expand Down
9 changes: 9 additions & 0 deletions src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ impl SourceId {
}
}

/// Creates a new `SourceId` from this source with Git reference turned into default
pub fn with_default_git_reference(self) -> SourceId {
assert!(self.is_git());
SourceId::wrap(SourceIdInner {
kind: SourceKind::Git(GitReference::DefaultBranch),
..(*self.inner).clone()
})
}

/// Creates a new `SourceId` from this source with the given `precise`.
pub fn with_precise(self, v: Option<String>) -> SourceId {
SourceId::wrap(SourceIdInner {
Expand Down
33 changes: 33 additions & 0 deletions tests/testsuite/lockfile_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,39 @@ dependencies = [
assert_lockfiles_eq(&lockfile, &lock);
}

// If explicitly specifying the master branch makes no difference
#[cargo_test]
fn master_branch_not_mentioned() {
let git = git::new("bar", |p| {
p.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
.file("src/lib.rs", "")
});

let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []

[dependencies]
bar = {{ git = '{}', branch = "master" }}
"#,
git.url()
),
)
.file("src/lib.rs", "")
.build();

p.cargo("generate-lockfile").run();

let lockfile = p.read_lockfile();
assert!(!lockfile.contains("master"), "master in {}", lockfile);
}

#[cargo_test]
fn v2_path_and_crates_io() {
let cksum010 = Package::new("a", "0.1.0").publish();
Expand Down