Skip to content

Commit

Permalink
add keystore, add restart to main
Browse files Browse the repository at this point in the history
  • Loading branch information
carbon-hvze committed Jul 12, 2023
1 parent 4696075 commit f9d20b9
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
.calva
.lsp
.vscode
keystore/
19 changes: 10 additions & 9 deletions src/zd/fs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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
Expand Down
106 changes: 57 additions & 49 deletions src/zd/gitsync.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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})))))
7 changes: 5 additions & 2 deletions src/zd/system.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
2 changes: 1 addition & 1 deletion test/zd/fs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions zrc/zd/demo.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f9d20b9

Please sign in to comment.