Goit

Simple and lightweight Git web server
Mirror of https://github.com/Jamozed/Goit
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2023-12-23 06:40:09
Commit2e13a8c43874e4e8cf38cf75da5a31ec8d7f170f
Parent2af4be7f45a151bffe8c6c1b1bcb9d99c1cde57e

Display upstream URL for mirror repositories

Diffstat

M res/repo/header.html | 14 +++++++++++---
M src/repo/commit.go | 27 +++++++++++----------------
M src/repo/edit.go | 13 +++----------
M src/repo/file.go | 18 +++++++-----------
M src/repo/log.go | 18 ++++++++----------
M src/repo/refs.go | 13 ++++---------
M src/repo/repo.go | 15 ++++++++++++++-
M src/repo/tree.go | 19 ++++++++-----------

8 files changed, 66 insertions, 71 deletions

diff --git a/res/repo/header.html b/res/repo/header.html
index 38b5c48..ca1a4cb 100644
--- a/res/repo/header.html
+++ b/res/repo/header.html
@@ -1,15 +1,17 @@
 <table>
 	<tr>
-		<td rowspan="2">
-			<a href="/"><img style="max-height: 24px;" src="/static/favicon.png"></a>
-		</td>
-		<td><h1 style="display: inline;">{{.Name}}</h1>{{if .IsMirror}} (mirror){{end}}</td>
-	</tr>
-	<tr>
-		<td>{{.Description}}</td>
+		<td rowspan="2"><a href="/"><img style="max-height: 24px;" src="/static/favicon.png"></a></td>
+		<td><h1 style="display: inline;">{{.Name}}</h1></td>
 	</tr>
+	{{if .Description}}<tr><td>{{.Description}}</td></tr>{{end}}
+	{{if .Mirror}}
+		<tr>
+			{{if .Description}}<td></td>{{end}}
+			<td>Mirror of <a href="{{.Mirror}}">{{.Mirror}}</a></td>
+		</tr>
+	{{end}}
 	<tr>
-		<td></td>
+		{{if or (.Description) (.Mirror)}}<td></td>{{end}}
 		<td>git clone <a href="{{.Url}}">{{.Url}}</a></td>
 	</tr>
 	<tr>
