Last active
May 28, 2025 09:57
-
-
Save sebnyberg/8a85341a805d7d51a353cc2bd6a51a54 to your computer and use it in GitHub Desktop.
GH Copilot CLI: suggest git commit message
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
#!/usr/bin/env bash | |
# | |
# Suggest a git commit using GitHub Copilot CLI. | |
# | |
# ./ghcommit [...flags] | |
# | |
max_characters=10000 | |
# check if gh is installed | |
if ! command -v gh &> /dev/null; then | |
echo "GitHub CLI (gh) is not installed. Please install it first:" | |
echo "https://cli.github.com/" | |
exit 1 | |
fi | |
# Check if gh copilot is installed | |
if ! gh extension list | grep -q copilot; then | |
echo "GitHub Copilot CLI is not installed. Please install it first:" | |
echo "" | |
echo "gh auth login" | |
echo "gh extension install github/gh-copilot" | |
exit 1 | |
fi | |
# Check if awk is installed | |
if ! command -v gawk &> /dev/null; then | |
echo "gawk is not installed. Please install it first:" | |
echo "" | |
echo "brew install gawk" | |
exit 1 | |
fi | |
# Check if the current directory is a git repository | |
if ! git rev-parse --is-inside-work-tree &> /dev/null; then | |
echo "This script must be run inside a git repository." | |
exit 1 | |
fi | |
args="$@" | |
filestat=$(git diff --stat --cached) | |
mkprompt() { | |
local prompt="$(cat <<EOF | |
You are a GitHub Copilot AI assistant that helps users write clear and concise commit messages for their git changes. | |
The commit message should follow these guidelines: | |
- Start with a short summary of the changes (50 characters or less). | |
- Use the imperative mood (e.g., "Fix bug" instead of "Fixed bug"). | |
- Use the present tense (e.g., "Add feature" instead of "Added feature"). | |
- Use semantic commit conventions. | |
- Focus on the "what" and "why" of the changes, not the "how". | |
- Avoid filler words and unnecessary details. | |
- Use bullet points to list key changes. | |
- Use as few bullet points as possible, possibly none. | |
- Refer to relevant files in the list of changes. | |
Store the commit message in a variable called \`msg\` and use it to commit the changes with the following git command: | |
git commit -e ${args} -m \$msg | |
### CONTEXT ### | |
File changes: | |
${filestat} | |
Diff of staged changes: | |
${1} | |
EOF | |
)" | |
echo $prompt | |
} | |
diff=$(git diff --staged --diff-filter=d) | |
if [[ -z "$diff" ]]; then | |
echo "No changes are staged." | |
exit 1 | |
fi | |
prompt=$(mkprompt "$diff") | |
# Ensure that the prompt does not exceed the maximum character limit | |
if [[ ${#prompt} -gt $max_characters ]]; then | |
echo "The prompt exceeds the maximum character limit of $max_characters characters." | |
echo "Remaking the prompt to prioritise files with small changes, this may take a while..." | |
diff="" | |
for file in $(git diff --staged --numstat --diff-filter=d | sort -r | gawk '{ print $3 }'); do | |
diff+="\n$(git diff --staged "$file")" | |
done | |
prompt=$(mkprompt "$diff") | |
prompt="${prompt:0:$max_characters}" | |
fi | |
# If ghcs is available, use it | |
if command -v ghcs &> /dev/null; then | |
echo "Using GitHub Copilot CLI (ghcs) to suggest commit message..." | |
ghcs suggest -t shell "${prompt}" | |
exit 0 | |
else | |
gh copilot suggest -t shell "${prompt}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment