Skip to content

Commit fff4ed0

Browse files
committed
getting diffs via View.diff for recent_activity page
1 parent 3343a63 commit fff4ed0

2 files changed

Lines changed: 43 additions & 25 deletions

File tree

canopy_article.ml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,21 @@ let to_tyxml article =
4949
Tyxml.Html.article [Unsafe.data article.content]
5050
]]
5151

52-
let to_tyxml_history cache =
53-
let activity_list = KeyMap.fold (fun k v list -> v :: list) cache [] in
54-
let format_activity activity =
55-
li ~a:[] [pcdata activity]
52+
let to_tyxml_history history_cache =
53+
let format_activity c_history =
54+
match c_history.Canopy_store.diffs with
55+
| Some diffs ->
56+
List.map (fun diff ->
57+
let content = match diff with
58+
| `Added value -> Printf.sprintf "+ : %s" value
59+
| `Removed value -> Printf.sprintf "- : %s" value
60+
| `Updated (removed, added) -> Printf.sprintf "* : %s and %s" removed added
61+
in
62+
li ~a:[] [pcdata content]
63+
) diffs
64+
| None -> []
5665
in
57-
let activity_list_html = List.map format_activity activity_list in
66+
let activity_list_html = List.map format_activity history_cache |> List.flatten in
5867
[div
5968
[ul ~a:[] activity_list_html]]
6069

canopy_store.ml

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ open Lwt.Infix
22
open Canopy_config
33
open Canopy_utils
44

5+
type commit_history = {
6+
mutable diffs : (string list * View.value Irmin.diff) list option;
7+
timestamp : Ptime.t option;
8+
author : string;
9+
}
10+
511
module Store (CTX: Irmin_mirage.CONTEXT) (INFL: Git.Inflate.S) = struct
612

713
module Hash = Irmin.Hash.SHA1
814
module Mirage_git_memory = Irmin_mirage.Irmin_git.Memory(CTX)(INFL)
915
module Store = Mirage_git_memory(Irmin.Contents.String)(Irmin.Ref.String)(Hash)
1016
module Sync = Irmin.Sync(Store)
1117
module Topological = Graph.Topological.Make(Store.History)
18+
module View = Irmin.View(Store)
1219

1320
let src = Logs.Src.create "canopy-store" ~doc:"Canopy store logger"
1421
module Log = (val Logs.src_log src : Logs.LOG)
@@ -77,33 +84,35 @@ module Store (CTX: Irmin_mirage.CONTEXT) (INFL: Git.Inflate.S) = struct
7784
in
7885
Topological.fold aux history (Lwt.return (commit, commit, None))
7986

87+
let set_diffs c_history repo c1 c2 =
88+
let view_of_commit repo commit_id =
89+
Store.of_commit_id task commit_id repo >>= fun t ->
90+
View.of_path (t "view") []
91+
in
92+
view_of_commit repo c1 >>= fun v1 ->
93+
view_of_commit repo c2 >>= fun v2 ->
94+
View.diff v1 v2 >|= fun diffs ->
95+
c_history.diffs <- Some diffs
96+
8097
let fill_history_cache () =
8198
new_task () >>= fun t ->
8299
repo () >>= fun repo ->
83100
Store.history (t "Reading history") >>= fun history ->
84-
let fn key value cache =
85-
value () >>= fun value ->
86-
match key_type key with
87-
| `Article -> (
88-
let uri = String.concat "/" key in
89-
match KeyMap.find_opt cache key with
90-
| None ->
91-
let create_event = Printf.sprintf "Article added: %s" uri in
92-
KeyMap.add key create_event cache |> Lwt.return
93-
| Some old_value ->
94-
if old_value = value then Lwt.return cache
95-
else
96-
let update_event = Printf.sprintf "Article modified: %s" uri in
97-
KeyMap.add key update_event cache |> Lwt.return
98-
)
99-
| `Static | `Config -> Lwt.return cache
100-
in
101101
let aux commit_id acc =
102-
Store.of_commit_id (Irmin.Task.none) commit_id repo >>= fun store ->
103102
acc >>= fun acc ->
104-
fold (store ()) fn acc
103+
match acc with
104+
| None, acc_list ->
105+
(Some commit_id, acc_list) |> Lwt.return
106+
| Some prev_commit_id, acc_list ->
107+
Store.Repo.task_of_commit_id repo commit_id >>= fun task ->
108+
let timestamp = Irmin.Task.date task |> Int64.to_float |> Ptime.of_float_s in
109+
let c_history = {timestamp; diffs = None; author = ""} in
110+
set_diffs c_history repo prev_commit_id commit_id >|= fun _ ->
111+
let acc_list = List.append acc_list [c_history] in
112+
(Some commit_id, acc_list)
105113
in
106-
Topological.fold aux history (Lwt.return KeyMap.empty)
114+
Topological.fold aux history (Lwt.return (None, [])) >|= fun (_, diffs) ->
115+
diffs
107116

108117
let date_updated_created key =
109118
new_task () >>= fun t ->

0 commit comments

Comments
 (0)