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-08-07 05:42:13
Commitb4ad3bf77335e5b65d31eac65fe46a3b6c80eeaf
Parent3e159d3dd66b838ce7d8089fe9b44c936df8f973

Add colour to commit view plusses and minuses

Diffstat

M res/repo/commit.html | 7 ++++++-
M res/repo/file.html | 2 +-
M res/repo/log.html | 1 +
M res/repo/refs.html | 2 ++
M res/repo/tree.html | 2 +-
M res/style.css | 4 ++--
M src/repo/commit.go | 30 ++++++++++++++++++++++++------

7 files changed, 37 insertions, 11 deletions

diff --git a/res/repo/commit.html b/res/repo/commit.html
index 1805337..fd18c70 100644
--- a/res/repo/commit.html
+++ b/res/repo/commit.html
@@ -18,11 +18,15 @@
 			{{range .DiffStat}}
 				<tr>
 					<td><a href="/{{$.Name}}/file/{{.Name}}">{{.Name}}</a></td>
-					<td>|</td><td>{{.Num}}</td><td>{{.Diff}}</td>
+					<td>|</td><td>{{.Num}}</td>
+					<td>
+						<span style="color: #008800;">{{.Plusses}}</span><!--
+						--><span style="color: #AA0000;">{{.Minuses}}</span>
+					</td>
 				</tr>
 			{{end}}
 		</table>
 		<p>{{.Summary}}</p>
-		<pre>{{.Diff}}</pre>
+		<pre style="tab-size: 4;">{{.Diff}}</pre>
 	</main>
 </body>
diff --git a/res/repo/file.html b/res/repo/file.html
index e97c399..d18ce17 100644
--- a/res/repo/file.html
+++ b/res/repo/file.html
@@ -6,7 +6,7 @@
 		{{.File}} ({{.Size}}) {{.Mode}}
 	</header><hr>
 	<main>
-		<table>
+		<table class="highlight-row">
 			{{if .Lines}}
 				{{range $i, $l := .Lines}}
 					<tr id="{{$i}}">
diff --git a/res/repo/log.html b/res/repo/log.html
index 6f458fd..539beeb 100644
--- a/res/repo/log.html
+++ b/res/repo/log.html
@@ -3,7 +3,7 @@
 <body>
 	<header>{{template "repo/header" .}}</header><hr>
 	<main>
-		<table>
+		<table class="highlight-row">
 			<thead>
 				<tr>
 					<td><b>Date</b></td>
diff --git a/res/repo/refs.html b/res/repo/refs.html
index bfed21c..9869b06 100644
--- a/res/repo/refs.html
+++ b/res/repo/refs.html
@@ -5,7 +5,7 @@
 	<main>
 	{{if .Branches}}
 		<h2>Branches</h2>
-		<table>
+		<table class="highlight-row">
 			<thead>
 				<tr>
 					<td><b>Name</b></td>
@@ -24,7 +24,7 @@
 	{{end}}
 	{{if .Tags}}
 		<h2>Tags</h2>
-		<table>
+		<table class="highlight-row">
 			<thead>
 				<tr>
 					<td><b>Name</b></td>
diff --git a/res/repo/tree.html b/res/repo/tree.html
index 331fbb4..f3c8eae 100644
--- a/res/repo/tree.html
+++ b/res/repo/tree.html
@@ -3,7 +3,7 @@
 <body>
 	<header>{{template "repo/header" .}}</header><hr>
 	<main>
-		<table>
+		<table class="highlight-row">
 			<thead>
 				<tr>
 					<td><b>Mode</b></td>
diff --git a/res/style.css b/res/style.css
index a39e700..8b0443f 100644
--- a/res/style.css
+++ b/res/style.css
@@ -9,7 +9,7 @@ hr { border: 0; height: 1rem; margin: 0; }
 table td { padding: 0 0.4rem; }
 table td:empty::after { content: "\00a0"; }
 
-main table tr:hover td { background-color: #222222; }
+.highlight-row tr:hover td { background-color: #222222; }
 
 table td.lnum { padding: 0; }
 table td.lnum a { color: inherit; display: block; padding: 0 0.4rem 0 0.8rem; }
@@ -38,7 +38,7 @@ form table textarea {
 
 .term-fg30 { color: #000000; } /* Black */
 .term-fg31 { color: #AA0000; } /* Red */
-.term-fg32 { color: #00AA00; } /* Green */
+.term-fg32 { color: #008800; } /* Green */
 .term-fg33 { color: #AA5500; } /* Yellow */
 .term-fg34 { color: #0000FF; } /* Blue */
 .term-fg35 { color: #AA00AA; } /* Magenta */
diff --git a/src/repo/commit.go b/src/repo/commit.go
index d43ce07..adaa868 100644
--- a/src/repo/commit.go
+++ b/src/repo/commit.go
@@ -6,6 +6,7 @@ import (
 	"html/template"
 	"log"
 	"net/http"
+	"path"
 	"strconv"
 	"strings"
 	"time"
@@ -36,7 +37,7 @@ func HandleCommit(w http.ResponseWriter, r *http.Request) {
 		Author, Date, Commit          string
 		Parents                       []string
 		MessageSubject, MessageBody   string
-		DiffStat                      []struct{ Name, Num, Diff string }
+		DiffStat                      []struct{ Name, Num, Plusses, Minuses string }
 		Summary                       string
 		Diff                          template.HTML
 	}{
@@ -51,6 +52,22 @@ func HandleCommit(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	ref, err := gr.Head()
+	if err != nil {
+		if !errors.Is(err, plumbing.ErrReferenceNotFound) {
+			log.Println("[/repo/log]", err.Error())
+			goit.HttpError(w, http.StatusInternalServerError)
+			return
+		}
+	} else {
+		if readme, _ := findReadme(gr, ref); readme != "" {
+			data.Readme = path.Join("/", repo.Name, "file", readme)
+		}
+		if licence, _ := findLicence(gr, ref); licence != "" {
+			data.Licence = path.Join("/", repo.Name, "file", licence)
+		}
+	}
+
 	commit, err := gr.CommitObject(plumbing.NewHash(mux.Vars(r)["hash"]))
 	if errors.Is(err, plumbing.ErrObjectNotFound) {
 		goit.HttpError(w, http.StatusNotFound)
@@ -82,15 +99,16 @@ func HandleCommit(w http.ResponseWriter, r *http.Request) {
 
 	var files, additions, deletions int = len(st), 0, 0
 	for _, s := range st {
-		/* TODO handle renames and colored plusses and minuses */
-		f := struct{ Name, Num, Diff string }{Name: s.Name}
+		/* TODO handle renames */
+		f := struct{ Name, Num, Plusses, Minuses string }{Name: s.Name}
 		f.Num = strconv.FormatInt(int64(s.Addition+s.Deletion), 10)
 
 		if s.Addition+s.Deletion > 80 {
-			f.Diff = strings.Repeat("+", (s.Addition*80)/(s.Addition+s.Deletion))
-			f.Diff += strings.Repeat("-", (s.Deletion*80)/(s.Addition+s.Deletion))
+			f.Plusses = strings.Repeat("+", (s.Addition*80)/(s.Addition+s.Deletion))
+			f.Minuses = strings.Repeat("-", (s.Deletion*80)/(s.Addition+s.Deletion))
 		} else {
-			f.Diff = strings.Repeat("+", s.Addition) + strings.Repeat("-", s.Deletion)
+			f.Plusses = strings.Repeat("+", s.Addition)
+			f.Minuses = strings.Repeat("-", s.Deletion)
 		}
 
 		data.DiffStat = append(data.DiffStat, f)