Skip to content

Commit 3b2da72

Browse files
authored
Merged to main
2 parents f7fb88a + fb7948d commit 3b2da72

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

git-commit-push-script.sh

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
source ~/.bash_profile
33

4-
# Add all changes
4+
# Stage all changes
55
git add -A
66

77
# Get branch name
@@ -10,8 +10,6 @@ base_branch=$(git rev-parse --abbrev-ref HEAD)
1010
# Extract ticket number from current directory
1111
ticket=$(echo $base_branch | grep -o -E '([A-Za-z]+-[0-9]{3,}|[A-Za-z]+-[0-9]{3,})')
1212

13-
echo "Ticket: $ticket"
14-
1513
# Get the git diff
1614
diff=$(git diff --cached)
1715

@@ -22,27 +20,41 @@ diff=$(echo $diff | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/\n/\\n/g')
2220
gemini_request='{"contents":[{"parts":[{"text": "Write a git commit message (72 character maximum) for the following git diff: '"$diff"' "}]}]}'
2321

2422
# Get commit message from Gemini API
25-
commit_message=$(curl \
23+
commit_message=$(curl -s \
2624
-H 'Content-Type: application/json' \
2725
-d "$gemini_request" \
2826
-X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY}" \
2927
| jq -r '.candidates[0].content.parts[0].text'
3028
)
3129

32-
echo "$commit_message"
33-
3430
# Prepare and execute commit command
3531
git commit -S -m "$ticket $commit_message"
3632

3733
# Check if the branch exists on the remote
3834
remote_branch=$(git ls-remote --heads origin $base_branch)
3935

36+
# Function: pull_push_after_failed_push - If push fails, attempt to pull and push again
37+
pull_push_after_failed_push() {
38+
echo "Push failed. Attempting to pull and push again."
39+
git pull
40+
git push
41+
}
42+
43+
# Check if the branch exists on the remote
4044
if [ -z "$remote_branch" ]; then
4145
echo "Branch '$base_branch' does not exist on remote. Creating it."
4246
# Push the local branch to the remote, setting the upstream branch
4347
git push --set-upstream origin $base_branch
48+
49+
if [ $? -ne 0 ]; then
50+
pull_push_after_failed_push
51+
fi
4452
else
4553
echo "Branch '$base_branch' exists on remote. Pushing changes."
4654
# Push changes to the remote
4755
git push
56+
57+
if [ $? -ne 0 ]; then
58+
pull_push_after_failed_push
59+
fi
4860
fi

0 commit comments

Comments
 (0)