guibranco / dotnet-aicommitmessage

🧠 🧰 This tool generates AI-powered commit messages via Git hooks, automating meaningful message suggestions from OpenAI and others to improve commit quality and efficiency
https://guibranco.github.io/dotnet-aicommitmessage/
MIT License
1 stars 0 forks source link

[FEATURE] Automatically Generate `prepare-commit-msg` Git Hook to Call AI Commit Message Tool #37

Open guibranco opened 1 hour ago

guibranco commented 1 hour ago

Description:

I would like to request a feature to automate the creation of a prepare-commit-msg Git hook in the repository’s hooks directory. The hook should call a tool that generates an AI-based commit message. The content of the hook should look like the following:

#!/bin/sh

COMMIT=$(dotnet-aicommitmessage generate-message)

echo "$COMMIT" > $COMMIT_MSG_FILE

This script will automate the commit message generation using the AI commit message tool (dotnet-aicommitmessage) before a commit is created, replacing the message in the commit file.

Why is this needed?

This feature would enable seamless integration of AI-generated commit messages into the development workflow. Developers wouldn’t need to manually run the AI tool or copy/paste the message into their commits—this would all happen automatically when committing. It provides an improved developer experience and better commit message consistency.

Suggested Implementation:

Example Code:

using System;
using System.Diagnostics;
using System.IO;

public class GitHookSetup
{
    public static void CreatePrepareCommitMsgHook()
    {
        string hooksDirectory = GetHooksDirectory();
        string hookFilePath = Path.Combine(hooksDirectory, "prepare-commit-msg");

        if (File.Exists(hookFilePath))
        {
            Console.WriteLine("prepare-commit-msg hook already exists.");
            return;
        }

        string hookContent = "#!/bin/sh\n\n" +
                             "COMMIT=$(dotnet-aicommitmessage generate-message)\n\n" +
                             "echo \"$COMMIT\" > $COMMIT_MSG_FILE";

        File.WriteAllText(hookFilePath, hookContent);
        MakeExecutable(hookFilePath);

        Console.WriteLine($"prepare-commit-msg hook created at {hookFilePath}");
    }

    private static string GetHooksDirectory()
    {
        var processStartInfo = new ProcessStartInfo
        {
            FileName = "git",
            Arguments = "config core.hooksPath",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using (var process = new Process { StartInfo = processStartInfo })
        {
            process.Start();
            string hooksPath = process.StandardOutput.ReadToEnd();
            process.WaitForExit();

            if (string.IsNullOrEmpty(hooksPath))
            {
                // Default path if no hooksPath is configured
                return Path.Combine(".git", "hooks");
            }

            return hooksPath.Trim();
        }
    }

    private static void MakeExecutable(string filePath)
    {
        var processStartInfo = new ProcessStartInfo
        {
            FileName = "chmod",
            Arguments = $"+x {filePath}",
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using (var process = new Process { StartInfo = processStartInfo })
        {
            process.Start();
            process.WaitForExit();
        }
    }
}

// Usage example
class Program
{
    static void Main()
    {
        GitHookSetup.CreatePrepareCommitMsgHook();
    }
}

This C# code creates a prepare-commit-msg file in the Git hooks directory that executes the dotnet-aicommitmessage tool. It ensures that the script is executable and ready to be used in the Git commit workflow.

Additional Context:

gitauto-ai[bot] commented 1 hour ago

Click the checkbox below to generate a PR!

@guibranco, You have 5 requests left in this cycle which refreshes on 2024-10-21 10:07:38+00:00. If you have any questions or concerns, please contact us at info@gitauto.ai.