blob: ed917a8a6f957adaafe1d670076fa945e638f194 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
open Lwt.Infix
module Store = Git_unix.Store
module Value = Git.Value
type user_record = { name : string; email : string }
let full_path path = Filename.concat Config.git_directory path
let store repo_path =
let path = Fpath.v @@ full_path repo_path in
Store.v ~dotgit:path path
let short_hash hash = String.sub hash 0 8
let repo_description repo_path =
let description_path = Filename.concat (full_path repo_path) "description" in
In_channel.with_open_text description_path In_channel.input_all
module Commit = struct
type t = {
hash : string;
parents : string list;
author : Git.User.t;
message : string option;
}
let of_hash store h =
Store.read store h >>= function
| Ok (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;
}
| Ok _ -> Lwt.return_error (`Msg "object is not a commit")
| Error e -> Lwt.return_error e
let recent_commits repo_path n =
let open Lwt_result.Syntax in
let* store = store repo_path 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
of_hash store hash >>= function
| Error e -> Lwt.return_error e
| Ok commit -> (
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_path id =
let open Lwt_result.Syntax in
let* store = store repo_path in
let id = Store.Hash.of_hex id in
of_hash store id
end
module Branch = struct
let all_branches repo_path = [ "foo"; "bar"; repo_path ]
end
|