Skip to content

Commit 679585b

Browse files
authored
Merged to main
2 parents 2a6fbcd + e6d6c92 commit 679585b

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

git-commit-push-script.sh

Lines changed: 42 additions & 46 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,54 +17,46 @@ 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 repsonse."}]}],
28-
"safetySettings": [{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","threshold": "BLOCK_NONE"}],
29-
"generationConfig": {
30-
"temperature": 0.2,
31-
"maxOutputTokens": 50
32-
}
33-
}'
34-
35-
# Request and parse the commit message from Gemini API
36-
commit_message=$(curl -s \
37-
-H 'Content-Type: application/json' \
38-
-d "$gemini_request" \
39-
-X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY}" \
40-
| jq -r '.candidates[0].content.parts[0].text'
41-
)
42-
43-
# If the commit message is empty, retry the request
44-
if [ -z "$commit_message" ]; then
45-
commit_message=$(curl -s \
46-
-H 'Content-Type: application/json' \
47-
-d "$gemini_request" \
48-
-X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY}" \
49-
| jq -r '.candidates[0].content.parts[0].text'
50-
)
51-
fi
20+
# Default model (change if desired)
21+
MODEL="mistral"
5222

53-
# Clean up commit message formatting - remove #, ```,
54-
commit_message=$(echo $commit_message | sed 's/#//g' | sed 's/```//g' | sed 's/Commit message title://g' | sed 's/Commit message summary://g')
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")
5525

56-
# Print the commit message
57-
echo $commit_message
26+
# Run the model and capture output
27+
COMMIT_MSG=$(echo "$PROMPT" | ollama run "$MODEL")
5828

59-
# If the Gemini retry request fails, exit
60-
if [ -z "$commit_message" ]; then
61-
echo "Error: 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."
6232
exit 1
6333
fi
6434

35+
# Clean up commit message formatting - remove #, ```, period . at the end of response
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')
37+
38+
# Echo the commit message
39+
echo $commit_message
40+
41+
# Set the GIT_SSH_PASSPHRASE environment variables
42+
export COMMIT_MESSAGE="$commit_message"
43+
export TICKET="$ticket"
44+
6545
# Prepare and execute commit command, remove -S to commit without signing
6646
if [ -z "$ticket" ]; then
67-
git commit -S -m "$commit_message"
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
6853
else
69-
git commit -S -m "$ticket $commit_message"
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
7060
fi
7161

7262
# Check if the branch exists on the remote
@@ -75,6 +65,7 @@ remote_branch=$(git ls-remote --heads origin $base_branch)
7565
# Function: pull_push_after_failed_push - If push fails, attempt to pull and push again
7666
pull_push_after_failed_push() {
7767
echo "Push failed. Attempting to pull and push again."
68+
git fetch origin $base_branch
7869
git pull
7970
git push --force
8071
}
@@ -84,19 +75,24 @@ if [ -z "$remote_branch" ]; then
8475
# If the branch does not exist on the remote, create it
8576
echo "Branch '$base_branch' does not exist on remote. Creating it."
8677
# Push the local branch to the remote, setting the upstream branch
78+
set -e
8779
git push --set-upstream origin $base_branch
8880

89-
# Check if the push was successful, if previous command is not a failure,
90-
# execute the function to handle a failed push
81+
# Check if the push was successful, if previous command is not a failure, execute the function to handle a failed push
9182
if [ $? -ne 0 ]; then
9283
pull_push_after_failed_push
9384
fi
9485
else # Branch exists on the remote, push changes to the remote branch
95-
echo "Branch '$base_branch' exists on remote. Pushing changes."
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..."
9692
git push
9793

9894
# Check if the push wasn't successful, execute the function to handle a failed push
9995
if [ $? -ne 0 ]; then
10096
pull_push_after_failed_push
10197
fi
102-
fi
98+
fi

0 commit comments

Comments
 (0)