diff --git a/src/cargo/core/resolver/encode.rs b/src/cargo/core/resolver/encode.rs index 7b30c470a6b..07a01ac0c45 100644 --- a/src/cargo/core/resolver/encode.rs +++ b/src/cargo/core/resolver/encode.rs @@ -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}; @@ -659,6 +659,13 @@ pub fn encodable_package_id(id: PackageId, state: &EncodeState<'_>) -> Encodable fn encode_source(id: SourceId) -> Option { 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) } diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index a493a18072c..5a58482ade7 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -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) -> SourceId { SourceId::wrap(SourceIdInner { diff --git a/tests/testsuite/lockfile_compat.rs b/tests/testsuite/lockfile_compat.rs index 9f4b28b8b4d..37c8980f05e 100644 --- a/tests/testsuite/lockfile_compat.rs +++ b/tests/testsuite/lockfile_compat.rs @@ -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();