Isolate Git helper functions from Views.

Commit
e92e763e06d58a03224189d53044c4f5e1f907f0
Author
Marius Peter <marius.peter@tutanota.com>
Author date
Committer
Marius Peter <marius.peter@tutanota.com>
Committer date
Changed files
lib/config.ml
index d47101fb..c391c434 100644..100644
@@ -1,2 +1,2 @@
1 1 let git_directory = Filename.concat (Unix.getenv "HOME") "git"
2 Removed: let author = "Marius Peter"
2 Added: let author = "Marius PETER"
lib/dune
index e9511c2c..3963ffd4 100644..100644
@@ -1,5 +1,5 @@
1 1 (library
2 2 (name ogit)
3 Removed: (libraries dream dream-html git-unix)
3 Added: (libraries dream dream-html git git-unix)
4 4 (preprocess
5 5 (pps lwt_ppx)))
lib/git_helpers.ml
index 00000000..d973a888 000000..100644
@@ -0,0 +1,20 @@
1 Added: open Lwt.Infix
2 Added: open Git_unix
3 Added:
4 Added: let get_head_commit_hash repo_path =
5 Added: let root = Fpath.v repo_path in
6 Added:
7 Added: (* 1. Open the Git repository *)
8 Added: let%lwt repo =
9 Added: Store.v root >>= function
10 Added: | Ok repo -> Lwt.return repo
11 Added: | Error _ -> Lwt.fail_with "Could not open the Git repository."
12 Added: in
13 Added:
14 Added: (* 2. Resolve HEAD to get the latest commit hash *)
15 Added: let%lwt commit_hash =
16 Added: Git_unix.Store.Ref.resolve repo Git.Reference.master >>= function
17 Added: | Ok hash -> Lwt.return hash
18 Added: | Error _ -> Lwt.fail_with "Failed to resolve HEAD"
19 Added: in
20 Added: Lwt.return @@ (commit_hash |> Digestif.SHA1.to_hex)
lib/views.ml
index b625ca69..797c4053 100644..100644
@@ -1,7 +1,7 @@
1 1 type head_data = { page_title : string }
2 2
3 3 type body_data = {
4 Removed: title : string;
4 Added: title : string Lwt.t;
5 5 subtitle : string;
6 6 topnav : Dream_html.node;
7 7 content : Dream_html.node list;
@@ -14,11 +14,13 @@
14 14 let header title subtitle =
15 15 null [ h1 [] [ txt "%s" title ]; h2 [] [ txt "%s" subtitle ] ]
16 16
17 Removed: let footer name =
17 Added: let footer =
18 18 let today = Unix.localtime (Unix.time ()) in
19 19 let year = today.Unix.tm_year + 1900 |> string_of_int in
20 20 let space = " " in
21 Removed: let footer_text = String.concat space [ "©"; year; name ] in
21 Added: let footer_text =
22 Added: String.concat space [ "Copyright"; year; Config.author ]
23 Added: in
22 24 footer [] [ txt "%s" footer_text ]
23 25
24 26 let head_data = { page_title = "Ogit" }
@@ -33,10 +35,10 @@
33 35 ];
34 36 body []
35 37 [
36 Removed: header body_data.title body_data.subtitle;
38 Added: header "Ogit" body_data.subtitle;
37 39 body_data.topnav;
38 40 div [ id "main" ] body_data.content;
39 Removed: footer "Marius PETER";
41 Added: footer;
40 42 ];
41 43 ]
42 44 end
@@ -52,7 +54,7 @@
52 54
53 55 let body_data =
54 56 {
55 Removed: title = "My repositories";
57 Added: title = Lwt.return "My repositories";
56 58 subtitle = "Repositories for " ^ Config.author;
57 59 topnav = null [];
58 60 content = [ repositories_in Config.git_directory ];
@@ -64,8 +66,6 @@
64 66 module Repo_root = struct
65 67 open Dream_html
66 68 open HTML
67 Removed: open Git
68 Removed: open Lwt.Infix
69 69
70 70 let topnav =
71 71 nav
@@ -73,62 +73,19 @@
73 73 [
74 74 ul []
75 75 [
76 Removed: li [] [ a [ href "/" ] [ txt "Home" ] ];
77 Removed: li [] [ a [ href "/" ] [ txt "Refs" ] ];
78 Removed: li [] [ a [ href "/" ] [ txt "Log" ] ];
79 Removed: li [] [ a [ href "/" ] [ txt "Tree" ] ];
80 Removed: li [] [ a [ href "/" ] [ txt "Commit" ] ];
81 Removed: li [] [ a [ href "/" ] [ txt "Diff" ] ];
76 Added: li [] [ a [ href "/" ] [ txt "summary" ] ];
77 Added: li [] [ a [ href "/" ] [ txt "refs" ] ];
78 Added: li [] [ a [ href "/" ] [ txt "log" ] ];
79 Added: li [] [ a [ href "/" ] [ txt "tree" ] ];
80 Added: li [] [ a [ href "/" ] [ txt "commit" ] ];
81 Added: li [] [ a [ href "/" ] [ txt "diff" ] ];
82 82 ];
83 83 ]
84 84
85 85 let render repo_name =
86 Removed: let repo_path = Filename.concat Config.git_directory repo_name in
87 Removed:
88 Removed: (* 1. Open the Git repository *)
89 Removed: let%lwt repo =
90 Removed: Git_unix.Store.v (Fpath.v repo_path) >>= function
91 Removed: | Ok repo -> Lwt.return repo
92 Removed: | Error _ -> Lwt.fail_with "Could not open the Git repository."
93 Removed: in
94 Removed:
95 Removed: (* 2. Resolve HEAD to get the latest commit hash *)
96 Removed: let%lwt commit_hash =
97 Removed: Git_unix.Store.Ref.resolve repo Reference.master >>= function
98 Removed: | Ok hash -> Lwt.return hash
99 Removed: | Error _ -> Lwt.fail_with "Failed to resolve HEAD"
100 Removed: in
101 Removed:
102 Removed: (* (\* 3. Read the commit *\) *)
103 Removed: (* let%lwt commit = *)
104 Removed: (* Commit.v repo commit_hash >>= function *)
105 Removed: (* | Ok commit -> Lwt.return commit *)
106 Removed: (* | Error _ -> Lwt.fail_with "Failed to read the commit." *)
107 Removed: (* in *)
108 Removed:
109 Removed: (* (\* 4. Read the tree from the commit *\) *)
110 Removed: (* let%lwt tree = *)
111 Removed: (* Tree.v commit.Commit.tree >>= function *)
112 Removed: (* | Ok tree -> Lwt.return tree *)
113 Removed: (* | Error _ -> Lwt.fail_with "Failed to read the tree." *)
114 Removed: (* in *)
115 Removed:
116 Removed: (* (\* 5. Generate HTML list items for each tree entry *\) *)
117 Removed: (* let entries = Tree.entries tree in *)
118 Removed: (* let items = *)
119 Removed: (* List.map *)
120 Removed: (* (fun entry -> *)
121 Removed: (* let icon = match entry.Tree.perm with `Dir -> "📁 " | _ -> "📄 " in *)
122 Removed: (* li [] [ a [ href "#" ] [ txt (icon ^ entry.Tree.name) ] ]) *)
123 Removed: (* entries *)
124 Removed: (* in *)
125 Removed:
126 Removed: (* 6. Assemble the page *)
127 Removed: let content =
128 Removed: [ ul [] [ li [] [ txt "%s" (commit_hash |> Digestif.SHA1.to_hex) ] ] ]
129 Removed: in
130 Removed: let title = repo_name in
131 Removed: let subtitle = Git_unix.(repo |> Store.root |> Fpath.to_string) in
86 Added: let title = Git_helpers.get_head_commit_hash repo_name in
87 Added: let subtitle = "Repository" in
88 Added: let content = [ null [] ] in
132 89 let body_data = { title; subtitle; topnav; content } in
133 90 Lwt.return @@ Layout.application body_data
134 91 end