fix: git diff doesn't work because we don't store the raw file content

pull/1764/head
Tienson Qin 2021-05-09 11:25:41 +08:00
parent a1fd8425c5
commit 7349910e6f
3 changed files with 74 additions and 73 deletions

View File

@ -6,6 +6,7 @@
[frontend.handler.file :as file]
[frontend.handler.notification :as notification]
[frontend.handler.common :as common-handler]
[frontend.handler.file :as file-handler]
[frontend.state :as state]
[clojure.string :as string]
[frontend.db :as db]
@ -49,9 +50,16 @@
:style {:background-color bg-color}}
value]))])
(rum/defc file < rum/reactive
[repo type path contents remote-oid]
(let [{:keys [collapse? resolved?]} (util/react (rum/cursor diff-state path))
(rum/defcs file < rum/reactive
{:will-mount (fn [state]
(let [*local-content (atom "")
[repo _ path & _others] (:rum/args state)]
(p/let [content (file-handler/load-file repo path )]
(reset! *local-content content))
(assoc state ::local-content *local-content)))}
[state repo type path contents remote-oid]
(let [local-content (rum/react (get state ::local-content))
{:keys [collapse? resolved?]} (util/react (rum/cursor diff-state path))
edit? (util/react *edit?)
delete? (= type "remove")]
[:div.cp__diff-file
@ -70,74 +78,73 @@
(let [content (get contents path)]
(if (or (and delete? (nil? content))
content)
(let [local-content (db/get-file path)]
(if (not= content local-content)
(let [local-content (or local-content "")
content (or content "")
diff (medley/indexed (diff/diff local-content content))
diff? (some (fn [[_idx {:keys [added removed]}]]
(or added removed))
diff)]
[:div.pre-line-white-space.p-2 {:class (if collapse? "hidden")
:style {:overflow "auto"}}
(if edit?
[:div.grid.grid-cols-2.gap-1
(diff-cp diff)
(ui/textarea
{:default-value local-content
:on-change (fn [e]
(reset! *edit-content (util/evalue e)))})]
(diff-cp diff))
(if (not= content local-content)
(let [local-content (or local-content "")
content (or content "")
diff (medley/indexed (diff/diff local-content content))
diff? (some (fn [[_idx {:keys [added removed]}]]
(or added removed))
diff)]
[:div.pre-line-white-space.p-2 {:class (if collapse? "hidden")
:style {:overflow "auto"}}
(if edit?
[:div.grid.grid-cols-2.gap-1
(diff-cp diff)
(ui/textarea
{:default-value local-content
:on-change (fn [e]
(reset! *edit-content (util/evalue e)))})]
(diff-cp diff))
(cond
edit?
[:div.mt-2
(ui/button "Save"
:on-click
(fn []
(reset! *edit? false)
(let [new-content @*edit-content]
(file/alter-file repo path new-content
{:commit? false
:re-render-root? true})
(swap! state/state
assoc-in [:github/contents repo remote-oid path] new-content)
(mark-as-resolved path))))]
(cond
edit?
[:div.mt-2
(ui/button "Save"
:on-click
(fn []
(reset! *edit? false)
(let [new-content @*edit-content]
(file/alter-file repo path new-content
{:commit? false
:re-render-root? true})
(swap! state/state
assoc-in [:github/contents repo remote-oid path] new-content)
(mark-as-resolved path))))]
diff?
[:div.mt-2
(ui/button "Use remote"
:on-click
(fn []
;; overwrite the file
(if delete?
(file/remove-file! repo path)
(file/alter-file repo path content
{:commit? false
:re-render-root? true}))
(mark-as-resolved path))
:background "green")
diff?
[:div.mt-2
(ui/button "Use remote"
:on-click
(fn []
;; overwrite the file
(if delete?
(file/remove-file! repo path)
(file/alter-file repo path content
{:commit? false
:re-render-root? true}))
(mark-as-resolved path))
:background "green")
[:span.pl-2.pr-2 "or"]
[:span.pl-2.pr-2 "or"]
(ui/button "Keep local"
:on-click
(fn []
;; overwrite the file
(swap! state/state
assoc-in [:github/contents repo remote-oid path] local-content)
(mark-as-resolved path))
:background "pink")
(ui/button "Keep local"
:on-click
(fn []
;; overwrite the file
(swap! state/state
assoc-in [:github/contents repo remote-oid path] local-content)
(mark-as-resolved path))
:background "pink")
[:span.pl-2.pr-2 "or"]
[:span.pl-2.pr-2 "or"]
(ui/button "Edit"
:on-click
(fn []
(reset! *edit? true)))]
(ui/button "Edit"
:on-click
(fn []
(reset! *edit? true)))]
:else
nil)])))
:else
nil)]))
[:div "loading..."]))]))
;; TODO: `n` shortcut for next diff, `p` for previous diff

View File

@ -88,13 +88,8 @@
;; raw-page-blocks (get grouped-blocks-by-file file-path raw-page-blocks)
page-blocks (block-handler/with-dummy-block raw-page-blocks format
(if (empty? raw-page-blocks)
(let [content (db/get-file repo file-path)]
{:block/page {:db/id (:db/id page)}
:block/file {:db/id (:db/id (:block/file page))}
:block/meta
(let [file-id (:db/id (:block/file page))]
{:start-pos (utf8/length (utf8/encode content))
:end-pos nil})}))
{:block/page {:db/id (:db/id page)}
:block/file {:db/id (:db/id (:block/file page))}})
{:journal? journal?
:page-name page-name})
hiccup-config {:id (if block? (str block-id) page-name)

View File

@ -22,7 +22,7 @@
(= "add" type)
(when-not (db/file-exists? repo path)
(let [_ (file-handler/alter-file repo path content {:re-render-root? true
:from-disk? true})]
:from-disk? true})]
(db/set-file-last-modified-at! repo path mtime)))
(and (= "change" type)
@ -33,7 +33,6 @@
(not= content (db/get-file path))
(when-let [last-modified-at (db/get-file-last-modified-at repo path)]
(> mtime last-modified-at)))
(let [_ (file-handler/alter-file repo path content {:re-render-root? true
:from-disk? true})]
(db/set-file-last-modified-at! repo path mtime))