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"
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"])
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")
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
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" }, ... ], })
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" }
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) }
? Running the Script
You can run this script using the CLI.
genaiscript run gcm
Since it uses the @inquirer/prompts package, you will need to install this package first:
npm install --save-dev @inquirer/prompts
If you are using npx,
npx -p @inquirer/prompts genaiscript -p genaiscript -- genaiscript run gcm
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" } }
Then you can run the script using:
npm run gcm
Acknowledgements
This script was inspired from Karpathy's commit message generator.
The above is the detailed content of GenAI Git Commits. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)
