shithub: shithub

Download patch

ref: 2f4a9aee3262d3c876851b803b1aeb2ad5376cc1
parent: ea71c33594b15937ba22faab3cf2905d3a138594
author: phil9 <telephil9@gmail.com>
date: Mon Feb 1 00:01:48 EST 2021

split common functions in a separate file

--- /dev/null
+++ b/common.rc
@@ -1,0 +1,187 @@
+nl='
+'
+
+fn htcat {
+	sed '
+		s/&/\&amp;/g;
+		s/</\&lt;/g;
+		s/>/\&gt;/g;
+		s/"/\&quot;/g;
+		s/''/\&#39;/g
+	' $*
+}
+
+fn resolveref {
+	if(~ $refname HEAD)
+		echo $refname
+	if not if(test -d /mnt/git/branch/$refname/tree)
+		echo branch/$refname
+	if not if(test -d /mnt/git/object/$refname/tree)
+		echo object/$refname
+	if not
+		status='bad ref'
+}
+
+fn repons {
+	mntgen
+	mntgen /mnt/mnt
+	ramfs -m /mnt/tmp
+	bind /bin /mnt/bin
+	bind /rc /mnt/rc
+	bind /sys /mnt/sys
+	bind -c /env /mnt/env
+	bind /dev /mnt/dev
+	bind $1/$2 /mnt/$repo
+	bind /mnt /
+	cd /mnt/$repo
+	git/fs
+	mntgen /mnt/scratch
+	rfork m
+}
+
+fn formatcommit {
+	puser=$1
+	prepo=$2
+	phash=$3
+
+	message=`{htcat /mnt/git/object/$phash/msg | sed 1q}
+	date=`{date -t `{mtime /mnt/git/object/$phash/msg | awk '{print $1}'}}
+	author=`"{htcat /mnt/git/object/$phash/author | awk '{print $1}'}
+	shorthash=`{echo $phash | awk '{print substr($0, 0, 8)}'}
+	echo '	<div id="commit">
+		<a href=/git/'$puser/$prepo/$phash'/commit.html>'$shorthash'</a>
+		 – '$author' – '$"date'
+		<pre>'$"message'</pre>
+		</div>'
+}
+
+fn shortlog {
+	puser=$1
+	prepo=$2
+	pref=$3
+	pcount=$4
+
+	d=`{pwd}
+	commithash=`{cat /mnt/git/$pref/hash | sed 1q}
+	count=()
+	while (! ~ $#commithash 0 && ! ~ $#count $pcount) {
+		count=($count 1)
+		formatcommit $puser $prepo $commithash
+		echo '<br/>'
+		commithash=`{cat /mnt/git/object/$commithash/parent | sed 1q}
+	}
+	if(! ~ $"commithash '')
+		echo '<a href="/git/'$puser/$prepo/$commithash'/log.html">Next</a>'
+	cd $d
+}
+
+fn difftohtml {
+	awk '
+	function printpre(id, text) { printf "<pre id='%s'>%s</pre>", id, text }
+	BEGIN { started = 0; diff = 0; }
+	/^---$/ { diff = 1; next }
+	/^diff .*$/ && diff && !started { started = 1; next }
+	/^\+\+\+ .*$/ && started { printpre("files", $0); next }
+	/^--- .*$/ && started { printpre("files", $0); next }
+	/^@@ .*$/ && started { printpre("sep", $0); next }
+	/^\+.*$/ && started { printpre("add", $0); next }
+	/^-.*$/ && started { printpre("del", $0); next }
+	started { printpre("ctx", $0); next }
+	'
+}
+
+fn prelude {
+	echo '
+	<!DOCTYPE html>
+	<html>
+	
+	<head>
+		<style type="text/css">
+			body{
+				padding: 3em;
+				margin: auto;
+				min-width: 50em;
+				width: min-content;
+				font-family: sans-serif;
+				tab-space: 8;
+			}
+			h1{
+				font-size: 1.5em;
+				color: #4c4c99;
+			}
+			h2{
+				font-size: 1.3em;
+				color: #4c4c99;
+			}
+			h3{
+				font-size: 1em;
+				color: #4c4c99;
+			}
+
+			#code{
+				background: #ffffea;
+				border: 1px solid #99994c;
+				overflow: auto;
+				padding: 4px;
+			}
+
+			#commit{
+				font-family: sans-serif;
+				background: #eeeeee;
+				border: 1px solid #cccccc;
+				padding: 4px;
+			}
+
+			#diff{
+				font-family: monospace;
+				border: 2px solid #efefef;
+			}
+
+			#diff #files{
+				background: #efefef;
+				margin: 0em;
+			}
+
+			#diff #sep{
+				background: #eaffff;
+				margin: 0em;
+			}
+
+			#diff #add{
+				background: #e6ffed;
+				margin: 0em;
+			}
+
+			#diff #del{
+				background: #ffeef0;
+				margin: 0em;
+			}
+			#diff #ctx{
+				margin: 0em;
+			}
+		</style>
+	 	<link rel="alternate" type="application/rss+xml" href="feed.rss" title="rss">
+		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+		<meta name="viewport" content="width=device-width,initial-scale=1">
+		<title>Shithub: '$"1'</title>
+	</head>
+	<body>
+	'
+}
+
+fn user_prelude {
+	puser=$1
+	prepo=$2
+	phash=$3
+
+	prelude $prepo
+	echo '	<h1><a href="/git/repos.html">Git</a>: 
+		<a href="/git/'$puser/$prepo/$phash'/info.html">'$prepo'</a></h1>
+		<div id="linkbar">
+		<a href="/git/'$puser/$prepo/$phash'/info.html">Info</a>
+		&nbsp;•&nbsp;
+		<a href="/git/'$puser/$prepo/$phash'/files.html">Files</a>
+		&nbsp;•&nbsp;
+		<a href="/git/'$puser/$prepo/$phash'/log.html">Log</a>
+		</div>'
+}
--- a/mkfile
+++ b/mkfile
@@ -2,11 +2,16 @@
 	shithub\
 	newrepo
 
