diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-31 11:55:15 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-31 11:55:15 +0800 |
commit | 7133932ac6b31530f009ba892e193d54116c7445 (patch) | |
tree | 841db35b37b39dd9ec476246b8107d6c5b1dbf94 | |
parent | Remove ? in builds.sr.ht URL (diff) | |
download | forge-7133932ac6b31530f009ba892e193d54116c7445.tar.gz forge-7133932ac6b31530f009ba892e193d54116c7445.tar.zst forge-7133932ac6b31530f009ba892e193d54116c7445.zip |
Add branches page
-rw-r--r-- | http_handle_branches.go | 44 | ||||
-rw-r--r-- | http_server.go | 6 | ||||
-rw-r--r-- | static/style.css | 2 | ||||
-rw-r--r-- | templates/repo_branches.tmpl | 66 | ||||
-rw-r--r-- | templates/repo_index.tmpl | 47 |
5 files changed, 124 insertions, 41 deletions
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 <https://runxiyu.org> + +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 <https://runxiyu.org> +*/}} +{{- define "repo_branches" -}} +{{- $root := . -}} +<!DOCTYPE html> +<html lang="en"> + <head> + {{- template "head_common" . -}} + <title>{{ .repo_name }} – {{ template "group_path_plain" .group_path }} – {{ .global.forge_title -}}</title> + </head> + <body class="repo-branches"> + {{- template "header" . -}} + <div class="repo-header"> + <h2>{{- .repo_name -}}</h2> + <ul class="nav-tabs-standalone"> + <li class="nav-item"> + <a class="nav-link" href="../{{- template "ref_query" $root -}}">Summary</a> + </li> + <li class="nav-item"> + <a class="nav-link" href="../tree/{{- template "ref_query" $root -}}">Tree</a> + </li> + <li class="nav-item"> + <a class="nav-link" href="../log/{{- template "ref_query" $root -}}">Log</a> + </li> + <li class="nav-item"> + <a class="nav-link active" href="../branches/">Branches</a> + </li> + <li class="nav-item"> + <a class="nav-link" href="../tags/">Tags</a> + </li> + <li class="nav-item"> + <a class="nav-link" href="../contrib/">Merge requests</a> + </li> + <li class="nav-item"> + <a class="nav-link" href="../settings/">Settings</a> + </li> + </ul> + </div> + <div class="repo-header-extension"> + <div class="repo-header-extension-content"> + {{- .repo_description -}} + </div> + </div> + <div class="padding-wrapper"> + <table id="branches"> + <thead> + <tr class="title-row"> + <th colspan="1">Branches</th> + </tr> + </thead> + <tbody> + {{- range .branches -}} + <tr> + <td> + <a href="./?branch={{ . }}">{{ . }}</a> + </td> + </tr> + {{- end -}} + </tbody> + </table> + </div> + </body> +</html> +{{- 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 -}} </div> </div> - <div class="padding-wrapper"> - <div class="key-val-grid-wrapper"> - <section id="repo-info" class="key-val-grid"> - <div class="title-row">Repo info</div> - <div class="row-label">Name</div> - <div class="row-value">{{- .repo_name -}}</div> - {{- if .repo_description -}} - <div class="row-label">Description</div> - <div class="row-value">{{- .repo_description -}}</div> - {{- end -}} - <div class="row-label">SSH remote</div> - <div class="row-value"><code>{{- .ssh_clone_url -}}</code></div> - {{- if .notes -}} - <div class="row-label">Notes</div> - <div class="row-value"> - <ul> - {{- range .notes -}}<li>{{- . -}}</li>{{- end -}} - </ul> - </div> - {{- end -}} - </section> - </div> - </div> - <div class="padding-wrapper"> - <table id="branches"> - <thead> - <tr class="title-row"> - <th colspan="1">Branches</th> - </tr> - </thead> - <tbody> - {{- range .branches -}} - <tr> - <td> - <a href="./?branch={{ . }}">{{ . }}</a> - </td> - </tr> - {{- end -}} - </tbody> - </table> + {{- if .notes -}} + <div id="notes">Notes</div> + <ul> + {{- range .notes -}}<li>{{- . -}}</li>{{- end -}} + </ul> </div> + {{- end -}} + <p class="readingwidth"><code>{{- .ssh_clone_url -}}</code></p> {{- if .commits -}} <div class="padding-wrapper scroll"> <table id="recent-commits" class="wide"> |