diff --git a/git/git-clone.yaml b/git/git-clone.yaml index 9873090b15..1dbdbd2a4b 100644 --- a/git/git-clone.yaml +++ b/git/git-clone.yaml @@ -36,22 +36,66 @@ spec: description: The precise commit SHA that was fetched by this Task steps: - name: clone - image: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init:latest + image: alpine/git:latest script: | + # Most errors handling are handled by the set -eux + set -eux CHECKOUT_DIR="$(workspaces.output.path)/$(inputs.params.subdirectory)" - /ko-app/git-init \ - -url "$(inputs.params.url)" \ - -revision "$(inputs.params.revision)" \ - -path "$CHECKOUT_DIR" \ - -sslVerify "$(inputs.params.sslVerify)" \ - -submodules "$(inputs.params.submodules)" \ - -depth "$(inputs.params.depth)" - cd $CHECKOUT_DIR - RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')" - EXIT_CODE="$?" - if [ "$EXIT_CODE" != 0 ] - then - exit $EXIT_CODE - fi - # Make sure we don't add a trailing newline to the result! - echo -n "$RESULT_SHA" > $(results.commit.path) + URL="$(inputs.params.url)" + REVISION="$(inputs.params.revision)" + SUBMODULES="$(inputs.params.submodules)" + SSLVERIFY="$(inputs.params.sslVerify)" + DEPTH="$(inputs.params.depth)" + + # https://git.io/JvBCV + ensureHomeEnv() { + local realhomedir=$(getent passwd $(whoami)|cut -f6 -d:) + if [[ ! -d ${realhomedir} ]];then + mkdir -p ${realhomedir} + fi + + if [[ ${realhomedir} == ${HOME} ]];then + return + fi + + if [[ ! -e ${realhomedir}/.ssh ]];then + ln -svf ${HOME}/.ssh ${realhomedir}/.ssh + fi + } + + # https://git.io/JvBCr + fetch() { + [[ ${REVISION} == "" ]] && REVISION=master + if [[ ${CHECKOUT_DIR} != "" ]];then + git init ${CHECKOUT_DIR} + cd ${CHECKOUT_DIR} + else + git init . + fi + trimmedURL=$(echo ${URL}|sed -e 's/^[ ]*//' -e 's/[ ]*$//') + git remote add origin ${trimmedURL} + + git config --global http.sslVerify ${SSLVERIFY} + + fetchArgs="fetch" + if [[ ${DEPTH} > 0 ]];then + fetchArgs="${fetchArgs} --depth=${DEPTH}" + fi + if git ${fetchArgs};then + git pull origin master + git checkout ${REVISION} + else + git reset --hard FETCH_HEAD + fi + + if [[ ${SUBMODULES} == "true" || ${SUBMODULES} == "yes" ]];then + git submodule init + git submodule update --recursive + fi + + echo "Successfully cloned ${trimmedURL} @ ${REVISION} in path ${PATH}" + } + + ensureHomeEnv + fetch + echo -n "$(git rev-parse HEAD | tr -d '\n')" > $(results.commit.path)