hg-site

git clone http://git.code.weiherhei.de/hg-site.git
Log | Files | Refs | README

hg-site.sh (10538B)


      1 #!/bin/bash
      2 
      3 REPO_DIR=`pwd`
      4 OUT_DIR=`pwd`
      5 SITE_NAME=`basename $REPO_DIR`
      6 DOMAIN_NAME=`basename $OUT_DIR`
      7 
      8 while getopts ":d:n:R:o:" opt; do
      9   case $opt in
     10     R)
     11       REPO_DIR=$OPTARG
     12       ;;
     13     o)
     14       OUT_DIR=$OPTARG
     15       ;;
     16     n)
     17       SITE_NAME=$OPTARG
     18       ;;
     19     d)
     20       DOMAIN=$OPTARG
     21       ;;
     22     \?)
     23       echo "Invalid option: -$OPTARG" >&2
     24       exit 1
     25       ;;
     26     :)
     27       echo "Option -$OPTARG requires an argument." >&2
     28       exit 1
     29       ;;
     30   esac
     31 done
     32 
     33 
     34 function header
     35 {
     36 	TITLE=$1
     37 	cat << EOF
     38 <!DOCTYPE html>
     39 <html lang="en">
     40   <head>
     41     <meta charset="utf-8">
     42     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     43     <meta name="viewport" content="width=device-width, initial-scale=1">
     44     <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
     45     <title>$TITLE</title>
     46 
     47     <!-- Bootstrap -->
     48     <link href="/css/bootstrap.min.css" rel="stylesheet">
     49     <link href="/css/bootstrap-thema.min.css" rel="stylesheet">
     50     <link href="/css/highlight-default.css" rel="stylesheet">
     51 
     52   </head>
     53   <body>
     54 
     55 
     56 <nav class="navbar navbar-default">
     57   <div class="container-fluid">
     58     <div class="navbar-header">
     59       <a class="navbar-brand" href="/">$SITE_NAME</a>
     60     </div>
     61     <div class="collapse navbar-collapse"> <p class="navbar-text">$TITLE</p></div>
     62   </div>
     63 </nav>
     64 
     65 EOF
     66 
     67 }
     68 
     69 
     70 function footer
     71 {
     72 	cat << EOF
     73     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
     74     <script src="/js/jquery-3.1.1.min.js"></script>
     75     <!-- Include all compiled plugins (below), or include individual files as needed -->
     76     <script src="/js/bootstrap.min.js"></script>
     77     
     78     <script src="/js/highlight.pack.js"></script>
     79     <script>hljs.initHighlightingOnLoad();</script>
     80   </body>
     81 </html>
     82 EOF
     83 }
     84 
     85 
     86 
     87 function rootIndex
     88 {
     89     header $SITE_NAME
     90     
     91     cat << EOF
     92     <ol class="breadcrumb">
     93   		<li class="active">$SITE_NAME</li>
     94 	</ol>
     95 EOF
     96 
     97 	if	[ -f $REPO_DIR/README ]; then
     98 		cat << EOF
     99 <div class="panel panel-default">
    100   <div class="panel-body">
    101 EOF
    102     	cat $REPO_DIR/README
    103 		cat << EOF
    104   </div>
    105 </div>
    106 EOF
    107 	fi
    108 	
    109     cat << EOF
    110 <table class="table"><tr><th>Repository</th><th>User</th><th>Date</th><th>Summary</th><th>Tag</th></tr>
    111 EOF
    112     for repo in $REPO_DIR/*; do
    113 	name=`basename $repo`
    114 	if [ ! -d $repo/.hg ]; then
    115 	    continue;
    116 	    
    117 	fi
    118 	hg log -l 1 -R $repo --template '<tr><td><a href="./'$name'">'$name'</a></td><td>{author}</td><td>{date|isodate}</td><td>{desc}</td><td>{tags}</td></tr>'
    119     
    120 
    121     done
    122     echo "</table>"
    123     footer
    124 }
    125 
    126 
    127 
    128 
    129 function folderContent
    130 {
    131 	local project=$1
    132 	local name=$2
    133 	local dir=$OUT_DIR/$project/raw/$name
    134 	local path
    135 	if [ $name == '.' ]; then
    136 	    title="source"
    137 	else
    138 	    title=`basename $name`
    139 	fi
    140 	
    141     header $title
    142     
    143     cat << EOF
    144     <ol class="breadcrumb">
    145   		<li><a href="/$project/">$project</a></li>
    146   		<li><a href="/$project/src">source</a></li>
    147 EOF
    148     href=
    149     for path in `pathSegments $name`; do
    150 	href=$href$path/
    151 	cat << EOF
    152 	    <li class="active"><a href="/$project/src/$href">$path</a></li>
    153 EOF
    154     done
    155     cat << EOF
    156 	</ol>
    157 EOF
    158 
    159 	cat << EOF
    160 <ul class="nav nav-tabs">
    161   <li role="presentation" class="active"><a href="./">Content</a></li>
    162   <li role="presentation"><a href="./history.html">History</a></li>
    163 </ul>
    164 EOF
    165 
    166 
    167 	
    168     cat << EOF
    169 <table class="table"><tr><th>Name</th><th>User</th><th>Date</th><th>Summary</th><th>Tag</th></tr>
    170 EOF
    171 	local f
    172 	for f in `ls -1 --group-directories-first $dir`; do
    173 		if	[ -f $dir/$f ]; then
    174 			link="$f-content.html"
    175 		fi
    176 		if	[ -d $dir/$f ]; then
    177 			link="$f/"
    178 		fi
    179 		
    180 		echo "<tr><td><a href="$link">$f</a></td>"
    181 		hg log -l 1 -R $REPO_DIR/$project --template '<td>{author}</td><td>{date|isodate}</td><td>{desc}</td><td>{tags}</td></tr>' $REPO_DIR/$project/$name/$f
    182 	done
    183     echo "</table>"
    184 
    185 	# Show README-File
    186 	if	[ -f $dir/README ]; then
    187 		cat << EOF
    188 <div class="panel panel-default">
    189   <div class="panel-body">
    190 EOF
    191     	cat $dir/README
    192 		cat << EOF
    193   </div>
    194 </div>
    195 EOF
    196 	fi
    197     
    198     footer
    199 }
    200 
    201 
    202 
    203 
    204 function folderHistory
    205 {
    206 	local project=$1
    207 	local name=$2
    208 	local dir=$REPO_DIR/$project/$name
    209 	local path
    210 
    211 	if [ $name == '.' ]; then
    212 	    title="source"
    213 	else
    214 	    title=`basename $name`
    215 	fi
    216 	
    217     header $title
    218     
    219     cat << EOF
    220     <ol class="breadcrumb">
    221   		<li><a href="/$project/">$project</a></li>
    222   		<li><a href="/$project/src">source</a></li>
    223 EOF
    224     href=
    225     for path in `pathSegments $name`; do
    226 	href=$href$path/
    227 	cat << EOF
    228 	    <li class="active"><a href="/$project/src/$href">$path</a></li>
    229 EOF
    230     done
    231     cat << EOF
    232 	</ol>
    233 EOF
    234 
    235 cat << EOF
    236 <ul class="nav nav-tabs">
    237   <li role="presentation"><a href="./">Content</a></li>
    238   <li role="presentation" class="active"><a href="">History</a></li>
    239 </ul>
    240 EOF
    241 
    242 	if	[ -f $dir/README ]; then
    243 		cat << EOF
    244 <div class="panel panel-default">
    245   <div class="panel-body">
    246 EOF
    247     	cat $dir/README
    248 		cat << EOF
    249   </div>
    250 </div>
    251 EOF
    252 	fi
    253 	
    254     cat << EOF
    255 <table class="table"><tr><th>Commit</th><th>User</th><th>Date</th><th>Summary</th><th>Tag</th></tr>
    256 EOF
    257 	hg log -R $REPO_DIR/$project --template '<tr><td><a href="/'$project'/commit/{node}.html">{node|short}</a></td><td>{author}</td><td>{date|isodate}</td><td>{desc}</td><td>{tags}</td></tr>' $dir
    258     echo "</table>"
    259     
    260     footer
    261 }
    262 
    263 
    264 function fileContent
    265 {
    266 
    267     local project=$1
    268     local name=$2
    269     local file=$REPO_DIR/$project/$name
    270     local path
    271 
    272     header `basename $name`
    273 
    274     cat << EOF
    275     <ol class="breadcrumb">
    276   		<li><a href="/$project/">$project</a></li>
    277   		<li><a href="/$project/src">source</a></li>
    278 EOF
    279     href=
    280     for path in `pathSegments $name`; do
    281 	href=$href$path/
    282 	cat << EOF
    283 	    <li class="active"><a href="/$project/src/$href">$path</a></li>
    284 EOF
    285     done
    286     cat << EOF
    287 	</ol>
    288 EOF
    289 
    290 
    291     cat << EOF
    292 <ul class="nav nav-tabs">
    293   <li role="presentation" class="active"><a href="`basename $name`-content.html">Content</a></li>
    294   <li role="presentation"><a href="`basename $name`-history.html">History</a></li>
    295   <li role="presentation"><a href="/$project/raw/$name">Raw</a></li>
    296 </ul>
    297 EOF
    298 
    299     echo "<pre><code>"
    300 
    301     # html-encode
    302     cat $file|sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g'
    303     echo "</code></pre>"
    304 
    305     footer
    306 }
    307 
    308 
    309 function fileHistory
    310 {
    311 	local project=$1
    312 	local name=$2
    313 	local file=$REPO_DIR/$project/$name
    314 	local path
    315 	
    316     header `basename $name`
    317 
    318 
    319     cat << EOF
    320     <ol class="breadcrumb">
    321   		<li><a href="/$project/">$project</a></li>
    322   		<li><a href="/$project/src">source</a></li>
    323 EOF
    324     href=
    325     for path in `pathSegments $name`; do
    326 	href=$href$path/
    327 	cat << EOF
    328 	    <li class="active"><a href="/$project/src/$href">$path</a></li>
    329 EOF
    330     done
    331     cat << EOF
    332 	</ol>
    333 EOF
    334     
    335 
    336 cat << EOF
    337 <ul class="nav nav-tabs">
    338   <li role="presentation"><a href="`basename $name`-content.html">Content</a></li>
    339   <li role="presentation" class="active"><a href="`basename $name`-history.html">History</a></li>
    340   <li role="presentation"><a href="/$project/raw/$name">Raw</a></li>
    341 </ul>
    342 EOF
    343 
    344 	
    345     cat << EOF
    346 <table class="table"><tr><th>Commit</th><th>User</th><th>Date</th><th>Summary</th><th>Tag</th></tr>
    347 EOF
    348 	hg log -R $REPO_DIR/$project --template '<tr><td><a href="/'$project'/commit/{node}.html">{node|short}</a></td><td>{author}</td><td>{date|isodate}</td><td>{desc}</td><td>{tags}</td></tr>' $file
    349     echo "</table>"
    350     
    351     footer
    352 }
    353 
    354 
    355 
    356 
    357 function pathSegments
    358 {
    359     filename=$1
    360 
    361     for path in `dirname $filename|tr "/" "\n"`; do
    362 	if	[ $path == "." ]; then
    363 	    continue;
    364 	fi
    365 	echo $path
    366     done
    367 }
    368 
    369 
    370 # commit
    371 # param 1: project name
    372 # param 2: commit hash
    373 function commit
    374 {
    375 	local project=$1
    376 	local commit=$2
    377 	
    378     header $name
    379     
    380     cat << EOF
    381     <ol class="breadcrumb">
    382   		<li><a href="../">$project</a></li>
    383   		<li class="active">Commit $commit</li>
    384 	</ol>
    385 EOF
    386 
    387 	
    388     cat << EOF
    389 EOF
    390 	echo "<pre><code>"
    391 	hg log -r $commit -p -R $REPO_DIR/$project
    392 	echo "</code></pre>"
    393     
    394     footer
    395 }
    396 
    397 
    398 
    399 function projectSummary
    400 {
    401     project=$1
    402     header $project
    403 
    404     cat << EOF
    405     <ol class="breadcrumb">
    406   		<li class="active">$project</li>
    407 	</ol>
    408 EOF
    409     
    410 cat << EOF
    411 
    412 <div class="well">Clone this repository with <code>hg clone static-http://$DOMAIN/$project</code></div>
    413 <ul class="nav nav-pills nav-stacked">
    414 <li role="presentation"><a href="/$project/$project.tar.gz">Download Tarball</a></li>
    415 <li role="presentation"><a href="/$project/changelog.txt">Changelog</a></li>
    416 <li role="presentation"><a href="/$project/src/">Source</a></li>
    417 </li>
    418 EOF
    419 
    420 	if	[ -f $REPO_DIR/$project/README ]; then
    421 		cat << EOF
    422 <div class="panel panel-default">
    423   <div class="panel-body">
    424 EOF
    425     	cat $REPO_DIR/$project/README
    426 		cat << EOF
    427   </div>
    428 </div>
    429 EOF
    430 	fi
    431 
    432     footer
    433 }
    434 
    435 
    436 # Create all Project files
    437 # param 1: project name
    438 function project
    439 {
    440     local name=$1
    441     local dir=$OUT_DIR/$name
    442 
    443     if	[ -f $dir/$name.tar.gz ]; then
    444         if	[ `hg log -R $REPO_DIR/$name -l 1 --template '{date(date,"%s")}'` -lt `stat -c "%Y" $dir/$name.tar.gz ` ]; then
    445 	    echo "$name did not change."
    446 	    return;
    447 	fi
    448     fi
    449     echo "Code-project $name has changed, now updating."
    450 
    451 
    452     rm -rf $dir/raw
    453     mkdir -p $dir/raw
    454 
    455     rm -rf $dir/src
    456     mkdir $dir/src
    457 
    458     hg clone -q $REPO_DIR/$name $dir/raw
    459     # Clean .hg
    460     rm -rf $dir/raw/.hg
    461 
    462 
    463 
    464     projectSummary $name > $dir/index.html
    465 
    466     for f in `cd $dir/raw && find . -type d`; do
    467 
    468         mkdir -p $dir/src/$f
    469 	folderContent $name $f > $dir/src/$f/index.html
    470 	folderHistory $name $f > $dir/src/$f/history.html
    471     done
    472 
    473     for f in `cd $dir/raw && find . -type f`; do
    474 	fileContent $name $f > $dir/src/$f-content.html
    475 	fileHistory $name $f > $dir/src/$f-history.html
    476     done
    477 
    478 
    479     #rm -rf $dir/commit
    480     mkdir -p $dir/commit
    481     for c in `hg log -R $REPO_DIR/$name --template '{node}\n'`; do
    482 	if [ ! -f $dir/commit/$c.html ]; then
    483 	    commit $name $c > $dir/commit/$c.html
    484 	fi
    485     done
    486 
    487 
    488     # create static archive (without .hg)
    489     `cd $dir/raw && tar cfz ../$name.tar.gz *`
    490     hg -R $REPO_DIR/$name log --style changelog > $dir/changelog.txt
    491 }
    492 
    493 
    494 
    495 
    496 # Create start page with all projects
    497 rootIndex > $OUT_DIR/index.html
    498 
    499 
    500 rm -f $OUT_DIR/.htaccess
    501 echo "# generated" > $OUT_DIR/.htaccess
    502 
    503 # Loop over all projects
    504 for f in $REPO_DIR/*; do
    505 
    506     project=`basename $f`
    507 	
    508     # is this a Mercurial archive?
    509     if [ ! -d $f/.hg ]; then
    510 		# not Mercurial versioned 
    511 		continue;
    512     fi
    513 
    514     # Call the project
    515     project $project
    516 
    517     # Serve the RAW files as text/plain (not all, images should stay images)
    518     echo "AddType text/plain .java .php .txt .xml .json" > $OUT_DIR/$project/raw/.htaccess
    519 done
    520