diff --git a/README.md b/README.md index 2f4b5b3..663e0b3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # AI Diff Commit -![Version](https://img.shields.io/badge/version-0.0.2-blue) +![Version](https://img.shields.io/badge/version-0.0.3-blue) Automates the creation of standardized Git commit messages using [OpenAI's API](https://platform.openai.com/docs/). @@ -26,11 +26,9 @@ Automates the creation of standardized Git commit messages using [OpenAI's API]( - **Internet Connection**: Requires an active internet connection to communicate with the OpenAI API. - **Requires a Git Repository**: Needs to be run in a Git repository to access the changes for generating commit messages. -## Dependencies - ## Installation Instructions -Commit Generator can be installed using npm: +Commit Generator can be installed globally using npm: ```bash npm install -g ai-diff-commit @@ -46,6 +44,7 @@ Once you have set up the script using the installation instructions, you can use - `-h`, `--help`: Display help information for the script. - `-m`, `--model`: Specify the OpenAI API language model to use for generating commit messages. See the [OpenAI API documentation](https://platform.openai.com/docs/models/) for available models. - `-p`, `--push`: Automatically push the changes to the remote repository after committing. + - If a remote branch is not specified, the changes are pushed to a new remote branch with the same name as the current branch. (I plan to make this more configurable in the future.) For example, to generate a commit message based on all changes in the repository and push the changes to the remote repository, you can use the following command: diff --git a/package-lock.json b/package-lock.json index 61ce693..6c15d32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ai-diff-commit", - "version": "0.0.2", + "version": "0.0.3", "lockfileVersion": 3, "requires": true, "packages": { @@ -15,7 +15,7 @@ "openai": "^4.68.4" }, "bin": { - "comgen": "dist/main.js" + "ai-diff-commit": "dist/main.js" }, "devDependencies": { "@types/inquirer": "^9.0.7", diff --git a/package.json b/package.json index e8a89cb..2712618 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,8 @@ { "name": "ai-diff-commit", - "version": "0.0.2", + "version": "0.0.3", "description": "A CLI tool to generate commit messages using AI, powered by OpenAI.", + "preferGlobal": true, "main": "dist/main.js", "bin": { "ai-diff-commit": "dist/main.js" diff --git a/src/main.ts b/src/main.ts index 22499f8..ddccbbc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,6 +14,7 @@ import { unstageAllFiles, getName, getEmail, + setupUpstreamBranch, } from './utils/git'; import { confirmCommitMessage, @@ -132,7 +133,15 @@ async function executeCommitWorkflow(systemPrompt: string, diff: string) { print('success', 'Commit successful.'); if (options.push) { - pushChanges(); + try { + pushChanges(); + print('success', 'Push successful.'); + } catch (error: any) { + if (error.message.includes('The current branch has no upstream branch.')) { + setupUpstreamBranch(); + } + } + print('success', 'Push successful.'); } } diff --git a/src/utils/git.ts b/src/utils/git.ts index 180193f..d0aefd6 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -21,7 +21,18 @@ export function isInGitRepo(): boolean { } export function pushChanges(): void { - execSync('git push'); + try { + execSync('git push'); + } catch (error: any) { + if (error.message.includes('fatal: The current branch')) { + throw new Error('The current branch has no upstream branch.'); + } + } +} + +export function setupUpstreamBranch() { + const branchName = getCurrentBranchName(); + execSync(`git push --set-upstream origin ${branchName}`); } export function addAllChanges(): void {