Commit ae6ddac
Refactors post-commit hook generation to eliminate code duplication, improve security, and ensure truly silent operation. (#13)
Follow on work from code review
## Changes
- **Factor out `generateHookContent()` helper** - Hook script generation
was duplicated in `init` and `install-hook` commands, risking drift. Now
uses a single shared function.
- **Security: prefer baked path over PATH lookup** - Hook now uses the
absolute path baked at install time first, only falling back to `command
-v roborev` if the baked binary is missing. Prevents PATH injection
attacks from repo-local or malicious binaries.
- **Add stderr redirect for silence** - Hook now redirects stderr
(`2>/dev/null`) so errors from `roborev enqueue --quiet` don't leak to
the terminal.
- **Add comprehensive tests** - `TestGenerateHookContent` verifies:
- Shebang and RoboRev comment present
- Baked path assignment comes before PATH fallback (security)
- Enqueue line has `--quiet`, `2>/dev/null`, and `&` on same line
- Baked path is properly quoted
## Hook before/after
**Before:**
```sh
#!/bin/sh
# RoboRev post-commit hook - auto-reviews every commit
roborev enqueue --quiet &
```
After:
```
#!/bin/sh
# RoboRev post-commit hook - auto-reviews every commit
ROBOREV="/path/to/roborev"
if [ ! -x "$ROBOREV" ]; then
ROBOREV=$(command -v roborev 2>/dev/null) || exit 0
[ ! -x "$ROBOREV" ] && exit 0
fi
"$ROBOREV" enqueue --quiet 2>/dev/null &
```
Test plan
- go test ./... passes
- Run roborev install-hook --force and verify hook content
- Make a commit and verify no output appears
- Test with baked path removed (should fall back to PATH)
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>1 parent 2015e36 commit ae6ddac
2 files changed
Lines changed: 92 additions & 31 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
| 263 | + | |
279 | 264 | | |
280 | 265 | | |
281 | 266 | | |
| |||
758 | 743 | | |
759 | 744 | | |
760 | 745 | | |
761 | | - | |
762 | | - | |
763 | | - | |
764 | | - | |
765 | | - | |
766 | | - | |
767 | | - | |
768 | | - | |
769 | | - | |
770 | | - | |
771 | | - | |
772 | | - | |
773 | | - | |
774 | | - | |
775 | | - | |
| 746 | + | |
776 | 747 | | |
777 | 748 | | |
778 | 749 | | |
| |||
1007 | 978 | | |
1008 | 979 | | |
1009 | 980 | | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
453 | 453 | | |
454 | 454 | | |
455 | 455 | | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
0 commit comments