DEV Community

Peli de Halleux
Peli de Halleux

Posted on • Edited on • Originally published at microsoft.github.io

GenAI Git Commits

Generating a git commit message is quickly become a classic GenAI application for developers.

To help with this, we will craft a GenAIScript script.

The script acts as a regular node.js automation script and uses runPrompt
to issue calls to the LLM and ask the user to confirm the generated text.

๐Ÿ” Explaining the Script

We start by using git check if there are any staged changes in the Git repository:

let { stdout } = await host.exec("git", ["diff", "--cached"])
Enter fullscreen mode Exit fullscreen mode

If no changes are staged, we ask the user if they want to stage all changes. If the user confirms, we stage all changes. Otherwise, we bail out.

    const stage = await host.confirm("Stage all changes?", {
        default: true,
    })
    if (stage) {
        await host.exec("git", ["add", "."])
        stdout = (await host.exec("git", ["diff", "--cached"])).stdout
    }
    if (!stdout) cancel("no staged changes")
Enter fullscreen mode Exit fullscreen mode

We generate an initial commit message using the staged changes:

message = (
    await runPrompt(
        (_) => {
            _.def("GIT_DIFF", stdout, { maxTokens: 20000 })
            _.$`GIT_DIFF is a diff of all staged changes, coming from the command:
\`\`\`
git diff --cached
\`\`\`
Please generate a concise, one-line commit message for these changes.
- do NOT add quotes`
        },
        { temperature: 0.8 }
    )
).text
Enter fullscreen mode Exit fullscreen mode

The prompt configuration above indicates that the message should be concise,
related to the "git diff --cached" output, and should not include quotes.

User chooses how to proceed with the generated message:

    choice = await host.select(
        message, [
            { name: "commit", value: "commit", description: "accept message and commit" },
            ...
        ])
Enter fullscreen mode Exit fullscreen mode

Options are given to edit or regenerate the message. If the user chooses to edit the message, we ask them to input a new message:

    if (choice === "edit") {
        message = await host.input("Edit commit message", {
            required: true,
        })
        choice = "commit"
    }
Enter fullscreen mode Exit fullscreen mode

If the user chooses to commit the message, we commit the changes:

    if (choice === "commit" && message) {
        console.log((await host.exec("git", ["commit", "-m", message])).stdout)
    }
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Running the Script

You can run this script using the CLI.

genaiscript run gcm
Enter fullscreen mode Exit fullscreen mode

If you are using npx,

npx --yes genaiscript run gcm
Enter fullscreen mode Exit fullscreen mode

This command will run the script, and guide you through the process of generating and committing a Git message using AI, making your commits more informative and consistent.

You can wrap this command in a gcm.sh file or in your package script section in package.json:

{
    "devDependencies": {
        "genaiscript": "..."
    },
    "scripts": {
        "gcm": "genaiscript run gcm"
    }
}
Enter fullscreen mode Exit fullscreen mode

Then you can run the script using:

npm run gcm
Enter fullscreen mode Exit fullscreen mode

Acknowledgements

This script was inspired from Karpathy's commit message generator.

Top comments (1)

Collapse
 
king_triton profile image
King Triton

Wow, this is a fantastic approach to streamline the process of generating Git commit messages! Utilizing AI to ensure concise and informative commits is a game-changer, especially for teams looking to maintain consistency in their repositories. The step-by-step breakdown of the script and its seamless integration with Node.js showcases how practical and user-friendly this solution is. I can already see this becoming an essential tool in my daily workflow. Kudos to the creators for this innovation! ๐Ÿš€