Skip to content

Commit

Permalink
chore(scripts): enhance commit message generation with additional fil…
Browse files Browse the repository at this point in the history
…e data

Included the reading of file contents directly in the `commit` function to provide a more comprehensive context for commit message generation. This addition will help in producing more accurate and context-aware commit messages by utilizing the contents of the files that are staged for commit. The implementation leverages the `readFileSync` method to gather the necessary data, ensuring all relevant file information is available during the commit message generation process.
  • Loading branch information
shorwood committed Apr 27, 2024
1 parent e3ec390 commit e2e6132
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions scripts/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OpenAI } from 'openai'
// eslint-disable-next-line n/no-unsupported-features/node-builtins
import { createInterface } from 'node:readline/promises'
import { readFile } from 'node:fs/promises'
import { readFileSync } from 'node:fs'
import { execFileSync, spawn } from 'node:child_process'
import { load as parseYaml } from 'js-yaml'
import 'dotenv/config'
Expand All @@ -24,9 +25,11 @@ export async function commit() {
const input = process.argv.slice(2).join(' ')

const diff = execFileSync('git', ['diff', '--cached', '--staged'], { encoding: 'utf8' })
const diffPaths = execFileSync('git', ['diff', '--name-only', '--cached', '--staged'], { encoding: 'utf8' })
const diffStat = execFileSync('git', ['diff', '--stat', '--cached', '--staged'], { encoding: 'utf8' })
const lastCommits = execFileSync('git', ['log', '-2', '--pretty=%B'], { encoding: 'utf8' })
const branchName = execFileSync('git', ['branch', '--show-current'], { encoding: 'utf8' })
const fileContents = diffPaths.split('\n').filter(Boolean).map(path => readFileSync(path, 'utf8'))

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })
const response = await openai.chat.completions.create({
Expand All @@ -36,6 +39,7 @@ export async function commit() {
{ content: `[LAST_COMMITS]\n${lastCommits}\n\n`, role: 'user' },
{ content: `[BRANCH_NAME]\n${branchName}\n\n`, role: 'user' },
{ content: `[DIFF_STAGED_STATS]\n${diffStat}\n\n`, role: 'user' },
{ content: `[FILE_CONTENTS]\n${fileContents.join('\n')}\n\n`, role: 'user' },
{ content: `[DIFF}]\n${diff}`, role: 'user' },
{ content: `[INPUT]\n${input}`, role: 'user' },
],
Expand Down

0 comments on commit e2e6132

Please sign in to comment.