diff --git a/main.go b/main.go index 658af0b..08fcfb7 100644 --- a/main.go +++ b/main.go @@ -15,9 +15,7 @@ import ( "path/filepath" "reflect" "strings" - "sync" "text/tabwriter" - "time" ) // EMPTY is git's magic empty tree hash. @@ -29,7 +27,7 @@ var tpl string func init() { // Override default usage output. flag.Usage = func() { - // Print usage example ahead of lisiting default options. + // Print usage example ahead of listing default options. fmt.Fprintln(flag.CommandLine.Output(), "usage:", os.Args[0], "[<options>] <path>") flag.PrintDefaults() } @@ -195,7 +193,6 @@ func main() { log.Fatalf("unable to filter branches: %v", err) } - var wg sync.WaitGroup t := template.Must(template.New("page").Funcs(template.FuncMap{ "diffstatbodyparser": diffstatbodyparser, "diffbodyparser": diffbodyparser, @@ -357,148 +354,142 @@ func main() { return } }() + writeNom(fmt.Sprintf("%s.html", dst), obj, pro, b, c, t, base) + } - func(nom string) { - f, err := os.Create(nom) - - defer f.Close() - - if err != nil { - log.Printf("unable to create object: %v", err) - - return - } - - o := &show{ - object: object{ - Hash: obj.Hash, - Path: obj.Path, - }, - Bin: types[filepath.Ext(obj.Path)], - } - - if o.Bin { - // TODO. - } else { - cmd := exec.Command("git", "show", "--no-notes", obj.Hash) - cmd.Dir = pro.repo - - out, err := cmd.Output() + createCommitPage(base, pro, c, b, t) + } + } - if err != nil { - log.Printf("unable to show object: %v", err) + writeTemplate(pro, opt, t, branches) +} - return - } +func writeNom(nom string, obj object, pro *project, b branch, c commit, t *template.Template, base string) { + f, err := os.Create(nom) + defer f.Close() - sep := []byte("\n") - var lines = make([]int, bytes.Count(out, sep)) + if err != nil { + log.Printf("unable to create object: %v", err) + return + } - for i := range lines { - lines[i] = i + 1 - } + o := &show{ + object: object{ + Hash: obj.Hash, + Path: obj.Path, + }, + Bin: types[filepath.Ext(obj.Path)], + } - if bytes.LastIndex(out, sep) != len(out)-1 { - lines = append(lines, len(lines)) - } + if o.Bin { + // TODO. + } else { + cmd := exec.Command("git", "show", "--no-notes", obj.Hash) + cmd.Dir = pro.repo - o.Lines = lines - o.Body = fmt.Sprintf("%s", out) - } + out, err := cmd.Output() - p := page{ - Data: Data{ - "Object": *o, - "Project": pro.Name, - }, - Base: "../../", - Title: strings.Join([]string{pro.Name, b.Name, c.Abbr, obj.Path}, ": "), - } + if err != nil { + log.Printf("unable to show object: %v", err) - if err := t.Execute(f, p); err != nil { - log.Printf("unable to apply template: %v", err) + return + } - return - } + sep := []byte("\n") + var lines = make([]int, bytes.Count(out, sep)) - lnk := filepath.Join(base, fmt.Sprintf("%s.html", obj.Path)) + for i := range lines { + lines[i] = i + 1 + } - if err := os.MkdirAll(filepath.Dir(lnk), 0755); err != nil { - if err != nil { - log.Printf("unable to create hard link path: %v", err) - } + if bytes.LastIndex(out, sep) != len(out)-1 { + lines = append(lines, len(lines)) + } - return - } + o.Lines = lines + o.Body = fmt.Sprintf("%s", out) + } - if err := os.Link(nom, lnk); err != nil { - if os.IsExist(err) { - return - } + p := page{ + Data: Data{ + "Object": *o, + "Project": pro.Name, + }, + Base: "../../", + Title: strings.Join([]string{pro.Name, b.Name, c.Abbr, obj.Path}, ": "), + } - log.Printf("unable to hard link object into commit folder: %v", err) - } - }(fmt.Sprintf("%s.html", dst)) - } + if err := t.Execute(f, p); err != nil { + log.Printf("unable to apply template: %v", err) + return + } - func() { - dst := filepath.Join(base, "index.html") - f, err := os.Create(dst) + lnk := filepath.Join(base, fmt.Sprintf("%s.html", obj.Path)) - defer f.Close() + if err := os.MkdirAll(filepath.Dir(lnk), 0755); err != nil { + if err != nil { + log.Printf("unable to create hard link path: %v", err) + } + return + } - if err != nil { - log.Printf("unable to create commit page: %v", err) + if err := os.Link(nom, lnk); err != nil { + if os.IsExist(err) { + return + } - return - } + log.Printf("unable to hard link object into commit folder: %v", err) + } +} - p := page{ - Data: Data{ - "Commit": c, - "Project": pro.Name, - }, - Base: "../../", - Title: strings.Join([]string{pro.Name, b.Name, c.Abbr}, ": "), - } +func createCommitPage(base string, pro *project, c commit, b branch, t *template.Template) { + dst := filepath.Join(base, "index.html") + f, err := os.Create(dst) - if err := t.Execute(f, p); err != nil { - log.Printf("unable to apply template: %v", err) + defer f.Close() - return - } - }() - } + if err != nil { + log.Printf("unable to create commit page: %v", err) + // TODO(spike): handle error? + return } - wg.Add(1) + p := page{ + Data: Data{ + "Commit": c, + "Project": pro.Name, + }, + Base: "../../", + Title: strings.Join([]string{pro.Name, b.Name, c.Abbr}, ": "), + } - go func() { - defer wg.Done() + if err := t.Execute(f, p); err != nil { + log.Printf("unable to apply template: %v", err) + return + } +} - // This is the main index or project home. - f, err := os.Create(filepath.Join(pro.base, "index.html")) +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() + 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 != nil { + log.Fatalf("unable to create home page: %v", err) + } - if err := t.Execute(f, p); err != nil { - log.Fatalf("unable to apply template: %v", err) - } - }() + p := page{ + Data: Data{ + "Branches": branches, + "Link": opt.URL, + "Project": pro.Name, + }, + Base: "./", + Title: pro.Name, + } - wg.Wait() + if err := t.Execute(f, p); err != nil { + log.Fatalf("unable to apply template: %v", err) + } } diff --git a/types.go b/types.go index 363bebf..7966a44 100644 --- a/types.go +++ b/types.go @@ -1,3 +1,10 @@ +package main + +import ( + "path/filepath" + "time" +) + // Data is the generic content map passed on to the page template. type Data map[string]interface{} type page struct { @@ -73,4 +80,3 @@ type author struct { Email string Name string } -
home › develop › 542911b › 8517fa9