aicommit/aicommit

67 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
set -e
ARGS=("${@-}")
TMP_FILE="/tmp/aicommit"
system=$(
cat - <<EOF
You are an expert programmer that values clear, unambiguous communication and are specialized in generating concise and informative git commit messages.
Only reply with the commit message and nothing else.
Give detail for every change in the diff.
EOF
)
pushd "$(pwd)" >/dev/null
hasFlag() {
local flags=("$@")
for var in "${ARGS[@]}"; do
for flag in "${flags[@]}"; do
if [[ ${flag} == "${var}" ]]; then
echo 'true'
return
fi
done
done
echo 'false'
}
if ! diff=$(git diff --cached); then
echo "Failed to get diff."
exit 1
fi
message_cat="${system} diff: ${diff}"
message=$(
jq -n \
".message = $(jq -R -s '@json' <<<"${message_cat}")"
)
# trunk-ignore(shellcheck/SC2091)
# trunk-ignore(shellcheck/SC2310)
if $(hasFlag -r --reset); then
if ! curl -s -X POST https://infer.x64.world/reset-conversation >/dev/null; then
echo "Failed to reset conversation."
exit 1
fi
fi
curl -s "https://infer.x64.world/chat" \
-X POST \
-H "Content-Type: application/json" \
-d "${message}" | jq -r '.content | gsub("\""; "")' >"${TMP_FILE}"
# trunk-ignore(shellcheck/SC2181)
if [[ 0 -ne $? ]]; then
echo "Failed to get response."
exit 1
fi
git commit -e -m "$(cat "${TMP_FILE}")" && rm "${TMP_FILE}"
popd >/dev/null