on master help sign in
log failures in the git integration (#6441)
commitabef92a9b341209aeae8802d30fc8c1f971a43df authorPascal Kuthe <pascal.kuthe@semimod.de> dateSun 26 Mar 2023, 11:44 (4 years ago) parent685ae23
5 files changed+65−35
Cargo.lock+1 helix-vcs/Cargo.toml+1 helix-vcs/src/git.rs+34−21 helix-vcs/src/git/test.rs+6−6 helix-vcs/src/lib.rs+23−8
Cargo.lock+1
@@ -1220,6 +1220,7 @@ dependencies = [
12201220name = "helix-vcs"
12211221version = "0.6.0"
12221222dependencies = [
1223+ "anyhow",
12231224 "arc-swap",
12241225 "gix",
12251226 "helix-core",
helix-vcs/Cargo.toml+1
@@ -19,6 +19,7 @@ arc-swap = { version = "1.6.0" }
1919
2020gix = { version = "0.41.0", default-features = false , optional = true }
2121imara-diff = "0.1.5"
22+anyhow = "1"
2223
2324log = "0.4"
2425
helix-vcs/src/git.rs+34−21
@@ -1,3 +1,4 @@
1+use anyhow::{bail, Context, Result};
12use arc_swap::ArcSwap;
23use std::path::Path;
34use std::sync::Arc;
@@ -14,7 +15,7 @@ mod test;
1415pub struct Git;
1516
1617impl 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> {
1819 // custom open options
1920 let mut git_open_opts_map = gix::sec::trust::Mapping::<gix::open::Options>::default();
2021
@@ -45,26 +46,31 @@ impl Git {
4546 open_options.ceiling_dirs = vec![ceiling_dir.to_owned()];
4647 }
4748
48− ThreadSafeRepository::discover_with_environment_overrides_opts(
49+ let res = ThreadSafeRepository::discover_with_environment_overrides_opts(
4950 path,
5051 open_options,
5152 git_open_opts_map,
52− )
53− .ok()
53+ )?;
54+
55+ Ok(res)
5456 }
5557}
5658
5759impl 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>> {
5961 debug_assert!(!file.exists() || file.is_file());
6062 debug_assert!(file.is_absolute());
6163
6264 // TODO cache repository lookup
63− let repo = Git::open_repo(file.parent()?, None)?.to_thread_local();
64− let head = repo.head_commit().ok()?;
65+
66+ let repo_dir = file.parent().context("file has no parent directory")?;
67+ let repo = Git::open_repo(repo_dir, None)
68+ .context("failed to open git repo")?
69+ .to_thread_local();
70+ let head = repo.head_commit()?;
6571 let file_oid = find_file_in_commit(&repo, &head, file)?;
6672
67− let file_object = repo.find_object(file_oid).ok()?;
73+ let file_object = repo.find_object(file_oid)?;
6874 let mut data = file_object.detach().data;
6975 // convert LF to CRLF if configured to avoid showing every line as changed
7076 if repo
@@ -87,35 +93,42 @@ impl DiffProvider for Git {
8793 }
8894 data = normalized_file
8995 }
90− Some(data)
96+ Ok(data)
9197 }
9298
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>>>> {
94100 debug_assert!(!file.exists() || file.is_file());
95101 debug_assert!(file.is_absolute());
96− let repo = Git::open_repo(file.parent()?, None)?.to_thread_local();
97− let head_ref = repo.head_ref().ok()?;
98− let head_commit = repo.head_commit().ok()?;
102+ let repo_dir = file.parent().context("file has no parent directory")?;
103+ let repo = Git::open_repo(repo_dir, None)
104+ .context("failed to open git repo")?
105+ .to_thread_local();
106+ let head_ref = repo.head_ref()?;
107+ let head_commit = repo.head_commit()?;
99108
100109 let name = match head_ref {
101110 Some(reference) => reference.name().shorten().to_string(),
102111 None => head_commit.id.to_hex_with_len(8).to_string(),
103112 };
104113
105− Some(Arc::new(ArcSwap::from_pointee(name.into_boxed_str())))
114+ Ok(Arc::new(ArcSwap::from_pointee(name.into_boxed_str())))
106115 }
107116}
108117
109118/// 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> {
111− let repo_dir = repo.work_dir()?;
112− let rel_path = file.strip_prefix(repo_dir).ok()?;
113− let tree = commit.tree().ok()?;
114− let tree_entry = tree.lookup_entry_by_path(rel_path).ok()??;
119+fn find_file_in_commit(repo: &Repository, commit: &Commit, file: &Path) -> Result<ObjectId> {
120+ let repo_dir = repo.work_dir().context("repo has no worktree")?;
121+ let rel_path = file.strip_prefix(repo_dir)?;
122+ let tree = commit.tree()?;
123+ let tree_entry = tree
124+ .lookup_entry_by_path(rel_path)?
125+ .context("file is untracked")?;
115126 match tree_entry.mode() {
116127 // 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())
130+ }
118131 // found a file
119− EntryMode::Blob | EntryMode::BlobExecutable => Some(tree_entry.object_id()),
132+ EntryMode::Blob | EntryMode::BlobExecutable => Ok(tree_entry.object_id()),
120133 }
121134}
helix-vcs/src/git/test.rs+6−6
@@ -54,7 +54,7 @@ fn missing_file() {
5454 let file = temp_git.path().join("file.txt");
5555 File::create(&file).unwrap().write_all(b"foo").unwrap();
5656
57− assert_eq!(Git.get_diff_base(&file), None);
57+ assert!(Git.get_diff_base(&file).is_err());
5858}
5959
6060#[test]
@@ -64,7 +64,7 @@ fn unmodified_file() {
6464 let contents = b"foo".as_slice();
6565 File::create(&file).unwrap().write_all(contents).unwrap();
6666 create_commit(temp_git.path(), true);
67− assert_eq!(Git.get_diff_base(&file), Some(Vec::from(contents)));
67+ assert_eq!(Git.get_diff_base(&file).unwrap(), Vec::from(contents));
6868}
6969
7070#[test]
@@ -76,7 +76,7 @@ fn modified_file() {
7676 create_commit(temp_git.path(), true);
7777 File::create(&file).unwrap().write_all(b"bar").unwrap();
7878
79− assert_eq!(Git.get_diff_base(&file), Some(Vec::from(contents)));
79+ assert_eq!(Git.get_diff_base(&file).unwrap(), Vec::from(contents));
8080}
8181
8282/// Test that `get_file_head` does not return content for a directory.
@@ -95,7 +95,7 @@ fn directory() {
9595
9696 std::fs::remove_dir_all(&dir).unwrap();
9797 File::create(&dir).unwrap().write_all(b"bar").unwrap();
98− assert_eq!(Git.get_diff_base(&dir), None);
98+ assert!(Git.get_diff_base(&dir).is_err());
9999}
100100
101101/// Test that `get_file_head` does not return content for a symlink.
@@ -116,6 +116,6 @@ fn symlink() {
116116 symlink("file.txt", &file_link).unwrap();
117117
118118 create_commit(temp_git.path(), true);
119− assert_eq!(Git.get_diff_base(&file_link), None);
120− assert_eq!(Git.get_diff_base(&file), Some(Vec::from(contents)));
119+ assert!(Git.get_diff_base(&file_link).is_err());
120+ assert_eq!(Git.get_diff_base(&file).unwrap(), Vec::from(contents));
121121}
helix-vcs/src/lib.rs+23−8
@@ -1,3 +1,4 @@
1+use anyhow::{bail, Result};
12use arc_swap::ArcSwap;
23use std::{path::Path, sync::Arc};
34
@@ -18,19 +19,19 @@ pub trait DiffProvider {
1819 /// if this provider is used.
1920 /// The data is returned as raw byte without any decoding or encoding performed
2021 /// to ensure all file encodings are handled correctly.
21− fn get_diff_base(&self, file: &Path) -> Option<Vec<u8>>;
22− fn get_current_head_name(&self, file: &Path) -> Option<Arc<ArcSwap<Box<str>>>>;
22+ fn get_diff_base(&self, file: &Path) -> Result<Vec<u8>>;
23+ fn get_current_head_name(&self, file: &Path) -> Result<Arc<ArcSwap<Box<str>>>>;
2324}
2425
2526#[doc(hidden)]
2627pub struct Dummy;
2728impl DiffProvider for Dummy {
28− fn get_diff_base(&self, _file: &Path) -> Option<Vec<u8>> {
29− None
29+ fn get_diff_base(&self, _file: &Path) -> Result<Vec<u8>> {
30+ bail!("helix was compiled without git support")
3031 }
3132
32− fn get_current_head_name(&self, _file: &Path) -> Option<Arc<ArcSwap<Box<str>>>> {
33− None
33+ fn get_current_head_name(&self, _file: &Path) -> Result<Arc<ArcSwap<Box<str>>>> {
34+ bail!("helix was compiled without git support")
3435 }
3536}
3637
@@ -42,13 +43,27 @@ impl DiffProviderRegistry {
4243 pub fn get_diff_base(&self, file: &Path) -> Option<Vec<u8>> {
4344 self.providers
4445 .iter()
45− .find_map(|provider| provider.get_diff_base(file))
46+ .find_map(|provider| match provider.get_diff_base(file) {
47+ Ok(res) => Some(res),
48+ Err(err) => {
49+ log::error!("{err:#?}");
50+ log::error!("failed to open diff base for {}", file.display());
51+ None
52+ }
53+ })
4654 }
4755
4856 pub fn get_current_head_name(&self, file: &Path) -> Option<Arc<ArcSwap<Box<str>>>> {
4957 self.providers
5058 .iter()
51− .find_map(|provider| provider.get_current_head_name(file))
59+ .find_map(|provider| match provider.get_current_head_name(file) {
60+ Ok(res) => Some(res),
61+ Err(err) => {
62+ log::error!("{err:#?}");
63+ log::error!("failed to obtain current head name for {}", file.display());
64+ None
65+ }
66+ })
5267 }
5368}
5469