Skip to content

Instantly share code, notes, and snippets.

@CasperEngl
Last active March 13, 2024 15:29
Show Gist options
  • Select an option

  • Save CasperEngl/a83cb5fd9172ef5b6fb43846c9955424 to your computer and use it in GitHub Desktop.

Select an option

Save CasperEngl/a83cb5fd9172ef5b6fb43846c9955424 to your computer and use it in GitHub Desktop.
OCM (Ollama Commit Message)
#!/bin/bash
ollama_commit_message_prompt="Generate a concise git commit message written in present tense for the following code diff with the given specifications: 1: Commit message must be a maximum of 255 characters. 2: One line of text and no lists of changes. 3: Exclude anything unnecessary such as translation. Your entire response will be passed directly into git commit."
ollama_commit_message_model="mistral"
if [ -n "$OCM_COMMIT_MESSAGE_PROMPT" ]; then
ollama_commit_message_prompt="$OCM_COMMIT_PROMPT"
fi
if [ -n "$OCM_COMMIT_MESSAGE_MODEL" ]; then
ollama_commit_message_model="$OCM_COMMIT_MESSAGE_MODEL"
fi
git_diff=$(git diff --staged)
if command -v ollama &>/dev/null; then
if [ -n "$git_diff" ]; then
commit_message=$(ollama run "$ollama_commit_message_model" "$ollama_commit_message_prompt $git_diff")
echo -e "Commit message: \n\n $commit_message\n"
read -p "Is the commit message correct? (Y/n) " confirm
if [ "${confirm:-y}" == "y" ]; then
git commit -m "$commit_message"
else
echo "Commit aborted."
fi
else
echo "No changes to commit"
fi
else
echo "Ollama is not installed. Please install it before running this script. https://ollama.com/"
fi
@CasperEngl
Copy link
Copy Markdown
Author

CasperEngl commented Mar 13, 2024

Install

Place the file in your bin directory: /usr/local/bin/ocm

Run chmod to make the file executable: chmod +x /usr/local/bin/ocm

Customize prompt

Add the following to your .bashrc or .zshrc.

export OCM_COMMIT_MESSAGE_PROMPT="Your custom prompt to pass into OCM"

Customize model

Pick any of the supported models from Ollama's model library: https://github.com/ollama/ollama?tab=readme-ov-file#model-library

Add the following to your .bashrc or .zshrc.

export OCM_COMMIT_MESSAGE_MODEL="mistral"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment