diff options
author | Marius Peter <marius.peter@tutanota.com> | 2025-05-01 19:28:20 +0200 |
---|---|---|
committer | Marius Peter <marius.peter@tutanota.com> | 2025-05-01 19:28:20 +0200 |
commit | f94d27e3df990d9dd7dae69191dd05e5f691b0bb (patch) | |
tree | 0306d92ed67b1b9ad10aa1f7926a25daea119ee5 /lib/git_helpers.ml | |
parent | ad02d028ab875c5d5ba90f3f8bed31f2678ed664 (diff) |
Perform deep refactoring.
Better separation of concern; this paves the way for better testing
down the road.
Diffstat (limited to 'lib/git_helpers.ml')
-rw-r--r-- | lib/git_helpers.ml | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/lib/git_helpers.ml b/lib/git_helpers.ml index 0626d02..4e2215e 100644 --- a/lib/git_helpers.ml +++ b/lib/git_helpers.ml @@ -1,11 +1,39 @@ open Lwt.Infix -let get_head_commit_hash repo_path = - let full_path = Filename.concat Config.git_directory repo_path in - let%lwt store_result = Git_unix.Store.v @@ Fpath.v full_path in +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)) |