diff --git a/README.md b/README.md index a9dff867..7e9bb6b5 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,6 @@ GITUI_LOGGING=true gitui # todo for 0.1 (first release) -* [ ] fix: run in non-git folder -> crash * [ ] (un)staging selected hunks * [ ] publish as homebrew-tap diff --git a/asyncgit/src/lib.rs b/asyncgit/src/lib.rs index c1dd238a..29e28263 100644 --- a/asyncgit/src/lib.rs +++ b/asyncgit/src/lib.rs @@ -14,6 +14,7 @@ pub use crate::{ sync::{ diff::{Diff, DiffLine, DiffLineType}, status::{StatusItem, StatusItemType}, + utils::is_repo, }, }; use std::{ diff --git a/asyncgit/src/sync/utils.rs b/asyncgit/src/sync/utils.rs index d4f87143..4ff8a7a2 100644 --- a/asyncgit/src/sync/utils.rs +++ b/asyncgit/src/sync/utils.rs @@ -4,6 +4,16 @@ use git2::{IndexAddOption, Repository, RepositoryOpenFlags}; use scopetime::scope_time; use std::path::Path; +/// +pub fn is_repo(repo_path: &str) -> bool { + Repository::open_ext( + repo_path, + RepositoryOpenFlags::empty(), + Vec::<&Path>::new(), + ) + .is_ok() +} + /// pub fn repo(repo_path: &str) -> Repository { let repo = Repository::open_ext( diff --git a/src/main.rs b/src/main.rs index d49642d2..5f853862 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,6 +40,11 @@ static TICK_INTERVAL: Duration = Duration::from_secs(5); fn main() -> Result<()> { setup_logging(); + if invalid_path() { + eprintln!("invalid git path"); + return Ok(()); + } + enable_raw_mode()?; io::stdout().execute(EnterAlternateScreen)?; defer! { @@ -87,6 +92,10 @@ fn main() -> Result<()> { Ok(()) } +fn invalid_path() -> bool { + !asyncgit::is_repo(asyncgit::CWD) +} + fn select_event( rx_input: &Receiver>, rx_git: &Receiver,