diff options
author | Marius Peter <marius.peter@tutanota.com> | 2025-05-25 19:25:38 +0200 |
---|---|---|
committer | Marius Peter <marius.peter@tutanota.com> | 2025-05-25 19:25:38 +0200 |
commit | 68a01545bf9de1d614490a2bdb0cca8f3b1ff7f5 (patch) | |
tree | c2370e4121100a905f5c99c6cfe53fad54158bec | |
parent | d5d725630fb8eca2b8eb79a479044646f312b056 (diff) |
Add TOML config management.
-rw-r--r-- | lib/config.ml | 28 | ||||
-rw-r--r-- | lib/default_config.toml | 4 | ||||
-rw-r--r-- | lib/dune | 4 | ||||
-rw-r--r-- | lib/git_presenters.ml | 16 | ||||
-rw-r--r-- | lib/views.ml | 25 |
5 files changed, 53 insertions, 24 deletions
diff --git a/lib/config.ml b/lib/config.ml index 61810fa..fb1fc88 100644 --- a/lib/config.ml +++ b/lib/config.ml @@ -1,2 +1,26 @@ -let git_directory = Filename.concat (Unix.getenv "HOME") "git.test" -let author = "Marius Peter" +let config_file = "default_config.toml" + +type t = { + repositories_root : string; + user : string; + default_branch : string; + max_commits_displayed : int; +} + +let config = + match Toml.Parser.from_filename config_file with + | `Error (e, _) -> failwith ("Config parse error: " ^ e) + | `Ok table -> + let find key = Toml.Types.Table.find (Toml.Min.key key) table in + let find_string key = + match find key with TString s -> s | _ -> raise Not_found + in + let find_int key = + match find key with TInt i -> i | _ -> raise Not_found + in + { + repositories_root = find_string "repositories_root"; + user = find_string "user"; + default_branch = find_string "default_branch"; + max_commits_displayed = find_int "max_commits_displayed"; + } diff --git a/lib/default_config.toml b/lib/default_config.toml new file mode 100644 index 0000000..0318182 --- /dev/null +++ b/lib/default_config.toml @@ -0,0 +1,4 @@ +repositories_root = "~/git.test/" +user = "Marius Peter" +default_branch = "master" +max_commits_displayed = 100
\ No newline at end of file @@ -1,5 +1,3 @@ (library (name ogit) - (libraries dream dream-html git git-unix) - (preprocess - (pps lwt_ppx))) + (libraries dream dream-html git-unix toml)) diff --git a/lib/git_presenters.ml b/lib/git_presenters.ml index cdd9caa..680a16c 100644 --- a/lib/git_presenters.ml +++ b/lib/git_presenters.ml @@ -1,13 +1,12 @@ module Store = Git_unix.Store +open Config -let full_path path = Filename.concat Config.git_directory path +let full_path path = Filename.concat config.repositories_root path let store repo = let path = Fpath.v @@ full_path repo in Store.v ~dotgit:path path -let short_hash hash = String.sub hash 0 8 - let repo_description repo = let description_path = Filename.concat (full_path repo) "description" in In_channel.with_open_text description_path In_channel.input_all @@ -21,18 +20,21 @@ module Commit = struct type t = { hash : string; + short_hash : string; parents : string list; author : User.t; message : string option; } - let of_hash store h = + let to_commit store h = let* v = Store.read store h in match v with | Git.Value.Commit c -> + let hash = Store.Hash.to_hex h in Lwt_result.return { - hash = Store.Hash.to_hex h; + hash; + short_hash = String.sub hash 0 8; parents = Store.Value.Commit.parents c |> List.map Store.Hash.to_hex; author = Store.Value.Commit.author c; message = Store.Value.Commit.message c; @@ -45,7 +47,7 @@ module Commit = struct let rec walk acc hash count = if count = 0 then Lwt_result.return (List.rev acc) else - let* commit = of_hash store hash in + let* commit = to_commit store hash in match commit.parents with | parent :: _ -> walk (commit :: acc) (Store.Hash.of_hex parent) (count - 1) @@ -57,7 +59,7 @@ module Commit = struct let open Lwt_result.Syntax in let* store = store repo in let id = Store.Hash.of_hex id in - of_hash store id + to_commit store id end module Branch = struct diff --git a/lib/views.ml b/lib/views.ml index 843afdc..0fc9764 100644 --- a/lib/views.ml +++ b/lib/views.ml @@ -1,3 +1,5 @@ +open Config + type head_data = { page_title : string } type body_data = { @@ -49,7 +51,7 @@ module Page = struct let footer () = let today = Unix.localtime (Unix.time ()) in let year = string_of_int (today.Unix.tm_year + 1900) in - let footer_text = Printf.sprintf "Copyright %s %s" year Config.author in + let footer_text = Printf.sprintf "Copyright %s %s" year config.user in footer [] [ txt "%s" footer_text ] let default_head_data = { page_title = "Ogit" } @@ -88,9 +90,9 @@ let root () = let body_data = { title = "Ogit"; - subtitle = "Repositories for " ^ Config.author; + subtitle = "Repositories for " ^ config.user; topnav = HTML.null []; - content = [ repositories_in Config.git_directory ]; + content = [ repositories_in config.repositories_root ]; } in Page.render body_data @@ -101,7 +103,7 @@ module Repo = struct let li_of_branch branch = HTML.(li [] [ a [ href "%s" branch ] [ txt "%s" branch ] ]) in - let li_of_commit (commit : Git_helpers.Commit.t) = + let li_of_commit (commit : Git_presenters.Commit.t) = match commit.message with | Some msg -> HTML.( @@ -109,7 +111,7 @@ module Repo = struct [ a [ href "commit/?id=%s" commit.hash ] - [ txt "%s %s" (Git_helpers.short_hash commit.hash) msg ]; + [ txt "%s %s" commit.short_hash msg ]; ]) | None -> HTML.(li [] [ a [ href "" ] [ txt "caca!!" ] ]) in @@ -130,7 +132,7 @@ module Repo = struct Page.render { title = repo; - subtitle = Git_helpers.repo_description repo; + subtitle = Git_presenters.repo_description repo; topnav = Components.topnav repo; content; } @@ -141,7 +143,7 @@ module Repo = struct HTML. { title = repo; - subtitle = Git_helpers.repo_description repo; + subtitle = Git_presenters.repo_description repo; topnav = Components.topnav repo; content = [ null [] ]; } @@ -152,7 +154,7 @@ module Repo = struct HTML. { title = repo; - subtitle = Git_helpers.repo_description repo; + subtitle = Git_presenters.repo_description repo; topnav = Components.topnav repo; content = [ null [] ]; } @@ -163,17 +165,16 @@ module Repo = struct HTML. { title = repo; - subtitle = Git_helpers.repo_description repo; + subtitle = Git_presenters.repo_description repo; topnav = Components.topnav repo; content = [ null [] ]; } - let commit repo (commit : Git_helpers.Commit.t) = + let commit repo (commit : Git_presenters.Commit.t) = let open Dream_html in - let open Git_helpers in let message = match commit.message with Some msg -> msg | None -> "" in let content = HTML.[ h3 [] [ txt "%s" message ] ] in - let title = Printf.sprintf "%s : %s" repo (short_hash commit.hash) in + let title = Printf.sprintf "%s : %s" repo commit.short_hash in Page.render { title; subtitle = ""; topnav = Components.topnav repo; content } end |