From b63917094e6a69c8c01f9e7df8716f3d5fc42385 Mon Sep 17 00:00:00 2001
From: Runxi Yu
Date: Fri, 7 Mar 2025 19:20:54 +0800
Subject: repo/index: Display repo info only, when commits/files unavailable
---
http_handle_repo_index.go | 27 ++++++------
http_template_funcs.go | 2 +-
resources.go | 6 +--
templates/repo_index.tmpl | 108 ++++++++++++++++++++++++----------------------
4 files changed, 74 insertions(+), 69 deletions(-)
diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go
index 66116c7..15b5173 100644
--- a/http_handle_repo_index.go
+++ b/http_handle_repo_index.go
@@ -23,29 +23,28 @@ func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string
repo, repo_name, group_path = params["repo"].(*git.Repository), params["repo_name"].(string), params["group_path"].([]string)
- if ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil {
- http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError)
- return
+ ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string))
+ if err != nil {
+ goto no_ref
}
if recent_commits, err = get_recent_commits(repo, ref_hash, 3); err != nil {
- http.Error(w, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError)
- return
+ goto no_ref
}
params["commits"] = recent_commits
- commit_object, err = repo.CommitObject(ref_hash)
- if err != nil {
- http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError)
- return
+
+ if commit_object, err = repo.CommitObject(ref_hash); err != nil {
+ goto no_ref
}
- tree, err = commit_object.Tree()
- if err != nil {
- http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError)
- return
+
+ if tree, err = commit_object.Tree(); err != nil {
+ goto no_ref
}
- params["readme_filename"], params["readme"] = render_readme_at_tree(tree)
params["files"] = build_display_git_tree(tree)
+ params["readme_filename"], params["readme"] = render_readme_at_tree(tree)
+
+no_ref:
params["http_clone_url"] = generate_http_remote_url(group_path, repo_name)
params["ssh_clone_url"] = generate_ssh_remote_url(group_path, repo_name)
diff --git a/http_template_funcs.go b/http_template_funcs.go
index a7ebb1e..016d268 100644
--- a/http_template_funcs.go
+++ b/http_template_funcs.go
@@ -4,9 +4,9 @@
package main
import (
+ "net/url"
"path"
"strings"
- "net/url"
)
func first_line(s string) string {
diff --git a/resources.go b/resources.go
index ff1286f..7ada53f 100644
--- a/resources.go
+++ b/resources.go
@@ -34,9 +34,9 @@ var templates *template.Template
func load_templates() (err error) {
templates, err = template.New("templates").Funcs(template.FuncMap{
- "first_line": first_line,
- "base_name": base_name,
- "path_escape": path_escape,
+ "first_line": first_line,
+ "base_name": base_name,
+ "path_escape": path_escape,
"query_escape": query_escape,
}).ParseFS(resources_fs, "templates/*")
return err
diff --git a/templates/repo_index.tmpl b/templates/repo_index.tmpl
index 617b8ad..da67df4 100644
--- a/templates/repo_index.tmpl
+++ b/templates/repo_index.tmpl
@@ -41,61 +41,67 @@
Merge requests
-
+ {{ end }}
+ {{ if .readme }}
+
+ {{ .readme }}
+
+ {{ end }}
--
cgit v1.2.3