random

- collection of un-sorted bollocks
git clone git://git.acid.vegas/random.git
Log | Files | Refs | Archive

stagit (15079B)

      1 #!/bin/sh
      2 # developed by acidvegas (https://git.acid.vegas/random)
      3 
      4 #set -ev
      5 
      6 ##### settings ########################################################################
      7 CLONE_URL='remote'   # base url for cloning repositories ('remote' = remote.origin.url)
      8 CNAME=''             # create a CNAME file with a custom domain (empty = do not create)
      9 MAX_DESC=250         # maximum characters in a description to display (0 = all)
     10 MAX_COMMITS=10      # maximum number of commits to show (0 = all)
     11 MAX_COMMIT_MSG=100   # maximum characters in a commit message to display (0 = all)
     12 MAX_COMMIT_LINES=10 # maximum number of lines to show in a commit (0 = all)
     13 REPO_DIR=$HOME/dev/git/acidvegas   # directory containing repositories
     14 TITLE='Acidvegas Repositories' # title used on homepage
     15 WWW_DIR=$HOME/www    # directory to output to
     16 #######################################################################################
     17 
     18 generate_404() {
     19 	{
     20 		echo "<!doctype html>"
     21 		echo "<title>Error</title>"
     22 		echo "<style>body{background-color:#111;color:#fff;font-family:'Courier New',monospace}</style>" # font family
     23 		echo "<br><br><br><center><h1>404</h1></center>"
     24 	} > $WWW_DIR/404.html
     25 }
     26 
     27 generate_css() {
     28 	{
     29 		echo "*{font-family:'Courier New',monospace}"
     30 		echo "a{color:#8dc}"
     31 		echo "a:hover{color:#8cd}"
     32 		echo "body{background-color:#111;color:#fff;;margin-top:0px}"
     33 		echo "h1{margin:0px}"
     34 		echo "h1 a{color:#fff;text-decoration:none}"
     35 		echo "h2{margin:0px}"
     36 		echo "h4{color:#777;margin:0px}"
     37 		echo "h5{font-style:italic;font-weight:normal;margin:0px}"
     38 		echo "hr{border:none;border-bottom:1px dashed #fff}"
     39 		echo "pre{margin:0px}"
     40 		echo ".bold{font-weight:bold}"
     41 		echo ".branch{color:green;font-weight:bold}"
     42 		echo ".commit{color:olive}"
     43 		echo ".deletion{color:red}"
     44 		echo ".hunk{color:teal}"
     45 		echo ".insertion{color:green}"
     46 		echo ".reference{color:teal;font-weight:bold}"
     47 		echo ".remote{color:red;font-weight:bold}"
     48 		echo ".item:hover{background-color:#1a1a1a}"
     49 		echo ".item td{white-space:nowrap}"
     50 		echo ".linenumber{text-align:right;-moz-user-select:-moz-none;-khtml-user-select: none;-webkit-user-select:none}"
     51 		echo ".linenumber a{text-decoration:none}"
     52 		echo "#footer{border-top:1px dashed #fff;color:#444;font-size:smaller;margin-top:10px;padding-top:5px;width:100%}"
     53 		echo "#footer a{color:#aaa;text-decoration:none}"
     54 		echo "#main{border-collapse:collapse;table-layout:fixed}"
     55 		echo "#main td,th{padding-left:5px;padding-right:5px}"
     56 		echo "#navigation{color:#fff;font-weight:900;text-align:left}"
     57 	} > $WWW_DIR/style.css
     58 }
     59 
     60 generate_files() {
     61 	_REPO_DIR=$1
     62 	_REPO_NAME=$2
     63 	_REPO_URL=$3
     64 	shift 3
     65 	_REPO_DESC="${@}"
     66 	mkdir $WWW_DIR/$_REPO_NAME/files
     67 	if [ -f $_REPO_DIR/../README.md ]; then
     68 		curl https://api.github.com/markdown/raw -X "POST" -H "Content-Type: text/plain" -d "$(cat $_REPO_DIR/../README.md)" > $WWW_DIR/$REPO_NAME/files/README.md.html
     69 		OPT_FILES=" | <a href=\"files/README.md.html\">README</a>"
     70 	fi
     71 	if [ -f $_REPO_DIR/../LICENSE ]; then
     72 		OPT_FILES="$OPT_FILES | <a href=\"files/LICENSE.html\">LICENSE</a>"
     73 	fi
     74 	{
     75 		echo "<!doctype html>"
     76 		echo "<link rel=\"stylesheet\" href=\"../../style.css\">"
     77 		echo "<title>$_REPO_NAME</title>"
     78 		echo "<h1><a href=\"../index.html\" title=\"Home\">&#8962;&#xFE0E;</a> $_REPO_NAME</h1>"
     79 		echo "<div style=\"padding-left:25px\">"
     80 		echo "<h4>$_REPO_DESC</h4>"
     81 		echo "<h5>git clone <a href=\"$_REPO_URL\" title=\"$_REPO_NAME clone URL\">$_REPO_URL</a></h5>"
     82 		echo "<br><h4><a href=\"index.html\">Logs</a> | <a href=\"files.html\">Files</a> | <a href=\"refs.html\">Refs</a>$OPT_FILES</h4>"
     83 		echo "</div>"
     84 		echo "<hr>"
     85 		echo "<table id=\"main\" cellpadding=\"0\" cellspacing=\"0\">"
     86 		echo "<tr id=\"navigation\"><th></th><th>Mode</th><th>Name</th><th align=\"right\">Size</th></tr>"
     87 	} > $WWW_DIR/$_REPO_NAME/files/index.html
     88 	FILES=$(find $_REPO_DIR/../* -name .git -prune -o  -exec sh -c 'stat --format="%A|%n|%s" {}' \; | tail -n +2)
     89 	for f in $FILES; do
     90 		PERMISSIONS=$(echo "$f" | cut -d'|' -f1)
     91 		FILE_NAME=$(echo "$f" | cut -d'|' -f2)
     92 		FILE_SIZE=$(echo "$f" | cut -d'|' -f3)
     93 		echo "<tr class=\"item\"><td>$PERMISSIONS</td><td><a href=\"$FILE_NAME.html\">$FILE_NAME</a></td><td>$FILE_SIZE</td>" >> $WWW_DIR/$_REPO_NAME/files/index.html
     94 	done
     95 	echo "</table>" >> $WWW_DIR/$_REPO_NAME/files/index.html
     96 	generate_footer $WWW_DIR/$_REPO_NAME/files/index.html
     97 }
     98 
     99 generate_index() {
    100 	{
    101 		echo "<!doctype html>"
    102 		echo "<link rel=\"stylesheet\" href=\"style.css\">"
    103 		echo "<title>$TITLE</title>"
    104 		echo "<h1>&#8962;&#xFE0E; $TITLE</h1>"
    105 		echo "<hr>"
    106 		echo "<table id=\"main\" cellpadding=\"0\" cellspacing=\"0\">"
    107 		echo "<tr id=\"navigation\"><th>Name</th><th>Description</th><th>Owner</th><th>Last commit</th></tr>"
    108 	} > $WWW_DIR/index.html
    109 }
    110 
    111 generate_logs() {
    112 	_REPO_DIR=$1
    113 	_REPO_NAME=$2
    114 	_REPO_URL=$3
    115 	shift 3
    116 	_REPO_DESC="${@}"
    117 	mkdir -p $WWW_DIR/$_REPO_NAME/commit
    118 	{
    119 		echo "<!doctype html>"
    120 		echo "<link rel=\"stylesheet\" href=\"../style.css\">"
    121 		echo "<title>$_REPO_NAME</title>"
    122 		echo "<h1><a href=\"../index.html\" title=\"Home\">&#8962;&#xFE0E;</a> $_REPO_NAME</h1>"
    123 		echo "<div style=\"padding-left:25px\">"
    124 		echo "<h4>$_REPO_DESC</h4>"
    125 		echo "<h5>git clone <a href=\"$_REPO_URL\" title=\"$_REPO_NAME clone URL\">$_REPO_URL</a></h5>"
    126 		echo "<br><h4><a href=\"#\">Logs</a> | <a href=\"files.html\">Files</a> | <a href=\"refs.html\">Refs</a> | <a href=\"file/README.md.html\">README</a> | <a href=\"file/LICENSE.html\">LICENSE</a></h4>"
    127 		echo "</div>"
    128 		echo "<hr>"
    129 		echo "<table id=\"main\" cellpadding=\"0\" cellspacing=\"0\">"
    130 		echo "<tr id=\"navigation\"><th></th><th>Hash</th><th>Commit message</th><th>Author</th><th>Date</th><th align=\"right\">Files</th><th align=\"right\">+</th><th align=\"right\">-</th></tr>"
    131 	} > $WWW_DIR/$_REPO_NAME/index.html
    132 	REPO_COMMITS=$(git --git-dir $_REPO_DIR log --all --date=format:"%Y-%m-%d %H:%M" --pretty=format:"%G?|%GS|%GK|%h|%s|%cn|%cd")
    133 	REMAINING_COMMITS=$(echo "$REPO_COMMITS" | wc -l)
    134 	if [ $REMAINING_COMMITS -gt $MAX_COMMITS ]; then
    135 		REMAINING_COMMITS=$(($REMAINING_COMMITS-$MAX_COMMITS))
    136 		if [ $MAX_COMMITS -ne 0 ]; then
    137 			REPO_COMMITS=$(echo "$REPO_COMMITS" | head -n $MAX_COMMITS)
    138 		fi
    139 	else
    140 		REMAINING_COMMITS=0
    141 	fi
    142 	echo "$REPO_COMMITS" | while IFS= read -r line; do
    143 		COMMIT_SIGNED=$(echo $line | cut -d'|' -f1)
    144 		COMMIT_KEY=$(echo $line | cut -d'|' -f2)
    145 		COMMIT_KEYID=$(echo $line | cut -d'|' -f3)
    146 		if [ $COMMIT_SIGNED = 'G' ]; then
    147 			COMMIT_SIGNED="<span style=\"color:green\" title=\"$COMMIT_KEY&#13;$COMMIT_KEYID\">&#128274;&#xFE0E;</span>"
    148 		elif [ $COMMIT_SIGNED = 'B' ] || [ $COMMIT_SIGNED = 'U' ]  || [ $COMMIT_SIGNED = 'X' ] || [ $COMMIT_SIGNED = 'Y' ] || [ $COMMIT_SIGNED = 'R' ]; then
    149 			COMMIT_SIGNED="<span style=\"color:red\" title=\"$COMMIT_KEY&#13;$COMMIT_KEYID&#13;key is bad, invalid, expired, or revoked\">&#128274;&#xFE0E;</span>"
    150 		else
    151 			COMMIT_SIGNED=""
    152 		fi
    153 		COMMIT_HASH=$(echo $line | cut -d'|' -f4)
    154 		COMMIT_MESSAGE=$(echo $line | cut -d'|' -f5 | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;s/"/\&quot;/g;')
    155 		if [ $MAX_COMMIT_MSG -ne 0 ] && [ ${#COMMIT_MESSAGE} -gt $MAX_COMMIT_MSG ]; then
    156 			COMMIT_MESSAGE=$(echo $COMMIT_MESSAGE | cut -c 1-$MAX_COMMIT_MSG)
    157 		fi
    158 		COMMIT_AUTHOR=$(echo $line | cut -d'|' -f6)
    159 		COMMIT_DATE=$(echo $line | cut -d'|' -f7)
    160 		COMMIT_SUMMARY=$(git --git-dir $_REPO_DIR show $COMMIT_HASH --stat | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6} END {printf "%s|+%s|-%s", files, inserted, deleted }')
    161 		COMMIT_FILES=$(echo $COMMIT_SUMMARY | cut -d'|' -f1)
    162 		COMMIT_INSERTIONS=$(echo $COMMIT_SUMMARY | cut -d'|' -f2)
    163 		COMMIT_DELETIONS=$(echo $COMMIT_SUMMARY | cut -d'|' -f3)
    164 		echo "<tr class=\"item\"><td>$COMMIT_SIGNED</td><td><a href=\"commit/$COMMIT_HASH.html\" title=\"View this commit\">$COMMIT_HASH</a></td><td>$COMMIT_MESSAGE</td><td>$COMMIT_AUTHOR</td><td>$COMMIT_DATE</td><td align=\"right\">$COMMIT_FILES</td><td align=\"right\">$COMMIT_INSERTIONS</td><td align=\"right\">$COMMIT_DELETIONS</td></tr>" >> $WWW_DIR/$_REPO_NAME/index.html
    165 		{
    166 			echo "<!doctype html>"
    167 			echo "<link rel=\"stylesheet\" href=\"../../style.css\">"
    168 			echo "<title>$_REPO_NAME :: $COMMIT_HASH</title>"
    169 			echo "<h1><a href=\"../index.html\" title=\"Back to Repository\">&larr;&#xFE0E;</a> $_REPO_NAME :: $COMMIT_HASH</h1>"
    170 			echo "<hr>"
    171 			echo "<table id=\"main\" cellpadding=\"0\" cellspacing=\"0\">"
    172 		} > $WWW_DIR/$_REPO_NAME/commit/$COMMIT_HASH.html
    173 		COMMIT_DIFF=$(git --git-dir $_REPO_DIR show $COMMIT_HASH -p --stat --decorate --color --expand-tabs=4 | tr -d '\0' | sed '/\\ No newline at end of file/d')
    174 		if [ $MAX_COMMIT_LINES -ne 0 ] && [ $(echo "$COMMIT_DIFF" | wc -l) -gt $MAX_COMMIT_LINES ]; then
    175 			COMMIT_DIFF=$(echo "$COMMIT_DIFF" | head -n $MAX_COMMIT_LINES)
    176 		fi
    177 		COMMIT_DIFF=$(echo "$COMMIT_DIFF" | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;s/"/\&quot;/g;' | tr -d '\r') # escape html
    178 		COMMIT_DIFF=$(echo "$COMMIT_DIFF" | sed 's/\[31m/<span class="deletion">/g; s/\[1\;31m/<span class="remote">/g; s/\[32m/<span class="insertion">/g; s/\[1\;32m/<span class="branch">/g; s/\[33m/<span class="commit">/g; s/\[36m/<span class="hunk">/g; s/\[1\;36m/<span class="reference">/g; s/\[1m/<span class="bold">/g; s/\[m/<\/span>/g') # add some color
    179 		COUNT=1
    180 		echo "$COMMIT_DIFF" | while IFS= read -r line; do
    181 			echo "<tr class=\"item\"><td class=\"linenumber\"><a href=\"#l$COUNT\" name=\"l$COUNT\">$COUNT</a></td><td><pre>$line</pre></td></tr>" >> $WWW_DIR/$_REPO_NAME/commit/$COMMIT_HASH.html
    182 			COUNT=`expr $COUNT + 1`
    183 		done
    184 		echo "</table>" >> $WWW_DIR/$_REPO_NAME/commit/$COMMIT_HASH.html
    185 		generate_footer $WWW_DIR/$_REPO_NAME/commit/$COMMIT_HASH.html
    186 
    187 	done
    188 	if [ $REMAINING_COMMITS -ne 0 ]; then
    189 		echo "<tr class=\"item\"><td></td><td></td><td><br><small><b><i>$REMAINING_COMMITS more commits remaining, fetch the repository</i></b></small></td><td></td><td></td><td></td><td></td><td></td></tr>" >> $WWW_DIR/$_REPO_NAME/index.html
    190 	fi
    191 	echo "</table>" >> $WWW_DIR/$_REPO_NAME/index.html
    192 	generate_footer $WWW_DIR/$_REPO_NAME/index.html
    193 }
    194 
    195 generate_readme() {
    196 	echo "not done"
    197 }
    198 
    199 generate_refs() {
    200     _REPO_DIR=$1
    201     _REPO_NAME=$2
    202     _REPO_URL=$3
    203     shift 3
    204     _REPO_DESC="${@}"
    205 
    206 	_BRANCHES=$(git --git-dir $_REPO_DIR branch --sort=-committerdate --format='%(refname:short)|%(committerdate:short)|%(authorname)')
    207 	_TAGS=$(git --git-dir $_REPO_DIR log --tags --simplify-by-decoration --format="%S|%cs|%an")
    208 	{
    209 		echo "<!doctype html>"
    210 		echo "<link rel=\"stylesheet\" href=\"../style.css\">"
    211 		echo "<title>$_REPO_NAME</title>"
    212 		echo "<h1><a href=\"../index.html\" title=\"Home\">&#8962;&#xFE0E;</a> $_REPO_NAME</h1>"
    213 		echo "<div style=\"padding-left:25px\">"
    214 		echo "<h4>$_REPO_DESC</h4>"
    215 		echo "<h5>git clone <a href=\"$_REPO_URL\" title=\"$_REPO_NAME clone URL\">$_REPO_URL</a></h5>"
    216 		echo "<br><h4><a href=\"index.html\">Logs</a> | <a href=\"files.html\">Files</a> | <a href=\"#\">Refs</a> | <a href=\"file/README.md.html\">README</a> | <a href=\"file/LICENSE.html\">LICENSE</a></h4>"
    217 		echo "</div>"
    218 		echo "<hr>"
    219 		echo "<h2>Branches</h2>"
    220 		echo "<table id=\"main\" cellpadding=\"0\" cellspacing=\"0\">"
    221 		echo "<tr id=\"navigation\"><th>Name</th><th>Last commit date</th><th>Author</th></tr>"
    222 	} > $WWW_DIR/$_REPO_NAME/refs.html
    223 	echo "$_BRANCHES" | while IFS= read -r line; do
    224 		BRANCH_NAME=$(echo $line | cut -d'|' -f1)
    225 		BRANCH_DATE=$(echo $line | cut -d'|' -f2)
    226 		BRANCH_AUTHOR=$(echo $line | cut -d'|' -f3)
    227 		echo "<tr class=\"item\"><td>$BRANCH_NAME</td><td>$BRANCH_DATE</td><td>$BRANCH_AUTHOR</td></tr>" >> $WWW_DIR/$_REPO_NAME/refs.html
    228 	done
    229 	echo "</table>" >> $WWW_DIR/$_REPO_NAME/refs.html
    230 	if [ ! -z "$_TAGS" ]; then
    231 		{
    232 			echo "<br><h2>Tags</h2>"
    233 			echo "<table id=\"main\" cellpadding=\"0\" cellspacing=\"0\">"
    234 			echo "<tr id=\"navigation\"><th>Name</th><th>Last commit date</th><th>Author</th></tr>"
    235 		} >> $WWW_DIR/$_REPO_NAME/refs.html
    236 		echo "$_TAGS" | while IFS= read -r line; do
    237 			TAG_NAME=$(echo $line | cut -d'|' -f1)
    238 			TAG_DATE=$(echo $line | cut -d'|' -f2)
    239 			TAG_AUTHOR=$(echo $line | cut -d'|' -f3)
    240 			echo "<tr class=\"item\"><td>$TAG_NAME</td><td>$TAG_DATE</td><td>$TAG_AUTHOR</td></tr>" >> $WWW_DIR/$_REPO_NAME/refs.html
    241 		done
    242 		echo "</table>" >> $WWW_DIR/$_REPO_NAME/refs.html
    243 	else
    244 		echo "<br>No tags found for this repository" >> $WWW_DIR/$_REPO_NAME/refs.html
    245 	fi
    246 	generate_footer $WWW_DIR/$_REPO_NAME/refs.html
    247 }
    248 
    249 generate_yaml() {
    250 	{
    251 		echo "pages:"
    252 		echo "  stage: deploy"
    253 		echo "  script:"
    254 		echo "  - mkdir .public"
    255 		echo "  - cp -r * .public"
    256 		echo "  - mv .public public"
    257 		echo "  artifacts:"
    258 		echo "    paths:"
    259 		echo "    - public"
    260 		echo "  only:"
    261 		echo "  - master"
    262 	} > $WWW_DIR/.gitlab-ci.yml
    263 }
    264 
    265 generate_footer() {
    266 	_DATE=$(date +"%m/%d/%y")
    267 	echo "<div id=\"footer\">site generated by <a href=\"https://github.com/acidvegas/stagit\" target=\"_blank\">stagit</a> on <a href=\"#\">$_DATE</a></div>" >> $1
    268 }
    269 
    270 # main
    271 if [ ! -d $REPO_DIR ]; then
    272 	echo "'$REPO_DIR' directory does not exist" && exit 1
    273 fi
    274 REPOS=$(for f in $(find $REPO_DIR -type d -name mirror -prune -o -type d -name .git -print); do echo "$(basename $(dirname $f)):$(dirname $f)"; done | sort | cut -d':' -f2) # Can we clean improve this?
    275 if [ -z "$REPOS" ]; then
    276 	echo "'$REPO_DIR' contains no repositories" && exit 1
    277 elif [ -d $WWW_DIR ]; then
    278 	rm -rf $WWW_DIR/*
    279 else
    280 	mkdir -p $WWW_DIR
    281 fi
    282 if [ -n $CNAME ]; then
    283 	echo $CNAME > $WWW_DIR/CNAME
    284 fi
    285 generate_404 && generate_css && generate_index && generate_yaml
    286 for REPO in $REPOS; do
    287 	REPO=$REPO/.git # Can we do without this?
    288 	REPO_NAME=$(basename -s .git `git --git-dir $REPO config --get remote.origin.url`)
    289 	echo "generating $REPO_NAME files"
    290 	mkdir $WWW_DIR/$REPO_NAME
    291 	if [ -f $REPO/description ]; then
    292 		REPO_DESC=$(cat $REPO/description)
    293 		if [ "$REPO_DESC" = "Unnamed repository; edit this file 'description' to name the repository." ]; then
    294 			REPO_DESC="no description available"
    295 		fi
    296 		if [ "$REPO_DESC" = "no description available" ]; then
    297 			echo "edit description for $REPO_NAME ($REPO/description)"
    298 		elif [ $MAX_DESC -ne 0 ]; then
    299 			REPO_DESC=$(echo $REPO_DESC | cut -c -$MAX_DESC)
    300 		fi
    301 	else
    302 		echo "missing description file for $REPO_NAME ($REPO/description)"
    303 		REPO_DESC="no description available"
    304 	fi
    305 	REPO_OWNER=$(git --git-dir $REPO log --reverse -1 --pretty=format:'%an') # note: want to parse organization names for github/gitlab
    306 	REPO_LAST_COMMIT=$(git --git-dir $REPO log -1 --pretty=format:'%cs')
    307 	echo "<tr class=\"item\"><td><a href=\"$REPO_NAME\" title=\"$REPO_NAME\">$REPO_NAME</a></td><td>$REPO_DESC</td><td>$REPO_OWNER</td><td>$REPO_LAST_COMMIT</td></tr>" >> $WWW_DIR/index.html
    308 	if [ $CLONE_URL = 'remote' ]; then
    309 		REPO_URL=$(git --git-dir $REPO config --get remote.origin.url | sed 's#git@github.com:#https://github.com/#' | sed 's#git@gitlab.com:#https://gitlab.com/#')
    310 	else
    311 		REPO_URL=$CLONE_URL/$REPO_NAME.git
    312 	fi
    313 	generate_files   $REPO $REPO_NAME $REPO_URL $REPO_DESC
    314 	generate_logs    $REPO $REPO_NAME $REPO_URL $REPO_DESC
    315 	generate_refs    $REPO $REPO_NAME $REPO_URL $REPO_DESC
    316 done
    317 echo "</table>" >> $WWW_DIR/index.html
    318 generate_footer $WWW_DIR/index.html