master
1package main
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "sort"
8
9 "github.com/antonmedv/gitmal/pkg/git"
10 "github.com/antonmedv/gitmal/pkg/templates"
11)
12
13// generateBranches creates a branches.html page at the root of the output
14// that lists all branches and links to their root directory listings.
15func generateBranches(branches []git.Ref, defaultBranch string, params Params) error {
16 outDir := params.OutputDir
17 if err := os.MkdirAll(outDir, 0o755); err != nil {
18 return err
19 }
20
21 entries := make([]templates.BranchEntry, 0, len(branches))
22 for _, b := range branches {
23 entries = append(entries, templates.BranchEntry{
24 Name: b.String(),
25 Href: filepath.ToSlash(filepath.Join("blob", b.DirName()) + "/index.html"),
26 IsDefault: b.String() == defaultBranch,
27 CommitsHref: filepath.ToSlash(filepath.Join("commits", b.DirName(), "index.html")),
28 })
29 }
30
31 // Ensure default branch is shown at the top of the list.
32 // Keep remaining branches sorted alphabetically for determinism.
33 sort.SliceStable(entries, func(i, j int) bool {
34 if entries[i].IsDefault != entries[j].IsDefault {
35 return entries[i].IsDefault && !entries[j].IsDefault
36 }
37 return entries[i].Name < entries[j].Name
38 })
39
40 f, err := os.Create(filepath.Join(outDir, "branches.html"))
41 if err != nil {
42 return err
43 }
44 defer f.Close()
45
46 // RootHref from root page is just ./
47 rootHref := "./"
48
49 err = templates.BranchesTemplate.ExecuteTemplate(f, "layout.gohtml", templates.BranchesParams{
50 LayoutParams: templates.LayoutParams{
51 Title: fmt.Sprintf("Branches %s %s", dot, params.Name),
52 Name: params.Name,
53 SiteName: params.SiteName,
54 Dark: params.Dark,
55 RootHref: rootHref + params.RootPrefix,
56 RepoHref: rootHref,
57 CurrentRefDir: params.DefaultRef.DirName(),
58 Selected: "branches",
59 InlineStyles: params.InlineStyles,
60 },
61 Branches: entries,
62 })
63 if err != nil {
64 return err
65 }
66
67 return nil
68}