package generator import ( "fmt" "os" "path/filepath" "sort" "mokhan.ca/xlgmokha/gitmal/internal/git" "mokhan.ca/xlgmokha/gitmal/internal/templates" ) func GenerateBranches(branches []git.Ref, defaultBranch string, params Params) error { outDir := params.OutputDir if err := os.MkdirAll(outDir, 0o755); err != nil { return err } entries := make([]templates.BranchEntry, 0, len(branches)) for _, b := range branches { entries = append(entries, templates.BranchEntry{ Name: b.String(), Href: filepath.ToSlash(filepath.Join("blob", b.DirName()) + "/index.html"), IsDefault: b.String() == defaultBranch, CommitsHref: filepath.ToSlash(filepath.Join("commits", b.DirName(), "index.html")), }) } // Ensure default branch is shown at the top of the list. // Keep remaining branches sorted alphabetically for determinism. sort.SliceStable(entries, func(i, j int) bool { if entries[i].IsDefault != entries[j].IsDefault { return entries[i].IsDefault && !entries[j].IsDefault } return entries[i].Name < entries[j].Name }) f, err := os.Create(filepath.Join(outDir, "branches.html")) if err != nil { return err } defer f.Close() // RootHref from root page is just ./ rootHref := "./" err = templates.BranchesTemplate.ExecuteTemplate(f, "layout.gohtml", templates.BranchesParams{ LayoutParams: templates.LayoutParams{ Title: fmt.Sprintf("Branches %s %s", Dot, params.Name), Name: params.Name, RootHref: rootHref, CurrentRefDir: params.DefaultRef.DirName(), Selected: "branches", }, Branches: entries, }) if err != nil { return err } return nil }