diff options
author | Runxi Yu <me@runxiyu.org> | 2025-02-11 23:13:04 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-02-11 23:15:51 +0800 |
commit | b288beea9ddc5709769997a9101f25a78e286b89 (patch) | |
tree | db548c0ca63f180b25acb6f9822ab38bc41c15d0 /url_misc.go | |
parent | style.css: Fix file content background (diff) | |
download | forge-b288beea9ddc5709769997a9101f25a78e286b89.tar.gz forge-b288beea9ddc5709769997a9101f25a78e286b89.tar.zst forge-b288beea9ddc5709769997a9101f25a78e286b89.zip |
*: Use URL params to specify commits/branches/tags
Diffstat (limited to '')
-rw-r--r-- | url_misc.go | 42 |
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 +} |