From 4e9a32f501c7050c9b50a941d8ecdbf134d21926 Mon Sep 17 00:00:00 2001 From: anaxios Date: Sat, 10 Aug 2024 11:40:21 -0700 Subject: [PATCH] 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. --- aicommit.clj | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) mode change 100644 => 100755 aicommit.clj 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*))