Skip to content
This repository was archived by the owner on May 3, 2025. It is now read-only.

Commit 1c95bb7

Browse files
authored
Version 0.0.3 - Handle push with no remote (#25)
* 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>
1 parent c9a018f commit 1c95bb7

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AI Diff Commit
22

3-
![Version](https://img.shields.io/badge/version-0.0.2-blue)
3+
![Version](https://img.shields.io/badge/version-0.0.3-blue)
44

55
Automates the creation of standardized Git commit messages using [OpenAI's API](https://platform.openai.com/docs/).
66

@@ -26,11 +26,9 @@ Automates the creation of standardized Git commit messages using [OpenAI's API](
2626
- **Internet Connection**: Requires an active internet connection to communicate with the OpenAI API.
2727
- **Requires a Git Repository**: Needs to be run in a Git repository to access the changes for generating commit messages.
2828

29-
## Dependencies
30-
3129
## Installation Instructions
3230

33-
Commit Generator can be installed using npm:
31+
Commit Generator can be installed globally using npm:
3432

3533
```bash
3634
npm install -g ai-diff-commit
@@ -46,6 +44,7 @@ Once you have set up the script using the installation instructions, you can use
4644
- `-h`, `--help`: Display help information for the script.
4745
- `-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.
4846
- `-p`, `--push`: Automatically push the changes to the remote repository after committing.
47+
- 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.)
4948

5049
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:
5150

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "ai-diff-commit",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "A CLI tool to generate commit messages using AI, powered by OpenAI.",
5+
"preferGlobal": true,
56
"main": "dist/main.js",
67
"bin": {
78
"ai-diff-commit": "dist/main.js"

src/main.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
unstageAllFiles,
1515
getName,
1616
getEmail,
17+
setupUpstreamBranch,
1718
} from './utils/git';
1819
import {
1920
confirmCommitMessage,
@@ -132,7 +133,15 @@ async function executeCommitWorkflow(systemPrompt: string, diff: string) {
132133
print('success', 'Commit successful.');
133134

134135
if (options.push) {
135-
pushChanges();
136+
try {
137+
pushChanges();
138+
print('success', 'Push successful.');
139+
} catch (error: any) {
140+
if (error.message.includes('The current branch has no upstream branch.')) {
141+
setupUpstreamBranch();
142+
}
143+
}
144+
136145
print('success', 'Push successful.');
137146
}
138147
}

src/utils/git.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ export function isInGitRepo(): boolean {
2121
}
2222

2323
export function pushChanges(): void {
24-
execSync('git push');
24+
try {
25+
execSync('git push');
26+
} catch (error: any) {
27+
if (error.message.includes('fatal: The current branch')) {
28+
throw new Error('The current branch has no upstream branch.');
29+
}
30+
}
31+
}
32+
33+
export function setupUpstreamBranch() {
34+
const branchName = getCurrentBranchName();
35+
execSync(`git push --set-upstream origin ${branchName}`);
2536
}
2637

2738
export function addAllChanges(): void {

0 commit comments

Comments
 (0)