diff --git a/git-branchless-smartlog/src/lib.rs b/git-branchless-smartlog/src/lib.rs index 868b96897..4e5c2c5a7 100644 --- a/git-branchless-smartlog/src/lib.rs +++ b/git-branchless-smartlog/src/lib.rs @@ -758,7 +758,16 @@ pub fn smartlog( reverse, } = options; - let repo = Repo::from_dir(&git_run_info.working_directory)?; + let repo = match Repo::from_dir(&git_run_info.working_directory) { + Ok(repo) => repo, + Err(err) => { + writeln!( + effects.get_error_stream(), + "{err}", + )?; + return Ok(Err(ExitCode(1))); + } + }; let head_info = repo.get_head_info()?; let conn = repo.get_db_conn()?; let event_log_db = EventLogDb::new(&conn)?; diff --git a/git-branchless-smartlog/tests/test_smartlog.rs b/git-branchless-smartlog/tests/test_smartlog.rs index 09aecf7aa..bb4e8f479 100644 --- a/git-branchless-smartlog/tests/test_smartlog.rs +++ b/git-branchless-smartlog/tests/test_smartlog.rs @@ -650,6 +650,24 @@ fn test_smartlog_sparse_main_false_head() -> eyre::Result<()> { Ok(()) } +#[test] +fn test_error_without_panic_on_missing_repo() -> eyre::Result<()> { + let git = make_git()?; + + { + let (_stdout, stderr) = git.branchless_with_options( + "smartlog", + &[], + &GitRunOptions { + expected_exit_code: 1, + ..Default::default() + })?; + insta::assert_snapshot!(stderr, @"could not open repository: could not find repository from ''; class=Repository (6); code=NotFound (-3)"); + } + + Ok(()) +} + #[test] fn test_smartlog_hidden() -> eyre::Result<()> { let git = make_git()?;