[OCaml] Mobile-friendly clone of cgit.
Refactor and provide better implementation for all_branches.
lib/git_presenters.ml
@@ -1,9 +1,10 @@
1
1
(* -*- mode: tuareg; -*- *)
2
2
3
3
module Store = Git_unix.Store
4
Added:
open Lwt_result.Syntax
4
5
open Config
5
6
6
Removed:
let full_path path = Filename.concat config.repositories_root_path path
7
Added:
let full_path path = Filename.concat config.git_project_root path
7
8
8
9
let store repo =
9
10
let path = Fpath.v @@ full_path repo in
@@ -13,68 +14,60 @@
13
14
let description_path = Filename.concat (full_path repo) "description" in
14
15
In_channel.with_open_text description_path In_channel.input_all
15
16
16
Removed:
module User = struct
17
Removed:
type t = Git.User.t
18
Removed:
end
17
Added:
type user = Git.User.t
19
18
20
Removed:
module Commit = struct
21
Removed:
open Lwt_result.Syntax
19
Added:
(* let all_authors store = *)
20
Added:
(* let* store = store repo in *)
22
21
23
Removed:
type t = {
24
Removed:
hash : string;
25
Removed:
short_hash : string;
26
Removed:
parents : string list;
27
Removed:
author : User.t;
28
Removed:
message : string option;
29
Removed:
}
22
Added:
type commit = {
23
Added:
hash : string;
24
Added:
short_hash : string;
25
Added:
parents : string list;
26
Added:
author : user;
27
Added:
message : string option;
28
Added:
}
30
29
31
Removed:
let to_commit store h =
32
Removed:
let* v = Store.read store h in
33
Removed:
match v with
34
Removed:
| Git.Value.Commit c ->
35
Removed:
let hash = Store.Hash.to_hex h in
36
Removed:
Lwt_result.return
37
Removed:
{
38
Removed:
hash;
39
Removed:
short_hash = String.sub hash 0 8;
40
Removed:
parents = Store.Value.Commit.parents c |> List.map Store.Hash.to_hex;
41
Removed:
author = Store.Value.Commit.author c;
42
Removed:
message = Store.Value.Commit.message c;
43
Removed:
}
44
Removed:
| _ -> Lwt_result.fail @@ `Msg "value is not a commit"
30
Added:
let to_commit store h =
31
Added:
let* v = Store.read store h in
32
Added:
match v with
33
Added:
| Git.Value.Commit c ->
34
Added:
let hash = Store.Hash.to_hex h in
35
Added:
Lwt_result.return
36
Added:
{
37
Added:
hash;
38
Added:
short_hash = String.sub hash 0 8;
39
Added:
parents = Store.Value.Commit.parents c |> List.map Store.Hash.to_hex;
40
Added:
author = Store.Value.Commit.author c;
41
Added:
message = Store.Value.Commit.message c;
42
Added:
}
43
Added:
| _ -> Lwt_result.fail @@ `Msg "value is not a commit"
45
44
46
Removed:
let recent_commits repo n =
47
Removed:
let* store = store repo in
48
Removed:
let* head = Store.Ref.resolve store Git.Reference.head in
49
Removed:
let rec walk acc hash count =
50
Removed:
if count = 0 then Lwt_result.return (List.rev acc)
51
Removed:
else
52
Removed:
let* commit = to_commit store hash in
53
Removed:
match commit.parents with
54
Removed:
| parent :: _ ->
55
Removed:
walk (commit :: acc) (Store.Hash.of_hex parent) (count - 1)
56
Removed:
| [] -> Lwt_result.return (List.rev (commit :: acc))
57
Removed:
in
58
Removed:
walk [] head n
45
Added:
let recent_commits repo n =
46
Added:
let* store = store repo in
47
Added:
let* head = Store.Ref.resolve store Git.Reference.head in
48
Added:
let rec walk acc hash count =
49
Added:
if count = 0 then Lwt_result.return (List.rev acc)
50
Added:
else
51
Added:
let* commit = to_commit store hash in
52
Added:
match commit.parents with
53
Added:
| parent :: _ ->
54
Added:
walk (commit :: acc) (Store.Hash.of_hex parent) (count - 1)
55
Added:
| [] -> Lwt_result.return (List.rev (commit :: acc))
56
Added:
in
57
Added:
walk [] head n
59
58
60
Removed:
let of_id repo id =
61
Removed:
let open Lwt_result.Syntax in
62
Removed:
let* store = store repo in
63
Removed:
let id = Store.Hash.of_hex id in
64
Removed:
to_commit store id
65
Removed:
end
59
Added:
let of_id repo id =
60
Added:
let* store = store repo in
61
Added:
let id = Store.Hash.of_hex id in
62
Added:
to_commit store id
66
63
67
Removed:
module Branch = struct
68
Removed:
type t = { name : string }
64
Added:
type branch = { name : string }
69
65
70
Removed:
let all_branches repo =
71
Removed:
let open Lwt_result.Syntax in
72
Removed:
let* store = Git_unix.Store.v (Fpath.v repo) in
73
Removed:
let open Lwt.Syntax in
74
Removed:
let* refs = Store.Ref.list store in
75
Removed:
let branches =
76
Removed:
(* Filter these references for branches! *)
77
Removed:
List.map (function _, x -> x |> Store.Hash.to_hex) refs
78
Removed:
in
79
Removed:
Lwt_result.return branches
80
Removed:
end
66
Added:
let all_branches repo =
67
Added:
let* store = store repo in
68
Added:
let open Lwt.Syntax in
69
Added:
let* refs = Store.Ref.list store in
70
Added:
let branches =
71
Added:
List.map (fun (ref, _) -> { name = Git.Reference.to_string ref }) refs
72
Added:
in
73
Added:
Lwt_result.return branches