37 feat add remove hook command#38
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors AL2DBML CLI git pre-commit hook management by extracting hook logic into a dedicated service, centralizing hook marker constants, and adding a CLI command to remove the AL2DBML hook section.
Changes:
- Introduced
HookServiceto write/update and remove the AL2DBML section in.git/hooks/pre-commit. - Added
remove-hookCLI command to remove the AL2DBML hook section. - Centralized hook marker strings into
HookMarkers.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/AL2DBML.CLI/Services/HookService.cs | Adds shared logic for writing/removing the AL2DBML pre-commit hook section. |
| src/AL2DBML.CLI/Commands/RemoveHookCommand.cs | New CLI command to remove the AL2DBML hook section. |
| src/AL2DBML.CLI/Constants/HookMarkers.cs | Centralizes start/end marker strings for hook sections. |
| src/AL2DBML.CLI/Commands/InitCommand.cs | Updates init flow to use HookService and the shared markers. |
| src/AL2DBML.CLI/Program.cs | Registers the new remove-hook command with the CLI app configuration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| app.Configure(config => | ||
| { | ||
| config.AddCommand<GenerateCommand>("generate"); | ||
| config.AddCommand<InitCommand>("init"); | ||
| config.AddCommand<RemoveHookCommand>("remove-hook"); | ||
| }); |
There was a problem hiding this comment.
RemoveHookCommand is added to the CLI config, but it isn’t registered in the DI container (unlike GenerateCommand/InitCommand). With the custom TypeRegistrar/TypeResolver, this can make command resolution fail at runtime. Register RemoveHookCommand in the ServiceCollection (e.g., .AddScoped<RemoveHookCommand>()) or adjust the registrar to construct unregistered command types.
| AnsiConsole.MarkupLine("[yellow]Removing existing pre-commit hook...[/]"); | ||
| HookService.Remove(); |
There was a problem hiding this comment.
The prompt asks "Create a pre-commit hook?", but when the user answers "No" and an AL2DBML hook already exists, the code removes the existing hook section. That’s a surprising/implicit behavior change (previously "No" would typically mean "leave as-is"). Consider either (a) changing the prompt text to reflect removal, or (b) adding a separate confirmation for removal / making "No" a no-op.
| AnsiConsole.MarkupLine("[yellow]Removing existing pre-commit hook...[/]"); | |
| HookService.Remove(); | |
| var removeHook = AnsiConsole.Confirm( | |
| "An existing AL2DBML pre-commit hook was found. Do you want to remove it?", | |
| false); | |
| if (removeHook) | |
| { | |
| AnsiConsole.MarkupLine("[yellow]Removing existing pre-commit hook...[/]"); | |
| HookService.Remove(); | |
| } |
| content = File.ReadAllText(HookPath); | ||
| var startIdx = content.IndexOf(HookMarkers.Start, StringComparison.Ordinal); | ||
| var endIdx = content.IndexOf(HookMarkers.End, StringComparison.Ordinal); | ||
|
|
||
| if (startIdx >= 0 && endIdx >= 0) | ||
| content = content[..startIdx] + hookSection + content[(endIdx + HookMarkers.End.Length)..]; | ||
| else |
There was a problem hiding this comment.
When replacing an existing hook section, the code only checks that both markers exist. If the end marker appears before the start marker (or the file contains multiple marker pairs), the slice math can remove/replace the wrong region. Consider validating endIdx > startIdx and (optionally) searching for the end marker starting from startIdx to ensure you match the correct pair.
| var content = File.ReadAllText(HookPath); | ||
| var startIdx = content.IndexOf(HookMarkers.Start, StringComparison.Ordinal); | ||
| var endIdx = content.IndexOf(HookMarkers.End, StringComparison.Ordinal); | ||
| if (startIdx < 0 || endIdx < 0) return false; | ||
|
|
||
| var before = content[..startIdx].TrimEnd(); | ||
| var after = content[(endIdx + HookMarkers.End.Length)..].TrimStart('\r', '\n'); |
There was a problem hiding this comment.
Remove() removes the region using the first occurrences of the start/end markers without validating ordering. If the end marker occurs before the start marker, after will be computed from the wrong position and the resulting file content will be corrupted. Validate endIdx > startIdx (and ideally find the end marker after the start marker).
| protected override int Execute(CommandContext context, RemoveHookSettings settings, CancellationToken cancellationToken) | ||
| { | ||
| if (!File.Exists(".git/hooks/pre-commit")) |
There was a problem hiding this comment.
The hook path string is duplicated here even though HookService already centralizes it. This increases the risk of future drift (e.g., if the hook path changes). Consider exposing the hook path via a constant/property in HookService (or another constants class) and reusing it here.
| protected override int Execute(CommandContext context, RemoveHookSettings settings, CancellationToken cancellationToken) | |
| { | |
| if (!File.Exists(".git/hooks/pre-commit")) | |
| private const string HookPath = ".git/hooks/pre-commit"; | |
| protected override int Execute(CommandContext context, RemoveHookSettings settings, CancellationToken cancellationToken) | |
| { | |
| if (!File.Exists(HookPath)) |
Description
This pull request refactors and improves the management of the pre-commit hook for the AL2DBML CLI, making the codebase more modular and user-friendly. The hook logic is now encapsulated in a dedicated
HookService, and a new CLI command is introduced for removing the hook. Additionally, constants for hook markers are centralized for maintainability.Pre-commit hook management refactor:
InitCommandinto a newHookServiceclass, centralizing the code for creating and removing the hook and improving maintainability. (src/AL2DBML.CLI/Commands/InitCommand.cs,src/AL2DBML.CLI/Services/HookService.cs) [1] [2] [3]RemoveHookCommandto allow users to remove the AL2DBML section from the pre-commit hook via the CLI. (src/AL2DBML.CLI/Commands/RemoveHookCommand.cs,src/AL2DBML.CLI/Program.cs) [1] [2]Constants and code clarity:
HookMarkersconstants class to avoid duplication and ensure consistency. (src/AL2DBML.CLI/Constants/HookMarkers.cs,src/AL2DBML.CLI/Commands/InitCommand.cs,src/AL2DBML.CLI/Services/HookService.cs) [1] [2] [3] [4]src/AL2DBML.CLI/Commands/InitCommand.cs)These changes make the hook management more robust, easier to maintain, and provide a better user experience for managing git hooks in projects using AL2DBML.
Closes
closes #37