diff --git a/src/repo/commit.go b/src/repo/commit.go
index 4c31712..919d8f9 100644
--- a/src/repo/commit.go
+++ b/src/repo/commit.go
@@ -15,7 +15,6 @@ import (
 	"time"
 
 	"github.com/Jamozed/Goit/src/goit"
-	"github.com/Jamozed/Goit/src/util"
 	"github.com/buildkite/terminal-to-html/v3"
 	"github.com/go-chi/chi/v5"
 	"github.com/go-git/go-git/v5"
@@ -44,20 +43,17 @@ func HandleCommit(w http.ResponseWriter, r *http.Request) {
 	}
 
 	data := struct {
-		Title, Name, Description, Url string
-		Readme, Licence               string
-		Author, Date, Commit          string
-		Parents                       []string
-		MessageSubject, MessageBody   string
-		Stats                         []stat
-		Summary                       string
-		Diff                          template.HTML
-		Editable, IsMirror            bool
+		HeaderFields
+		Title                       string
+		Author, Date, Commit        string
+		Parents                     []string
+		MessageSubject, MessageBody string
+		Stats                       []stat
+		Summary                     string
+		Diff                        template.HTML
 	}{
-		Title: repo.Name + " - Log", Name: repo.Name, Description: repo.Description,
-		Url:      util.If(goit.Conf.UsesHttps, "https://", "http://") + r.Host + "/" + repo.Name,
-		Editable: (auth && repo.OwnerId == user.Id),
-		IsMirror: repo.IsMirror,
+		Title:        repo.Name + " - Log",
+		HeaderFields: GetHeaderFields(auth, user, repo, r.Host),
 	}
 
 	gr, err := git.PlainOpen(goit.RepoPath(repo.Name, true))
diff --git a/src/repo/edit.go b/src/repo/edit.go
index e007c9d..a12c81c 100644
--- a/src/repo/edit.go
+++ b/src/repo/edit.go
@@ -15,7 +15,6 @@ import (
 
 	"github.com/Jamozed/Goit/src/cron"
 	"github.com/Jamozed/Goit/src/goit"
-	"github.com/Jamozed/Goit/src/util"
 	"github.com/go-chi/chi/v5"
 	"github.com/go-git/go-git/v5"
 	"github.com/go-git/go-git/v5/plumbing"
@@ -57,9 +56,8 @@ func HandleEdit(w http.ResponseWriter, r *http.Request) {
 	}
 
 	data := struct {
-		Title, Name, Description, Url string
-		Readme, Licence               string
-		Editable, IsMirror            bool
+		HeaderFields
+		Title string
 
 		Edit struct {
 			Id, Owner, Name, Description, Upstream string
@@ -72,12 +70,8 @@ func HandleEdit(w http.ResponseWriter, r *http.Request) {
 
 		CsrfField template.HTML
 	}{
-		Title:       "Repository - Edit",
-		Name:        repo.Name,
-		Description: repo.Description,
-		Url:         util.If(goit.Conf.UsesHttps, "https://", "http://") + r.Host + "/" + repo.Name,
-		Editable:    (auth && repo.OwnerId == user.Id),
-		IsMirror:    repo.IsMirror,
+		Title:        "Repository - Edit",
+		HeaderFields: GetHeaderFields(auth, user, repo, r.Host),
 
 		CsrfField: csrf.TemplateField(r),
 	}
diff --git a/src/repo/file.go b/src/repo/file.go
index 0ec7a1b..f45935a 100644
--- a/src/repo/file.go
+++ b/src/repo/file.go
@@ -41,18 +41,14 @@ func HandleFile(w http.ResponseWriter, r *http.Request) {
 	}
 
 	data := struct {
-		Title, Name, Description, Url string
-		Readme, Licence               string
-		Path, LineC, Size, Mode       string
-		Lines                         []string
-		Body                          string
-		Editable, IsMirror            bool
-		HtmlPath                      template.HTML
+		HeaderFields
+		Title, Path, LineC, Size, Mode string
+		Lines                          []string
+		Body                           string
+		HtmlPath                       template.HTML
 	}{
-		Title: repo.Name + " - File", Name: repo.Name, Description: repo.Description,
-		Url:      util.If(goit.Conf.UsesHttps, "https://", "http://") + r.Host + "/" + repo.Name,
-		Editable: (auth && repo.OwnerId == user.Id),
-		IsMirror: repo.IsMirror,
+		Title:        repo.Name + " - File",
+		HeaderFields: GetHeaderFields(auth, user, repo, r.Host),
 	}
 
 	gr, err := git.PlainOpen(goit.RepoPath(repo.Name, true))
diff --git a/src/repo/log.go b/src/repo/log.go
index f36200e..45b38f4 100644
--- a/src/repo/log.go
+++ b/src/repo/log.go
@@ -54,16 +54,14 @@ func HandleLog(w http.ResponseWriter, r *http.Request) {
 
 	type row struct{ Hash, Date, Message, Author, Files, Additions, Deletions string }
 	data := struct {
-		Title, Name, Description, Url string
-		Readme, Licence               string
-		Commits                       []row
-		Editable, IsMirror            bool
-		Page, PrevOffset, NextOffset  int64
+		HeaderFields
+		Title                        string
+		Commits                      []row
+		Page, PrevOffset, NextOffset int64
 	}{
-		Title: repo.Name + " - Log", Name: repo.Name, Description: repo.Description,
-		Url:        util.If(goit.Conf.UsesHttps, "https://", "http://") + r.Host + "/" + repo.Name,
-		Editable:   (auth && repo.OwnerId == user.Id),
-		IsMirror:   repo.IsMirror,
+		Title:        repo.Name + " - Log",
+		HeaderFields: GetHeaderFields(auth, user, repo, r.Host),
+
 		Page:       offset/PAGE + 1,
 		PrevOffset: util.Max(offset-PAGE, -1),
 		NextOffset: offset + PAGE,
diff --git a/src/repo/refs.go b/src/repo/refs.go
index 152df60..f1454af 100644
--- a/src/repo/refs.go
+++ b/src/repo/refs.go
@@ -13,7 +13,6 @@ import (
 	"time"
 
 	"github.com/Jamozed/Goit/src/goit"
-	"github.com/Jamozed/Goit/src/util"
 	"github.com/go-chi/chi/v5"
 	"github.com/go-git/go-git/v5"
 	"github.com/go-git/go-git/v5/plumbing"
@@ -38,15 +37,12 @@ func HandleRefs(w http.ResponseWriter, r *http.Request) {
 
 	type row struct{ Name, Message, Author, LastCommit, Hash string }
 	data := struct {
-		Title, Name, Description, Url string
-		Readme, Licence               string
-		Branches, Tags                []row
-		Editable, IsMirror            bool
+		HeaderFields
+		Title          string
+		Branches, Tags []row
 	}{
-		Title: repo.Name + " - References", Name: repo.Name, Description: repo.Description,
-		Url:      util.If(goit.Conf.UsesHttps, "https://", "http://") + r.Host + "/" + repo.Name,
-		Editable: (auth && repo.OwnerId == user.Id),
-		IsMirror: repo.IsMirror,
+		Title:        repo.Name + " - References",
+		HeaderFields: GetHeaderFields(auth, user, repo, r.Host),
 	}
 
 	gr, err := git.PlainOpen(goit.RepoPath(repo.Name, true))
diff --git a/src/repo/repo.go b/src/repo/repo.go
index aa799a7..fa38468 100644
--- a/src/repo/repo.go
+++ b/src/repo/repo.go
@@ -6,13 +6,25 @@ package repo
 import (
 	"regexp"
 
+	"github.com/Jamozed/Goit/src/goit"
+	"github.com/Jamozed/Goit/src/util"
 	"github.com/go-git/go-git/v5"
 	"github.com/go-git/go-git/v5/plumbing"
 )
 
 type HeaderFields struct {
-	Name, Description, Url, Readme, Licence string
-	Editable                                bool
+	Name, Description, Url  string
+	Readme, Licence, Mirror string
+	Editable                bool
+}
+
+func GetHeaderFields(auth bool, user *goit.User, repo *goit.Repo, host string) HeaderFields {
+	return HeaderFields{
+		Name: repo.Name, Description: repo.Description,
+		Url:      util.If(goit.Conf.UsesHttps, "https://", "http://") + host + "/" + repo.Name,
+		Editable: (auth && repo.OwnerId == user.Id),
+		Mirror:   util.If(repo.IsMirror, repo.Upstream, ""),
+	}
 }
 
 var readmePattern = regexp.MustCompile(`(?i)^readme(?:\.?(?:md|txt))?$`)
diff --git a/src/repo/tree.go b/src/repo/tree.go
index 598e3bd..ea735ef 100644
--- a/src/repo/tree.go
+++ b/src/repo/tree.go
@@ -44,17 +44,13 @@ func HandleTree(w http.ResponseWriter, r *http.Request) {
 		IsFile, B                       bool
 	}
 	data := struct {
-		Title, Name, Description, Url string
-		Readme, Licence               string
-		Path, Size                    string
-		Files                         []row
-		Editable, IsMirror            bool
-		HtmlPath                      template.HTML
+		HeaderFields
+		Title, Path, Size string
+		Files             []row
+		HtmlPath          template.HTML
 	}{
-		Title: repo.Name + " - Tree", Name: repo.Name, Description: repo.Description,
-		Url:      util.If(goit.Conf.UsesHttps, "https://", "http://") + r.Host + "/" + repo.Name,
-		Editable: (auth && repo.OwnerId == user.Id),
-		IsMirror: repo.IsMirror,
+		Title:        repo.Name + " - Tree",
+		HeaderFields: GetHeaderFields(auth, user, repo, r.Host),
 	}
 
 	parts := strings.Split(tpath, "/")
@@ -67,6 +63,7 @@ func HandleTree(w http.ResponseWriter, r *http.Request) {
 	}
 	htmlPath += parts[len(parts)-1]
 
+	data.Path = tpath
 	data.HtmlPath = template.HTML(htmlPath)
 
 	gr, err := git.PlainOpen(goit.RepoPath(repo.Name, true))