From 7133932ac6b31530f009ba892e193d54116c7445 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 31 Mar 2025 11:55:15 +0800 Subject: [PATCH] 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 +++++++---------------------------------------- diff --git a/http_handle_branches.go b/http_handle_branches.go new file mode 100644 index 0000000000000000000000000000000000000000..c7f9d39b7c76a52cafafca63396138ab4408ac8a --- /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 24be7e41fe9b75bfdb2a7677aea7d2bbde50a903..a877b1695c3ca5a023501f71a4717f6585b78a54 100644 --- a/http_server.go +++ b/http_server.go @@ -199,6 +199,12 @@ if len(segments) < sepIndex+5 && redirectDir(writer, request) { 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 ba85d3f0eb24e51bdb54fc266dce2f9a9ccacd8d..3c36e191e31aef41668e58597fc52720f7279aa2 100644 --- a/static/style.css +++ b/static/style.css @@ -555,7 +555,7 @@ padding-top: 0.3rem; 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 0000000000000000000000000000000000000000..b06da10f6b626e82f7b7ba83a66a574bc6f4fb1b --- /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 d07ebf5c8668ea732753c266d5829dc23af89e25..4c0595ee4249b0164e89684e690ba1ebe830d692 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 -}} -
-
+ {{- if .notes -}} +
Notes
+
    + {{- range .notes -}}
  • {{- . -}}
  • {{- end -}} +
-
- - - - - - - - {{- range .branches -}} - - - - {{- end -}} - -
Branches
- {{ . }} -
-
+ {{- end -}} +

{{- .ssh_clone_url -}}

{{- if .commits -}}
-- 2.48.1