+LIBFILES=\
+	common.rc
+
 WEBFILES=\
 	index.html\
 	toilet.jpg
 
 install:
+	mkdir -p /sys/lib/shithub
+	cp $LIBFILES /sys/lib/shithub
 	cp $RCFILES /rc/bin
 	cp $WEBFILES /usr/web
 	cp gitrules /sys/lib/tcp80
--- a/shithub
+++ b/shithub
@@ -1,5 +1,7 @@
 #!/bin/rc -e
 
+. /sys/lib/shithub/common.rc
+
 cd $1
 shift
 
@@ -6,191 +8,6 @@
 rfork ne
 nl='
 '
-
-fn htcat {
-	sed '
-		s/&/\&amp;/g;
-		s/</\&lt;/g;
-		s/>/\&gt;/g;
-		s/"/\&quot;/g;
-		s/''/\&#39;/g
-	' $*
-}
-
-fn resolveref {
-	if(~ $refname HEAD)
-		echo $refname
-	if not if(test -d /mnt/git/branch/$refname/tree)
-		echo branch/$refname
-	if not if(test -d /mnt/git/object/$refname/tree)
-		echo object/$refname
-	if not
-		status='bad ref'
-}
-
-fn repons {
-	mntgen
-	mntgen /mnt/mnt
-	ramfs -m /mnt/tmp
-	bind /bin /mnt/bin
-	bind /rc /mnt/rc
-	bind /sys /mnt/sys
-	bind -c /env /mnt/env
-	bind /dev /mnt/dev
-	bind $1/$2 /mnt/$repo
-	bind /mnt /
-	cd /mnt/$repo
-	git/fs
-	mntgen /mnt/scratch
-	rfork m
-}
-
-fn formatcommit {
-	puser=$1
-	prepo=$2
-	phash=$3
-
-	message=`{htcat /mnt/git/object/$phash/msg | sed 1q}
-	date=`{date -t `{mtime /mnt/git/object/$phash/msg | awk '{print $1}'}}
-	author=`"{htcat /mnt/git/object/$phash/author | awk '{print $1}'}
-	shorthash=`{echo $phash | awk '{print substr($0, 0, 8)}'}
-	echo '	<div id="commit">
-		<a href=/git/'$puser/$prepo/$phash'/commit.html>'$shorthash'</a>
-		 – '$author' – '$"date'
-		<pre>'$"message'</pre>
-		</div>'
-}
-
-fn shortlog {
-	puser=$1
-	prepo=$2
-	pref=$3
-	pcount=$4
-
-	d=`{pwd}
-	commithash=`{cat /mnt/git/$pref/hash | sed 1q}
-	count=()
-	while (! ~ $#commithash 0 && ! ~ $#count $pcount) {
-		count=($count 1)
-		formatcommit $puser $prepo $commithash
-		echo '<br/>'
-		commithash=`{cat /mnt/git/object/$commithash/parent | sed 1q}
-	}
-	if(! ~ $"commithash '')
-		echo '<a href="/git/'$puser/$prepo/$commithash'/log.html">Next</a>'
-	cd $d
-}
-
-fn difftohtml {
-	awk '
-	function printpre(id, text) { printf "<pre id='%s'>%s</pre>", id, text }
-	BEGIN { started = 0; diff = 0; }
-	/^---$/ { diff = 1; next }
-	/^diff .*$/ && diff && !started { started = 1; next }
-	/^\+\+\+ .*$/ && started { printpre("files", $0); next }
-	/^--- .*$/ && started { printpre("files", $0); next }
-	/^@@ .*$/ && started { printpre("sep", $0); next }
-	/^\+.*$/ && started { printpre("add", $0); next }
-	/^-.*$/ && started { printpre("del", $0); next }
-	started { printpre("ctx", $0); next }
-	'
-}
-
-fn prelude {
-	echo '
-	<!DOCTYPE html>
-	<html>
-	
-	<head>
-		<style type="text/css">
-			body{
-				padding: 3em;
-				margin: auto;
-				min-width: 50em;
-				width: min-content;
-				font-family: sans-serif;
-				tab-space: 8;
-			}
-			h1{
-				font-size: 1.5em;
-				color: #4c4c99;
-			}
-			h2{
-				font-size: 1.3em;
-				color: #4c4c99;
-			}
-			h3{
-				font-size: 1em;
-				color: #4c4c99;
-			}
-
-			#code{
-				background: #ffffea;
-				border: 1px solid #99994c;
-				overflow: auto;
-				padding: 4px;
-			}
-
-			#commit{
-				font-family: sans-serif;
-				background: #eeeeee;
-				border: 1px solid #cccccc;
-				padding: 4px;
-			}
-
-			#diff{
-				font-family: monospace;
-				border: 2px solid #efefef;
-			}
-
-			#diff #files{
-				background: #efefef;
-				margin: 0em;
-			}
-
-			#diff #sep{
-				background: #eaffff;
-				margin: 0em;
-			}
-
-			#diff #add{
-				background: #e6ffed;
-				margin: 0em;
-			}
-
-			#diff #del{
-				background: #ffeef0;
-				margin: 0em;
-			}
-			#diff #ctx{
-				margin: 0em;
-			}
-		</style>
-	 	<link rel="alternate" type="application/rss+xml" href="feed.rss" title="rss">
-		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-		<meta name="viewport" content="width=device-width,initial-scale=1">
-		<title>Shithub: '$"1'</title>
-	</head>
-	<body>
-	'
-}
-
-fn user_prelude {
-	puser=$1
-	prepo=$2
-	phash=$3
-
-	prelude $prepo
-	echo '	<h1><a href="/git/repos.html">Git</a>: 
-		<a href="/git/'$puser/$prepo/$phash'/info.html">'$prepo'</a></h1>
-		<div id="linkbar">
-		<a href="/git/'$puser/$prepo/$phash'/info.html">Info</a>
-		&nbsp;•&nbsp;
-		<a href="/git/'$puser/$prepo/$phash'/files.html">Files</a>
-		&nbsp;•&nbsp;
-		<a href="/git/'$puser/$prepo/$phash'/log.html">Log</a>
-		</div>'
-}
 
 switch($1){
 case 'list'