diff options
author | Marius Peter <marius.peter@tutanota.com> | 2025-06-29 20:52:45 +0200 |
---|---|---|
committer | Marius Peter <marius.peter@tutanota.com> | 2025-06-29 20:52:45 +0200 |
commit | 4eab19b89e68c86d16b5711c7ccfecb7e94312c9 (patch) | |
tree | d61ff04611050b7312b839c29eef21033252a38e | |
parent | 399101966720517d67d887129dfd6a35558804aa (diff) |
Unify tags and branches under references, add repo module.
-rw-r--r-- | lib/resolvers.ml | 61 |
1 files changed, 46 insertions, 15 deletions
diff --git a/lib/resolvers.ml b/lib/resolvers.ml index d6c62a8..873b137 100644 --- a/lib/resolvers.ml +++ b/lib/resolvers.ml @@ -27,13 +27,13 @@ module Commit = struct } let to_t c = - let open Store.Value in - { - hash = Commit.digest c |> Store.Hash.to_hex; - parents = Commit.parents c |> List.map Store.Hash.to_hex; - author = Commit.author c; - message = Commit.message c; - } + Store. + { + hash = Value.Commit.digest c |> Hash.to_hex; + parents = Value.Commit.parents c |> List.map Hash.to_hex; + author = Value.Commit.author c; + message = Value.Commit.message c; + } let of_id repo id = let* store = store repo in @@ -61,7 +61,7 @@ module Commit = struct walk [] head_commit.hash n end -module Branch = struct +module Reference = struct type t = { name : string; hash : string } let to_t (reference, hash) = @@ -70,19 +70,31 @@ module Branch = struct let all repo = let* store = store repo in let open Lwt.Syntax in - let* refs = Store.Ref.list store in - let branches = List.map to_t refs in - Lwt_result.return branches + let* references = Store.Ref.list store in + let references = List.map to_t references in + Lwt_result.return references + + let branches repo = + let* references = all repo in + let is_branch reference = + not (String.starts_with ~prefix:"v" reference.name) + in + Lwt_result.return @@ List.filter is_branch references + + let tags repo = + let* references = all repo in + let is_branch reference = String.starts_with ~prefix:"v" reference.name in + Lwt_result.return @@ List.filter is_branch references let of_id repo id = - let* branches = all repo in + let* branches = branches repo in let branch = branches |> List.find_opt (fun branch -> Filename.basename branch.name = id) in match branch with | Some branch -> Lwt_result.return branch - | None -> Lwt_result.fail @@ `Msg ("no branch matches id " ^ id) + | None -> Lwt_result.fail @@ `Msg ("no reference matches id " ^ id) end module Entry = struct @@ -101,6 +113,9 @@ module Entry = struct | `Normal -> 0o100644 in { hash; name; perm } + + let is_readme { name; _ } = + String.lowercase_ascii name |> String.starts_with ~prefix:"readme" end module Tree = struct @@ -145,8 +160,24 @@ end let blob_or_tree repo id = let* store = store repo in let hash = Store.Hash.of_hex id in - let* obj = Store.read store hash in - match obj with + Lwt_result.bind (Store.read store hash) @@ function | Git.Value.Tree tree -> Lwt_result.return @@ `Tree (Tree.to_t tree) | Git.Value.Blob blob -> Lwt_result.return @@ `Blob (Blob.to_t blob) | _ -> Lwt_result.fail @@ `Msg ("No tree or blob matches id " ^ id) + +module Repo = struct + let has_readme repo = + let* tree = Tree.head repo in + Lwt_result.return @@ List.exists Entry.is_readme tree.entries + + let readme repo = + let* tree = Tree.head repo in + match List.find_opt Entry.is_readme tree.entries with + | None -> Lwt_result.return None + | Some readme -> ( + let* store = store repo in + let hash = Store.Hash.of_hex readme.hash in + Lwt_result.bind (Store.read store hash) @@ function + | Git.Value.Blob blob -> Lwt_result.return @@ Some (Blob.to_t blob) + | _ -> Lwt_result.fail @@ `Msg ("couldn't read file " ^ readme.name)) +end |