Commit a41a46b

Eric Bower  ·  2023-06-25 14:51:17 -0400 EDT
parent 38c6478
refactor: change directory structure

And other tweaks
3 files changed,  +101, -57
+0, -13
......@@ -1,13 +0,0 @@
1-FROM golang:1.20 as builder
2-COPY . /app
3-WORKDIR /app
4-RUN go build -o pgit ./main.go
5-
6-RUN git clone https://github.com/picosh/pico.git
7-RUN git clone https://github.com/picosh/ops.git
8-
9-RUN ./pgit
10-
11-FROM nginx:latest
12-COPY --from=builder /app/public /usr/share/nginx/html
13-EXPOSE 80
+7, -2
......@@ -1,3 +1,8 @@
1+url: git.erock.io
12 repos:
2- - /app/pico
3- - /app/ops
3+ - path: /home/erock/dev/pico/pico
4+ refs:
5+ - main
6+ - path: /home/erock/dev/pico/pico-ops
7+ refs:
8+ - main
+94, -42
......@@ -4,7 +4,7 @@ import (
44 "fmt"
55 html "html/template"
66 "os"
7- "path"
7+ "path/filepath"
88 "strings"
99
1010 git "github.com/gogs/git-module"
......@@ -83,11 +83,16 @@ func CommitURL(repo string, commitID string) string {
8383 }
8484
8585 func repoName(root string) string {
86- _, file := path.Split(root)
86+ _, file := filepath.Split(root)
8787 return file
8888 }
89-func findDefaultBranch(refs []*git.Reference) *git.Reference {
90- for _, def := range defaultBranches {
89+func findDefaultBranch(config *RepoConfig, refs []*git.Reference) *git.Reference {
90+ branches := config.Refs
91+ if len(branches) == 0 {
92+ branches = defaultBranches
93+ }
94+
95+ for _, def := range branches {
9196 for _, ref := range refs {
9297 if "refs/heads/"+def == ref.Refspec {
9398 return ref
......@@ -113,7 +118,7 @@ func walkTree(tree *git.Tree, branch string, curpath string, aggregate []*TreeIt
113118 aggregate = append(aggregate, &TreeItem{
114119 Path: fname,
115120 Entry: entry,
116- URL: path.Join("/tree", branch, fname),
121+ URL: filepath.Join("/", "tree", branch, "item", fname),
117122 })
118123 }
119124 }
......@@ -133,13 +138,13 @@ func writeHtml(data *WriteData) {
133138 bail(err)
134139
135140 outdir := viper.GetString("outdir")
136- dir := path.Join(outdir, data.RepoName, data.Subdir)
141+ dir := filepath.Join(outdir, data.RepoName, data.Subdir)
137142 fmt.Println(dir)
138143 fmt.Println(data.Name)
139144 err = os.MkdirAll(dir, os.ModePerm)
140145 bail(err)
141146
142- w, err := os.OpenFile(path.Join(dir, data.Name), os.O_WRONLY|os.O_CREATE, 0755)
147+ w, err := os.OpenFile(filepath.Join(dir, data.Name), os.O_WRONLY|os.O_CREATE, 0755)
143148 bail(err)
144149
145150 err = ts.Execute(w, data)
......@@ -158,20 +163,31 @@ func writeIndex(data *IndexPage) {
158163 bail(err)
159164
160165 outdir := viper.GetString("outdir")
161- dir := path.Join(outdir)
166+ dir := filepath.Join(outdir)
162167 fmt.Println(dir)
163168 err = os.MkdirAll(dir, os.ModePerm)
164169 bail(err)
165170
166- w, err := os.OpenFile(path.Join(dir, "index.html"), os.O_WRONLY|os.O_CREATE, 0755)
171+ w, err := os.OpenFile(filepath.Join(dir, "index.html"), os.O_WRONLY|os.O_CREATE, 0755)
167172 bail(err)
168173
169174 err = ts.Execute(w, data)
170175 bail(err)
171176 }
177+
178+func writeRootSummary(data *PageData) {
179+ writeHtml(&WriteData{
180+ Name: "index.html",
181+ Template: "./html/summary.page.tmpl",
182+ Data: data,
183+ RepoName: data.Repo.Name,
184+ Repo: data.Repo,
185+ })
186+}
172187 func writeSummary(data *PageData) {
173188 writeHtml(&WriteData{
174189 Name: "index.html",
190+ Subdir: filepath.Join("tree", data.RevName),
175191 Template: "./html/summary.page.tmpl",
176192 Data: data,
177193 RepoName: data.Repo.Name,
......@@ -180,7 +196,8 @@ func writeSummary(data *PageData) {
180196 }
181197 func writeTree(data *PageData) {
182198 writeHtml(&WriteData{
183- Name: "tree.html",
199+ Name: "index.html",
200+ Subdir: filepath.Join("tree", data.RevName),
184201 Template: "./html/tree.page.tmpl",
185202 Data: data,
186203 RepoName: data.Repo.Name,
......@@ -189,7 +206,8 @@ func writeTree(data *PageData) {
189206 }
190207 func writeLog(data *PageData) {
191208 writeHtml(&WriteData{
192- Name: "log.html",
209+ Name: "index.html",
210+ Subdir: filepath.Join("logs", data.RevName),
193211 Template: "./html/log.page.tmpl",
194212 Data: data,
195213 RepoName: data.Repo.Name,
......@@ -216,13 +234,13 @@ func writeHTMLTreeFiles(data *PageData) {
216234 bail(err)
217235 file.NumLines = len(strings.Split(string(b), "\n"))
218236
219- d := path.Dir(file.Path)
237+ d := filepath.Dir(file.Path)
220238 writeHtml(&WriteData{
221239 Name: fmt.Sprintf("%s.html", file.Entry.Name()),
222240 Template: "./html/file.page.tmpl",
223241 Data: &FileData{Contents: string(b)},
224242 RepoName: data.Repo.Name,
225- Subdir: path.Join("tree", data.RevName, d),
243+ Subdir: filepath.Join("tree", data.RevName, "item", d),
226244 Repo: data.Repo,
227245 })
228246 }
......@@ -285,40 +303,62 @@ func writeLogDiffs(project string, repo *git.Repository, data *PageData, cache m
285303 }
286304 }
287305
288-func writeRepo(root string) {
289- repo, err := git.Open(root)
306+func writeRepo(config *RepoConfig) {
307+ repo, err := git.Open(config.Path)
290308 bail(err)
291309
292- name := repoName(root)
310+ name := repoName(config.Path)
311+
312+ heads, err := repo.ShowRef(git.ShowRefOptions{Heads: true, Tags: false})
313+ bail(err)
314+
315+ rev := findDefaultBranch(config, heads)
316+ if rev == nil {
317+ bail(fmt.Errorf("no default branch found"))
318+ }
319+ _, revName := filepath.Split(rev.Refspec)
320+
293321 repoData := &RepoData{
294322 Name: name,
295323 SummaryURL: fmt.Sprintf("/%s/index.html", name),
296- TreeURL: fmt.Sprintf("/%s/tree.html", name),
297- LogURL: fmt.Sprintf("/%s/log.html", name),
324+ TreeURL: fmt.Sprintf("/%s/tree/%s/index.html", name, revName),
325+ LogURL: fmt.Sprintf("/%s/logs/%s/index.html", name, revName),
298326 RefsURL: fmt.Sprintf("/%s/refs.html", name),
299327 CloneURL: fmt.Sprintf("/%s.git", name),
300328 }
301329
302- heads, err := repo.ShowRef(git.ShowRefOptions{Heads: true, Tags: false})
303- bail(err)
304-
305330 tags, _ := repo.ShowRef(git.ShowRefOptions{Heads: false, Tags: true})
306331
307332 cache := make(map[string]bool)
308- rev := findDefaultBranch(heads)
309- if rev != nil {
310- // for _, rev := range heads {
311- _, revName := path.Split(rev.Refspec)
312- data := &PageData{
313- Branches: heads,
314- Tags: tags,
315- Rev: rev,
316- RevName: revName,
317- Repo: repoData,
318- Readme: "",
319- }
320333
321- writeBranch(repo, data, cache)
334+ data := &PageData{
335+ Branches: heads,
336+ Tags: tags,
337+ Rev: rev,
338+ RevName: revName,
339+ Repo: repoData,
340+ Readme: "",
341+ }
342+ writeRootSummary(data)
343+ writeRefs(data)
344+
345+ for _, revn := range config.Refs {
346+ for _, head := range heads {
347+ _, headName := filepath.Split(head.Refspec)
348+ if revn != headName {
349+ continue
350+ }
351+ data := &PageData{
352+ Branches: heads,
353+ Tags: tags,
354+ Rev: head,
355+ RevName: headName,
356+ Repo: repoData,
357+ Readme: "",
358+ }
359+
360+ writeBranch(repo, data, cache)
361+ }
322362 }
323363 }
324364
......@@ -340,11 +380,12 @@ func writeBranch(repo *git.Repository, pageData *PageData, cache map[string]bool
340380 treeEntries := walkTree(tree, pageData.RevName, "", entries)
341381 for _, entry := range treeEntries {
342382 entry.Path = strings.TrimPrefix(entry.Path, "/")
343- entry.URL = path.Join(
383+ entry.URL = filepath.Join(
344384 "/",
345385 pageData.Repo.Name,
346386 "tree",
347387 pageData.RevName,
388+ "item",
348389 fmt.Sprintf("%s.html", entry.Path),
349390 )
350391 }
......@@ -352,9 +393,7 @@ func writeBranch(repo *git.Repository, pageData *PageData, cache map[string]bool
352393 pageData.Log = logs
353394 pageData.Tree = treeEntries
354395
355- writeSummary(pageData)
356396 writeLog(pageData)
357- writeRefs(pageData)
358397 writeHTMLTreeFiles(pageData)
359398 writeLogDiffs(pageData.Repo.Name, repo, pageData, cache)
360399
......@@ -365,6 +404,15 @@ func writeBranch(repo *git.Repository, pageData *PageData, cache map[string]bool
365404 }
366405 }
367406
407+type RepoConfig struct {
408+ Path string `mapstructure:"path"`
409+ Refs []string `mapstructure:"refs"`
410+}
411+type Config struct {
412+ Repos []*RepoConfig `mapstructure:"repos"`
413+ URL string `mapstructure:"url"`
414+}
415+
368416 func main() {
369417 viper.SetDefault("outdir", "./public")
370418 viper.SetConfigName("config")
......@@ -373,11 +421,15 @@ func main() {
373421 err := viper.ReadInConfig()
374422 bail(err)
375423
376- repos := viper.GetStringSlice("repos")
424+ var config Config
425+ if err := viper.Unmarshal(&config); err != nil {
426+ fmt.Println(err)
427+ return
428+ }
377429 repoList := []*RepoItemData{}
378- for _, r := range repos {
379- name := repoName(r)
380- url := path.Join("/", name, "index.html")
430+ for _, r := range config.Repos {
431+ name := repoName(r.Path)
432+ url := filepath.Join("/", name, "index.html")
381433 repoList = append(repoList, &RepoItemData{
382434 URL: url,
383435 Name: name,
......@@ -386,7 +438,7 @@ func main() {
386438 writeIndex(&IndexPage{
387439 RepoList: repoList,
388440 })
389- for _, r := range repos {
441+ for _, r := range config.Repos {
390442 writeRepo(r)
391443 }
392444 }