summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jenkins-helpers.sh33
1 files changed, 33 insertions, 0 deletions
diff --git a/jenkins-helpers.sh b/jenkins-helpers.sh
index 58c120ed..363afa33 100644
--- a/jenkins-helpers.sh
+++ b/jenkins-helpers.sh
@@ -123,3 +123,36 @@ remote_exec ()
for i in "$@"; do cmd+=($(printf '%q' "$i")); done
ssh ${port:+-p$port} $host "${dir:+cd "$(printf '%q' "$dir")" &&} exec ${cmd[@]}"
}
+
+# Clone or update a git repo
+# $1 -- repo directory
+# $2 -- branch, tag or refspec
+# $3 -- master git repo
+clone_or_update_repo ()
+{
+ local dir="$1"
+ local ref="$2"
+ local url="$3"
+
+ if ! [ -d "$dir/.git" ]; then
+ rm -rf "$dir"
+ git clone "$url" "$dir"
+ fi
+
+ (
+ cd "$dir"
+
+ # Convert git branch/tag names into SHA1
+ local sha1
+ sha1=$(git ls-remote "$url" "$ref" | cut -f 1)
+
+ # Update from URL.
+ git remote set-url origin "$url"
+ git remote update -p
+
+ # Checkout
+ git reset --hard
+ git clean -dfx
+ git checkout --detach "$sha1"
+ )
+}