summaryrefslogtreecommitdiff
path: root/lib/git_helpers.ml
blob: d973a888b656e91aff205678422d46b319412fad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
open Lwt.Infix
open Git_unix

let get_head_commit_hash repo_path =
  let root = Fpath.v repo_path in

  (* 1. Open the Git repository *)
  let%lwt repo =
    Store.v root >>= function
    | Ok repo -> Lwt.return repo
    | Error _ -> Lwt.fail_with "Could not open the Git repository."
  in

  (* 2. Resolve HEAD to get the latest commit hash *)
  let%lwt commit_hash =
    Git_unix.Store.Ref.resolve repo Git.Reference.master >>= function
    | Ok hash -> Lwt.return hash
    | Error _ -> Lwt.fail_with "Failed to resolve HEAD"
  in
  Lwt.return @@ (commit_hash |> Digestif.SHA1.to_hex)
Copyright 2019--2025 Marius PETER