Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: propagate error from `is_bare()` instead of panicking (#1221)

Ben Drucker78f045146529
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rs
index 7a8e0e0e9..8f02932fa 100644
--- a/src/commands/step_commands.rs
+++ b/src/commands/step_commands.rs
@@ -1073,7 +1073,7 @@ pub fn handle_promote(branch: Option<&str>) -> anyhow::Result<PromoteResult> {
// For normal repos, worktrees[0] is the main worktree
// For bare repos, there's no main worktree - we don't support promote there
- if repo.is_bare() {
+ if repo.is_bare()? {
anyhow::bail!("wt step promote is not supported in bare repositories");
}
diff --git a/src/commands/worktree/resolve.rs b/src/commands/worktree/resolve.rs
index f3a830a19..42a1e41b0 100644
--- a/src/commands/worktree/resolve.rs
+++ b/src/commands/worktree/resolve.rs
@@ -82,7 +82,7 @@ pub fn compute_worktree_path(
) -> anyhow::Result<PathBuf> {
let repo_root = repo.repo_path();
let default_branch = repo.default_branch().unwrap_or_default();
- let is_bare = repo.is_bare();
+ let is_bare = repo.is_bare()?;
// Default branch lives at repo root (main worktree), not a templated path.
// Exception: bare repos have no main worktree, so all branches use templated paths.
diff --git a/src/git/repository/config.rs b/src/git/repository/config.rs
index 9aee9115c..a840b738e 100644
--- a/src/git/repository/config.rs
+++ b/src/git/repository/config.rs
@@ -292,7 +292,7 @@ impl Repository {
// - Empty repos: No branches exist yet, but HEAD tells us the intended default
// - Linked worktrees: HEAD points to CURRENT branch, so skip this heuristic
// - Normal repos: HEAD points to CURRENT branch, so skip this heuristic
- let is_bare = self.is_bare();
+ let is_bare = self.is_bare()?;
let in_linked_worktree = self.current_worktree().is_linked()?;
if ((is_bare && !in_linked_worktree) || branches.is_empty())
&& let Ok(head_ref) = self.run_command(&["symbolic-ref", "HEAD"])
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index fd2913b2f..88151731e 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -415,7 +415,7 @@ impl Repository {
pub fn repo_path(&self) -> &Path {
self.cache.repo_path.get_or_init(|| {
// Bare repos have no worktree — the git directory IS the repo
- if self.is_bare() {
+ if self.is_bare().unwrap_or(false) {
return self.git_common_dir.clone();
}
@@ -446,19 +446,23 @@ impl Repository {
/// Result is cached in the repository's shared cache (same for all clones).
/// Runs `git rev-parse --is-bare-repository` from git_common_dir to correctly
/// detect bare repos even when called from a linked worktree.
- pub fn is_bare(&self) -> bool {
- *self.cache.is_bare.get_or_init(|| {
- // Run from git_common_dir, not discovery_path. This is important for
- // worktrees of bare repos: running from the worktree returns false,
- // but running from the bare repo returns true.
- let output = Cmd::new("git")
- .args(["rev-parse", "--is-bare-repository"])
- .current_dir(&self.git_common_dir)
- .context(path_to_logging_context(&self.git_common_dir))
- .run()
- .expect("git rev-parse failed on valid repo");
- output.status.success() && String::from_utf8_lossy(&output.stdout).trim() == "true"
- })
+ pub fn is_bare(&self) -> anyhow::Result<bool> {
+ self.cache
+ .is_bare
+ .get_or_try_init(|| {
+ // Run from git_common_dir, not discovery_path. This is important for
+ // worktrees of bare repos: running from the worktree returns false,
+ // but running from the bare repo returns true.
+ let output = Cmd::new("git")
+ .args(["rev-parse", "--is-bare-repository"])
+ .current_dir(&self.git_common_dir)
+ .context(path_to_logging_context(&self.git_common_dir))
+ .run()
+ .context("failed to check if repository is bare")?;
+ Ok(output.status.success()
+ && String::from_utf8_lossy(&output.stdout).trim() == "true")
+ })
+ .copied()
}
/// Get the sparse checkout paths for this repository.
diff --git a/src/git/repository/worktrees.rs b/src/git/repository/worktrees.rs
index fb3df1974..c512cbb19 100644
--- a/src/git/repository/worktrees.rs
+++ b/src/git/repository/worktrees.rs
@@ -88,7 +88,7 @@ impl Repository {
/// Used as the default source for `copy-ignored` and the `{{ primary_worktree_path }}` template.
/// Returns `None` for bare repos when no worktree has the default branch.
pub fn primary_worktree(&self) -> anyhow::Result<Option<PathBuf>> {
- if self.is_bare() {
+ if self.is_bare()? {
let Some(branch) = self.default_branch() else {
return Ok(None);
};