fix crash running outside of git repo

This commit is contained in:
Stephan Dilly 2020-04-03 23:30:33 +02:00
parent 923f7a46d6
commit e51609ed91
4 changed files with 20 additions and 1 deletions

View file

@ -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

View file

@ -14,6 +14,7 @@ pub use crate::{
sync::{
diff::{Diff, DiffLine, DiffLineType},
status::{StatusItem, StatusItemType},
utils::is_repo,
},
};
use std::{

View file

@ -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(

View file

@ -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<Vec<QueueEvent>>,
rx_git: &Receiver<AsyncNotification>,