2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # stefan.k.berg@ericsson.com
5 # jonas.bjurel@ericsson.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
14 if [ -d "$TMPDIR" ]; then
21 CACHETRANSPORT=${CACHETRANSPORT:-"curl --silent"}
22 CACHEBASE=${CACHEBASE:-"file://${HOME}/cache"}
23 CACHEMAXAGE=${CACHEMAXAGE:-$[14*24*3600]}
24 CACHEDEBUG=${CACHEDEBUG:-1}
27 if [ "$CACHEDEBUG" -eq 1 ]; then
37 # Get a SHA1 based on what's piped into the cache command
39 debugmsg "Generating sha1sum"
40 sha1sum | sed 's/ .*//'
47 debugmsg "SHA1 $1 already in cache, skipping storage"
49 debugmsg "Storing SHA1 $1 in cache"
50 ${CACHETRANSPORT} -T - ${CACHEBASE}/$1.blob
51 echo "Expires: $[`date +"%s"` + $CACHEMAXAGE]" | ${CACHETRANSPORT} -T - ${CACHEBASE}/$1.meta
60 ${CACHETRANSPORT} -o - ${CACHEBASE}/$1.blob 2>/dev/null
63 if [ $rc -eq 0 ]; then
64 echo "Got SHA1 $1 from cache" 2>/dev/null
66 echo "Tried to get SHA1 $1 from cache but failed" 2>/dev/null
76 ${CACHETRANSPORT} ${CACHEBASE}/$1.meta &>/dev/null
79 if [ $rc -eq 0 ]; then
80 debugmsg "Checking for SHA1 $1 in cache and found it, rc = $rc"
82 debugmsg "Checking for SHA1 $1 in cache and failed, rc = $rc"
88 # Verify that SHA1 seems to be a SHA1...
90 if [ $(echo $1 | wc -c) -ne 41 ]; then
97 # Figure out commit ID from URI and tag/branch/commit ID
99 HEADMATCH=`git ls-remote $1 | grep "refs/heads/$2$" | awk '{ print $1 }'`
100 TAGMATCH=`git ls-remote $1 | grep "refs/tags/$2$" | awk '{ print $1 }'`
102 if [ -n "$HEADMATCH" ]; then
104 elif [ -n "$TAGMATCH" ]; then
107 TMPDIR=`mktemp -d /tmp/cacheXXXXX`
109 git clone $1 &>/dev/null || errorexit "Could not clone $1"
110 cd * || errorexit "Could not enter clone of $1"
111 git show $2 &>/dev/null || errorexit "Could not find commit $2"
112 git show $2 | head -1 | awk '{ print $2 }'
118 if [ $# -ne 3 ]; then
119 errorexit "Arg 1 needs to be URI and arg 2 tag/branch/commit"
125 if [ $# -ne 1 ]; then
126 errorexit "No arguments can be given to getid!"
131 if [ $# -ne 2 ]; then
132 errorexit "Only one argument, the SHA1 sum, can be given to getid!"
134 if ! validSHA1 $2; then
135 errorexit "Invalid SHA1 format!"
143 errorexit "I only know about getcommitid, getid, check, get and put!"