Skip to content

Commit cf0f41f

Browse files
committed
TEST-1234 feat: Use Gemini API for commit message generation
This commit introduces a new feature that uses the Gemini API to generate commit messages based on the git diff. The API request includes the diff stringified for easier processing. The generated message is then used for the git commit.
1 parent f19171d commit cf0f41f

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

git-commit-push-script.sh

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,29 @@ base_branch=$(git rev-parse --abbrev-ref HEAD)
1010
# Extract ticket number from current directory
1111
ticket=$(echo $base_branch | grep -o -E '([A-Za-z]+-[0-9]{3,}|[A-Za-z]+-[0-9]{3,})')
1212

13-
# Prompt for commit message
14-
read -p "Enter commit message: " message
15-
echo "Commit message: $ticket $message"
13+
echo "Ticket: $ticket"
14+
15+
# Get the git diff
16+
diff=$(git diff --cached)
17+
18+
# Stringify the diff
19+
diff=$(echo $diff | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/\n/\\n/g')
20+
21+
# Prepare the Gemini API request
22+
gemini_request='{"contents":[{"parts":[{"text": "Write a git commit message (72 character maximum) for the following git diff: '"$diff"' "}]}]}'
23+
24+
# Get commit message from Gemini API
25+
commit_message=$(curl \
26+
-H 'Content-Type: application/json' \
27+
-d "$gemini_request" \
28+
-X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY}" \
29+
| jq -r '.candidates[0].content.parts[0].text'
30+
)
31+
32+
echo "$commit_message"
1633

1734
# Prepare and execute commit command
18-
git commit -S -m "$ticket $message"
35+
git commit -S -m "$ticket $commit_message"
1936

2037
# Check if the branch exists on the remote
2138
remote_branch=$(git ls-remote --heads origin $base_branch)

0 commit comments

Comments
 (0)