1#!/bin/bash -eux 2# Copyright 2016 Google Inc. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16################################################################################ 17 18# Deterimine srcmap of checked out source code 19 20SRCMAP=$(tempfile) 21echo "{}" > $SRCMAP 22 23# $1 - json file, $2 - jq program 24function jq_inplace() { 25 F=$(tempfile) && cat $1 | jq "$2" > $F && mv $F $1 26} 27 28PATHS_TO_SCAN="$SRC" 29 30if [[ $FUZZING_LANGUAGE == "go" ]]; then 31 PATHS_TO_SCAN="$PATHS_TO_SCAN $GOPATH" 32fi 33 34# Git 35for DOT_GIT_DIR in $(find $PATHS_TO_SCAN -name ".git" -type d); do 36 GIT_DIR=$(dirname $DOT_GIT_DIR) 37 cd $GIT_DIR 38 GIT_URL=$(git config --get remote.origin.url) 39 GIT_REV=$(git rev-parse HEAD) 40 jq_inplace $SRCMAP ".\"$GIT_DIR\" = { type: \"git\", url: \"$GIT_URL\", rev: \"$GIT_REV\" }" 41done 42 43# Subversion 44for DOT_SVN_DIR in $(find $PATHS_TO_SCAN -name ".svn" -type d); do 45 SVN_DIR=$(dirname $DOT_SVN_DIR) 46 cd $SVN_DIR 47 SVN_URL=$(svn info | grep "^URL:" | sed 's/URL: //g') 48 SVN_REV=$(svn info -r HEAD | grep "^Revision:" | sed 's/Revision: //g') 49 jq_inplace $SRCMAP ".\"$SVN_DIR\" = { type: \"svn\", url: \"$SVN_URL\", rev: \"$SVN_REV\" }" 50done 51 52# Mercurial 53for DOT_HG_DIR in $(find $PATHS_TO_SCAN -name ".hg" -type d); do 54 HG_DIR=$(dirname $DOT_HG_DIR) 55 cd $HG_DIR 56 HG_URL=$(hg paths default) 57 HG_REV=$(hg --debug id -r. -i) 58 jq_inplace $SRCMAP ".\"$HG_DIR\" = { type: \"hg\", url: \"$HG_URL\", rev: \"$HG_REV\" }" 59done 60 61if [ "${OSSFUZZ_REVISION-}" != "" ]; then 62 jq_inplace $SRCMAP ".\"/src\" = { type: \"git\", url: \"https://github.com/google/oss-fuzz.git\", rev: \"$OSSFUZZ_REVISION\" }" 63fi 64 65cat $SRCMAP 66rm $SRCMAP 67