From f9d20b93ddf66820f55733296508573136a8a0fe Mon Sep 17 00:00:00 2001 From: carbon-hvze <6391474+carbon-hvze@users.noreply.github.com> Date: Wed, 12 Jul 2023 15:11:26 +0300 Subject: [PATCH] add keystore, add restart to main --- .gitignore | 1 + src/zd/fs.clj | 19 ++++---- src/zd/gitsync.clj | 106 ++++++++++++++++++++++++-------------------- src/zd/system.clj | 7 ++- test/zd/fs_test.clj | 2 +- zrc/zd/demo.edn | 2 + 6 files changed, 76 insertions(+), 61 deletions(-) diff --git a/.gitignore b/.gitignore index 7668507..e3bc71f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ .calva .lsp .vscode +keystore/ diff --git a/src/zd/fs.clj b/src/zd/fs.clj index 1be4389..ba51c61 100644 --- a/src/zd/fs.clj +++ b/src/zd/fs.clj @@ -9,8 +9,8 @@ [clojure.java.io :as io] [zen.core :as zen])) -(defn get-repo [ztx] - (->> [:zen/state :zd.fs :state :remote :repo] +(defn get-gistate [ztx] + (->> [:zen/state :zd.fs :state :remote :gistate] (get-in @ztx))) (defn get-state [ztx] @@ -67,7 +67,7 @@ fs-delete (utils/safecall ztx (fn [ag] (io/delete-file filepath) - (when-let [repo (get-repo ztx)] + (when-let [repo (get-gistate ztx)] (gitsync/delete-doc ztx repo {:docpath filepath :docname docname})) ;; TODO implement deletion of a single document (reload ztx r pths)) @@ -102,7 +102,7 @@ {:type :zd.fs/save-error}) fs-reload (utils/safecall ztx (fn [ag] - (when-let [repo (get-repo ztx)] + (when-let [repo (get-gistate ztx)] (gitsync/commit-doc ztx repo {:docpath filepath :docname docname})) (memstore/eval-macros! ztx) 'ok) @@ -115,12 +115,13 @@ [ztx {zd-config :zendoc :as config} & args] ;; TODO impl graceful shutdown if start is not possible (let [{:keys [remote root paths pull-rate]} (zen/get-symbol ztx zd-config) - init-remote* (utils/safecall ztx gitsync/init-remote {:type :gitsync/remote-init-error}) - repo (-> (init-remote* ztx remote) (:result)) + {repo :repo :as gistate} + (-> ((utils/safecall ztx gitsync/init-remote {:type :gitsync/remote-init-error}) ztx remote) + (:result)) reload-fn* (fn [ag] - (let [pr* (utils/safecall ztx gitsync/sync-remote {:type :gitsync/pull-remote-error}) - {st :status} (-> (pr* ztx repo) (:result))] + (let [sync-fn* (utils/safecall ztx gitsync/sync-remote {:type :gitsync/pull-remote-error}) + {st :status} (-> (sync-fn* ztx gistate) (:result))] (when (= :updated st) (reload ztx root paths)) 'reload-complete)) @@ -140,7 +141,7 @@ {:ag queue :paths paths :root root - :remote (assoc remote :repo repo)}) + :remote (assoc remote :gistate gistate)}) ;; TODO if no git repo schedule init retry {:ag queue :root root diff --git a/src/zd/gitsync.clj b/src/zd/gitsync.clj index d55fe73..e0889b0 100644 --- a/src/zd/gitsync.clj +++ b/src/zd/gitsync.clj @@ -3,55 +3,63 @@ [clojure.java.io :as io] [clj-jgit.porcelain :as git])) -(defn commit-doc [ztx repo {p :docpath d :docname}] - (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)] - (doseq [m (into untracked modified)] - (when (str/includes? p m) - (let [uname (or (.getString git-config "user" nil "name") "unknown editor") - email (or (.getString git-config "user" nil "email") "unknown-editor@example.com")] - (git/git-add repo m) - (let [msg (if (contains? untracked m) - (str "Create " d) - (str "Edit " d))] - (git/git-commit repo msg :committer {:name uname :email email}))))))) +(defn commit-doc [ztx {:keys [repo ident]} {p :docpath d :docname}] + (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)] + (doseq [m (into untracked modified)] + (when (str/includes? p m) + (let [uname (or (.getString git-config "user" nil "name") "unknown editor") + email (or (.getString git-config "user" nil "email") "unknown-editor@example.com")] + (git/git-add repo m) + (let [msg (if (contains? untracked m) + (str "Create " d) + (str "Edit " d))] + (git/git-commit repo msg :committer {:name uname :email email})))))))) -(defn delete-doc [ztx repo {p :docpath d :docname}] - (let [{:keys [missing]} (git/git-status repo) - git-config (git/git-config-load repo) - uname (or (.getString git-config "user" nil "name") "unknown editor") - email (or (.getString git-config "user" nil "email") "unknown-editor@example.com")] - (doseq [m missing] - (when (str/includes? p m) - (git/git-rm repo m) - (git/git-commit repo (str "Delete " d) :committer {:name uname :email email}))))) +(defn delete-doc [ztx {:keys [repo ident]} {p :docpath d :docname}] + (git/with-identity ident + (let [{:keys [missing]} (git/git-status repo) + git-config (git/git-config-load repo) + uname (or (.getString git-config "user" nil "name") "unknown editor") + email (or (.getString git-config "user" nil "email") "unknown-editor@zendoc.me")] + (doseq [m missing] + (when (str/includes? p m) + (git/git-rm repo m) + (git/git-commit repo (str "Delete " d) :committer {:name uname :email email})))))) -(defn sync-remote [ztx repo] - (let [pull-result (git/git-pull repo)] - ;; TODO resolve merge conflicts - (when (.isSuccessful pull-result) - (let [updated? (-> (.getFetchResult pull-result) - (.getTrackingRefUpdates) - (.isEmpty) - (not))] - ;; TODO check index status? - (git/git-push repo) - (when updated? - (println :zd.gitsync/sync-reload) - {:status :updated}))))) +(defn sync-remote [ztx {repo :repo ident :ident}] + (git/with-identity ident + (let [pull-result (git/git-pull repo)] + ;; TODO resolve merge conflicts + (when (.isSuccessful pull-result) + (let [updated? (-> (.getFetchResult pull-result) + (.getTrackingRefUpdates) + (.isEmpty) + (not))] + ;; TODO check index status? + (git/git-push repo) + (when updated? + (println :zd.gitsync/sync-reload) + {:status :updated})))))) -(defn init-remote [ztx {:keys [from branch to] :as remote}] - (when (string? to) - (let [pulled? (.exists (io/file to)) - repo (if pulled? - (git/load-repo to) - (git/git-clone from :dir to))] - ;; TODO add create/checkout default branch if necessary? - (when branch - (git/git-checkout repo branch)) - (git/git-pull repo) - #_(when-not pulled? - (git/git-submodule-init repo)) - #_(git/git-submodule-update repo :strategy :recursive) - repo))) +(defn init-remote [ztx {:keys [from branch keystore to] k :key :as remote}] + (let [ident {:name "pubkey"#_(or k ["id_rsa" "id_dsa" "id_ecdsa" "id_ed25519"]) + ;; TODO support for other os + :trust-all? true + :key-dir (or keystore "~/.ssh")}] + (when (string? to) + (git/with-identity ident + (let [pulled? (.exists (io/file to)) + repo (if pulled? + (git/load-repo to) + (git/git-clone from :dir to))] + ;; TODO add checkout branch if necessary? + (when branch + (git/git-checkout repo branch)) + (git/git-pull repo) + #_(when-not pulled? + (git/git-submodule-init repo)) + #_(git/git-submodule-update repo :strategy :recursive) + {:ident ident :repo repo}))))) diff --git a/src/zd/system.clj b/src/zd/system.clj index 736daa5..9ed418b 100644 --- a/src/zd/system.clj +++ b/src/zd/system.clj @@ -2,7 +2,8 @@ (:require [zen.core :as zen] [zd.api :as api])) -(defn start [ztx] +(defn restart [ztx] + (zen/stop-system ztx) (zen/read-ns ztx 'zd) (zen/read-ns ztx 'zd.demo) (zen/start-system ztx 'zd.demo/system) @@ -14,10 +15,12 @@ (defn -main [& opts] (let [ztx (zen/new-context {})] (reset! dtx ztx) - (start ztx))) + (restart ztx))) (comment (-main) + (restart @dtx) + (zen/stop-system @dtx)) diff --git a/test/zd/fs_test.clj b/test/zd/fs_test.clj index b23730d..60f5f50 100644 --- a/test/zd/fs_test.clj +++ b/test/zd/fs_test.clj @@ -152,7 +152,7 @@ (is (nil? (agent-errors fs/queue))) ;; repo is ready - (is (instance? org.eclipse.jgit.api.Git (get-in st [:remote :repo]))) + (is (instance? org.eclipse.jgit.api.Git (get-in st [:remote :gistate :repo]))) (matcho/assert {:status 200} diff --git a/zrc/zd/demo.edn b/zrc/zd/demo.edn index 19b3a08..9047ac6 100644 --- a/zrc/zd/demo.edn +++ b/zrc/zd/demo.edn @@ -11,6 +11,8 @@ :remote ;; TODO think about how to make the test reproducible {:from "git@github.com:zen-lang/zendoc.git" + :key "pubkey" + :keystore "keystore" :to "."}} datalog