diff options
Diffstat (limited to '')
-rw-r--r-- | http_handle_repo_tree.go | 53 |
1 files changed, 31 insertions, 22 deletions
diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index a6173ae..190f082 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -10,30 +10,36 @@ import ( "path" "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" "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) { - 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 } @@ -42,10 +48,16 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] 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 + var lexer chroma.Lexer + var iterator chroma.Iterator + var style *chroma.Style + var formatter *chroma_formatters_html.Formatter + var formatted_encapsulated template.HTML + + if file, err = tree.File(path_spec); err != nil { http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } @@ -53,29 +65,26 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] 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 } - lexer := chroma_lexers.Match(path_spec) + lexer = chroma_lexers.Match(path_spec) if lexer == nil { lexer = chroma_lexers.Fallback } - iterator, err := lexer.Tokenise(nil, file_contents) - if err != nil { + if iterator, err = lexer.Tokenise(nil, file_contents); 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)) - err = formatter.Format(&formatted_unencapsulated, style, iterator) - if err != nil { + 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 { http.Error(w, "Error formatting code: "+err.Error(), http.StatusInternalServerError) return } - formatted_encapsulated := template.HTML(formatted_unencapsulated.Bytes()) + formatted_encapsulated = template.HTML(formatted_unencapsulated.Bytes()) params["file_contents"] = formatted_encapsulated render_template(w, "repo_tree_file", params) |