[New] install script: Allow installation from other repository also for git method
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by nmarghetti · committed by nmarghetti · parent 589c237 · Browse files · 2 files·+152 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+152 added−11 removed
- tests:+121 added−0 removed
- other:+31 added−11 removed
Provenance
- Commit
502089a- Tree
337e66a- Author
nmarghetti <marghettinicolas@hotmail.com>- Committer
nmarghetti <marghettinicolas@hotmail.com>- Parent
589c237
2 files changed+152 added−11 removed
added:test/install_script/install_nvm_from_git+121 added
1+#!/bin/sh2+3+SAVE_NVM_DIR="$NVM_DIR"4+SAVE_NVM_INSTALL_VERSION="$NVM_INSTALL_VERSION"5+6+cleanup () {7+ NVM_DIR="$SAVE_NVM_DIR"8+ NVM_INSTALL_VERSION="$SAVE_NVM_INSTALL_VERSION"9+ unset SAVE_NVM_DIR SAVE_NVM_INSTALL_VERSION10+ unset -f die cleanup test_step get_head_ref get_head_changeset get_fetch_head_changeset \11+ step_to_previous_changeset test_install_data test_install12+}13+14+die () { echo -e "$@" ; cleanup ; exit 1; }15+16+NVM_ENV=testing \. ../../install.sh17+18+set -ex19+20+# nvm_do_install is available21+type install_nvm_from_git > /dev/null 2>&1 || die 'install_nvm_from_git is not available'22+23+test_step() {24+ echo -e "\e[33m$*\e[0m"25+}26+27+get_head_ref(){28+ git --git-dir "$NVM_DIR"/.git --no-pager log --pretty=format:'%d' -129+}30+31+get_head_changeset() {32+ git --git-dir "$NVM_DIR"/.git rev-parse --verify HEAD33+}34+35+get_fetch_head_changeset() {36+ git --git-dir "$NVM_DIR"/.git rev-parse --verify FETCH_HEAD37+}38+39+step_to_previous_changeset() {40+ git --git-dir "$NVM_DIR"/.git fetch origin +"$(get_head_changeset)" --depth=241+ git --git-dir "$NVM_DIR"/.git reset --hard HEAD~142+}43+44+# args:45+# - 1: current ref46+# - 2: current changeset47+# - 3: error message48+# - 4: ref to check ("" if none)49+# - 5: changeset to check ("" if none)50+# - 6: ref to avoid ("" if none)51+test_install_data() {52+ local current_ref="$1"53+ local current_changeset="$2"54+ local message="$3"55+ local ref="$4"56+ local changeset="$5"57+ local avoid_ref="$6"58+ if [ -n "$ref" ]; then59+ echo "$current_ref" | grep -q "$ref" || die "install_nvm_from_git $message did not clone with ref $ref"60+ fi61+ if [ -n "$avoid_ref" ]; then62+ echo "$current_ref" | grep -q "$avoid_ref" && die "install_nvm_from_git $message did clone with unwanted ref $avoid_ref"63+ fi64+ if [ -n "$changeset" ]; then65+ echo "$current_changeset" | grep -q "$changeset" || die "install_nvm_from_git $message did not clone with changeset $changeset"66+ fi67+}68+69+# args:70+# - 1: version to install ("" for latest, tag, ref or changeset)71+# - 2: error message72+# - 3: ref to check ("" if none)73+# - 4: changeset to check ("" if none)74+# - 5: ref to avoid ("" if none)75+test_install() {76+ NVM_INSTALL_VERSION="$1"77+ local message="$2"78+ local ref="$3"79+ local changeset="$4"80+ local avoid_ref="$5"81+82+ NVM_DIR=$(mktemp -d) || die 'Unable to create temporary directory'83+ NVM_DIR="$NVM_DIR/clone"84+85+ # Ensure it clones the repository for non existing directory86+ test_step "Clones repo $message"87+ install_nvm_from_git88+ test_install_data "$(get_head_ref)" "$(get_head_changeset)" "$message" "$ref" "$changeset" "$avoid_ref"89+ rm -rf "$NVM_DIR"90+91+ # Ensure it initializes the repository for an empty existing repository92+ mkdir -p "$NVM_DIR" || die 'Unable to create directory'93+ test_step "Initialize repo $message"94+ install_nvm_from_git95+ test_install_data "$(get_head_ref)" "$(get_head_changeset)" "$message" "$ref" "$changeset" "$avoid_ref"96+ rm -rf "$NVM_DIR"97+98+ # Ensure it updates the repository for an existing git repository99+ git clone "$(nvm_source "git")" -b "v0.36.0" --depth=2 "$NVM_DIR" || die 'Unable to clone repository'100+ step_to_previous_changeset101+ test_step "Updates repo $message"102+ install_nvm_from_git103+ test_install_data "$(get_head_ref)" "$(get_head_changeset)" "$message" "$ref" "$changeset" "$avoid_ref"104+ rm -rf "$NVM_DIR"105+}106+107+# Handle latest version108+test_install "" "latest version" "$(nvm_latest_version)"109+110+# Handle given changeset111+changeset="3abb98124e8d30c9652976c9d34a7380036083b5"112+test_install "$changeset" "with changeset $changeset" "" "$changeset" "$(nvm_latest_version)"113+114+# Handle given tag115+test_install "v0.37.0" "version v0.37.0" "v0.37.0" "4054bd70cedb9998015c2d8cc468c818c7d2f57d" "$(nvm_latest_version)"116+117+# Handle given ref118+test_install "refs/pull/2397/head" "with refs/pull/2397/head" "" "9849bf494d50e74aa810451fb1f83208b0092dd6" "$(nvm_latest_version)"119+test_install "master" "master" "" "" "$(nvm_latest_version)"120+121+cleanupmodified:install.sh+31 added-11 removed
98 unmodified lines99 100 install_nvm_from_git() {101 local INSTALL_DIR102 INSTALL_DIR="$(nvm_install_dir)"103+ local NVM_VERSION104+ NVM_VERSION="${NVM_INSTALL_VERSION:-$(nvm_latest_version)}"105+ if [ -n "${NVM_INSTALL_VERSION:-}" ]; then106+ # Check if version is an existing ref107+ if command git ls-remote "$(nvm_source "git")" "$NVM_VERSION" | nvm_grep -q "$NVM_VERSION" ; then108+ :109+ # Check if version is an existing changeset110+ elif ! nvm_download -o /dev/null "$(nvm_source "script-nvm-exec")"; then111+ echo >&2 "Failed to find '$NVM_VERSION' version."112+ exit 1113+ fi114+ fi115 116+ local fetch_error117 if [ -d "$INSTALL_DIR/.git" ]; then118+ # Updating repo119 echo "=> nvm is already installed in $INSTALL_DIR, trying to update using git"120 command printf '\r=> '107- command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin tag "$(nvm_latest_version)" --depth=1 2> /dev/null || {108- echo >&2 "Failed to update nvm, run 'git fetch' in $INSTALL_DIR yourself."109- exit 1110- }121+ fetch_error="Failed to update nvm with $NVM_VERSION, run 'git fetch' in $INSTALL_DIR yourself."122 else112- # Cloning to $INSTALL_DIR123+ fetch_error="Failed to fetch origin with $NVM_VERSION. Please report this!"124 echo "=> Downloading nvm from git to '$INSTALL_DIR'"125 command printf '\r=> '126 mkdir -p "${INSTALL_DIR}"127 if [ "$(ls -A "${INSTALL_DIR}")" ]; then128+ # Initializing repo129 command git init "${INSTALL_DIR}" || {130 echo >&2 'Failed to initialize nvm repo. Please report this!'131 exit 2132 }1 unmodified line134 || command git --git-dir="${INSTALL_DIR}/.git" remote set-url origin "$(nvm_source)" || {135 echo >&2 'Failed to add remote "origin" (or set the URL). Please report this!'136 exit 2137 }126- command git --git-dir="${INSTALL_DIR}/.git" fetch origin tag "$(nvm_latest_version)" --depth=1 || {127- echo >&2 'Failed to fetch origin with tags. Please report this!'128- exit 2129- }138 else131- command git -c advice.detachedHead=false clone "$(nvm_source)" -b "$(nvm_latest_version)" --depth=1 "${INSTALL_DIR}" || {139+ # Cloning repo140+ command git clone "$(nvm_source)" --depth=1 "${INSTALL_DIR}" || {141 echo >&2 'Failed to clone nvm repo. Please report this!'142 exit 2143 }144 fi145 fi146+ # Try to fetch tag147+ if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin tag "$NVM_VERSION" --depth=1 2>/dev/null; then148+ :149+ # Fetch given version150+ elif ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin "$NVM_VERSION" --depth=1; then151+ echo >&2 "$fetch_error"152+ exit 1153+ fi137- command git -c advice.detachedHead=false --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" checkout -f --quiet "$(nvm_latest_version)"154+ command git -c advice.detachedHead=false --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" checkout -f --quiet FETCH_HEAD || {155+ echo >&2 "Failed to checkout the given version $NVM_VERSION. Please report this!"156+ exit 2157+ }158 if [ -n "$(command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" show-ref refs/heads/master)" ]; then159 if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet 2>/dev/null; then160 command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet -D master >/dev/null 2>&1161 else296 unmodified lines