From be190de930e98b8f2eaa5cd4cc4cc8cc20baf391 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 13 Apr 2020 10:54:22 +0800 Subject: [PATCH] Add sync button --- web/src/main/frontend/components/sidebar.cljs | 15 ++++++++-- web/src/main/frontend/git.cljs | 29 ++++++++++++------- web/src/main/frontend/handler.cljs | 14 +++++++-- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/web/src/main/frontend/components/sidebar.cljs b/web/src/main/frontend/components/sidebar.cljs index 8b8a2becd..453ce7a8b 100644 --- a/web/src/main/frontend/components/sidebar.cljs +++ b/web/src/main/frontend/components/sidebar.cljs @@ -144,14 +144,25 @@ {:placeholder "Search"}]]]] [:div.ml-4.flex.items-center.md:ml-6 [:button.p-1.text-gray-400.rounded-full.hover:bg-gray-100.hover:text-gray-500.focus:outline-none.focus:shadow-outline.focus:text-gray-500 + {:on-click handler/pull-current-repo} [:svg.h-6.w-6 {:viewBox "0 0 24 24", :fill "none", :stroke "currentColor"} [:path {:d - "M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9", + "M6 18.7V21a1 1 0 0 1-2 0v-5a1 1 0 0 1 1-1h5a1 1 0 1 1 0 2H7.1A7 7 0 0 0 19 12a1 1 0 1 1 2 0 9 9 0 0 1-15 6.7zM18 5.3V3a1 1 0 0 1 2 0v5a1 1 0 0 1-1 1h-5a1 1 0 0 1 0-2h2.9A7 7 0 0 0 5 12a1 1 0 1 1-2 0 9 9 0 0 1 15-6.7z" :stroke-width "2", :stroke-linejoin "round", - :stroke-linecap "round"}]]] + :stroke-linecap "round"}]] + + ;; [:svg.h-6.w-6 + ;; {:viewBox "0 0 24 24", :fill "none", :stroke "currentColor"} + ;; [:path + ;; {:d + ;; "M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9", + ;; :stroke-width "2", + ;; :stroke-linejoin "round", + ;; :stroke-linecap "round"}]] + ] (ui/dropdown-with-links [{:title "Your Profile" :options {:href "/me"}} diff --git a/web/src/main/frontend/git.cljs b/web/src/main/frontend/git.cljs index ece89d16c..f965d7b47 100644 --- a/web/src/main/frontend/git.cljs +++ b/web/src/main/frontend/git.cljs @@ -5,6 +5,7 @@ [clojure.string :as string] [frontend.state :as state])) +(defonce default-branch "master") ;; only support Github now (defn auth [token] @@ -54,7 +55,7 @@ [repo-url token] (js/git.fetch (with-auth token {:dir (get-repo-dir repo-url) - :ref "master" + :ref default-branch :singleBranch true :depth 1 :tags false}))) @@ -63,14 +64,21 @@ [repo-url] (js/git.merge (clj->js {:dir (get-repo-dir repo-url) - :ours "master" - :theirs "remotes/origin/master"}))) + :ours default-branch + :theirs (str "remotes/origin/" default-branch) + :fastForwardOnly true}))) + +(defn checkout + [repo-url] + (js/git.checkout (clj->js + {:dir (get-repo-dir repo-url) + :ref default-branch}))) (defn log [repo-url token depth] (js/git.log (with-auth token {:dir (get-repo-dir repo-url) - :ref "master" + :ref default-branch :depth depth :singleBranch true}))) @@ -78,7 +86,7 @@ [repo-url token] (js/git.pull (with-auth token {:dir (get-repo-dir repo-url) - :ref "master" + :ref default-branch :singleBranch true :fast true}))) (defn add @@ -91,18 +99,17 @@ [repo-url message] (let [{:keys [name email]} (:me @state/state)] (js/git.commit (clj->js - {:dir (get-repo-dir repo-url) - :message message - :author {:name name - :email email}})))) + {:dir (get-repo-dir repo-url) + :message message + :author {:name name + :email email}})))) (defn push [repo-url token] (js/git.push (with-auth token {:dir (get-repo-dir repo-url) :remote "origin" - :ref "master" - }))) + :ref default-branch}))) (defn add-commit-push [repo-url file message token push-ok-handler push-error-handler] diff --git a/web/src/main/frontend/handler.cljs b/web/src/main/frontend/handler.cljs index e1f7130dc..9009ea9fe 100644 --- a/web/src/main/frontend/handler.cljs +++ b/web/src/main/frontend/handler.cljs @@ -218,8 +218,12 @@ (reset! remote-changed? true)) _ (set-latest-commit! fetchHead)] (-> (git/merge repo-url) - (p/then (fn [] - (load-db-and-journals! repo-url @remote-changed? false))) + (p/then (fn [result] + (-> (git/checkout repo-url) + (p/then (fn [result] + (load-db-and-journals! repo-url @remote-changed? false))) + (p/catch (fn [error] + (prn "checkout error: " error)))))) (p/catch (fn [_error] (show-notification! [:p.content @@ -233,6 +237,12 @@ " to pull the latest changes."] :error)))))))) +(defn pull-current-repo + [] + (when-let [repo (db/get-current-repo)] + (when-let [token (get-github-token)] + (pull repo token)))) + (defn periodically-pull [repo-url pull-now?] (when-let [token (get-github-token)]