aboutsummaryrefslogtreecommitdiff
path: root/handle_repo_tree.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-02-11 23:13:04 +0800
committerRunxi Yu <me@runxiyu.org>2025-02-11 23:15:51 +0800
commitb288beea9ddc5709769997a9101f25a78e286b89 (patch)
treedb548c0ca63f180b25acb6f9822ab38bc41c15d0 /handle_repo_tree.go
parentstyle.css: Fix file content background (diff)
downloadforge-b288beea9ddc5709769997a9101f25a78e286b89.tar.gz
forge-b288beea9ddc5709769997a9101f25a78e286b89.tar.zst
forge-b288beea9ddc5709769997a9101f25a78e286b89.zip
*: Use URL params to specify commits/branches/tags
Diffstat (limited to 'handle_repo_tree.go')
-rw-r--r--handle_repo_tree.go20
1 files changed, 14 insertions, 6 deletions
diff --git a/handle_repo_tree.go b/handle_repo_tree.go
index 2c1c31e..824f5df 100644
--- a/handle_repo_tree.go
+++ b/handle_repo_tree.go
@@ -2,6 +2,7 @@ package main
import (
"bytes"
+ "errors"
"html/template"
"net/http"
"path"
@@ -10,7 +11,6 @@ import (
chroma_formatters_html "github.com/alecthomas/chroma/v2/formatters/html"
chroma_lexers "github.com/alecthomas/chroma/v2/lexers"
chroma_styles "github.com/alecthomas/chroma/v2/styles"
- "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
@@ -18,19 +18,27 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request) {
data := make(map[string]any)
// TODO: Sanitize path values
raw_path_spec := r.PathValue("rest")
- ref_name, group_name, repo_name, path_spec := r.PathValue("ref"), r.PathValue("group_name"), r.PathValue("repo_name"), strings.TrimSuffix(raw_path_spec, "/")
- data["ref"], data["group_name"], data["repo_name"], data["path_spec"] = ref_name, group_name, repo_name, path_spec
+ group_name, repo_name, path_spec := r.PathValue("group_name"), r.PathValue("repo_name"), strings.TrimSuffix(raw_path_spec, "/")
+ ref_type, ref_name, err := get_param_ref_and_type(r)
+ if err != nil {
+ if errors.Is(err, err_no_ref_spec) {
+ ref_type = "head"
+ } else {
+ _, _ = w.Write([]byte("Error querying ref type: " + err.Error()))
+ return
+ }
+ }
+ data["ref_type"], data["ref"], data["group_name"], data["repo_name"], data["path_spec"] = ref_type, ref_name, group_name, repo_name, path_spec
repo, err := open_git_repo(group_name, repo_name)
if err != nil {
_, _ = w.Write([]byte("Error opening repo: " + err.Error()))
return
}
- ref, err := repo.Reference(plumbing.NewBranchReferenceName(ref_name), true)
+ ref_hash, err := get_ref_hash_from_type_and_name(repo, ref_type, ref_name)
if err != nil {
- _, _ = w.Write([]byte("Error getting repo reference: " + err.Error()))
+ _, _ = w.Write([]byte("Error getting ref hash: " + err.Error()))
return
}
- ref_hash := ref.Hash()
commit_object, err := repo.CommitObject(ref_hash)
if err != nil {
_, _ = w.Write([]byte("Error getting commit object: " + err.Error()))