nikivdev/code · error

{} is not inside a git repository

Error message

{} is not inside a git repository

What it means

When resolving the repository root, the tool runs `git rev-parse --show-toplevel` from the starting directory. If git exits non-zero, the directory is not inside a git work tree, and this error names the offending path.

Source

Thrown at src/pr_preview.rs:1344

    let head_sha = git_rev_parse(repo_root, head_ref)?;
    Ok(ResolvedBase {
        requested: requested.to_string(),
        resolved: head_ref.to_string(),
        merge_base: head_sha,
        compare_label: compare_head_label.to_string(),
        fallback_used: true,
    })
}

fn resolve_git_repo_root(start: &Path) -> Result<PathBuf> {
    let output = Command::new("git")
        .current_dir(start)
        .args(["rev-parse", "--show-toplevel"])
        .output()
        .with_context(|| format!("failed to run git rev-parse in {}", start.display()))?;
    if !output.status.success() {
        bail!("{} is not inside a git repository", start.display());
    }
    Ok(PathBuf::from(
        String::from_utf8_lossy(&output.stdout).trim(),
    ))
}

fn resolve_preview_context(start: &Path) -> Result<PreviewContext> {
    if let Ok(repo_root) = resolve_git_repo_root(start) {
        let head_label = detect_head_label(&repo_root)?;
        return Ok(PreviewContext {
            repo_root: repo_root.clone(),
            review_root: repo_root,
            head_ref: "HEAD".to_string(),
            head_label,
            compare_head_label: "HEAD".to_string(),
            work_tree_root: None,
        });
    }

View on GitHub (pinned to a747e741ae)

Solutions

  1. Run the command from inside a git repository or pass --path pointing at one
  2. Verify the target has a valid .git directory (`git -C <dir> rev-parse --show-toplevel`)
  3. Re-clone the repository if .git is corrupted

Example fix

// before
f pr preview --path /tmp/scratch   # not a git repo
// after
cd ~/repos/api && f pr preview    # inside a git repo
Defensive patterns

Strategy: validation

Validate before calling

let out = std::process::Command::new("git")
    .current_dir(path)
    .args(["rev-parse", "--show-toplevel"])
    .output()?;
if !out.status.success() {
    anyhow::bail!("{} is not a git repo; fix --path", path.display());
}

Type guard

fn is_git_repo(dir: &Path) -> bool {
    std::process::Command::new("git")
        .current_dir(dir)
        .args(["rev-parse", "--show-toplevel"])
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

Try / catch

if let Err(e) = run_pr_preview(cmd) {
    if e.to_string().ends_with("is not inside a git repository") {
        eprintln!("Point --path at a valid git work tree");
    }
}

Prevention

When it happens

Trigger: Running `f pr preview` (with --path or from the cwd) pointing at a directory that is not part of any git repository, or a corrupt .git dir causing rev-parse to fail.

Common situations: Wrong --path pointing at a scratch/exported directory; running from a bare checkout's parent; .git directory deleted or corrupted; running outside any repo.

Related errors


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