noxz-sites

A collection of a builder and various scripts creating the noxz.tech sites
git clone https://noxz.tech/git/noxz-sites.git
noxz-sites

commit: e127b93ba80b8460deecc217763bb1678f07c75e
parent: 12fddfb30d49e02fb9392a12f2397ea65caade67
author: Chris Noxz <chris@noxz.tech>
date:   Sun, 12 Mar 2023 17:24:36 +0100
add feature to include commit diffs in git repo
Mbuild195+++++++++++++++-----
Mgit.noxz.tech/pub/style.css23++-
2 files changed, 174 insertions(+), 44 deletions(-)
diff --git a/build b/build
@@ -542,6 +542,131 @@ render_main() {
 	print_done
 }
 
+render_git_commit() {
+	git_commit_files="$(git -C "${1}" whatchanged "${2}"^! | grep '^:.*$' | cut -d' ' -f5 | sed 's/\t/:/g')"
+	git -C "${1}" show --stat=1000 --stat-graph-width=20 "${2}"             \
+		--format="format:%H%n%P%n%aN%n%aE%n%aD%n%B%H"                       \
+		| sed 's/{\(.*\) => \(.*\)}/\1/g'                                   \
+		| html_encode | awk -v flist="${git_commit_files}" -v base="//${1}" '
+	BEGIN {
+		marker=""; files=0; split(flist, farr, "\n");
+		for (i in farr) {
+			split(farr[i], ftmp, ":");
+			farr[i,1] = substr(ftmp[1], 1, 1);
+			farr[i,2] = ftmp[2];
+			farr[i,3] = ftmp[3];
+		}
+	}
+	{
+		switch (NR) {
+		case 1:
+			marker=$0
+			printf "<pre>\n"
+			printf "<b>commit</b>: <a href=\"%s/commit/%s.html\">%s</a>\n",base,$0,$0
+		break
+		case 2:
+			printf "<b>parent</b>: <a href=\"%s/commit/%s.html\">%s</a>\n",base,$0,$0
+		break
+		case 3:
+			printf "<b>author</b>: %s",$0
+		break
+		case 4:
+			if ($0 != "") { printf " &lt;<a href=\"mailto:%s\">%s</a>&gt;",$0,$0 }
+			printf "\n"
+		break
+		case 5:
+			printf "<b>date</b>:   %s\n",$0
+			printf "</pre>\n"
+		break
+		case 6:
+			printf "<pre class=\"body\">\n"
+		default:
+			if (files) {
+				for (i in farr) {
+					if ($1 == farr[i,2]) {
+						printf "<tr>"
+						printf "<td>%s</td><td><a href=\"#f%d\">%s</a>",farr[i,1],i,farr[i,2]
+						if (farr[i,3] != "") {
+							printf " &rarr; <a href=\"#f%d\">%s</a>",i,farr[i,3]
+						}
+						printf "</td>",$3
+						printf "<td>%s</td>",$3
+						printf "<td><span class=\"i\">"
+						p=1
+						for (j = 1; j <= length($4); j++) {
+							if (p && substr($4, j, 1) != "+") {
+								p=0; printf "</span><span class=\"d\">"
+							}
+							printf "%s",substr($4,j,1)
+						}
+						printf "</span></td>"
+						print "</tr>"
+						next
+					}
+				}
+				sub(/^[ \t\r\n]+/, "", $0)
+				printf "</table>\n%s\n",$0
+			} else {
+				if (marker != "" && marker == $0) { printf "</pre>\n<table>\n"; files=1 }
+				else { print }
+			}
+		break
+		}
+	}'
+	echo "<hr />"
+	git -C "${1}" show "${2}" --format="" | html_encode | awk -v flist="${git_commit_files}" '
+	BEGIN {
+		indiff=0; files=0; h=0; l=0 split(flist, farr, "\n");
+		for (i in farr) {
+			split(farr[i], ftmp, ":");
+			farr[i,1] = ftmp[1];
+			farr[i,2] = ftmp[2];
+		}
+		print "<pre>"
+	}
+	/^diff --git/ {
+		indiff=1
+		if (match($0, /^diff --git a\/(.*) b\/(.*)$/, m)) {
+			for (i in farr) {
+				if (m[1] == farr[i,2]) {
+					printf "<b>diff --git"
+					printf " a/<a id=\"f%d\" href=\"#f%d\">%s</a>",i,i,m[1]
+					printf " b/<a href=\"#f%d\">%s</a>",i,m[2]
+					printf "</b>\n"
+					if (m[1] != m[2]) {
+						printf "<b class=\"r\">rename %s &rarr; ",m[1]
+						printf "%s</b>\n",m[2]
+					}
+					next
+				}
+			}
+		}
+		next
+	}
+	/^@@/ {
+		indiff=0
+		printf "<a href=\"#h%d\" id=\"h%d\" class=\"h\">%s</a>\n",h,h++,$0
+		next
+	}
+	/^\+.*$/ {
+		if (indiff) { next }
+		printf "<a href=\"#l%d\" id=\"l%d\" class=\"i\">%s</a>\n",l,l++,$0
+		next
+	}
+	/^\-.*$/ {
+		if (indiff) { next }
+		printf "<a href=\"#l%d\" id=\"l%d\" class=\"d\">%s</a>\n",l,l++,$0
+		next
+	}
+	{
+		if (indiff) { next }
+		print
+	}
+	END {
+		print "</pre>"
+	}'
+}
+
 render_git_file() {
 	printf '\t<span class="git-file">%s</span>\n\t<hr />\n' "${2}"
 	if git -C "${1}" diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904          \
@@ -556,7 +681,7 @@ render_git_file() {
 			}
 
 			{
-				print "<span>" NR "</span>" $0
+				print "<a class=\"line\" href=\"#l" NR "\" id=\"l" NR "\">" NR "</a>" $0
 			}
 
 			END {
@@ -572,9 +697,8 @@ render_git_repo() {
 	repo_path_log="${1}/${2}/log.html.tmp"
 	repo_path_files="${1}/${2}/files.html.tmp"
 	repo_path_file="${1}/${2}/file"
+	repo_path_commit="${1}/${2}/commit"
 	repo_path_tags="${1}/${2}/tags.html.tmp"
-	repo_path_readme="${1}/${2}/readme.html.tmp"
-	repo_path_license="${1}/${2}/license.html.tmp"
 	repo_path_archive="${1}/${2}/archive"
 	repo_name="${2%.*}"
 	repo_description=""
@@ -604,6 +728,16 @@ render_git_repo() {
 			render_git_file "${1}/${2}" "${file}" > "${file_path}.html.tmp"
 		done
 
+	# create commit files
+	mkdir "${repo_path_commit}"
+	git -C "${1}/${2}" log --pretty="format:%H"                             \
+		| grep .                                                            \
+		| while read -r file; do
+			file_path="$(echo "${repo_path_commit}/${file}")"
+			mkdir -p "${file_path%/*}"
+			render_git_commit "${1}/${2}" "${file}" > "${file_path}.html.tmp"
+		done
+
 	# render common html head
 	awk                                                                     \
 		-v var_site="$(awk_safe "${1}")"                                    \
@@ -617,9 +751,9 @@ render_git_repo() {
 			git -C "${1}/${2}" describe --tags >/dev/null 2>&1 &&
 			printf ' | <a href="//%s/tags.html">Tags</a>' "${1}/${2}"
 			git -C "${1}/${2}" show HEAD:README >/dev/null 2>&1 &&          \
-			printf ' | <a href="//%s/readme.html">README</a>' "${1}/${2}"
+			printf ' | <a href="//%s/file/README.html">README</a>' "${1}/${2}"
 			git -C "${1}/${2}" show HEAD:LICENSE >/dev/null 2>&1 &&         \
-			printf ' | <a href="//%s/license.html">LICENSE</a>' "${1}/${2}"
+			printf ' | <a href="//%s/file/LICENSE.html">LICENSE</a>' "${1}/${2}"
 		)"                                                                  \
 	'
 		{
@@ -635,7 +769,7 @@ render_git_repo() {
 
 	# render log
 	git -C "${1}/${2}" log                                                  \
-		--pretty="format:%ai%n%s%n%an%n%h"                                  \
+		--pretty="format:%ai%n%s%n%an%n%H"                                  \
 	| html_encode                                                           \
 	| awk '
 		BEGIN {
@@ -654,7 +788,13 @@ render_git_repo() {
 			if (NR % 4 == 0) {
 				printf "\t\t\t<tr>"
 				for (i = 0; i < 4; i++) {
-					printf "<td>%s</td>",entry[i]
+					if (i == 1) {
+						printf "<td><a href=\"commit/%s.html\">%s</a></td>",entry[3],entry[i]
+					} else if (i == 3) {
+						printf "<td><a href=\"commit/%s.html\">%*.*s</a></td>",entry[i],8,8,entry[i]
+					} else {
+						printf "<td>%s</td>",entry[i]
+					}
 				}
 				printf "</tr>\n"
 			}
@@ -781,44 +921,13 @@ render_git_repo() {
 		}
 	' | sed "s/__REPONAME__/${repo_name}/g" > "${repo_path_tags}"
 
-	# render readme if existing
+	# create redirection to README if it exists, else symlink to log
 	git -C "${1}/${2}" show HEAD:README >/dev/null 2>&1 &&                  \
-	git -C "${1}/${2}" cat-file HEAD:README -p                              \
-	| html_encode                                                           \
-	| awk '
-		BEGIN {
-			print "<pre>"
-		}
-
-		{
-			print "<span>" NR "</span>" $0
-		}
-
-		END {
-			print "</pre>"
-		}
-	' > "${repo_path_readme}" &&                                            \
-	ln -s "readme.html" "${1}/${2}/index.html"
-
-	# render license if existing
-	git -C "${1}/${2}" show HEAD:LICENSE >/dev/null 2>&1 &&                 \
-	git -C "${1}/${2}" cat-file HEAD:LICENSE -p                             \
-	| html_encode                                                           \
-	| awk '
-		BEGIN {
-			print "<pre>"
-		}
-
-		{
-			print "<span>" NR "</span>" $0
-		}
-
-		END {
-			print "</pre>"
-		}
-	' > "${repo_path_license}"
+	printf '<meta http-equiv="refresh" content="0; url=%s" />'              \
+		"${repo_path_file##*/}/README.html"                                 \
+		> "${1}/${2}/index.html"
 
-	[ ! -h "${1}/${2}/index.html" ] && ln -s "log.html" "${1}/${2}/index.html"
+	[ ! -f "${1}/${2}/index.html" ] && ln -s "log.html" "${1}/${2}/index.html"
 }
 
 render_git() {
diff --git a/git.noxz.tech/pub/style.css b/git.noxz.tech/pub/style.css
@@ -32,6 +32,7 @@ table tr.nohi td {
 
 table td {
 	padding                         : 0 0.4em;
+	white-space                     : nowrap;
 }
 
 #index,
@@ -99,7 +100,7 @@ pre {
 	tab-size                        : 4;
 }
 
-pre span {
+pre a.line {
 	display                         : inline-block;
 	text-align                      : right;
 	width                           : 32px;
@@ -113,3 +114,23 @@ pre span {
 	-ms-user-select                 : none;
 	user-select                     : none;
 }
+
+pre a.h {
+	color                           : #00c;
+	text-decoration                 : none;
+}
+
+pre a.i, table span.i {
+	color                           : #080;
+	text-decoration                 : none;
+}
+
+pre a.d, table span.d {
+	color                           : #d00;
+	text-decoration                 : none;
+}
+
+pre b.r {
+	color                           : #808;
+	padding-left                    : 1em;
+}