#!/bin/bash # Copyright (c) 2015, Intel Corporation # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of Intel Corporation nor the names of its contributors # may be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. function usage() { echo "Usage: $0 SHA1_DB sha1 (add version path upstream | add_git git_repo binary_path | read_attr (version | path | upstream | \$attr) | write_attr \$attr \$value)" exit $1 } function exit_error() { echo $@ exit 1 } function db_get_file() { sha1_db=$1 sha1=$2 echo "$sha1_db/db/$sha1" } function db_add() { sha1_db=$1 sha1=$2 version=$3 path=$4 upstream=$5 sha1_db_file=$(db_get_file $sha1_db $sha1) if [ -f $sha1_db_file ] then orig_path=$(db_read $sha1_db $sha1 path) echo "SHA1_DB: The SHA1 $sha1 already exists (orig=$orig_path vs new=$path), exit..." exit 0 fi mkdir -p $(dirname $sha1_db_file) 2> /dev/null echo "SHA1_DB_VERSION=1" > $sha1_db_file echo "version=$version" >> $sha1_db_file echo "path=$path" >> $sha1_db_file echo "upstream=$upstream" >> $sha1_db_file echo "SHA1_DB: '$path' ($sha1) add under version=$version, upstream=$upstream" exit 0 } function db_add_git() { sha1_db=$1 sha1=$2 git_repo=$3 binary=$4 if [ "$sha1" == "-" ] then sha1=$(sha1sum $binary | cut -d ' ' -f 1) fi pwd=$(pwd) cd $git_repo 2> /dev/null || exit_error "Error: cannot open the folder '$git_repo'." git status > /dev/null 2>&1 || exit_error "Error: the folder '$git_repo' is not a git repo." git_sha1=$(git rev-parse --short HEAD) tracked_branch=$(git branch -vv --no-color | grep --color=never "^*" | cut -d '[' -f 2 | cut -d ']' -f 1 | cut -d : -f 1) remote=$(echo "$tracked_branch" | cut -d '/' -f 1) branch=$(echo "$tracked_branch" | cut -d '/' -f 2) # handle local branches by just using the local if [ -n "$(echo "$branch" | grep " ")" ] then # Local branch detected fetch_url="$USER@$(hostname):$(pwd)" branch=$(git rev-parse --abbrev-ref HEAD) else fetch_url=$(git remote show -n origin | grep "Fetch URL:" | cut -d ':' -f 2- | cut -d ' ' -f 2-) fi cd $pwd db_add "$sha1_db" "$sha1" "$git_sha1" "$binary" "$fetch_url/$branch" } function db_read() { sha1_db=$1 sha1=$2 attribute=$3 sha1_db_file=$(db_get_file $sha1_db $sha1) [ ! -f $sha1_db_file ] && exit 1 value=$(grep $attribute $sha1_db_file | cut -d '=' -f 2-) [ -z "$value" ] && exit 1 echo $value exit 0 } function db_write() { sha1_db=$1 sha1=$2 attribute=$3 value=$4 sha1_db_file=$(db_get_file $sha1_db $sha1) [ ! -f $sha1_db_file ] && exit 1 existing_value=$(grep $attribute $sha1_db_file | cut -d '=' -f 2-) if [ -z "$value" ] then # add the value echo "$attribute=$value" > $sha1_db_file else #replace the value sed -i "s~$attribute=$existing_value~$attribute=$value~g" $sha1_db_file fi exit 0 } if [[ $# -lt 4 ]] then usage 1 fi sha1_db=$1 sha1=$2 action=$3 case "$action" in add) if [[ $# -ne 6 ]] then usage 1 fi version=$4 path=$5 upstream=$6 db_add "$sha1_db" "$sha1" "$version" "$path" "$upstream" ;; add_git) if [[ $# -ne 5 ]] then usage 1 fi git_repo=$4 binary=$5 db_add_git "$sha1_db" "$sha1" "$git_repo" "$binary" ;; read_attr) attribute=$4 db_read "$sha1_db" "$sha1" "$attribute" ;; write_attr) if [[ $# -ne 5 ]] then usage 1 fi attribute=$4 value=$5 db_write "$sha1_db" "$sha1" "$attribute" "$value" ;; *) echo "SHA1_DB: invalid action '$action'" ;; esac