Now stands as middleware between the git commit command and the default

editor.
This commit is contained in:
ilguappo 2024-08-08 01:30:10 -07:00
parent 455ae71cd3
commit f863bce0fa
Signed by: ilguappo
GPG Key ID: 98486F29C8969FD0

View File

@ -3,7 +3,8 @@ set -e
ARGS=("${@-}") ARGS=("${@-}")
TMP_FILE="/tmp/aicommit" TMP_FILE="/tmp/aicommit_tmp"
system=$( system=$(
cat - <<EOF cat - <<EOF
@ -14,21 +15,6 @@ Give detail for every change in the diff.
EOF 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 if ! diff=$(git diff --cached); then
echo "Failed to get diff." echo "Failed to get diff."
exit 1 exit 1
@ -41,26 +27,27 @@ message=$(
".message = $(jq -R -s '@json' <<<"${message_cat}")" ".message = $(jq -R -s '@json' <<<"${message_cat}")"
) )
# trunk-ignore(shellcheck/SC2091) echo ""
# trunk-ignore(shellcheck/SC2310) echo "Please wait for AI reponse..."
if $(hasFlag -r --reset); then
if ! curl -s -X POST https://infer.x64.world/reset-conversation >/dev/null; then if ! curl -s -X POST https://infer.x64.world/reset-conversation >/dev/null; then
echo "Failed to reset conversation." echo "Failed to reset conversation."
exit 1 exit 1
fi fi
fi
curl -s "https://infer.x64.world/chat" \ curl -s "https://infer.x64.world/chat" \
-X POST \ -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "${message}" | jq -r '.content | gsub("\""; "")' >"${TMP_FILE}" -d "${message}" | jq -r '.content | gsub("\""; "")' >"${TMP_FILE}"
# trunk-ignore(shellcheck/SC2181)
if [[ 0 -ne $? ]]; then if [[ 0 -ne $? ]]; then
echo "Failed to get response." echo "Failed to get response."
exit 1 exit 1
fi fi
git commit -e -m "$(cat "${TMP_FILE}")" && rm "${TMP_FILE}" cat ".git/COMMIT_EDITMSG" >>"${TMP_FILE}"
cat "${TMP_FILE}" >".git/COMMIT_EDITMSG"
popd >/dev/null rm "${TMP_FILE}"
"${EDITOR}" "$@"