|
5 | 5 | #!/bin/bash
|
6 | 6 | source ~/.bash_profile
|
7 | 7 |
|
| 8 | +# Pre-warm the model (optional - keeps it loaded) |
| 9 | +ollama run mistral-commit "test" > /dev/null 2>&1 & |
| 10 | + |
8 | 11 | # Stage all changes
|
9 | 12 | git add -A
|
10 | 13 |
|
11 | 14 | # Get the branch name
|
12 | 15 | 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" |
13 | 21 |
|
14 | 22 | # Extract Jira ticket number from current directory
|
15 | 23 | ticket=$(echo $base_branch | grep -o -E '([A-Za-z]+-[0-9]{3,}|[A-Za-z]+-[0-9]{3,})')
|
16 | 24 |
|
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") |
19 | 37 |
|
20 |
| -# Default model (change if desired) |
21 |
| -MODEL="mistral" |
| 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 |
22 | 43 |
|
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") |
| 44 | +# Stringify the diff |
| 45 | +diff=$(echo $diff | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/\n/\\n/g') |
25 | 46 |
|
26 |
| -# Run the model and capture output |
27 |
| -COMMIT_MSG=$(echo "$PROMPT" | ollama run "$MODEL") |
| 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."}]}]}' |
28 | 49 |
|
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." |
| 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 | + ) |
| 57 | + |
| 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." |
32 | 65 | exit 1
|
33 | 66 | fi
|
34 | 67 |
|
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') |
37 | 70 |
|
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 |
42 | 75 |
|
43 | 76 | # Echo the commit message
|
44 | 77 | echo $commit_message
|
|
0 commit comments