Last active
August 27, 2024 02:27
-
-
Save wipash/1d9a92006643ac5823219c5c6f20da89 to your computer and use it in GitHub Desktop.
AI-powered git commit message generator, for Fish shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gcm | |
# Check if llm is installed, if not, install it | |
if not type -q llm | |
echo "'llm' is not installed. Attempting to install it using brew..." | |
if brew install llm | |
echo "'llm' installed successfully." | |
else | |
echo "Failed to install 'llm'. Please install it manually and try again." | |
return 1 | |
end | |
end | |
# Check if an API key is set up for llm | |
set llm_keys (llm keys) | |
if test "$llm_keys" = "No keys found" | |
echo "No API key found for 'llm'. You need to set it up." | |
llm keys set openai | |
if test $status -ne 0 | |
echo "Failed to set up the OpenAI key. Please try again manually." | |
return 1 | |
else | |
echo "OpenAI key set successfully." | |
end | |
end | |
# Function to generate commit message | |
function generate_commit_message | |
set -l prompt_text " | |
Below is a diff of all changes, coming from the command: | |
\`\`\` | |
git diff -U1000 --cached | |
\`\`\` | |
Please generate a concise, one-line commit message that accurately summarises all the changes, following the | |
Conventional Commits style. | |
Follow this by an empty line, then a short, terse description in one sentences of the changes contained within | |
this commit, only covering detail not included in the summary. The description line should contain no capitals | |
or punctuation, and be as brief and blunt as possible. | |
Don't wrap your response in backticks or any other characters. | |
---------- | |
" | |
set -l diff_output | |
if contains -- -a $argv | |
set diff_output (git diff -U1000 HEAD) | |
else | |
set diff_output (git diff -U1000 --cached) | |
end | |
set prompt_text "$prompt_text$diff_output" | |
echo $prompt_text | llm | string collect | |
end | |
# Function to read user input | |
function read_input | |
set -l prompt $argv[1] | |
read -P $prompt reply | |
echo $reply | |
end | |
# Main script | |
echo "Generating AI-powered commit message..." | |
set commit_message (generate_commit_message) | |
while true | |
echo -e "Proposed commit message:\n" | |
echo -e "$commit_message" | |
echo -e "" | |
set choice (read_input "Do you want to (c)ancel, (a)ccept, (e)dit or (r)egenerate? ") | |
switch $choice | |
case 'a' 'A' '' | |
if echo "$commit_message" | git commit $argv --file - | |
echo "Changes committed successfully!" | |
return 0 | |
else | |
echo "Commit failed. Please check your changes and try again." | |
return 1 | |
end | |
case 'e' 'E' | |
set commit_message (read -P "Enter your commit message: " -m) | |
if [ -n "$commit_message" ] | |
if echo "$commit_message" | git commit $argv --file - | |
echo "Changes committed successfully with your message!" | |
return 0 | |
else | |
echo "Commit failed. Please check your message and try again." | |
return 1 | |
end | |
else | |
echo "Empty commit message. Please try again." | |
end | |
case 'r' 'R' | |
echo "Regenerating commit message..." | |
set commit_message (generate_commit_message) | |
case 'c' 'C' 'q' 'Q' | |
echo "Commit cancelled." | |
return 1 | |
case '*' | |
echo "Invalid choice. Please try again." | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment