Skip to content

Commit e6d6c92

Browse files
committed
Automating Staging, Committing and Pushing to GitHub with Mistral AI (Generated by Mistral AI) (Short commit message based on the provided git diff)
1 parent 1c3f0f1 commit e6d6c92

File tree

1 file changed

+72
-113
lines changed

1 file changed

+72
-113
lines changed

git-commit-push-script.sh

Lines changed: 72 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
## Automating Staging, Committing and Pushing to GitHub with Gemini AI 👨🏻‍💻➡️
1+
## Automating Staging, Committing and Pushing to GitHub with Ollama and Mistral AI 👨🏻‍💻➡️
22
## 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/
53
## Configuration instructions: https://github.com/wesleyscholl/git-commit-push-script
64

75
#!/bin/bash
@@ -19,121 +17,82 @@ ticket=$(echo $base_branch | grep -o -E '([A-Za-z]+-[0-9]{3,}|[A-Za-z]+-[0-9]{3,
1917
# Get the git diff
2018
diff=$(git diff --cached)
2119

22-
# Stringify the diff
23-
diff=$(echo $diff | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/\n/\\n/g')
24-
25-
# Prepare the Gemini API request
26-
# gemini_request='{
27-
# "contents":[{"parts":[{"text": "Write a git commit message title (no more than 72 characters total) for the following git diff: '"$diff"' Do not include any other text in the response."}]}],
28-
# "safetySettings": [{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","threshold": "BLOCK_NONE"}],
29-
# "generationConfig": {
30-
# "temperature": 0.2,
31-
# "maxOutputTokens": 50
32-
# }
33-
# }'
34-
35-
# Prepare the Ollama Gemma3n API request
36-
gemma_request='{
37-
"model": "gemma3n",
38-
"prompt": "Write a git commit message title (no more than 72 characters total) for the following git diff: '"$diff"' Do not include any other text in the response."
39-
}'
40-
41-
# Request a commit message from Ollama Gemma3n local API
42-
commit_message=$(curl http://localhost:11434/api/generate -d "$gemma_request" -H "Content-Type: application/json" | jq -r '.response')
43-
44-
# If the commit message is empty, retry the request
45-
if [ -z "$commit_message" ]; then
46-
commit_message=$(curl -s \
47-
-H 'Content-Type: application/json' \
48-
-d "$gemma_request" \
49-
-X POST "http://localhost:11434/api/generate" \
50-
| jq -r '.response'
51-
)
52-
fi
20+
# Default model (change if desired)
21+
MODEL="mistral"
22+
23+
# Prepare the prompt
24+
PROMPT=$(printf "You are an expert software engineer.\n\nYour job is to generate a short, commit message from the following git diff.\nNo more than 72 characters total.\nOnly return the commit message. Do not include any other text.\n\nGit diff:\n%s" "$diff")
25+
26+
# Run the model and capture output
27+
COMMIT_MSG=$(echo "$PROMPT" | ollama run "$MODEL")
5328

54-
# If the commit message is still empty, exit with an error
55-
if [ -z "$commit_message" ]; then
56-
echo "Error: Ollama API request for commit message failed. Please try again."
29+
# If the commit message is empty, exit with an error
30+
if [ -z "$COMMIT_MSG" ]; then
31+
echo "Error: Commit message is empty. Please check the diff and try again."
5732
exit 1
5833
fi
5934

60-
# # Request and parse the commit message from Gemini API
61-
# commit_message=$(curl -s \
62-
# -H 'Content-Type: application/json' \
63-
# -d "$gemini_request" \
64-
# -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY}" \
65-
# | jq -r '.candidates[0].content.parts[0].text'
66-
# )
67-
68-
# If the commit message is empty, retry the request
69-
# if [ -z "$commit_message" ]; then
70-
# commit_message=$(curl -s \
71-
# -H 'Content-Type: application/json' \
72-
# -d "$gemini_request" \
73-
# -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY}" \
74-
# | jq -r '.candidates[0].content.parts[0].text'
75-
# )
76-
# fi
77-
7835
# Clean up commit message formatting - remove #, ```, period . at the end of response
79-
commit_message=$(echo $commit_message | sed 's/#//g' | sed 's/```//g' | sed 's/Commit message title://g' | sed 's/Commit message summary://g' | sed 's/\.//g')
36+
commit_message=$(echo $COMMIT_MSG | sed 's/#//g' | sed 's/```//g' | sed 's/Commit message title://g' | sed 's/Commit message summary://g' | sed 's/\.//g')
8037

81-
# Print the commit message
38+
# Echo the commit message
8239
echo $commit_message
8340

84-
# # If the Gemini retry request fails, exit
85-
# if [ -z "$commit_message" ]; then
86-
# echo "Error: API request for commit message failed. Please try again."
87-
# exit 1
88-
# fi
89-
90-
# export COMMIT_MESSAGE="$commit_message"
91-
# export TICKET="$ticket"
92-
# # Prepare and execute commit command, remove -S to commit without signing
93-
# if [ -z "$ticket" ]; then
94-
# expect <<'EOF'
95-
# spawn git commit -S -m "$env(COMMIT_MESSAGE)"
96-
# expect "Enter passphrase for \"/Users/wscholl/.ssh/id_ed25519\":"
97-
# send "$env(GIT_SSH_PASSPHRASE)\r"
98-
# expect eof
99-
# EOF
100-
# else
101-
# expect <<'EOF'
102-
# spawn git commit -S -m "$env(TICKET) $env(COMMIT_MESSAGE)"
103-
# expect "Enter passphrase for \"/Users/wscholl/.ssh/id_ed25519\":"
104-
# send "$env(GIT_SSH_PASSPHRASE)\r"
105-
# expect eof
106-
# EOF
107-
# fi
108-
109-
# # Check if the branch exists on the remote
110-
# remote_branch=$(git ls-remote --heads origin $base_branch)
111-
112-
# # Function: pull_push_after_failed_push - If push fails, attempt to pull and push again
113-
# pull_push_after_failed_push() {
114-
# echo "Push failed. Attempting to pull and push again."
115-
# git pull
116-
# git push --force
117-
# }
118-
119-
# # Check if the branch exists on the remote
120-
# if [ -z "$remote_branch" ]; then
121-
# # If the branch does not exist on the remote, create it
122-
# echo "Branch '$base_branch' does not exist on remote. Creating it."
123-
# # Push the local branch to the remote, setting the upstream branch
124-
# git push --set-upstream origin $base_branch
125-
126-
# # Check if the push was successful, if previous command is not a failure,
127-
# # execute the function to handle a failed push
128-
# if [ $? -ne 0 ]; then
129-
# pull_push_after_failed_push
130-
# fi
131-
# else # Branch exists on the remote, push changes to the remote branch
132-
# echo "Branch '$base_branch' exists on remote. Pushing changes."
133-
# git push
134-
135-
# # Check if the push wasn't successful, execute the function to handle a failed push
136-
# if [ $? -ne 0 ]; then
137-
# pull_push_after_failed_push
138-
# fi
139-
# fi
41+
# Set the GIT_SSH_PASSPHRASE environment variables
42+
export COMMIT_MESSAGE="$commit_message"
43+
export TICKET="$ticket"
44+
45+
# Prepare and execute commit command, remove -S to commit without signing
46+
if [ -z "$ticket" ]; then
47+
expect <<'EOF'
48+
spawn git commit -S -m "$env(COMMIT_MESSAGE)"
49+
expect "Enter passphrase for \"/Users/wscholl/.ssh/id_ed25519\":"
50+
send "$env(GIT_SSH_PASSPHRASE)\r"
51+
expect eof
52+
EOF
53+
else
54+
expect <<'EOF'
55+
spawn git commit -S -m "$env(TICKET) $env(COMMIT_MESSAGE)"
56+
expect "Enter passphrase for \"/Users/wscholl/.ssh/id_ed25519\":"
57+
send "$env(GIT_SSH_PASSPHRASE)\r"
58+
expect eof
59+
EOF
60+
fi
61+
62+
# Check if the branch exists on the remote
63+
remote_branch=$(git ls-remote --heads origin $base_branch)
64+
65+
# Function: pull_push_after_failed_push - If push fails, attempt to pull and push again
66+
pull_push_after_failed_push() {
67+
echo "Push failed. Attempting to pull and push again."
68+
git fetch origin $base_branch
69+
git pull
70+
git push --force
71+
}
72+
73+
# Check if the branch exists on the remote
74+
if [ -z "$remote_branch" ]; then
75+
# If the branch does not exist on the remote, create it
76+
echo "Branch '$base_branch' does not exist on remote. Creating it."
77+
# Push the local branch to the remote, setting the upstream branch
78+
set -e
79+
git push --set-upstream origin $base_branch
80+
81+
# Check if the push was successful, if previous command is not a failure, execute the function to handle a failed push
82+
if [ $? -ne 0 ]; then
83+
pull_push_after_failed_push
84+
fi
85+
else # Branch exists on the remote, push changes to the remote branch
86+
echo "Branch '$base_branch' exists on remote."
87+
# Pull the latest changes from the remote branch
88+
echo "Pulling latest changes from remote branch..."
89+
git fetch origin $base_branch
90+
git pull
91+
echo "Pushing changes to remote $base_branch branch..."
92+
git push
93+
94+
# Check if the push wasn't successful, execute the function to handle a failed push
95+
if [ $? -ne 0 ]; then
96+
pull_push_after_failed_push
97+
fi
98+
fi

0 commit comments

Comments
 (0)