{"record":{"id":"39c36c75a108d5a8","repo":"gitbutlerapp/gitbutler","slug":"stale-pre-commit-index-backup-at-restore-it","errorCode":null,"errorMessage":"stale pre-commit index backup at '{}'; restore it to '{}' before retrying","messagePattern":"stale pre-commit index backup at '(.+?)'; restore it to '(.+?)' before retrying","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gitbutler-repo/src/hooks.rs","lineNumber":108,"sourceCode":"    // 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)\n                .context(\"failed to finalize pre-commit index backup\")?;\n            true\n        }","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/gitbutler-repo/src/hooks.rs#L90-L126","documentation":"Before swapping the index, pre_commit_with_tree backs it up; a leftover backup file from a previous crashed/failed hook means the index on disk may be a swapped copy. The API refuses to overwrite the stale backup and asks the user to restore it to the real index before retrying.","triggerScenarios":"Calling pre_commit_with_tree when symlink_metadata(backup_path) succeeds — i.e. the pre-commit index backup file exists from a previous hook run that failed to restore the index (crash, kill, panic mid-hook).","commonSituations":"App or machine crashed during a commit; a hook was killed mid-run; a prior restore failure (pre_commit_propagates_index_restore_failure) left the backup behind.","solutions":["Restore the backup over the index as the message says: copy/move the backup file back to .git/index, then retry.","If the current index is known-good, delete the stale backup file and retry.","Re-run `git reset --mixed` (or `git status` to verify) to re-sync the index with HEAD if state is unclear."],"exampleFix":"// before\n$ but commit ... # error: stale pre-commit index backup\n// after\n$ cp .git/gitbutler-index-backup .git/index  # restore, path per message\n$ rm .git/gitbutler-index-backup\n$ but commit ...","handlingStrategy":"validation","validationCode":"let backup = index_path.with_extension(\"gitbutler-index-backup\");\nif backup.exists() {\n    std::fs::copy(&backup, &index_path)?; // restore first\n    std::fs::remove_file(&backup)?;\n}","typeGuard":"fn has_stale_index_backup(index_path: &Path) -> bool {\n    index_path.with_extension(\"gitbutler-index-backup\").exists()\n}","tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"stale pre-commit index backup\") => {\n        restore_backup_then_retry();\n    }\n    other => other,\n}","preventionTips":["Restore or delete leftover index backups after crashes before committing.","Ensure hooks always restore the index in cleanup paths (drop guards).","Monitor for killed hook processes during long commits."],"tags":["git","hooks","index","stale-state","rust"],"backgroundTag":"internal-invariant-violation","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"}