repos / pgit

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

commit
029d56d
parent
c69f86f
author
Eric Bower
date
2023-08-09 04:13:46 +0000 UTC
progress
1 files changed,  +80, -56
M main.go
+80, -56
  1@@ -32,8 +32,10 @@ type Config struct {
  2 	RepoPath string
  3 
  4 	// optional params
  5-	// generate logs and trees based on the refs provided
  6+	// generate logs and trees based on the git references provided
  7 	Refs []string
  8+	// generate logs anad tree based on the git revisions provided
  9+	Revs []string
 10 	// description of repo used in the header of site
 11 	Desc string
 12 	// maximum number of commits that we will process in descending order
 13@@ -57,11 +59,12 @@ type Config struct {
 14 	RepoName string
 15 }
 16 
 17+// revision data
 18 type RevData struct {
 19+	ID      string
 20 	RevName string
 21 	TreeURL template.URL
 22 	LogURL  template.URL
 23-	Ref     *git.Reference
 24 }
 25 
 26 type CommitData struct {
 27@@ -77,12 +80,12 @@ type TreeItem struct {
 28 	IsTextFile bool
 29 	Size       string
 30 	NumLines   int
 31-	URL        string
 32 	Path       string
 33-	Entry      *git.TreeEntry
 34+	URL        template.URL
 35 	CommitURL  template.URL
 36 	Summary    string
 37 	When       string
 38+	Entry      *git.TreeEntry
 39 }
 40 
 41 type DiffRender struct {
 42@@ -271,7 +274,7 @@ func walkTree(tree *git.Tree, branch string, curpath string, aggregate []*TreeIt
 43 				Size:  toPretty(entry.Size()),
 44 				Path:  fname,
 45 				Entry: entry,
 46-				URL:   filepath.Join("/", "tree", branch, "item", fname),
 47+				URL:   template.URL(filepath.Join("/", "tree", branch, "item", fname)),
 48 			})
 49 		}
 50 	}
 51@@ -514,6 +517,10 @@ func (c *Config) getURLs() *SiteURLs {
 52 	}
 53 }
 54 
 55+func getShortID(id string) string {
 56+	return id[:7]
 57+}
 58+
 59 func (c *Config) writeRepo() *BranchOutput {
 60 	repo, err := git.Open(c.RepoPath)
 61 	bail(err)
 62@@ -521,57 +528,72 @@ func (c *Config) writeRepo() *BranchOutput {
 63 	refs, err := repo.ShowRef(git.ShowRefOptions{Heads: true, Tags: true})
 64 	bail(err)
 65 
 66-	var first *git.Reference
 67-	fitRefs := []*git.Reference{}
 68+	var first *RevData
 69+	revs := []*RevData{}
 70 	for _, refStr := range c.Refs {
 71 		for _, ref := range refs {
 72-			rH := strings.Replace(ref.Refspec, "refs/heads/", "", 1)
 73-			rT := strings.Replace(rH, "refs/tags/", "", 1)
 74-			if rT == refStr {
 75-				if first == nil {
 76-					first = ref
 77-				}
 78-				fitRefs = append(fitRefs, ref)
 79+			refName := git.RefShortName(ref.Refspec)
 80+			if refName != refStr {
 81+				continue
 82+			}
 83+
 84+			data := &RevData{
 85+				ID:      ref.ID,
 86+				RevName: refName,
 87+				TreeURL: c.getTreeUrl(refName),
 88+				LogURL:  c.getLogsUrl(refName),
 89+			}
 90+			if first == nil {
 91+				first = data
 92 			}
 93+			revs = append(revs, data)
 94+		}
 95+	}
 96+
 97+	for _, revStr := range c.Revs {
 98+		fullRevID, err := repo.RevParse(revStr)
 99+		bail(err)
100+
101+		revName := getShortID(fullRevID)
102+		data := &RevData{
103+			ID:      fullRevID,
104+			RevName: revName,
105+			TreeURL: c.getTreeUrl(revName),
106+			LogURL:  c.getLogsUrl(revName),
107 		}
108+		if first == nil {
109+			first = data
110+		}
111+		revs = append(revs, data)
112 	}
113 
114 	if first == nil {
115 		bail(fmt.Errorf("could find find a git reference that matches criteria"))
116 	}
117 
118-	_, revName := filepath.Split(first.Refspec)
119-
120 	refInfoMap := map[string]*RefInfo{}
121 	mainOutput := &BranchOutput{}
122 	claimed := false
123-		for _, ref := range fitRefs {
124-			_, revn := filepath.Split(ref.Refspec)
125-			refInfoMap[ref.ID] = &RefInfo{
126-				Refspec: strings.TrimPrefix(ref.Refspec, "refs/"),
127-				URL:     c.getTreeUrl(revn),
128-			}
129-
130-			branchRepo := &RevData{
131-				TreeURL: c.getTreeUrl(revn),
132-				LogURL:  c.getLogsUrl(revn),
133-				RevName: revn,
134-				Ref:     ref,
135-			}
136+	for _, revData := range revs {
137+		refInfoMap[revData.ID] = &RefInfo{
138+			Refspec: revData.RevName,
139+			URL:     revData.TreeURL,
140+		}
141 
142-			data := &PageData{
143-				Repo:     c,
144-				RevData:  branchRepo,
145-				SiteURLs: c.getURLs(),
146-			}
147+		data := &PageData{
148+			Repo:     c,
149+			RevData:  revData,
150+			SiteURLs: c.getURLs(),
151+		}
152 
153-			branchOutput := c.writeBranch(repo, data)
154-			if !claimed {
155-				mainOutput = branchOutput
156-				claimed = true
157-			}
158+		branchOutput := c.writeBranch(repo, data)
159+		if !claimed {
160+			mainOutput = branchOutput
161+			claimed = true
162 		}
163+	}
164 
165+	// loop through ALL refs that don't have URLs
166 	for _, ref := range refs {
167 		if refInfoMap[ref.ID] != nil {
168 			continue
169@@ -598,9 +620,9 @@ func (c *Config) writeRepo() *BranchOutput {
170 	})
171 
172 	revData := &RevData{
173-		TreeURL: c.getTreeUrl(revName),
174-		LogURL:  c.getLogsUrl(revName),
175-		RevName: revName,
176+		TreeURL: c.getTreeUrl(first.RevName),
177+		LogURL:  c.getLogsUrl(first.RevName),
178+		RevName: first.RevName,
179 	}
180 
181 	data := &PageData{
182@@ -622,7 +644,7 @@ func (c *Config) writeBranch(repo *git.Repository, pageData *PageData) *BranchOu
183 		pageSize = 5000
184 	}
185 
186-	commits, err := repo.CommitsByPage(pageData.RevData.Ref.ID, 0, pageSize)
187+	commits, err := repo.CommitsByPage(pageData.RevData.ID, 0, pageSize)
188 	bail(err)
189 
190 	logs := []*CommitData{}
191@@ -641,7 +663,7 @@ func (c *Config) writeBranch(repo *git.Repository, pageData *PageData) *BranchOu
192 		})
193 	}
194 
195-	tree, err := repo.LsTree(pageData.RevData.Ref.ID)
196+	tree, err := repo.LsTree(pageData.RevData.ID)
197 	bail(err)
198 
199 	entries := []*TreeItem{}
200@@ -654,7 +676,7 @@ func (c *Config) writeBranch(repo *git.Repository, pageData *PageData) *BranchOu
201 		if pageData.Repo.HideTreeLastCommit {
202 			fmt.Println("skipping finding last commit for each file")
203 		} else {
204-			lastCommits, err = repo.RevList([]string{pageData.RevData.Ref.Refspec}, git.RevListOptions{
205+			lastCommits, err = repo.RevList([]string{pageData.RevData.ID}, git.RevListOptions{
206 				Path:           entry.Path,
207 				CommandOptions: git.CommandOptions{Args: []string{"-1"}},
208 			})
209@@ -668,14 +690,14 @@ func (c *Config) writeBranch(repo *git.Repository, pageData *PageData) *BranchOu
210 			entry.Summary = lc.Summary()
211 			entry.When = timediff.TimeDiff(lc.Author.When)
212 		}
213-		entry.URL = filepath.Join(
214+		entry.URL = template.URL(filepath.Join(
215 			"/",
216 			c.RepoName,
217 			"tree",
218 			pageData.RevData.RevName,
219 			"item",
220 			fmt.Sprintf("%s.html", entry.Path),
221-		)
222+		))
223 	}
224 
225 	fmt.Printf("compilation complete (%s) branch (%s)\n", c.RepoName, pageData.RevData.RevName)
226@@ -690,9 +712,10 @@ func (c *Config) writeBranch(repo *git.Repository, pageData *PageData) *BranchOu
227 }
228 
229 func main() {
230-	var outdir = flag.String("out", "./public", "output directory (default: ./public)")
231+	var outdir = flag.String("out", "./public", "output directory")
232 	var rpath = flag.String("repo", ".", "path to git repo")
233-	var refsFlag = flag.String("refs", "", "path to git repo")
234+	var refsFlag = flag.String("refs", "", "list of refs to generate logs and tree (e.g. main,v1)")
235+	var revsFlag = flag.String("revs", "HEAD", "list of revs to generate logs and tree (e.g. c69f86f,7415be1")
236 
237 	flag.Parse()
238 
239@@ -703,7 +726,12 @@ func main() {
240 
241 	refs := strings.Split(*refsFlag, ",")
242 	if len(refs) == 1 && refs[0] == "" {
243-		refs = []string{"main", "master"}
244+		refs = []string{}
245+	}
246+
247+	revs := strings.Split(*revsFlag, ",")
248+	if len(revs) == 1 && revs[0] == "" {
249+		revs = []string{}
250 	}
251 
252 	config := &Config{
253@@ -712,14 +740,10 @@ func main() {
254 		RepoName: repoName(repoPath),
255 		Cache:    make(map[string]bool),
256 		Refs:     refs,
257+		Revs:     revs,
258 	}
259 
260-	fmt.Println(config.Outdir)
261-	fmt.Println(config.RepoPath)
262-	fmt.Println(config.RepoName)
263-	fmt.Println(config.Refs)
264-
265 	config.writeRepo()
266-	url := filepath.Join(config.Outdir, config.RepoName, "index.html")
267+	url := filepath.Join("/", config.RepoName, "index.html")
268 	fmt.Println(url)
269 }