summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/views/dune2
-rw-r--r--lib/views/ogit_root.ml16
-rw-r--r--lib/views/repo.ml38
-rw-r--r--lib/views/repo_root.ml41
4 files changed, 47 insertions, 50 deletions
diff --git a/lib/views/dune b/lib/views/dune
index 1b722c5..2c9ab5e 100644
--- a/lib/views/dune
+++ b/lib/views/dune
@@ -1,4 +1,4 @@
(library
(name views)
(public_name ogit.views)
- (libraries dream-html git))
+ (libraries dream-html git-unix))
diff --git a/lib/views/ogit_root.ml b/lib/views/ogit_root.ml
index 02928c9..ca8e870 100644
--- a/lib/views/ogit_root.ml
+++ b/lib/views/ogit_root.ml
@@ -3,16 +3,10 @@ open HTML
let git_directory = Filename.concat (Unix.getenv "HOME") "git"
-let repositories_in dir =
- let links =
- (* if Sys.file_exists dir && Sys.is_directory dir then *)
- let repositories = Sys.readdir dir |> Array.to_list
- and repo_to_li repo = li [] [ a [ href "%s" repo ] [ txt "%s" repo ] ] in
- let repositories_as_items = List.map repo_to_li repositories in
- ul [] repositories_as_items
- (* else txt "No repositories found in %s" dir *)
- in
- div [ id "repositories" ] [ links ]
+let repositories_in directory =
+ let repositories = Sys.readdir directory |> Array.to_list
+ and li_of_repo repo = li [] [ a [ href "%s" repo ] [ txt "%s" repo ] ] in
+ div [ id "repositories" ] [ ul [] @@ List.map li_of_repo repositories ]
-let main_content = div [] [ txt "Hello World!"; repositories_in git_directory ]
+let main_content = null [ txt "Hello World!"; repositories_in git_directory ]
let render = Layouts.application ~page_title:"My repositories" ~main_content
diff --git a/lib/views/repo.ml b/lib/views/repo.ml
deleted file mode 100644
index 7fefce8..0000000
--- a/lib/views/repo.ml
+++ /dev/null
@@ -1,38 +0,0 @@
-open Dream_html
-open HTML
-open Git_unix (* Import Git_unix for working with repositories *)
-
-let git_directory = Filename.concat (Unix.getenv "HOME") "git"
-let repo_path repo_name = Filename.concat git_directory repo_name
-
-let list_commits repo_name =
- let repo_dir = repo_path repo_name in
- if Sys.file_exists repo_dir && Sys.is_directory repo_dir then
- let store = Store.v (Fpath.v repo_dir) |> Result.get_ok in
- let head = Store.Head.get store |> Result.get_ok in
- let rec read_commits commits acc =
- match commits with
- | [] -> acc
- | hash :: rest ->
- let commit = Store.read_commit store hash |> Result.get_ok in
- let hash_str = Hash.to_hex hash in
- let message = Commit.message commit in
- let author = Commit.author commit in
- let date = Commit.date commit in
- let formatted_commit =
- li []
- [
- txt (Printf.sprintf "%s - %s (%s)" hash_str message author);
- txt (" on " ^ date);
- ]
- in
- read_commits rest (formatted_commit :: acc)
- in
- let all_commits = Store.Commit.list store head in
- ul [] (read_commits all_commits [])
- else ul [] [ li [] [ txt "Error: Repository not found." ] ]
-
-(* Render the repository's commit list as HTML *)
-let render repo_name =
- let commits = list_commits repo_name in
- Layouts.application ~page_title:("Commits for " ^ repo_name) commits
diff --git a/lib/views/repo_root.ml b/lib/views/repo_root.ml
new file mode 100644
index 0000000..dd391d8
--- /dev/null
+++ b/lib/views/repo_root.ml
@@ -0,0 +1,41 @@
+open Dream_html
+open HTML
+open Git_unix
+
+let git_directory = Filename.concat (Unix.getenv "HOME") "git"
+
+let err_to_string err = Fmt.to_to_string Store.pp_error err
+
+let get_head_commit repo_path =
+ Lwt_main.run (
+ let open Lwt.Infix in
+ match Git.Reference.of_string "HEAD" with
+ | Error err ->
+ Lwt.return (Error ("Invalid HEAD reference string: " ^ err_to_string err))
+ | Ok head_ref_name ->
+ Store.v (Fpath.v repo_path) >>= function
+ | Error err ->
+ Lwt.return (Error ("Failed to open repository: " ^ err_to_string err))
+ | Ok store ->
+ Store.Ref.resolve store head_ref_name >>= function
+ | Error err ->
+ Lwt.return (Error ("Failed to resolve HEAD: " ^ err_to_string err))
+ | Ok head_oid -> Lwt.return (Ok head_oid)
+ (* Git.Commit.v store head_oid >>= function *)
+ (* | Error err -> *)
+ (* Lwt.return (Error ("Failed to get commit from hash: " ^ err_to_string err)) *)
+ (* | Ok commit -> *)
+ (* Lwt.return (Ok commit) *)
+ )
+
+let main_content =
+ let open Lwt.Infix in
+ Lwt.async (fun _ ->
+ get_head_commit git_directory >>= function
+ | Error msg ->
+ Lwt.map (fun content -> [ p [] [ txt content ]]) (Error msg)
+ | Ok head_oid ->
+ Lwt.map (fun content -> [ p [] [ txt "HEAD commit hash: %s" content ]]) (Store.Hash.to_string head_oid)
+ )
+
+let render = Layouts.application ~page_title:"My repositories" ~main_content
Copyright 2019--2025 Marius PETER