summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarius Peter <marius.peter@tutanota.com>2025-06-25 18:12:03 +0200
committerMarius Peter <marius.peter@tutanota.com>2025-06-25 18:12:03 +0200
commitca5c67a40dedf8478b6c4c59f6e21828c4578428 (patch)
tree8e159960743376be15b3a368185a4e11c7a91dc0
parent08a42b99a2ba69e953dc5ffd7e2b429f2f808874 (diff)
Refactor views.
-rw-r--r--lib/handlers.ml4
-rw-r--r--lib/resolvers.ml7
-rw-r--r--lib/views.ml124
3 files changed, 57 insertions, 78 deletions
diff --git a/lib/handlers.ml b/lib/handlers.ml
index 7a37b20..141d197 100644
--- a/lib/handlers.ml
+++ b/lib/handlers.ml
@@ -29,7 +29,7 @@ module Repo = struct
let file_id req =
let repo = Dream.param req "repo" in
- let id = Dream.param req "repo" in
+ let id = Dream.param req "id" in
let* blob = Resolvers.Blob.of_id repo id in
Views.Repo.file repo blob
@@ -56,7 +56,7 @@ let all_handlers =
get "/summary/" summary;
get "/log/" log;
get "/files/" files_at_head;
- get "/files/:id" file_id;
+ get "/file/:id" file_id;
get "/refs/" refs;
get "/commit/:id" commit;
];
diff --git a/lib/resolvers.ml b/lib/resolvers.ml
index f7bf31a..0beb675 100644
--- a/lib/resolvers.ml
+++ b/lib/resolvers.ml
@@ -122,10 +122,9 @@ module Tree = struct
let of_id repo id =
let* store = store repo in
let hash = Store.Hash.of_hex id in
- Store.read store hash
- |> Lwt_result.map @@ function
- | Git.Value.Tree tree -> to_t tree
- | _ -> failwith "no head tree id"
+ Lwt_result.bind (Store.read store hash) @@ function
+ | Git.Value.Tree tree -> to_t tree |> Lwt_result.return
+ | _ -> `Msg "no head tree id" |> Lwt_result.fail
let head repo : (t, Store.error) Lwt_result.t =
let* store = store repo in
diff --git a/lib/views.ml b/lib/views.ml
index 3a5de9d..024aeea 100644
--- a/lib/views.ml
+++ b/lib/views.ml
@@ -79,15 +79,14 @@ let root () =
in
HTML.(div [ id "repositories" ] [ ul [] @@ List.map li_of_repo repos ])
in
- let body_data =
- {
- title = "Ogit";
- subtitle = "Repositories for " ^ config.user;
- topnav = HTML.null [];
- content = [ all_repositories ];
- }
- in
- respond @@ Page.render body_data
+ respond
+ @@ Page.render
+ {
+ title = "Ogit";
+ subtitle = "Repositories for " ^ config.user;
+ topnav = HTML.null [];
+ content = [ all_repositories ];
+ }
module Repo = struct
let page_title repo =
@@ -100,22 +99,17 @@ module Repo = struct
let li_of_commit repo (commit : Resolvers.Commit.t) =
let short_hash = Resolvers.short_hash commit.hash in
- let content =
+ let hash_span =
+ HTML.(span [ class_ "commit-hash" ] [ txt "%s" short_hash ])
+ in
+ let description =
match commit.message with
- | None -> txt "%s" short_hash
- | Some msg ->
- let route = Routes.Commit (repo, commit.hash) in
- let node =
- HTML.(
- null
- [
- span [ class_ "commit-hash" ] [ txt "%s" short_hash ];
- txt " — %s" msg;
- ])
- in
- Routes.link_to route node
+ | None -> HTML.null []
+ | Some msg -> txt " %s" msg
in
- HTML.li [] [ content ]
+ let route = Routes.Commit (repo, commit.hash) in
+ let node = HTML.null [ hash_span; description ] in
+ HTML.li [] [ Routes.link_to route node ]
let li_of_entry repo (entry : Resolvers.Entry.t) =
let display_name =
@@ -128,79 +122,70 @@ module Repo = struct
HTML.(li [] [ Routes.link_to route @@ txt "%s" display_name ])
let summary repo branches commits authors =
- let content =
- HTML.
- [
- h3 [] [ txt "Branches" ];
- ul [] (List.map (li_of_branch repo) branches);
- h3 [] [ txt "Latest commits" ];
- ul [] (List.map (li_of_commit repo) commits);
- h3 [] [ txt "Authors" ];
- ul [] (List.map li_of_author authors);
- ]
- in
respond
@@ Page.render ~page_title:(page_title repo)
{
title = repo;
subtitle = Resolvers.repo_description repo;
topnav = Components.Topnav.(v ~active_path:Summary repo);
- content;
+ content =
+ HTML.
+ [
+ h3 [] [ txt "Branches" ];
+ ul [] (List.map (li_of_branch repo) branches);
+ h3 [] [ txt "Latest commits" ];
+ ul [] (List.map (li_of_commit repo) commits);
+ h3 [] [ txt "Authors" ];
+ ul [] (List.map li_of_author authors);
+ ];
}
let refs repo branches =
- let content =
- HTML.
- [
- h3 [] [ txt "Branches" ];
- ul [] (List.map (li_of_branch repo) branches);
- ]
- in
respond
@@ Page.render ~page_title:(page_title repo)
{
title = repo;
subtitle = Resolvers.repo_description repo;
topnav = Components.Topnav.(v ~active_path:Refs repo);
- content;
+ content =
+ HTML.
+ [
+ h3 [] [ txt "Branches" ];
+ ul [] (List.map (li_of_branch repo) branches);
+ ];
}
let log repo commits =
- let content =
- HTML.
- [
- h3 [] [ txt "All commits" ];
- ul [] (List.map (li_of_commit repo) commits);
- ]
- in
respond
@@ Page.render ~page_title:(page_title repo)
{
title = repo;
subtitle = Resolvers.repo_description repo;
topnav = Components.Topnav.(v ~active_path:Log repo);
- content;
+ content =
+ HTML.
+ [
+ h3 [] [ txt "All commits" ];
+ ul [] (List.map (li_of_commit repo) commits);
+ ];
}
let files repo (tree : Resolvers.Tree.t) =
- let content =
- HTML.
- [
- h3 [] [ txt "Files %s" @@ Resolvers.short_hash tree.hash ];
- ul [] (List.map (li_of_entry repo) tree.entries);
- ]
- in
respond
@@ Page.render ~page_title:(page_title repo)
{
title = Printf.sprintf "%s" repo;
subtitle = Resolvers.repo_description repo;
topnav = Components.Topnav.(v ~active_path:Files repo);
- content;
+ content =
+ HTML.
+ [
+ h3 [] [ txt "Files %s" @@ Resolvers.short_hash tree.hash ];
+ ul [] (List.map (li_of_entry repo) tree.entries);
+ ];
}
let file repo (blob : Resolvers.Blob.t) =
- let title = Printf.sprintf "%s" repo in
let to_numbered_line number line =
let n = number + 1 in
HTML.
@@ -213,31 +198,26 @@ module Repo = struct
String.split_on_char '\n' blob.content
|> List.mapi to_numbered_line |> List.flatten
in
- let content =
- HTML.[ h3 [] [ txt "File" ]; div [ id "blob" ] formatted_blob ]
- in
respond
@@ Page.render ~page_title:(page_title repo)
{
- title;
+ title = Printf.sprintf "%s" repo;
subtitle = Resolvers.repo_description repo;
- topnav = Components.Topnav.(v ~active_path:Files repo);
- content;
+ topnav = Components.Topnav.v ~active_path:Files repo;
+ content =
+ HTML.[ h3 [] [ txt "File" ]; div [ id "blob" ] formatted_blob ];
}
let commit repo (commit : Resolvers.Commit.t) =
let message = match commit.message with Some msg -> msg | None -> "" in
- let title =
- Printf.sprintf "%s : %s" repo @@ Resolvers.short_hash commit.hash
- in
- let content = HTML.[ h3 [] [ txt "%s" message ] ] in
respond
@@ Page.render ~page_title:(page_title repo)
{
- title;
+ title =
+ Printf.sprintf "%s : %s" repo @@ Resolvers.short_hash commit.hash;
subtitle = Resolvers.repo_description repo;
topnav = Components.Topnav.v repo;
- content;
+ content = HTML.[ h3 [] [ txt "%s" message ] ];
}
end
Copyright 2019--2025 Marius PETER