1
+ # # Automating Staging, Committing and Pushing to GitHub with Gemini AI 👨🏻💻➡️
2
+ # # AI commits generated from git diff
3
+
4
+ # # *** A free Gemini AI API key is required to run this shell script *** - https://www.getgemini.ai/
5
+ # # Configuration instructions: https://github.com/wesleyscholl/git-commit-push-script
6
+
1
7
#! /bin/bash
2
8
source ~ /.bash_profile
3
9
4
10
# Stage all changes
5
11
git add -A
6
12
7
- # Get branch name
13
+ # Get the branch name
8
14
base_branch=$( git rev-parse --abbrev-ref HEAD)
9
15
10
- # Extract ticket number from current directory
16
+ # Extract Jira ticket number from current directory
11
17
ticket=$( echo $base_branch | grep -o -E ' ([A-Za-z]+-[0-9]{3,}|[A-Za-z]+-[0-9]{3,})' )
12
18
13
19
# Get the git diff
@@ -26,15 +32,15 @@ gemini_request='{
26
32
}
27
33
}'
28
34
29
- # Get commit message from Gemini API
35
+ # Request and parse the commit message from Gemini API
30
36
commit_message=$( curl -s \
31
37
-H ' Content-Type: application/json' \
32
38
-d " $gemini_request " \
33
39
-X POST " https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY} " \
34
40
| jq -r ' .candidates[0].content.parts[0].text'
35
41
)
36
42
37
- # If the commit message is empty, try again
43
+ # If the commit message is empty, retry the request
38
44
if [ -z " $commit_message " ]; then
39
45
commit_message=$( curl -s \
40
46
-H ' Content-Type: application/json' \
47
53
# Clean up commit message formatting - remove #, ```,
48
54
commit_message=$( echo $commit_message | sed ' s/#//g' | sed ' s/```//g' | sed ' s/Commit message title://g' | sed ' s/Commit message summary://g' )
49
55
56
+ # Print the commit message
50
57
echo $commit_message
51
58
59
+ # If the Gemini retry request fails, exit
52
60
if [ -z " $commit_message " ]; then
53
61
echo " Error: API request for commit message failed. Please try again."
54
62
exit 1
55
63
fi
56
64
57
- # Prepare and execute commit command
65
+ # Prepare and execute commit command, remove -S to commit without signing
58
66
if [ -z " $ticket " ]; then
59
67
git commit -S -m " $commit_message "
60
68
else
@@ -73,18 +81,21 @@ pull_push_after_failed_push() {
73
81
74
82
# Check if the branch exists on the remote
75
83
if [ -z " $remote_branch " ]; then
84
+ # If the branch does not exist on the remote, create it
76
85
echo " Branch '$base_branch ' does not exist on remote. Creating it."
77
86
# Push the local branch to the remote, setting the upstream branch
78
87
git push --set-upstream origin $base_branch
79
88
89
+ # Check if the push was successful, if previous command is not a failure,
90
+ # execute the function to handle a failed push
80
91
if [ $? -ne 0 ]; then
81
92
pull_push_after_failed_push
82
93
fi
83
- else
94
+ else # Branch exists on the remote, push changes to the remote branch
84
95
echo " Branch '$base_branch ' exists on remote. Pushing changes."
85
- # Push changes to the remote
86
96
git push
87
97
98
+ # Check if the push wasn't successful, execute the function to handle a failed push
88
99
if [ $? -ne 0 ]; then
89
100
pull_push_after_failed_push
90
101
fi
0 commit comments