Skip to content

37 feat add remove hook command#38

Merged
OGR-67 merged 10 commits into
devfrom
37-feat-add-remove-hook-command
Mar 22, 2026
Merged

37 feat add remove hook command#38
OGR-67 merged 10 commits into
devfrom
37-feat-add-remove-hook-command

Conversation

@OGR-67

@OGR-67 OGR-67 commented Mar 22, 2026

Copy link
Copy Markdown
Owner

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:

  • Extracted all pre-commit hook logic from InitCommand into a new HookService class, 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]
  • Added a new RemoveHookCommand to 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:

  • Moved hook marker strings to a new HookMarkers constants 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]
  • Updated usages of hook marker strings in code to reference the new constants. (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

Copilot AI review requested due to automatic review settings March 22, 2026 10:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 HookService to write/update and remove the AL2DBML section in .git/hooks/pre-commit.
  • Added remove-hook CLI 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.

Comment on lines 22 to 27
app.Configure(config =>
{
config.AddCommand<GenerateCommand>("generate");
config.AddCommand<InitCommand>("init");
config.AddCommand<RemoveHookCommand>("remove-hook");
});

Copilot AI Mar 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +68 to +69
AnsiConsole.MarkupLine("[yellow]Removing existing pre-commit hook...[/]");
HookService.Remove();

Copilot AI Mar 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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();
}

Copilot uses AI. Check for mistakes.
Comment on lines +20 to +26
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

Copilot AI Mar 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +47 to +53
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');

Copilot AI Mar 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Comment on lines +13 to +15
protected override int Execute(CommandContext context, RemoveHookSettings settings, CancellationToken cancellationToken)
{
if (!File.Exists(".git/hooks/pre-commit"))

Copilot AI Mar 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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))

Copilot uses AI. Check for mistakes.
@OGR-67 OGR-67 merged commit c644808 into dev Mar 22, 2026
1 check passed
@OGR-67 OGR-67 deleted the 37-feat-add-remove-hook-command branch March 22, 2026 10:27
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.

2 participants