From 134b84f2672a9fe3e2e8a92b712261b47c4bd022 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Wed, 5 Mar 2025 09:32:40 +0800 Subject: repo/*: Use var instead of := --- http_handle_repo_raw.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'http_handle_repo_raw.go') diff --git a/http_handle_repo_raw.go b/http_handle_repo_raw.go index 68b69f6..a0f7430 100644 --- a/http_handle_repo_raw.go +++ b/http_handle_repo_raw.go @@ -10,27 +10,32 @@ import ( "strings" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]any) { - raw_path_spec := params["rest"].(string) - repo, path_spec := params["repo"].(*git.Repository), strings.TrimSuffix(raw_path_spec, "/") + var raw_path_spec, path_spec string + var repo *git.Repository + var ref_hash plumbing.Hash + var commit_object *object.Commit + var tree *object.Tree + var err error + + raw_path_spec = params["rest"].(string) + repo, path_spec = params["repo"].(*git.Repository), strings.TrimSuffix(raw_path_spec, "/") params["path_spec"] = path_spec - ref_hash, err := get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)) - if err != nil { + if ref_hash, err = get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } - commit_object, err := repo.CommitObject(ref_hash) - if err != nil { + if commit_object, err = repo.CommitObject(ref_hash); err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } - tree, err := commit_object.Tree() - if err != nil { + if tree, err = commit_object.Tree(); err != nil { http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } @@ -39,10 +44,10 @@ func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]a if path_spec == "" { target = tree } else { - target, err = tree.Tree(path_spec) - if err != nil { - file, err := tree.File(path_spec) - if err != nil { + if target, err = tree.Tree(path_spec); err != nil { + var file *object.File + var file_contents string + if file, err = tree.File(path_spec); err != nil { http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } @@ -50,12 +55,11 @@ func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]a http.Redirect(w, r, "../"+path_spec, http.StatusSeeOther) return } - file_contents, err := file.Contents() - if err != nil { + if file_contents, err = file.Contents(); err != nil { http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError) return } - fmt.Fprintln(w, file_contents) + fmt.Fprint(w, file_contents) return } } -- cgit v1.2.3