nikivdev/code · error

git {} failed

Error message

git {} failed

What it means

This error is raised by git_capture_in when a spawned git command (run in a given repo root, capturing stdout) exits with a non-zero status. It means the git subcommand itself failed — e.g. bad arguments, not a git repository, or the queried key/config does not resolve. The message intentionally omits git's stderr because output is captured and not inherited.

Source

Thrown at src/repos.rs:1520

fn git_ref_exists_in(repo_root: &Path, reference: &str) -> bool {
    Command::new("git")
        .current_dir(repo_root)
        .args(["rev-parse", "--verify", reference])
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|status| status.success())
        .unwrap_or(false)
}

fn git_capture_in(repo_root: &Path, args: &[&str]) -> Result<String> {
    let output = Command::new("git")
        .current_dir(repo_root)
        .args(args)
        .output()
        .with_context(|| format!("failed to run git {}", args.join(" ")))?;
    if !output.status.success() {
        bail!("git {} failed", args.join(" "));
    }
    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

fn git_config_get(repo_root: &Path, key: &str) -> Option<String> {
    git_capture_in(repo_root, &["config", "--get", key])
        .ok()
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
}

fn git_run_in(repo_root: &Path, args: &[&str], quiet: bool) -> Result<()> {
    let mut command = Command::new("git");
    command
        .current_dir(repo_root)
        .args(args)
        .stdin(Stdio::inherit());
    if quiet {

View on GitHub (pinned to a747e741ae)

Solutions

  1. Run the same git command manually inside repo_root and read git's stderr to see the real failure
  2. Verify repo_root is a git repository (check for .git or run git rev-parse --is-inside-work-tree)
  3. Check that the queried key/config actually exists (git config --get <key>) before treating absence as a bug
  4. Confirm the installed git version supports the flags being passed

Example fix

// before
let url = git_config_get(repo_root, "remote.origin.url").ok_or_else(|| anyhow!("no remote"))?;
// after
let url = match git_config_get(repo_root, "remote.origin.url") {
    Some(u) => u,
    None => bail!("remote.origin.url is not set in {}", repo_root.display()),
};
Defensive patterns

Strategy: try-catch

Validate before calling

fn ensure_git_repo(root: &Path) -> anyhow::Result<()> {
    anyhow::ensure!(root.join(".git").exists(), "{} is not a git repository", root.display());
    Ok(())
}

Type guard

fn is_git_repo(root: &Path) -> bool {
    root.join(".git").exists()
}

Try / catch

match git_config_get(repo_root, "remote.origin.url") {
    Some(url) => url,
    None => { eprintln!("git config failed in {}", repo_root.display()); fallback() }
}

Prevention

When it happens

Trigger: Calling git_capture_in (or helpers like git_config_get) with args such as ["config","--get",key] when the key is unset, running outside a valid repo (bad repo_root), or passing a subcommand/flag combination the installed git rejects.

Common situations: Querying git config for a key that was never set; repo_root pointing at a non-repo directory (e.g. after a failed clone); older git versions lacking a flag the code passes; detached/empty repos where commands like branch --show-current fail.

Understand the failure class

Background: "git command failed": what it means when a tool shells out to git and git exits non-zero — this error's family across 21 libraries.

Related errors


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