From b288beea9ddc5709769997a9101f25a78e286b89 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Tue, 11 Feb 2025 23:13:04 +0800 Subject: [PATCH] *: Use URL params to specify commits/branches/tags --- handle_repo_raw.go | 25 +++++++++++++++++++------ handle_repo_tree.go | 20 ++++++++++++++------ main.go | 4 ++-- ref.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ templates/repo_commit.html.tmpl | 4 ++-- templates/repo_index.html.tmpl | 2 +- templates/repo_tree_file.html.tmpl | 2 +- url_misc.go | 42 ++++++++++++++++++++++++++++++++++++++++++ diff --git a/handle_repo_raw.go b/handle_repo_raw.go index 5693660a67346c02b5143c432bfb4852ba49c559..d3ffe3e4aaf2cabc2dddb29fd9a7a782a8dea1fb 100644 --- a/handle_repo_raw.go +++ b/handle_repo_raw.go @@ -1,11 +1,11 @@ package main import ( + "errors" "net/http" "path" "strings" - "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) @@ -13,19 +13,32 @@ func handle_repo_raw(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())) diff --git a/handle_repo_tree.go b/handle_repo_tree.go index 2c1c31e6827fb1530a36d8e8634a9058dbe73f2e..824f5dfa129dc6eb74b9c4d6cea973bd0dcef36f 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 @@ 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())) diff --git a/main.go b/main.go index b7a9b76a50a2c3b762c7b6f7925e79e92954cccc..8c06a77fa121bfd3ad0567173c015a48ac193537 100644 --- a/main.go +++ b/main.go @@ -36,8 +36,8 @@ http.HandleFunc("/{$}", handle_index) http.HandleFunc("/g/{group_name}/repos/{$}", handle_group_repos) http.HandleFunc("/g/{group_name}/repos/{repo_name}/{$}", handle_repo_index) - http.HandleFunc("/g/{group_name}/repos/{repo_name}/tree/{ref}/{rest...}", handle_repo_tree) - http.HandleFunc("/g/{group_name}/repos/{repo_name}/raw/{ref}/{rest...}", handle_repo_raw) + http.HandleFunc("/g/{group_name}/repos/{repo_name}/tree/{rest...}", handle_repo_tree) + http.HandleFunc("/g/{group_name}/repos/{repo_name}/raw/{rest...}", handle_repo_raw) http.HandleFunc("/g/{group_name}/repos/{repo_name}/log/{ref}/", handle_repo_log) http.HandleFunc("/g/{group_name}/repos/{repo_name}/commit/{commit_id}", handle_repo_commit) diff --git a/ref.go b/ref.go new file mode 100644 index 0000000000000000000000000000000000000000..19856e74d73000f4df400c726a5b08eaa5aa229c --- /dev/null +++ b/ref.go @@ -0,0 +1,46 @@ +package main + +import ( + "errors" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" + "go.lindenii.runxiyu.org/lindenii-common/misc" +) + +var ( + err_getting_tag_reference = errors.New("Error getting tag reference") + err_getting_branch_reference = errors.New("Error getting branch reference") + err_getting_head = errors.New("Error getting HEAD") +) + +func get_ref_hash_from_type_and_name(repo *git.Repository, ref_type, ref_name string) (ref_hash plumbing.Hash, ret_err error) { + switch ref_type { + case "head": + head, err := repo.Head() + if err != nil { + ret_err = misc.Wrap_one_error(err_getting_head, err) + return + } + ref_hash = head.Hash() + case "commit": + ref_hash = plumbing.NewHash(ref_name) + case "branch": + ref, err := repo.Reference(plumbing.NewBranchReferenceName(ref_name), true) + if err != nil { + ret_err = misc.Wrap_one_error(err_getting_branch_reference, err) + return + } + ref_hash = ref.Hash() + case "tag": + ref, err := repo.Reference(plumbing.NewTagReferenceName(ref_name), true) + if err != nil { + ret_err = misc.Wrap_one_error(err_getting_tag_reference, err) + return + } + ref_hash = ref.Hash() + default: + panic("Invalid ref type " + ref_type) + } + return +} diff --git a/templates/repo_commit.html.tmpl b/templates/repo_commit.html.tmpl index a288949234656c1e322ae3efc61836eb9c7a6dec..d56888773de32b648b038e342c9d5ec29b731183 100644 --- a/templates/repo_commit.html.tmpl +++ b/templates/repo_commit.html.tmpl @@ -53,9 +53,9 @@
diff --git a/templates/repo_index.html.tmpl b/templates/repo_index.html.tmpl index c3b47595077a7e5a33ad4c36c0f2bdcf29b8b5ab..2f41aacf6d7442e80e83136bcb3bc1efff360ad3 100644 --- a/templates/repo_index.html.tmpl +++ b/templates/repo_index.html.tmpl @@ -42,7 +42,7 @@ {{- $ref := .ref }} {{- range .files }} {{ .Mode }} - {{ .Name }}{{ if not .Is_file }}/{{ end }} + {{ .Name }}{{ if not .Is_file }}/{{ end }} {{ .Size }} {{- end }} diff --git a/templates/repo_tree_file.html.tmpl b/templates/repo_tree_file.html.tmpl index 7ddfce1d512226cd4608cfcfe4cdcac20688bc9f..0401bb07eaf85aa9251347329e3e62d8647585a4 100644 --- a/templates/repo_tree_file.html.tmpl +++ b/templates/repo_tree_file.html.tmpl @@ -8,7 +8,7 @@ {{ .group_name }}/repos/{{ .repo_name }}/{{ .path_spec }} – Lindenii Forge

- /{{ .path_spec }} (raw) + /{{ .path_spec }} (raw)

{{ .file_contents }}