Start work on Git "un"helper functions.

Worse is better. I'll revisit the OCaml Git package once I'm more comfortable with OCaml overall.

Commit
4946275b48bfc92ce4b420e36c6cf48694776bbc
Author
Marius Peter <marius.peter@tutanota.com>
Author date
Committer
Marius Peter <marius.peter@tutanota.com>
Committer date
Changed files
lib/config.ml
index c391c434..d47101fb 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 3963ffd4..076d7c62 100644..100644
@@ -1,5 +1,5 @@
1 1 (library
2 2 (name ogit)
3 Removed: (libraries dream dream-html git git-unix)
3 Added: (libraries unix dream dream-html git git-unix)
4 4 (preprocess
5 5 (pps lwt_ppx)))
lib/git_helpers.ml
index d973a888..0626d028 100644..100644
@@ -1,20 +1,11 @@
1 1 open Lwt.Infix
2 Removed: open Git_unix
3 2
4 3 let get_head_commit_hash repo_path =
5 Removed: let root = Fpath.v repo_path in
6 Removed:
7 Removed: (* 1. Open the Git repository *)
8 Removed: let%lwt repo =
9 Removed: Store.v root >>= function
10 Removed: | Ok repo -> Lwt.return repo
11 Removed: | Error _ -> Lwt.fail_with "Could not open the Git repository."
12 Removed: in
13 Removed:
14 Removed: (* 2. Resolve HEAD to get the latest commit hash *)
15 Removed: let%lwt commit_hash =
16 Removed: Git_unix.Store.Ref.resolve repo Git.Reference.master >>= function
17 Removed: | Ok hash -> Lwt.return hash
18 Removed: | Error _ -> Lwt.fail_with "Failed to resolve HEAD"
19 Removed: in
20 Removed: Lwt.return @@ (commit_hash |> Digestif.SHA1.to_hex)
4 Added: let full_path = Filename.concat Config.git_directory repo_path in
5 Added: let%lwt store_result = Git_unix.Store.v @@ Fpath.v full_path in
6 Added: match store_result with
7 Added: | Error _ -> Lwt.return_error "Could not open the Git repository."
8 Added: | Ok store -> (
9 Added: Git_unix.Store.Ref.resolve store Git.Reference.head >|= function
10 Added: | Error _ -> Error ("Failed to resolve HEAD for repo " ^ repo_path)
11 Added: | Ok hash -> Ok (Git_unix.Store.Hash.to_hex hash))
lib/git_unhelpers.ml
index 00000000..28d4d3a8 000000..100644
@@ -0,0 +1,19 @@
1 Added: (* These will be reimplemented using OCaml's Git library, one day... *)
2 Added:
3 Added: let get_git_log repo_path =
4 Added: let full_path = Filename.concat Config.git_directory repo_path in
5 Added: let full_cmd =
6 Added: let cmd = Printf.sprintf "git -C %s log" full_path in
7 Added: let options = [ "--pretty=format:'%ad %s'"; "--date=short"; "-n 10" ] in
8 Added: String.concat " " (cmd :: options)
9 Added: in
10 Added: let ic = Unix.open_process_in full_cmd in
11 Added: let rec read_lines acc =
12 Added: try
13 Added: let line = input_line ic in
14 Added: read_lines (line :: acc)
15 Added: with End_of_file ->
16 Added: ignore (Unix.close_process_in ic);
17 Added: List.rev acc
18 Added: in
19 Added: read_lines []
lib/handlers.ml
index 7c80bcb4..4a6e0ca5 100644..100644
@@ -1,6 +1,6 @@
1 1 open Lwt.Infix
2 2
3 Removed: let ogit_root _ = Views.Ogit_root.render () >>= Dream_html.respond
3 Added: let ogit_root _ = Views.Ogit_root.render () |> Dream_html.respond
4 4
5 5 let repo_root repo_name =
6 6 Views.Repo_root.render repo_name >>= Dream_html.respond
lib/static/styles.css
index 680a6b17..cd87fc10 100644..100644
@@ -1,18 +1,25 @@
1 1 body {
2 2 font-family: sans-serif;
3 Added: max-width: 60em;
4 Added: margin: auto;
5 Added: padding: 0 1em;
6 Added: background-color: #181818;
7 Added: color: white;
3 8 }
4 9
5 10 nav#top {
6 Removed: background-color: dimgray;
11 Added: background-color: black;
7 12 }
8 13
9 14 nav#top ul {
10 Removed: display: flex;
15 Added: display: flex;
16 Added: flex-wrap: wrap;
11 17 list-style-type: none;
18 Added: padding: 0;
12 19 }
13 20
14 21 nav#top ul li {
15 Removed: padding: 1em 0;
22 Added: padding: 0.5em 0;
16 23 }
17 24
18 25 nav#top ul li a {
@@ -22,6 +29,19 @@
22 29 }
23 30
24 31 nav#top ul li a:hover {
25 Removed: background-color: #555;
32 Added: background-color: white;
33 Added: color: black;
26 34 text-decoration: revert;
35 Added: }
36 Added:
37 Added: ul {
38 Added: padding-left: 1em;
39 Added: }
40 Added:
41 Added: li {
42 Added: line-height: 1.5;
43 Added: }
44 Added:
45 Added: a {
46 Added: color: white;
27 47 }
lib/views.ml
index 797c4053..446d6015 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 Lwt.t;
4 Added: title : string;
5 5 subtitle : string;
6 6 topnav : Dream_html.node;
7 7 content : Dream_html.node list;
@@ -16,16 +16,13 @@
16 16
17 17 let footer =
18 18 let today = Unix.localtime (Unix.time ()) in
19 Removed: let year = today.Unix.tm_year + 1900 |> string_of_int in
20 Removed: let space = " " in
21 Removed: let footer_text =
22 Removed: String.concat space [ "Copyright"; year; Config.author ]
23 Removed: in
19 Added: let year = string_of_int (today.Unix.tm_year + 1900) in
20 Added: let footer_text = Printf.sprintf "Copyright %s %s" year Config.author in
24 21 footer [] [ txt "%s" footer_text ]
25 22
26 Removed: let head_data = { page_title = "Ogit" }
23 Added: let default_head_data = { page_title = "Ogit" }
27 24
28 Removed: let application ?(head_data = head_data) body_data =
25 Added: let application ?(head_data = default_head_data) body_data =
29 26 html []
30 27 [
31 28 head []
@@ -48,44 +45,63 @@
48 45 open HTML
49 46
50 47 let repositories_in directory =
51 Removed: let repositories = Sys.readdir directory |> Array.to_list
52 Removed: and li_of_repo repo = li [] [ a [ href "%s" repo ] [ txt "%s" repo ] ] in
53 Removed: div [ id "repositories" ] [ ul [] @@ List.map li_of_repo repositories ]
48 Added: try
49 Added: let repos =
50 Added: Sys.readdir directory |> Array.to_list |> List.sort String.compare
51 Added: in
52 Added: let li_of_repo repo = li [] [ a [ href "%s" repo ] [ txt "%s" repo ] ] in
53 Added: div [ id "repositories" ] [ ul [] @@ List.map li_of_repo repos ]
54 Added: with Sys_error _ ->
55 Added: div [] [ txt "Error: Unable to read repository list." ]
54 56
55 57 let body_data =
56 58 {
57 Removed: title = Lwt.return "My repositories";
59 Added: title = "My repositories";
58 60 subtitle = "Repositories for " ^ Config.author;
59 61 topnav = null [];
60 62 content = [ repositories_in Config.git_directory ];
61 63 }
62 64
63 Removed: let render () = Lwt.return @@ Layout.application body_data
65 Added: let render () = Layout.application body_data
64 66 end
65 67
66 68 module Repo_root = struct
67 69 open Dream_html
68 70 open HTML
71 Added: (* open Lwt.Syntax *)
69 72
70 Removed: let topnav =
71 Removed: nav
72 Removed: [ id "top" ]
73 Added: let render repo_path =
74 Added: (* let* title_result = Git_helpers.get_head_commit_hash repo_path in *)
75 Added: (* let title = *)
76 Added: (* match title_result with Ok hash -> hash | Error msg -> "Error: " ^ msg *)
77 Added: (* in *)
78 Added: let title = "Finble" in
79 Added: let subtitle = Filename.concat Config.git_directory repo_path in
80 Added: let topnav =
81 Added: nav
82 Added: [ id "top" ]
83 Added: [
84 Added: ul []
85 Added: [
86 Added: li [] [ a [ href "/" ] [ txt "summary" ] ];
87 Added: li [] [ a [ href "/" ] [ txt "refs" ] ];
88 Added: li [] [ a [ href "/" ] [ txt "log" ] ];
89 Added: li [] [ a [ href "/" ] [ txt "tree" ] ];
90 Added: li [] [ a [ href "/" ] [ txt "commit" ] ];
91 Added: li [] [ a [ href "/" ] [ txt "diff" ] ];
92 Added: ];
93 Added: ]
94 Added: in
95 Added: let recent_commits = Git_unhelpers.get_git_log repo_path in
96 Added: let li_of_commit commit =
97 Added: li [] [ a [ href "%s" commit ] [ txt "%s" commit ] ]
98 Added: in
99 Added: let content =
73 100 [
74 Removed: ul []
75 Removed: [
76 Removed: li [] [ a [ href "/" ] [ txt "summary" ] ];
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" ] ];
82 Removed: ];
101 Added: h3 [] [ txt "Recent commits" ];
102 Added: ul [] @@ List.map li_of_commit recent_commits;
83 103 ]
84 Removed:
85 Removed: let render repo_name =
86 Removed: let title = Git_helpers.get_head_commit_hash repo_name in
87 Removed: let subtitle = "Repository" in
88 Removed: let content = [ null [] ] in
104 Added: in
89 105 let body_data = { title; subtitle; topnav; content } in
90 106 Lwt.return @@ Layout.application body_data
91 107 end
@@ -93,11 +109,16 @@
93 109 module Repo_tree = struct
94 110 open Dream_html
95 111 open HTML
112 Added: open Lwt.Syntax
96 113
97 Removed: let render repo_name =
98 Removed: let title = repo_name and content = [ txt "foobar" ] in
114 Added: let render repo_path =
115 Added: let* title_result = Git_helpers.get_head_commit_hash repo_path in
116 Added: let title =
117 Added: match title_result with Ok hash -> hash | Error msg -> "Error: " ^ msg
118 Added: in
99 119 let subtitle = "Dinglefops" in
100 120 let topnav = null [] in
121 Added: let content = [ txt "foobar" ] in
101 122 let body_data = { title; subtitle; topnav; content } in
102 123 Lwt.return @@ Layout.application body_data
103 124 end