internal/site/navigation.go2.7 KB · Go
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
package site

import (
	"context"
	"encoding/json"
	"path"
	"sort"
)

type repositoryNavigation struct {
	DefaultBranch string   `json:"defaultBranch"`
	Branches      []string `json:"branches"`
}

type fileNavigation struct {
	Files []fileNavigationEntry `json:"files"`
}

type fileNavigationEntry struct {
	Path string `json:"path"`
	URL  string `json:"url"`
}

func (g *Generator) SetBranches(branches []string, defaultBranch string) {
	seen := map[string]struct{}{}
	g.Branches = g.Branches[:0]
	for _, branch := range branches {
		if branch == "" {
			continue
		}
		if _, exists := seen[branch]; exists {
			continue
		}
		seen[branch] = struct{}{}
		g.Branches = append(g.Branches, branch)
	}
	sort.Strings(g.Branches)
	g.DefaultBranch = defaultBranch
	if g.DefaultBranch == "" && len(g.Branches) > 0 {
		g.DefaultBranch = g.Branches[0]
	}
}

func (g *Generator) ensureBranch(branch string, isHead bool) {
	if isHead || g.DefaultBranch == "" {
		g.DefaultBranch = branch
	}
	for _, existing := range g.Branches {
		if existing == branch {
			return
		}
	}
	g.Branches = append(g.Branches, branch)
	sort.Strings(g.Branches)
}

func (g *Generator) filesPage(ctx context.Context, branch, oid string) (Page, error) {
	entries, err := g.listTreeRecursive(ctx, oid)
	if err != nil {
		return Page{}, err
	}
	g.decorateSubmodules(ctx, oid, "", entries)
	manifest := fileNavigation{}
	for _, entry := range entries {
		if entry.Type == "tree" {
			continue
		}
		entryURL := routeURL(g.BaseURL, path.Join("blob", branchRouteSegment(branch), entry.Name))
		if entry.SubmoduleURL != "" {
			entryURL = entry.SubmoduleURL
		}
		manifest.Files = append(manifest.Files, fileNavigationEntry{Path: entry.Name, URL: entryURL})
	}
	sort.Slice(manifest.Files, func(i, j int) bool {
		return manifest.Files[i].Path < manifest.Files[j].Path
	})
	body, err := json.Marshal(manifest)
	if err != nil {
		return Page{}, err
	}
	return Page{
		Route:       path.Join(".dumbforge/files", branchRouteSegment(branch)+".json"),
		Body:        body,
		ContentType: "application/json; charset=utf-8",
		Cache:       siteCache,
	}, nil
}

func (g *Generator) RepositoryNavigationBuild(deletedBranches []string) (Build, error) {
	manifest := repositoryNavigation{
		DefaultBranch: g.DefaultBranch,
		Branches:      append([]string(nil), g.Branches...),
	}
	body, err := json.Marshal(manifest)
	if err != nil {
		return Build{}, err
	}
	build := Build{Pages: []Page{{
		Route:       ".dumbforge/repository.json",
		Body:        body,
		ContentType: "application/json; charset=utf-8",
		Cache:       siteCache,
	}}}
	for _, branch := range deletedBranches {
		build.Deletes = append(build.Deletes, path.Join(".dumbforge/files", branchRouteSegment(branch)+".json"))
	}
	return build, nil
}