| Refactoring pass. - staticgit - A git static site generator, the site you are v… | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit 91bdb33c51f86b1de9afbcc1f73122c300049226 | |
| parent 040e0e7448c05b1ad6b46fa90b2305d249f05ecf | |
| Author: Jay Scott <[email protected]> | |
| Date: Thu, 11 Jul 2024 23:48:08 +0100 | |
| Refactoring pass. | |
| Diffstat: | |
| M sealgit.go | 476 ++++++++++++++++++-----------… | |
| 1 file changed, 278 insertions(+), 198 deletions(-) | |
| --- | |
| diff --git a/sealgit.go b/sealgit.go | |
| @@ -10,9 +10,23 @@ import ( | |
| "strings" | |
| git "github.com/go-git/go-git/v5" | |
| + "github.com/go-git/go-git/v5/plumbing" | |
| "github.com/go-git/go-git/v5/plumbing/object" | |
| ) | |
| +type BranchInfo struct { | |
| + Name string | |
| + LastCommit string | |
| + LastCommitDate string | |
| +} | |
| + | |
| +type CommitInfo struct { | |
| + Hash string | |
| + Author string | |
| + Date string | |
| + Message string | |
| +} | |
| + | |
| type Config struct { | |
| ReposPath string | |
| GroupFlag bool | |
| @@ -27,38 +41,71 @@ type RepoInfo struct { | |
| Group string | |
| } | |
| -type CommitInfo struct { | |
| - Hash string | |
| - Author string | |
| - Date string | |
| - Message string | |
| -} | |
| - | |
| const ( | |
| - indexTemplate = ` | |
| + baseTemplate = ` | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | |
| -<title>repos for days!</title> | |
| -<link rel="icon" type="image/png" href="favicon.png" /> | |
| -<link rel="stylesheet" type="text/css" href="style.css" /> | |
| +<title>{{.Title}}</title> | |
| +<link rel="icon" type="image/png" href="{{.IconPath}}favicon.png" /> | |
| +<link rel="stylesheet" type="text/css" href="{{.IconPath}}style.css" /> | |
| </head> | |
| <body> | |
| <table> | |
| -<tr><td><img src="logo.png" alt="" width="32" height="32" /></td> | |
| -<td><span class="desc">repos for days!</span></td></tr><tr><td></td><td> | |
| +<tr><td><img src="{{.IconPath}}logo.png" alt="" width="32" height="32" /></td> | |
| +<td><span class="desc">{{.Title}}</span></td></tr><tr><td></td><td> | |
| </td></tr> | |
| </table> | |
| <hr/> | |
| <div id="content"> | |
| +{{template "content" .}} | |
| +</div> | |
| +</body> | |
| +</html> | |
| +` | |
| + | |
| + branchesContent = ` | |
| +{{define "content"}} | |
| +<h1>{{.RepoName}} - Branches</h1> | |
| +<table> | |
| +<thead> | |
| +<tr><td><b>Branch Name</b></td><td><b>Last Commit</b></td><td><b>Last Commit D… | |
| +</thead> | |
| +<tbody> | |
| +{{range .Branches}} | |
| +<tr><td>{{.Name}}</td><td>{{.LastCommit}}</td><td>{{.LastCommitDate}}</td></tr> | |
| +{{end}} | |
| +</tbody> | |
| +</table> | |
| +{{end}} | |
| +` | |
| + | |
| + commitHistoryContent = ` | |
| +{{define "content"}} | |
| +<h1>{{.RepoName}} - Commit History</h1> | |
| +<table> | |
| +<thead> | |
| +<tr><td><b>Hash</b></td><td><b>Author</b></td><td><b>Date</b></td><td><b>Messa… | |
| +</thead> | |
| +<tbody> | |
| +{{range .Commits}} | |
| +<tr><td>{{.Hash}}</td><td>{{.Author}}</td><td>{{.Date}}</td><td>{{.Message}}</… | |
| +{{end}} | |
| +</tbody> | |
| +</table> | |
| +{{end}} | |
| +` | |
| + | |
| + indexContent = ` | |
| +{{define "content"}} | |
| <table id="index"> | |
| <thead> | |
| <tr><td><b>Name</b></td><td><b>Description</b></td><td><b>Last commit</b></td>… | |
| </thead> | |
| <tbody> | |
| -{{range $group, $repos := .}} | |
| +{{range $group, $repos := .Repos}} | |
| <tr><td colspan="4"><b>{{if eq $group ""}} {{else}}<hr>{{end}}</b></td></tr> | |
| {{range $repos}} | |
| <tr> | |
| @@ -66,172 +113,79 @@ const ( | |
| <td>{{.Description}}</td> | |
| <td>| {{.LastCommit}} | </td> | |
| <td> | |
| - <a href="{{.Name}}/README.html">README</a> | | |
| - <a href="{{.Name}}/commit.html">COMMITS</a> | |
| + <a href="{{.Name}}/README.html">readme</a> | | |
| + <a href="{{.Name}}/commits.html">commits</a> | | |
| + <a href="{{.Name}}/branches.html">branches</a> | |
| </td> | |
| </tr> | |
| {{end}} | |
| {{end}} | |
| </tbody> | |
| </table> | |
| -</div> | |
| -</body> | |
| -</html> | |
| +{{end}} | |
| ` | |
| - | |
| - readmeTemplate = ` | |
| -<!DOCTYPE html> | |
| -<html> | |
| -<head> | |
| -<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
| -<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
| -<title>{{.RepoName}} - Readme!</title> | |
| -<link rel="icon" type="image/png" href="../favicon.png" /> | |
| -<link rel="stylesheet" type="text/css" href="../style.css" /> | |
| -</head> | |
| -<body> | |
| -<table> | |
| -<tr><td><img src="../logo.png" alt="" width="32" height="32" /></td> | |
| -<td><span class="desc"><a href='../index.html'>..back</a></span></td></tr><tr>… | |
| -</td></tr> | |
| -</table> | |
| -<hr/> | |
| + readmeContent = ` | |
| +{{define "content"}} | |
| <h1>{{.RepoName}}</h1> | |
| -<div id="content"> | |
| <pre>{{.ReadmeContent}}</pre> | |
| -</div> | |
| -</body> | |
| -</html> | |
| -` | |
| - | |
| - commitHistoryTemplate = ` | |
| -<!DOCTYPE html> | |
| -<html> | |
| -<head> | |
| -<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
| -<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
| -<title>{{.RepoName}} - History</title> | |
| -<link rel="icon" type="image/png" href="../favicon.png" /> | |
| -<link rel="stylesheet" type="text/css" href="../style.css" /> | |
| -</head> | |
| -<body> | |
| -<table> | |
| -<tr><td><img src="../logo.png" alt="" width="32" height="32" /></td> | |
| -<td><span class="desc"><a href='../index.html'>..back</a></span></td></tr><tr>… | |
| -</td></tr> | |
| -</table> | |
| -<hr/> | |
| -<h1>{{.RepoName}} - Commit History</h1> | |
| -<div id="content"> | |
| -<table> | |
| -<thead> | |
| -<tr><td><b>Hash</b></td><td><b>Author</b></td><td><b>Date</b></td><td><b>Messa… | |
| -</thead> | |
| -<tbody> | |
| -{{range .Commits}} | |
| -<tr><td>{{.Hash}}</td><td>{{.Author}}</td><td>{{.Date}}</td><td>{{.Message}}</… | |
| {{end}} | |
| -</tbody> | |
| -</table> | |
| -</div> | |
| -</body> | |
| -</html> | |
| ` | |
| ) | |
| var ( | |
| - indexTmpl = template.Must(template.New("index").Parse(indexTem… | |
| - readmeTmpl = template.Must(template.New("readme").Parse(readmeT… | |
| - commitHistoryTmpl = template.Must(template.New("commitHistory").Parse(… | |
| + branchesTmpl = template.Must(template.New("base").Parse(baseTempl… | |
| + commitHistoryTmpl = template.Must(template.New("base").Parse(baseTempl… | |
| + indexTmpl = template.Must(template.New("base").Parse(baseTempl… | |
| + readmeTmpl = template.Must(template.New("base").Parse(baseTempl… | |
| ) | |
| -func parseIgnoreDirs(ignoreDirs string) map[string]bool { | |
| - ignoreMap := make(map[string]bool) | |
| - for _, dir := range strings.Split(ignoreDirs, ",") { | |
| - if trimmedDir := strings.TrimSpace(dir); trimmedDir != "" { | |
| - ignoreMap[trimmedDir] = true | |
| - } | |
| - } | |
| - return ignoreMap | |
| -} | |
| +func generateIndexHTML(cfg *Config, repoInfos []RepoInfo) error { | |
| + groupedRepos := groupRepos(repoInfos, cfg.GroupFlag) | |
| + indexOutputPath := filepath.Join(cfg.OutputRoot, "index.html") | |
| -func processRepositories(cfg *Config) error { | |
| - repos, err := os.ReadDir(cfg.ReposPath) | |
| + indexFile, err := os.Create(indexOutputPath) | |
| if err != nil { | |
| - return fmt.Errorf("failed to read repos directory: %w", err) | |
| - } | |
| - | |
| - var repoInfos []RepoInfo | |
| - for _, r := range repos { | |
| - if r.IsDir() && !cfg.IgnoreDirs[r.Name()] { | |
| - repoPath := filepath.Join(cfg.ReposPath, r.Name()) | |
| - repoInfo, err := getRepoInfo(repoPath, cfg.GroupFlag) | |
| - if err != nil { | |
| - fmt.Printf("Failed to get info for repo %s: %v… | |
| - continue | |
| - } | |
| - repoInfos = append(repoInfos, repoInfo) | |
| - | |
| - outputDir := filepath.Join(cfg.OutputRoot, r.Name()) | |
| - if err := os.MkdirAll(outputDir, 0755); err != nil { | |
| - fmt.Printf("Failed to create output directory … | |
| - continue | |
| - } | |
| - | |
| - if err := processReadme(cfg, r.Name(), repoPath, outpu… | |
| - fmt.Printf("Failed to process README for repo … | |
| - } | |
| - | |
| - if err := processCommitHistory(cfg, r.Name(), repoPath… | |
| - fmt.Printf("Failed to process commit history f… | |
| - } | |
| - } | |
| + return fmt.Errorf("failed to create index HTML file: %w", err) | |
| } | |
| + defer indexFile.Close() | |
| - return generateIndexHTML(cfg, repoInfos) | |
| + return indexTmpl.Execute(indexFile, struct { | |
| + Title string | |
| + IconPath string | |
| + Repos map[string][]RepoInfo | |
| + }{ | |
| + Title: "git clone [email protected]:<reponame>", | |
| + IconPath: "./", | |
| + Repos: groupedRepos, | |
| + }) | |
| } | |
| -func getRepoInfo(repoPath string, groupFlag bool) (RepoInfo, error) { | |
| - repo, err := git.PlainOpen(repoPath) | |
| - if err != nil { | |
| - return RepoInfo{}, fmt.Errorf("failed to open repository: %w",… | |
| - } | |
| - | |
| - headRef, err := repo.Head() | |
| +func getBranchInfo(repo *git.Repository) ([]BranchInfo, error) { | |
| + branches, err := repo.Branches() | |
| if err != nil { | |
| - return RepoInfo{}, fmt.Errorf("failed to get HEAD reference: %… | |
| + return nil, fmt.Errorf("failed to get branches: %w", err) | |
| } | |
| - commit, err := repo.CommitObject(headRef.Hash()) | |
| - if err != nil { | |
| - return RepoInfo{}, fmt.Errorf("failed to get commit object: %w… | |
| - } | |
| - | |
| - description, group := getDescriptionAndGroup(repoPath, groupFlag) | |
| - | |
| - return RepoInfo{ | |
| - Name: filepath.Base(repoPath), | |
| - Description: description, | |
| - LastCommit: commit.Committer.When.Format("02 Jan 2006"), | |
| - Group: group, | |
| - }, nil | |
| -} | |
| + var branchInfos []BranchInfo | |
| + err = branches.ForEach(func(branch *plumbing.Reference) error { | |
| + commit, err := repo.CommitObject(branch.Hash()) | |
| + if err != nil { | |
| + return fmt.Errorf("failed to get commit for branch %s:… | |
| + } | |
| -func getDescriptionAndGroup(repoPath string, groupFlag bool) (string, string) { | |
| - descPath := filepath.Join(repoPath, "description") | |
| - description, _ := os.ReadFile(descPath) | |
| - desc := strings.TrimSpace(string(description)) | |
| - groupRegex := regexp.MustCompile(`\[(.*?)\]`) | |
| + branchInfos = append(branchInfos, BranchInfo{ | |
| + Name: branch.Name().Short(), | |
| + LastCommit: commit.Hash.String()[:7], | |
| + LastCommitDate: commit.Author.When.Format("02 Jan 2006… | |
| + }) | |
| + return nil | |
| + }) | |
| - var group string | |
| - if groupFlag { | |
| - matches := groupRegex.FindStringSubmatch(desc) | |
| - if len(matches) > 1 { | |
| - group = matches[1] | |
| - } | |
| + if err != nil { | |
| + return nil, fmt.Errorf("failed to iterate over branches: %w", … | |
| } | |
| - return desc, group | |
| + return branchInfos, nil | |
| } | |
| func getCommitHistory(repo *git.Repository) ([]CommitInfo, error) { | |
| @@ -263,49 +217,21 @@ func getCommitHistory(repo *git.Repository) ([]CommitInfo… | |
| return commitHistory, nil | |
| } | |
| -func processCommitHistory(cfg *Config, repoName, repoPath, outputDir string) e… | |
| - repo, err := git.PlainOpen(repoPath) | |
| - if err != nil { | |
| - return fmt.Errorf("failed to open git repository: %w", err) | |
| - } | |
| - | |
| - commits, err := getCommitHistory(repo) | |
| - if err != nil { | |
| - return fmt.Errorf("failed to get commit history: %w", err) | |
| - } | |
| - | |
| - outputPath := filepath.Join(outputDir, "commit.html") | |
| - | |
| - f, err := os.Create(outputPath) | |
| - if err != nil { | |
| - return fmt.Errorf("failed to create commit history HTML file: … | |
| - } | |
| - defer f.Close() | |
| - | |
| - return commitHistoryTmpl.Execute(f, struct { | |
| - RepoName string | |
| - Commits []CommitInfo | |
| - }{RepoName: repoName, Commits: commits}) | |
| -} | |
| - | |
| -func processReadme(cfg *Config, repoName, repoPath, outputDir string) error { | |
| - readme, err := getReadme(repoPath) | |
| - if err != nil { | |
| - return fmt.Errorf("failed to get README: %w", err) | |
| - } | |
| - | |
| - outputPath := filepath.Join(outputDir, "README.html") | |
| +func getDescriptionAndGroup(repoPath string, groupFlag bool) (string, string) { | |
| + descPath := filepath.Join(repoPath, "description") | |
| + description, _ := os.ReadFile(descPath) | |
| + desc := strings.TrimSpace(string(description)) | |
| + groupRegex := regexp.MustCompile(`\[(.*?)\]`) | |
| - f, err := os.Create(outputPath) | |
| - if err != nil { | |
| - return fmt.Errorf("failed to create README HTML file: %w", err) | |
| + var group string | |
| + if groupFlag { | |
| + matches := groupRegex.FindStringSubmatch(desc) | |
| + if len(matches) > 1 { | |
| + group = matches[1] | |
| + } | |
| } | |
| - defer f.Close() | |
| - return readmeTmpl.Execute(f, struct { | |
| - RepoName string | |
| - ReadmeContent string | |
| - }{RepoName: repoName, ReadmeContent: readme}) | |
| + return desc, group | |
| } | |
| func getReadme(repoPath string) (string, error) { | |
| @@ -347,17 +273,30 @@ func getReadme(repoPath string) (string, error) { | |
| return "No README found!", nil | |
| } | |
| -func generateIndexHTML(cfg *Config, repoInfos []RepoInfo) error { | |
| - groupedRepos := groupRepos(repoInfos, cfg.GroupFlag) | |
| - indexOutputPath := filepath.Join(cfg.OutputRoot, "index.html") | |
| +func getRepoInfo(repoPath string, groupFlag bool) (RepoInfo, error) { | |
| + repo, err := git.PlainOpen(repoPath) | |
| + if err != nil { | |
| + return RepoInfo{}, fmt.Errorf("failed to open repository: %w",… | |
| + } | |
| - indexFile, err := os.Create(indexOutputPath) | |
| + headRef, err := repo.Head() | |
| if err != nil { | |
| - return fmt.Errorf("failed to create index HTML file: %w", err) | |
| + return RepoInfo{}, fmt.Errorf("failed to get HEAD reference: %… | |
| + } | |
| + | |
| + commit, err := repo.CommitObject(headRef.Hash()) | |
| + if err != nil { | |
| + return RepoInfo{}, fmt.Errorf("failed to get commit object: %w… | |
| } | |
| - defer indexFile.Close() | |
| - return indexTmpl.Execute(indexFile, groupedRepos) | |
| + description, group := getDescriptionAndGroup(repoPath, groupFlag) | |
| + | |
| + return RepoInfo{ | |
| + Name: filepath.Base(repoPath), | |
| + Description: description, | |
| + LastCommit: commit.Committer.When.Format("02 Jan 2006"), | |
| + Group: group, | |
| + }, nil | |
| } | |
| func groupRepos(repos []RepoInfo, groupFlag bool) map[string][]RepoInfo { | |
| @@ -378,3 +317,144 @@ func groupRepos(repos []RepoInfo, groupFlag bool) map[str… | |
| return groupedRepos | |
| } | |
| + | |
| +func parseIgnoreDirs(ignoreDirs string) map[string]bool { | |
| + ignoreMap := make(map[string]bool) | |
| + for _, dir := range strings.Split(ignoreDirs, ",") { | |
| + if trimmedDir := strings.TrimSpace(dir); trimmedDir != "" { | |
| + ignoreMap[trimmedDir] = true | |
| + } | |
| + } | |
| + return ignoreMap | |
| +} | |
| + | |
| +func processBranches(cfg *Config, repoName, repoPath, outputDir string) error { | |
| + repo, err := git.PlainOpen(repoPath) | |
| + if err != nil { | |
| + return fmt.Errorf("failed to open git repository: %w", err) | |
| + } | |
| + | |
| + branches, err := getBranchInfo(repo) | |
| + if err != nil { | |
| + return fmt.Errorf("failed to get branch information: %w", err) | |
| + } | |
| + | |
| + outputPath := filepath.Join(outputDir, "branches.html") | |
| + | |
| + f, err := os.Create(outputPath) | |
| + if err != nil { | |
| + return fmt.Errorf("failed to create branches HTML file: %w", e… | |
| + } | |
| + defer f.Close() | |
| + | |
| + return branchesTmpl.Execute(f, struct { | |
| + Title string | |
| + IconPath string | |
| + RepoName string | |
| + Branches []BranchInfo | |
| + }{ | |
| + Title: repoName + " - Branches", | |
| + IconPath: "../", | |
| + RepoName: repoName, | |
| + Branches: branches, | |
| + }) | |
| +} | |
| + | |
| +func processCommitHistory(cfg *Config, repoName, repoPath, outputDir string) e… | |
| + repo, err := git.PlainOpen(repoPath) | |
| + if err != nil { | |
| + return fmt.Errorf("failed to open git repository: %w", err) | |
| + } | |
| + | |
| + commits, err := getCommitHistory(repo) | |
| + if err != nil { | |
| + return fmt.Errorf("failed to get commit history: %w", err) | |
| + } | |
| + | |
| + outputPath := filepath.Join(outputDir, "commits.html") | |
| + | |
| + f, err := os.Create(outputPath) | |
| + if err != nil { | |
| + return fmt.Errorf("failed to create commit history HTML file: … | |
| + } | |
| + defer f.Close() | |
| + | |
| + return commitHistoryTmpl.Execute(f, struct { | |
| + Title string | |
| + IconPath string | |
| + RepoName string | |
| + Commits []CommitInfo | |
| + }{ | |
| + Title: repoName + " - History", | |
| + IconPath: "../", | |
| + RepoName: repoName, | |
| + Commits: commits, | |
| + }) | |
| +} | |
| + | |
| +func processReadme(cfg *Config, repoName, repoPath, outputDir string) error { | |
| + readme, err := getReadme(repoPath) | |
| + if err != nil { | |
| + return fmt.Errorf("failed to get README: %w", err) | |
| + } | |
| + | |
| + outputPath := filepath.Join(outputDir, "README.html") | |
| + | |
| + f, err := os.Create(outputPath) | |
| + if err != nil { | |
| + return fmt.Errorf("failed to create README HTML file: %w", err) | |
| + } | |
| + defer f.Close() | |
| + | |
| + return readmeTmpl.Execute(f, struct { | |
| + Title string | |
| + IconPath string | |
| + RepoName string | |
| + ReadmeContent string | |
| + }{ | |
| + Title: repoName + " - Readme!", | |
| + IconPath: "../", | |
| + RepoName: repoName, | |
| + ReadmeContent: readme, | |
| + }) | |
| +} | |
| + | |
| +func processRepositories(cfg *Config) error { | |
| + repos, err := os.ReadDir(cfg.ReposPath) | |
| + if err != nil { | |
| + return fmt.Errorf("failed to read repos directory: %w", err) | |
| + } | |
| + | |
| + var repoInfos []RepoInfo | |
| + for _, r := range repos { | |
| + if r.IsDir() && !cfg.IgnoreDirs[r.Name()] { | |
| + repoPath := filepath.Join(cfg.ReposPath, r.Name()) | |
| + repoInfo, err := getRepoInfo(repoPath, cfg.GroupFlag) | |
| + if err != nil { | |
| + fmt.Printf("Failed to get info for repo %s: %v… | |
| + continue | |
| + } | |
| + repoInfos = append(repoInfos, repoInfo) | |
| + | |
| + outputDir := filepath.Join(cfg.OutputRoot, r.Name()) | |
| + if err := os.MkdirAll(outputDir, 0755); err != nil { | |
| + fmt.Printf("Failed to create output directory … | |
| + continue | |
| + } | |
| + | |
| + if err := processReadme(cfg, r.Name(), repoPath, outpu… | |
| + fmt.Printf("Failed to process README for repo … | |
| + } | |
| + | |
| + if err := processCommitHistory(cfg, r.Name(), repoPath… | |
| + fmt.Printf("Failed to process commit history f… | |
| + } | |
| + | |
| + if err := processBranches(cfg, r.Name(), repoPath, out… | |
| + fmt.Printf("Failed to process branches for rep… | |
| + } | |
| + } | |
| + } | |
| + | |
| + return generateIndexHTML(cfg, repoInfos) | |
| +} |