repos / pgit

static site generator for git
git clone https://github.com/picosh/pgit.git

commit
5b50e23
parent
ea6d2bf
author
Eric Bower
date
2023-08-10 03:46:05 +0000 UTC
progress
3 files changed,  +65, -52
M html/log.page.tmpl
+9, -1
 1@@ -12,8 +12,16 @@
 2         <div>
 3           <a href="{{.URL}}">{{.SummaryStr}}</a>
 4         </div>
 5-        <div>
 6+        <div class="flex gap">
 7           {{.ShortID}}
 8+
 9+          {{range .Refs}}
10+            {{if .URL}}
11+              <a href="{{.URL}}">({{.Refspec}})</a>
12+            {{else}}
13+              ({{.Refspec}})
14+            {{end}}
15+          {{end}}
16         </div>
17       </div>
18 
M html/refs.page.tmpl
+2, -1
 1@@ -6,6 +6,7 @@
 2   {{template "header" .}}
 3 
 4   <h2 class="text-md font-bold">refs</h2>
 5+
 6   <ul>
 7   {{range .Refs}}
 8     {{if .URL}}
 9@@ -14,5 +15,5 @@
10       <li>{{.Refspec}}</li>
11     {{end}}
12   {{end}}
13-  </ol>
14+  </ul>
15 {{end}}
M main.go
+54, -50
  1@@ -34,8 +34,6 @@ type Config struct {
  2 	RepoPath string
  3 
  4 	// optional params
  5-	// generate logs and trees based on the git references provided
  6-	Refs []string
  7 	// generate logs anad tree based on the git revisions provided
  8 	Revs []string
  9 	// description of repo used in the header of site
 10@@ -73,12 +71,18 @@ type RevData struct {
 11 	LogURL  template.URL
 12 }
 13 
 14+type TagData struct {
 15+	Name string
 16+	URL template.URL
 17+}
 18+
 19 type CommitData struct {
 20 	SummaryStr string
 21 	URL        template.URL
 22 	WhenStr    string
 23 	AuthorStr  string
 24 	ShortID    string
 25+	Refs []*RefInfo
 26 	*git.Commit
 27 }
 28 
 29@@ -113,6 +117,7 @@ type DiffRenderFile struct {
 30 }
 31 
 32 type RefInfo struct {
 33+	ID string
 34 	Refspec string
 35 	URL     template.URL
 36 }
 37@@ -408,6 +413,7 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs []
 38 		commitID := commit.ID.String()
 39 
 40 		if c.Cache[commitID] {
 41+			c.Logger.Infof("(%s) commit file already generated, skipping", getShortID(commitID))
 42 			continue
 43 		} else {
 44 			c.Cache[commitID] = true
 45@@ -469,9 +475,9 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs []
 46 		commitData := &CommitPageData{
 47 			PageData:  pageData,
 48 			Commit:    commit,
 49-			CommitID:  commit.ID.String()[:7],
 50+			CommitID:  getShortID(commitID),
 51 			Diff:      rnd,
 52-			Parent:    parentID[:7],
 53+			Parent:    getShortID(parentID),
 54 			CommitURL: c.getCommitURL(commitID),
 55 			ParentURL: c.getCommitURL(parentID),
 56 		}
 57@@ -537,31 +543,19 @@ func (c *Config) writeRepo() *BranchOutput {
 58 
 59 	var first *RevData
 60 	revs := []*RevData{}
 61-	for _, refStr := range c.Refs {
 62-		for _, ref := range refs {
 63-			refName := git.RefShortName(ref.Refspec)
 64-			if refName != refStr {
 65-				continue
 66-			}
 67-
 68-			data := &RevData{
 69-				ID:      ref.ID,
 70-				RevName: refName,
 71-				TreeURL: c.getTreeUrl(refName),
 72-				LogURL:  c.getLogsUrl(refName),
 73-			}
 74-			if first == nil {
 75-				first = data
 76-			}
 77-			revs = append(revs, data)
 78-		}
 79-	}
 80-
 81 	for _, revStr := range c.Revs {
 82 		fullRevID, err := repo.RevParse(revStr)
 83 		bail(err)
 84 
 85 		revName := getShortID(fullRevID)
 86+		// if it's a reference then label it as such
 87+		for _, ref := range refs {
 88+			if revStr == git.RefShortName(ref.Refspec) || revStr == ref.Refspec {
 89+				revName = revStr
 90+				break
 91+			}
 92+		}
 93+
 94 		data := &RevData{
 95 			ID:      fullRevID,
 96 			RevName: revName,
 97@@ -582,33 +576,24 @@ func (c *Config) writeRepo() *BranchOutput {
 98 	mainOutput := &BranchOutput{}
 99 	claimed := false
100 	for _, revData := range revs {
101-		refInfoMap[revData.ID] = &RefInfo{
102+		refInfoMap[revData.RevName] = &RefInfo{
103+			ID: revData.ID,
104 			Refspec: revData.RevName,
105 			URL:     revData.TreeURL,
106 		}
107-
108-		data := &PageData{
109-			Repo:     c,
110-			RevData:  revData,
111-			SiteURLs: c.getURLs(),
112-		}
113-
114-		branchOutput := c.writeBranch(repo, data)
115-		if !claimed {
116-			mainOutput = branchOutput
117-			claimed = true
118-		}
119 	}
120 
121 	// loop through ALL refs that don't have URLs
122 	// and add them to the map
123 	for _, ref := range refs {
124-		if refInfoMap[ref.ID] != nil {
125+		refspec := git.RefShortName(ref.Refspec)
126+		if refInfoMap[refspec] != nil {
127 			continue
128 		}
129 
130-		refInfoMap[ref.ID] = &RefInfo{
131-			Refspec: strings.TrimPrefix(ref.Refspec, "refs/"),
132+		refInfoMap[refspec] = &RefInfo{
133+			ID: ref.ID,
134+			Refspec: refspec,
135 		}
136 	}
137 
138@@ -628,6 +613,20 @@ func (c *Config) writeRepo() *BranchOutput {
139 		return urlI > urlJ
140 	})
141 
142+	for _, revData := range revs {
143+	data := &PageData{
144+			Repo:     c,
145+			RevData:  revData,
146+			SiteURLs: c.getURLs(),
147+		}
148+
149+		branchOutput := c.writeBranch(repo, data, refInfoList)
150+		if !claimed {
151+			mainOutput = branchOutput
152+			claimed = true
153+		}
154+	}
155+
156 	// use the first revision in our list to generate
157 	// the root summary, logs, and tree the user can click
158 	revData := &RevData{
159@@ -646,7 +645,7 @@ func (c *Config) writeRepo() *BranchOutput {
160 	return mainOutput
161 }
162 
163-func (c *Config) writeBranch(repo *git.Repository, pageData *PageData) *BranchOutput {
164+func (c *Config) writeBranch(repo *git.Repository, pageData *PageData, refs []*RefInfo) *BranchOutput {
165 	fmt.Printf("compiling (%s) branch (%s)\n", c.RepoName, pageData.RevData.RevName)
166 
167 	output := &BranchOutput{}
168@@ -664,13 +663,21 @@ func (c *Config) writeBranch(repo *git.Repository, pageData *PageData) *BranchOu
169 			output.LastCommit = commit
170 		}
171 
172+		tags := []*RefInfo{}
173+		for _, ref := range refs {
174+			if commit.ID.String() == ref.ID {
175+				tags = append(tags, ref)
176+			}
177+		}
178+
179 		logs = append(logs, &CommitData{
180 			URL:        c.getCommitURL(commit.ID.String()),
181-			ShortID:    commit.ID.String()[:7],
182+			ShortID:    getShortID(commit.ID.String()),
183 			SummaryStr: commit.Summary(),
184 			AuthorStr:  commit.Author.Name,
185 			WhenStr:    timediff.TimeDiff(commit.Author.When),
186 			Commit:     commit,
187+			Refs: tags,
188 		})
189 	}
190 
191@@ -729,8 +736,7 @@ func (c *Config) writeBranch(repo *git.Repository, pageData *PageData) *BranchOu
192 func main() {
193 	var outdir = flag.String("out", "./public", "output directory")
194 	var rpath = flag.String("repo", ".", "path to git repo")
195-	var refsFlag = flag.String("refs", "", "list of refs to generate logs and tree (e.g. main,v1)")
196-	var revsFlag = flag.String("revs", "HEAD", "list of revs to generate logs and tree (e.g. c69f86f,7415be1")
197+	var revsFlag = flag.String("revs", "HEAD", "list of revs to generate logs and tree (e.g. main,v1,c69f86f,HEAD")
198 	var themeFlag = flag.String("theme", "dracula", "theme to use for site")
199 
200 	flag.Parse()
201@@ -740,16 +746,15 @@ func main() {
202 	repoPath, err := filepath.Abs(*rpath)
203 	bail(err)
204 
205-	refs := strings.Split(*refsFlag, ",")
206-	if len(refs) == 1 && refs[0] == "" {
207-		refs = []string{}
208-	}
209-
210 	revs := strings.Split(*revsFlag, ",")
211 	if len(revs) == 1 && revs[0] == "" {
212 		revs = []string{}
213 	}
214 
215+	if len(revs) == 0 {
216+		bail(fmt.Errorf("you must provide --revs"))
217+	}
218+
219 	theme := styles.Get(*themeFlag)
220 
221 	lg, err := zap.NewProduction()
222@@ -764,7 +769,6 @@ func main() {
223 		RepoPath: repoPath,
224 		RepoName: repoName(repoPath),
225 		Cache:    make(map[string]bool),
226-		Refs:     refs,
227 		Revs:     revs,
228 		Theme:    theme,
229 		Logger:   logger,