Migration This is my new public git source viewer. Source coming soon. Please contact with concerns/bugs. Contact me

internal/modules/tree.go

38fa4e6cd9bb9dbf0ae23ef02e50b1c75cd84ebb · 1.1 KB · 48 lines raw

1 package modules
2
3 import (
4 "net/http"
5 "strings"
6
7 "gsg/internal/core"
8 "gsg/internal/gitx"
9 )
10
11 type Tree struct {
12 Path string
13 Crumbs []Crumb
14 Entries []gitx.Entry
15 }
16
17 type Crumb struct{ Name, Path string }
18
19 func init() { core.Register("tree", treeMod) }
20
21 // tree: one directory level at Ctx.Path, with breadcrumbs.
22 // Optional arg overrides the path: {{ mod "tree" "docs" }}
23 func treeMod(c *core.Ctx, args ...string) (any, error) {
24 path := c.Path
25 if len(args) > 0 {
26 path = args[0]
27 }
28 // Resolve against the SHA, not the ref name: the ref could move between
29 // the resolve in buildCtx and here, which would make the ETag a lie.
30 rev := c.Commit
31 if rev == "" {
32 rev = c.Ref
33 }
34 entries, err := c.Repo.Tree(rev, path)
35 if err != nil {
36 // A valid commit with a bad path is a 404, not a server error.
37 return nil, core.Errorf(http.StatusNotFound, "no such path %q", path)
38 }
39 t := Tree{Path: path, Entries: entries}
40 if path != "" {
41 var acc []string
42 for _, seg := range strings.Split(path, "/") {
43 acc = append(acc, seg)
44 t.Crumbs = append(t.Crumbs, Crumb{Name: seg, Path: strings.Join(acc, "/")})
45 }
46 }
47 return t, nil
48 }