{"record":{"id":"581f0c5818cd90a0","repo":"gitbutlerapp/gitbutler","slug":"another-pre-commit-hook-is-already-using-the-repos","errorCode":null,"errorMessage":"another pre-commit hook is already using the repository index","messagePattern":"another pre-commit hook is already using the repository index","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/gitbutler-repo/src/hooks.rs","lineNumber":105,"sourceCode":"    // Back up the index file byte for byte; a round-trip through a tree would fail\n    // on an index with unmerged entries (a conflict in an uncommitted file) and\n    // could not bring those entries back. A sibling file copy keeps memory flat and\n    // lets the restore be a single atomic rename that also keeps the permissions.\n    let index_path = repo\n        .index()?\n        .path()\n        .context(\"repository index has no backing file\")?\n        .to_owned();\n    let backup_path = index_path.with_extension(\"gitbutler-hook-backup\");\n    let backup_tmp_path = index_path.with_extension(\"gitbutler-hook-backup.tmp\");\n    let mut transaction_lock =\n        but_core::sync::LockFile::open(index_path.with_extension(\"gitbutler-hook-lock\"))\n            .context(\"failed to open pre-commit index lock\")?;\n    if !transaction_lock\n        .try_lock()\n        .context(\"failed to lock the index for a pre-commit hook\")?\n    {\n        anyhow::bail!(\"another pre-commit hook is already using the repository index\");\n    }\n    match std::fs::symlink_metadata(&backup_path) {\n        Ok(_) => anyhow::bail!(\n            \"stale pre-commit index backup at '{}'; restore it to '{}' before retrying\",\n            backup_path.display(),\n            index_path.display()\n        ),\n        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}\n        Err(err) => return Err(err).context(\"failed to inspect pre-commit index backup\"),\n    }\n    match std::fs::remove_file(&backup_tmp_path) {\n        Ok(()) => {}\n        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}\n        Err(err) => return Err(err).context(\"failed to remove stale temporary index backup\"),\n    }\n    let had_index = match std::fs::copy(&index_path, &backup_tmp_path) {\n        Ok(_) => {\n            std::fs::rename(&backup_tmp_path, &backup_path)","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/gitbutler-repo/src/hooks.rs#L87-L123","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Wait for the in-flight pre-commit hook to finish and retry the operation.","Serialize commit operations in your tooling (one commit at a time per repository).","Remove a leftover gitbutler-hook-lock only after confirming no hook process is running.","If a test harness triggers it, run hook tests sequentially or use separate clones."],"exampleFix":"// before\nspawn(commit_a); spawn(commit_b); // race on index\n// after\nlet (tx, rx) = channel(); spawn(commit_a_tx(tx)); spawn(commit_b_tx(tx)); // serialized","handlingStrategy":"retry","validationCode":"let lock_path = index_path.with_extension(\"gitbutler-hook-lock\");\nif lock_path.exists() {\n    // another hook may be active; defer the operation\n}","typeGuard":null,"tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"another pre-commit hook is already using the repository index\") => {\n        std::thread::sleep(Duration::from_millis(250));\n        // retry with backoff, bounded\n    }\n    other => other,\n}","preventionTips":["Serialize commit/hook operations per repository.","Avoid spawning parallel commits from IDE + CLI simultaneously.","Never invoke git commit recursively from inside a pre-commit hook."],"tags":["git","hooks","concurrency","lock","rust"],"backgroundTag":"address-already-in-use","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}