commit:     49748d5bfee72b7e80a93a11250a1ba78eadfcab
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Sun Sep  6 13:24:28 2015 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Sun Sep  6 13:24:28 2015 +0000
URL:        https://gitweb.gentoo.org/proj/qa-scripts.git/commit/?id=49748d5b

cgi: Add script to output files from git repo

 cgi-bin/get-git-file.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/cgi-bin/get-git-file.sh b/cgi-bin/get-git-file.sh
new file mode 100755
index 0000000..8b48b08
--- /dev/null
+++ b/cgi-bin/get-git-file.sh
@@ -0,0 +1,48 @@
+#!/usr/bin/env bash
+
+if [[ ! ${QUERY_STRING} ]]; then
+       echo "Script must be run through CGI" >&2
+       exit 1
+fi
+
+main() {
+       local qs=${QUERY_STRING}
+       local repo=${qs%%;*}
+       qs=${qs#*;}
+       local commit=${qs%%;*}
+       qs=${qs#*;}
+       local file=${qs%%;*}
+
+       if [[ ${repo} == */* ]]; then
+               echo "DANGER! SOMEONE TRIES TO ABUSE ME!" >&2
+               exit 1
+       fi
+
+       if ! cd "$(dirname "${0}")/../htdocs/output/${repo}" 2>/dev/null; then
+               echo "Status: 404 Not Found"
+               echo
+               echo "404 Not Found"
+               exit 0
+       fi
+
+       local tree=( $(git ls-tree "${commit}" "${file}" 2>/dev/null) )
+       if [[ ! ${tree[*]} ]]; then
+               echo "Status: 404 Not Found"
+               echo
+               echo "404 Not Found"
+               exit 0
+       fi
+
+       local ct
+       case "${file}" in
+               *.css) ct=text/css;;
+               *.html) ct=text/html;;
+               *) ct=text/plain;;
+       esac
+
+       echo "Content-Type: ${ct}"
+       echo
+       git cat-file -p "${tree[2]}"
+}
+
+main

Reply via email to