Skip to content

Instantly share code, notes, and snippets.

@alexaivars
Created January 16, 2024 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexaivars/1cd8248721ef344c72f2cba8bcc276b4 to your computer and use it in GitHub Desktop.
Save alexaivars/1cd8248721ef344c72f2cba8bcc276b4 to your computer and use it in GitHub Desktop.
This script analyzes the output of 'git diff --cached' to generate a meaningful commit message.
/**
* Usage:
* After staging files with git, run this script to generate a commit message.
* Command: ts-node tools/message.ts
* This script analyzes the output of 'git diff --cached' to generate a meaningful commit message.
* It utilizes the OpenAI client to process the diff and suggests a concise summary.
*/
import { config as dotenvConfig } from "dotenv";
import {
OpenAIClient,
AzureKeyCredential,
ChatRequestMessage,
} from "@azure/openai";
import { exec } from "child_process";
dotenvConfig();
const AZURE_OPENAI_DEPLOYMENT_ADVANCED: string = process.env.AZURE_OPENAI_DEPLOYMENT_ADVANCED || "";
const AZURE_OPENAI_ENDPOINT: string = process.env.AZURE_OPENAI_ENDPOINT || "";
const AZURE_OPENAI_KEY: string = process.env.AZURE_OPENAI_KEY || "";
if (!AZURE_OPENAI_DEPLOYMENT_ADVANCED.trim() || !AZURE_OPENAI_ENDPOINT.trim() || !AZURE_OPENAI_KEY.trim()) {
throw new Error("Please set the following environment variables: AZURE_OPENAI_DEPLOYMENT_ADVANCED, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_KEY");
}
async function generateMessage(gitDiffText: string) {
const message: ChatRequestMessage[] = [
{
role: "system",
content: `
Your task is to analyze the output of a 'git diff' command to generate a concise and meaningful commit message.
Look for changes in the code, such as additions, deletions, or modifications.
Identify the purpose of these changes and the files affected.
Consider coding style alterations, feature implementations, bug fixes, refactoring, or documentation updates.
Based on your analysis, summarize the changes in a simple, clear commit message.
The commit message should briefly describe what was changed and why, without using markdown or code block formatting.
`,
},
{
role: "user",
content: `Please analyze the following 'git diff' output and generate a commit message: ${gitDiffText}`,
},
];
const client = new OpenAIClient(
AZURE_OPENAI_ENDPOINT,
new AzureKeyCredential(AZURE_OPENAI_KEY)
);
const events = await client.streamChatCompletions(
AZURE_OPENAI_DEPLOYMENT_ADVANCED,
message,
{
maxTokens: 512,
}
);
let result: string[] = [];
for await (const event of events) {
for (const choice of event.choices) {
if (choice.delta?.content) {
result.push(choice.delta.content);
process.stdout.write(
`Received ${result.join("").length} chars of text\r`
);
}
}
}
console.log();
console.log(result.join(""));
}
(async () => {
exec("git diff --cached", async (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
const gitDiffText: string = stdout;
await generateMessage(gitDiffText);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment