Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
57 changes: 49 additions & 8 deletions asyncgit/src/sync/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,12 +26,16 @@ pub struct Head {

///
pub fn repo_open_error(repo_path: &RepoPath) -> Option<String> {
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(
Expand Down Expand Up @@ -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,
},
};
Expand All @@ -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");
Expand Down