nikivdev/code · error

failed to resolve git repo root

Error message

failed to resolve git repo root

What it means

The library shells out to `git rev-parse --show-toplevel` to locate the repository root. If git exits with a non-zero status, it bails with this error because every subsequent operation (staging, diffing, committing) depends on an absolute repo root path. It is typically thrown when the process cwd is not inside a git repository.

Source

Thrown at src/commit.rs:2638

            return cfg.options.commit_with_check_use_repo_root.unwrap_or(true);
        }
    }

    true
}

fn resolve_commit_with_check_root() -> Result<std::path::PathBuf> {
    if !commit_with_check_use_repo_root() {
        return std::env::current_dir().context("failed to get current directory");
    }

    let output = Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .output()
        .context("failed to run git rev-parse --show-toplevel")?;

    if !output.status.success() {
        bail!("failed to resolve git repo root");
    }

    let root = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if root.is_empty() {
        bail!("git repo root was empty");
    }

    Ok(std::path::PathBuf::from(root))
}

const DEFAULT_COMMIT_WITH_CHECK_TIMEOUT_SECS: u64 = 300;
const MAX_COMMIT_WITH_CHECK_TIMEOUT_SECS: u64 = 3600;
const DEFAULT_COMMIT_WITH_CHECK_REVIEW_RETRIES: u32 = 2;
const MAX_COMMIT_WITH_CHECK_REVIEW_RETRIES: u32 = 5;
const DEFAULT_COMMIT_WITH_CHECK_RETRY_BACKOFF_SECS: u64 = 3;

fn commit_with_check_timeout_from_env() -> Option<u64> {
    for key in [

View on GitHub (pinned to a747e741ae)

Solutions

  1. cd into (or point the tool at) a directory inside a valid git repository.
  2. Run `git rev-parse --show-toplevel` yourself to confirm git can find the repo.
  3. If the repo is gone, re-clone or `git init` as appropriate.
  4. Unset stray GIT_DIR / GIT_WORK_TREE environment variables that misdirect git.
Defensive patterns

Strategy: validation

Validate before calling

let out = Command::new("git").args(["rev-parse", "--show-toplevel"]).output()?;
if !out.status.success() {
    return Err("current directory is not inside a git repository".into());
}

Prevention

When it happens

Trigger: Calling any commit/queue API from a directory outside a git work tree; running in a directory whose .git is corrupted; invoking from a subdirectory of a repo that was deleted mid-run.

Common situations: Running the tool from $HOME or /tmp instead of the project; a missing or renamed .git directory; CI checkouts done with depth settings that skip .git; GIT_DIR/GIT_WORK_TREE env overrides pointing nowhere.

Related errors


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