shithub: shithub

ref: b2f9582d4c988b7e8de59338402b63158470acd4
dir: /shithub/

View raw version
#!/bin/rc -e

cd $1
shift

rfork ne
nl='
'

fn htcat {
	sed '
		s/&/\&/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
	bind /bin /mnt/bin
	bind /tmp /mnt/tmp
	bind -c /env /mnt/env
	bind $1/$2 /mnt/$repo
	bind /mnt /
	cd /mnt/$repo
	git/fs
	rfork m
}


fn prelude {
	echo '
	<!DOCTYPE html>
	<html>
	
	<head>
		<style type="text/css">
			body{
				padding: 3em;
				margin: auto;
				max-width: 50em;
				font-family: sans-serif;
			}
			h1{
				font-size: 1.5em;
				color: #4c4c99;
			}
			h2{
				font-size: 1.3em;
				color: #4c4c99;
			}
			h3{
				font-size: 1em;
				color: #4c4c99;
			}

			#code{
				background: #ffffea;
				border: #99994cff;
				overflow: auto;
			}
		</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>
		</div>'
}

switch($1){
case 'list'
	rfork m
	prelude Repolist
	echo '	<h1>Repos</h1>'
	udir=()
	for(repo in `$nl{ls */}){
		ndir=`{basename -d $repo}
		if(! ~ $udir $ndir)
			echo '</dl>'
		if(! ~ $udir $ndir){
			echo '<h3>'$ndir'</h3>'
			echo '<dl>'
			udir=$ndir
		}
		if(test -e $repo/.git/webpublish){
			echo '<dt><a href="/git/'$repo'/HEAD/info.html">'$repo'</a></dt>'
			echo '<dd>'
			if(test -f $repo/.git/desc)
				htcat $repo/.git/desc
			if not if(test -f $repo/.git/description)
				htcat $repo/.git/description
			if not
				echo 'probably some code'
			echo '</dd>'
		}
	}

case 'tar'
	gituser=$2
	repo=$3
	refname=$4

	repons $gituser $repo
	if(! ref=`{resolveref $refname}){
		echo '<b>invalid ref '$refname'</b>'
		exit
	}
	bind /mnt/git/$ref/tree /mnt/$repo
	cd /mnt
	tar cz $repo

case 'info'
	gituser=$2
	repo=$3
	refname=$4

	repons $gituser $repo
	repodir=/mnt/$repo/.git
	if(! ref=`{resolveref $refname}){
		echo '<b>invalid ref '$refname'</b>'
		exit
	}

	if(test -f /mnt/git/$ref/hash)
		hash=`{cat /mnt/git/$ref/hash}
	if not
		hash=$ref

	user_prelude $gituser $repo $hash		
	echo '	<h3>Clone</h3>
		<p>
		<b>clone:</b> git://shithub.us/'$gituser/$repo' gits://shithub.us/'$gituser/$repo'<br>
		<b>push:</b> hjgit://shithub.us/'$gituser/$repo'<br>'
	if(test -f $repodir/contact)
		echo '	<b>patches to: </b>'^`$nl{cat $repodir/contact}^'<br>
			</p>'

	if(test -f /mnt/git/object/$hash/msg){
		echo '	<h3>Last commit</h3>
			<p>'$repo' @ <a href='/git/$gituser/$repo/$hash/f.html'>'$hash'</a>
			<pre id="desc">'
		htcat /mnt/git/object/$hash/msg
		echo '	</pre>'
	}

	echo '	<h3>About</h3>
		<pre id="desc">'
	if(test -f $repodir/README)
		htcat $repodir/README
	if not if(test -f README)
		htcat README
	if not if (test -f README.md)
		htcat README.md
	if not if(test -f $repodir/desc)
		htcat $repodir/desc
	if not if(test -f $repodir/description)
		htcat $repodir/description
	if not
		echo 'this repo has no description'
	echo '
		</pre>
		</body>
		</html>
	'

case 'files'
	gituser=$2
	repo=$3
	refname=$4

	repons $gituser $repo
	repodir=/mnt/$repo/.git
	if(! ref=`{resolveref $refname}){
		echo '<b>invalid ref '$refname'</b>'
		exit
	}

	if(test -f /mnt/git/$ref/hash)
		hash=`{cat /mnt/git/$ref/hash}
	if not
		hash=$ref

	user_prelude $gituser $repo $hash

	if(! test -d /mnt/git/$ref/tree){
		echo '	<p>No code pushed</p>
			</body>
			</html>
		'
		exit
	}

	cd /mnt/git/$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>'

	echo '<p><pre 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>'
	}		
	echo '	</pre>
		</p>
		</body>
		</html>'

case 'view'
	gituser=$2
	repo=$3
	refname=$4
	file=$5

	repons $gituser $repo
	if(! ref=`{resolveref $refname}){
		echo '<b>invalid ref '$refname'</b>'
		exit
	}
	cd /mnt/git/$ref/tree
	if(~ $file '')
		file='.'
	hash=`{cat /mnt/git/$ref/hash}

	user_prelude $gituser $repo $hash

	echo '	<p>'$repo' @ <a href="'/git/$gituser/$repo/$hash/f.html'">'$hash'</a><br/></p>'
	if(test -f $file){
		echo '	<a href="'/git/$gituser/$repo/$hash/$file'/raw">View raw version</a>'
		echo '	<pre id="code">'
		htcat $file
	}
	if not if(test -d $file){
		cd $file
		echo '	<pre 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>'
		}
	}
	echo '	</pre>
		</body>
		</html>'

case 'viewraw'
	gituser=$2
	repo=$3
	refname=$4
	file=$5

	repons $gituser $repo
	if(! ref=`{resolveref $refname}){
		echo 'invalid ref '$refname''
		exit
	}
	cd /mnt/git/$ref/tree
	cat $file

}