Don't submit empty git commit

Resolved https://github.com/logseq/logseq/issues/158
pull/645/head
Tienson Qin 2020-08-24 11:47:38 +08:00
parent 5885529120
commit 294b6060db
3 changed files with 66 additions and 49 deletions

View File

@ -93,9 +93,13 @@
{:class (if (or should-push? pushing?) "bg-orange-400" "bg-green-600") {:class (if (or should-push? pushing?) "bg-orange-400" "bg-green-600")
:style {:border-radius "50%" :style {:border-radius "50%"
:margin-top 2} :margin-top 2}
:on-mouse-over toggle-fn}]) :on-mouse-over
(fn [e]
(toggle-fn)
(js/setTimeout repo-handler/check-changed-files-status 0))}])
(fn [{:keys [toggle-fn]}] (fn [{:keys [toggle-fn]}]
[:div.p-2.rounded-md.shadow-xs.bg-base-3.flex.flex-col.sync-content [:div.p-2.rounded-md.shadow-xs.bg-base-3.flex.flex-col.sync-content
{:on-mouse-leave toggle-fn}
(if (and should-push? (seq changed-files)) (if (and should-push? (seq changed-files))
[:div [:div
[:div.changes [:div.changes

View File

@ -46,8 +46,8 @@
(contains? config/mldoc-support-formats format))) (contains? config/mldoc-support-formats format)))
contents) contents)
blocks-pages (if (seq parsed-files) blocks-pages (if (seq parsed-files)
(db/extract-all-blocks-pages repo-url parsed-files) (db/extract-all-blocks-pages repo-url parsed-files)
[])] [])]
(db/reset-contents-and-blocks! repo-url contents blocks-pages delete-files delete-blocks) (db/reset-contents-and-blocks! repo-url contents blocks-pages delete-files delete-blocks)
(let [config-file (str config/app-name "/" config/config-file)] (let [config-file (str config/app-name "/" config/config-file)]
(when (contains? (set files) config-file) (when (contains? (set files) config-file)
@ -96,14 +96,16 @@
(let [default-content config/config-default-content] (let [default-content config/config-default-content]
(p/let [file-exists? (fs/create-if-not-exists repo-dir (str app-dir "/" config/config-file) default-content)] (p/let [file-exists? (fs/create-if-not-exists repo-dir (str app-dir "/" config/config-file) default-content)]
(let [path (str app-dir "/" config/config-file) (let [path (str app-dir "/" config/config-file)
old-content (when file-exists?
(db/get-file repo-url path))
content (or content (or
(when file-exists? (and old-content
(when-let [content (db/get-file repo-url path)] (string/replace old-content "heading" "block"))
(string/replace content "heading" "block")))
default-content)] default-content)]
(db/reset-file! repo-url path content) (db/reset-file! repo-url path content)
(db/reset-config! repo-url content) (db/reset-config! repo-url content)
(git-handler/git-add repo-url path))) (when-not (= content old-content)
(git-handler/git-add repo-url path))))
;; (p/let [file-exists? (fs/create-if-not-exists repo-dir (str app-dir "/" config/metadata-file) default-content)] ;; (p/let [file-exists? (fs/create-if-not-exists repo-dir (str app-dir "/" config/metadata-file) default-content)]
;; (let [path (str app-dir "/" config/metadata-file)] ;; (let [path (str app-dir "/" config/metadata-file)]
;; (when-not file-exists? ;; (when-not file-exists?
@ -269,55 +271,62 @@
:error) :error)
(route-handler/redirect! {:to :diff}))))))))))) (route-handler/redirect! {:to :diff})))))))))))
(defn check-changed-files-status
[f]
(p/let [files (js/window.workerThread.getChangedFiles (util/get-repo-dir (state/get-current-repo)))]
(let [files (bean/->clj files)]
(state/reset-changed-files! files))))
(defn push (defn push
([repo-url] ([repo-url]
(push repo-url "Logseq auto save")) (push repo-url "Logseq auto save"))
([repo-url commit-message] ([repo-url commit-message]
(let [status (db/get-key-value repo-url :git/status)] (let [status (db/get-key-value repo-url :git/status)]
;; (println {:changed-files (seq (state/get-changed-files repo-url))})
(when (and (when (and
;; (not= status :push-failed) ;; (not= status :push-failed)
;; (db/get-key-value repo-url :git/write-permission?) ;; (db/get-key-value repo-url :git/write-permission?)
(not (state/get-edit-input-id)) (not (state/get-edit-input-id))
(seq (state/get-changed-files repo-url))) ;; getChangedFiles is not very reliable
;; auto commit if there are any un-committed changes (seq (state/get-changed-files repo-url))
(let [commit-message (if (string/blank? commit-message) )
"Logseq auto save" (p/let [files (js/window.workerThread.getChangedFiles (util/get-repo-dir (state/get-current-repo)))]
commit-message)] (when (seq files)
(p/let [_ (git/commit repo-url commit-message)] ;; auto commit if there are any un-committed changes
;; (println "Commit successfully!") (let [commit-message (if (string/blank? commit-message)
(git-handler/set-git-status! repo-url :pushing) "Logseq auto save"
(let [token (state/get-github-token)] commit-message)]
(util/p-handle (p/let [_ (git/commit repo-url commit-message)]
(git/push repo-url token) (git-handler/set-git-status! repo-url :pushing)
(fn [] (let [token (state/get-github-token)]
;; (println "Push successfully!") (util/p-handle
(git-handler/set-git-status! repo-url nil) (git/push repo-url token)
(git-handler/set-git-error! repo-url nil) (fn []
(git-handler/set-latest-commit-if-exists! repo-url) (git-handler/set-git-status! repo-url nil)
(state/clear-changed-files! repo-url)) (git-handler/set-git-error! repo-url nil)
(fn [error] (git-handler/set-latest-commit-if-exists! repo-url)
(if (and (string? error) (state/clear-changed-files! repo-url))
(= error "Failed to fetch")) (fn [error]
(println "Failed to fetch") (if (and (string? error)
(do (= error "Failed to fetch"))
(println "Failed to push") (println "Failed to fetch")
(js/console.dir error) (do
(git-handler/set-git-status! repo-url :push-failed) (println "Failed to push")
(git-handler/set-git-error! repo-url error) (js/console.dir error)
(notification/show! (git-handler/set-git-status! repo-url :push-failed)
[:p.content (git-handler/set-git-error! repo-url error)
"Failed to push, please " (notification/show!
[:span.text-gray-700.font-bold.mr-2 [:p.content
"resolve any diff first."] "Failed to push, please "
(ui/button [:span.text-gray-700.font-bold.mr-2
"Go to diff" "resolve any diff first."]
:href "/diff")] (ui/button
:error "Go to diff"
false) :href "/diff")]
(p/let [result (git/fetch repo-url (state/get-github-token)) :error
{:keys [fetchHead]} (bean/->clj result)] false)
(git-handler/set-latest-commit! repo-url fetchHead))))))))))))) (p/let [result (git/fetch repo-url (state/get-github-token))
{:keys [fetchHead]} (bean/->clj result)]
(git-handler/set-latest-commit! repo-url fetchHead)))))))))))))))
(defn pull-current-repo (defn pull-current-repo
[] []
@ -428,8 +437,7 @@
(periodically-pull repo-url pull-now?) (periodically-pull repo-url pull-now?)
(when (and (when (and
(or (not config/dev?) (or (not config/dev?)
;; (= repo-url "https://github.com/tiensonqin/empty-repo") (= repo-url "https://github.com/tiensonqin/empty-repo"))
)
(not (false? (:git-auto-push (state/get-config repo-url))))) (not (false? (:git-auto-push (state/get-config repo-url)))))
(periodically-push-tasks repo-url))) (periodically-push-tasks repo-url)))

View File

@ -595,6 +595,11 @@
(fn [files] (distinct (conj files file)))) (fn [files] (distinct (conj files file))))
(storage/set "git-changed-files" (:repo/changed-files @state))) (storage/set "git-changed-files" (:repo/changed-files @state)))
(defn reset-changed-files!
[files]
(when-let [repo (get-current-repo)]
(swap! state assoc-in [:repo/changed-files repo] files)))
(defn clear-changed-files! (defn clear-changed-files!
[repo] [repo]
(set-state! [:repo/changed-files repo] nil) (set-state! [:repo/changed-files repo] nil)