[OCaml] Mobile-friendly clone of cgit.
Refactor helpers as presenters.
Rather than generically ``helping'', this module provides intermediate representations of ocaml-git objects usable by the Views module.
Changed files
lib/git_helpers.ml
@@ -1,76 +0,0 @@
1
Removed:
module Store = Git_unix.Store
2
Removed:
3
Removed:
let full_path path = Filename.concat Config.git_directory path
4
Removed:
5
Removed:
let store repo =
6
Removed:
let path = Fpath.v @@ full_path repo in
7
Removed:
Store.v ~dotgit:path path
8
Removed:
9
Removed:
let short_hash hash = String.sub hash 0 8
10
Removed:
11
Removed:
let repo_description repo =
12
Removed:
let description_path = Filename.concat (full_path repo) "description" in
13
Removed:
In_channel.with_open_text description_path In_channel.input_all
14
Removed:
15
Removed:
module Commit = struct
16
Removed:
open Lwt_result.Syntax
17
Removed:
18
Removed:
type t = {
19
Removed:
hash : string;
20
Removed:
parents : string list;
21
Removed:
author : Git.User.t;
22
Removed:
message : string option;
23
Removed:
}
24
Removed:
25
Removed:
let of_hash store h =
26
Removed:
let* v = Store.read store h in
27
Removed:
match v with
28
Removed:
| Git.Value.Commit c ->
29
Removed:
Lwt_result.return
30
Removed:
{
31
Removed:
hash = Store.Hash.to_hex h;
32
Removed:
parents = Store.Value.Commit.parents c |> List.map Store.Hash.to_hex;
33
Removed:
author = Store.Value.Commit.author c;
34
Removed:
message = Store.Value.Commit.message c;
35
Removed:
}
36
Removed:
| _ -> Lwt_result.fail @@ `Msg "value is not a commit"
37
Removed:
38
Removed:
let recent_commits repo n =
39
Removed:
let* store = store repo in
40
Removed:
let* head = Store.Ref.resolve store Git.Reference.head in
41
Removed:
let rec walk acc hash count =
42
Removed:
if count = 0 then Lwt_result.return (List.rev acc)
43
Removed:
else
44
Removed:
let* commit = of_hash store hash in
45
Removed:
match commit.parents with
46
Removed:
| parent :: _ ->
47
Removed:
walk (commit :: acc) (Store.Hash.of_hex parent) (count - 1)
48
Removed:
| [] -> Lwt_result.return (List.rev (commit :: acc))
49
Removed:
in
50
Removed:
walk [] head n
51
Removed:
52
Removed:
let of_id repo id =
53
Removed:
let open Lwt_result.Syntax in
54
Removed:
let* store = store repo in
55
Removed:
let id = Store.Hash.of_hex id in
56
Removed:
of_hash store id
57
Removed:
end
58
Removed:
59
Removed:
module Branch = struct
60
Removed:
type t = { name : string }
61
Removed:
62
Removed:
let all_branches repo =
63
Removed:
let open Lwt_result.Syntax in
64
Removed:
let* store = Git_unix.Store.v (Fpath.v repo) in
65
Removed:
let open Lwt.Syntax in
66
Removed:
let* refs = Store.Ref.list store in
67
Removed:
let branches =
68
Removed:
(* Filter these references for branches! *)
69
Removed:
List.map (function _, x -> x |> Store.Hash.to_hex) refs
70
Removed:
in
71
Removed:
Lwt_result.return branches
72
Removed:
end
73
Removed:
74
Removed:
module User = struct
75
Removed:
type t = Git.User.t
76
Removed:
end
lib/git_presenters.ml
@@ -0,0 +1,76 @@
1
Added:
module Store = Git_unix.Store
2
Added:
3
Added:
let full_path path = Filename.concat Config.git_directory path
4
Added:
5
Added:
let store repo =
6
Added:
let path = Fpath.v @@ full_path repo in
7
Added:
Store.v ~dotgit:path path
8
Added:
9
Added:
let short_hash hash = String.sub hash 0 8
10
Added:
11
Added:
let repo_description repo =
12
Added:
let description_path = Filename.concat (full_path repo) "description" in
13
Added:
In_channel.with_open_text description_path In_channel.input_all
14
Added:
15
Added:
module User = struct
16
Added:
type t = Git.User.t
17
Added:
end
18
Added:
19
Added:
module Commit = struct
20
Added:
open Lwt_result.Syntax
21
Added:
22
Added:
type t = {
23
Added:
hash : string;
24
Added:
parents : string list;
25
Added:
author : User.t;
26
Added:
message : string option;
27
Added:
}
28
Added:
29
Added:
let of_hash store h =
30
Added:
let* v = Store.read store h in
31
Added:
match v with
32
Added:
| Git.Value.Commit c ->
33
Added:
Lwt_result.return
34
Added:
{
35
Added:
hash = Store.Hash.to_hex h;
36
Added:
parents = Store.Value.Commit.parents c |> List.map Store.Hash.to_hex;
37
Added:
author = Store.Value.Commit.author c;
38
Added:
message = Store.Value.Commit.message c;
39
Added:
}
40
Added:
| _ -> Lwt_result.fail @@ `Msg "value is not a commit"
41
Added:
42
Added:
let recent_commits repo n =
43
Added:
let* store = store repo in
44
Added:
let* head = Store.Ref.resolve store Git.Reference.head in
45
Added:
let rec walk acc hash count =
46
Added:
if count = 0 then Lwt_result.return (List.rev acc)
47
Added:
else
48
Added:
let* commit = of_hash store hash in
49
Added:
match commit.parents with
50
Added:
| parent :: _ ->
51
Added:
walk (commit :: acc) (Store.Hash.of_hex parent) (count - 1)
52
Added:
| [] -> Lwt_result.return (List.rev (commit :: acc))
53
Added:
in
54
Added:
walk [] head n
55
Added:
56
Added:
let of_id repo id =
57
Added:
let open Lwt_result.Syntax in
58
Added:
let* store = store repo in
59
Added:
let id = Store.Hash.of_hex id in
60
Added:
of_hash store id
61
Added:
end
62
Added:
63
Added:
module Branch = struct
64
Added:
type t = { name : string }
65
Added:
66
Added:
let all_branches repo =
67
Added:
let open Lwt_result.Syntax in
68
Added:
let* store = Git_unix.Store.v (Fpath.v repo) in
69
Added:
let open Lwt.Syntax in
70
Added:
let* refs = Store.Ref.list store in
71
Added:
let branches =
72
Added:
(* Filter these references for branches! *)
73
Added:
List.map (function _, x -> x |> Store.Hash.to_hex) refs
74
Added:
in
75
Added:
Lwt_result.return branches
76
Added:
end
lib/handlers.ml
@@ -12,8 +12,8 @@
12
12
let repo req = Dream.param req "repo_name"
13
13
14
14
let summary req =
15
Removed:
let* branches = Git_helpers.Branch.all_branches (repo req) in
16
Removed:
let* commits = Git_helpers.Commit.recent_commits (repo req) 10 in
15
Added:
let* branches = Git_presenters.Branch.all_branches (repo req) in
16
Added:
let* commits = Git_presenters.Commit.recent_commits (repo req) 10 in
17
17
let authors = [ "John Pork"; "Sebastian Jellybean" ] in
18
18
Views.Repo.summary (repo req) branches commits authors |> Dream_html.respond
19
19
@@ -23,7 +23,7 @@
23
23
24
24
let commit req =
25
25
let id = match Dream.query req "id" with Some id -> id | None -> "" in
26
Removed:
let* commit = Git_helpers.Commit.of_id (repo req) id in
26
Added:
let* commit = Git_presenters.Commit.of_id (repo req) id in
27
27
Views.Repo.commit (repo req) commit |> Dream_html.respond
28
28
end
29
29