From e347064abe3ce4c90fbad23d36e5d61a149e2389 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Fri, 14 Feb 2025 08:48:07 +0800 Subject: [PATCH] http_*: Refactor to reduce duplication --- git_ref.go | 2 +- http_handle_repo_index.go | 1 - http_handle_repo_log.go | 2 +- http_handle_repo_raw.go | 15 ++------------- http_handle_repo_tree.go | 14 ++------------ http_server.go | 16 ++++++++++++++-- templates/repo_index.html.tmpl | 6 +++--- templates/repo_log.html.tmpl | 2 +- templates/repo_raw_dir.html.tmpl | 6 +++--- templates/repo_tree_dir.html.tmpl | 6 +++--- templates/repo_tree_file.html.tmpl | 2 +- diff --git a/git_ref.go b/git_ref.go index 08d757bbd1e83acf75fe3b3f44cc8840698cec8d..46f08e04be6a128995ab1aaf26c80bde443154c9 100644 --- a/git_ref.go +++ b/git_ref.go @@ -16,7 +16,7 @@ ) func get_ref_hash_from_type_and_name(repo *git.Repository, ref_type, ref_name string) (ref_hash plumbing.Hash, ret_err error) { switch ref_type { - case "head": + case "": head, err := repo.Head() if err != nil { ret_err = misc.Wrap_one_error(err_getting_head, err) diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index c9c4949978f64b1dedea7ef812e4b37aae0de784..ea1854e3cd883e7c35e87c0a7cd4455ef0bca607 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -17,7 +17,6 @@ if err != nil { http.Error(w, "Error getting repo HEAD: "+err.Error(), http.StatusInternalServerError) return } - params["ref"] = head.Name().Short() head_hash := head.Hash() recent_commits, err := get_recent_commits(repo, head_hash, 3) if err != nil { diff --git a/http_handle_repo_log.go b/http_handle_repo_log.go index 72943be72c64657bb6b08372dd7d412496081705..d4422b7bdc19accd9bc1a04b7961259895d6b184 100644 --- a/http_handle_repo_log.go +++ b/http_handle_repo_log.go @@ -8,7 +8,7 @@ ) // TODO: I probably shouldn't include *all* commits here... func handle_repo_log(w http.ResponseWriter, r *http.Request, params map[string]any) { - group_name, repo_name, ref_name := params["group_name"].(string), params["repo_name"].(string), params["ref"].(string) + group_name, repo_name, ref_name := params["group_name"].(string), params["repo_name"].(string), params["ref_name"].(string) repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) diff --git a/http_handle_repo_raw.go b/http_handle_repo_raw.go index b134f8dc9400da3212bc52159271f08a864ee612..1d04f6cda6242bf9c9f59a416398d0a2527d2e7a 100644 --- a/http_handle_repo_raw.go +++ b/http_handle_repo_raw.go @@ -1,7 +1,6 @@ package main import ( - "errors" "fmt" "net/http" "path" @@ -14,17 +13,7 @@ func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]any) { raw_path_spec := params["rest"].(string) group_name, repo_name, path_spec := params["group_name"].(string), params["repo_name"].(string), strings.TrimSuffix(raw_path_spec, "/") - ref_type, ref_name, err := get_param_ref_and_type(r) - if err != nil { - if errors.Is(err, err_no_ref_spec) { - ref_type = "head" - } else { - http.Error(w, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) - return - } - } - - params["ref_type"], params["ref"], params["path_spec"] = ref_type, ref_name, path_spec + params["path_spec"] = path_spec repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { @@ -33,7 +22,7 @@ return } params["repo_description"] = description - ref_hash, err := get_ref_hash_from_type_and_name(repo, ref_type, ref_name) + ref_hash, err := get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)) if err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index 69bc40f87742e624aab1304d8993963ad9aa8a7f..f5fbaadca8e3a59d30291f1fa42c3adf5321b8e5 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "errors" "html/template" "net/http" "path" @@ -17,16 +16,7 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string]any) { raw_path_spec := params["rest"].(string) group_name, repo_name, path_spec := params["group_name"].(string), params["repo_name"].(string), strings.TrimSuffix(raw_path_spec, "/") - ref_type, ref_name, err := get_param_ref_and_type(r) - if err != nil { - if errors.Is(err, err_no_ref_spec) { - ref_type = "head" - } else { - http.Error(w, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) - return - } - } - params["ref_type"], params["ref"], params["path_spec"] = ref_type, ref_name, path_spec + params["path_spec"] = path_spec repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) @@ -34,7 +24,7 @@ return } params["repo_description"] = description - ref_hash, err := get_ref_hash_from_type_and_name(repo, ref_type, ref_name) + ref_hash, err := get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)) if err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return diff --git a/http_server.go b/http_server.go index 3804df77b4132b9442fce275727fcc607f28e616..6626ae45171b41cab7f84182a6ca510c6d2df2b7 100644 --- a/http_server.go +++ b/http_server.go @@ -113,6 +113,15 @@ params["group_name"] = segments[0] switch module_type { case "repos": params["repo_name"] = module_name + params["ref_type"], params["ref_name"], err = get_param_ref_and_type(r) + if err != nil { + if errors.Is(err, err_no_ref_spec) { + params["ref_type"] = "" + } else { + http.Error(w, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) + return + } + } // TODO: subgroups if non_empty_last_segments_len == separator_index+3 { if !dir_mode { @@ -133,15 +142,18 @@ case "raw": params["rest"] = strings.Join(segments[separator_index+4:], "/") handle_repo_raw(w, r, params) case "log": - if non_empty_last_segments_len != separator_index+5 { + if non_empty_last_segments_len > separator_index+5 { http.Error(w, "Too many parameters", http.StatusBadRequest) + return + } else if non_empty_last_segments_len < separator_index+5 { + http.Error(w, "Insufficient parameters", http.StatusBadRequest) return } if dir_mode { http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusSeeOther) return } - params["ref"] = segments[separator_index+4] + params["ref_name"] = segments[separator_index+4] handle_repo_log(w, r, params) case "commit": if dir_mode { diff --git a/templates/repo_index.html.tmpl b/templates/repo_index.html.tmpl index f6615d77cf31957505d36ec7399e36d5de884060..7faea1efc947aeacd9132c9c98610d577f5d66c1 100644 --- a/templates/repo_index.html.tmpl +++ b/templates/repo_index.html.tmpl @@ -37,7 +37,7 @@ - + @@ -60,11 +60,11 @@
- + - {{- $ref := .ref }} + {{- $ref := .ref_name }} {{- range .files }} diff --git a/templates/repo_log.html.tmpl b/templates/repo_log.html.tmpl index 0ad6f39f610bacfb774da6f3bcdfe4c25a345ffa..88b978df9aa636c2dd26831d0d8e62f0b201973c 100644 --- a/templates/repo_log.html.tmpl +++ b/templates/repo_log.html.tmpl @@ -11,7 +11,7 @@
{{ .Mode }}
- + diff --git a/templates/repo_raw_dir.html.tmpl b/templates/repo_raw_dir.html.tmpl index dc1a5b64ae905743cfe162436ecdca8d94255d57..d306bbf315b935cdb7b8ecdcd7a704d9ff9ba205 100644 --- a/templates/repo_raw_dir.html.tmpl +++ b/templates/repo_raw_dir.html.tmpl @@ -12,18 +12,18 @@
Commits on {{ .ref }}Commits on {{ .ref_name }}
ID
{{- $path_spec := .path_spec }} - {{- $ref := .ref }} + {{- $ref := .ref_name }} {{- $ref_type := .ref_type }} {{- range .files }} - + {{- end }} diff --git a/templates/repo_tree_dir.html.tmpl b/templates/repo_tree_dir.html.tmpl index 5e56dc9e87ff19a48175fadf972797105d9eab30..34559da26b8802402aa72cc76f8e810297ba186e 100644 --- a/templates/repo_tree_dir.html.tmpl +++ b/templates/repo_tree_dir.html.tmpl @@ -12,18 +12,18 @@
- (Raw) /{{ .path_spec }}{{ if ne .path_spec "" }}/{{ end }}{{ if .ref }} on {{ .ref }}{{ end }} + (Raw) /{{ .path_spec }}{{ if ne .path_spec "" }}/{{ end }}{{ if .ref_name }} on {{ .ref_name }}{{ end }}
{{ .Mode }}{{ .Name }}{{ if not .Is_file }}/{{ end }}{{ .Name }}{{ if not .Is_file }}/{{ end }} {{ .Size }}
{{- $path_spec := .path_spec }} - {{- $ref := .ref }} + {{- $ref := .ref_name }} {{- $ref_type := .ref_type }} {{- range .files }} - + {{- end }} diff --git a/templates/repo_tree_file.html.tmpl b/templates/repo_tree_file.html.tmpl index 0bf9164146eb6b7ef6c8cd34bac8615b0b3f4469..8eacd288c21565f9d4273d170d381ea1a9f6d535 100644 --- a/templates/repo_tree_file.html.tmpl +++ b/templates/repo_tree_file.html.tmpl @@ -10,7 +10,7 @@ {{ template "header" . }}

- /{{ .path_spec }} (raw) + /{{ .path_spec }} (raw)

{{ .file_contents }}
-- 2.48.1
- /{{ .path_spec }}{{ if ne .path_spec "" }}/{{ end }}{{ if .ref }} on {{ .ref }}{{ end }} + /{{ .path_spec }}{{ if ne .path_spec "" }}/{{ end }}{{ if .ref_name }} on {{ .ref_name }}{{ end }}
{{ .Mode }}{{ .Name }}{{ if not .Is_file }}/{{ end }}{{ .Name }}{{ if not .Is_file }}/{{ end }} {{ .Size }}