git3html

git clone https://orangeshoelaces.net/git/git3html.git

93c28fa5e02dd52fece0646fe603ee9e1e7a69e4

Author: Vasilii Kolobkov on 01/14/2019

Committer: Vasilii Kolobkov on 01/14/2019

Show contents of text files

Stats

git3html | 62 +++++++-
1 file changed, 54 insertions(+), 8 deletions(-)

Patch

diff --git a/git3html b/git3html
index de74a3a..2f9c47e 100755
--- a/git3html
+++ b/git3html
@@ -37,7 +37,7 @@ parent() {
 
 # snatched from libgit2
 isbinary() {
-	od -A n -N 256 -t u1 | awk -f '
+	od -A n -N 256 -t u1 | awk '
 	{
 		for(i=1; i <= NF; ++i) {
 			c=$i
@@ -61,7 +61,7 @@ isbinary() {
 }
 
 g() {
-	git --git-dir="$repodir" "$@"
+	git -C "$repodir" "$@"
 }
 
 # tree of the most recent commit
@@ -126,14 +126,52 @@ readme() (
 )
 
 dirent() {
-	printf "<li><pre><code>d--------- <a href=\"%s\">%s</a></code></pre></li>\n" \
+	printf "<li><pre><code>d <a href=\"%s\">%s</a></code></pre></li>\n" \
 		"$(echo "$2" | escape)" "$(echo "$1" | escape)"
 }
 
-blobent() {
-	printf "<li><pre><code>f--------- <a href=\"%s\">%s</code></pre></a></li>\n" \
-		"$(echo "$2" | escape)" "$(echo "$1" | escape)"
-}
+blobent() (
+	mode="$1"
+	blob="$2"
+	name="$3"
+	outdir="$4"
+	relout="$5"	# outdir relative to index dir
+	reppath="$6"
+	relbaseoutdir="$7"
+
+	type="$(expr \( $mode - $mode % 10000 \) / 10000)"
+	if [ "$type" -eq 10 ]; then	# regular file
+		if [ "$(g cat-file -p "$blob" | isbinary)" = y ]; then
+			printf '<li><pre><code>b %s</code></pre></li>\n' \
+				"$(echo "$name" | escape)"
+		else
+			textfile "$blob" "$reppath" "$relbaseoutdir" > "${outdir%/}/${name}.html"
+			printf '<li><pre><code>f <a href="%s">%s</a></code></pre></li>\n' \
+				"$(echo "${relout%/}/${name}.html" | escape)" \
+				"$(echo "$name" | escape)"
+		fi
+	elif [ "$type" -eq 12 ]; then	# link
+		printf "<li><pre><code>l %s</code></pre></li>\n" "$(echo "$name" | escape)"
+	else
+		printf "<li><pre><code>o %s</code></pre></li>\n" "$(echo "$name" | escape)"
+	fi
+)
+
+textfile() (
+	blob="$1"
+	reppath="$2"
+	relbaseoutdir="$3"
+
+	header "${reppath}:" "$relbaseoutdir"
+	printf '<code>%s:</code>\n<pre>\n<code>\n' "$(echo "$reppath" | escape)"
+	n=0
+	g cat-file blob "$blob" | escape | awk -F '' '{
+		++n
+		printf "<a href=\"#l%d\">%4d</a> %s", n, n, $0
+	}'
+	printf '</code>\n</pre>\n'
+	footer
+)
 
 files() (
 	tree="$1"
@@ -145,6 +183,7 @@ files() (
 
 	{
 		header "${reppath}:" "$relbaseoutdir"
+		printf '<code>%s</code>\n' "$(echo "$reppath" | escape)"
 		echo '<ul>'
 		if [ -n "$relparidx" ]; then
 			dirent '..' "$relparidx"
@@ -163,8 +202,15 @@ files() (
 			dirent "${stname}" "${name}/${stname}.html"
 		done
 		g ls-tree "$tree" | awk '$2 == "blob"' | while read rec; do
+			bmode="$(echo "$rec" | awk '{ print $1 }')"
+			blob="$(echo "$rec" | awk '{ print $3 }')"
 			bname="$(echo "$rec" | cut -d "$ht" -f2)"
-			blobent "$bname" "${name}/${bname}.html"
+			boutdir="${outdir%/}/${name}"
+			if [ ! -d "$boutdir" ]; then
+				mkdir "$boutdir"
+			fi
+			blobent "$bmode" "$blob" "$bname" "$boutdir" "$name" \
+				"${reppath%/}/${bname}" "$(parent "$relbaseoutdir")"
 		done
 		echo '</ul>'
 		footer