main
1package main
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "regexp"
8 "runtime/pprof"
9 "strings"
10
11 "github.com/antonmedv/gitmal/pkg/git"
12
13 flag "github.com/spf13/pflag"
14)
15
16var (
17 flagOwner string
18 flagName string
19 flagOutput string
20 flagBranches string
21 flagDefaultBranch string
22 flagLightTheme string
23 flagDarkTheme string
24 flagPreviewThemes bool
25 flagMinify bool
26 flagGzip bool
27)
28
29type Params struct {
30 Owner string
31 Name string
32 RepoDir string
33 Ref git.Ref
34 OutputDir string
35 LightStyle string
36 DarkStyle string
37 DefaultRef git.Ref
38 AssetNames map[string]string
39}
40
41func main() {
42 if _, ok := os.LookupEnv("GITMAL_PPROF"); ok {
43 f, err := os.Create("cpu.prof")
44 if err != nil {
45 panic(err)
46 }
47 err = pprof.StartCPUProfile(f)
48 if err != nil {
49 panic(err)
50 }
51 defer f.Close()
52 defer pprof.StopCPUProfile()
53 memProf, err := os.Create("mem.prof")
54 if err != nil {
55 panic(err)
56 }
57 defer memProf.Close()
58 defer pprof.WriteHeapProfile(memProf)
59 }
60
61 _, noFiles := os.LookupEnv("NO_FILES")
62 _, noCommitsList := os.LookupEnv("NO_COMMITS_LIST")
63
64 flag.StringVar(&flagOwner, "owner", "", "Project owner")
65 flag.StringVar(&flagName, "name", "", "Project name")
66 flag.StringVar(&flagOutput, "output", "output", "Output directory for generated HTML files")
67 flag.StringVar(&flagBranches, "branches", "", "Regex for branches to include")
68 flag.StringVar(&flagDefaultBranch, "default-branch", "", "Default branch to use (autodetect master or main)")
69 flag.StringVar(&flagLightTheme, "light-theme", "github", "Style theme, light mode")
70 flag.StringVar(&flagDarkTheme, "dark-theme", "github-dark", "Style theme, dark mode")
71 flag.BoolVar(&flagPreviewThemes, "preview-themes", false, "Preview available themes")
72 flag.BoolVar(&flagMinify, "minify", false, "Minify all generated HTML files")
73 flag.BoolVar(&flagGzip, "gzip", false, "Compress all generated HTML files")
74 flag.Usage = usage
75 flag.Parse()
76
77 input := "."
78 args := flag.Args()
79 if len(args) == 1 {
80 input = args[0]
81 }
82 if len(args) > 1 {
83 panic("Multiple repos not supported yet")
84 }
85
86 if flagPreviewThemes {
87 previewThemes()
88 os.Exit(0)
89 }
90
91 outputDir, err := filepath.Abs(flagOutput)
92 if err != nil {
93 panic(err)
94 }
95
96 absInput, err := filepath.Abs(input)
97 if err != nil {
98 panic(err)
99 }
100 input = absInput
101
102 if flagName == "" {
103 flagName = filepath.Base(input)
104 flagName = strings.TrimSuffix(flagName, ".git")
105 }
106
107 themeColorLight, ok := themeStyles[flagLightTheme]
108 if !ok {
109 panic("Invalid theme: " + flagLightTheme)
110 }
111 if themeColorLight != "light" {
112 panic("Specified light theme (" + flagLightTheme + ") is not a light theme")
113 }
114
115 themeColorDark, ok := themeStyles[flagDarkTheme]
116 if !ok {
117 panic("Invalid theme: " + flagDarkTheme)
118 }
119 if themeColorDark != "dark" {
120 panic("Specified dark theme (" + flagLightTheme + ") is not a dark theme")
121 }
122
123 branchesFilter, err := regexp.Compile(flagBranches)
124 if err != nil {
125 panic(err)
126 }
127
128 branches, err := git.Branches(input, branchesFilter, flagDefaultBranch)
129 if err != nil {
130 panic(err)
131 }
132
133 tags, err := git.Tags(input)
134 if err != nil {
135 panic(err)
136 }
137
138 if flagDefaultBranch == "" {
139 if containsBranch(branches, "master") {
140 flagDefaultBranch = "master"
141 } else if containsBranch(branches, "main") {
142 flagDefaultBranch = "main"
143 } else {
144 echo("No default branch found. Specify one using --default-branch flag.")
145 os.Exit(1)
146 }
147 }
148
149 if !containsBranch(branches, flagDefaultBranch) {
150 echo(fmt.Sprintf("Default branch %q not found.", flagDefaultBranch))
151 echo("Specify a valid branch using --default-branch flag.")
152 os.Exit(1)
153 }
154
155 if yes, a, b := hasConflictingBranchNames(branches); yes {
156 echo(fmt.Sprintf("Conflicting branchs %q and %q, both want to use %q dir name.", a, b, a.DirName()))
157 os.Exit(1)
158 }
159
160 // Start generating pages
161
162 params := Params{
163 Owner: flagOwner,
164 Name: flagName,
165 RepoDir: input,
166 OutputDir: outputDir,
167 LightStyle: flagLightTheme,
168 DarkStyle: flagDarkTheme,
169 DefaultRef: git.NewRef(flagDefaultBranch),
170 AssetNames: nil,
171 }
172
173 // first, get our assets together as we refer to them in the templates
174 if params.AssetNames, err = createAssets(params, flagMinify); err != nil {
175 panic(err)
176 }
177
178 commits := make(map[string]git.Commit)
179 commitsFor := make(map[git.Ref][]git.Commit, len(branches))
180
181 for _, branch := range branches {
182 commitsFor[branch], err = git.Commits(branch, params.RepoDir)
183 if err != nil {
184 panic(err)
185 }
186
187 for _, commit := range commitsFor[branch] {
188 if alreadyExisting, ok := commits[commit.Hash]; ok && alreadyExisting.Branch == params.DefaultRef {
189 continue
190 }
191 commit.Branch = branch
192 commits[commit.Hash] = commit
193 }
194 }
195
196 // Add commits from tags
197 for _, tag := range tags {
198 commitsForTag, err := git.Commits(git.NewRef(tag.Name), params.RepoDir)
199 if err != nil {
200 panic(err)
201 }
202 for _, commit := range commitsForTag {
203 // Only add new commits
204 if alreadyExisting, ok := commits[commit.Hash]; ok && !alreadyExisting.Branch.IsEmpty() {
205 continue
206 }
207 commits[commit.Hash] = commit
208 }
209 }
210
211 echo(fmt.Sprintf("> %s: %d branches, %d tags, %d commits", params.Name, len(branches), len(tags), len(commits)))
212
213 if err := generateBranches(branches, flagDefaultBranch, params); err != nil {
214 panic(err)
215 }
216
217 var defaultBranchFiles []git.Blob
218
219 for i, branch := range branches {
220 echo(fmt.Sprintf("> [%d/%d] %s@%s", i+1, len(branches), params.Name, branch))
221 params.Ref = branch
222
223 if !noFiles {
224 files, err := git.Files(params.Ref, params.RepoDir)
225 if err != nil {
226 panic(err)
227 }
228
229 if branch.String() == flagDefaultBranch {
230 defaultBranchFiles = files
231 }
232
233 err = generateBlobs(files, params)
234 if err != nil {
235 panic(err)
236 }
237
238 err = generateLists(files, params)
239 if err != nil {
240 panic(err)
241 }
242 }
243
244 if !noCommitsList {
245 err = generateLogForBranch(commitsFor[branch], params)
246 if err != nil {
247 panic(err)
248 }
249 }
250 }
251
252 // Back to the default branch
253 params.Ref = git.NewRef(flagDefaultBranch)
254
255 // Commits pages generation
256 echo("> generating commit pages...")
257 err = generateCommits(commits, params)
258 if err != nil {
259 panic(err)
260 }
261
262 // Tags page generation
263 if err := generateTags(tags, params); err != nil {
264 panic(err)
265 }
266
267 // Index page generation
268 if !noFiles {
269 if len(defaultBranchFiles) == 0 {
270 panic("No files found for default branch")
271 }
272 err = generateIndex(defaultBranchFiles, params)
273 if err != nil {
274 panic(err)
275 }
276 }
277
278 if flagMinify || flagGzip {
279 echo("> post-processing...")
280 if err := postProcess(params.OutputDir, flagMinify, flagGzip); err != nil {
281 panic(err)
282 }
283 }
284}
285
286func usage() {
287 fmt.Fprintf(os.Stderr, "Usage: gitmal [options] [path ...]\n")
288 flag.PrintDefaults()
289}