summaryrefslogtreecommitdiff
path: root/lib/git_unhelpers.ml
blob: 5b55c1680b71b72ea1d553c4a7f2e5989f2105a8 (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
(* These will be reimplemented using OCaml's Git library, one day... *)

let get_latest_commits repo_path count =
  let open Printf in
  let full_path = Filename.concat Config.git_directory repo_path in
  let full_cmd =
    let command = sprintf "git -C %s log" full_path in
    let options =
      let format = "--pretty=format:'%ad %s'" in
      let date = "--date=short" in
      let count = sprintf "-n %s" (string_of_int count) in
      [ format; date; count ]
    in
    String.concat " " (command :: options)
  in
  let ic = Unix.open_process_in full_cmd in
  let rec read_lines acc =
    try
      let line = input_line ic in
      read_lines (line :: acc)
    with End_of_file ->
      ignore (Unix.close_process_in ic);
      List.rev acc
  in
  read_lines []

let get_all_branches repo_path =
  let open Printf in
  let full_path = Filename.concat Config.git_directory repo_path in
  let full_cmd = sprintf "git -C %s branch" full_path in
  let ic = Unix.open_process_in full_cmd in
  let rec read_lines acc =
    try
      let line = input_line ic |> String.trim in
      let clean_line =
        if String.length line > 2 && String.sub line 0 2 = "* " then
          String.sub line 2 (String.length line - 2)
          (* Remove "* " from active branch *)
        else line
      in
      read_lines (clean_line :: acc)
    with End_of_file ->
      ignore (Unix.close_process_in ic);
      List.rev acc
  in
  read_lines []
Copyright 2019--2025 Marius PETER