Skip to content

Commit

Permalink
Serialize GitReference::Branch("master") as GitReference::DefaultBranch
Browse files Browse the repository at this point in the history
Fixes #8468 which was a regression of #8364 .
  • Loading branch information
est31 committed Jul 10, 2020
1 parent 0506d8d commit 9cc3064
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
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

0 comments on commit 9cc3064

Please sign in to comment.