gshr
git static host repo -- generates static html for repos
git clone https://git.vogt.world/gshr.git
Log | Files | README.md | LICENSE
← Commit log
commit
message
Naming conventions, documentation
author
Ben Vogt <[email protected]>
date
2023-04-10 18:57:21
stats
8 file(s) changed, 67 insertions(+), 32 deletions(-)
files
README.md
commit.go
config.go
file.go
files.go
index.go
log.go
main.go
  1diff --git a/README.md b/README.md
  2index d3ee0ca..ba78366 100644
  3--- a/README.md
  4+++ b/README.md
  5@@ -73,18 +73,20 @@ alt_link = "https://github.com/vogtb/gshr"
  6 ## Output
  7 
  8 ```text
  9-{output_dir}
 10+{output_dir}/
 11   index.html
 12-  {repo_name}
 13+  {repo_name}/
 14     log.html
 15-    commits
 16+    commits/
 17       {hash}/commit.html
 18     files.html
 19-    files
 20+    files/
 21       {full_file_name}/file.html
 22+  {repo_name}.git
 23+      {...raw git file data}
 24 ```
 25 
 26-For example:
 27+Example:
 28 
 29 ```text
 30 output
 31@@ -118,6 +120,25 @@ output
 32 │   ├── files.html
 33 │   └── log.html
 34 ├── gshr.css
 35+├── gshr.git
 36+│   ├── HEAD
 37+│   ├── config
 38+│   ├── index
 39+│   ├── info
 40+│   │   └── refs
 41+│   ├── objects
 42+│   │   ├── info
 43+│   │   │   └── packs
 44+│   │   └── pack
 45+│   │       ├── pack-a6e75f15316a2d809290159b8bdc88303c8090cb.idx
 46+│   │       └── pack-a6e75f15316a2d809290159b8bdc88303c8090cb.pack
 47+│   └── refs
 48+│       ├── heads
 49+│       │   └── main
 50+│       ├── remotes
 51+│       │   └── origin
 52+│       │       └── main
 53+│       └── tags
 54 └── index.html
 55 ```
 56 
 57diff --git a/commit.go b/commit.go
 58index 8251934..3dc97e8 100644
 59--- a/commit.go
 60+++ b/commit.go
 61@@ -12,7 +12,7 @@ import (
 62 	"github.com/go-git/go-git/v5/plumbing/object"
 63 )
 64 
 65-type CommitPage struct {
 66+type commitPage struct {
 67 	RepoData        repoData
 68 	Author          string
 69 	AuthorEmail     string
 70@@ -26,7 +26,7 @@ type CommitPage struct {
 71 	DiffContent     template.HTML
 72 }
 73 
 74-func (c *CommitPage) RenderPage(t *template.Template) {
 75+func (c *commitPage) renderPage(t *template.Template) {
 76 	debug("commit %v %v", c.RepoData.Name, c.Hash)
 77 	err := os.MkdirAll(path.Join(args.OutputDir, c.RepoData.Name, "commit", c.Hash), 0755)
 78 	checkErr(err)
 79@@ -36,7 +36,7 @@ func (c *CommitPage) RenderPage(t *template.Template) {
 80 	checkErr(err)
 81 }
 82 
 83-func RenderAllCommitPages(data repoData, r *git.Repository) {
 84+func renderAllCommitPages(data repoData, r *git.Repository) {
 85 	t, err := template.ParseFS(htmlTemplates, "template.commit.html", "template.partials.html")
 86 	checkErr(err)
 87 	ref, err := r.Head()
 88@@ -96,7 +96,7 @@ func RenderAllCommitPages(data repoData, r *git.Repository) {
 89 			deleted += stat.Deletion
 90 		}
 91 		checkErr(err)
 92-		(&CommitPage{
 93+		(&commitPage{
 94 			RepoData:        data,
 95 			Author:          c.Author.Name,
 96 			AuthorEmail:     c.Author.Email,
 97@@ -108,7 +108,7 @@ func RenderAllCommitPages(data repoData, r *git.Repository) {
 98 			LinesDeleted:    deleted,
 99 			FilesChanged:    filesChanged,
100 			DiffContent:     diffContent,
101-		}).RenderPage(t)
102+		}).renderPage(t)
103 		return nil
104 	})
105 	checkErr(err)
106diff --git a/config.go b/config.go
107index 819d20a..4cffdfd 100644
108--- a/config.go
109+++ b/config.go
110@@ -22,6 +22,7 @@ func (c *config) validate() {
111 		if duplicate {
112 			checkErr(errors.New(fmt.Sprintf("duplicate repo name: '%s'", r.Name)))
113 		}
114+		r.validate()
115 		names[r.Name] = true
116 	}
117 }
118diff --git a/file.go b/file.go
119index c275856..e723d09 100644
120--- a/file.go
121+++ b/file.go
122@@ -10,7 +10,7 @@ import (
123 	"strings"
124 )
125 
126-type FilePage struct {
127+type filePage struct {
128 	RepoData       repoData
129 	Mode           string
130 	Name           string
131@@ -23,7 +23,7 @@ type FilePage struct {
132 	Content        template.HTML
133 }
134 
135-func (f *FilePage) RenderPage(t *template.Template) {
136+func (f *filePage) renderPage(t *template.Template) {
137 	debug("file %v %v", f.RepoData.Name, f.Name)
138 	err := os.MkdirAll(f.DestinationDir, 0777)
139 	checkErr(err)
140@@ -35,7 +35,7 @@ func (f *FilePage) RenderPage(t *template.Template) {
141 	checkErr(err)
142 }
143 
144-func RenderSingleFilePages(data repoData) {
145+func renderIndividualFilePages(data repoData) {
146 	t, err := template.ParseFS(htmlTemplates, "template.file.html", "template.partials.html")
147 	checkErr(err)
148 	err = filepath.Walk(data.cloneDir(), func(filename string, info fs.FileInfo, err error) error {
149@@ -62,7 +62,7 @@ func RenderSingleFilePages(data repoData) {
150 				checkErr(err)
151 				content = template.HTML(highlighted)
152 			}
153-			(&FilePage{
154+			(&filePage{
155 				RepoData:       data,
156 				Mode:           info.Mode().String(),
157 				Size:           fmt.Sprintf("%v", info.Size()),
158@@ -73,7 +73,7 @@ func RenderSingleFilePages(data repoData) {
159 				Destination:    outputName,
160 				DestinationDir: destDir,
161 				Content:        content,
162-			}).RenderPage(t)
163+			}).renderPage(t)
164 		}
165 		return nil
166 	})
167diff --git a/files.go b/files.go
168index 5254499..b932c49 100644
169--- a/files.go
170+++ b/files.go
171@@ -30,7 +30,7 @@ func (f *FilesPage) RenderPage(t *template.Template) {
172 	checkErr(err)
173 }
174 
175-func RenderAllFilesPage(data repoData) {
176+func renderAllFilesPage(data repoData) {
177 	t, err := template.ParseFS(htmlTemplates, "template.files.html", "template.partials.html")
178 	checkErr(err)
179 	files := make([]FileOverview, 0)
180diff --git a/index.go b/index.go
181index e27b16a..eebd00c 100644
182--- a/index.go
183+++ b/index.go
184@@ -11,7 +11,7 @@ type IndexPage struct {
185 	Repos    []repoData
186 }
187 
188-func (l *IndexPage) RenderPage(t *template.Template) {
189+func (l *IndexPage) renderPage(t *template.Template) {
190 	debug("index for '%v'", l.HeadData.SiteName)
191 	output, err := os.Create(path.Join(args.OutputDir, "index.html"))
192 	checkErr(err)
193@@ -19,7 +19,7 @@ func (l *IndexPage) RenderPage(t *template.Template) {
194 	checkErr(err)
195 }
196 
197-func RenderIndexPage(repos []repoData) {
198+func renderIndexPage(repos []repoData) {
199 	t, err := template.ParseFS(htmlTemplates, "template.index.html", "template.partials.html")
200 	checkErr(err)
201 	(&IndexPage{
202@@ -28,5 +28,5 @@ func RenderIndexPage(repos []repoData) {
203 			SiteName: conf.Site.Name,
204 		},
205 		Repos: repos,
206-	}).RenderPage(t)
207+	}).renderPage(t)
208 }
209diff --git a/log.go b/log.go
210index 870c35b..8f7b6fe 100644
211--- a/log.go
212+++ b/log.go
213@@ -34,7 +34,7 @@ func (l *LogPage) RenderPage(t *template.Template) {
214 	checkErr(err)
215 }
216 
217-func RenderLogPage(data repoData, r *git.Repository) {
218+func renderLogPage(data repoData, r *git.Repository) {
219 	t, err := template.ParseFS(htmlTemplates, "template.log.html", "template.partials.html")
220 	checkErr(err)
221 	commits := make([]LogPageCommit, 0)
222diff --git a/main.go b/main.go
223index 1c48629..23735cc 100644
224--- a/main.go
225+++ b/main.go
226@@ -27,7 +27,7 @@ var css []byte
227 //go:embed favicon.ico
228 var favicon []byte
229 
230-var args cmArgs
231+var args cmdArgs
232 
233 var conf config
234 
235@@ -35,28 +35,28 @@ var stt settings
236 
237 func main() {
238 	var r *git.Repository = &git.Repository{}
239-	Init()
240+	initialize()
241 	allRepoData := []repoData{}
242 	for _, repo := range conf.Repos {
243-		data := CloneAndGetData(repo, r)
244+		data := cloneAndGetData(repo, r)
245 		allRepoData = append(allRepoData, data)
246-		RenderLogPage(data, r)
247-		RenderAllCommitPages(data, r)
248-		RenderAllFilesPage(data)
249-		RenderSingleFilePages(data)
250+		renderLogPage(data, r)
251+		renderAllCommitPages(data, r)
252+		renderAllFilesPage(data)
253+		renderIndividualFilePages(data)
254 	}
255-	RenderIndexPage(allRepoData)
256+	renderIndexPage(allRepoData)
257 	renderAssets()
258 	for _, repo := range conf.Repos {
259 		hostRepo(repo)
260 	}
261 }
262 
263-func Init() {
264+func initialize() {
265 	log.SetFlags(0)
266 	log.SetOutput(new(logger))
267-	args = DefaultCmdArgs()
268-	stt = DefaultSettings()
269+	args = defaultCmdArgs()
270+	stt = defaultSettings()
271 	pwd, err := os.Getwd()
272 	checkErr(err)
273 	args.Wd = pwd
274@@ -84,9 +84,10 @@ func Init() {
275 	conf = parseConfig(configString)
276 	debug("base_url '%v'", conf.Site.BaseURL)
277 	debug("site_name '%v'", conf.Site.Name)
278+	conf.validate()
279 }
280 
281-func CloneAndGetData(repo repoConfig, r *git.Repository) repoData {
282+func cloneAndGetData(repo repoConfig, r *git.Repository) repoData {
283 	err := os.MkdirAll(repo.cloneDir(), 0755)
284 	checkErr(err)
285 	err = os.MkdirAll(path.Join(args.OutputDir, repo.Name), 0755)
286@@ -124,7 +125,7 @@ func hostRepo(data repoConfig) {
287 	renamed := path.Join(args.OutputDir, fmt.Sprintf("%v.git", data.Name))
288 	repoFiles := path.Join(args.OutputDir, data.Name, "git")
289 	final := path.Join(args.OutputDir, fmt.Sprintf("%v.git", data.Name))
290-	debug("renaming '%v', new %v", data.Name, renamed)
291+	debug("renaming '%v' to %v", data.Name, renamed)
292 	checkErr(os.Rename(old, renamed))
293 	debug("running 'git update-server-info' in %v", renamed)
294 	cmd := exec.Command("git", "update-server-info")
295@@ -184,15 +185,15 @@ func highlight(pathOrExtension string, data *string) string {
296 	return buf.String()
297 }
298 
299-type cmArgs struct {
300+type cmdArgs struct {
301 	Silent     bool
302 	Wd         string
303 	ConfigPath string
304 	OutputDir  string
305 }
306 
307-func DefaultCmdArgs() cmArgs {
308-	return cmArgs{
309+func defaultCmdArgs() cmdArgs {
310+	return cmdArgs{
311 		Silent:     true,
312 		ConfigPath: "",
313 		OutputDir:  "",
314@@ -206,7 +207,7 @@ type settings struct {
315 	AllowedReadMeFiles  map[string]bool
316 }
317 
318-func DefaultSettings() settings {
319+func defaultSettings() settings {
320 	return settings{
321 		TextExtensions: map[string]bool{
322 			".c":              true,