diff options
Diffstat (limited to 'http_handle_repo_tree.go')
-rw-r--r-- | http_handle_repo_tree.go | 72 |
1 files changed, 36 insertions, 36 deletions
diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index 121e76a..a1a3ca4 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -11,94 +11,94 @@ import ( "strings" "github.com/alecthomas/chroma/v2" - 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" + chromaHTML "github.com/alecthomas/chroma/v2/formatters/html" + chromaLexers "github.com/alecthomas/chroma/v2/lexers" + chromaStyles "github.com/alecthomas/chroma/v2/styles" "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_tree(w http.ResponseWriter, r *http.Request, params map[string]any) { - var raw_path_spec, path_spec string +func httpHandleRepoTree(w http.ResponseWriter, r *http.Request, params map[string]any) { + var rawPathSpec, pathSpec string var repo *git.Repository - var ref_hash plumbing.Hash - var commit_object *object.Commit + var refHash plumbing.Hash + var commitObject *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 + rawPathSpec = params["rest"].(string) + repo, pathSpec = params["repo"].(*git.Repository), strings.TrimSuffix(rawPathSpec, "/") + params["path_spec"] = pathSpec - if ref_hash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { + if refHash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } - if commit_object, err = repo.CommitObject(ref_hash); err != nil { + if commitObject, err = repo.CommitObject(refHash); err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } - if tree, err = commit_object.Tree(); err != nil { + if tree, err = commitObject.Tree(); err != nil { http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } var target *object.Tree - if path_spec == "" { + if pathSpec == "" { target = tree } else { - if target, err = tree.Tree(path_spec); err != nil { + if target, err = tree.Tree(pathSpec); err != nil { var file *object.File - var file_contents string + var fileContent string var lexer chroma.Lexer var iterator chroma.Iterator var style *chroma.Style - var formatter *chroma_formatters_html.Formatter - var formatted_encapsulated template.HTML + var formatter *chromaHTML.Formatter + var formattedHTML template.HTML - if file, err = tree.File(path_spec); err != nil { + if file, err = tree.File(pathSpec); err != nil { http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } - if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] == '/' { - http.Redirect(w, r, "../"+path_spec, http.StatusSeeOther) + if len(rawPathSpec) != 0 && rawPathSpec[len(rawPathSpec)-1] == '/' { + http.Redirect(w, r, "../"+pathSpec, http.StatusSeeOther) return } - if file_contents, err = file.Contents(); err != nil { + if fileContent, err = file.Contents(); err != nil { http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError) return } - lexer = chroma_lexers.Match(path_spec) + lexer = chromaLexers.Match(pathSpec) if lexer == nil { - lexer = chroma_lexers.Fallback + lexer = chromaLexers.Fallback } - if iterator, err = lexer.Tokenise(nil, file_contents); err != nil { + if iterator, err = lexer.Tokenise(nil, fileContent); err != nil { http.Error(w, "Error tokenizing code: "+err.Error(), http.StatusInternalServerError) return } - var formatted_unencapsulated bytes.Buffer - style = chroma_styles.Get("autumn") - formatter = chroma_formatters_html.New(chroma_formatters_html.WithClasses(true), chroma_formatters_html.TabWidth(8)) - if err = formatter.Format(&formatted_unencapsulated, style, iterator); err != nil { + var formattedHTMLStr bytes.Buffer + style = chromaStyles.Get("autumn") + formatter = chromaHTML.New(chromaHTML.WithClasses(true), chromaHTML.TabWidth(8)) + if err = formatter.Format(&formattedHTMLStr, style, iterator); err != nil { http.Error(w, "Error formatting code: "+err.Error(), http.StatusInternalServerError) return } - formatted_encapsulated = template.HTML(formatted_unencapsulated.Bytes()) //#nosec G203 - params["file_contents"] = formatted_encapsulated + formattedHTML = template.HTML(formattedHTMLStr.Bytes()) //#nosec G203 + params["file_contents"] = formattedHTML - render_template(w, "repo_tree_file", params) + renderTemplate(w, "repo_tree_file", params) return } } - if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] != '/' { - http.Redirect(w, r, path.Base(path_spec)+"/", http.StatusSeeOther) + if len(rawPathSpec) != 0 && rawPathSpec[len(rawPathSpec)-1] != '/' { + http.Redirect(w, r, path.Base(pathSpec)+"/", http.StatusSeeOther) return } - params["readme_filename"], params["readme"] = render_readme_at_tree(target) + params["readme_filename"], params["readme"] = renderReadmeAtTree(target) params["files"] = makeDisplayTree(target) - render_template(w, "repo_tree_dir", params) + renderTemplate(w, "repo_tree_dir", params) } |