Skip to content

Commit

Permalink
fix: revert Cargo.lock changes after running cargo package (#1803)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoIeni authored Oct 27, 2024
1 parent b2fc69a commit f22270c
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions crates/release_plz_core/src/next_ver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,9 @@ impl Updater<'_> {

// Check if files changed in git commit belong to the current package.
// This is required because a package can contain another package in a subdirectory.
let are_changed_files_in_pkg =
|| are_changed_files_in_package(package_path, repository, &current_commit_hash);
let are_changed_files_in_pkg = || {
self.are_changed_files_in_package(package_path, repository, &current_commit_hash)
};

if let Some(registry_package) = registry_package {
debug!(
Expand Down Expand Up @@ -980,7 +981,7 @@ impl Updater<'_> {
info!("{}: the local package has already a different version with respect to the registry package, so release-plz will not update it", package.name);
diff.set_version_unpublished();
break;
} else if are_changed_files_in_pkg() {
} else if are_changed_files_in_pkg()? {
debug!("packages are different");
// At this point of the git history, the two packages are different,
// which means that this commit is not present in the published package.
Expand All @@ -989,7 +990,7 @@ impl Updater<'_> {
current_commit_message.clone(),
));
}
} else if are_changed_files_in_pkg() {
} else if are_changed_files_in_pkg()? {
diff.commits.push(Commit::new(
current_commit_hash,
current_commit_message.clone(),
Expand Down Expand Up @@ -1109,23 +1110,40 @@ impl Updater<'_> {
};
Ok(next_version)
}
}

/// `hash` is only used for logging purposes.
fn are_changed_files_in_package(package_path: &Utf8Path, repository: &Repo, hash: &str) -> bool {
let Ok(package_files) = get_package_files(package_path, repository).inspect_err(|e| {
debug!("failed to get package files at commit {hash}: {e:?}");
}) else {
// `cargo package` can fail if the package doesn't contain a Cargo.toml file yet.
return true;
};
let Ok(changed_files) = repository.files_of_current_commit().inspect_err(|e| {
warn!("failed to get changed files of commit {hash}: {e:?}");
}) else {
// Assume that this commit contains changes to the package.
return true;
};
!package_files.is_disjoint(&changed_files)
/// `hash` is only used for logging purposes.
fn are_changed_files_in_package(
&self,
package_path: &Utf8Path,
repository: &Repo,
hash: &str,
) -> anyhow::Result<bool> {
// We run `cargo package` to get package files, which can edit files, such as `Cargo.lock`.
// Store its path so it can be reverted after comparison.
let cargo_lock_path = self
.get_cargo_lock_path(repository)
.context("failed to determine Cargo.lock path")?;
let package_files_res = get_package_files(package_path, repository);
if let Some(cargo_lock_path) = cargo_lock_path.as_deref() {
// Revert any changes to `Cargo.lock`
repository
.checkout(cargo_lock_path)
.context("cannot revert changes introduced when comparing packages")?;
}
let Ok(package_files) = package_files_res.inspect_err(|e| {
debug!("failed to get package files at commit {hash}: {e:?}");
}) else {
// `cargo package` can fail if the package doesn't contain a Cargo.toml file yet.
return Ok(true);
};
let Ok(changed_files) = repository.files_of_current_commit().inspect_err(|e| {
warn!("failed to get changed files of commit {hash}: {e:?}");
}) else {
// Assume that this commit contains changes to the package.
return Ok(true);
};
Ok(!package_files.is_disjoint(&changed_files))
}
}

/// Get files that belong to the package.
Expand Down

0 comments on commit f22270c

Please sign in to comment.