@@ -1,3 +1,4 @@
1+use anyhow::{bail, Context, Result};
1use arc_swap::ArcSwap;
2use arc_swap::ArcSwap;
2use std::path::Path;
3use std::path::Path;
3use std::sync::Arc;
4use std::sync::Arc;
@@ -14,7 +15,7 @@ mod test;
14pub struct Git;
15pub struct Git;
17− fn open_repo(path: &Path, ceiling_dir: Option<&Path>) -> Option<ThreadSafeRepository> {
18+ fn open_repo(path: &Path, ceiling_dir: Option<&Path>) -> Result<ThreadSafeRepository> {
18 // custom open options
19 // custom open options
19 let mut git_open_opts_map = gix::sec::trust::Mapping::<gix::open::Options>::default();
20 let mut git_open_opts_map = gix::sec::trust::Mapping::<gix::open::Options>::default();
@@ -45,26 +46,31 @@ impl Git {
45 open_options.ceiling_dirs = vec![ceiling_dir.to_owned()];
46 open_options.ceiling_dirs = vec![ceiling_dir.to_owned()];
48− ThreadSafeRepository::discover_with_environment_overrides_opts(
49+ let res = ThreadSafeRepository::discover_with_environment_overrides_opts(
50 open_options,
51 open_options,
51 git_open_opts_map,
52 git_open_opts_map,
57impl DiffProvider for Git {
59impl DiffProvider for Git {
58− fn get_diff_base(&self, file: &Path) -> Option<Vec<u8>> {
60+ fn get_diff_base(&self, file: &Path) -> Result<Vec<u8>> {
59 debug_assert!(!file.exists() || file.is_file());
61 debug_assert!(!file.exists() || file.is_file());
60 debug_assert!(file.is_absolute());
62 debug_assert!(file.is_absolute());
62 // TODO cache repository lookup
64 // TODO cache repository lookup
66+ let repo_dir = file.parent().context("file has no parent directory")?;
63− let repo = Git::open_repo(file.parent()?, None)?.to_thread_local();
67+ let repo = Git::open_repo(repo_dir, None)
68+ .context("failed to open git repo")?
64− let head = repo.head_commit().ok()?;
70+ let head = repo.head_commit()?;
65 let file_oid = find_file_in_commit(&repo, &head, file)?;
71 let file_oid = find_file_in_commit(&repo, &head, file)?;
67− let file_object = repo.find_object(file_oid).ok()?;
73+ let file_object = repo.find_object(file_oid)?;
68 let mut data = file_object.detach().data;
74 let mut data = file_object.detach().data;
69 // convert LF to CRLF if configured to avoid showing every line as changed
75 // convert LF to CRLF if configured to avoid showing every line as changed
@@ -87,35 +93,42 @@ impl DiffProvider for Git {
88 data = normalized_file
94 data = normalized_file
90− Some(data)
96+ Ok(data)
93− fn get_current_head_name(&self, file: &Path) -> Option<Arc<ArcSwap<Box<str>>>> {
99+ fn get_current_head_name(&self, file: &Path) -> Result<Arc<ArcSwap<Box<str>>>> {
94 debug_assert!(!file.exists() || file.is_file());
100 debug_assert!(!file.exists() || file.is_file());
95 debug_assert!(file.is_absolute());
101 debug_assert!(file.is_absolute());
102+ let repo_dir = file.parent().context("file has no parent directory")?;
96− let repo = Git::open_repo(file.parent()?, None)?.to_thread_local();
103+ let repo = Git::open_repo(repo_dir, None)
104+ .context("failed to open git repo")?
97− let head_ref = repo.head_ref().ok()?;
106+ let head_ref = repo.head_ref()?;
98− let head_commit = repo.head_commit().ok()?;
107+ let head_commit = repo.head_commit()?;
100 let name = match head_ref {
109 let name = match head_ref {
101 Some(reference) => reference.name().shorten().to_string(),
110 Some(reference) => reference.name().shorten().to_string(),
102 None => head_commit.id.to_hex_with_len(8).to_string(),
111 None => head_commit.id.to_hex_with_len(8).to_string(),
105− Some(Arc::new(ArcSwap::from_pointee(name.into_boxed_str())))
114+ Ok(Arc::new(ArcSwap::from_pointee(name.into_boxed_str())))
109/// Finds the object that contains the contents of a file at a specific commit.
118/// Finds the object that contains the contents of a file at a specific commit.
110−fn find_file_in_commit(repo: &Repository, commit: &Commit, file: &Path) -> Option<ObjectId> {
119+fn find_file_in_commit(repo: &Repository, commit: &Commit, file: &Path) -> Result<ObjectId> {
111− let repo_dir = repo.work_dir()?;
120+ let repo_dir = repo.work_dir().context("repo has no worktree")?;
112− let rel_path = file.strip_prefix(repo_dir).ok()?;
121+ let rel_path = file.strip_prefix(repo_dir)?;
113− let tree = commit.tree().ok()?;
122+ let tree = commit.tree()?;
123+ let tree_entry = tree
114− let tree_entry = tree.lookup_entry_by_path(rel_path).ok()??;
124+ .lookup_entry_by_path(rel_path)?
125+ .context("file is untracked")?;
115 match tree_entry.mode() {
126 match tree_entry.mode() {
116 // not a file, everything is new, do not show diff
127 // not a file, everything is new, do not show diff
117− EntryMode::Tree | EntryMode::Commit | EntryMode::Link => None,
128+ mode @ (EntryMode::Tree | EntryMode::Commit | EntryMode::Link) => {
129+ bail!("entry at {} is not a file but a {mode:?}", file.display())
118 // found a file
131 // found a file
119− EntryMode::Blob | EntryMode::BlobExecutable => Some(tree_entry.object_id()),
132+ EntryMode::Blob | EntryMode::BlobExecutable => Ok(tree_entry.object_id()),