gitbutlerapp/gitbutler · warning

another pre-commit hook is already using the repository…

Error message

another pre-commit hook is already using the repository index

What it means

pre_commit_with_tree swaps the repository index while running the pre-commit hook, guarded by a try-lock on a gitbutler-hook-lock file. If another pre-commit hook invocation already holds the lock, it bails with this message instead of corrupting the index.

Solutions

  1. Wait for the in-flight pre-commit hook to finish and retry the operation.
  2. Serialize commit operations in your tooling (one commit at a time per repository).
  3. Remove a leftover gitbutler-hook-lock only after confirming no hook process is running.
  4. If a test harness triggers it, run hook tests sequentially or use separate clones.

Example fix

// before
spawn(commit_a); spawn(commit_b); // race on index
// after
let (tx, rx) = channel(); spawn(commit_a_tx(tx)); spawn(commit_b_tx(tx)); // serialized
Defensive patterns

Strategy: retry

Validate before calling

let lock_path = index_path.with_extension("gitbutler-hook-lock");
if lock_path.exists() {
    // another hook may be active; defer the operation
}

Try / catch

match result {
    Err(e) if e.to_string().contains("another pre-commit hook is already using the repository index") => {
        std::thread::sleep(Duration::from_millis(250));
        // retry with backoff, bounded
    }
    other => other,
}

Prevention

When it happens

Trigger: Invoking pre_commit_with_tree while a concurrent pre-commit hook (same repo) holds the transaction lock — e.g. overlapping commit operations, parallel test runs, or a hook re-entering git from within the hook.

Common situations: IDE auto-commit racing with CLI commit; parallel test suites (see the tests that assert this error) running hooks concurrently; recursive git commit invocations from inside a pre-commit hook.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/581f0c5818cd90a0. Report an issue: GitHub.

Appendix: source

Thrown at crates/gitbutler-repo/src/hooks.rs:105

    // Back up the index file byte for byte; a round-trip through a tree would fail
    // on an index with unmerged entries (a conflict in an uncommitted file) and
    // could not bring those entries back. A sibling file copy keeps memory flat and
    // lets the restore be a single atomic rename that also keeps the permissions.
    let index_path = repo
        .index()?
        .path()
        .context("repository index has no backing file")?
        .to_owned();
    let backup_path = index_path.with_extension("gitbutler-hook-backup");
    let backup_tmp_path = index_path.with_extension("gitbutler-hook-backup.tmp");
    let mut transaction_lock =
        but_core::sync::LockFile::open(index_path.with_extension("gitbutler-hook-lock"))
            .context("failed to open pre-commit index lock")?;
    if !transaction_lock
        .try_lock()
        .context("failed to lock the index for a pre-commit hook")?
    {
        anyhow::bail!("another pre-commit hook is already using the repository index");
    }
    match std::fs::symlink_metadata(&backup_path) {
        Ok(_) => anyhow::bail!(
            "stale pre-commit index backup at '{}'; restore it to '{}' before retrying",
            backup_path.display(),
            index_path.display()
        ),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
        Err(err) => return Err(err).context("failed to inspect pre-commit index backup"),
    }
    match std::fs::remove_file(&backup_tmp_path) {
        Ok(()) => {}
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
        Err(err) => return Err(err).context("failed to remove stale temporary index backup"),
    }
    let had_index = match std::fs::copy(&index_path, &backup_tmp_path) {
        Ok(_) => {
            std::fs::rename(&backup_tmp_path, &backup_path)

View on GitHub (pinned to 58e5313667)