nikivdev/code · error

git {} failed

Error message

git {} failed

What it means

git_capture runs a git subcommand capturing stdout (used by ensure_repo, ensure_origin_url, default_branch). If the git process exits non-zero, the helper bails with `git <args> failed`. It indicates the queried repository state could not be read (bad ref, missing remote, not a repo, etc.).

Source

Thrown at src/home.rs:699

        .args(["rev-parse", "--verify", "--quiet", reference])
        .current_dir(dest)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .context("failed to run git")?;
    Ok(status.success())
}

fn git_capture(dest: &Path, args: &[&str]) -> Result<String> {
    let output = Command::new("git")
        .args(args)
        .current_dir(dest)
        .stdin(Stdio::null())
        .output()
        .context("failed to run git")?;
    if !output.status.success() {
        bail!("git {} failed", args.join(" "));
    }
    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

fn run_command(cmd: &str, args: &[&str], cwd: Option<&Path>) -> Result<()> {
    let mut command = Command::new(cmd);
    command.args(args);
    if let Some(dir) = cwd {
        command.current_dir(dir);
    }
    let status = command
        .stdin(Stdio::inherit())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status()
        .with_context(|| format!("failed to run {}", cmd))?;
    if !status.success() {
        bail!("{} failed with status {}", cmd, status);

View on GitHub (pinned to a747e741ae)

Solutions

  1. Run the same git command manually in <dest> to see git's real stderr
  2. Add the missing remote/ref the command expects (e.g. `git remote add origin <url>`)
  3. Verify <dest> contains a valid .git directory
  4. Re-clone the repository if .git is corrupt

Example fix

// before
git remote get-url origin   # -> error: no such remote 'origin'
// after
git remote add origin git@github.com:owner/repo.git
git remote get-url origin
Defensive patterns

Strategy: try-catch

Validate before calling

if !dest.join(".git").exists() {
    bail!("{} is not a git repo; git queries will fail", dest.display());
}

Try / catch

match git_capture(dest, &["remote", "get-url", "origin"]) {
    Ok(url) => use(url),
    Err(e) if e.to_string().starts_with("git ") => {
        eprintln!("git query failed in {} — check remotes/refs manually", dest.display());
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: ensure_repo / ensure_origin_url / default_branch call git_capture with args like ["remote","get-url","origin"] or branch queries and the underlying git command returns a non-zero status.

Common situations: Querying a repo with no origin remote configured; asking for a branch that doesn't exist; dest is not actually a git working tree; corrupt .git directory; git not handling an unusual remote config.

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/882fe6a3fe4595d6. Report an issue: GitHub.