From ef5fc9a33c2ecc98458932fb80514f4a5e0d056d Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Thu, 25 Jan 2024 11:04:43 +0100 Subject: [PATCH] Log error if worktree fails to relativize git repo path We saw a panic that was caused by the previous `Option.unwrap()`, so this changes the method to return a `Result` and logs the error if possible. Co-authored-by: Antonio --- crates/project/src/worktree.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index eb0d2d5aeeb24f7ff9f20b52f494bd165cc3440c..62203e8b8ae98fccbad455433934ce573a7cf7a2 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -194,12 +194,14 @@ impl AsRef for RepositoryWorkDirectory { pub struct WorkDirectoryEntry(ProjectEntryId); impl WorkDirectoryEntry { - pub(crate) fn relativize(&self, worktree: &Snapshot, path: &Path) -> Option { - worktree.entry_for_id(self.0).and_then(|entry| { - path.strip_prefix(&entry.path) - .ok() - .map(move |path| path.into()) - }) + pub(crate) fn relativize(&self, worktree: &Snapshot, path: &Path) -> Result { + let entry = worktree + .entry_for_id(self.0) + .ok_or_else(|| anyhow!("entry not found"))?; + let path = path + .strip_prefix(&entry.path) + .map_err(|_| anyhow!("could not relativize {:?} against {:?}", path, entry.path))?; + Ok(path.into()) } } @@ -970,13 +972,15 @@ impl LocalWorktree { let mut index_task = None; let snapshot = this.update(&mut cx, |this, _| this.as_local().unwrap().snapshot())?; if let Some(repo) = snapshot.repository_for_path(&path) { - let repo_path = repo.work_directory.relativize(&snapshot, &path).unwrap(); - if let Some(repo) = snapshot.git_repositories.get(&*repo.work_directory) { - let repo = repo.repo_ptr.clone(); - index_task = Some( - cx.background_executor() - .spawn(async move { repo.lock().load_index_text(&repo_path) }), - ); + if let Some(repo_path) = repo.work_directory.relativize(&snapshot, &path).log_err() + { + if let Some(repo) = snapshot.git_repositories.get(&*repo.work_directory) { + let repo = repo.repo_ptr.clone(); + index_task = Some( + cx.background_executor() + .spawn(async move { repo.lock().load_index_text(&repo_path) }), + ); + } } }