Skip to content

Commit

Permalink
fix(xtask-bump-check): query by package name to detect changes
Browse files Browse the repository at this point in the history
versions and paths of a workspace members between the original and
a checked-out workspace are different, and shouldn't be included in
hash keys when querying packages.
  • Loading branch information
weihanglo committed Aug 16, 2023
1 parent 93e1aa2 commit 1ee9db3
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions crates/xtask-bump-check/src/xtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! but forgot to bump its version.
//! ```

use std::collections::HashSet;
use std::collections::HashMap;
use std::fmt::Write;
use std::fs;
use std::task;
Expand Down Expand Up @@ -124,9 +124,9 @@ fn bump_check(args: &clap::ArgMatches, config: &mut cargo::util::Config) -> Carg
if let Some(referenced_commit) = referenced_commit.as_ref() {
status(&format!("compare against `{}`", referenced_commit.id()))?;
for referenced_member in checkout_ws(&ws, &repo, referenced_commit)?.members() {
let Some(changed_member) = changed_members.get(referenced_member) else {
let name = referenced_member.name().as_str();
tracing::trace!("skipping {name}, may be removed or not published");
let pkg_name = referenced_member.name().as_str();
let Some(changed_member) = changed_members.get(pkg_name) else {
tracing::trace!("skipping {pkg_name}, may be removed or not published");
continue;
};

Expand Down Expand Up @@ -317,7 +317,7 @@ fn changed<'r, 'ws>(
repo: &'r git2::Repository,
base_commit: &git2::Commit<'r>,
head: &git2::Commit<'r>,
) -> CargoResult<HashSet<&'ws Package>> {
) -> CargoResult<HashMap<&'ws str, &'ws Package>> {
let root_pkg_name = ws.current()?.name(); // `cargo` crate.
let ws_members = ws
.members()
Expand All @@ -334,19 +334,20 @@ fn changed<'r, 'ws>(
let head_tree = head.as_object().peel_to_tree()?;
let diff = repo.diff_tree_to_tree(Some(&base_tree), Some(&head_tree), Default::default())?;

let mut changed_members = HashSet::new();
let mut changed_members = HashMap::new();

for delta in diff.deltas() {
let old = delta.old_file().path().unwrap();
let new = delta.new_file().path().unwrap();
for (ref pkg_root, pkg) in ws_members.iter() {
if old.starts_with(pkg_root) || new.starts_with(pkg_root) {
changed_members.insert(*pkg);
changed_members.insert(pkg.name().as_str(), *pkg);
break;
}
}
}

tracing::trace!("changed_members: {:?}", changed_members.keys());
Ok(changed_members)
}

Expand All @@ -355,7 +356,7 @@ fn changed<'r, 'ws>(
/// Assumption: We always release a version larger than all existing versions.
fn check_crates_io<'a>(
config: &Config,
changed_members: &HashSet<&'a Package>,
changed_members: &HashMap<&'a str, &'a Package>,
needs_bump: &mut Vec<&'a Package>,
) -> CargoResult<()> {
let source_id = SourceId::crates_io(config)?;
Expand All @@ -366,10 +367,10 @@ fn check_crates_io<'a>(
STATUS,
format_args!("compare against `{}`", source_id.display_registry_name()),
)?;
for member in changed_members {
let (name, current) = (member.name(), member.version());
for (name, member) in changed_members {
let current = member.version();
let version_req = format!(">={current}");
let query = Dependency::parse(name, Some(&version_req), source_id)?;
let query = Dependency::parse(*name, Some(&version_req), source_id)?;
let possibilities = loop {
// Exact to avoid returning all for path/git
match registry.query_vec(&query, QueryKind::Exact) {
Expand All @@ -382,6 +383,13 @@ fn check_crates_io<'a>(
if possibilities.is_empty() {
tracing::trace!("dep `{name}` has no version greater than or equal to `{current}`");
} else {
tracing::trace!(
"`{name}@{current}` needs a bump because its should have a version newer than crates.io: {:?}`",
possibilities
.iter()
.map(|s| format!("{}@{}", s.name(), s.version()))
.collect::<Vec<_>>(),
);
needs_bump.push(member);
}
}
Expand Down

0 comments on commit 1ee9db3

Please sign in to comment.