gtx


Branch: develop

Author
Spike Lindsey <spike01@gmail.com>
Date
Jan. 31 '23 00:36:48
Commit
696f49eb28916021071002241dfc33620bcc96c1
Parent
8517fa9c696065753ebebd10e660f45de3e2f89a
Changes
diff --git a/main.go b/main.go
index 08fcfb7..81f5faa 100644
--- a/main.go
+++ b/main.go
@@ -198,7 +198,12 @@ func main() {
 		"diffbodyparser":     diffbodyparser,
 	}).Parse(tpl))
 
-	// Update each branch.
+	updateBranches(branches, pro)
+	writePages(branches, pro, t)
+	writeMainIndex(pro, opt, t, branches)
+}
+
+func updateBranches(branches []branch, pro *project) {
 	for _, b := range branches {
 		// NOTE: Is this needed still if the repo is downloaded each time the script is run?
 		ref := fmt.Sprintf("refs/heads/%s:refs/origin/%s", b, b)
@@ -210,158 +215,183 @@ func main() {
 
 		if _, err := cmd.Output(); err != nil {
 			log.Printf("unable to fetch branch: %v", err)
-
 			continue
 		}
 	}
+}
+
 
+func writePages(branches []branch, pro *project, t *template.Template) {
 	for _, b := range branches {
 		log.Printf("processing branch: %s", b)
 
-		go func() {
-			dst := filepath.Join(pro.base, "branch", b.Name, "index.html")
+		go writeBranchPage(pro, b, t)
+
+		for i, c := range b.Commits {
+			log.Printf("processing commit: %s: %d/%d", c.Abbr, i+1, len(b.Commits))
+
+			base := filepath.Join(pro.base, "commit", c.Hash)
 
-			if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
+			if err := os.MkdirAll(base, 0755); err != nil {
 				if err != nil {
-					log.Fatalf("unable to create branch directory: %v", err)
+					log.Printf("unable to create commit directory: %v", err)
 				}
 
-				return
+				continue
 			}
 
-			f, err := os.Create(dst)
-
-			defer f.Close()
+			for _, par := range c.Parents {
+				writeCommitDiff(par, c, pro, base, b, t)
+			}
 
-			if err != nil {
-				// TODO: Remove from branches slice?
-				log.Printf("unable to create branch page: %v", err)
+			for _, obj := range c.Tree {
+				dst := filepath.Join(pro.base, "object", obj.Dir())
 
-				return
-			}
+				if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
+					if err != nil {
+						log.Printf("unable to create object directory: %v", err)
+					}
+					continue
+				}
 
-			p := page{
-				Data: Data{
-					"Commits": b.Commits,
-					"Branch":  b,
-					"Project": pro.Name,
-				},
-				Base:  "../../",
-				Title: strings.Join([]string{pro.Name, b.Name}, ": "),
+				writeObjectBlob(obj, pro, dst)
+				writeNom(fmt.Sprintf("%s.html", dst), obj, pro, b, c, t, base)
 			}
 
-			if err := t.Execute(f, p); err != nil {
-				log.Printf("unable to apply template: %v", err)
+			writeCommitPage(base, pro, c, b, t)
+		}
+	}
+}
 
-				return
-			}
-		}()
+func writeMainIndex(pro *project, opt *options, t *template.Template, branches []branch) {
+	// This is the main index or project home.
+	f, err := os.Create(filepath.Join(pro.base, "index.html"))
 
-		for i, c := range b.Commits {
-			log.Printf("processing commit: %s: %d/%d", c.Abbr, i+1, len(b.Commits))
+	defer f.Close()
 
-			base := filepath.Join(pro.base, "commit", c.Hash)
+	if err != nil {
+		log.Fatalf("unable to create home page: %v", err)
+	}
 
-			if err := os.MkdirAll(base, 0755); err != nil {
-				if err != nil {
-					log.Printf("unable to create commit directory: %v", err)
-				}
+	p := page{
+		Data: Data{
+			"Branches": branches,
+			"Link":     opt.URL,
+			"Project":  pro.Name,
+		},
+		Base:  "./",
+		Title: pro.Name,
+	}
 
-				continue
-			}
+	if err := t.Execute(f, p); err != nil {
+		log.Fatalf("unable to apply template: %v", err)
+	}
+}
 
-			for _, par := range c.Parents {
-				func() {
-					cmd := exec.Command("git", "diff", "-p", fmt.Sprintf("%s..%s", par, c.Hash))
-					cmd.Dir = pro.repo
+func writeCommitDiff(par string, c commit, pro *project, base string, b branch, t *template.Template) {
+	cmd := exec.Command("git", "diff", "-p", fmt.Sprintf("%s..%s", par, c.Hash))
+	cmd.Dir = pro.repo
 
-					out, err := cmd.Output()
+	out, err := cmd.Output()
 
-					if err != nil {
-						log.Printf("unable to diff against parent: %v", err)
+	if err != nil {
+		log.Printf("unable to diff against parent: %v", err)
 
-						return
-					}
+		return
+	}
 
-					dst := filepath.Join(base, fmt.Sprintf("diff-%s.html", par))
-					f, err := os.Create(dst)
+	dst := filepath.Join(base, fmt.Sprintf("diff-%s.html", par))
+	f, err := os.Create(dst)
 
-					defer f.Close()
+	defer f.Close()
 
-					if err != nil {
-						log.Printf("unable to create commit diff to parent: %v", err)
+	if err != nil {
+		log.Printf("unable to create commit diff to parent: %v", err)
 
-						return
-					}
+		return
+	}
 
-					p := page{
-						Data: Data{
-							"Diff": diff{
-								Body:   fmt.Sprintf("%s", out),
-								Commit: c,
-								Parent: par,
-							},
-							"Project": pro.Name,
-						},
-						Base:  "../../",
-						Title: strings.Join([]string{pro.Name, b.Name, c.Abbr}, ": "),
-					}
+	p := page{
+		Data: Data{
+			"Diff": diff{
+				Body:   fmt.Sprintf("%s", out),
+				Commit: c,
+				Parent: par,
+			},
+			"Project": pro.Name,
+		},
+		Base:  "../../",
+		Title: strings.Join([]string{pro.Name, b.Name, c.Abbr}, ": "),
+	}
 
-					if err := t.Execute(f, p); err != nil {
-						log.Printf("unable to apply template: %v", err)
+	if err := t.Execute(f, p); err != nil {
+		log.Printf("unable to apply template: %v", err)
 
-						return
-					}
-				}()
-			}
+		return
+	}
+}
 
-			for _, obj := range c.Tree {
-				dst := filepath.Join(pro.base, "object", obj.Dir())
+func writeBranchPage(pro *project, b branch, t *template.Template) {
+	dst := filepath.Join(pro.base, "branch", b.Name, "index.html")
 
-				if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
-					if err != nil {
-						log.Printf("unable to create object directory: %v", err)
-					}
+	if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
+		if err != nil {
+			log.Fatalf("unable to create branch directory: %v", err)
+		}
+		return
+	}
 
-					continue
-				}
+	f, err := os.Create(dst)
 
-				func() {
-					cmd := exec.Command("git", "cat-file", "blob", obj.Hash)
-					cmd.Dir = pro.repo
+	defer f.Close()
 
-					out, err := cmd.Output()
+	if err != nil {
+		// TODO: Remove from branches slice?
+		log.Printf("unable to create branch page: %v", err)
 
-					if err != nil {
-						log.Printf("unable to save object: %v", err)
+		return
+	}
 
-						return
-					}
+	p := page{
+		Data: Data{
+			"Commits": b.Commits,
+			"Branch":  b,
+			"Project": pro.Name,
+		},
+		Base:  "../../",
+		Title: strings.Join([]string{pro.Name, b.Name}, ": "),
+	}
 
-					f, err := os.Create(dst)
+	if err := t.Execute(f, p); err != nil {
+		log.Printf("unable to apply template: %v", err)
+		return
+	}
+}
 
-					defer f.Close()
+func writeObjectBlob(obj object, pro *project, dst string) {
+	cmd := exec.Command("git", "cat-file", "blob", obj.Hash)
+	cmd.Dir = pro.repo
 
-					if err != nil {
-						log.Printf("unable to create object: %v", err)
+	out, err := cmd.Output()
 
-						return
-					}
+	if err != nil {
+		log.Printf("unable to save object: %v", err)
+		return
+	}
 
-					if _, err := f.Write(out); err != nil {
-						log.Printf("unable to write object blob: %v", err)
+	f, err := os.Create(dst)
 
-						return
-					}
-				}()
-				writeNom(fmt.Sprintf("%s.html", dst), obj, pro, b, c, t, base)
-			}
+	defer f.Close()
 
-			createCommitPage(base, pro, c, b, t)
-		}
+	if err != nil {
+		log.Printf("unable to create object: %v", err)
+		return
 	}
 
