Skip to content

Commit

Permalink
Don't update lock file when not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuzhao committed Mar 26, 2024
1 parent a1aa492 commit a504ac3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion harness-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "harness-cli"
version = "0.0.6"
version = "0.0.7"
description = "Precise and reproducible benchmarking"
repository = "https://github.com/wenyuzhao/harness"
homepage = "https://github.com/wenyuzhao/harness"
Expand Down
13 changes: 13 additions & 0 deletions harness-cli/src/utils/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ pub fn load_lockfiles(crate_info: &CrateInfo, profile: &Profile) -> anyhow::Resu
pub struct TempLockfileGuard {
lockfile_path: std::path::PathBuf,
original_lockfile: String,
changed: bool,
}

impl Drop for TempLockfileGuard {
fn drop(&mut self) {
if !self.changed {
return;
}
std::fs::write(&self.lockfile_path, &self.original_lockfile).unwrap();
}
}
Expand All @@ -67,9 +71,18 @@ pub fn replay_lockfile(run_info: &RunInfo, mut hash: &str) -> anyhow::Result<Tem
.ok_or_else(|| anyhow::anyhow!("Lockfile for commit `{}` not found", hash))?;
let lockfile_path = run_info.crate_info.workspace_root.join("Cargo.lock");
let original_lockfile = std::fs::read_to_string(&lockfile_path)?;
let old_lockfile: toml::Value = toml::from_str(&original_lockfile)?;
if &old_lockfile == lockfile {
return Ok(TempLockfileGuard {
lockfile_path,
original_lockfile,
changed: false,
});
}
std::fs::write(&lockfile_path, toml::to_string(lockfile)?)?;
Ok(TempLockfileGuard {
lockfile_path,
original_lockfile,
changed: true,
})
}

0 comments on commit a504ac3

Please sign in to comment.