#!/bin/bash # Author: Daniel Mohr # Date: 2022-06-27 # # This script should run as gitolite trigger at 'POST_GIT'. # It will add a landing page on the default branch. # # If necessary, in the commited repository the branch 'landing_page' # is created. # # In the branch 'landing_page' a file 'index.html' is created based # on the default branch (HEAD) # command line parameters and environment are desribed on: # https://gitolite.com/gitolite/triggers.html # run trigger from gitolite account command line: # GL_LOGFILE="$HOME/.gitolite/logs/gitolite-2022-06.log" GL_REPO_BASE="$HOME/repositories" GL_REPO="foo/foo" /usr/share/gitolite3/triggers/riaf_landing_page POST_GIT foo/foo testuser W any git-receive-pack # we should use jq to extract values from .dabu.json and # present on landing page (e. g. author, license, ...) # https://stedolan.github.io/jq/manual/ #set -e runtime=$(date +%Y-%m-%d.%H:%M:%S) if [ $# -gt 0 ]; then # check command line parameters trigger=$1 else exit 1 fi if [ "$trigger" != "POST_GIT" ]; then # check first command line parameter exit 1 fi if [ "$6" != "git-receive-pack" ]; then exit 0 fi echo -e "$runtime\t$$\tdo riaf_landing_page $(date -Iseconds)" >> "$GL_LOGFILE" # POST_GIT a/bar testuser W any git-receive-pack # $HOME # $GL_REPO_BASE # $GL_REPO # $GL_LOGFILE # $GL_USER repopath=$GL_REPO_BASE/$GL_REPO.git tmpdira=$(mktemp --directory) tmpdirb=$(mktemp --directory) # Maybe we should use a better tmpdir to allow hardlinking (--local). git clone --depth 1 --no-single-branch "$repopath" "$tmpdira" # We assume here, that server_timestamping was done just before this script # runs. # check if last commit was on branch 'server_timestamping' lastcommittedbranch=$(cd "$tmpdira" && git branch --list --all --sort=creatordate --format "%(refname)" | tail -n 1) echo "$lastcommittedbranch" | grep --quiet "server_timestamping" && res=1 || res=0 if [ $res == 0 ]; then # the last commit was not done to default branch (HEAD) rm -rf "$tmpdira" "$tmpdirb" echo -e "$runtime\t$$\tdo not riaf_landing_page on branch $lastcommittedbranch" >> "$GL_LOGFILE" exit 0 fi # check if metadata available isdabu=0 if [ -f "$tmpdira/.dabu.json" ]; then if [ -f "$tmpdira/.dabu.schema" ]; then isdabu=1 fi fi if [ $isdabu = 0 ]; then # no metadata availalbe rm -rf "$tmpdira" "$tmpdirb" echo -e "$runtime\t$$\tdo not riaf_landing_page without metadata in repo" >> "$GL_LOGFILE" exit 0 fi # check if metadata are ok #jsonschema -i "$tmpdira/.dabu.json" "$tmpdira/.dabu.schema" ; dabunotOK=$? pydabu check_data_bubble -dir "$tmpdira" ; dabunotOK=$? if [ $dabunotOK = 0 ]; then cp -p "$tmpdira/.dabu.json" "$tmpdira/.dabu.schema" "$tmpdirb" else pydabuerror=$(pydabu check_data_bubble -dir "$tmpdira") fi # set git environment in repo (cd "$tmpdira" && git config user.name "$USER") (cd "$tmpdira" && git config user.email "$USER@$(hostname)") # check if branch landing_page available (cd "$tmpdira" && git branch --list --all | grep --quiet landing_page) && res=1 || res=0 if [ $res == 0 ]; then echo -e "$runtime\t$$\tcreate orphan branch landing_page in $repopath" >> "$GL_LOGFILE" (cd "$tmpdira" && git checkout --orphan landing_page) (cd "$tmpdira" && git reset --hard) else (cd "$tmpdira" && git checkout landing_page) fi index="$tmpdira/index.html" { echo '' echo '' echo '' echo "

$GL_REPO

" } > "$index" if [ $dabunotOK = 0 ]; then cp -p "$tmpdirb/.dabu.json" "$tmpdira" (cd "$tmpdira" && git add .dabu.json) { echo '

meta data ".dabu.json" follow ".dabu.schema"

' echo '

' } >> "$index" else { echo "

error

" echo '

meta data ".dabu.json" do not follow ".dabu.schema":' echo "

$pydabuerror

" } >> "$index" fi { echo '' echo '' } >> "$index" (cd "$tmpdira" && git add index.html) if [ $dabunotOK = 0 ]; then if [ $res == 0 ]; then (cd "$tmpdira" && git commit -m "added landing page and .dabu.json") else (cd "$tmpdira" && git commit -m "updated landing page and adding .dabu.json") fi else if [ $res == 0 ]; then (cd "$tmpdira" && git commit -m "added landing page with error message") else (cd "$tmpdira" && git commit -m "updated landing page with error message") fi fi if [ $res == 0 ]; then (cd "$tmpdira" && git push --set-upstream origin landing_page) else (cd "$tmpdira" && git push) fi rm -rf "$tmpdira" "$tmpdirb" #env > /data/gitolite/e1 #echo $* > /data/gitolite/e2 echo -e "$runtime\t$$\tdid riaf_landing_page in $repopath" >> "$GL_LOGFILE"