#!/bin/bash # # pre-commit hook script that activates the Sesame serializer to serialize any OWL files in a standard format just before # they are committed to the (local) Github repository. # # Called by "git commit" with no arguments. This hook can stop the commit when it encounters an error. # # To enable this hook, rename this file to "pre-commit" and save it in the directory /.git/hooks # ### CONFIG ### # If you want to set your Java directory, uncomment the following line and change to your Java directory # export RDF_TOOLKIT_JAVA_HOME= # Redirect all output generated by this script to stderr. exec 1>&2 log_prefix="rdf-toolkit: sesame-serializer: " function log() { echo "${log_prefix}$@" } function log_error() { log "ERROR: $@" } log "This is the pre-commit hook" java_exe="" base_dir="" function findJava() { whichJava="" java_home="" if [ "${RDF_TOOLKIT_JAVA_HOME}" != "" ] ; then java_home="${RDF_TOOLKIT_JAVA_HOME}" elif [ "${JAVA_HOME}" != "" ] ; then java_home="${JAVA_HOME}" fi if [ "${java_home}" == "" ] ; then log_error A-$JAVA_HOME log_error "Please set RDF_TOOLKIT_JAVA_HOME or JAVA_HOME to point to a Java 7 or later installation." return 1 fi java_home=${java_home/C:\\/\/c\/} java_home=${java_home//\\/\/} log "java_home =" ${java_home} if [ -x "${java_home}/bin/java" ] ; then whichJava="${java_home}/bin/java" else log_error "Could not find java in your RDF_TOOLKIT_JAVA_HOME or JAVA_HOME: ${java_home}" log_error "Please set RDF_TOOLKIT_JAVA_HOME or JAVA_HOME to point to a Java 7 or later installation." return 1 fi log "whichJava = $whichJava" local java_version=$( "$whichJava" -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1) if [ "$java_version" -lt 7 ] ; then log_error "You are using unsupported Java version '$java_version'. The Sesame serializer requires Java 7 or higher." return 2 fi java_exe="${whichJava}" return 0 } function findSerializerJar() { if [ "${RDF_TOOLKIT_JAR}" == "" ] ; then RDF_TOOLKIT_JAR="$PWD/.git/hooks/rdf-toolkit.jar" fi if [ -f "${RDF_TOOLKIT_JAR}" ] ; then log "Found rdf-toolkit: ${RDF_TOOLKIT_JAR}" return 0 fi log_error "Could not find the rdf-toolkit JAR, please reinstall. Set RDF_TOOLKIT_JAR or put rdf-toolkit.jar in the .git/hooks directory." return 1 } # function findBaseDir() { # # # # For now, we just take the top level directory of the current git repository as the base dir # # # base_dir=$(git rev-parse --show-toplevel) # if [ "${base_dir}" == "" ] ; then # log_error "Could not find base directory" # return 1 # fi # if [ ! -d "${base_dir}" ] ; then # log_error "Could not find base directory" # return 1 # fi # return 0 # } function showFilesThatAreInCommit() { git diff --cached --name-only } function serialize() { local file="$1" if [ -f "$file" ] ; then local extension="${file##*.}" local target_format="rdf-xml" if [ -d "$TEMP" ] ; then local logcfg="$TEMP/sesame-serializer-log" else if [ -d "/tmp/" ] ; then local logcfg="/tmp/sesame-serializer-log" fi fi case ${extension} in rdf) target_format="rdf-xml" ;; ttl) target_format="turtle" ;; *) log "Skipping unsupported file $file" return 0 esac if [[ $file =~ .*About.* ]] ; then log "Skipping unsupported file $file" return 0 fi if [[ $file =~ .*OWL.* ]] ; then log "Skipping unsupported file $file" return 0 fi if [ -x "$logcfg" ] ; then cat > ${logcfg} << __log_config__ [logger_root] level: error handlers: h1 [handler_h1] level: error class: ConsoleHandler formatter: f1 [formatter_f1] class: DefaultFormatter format: (%l) %t __log_config__ fi log "Launching the sesame-serializer with --source ${file}" set -x "${java_exe}" -Xmx1g "-Dorg.clapper.avsl.config=${logcfg}" -cp "${RDF_TOOLKIT_JAR}" org.edmcouncil.rdf_toolkit.RdfFormatter \ --source "${file}" \ --target "${file}X" \ --target-format ${target_format} \ --use-dtd-subset \ --inline-blank-nodes \ --infer-base-iri \ --omit-xmlns-namespace rc=$? set +x if [ -x "$logcfg" ] ; then rm -f "${logcfg}" >/dev/null 2>&1 fi # sed '/rdfs:label/!b;:a;/datatype="&xsd;string"/bb;$!{N;ba};:b;s/rdfs:label[^>]*datatype=".....string"/rdfs:label/' "${file}" > temp # rm "${file}" # mv temp "${file}" if [ ${rc} -eq 0 ] ; then log "Re-adding potentially re-serialized file to git staging area: ${file}" rm "${file}" mv "${file}X" "${file}" git add --update "${file}" else log_error "sesame-serializer ended with error code ${rc}" fi return ${rc} else return 0 fi } function serialize_all() { #echo "Checking the following files:" #showFilesThatAreInCommit for fileToBeCommitted in $(git diff --cached --name-only) ; do if ! serialize "$fileToBeCommitted" ; then return 1 fi done return 0 } function main() { findJava || return 1 findSerializerJar || return 2 # findBaseDir || return 3 serialize_all } main $* rc=$? if [ $rc -gt 0 ] ; then log_error "Could not commit your files" fi exit $rc