Skip to content

Commit 7eb2f34

Browse files
committed
Removed file
1 parent 531a9ee commit 7eb2f34

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

git-commit-push-script.sh

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,79 @@
55
#!/bin/bash
66
source ~/.bash_profile
77

8+
# Pre-warm the model (optional - keeps it loaded)
9+
ollama run mistral-commit "test" > /dev/null 2>&1 &
10+
811
# Stage all changes
912
git add -A
1013

1114
# Get the branch name
1215
base_branch=$(git rev-parse --abbrev-ref HEAD)
16+
echo "Current branch: $base_branch"
17+
18+
# Get default branch or main branch
19+
default_branch=$(git rev-parse --abbrev-ref origin/HEAD | sed 's@^origin/@@')
20+
echo "Default branch: $default_branch"
1321

1422
# Extract Jira ticket number from current directory
1523
ticket=$(echo $base_branch | grep -o -E '([A-Za-z]+-[0-9]{3,}|[A-Za-z]+-[0-9]{3,})')
1624

17-
# Get the git diff
18-
diff=$(git diff --cached)
25+
# Get the git diff comparing the current branch with the default branch
26+
diff=$(git diff origin/$default_branch)
27+
# echo "Git diff:\n\n$diff"
28+
29+
# # Default model (change if desired)
30+
# MODEL="mistral-commit"
31+
32+
# # Prepare the prompt
33+
# PROMPT="$diff"
34+
35+
# # Run the model and capture output
36+
# COMMIT_MSG=$(ollama run "$MODEL" "$PROMPT")
37+
38+
# # If the commit message is empty, exit with an error
39+
# if [ -z "$COMMIT_MSG" ]; then
40+
# echo "Error: Commit message is empty. Please check the diff and try again."
41+
# exit 1
42+
# fi
1943

20-
# Default model (change if desired)
21-
MODEL="mistral"
44+
# Stringify the diff
45+
diff=$(echo $diff | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/\n/\\n/g')
2246

23-
# Prepare the prompt
24-
PROMPT=$(printf "You are an expert software engineer.\n\nYour job is to generate a concise, descriptive commit message from the following git diff.\nThe commit message MUST be no more than 72 characters in length - this is a strict requirement.\nOnly return the commit message itself without quotes, explanations or additional text.\nDon't include phrases like 'It appears you have', 'I see that you', or 'You seem to have', 'It looks like' or similar in your response.\nThe response should be factual and focused on the changes made.\n\nGit diff:\n%s" "$diff")
47+
# Prepare the Gemini API request
48+
gemini_request='{"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."}]}]}'
2549

26-
# Run the model and capture output
27-
COMMIT_MSG=$(echo "$PROMPT" | ollama run "$MODEL")
50+
# Get commit message from Gemini API
51+
commit_message=$(curl -s \
52+
-H 'Content-Type: application/json' \
53+
-d "$gemini_request" \
54+
-X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY}" \
55+
| jq -r '.candidates[0].content.parts[0].text'
56+
)
2857

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."
58+
# Clean up commit message formatting - remove #, ```, "", '', ()), and period . at the end of response
59+
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' | sed 's/\"//g' | sed "s/'//g" | sed 's/())//g' | sed 's/()//g' | sed 's/Commit message://g' | sed 's/Commit message title: //g' | sed 's/Commit message summary: //g' | sed 's/Commit message body: //g' | sed 's/Commit message body://g')
60+
61+
echo $commit_message
62+
63+
if [ -z "$commit_message" ]; then
64+
echo "Error: API request for commit message failed. Please try again."
3265
exit 1
3366
fi
3467

35-
# Clean up commit message formatting - remove #, ```, "", '', and 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' | sed 's/\"//g' | sed "s/'//g")
68+
# # Clean up commit message formatting - remove #, ```, "", '', ()), and period . at the end of response
69+
# 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' | sed 's/\"//g' | sed "s/'//g" | sed 's/())//g' | sed 's/()//g' | sed 's/Commit message://g' | sed 's/Commit message title: //g' | sed 's/Commit message summary: //g' | sed 's/Commit message body: //g' | sed 's/Commit message body://g')
3770

38-
# If the commit message is longer than 72 characters, truncate at the last word boundary
39-
if [ ${#commit_message} -gt 72 ]; then
40-
commit_message=$(echo $commit_message | cut -d' ' -f1-18)
41-
fi
71+
# # If the commit message is longer than 72 characters, truncate at the last word boundary
72+
# if [ ${#commit_message} -gt 72 ]; then
73+
# commit_message=$(echo $commit_message | cut -d' ' -f1-18)
74+
# fi
4275

4376
# Echo the commit message
4477
echo $commit_message
4578

79+
$commit_message == null ? commit_message="Updated ${base_branch} branch" : echo "Commit message: $commit_message"
80+
4681
# Set the GIT_SSH_PASSPHRASE environment variables
4782
export COMMIT_MESSAGE="$commit_message"
4883
export TICKET="$ticket"

0 commit comments

Comments
 (0)