Heim > Web-Frontend > js-Tutorial > Hauptteil

GenAI-Git-Commits

WBOY
Freigeben: 2024-08-28 06:08:33
Original
878 Leute haben es durchsucht

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

The script begins by importing necessary functions from @inquirer/prompts:

import { select, input, confirm } from "@inquirer/prompts"
Nach dem Login kopieren

These functions will be used to interact with the user, asking them to confirm actions or input data.

Next, we check if there are any staged changes in the Git repository:

let { stdout } = await host.exec("git", ["diff", "--cached"])
Nach dem Login kopieren

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 confirm({
        message: "No staged changes. 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")
Nach dem Login kopieren

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`
        },
        { cache: false, temperature: 0.8 }
    )
).text
Nach dem Login kopieren

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 select({
        message,
        choices: [
            { name: "commit", value: "commit", description: "accept message and commit" },
            ...
        ],
    })
Nach dem Login kopieren

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 input({
            message: "Edit commit message",
            required: true,
        })
        choice = "commit"
    }
Nach dem Login kopieren

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)
    }
Nach dem Login kopieren

? Running the Script

You can run this script using the CLI.

genaiscript run gcm
Nach dem Login kopieren

Since it uses the @inquirer/prompts package, you will need to install this package first:

npm install --save-dev @inquirer/prompts
Nach dem Login kopieren

If you are using npx,

npx -p @inquirer/prompts genaiscript -p genaiscript  -- genaiscript run gcm
Nach dem Login kopieren

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": {
        "@inquirer/prompts": "...",
        "genaiscript": "..."
    },
    "scripts": {
        "gcm": "genaiscript run gcm"
    }
}
Nach dem Login kopieren

Then you can run the script using:

npm run gcm
Nach dem Login kopieren

Acknowledgements

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

Das obige ist der detaillierte Inhalt vonGenAI-Git-Commits. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:dev.to
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!