refactor: simplify git state

Retreive both local and remote oids using resolveRef.
This commit is contained in:
Tienson Qin
2020-11-11 16:19:45 +08:00
parent 83c62b1825
commit b5e3f7de6e
7 changed files with 162 additions and 178 deletions

View File

@@ -4,8 +4,22 @@
[cljs-bean.core :as bean]
[promesa.core :as p]
[frontend.util :as util]
[frontend.text :as text]))
[frontend.text :as text]
[frontend.git :as git]
[frontend.db :as db]))
(defn get-ref
[repo-url]
(git/resolve-ref repo-url "HEAD"))
(defn get-remote-ref
[repo-url]
(let [branch (state/get-repo-branch repo-url)]
;; TODO: what if the remote is not named "origin", check the api from isomorphic-git
(git/resolve-ref repo-url (str "refs/remotes/origin/" branch))))
;; Should include un-pushed committed files too
(defn check-changed-files-status
[]
(when-let [repo (state/get-current-repo)]
@@ -15,10 +29,28 @@
(->
(p/let [files (js/window.workerThread.getChangedFiles (util/get-repo-dir repo))]
(let [files (bean/->clj files)]
(state/set-changed-files! repo files)))
(p/let [remote-latest-commit (get-remote-ref repo)
local-latest-commit (get-ref repo)
descendent? (git/descendent? repo local-latest-commit remote-latest-commit)
diffs (git/get-diffs repo local-latest-commit remote-latest-commit)]
(let [files (if descendent?
(->> (concat (map :path diffs) files)
distinct)
files)]
(state/set-changed-files! repo files)))))
(p/catch (fn [error]
(js/console.dir error)))))))
(defn copy-to-clipboard-without-id-property!
[content]
(util/copy-to-clipboard! (text/remove-id-property content)))
(comment
(let [repo (state/get-current-repo)]
(p/let [remote-oid (get-remote-ref repo)
local-oid (get-ref repo)
diffs (git/get-diffs repo local-oid remote-oid)]
(println {:local-oid local-oid
:remote-oid remote-oid
:diffs diffs})))
)