|
| 1 | +#!/bin/bash |
| 2 | +# vim: set ft=sh |
| 3 | + |
| 4 | +set -e |
| 5 | + |
| 6 | +exec 3>&1 # make stdout available as fd 3 for the result |
| 7 | +exec 1>&2 # redirect all output to stderr for logging |
| 8 | + |
| 9 | +source /opt/git-resource/common.sh |
| 10 | + |
| 11 | +destination=$1 |
| 12 | + |
| 13 | +if [ -z "$destination" ]; then |
| 14 | + echo "usage: $0 <path/to/destination>" >&2 |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +# for jq |
| 19 | +PATH=/usr/local/bin:$PATH |
| 20 | + |
| 21 | +bin_dir="${0%/*}" |
| 22 | +if [ "${bin_dir#/}" == "$bin_dir" ]; then |
| 23 | + bin_dir="$PWD/$bin_dir" |
| 24 | +fi |
| 25 | + |
| 26 | +payload=$(mktemp $TMPDIR/git-resource-request.XXXXXX) |
| 27 | + |
| 28 | +cat > $payload <&0 |
| 29 | + |
| 30 | +load_pubkey $payload |
| 31 | +configure_https_tunnel $payload |
| 32 | +configure_git_ssl_verification $payload |
| 33 | +configure_credentials $payload |
| 34 | + |
| 35 | +uri=$(jq -r '.source.uri // ""' < $payload) |
| 36 | +git_config_payload=$(jq -r '.source.git_config // []' < $payload) |
| 37 | +ref=$(jq -r '.version.ref // "HEAD"' < $payload) |
| 38 | +depth=$(jq -r '(.params.depth // 0)' < $payload) |
| 39 | +short_ref_format=$(jq -r '(.params.short_ref_format // "%s")' < $payload) |
| 40 | + |
| 41 | +configure_git_global "${git_config_payload}" |
| 42 | + |
| 43 | +if [ -z "$uri" ]; then |
| 44 | + echo "invalid payload (missing uri):" >&2 |
| 45 | + cat $payload >&2 |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +depthflag="" |
| 50 | +if test "$depth" -gt 0 2> /dev/null; then |
| 51 | + depthflag="--depth $depth" |
| 52 | +fi |
| 53 | + |
| 54 | +git init $destination |
| 55 | +cd $destination |
| 56 | + |
| 57 | +git remote add origin $uri |
| 58 | +git fetch origin "$ref" $depthflag |
| 59 | + |
| 60 | +# this will set master to the current commit |
| 61 | +git reset --hard FETCH_HEAD |
| 62 | + |
| 63 | +git log -1 --oneline |
| 64 | +git clean --force --force -d |
| 65 | + |
| 66 | +if [ "$ref" == "HEAD" ]; then |
| 67 | + return_ref=$(git rev-parse HEAD) |
| 68 | +else |
| 69 | + return_ref=$ref |
| 70 | +fi |
| 71 | + |
| 72 | +# Store committer email in .git/committer. Can be used to send email to last committer on failed build |
| 73 | +# Using https://github.com/mdomke/concourse-email-resource for example |
| 74 | +git --no-pager log -1 --pretty=format:"%ae" > .git/committer |
| 75 | + |
| 76 | +# Store git-resource returned version ref .git/ref. Useful to know concourse |
| 77 | +# pulled ref in following tasks and resources. |
| 78 | +echo "${return_ref}" > .git/ref |
| 79 | + |
| 80 | +# Store short ref with templating. Useful to build Docker images with |
| 81 | +# a custom tag |
| 82 | +echo "${return_ref}" | cut -c1-7 | awk "{ printf \"${short_ref_format}\", \$1 }" > .git/short_ref |
| 83 | + |
| 84 | +# Store commit message in .git/commit_message. Can be used to inform about |
| 85 | +# the content of a successfull build. |
| 86 | +# Using https://github.com/cloudfoundry-community/slack-notification-resource |
| 87 | +# for example |
| 88 | +git log -1 --format=format:%B > .git/commit_message |
| 89 | + |
| 90 | +metadata=$(git_metadata) |
| 91 | + |
| 92 | +jq -n "{ |
| 93 | + version: {ref: $(echo $return_ref | jq -R .)}, |
| 94 | + metadata: $metadata |
| 95 | +}" >&3 |
0 commit comments