Skip to content

Commit 2b8ef40

Browse files
Fix log crashing in subdirectories (#2301)
* Fix log crashing in subdirectories Replace `gix::open` by `gix::discover`. `gix::open` errors when the passed directory is not a git repository. * Add test for AsyncLog in sub-directory * Respect env variables when discovering repo --------- Co-authored-by: extrawurst <776816+extrawurst@users.noreply.github.com>
1 parent 0f5cf89 commit 2b8ef40

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

asyncgit/src/error.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ pub enum Error {
9494
Sign(#[from] crate::sync::sign::SignError),
9595

9696
///
97-
#[error("gix::open error: {0}")]
98-
GixOpen(#[from] Box<gix::open::Error>),
97+
#[error("gix::discover error: {0}")]
98+
GixDiscover(#[from] Box<gix::discover::Error>),
9999

100100
///
101101
#[error("gix::reference::find::existing error: {0}")]
@@ -139,8 +139,8 @@ impl<T> From<crossbeam_channel::SendError<T>> for Error {
139139
}
140140
}
141141

142-
impl From<gix::open::Error> for Error {
143-
fn from(error: gix::open::Error) -> Self {
144-
Self::GixOpen(Box::new(error))
142+
impl From<gix::discover::Error> for Error {
143+
fn from(error: gix::discover::Error) -> Self {
144+
Self::GixDiscover(Box::new(error))
145145
}
146146
}

asyncgit/src/revlog.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ impl AsyncLog {
276276
let mut entries = vec![CommitId::default(); LIMIT_COUNT];
277277
entries.resize(0, CommitId::default());
278278

279-
let mut repo = gix::open(repo_path.gitpath())?;
279+
let mut repo: gix::Repository =
280+
gix::ThreadSafeRepository::discover_with_environment_overrides(repo_path.gitpath())
281+
.map(Into::into)?;
280282
let mut walker =
281283
LogWalkerWithoutFilter::new(&mut repo, LIMIT_COUNT)?;
282284

@@ -321,3 +323,49 @@ impl AsyncLog {
321323
.expect("error sending");
322324
}
323325
}
326+
327+
#[cfg(test)]
328+
mod tests {
329+
use std::sync::atomic::AtomicBool;
330+
use std::sync::{Arc, Mutex};
331+
use std::time::Duration;
332+
333+
use crossbeam_channel::unbounded;
334+
335+
use crate::sync::tests::{debug_cmd_print, repo_init};
336+
use crate::sync::RepoPath;
337+
use crate::AsyncLog;
338+
339+
use super::AsyncLogResult;
340+
341+
#[test]
342+
fn test_smoke_in_subdir() {
343+
let (_td, repo) = repo_init().unwrap();
344+
let root = repo.path().parent().unwrap();
345+
let repo_path: RepoPath =
346+
root.as_os_str().to_str().unwrap().into();
347+
348+
let (tx_git, _rx_git) = unbounded();
349+
350+
debug_cmd_print(&repo_path, "mkdir subdir");
351+
352+
let subdir = repo.path().parent().unwrap().join("subdir");
353+
let subdir_path: RepoPath =
354+
subdir.as_os_str().to_str().unwrap().into();
355+
356+
let arc_current = Arc::new(Mutex::new(AsyncLogResult {
357+
commits: Vec::new(),
358+
duration: Duration::default(),
359+
}));
360+
let arc_background = Arc::new(AtomicBool::new(false));
361+
362+
let result = AsyncLog::fetch_helper_without_filter(
363+
&subdir_path,
364+
&arc_current,
365+
&arc_background,
366+
&tx_git,
367+
);
368+
369+
assert!(result.is_ok());
370+
}
371+
}

asyncgit/src/sync/logwalker.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a> LogWalker<'a> {
113113
///
114114
/// `SharedCommitFilterFn` requires access to a `git2::repo::Repository` because, under the hood,
115115
/// it calls into functions that work with a `git2::repo::Repository`. It seems unwise to open a
116-
/// repo both through `gix::open` and `Repository::open_ext` at the same time, so there is a
116+
/// repo both through `gix::discover` and `Repository::open_ext` at the same time, so there is a
117117
/// separate struct that works with `gix::Repository` only.
118118
///
119119
/// A more long-term option is to refactor filtering to work with a `gix::Repository` and to remove
@@ -270,7 +270,10 @@ mod tests {
270270
stage_add_file(repo_path, file_path).unwrap();
271271
let oid2 = commit(repo_path, "commit2").unwrap();
272272

273-
let mut repo = gix::open(repo_path.gitpath()).unwrap();
273+
let mut repo: gix::Repository =
274+
gix::ThreadSafeRepository::discover_with_environment_overrides(repo_path.gitpath())
275+
.map(Into::into)
276+
.unwrap();
274277
let mut walk = LogWalkerWithoutFilter::new(&mut repo, 100)?;
275278
let mut items = Vec::new();
276279
assert!(matches!(walk.read(&mut items), Ok(2)));

asyncgit/src/sync/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub use utils::{
110110
pub use git2::ResetType;
111111

112112
#[cfg(test)]
113-
mod tests {
113+
pub mod tests {
114114
use super::{
115115
commit,
116116
repository::repo,

0 commit comments

Comments
 (0)