package modules import ( "net/http" "strings" "gsg/internal/core" "gsg/internal/gitx" ) type Tree struct { Path string Crumbs []Crumb Entries []gitx.Entry } type Crumb struct{ Name, Path string } func init() { core.Register("tree", treeMod) } // tree: one directory level at Ctx.Path, with breadcrumbs. // Optional arg overrides the path: {{ mod "tree" "docs" }} func treeMod(c *core.Ctx, args ...string) (any, error) { path := c.Path if len(args) > 0 { path = args[0] } // Resolve against the SHA, not the ref name: the ref could move between // the resolve in buildCtx and here, which would make the ETag a lie. rev := c.Commit if rev == "" { rev = c.Ref } entries, err := c.Repo.Tree(rev, path) if err != nil { // A valid commit with a bad path is a 404, not a server error. return nil, core.Errorf(http.StatusNotFound, "no such path %q", path) } t := Tree{Path: path, Entries: entries} if path != "" { var acc []string for _, seg := range strings.Split(path, "/") { acc = append(acc, seg) t.Crumbs = append(t.Crumbs, Crumb{Name: seg, Path: strings.Join(acc, "/")}) } } return t, nil }