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
License and readme links in metadata
author
Ben Vogt <[email protected]>
date
2023-04-04 21:24:13
stats
5 file(s) changed, 82 insertions(+), 8 deletions(-)
files
LICENSE
README.md
log.go
main.go
templates/partials.html
  1diff --git a/LICENSE b/LICENSE
  2new file mode 100644
  3index 0000000..d4ae14f
  4--- /dev/null
  5+++ b/LICENSE
  6@@ -0,0 +1,21 @@
  7+The MIT License (MIT)
  8+
  9+Copyright (c) 2023 Ben Vogt
 10+
 11+Permission is hereby granted, free of charge, to any person obtaining a copy
 12+of this software and associated documentation files (the "Software"), to deal
 13+in the Software without restriction, including without limitation the rights
 14+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 15+copies of the Software, and to permit persons to whom the Software is
 16+furnished to do so, subject to the following conditions:
 17+
 18+The above copyright notice and this permission notice shall be included in all
 19+copies or substantial portions of the Software.
 20+
 21+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 22+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 23+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 24+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 25+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 26+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 27+SOFTWARE.
 28\ No newline at end of file
 29diff --git a/README.md b/README.md
 30index 8ecd3cf..dbd365b 100644
 31--- a/README.md
 32+++ b/README.md
 33@@ -6,6 +6,5 @@ Git static host repo.
 34 
 35 Tasks to do:
 36 
 37-* Render full commit diff.
 38 * README and LICENSE linking in main page.
 39 * Multi-repo namespacing.
 40\ No newline at end of file
 41diff --git a/log.go b/log.go
 42index 6d3088d..79675c8 100644
 43--- a/log.go
 44+++ b/log.go
 45@@ -20,8 +20,10 @@ type LogPageCommit struct {
 46 }
 47 
 48 type LogPage struct {
 49-	RepoData RepoData
 50-	Commits  []LogPageCommit
 51+	RepoData   RepoData
 52+	HasReadMe  bool
 53+	ReadMePath string
 54+	Commits    []LogPageCommit
 55 }
 56 
 57 func (mi *LogPage) Render(t *template.Template) {
 58@@ -39,7 +41,6 @@ func RenderLogPage(r *git.Repository) {
 59 	checkErr(err)
 60 	cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
 61 	checkErr(err)
 62-
 63 	err = cIter.ForEach(func(c *object.Commit) error {
 64 		stats, err := c.Stats()
 65 		added := 0
 66@@ -61,7 +62,6 @@ func RenderLogPage(r *git.Repository) {
 67 		})
 68 		return nil
 69 	})
 70-
 71 	checkErr(err)
 72 	(&LogPage{
 73 		RepoData: config.RepoData,
 74diff --git a/main.go b/main.go
 75index 8997465..fd6bba8 100644
 76--- a/main.go
 77+++ b/main.go
 78@@ -99,10 +99,14 @@ func DefaultConfig() Config {
 79 }
 80 
 81 type RepoData struct {
 82-	Name        string
 83-	GitURL      string
 84-	Description string
 85-	BaseURL     string
 86+	Name            string
 87+	GitURL          string
 88+	Description     string
 89+	BaseURL         string
 90+	HasReadMe       bool
 91+	ReadMePath      string
 92+	HasLicenseFile  bool
 93+	LicenseFilePath string
 94 }
 95 
 96 func main() {
 97@@ -132,6 +136,10 @@ func main() {
 98 	debug("clone = %v", config.CloneDir)
 99 	debug("base-url = %v", config.BaseURL)
100 	r := CloneAndInfo()
101+	config.RepoData.ReadMePath = getReadmePath()
102+	config.RepoData.HasReadMe = config.RepoData.ReadMePath != ""
103+	config.RepoData.LicenseFilePath = getLicenseFilePath()
104+	config.RepoData.HasLicenseFile = config.RepoData.LicenseFilePath != ""
105 	RenderLogPage(r)
106 	RenderAllCommitPages(r)
107 	RenderAllFilesPage()
108@@ -185,3 +193,35 @@ func highlight(pathOrExtension string, data *string) string {
109 	checkErr(err)
110 	return buf.String()
111 }
112+
113+func getReadmePath() string {
114+	for _, file := range []string{
115+		"readme.md",
116+		"README.md",
117+		"readme.txt",
118+		"README.txt",
119+		"README",
120+	} {
121+		if stat, err := os.Stat(path.Join(config.CloneDir, file)); err == nil {
122+			return stat.Name()
123+		}
124+	}
125+	return ""
126+}
127+
128+func getLicenseFilePath() string {
129+	for _, file := range []string{
130+		"license-mit",
131+		"LICENSE-MIT",
132+		"license.md",
133+		"LICENSE.md",
134+		"license.txt",
135+		"LICENSE.txt",
136+		"LICENSE",
137+	} {
138+		if stat, err := os.Stat(path.Join(config.CloneDir, file)); err == nil {
139+			return stat.Name()
140+		}
141+	}
142+	return ""
143+}
144diff --git a/templates/partials.html b/templates/partials.html
145index a605666..d263e5d 100644
146--- a/templates/partials.html
147+++ b/templates/partials.html
148@@ -35,6 +35,18 @@
149           <a href="{{ .BaseURL }}log.html">Log</a>
150           |
151           <a href="{{ .BaseURL }}files.html">Files</a>
152+          {{ if .HasReadMe }}
153+          |
154+          <a href="{{ .BaseURL }}files/{{ .ReadMePath }}/index.html">
155+            {{ .ReadMePath }}
156+          </a>
157+          {{ end }}
158+          {{ if .HasLicenseFile }}
159+          |
160+          <a href="{{ .BaseURL }}files/{{ .LicenseFilePath }}/index.html">
161+            {{ .LicenseFilePath }}
162+          </a>
163+          {{ end }}
164         </td>
165       </tr>
166     </tbody>