mirror of
https://github.com/kuhyx/scripts.git
synced 2026-07-04 11:43:03 +02:00
Add duplicate code detection to pre-commit hook
Uses jscpd to detect code clones in shell scripts. Blocks commit if duplication exceeds 5% threshold. Suggests extracting common code to scripts/lib/common.sh.
This commit is contained in:
parent
934f83ed43
commit
e4d414b746
@ -51,4 +51,38 @@ if [[ ${#staged_shell_files[@]} -gt 0 ]]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
printf 'shell_check passed. Proceeding with commit.\n'
|
||||
printf 'shell_check passed.\n'
|
||||
|
||||
# Run duplicate code detection
|
||||
printf 'Running duplicate code detection...\n'
|
||||
|
||||
JSCPD_BIN="${HOME}/.local/node_modules/.bin/jscpd"
|
||||
if [[ -x "$JSCPD_BIN" ]]; then
|
||||
# Run jscpd and capture output
|
||||
# Using threshold to fail if duplication exceeds limit
|
||||
# --min-lines 10: minimum 10 lines to consider a clone
|
||||
# --min-tokens 50: minimum 50 tokens to consider a clone
|
||||
# --threshold 5: fail if more than 5% duplication
|
||||
jscpd_output=$("$JSCPD_BIN" \
|
||||
--pattern "**/*.sh" \
|
||||
--min-lines 10 \
|
||||
--min-tokens 50 \
|
||||
--threshold 5 \
|
||||
--reporters "console" \
|
||||
--ignore "**/node_modules/**,**/.git/**" \
|
||||
. 2>&1) || jscpd_exit=$?
|
||||
|
||||
if [[ ${jscpd_exit:-0} -ne 0 ]]; then
|
||||
printf '\n%s\n' "$jscpd_output"
|
||||
printf '\nCommit aborted: code duplication exceeds threshold (5%%).\n' >&2
|
||||
printf 'Consider extracting common code to scripts/lib/common.sh\n' >&2
|
||||
printf 'To see all duplicates: %s --pattern "**/*.sh" --min-lines 5 .\n' "$JSCPD_BIN" >&2
|
||||
exit 1
|
||||
fi
|
||||
printf ' ✓ Duplication check passed (below 5%% threshold)\n'
|
||||
else
|
||||
printf ' ⚠ jscpd not installed, skipping duplication check\n'
|
||||
printf ' Install with: npm install --prefix ~/.local jscpd\n'
|
||||
fi
|
||||
|
||||
printf 'All checks passed. Proceeding with commit.\n'
|
||||
|
||||
Loading…
Reference in New Issue
Block a user