summaryrefslogtreecommitdiff
path: root/lib/git_helpers.ml
blob: 4e2215e84bf4a72bc05c05d839d313b88146e745 (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
open Lwt.Infix

let full_path path = Filename.concat Config.git_directory path

let head_commit_hash repo_path =
  let%lwt store_result = Git_unix.Store.v @@ Fpath.v @@ full_path repo_path in
  match store_result with
  | Error _ -> Lwt.return_error "Could not open the Git repository."
  | Ok store -> (
      Git_unix.Store.Ref.resolve store Git.Reference.head >|= function
      | Error _ -> Error ("Failed to resolve HEAD for repo " ^ repo_path)
      | Ok hash -> Ok (Git_unix.Store.Hash.to_hex hash))

let latest_commits repo_path count =
  let cmd =
    Printf.sprintf "git -C %s log --pretty=format:'%%ad %%s' --date=short -n %d"
      (full_path repo_path) count
  in
  Lwt.catch
    (fun () ->
      let%lwt output = Lwt_process.pread ("", [| "sh"; "-c"; cmd |]) in
      let lines = String.split_on_char '\n' output in
      Lwt.return_ok lines)
    (fun exn -> Lwt.return_error (Printexc.to_string exn))

let all_branches repo_path =
  let cmd =
    Printf.sprintf "git -C %s branch --format=%%(refname:short)"
    @@ full_path repo_path
  in
  Lwt.catch
    (fun () ->
      let%lwt output = Lwt_process.pread ("", [| "sh"; "-c"; cmd |]) in
      let branches =
        String.split_on_char '\n' output
        |> List.filter (fun s -> String.trim s <> "")
      in
      Lwt.return_ok branches)
    (fun exn -> Lwt.return_error (Printexc.to_string exn))
Copyright 2019--2025 Marius PETER