1
1
#! /bin/bash
2
2
source ~ /.bash_profile
3
3
4
- # Add all changes
4
+ # Stage all changes
5
5
git add -A
6
6
7
7
# Get branch name
@@ -10,8 +10,6 @@ base_branch=$(git rev-parse --abbrev-ref HEAD)
10
10
# Extract ticket number from current directory
11
11
ticket=$( echo $base_branch | grep -o -E ' ([A-Za-z]+-[0-9]{3,}|[A-Za-z]+-[0-9]{3,})' )
12
12
13
- echo " Ticket: $ticket "
14
-
15
13
# Get the git diff
16
14
diff=$( git diff --cached)
17
15
@@ -22,27 +20,41 @@ diff=$(echo $diff | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/\n/\\n/g')
22
20
gemini_request=' {"contents":[{"parts":[{"text": "Write a git commit message (72 character maximum) for the following git diff: ' " $diff " ' "}]}]}'
23
21
24
22
# Get commit message from Gemini API
25
- commit_message=$( curl \
23
+ commit_message=$( curl -s \
26
24
-H ' Content-Type: application/json' \
27
25
-d " $gemini_request " \
28
26
-X POST " https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY} " \
29
27
| jq -r ' .candidates[0].content.parts[0].text'
30
28
)
31
29
32
- echo " $commit_message "
33
-
34
30
# Prepare and execute commit command
35
31
git commit -S -m " $ticket $commit_message "
36
32
37
33
# Check if the branch exists on the remote
38
34
remote_branch=$( git ls-remote --heads origin $base_branch )
39
35
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
40
44
if [ -z " $remote_branch " ]; then
41
45
echo " Branch '$base_branch ' does not exist on remote. Creating it."
42
46
# Push the local branch to the remote, setting the upstream branch
43
47
git push --set-upstream origin $base_branch
48
+
49
+ if [ $? -ne 0 ]; then
50
+ pull_push_after_failed_push
51
+ fi
44
52
else
45
53
echo " Branch '$base_branch ' exists on remote. Pushing changes."
46
54
# Push changes to the remote
47
55
git push
56
+
57
+ if [ $? -ne 0 ]; then
58
+ pull_push_after_failed_push
59
+ fi
48
60
fi
0 commit comments