Skip to content

Commit

Permalink
Generate git commit message from pipe input
Browse files Browse the repository at this point in the history
Refactor code to generate commit message from pipe input and
improve message generation process.

Generated by gpt-3.5-turbo
  • Loading branch information
joone committed Feb 24, 2024
1 parent 6231c24 commit 08fe197
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
22 changes: 21 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,27 @@ async function handlePromptInput(prompt: any) {
async function handleGitCommandFromPipe() {
// If the stdin is not a TTY, but from a pipe
if (!process.stdin.isTTY) {
await loz.generateGitCommitMessageFromPipe();
process.stdin.setEncoding("utf8");
process.stdin.on("data", async (data: String) => {
// Remove the first line from data
// because it is not needed for GPT-3.
data = data.replace(/.*\n/, "");
// Remove Author and Date from commit message
// because it is not needed for GPT-3.
const diff = data
.toString()
.replace(/Author: .*\n/, "")
.replace(/Date: .*\n/, "");
const completion = await loz.generateGitCommitMessage(diff);
if (completion.content === "") {
console.log("Failed to generate a commit message");
return;
}

process.stdout.write(
completion.content + "\n\nGenerated by " + completion.model + "\n"
);
});
} else {
console.log("Run loz like this: git diff | loz --git");
}
Expand Down
39 changes: 6 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,41 +250,14 @@ export class Loz {
}

// git diff | loz --git
async generateGitCommitMessageFromPipe() {
async generateGitCommitMessage(diff: string) {
if (DEBUG) console.log("writeGitCommitMessage");
let params: LLMSettings;
params = this.defaultSettings;
params.max_tokens = 500;
params.prompt = promptForGIT + diff + "\n" + "Commit Message: ";

process.stdin.setEncoding("utf8");

process.stdin.on("data", async (data: String) => {
// Remove the first line from data
// because it is not needed for GPT-3.
data = data.replace(/.*\n/, "");
// Remove Author and Date from commit message
// because it is not needed for GPT-3.
const diff = data
.toString()
.replace(/Author: .*\n/, "")
.replace(/Date: .*\n/, "");

let params: LLMSettings;
params = this.defaultSettings;
params.max_tokens = 500;
params.prompt = promptForGIT + diff + "\n" + "Commit Message: ";

const complete = await this.llmAPI.completion(params);
if (complete.content === "") {
console.log("Failed to generate a commit message");
return;
}

process.stdout.write(
complete.content + "\n\nGenerated by " + complete.model + "\n"
);
});

process.stdin.on("end", () => {
//process.exit();
});
return await this.llmAPI.completion(params);
}

// Interactive mode
Expand Down

0 comments on commit 08fe197

Please sign in to comment.