From 7133932ac6b31530f009ba892e193d54116c7445 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 31 Mar 2025 11:55:15 +0800 Subject: Add branches page --- http_handle_branches.go | 44 +++++++++++++++++++++++++++++ http_server.go | 6 ++++ static/style.css | 2 +- templates/repo_branches.tmpl | 66 ++++++++++++++++++++++++++++++++++++++++++++ templates/repo_index.tmpl | 47 +++++-------------------------- 5 files changed, 124 insertions(+), 41 deletions(-) create mode 100644 http_handle_branches.go create mode 100644 templates/repo_branches.tmpl diff --git a/http_handle_branches.go b/http_handle_branches.go new file mode 100644 index 0000000..c7f9d39 --- /dev/null +++ b/http_handle_branches.go @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-FileContributor: Runxi Yu + +package main + +import ( + "net/http" + "strings" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/storer" +) + +func httpHandleRepoBranches(writer http.ResponseWriter, _ *http.Request, params map[string]any) { + var repo *git.Repository + var repoName string + var groupPath []string + var err error + var notes []string + var branches []string + var branchesIter storer.ReferenceIter + + repo, repoName, groupPath = params["repo"].(*git.Repository), params["repo_name"].(string), params["group_path"].([]string) + + if strings.Contains(repoName, "\n") || sliceContainsNewlines(groupPath) { + notes = append(notes, "Path contains newlines; HTTP Git access impossible") + } + + branchesIter, err = repo.Branches() + if err == nil { + _ = branchesIter.ForEach(func(branch *plumbing.Reference) error { + branches = append(branches, branch.Name().Short()) + return nil + }) + } + params["branches"] = branches + + params["http_clone_url"] = genHTTPRemoteURL(groupPath, repoName) + params["ssh_clone_url"] = genSSHRemoteURL(groupPath, repoName) + params["notes"] = notes + + renderTemplate(writer, "repo_branches", params) +} diff --git a/http_server.go b/http_server.go index 24be7e4..a877b16 100644 --- a/http_server.go +++ b/http_server.go @@ -199,6 +199,12 @@ func (router *forgeHTTPRouter) ServeHTTP(writer http.ResponseWriter, request *ht return } httpHandleRepoTree(writer, request, params) + case "branches": + if redirectDir(writer, request) { + return + } + httpHandleRepoBranches(writer, request, params) + return case "raw": if anyContain(segments[sepIndex+4:], "/") { errorPage400(writer, params, "Repo tree paths may not contain slashes in any segments") diff --git a/static/style.css b/static/style.css index ba85d3f..3c36e19 100644 --- a/static/style.css +++ b/static/style.css @@ -555,7 +555,7 @@ td > ul { padding-bottom: 0.2rem; } -.repo-header, .padding-wrapper, .repo-header-extension-content, #main-header { +.repo-header, .padding-wrapper, .repo-header-extension-content, #main-header, .readingwidth { padding-left: 1rem; padding-right: 1rem; max-width: 60rem; diff --git a/templates/repo_branches.tmpl b/templates/repo_branches.tmpl new file mode 100644 index 0000000..b06da10 --- /dev/null +++ b/templates/repo_branches.tmpl @@ -0,0 +1,66 @@ +{{/* + SPDX-License-Identifier: AGPL-3.0-only + SPDX-FileContributor: Runxi Yu +*/}} +{{- define "repo_branches" -}} +{{- $root := . -}} + + + + {{- template "head_common" . -}} + {{ .repo_name }} – {{ template "group_path_plain" .group_path }} – {{ .global.forge_title -}} + + + {{- template "header" . -}} +
+

{{- .repo_name -}}

+ +
+
+
+ {{- .repo_description -}} +
+
+
+ + + + + + + + {{- range .branches -}} + + + + {{- end -}} + +
Branches
+ {{ . }} +
+
+ + +{{- end -}} diff --git a/templates/repo_index.tmpl b/templates/repo_index.tmpl index d07ebf5..4c0595e 100644 --- a/templates/repo_index.tmpl +++ b/templates/repo_index.tmpl @@ -43,47 +43,14 @@ {{- .repo_description -}} -
-
-
-
Repo info
-
Name
-
{{- .repo_name -}}
- {{- if .repo_description -}} -
Description
-
{{- .repo_description -}}
- {{- end -}} -
SSH remote
-
{{- .ssh_clone_url -}}
- {{- if .notes -}} -
Notes
-
-
    - {{- range .notes -}}
  • {{- . -}}
  • {{- end -}} -
-
- {{- end -}} -
-
-
-
- - - - - - - - {{- range .branches -}} - - - - {{- end -}} - -
Branches
- {{ . }} -
+ {{- if .notes -}} +
Notes
+
    + {{- range .notes -}}
  • {{- . -}}
  • {{- end -}} +
+ {{- end -}} +

{{- .ssh_clone_url -}}

{{- if .commits -}}
-- cgit v1.2.3