Last active
October 24, 2024 23:50
-
-
Save JakobBruenker/d8db773475f86236acf7655beffc7826 to your computer and use it in GitHub Desktop.
JJ bookmark names via GPT
This file contains 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 | |
# Generate bookmark names bases on jj change descriptions | |
# init vars | |
api_key="${OPENAI_API_KEY}" | |
base_rev="${1:-trunk()}" | |
if [ -z "$api_key" ]; then | |
echo "Error: OPENAI_API_KEY environment variable not set" >&2 | |
exit 1 | |
fi | |
# show actual trunk bookmark if default was used | |
if [ "$#" -eq 0 ]; then | |
trunk_bookmark=$(jj log -r 'trunk()' -T 'local_bookmarks' --ignore-working-copy --no-graph) | |
echo "Using default base revision: $trunk_bookmark" | |
fi | |
# get commit msgs since merge base | |
commit_msgs=$(jj log -T 'description ++ "-----\n"' --no-graph -r "heads(::@ & ::$base_rev)..@") | |
if [ -z "$commit_msgs" ]; then | |
echo "Error: No commits found between @ and $base_rev" >&2 | |
exit 1 | |
fi | |
# build request json properly with jq | |
request=$(jq -n \ | |
--arg msgs "$commit_msgs" \ | |
--arg sys_prompt "You are a helpful assistant that generates git/jj branch names. Generate short, descriptive branch names based on commit messages. Return JSON with a single 'branch_name' field. Use kebab-case. Never include ticket numbers. Keep names under 40 chars." \ | |
'{ | |
model: "gpt-4o-mini", | |
messages: [ | |
{ | |
role: "system", | |
content: $sys_prompt | |
}, | |
{ | |
role: "user", | |
content: ("Generate a branch name based on these commit messages:\n\n" + $msgs) | |
} | |
], | |
response_format: { type: "json_object" }, | |
temperature: 0.7 | |
}') | |
# call api | |
response=$(curl -s -X POST \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $api_key" \ | |
-d "$request" \ | |
https://api.openai.com/v1/chat/completions) | |
# extract name with better error handling | |
if ! suggested_name=$(echo "$response" | jq -r '.choices[0].message.content' | jq -r '.branch_name' 2>/dev/null); then | |
echo "Error: Failed to parse API response" >&2 | |
echo "Raw response: $response" >&2 | |
exit 1 | |
fi | |
if [ "$suggested_name" = "null" ] || [ -z "$suggested_name" ]; then | |
echo "Error: API returned empty bookmark name" >&2 | |
echo "Raw response: $response" >&2 | |
exit 1 | |
fi | |
suggested_name=$(echo "$suggested_name" | sed 's/[^a-zA-Z0-9-]/-/g') | |
# show suggestion & get user input | |
echo -e "\nSuggested bookmark name: $suggested_name" | |
echo -n "Accept suggested name? (press enter to accept or type new name): " | |
read -r user_input | |
branch_name=${user_input:-$suggested_name} | |
# create bookmark | |
jj bookmark create "$branch_name" | |
echo "Created bookmark '$branch_name'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment