From 538923ffe5a5697bcbf2f3a349d54284702c94a9 Mon Sep 17 00:00:00 2001 From: Skyler Grey Date: Sat, 20 Sep 2025 12:19:35 +0000 Subject: [PATCH] knotserver: return README files from tree pages Change-Id: nytovrnpksxvsrolqtsomruymromromk We have README files displayed on the main page of a repository, but we don't do that on subfolders. The first step in making those available is returning README files when they exist Signed-off-by: Skyler Grey --- knotserver/handler.go | 23 ++++++++++++++++++----- types/repo.go | 12 +++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/knotserver/handler.go b/knotserver/handler.go index 9af9c7c..100c363 100644 --- a/knotserver/handler.go +++ b/knotserver/handler.go @@ -239,12 +239,25 @@ func (h *Handle) RepoTree(w http.ResponseWriter, r *http.Request) { return } + var readmeContent string + var readmeFile string + for _, readme := range h.c.Repo.Readme { + maybeReadmeFile, _ := securejoin.SecureJoin(treePath, readme) + content, _ := gr.FileContent(maybeReadmeFile) + if len(content) > 0 { + readmeContent = string(content) + readmeFile = maybeReadmeFile + } + } + resp := types.RepoTreeResponse{ - Ref: ref, - Parent: treePath, - Description: getDescription(path), - DotDot: filepath.Dir(treePath), - Files: files, + Ref: ref, + Parent: treePath, + Description: getDescription(path), + DotDot: filepath.Dir(treePath), + Files: files, + Readme: readmeContent, + ReadmeFileName: readmeFile, } writeJSON(w, resp) diff --git a/types/repo.go b/types/repo.go index e0d3d88..498049b 100644 --- a/types/repo.go +++ b/types/repo.go @@ -41,11 +41,13 @@ type RepoFormatPatchResponse struct { } type RepoTreeResponse struct { - Ref string `json:"ref,omitempty"` - Parent string `json:"parent,omitempty"` - Description string `json:"description,omitempty"` - DotDot string `json:"dotdot,omitempty"` - Files []NiceTree `json:"files,omitempty"` + Ref string `json:"ref,omitempty"` + Parent string `json:"parent,omitempty"` + Description string `json:"description,omitempty"` + DotDot string `json:"dotdot,omitempty"` + Files []NiceTree `json:"files,omitempty"` + Readme string `json:"readme,omitempty"` + ReadmeFileName string `json:"readme_file_name,omitempty"` } type TagReference struct { -- 2.43.0