affaan-m/ECC · error

no staged changes to commit

Error message

no staged changes to commit

What it means

commit_staged checks has_staged_changes before running `git commit` and refuses to proceed when the index matches HEAD, because committing would produce an empty commit. The library surfaces a dedicated error instead of letting git fail with its own 'nothing to commit' message.

Solutions

  1. Stage changes first via stage_path or stage_hunk, then commit.
  2. Refresh the status view before committing so the UI reflects the real index state.
  3. Guard the commit action: only enable when the staged section of the status view is non-empty.

Example fix

// before
commit_staged(&worktree, msg)?;

// after
if !has_staged_changes(&worktree)? {
    // inform user: nothing staged
    return Ok(String::new());
}
commit_staged(&worktree, msg)?;
Defensive patterns

Strategy: validation

Validate before calling

if !has_staged_changes(&worktree)? {
    return Err(anyhow!("nothing staged; stage changes before committing"));
}

Type guard

None

Try / catch

match commit_staged(&worktree, &msg) {
    Ok(hash) => println!("committed {hash}"),
    Err(e) if e.to_string().contains("no staged changes") => {
        show_info("Stage some changes first");
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling commit_staged when nothing has been added to the index: no prior stage_path/stage_hunk calls, all changes previously unstaged or reset, or the worktree is clean.

Common situations: User hits commit before staging anything; stale UI state where staged files were unstaged by another process (e.g. a second window or external git command); race where another commit consumed the staged changes.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of affaan-m/ECC@8321021c54 (2026-09-16). Data as JSON: /api/errors/48b3b4530ab2940f. Report an issue: GitHub.

Appendix: source

Thrown at ecc2/src/worktree/mod.rs:461

                );
            }
            git_apply_patch(
                &worktree.path,
                &["-R", "--index"],
                &hunk.patch,
                "reset selected staged hunk",
            )
        }
    }
}

pub fn commit_staged(worktree: &WorktreeInfo, message: &str) -> Result<String> {
    let message = message.trim();
    if message.is_empty() {
        anyhow::bail!("commit message cannot be empty");
    }
    if !has_staged_changes(worktree)? {
        anyhow::bail!("no staged changes to commit");
    }

    let output = Command::new("git")
        .arg("-C")
        .arg(&worktree.path)
        .args(["commit", "-m", message])
        .output()
        .context("Failed to create commit")?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("git commit failed: {stderr}");
    }

    let rev_parse = Command::new("git")
        .arg("-C")
        .arg(&worktree.path)
        .args(["rev-parse", "--short", "HEAD"])
        .output()

View on GitHub (pinned to 8321021c54)