package mgit

  1. Overview
  2. Docs

Source file mgit_closure.ml

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
type read = Carton.Uid.t -> (Carton.Kind.t * string) option

let error_msgf fmt = Fmt.kstr (fun msg -> Error (`Msg msg)) fmt

exception Missing of Carton.Uid.t
exception Not_a_commit of Carton.Uid.t

let commits ~read ~depth roots =
  let seen = Hashtbl.create 0x10 in
  let kept = ref [] and boundary = ref [] in
  let visited uid =
    let uid = (uid : Carton.Uid.t :> string) in
    if Hashtbl.mem seen uid then true
    else (
      Hashtbl.replace seen uid ();
      false)
  in
  let rec go = function
    | [] -> ()
    | (uid, _) :: rest when visited uid -> go rest
    | (uid, d) :: rest when depth > 0 && d > depth ->
        boundary := uid :: !boundary;
        go rest
    | (uid, d) :: rest ->
        begin match read uid with
        | None ->
            boundary := uid :: !boundary;
            go rest
        | Some (`A, payload) ->
            kept := uid :: !kept;
            let parents = Mgit_object.parents_of_commit payload in
            let parents = List.map (fun uid -> (uid, d + 1)) parents in
            go (rest @ parents)
        | Some _ -> raise (Not_a_commit uid)
        end
  in
  go (List.map (fun root -> (root, 1)) roots);
  (List.rev !kept, List.rev !boundary)

let objects ~read commits =
  let seen = Hashtbl.create 0x100 in
  let trees = ref [] and blobs = ref [] in
  let visited uid =
    let uid = (uid : Carton.Uid.t :> string) in
    if Hashtbl.mem seen uid then true
    else (
      Hashtbl.replace seen uid ();
      false)
  in
  let rec go = function
    | [] -> ()
    | uid :: rest when visited uid -> go rest
    | uid :: rest ->
        begin match read uid with
        | None -> raise (Missing uid)
        | Some (`C, _) ->
            blobs := uid :: !blobs;
            go rest
        | Some (`B, payload) ->
            trees := uid :: !trees;
            begin match Mgit_object.Tree.of_string payload with
            | Ok tree ->
                let fn { Mgit_object.Tree.perm; node; _ } =
                  if perm = `Commit then None else Some node
                in
                go (rest @ List.filter_map fn (Mgit_object.Tree.to_list tree))
            | Error (`Msg _) -> raise (Missing uid)
            end
        | Some ((`A | `D), _) -> raise (Not_a_commit uid)
        end
  in
  let roots =
    let fn uid =
      match read uid with
      | Some (`A, payload) -> Mgit_object.tree_of_commit payload
      | _ -> None
    in
    List.filter_map fn commits
  in
  List.iter (fun uid -> go [ uid ]) roots;
  (List.rev !trees, List.rev !blobs)

let closure ~read ~depth roots =
  match
    let kept, boundary = commits ~read ~depth roots in
    let trees, blobs = objects ~read kept in
    (kept @ trees @ blobs, boundary)
  with
  | value -> Ok value
  | exception Missing uid ->
      error_msgf "%a is unavailable" Mgit_object.pp_uid uid
  | exception Not_a_commit uid ->
      error_msgf "%a is not the expected kind of object" Mgit_object.pp_uid uid

let uncommon ~read ~exclude roots =
  match
    let everything roots =
      let kept, _ = commits ~read ~depth:max_int roots in
      let trees, blobs = objects ~read kept in
      kept @ trees @ blobs
    in
    let known = Hashtbl.create 0x100 in
    let fn uid =
      match read uid with
      | Some (`A, _) ->
          List.iter
            (fun uid -> Hashtbl.replace known (uid : Carton.Uid.t :> string) ())
            (everything [ uid ])
      | _ -> ()
    in
    List.iter fn exclude;
    let unknown uid = not (Hashtbl.mem known (uid : Carton.Uid.t :> string)) in
    List.filter unknown (everything roots)
  with
  | value -> Ok value
  | exception Missing uid ->
      error_msgf "%a is unavailable" Mgit_object.pp_uid uid
  | exception Not_a_commit uid ->
      error_msgf "%a is not the expected kind of object" Mgit_object.pp_uid uid