Skip to content

Commit

Permalink
fix: hc cache deletion logic
Browse files Browse the repository at this point in the history
The `cache delete` subcommand had a bug in how it was sorting entries to
be deleted or preserved, so that `-s all` would actually choose to keep
all instead of delete all entries. Also updated the per-entry deletion
logic to also clean up the parent dir (corresponding to github owner)
if it was made empty by deleting the child dir.
  • Loading branch information
j-lanson committed Nov 7, 2024
1 parent 0f649fb commit 50ed134
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions hipcheck/src/cache/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl HcRepoCache {
let (to_keep, to_del): (Vec<RepoCacheEntry>, Vec<RepoCacheEntry>) = match scope {
RepoCacheDeleteScope::All => self.entries.drain(0..).partition(|e| match &opt_pat {
Some(pat) => !pat.is_match(e.name.as_str()),
None => true,
None => false,
}),
RepoCacheDeleteScope::Group { sort, invert, n } => {
// First sort entries in-place in self.entries
Expand Down Expand Up @@ -348,8 +348,19 @@ impl HcRepoCache {
}
/// Internal helper that performs the actual dir deletion
fn internal_delete(&mut self, entry: &RepoCacheEntry) -> Result<()> {
let path = pathbuf![self.path.as_path(), entry.parent.as_path(), &entry.name];
// @Todo - probably should have an enum that categorizes entries as in
// `github`, `local`, `unknown` and add as field in `RepoCacheEntry`.
let is_github = entry.parent.as_path().starts_with("github");

let parent_path = pathbuf![self.path.as_path(), entry.parent.as_path()];
let path = pathbuf![&parent_path, &entry.name];
std::fs::remove_dir_all(path)?;

// Clear owner/org dir if deleting this entry from 'github' made it empty
if is_github && parent_path.read_dir()?.next().is_none() {
std::fs::remove_dir_all(parent_path)?;
}

Ok(())
}
/// Internal helper for list functions
Expand Down

0 comments on commit 50ed134

Please sign in to comment.