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 /http_handle_branches.go | |
parent | Remove ? in builds.sr.ht URL (diff) | |
download | forge-7133932ac6b31530f009ba892e193d54116c7445.tar.gz forge-7133932ac6b31530f009ba892e193d54116c7445.tar.zst forge-7133932ac6b31530f009ba892e193d54116c7445.zip |
Add branches page
Diffstat (limited to 'http_handle_branches.go')
-rw-r--r-- | http_handle_branches.go | 44 |
1 files changed, 44 insertions, 0 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) +} |