diff --git a/aicommit.clj b/aicommit.clj old mode 100644 new mode 100755 index 9007ee3..ca88138 --- a/aicommit.clj +++ b/aicommit.clj @@ -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*))