Make all calls to git require the graph path to operate on

pull/10934/head
Vladimir Pouzanov 2024-02-18 20:53:59 +00:00 committed by Andelf
parent 0bb7e6d0cc
commit 227f3efc85
5 changed files with 75 additions and 75 deletions

View File

@ -13,8 +13,8 @@
(def log-error (partial logger/error "[Git]")) (def log-error (partial logger/error "[Git]"))
(defn get-graph-git-dir (defn get-graph-git-dir
[] [graph-path]
(when-let [graph-path (some-> (state/get-graph-path) (when-let [graph-path (some-> graph-path
(string/replace "/" "_") (string/replace "/" "_")
(string/replace ":" "comma"))] (string/replace ":" "comma"))]
(let [dir (.join node-path (.homedir os) ".logseq" "git" graph-path ".git")] (let [dir (.join node-path (.homedir os) ".logseq" "git" graph-path ".git")]
@ -22,38 +22,35 @@
dir))) dir)))
(defn run-git! (defn run-git!
[commands] [graph-path commands]
(when-let [path (state/get-graph-path)] (when (and graph-path (fs/existsSync graph-path))
(when (fs/existsSync path) (p/let [result (.exec GitProcess commands graph-path)]
(p/let [result (.exec GitProcess commands path)] (if (zero? (gobj/get result "exitCode"))
(if (zero? (gobj/get result "exitCode")) (let [result (gobj/get result "stdout")]
(let [result (gobj/get result "stdout")] (p/resolved result))
(p/resolved result)) (let [error (gobj/get result "stderr")]
(let [error (gobj/get result "stderr")] (when-not (string/blank? error)
(when-not (string/blank? error) (log-error error))
(log-error error)) (p/rejected error))))))
(p/rejected error)))))))
(defn run-git2! (defn run-git2!
[commands] [graph-path commands]
(when-let [path (state/get-graph-path)] (when (and graph-path (fs/existsSync graph-path))
(when (fs/existsSync path) (p/let [^js result (.exec GitProcess commands graph-path)]
(p/let [^js result (.exec GitProcess commands path)] result)))
result))))
(defn git-dir-exists? (defn git-dir-exists?
[] [graph-path]
(try (try
(let [p (.join node-path (state/get-graph-path) ".git")] (let [p (.join node-path graph-path ".git")]
(.isDirectory (fs/statSync p))) (.isDirectory (fs/statSync p)))
(catch :default _e (catch :default _e
nil))) nil)))
(defn remove-dot-git-file! (defn remove-dot-git-file!
[] [graph-path]
(try (try
(let [graph-path (state/get-graph-path) (let [_ (when (string/blank? graph-path)
_ (when (string/blank? graph-path)
(utils/send-to-renderer :setCurrentGraph {}) (utils/send-to-renderer :setCurrentGraph {})
(throw (js/Error. "Empty graph path"))) (throw (js/Error. "Empty graph path")))
p (.join node-path graph-path ".git")] p (.join node-path graph-path ".git")]
@ -70,23 +67,23 @@
(log-error e)))) (log-error e))))
(defn init! (defn init!
[] [graph-path]
(let [_ (remove-dot-git-file!) (let [_ (remove-dot-git-file! graph-path)
separate-git-dir (get-graph-git-dir) separate-git-dir (get-graph-git-dir graph-path)
args (cond args (cond
(git-dir-exists?) (git-dir-exists? graph-path)
["init"] ["init"]
separate-git-dir separate-git-dir
["init" (str "--separate-git-dir=" separate-git-dir)] ["init" (str "--separate-git-dir=" separate-git-dir)]
:else :else
["init"])] ["init"])]
(p/let [_ (run-git! (clj->js args))] (p/let [_ (run-git! graph-path (clj->js args))]
(when utils/win32? (when utils/win32?
(run-git! #js ["config" "core.safecrlf" "false"]))))) (run-git! graph-path #js ["config" "core.safecrlf" "false"])))))
(defn add-all! (defn add-all!
[] [graph-path]
(-> (run-git! #js ["add" "--ignore-errors" "./*"]) (-> (run-git! graph-path #js ["add" "--ignore-errors" "./*"])
(p/catch (fn [error] (p/catch (fn [error]
(let [error (string/lower-case (str error))] (let [error (string/lower-case (str error))]
(if (or (string/includes? error "permission denied") (if (or (string/includes? error "permission denied")
@ -97,34 +94,38 @@
;; git log -100 --oneline -p ~/Desktop/org/pages/contents.org ;; git log -100 --oneline -p ~/Desktop/org/pages/contents.org
(defn commit! (defn commit!
[message] [graph-path message]
(p/do! (p/do!
(run-git! #js ["config" "core.quotepath" "false"]) (run-git! graph-path #js ["config" "core.quotepath" "false"])
(run-git! #js ["commit" "-m" message]))) (run-git! graph-path #js ["commit" "-m" message])))
(defn add-all-and-commit-single-graph!
[graph-path message]
(let [message (if (string/blank? message)
"Auto saved by Logseq"
message)]
(->
(p/let [_ (init! graph-path)
_ (add-all! graph-path)]
(commit! graph-path message))
(p/catch (fn [error]
(when (and
(string? error)
(not (string/blank? error)))
(if (string/starts-with? error "Author identity unknown")
(utils/send-to-renderer "setGitUsernameAndEmail" {:type "git"})
(utils/send-to-renderer "notification" {:type "error"
:payload (str error "\nIf you don't want to see those errors or don't need git, you can disable the \"Git auto commit\" feature on Settings > Version control.")}))))))))
(defn add-all-and-commit! (defn add-all-and-commit!
([] ([]
(add-all-and-commit! nil)) (add-all-and-commit! nil))
([message] ([message]
(let [message (if (string/blank? message) (doseq [path (state/get-all-graph-paths)] (add-all-and-commit-single-graph! path message))))
"Auto saved by Logseq"
message)]
(->
(p/let [_ (init!)
_ (add-all!)]
(commit! message))
(p/catch (fn [error]
(when (and
(string? error)
(not (string/blank? error)))
(if (string/starts-with? error "Author identity unknown")
(utils/send-to-renderer "setGitUsernameAndEmail" {:type "git"})
(utils/send-to-renderer "notification" {:type "error"
:payload (str error "\nIf you don't want to see those errors or don't need git, you can disable the \"Git auto commit\" feature on Settings > Version control.")})))))))))
(defn short-status! (defn short-status!
[] [graph-path]
(run-git! #js ["status" "--porcelain"])) (run-git! graph-path #js ["status" "--porcelain"]))
(defonce quotes-regex #"\"[^\"]+\"") (defonce quotes-regex #"\"[^\"]+\"")
(defn wrapped-by-quotes? (defn wrapped-by-quotes?
@ -152,8 +153,8 @@
(remove string/blank?)))) (remove string/blank?))))
(defn raw! (defn raw!
[args] [graph-path args]
(init!) (init! graph-path)
(let [args (if (string? args) (let [args (if (string? args)
(split-args args) (split-args args)
args) args)
@ -166,8 +167,8 @@
(p/rejected error)))] (p/rejected error)))]
(-> (->
(p/let [_ (when (= (first args) "commit") (p/let [_ (when (= (first args) "commit")
(add-all!)) (add-all! graph-path))
result (run-git! (clj->js args))] result (run-git! graph-path (clj->js args))]
(p/resolved result)) (p/resolved result))
(p/catch error-handler)))) (p/catch error-handler))))

View File

@ -461,7 +461,6 @@
(let [old-path (state/get-window-graph-path window)] (let [old-path (state/get-window-graph-path window)]
(when (and old-path graph-path (not= old-path graph-path)) (when (and old-path graph-path (not= old-path graph-path))
(close-watcher-when-orphaned! window old-path)) (close-watcher-when-orphaned! window old-path))
(swap! state/state assoc :graph/current graph-path)
(swap! state/state assoc-in [:window/graph window] graph-path) (swap! state/state assoc-in [:window/graph window] graph-path)
nil)) nil))
@ -469,14 +468,14 @@
(when graph-name (when graph-name
(set-current-graph! window (utils/get-graph-dir graph-name)))) (set-current-graph! window (utils/get-graph-dir graph-name))))
(defmethod handle :runGit [_ [_ args]] (defmethod handle :runGit [_ [_ {:keys [repo command]}]]
(when (seq args) (when (seq command)
(git/raw! args))) (git/raw! (utils/get-graph-dir repo) command)))
(defmethod handle :runGitWithinCurrentGraph [_ [_ args]] (defmethod handle :runGitWithinCurrentGraph [_ [_ {:keys [repo command]}]]
(when (seq args) (when (seq command)
(git/init!) (git/init! (utils/get-graph-dir repo))
(git/run-git2! (clj->js args)))) (git/run-git2! (utils/get-graph-dir repo) (clj->js command))))
(defmethod handle :runCli [window [_ {:keys [command args returnResult]}]] (defmethod handle :runCli [window [_ {:keys [command args returnResult]}]]
(try (try
@ -499,8 +498,8 @@
(defmethod handle :gitCommitAll [_ [_ message]] (defmethod handle :gitCommitAll [_ [_ message]]
(git/add-all-and-commit! message)) (git/add-all-and-commit! message))
(defmethod handle :gitStatus [_ [_]] (defmethod handle :gitStatus [_ [_ repo]]
(git/short-status!)) (git/short-status! (utils/get-graph-dir repo)))
(def debounced-configure-auto-commit! (debounce git/configure-auto-commit! 5000)) (def debounced-configure-auto-commit! (debounce git/configure-auto-commit! 5000))
(defmethod handle :setGitAutoCommit [] (defmethod handle :setGitAutoCommit []

View File

@ -8,9 +8,6 @@
(defonce state (defonce state
(atom {:config (config/get-config) (atom {:config (config/get-config)
;; FIXME: replace with :window/graph
:graph/current nil
;; window -> current graph ;; window -> current graph
:window/graph {} :window/graph {}
@ -39,15 +36,16 @@
[] []
(get-in @state [:config :git/commit-on-close?] false)) (get-in @state [:config :git/commit-on-close?] false))
(defn get-graph-path
[]
(:graph/current @state))
(defn get-window-graph-path (defn get-window-graph-path
"Get the path of the graph of a window (might be `nil`)" "Get the path of the graph of a window (might be `nil`)"
[window] [window]
(get (:window/graph @state) window)) (get (:window/graph @state) window))
(defn get-all-graph-paths
"Get the paths of all graphs currently open in all windows."
[]
(set (vals (:window/graph @state))))
(defn get-active-window-graph-path (defn get-active-window-graph-path
"Get the path of the graph of the currently focused window (might be `nil`)" "Get the path of the graph of the currently focused window (might be `nil`)"
[] []

View File

@ -39,7 +39,7 @@
(rum/defcs add-commit-message < rum/reactive (rum/defcs add-commit-message < rum/reactive
(rum/local nil ::git-status) (rum/local nil ::git-status)
{:will-mount (fn [state] {:will-mount (fn [state]
(-> (ipc/ipc "gitStatus") (-> (ipc/ipc "gitStatus" (state/get-current-repo))
(p/then (fn [status] (p/then (fn [status]
(reset! (get state ::git-status) status)))) (reset! (get state ::git-status) status))))
state) state)

View File

@ -11,11 +11,13 @@
(defn run-git-command! (defn run-git-command!
[command] [command]
(ipc/ipc :runGit command)) (ipc/ipc :runGit {:repo (state/get-current-repo)
:command command}))
(defn run-git-command2! (defn run-git-command2!
[command] [command]
(ipc/ipc :runGitWithinCurrentGraph command)) (ipc/ipc :runGitWithinCurrentGraph {:repo (state/get-current-repo)
:command command}))
(defn run-cli-command! (defn run-cli-command!
[command args] [command args]