Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion apps/staged/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,40 @@ fn delete_action_context(
context_id: String,
) -> Result<(), String> {
let store = get_store(&store)?;

// Look up the context before deleting so we can clean up the clone directory.
let context = store
.get_action_context(&context_id)
.map_err(|e| e.to_string())?;

store
.delete_action_context(&context_id)
.map_err(|e| e.to_string())
.map_err(|e| e.to_string())?;

// Clean up the git clone directory if no other action contexts reference the same repo.
if let Some(ctx) = context {
let remaining = store
.count_action_contexts_for_repo(&ctx.github_repo)
.unwrap_or(1);
if remaining == 0 {
if let Some(clone_path) = crate::paths::clone_path_for(&ctx.github_repo) {
if clone_path.exists() {
if let Err(e) = std::fs::remove_dir_all(&clone_path) {
log::warn!(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Serialize clone cleanup with per-repo lock

This cleanup path removes the clone directory directly without taking the repo-level mutex used by git::ensure_local_clone/clone_lock_for_repo in git/github.rs to serialize clone create/delete operations. If delete_action_context runs while another task is cloning or preparing worktree state for the same repo, this can delete the directory after the other path has validated it, causing downstream git commands to fail with missing paths. Move this deletion behind the same lock (or a shared git helper) so clone lifecycle operations are coordinated.

Useful? React with 👍 / 👎.

"Failed to remove clone directory {}: {e}",
clone_path.display()
);
}
}
// Try to remove the parent owner directory if it's now empty.
if let Some(parent) = clone_path.parent() {
let _ = std::fs::remove_dir(parent);
}
}
}
}

Ok(())
}

// =============================================================================
Expand Down
22 changes: 22 additions & 0 deletions apps/staged/src-tauri/src/store/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ impl Store {
rows.collect::<Result<Vec<_>, _>>().map_err(Into::into)
}

pub fn get_action_context(&self, id: &str) -> Result<Option<ActionContext>, StoreError> {
let conn = self.conn.lock().unwrap();
conn.query_row(
"SELECT id, github_repo, subpath, has_detected_actions, detecting_actions, created_at, updated_at
FROM action_contexts WHERE id = ?1",
params![id],
Self::row_to_action_context,
)
.optional()
.map_err(Into::into)
}

pub fn count_action_contexts_for_repo(&self, github_repo: &str) -> Result<i64, StoreError> {
let conn = self.conn.lock().unwrap();
conn.query_row(
"SELECT COUNT(*) FROM action_contexts WHERE github_repo = ?1",
params![github_repo],
|row| row.get(0),
)
.map_err(Into::into)
}

pub fn get_action_context_by_repo_and_subpath(
&self,
github_repo: &str,
Expand Down