Skip to content

Commit

Permalink
Rewrite git-clone as shell script
Browse files Browse the repository at this point in the history
We are rewritting git-clone as a 'pure' bourne shell script with no dependence
on the git-init image.

I have rewritten almost word by word the go code from here https://git.io/JvBCr

Error handling are handled by shell's set -e so if any command fails it will
just exit 1,

There is some bugs in go code submodules which is done always so i have handled
this differently to properly be conditional to the parameter.

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel committed Feb 24, 2020
1 parent cdd89a6 commit f87e20e
Showing 1 changed file with 66 additions and 17 deletions.
83 changes: 66 additions & 17 deletions git/git-clone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,71 @@ 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: |
#!/usr/bin/env sh
# 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} --tags --depth=${DEPTH}"
fi
# Translating original go code from https://git.io/JvEtG
# original comment: Fetch can fail if an old commitid was used so try
# git pull, performing regardless of error as no guarantee that the
# same error is returned by all git servers gitlab, github etc...
if git ${fetchArgs};then
git reset --hard ${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)"

0 comments on commit f87e20e

Please sign in to comment.