[OCaml] Mobile-friendly clone of cgit.
Add custom types.
lib/git_helpers.ml
@@ -1,33 +1,61 @@
1
Removed:
type commit_info = {
1
Added:
open Lwt.Infix
2
Added:
module Store = Git_unix.Store
3
Added:
module Value = Git.Value
4
Added:
5
Added:
type user_record = { name : string; email : string }
6
Added:
7
Added:
type commit_record = {
2
8
hash : string;
9
Added:
parents : string list;
10
Added:
author : Git.User.t;
3
11
message : string option;
4
Removed:
author : string;
5
Removed:
date : string;
6
12
}
7
13
8
14
let full_path path = Filename.concat Config.git_directory path
9
15
10
16
let store repo_path =
11
17
let path = Fpath.v @@ full_path repo_path in
12
Removed:
Git_unix.Store.v ~dotgit:path path
18
Added:
Store.v ~dotgit:path path
13
19
14
Removed:
let latest_commits repo_path ~count =
15
Removed:
let open Lwt_result.Syntax in
16
Removed:
let* store_result = store repo_path in
17
Removed:
let* commits = Git.Commit.Make in
18
Removed:
commits
20
Added:
let short_hash hash = String.sub hash 0 8
19
21
20
Removed:
let all_branches repo_path =
21
Removed:
let cmd =
22
Removed:
Printf.sprintf "git -C %s branch --format=%%(refname:short)"
23
Removed:
@@ full_path repo_path
22
Added:
let repo_description repo_path =
23
Added:
let description_path = Filename.concat (full_path repo_path) "description" in
24
Added:
In_channel.with_open_text description_path In_channel.input_all
25
Added:
26
Added:
(* Read a Git object and turn it into our [commit_record], or propagate an error. *)
27
Added:
let get_commit_record store h =
28
Added:
Store.read store h >>= function
29
Added:
| Ok (Value.Commit c) ->
30
Added:
Lwt.return_ok
31
Added:
{
32
Added:
hash = Store.Hash.to_hex h;
33
Added:
parents = Store.Value.Commit.parents c |> List.map Store.Hash.to_hex;
34
Added:
author = Store.Value.Commit.author c;
35
Added:
message = Store.Value.Commit.message c;
36
Added:
}
37
Added:
| Ok _ -> Lwt.return_error (`Msg "object is not a commit")
38
Added:
| Error e -> Lwt.return_error e
39
Added:
40
Added:
let recent_commits repo_path n =
41
Added:
let open Lwt_result.Syntax in
42
Added:
let* store = store repo_path in
43
Added:
let* head = Store.Ref.resolve store Git.Reference.head in
44
Added:
let rec walk acc hash count =
45
Added:
if count = 0 then Lwt.return_ok (List.rev acc)
46
Added:
else
47
Added:
get_commit_record store hash >>= function
48
Added:
| Error e -> Lwt.return_error e
49
Added:
| Ok commit -> (
50
Added:
match commit.parents with
51
Added:
| parent :: _ ->
52
Added:
walk (commit :: acc) (Store.Hash.of_hex parent) (count - 1)
53
Added:
| [] -> Lwt.return_ok (List.rev (commit :: acc)))
24
54
in
25
Removed:
Lwt.catch
26
Removed:
(fun () ->
27
Removed:
let%lwt output = Lwt_process.pread ("", [| "sh"; "-c"; cmd |]) in
28
Removed:
let branches =
29
Removed:
String.split_on_char '\n' output
30
Removed:
|> List.filter (fun s -> String.trim s <> "")
31
Removed:
in
32
Removed:
Lwt.return_ok branches)
33
Removed:
(fun exn -> Lwt.return_error (Printexc.to_string exn))
55
Added:
walk [] head n
56
Added:
57
Added:
let get_commit repo_path id =
58
Added:
let open Lwt_result.Syntax in
59
Added:
let* store = store repo_path in
60
Added:
let id = Store.Hash.of_hex id in
61
Added:
get_commit_record store id