|
| 1 | +# Git-X Architecture Documentation |
| 2 | + |
| 3 | +This document describes the new layered architecture implemented for the git-x CLI tool, designed to improve maintainability, testability, and extensibility. |
| 4 | + |
| 5 | +## Architecture Overview |
| 6 | + |
| 7 | +The codebase is now organized into distinct layers with clear responsibilities: |
| 8 | + |
| 9 | +``` |
| 10 | +┌─────────────────────┐ |
| 11 | +│ CLI Layer │ ← User interaction, argument parsing |
| 12 | +├─────────────────────┤ |
| 13 | +│ Adapter Layer │ ← Bridges CLI and Domain |
| 14 | +├─────────────────────┤ |
| 15 | +│ Domain Layer │ ← Business logic, workflows |
| 16 | +├─────────────────────┤ |
| 17 | +│ Core Layer │ ← Git operations, utilities |
| 18 | +└─────────────────────┘ |
| 19 | +``` |
| 20 | + |
| 21 | +## Layer Descriptions |
| 22 | + |
| 23 | +### 1. Core Layer (`src/core/`) |
| 24 | + |
| 25 | +**Purpose**: Low-level utilities and abstractions for git operations. |
| 26 | + |
| 27 | +**Modules**: |
| 28 | +- `traits.rs` - Common trait abstractions (Command, GitCommand, Destructive, etc.) |
| 29 | +- `git.rs` - Git operation wrappers (GitOperations, BranchOperations, etc.) |
| 30 | +- `output.rs` - Output formatting and buffering utilities |
| 31 | +- `validation.rs` - Input validation with security focus |
| 32 | +- `interactive.rs` - Interactive UI utilities with fuzzy search |
| 33 | +- `safety.rs` - Safety mechanisms for destructive operations |
| 34 | + |
| 35 | +**Responsibilities**: |
| 36 | +- Execute git commands safely |
| 37 | +- Provide consistent error handling |
| 38 | +- Handle user input validation |
| 39 | +- Manage interactive prompts |
| 40 | +- Implement safety checks |
| 41 | + |
| 42 | +### 2. Domain Layer (`src/domain/`) |
| 43 | + |
| 44 | +**Purpose**: Business logic and domain-specific workflows. |
| 45 | + |
| 46 | +**Modules**: |
| 47 | +- `git_repository.rs` - Repository-level operations and state |
| 48 | +- `branch_manager.rs` - Branch lifecycle management |
| 49 | +- `commit_manager.rs` - Commit-related operations |
| 50 | +- `analysis_engine.rs` - Repository analysis and reporting |
| 51 | + |
| 52 | +**Responsibilities**: |
| 53 | +- Implement business rules |
| 54 | +- Coordinate complex workflows |
| 55 | +- Provide type-safe APIs |
| 56 | +- Handle domain-specific validation |
| 57 | +- Manage operation state |
| 58 | + |
| 59 | +**Key Concepts**: |
| 60 | +- **Request/Response DTOs**: Strongly-typed data transfer objects |
| 61 | +- **Domain Services**: High-level operation coordinators |
| 62 | +- **Business Rules**: Domain-specific validation and logic |
| 63 | + |
| 64 | +### 3. Adapter Layer (`src/adapters/`) |
| 65 | + |
| 66 | +**Purpose**: Bridge between CLI and domain layers. |
| 67 | + |
| 68 | +**Modules**: |
| 69 | +- `cli_handlers.rs` - CLI command handlers |
| 70 | +- `formatters.rs` - Output formatting for CLI |
| 71 | + |
| 72 | +**Responsibilities**: |
| 73 | +- Convert CLI arguments to domain requests |
| 74 | +- Handle CLI-specific concerns (interactive vs non-interactive) |
| 75 | +- Format domain responses for CLI output |
| 76 | +- Manage CLI workflow coordination |
| 77 | + |
| 78 | +### 4. Command Layer (`src/commands/`) |
| 79 | + |
| 80 | +**Purpose**: Organized command implementations by functional area. |
| 81 | + |
| 82 | +**Modules**: |
| 83 | +- `branch.rs` - Branch-related commands |
| 84 | +- `commit.rs` - Commit-related commands |
| 85 | +- `repository.rs` - Repository-level commands |
| 86 | +- `analysis.rs` - Analysis and reporting commands |
| 87 | + |
| 88 | +### 5. CLI Layer (`src/cli.rs`) |
| 89 | + |
| 90 | +**Purpose**: Command-line interface definition and argument parsing. |
| 91 | + |
| 92 | +**Responsibilities**: |
| 93 | +- Define CLI structure with clap |
| 94 | +- Parse command-line arguments |
| 95 | +- Route commands to appropriate handlers |
| 96 | +- Handle global CLI concerns |
| 97 | + |
| 98 | +## Design Patterns Used |
| 99 | + |
| 100 | +### 1. Command Pattern |
| 101 | +Commands implement the `Command` trait with standardized execution methods. |
| 102 | + |
| 103 | +```rust |
| 104 | +pub trait Command { |
| 105 | + fn execute(&self) -> Result<String>; |
| 106 | + fn name(&self) -> &'static str; |
| 107 | + fn description(&self) -> &'static str; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### 2. Request/Response Pattern |
| 112 | +Domain operations use typed request/response objects: |
| 113 | + |
| 114 | +```rust |
| 115 | +pub struct CreateBranchRequest { |
| 116 | + pub name: String, |
| 117 | + pub from: Option<String>, |
| 118 | + pub create_backup: bool, |
| 119 | +} |
| 120 | + |
| 121 | +pub struct BranchCreationResult { |
| 122 | + pub branch_name: String, |
| 123 | + pub backup_branch: Option<String>, |
| 124 | + pub switched: bool, |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +### 3. Builder Pattern |
| 129 | +Complex operations use builders for configuration: |
| 130 | + |
| 131 | +```rust |
| 132 | +SafetyBuilder::new("delete branches") |
| 133 | + .with_backup() |
| 134 | + .with_confirmation() |
| 135 | + .execute(|| { /* operation */ }) |
| 136 | +``` |
| 137 | + |
| 138 | +### 4. Factory Pattern |
| 139 | +Handlers are created through factories: |
| 140 | + |
| 141 | +```rust |
| 142 | +let handler = CliHandlerFactory::create_branch_handler()?; |
| 143 | +``` |
| 144 | + |
| 145 | +## Benefits of This Architecture |
| 146 | + |
| 147 | +### 1. **Separation of Concerns** |
| 148 | +- CLI logic separated from business logic |
| 149 | +- Git operations isolated from domain rules |
| 150 | +- Output formatting decoupled from data processing |
| 151 | + |
| 152 | +### 2. **Testability** |
| 153 | +- Each layer can be unit tested independently |
| 154 | +- Domain logic testable without CLI dependencies |
| 155 | +- Mock implementations possible at layer boundaries |
| 156 | + |
| 157 | +### 3. **Maintainability** |
| 158 | +- Changes to git operations don't affect CLI |
| 159 | +- Business logic changes isolated to domain layer |
| 160 | +- Clear responsibility boundaries |
| 161 | + |
| 162 | +### 4. **Type Safety** |
| 163 | +- Strongly-typed request/response objects |
| 164 | +- Compile-time validation of data flow |
| 165 | +- Reduced runtime errors |
| 166 | + |
| 167 | +### 5. **Extensibility** |
| 168 | +- New commands easily added through existing patterns |
| 169 | +- Alternative interfaces (GUI, API) can reuse domain layer |
| 170 | +- Plugin architecture possible |
| 171 | + |
| 172 | +## Migration Strategy |
| 173 | + |
| 174 | +The new architecture coexists with legacy code during transition: |
| 175 | + |
| 176 | +1. **Phase 1**: Core utilities and traits (✅ Complete) |
| 177 | +2. **Phase 2**: Domain layer for key operations (✅ Complete) |
| 178 | +3. **Phase 3**: Adapter layer and CLI handlers (✅ Complete) |
| 179 | +4. **Phase 4**: Migrate existing commands incrementally |
| 180 | +5. **Phase 5**: Remove legacy code |
| 181 | + |
| 182 | +## Usage Examples |
| 183 | + |
| 184 | +### Creating a New Command |
| 185 | + |
| 186 | +1. **Define domain operation** in appropriate manager: |
| 187 | +```rust |
| 188 | +impl BranchManager { |
| 189 | + pub fn create_feature_branch(&self, request: FeatureBranchRequest) -> Result<FeatureBranchResult> { |
| 190 | + // Business logic here |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +2. **Add CLI handler** method: |
| 196 | +```rust |
| 197 | +impl BranchCliHandler { |
| 198 | + pub fn handle_feature_branch(&self, name: String) -> Result<String> { |
| 199 | + let request = FeatureBranchRequest { name }; |
| 200 | + let result = self.branch_manager.create_feature_branch(request)?; |
| 201 | + Ok(self.formatter.format_feature_result(&result)) |
| 202 | + } |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +3. **Add CLI command** definition: |
| 207 | +```rust |
| 208 | +#[derive(Subcommand)] |
| 209 | +pub enum BranchCommands { |
| 210 | + Feature { name: String }, |
| 211 | + // ... other commands |
| 212 | +} |
| 213 | +``` |
| 214 | + |
| 215 | +### Testing Domain Logic |
| 216 | + |
| 217 | +```rust |
| 218 | +#[test] |
| 219 | +fn test_branch_creation_validation() { |
| 220 | + let repo = MockRepository::new(); |
| 221 | + let manager = BranchManager::new(repo); |
| 222 | + |
| 223 | + let request = CreateBranchRequest { |
| 224 | + name: "invalid..name".to_string(), |
| 225 | + from: None, |
| 226 | + create_backup: false, |
| 227 | + }; |
| 228 | + |
| 229 | + let result = manager.create_branch(request); |
| 230 | + assert!(result.is_err()); |
| 231 | +} |
| 232 | +``` |
| 233 | + |
| 234 | +## Code Quality Improvements |
| 235 | + |
| 236 | +### Error Handling |
| 237 | +- Consistent `Result<T>` patterns |
| 238 | +- Domain-specific error types |
| 239 | +- Proper error propagation |
| 240 | + |
| 241 | +### Input Validation |
| 242 | +- Security-focused validation (shell injection prevention) |
| 243 | +- Type-safe validation with clear error messages |
| 244 | +- Validation at appropriate layer boundaries |
| 245 | + |
| 246 | +### Safety Features |
| 247 | +- Destructive operation confirmations |
| 248 | +- Automatic backup creation |
| 249 | +- Working directory state validation |
| 250 | +- Recovery mechanisms |
| 251 | + |
| 252 | +### Performance |
| 253 | +- Output buffering for better performance |
| 254 | +- Optimized git command execution |
| 255 | +- Reduced redundant git calls |
| 256 | + |
| 257 | +## Future Enhancements |
| 258 | + |
| 259 | +The new architecture enables: |
| 260 | + |
| 261 | +1. **Configuration System**: Domain-driven configuration management |
| 262 | +2. **Plugin System**: Extensible command architecture |
| 263 | +3. **API Layer**: REST or GraphQL API using domain layer |
| 264 | +4. **GUI Interface**: Desktop or web interface using domain layer |
| 265 | +5. **Async Operations**: Parallel git operations for better performance |
| 266 | +6. **Caching Layer**: Intelligent caching of git operations |
| 267 | +7. **Event System**: Hook system for operation notifications |
| 268 | + |
| 269 | +## Conclusion |
| 270 | + |
| 271 | +This architecture transformation provides a solid foundation for future development while maintaining backward compatibility. The clear separation of concerns, improved testability, and type safety will significantly improve the long-term maintainability of the git-x project. |
0 commit comments