From e347064abe3ce4c90fbad23d36e5d61a149e2389 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Fri, 14 Feb 2025 08:48:07 +0800 Subject: 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 +- 11 files changed, 31 insertions(+), 41 deletions(-) diff --git a/git_ref.go b/git_ref.go index 08d757b..46f08e0 100644 --- a/git_ref.go +++ b/git_ref.go @@ -16,7 +16,7 @@ var ( 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 c9c4949..ea1854e 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -17,7 +17,6 @@ func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string 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 72943be..d4422b7 100644 --- a/http_handle_repo_log.go +++ b/http_handle_repo_log.go @@ -8,7 +8,7 @@ import ( // 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 b134f8d..1d04f6c 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]a 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 @@ func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]a } 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 69bc40f..f5fbaad 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 @@ import ( 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 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] } 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 3804df7..6626ae4 100644 --- a/http_server.go +++ b/http_server.go @@ -113,6 +113,15 @@ func (router *http_router_t) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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 @@ func (router *http_router_t) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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 f6615d7..7faea1e 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 0ad6f39..88b978d 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 dc1a5b6..d306bbf 100644 --- a/templates/repo_raw_dir.html.tmpl +++ b/templates/repo_raw_dir.html.tmpl @@ -12,18 +12,18 @@ {{- $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 5e56dc9..34559da 100644 --- a/templates/repo_tree_dir.html.tmpl +++ b/templates/repo_tree_dir.html.tmpl @@ -12,18 +12,18 @@ {{- $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 0bf9164..8eacd28 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 }}
-- cgit v1.2.3
Commits on {{ .ref }}Commits on {{ .ref_name }}
ID
- (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 }}{{ 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 }}