Fix unwrap in git2 library causing panics in Zed

This commit is contained in:
Mikayla Maki 2023-03-16 13:04:13 -07:00
parent c3325430ca
commit 6571555c4d

View File

@ -2,7 +2,7 @@ use anyhow::Result;
use collections::HashMap; use collections::HashMap;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{ use std::{
path::{Path, PathBuf}, path::{Component, Path, PathBuf},
sync::Arc, sync::Arc,
}; };
@ -27,7 +27,11 @@ impl GitRepository for LibGitRepository {
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> { fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
const STAGE_NORMAL: i32 = 0; const STAGE_NORMAL: i32 = 0;
let index = repo.index()?; let index = repo.index()?;
let oid = match index.get_path(relative_file_path, STAGE_NORMAL) {
// This check is required because index.get_path() unwraps internally :(
check_path_to_repo_path_errors(relative_file_path)?;
let oid = match index.get_path(&relative_file_path, STAGE_NORMAL) {
Some(entry) => entry.id, Some(entry) => entry.id,
None => return Ok(None), None => return Ok(None),
}; };
@ -69,3 +73,32 @@ impl GitRepository for FakeGitRepository {
state.index_contents.get(path).cloned() state.index_contents.get(path).cloned()
} }
} }
fn check_path_to_repo_path_errors(relative_file_path: &Path) -> Result<()> {
match relative_file_path.components().next() {
None => anyhow::bail!("repo path should not be empty"),
Some(Component::Prefix(_)) => anyhow::bail!(
"repo path `{}` should be relative, not a windows prefix",
relative_file_path.to_string_lossy()
),
Some(Component::RootDir) => {
anyhow::bail!(
"repo path `{}` should be relative",
relative_file_path.to_string_lossy()
)
}
Some(Component::CurDir) => {
anyhow::bail!(
"repo path `{}` should not start with `.`",
relative_file_path.to_string_lossy()
)
}
Some(Component::ParentDir) => {
anyhow::bail!(
"repo path `{}` should not start with `..`",
relative_file_path.to_string_lossy()
)
}
_ => Ok(()),
}
}