Skip to content

Commit af3f3c4

Browse files
Add prefix toggle feature for commit messages
- Introduced a closure-based prefixState for managing the state of commit message prefixes safely. - Added functionality to enable or disable commit message prefixes through a new prefix command. - Updated the
1 parent 4cfba3c commit af3f3c4

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

index.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ import { sanitizeCommitMessage } from './utils/sanitizeCommitMessage.js'
1515
let openai
1616
let model = 'gpt-4o' // Default model
1717
let language = 'English' // Default language
18+
// Define prefixState using closure for safer state management
19+
const prefixState = (() => {
20+
let enabled = false // Default is disabled
21+
return {
22+
isEnabled: () => enabled,
23+
setEnabled: (value) => {
24+
enabled = value
25+
return value
26+
},
27+
}
28+
})()
1829
const CONFIG_FILE = path.join(os.homedir(), '.git-gpt-commit-config.json')
1930

2031
// Function to save config to file
@@ -44,6 +55,9 @@ function loadConfig() {
4455
if (config.language) {
4556
language = config.language
4657
}
58+
if (config.prefixEnabled !== undefined) {
59+
prefixState.setEnabled(config.prefixEnabled)
60+
}
4761
}
4862
} catch (error) {
4963
console.error('Error loading configuration:', error)
@@ -91,7 +105,9 @@ const gptCommit = async () => {
91105
},
92106
{
93107
role: 'user',
94-
content: `Generate a Git commit message based on the following summary: ${gitSummary}\n\nCommit message: `,
108+
content: prefixState.isEnabled()
109+
? `Generate a Git commit message based on the following summary, with an appropriate prefix (add:, fix:, feat:, refactor:, chore:, perf:, test:, style:, docs:, merge:, chore:, build:, ci:, revert:, merge:) based on the type of changes: ${gitSummary}\n\nCommit message: `
110+
: `Generate a Git commit message based on the following summary: ${gitSummary}\n\nCommit message: `,
95111
},
96112
]
97113

@@ -191,12 +207,43 @@ const gitExtension = (_args) => {
191207
console.log(`Language set to ${language} and saved to configuration`)
192208
})
193209

210+
program
211+
.command('prefix')
212+
.description('Toggle commit message prefix (e.g., fix:, feat:, refactor:)')
213+
.action(async () => {
214+
// Show the current state for user information
215+
console.log(
216+
`Prefixes are currently ${prefixState.isEnabled() ? 'enabled' : 'disabled'}.`,
217+
)
218+
219+
const response = await prompts({
220+
type: 'select',
221+
name: 'value',
222+
message: 'Set commit message prefixes',
223+
choices: [
224+
{ title: 'Enable prefixes', value: true },
225+
{ title: 'Disable prefixes', value: false },
226+
],
227+
initial: prefixState.isEnabled() ? 0 : 1,
228+
})
229+
230+
// Update state and save to config
231+
const newValue = prefixState.setEnabled(response.value)
232+
saveConfig({ prefixEnabled: newValue })
233+
console.log(
234+
`Prefixes ${newValue ? 'enabled' : 'disabled'} and saved to configuration`,
235+
)
236+
})
237+
194238
program
195239
.command('config')
196240
.description('Show current configuration')
197241
.action(() => {
198242
console.log(`Current model: ${model}`)
199243
console.log(`Current language: ${language}`)
244+
console.log(
245+
`Prefixes: ${prefixState.isEnabled() ? 'enabled' : 'disabled'}`,
246+
)
200247
})
201248

202249
// Handle invalid commands

0 commit comments

Comments
 (0)