Skip to content

Commit

Permalink
Version 0.0.3 - Handle push with no remote (#25)
Browse files Browse the repository at this point in the history
* docs: update installation instructions in README (#22)

* Push no remote (#23)

* feat: enhance push functionality with upstream branch setup

---------

Co-authored-by: IanSkelskey <IanSkelskey@users.noreply.github.com>

* chore: update `README.md` by removing Dependencies section (#24)

* chore: bump version to 0.0.3

---------

Co-authored-by: IanSkelskey <IanSkelskey@users.noreply.github.com>
  • Loading branch information
IanSkelskey and IanSkelskey authored Oct 30, 2024
1 parent c9a018f commit 1c95bb7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/).

Expand All @@ -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
Expand All @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
11 changes: 10 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
unstageAllFiles,
getName,
getEmail,
setupUpstreamBranch,
} from './utils/git';
import {
confirmCommitMessage,
Expand Down Expand Up @@ -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.');
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1c95bb7

Please sign in to comment.