Skip to content

feat(samoid): implement hook execution runtime (#3)#15

Merged
behrangsa merged 2 commits intomasterfrom
feature/hook-execution-runtime
Jul 28, 2025
Merged

feat(samoid): implement hook execution runtime (#3)#15
behrangsa merged 2 commits intomasterfrom
feature/hook-execution-runtime

Conversation

@behrangsa
Copy link
Contributor

Summary

Implement the hook runner that executes actual hook scripts with proper environment setup, error handling, and debugging support as specified in issue #3.

🎯 Acceptance Criteria Complete

All acceptance criteria from issue #3 have been successfully implemented:

  • ✅ Execute hook scripts from project root directory
  • ✅ Load initialization script from ~/.config/samoid/init.sh
  • ✅ Handle SAMOID=0 environment variable to skip execution
  • ✅ Support SAMOID=2 for debug mode with script tracing
  • ✅ Exit with hook script's exit code
  • ✅ Display informative error messages for failed hooks
  • ✅ Show "command not found" message when PATH is incorrect
  • ✅ Skip execution if hook script file doesn't exist

🏗️ Architecture & Design

Hook Runner Binary

  • New samoid-hook binary that serves as the actual Git hook executor
  • Added to Cargo.toml with dedicated binary configuration
  • Designed to be installed as the Git hook script

Environment Variable Controls

  • SAMOID=0: Skip all hook execution (useful for CI/deployment, rebasing)
  • SAMOID=1: Normal execution mode (default)
  • SAMOID=2: Enable debug mode with detailed script tracing

Dependency Injection Pattern

Follows established codebase patterns for complete test isolation:

  • Environment, CommandRunner, FileSystem trait abstractions
  • Production implementations: SystemEnvironment, SystemCommandRunner, SystemFileSystem
  • Mock implementations with Arc<Mutex<T>> for thread-safe testing

Cross-Platform Support

  • Primary path: ~/.config/samoid/init.sh (using XDG_CONFIG_HOME)
  • Windows fallback: %USERPROFILE%\.config\samoid\init.sh
  • Home directory detection via HOME or USERPROFILE environment variables

🧪 Testing Strategy

Comprehensive Unit Testing

  • 30+ Unit Tests: Cover all execution paths and error scenarios
  • Mock-Based Testing: Complete isolation from system dependencies
  • Error Scenario Coverage: Exit codes, command not found, file system errors
  • Argument Handling: Various hook types and argument passing

Integration Testing

  • Real Hook Execution: Validated with actual shell scripts
  • Environment Mode Testing: Verified skip (SAMOID=0) and debug (SAMOID=2) modes
  • Error Propagation: Confirmed proper exit code handling and error messages

Quality Metrics

Following the codebase's high-quality testing standards established in the existing implementation.

📦 Implementation Details

Hook Execution Flow

  1. Parse SAMOID environment variable for execution mode
  2. Extract hook name from command arguments
  3. Load initialization script from ~/.config/samoid/init.sh (if exists)
  4. Locate hook script at .samoid/_/{hook_name}
  5. Execute hook script with proper environment and arguments
  6. Handle errors and propagate exit codes to Git

Error Handling

  • Graceful Exit: Silent exit when hook script doesn't exist (normal Git behavior)
  • Clear Error Messages: Descriptive failures for script execution errors
  • Command Not Found: Special handling for exit code 127 with PATH information
  • Debug Information: Detailed tracing in debug mode (SAMOID=2)

Files Modified

  • Cargo.toml: Added samoid-hook binary configuration
  • src/hook_runner.rs: Complete hook runner implementation with tests

🔗 Dependencies

📋 Test Plan

  • Unit tests pass with comprehensive coverage
  • Integration tests validate real hook execution
  • Manual testing of all environment modes (SAMOID=0/1/2)
  • Error scenario validation (failed hooks, command not found)
  • Cross-platform path handling verification

🚀 Ready for Review

This implementation provides a robust, well-tested hook execution runtime that forms the core of the samoid Git hooks system. The dependency injection pattern ensures maintainability and testability while supporting all specified requirements.

Closes #3

## Summary
Implement the hook runner that executes actual hook scripts with proper
environment setup, error handling, and debugging support.

## Implementation Details
- **Hook Runner Binary**: New `samoid-hook` binary for executing Git hooks
- **Environment Control**: Support for SAMOID=0/1/2 (skip/normal/debug modes)
- **Init Script Loading**: Automatic loading of `~/.config/samoid/init.sh`
- **Error Handling**: Proper exit code propagation and clear error messages
- **Debug Mode**: Detailed script tracing with SAMOID=2
- **Cross-Platform**: Windows home directory fallback support

## Architecture
The hook runner follows dependency injection pattern for testability:
- Environment, CommandRunner, and FileSystem traits
- Mock implementations for comprehensive unit testing
- Thread-safe mocks using Arc<Mutex<T>>

## Key Features
✅ Execute hook scripts from project root directory
✅ Load initialization script from `~/.config/samoid/init.sh`
✅ Handle `SAMOID=0` environment variable to skip execution
✅ Support `SAMOID=2` for debug mode with script tracing
✅ Exit with hook script's exit code
✅ Display informative error messages for failed hooks
✅ Show "command not found" message when PATH is incorrect
✅ Skip execution if hook script file doesn't exist

## Testing
- 30+ unit tests with dependency injection pattern
- Mock implementations for complete test isolation
- Integration tests with real hook script execution
- Error scenario coverage (exit codes, command not found)

## Acceptance Criteria Met
All acceptance criteria from issue #3 have been implemented and tested.

Closes #3
Auto-formatting and linting fixes applied to samoid source files:
- src/config.rs: Code formatting improvements
- src/hook_runner.rs: Formatting and style consistency
- src/main.rs: Code style improvements
- src/project.rs: Formatting consistency

No functional changes - maintaining code quality standards.
@behrangsa behrangsa merged commit 1e45bae into master Jul 28, 2025
@behrangsa behrangsa deleted the feature/hook-execution-runtime branch July 28, 2025 06:38
behrangsa added a commit that referenced this pull request Jul 31, 2025
* feat(samoid): implement hook execution runtime (#3)

## Summary
Implement the hook runner that executes actual hook scripts with proper
environment setup, error handling, and debugging support.

## Implementation Details
- **Hook Runner Binary**: New `samoid-hook` binary for executing Git hooks
- **Environment Control**: Support for SAMOID=0/1/2 (skip/normal/debug modes)
- **Init Script Loading**: Automatic loading of `~/.config/samoid/init.sh`
- **Error Handling**: Proper exit code propagation and clear error messages
- **Debug Mode**: Detailed script tracing with SAMOID=2
- **Cross-Platform**: Windows home directory fallback support

## Architecture
The hook runner follows dependency injection pattern for testability:
- Environment, CommandRunner, and FileSystem traits
- Mock implementations for comprehensive unit testing
- Thread-safe mocks using Arc<Mutex<T>>

## Key Features
✅ Execute hook scripts from project root directory
✅ Load initialization script from `~/.config/samoid/init.sh`
✅ Handle `SAMOID=0` environment variable to skip execution
✅ Support `SAMOID=2` for debug mode with script tracing
✅ Exit with hook script's exit code
✅ Display informative error messages for failed hooks
✅ Show "command not found" message when PATH is incorrect
✅ Skip execution if hook script file doesn't exist

## Testing
- 30+ unit tests with dependency injection pattern
- Mock implementations for complete test isolation
- Integration tests with real hook script execution
- Error scenario coverage (exit codes, command not found)

## Acceptance Criteria Met
All acceptance criteria from issue #3 have been implemented and tested.

Closes #3

* chore: apply formatting and linting improvements

Auto-formatting and linting fixes applied to samoid source files:
- src/config.rs: Code formatting improvements
- src/hook_runner.rs: Formatting and style consistency
- src/main.rs: Code style improvements
- src/project.rs: Formatting consistency

No functional changes - maintaining code quality standards.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hook Execution Runtime

1 participant