-	writeTemplate(pro, opt, t, branches)
+	if _, err := f.Write(out); err != nil {
+		log.Printf("unable to write object blob: %v", err)
+		return
+	}
 }
 
 func writeNom(nom string, obj object, pro *project, b branch, c commit, t *template.Template, base string) {
@@ -442,7 +472,7 @@ func writeNom(nom string, obj object, pro *project, b branch, c commit, t *templ
 	}
 }
 
-func createCommitPage(base string, pro *project, c commit, b branch, t *template.Template) {
+func writeCommitPage(base string, pro *project, c commit, b branch, t *template.Template) {
 	dst := filepath.Join(base, "index.html")
 	f, err := os.Create(dst)
 
@@ -468,28 +498,3 @@ func createCommitPage(base string, pro *project, c commit, b branch, t *template
 		return
 	}
 }
-
-func writeTemplate(pro *project, opt *options, t *template.Template, branches []branch) {
-	// This is the main index or project home.
-	f, err := os.Create(filepath.Join(pro.base, "index.html"))
-
-	defer f.Close()
-
-	if err != nil {
-		log.Fatalf("unable to create home page: %v", err)
-	}
-
-	p := page{
-		Data: Data{
-			"Branches": branches,
-			"Link":     opt.URL,
-			"Project":  pro.Name,
-		},
-		Base:  "./",
-		Title: pro.Name,
-	}
-
-	if err := t.Execute(f, p); err != nil {
-		log.Fatalf("unable to apply template: %v", err)
-	}
-}