shithub: shithub

Download patch

ref: 7c3ef1d4f6ff9b6dfcc96d4b0bce2c93cdc1d069
parent: 26fca7ea9668ad3f4c94efe74893f28baa1370a7
author: phil9 <telephil9@gmail.com>
date: Tue Nov 16 08:11:22 EST 2021

switch to tcp80x

	This commit changes all scripts to use a new url scheme made possible
	by switching to tcp80x.
	The new url scheme now is: http://$host/<USER>/<REPO>[/<PAGE>/<HASH>]
	where PAGE is one of the usual page: browse, view, log, commit, ...

	The scripts now run as CGI meaning they need to send a proper HTTP
	response. The `http_response` function in `common.rc` does that.
	While requiring an extra step, this allows to respond with the proper
	Content-Type and to customize filenames (e.g. snap.tar.gz is now named
	$repo-$date.tgz).

	In addition, file browsing as been changed so that we `browse` directories
	and `view` files instead of `browsing` the toplevel directory and viewing
	all subdirectories leading to code duplication between the `files` and
	`view` scripts.

--- a/common.rc
+++ b/common.rc
@@ -3,6 +3,15 @@
 nl='
 '
 
+fn http_response {
+	ct = 'text/html'
+	if(! ~ $1 '') ct=$1
+	echo 'HTTP/1.1 200'
+	echo 'Content-Type: '^$ct
+	if(! ~ $2 '') echo 'Content-Disposition: attachment; filename="'^$2^'"'
+	echo ''
+}
+
 fn htcat {
 	sed '
 		s/&/\&amp;/g;
@@ -53,7 +62,7 @@
 	shorthash=`{echo $phash | awk '{print substr($0, 0, 8)}'}
 	if (test -f $gitfs/object/$phash/committer) committer=`"{htcat $gitfs/object/$phash/committer | awk '{print $1}'}
 	echo '	<div id="commit">
-		<a href=/'$puser/$prepo/$phash'/commit.html>'$shorthash'</a>
+		<a href=/'$puser/$prepo/commit/$phash'>'$shorthash'</a>
 		 – '$author' authored'
 	if (! ~ $#committer 0 && ! ~ $committer $author) echo ' and '^$committer^' committed'
 	echo ' on '$"date'
@@ -77,7 +86,7 @@
 		commithash=`{cat $gitfs/object/$commithash/parent | sed 1q}
 	}
 	if(! ~ $"commithash '')
-		echo '<a href="/'$puser/$prepo/$commithash'/log.html">Next</a>'
+		echo '<a href="/'$puser/$prepo/log/$commithash'">Next</a>'
 	cd $d
 }
 
@@ -186,12 +195,12 @@
 
 	prelude $puser $prepo
 	echo '	<h1><a href="/">'$title'</a>: 
-		<a href="/'$puser/$prepo'/HEAD/info.html">'$prepo'</a></h1>
+		<a href="/'$puser/$prepo'/">'$prepo'</a></h1>
 		<div id="linkbar">
-		<a href="/'$puser/$prepo'/HEAD/info.html">Info</a>
+		<a href="/'$puser/$prepo'/">Info</a>
 		&nbsp;•&nbsp;
-		<a href="/'$puser/$prepo/$phash'/files.html">Files</a>
+		<a href="/'$puser/$prepo'/browse/'$phash'/">Files</a>
 		&nbsp;•&nbsp;
-		<a href="/'$puser/$prepo/$phash'/log.html">Log</a>
+		<a href="/'$puser/$prepo'/log/'$phash'/">Log</a>
 		</div>'
 }
--- a/config.rc
+++ b/config.rc
@@ -11,13 +11,13 @@
 	<a href="mailto:ori@eigenstate.org">Ori Bernstein &lt;ori@eigenstate.org&gt;</a>.
 	</p>
 
-	<p>To get started using shithub, read our <a href="/static/guide.html">
+	<p>To get started using shithub, read our <a href="/guide.html">
 	user guide</a></p>
 	
 	<p>Shithub is a community service, written by people who
 	avoid browsers. As a result, the web interface is a work
 	in progress. Take a look at
-	<a href="/static/plots.html">what we want</a>.
+	<a href="/plots.html">what we want</a>.
 	</p>
 
 	<h2>Repositories</h2>
--- a/feed
+++ b/feed
@@ -1,4 +1,4 @@
-#!/bin/rc -e
+#!/bin/rc
 
 . /sys/lib/shithub/common.rc
 
@@ -8,7 +8,7 @@
 	phash=$3
 
 	d=`{pwd}
-	guid='http://'$host'/git/'$puser/$prepo/$phash'/commit.html'
+	guid='http://'^$host/git/$puser/$prepo/commit/$phash
 	message=`{htcat $gitfs/object/$phash/msg | sed 1q}
 	description=`''{htcat $gitfs/object/$phash/msg}
 	date=`{date -uf$dfmt `{mtime $gitfs/object/$phash/msg |awk '{print $1}'}}
@@ -33,7 +33,7 @@
 
 gituser=$1
 repo=$2
-refname=$3
+refname=HEAD
 dfmt='WW, DD MMM YYYY hh:mm:ss Z'
 
 repons $gituser $repo
@@ -48,12 +48,13 @@
 if not
 	hash=$ref
 
+http_response 'application/rss+xml'
 echo '<?xml version="1.0" encoding="utf-8" ?>
 	<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
 	<channel>
-	<atom:link href="http://'$host/$gituser/$repo/'HEAD/feed.rss" rel="self" type="application/rss+xml" />
+	<atom:link href="http://'$host/$gituser/$repo/'feed.rss" rel="self" type="application/rss+xml" />
 	<title>'$gituser/$repo' git log</title>
-	<link>http://'$host'/git/'$gituser/$repo/'HEAD/log.html</link>
+	<link>http://'$host'/git/'$gituser/$repo/'log/HEAD</link>
 	<description>shithub git/log for repository '$gituser/$repo'</description>
 	<lastBuildDate>'`"{date -uf$dfmt}'</lastBuildDate>
 	<language>en-us</language>
--- a/files
+++ b/files
@@ -1,4 +1,4 @@
-#!/bin/rc -e
+#!/bin/rc
 
 . /sys/lib/shithub/common.rc
 
@@ -13,6 +13,7 @@
 gituser=$1
 repo=$2
 refname=$3
+dir=$4
 
 repons $gituser $repo
 repodir=/mnt/$repo/.git
@@ -26,6 +27,7 @@
 if not
 	hash=$ref
 
+http_response
 user_prelude $gituser $repo $hash
 
 if(! test -d $gitfs/$ref/tree){
@@ -39,14 +41,19 @@
 cd $gitfs/$ref/tree
 
 echo '	<p>
-	<b>ref:</b> <a href="'/git/$gituser/$repo/$hash/files.html'">'$hash'</a><br/>
-	<b>tar:</b> <a href="'/git/$gituser/$repo/$hash/snap.tar.gz'">snap.tar.gz</a><br/></p>'
+	<b>ref:</b> <a href="'/git/$gituser/$repo/browse/$hash/'">'$hash'</a><br/>
+	<b>tar:</b> <a href="'/git/$gituser/$repo/snap/$hash/'">download</a><br/></p>'
 
+if (! ~ $#dir 0) cd .$dir
+
 echo '<p><div id="code">'
 for(f in `$nl{ls}){
-	url=`$nl{echo -n $f/f.html | urlencode}
+echo '<!-- f: ' $f ' -->'
+	url=`$nl{echo -n $f | urlencode}
 	fname=`$nl{echo -n $f | htcat}
-	echo -n '<a href="'$url'">'$fname
+	action='view'
+	if (test -d $f) action='browse'
+	echo -n '<a href="'/$gituser/$repo/$action/$refname^$dir/$url'">'$fname
 	if (test -d $f) echo '/'
 	echo '</a><br/>'
 }		
--- a/gitrules
+++ b/gitrules
@@ -1,11 +1,10 @@
-#/(shithub.png)					/bin/shithub/static	/usr/git '\1'
-/index.html					/bin/shithub/list	/usr/git
-([^'/]+)/([^'/]+)/([^'/]+)/info.html		/bin/shithub/info	/usr/git '\1' '\2' '\3'
-([^'/]+)/([^'/]+)/([^'/]+)/files.html		/bin/shithub/files	/usr/git '\1' '\2' '\3'
-([^'/]+)/([^'/]+)/([^'/]+)/snap.tar.gz		/bin/shithub/tar	/usr/git '\1' '\2' '\3'
-([^'/]+)/([^'/]+)/([^'/]+)/(([^']+)/)?f.html	/bin/shithub/view	/usr/git '\1' '\2' '\3' '\5'
-([^'/]+)/([^'/]+)/([^'/]+)/(([^']+)/)?raw	/bin/shithub/viewraw	/usr/git '\1' '\2' '\3' '\5'
-([^'/]+)/([^'/]+)/([^'/]+)/log.html		/bin/shithub/log	/usr/git '\1' '\2' '\3'
-([^'/]+)/([^'/]+)/([^'/]+)/commit.html		/bin/shithub/show	/usr/git '\1' '\2' '\3'
-([^'/]+)/([^'/]+)/([^'/]+)/_patch		/bin/shithub/patch	/usr/git '\1' '\2' '\3'
-([^'/]+)/([^'/]+)/([^'/]+)/feed.rss		/bin/shithub/feed	/usr/git '\1' '\2' '\3'
+([^'/]+)/([^'/]+)/view/([^'/]+)/([^']+)		/bin/shithub/view		/usr/git '\1' '\2' '\3' '\4'
+([^'/]+)/([^'/]+)/viewraw/([^'/]+)/([^']+)	/bin/shithub/viewraw	/usr/git '\1' '\2' '\3' '\4'
+([^'/]+)/([^'/]+)/browse/([^'/]+)(/[^']+)*	/bin/shithub/files		/usr/git '\1' '\2' '\3' '\4'
+([^'/]+)/([^'/]+)/snap/([^'/]+)				/bin/shithub/tar		/usr/git '\1' '\2' '\3'
+([^'/]+)/([^'/]+)/log/([^'/]+)				/bin/shithub/log		/usr/git '\1' '\2' '\3'
+([^'/]+)/([^'/]+)/commit/([^'/]+)			/bin/shithub/show		/usr/git '\1' '\2' '\3'
+([^'/]+)/([^'/]+)/patch/([^'/]+)			/bin/shithub/patch		/usr/git '\1' '\2' '\3'
+([^'/]+)/([^'/]+)/feed.rss					/bin/shithub/feed		/usr/git '\1' '\2'
+([^'/]+)/([^'/]+)/?							/bin/shithub/info		/usr/git '\1' '\2' '\3'
+/$											/bin/shithub/list		/usr/git
--- a/guide.html
+++ b/guide.html
@@ -71,7 +71,7 @@
 </head>
 <body>
 <a href="/">
-<img src="/static/shithub.png"/>
+<img src="/shithub.png"/>
 </a>
 <br>
 
--- a/info
+++ b/info
@@ -1,4 +1,4 @@
-#!/bin/rc -e
+#!/bin/rc
 
 . /sys/lib/shithub/common.rc
 
@@ -11,7 +11,7 @@
 
 gituser=$1
 repo=$2
-refname=$3
+refname=HEAD
 
 repons $gituser $repo
 repodir=/mnt/$repo/.git
@@ -25,6 +25,7 @@
 if not
 	hash=$ref
 
+http_response
 user_prelude $gituser $repo $hash		
 echo '	<h3>Clone</h3>
 	<div>
--- a/list
+++ b/list
@@ -1,4 +1,4 @@
-#!/bin/rc -e
+#!/bin/rc
 
 . /sys/lib/shithub/common.rc
 
@@ -9,8 +9,9 @@
 nl='
 '
 
+http_response
 prelude '' 'the fragrant git host'
-echo '<img src="/static/'$logo'" /><br/>'
+echo '<img src="/'$logo'" /><br/>'
 
 if(~ $#intro 0)
 	echo '<h2>Repositories</h2><br/>'
@@ -28,7 +29,7 @@
 		udir=$ndir
 	}
 	if(test -e $repo/.git/webpublish){
-		echo '<dt><a href="/'$repo'/HEAD/info.html">'$repo'</a></dt>'
+		echo '<dt><a href="/'$repo'/">'$repo'</a></dt>'
 		echo '<dd>'
 		if(test -f $repo/.git/desc)
 			htcat $repo/.git/desc
--- a/log
+++ b/log
@@ -1,4 +1,4 @@
-#!/bin/rc -e
+#!/bin/rc
 
 . /sys/lib/shithub/common.rc
 
@@ -25,6 +25,7 @@
 if not
 	hash=$ref
 
+http_response
 user_prelude $gituser $repo $hash
 
 if(! test -d $gitfs/$ref/tree){
@@ -36,7 +37,7 @@
 }
 cd $gitfs/$ref/tree
 echo '	<p>
-	<a href="/'$gituser/$repo'/HEAD/feed.rss">RSS feed</a>
+	<a href="/'$gituser/$repo'/feed.rss">RSS feed</a>
 	</p>
 	<p>'
 shortlog $gituser $repo $ref 100
--- a/patch
+++ b/patch
@@ -1,4 +1,4 @@
-#!/bin/rc -e
+#!/bin/rc
 
 . /sys/lib/shithub/common.rc
 
@@ -15,4 +15,6 @@
 
 repons $gituser $repo
 cd /mnt/$repo
+
+http_response 'text/plain'
 git/export $hash
--- a/plots.html
+++ b/plots.html
@@ -72,7 +72,7 @@
 <body>
 
 <a href="/">
-<img src="/static/shithub.png"/>
+<img src="/shithub.png"/>
 </a>
 <br>
 
--- a/show
+++ b/show
@@ -1,4 +1,4 @@
-#!/bin/rc -e
+#!/bin/rc
 
 . /sys/lib/shithub/common.rc
 
@@ -20,6 +20,7 @@
 	exit
 }
 
+http_response
 user_prelude $gituser $repo $refname
 
 oldcommit=`{cat $gitfs/$ref/parent}
@@ -27,12 +28,12 @@
 date=`''{date `{mtime $gitfs/$ref/msg | awk '{print $1}'}}
 msg=`''{htcat $gitfs/$ref/msg}
 echo '	<p>
-	<a href="/'$gituser/$repo/$refname'/_patch">Download patch</a><br/>
+	<a href="/'$gituser/$repo/patch/$refname'">Download patch</a><br/>
 	</p>
 	<p>
-	<b>ref:</b> <a href="/'$gituser/$repo/$refname'/files.html">'$refname'</a><br/>'
+	<b>ref:</b> <a href="/'$gituser/$repo/browse/$refname'">'$refname'</a><br/>'
 if(! ~ $#oldcommit 0) {
-	echo '<b>parent:</b> <a href="/'$gituser/$repo/$oldcommit'/files.html">'$oldcommit'</a><br/>'
+	echo '<b>parent:</b> <a href="/'$gituser/$repo/browse/$oldcommit'">'$oldcommit'</a><br/>'
 }
 echo '	<b>author:</b> '$author'<br/>
 	<b>date:</b> '$date'
--- a/tar
+++ b/tar
@@ -1,4 +1,4 @@
-#!/bin/rc -e
+#!/bin/rc
 
 . /sys/lib/shithub/common.rc
 
@@ -12,6 +12,7 @@
 gituser=$1
 repo=$2
 refname=$3
+filename=$repo^'-'^`{date -f 'YYYYMMDD-hhmm'}^'.tgz'
 
 repons $gituser $repo
 if(! ref=`{resolveref $refname}){
@@ -20,4 +21,5 @@
 }
 bind $gitfs/$ref/tree /mnt/$repo
 cd /mnt
+http_response 'application/x-gzip' $filename
 tar c $repo | gzip -n
--- a/view
+++ b/view
@@ -1,4 +1,4 @@
-#!/bin/rc -e
+#!/bin/rc
 
 . /sys/lib/shithub/common.rc
 
@@ -24,13 +24,14 @@
 	file='.'
 hash=`{cat $gitfs/$ref/hash}
 
+http_response
 user_prelude $gituser $repo $hash
 
 echo '	<p>
-	<b>ref:</b> <a href="'/$gituser/$repo/$hash/files.html'">'$hash'</a><br/>
+	<b>ref:</b> <a href="'/$gituser/$repo/browse/$hash/'">'$hash'</a><br/>
+	<b>raw:</b>	<a href="'/$gituser/$repo/viewraw/$hash/$file'">view</a>
 	</p>'
 if(test -f $file){
-	echo '	<a href="'/$gituser/$repo/$hash/$file'/raw">View raw version</a>'
 	type=`{file -m $file}
 	switch($type){
 	case text/*
@@ -38,20 +39,10 @@
 		htcat $file
 		echo '	</pre>'
 	case image/*
-		echo '	<br/><div><img src="'/$gituser/$repo/$hash/$file'/raw" /></div>'
+		echo '	<br/><div><img src="'/$gituser/$repo/viewraw/$hash/$file'" /></div>'
 	case *
 		echo '	<p>Binary file not displayed</p>'
 	}
-}
-if not if(test -d $file){
-	cd $file
-	echo '	<div id="code">'
-	for(f in `$nl{ls}){
-		url=`$nl{echo -n $f/f.html | urlencode}
-		fname=`$nl{echo -n $f | htcat}
-		echo '<a href="'$url'">'$fname'</a><br/>'
-	}
-	echo '	</div>'
 }
 echo '	</body>
 	</html>'
--- a/viewraw
+++ b/viewraw
@@ -1,4 +1,4 @@
-#!/bin/rc -e
+#!/bin/rc
 
 . /sys/lib/shithub/common.rc
 
@@ -20,4 +20,5 @@
 	exit
 }
 cd $gitfs/$ref/tree
+http_response `{file -m $file}
 cat $file