diff --git a/package.json b/package.json index 61cadb8..3f76f95 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cloving", - "version": "0.3.19", + "version": "0.3.20", "packageManager": "yarn@1.22.22", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index a4bba6c..753e405 100644 --- a/src/index.ts +++ b/src/index.ts @@ -90,6 +90,7 @@ program '-m, --model ', 'Select the model to use (e.g., openai, claude, ollama, ollama:llama3, claude:claude-3-5-sonnet-20240620)', ) + .option('-a, --autoAccept', 'Automatically accept the generated commit message without editing') .action(commit) // Generate commands @@ -143,6 +144,7 @@ generate '-m, --model ', 'Select the model to use (e.g., openai, claude, ollama, ollama:llama3, claude:claude-3-5-sonnet-20240620)', ) + .option('-a, --autoAccept', 'Automatically accept the generated commit message without editing') .action(commit) generate diff --git a/src/managers/CommitManager.ts b/src/managers/CommitManager.ts index 196bbd2..e923635 100644 --- a/src/managers/CommitManager.ts +++ b/src/managers/CommitManager.ts @@ -10,9 +10,11 @@ import type { AxiosError } from 'axios' class CommitManager { private gpt: ClovingGPT + private autoAccept: boolean constructor(options: ClovingGPTOptions) { options.silent = getConfig(options).globalSilent || false + this.autoAccept = options.autoAccept || false this.gpt = new ClovingGPT(options) } @@ -43,24 +45,35 @@ class CommitManager { // Clean the commit message using extractMarkdown and remove code blocks const commitMessage = this.cleanCommitMessage(extractMarkdown(rawCommitMessage)) - // Write the commit message to a temporary file - const tempCommitFilePath = path.join('.git', 'SUGGESTED_COMMIT_EDITMSG') - fs.writeFileSync(tempCommitFilePath, commitMessage) + if (this.autoAccept) { + // Directly commit with the generated message without opening an editor + try { + execFileSync('git', ['commit', '-a', '-m', commitMessage], { + stdio: 'inherit', + }) + } catch (commitError) { + console.log('Commit failed:', (commitError as Error).message) + } + } else { + // Write the commit message to a temporary file + const tempCommitFilePath = path.join('.git', 'SUGGESTED_COMMIT_EDITMSG') + fs.writeFileSync(tempCommitFilePath, commitMessage) - // Commit the changes using the generated commit message - try { - execFileSync('git', ['commit', '-a', '--edit', '--file', tempCommitFilePath], { - stdio: 'inherit', + // Commit the changes using the generated commit message + try { + execFileSync('git', ['commit', '-a', '--edit', '--file', tempCommitFilePath], { + stdio: 'inherit', + }) + } catch (commitError) { + // If commit is canceled (non-zero exit), handle it here + console.log('Commit was canceled or failed.') + } + + // Remove the temporary file using fs + fs.unlink(tempCommitFilePath, (err) => { + if (err) throw err }) - } catch (commitError) { - // If commit is canceled (non-zero exit), handle it here - console.log('Commit was canceled or failed.') } - - // Remove the temporary file using fs - fs.unlink(tempCommitFilePath, (err) => { - if (err) throw err - }) } catch (err) { const error = err as AxiosError console.error('Could not generate commit message:', error.message) diff --git a/src/utils/types.ts b/src/utils/types.ts index fe7922f..85b6804 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -45,6 +45,7 @@ export interface ClovingGPTOptions { port?: number stream?: boolean timeout?: number + autoAccept?: boolean } export interface ClovingModelConfig {