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+
17#! /bin/bash
28source ~ /.bash_profile
39
410# Stage all changes
511git add -A
612
7- # Get branch name
13+ # Get the branch name
814base_branch=$( git rev-parse --abbrev-ref HEAD)
915
10- # Extract ticket number from current directory
16+ # Extract Jira ticket number from current directory
1117ticket=$( echo $base_branch | grep -o -E ' ([A-Za-z]+-[0-9]{3,}|[A-Za-z]+-[0-9]{3,})' )
1218
1319# Get the git diff
@@ -26,15 +32,15 @@ gemini_request='{
2632 }
2733}'
2834
29- # Get commit message from Gemini API
35+ # Request and parse the commit message from Gemini API
3036commit_message=$( curl -s \
3137 -H ' Content-Type: application/json' \
3238 -d " $gemini_request " \
3339 -X POST " https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY} " \
3440 | jq -r ' .candidates[0].content.parts[0].text'
3541 )
3642
37- # If the commit message is empty, try again
43+ # If the commit message is empty, retry the request
3844if [ -z " $commit_message " ]; then
3945 commit_message=$( curl -s \
4046 -H ' Content-Type: application/json' \
4753# Clean up commit message formatting - remove #, ```,
4854commit_message=$( echo $commit_message | sed ' s/#//g' | sed ' s/```//g' | sed ' s/Commit message title://g' | sed ' s/Commit message summary://g' )
4955
56+ # Print the commit message
5057echo $commit_message
5158
59+ # If the Gemini retry request fails, exit
5260if [ -z " $commit_message " ]; then
5361 echo " Error: API request for commit message failed. Please try again."
5462 exit 1
5563fi
5664
57- # Prepare and execute commit command
65+ # Prepare and execute commit command, remove -S to commit without signing
5866if [ -z " $ticket " ]; then
5967 git commit -S -m " $commit_message "
6068else
@@ -73,18 +81,21 @@ pull_push_after_failed_push() {
7381
7482# Check if the branch exists on the remote
7583if [ -z " $remote_branch " ]; then
84+ # If the branch does not exist on the remote, create it
7685 echo " Branch '$base_branch ' does not exist on remote. Creating it."
7786 # Push the local branch to the remote, setting the upstream branch
7887 git push --set-upstream origin $base_branch
7988
89+ # Check if the push was successful, if previous command is not a failure,
90+ # execute the function to handle a failed push
8091 if [ $? -ne 0 ]; then
8192 pull_push_after_failed_push
8293 fi
83- else
94+ else # Branch exists on the remote, push changes to the remote branch
8495 echo " Branch '$base_branch ' exists on remote. Pushing changes."
85- # Push changes to the remote
8696 git push
8797
98+ # Check if the push wasn't successful, execute the function to handle a failed push
8899 if [ $? -ne 0 ]; then
89100 pull_push_after_failed_push
90101 fi
0 commit comments