factor out gitweb-descriptions
[iankelling.org] / gitweb-descriptions
diff --git a/gitweb-descriptions b/gitweb-descriptions
new file mode 100755 (executable)
index 0000000..34c797f
--- /dev/null
@@ -0,0 +1,69 @@
+#!/bin/bash -l
+
+# my projects all have README or --help with a short single line
+# description which I parse and put into the gitweb description.
+
+# If it's a repo with just a single bash script or *-function script,
+# then call it with --help, grap the 2nd line.
+# else, parse the readme, find the first non blank, not starting with [#*],
+# and use that line.
+
+set -eE -o pipefail
+trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
+
+if [[ ! $1 ]]; then
+    echo "need gitroot arg"
+    exit 1
+fi
+
+gitroot=$1
+cd $gitroot
+
+shopt -s extglob
+
+_git_desc_readme() {
+    while read -r line; do
+        [[ $line ]] || continue
+        if echo "$line" | grep "^ *[#*]" &>/dev/null; then
+            continue
+        fi
+        echo "$line" > .git/description
+        break
+    done < README*
+}
+
+dirs=()
+for d in $gitroot/*; do
+    if [[ -d $d && ! -L $d ]]; then
+        for sub in $d/*; do
+            dirs+=($sub)
+        done
+    else
+        dirs+=($d)
+    fi
+done
+
+gitweb-descriptions() {
+    for d in ${dirs[@]}; do
+        d=$(readlink -f $d)
+        cd $d/..
+        e ${PWD##*/}
+        shopt -s nullglob
+        f=(!(LICENSE|COPYING|README|.git))
+        shopt -u nullglob
+        if [[ ${#f[@]} == 1 && ! -d $f ]]; then
+            if [[ ! -x $f ]]; then
+                if [[ $f == *-function ]]; then
+                    ${f%-function} --help | sed -n '2p' > .git/description
+                else
+                    _git_desc_readme
+                fi
+            else
+                $f --help | sed -n '2p' > .git/description
+            fi
+        else
+            _git_desc_readme
+        fi
+    done
+}
+gitweb-descriptions "$@"