nikivdev/code · error

git {} failed for preview in {}

Error message

git {} failed for preview in {}

What it means

Same failure mode as the general git helper, but for the preview-specific variant that can operate on an alternate work tree root. When the `git <args>` command run for building the preview exits non-zero, the tool bails with the args and the effective root (work_tree_root if provided, otherwise repo_root).

Source

Thrown at src/pr_preview.rs:1571

    if let Some(work_tree_root) = work_tree_root {
        command
            .arg("--git-dir")
            .arg(repo_root.join(".git"))
            .arg("--work-tree")
            .arg(work_tree_root)
            .current_dir(work_tree_root);
    } else {
        command.current_dir(repo_root);
    }
    let output = command.args(args).output().with_context(|| {
        format!(
            "failed to run git {} for preview in {}",
            args.join(" "),
            work_tree_root.unwrap_or(repo_root).display()
        )
    })?;
    if !output.status.success() {
        bail!(
            "git {} failed for preview in {}",
            args.join(" "),
            work_tree_root.unwrap_or(repo_root).display()
        );
    }
    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

fn first_diff_hunk(
    repo_root: &Path,
    work_tree_root: Option<&Path>,
    merge_base: &str,
    relative_path: &str,
) -> Result<Option<String>> {
    let patch = git_capture_for_preview(
        repo_root,
        work_tree_root,
        &[

View on GitHub (pinned to a747e741ae)

Solutions

  1. Re-run the printed `git <args>` in the displayed root to see git's underlying error.
  2. If using a work tree, confirm it is valid: `git worktree list` and re-add with `git worktree add` if missing.
  3. Verify repo_root/work_tree_root both exist and contain .git (or are registered worktrees).
  4. Check the refs and paths passed to the preview git command actually exist.
Defensive patterns

Strategy: validation

Validate before calling

let root = work_tree_root.unwrap_or(repo_root);
if !root.exists() {
    return Err(anyhow!("preview root {} does not exist", root.display()));
}
if !root.join(".git").exists() {
    return Err(anyhow!("preview root {} is not a git work tree", root.display()));
}

Try / catch

match git_capture_for_preview(repo_root, work_tree_root, &args) {
    Ok(out) => out,
    Err(e) => { log::error!("preview git {:?} failed: {e}", args); Err(e) }
}

Prevention

When it happens

Trigger: Preview-time git calls (diff, rev-parse, etc.) failing in the work tree: the work tree path does not exist or is not registered with git (`git worktree` pruned), the repo root fallback is not a repository, or the command's target refs/files are missing.

Common situations: A git worktree was removed from disk but not unregistered, the preview is pointed at a bare or invalid root, renamed/deleted branches referenced by the preview, or pathspec errors in the constructed git args.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/0ebb13e4b51f05c8. Report an issue: GitHub.