Skip to content

Commit b3860a4

Browse files
committed
Move infer_registry into a more public spot
1 parent 37812e3 commit b3860a4

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::core::resolver::HasDevUnits;
1414
use crate::core::{Feature, PackageIdSpecQuery, Shell, Verbosity, Workspace};
1515
use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
1616
use crate::ops::lockfile::LOCKFILE_NAME;
17-
use crate::ops::registry::RegistryOrIndex;
17+
use crate::ops::registry::{infer_registry, RegistryOrIndex};
1818
use crate::sources::registry::index::{IndexPackage, RegistryDependency};
1919
use crate::sources::{PathSource, SourceConfigMap, CRATES_IO_REGISTRY};
2020
use crate::util::cache_lock::CacheLockMode;
@@ -174,45 +174,6 @@ fn create_package(
174174
return Ok(dst);
175175
}
176176

177-
/// If this set of packages has an unambiguous publish registry, find it.
178-
pub(crate) fn infer_registry(pkgs: &[&Package]) -> CargoResult<Option<RegistryOrIndex>> {
179-
if pkgs[1..].iter().all(|p| p.publish() == pkgs[0].publish()) {
180-
// If all packages have the same publish settings, we take that as the default.
181-
match pkgs[0].publish().as_deref() {
182-
Some([unique_pkg_reg]) => {
183-
Ok(Some(RegistryOrIndex::Registry(unique_pkg_reg.to_owned())))
184-
}
185-
None | Some([]) => Ok(None),
186-
Some(regs) => {
187-
let mut regs: Vec<_> = regs.iter().map(|s| format!("\"{}\"", s)).collect();
188-
regs.sort();
189-
regs.dedup();
190-
// unwrap: the match block ensures that there's more than one reg.
191-
let (last_reg, regs) = regs.split_last().unwrap();
192-
bail!(
193-
"--registry is required to disambiguate between {} or {} registries",
194-
regs.join(", "),
195-
last_reg
196-
)
197-
}
198-
}
199-
} else {
200-
let common_regs = pkgs
201-
.iter()
202-
// `None` means "all registries", so drop them instead of including them
203-
// in the intersection.
204-
.filter_map(|p| p.publish().as_deref())
205-
.map(|p| p.iter().collect::<HashSet<_>>())
206-
.reduce(|xs, ys| xs.intersection(&ys).cloned().collect())
207-
.unwrap_or_default();
208-
if common_regs.is_empty() {
209-
bail!("conflicts between `package.publish` fields in the selected packages");
210-
} else {
211-
bail!("--registry is required because not all `package.publish` settings agree",);
212-
}
213-
}
214-
}
215-
216177
/// Packages an entire workspace.
217178
///
218179
/// Returns the generated package files. If `opts.list` is true, skips

src/cargo/ops/registry/mod.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use cargo_credential::{Operation, Secret};
1919
use crates_io::Registry;
2020
use url::Url;
2121

22-
use crate::core::{PackageId, SourceId};
22+
use crate::core::{Package, PackageId, SourceId};
2323
use crate::sources::source::Source;
2424
use crate::sources::{RegistrySource, SourceConfigMap};
2525
use crate::util::auth;
@@ -322,3 +322,42 @@ pub(crate) struct RegistrySourceIds {
322322
/// User-defined source replacement is not applied.
323323
pub(crate) replacement: SourceId,
324324
}
325+
326+
/// If this set of packages has an unambiguous publish registry, find it.
327+
pub(crate) fn infer_registry(pkgs: &[&Package]) -> CargoResult<Option<RegistryOrIndex>> {
328+
if pkgs[1..].iter().all(|p| p.publish() == pkgs[0].publish()) {
329+
// If all packages have the same publish settings, we take that as the default.
330+
match pkgs[0].publish().as_deref() {
331+
Some([unique_pkg_reg]) => {
332+
Ok(Some(RegistryOrIndex::Registry(unique_pkg_reg.to_owned())))
333+
}
334+
None | Some([]) => Ok(None),
335+
Some(regs) => {
336+
let mut regs: Vec<_> = regs.iter().map(|s| format!("\"{}\"", s)).collect();
337+
regs.sort();
338+
regs.dedup();
339+
// unwrap: the match block ensures that there's more than one reg.
340+
let (last_reg, regs) = regs.split_last().unwrap();
341+
bail!(
342+
"--registry is required to disambiguate between {} or {} registries",
343+
regs.join(", "),
344+
last_reg
345+
)
346+
}
347+
}
348+
} else {
349+
let common_regs = pkgs
350+
.iter()
351+
// `None` means "all registries", so drop them instead of including them
352+
// in the intersection.
353+
.filter_map(|p| p.publish().as_deref())
354+
.map(|p| p.iter().collect::<HashSet<_>>())
355+
.reduce(|xs, ys| xs.intersection(&ys).cloned().collect())
356+
.unwrap_or_default();
357+
if common_regs.is_empty() {
358+
bail!("conflicts between `package.publish` fields in the selected packages");
359+
} else {
360+
bail!("--registry is required because not all `package.publish` settings agree",);
361+
}
362+
}
363+
}

0 commit comments

Comments
 (0)