diff options
author | Marius Peter <marius.peter@tutanota.com> | 2025-02-02 18:56:47 +0100 |
---|---|---|
committer | Marius Peter <marius.peter@tutanota.com> | 2025-02-02 18:56:47 +0100 |
commit | e92e763e06d58a03224189d53044c4f5e1f907f0 (patch) | |
tree | aa0f3707d500fa883e6129d3f99da4b530e2011a /lib | |
parent | aacec6588c3aebffda6c6221b02622576c85c407 (diff) |
Diffstat (limited to 'lib')
-rw-r--r-- | lib/config.ml | 2 | ||||
-rw-r--r-- | lib/dune | 2 | ||||
-rw-r--r-- | lib/git_helpers.ml | 20 | ||||
-rw-r--r-- | lib/views.ml | 77 |
4 files changed, 39 insertions, 62 deletions
diff --git a/lib/config.ml b/lib/config.ml index d47101f..c391c43 100644 --- a/lib/config.ml +++ b/lib/config.ml @@ -1,2 +1,2 @@ let git_directory = Filename.concat (Unix.getenv "HOME") "git" -let author = "Marius Peter" +let author = "Marius PETER" @@ -1,5 +1,5 @@ (library (name ogit) - (libraries dream dream-html git-unix) + (libraries dream dream-html git git-unix) (preprocess (pps lwt_ppx))) diff --git a/lib/git_helpers.ml b/lib/git_helpers.ml new file mode 100644 index 0000000..d973a88 --- /dev/null +++ b/lib/git_helpers.ml @@ -0,0 +1,20 @@ +open Lwt.Infix +open Git_unix + +let get_head_commit_hash repo_path = + let root = Fpath.v repo_path in + + (* 1. Open the Git repository *) + let%lwt repo = + Store.v root >>= 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 Git.Reference.master >>= function + | Ok hash -> Lwt.return hash + | Error _ -> Lwt.fail_with "Failed to resolve HEAD" + in + Lwt.return @@ (commit_hash |> Digestif.SHA1.to_hex) diff --git a/lib/views.ml b/lib/views.ml index b625ca6..797c405 100644 --- a/lib/views.ml +++ b/lib/views.ml @@ -1,7 +1,7 @@ type head_data = { page_title : string } type body_data = { - title : string; + title : string Lwt.t; subtitle : string; topnav : Dream_html.node; content : Dream_html.node list; @@ -14,11 +14,13 @@ module Layout = struct let header title subtitle = null [ h1 [] [ txt "%s" title ]; h2 [] [ txt "%s" subtitle ] ] - let footer name = + let footer = 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 + let footer_text = + String.concat space [ "Copyright"; year; Config.author ] + in footer [] [ txt "%s" footer_text ] let head_data = { page_title = "Ogit" } @@ -33,10 +35,10 @@ module Layout = struct ]; body [] [ - header body_data.title body_data.subtitle; + header "Ogit" body_data.subtitle; body_data.topnav; div [ id "main" ] body_data.content; - footer "Marius PETER"; + footer; ]; ] end @@ -52,7 +54,7 @@ module Ogit_root = struct let body_data = { - title = "My repositories"; + title = Lwt.return "My repositories"; subtitle = "Repositories for " ^ Config.author; topnav = null []; content = [ repositories_in Config.git_directory ]; @@ -64,8 +66,6 @@ end module Repo_root = struct open Dream_html open HTML - open Git - open Lwt.Infix let topnav = nav @@ -73,62 +73,19 @@ module Repo_root = struct [ 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" ] ]; + li [] [ a [ href "/" ] [ txt "summary" ] ]; + 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 render 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.master >>= 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 content = - [ ul [] [ li [] [ txt "%s" (commit_hash |> Digestif.SHA1.to_hex) ] ] ] - in - let title = repo_name in - let subtitle = Git_unix.(repo |> Store.root |> Fpath.to_string) in + let title = Git_helpers.get_head_commit_hash repo_name in + let subtitle = "Repository" in + let content = [ null [] ] in let body_data = { title; subtitle; topnav; content } in Lwt.return @@ Layout.application body_data end |