diff --git a/CHANGELOG.md b/CHANGELOG.md index a1ca0eaac2..e455698ddd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * crash when opening submodule ([#2895](https://github.com/gitui-org/gitui/issues/2895)) * when staging the last file in a directory, the first item after the directory is no longer skipped [[@Tillerino](https://github.com/Tillerino)] ([#2748](https://github.com/gitui-org/gitui/issues/2748)) * index-out-of-bounds panic when unstaging lines near the end of a diff ([#2953](https://github.com/gitui-org/gitui/issues/2953)) +* report a clear startup error when the repository has no working tree [[@jlu-lujing](https://github.com/jlu-lujing)] ([#2208](https://github.com/gitui-org/gitui/issues/2208)) ## [0.28.1] - 2026-03-21 diff --git a/asyncgit/src/sync/utils.rs b/asyncgit/src/sync/utils.rs index 148e29baa9..96965e60af 100644 --- a/asyncgit/src/sync/utils.rs +++ b/asyncgit/src/sync/utils.rs @@ -7,7 +7,7 @@ use crate::{ error::{Error, Result}, sync::config::untracked_files_config_repo, }; -use git2::{IndexAddOption, Repository, RepositoryOpenFlags}; +use git2::{IndexAddOption, Repository}; use scopetime::scope_time; use std::{ fs::File, @@ -26,12 +26,16 @@ pub struct Head { /// pub fn repo_open_error(repo_path: &RepoPath) -> Option { - if let Err(e) = Repository::open_ext( - repo_path.gitpath(), - RepositoryOpenFlags::FROM_ENV, - Vec::<&Path>::new(), - ) { - return Some(e.to_string()); + let repo = match repo(repo_path) { + Ok(repo) => repo, + Err(e) => return Some(e.to_string()), + }; + + if repo.workdir().is_none() { + return Some( + "repository has no working tree; run from a checked-out work tree or pass --workdir" + .to_string(), + ); } gix::ThreadSafeRepository::discover_with_environment_overrides( @@ -231,7 +235,7 @@ mod tests { diff::get_diff, status::{get_status, StatusType}, tests::{ - debug_cmd_print, get_statuses, repo_init, + debug_cmd_print, get_statuses, repo_init, repo_init_bare, repo_init_empty, write_commit_file, }, }; @@ -252,6 +256,43 @@ mod tests { assert!(stage_add_file(&repo_path.into(), file_path).is_err()); } + #[test] + fn test_repo_open_error_rejects_no_worktree() { + let (td, _repo) = repo_init_bare().unwrap(); + let repo_path: RepoPath = td.path().to_path_buf().into(); + + assert_eq!( + repo_open_error(&repo_path), + Some( + "repository has no working tree; run from a checked-out work tree or pass --workdir" + .to_string() + ) + ); + } + + #[test] + fn test_repo_open_error_accepts_worktree() -> Result<()> { + let (_td, repo) = repo_init()?; + let repo_path: RepoPath = + repo.workdir().unwrap().to_path_buf().into(); + + assert_eq!(repo_open_error(&repo_path), None); + + Ok(()) + } + + #[test] + fn test_repo_open_error_accepts_explicit_worktree() { + let (git_dir, _repo) = repo_init_bare().unwrap(); + let work_dir = tempfile::TempDir::new().unwrap(); + let repo_path = RepoPath::Workdir { + gitdir: git_dir.path().to_path_buf(), + workdir: work_dir.path().to_path_buf(), + }; + + assert_eq!(repo_open_error(&repo_path), None); + } + #[test] fn test_staging_one_file() { let file_path = Path::new("file1.txt");