summaryrefslogtreecommitdiff
path: root/lib/views.ml
blob: 64b003ef3e16c9dbc4cc5571cd1bafd7c6adeac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
type page_data = { title : string; main_content : Dream_html.node list }

module Layout = struct
  open Dream_html
  open HTML

  let header =
    null
      [
        h1 [] [ txt "ogit" ];
        h2 [] [ txt "A mobile-friendly Git repository viewer" ];
      ]

  let topnav =
    nav
      [ id "top" ]
      [
        ul []
          [
            li [] [ a [ href "/" ] [ txt "Home" ] ];
            li [] [ a [ href "/" ] [ txt "Refs" ] ];
            li [] [ a [ href "/" ] [ txt "Log" ] ];
            li [] [ a [ href "/" ] [ txt "Tree" ] ];
            li [] [ a [ href "/" ] [ txt "Commit" ] ];
            li [] [ a [ href "/" ] [ txt "Diff" ] ];
          ];
      ]

  let footer name =
    let today = Unix.localtime (Unix.time ()) in
    let year = today.Unix.tm_year + 1900 |> string_of_int in
    let space = " " in
    let footer_text = String.concat space [ "©"; year; name ] in
    footer [] [ txt "%s" footer_text ]

  let application page =
    html []
      [
        head [] [ title [] "%s" page.title ];
        link [ rel "stylesheet"; href "/static/styles.css" ];
        body []
          [
            header;
            topnav;
            div [ id "main" ] page.main_content;
            footer "Marius PETER";
          ];
      ]
end

module Ogit_root = struct
  open Dream_html
  open HTML

  let repositories_in directory =
    let repositories = Sys.readdir directory |> Array.to_list
    and li_of_repo repo = li [] [ a [ href "%s" repo ] [ txt "%s" repo ] ] in
    div [ id "repositories" ] [ ul [] @@ List.map li_of_repo repositories ]

  let page_data =
    {
      title = "My repositories";
      main_content = [ repositories_in Config.git_directory ];
    }

  let render () = Layout.application page_data
end

module Repo_root = struct
  open Dream_html
  open HTML
  open Git
  open Lwt.Infix

  let page_promise repo_name =
    let repo_path = Filename.concat Config.git_directory repo_name in

    (* 1. Open the Git repository *)
    let%lwt repo =
      Git_unix.Store.v (Fpath.v repo_path) >>= function
      | Ok repo -> Lwt.return repo
      | Error _ -> Lwt.fail_with "Could not open the Git repository."
    in

    (* 2. Resolve HEAD to get the latest commit hash *)
    let%lwt commit_hash =
      Git_unix.Store.Ref.resolve repo Reference.head >>= function
      | Ok hash -> Lwt.return hash
      | Error _ -> Lwt.fail_with "Failed to resolve HEAD"
    in

    (* (\* 3. Read the commit *\) *)
    (* let%lwt commit =  *)
    (*   Commit.v repo commit_hash >>= function *)
    (*   | Ok commit -> Lwt.return commit *)
    (*   | Error _ -> Lwt.fail_with "Failed to read the commit." *)
    (* in *)

    (* (\* 4. Read the tree from the commit *\) *)
    (* let%lwt tree = *)
    (*   Tree.v commit.Commit.tree >>= function *)
    (*   | Ok tree -> Lwt.return tree *)
    (*   | Error _ -> Lwt.fail_with "Failed to read the tree." *)
    (* in *)

    (* (\* 5. Generate HTML list items for each tree entry *\) *)
    (* let entries = Tree.entries tree in *)
    (* let items = *)
    (*   List.map *)
    (*     (fun entry -> *)
    (*       let icon = match entry.Tree.perm with `Dir -> "📁 " | _ -> "📄 " in *)
    (*       li [] [ a [ href "#" ] [ txt (icon ^ entry.Tree.name) ] ]) *)
    (*     entries *)
    (* in *)

    (* 6. Assemble the page *)
    let main_content =
      [
        ul []
          [ li [] [ txt "%s" (commit_hash |> Digestif.SHA1.to_raw_string) ] ];
      ]
    in
    let page_data = { title = repo_name; main_content } in
    Lwt.return (Layout.application page_data)

  let render repo_name = page_promise repo_name >>= fun page -> Lwt.return page
end

module Repo_tree = struct
  open Dream_html

  let render repo_name =
    let title = repo_name and main_content = [ txt "foobar" ] in
    let page_data = { title; main_content } in
    Layout.application page_data
end
Copyright 2019--2025 Marius PETER