Skip to content

Commit 9cc3064

Browse files
committed
Serialize GitReference::Branch("master") as GitReference::DefaultBranch
Fixes #8468 which was a regression of #8364 .
1 parent 0506d8d commit 9cc3064

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/cargo/core/resolver/encode.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ use serde::de;
9898
use serde::ser;
9999
use serde::{Deserialize, Serialize};
100100

101-
use crate::core::{Dependency, Package, PackageId, SourceId, Workspace};
101+
use crate::core::{Dependency, GitReference, Package, PackageId, SourceId, Workspace};
102102
use crate::util::errors::{CargoResult, CargoResultExt};
103103
use crate::util::interning::InternedString;
104104
use crate::util::{internal, Graph};
@@ -659,6 +659,13 @@ pub fn encodable_package_id(id: PackageId, state: &EncodeState<'_>) -> Encodable
659659
fn encode_source(id: SourceId) -> Option<SourceId> {
660660
if id.is_path() {
661661
None
662+
} else if let Some(git_reference) = id.git_reference() {
663+
match git_reference {
664+
GitReference::Branch(branch) if branch == "master" => {
665+
Some(id.with_default_git_reference())
666+
}
667+
_ => Some(id),
668+
}
662669
} else {
663670
Some(id)
664671
}

src/cargo/core/source/source_id.rs

+9
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ impl SourceId {
322322
}
323323
}
324324

325+
/// Creates a new `SourceId` from this source with Git reference turned into default
326+
pub fn with_default_git_reference(self) -> SourceId {
327+
assert!(self.is_git());
328+
SourceId::wrap(SourceIdInner {
329+
kind: SourceKind::Git(GitReference::DefaultBranch),
330+
..(*self.inner).clone()
331+
})
332+
}
333+
325334
/// Creates a new `SourceId` from this source with the given `precise`.
326335
pub fn with_precise(self, v: Option<String>) -> SourceId {
327336
SourceId::wrap(SourceIdInner {

tests/testsuite/lockfile_compat.rs

+33
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,39 @@ dependencies = [
566566
assert_lockfiles_eq(&lockfile, &lock);
567567
}
568568

569+
// If explicitly specifying the master branch makes no difference
570+
#[cargo_test]
571+
fn master_branch_not_mentioned() {
572+
let git = git::new("bar", |p| {
573+
p.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
574+
.file("src/lib.rs", "")
575+
});
576+
577+
let p = project()
578+
.file(
579+
"Cargo.toml",
580+
&format!(
581+
r#"
582+
[project]
583+
name = "foo"
584+
version = "0.0.1"
585+
authors = []
586+
587+
[dependencies]
588+
bar = {{ git = '{}', branch = "master" }}
589+
"#,
590+
git.url()
591+
),
592+
)
593+
.file("src/lib.rs", "")
594+
.build();
595+
596+
p.cargo("generate-lockfile").run();
597+
598+
let lockfile = p.read_lockfile();
599+
assert!(!lockfile.contains("master"), "master in {}", lockfile);
600+
}
601+
569602
#[cargo_test]
570603
fn v2_path_and_crates_io() {
571604
let cksum010 = Package::new("a", "0.1.0").publish();

0 commit comments

Comments
 (0)