From d5d725630fb8eca2b8eb79a479044646f312b056 Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Sun, 25 May 2025 15:49:46 +0200 Subject: Refactor helpers as presenters. Rather than generically ``helping'', this module provides intermediate representations of ocaml-git objects usable by the Views module. --- lib/git_helpers.ml | 76 --------------------------------------------------- lib/git_presenters.ml | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/handlers.ml | 6 ++-- 3 files changed, 79 insertions(+), 79 deletions(-) delete mode 100644 lib/git_helpers.ml create mode 100644 lib/git_presenters.ml diff --git a/lib/git_helpers.ml b/lib/git_helpers.ml deleted file mode 100644 index 1b32735..0000000 --- a/lib/git_helpers.ml +++ /dev/null @@ -1,76 +0,0 @@ -module Store = Git_unix.Store - -let full_path path = Filename.concat Config.git_directory 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 - -module Commit = struct - open Lwt_result.Syntax - - type t = { - hash : string; - parents : string list; - author : Git.User.t; - message : string option; - } - - let of_hash store h = - let* v = Store.read store h in - match v with - | Git.Value.Commit c -> - Lwt_result.return - { - hash = Store.Hash.to_hex h; - parents = Store.Value.Commit.parents c |> List.map Store.Hash.to_hex; - author = Store.Value.Commit.author c; - message = Store.Value.Commit.message c; - } - | _ -> Lwt_result.fail @@ `Msg "value is not a commit" - - let recent_commits repo n = - let* store = store repo in - let* head = Store.Ref.resolve store Git.Reference.head in - let rec walk acc hash count = - if count = 0 then Lwt_result.return (List.rev acc) - else - let* commit = of_hash store hash in - match commit.parents with - | parent :: _ -> - walk (commit :: acc) (Store.Hash.of_hex parent) (count - 1) - | [] -> Lwt_result.return (List.rev (commit :: acc)) - in - walk [] head n - - let of_id repo id = - let open Lwt_result.Syntax in - let* store = store repo in - let id = Store.Hash.of_hex id in - of_hash store id -end - -module Branch = struct - type t = { name : string } - - let all_branches repo = - let open Lwt_result.Syntax in - let* store = Git_unix.Store.v (Fpath.v repo) in - let open Lwt.Syntax in - let* refs = Store.Ref.list store in - let branches = - (* Filter these references for branches! *) - List.map (function _, x -> x |> Store.Hash.to_hex) refs - in - Lwt_result.return branches -end - -module User = struct - type t = Git.User.t -end diff --git a/lib/git_presenters.ml b/lib/git_presenters.ml new file mode 100644 index 0000000..cdd9caa --- /dev/null +++ b/lib/git_presenters.ml @@ -0,0 +1,76 @@ +module Store = Git_unix.Store + +let full_path path = Filename.concat Config.git_directory 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 + +module User = struct + type t = Git.User.t +end + +module Commit = struct + open Lwt_result.Syntax + + type t = { + hash : string; + parents : string list; + author : User.t; + message : string option; + } + + let of_hash store h = + let* v = Store.read store h in + match v with + | Git.Value.Commit c -> + Lwt_result.return + { + hash = Store.Hash.to_hex h; + parents = Store.Value.Commit.parents c |> List.map Store.Hash.to_hex; + author = Store.Value.Commit.author c; + message = Store.Value.Commit.message c; + } + | _ -> Lwt_result.fail @@ `Msg "value is not a commit" + + let recent_commits repo n = + let* store = store repo in + let* head = Store.Ref.resolve store Git.Reference.head in + let rec walk acc hash count = + if count = 0 then Lwt_result.return (List.rev acc) + else + let* commit = of_hash store hash in + match commit.parents with + | parent :: _ -> + walk (commit :: acc) (Store.Hash.of_hex parent) (count - 1) + | [] -> Lwt_result.return (List.rev (commit :: acc)) + in + walk [] head n + + let of_id repo id = + let open Lwt_result.Syntax in + let* store = store repo in + let id = Store.Hash.of_hex id in + of_hash store id +end + +module Branch = struct + type t = { name : string } + + let all_branches repo = + let open Lwt_result.Syntax in + let* store = Git_unix.Store.v (Fpath.v repo) in + let open Lwt.Syntax in + let* refs = Store.Ref.list store in + let branches = + (* Filter these references for branches! *) + List.map (function _, x -> x |> Store.Hash.to_hex) refs + in + Lwt_result.return branches +end diff --git a/lib/handlers.ml b/lib/handlers.ml index a99f8ae..dadf0e1 100644 --- a/lib/handlers.ml +++ b/lib/handlers.ml @@ -12,8 +12,8 @@ module Repo = struct let repo req = Dream.param req "repo_name" let summary req = - let* branches = Git_helpers.Branch.all_branches (repo req) in - let* commits = Git_helpers.Commit.recent_commits (repo req) 10 in + let* branches = Git_presenters.Branch.all_branches (repo req) in + let* commits = Git_presenters.Commit.recent_commits (repo req) 10 in let authors = [ "John Pork"; "Sebastian Jellybean" ] in Views.Repo.summary (repo req) branches commits authors |> Dream_html.respond @@ -23,7 +23,7 @@ module Repo = struct let commit req = let id = match Dream.query req "id" with Some id -> id | None -> "" in - let* commit = Git_helpers.Commit.of_id (repo req) id in + let* commit = Git_presenters.Commit.of_id (repo req) id in Views.Repo.commit (repo req) commit |> Dream_html.respond end -- cgit v1.2.3