diff --git a/src/zd/core.clj b/src/zd/core.clj index c1b6d64..a21538f 100644 --- a/src/zd/core.clj +++ b/src/zd/core.clj @@ -10,6 +10,8 @@ [ring.util.codec] [zd.view.core :as view]) (:import [org.httpkit BytesInputStream] + [org.eclipse.jgit.revwalk RevWalk] + [org.eclipse.jgit.diff DiffFormatter] [java.io ByteArrayOutputStream])) @@ -207,17 +209,14 @@ (def ztx (start)) - (stop ztx) (:zd/backlinks @ztx) - (def out (ByteArrayOutputStream.)) + (git/exec {:dir (:zd/dir @ztx) :exec ["git" "log" "-1"]}) - (-> (.diff (:zd/repo @ztx)) - (.setShowNameAndStatusOnly true) - (.setOutputStream out) - (.call)) + (git/exec {:dir (:zd/dir @ztx) :exec ["git" "diff" "--name-only" "4a569701e759120e8638eb46f8bedd4964b3250a" + "7b7fa19bd92422ab9a2544599a1deb1360a19a25"]}) (config ztx) (store/re-validate ztx) diff --git a/src/zd/store.clj b/src/zd/store.clj index d6a6f4e..e28167f 100644 --- a/src/zd/store.clj +++ b/src/zd/store.clj @@ -1,5 +1,6 @@ (ns zd.store (:require [zen.core :as zen] + [zd.git :as zgit] [clj-jgit.porcelain :as git] [zd.parser] [xtdb.api :as xt] @@ -105,7 +106,10 @@ (defn put-doc [ztx {docname :zd/docname :as doc}] (if (and doc docname) - (swap! ztx assoc-in [:zdb docname] (assoc doc :zd/parent (parent-name docname))) + (let [doc* (assoc doc :zd/parent (parent-name docname))] + (swap! ztx assoc-in [:zdb docname] doc*) + (zen/pub ztx 'zd.events/on-doc-load doc*)) + ;; TODO emit zen event (println :put/error doc)) doc) @@ -390,6 +394,15 @@ (extract-docname ":a 1\n:zd/docname docname\n:b 1") +(defn last-commit-hash [ztx] + (->> {:dir (:zd/dir @ztx) :exec ["git" "log" "-1"]} + (zgit/exec) + (:stdout) + (first) + (drop-while #(not= % \space)) + (rest) + (apply str))) + (defn commit-changes "commit added, changed files and push" [ztx docpath docname] @@ -397,8 +410,15 @@ (git/with-identity ident (let [;; TODO sync all untracked docs at gitsync start? {:keys [untracked modified] :as status} (git/git-status repo) - git-config (git/git-config-load repo)] + git-config (git/git-config-load repo) + ;; TODO use jgit instead of execs, understand interop + ours-last (last-commit-hash ztx)] (git/git-pull repo {:ff-mode :ff :rebase-mode :none :strategy :ours}) + (let [their-last (last-commit-hash ztx)] + (when (not= ours-last their-last) + (zgit/exec {:dir (:zd/dir @ztx) + :exec ["git" "diff" "--name-only" their-last ours-last]}))) + (doseq [m (into untracked modified)] (when (str/includes? docpath m) (let [uname (or (.getString git-config "user" nil "name") "unknown editor") @@ -486,6 +506,7 @@ "read docs from filesystem and load into memory" [ztx & [dir]] (let [dir (or dir (:zd/dir @ztx))] + (zen/pub ztx 'zd.events/on-load-start {}) (load-meta ztx) (load-repo ztx) (->> (dir-read ztx dir) @@ -497,7 +518,8 @@ (fn [_docname doc] (let [idoc (doc-inference ztx doc)] (re-index-doc ztx idoc) - idoc))))) + idoc))) + (zen/pub ztx 'zd.events/on-load-complete {}))) (defn menu "return navigation" diff --git a/zrc/zd.edn b/zrc/zd.edn index 6ad8509..90fad64 100644 --- a/zrc/zd.edn +++ b/zrc/zd.edn @@ -1,29 +1,10 @@ {:ns zd :import #{zen-web zd.engines zd.events} - remote - {:zen/tags #{zen/schema} - :type zen/map - ;; TODO use zen-web/url - :require #{:from} - :keys {:from {:type zen/string} - :key {:type zen/string - :zen/desc "private ssh key to use"} - :keystore {:type zen/string - :zen/desc "ssh keys directory"} - :to {:type zen/string} - :branch {:type zen/string}}} - zendoc-config {:zen/tags #{zen/tag zen/schema} :type zen/map - :keys {:root {:type zen/string} - :paths {:type zen/vector - :every {:type zen/string}} - :pull-rate {:type zen/integer} - :remote {:confirms #{remote}} - :remotes {:type zen/vector - :every {:confirms #{remote}}}}} + :keys {:dir {:type zen/string}}} query {:zen/tags #{zen/op} @@ -136,7 +117,7 @@ :api api} zendoc - {:zen/tags #{zen/start} + {:zen/tags #{zen/start zendoc-config} :zen/state-key :zd/config :dir "docs"} diff --git a/zrc/zd/events.edn b/zrc/zd/events.edn index 1e8f1c3..f57f561 100644 --- a/zrc/zd/events.edn +++ b/zrc/zd/events.edn @@ -5,11 +5,6 @@ :zen/desc "event when key rendering throws exception" :type zen/map} - sub - {:zen/tags #{zen/tag zen/schema} - :type zen/map - :keys {:config {:type zen/symbol :tags #{zd/zendoc}}}} - on-doc-save {:zen/tags #{zen/event zen/schema} :zen/desc "save document in an api"