aboutsummaryrefslogtreecommitdiff
path: root/url_misc.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 /url_misc.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 'url_misc.go')
-rw-r--r--url_misc.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/url_misc.go b/url_misc.go
new file mode 100644
index 0000000..906e06e
--- /dev/null
+++ b/url_misc.go
@@ -0,0 +1,42 @@
+package main
+
+import (
+ "errors"
+ "net/http"
+ "net/url"
+)
+
+var (
+ err_duplicate_ref_spec = errors.New("Duplicate ref spec")
+ err_no_ref_spec = errors.New("No ref spec")
+)
+
+func get_param_ref_and_type(r *http.Request) (ref_type, ref string, err error) {
+ qr := r.URL.RawQuery
+ q, err := url.ParseQuery(qr)
+ if err != nil {
+ return
+ }
+ done := false
+ for _, _ref_type := range []string{"commit", "branch", "tag"} {
+ _ref, ok := q[_ref_type]
+ if ok {
+ if done {
+ err = err_duplicate_ref_spec
+ return
+ } else {
+ done = true
+ if len(_ref) != 1 {
+ err = err_duplicate_ref_spec
+ return
+ }
+ ref = _ref[0]
+ ref_type = _ref_type
+ }
+ }
+ }
+ if !done {
+ err = err_no_ref_spec
+ }
+ return
+}