repos / pgit

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

commit
50d81f2
parent
e93b271
author
Eric Bower
date
2023-08-11 18:30:04 +0000 UTC
fix: use commit for refs in url
1 files changed,  +19, -23
M main.go
+19, -23
  1@@ -283,7 +283,7 @@ func readmeFile(repo *Config) string {
  2 	return strings.ToLower(repo.Readme)
  3 }
  4 
  5-func walkTree(tree *git.Tree, branch string, curpath string, aggregate []*TreeItem) []*TreeItem {
  6+func walkTree(tree *git.Tree, commitID string, curpath string, aggregate []*TreeItem) []*TreeItem {
  7 	entries, err := tree.Entries()
  8 	bail(err)
  9 
 10@@ -292,7 +292,7 @@ func walkTree(tree *git.Tree, branch string, curpath string, aggregate []*TreeIt
 11 		typ := entry.Type()
 12 		if typ == git.ObjectTree {
 13 			re, _ := tree.Subtree(entry.Name())
 14-			aggregate = walkTree(re, branch, fname, aggregate)
 15+			aggregate = walkTree(re, commitID, fname, aggregate)
 16 		}
 17 
 18 		if entry.Type() == git.ObjectBlob {
 19@@ -300,7 +300,7 @@ func walkTree(tree *git.Tree, branch string, curpath string, aggregate []*TreeIt
 20 				Size:  toPretty(entry.Size()),
 21 				Path:  fname,
 22 				Entry: entry,
 23-				URL:   template.URL(filepath.Join("/", "tree", branch, "item", fname)),
 24+				URL:   template.URL(filepath.Join("/", "tree", commitID, "item", fname)),
 25 			})
 26 		}
 27 	}
 28@@ -415,7 +415,7 @@ func (c *Config) writeHTMLTreeFiles(pageData *PageData, tree []*TreeItem) string
 29 				Contents: template.HTML(contents),
 30 				Path:     file.Path,
 31 			},
 32-			Subdir: filepath.Join("tree", pageData.RevData.RevName, "item", d),
 33+			Subdir: filepath.Join("tree", getShortID(pageData.RevData.ID), "item", d),
 34 		})
 35 	}
 36 	return readme
 37@@ -504,27 +504,22 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs []
 38 	}
 39 }
 40 
 41-func (c *Config) getCloneURL() template.URL {
 42-	url := fmt.Sprintf("https://%s/%s.git", c.CloneURL, c.RepoName)
 43-	return template.URL(url)
 44-}
 45-
 46-func (c *Config) getSummaryUrl() template.URL {
 47+func (c *Config) getSummaryURL() template.URL {
 48 	url := fmt.Sprintf("/%s/index.html", c.RepoName)
 49 	return template.URL(url)
 50 }
 51 
 52-func (c *Config) getRefsUrl() template.URL {
 53+func (c *Config) getRefsURL() template.URL {
 54 	url := fmt.Sprintf("/%s/refs.html", c.RepoName)
 55 	return template.URL(url)
 56 }
 57 
 58-func (c *Config) getTreeUrl(revn string) template.URL {
 59+func (c *Config) getTreeURL(revn string) template.URL {
 60 	url := fmt.Sprintf("/%s/tree/%s/index.html", c.RepoName, revn)
 61 	return template.URL(url)
 62 }
 63 
 64-func (c *Config) getLogsUrl(revn string) template.URL {
 65+func (c *Config) getLogsURL(revn string) template.URL {
 66 	url := fmt.Sprintf("/%s/logs/%s/index.html", c.RepoName, revn)
 67 	return template.URL(url)
 68 }
 69@@ -538,8 +533,8 @@ func (c *Config) getURLs() *SiteURLs {
 70 	return &SiteURLs{
 71 		RootURL:    c.HomeUrl,
 72 		CloneURL:   c.CloneURL,
 73-		RefsURL:    c.getRefsUrl(),
 74-		SummaryURL: c.getSummaryUrl(),
 75+		RefsURL:    c.getRefsURL(),
 76+		SummaryURL: c.getSummaryURL(),
 77 	}
 78 }
 79 
 80@@ -572,8 +567,8 @@ func (c *Config) writeRepo() *BranchOutput {
 81 		data := &RevData{
 82 			ID:      fullRevID,
 83 			RevName: revName,
 84-			TreeURL: c.getTreeUrl(revName),
 85-			LogURL:  c.getLogsUrl(revName),
 86+			TreeURL: c.getTreeURL(revName),
 87+			LogURL:  c.getLogsURL(revName),
 88 		}
 89 		if first == nil {
 90 			first = data
 91@@ -643,8 +638,8 @@ func (c *Config) writeRepo() *BranchOutput {
 92 	// use the first revision in our list to generate
 93 	// the root summary, logs, and tree the user can click
 94 	revData := &RevData{
 95-		TreeURL: c.getTreeUrl(first.RevName),
 96-		LogURL:  c.getLogsUrl(first.RevName),
 97+		TreeURL: c.getTreeURL(getShortID(first.ID)),
 98+		LogURL:  c.getLogsURL(getShortID(first.ID)),
 99 		RevName: first.RevName,
100 	}
101 
102@@ -702,7 +697,7 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
103 	bail(err)
104 
105 	entries := []*TreeItem{}
106-	treeEntries := walkTree(tree, pageData.RevData.RevName, "", entries)
107+	treeEntries := walkTree(tree, getShortID(pageData.RevData.ID), "", entries)
108 	for _, entry := range treeEntries {
109 		entry.Path = strings.TrimPrefix(entry.Path, "/")
110 
111@@ -725,14 +720,15 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
112 			entry.Summary = lc.Summary()
113 			entry.When = lc.Author.When.Format(time.RFC822)
114 		}
115-		entry.URL = template.URL(filepath.Join(
116+		fpath := filepath.Join(
117 			"/",
118 			c.RepoName,
119 			"tree",
120-			pageData.RevData.RevName,
121+			getShortID(pageData.RevData.ID),
122 			"item",
123 			fmt.Sprintf("%s.html", entry.Path),
124-		))
125+		)
126+		entry.URL = template.URL(fpath)
127 	}
128 
129 	c.Logger.Infof(