refactor rename config 'user' to 'user_name', default to empty

The root page navbar now shows 'Repositories' when user_name is not configured, preventing the server hostname from leaking into the dashboard. The footer also omits the name when empty. Removes the now-redundant getenv_first helper since user_name no longer derives from environment variables.

Commit
8738fb6623b7b59b5ddbd903c88bc442abe4fbb5
Author
Marius Peter <dev@marius-peter.com>
Author date
Committer
Marius Peter <dev@marius-peter.com>
Committer date
Changed files
config.toml.sample
index ce551010..610898f7 100644..100644
@@ -3,9 +3,11 @@
3 3 # or set OGIT_CONFIG to point to a custom location.
4 4 # Uncomment and modify values as needed.
5 5
6 Removed: # Username displayed in the page footer and default title.
6 Added: # Username displayed in the page footer and navigation title.
7 Added: # When empty, the navigation shows "Repositories" and the footer
8 Added: # omits the name.
7 9 # Type: string
8 Removed: # user = "git"
10 Added: # user_name = ""
9 11
10 12 # Default branch to resolve when HEAD is detached or missing.
11 13 # Type: string
lib/config.ml
index b8b34088..9fb8022a 100644..100644
@@ -3,7 +3,7 @@
3 3 open Toml
4 4
5 5 type t = {
6 Removed: user : string;
6 Added: user_name : string;
7 7 default_branch : string;
8 8 git_project_root : string;
9 9 commits_max_displayed : int;
@@ -23,13 +23,10 @@
23 23 let environment_value name =
24 24 match Sys.getenv_opt name with Some "" | None -> None | value -> value
25 25
26 Removed: let getenv_first names ~default =
27 Removed: List.find_map environment_value names |> Option.value ~default
28 Removed:
29 26 let default =
30 Removed: let home = getenv_first [ "HOME" ] ~default:"." in
27 Added: let home = environment_value "HOME" |> Option.value ~default:"." in
31 28 {
32 Removed: user = getenv_first [ "LOGNAME"; "USER" ] ~default:"git";
29 Added: user_name = "";
33 30 default_branch = "main";
34 31 git_project_root = Filename.concat home "git";
35 32 commits_max_displayed = 10;
@@ -57,7 +54,7 @@
57 54 List.map
58 55 (fun (key, value) -> (Min.key key, value))
59 56 [
60 Removed: ("user", TString t.user);
57 Added: ("user_name", TString t.user_name);
61 58 ("default_branch", TString t.default_branch);
62 59 ("git_project_root", TString t.git_project_root);
63 60 ("commits_max_displayed", TInt t.commits_max_displayed);
@@ -109,7 +106,9 @@
109 106 let of_table table =
110 107 let ( let* ) = Result.bind in
111 108 let* git_project_root = find_string table "git_project_root" in
112 Removed: let* user = find_string table "user" in
109 Added: let* user_name =
110 Added: find_string_opt table "user_name" ~default:default.user_name
111 Added: in
113 112 let* default_branch = find_string table "default_branch" in
114 113 let* commits_max_displayed = find_int table "commits_max_displayed" in
115 114 let* title = find_string_opt table "title" ~default:default.title in
@@ -129,7 +128,7 @@
129 128 Ok
130 129 {
131 130 git_project_root;
132 Removed: user;
131 Added: user_name;
133 132 default_branch;
134 133 commits_max_displayed;
135 134 title;
lib/handlers.ml
index 1a0222c2..31988f82 100644..100644
@@ -17,7 +17,9 @@
17 17 Views.error_page ~status ~title message
18 18
19 19 let root_title config =
20 Removed: if config.Config.title = "" then "Repositories for " ^ config.Config.user
20 Added: if config.Config.title = "" then
21 Added: if config.Config.user_name = "" then "Repositories"
22 Added: else "Repositories for " ^ config.Config.user_name
21 23 else config.Config.title
22 24
23 25 let collect_repo_paths nodes =
@@ -68,8 +70,9 @@
68 70 config.Config.favorite_repositories
69 71 in
70 72 let readme = Resolvers.read_root_readme config in
71 Removed: Views.root ~user:config.Config.user ~root_title:(root_title config) ~dates
72 Removed: ~favorites:sorted_favorites ~archived ?readme regular
73 Added: Views.root ~user_name:config.Config.user_name
74 Added: ~root_title:(root_title config) ~dates ~favorites:sorted_favorites
75 Added: ~archived ?readme regular
73 76 | Error error -> error_response error
74 77
75 78 module Repo = struct
@@ -79,7 +82,8 @@
79 82 | Error error -> error_response error
80 83
81 84 let view_context config repository =
82 Removed: Views.Repo.context ~user:config.Config.user ~root_title:(root_title config)
85 Added: Views.Repo.context ~user_name:config.Config.user_name
86 Added: ~root_title:(root_title config)
83 87 ~repo:(Resolvers.repository_name repository)
84 88 ~description:(Resolvers.repository_description repository)
85 89
lib/views/layout.ml
index d933697d..8a56215d 100644..100644
@@ -83,10 +83,11 @@
83 83 if subtitle = "" then []
84 84 else [ p [ class_ "subtitle" ] [ txt "%s" subtitle ] ]))
85 85
86 Removed: let page_footer user =
86 Added: let page_footer user_name =
87 87 let now = Unix.(time () |> localtime) in
88 88 let year = string_of_int (now.tm_year + 1900) in
89 Removed: HTML.footer [] [ txt "Copyright %s %s" year user ]
89 Added: if user_name = "" then HTML.footer [] [ txt "Copyright %s" year ]
90 Added: else HTML.footer [] [ txt "Copyright %s %s" year user_name ]
90 91
91 92 let head page_title =
92 93 let open HTML in
@@ -111,7 +112,7 @@
111 112 "";
112 113 ]
113 114
114 Removed: let body ~user ~root_title page_data =
115 Added: let body ~user_name ~root_title page_data =
115 116 let open HTML in
116 117 body []
117 118 [
@@ -123,12 +124,12 @@
123 124 | None -> HTML.null []
124 125 | Some _ -> page_header ~has_repo:true page_data.title page_data.subtitle);
125 126 div [ id "main" ] page_data.content;
126 Removed: page_footer user;
127 Added: page_footer user_name;
127 128 script []
128 129 {|document.addEventListener("DOMContentLoaded",function(){var b=document.getElementById("blob");if(!b||typeof hljs==="undefined")return;var cls=b.className.match(/language-([\w-]+)/);if(!cls)return;var lang=cls[1];b.querySelectorAll("span.line").forEach(function(el){var r=hljs.highlight(el.textContent,{language:lang,ignoreIllegals:true});el.innerHTML=r.value})});|};
129 130 ]
130 131
131 Removed: let render ?(page_title = "Ogit") ~user ~root_title body_data =
132 Added: let render ?(page_title = "Ogit") ~user_name ~root_title body_data =
132 133 HTML.html
133 134 [ HTML.lang "en" ]
134 Removed: [ head page_title; body ~user ~root_title body_data ]
135 Added: [ head page_title; body ~user_name ~root_title body_data ]
lib/views/repo.ml
index f2de1a85..d49cb1d7 100644..100644
@@ -5,14 +5,14 @@
5 5 type context = {
6 6 repo : string;
7 7 description : string;
8 Removed: user : string;
8 Added: user_name : string;
9 9 root_title : string;
10 10 }
11 11
12 12 type commit_message = { summary : string; body : string }
13 13
14 Removed: let context ~user ~root_title ~repo ~description =
15 Removed: { repo; description; user; root_title }
14 Added: let context ~user_name ~root_title ~repo ~description =
15 Added: { repo; description; user_name; root_title }
16 16
17 17 let language_of_filename name =
18 18 match Filename.extension name |> String.lowercase_ascii with
@@ -251,7 +251,7 @@
251 251
252 252 let render_page ?heading context ~active content =
253 253 respond
254 Removed: @@ Layout.render ~user:context.user ~root_title:context.root_title
254 Added: @@ Layout.render ~user_name:context.user_name ~root_title:context.root_title
255 255 ~page_title:(page_title context)
256 256 {
257 257 repo = Some context.repo;
lib/views/root.ml
index 350b351e..2c53aabc 100644..100644
@@ -55,8 +55,8 @@
55 55 ];
56 56 ])
57 57
58 Removed: let render ~user ~root_title ~dates ?(favorites = []) ?(archived = []) ?readme
59 Removed: nodes =
58 Added: let render ~user_name ~root_title ~dates ?(favorites = []) ?(archived = [])
59 Added: ?readme nodes =
60 60 let repo_list nodes =
61 61 HTML.(ul [] (List.map (li_of_fs_node ~prefix:"" ~dates) nodes))
62 62 in
@@ -118,7 +118,7 @@
118 118 [ h3 [] [ txt "README" ]; div [ class_ "blob" ] formatted ])
119 119 in
120 120 respond
121 Removed: @@ Layout.render ~user ~root_title
121 Added: @@ Layout.render ~user_name ~root_title
122 122 {
123 123 title = root_title;
124 124 repo = None;
test/test_config.ml
index 6fecc3e6..3af2cc7c 100644..100644
@@ -7,7 +7,7 @@
7 7 let config =
8 8 Ogit.Config.
9 9 {
10 Removed: user = "alice";
10 Added: user_name = "alice";
11 11 default_branch = "main";
12 12 git_project_root = "/srv/git";
13 13 commits_max_displayed = 25;
@@ -21,7 +21,7 @@
21 21 Ogit.Config.write_file ~file (Ogit.Config.to_table config);
22 22 match Ogit.Config.read_file ~file () with
23 23 | Ok config' ->
24 Removed: Alcotest.(check string) "user" config.user config'.user;
24 Added: Alcotest.(check string) "user_name" config.user_name config'.user_name;
25 25 Alcotest.(check string)
26 26 "branch" config.default_branch config'.default_branch;
27 27 Alcotest.(check string)
@@ -36,12 +36,12 @@
36 36 with_temp_file "ogit" ".toml" (fun file ->
37 37 Out_channel.with_open_text file (fun channel ->
38 38 Printf.fprintf channel
39 Removed: "user = \"bob\"\n\
40 Removed: default_branch = \"main\"\n\
39 Added: "default_branch = \"main\"\n\
41 40 git_project_root = \"/srv/git\"\n\
42 41 commits_max_displayed = 10\n");
43 42 match Ogit.Config.read_file ~file () with
44 43 | Ok config ->
44 Added: Alcotest.(check string) "default user_name" "" config.user_name;
45 45 Alcotest.(check string) "default host" "127.0.0.1" config.host;
46 46 Alcotest.(check int) "default port" 8081 config.port
47 47 | Error error -> fail_config_error error)