Problem
The README states:
The install() call is idempotent. It won't modify the existing setup and hooks.
However, the implementation in src/lib.rs uses fs::write() directly without checking if files already exist:
fs::write(
self._hooks_abs_dir(repo_path.as_str()).join("pre-commit"),
r#"#!/bin/sh
. "$(dirname "$0")/_/sloughi.sh"
echo "Running Sloughi pre-commit..."
"#,
)?;
fs::write() truncates and overwrites existing files, meaning any custom pre-commit hook content is lost on every cargo build.
Expected Behavior
If a hook file already exists, install() should either:
- Skip writing to that file entirely (true idempotent behavior)
- Only update the
_/sloughi.sh helper while preserving user hooks
- Provide an option to force overwrite vs preserve existing
Reproduction
- Add sloughi to build-dependencies with
.custom_path(".githooks")
- Create a custom
.githooks/pre-commit with additional logic (fmt, clippy, etc.)
- Run
cargo build
- Observe that
.githooks/pre-commit is overwritten with the minimal template
Suggested Fix
Check if the file exists before writing:
let pre_commit_path = self._hooks_abs_dir(repo_path.as_str()).join("pre-commit");
if !pre_commit_path.exists() {
fs::write(pre_commit_path, r#"#!/bin/sh..."#)?;
}
Or use OpenOptions with create_new(true) to fail if the file exists.
Problem
The README states:
However, the implementation in
src/lib.rsusesfs::write()directly without checking if files already exist:fs::write()truncates and overwrites existing files, meaning any custom pre-commit hook content is lost on everycargo build.Expected Behavior
If a hook file already exists,
install()should either:_/sloughi.shhelper while preserving user hooksReproduction
.custom_path(".githooks").githooks/pre-commitwith additional logic (fmt, clippy, etc.)cargo build.githooks/pre-commitis overwritten with the minimal templateSuggested Fix
Check if the file exists before writing:
Or use
OpenOptionswithcreate_new(true)to fail if the file exists.