Added dependency on babashka.fs and babashka.cli.

Introduced functions to find git repository and read/write commit message.
Modified response processing to append to COMMIT_EDITMSG and open in editor.
This commit is contained in:
ilguappo 2024-08-10 11:40:21 -07:00
parent 5fb26e6220
commit 4e9a32f501
Signed by: ilguappo
GPG Key ID: 98486F29C8969FD0

19
aicommit.clj Normal file → Executable file
View File

@ -2,7 +2,9 @@
(require '[babashka.curl :as curl]
'[babashka.process :as p]
'[cheshire.core :as json])
'[cheshire.core :as json]
'[babashka.fs :as fs]
'[babashka.cli :as cli])
(def url "https://api.groq.com/openai/v1/chat/completions")
@ -16,6 +18,17 @@ Only reply with the commit message and nothing else.
(def diff (:out (p/check (p/sh "git" "diff" "--cached"))))
(defn find-git
[dir]
(loop [d dir]
(if (fs/directory? (fs/path d ".git"))
(fs/path d ".git")
(recur (fs/parent d)))))
(def commit-message-path (fs/unixify (fs/path (find-git (fs/cwd)) "COMMIT_EDITMSG")))
(def commit-message (slurp commit-message-path))
(def message (json/generate-string
{:messages [{:role "system" :content system-msg}
{:role "user" :content diff}]
@ -34,4 +47,6 @@ Only reply with the commit message and nothing else.
(def content (get-in (:choices (json/parse-string (:body response) true)) [0 :message :content]))
(println content)
(spit commit-message-path (str content "\n" commit-message))
(p/shell (System/getenv "EDITOR") (first *command-line-args*))